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 () {
|
|
|
|
var res = this._super.apply(this, arguments);
|
|
|
|
var self = this;
|
|
|
|
var instanceReady = false;
|
|
|
|
this.rte.on('instanceReady', this, function () {
|
|
|
|
clearTimeout(instanceReady);
|
2013-09-09 13:28:00 +00:00
|
|
|
instanceReady = setTimeout(function () {
|
|
|
|
self.activate_building_block_manipulators();
|
|
|
|
self.activate_snippet_click_editor();
|
|
|
|
}, 0);
|
|
|
|
|
2013-09-06 15:04:08 +00:00
|
|
|
});
|
|
|
|
return res;
|
|
|
|
},
|
2013-09-09 13:28:00 +00:00
|
|
|
|
|
|
|
activate_building_block_manipulators: function () {
|
|
|
|
window.snippets = this.snippets = new website.snippet.BuildingBlock(this);
|
|
|
|
this.snippets.appendTo($(document.body));
|
|
|
|
this.snippets.activate_snippet_manipulators();
|
|
|
|
},
|
2013-08-29 10:29:56 +00:00
|
|
|
snippet: function (ev) {
|
|
|
|
this.snippets.toggle();
|
|
|
|
},
|
2013-09-09 13:28:00 +00:00
|
|
|
activate_snippet_click_editor: function () {
|
2013-08-29 10:29:56 +00:00
|
|
|
var self = this;
|
2013-09-06 15:04:08 +00:00
|
|
|
var $snipped_id = false;
|
|
|
|
var snipped_event_flag = false;
|
2013-09-09 13:28:00 +00:00
|
|
|
$("[data-snippet-id]").on('click', function (event) {
|
2013-09-06 15:04:08 +00:00
|
|
|
if (snipped_event_flag) {
|
|
|
|
return;
|
2013-09-04 14:01:38 +00:00
|
|
|
}
|
2013-09-06 15:04:08 +00:00
|
|
|
|
|
|
|
snipped_event_flag = true;
|
|
|
|
setTimeout(function () {snipped_event_flag = false;}, 0);
|
|
|
|
|
|
|
|
if ($snipped_id && $snipped_id.get(0) == event.currentTarget) {
|
|
|
|
return;
|
2013-09-04 14:01:38 +00:00
|
|
|
}
|
2013-09-06 15:04:08 +00:00
|
|
|
|
2013-09-09 13:28:00 +00:00
|
|
|
self.snippet_blur($snipped_id);
|
2013-09-06 15:04:08 +00:00
|
|
|
|
|
|
|
$snipped_id = $(event.currentTarget);
|
|
|
|
|
2013-09-06 16:00:06 +00:00
|
|
|
if (typeof $snipped_id.data("snippet-editor") === 'undefined') {
|
2013-09-09 13:28:00 +00:00
|
|
|
$snipped_id.data("snippet-editor", new (website.snippet.editorRegistry[$snipped_id.data("snippet-id")] || website.snippet.editorRegistry.box)(self, $snipped_id));
|
2013-09-06 15:04:08 +00:00
|
|
|
}
|
2013-09-09 13:28:00 +00:00
|
|
|
self.snippet_focus($snipped_id);
|
2013-09-04 14:01:38 +00:00
|
|
|
});
|
2013-09-06 15:04:08 +00:00
|
|
|
$("body > :not(:has(#website-top-view))").on('click', function (ev) {
|
|
|
|
if (!snipped_event_flag && $snipped_id) {
|
2013-09-09 13:28:00 +00:00
|
|
|
self.snippet_blur($snipped_id);
|
2013-09-06 15:04:08 +00:00
|
|
|
$snipped_id = false;
|
2013-09-04 14:01:38 +00:00
|
|
|
}
|
|
|
|
});
|
2013-09-06 15:04:08 +00:00
|
|
|
},
|
2013-09-09 13:28:00 +00:00
|
|
|
snippet_blur: function ($snipped_id) {
|
2013-09-06 15:04:08 +00:00
|
|
|
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-09-09 13:28:00 +00:00
|
|
|
snippet_focus: function ($snipped_id) {
|
2013-09-06 15:04:08 +00:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
}
|
2013-08-29 10:29:56 +00:00
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
/* ----- 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-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-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-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 () {
|
|
|
|
|
|
|
|
var $selected_target = null;
|
|
|
|
$("body").mouseover(function (event){
|
|
|
|
var $target = $(event.srcElement).parents("[data-snippet-id]:first");
|
|
|
|
if($target.length && $selected_target != $target){
|
|
|
|
if($selected_target){
|
|
|
|
$selected_target.data('manipulator').removeClass('oe_selected');
|
|
|
|
}
|
|
|
|
$selected_target = $target;
|
|
|
|
$target.data('manipulator').addClass('oe_selected');
|
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
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
|
|
|
|
|
|
|
self.deactivate_snippet_manipulators();
|
|
|
|
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
|
|
|
});
|
|
|
|
}else if( action === 'mutate' ){
|
2013-09-06 15:04:08 +00:00
|
|
|
self.activate_overlay_zones($snippet.data('selector'));
|
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(){
|
|
|
|
if( action === 'insert' ){
|
2013-09-06 15:04:08 +00:00
|
|
|
var $toInsert = $snippet.find('.oe_snippet_body').clone();
|
|
|
|
$toInsert.removeClass('oe_snippet_body');
|
|
|
|
$toInsert.addClass('oe_snippet_instance');
|
|
|
|
$toInsert.data('snippet-id',$snippet.data('snippet-id'));
|
|
|
|
$(".oe_drop_zone.oe_hover").replaceWith($toInsert);
|
2013-08-29 10:29:56 +00:00
|
|
|
}else if( action === 'mutate' ){
|
2013-09-06 15:04:08 +00:00
|
|
|
self.path_eval($snippet.data('action-function'))( $(".oe_drop_zone.oe_hover").data('target') );
|
2013-08-29 10:29:56 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
});
|
|
|
|
},
|
|
|
|
stop: function(){
|
|
|
|
self.deactivate_zones();
|
|
|
|
self.activate_snippet_manipulators();
|
|
|
|
},
|
|
|
|
});
|
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.deactivate_snippet_manipulators();
|
|
|
|
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;
|
|
|
|
var child_selector = selector.childs || '';
|
|
|
|
var sibling_selector = selector.siblings || '';
|
|
|
|
var zone_template = "<div class='oe_drop_zone oe_insert'></div>";
|
|
|
|
|
2013-09-06 15:04:08 +00:00
|
|
|
$('.oe_drop_zone').remove();
|
2013-08-29 10:29:56 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
$('.oe_snippets .oe_drop_zone').remove(); // no zone in the snippet selector ...
|
2013-09-06 15:04:08 +00:00
|
|
|
$('#website-top-view .oe_drop_zone').remove(); // no zone in the top bars ...
|
|
|
|
$('#website-top-edit .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
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
deactivate_zones: function(){
|
|
|
|
$('.oe_drop_zone').remove();
|
|
|
|
},
|
|
|
|
|
2013-09-06 15:04:08 +00:00
|
|
|
// generate drop zones covering the elements selected by the selector
|
2013-08-29 10:29:56 +00:00
|
|
|
activate_overlay_zones: function(selector){
|
|
|
|
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-08-29 10:29:56 +00:00
|
|
|
var zone_template = "<div class='oe_drop_zone oe_overlay'></div>";
|
|
|
|
$('.oe_drop_zone').remove();
|
|
|
|
|
|
|
|
for(var i = 0, len = $targets.length; i < len; i++){
|
|
|
|
var $target = $targets.eq(i);
|
|
|
|
var $zone = $(zone_template);
|
2013-09-09 13:28:00 +00:00
|
|
|
website.snippet.cover_target($zone,$target);
|
2013-09-06 15:04:08 +00:00
|
|
|
$zone.appendTo('#oe_manipulators');
|
2013-08-29 10:29:56 +00:00
|
|
|
$zone.data('target',$target);
|
|
|
|
}
|
|
|
|
},
|
2013-09-06 15:04:08 +00:00
|
|
|
|
|
|
|
// activates the manipulator boxes (with resizing handles) for all snippets
|
2013-08-29 10:29:56 +00:00
|
|
|
activate_snippet_manipulators: function(){
|
|
|
|
var self = this;
|
2013-09-06 15:04:08 +00:00
|
|
|
// we generate overlay drop zones only to get an idea of where the snippet are, the drop
|
|
|
|
// zones are replaced by manipulators
|
2013-09-09 13:28:00 +00:00
|
|
|
this.activate_overlay_zones('#wrap [data-snippet-id]');
|
2013-08-29 10:29:56 +00:00
|
|
|
|
2013-09-09 13:28:00 +00:00
|
|
|
$('.oe_drop_zone').each(function(){
|
2013-09-06 15:04:08 +00:00
|
|
|
|
|
|
|
var $zone = $(this);
|
|
|
|
var $snippet = $zone.data('target');
|
2013-08-29 10:29:56 +00:00
|
|
|
var $manipulator = $(openerp.qweb.render('website.snippet_manipulator'));
|
2013-09-06 15:04:08 +00:00
|
|
|
|
2013-09-09 13:28:00 +00:00
|
|
|
website.snippet.cover_target($manipulator, $zone);
|
2013-09-06 15:04:08 +00:00
|
|
|
$manipulator.data('target',$snippet);
|
2013-09-09 13:28:00 +00:00
|
|
|
$snippet.data('manipulator',$manipulator);
|
2013-09-06 15:04:08 +00:00
|
|
|
$manipulator.appendTo('#oe_manipulators');
|
|
|
|
$zone.remove();
|
2013-09-09 13:28:00 +00:00
|
|
|
$snippet.off("resize").on("resize", function () {
|
|
|
|
self.deactivate_snippet_manipulators();
|
|
|
|
self.activate_snippet_manipulators();
|
2013-09-06 15:04:08 +00:00
|
|
|
});
|
2013-08-29 10:29:56 +00:00
|
|
|
|
2013-09-06 15:04:08 +00:00
|
|
|
});
|
2013-08-29 10:29:56 +00:00
|
|
|
},
|
|
|
|
deactivate_snippet_manipulators: function(){
|
|
|
|
$('.oe_snippet_manipulator').remove();
|
|
|
|
},
|
|
|
|
toggle: function(){
|
2013-08-30 07:09:06 +00:00
|
|
|
if(this.$el.hasClass('hide')){
|
|
|
|
this.$el.removeClass('hide');
|
2013-09-09 13:28:00 +00:00
|
|
|
//this.activate_snippet_manipulators();
|
2013-08-29 10:29:56 +00:00
|
|
|
}else{
|
2013-08-30 07:09:06 +00:00
|
|
|
this.$el.addClass('hide');
|
2013-09-09 13:28:00 +00:00
|
|
|
//this.deactivate_snippet_manipulators();
|
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
|
|
|
|
* get the options saved in the html view
|
|
|
|
*/
|
|
|
|
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 = {};
|
|
|
|
website.snippet.Editor = openerp.Widget.extend({
|
|
|
|
init: function (parent, dom) {
|
|
|
|
this.$target = $(dom);
|
|
|
|
this.parent = parent;
|
|
|
|
this._super.apply(this, arguments);
|
|
|
|
this.renderElement();
|
|
|
|
this.start();
|
|
|
|
},
|
|
|
|
|
|
|
|
renderElement: function() {
|
|
|
|
var $el;
|
|
|
|
if (this.template) {
|
|
|
|
$el = $(openerp.qweb.render(this.template, {widget: this}).trim());
|
2013-09-09 13:28:00 +00:00
|
|
|
this.$editor = $el.find(".oe_snippet_editorbar");
|
|
|
|
this.$thumbnail = $el.find(".oe_snippet_thumbnail");
|
|
|
|
this.$body = $el.find(".oe_snippet_body");
|
2013-09-06 15:04:08 +00:00
|
|
|
} else {
|
|
|
|
$el = this._make_descriptive();
|
|
|
|
}
|
|
|
|
this.replaceElement($el);
|
|
|
|
},
|
|
|
|
|
|
|
|
/* onFocus
|
|
|
|
* called when the user click inside the snippet dom
|
|
|
|
*/
|
|
|
|
onFocus : function () {
|
2013-09-09 13:28:00 +00:00
|
|
|
if(this.$editor) this.$editor.prependTo(this.parent.$('#website-top-edit .nav.pull-right'));
|
2013-09-06 15:04:08 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/* onFocus
|
|
|
|
* called when the user click outide the snippet dom
|
|
|
|
*/
|
|
|
|
onBlur : function () {
|
2013-09-09 13:28:00 +00:00
|
|
|
if(this.$editor) this.$editor.detach();
|
2013-09-06 15:04:08 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/* setOptions
|
|
|
|
* saved the options in the html view
|
|
|
|
*/
|
|
|
|
setOptions: function (options) {
|
|
|
|
$target.attr("data-snippet-options", JSON.stringify(options));
|
|
|
|
},
|
|
|
|
|
|
|
|
/* getOptions
|
|
|
|
* get the options saved in the html view
|
|
|
|
*/
|
|
|
|
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-09 13:28:00 +00:00
|
|
|
website.snippet.editorRegistry.box = website.snippet.Editor.extend({
|
|
|
|
onFocus : function () {
|
|
|
|
var self = this;
|
2013-09-09 14:49:40 +00:00
|
|
|
var $box = $(openerp.qweb.render("website.snippets.box"));
|
|
|
|
|
|
|
|
$box.find(".oe_snippet_editorbar").prependTo(this.parent.$('#website-top-edit .nav.pull-right'));
|
|
|
|
|
|
|
|
this.$target.data('manipulator').append($box.find(".oe_handles").html()).addClass('oe_active');
|
|
|
|
|
|
|
|
function padding_margin_cover ($manipulator, $snippet) {
|
|
|
|
var height = parseInt($snippet.height() || 0);
|
|
|
|
var width = parseInt($snippet.width() || 0);
|
|
|
|
|
2013-09-09 15:03:19 +00:00
|
|
|
var pcss = {
|
2013-09-09 14:49:40 +00:00
|
|
|
top: parseInt($snippet.css('padding-top')) + 'px',
|
|
|
|
bottom: parseInt($snippet.css('padding-bottom')) + 'px',
|
|
|
|
left: parseInt($snippet.css('padding-left')) + 'px',
|
|
|
|
right: parseInt($snippet.css('padding-right')) + 'px',
|
2013-09-09 15:03:19 +00:00
|
|
|
};
|
|
|
|
var $padding = $manipulator.find(".oe_boxsize").css(pcss);
|
|
|
|
|
|
|
|
var mcss = {
|
2013-09-09 14:49:40 +00:00
|
|
|
top: -parseInt($snippet.css('margin-top')) + 'px',
|
|
|
|
bottom: -parseInt($snippet.css('margin-bottom')) + 'px',
|
|
|
|
left: -parseInt($snippet.css('margin-left')) + 'px',
|
|
|
|
right: -parseInt($snippet.css('margin-right')) + 'px',
|
2013-09-09 15:03:19 +00:00
|
|
|
};
|
|
|
|
var $margin = $manipulator.find(".oe_margin").css(mcss);
|
2013-09-09 14:49:40 +00:00
|
|
|
}
|
|
|
|
padding_margin_cover( this.$target.data('manipulator'), this.$target);
|
2013-09-06 16:00:06 +00:00
|
|
|
|
2013-09-09 13:28:00 +00:00
|
|
|
this.$target.data('manipulator').find(".oe_handle").on('mousedown', function (event){
|
|
|
|
event.preventDefault();
|
2013-09-06 15:04:08 +00:00
|
|
|
|
2013-09-09 13:28:00 +00:00
|
|
|
var $handle = $(this);
|
|
|
|
var $manipulator = self.$target.data('manipulator');
|
|
|
|
var $snippet = self.$target;
|
|
|
|
|
|
|
|
var x = event.pageX;
|
|
|
|
var y = event.pageY;
|
|
|
|
|
|
|
|
var padding=[parseInt($snippet.css('padding-top') || 0),
|
|
|
|
parseInt($snippet.css('padding-bottom') || 0),
|
|
|
|
parseInt($snippet.css('padding-left') || 0),
|
|
|
|
parseInt($snippet.css('padding-right') || 0)];
|
|
|
|
|
|
|
|
$manipulator.addClass('oe_hover');
|
|
|
|
|
|
|
|
var body_mousemove = function (event){
|
|
|
|
event.preventDefault();
|
|
|
|
var dx = event.pageX - x;
|
|
|
|
var dy = event.pageY - y;
|
|
|
|
|
|
|
|
if($handle.hasClass('n') || $handle.hasClass('nw') || $handle.hasClass('ne')){
|
2013-09-09 15:03:19 +00:00
|
|
|
$snippet.css('padding-top', (padding[0]+dy)+'px');
|
2013-09-09 13:28:00 +00:00
|
|
|
}
|
|
|
|
if($handle.hasClass('s') || $handle.hasClass('sw') || $handle.hasClass('se')){
|
2013-09-09 15:03:19 +00:00
|
|
|
$snippet.css('padding-bottom',(padding[1]-dy)+'px');
|
2013-09-09 13:28:00 +00:00
|
|
|
}
|
|
|
|
if($handle.hasClass('w') || $handle.hasClass('sw') || $handle.hasClass('nw')){
|
|
|
|
$snippet.css('padding-left',(padding[2]+dx)+'px');
|
|
|
|
}
|
|
|
|
if($handle.hasClass('e') || $handle.hasClass('se') || $handle.hasClass('ne')){
|
2013-09-09 15:03:19 +00:00
|
|
|
$snippet.css('padding-right',(padding[3]-dx)+'px');
|
2013-09-09 13:28:00 +00:00
|
|
|
}
|
|
|
|
website.snippet.cover_target($manipulator, $snippet);
|
2013-09-09 14:49:40 +00:00
|
|
|
padding_margin_cover($manipulator, $snippet);
|
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);
|
|
|
|
$snippet.trigger("resize");
|
|
|
|
self.onBlur();
|
|
|
|
self.onFocus();
|
|
|
|
};
|
|
|
|
$('body').mouseup(body_mouseup);
|
|
|
|
});
|
2013-09-09 14:49:40 +00:00
|
|
|
this._super();
|
2013-09-09 13:28:00 +00:00
|
|
|
},
|
|
|
|
onBlur : function () {
|
|
|
|
this._super();
|
|
|
|
this.$target.data('manipulator')
|
|
|
|
.removeClass('oe_active')
|
|
|
|
.empty();
|
2013-09-09 14:49:40 +00:00
|
|
|
this.parent.$('#website-top-edit .nav.pull-right > .box_resize').remove();
|
|
|
|
},
|
2013-09-09 13:28:00 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
website.snippet.editorRegistry.carousel = website.snippet.editorRegistry.box.extend({
|
|
|
|
template : "website.snippets.carousel",
|
2013-09-06 15:04:08 +00:00
|
|
|
start : function () {
|
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);
|
2013-09-09 13:28:00 +00:00
|
|
|
this.$target.trigger("resize");
|
2013-09-06 16:00:06 +00:00
|
|
|
},
|
|
|
|
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);
|
2013-09-09 13:28:00 +00:00
|
|
|
this.$target.trigger("resize");
|
2013-09-06 16:00:06 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
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-08-29 10:29:56 +00:00
|
|
|
})();
|