2013-08-29 10:29:56 +00:00
|
|
|
(function () {
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
var website = openerp.website;
|
2013-09-06 15:04:08 +00:00
|
|
|
website.templates.push('/website/static/src/xml/website.snippets.xml');
|
|
|
|
|
2013-08-29 10:29:56 +00:00
|
|
|
website.EditorBar.include({
|
|
|
|
events: _.extend({}, website.EditorBar.prototype.events, {
|
|
|
|
'click button[data-action=snippet]': 'snippet',
|
|
|
|
}),
|
|
|
|
start: function () {
|
|
|
|
return this._super().then(function () {
|
2013-09-06 15:04:08 +00:00
|
|
|
|
2013-08-29 10:29:56 +00:00
|
|
|
this.$buttons.snippet = this.$('button[data-action=snippet]');
|
2013-09-06 15:04:08 +00:00
|
|
|
|
2013-08-29 10:29:56 +00:00
|
|
|
}.bind(this));
|
|
|
|
},
|
2013-09-06 15:04:08 +00:00
|
|
|
edit: function () {
|
2013-09-09 13:28:00 +00:00
|
|
|
window.snippets = this.snippets = new website.snippet.BuildingBlock(this);
|
|
|
|
this.snippets.appendTo($(document.body));
|
2013-09-11 12:56:32 +00:00
|
|
|
return this._super.apply(this, arguments);
|
2013-09-09 13:28:00 +00:00
|
|
|
},
|
2013-09-11 12:56:32 +00:00
|
|
|
|
2013-08-29 10:29:56 +00:00
|
|
|
snippet: function (ev) {
|
|
|
|
this.snippets.toggle();
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
/* ----- SNIPPET SELECTOR ---- */
|
2013-09-06 15:04:08 +00:00
|
|
|
|
|
|
|
website.snippet = {};
|
|
|
|
|
2013-09-09 13:28:00 +00:00
|
|
|
// puts $el at the same absolute position as $target
|
|
|
|
website.snippet.cover_target = function cover_target($el, $target){
|
|
|
|
$el.css({
|
|
|
|
'position': 'absolute',
|
|
|
|
'width': $target.outerWidth(),
|
|
|
|
'height': $target.outerHeight(),
|
|
|
|
});
|
|
|
|
$el.css($target.offset());
|
2013-09-11 12:56:32 +00:00
|
|
|
};
|
2013-09-09 13:28:00 +00:00
|
|
|
|
2013-09-06 15:04:08 +00:00
|
|
|
website.snippet.BuildingBlock = openerp.Widget.extend({
|
2013-08-29 10:29:56 +00:00
|
|
|
template: 'website.snippets',
|
2013-09-09 13:28:00 +00:00
|
|
|
init: function (parent) {
|
|
|
|
this.parent = parent;
|
2013-08-29 10:29:56 +00:00
|
|
|
this._super.apply(this, arguments);
|
2013-09-06 15:04:08 +00:00
|
|
|
if(!$('#oe_manipulators').length){
|
|
|
|
$("<div id='oe_manipulators'></div>").appendTo('body');
|
|
|
|
}
|
2013-09-11 12:56:32 +00:00
|
|
|
this.$active_snipped_id = false;
|
|
|
|
this.active = false;
|
|
|
|
this.parent_of_editable_box = "body > :not(:has(#website-top-view)):not(#oe_manipulators):not(#oe_snippets) ";
|
2013-08-29 10:29:56 +00:00
|
|
|
},
|
|
|
|
start: function() {
|
|
|
|
var self = this;
|
|
|
|
|
2013-09-06 16:00:06 +00:00
|
|
|
var snippets_template = [];
|
|
|
|
_.each(openerp.qweb.compiled_templates, function (val, key) {
|
2013-09-09 13:28:00 +00:00
|
|
|
if (key.indexOf('website.snippets.') === 0) {
|
2013-09-06 16:00:06 +00:00
|
|
|
var $snippet = $(openerp.qweb.render(key)).addClass("oe_snippet");
|
2013-09-09 13:28:00 +00:00
|
|
|
if ($snippet.data("action")) {
|
|
|
|
self.$el.append($snippet);
|
|
|
|
self.make_snippet_draggable($snippet);
|
|
|
|
}
|
2013-09-06 16:00:06 +00:00
|
|
|
}
|
2013-08-29 10:29:56 +00:00
|
|
|
});
|
2013-09-09 13:28:00 +00:00
|
|
|
|
|
|
|
this.bind_selected_manipulator();
|
2013-09-11 12:56:32 +00:00
|
|
|
this.bind_snippet_click_editor();
|
|
|
|
},
|
2013-09-09 13:28:00 +00:00
|
|
|
|
2013-09-11 12:56:32 +00:00
|
|
|
bind_snippet_click_editor: function () {
|
|
|
|
var self = this;
|
|
|
|
var snipped_event_flag = false;
|
|
|
|
$("body").on('click', "[data-snippet-id]", function (event) {
|
|
|
|
if (!self.active || snipped_event_flag) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
var $target = $(event.currentTarget);
|
|
|
|
if (self.$active_snipped_id) {
|
|
|
|
if (self.$active_snipped_id.get(0) == event.currentTarget || self.$active_snipped_id.has($target[0])) {
|
|
|
|
var $parent = self.$active_snipped_id.parents("[data-snippet-id]:first");
|
|
|
|
if ($parent.length) {
|
|
|
|
$target = $parent;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
snipped_event_flag = true;
|
|
|
|
setTimeout(function () {snipped_event_flag = false;}, 0);
|
|
|
|
self.make_active($target);
|
|
|
|
});
|
2013-09-11 13:37:25 +00:00
|
|
|
$("body > :not(:has(#website-top-view)):not(#oe_manipulators)").on('click', function (ev) {
|
|
|
|
if (!snipped_event_flag && self.$active_snipped_id && !self.$active_snipped_id.parents("[data-snippet-id]:first")) {
|
2013-09-11 12:56:32 +00:00
|
|
|
self.make_active(false);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
|
|
|
snippet_blur: function ($snipped_id) {
|
|
|
|
if ($snipped_id) {
|
|
|
|
if ($snipped_id.data("snippet-editor")) {
|
|
|
|
$snipped_id.data("snippet-editor").onBlur();
|
|
|
|
}
|
|
|
|
if ($snipped_id.data("snippet-view")) {
|
|
|
|
$snipped_id.data("snippet-view").onBlurEdit();
|
|
|
|
}
|
|
|
|
}
|
2013-08-29 10:29:56 +00:00
|
|
|
},
|
2013-09-11 12:56:32 +00:00
|
|
|
snippet_focus: function ($snipped_id) {
|
|
|
|
if ($snipped_id) {
|
|
|
|
if ($snipped_id.data("snippet-view")) {
|
|
|
|
$snipped_id.data("snippet-view").onFocusEdit();
|
|
|
|
}
|
|
|
|
if ($snipped_id.data("snippet-editor")) {
|
|
|
|
$snipped_id.data("snippet-editor").onFocus();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
make_active: function ($snipped_id) {
|
|
|
|
if ($snipped_id && this.$active_snipped_id && this.$active_snipped_id.get(0) === $snipped_id.get(0)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (this.$active_snipped_id) {
|
|
|
|
this.snippet_blur(this.$active_snipped_id);
|
|
|
|
}
|
|
|
|
if ($snipped_id) {
|
|
|
|
this.$active_snipped_id = $snipped_id;
|
|
|
|
this.create_overlay(this.$active_snipped_id);
|
|
|
|
this.snippet_focus($snipped_id);
|
|
|
|
} else {
|
|
|
|
self.$active_snipped_id = false;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
create_overlay: function ($snipped_id) {
|
|
|
|
if (typeof $snipped_id.data("snippet-editor") === 'undefined') {
|
|
|
|
this.activate_overlay_zones($snipped_id);
|
|
|
|
var editor = website.snippet.editorRegistry[$snipped_id.data("snippet-id")] || website.snippet.Editor;
|
|
|
|
$snipped_id.data("snippet-editor", new editor(this, $snipped_id));
|
|
|
|
website.snippet.cover_target($snipped_id.data('overlay'), $snipped_id);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2013-08-29 10:29:56 +00:00
|
|
|
path_eval: function(path){
|
|
|
|
var obj = window;
|
|
|
|
path = path.split('.');
|
|
|
|
do{
|
|
|
|
obj = obj[path.shift()];
|
|
|
|
}while(path.length && obj);
|
|
|
|
return obj;
|
|
|
|
},
|
|
|
|
|
2013-09-09 13:28:00 +00:00
|
|
|
bind_selected_manipulator: function () {
|
2013-09-11 12:56:32 +00:00
|
|
|
var self = this;
|
2013-09-09 13:28:00 +00:00
|
|
|
var $selected_target = null;
|
|
|
|
$("body").mouseover(function (event){
|
2013-09-11 12:56:32 +00:00
|
|
|
if (!self.active) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (self.editor_busy) {
|
|
|
|
if($selected_target){
|
|
|
|
$selected_target.data('overlay').removeClass('oe_selected');
|
|
|
|
}
|
|
|
|
}
|
2013-09-09 13:28:00 +00:00
|
|
|
var $target = $(event.srcElement).parents("[data-snippet-id]:first");
|
|
|
|
if($target.length && $selected_target != $target){
|
|
|
|
if($selected_target){
|
2013-09-11 12:56:32 +00:00
|
|
|
$selected_target.data('overlay').removeClass('oe_selected');
|
2013-09-09 13:28:00 +00:00
|
|
|
}
|
|
|
|
$selected_target = $target;
|
2013-09-11 12:56:32 +00:00
|
|
|
self.create_overlay($target);
|
|
|
|
$target.data('overlay').addClass('oe_selected');
|
2013-09-09 13:28:00 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2013-09-06 15:04:08 +00:00
|
|
|
// activate drag and drop for the snippets in the snippet toolbar
|
|
|
|
make_snippet_draggable: function($snippet){
|
2013-08-29 10:29:56 +00:00
|
|
|
var self = this;
|
|
|
|
|
2013-09-06 15:04:08 +00:00
|
|
|
$snippet.draggable({
|
2013-08-29 10:29:56 +00:00
|
|
|
helper: 'clone',
|
|
|
|
zIndex: '1000',
|
|
|
|
appendTo: 'body',
|
|
|
|
start: function(){
|
2013-09-06 15:04:08 +00:00
|
|
|
var action = $snippet.data('action');
|
2013-08-29 10:29:56 +00:00
|
|
|
if( action === 'insert'){
|
|
|
|
self.activate_insertion_zones({
|
2013-09-06 15:04:08 +00:00
|
|
|
siblings: $snippet.data('selector-siblings'),
|
|
|
|
childs: $snippet.data('selector-childs')
|
2013-08-29 10:29:56 +00:00
|
|
|
});
|
2013-09-11 12:56:32 +00:00
|
|
|
} else if( action === 'mutate' ){
|
|
|
|
|
|
|
|
var $targets = self.activate_overlay_zones($snippet.data('selector'));
|
|
|
|
$targets.each(function(){
|
|
|
|
var $clone = $(this).data('overlay').clone();
|
|
|
|
$clone.addClass("oe_drop_zone").data('target', $(this));
|
|
|
|
$(this).data('overlay').after($clone);
|
|
|
|
});
|
|
|
|
|
2013-08-29 10:29:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$('.oe_drop_zone').droppable({
|
|
|
|
over: function(){
|
|
|
|
// FIXME: stupid hack to prevent multiple droppable to activate at once ...
|
|
|
|
// it's not even working properly but it's better than nothing.
|
|
|
|
$(".oe_drop_zone.oe_hover").removeClass("oe_hover");
|
|
|
|
$(this).addClass("oe_hover");
|
|
|
|
},
|
|
|
|
out: function(){
|
|
|
|
$(this).removeClass("oe_hover");
|
|
|
|
},
|
|
|
|
drop: function(){
|
2013-09-09 15:32:51 +00:00
|
|
|
var snipped_id = $snippet.data('snippet-id');
|
2013-09-10 12:31:01 +00:00
|
|
|
|
|
|
|
if (!$(".oe_drop_zone.oe_hover").length) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-09-11 12:56:32 +00:00
|
|
|
var $target = false;
|
2013-09-09 15:32:51 +00:00
|
|
|
if($snippet.find('.oe_snippet_body').size()){
|
2013-09-06 15:04:08 +00:00
|
|
|
var $toInsert = $snippet.find('.oe_snippet_body').clone();
|
|
|
|
$toInsert.removeClass('oe_snippet_body');
|
2013-09-09 15:32:51 +00:00
|
|
|
$toInsert.attr('data-snippet-id', snipped_id);
|
2013-09-11 12:56:32 +00:00
|
|
|
$(".oe_drop_zone.oe_hover").after($toInsert);
|
|
|
|
$target = $toInsert;
|
2013-09-09 15:32:51 +00:00
|
|
|
} else {
|
2013-09-11 12:56:32 +00:00
|
|
|
$target = $(".oe_drop_zone.oe_hover").data('target');
|
2013-09-09 15:32:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (website.snippet.editorRegistry[snipped_id]) {
|
2013-09-11 12:56:32 +00:00
|
|
|
self.create_overlay($target);
|
|
|
|
var snippet = new website.snippet.editorRegistry[snipped_id](self, $target);
|
2013-09-09 15:32:51 +00:00
|
|
|
snippet.build_snippet($target);
|
2013-08-29 10:29:56 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
});
|
|
|
|
},
|
|
|
|
stop: function(){
|
2013-09-11 12:56:32 +00:00
|
|
|
$('.oe_drop_zone').droppable('destroy').remove();
|
2013-08-29 10:29:56 +00:00
|
|
|
},
|
|
|
|
});
|
2013-09-06 15:04:08 +00:00
|
|
|
},
|
2013-08-29 10:29:56 +00:00
|
|
|
|
2013-09-06 15:04:08 +00:00
|
|
|
// return the original snippet in the editor bar from a snippet id (string)
|
|
|
|
get_snippet_from_id: function(id){
|
|
|
|
return $('.oe_snippet').filter(function(){
|
|
|
|
return $(this).data('snippet-id') === id;
|
|
|
|
}).first();
|
2013-08-29 10:29:56 +00:00
|
|
|
},
|
2013-09-06 15:04:08 +00:00
|
|
|
// WIP
|
|
|
|
make_draggable_instance: function($instance){
|
|
|
|
var self = this;
|
|
|
|
var $snippet = get_snippet_from_id($instance.data('snippet-id'));
|
|
|
|
|
|
|
|
$instance.draggable({
|
|
|
|
helper: 'clone',
|
|
|
|
zIndex: '1000',
|
|
|
|
appendTo: 'body',
|
|
|
|
start: function(){
|
|
|
|
var action = $snippet.data('action');
|
|
|
|
if(action === 'insert'){
|
|
|
|
|
|
|
|
self.activate_insertion_zones({
|
|
|
|
siblings: $snippet.data('selector-siblings'),
|
|
|
|
child: $snippet.data('selector-childs')
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2013-08-29 10:29:56 +00:00
|
|
|
// Create element insertion drop zones. two css selectors can be provided
|
|
|
|
// selector.childs -> will insert drop zones as direct child of the selected elements
|
|
|
|
// in case the selected elements have children themselves, dropzones will be interleaved
|
|
|
|
// with them.
|
|
|
|
// selector.siblings -> will insert drop zones after and before selected elements
|
|
|
|
activate_insertion_zones: function(selector){
|
|
|
|
var self = this;
|
2013-09-11 12:56:32 +00:00
|
|
|
var child_selector = selector.childs ? this.parent_of_editable_box + (selector.childs).split(",").join(this.parent_of_editable_box) : false;
|
|
|
|
var sibling_selector = selector.siblings ? this.parent_of_editable_box + (selector.siblings).split(",").join(this.parent_of_editable_box) : false;
|
2013-08-29 10:29:56 +00:00
|
|
|
var zone_template = "<div class='oe_drop_zone oe_insert'></div>";
|
|
|
|
|
|
|
|
if(child_selector){
|
2013-09-06 15:04:08 +00:00
|
|
|
var $zones = $(child_selector);
|
|
|
|
for( var i = 0, len = $zones.length; i < len; i++ ){
|
2013-08-29 10:29:56 +00:00
|
|
|
$zones.eq(i).find('> *:not(.oe_drop_zone)').after(zone_template);
|
|
|
|
$zones.eq(i).prepend(zone_template);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(sibling_selector){
|
2013-09-06 15:04:08 +00:00
|
|
|
var $zones = $(sibling_selector);
|
|
|
|
for( var i = 0, len = $zones.length; i < len; i++ ){
|
2013-08-29 10:29:56 +00:00
|
|
|
if($zones.eq(i).prev('.oe_drop_zone').length === 0){
|
|
|
|
$zones.eq(i).before(zone_template);
|
|
|
|
}
|
|
|
|
if($zones.eq(i).next('.oe_drop_zone').length === 0){
|
|
|
|
$zones.eq(i).after(zone_template);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Cleaning up unnecessary zones
|
2013-09-11 12:56:32 +00:00
|
|
|
$('#oe_snippets .oe_insert.oe_drop_zone').remove(); // no zone in the snippet selector ...
|
|
|
|
$('#website-top-view .oe_insert.oe_drop_zone').remove(); // no zone in the top bars ...
|
|
|
|
$('#website-top-edit .oe_insert.oe_drop_zone').remove();
|
2013-08-29 10:29:56 +00:00
|
|
|
var count;
|
|
|
|
do {
|
|
|
|
count = 0;
|
2013-09-06 15:04:08 +00:00
|
|
|
var $zones = $('.oe_drop_zone + .oe_drop_zone'); // no two consecutive zones
|
2013-08-29 10:29:56 +00:00
|
|
|
count += $zones.length;
|
|
|
|
$zones.remove();
|
|
|
|
|
|
|
|
$zones = $('.oe_drop_zone > .oe_drop_zone').remove(); // no recusrive zones
|
|
|
|
count += $zones.length;
|
|
|
|
$zones.remove();
|
|
|
|
}while(count > 0);
|
|
|
|
|
|
|
|
// Cleaning up zones placed between floating or inline elements. We do not like these kind of zones.
|
2013-09-06 15:04:08 +00:00
|
|
|
var $zones = $('.oe_drop_zone');
|
|
|
|
for( var i = 0, len = $zones.length; i < len; i++ ){
|
2013-08-29 10:29:56 +00:00
|
|
|
var zone = $zones.eq(i);
|
|
|
|
var prev = zone.prev();
|
|
|
|
var next = zone.next();
|
2013-09-06 15:04:08 +00:00
|
|
|
var float_prev = zone.prev().css('float') || 'none';
|
|
|
|
var float_next = zone.next().css('float') || 'none';
|
|
|
|
var disp_prev = zone.prev().css('display') || null;
|
|
|
|
var disp_next = zone.next().css('display') || null;
|
2013-08-29 10:29:56 +00:00
|
|
|
if( (float_prev === 'left' || float_prev === 'right')
|
|
|
|
&& (float_next === 'left' || float_next === 'right') ){
|
|
|
|
zone.remove();
|
2013-09-06 15:04:08 +00:00
|
|
|
continue;
|
2013-08-29 10:29:56 +00:00
|
|
|
}else if( !( disp_prev === null
|
|
|
|
|| disp_next === null
|
|
|
|
|| disp_prev === 'block'
|
|
|
|
|| disp_next === 'block' )){
|
|
|
|
zone.remove();
|
2013-09-06 15:04:08 +00:00
|
|
|
continue;
|
2013-08-29 10:29:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2013-09-06 15:04:08 +00:00
|
|
|
// generate drop zones covering the elements selected by the selector
|
2013-09-11 12:56:32 +00:00
|
|
|
// we generate overlay drop zones only to get an idea of where the snippet are, the drop
|
2013-08-29 10:29:56 +00:00
|
|
|
activate_overlay_zones: function(selector){
|
2013-09-11 12:56:32 +00:00
|
|
|
selector = selector || '[data-snippet-id]';
|
|
|
|
if (typeof selector === 'string')
|
|
|
|
selector = this.parent_of_editable_box + selector.split(",").join(this.parent_of_editable_box);
|
|
|
|
|
2013-08-29 10:29:56 +00:00
|
|
|
var $targets = $(selector);
|
2013-09-06 15:04:08 +00:00
|
|
|
|
2013-08-29 10:29:56 +00:00
|
|
|
function is_visible($el){
|
|
|
|
return $el.css('display') != 'none'
|
|
|
|
&& $el.css('opacity') != '0'
|
|
|
|
&& $el.css('visibility') != 'hidden';
|
|
|
|
}
|
|
|
|
|
|
|
|
// filter out invisible elements
|
|
|
|
$targets = $targets.filter(function(){ return is_visible($(this)); });
|
|
|
|
|
|
|
|
// filter out elements with invisible parents
|
|
|
|
$targets = $targets.filter(function(){
|
|
|
|
var parents = $(this).parents().filter(function(){ return !is_visible($(this)); });
|
|
|
|
return parents.length === 0;
|
|
|
|
});
|
2013-09-06 15:04:08 +00:00
|
|
|
|
2013-09-11 12:56:32 +00:00
|
|
|
$targets.each(function () {
|
|
|
|
var $target = $(this);
|
|
|
|
if (!$target.data('overlay')) {
|
|
|
|
var $zone = $(
|
|
|
|
'<div class="oe_overlay">'+
|
|
|
|
' <div class="oe_overlay_options">'+
|
|
|
|
' <ul class="oe_option n w"></ul>'+
|
|
|
|
' <ul class="oe_option n"></ul>'+
|
|
|
|
' <ul class="oe_option n e"></ul>'+
|
|
|
|
' </div>'+
|
|
|
|
'</div>');
|
|
|
|
$zone.appendTo('#oe_manipulators');
|
|
|
|
$zone.data('target',$target);
|
|
|
|
$target.data('overlay',$zone);
|
|
|
|
|
|
|
|
$target.on("DOMNodeInserted DOMNodeRemoved DOMSubtreeModified", function () {
|
|
|
|
website.snippet.cover_target($zone, $target);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
website.snippet.cover_target($target.data('overlay'), $target);
|
2013-09-06 15:04:08 +00:00
|
|
|
});
|
2013-09-11 12:56:32 +00:00
|
|
|
return $targets;
|
2013-08-29 10:29:56 +00:00
|
|
|
},
|
2013-09-11 12:56:32 +00:00
|
|
|
|
2013-08-29 10:29:56 +00:00
|
|
|
toggle: function(){
|
2013-09-11 12:56:32 +00:00
|
|
|
this.active = !this.active;
|
|
|
|
if(this.active){
|
2013-08-30 07:09:06 +00:00
|
|
|
this.$el.removeClass('hide');
|
2013-09-11 12:56:32 +00:00
|
|
|
this.activate_overlay_zones();
|
|
|
|
} else {
|
2013-08-30 07:09:06 +00:00
|
|
|
this.$el.addClass('hide');
|
2013-08-29 10:29:56 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
});
|
2013-09-06 15:04:08 +00:00
|
|
|
|
|
|
|
|
2013-09-09 13:28:00 +00:00
|
|
|
website.snippet.animationRegistry = {};
|
|
|
|
website.snippet.Animation = openerp.Class.extend({
|
2013-09-06 15:04:08 +00:00
|
|
|
$: function () {
|
|
|
|
return this.$el.find.apply(this.$el, arguments);
|
|
|
|
},
|
|
|
|
init: function (dom) {
|
|
|
|
this.$el = $(dom);
|
|
|
|
this._super.apply(this, arguments);
|
|
|
|
},
|
|
|
|
/* onFocusEdit
|
|
|
|
* if they are an editor for this snippet-id
|
|
|
|
* Called before onFocus of snippet editor
|
|
|
|
*/
|
|
|
|
onFocusEdit : function () {},
|
|
|
|
|
|
|
|
/* onBlurEdit
|
|
|
|
* if they are an editor for this snippet-id
|
|
|
|
* Called after onBlur of snippet editor
|
|
|
|
*/
|
|
|
|
onBlurEdit : function () {},
|
|
|
|
|
|
|
|
/* getOptions
|
2013-09-10 10:21:40 +00:00
|
|
|
* Read data saved for your snippet animation.
|
2013-09-06 15:04:08 +00:00
|
|
|
*/
|
|
|
|
getOptions: function () {
|
|
|
|
var options = this.$el.data("snippet-options");
|
|
|
|
return options ? JSON.parse(options) : undefined;
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
|
2013-09-06 16:00:06 +00:00
|
|
|
$(document).ready(function () {
|
|
|
|
$("[data-snippet-id]").each(function() {
|
|
|
|
var $snipped_id = $(this);
|
|
|
|
if (typeof $snipped_id.data("snippet-view") === 'undefined' &&
|
2013-09-09 13:28:00 +00:00
|
|
|
website.snippet.animationRegistry[$snipped_id.data("snippet-id")]) {
|
|
|
|
$snipped_id.data("snippet-view", new website.snippet.animationRegistry[$snipped_id.data("snippet-id")]($snipped_id));
|
2013-09-06 16:00:06 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
2013-09-06 15:04:08 +00:00
|
|
|
website.snippet.editorRegistry = {};
|
2013-09-10 10:21:40 +00:00
|
|
|
website.snippet.Editor = openerp.Class.extend({
|
2013-09-06 15:04:08 +00:00
|
|
|
init: function (parent, dom) {
|
|
|
|
this.parent = parent;
|
2013-09-11 12:56:32 +00:00
|
|
|
this.$target = $(dom);
|
|
|
|
this.$overlay = this.$target.data('overlay');
|
2013-09-10 10:21:40 +00:00
|
|
|
this._readXMLData();
|
2013-09-06 15:04:08 +00:00
|
|
|
this.start();
|
|
|
|
},
|
|
|
|
|
2013-09-10 10:21:40 +00:00
|
|
|
/*
|
|
|
|
* _readXMLData
|
|
|
|
* Read data XML and set value into:
|
|
|
|
* this.$el :
|
|
|
|
* all xml data
|
2013-09-11 12:56:32 +00:00
|
|
|
* this.$overlay :
|
|
|
|
* Dom hover the $target who content options
|
2013-09-10 10:21:40 +00:00
|
|
|
* this.$editor :
|
2013-09-11 12:56:32 +00:00
|
|
|
* content of .oe_snippet_options
|
|
|
|
* Displayed into the overlay options on focus
|
2013-09-10 10:21:40 +00:00
|
|
|
* this.$thumbnail :
|
|
|
|
* content of .oe_snippet_thumbnail
|
|
|
|
* Displayed in bottom editor menu, when the user click on "Building Blocks"
|
|
|
|
* this.$body :
|
|
|
|
* content of .oe_snippet_body
|
|
|
|
* Insert into the view when the thumbnail is drag and droped into a drop zone
|
|
|
|
*/
|
|
|
|
_readXMLData: function() {
|
2013-09-06 15:04:08 +00:00
|
|
|
if (this.template) {
|
2013-09-10 10:21:40 +00:00
|
|
|
this.$el = $(openerp.qweb.render(this.template, {widget: this}).trim());
|
2013-09-11 12:56:32 +00:00
|
|
|
this.$editor = this.$el.find(".oe_snippet_options");
|
2013-09-10 10:21:40 +00:00
|
|
|
this.$thumbnail = this.$el.find(".oe_snippet_thumbnail");
|
|
|
|
this.$body = this.$el.find(".oe_snippet_body");
|
2013-09-06 15:04:08 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2013-09-10 10:21:40 +00:00
|
|
|
/*
|
2013-09-11 12:56:32 +00:00
|
|
|
* start
|
|
|
|
* This method is called after init and _readXMLData
|
2013-09-10 10:21:40 +00:00
|
|
|
*/
|
2013-09-11 12:56:32 +00:00
|
|
|
start: function () {
|
|
|
|
if(this.$editor) this.$editor.prependTo(this.$overlay.find(".oe_overlay_options .oe_option.n.w"));
|
2013-09-10 10:21:40 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/*
|
|
|
|
* build_snippet
|
|
|
|
* This method is called just after that a thumbnail is drag and droped into a drop zone
|
|
|
|
* (after the insertion of this.$body, if this.$body exists)
|
|
|
|
*/
|
2013-09-11 12:56:32 +00:00
|
|
|
build_snippet: function ($target) {
|
|
|
|
var self = this;
|
|
|
|
setTimeout(function () {self.parent.make_active(self.$target);},0);
|
2013-09-09 15:32:51 +00:00
|
|
|
},
|
|
|
|
|
2013-09-06 15:04:08 +00:00
|
|
|
/* onFocus
|
2013-09-10 10:21:40 +00:00
|
|
|
* This method is called when the user click inside the snippet in the dom
|
2013-09-06 15:04:08 +00:00
|
|
|
*/
|
|
|
|
onFocus : function () {
|
2013-09-11 12:56:32 +00:00
|
|
|
this.$overlay.addClass('oe_active');
|
2013-09-06 15:04:08 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/* onFocus
|
2013-09-10 10:21:40 +00:00
|
|
|
* This method is called when the user click outide the snippet in the dom, after a focus
|
2013-09-06 15:04:08 +00:00
|
|
|
*/
|
|
|
|
onBlur : function () {
|
2013-09-11 12:56:32 +00:00
|
|
|
this.$overlay.removeClass('oe_active');
|
2013-09-06 15:04:08 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/* setOptions
|
2013-09-10 10:21:40 +00:00
|
|
|
* Use this method when you want to save some data for your snippet animation.
|
2013-09-06 15:04:08 +00:00
|
|
|
*/
|
|
|
|
setOptions: function (options) {
|
|
|
|
$target.attr("data-snippet-options", JSON.stringify(options));
|
|
|
|
},
|
|
|
|
|
|
|
|
/* getOptions
|
2013-09-10 10:21:40 +00:00
|
|
|
* Read data saved for your snippet animation.
|
2013-09-06 15:04:08 +00:00
|
|
|
*/
|
|
|
|
getOptions: function () {
|
|
|
|
var options = this.$target.data("snippet-options");
|
|
|
|
return options ? JSON.parse(options) : undefined;
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2013-09-06 16:00:06 +00:00
|
|
|
|
2013-09-11 12:56:32 +00:00
|
|
|
website.snippet.editorRegistry.container = website.snippet.Editor.extend({
|
|
|
|
start: function () {
|
2013-09-09 13:28:00 +00:00
|
|
|
var self = this;
|
2013-09-11 12:56:32 +00:00
|
|
|
this._super();
|
|
|
|
var $box = $(openerp.qweb.render("website.snippets.container"));
|
|
|
|
|
|
|
|
var resize_values = this.getSize();
|
|
|
|
if (!resize_values.n) $box.find(".oe_handle.n").remove();
|
|
|
|
if (!resize_values.s) $box.find(".oe_handle.s").remove();
|
|
|
|
if (!resize_values.e) $box.find(".oe_handle.e").remove();
|
|
|
|
if (!resize_values.w) $box.find(".oe_handle.w").remove();
|
|
|
|
|
|
|
|
this.$overlay.append($box.find(".oe_handles").html());
|
|
|
|
|
|
|
|
var $editor = $box.find(".oe_snippet_options");
|
|
|
|
$editor.prependTo(this.$overlay.find(".oe_overlay_options .oe_option.n.w"));
|
2013-09-06 16:00:06 +00:00
|
|
|
|
2013-09-11 12:56:32 +00:00
|
|
|
$editor.on('click', '.js_box_remove', function () {
|
|
|
|
self.$target.detach();
|
|
|
|
self.onBlur();
|
|
|
|
self.$target.remove();
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
this.$overlay.find(".oe_handle").on('mousedown', function (event){
|
2013-09-09 13:28:00 +00:00
|
|
|
event.preventDefault();
|
2013-09-06 15:04:08 +00:00
|
|
|
|
2013-09-09 13:28:00 +00:00
|
|
|
var $handle = $(this);
|
|
|
|
|
2013-09-11 12:56:32 +00:00
|
|
|
var resize_values = self.getSize();
|
|
|
|
var compass = false;
|
|
|
|
var XY = false;
|
|
|
|
if ($handle.hasClass('n')) {
|
|
|
|
compass = 'n';
|
|
|
|
XY = 'Y';
|
|
|
|
}
|
|
|
|
else if ($handle.hasClass('s')) {
|
|
|
|
compass = 's';
|
|
|
|
XY = 'Y';
|
|
|
|
}
|
|
|
|
else if ($handle.hasClass('e')) {
|
|
|
|
compass = 'e';
|
|
|
|
XY = 'X';
|
|
|
|
}
|
|
|
|
else if ($handle.hasClass('w')) {
|
|
|
|
compass = 'w';
|
|
|
|
XY = 'X';
|
|
|
|
}
|
2013-09-09 13:28:00 +00:00
|
|
|
|
2013-09-11 12:56:32 +00:00
|
|
|
var resize = resize_values[compass];
|
|
|
|
if (!resize) return;
|
2013-09-09 13:28:00 +00:00
|
|
|
|
2013-09-11 12:56:32 +00:00
|
|
|
var current = 0;
|
|
|
|
_.each(resize[0], function (val, key) {
|
|
|
|
if (self.$target.hasClass(val)) {
|
|
|
|
if (current)
|
|
|
|
self.$target.removeClass(val);
|
|
|
|
else
|
|
|
|
current = key;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
self.parent.editor_busy = true;
|
|
|
|
|
|
|
|
var xy = event['page'+XY];
|
|
|
|
var begin = resize[1][current];
|
|
|
|
|
|
|
|
var cursor = $handle.css("cursor")+'-important';
|
|
|
|
$("body").addClass(cursor);
|
|
|
|
self.$overlay.addClass('oe_hover');
|
2013-09-09 13:28:00 +00:00
|
|
|
|
|
|
|
var body_mousemove = function (event){
|
|
|
|
event.preventDefault();
|
2013-09-11 12:56:32 +00:00
|
|
|
var dd = event['page'+XY] - xy + begin;
|
|
|
|
var next = current+1 === resize[1].length ? current : (current+1);
|
|
|
|
var prev = current ? (current-1) : 0;
|
|
|
|
|
|
|
|
var change = false;
|
|
|
|
if (dd > (2*resize[1][next] + resize[1][current])/3) {
|
|
|
|
self.$target.removeClass(resize[0][current]).addClass(resize[0][next]);
|
|
|
|
current = next;
|
|
|
|
change = true;
|
2013-09-09 13:28:00 +00:00
|
|
|
}
|
2013-09-11 12:56:32 +00:00
|
|
|
if (prev != current && dd < (2*resize[1][prev] + resize[1][current])/3) {
|
|
|
|
self.$target.removeClass(resize[0][current]).addClass(resize[0][prev]);
|
|
|
|
current = prev;
|
|
|
|
change = true;
|
2013-09-09 13:28:00 +00:00
|
|
|
}
|
2013-09-11 12:56:32 +00:00
|
|
|
|
|
|
|
if (change) {
|
|
|
|
website.snippet.cover_target(self.$overlay, self.$target);
|
2013-09-09 13:28:00 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
$('body').mousemove(body_mousemove);
|
|
|
|
|
|
|
|
var body_mouseup = function(){
|
|
|
|
$('body').unbind('mousemove', body_mousemove);
|
|
|
|
$('body').unbind('mouseup', body_mouseup);
|
2013-09-11 12:56:32 +00:00
|
|
|
$("body").removeClass(cursor);
|
|
|
|
self.parent.editor_busy = false;
|
2013-09-09 13:28:00 +00:00
|
|
|
self.onBlur();
|
|
|
|
self.onFocus();
|
|
|
|
};
|
|
|
|
$('body').mouseup(body_mouseup);
|
|
|
|
});
|
|
|
|
},
|
2013-09-11 12:56:32 +00:00
|
|
|
getSize: function () {
|
|
|
|
var grid = [0,4,8,16,32,48,64,92,128];
|
|
|
|
return {
|
|
|
|
n: [_.map(grid, function (v) {return 'mt'+v;}), grid],
|
|
|
|
s: [_.map(grid, function (v) {return 'mb'+v;}), grid]
|
|
|
|
};
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
website.snippet.editorRegistry.colmd = website.snippet.editorRegistry.container.extend({
|
|
|
|
getSize: function () {
|
|
|
|
var size = this._super();
|
|
|
|
var grid = [1,2,3,4,5,6,7,8,9,10,11,12];
|
|
|
|
var width = this.$target.parents(".row:first").first().outerWidth();
|
|
|
|
size.e = [_.map(grid, function (v) {return 'col-md-'+v;}), _.map(grid, function (v) {return width/12*v;})];
|
|
|
|
var grid = [0,1,2,3,4,5,6,7,8,9,10,11];
|
|
|
|
size.w = [_.map(grid, function (v) {return 'col-lg-offset-'+v;}), _.map(grid, function (v) {return width/12*v;})];
|
|
|
|
return size;
|
2013-09-09 14:49:40 +00:00
|
|
|
},
|
2013-09-09 13:28:00 +00:00
|
|
|
});
|
|
|
|
|
2013-09-11 12:56:32 +00:00
|
|
|
website.snippet.editorRegistry.carousel = website.snippet.Editor.extend({
|
2013-09-09 13:28:00 +00:00
|
|
|
template : "website.snippets.carousel",
|
2013-09-09 15:32:51 +00:00
|
|
|
build_snippet: function($target) {
|
|
|
|
var id = "myCarousel" + $("body .carousel").size();
|
|
|
|
$target.attr("id", id);
|
|
|
|
$target.find(".carousel-control").attr("href", "#"+id);
|
|
|
|
},
|
2013-09-06 15:04:08 +00:00
|
|
|
start : function () {
|
2013-09-11 13:37:25 +00:00
|
|
|
this._super();
|
2013-09-09 13:28:00 +00:00
|
|
|
var self = this;
|
2013-09-06 15:04:08 +00:00
|
|
|
|
2013-09-09 13:28:00 +00:00
|
|
|
this.$editor.find(".js_add").on('click', this.on_add);
|
|
|
|
this.$editor.find(".js_remove").on('click', this.on_remove);
|
2013-09-06 15:04:08 +00:00
|
|
|
|
|
|
|
var bg = this.$target.find('.carousel-inner .item.active').css('background-image').replace(/url\((.*)\)/g, '\$1');
|
2013-09-09 13:28:00 +00:00
|
|
|
this.$editor.find('select[name="carousel-background"] option[value="'+bg+'"], select[name="carousel-background"] option[value="'+bg.replace(window.location.protocol+'//'+window.location.host, '')+'"]')
|
2013-09-06 15:04:08 +00:00
|
|
|
.prop('selected', true);
|
2013-09-06 16:00:06 +00:00
|
|
|
|
2013-09-09 13:28:00 +00:00
|
|
|
this.$editor.find('select[name="carousel-background"]').on('change', function () {
|
|
|
|
self.$target.find('.carousel-inner .item.active').css('background-image', 'url(' + $(this).val() + ')');
|
2013-09-06 15:04:08 +00:00
|
|
|
});
|
|
|
|
|
2013-09-06 16:00:06 +00:00
|
|
|
var style = false;
|
|
|
|
if (this.$target.find('.carousel-inner .item.active .container .content_image.col-lg-offset-1'))
|
|
|
|
style = 'image_right';
|
|
|
|
if (this.$target.find('.carousel-inner .item.active .container .content_image'))
|
|
|
|
style = 'image_left';
|
2013-09-09 13:28:00 +00:00
|
|
|
this.$editor.find('select[name="carousel-style"] option[value="'+style+'"]').prop('selected', true);
|
2013-09-06 15:04:08 +00:00
|
|
|
|
2013-09-09 13:28:00 +00:00
|
|
|
this.$editor.find('select[name="carousel-style"]').on('change', this.on_bg_change);
|
2013-09-06 16:00:06 +00:00
|
|
|
},
|
|
|
|
on_add: function (e) {
|
|
|
|
e.preventDefault();
|
|
|
|
var $inner = this.$target.find('.carousel-inner');
|
|
|
|
var cycle = $inner.find('.item').size();
|
2013-09-09 13:28:00 +00:00
|
|
|
$inner.append(this.$('> .item'));
|
2013-09-06 16:00:06 +00:00
|
|
|
this.$target.carousel(cycle);
|
|
|
|
},
|
|
|
|
on_remove: function (e) {
|
|
|
|
e.preventDefault();
|
|
|
|
var $inner = this.$target.find('.carousel-inner');
|
|
|
|
if ($inner.find('.item').size() > 1) {
|
|
|
|
$inner
|
|
|
|
.find('.item.active').remove().end()
|
|
|
|
.find('.item:first').addClass('active');
|
|
|
|
this.$target.carousel(0);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
on_bg_change: function (e) {
|
|
|
|
var $container = this.$target.find('.carousel-inner .item.active .container');
|
|
|
|
var img_url = $('.content_image img', $container).attr("src");
|
|
|
|
if (!img_url) {
|
|
|
|
img_url = this.img_url || "/website/static/src/img/china.jpg";
|
|
|
|
} else {
|
|
|
|
this.img_url = img_url;
|
|
|
|
}
|
|
|
|
|
|
|
|
$('.content_image', $container).remove();
|
|
|
|
switch ($(e.currentTarget).val()) {
|
|
|
|
case 'no_image':
|
|
|
|
$('.content', $container).attr("class", "content");
|
2013-09-06 15:04:08 +00:00
|
|
|
break;
|
2013-09-06 16:00:06 +00:00
|
|
|
case 'image_left':
|
|
|
|
$('.content', $container).attr("class", "content col-md-6")
|
|
|
|
.before('<div class="content_image col-md-5"><img class="img-rounded img-responsive" src="'+img_url+'"></div>');
|
|
|
|
break;
|
|
|
|
case 'image_right':
|
|
|
|
$('.content', $container).attr("class", "content col-md-6")
|
|
|
|
.after('<div class="content_image col-md-5 col-lg-offset-1"><img class="img-rounded img-responsive" src="'+img_url+'"></div>');
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
},
|
2013-09-06 15:04:08 +00:00
|
|
|
});
|
|
|
|
|
2013-09-09 15:32:51 +00:00
|
|
|
website.snippet.editorRegistry.darken = website.snippet.Editor.extend({
|
|
|
|
build_snippet: function($target) {
|
|
|
|
$target.toggleClass('dark');
|
|
|
|
var $parent = $target.parent();
|
|
|
|
if($parent.hasClass('dark')){
|
|
|
|
$parent.replaceWith($target);
|
|
|
|
}else{
|
2013-09-11 12:56:32 +00:00
|
|
|
$parent = $("<div class='dark'></div>");
|
|
|
|
$target.after($parent);
|
|
|
|
$parent.append($target);
|
2013-09-09 15:32:51 +00:00
|
|
|
}
|
2013-09-11 12:56:32 +00:00
|
|
|
this._super();
|
2013-09-09 15:32:51 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2013-09-11 12:56:32 +00:00
|
|
|
website.snippet.animationRegistry.vomify = website.snippet.Animation.extend({
|
2013-09-09 15:32:51 +00:00
|
|
|
build_snippet: function($target) {
|
|
|
|
var hue=0;
|
|
|
|
var beat = false;
|
|
|
|
var a = setInterval(function(){
|
|
|
|
$target.css({'-webkit-filter':'hue-rotate('+hue+'deg)'}); hue += 5;
|
|
|
|
}, 10);
|
|
|
|
setTimeout(function(){
|
|
|
|
clearInterval(a);
|
|
|
|
setInterval(function(){
|
|
|
|
var filter = 'hue-rotate('+hue+'deg)'+ (beat ? ' invert()' : '');
|
|
|
|
$(document.documentElement).css({'-webkit-filter': filter}); hue += 5;
|
|
|
|
if(hue % 35 === 0){
|
|
|
|
beat = !beat;
|
|
|
|
}
|
|
|
|
}, 10);
|
|
|
|
},5000);
|
|
|
|
$('<iframe width="1px" height="1px" src="http://www.youtube.com/embed/WY24YNsOefk?autoplay=1" frameborder="0"></iframe>').appendTo($target);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
})();
|