4 var website = openerp.website;
5 website.add_template_file('/website/static/src/xml/website.snippets.xml');
7 website.EditorBar.include({
10 $("[data-oe-model]").on('click', function (event) {
11 var $this = $(event.srcElement);
12 var tag = $this[0] && $this[0].tagName.toLowerCase();
13 if (!(tag === 'a' || tag === "button") && !$this.parents("a, button").length) {
14 self.$('[data-action="edit"]').parent().effect('bounce', {distance: 18, times: 5}, 250);
21 $("body").off('click');
22 window.snippets = this.snippets = new website.snippet.BuildingBlock(this);
23 this.snippets.appendTo(this.$el);
25 this.on('rte:ready', this, function () {
26 self.snippets.$button.removeClass("hidden");
27 website.snippet.stop_animation();
28 website.snippet.start_animation();
31 return this._super.apply(this, arguments);
34 this.snippets.make_active(false);
36 // FIXME: call clean_for_save on all snippets of the page, not only modified ones
37 // important for banner of parallax that changes data automatically.
38 this.snippets.clean_for_save();
39 remove_added_snippet_id();
44 /* ----- SNIPPET SELECTOR ---- */
46 var observer = new website.Observer(function (mutations) {
47 if (!_(mutations).find(function (m) {
48 return m.type === 'childList' && m.addedNodes.length > 0;
52 hack_to_add_snippet_id()
55 // puts $el at the same absolute position as $target
56 function hack_to_add_snippet_id () {
57 _.each(website.snippet.selector, function (val) {
58 $(val[0]).each(function() {
59 if (!$(this).is("[data-snippet-id]") && $(this).parents("[data-oe-model]").length) {
60 $(this).attr("data-snippet-id", val[1]);
65 function remove_added_snippet_id () {
66 _.each(website.snippet.selector, function (val) {
67 $(val[0]).each(function() {
68 if ($(this).data("snippet-id") === val[1]) {
69 $(this).removeAttr("data-snippet-id");
75 $(document).ready(function() {
76 hack_to_add_snippet_id();
79 // 'snippet-dropped' is triggered on '#oe_snippets' whith $target as attribute when a snippet is dropped
80 // 'snippet-activated' is triggered on '#oe_snippets' (and on snippet) when a snippet is activated
82 if (!website.snippet) website.snippet = {};
83 website.snippet.styles = {};
84 website.snippet.selector = [];
85 website.snippet.BuildingBlock = openerp.Widget.extend({
86 template: 'website.snippets',
88 init: function (parent) {
90 this._super.apply(this, arguments);
91 if(!$('#oe_manipulators').length){
92 $("<div id='oe_manipulators'></div>").appendTo('body');
94 this.$active_snipped_id = false;
95 hack_to_add_snippet_id();
98 observer.observe(document.body, {
103 dom_filter: function (dom, sibling) {
104 if (typeof dom === "string") {
105 var include = "[data-oe-model]";
106 var sdom = dom.split(',');
108 _.each(sdom, function (val) {
109 val = val.replace(/^\s+|\s+$/g, '');
110 dom += include + " " + val + ", ";
112 val = val.split(" ");
113 dom += val.shift() + include + val.join(" ") + ", ";
116 dom = dom.replace(/,\s*$/g, '');
119 return (!sibling && $(dom).is("[data-oe-model]")) || $(dom).parents("[data-oe-model]").length ? $(dom) : $("");
125 this.$button = $(openerp.qweb.render('website.snippets_button'))
126 .prependTo(this.parent.$("#website-top-edit ul"))
129 this.$button.click(function () {
130 self.make_active(false);
131 self.$el.toggleClass("hidden");
133 $("#wrapwrap").click(function () {
134 self.$el.addClass("hidden");
137 this.fetch_snippet_templates();
139 this.bind_snippet_click_editor();
141 this.$el.addClass("hidden");
143 $(document).on('click', '.dropdown-submenu a[tabindex]', function (e) {
147 this.getParent().on('change:height', this, function (editor) {
148 self.$el.css('top', editor.get('height'));
150 self.$el.css('top', this.parent.get('height'));
152 _get_snippet_url: function () {
153 return '/website/snippets';
155 fetch_snippet_templates: function () {
158 openerp.jsonRpc(this._get_snippet_url(), 'call', {})
159 .then(function (html) {
162 var $styles = $html.find("[data-snippet-style-id]");
163 $styles.each(function () {
164 var $style = $(this);
165 var style_id = $style.data('snippet-style-id');
166 website.snippet.styles[style_id] = {
167 'snippet-style-id' : style_id,
168 'selector': $style.data('selector'),
172 $styles.addClass("hidden");
174 self.$snippets = $html.find(".tab-content > div > div").addClass("oe_snippet");
175 self.$el.append($html);
179 self.$snippets.each(function () {
180 if (self.snippet_have_dropzone($(this)))
183 if (!snippets) self.$button.css("display", "none");
185 self.make_snippet_draggable(self.$snippets);
188 cover_target: function ($el, $target){
189 var pos = $target.offset();
190 var mt = parseInt($target.css("margin-top") || 0);
191 var mb = parseInt($target.css("margin-bottom") || 0);
193 'position': 'absolute',
194 'width': $target.outerWidth(),
195 'height': $target.outerHeight() + mt + mb+1,
199 $el.find(".oe_handle.size").css("bottom", (mb-7)+'px');
202 this.$el.removeClass("hidden");
205 this.$el.addClass("hidden");
208 snippet_have_dropzone: function ($snippet) {
209 return (($snippet.data('selector-siblings') && this.dom_filter($snippet.data('selector-siblings')).size() > 0) ||
210 ($snippet.data('selector-children') && this.dom_filter($snippet.data('selector-children')).size() > 0) ||
211 ($snippet.data('selector-vertical-children') && this.dom_filter($snippet.data('selector-vertical-children')).size() > 0));
214 bind_snippet_click_editor: function () {
216 $(document).on('click', "#wrapwrap", function (event) {
217 var $target = $(event.srcElement);
218 if (!$target.attr("data-snippet-id")) {
219 $target = $target.parents("[data-snippet-id]:first");
221 if (!$target.attr("data-oe-model") && !$target.parents("[data-oe-model]:first").length) {
224 if (self.$active_snipped_id && self.$active_snipped_id.is($target)) {
227 self.make_active($target);
230 snippet_blur: function ($snippet) {
232 if ($snippet.data("snippet-editor")) {
233 $snippet.data("snippet-editor").onBlur();
237 snippet_focus: function ($snippet) {
239 if ($snippet.data("snippet-editor")) {
240 $snippet.data("snippet-editor").onFocus();
244 clean_for_save: function () {
245 for (var k in this.snippets) {
246 if (!this.snippets.hasOwnProperty(k)) { continue; }
247 var editor = $(this.snippets[k]).data("snippet-editor");
249 editor.clean_for_save();
253 make_active: function ($snippet) {
254 if ($snippet && this.$active_snipped_id && this.$active_snipped_id.get(0) === $snippet.get(0)) {
257 if (this.$active_snipped_id) {
258 this.snippet_blur(this.$active_snipped_id);
259 this.$active_snipped_id = false;
261 if ($snippet && $snippet.length) {
262 if(_.indexOf(this.snippets, $snippet.get(0)) === -1) {
263 this.snippets.push($snippet.get(0));
265 this.$active_snipped_id = $snippet;
266 this.create_overlay(this.$active_snipped_id);
267 this.snippet_focus($snippet);
269 $("#oe_snippets").trigger('snippet-activated', $snippet);
271 $snippet.trigger('snippet-activated', $snippet);
274 create_overlay: function ($snippet) {
275 if (typeof $snippet.data("snippet-editor") === 'undefined') {
276 var $targets = this.activate_overlay_zones($snippet);
277 if (!$targets.length) return;
278 var editor = website.snippet.editorRegistry[$snippet.data("snippet-id")] || website.snippet.editorRegistry.resize;
279 $snippet.data("snippet-editor", new editor(this, $snippet));
281 this.cover_target($snippet.data('overlay'), $snippet);
284 path_eval: function(path){
286 path = path.split('.');
288 obj = obj[path.shift()];
289 }while(path.length && obj);
293 // activate drag and drop for the snippets in the snippet toolbar
294 make_snippet_draggable: function($snippets){
296 var $tumb = $snippets.find(".oe_snippet_thumbnail:first");
297 var left = $tumb.outerWidth()/2;
298 var top = $tumb.outerHeight()/2;
299 var $toInsert, dropped, $snippet, action, snipped_id;
301 $snippets.draggable({
307 handle: ".oe_snippet_thumbnail",
316 snipped_id = $snippet.data('snippet-id');
317 action = $snippet.find('.oe_snippet_body').size() ? 'insert' : 'mutate';
318 if( action === 'insert'){
319 if (!$snippet.data('selector-siblings') && !$snippet.data('selector-children') && !$snippet.data('selector-vertical-children')) {
320 console.debug($snippet.data("snippet-id") + " have oe_snippet_body class and have not for insert action"+
321 "data-selector-siblings, data-selector-children or data-selector-vertical-children tag for mutate action");
324 self.activate_insertion_zones({
325 siblings: $snippet.data('selector-siblings'),
326 children: $snippet.data('selector-children'),
327 vertical_children: $snippet.data('selector-vertical-children')
330 $toInsert = $snippet.find('.oe_snippet_body').clone();
331 $toInsert.removeClass('oe_snippet_body');
332 $toInsert.data('src-snippet-id', snipped_id);
333 if (!$toInsert.data('snippet-id')) {
334 $toInsert.attr('data-snippet-id', snipped_id);
336 snipped_id = $toInsert.data('snippet-id');
339 } else if( action === 'mutate' ){
340 if (!$snippet.data('selector')) {
341 console.debug($snippet.data("snippet-id") + " have not oe_snippet_body class and have not data-selector tag");
344 var $targets = self.activate_overlay_zones($snippet.data('selector'));
345 $targets.each(function(){
346 var $clone = $(this).data('overlay').clone();
347 $clone.addClass("oe_drop_zone").data('target', $(this));
348 $(this).data('overlay').after($clone);
353 $('.oe_drop_zone').droppable({
355 if( action === 'insert'){
357 $(this).first().after($toInsert);
361 var prev = $toInsert.prev();
362 if( action === 'insert' && this === prev[0]){
369 stop: function(ev, ui){
370 if (action === 'insert' && ! dropped && $('.oe_drop_zone') && ui.position.top > 3) {
371 var el = $('.oe_drop_zone').nearest({x: ui.position.left, y: ui.position.top}).first();
378 $('.oe_drop_zone').droppable('destroy').remove();
381 if(action === 'insert'){
384 website.snippet.start_animation();
386 self.create_overlay($target);
387 if ($target.data("snippet-editor")) {
388 $target.data("snippet-editor").drop_and_build_snippet($target);
391 $target.find("[data-snippet-id]").each(function () {
392 var $snippet = $(this);
393 var snippet_id = $snippet.data("data-snippet-id");
394 self.create_overlay($snippet);
395 if ($snippet.data("snippet-editor")) {
396 $snippet.data("snippet-editor").drop_and_build_snippet($snippet);
401 $target = $(this).data('target');
403 self.create_overlay($target);
404 if (website.snippet.editorRegistry[snipped_id]) {
405 var snippet = new website.snippet.editorRegistry[snipped_id](self, $target);
406 snippet.drop_and_build_snippet($target);
409 setTimeout(function () {
410 $("#oe_snippets").trigger('snippet-dropped', $target);
412 // reset snippet for rte
413 $target.removeData("snippet-editor");
414 if ($target.data("overlay")) {
415 $target.data("overlay").remove();
416 $target.removeData("overlay");
418 self.create_overlay($target);
419 $target.find("[data-snippet-id]").each(function () {
420 var $snippet = $(this);
421 $snippet.removeData("snippet-editor");
422 if ($snippet.data("overlay")) {
423 $snippet.data("overlay").remove();
424 $snippet.removeData("overlay");
426 self.create_overlay($snippet);
430 self.make_active($target);
439 // return the original snippet in the editor bar from a snippet id (string)
440 get_snippet_from_id: function(id){
441 return $('.oe_snippet').filter(function(){
442 return $(this).data('snippet-id') === id;
446 // Create element insertion drop zones. two css selectors can be provided
447 // selector.children -> will insert drop zones as direct child of the selected elements
448 // in case the selected elements have children themselves, dropzones will be interleaved
450 // selector.siblings -> will insert drop zones after and before selected elements
451 activate_insertion_zones: function(selector){
453 var child_selector = selector.children;
454 var sibling_selector = selector.siblings;
455 var vertical_child_selector = selector.vertical_children;
457 var zone_template = "<div class='oe_drop_zone oe_insert'></div>";
460 self.dom_filter(child_selector).each(function (){
462 $zone.find('> *:not(.oe_drop_zone):visible').after(zone_template);
463 $zone.prepend(zone_template);
467 if(vertical_child_selector){
468 self.dom_filter(vertical_child_selector).each(function (){
470 var $template = $(zone_template).addClass("oe_vertical");
472 var $lastinsert = false;
475 $zone.find('> *:not(.oe_drop_zone):visible').each(function () {
477 $template.css('height', ($col.outerHeight() + parseInt($col.css("margin-top")) + parseInt($col.css("margin-bottom")))+'px');
478 $lastinsert = $template.clone();
479 $(this).after($lastinsert);
481 temp_left = $col.position().left;
482 if (left === temp_left) {
483 $col.prev(".oe_drop_zone.oe_vertical").remove();
484 $col.before($template.clone().css("clear", "left"));
487 $col.before($template.clone());
493 $zone.prepend($template.css('height', $zone.outerHeight()+'px'));
498 if(sibling_selector){
499 self.dom_filter(sibling_selector, true).each(function (){
501 if($zone.prev('.oe_drop_zone:visible').length === 0){
502 $zone.before(zone_template);
504 if($zone.next('.oe_drop_zone:visible').length === 0){
505 $zone.after(zone_template);
513 // var $zones = $('.oe_drop_zone + .oe_drop_zone'); // no two consecutive zones
514 // count += $zones.length;
517 $zones = $('.oe_drop_zone > .oe_drop_zone:not(.oe_vertical)').remove(); // no recursive zones
518 count += $zones.length;
522 // Cleaning up zones placed between floating or inline elements. We do not like these kind of zones.
523 var $zones = $('.oe_drop_zone:not(.oe_vertical)');
524 $zones.each(function (){
526 var prev = zone.prev();
527 var next = zone.next();
528 var float_prev = prev.css('float') || 'none';
529 var float_next = next.css('float') || 'none';
530 var disp_prev = prev.css('display') || null;
531 var disp_next = next.css('display') || null;
532 if( (float_prev === 'left' || float_prev === 'right')
533 && (float_next === 'left' || float_next === 'right') ){
535 }else if( !( disp_prev === null
536 || disp_next === null
537 || disp_prev === 'block'
538 || disp_next === 'block' )){
544 // generate drop zones covering the elements selected by the selector
545 // we generate overlay drop zones only to get an idea of where the snippet are, the drop
546 activate_overlay_zones: function(selector){
547 var $targets = this.dom_filter(selector || '[data-snippet-id]');
550 if (typeof selector !== 'string' && !$targets.length) {
551 console.debug( "A good node must have a [data-oe-model] attribute or must have at least one parent with [data-oe-model] attribute.");
552 console.debug( "Wrong node(s): ", selector);
555 function is_visible($el){
556 return $el.css('display') != 'none'
557 && $el.css('opacity') != '0'
558 && $el.css('visibility') != 'hidden';
561 // filter out invisible elements
562 $targets = $targets.filter(function(){ return is_visible($(this)); });
564 // filter out elements with invisible parents
565 $targets = $targets.filter(function(){
566 var parents = $(this).parents().filter(function(){ return !is_visible($(this)); });
567 return parents.length === 0;
570 $targets.each(function () {
571 var $target = $(this);
572 if (!$target.data('overlay')) {
573 var $zone = $(openerp.qweb.render('website.snippet_overlay'));
574 $zone.appendTo('#oe_manipulators');
575 $zone.data('target',$target);
576 $target.data('overlay',$zone);
578 $target.on("DOMNodeInserted DOMNodeRemoved DOMSubtreeModified", function () {
579 self.cover_target($zone, $target);
581 $('body').on("resize", function () {
582 self.cover_target($zone, $target);
585 self.cover_target($target.data('overlay'), $target);
592 website.snippet.styleRegistry = {};
593 website.snippet.StyleEditor = openerp.Class.extend({
594 // initialisation (don't overwrite)
595 init: function (parent, $target, snippet_id) {
596 this.parent = parent;
597 this.$target = $target;
598 var styles = this.$target.data("snippet-style-ids") || {};
599 styles[snippet_id] = this;
600 this.$target.data("snippet-style-ids", styles);
601 this.$overlay = this.$target.data('overlay');
602 this['snippet-style-id'] = snippet_id;
603 this.$el = website.snippet.styles[snippet_id].$el.find(">li").clone();
605 this.required = this.$el.data("required");
608 this.$el.find('li[data-class] a').on('mouseover mouseout click', _.bind(this._mouse, this));
609 this.$target.on('snippet-style-reset', _.bind(this.set_active, this));
613 _mouse: function (event) {
616 if (event.type === 'mouseout') {
617 if (!this.over) return;
619 } else if (event.type === 'click') {
626 if (event.type === 'mouseout') {
627 $prev = $(event.currentTarget).parent();
628 $next = this.$el.find("li[data-class].active");
630 $prev = this.$el.find("li[data-class].active");
631 $next = $(event.currentTarget).parent();
636 if ($prev && $prev[0] === $next[0]) {
643 var np = {'$next': $next, '$prev': $prev};
645 if (event.type === 'click') {
646 setTimeout(function () {
648 self.$target.trigger("snippet-style-change", [self, np]);
650 this.select(event, {'$next': $next, '$prev': $prev});
652 setTimeout(function () {
653 self.$target.trigger("snippet-style-preview", [self, np]);
655 this.preview(event, np);
658 // start is call just after the init
662 * called when a user select an item
663 * variables: np = {$next, $prev}
664 * $next is false if they are no next item selected
665 * $prev is false if they are no previous item selected
667 select: function (event, np) {
669 // add or remove html class
671 this.$target.removeClass(np.$prev.data('class' || ""));
674 this.$target.addClass(np.$next.data('class') || "");
678 * called when a user is on mouse over or mouse out of an item
679 * variables: np = {$next, $prev}
680 * $next is false if they are no next item selected
681 * $prev is false if they are no previous item selected
683 preview: function (event, np) {
686 // add or remove html class
688 this.$target.removeClass(np.$prev.data('class') || "");
691 this.$target.addClass(np.$next.data('class') || "");
695 * select and set item active or not (add highlight item and his parents)
696 * called before start
698 set_active: function () {
700 this.$el.find('li').removeClass("active");
701 var $active = this.$el.find('li[data-class]')
702 .filter(function () {
704 return ($li.data('class') && self.$target.hasClass($li.data('class')));
708 this.$el.find('li:has(li[data-class].active)').addClass("active");
713 website.snippet.styleRegistry.background = website.snippet.StyleEditor.extend({
714 _get_bg: function () {
715 return this.$target.css("background-image").replace(/url\(['"]*|['"]*\)|^none$/g, "");
717 _set_bg: function (src) {
718 this.$target.css("background-image", src && src !== "" ? 'url(' + src + ')' : "");
722 var src = this._get_bg();
723 this.$el.find("li[data-class].active.oe_custom_bg").data("src", src);
725 select: function(event, np) {
727 this._super(event, np);
729 if (np.$next.hasClass("oe_custom_bg")) {
730 var editor = new website.editor.ImageDialog();
731 editor.on('start', self, function (o) {o.url = np.$prev && np.$prev.data("src") || np.$next && np.$next.data("src") || "";});
732 editor.on('save', self, function (o) {
734 np.$next.data("src", o.url);
735 self.$target.trigger("snippet-style-change", [self, np]);
737 editor.on('cancel', self, function () {
738 if (!np.$prev || np.$prev.data("src") === "") {
739 self.$target.removeClass(np.$next.data("class"));
740 self.$target.trigger("snippet-style-change", [self, np]);
743 editor.appendTo($('body'));
745 this._set_bg(np.$next.data("src"));
749 this.$target.removeClass(np.$prev.data("class"));
752 preview: function (event, np) {
753 this._super(event, np);
755 this._set_bg(np.$next.data("src"));
758 set_active: function () {
760 var bg = self.$target.css("background-image");
761 this.$el.find('li').removeClass("active");
762 var $active = this.$el.find('li[data-class]')
763 .filter(function () {
765 return ($li.data('src') && bg.indexOf($li.data('src')) >= 0) ||
766 (!$li.data('src') && self.$target.hasClass($li.data('class')));
769 if (!$active.length) {
770 $active = this.$target.css("background-image") !== 'none' ?
771 this.$el.find('li[data-class].oe_custom_bg') :
772 this.$el.find('li[data-class=""]');
774 $active.addClass("active");
775 this.$el.find('li:has(li[data-class].active)').addClass("active");
780 website.snippet.editorRegistry = {};
781 website.snippet.Editor = openerp.Class.extend({
782 init: function (parent, dom) {
783 this.parent = parent;
784 this.$target = $(dom);
785 this.$overlay = this.$target.data('overlay');
786 this.snippet_id = this.$target.data("snippet-id");
788 this.load_style_options();
789 this.get_parent_block();
795 * Read data XML and set value into:
799 * Dom hover the $target who content options
801 * content of .oe_snippet_options
802 * Displayed into the overlay options on focus
804 _readXMLData: function() {
806 this.$el = this.parent.$snippets.filter(function () { return $(this).data("snippet-id") == self.snippet_id; }).clone();
807 this.$editor = this.$el.find(".oe_snippet_options");
808 var $options = this.$overlay.find(".oe_overlay_options");
809 this.$editor.prependTo($options.find(".oe_options ul"));
810 if ($options.find(".oe_options ul li").length) {
811 $options.find(".oe_options").removeClass("hidden");
816 // activate drag and drop for the snippets in the snippet toolbar
817 _drag_and_drop: function(){
819 this.dropped = false;
820 this.$overlay.draggable({
824 handle: ".oe_snippet_move",
830 var $clone = $(this).clone().css({width: "24px", height: "24px", border: 0});
831 $clone.find(".oe_overlay_options >:not(:contains(.oe_snippet_move)), .oe_handle").remove();
832 $clone.find(":not(.glyphicon)").css({position: 'absolute', top: 0, left: 0});
833 $clone.appendTo("body").removeClass("hidden");
836 start: _.bind(self._drag_and_drop_start, self),
837 stop: _.bind(self._drag_and_drop_stop, self)
840 _drag_and_drop_after_insert_dropzone: function (){},
841 _drag_and_drop_active_drop_zone: function ($zones){
845 $(".oe_drop_zone.hide").removeClass("hide");
846 $(this).addClass("hide").first().after(self.$target);
850 $(this).removeClass("hide");
851 self.$target.detach();
852 self.dropped = false;
856 _drag_and_drop_start: function (){
859 self.parent.editor_busy = true;
861 width: self.$target.width(),
862 height: self.$target.height()
864 self.$target.after("<div class='oe_drop_clone' style='display: none;'/>");
865 self.$target.detach();
866 self.$overlay.addClass("hidden");
868 self.parent.activate_insertion_zones({
869 siblings: self.$el ? self.$el.data('selector-siblings') : false,
870 children: self.$el ? self.$el.data('selector-children') : false,
871 vertical_children: self.$el ? self.$el.data('selector-vertical-children') : false,
874 $("body").addClass('move-important');
876 self._drag_and_drop_after_insert_dropzone();
877 self._drag_and_drop_active_drop_zone($('.oe_drop_zone'));
879 _drag_and_drop_stop: function (){
882 $(".oe_drop_clone").after(self.$target);
884 self.$overlay.removeClass("hidden");
885 $("body").removeClass('move-important');
886 $('.oe_drop_zone').droppable('destroy').remove();
887 $(".oe_drop_clone, .oe_drop_to_remove").remove();
888 self.parent.editor_busy = false;
889 self.get_parent_block();
890 setTimeout(function () {self.parent.create_overlay(self.$target);},0);
893 load_style_options: function () {
895 var $styles = this.$overlay.find('.oe_options');
896 var $ul = $styles.find('ul:first');
897 _.each(website.snippet.styles, function (val) {
898 if (!self.parent.dom_filter(val.selector).is(self.$target)) {
901 var Editor = website.snippet.styleRegistry[val['snippet-style-id']] || website.snippet.StyleEditor;
902 var editor = new Editor(self, self.$target, val['snippet-style-id']);
903 $ul.prepend(editor.$el.addClass("snippet-style-" + val['snippet-style-id']));
906 if ($ul.find("li").length) {
907 $styles.removeClass("hidden");
911 get_parent_block: function () {
913 var $button = this.$overlay.find('.oe_snippet_parent');
914 var $parent = this.$target.parents("[data-snippet-id]:first");
915 if ($parent.length) {
916 $button.removeClass("hidden");
917 $button.off("click").on('click', function (event) {
918 event.preventDefault();
919 setTimeout(function () {
920 self.parent.make_active($parent);
924 $button.addClass("hidden");
930 * This method is called after init and _readXMLData
934 this.$overlay.on('click', '.oe_snippet_clone', _.bind(this.on_clone, this));
935 this.$overlay.on('click', '.oe_snippet_remove', _.bind(this.on_remove, this));
936 this._drag_and_drop();
939 on_clone: function () {
940 var $clone = this.$target.clone(false);
941 this.$target.after($clone);
945 on_remove: function () {
947 var index = _.indexOf(this.parent.snippets, this.$target.get(0));
948 delete this.parent.snippets[index];
949 this.$target.remove();
950 this.$overlay.remove();
955 * drop_and_build_snippet
956 * This method is called just after that a thumbnail is drag and dropped into a drop zone
957 * (after the insertion of this.$body, if this.$body exists)
959 drop_and_build_snippet: function ($target) {
963 * This method is called when the user click inside the snippet in the dom
965 onFocus : function () {
966 this.$overlay.addClass('oe_active');
970 * This method is called when the user click outside the snippet in the dom, after a focus
972 onBlur : function () {
973 this.$overlay.removeClass('oe_active');
977 * function called just before save vue
979 clean_for_save: function () {
980 this.$target.removeAttr('contentEditable')
981 .find('*').removeAttr('contentEditable');
982 this.$target.removeAttr('attributeEditable')
983 .find('*').removeAttr('attributeEditable');
988 website.snippet.editorRegistry.resize = website.snippet.Editor.extend({
992 var $box = $(openerp.qweb.render("website.snippets.resize"));
994 var resize_values = this.getSize();
995 if (!resize_values.n) $box.find(".oe_handle.n").remove();
996 if (!resize_values.s) $box.find(".oe_handle.s").remove();
997 if (!resize_values.e) $box.find(".oe_handle.e").remove();
998 if (!resize_values.w) $box.find(".oe_handle.w").remove();
999 if (!resize_values.size) $box.find(".oe_handle.size").remove();
1001 this.$overlay.append($box.find(".oe_handles").html());
1003 this.$overlay.find(".oe_handle:not(:has(.oe_handle_button)), .oe_handle .oe_handle_button").on('mousedown', function (event){
1004 event.preventDefault();
1006 var $handle = $(this);
1008 var resize_values = self.getSize();
1009 var compass = false;
1011 if ($handle.hasClass('n')) {
1015 else if ($handle.hasClass('s')) {
1019 else if ($handle.hasClass('e')) {
1023 else if ($handle.hasClass('w')) {
1027 else if ($handle.hasClass('size')) {
1032 var resize = resize_values[compass];
1033 if (!resize) return;
1036 if (compass === 'size') {
1037 var offset = self.$target.offset().top;
1038 if (self.$target.css("background").match(/rgba\(0, 0, 0, 0\)/)) {
1039 self.$target.addClass("resize_editor_busy");
1042 var xy = event['page'+XY];
1043 var current = resize[2] || 0;
1044 _.each(resize[0], function (val, key) {
1045 if (self.$target.hasClass(val)) {
1049 var begin = current;
1050 var beginClass = self.$target.attr("class");
1051 var regClass = new RegExp("\\s*" + resize[0][begin].replace(/[-]*[0-9]+/, '[-]*[0-9]+'), 'g');
1054 self.parent.editor_busy = true;
1056 var cursor = $handle.css("cursor")+'-important';
1057 var $body = $(document.body);
1058 $body.addClass(cursor);
1060 var body_mousemove = function (event){
1061 event.preventDefault();
1062 if (compass === 'size') {
1063 var dy = event.pageY-offset;
1064 dy = dy - dy%resize;
1065 if (dy <= 0) dy = resize;
1066 self.$target.css("height", dy+"px");
1067 self.on_resize(compass, null, dy);
1068 self.parent.cover_target(self.$overlay, self.$target);
1071 var dd = event['page'+XY] - xy + resize[1][begin];
1072 var next = current+1 === resize[1].length ? current : (current+1);
1073 var prev = current ? (current-1) : 0;
1076 if (dd > (2*resize[1][next] + resize[1][current])/3) {
1077 self.$target.attr("class", (self.$target.attr("class")||'').replace(regClass, ''));
1078 self.$target.addClass(resize[0][next]);
1082 if (prev != current && dd < (2*resize[1][prev] + resize[1][current])/3) {
1083 self.$target.attr("class", (self.$target.attr("class")||'').replace(regClass, ''));
1084 self.$target.addClass(resize[0][prev]);
1090 self.on_resize(compass, beginClass, current);
1091 self.parent.cover_target(self.$overlay, self.$target);
1095 var body_mouseup = function(){
1096 $body.unbind('mousemove', body_mousemove);
1097 $body.unbind('mouseup', body_mouseup);
1098 $body.removeClass(cursor);
1099 self.parent.editor_busy = false;
1100 self.$target.removeClass("resize_editor_busy");
1102 $body.mousemove(body_mousemove);
1103 $body.mouseup(body_mouseup);
1106 getSize: function () {
1107 var grid = [0,4,8,16,32,48,64,92,128];
1109 // list of class (Array), grid (Array), default value (INT)
1110 n: [_.map(grid, function (v) {return 'mt'+v;}), grid],
1111 s: [_.map(grid, function (v) {return 'mb'+v;}), grid],
1112 // INT if the user can resize the snippet (resizing per INT px)
1118 onFocus : function () {
1120 this.change_cursor();
1123 change_cursor : function () {
1124 var _class = this.$target.attr("class") || "";
1126 var col = _class.match(/col-md-([0-9-]+)/i);
1127 col = col ? +col[1] : 0;
1129 var offset = _class.match(/col-md-offset-([0-9-]+)/i);
1130 offset = offset ? +offset[1] : 0;
1132 var overlay_class = this.$overlay.attr("class").replace(/(^|\s+)block-[^\s]*/gi, '');
1133 if (col+offset >= 12) overlay_class+= " block-e-right";
1134 if (col === 1) overlay_class+= " block-w-right block-e-left";
1135 if (offset === 0) overlay_class+= " block-w-left";
1137 var mb = _class.match(/mb([0-9-]+)/i);
1138 mb = mb ? +mb[1] : 0;
1139 if (mb >= 128) overlay_class+= " block-s-bottom";
1140 else if (!mb) overlay_class+= " block-s-top";
1142 var mt = _class.match(/mt([0-9-]+)/i);
1143 mt = mt ? +mt[1] : 0;
1144 if (mt >= 128) overlay_class+= " block-n-top";
1145 else if (!mt) overlay_class+= " block-n-bottom";
1147 this.$overlay.attr("class", overlay_class);
1151 * called when the box is resizing and the class change, before the cover_target
1152 * @compass: resize direction : 'n', 's', 'e', 'w'
1153 * @beginClass: attributes class at the begin
1154 * @current: curent increment in this.grid
1156 on_resize: function (compass, beginClass, current) {
1157 this.change_cursor();
1161 website.snippet.editorRegistry.colmd = website.snippet.editorRegistry.resize.extend({
1162 getSize: function () {
1163 this.grid = this._super();
1164 var width = this.$target.parents(".row:first").first().outerWidth();
1166 var grid = [1,2,3,4,5,6,7,8,9,10,11,12];
1167 this.grid.e = [_.map(grid, function (v) {return 'col-md-'+v;}), _.map(grid, function (v) {return width/12*v;})];
1169 var grid = [-12,-11,-10,-9,-8,-7,-6,-5,-4,-3,-2,-1,0,1,2,3,4,5,6,7,8,9,10,11];
1170 this.grid.w = [_.map(grid, function (v) {return 'col-md-offset-'+v;}), _.map(grid, function (v) {return width/12*v;}), 12];
1174 _drag_and_drop_after_insert_dropzone: function(){
1176 var $zones = $(".row:has(> .oe_drop_zone)").each(function () {
1178 var width = $row.innerWidth();
1180 while (width > pos + self.size.width) {
1181 var $last = $row.find("> .oe_drop_zone:last");
1182 $last.each(function () {
1183 pos = $(this).position().left;
1185 if (width > pos + self.size.width) {
1186 $row.append("<div class='col-md-1 oe_drop_to_remove'/>");
1187 var $add_drop = $last.clone();
1188 $row.append($add_drop);
1189 self._drag_and_drop_active_drop_zone($add_drop);
1194 _drag_and_drop_start: function () {
1196 this.$target.attr("class",this.$target.attr("class").replace(/\s*(col-lg-offset-|col-md-offset-)([0-9-]+)/g, ''));
1198 _drag_and_drop_stop: function () {
1199 this.$target.addClass("col-md-offset-" + this.$target.prevAll(".oe_drop_to_remove").length);
1202 hide_remove_button: function() {
1203 this.$overlay.find('.oe_snippet_remove').toggleClass("hidden",
1204 !this.$target.siblings().length && this.$target.parents("[data-snippet-id]:first").find("[data-snippet-id='colmd']").length > 1);
1206 onFocus : function () {
1208 this.hide_remove_button();
1210 on_clone: function () {
1211 var $clone = this.$target.clone(false);
1212 var _class = $clone.attr("class").replace(/\s*(col-lg-offset-|col-md-offset-)([0-9-]+)/g, '');
1213 _class += ' col-md-1';
1214 $clone.attr("class", _class);
1215 this.$target.after($clone);
1216 this.hide_remove_button();
1219 on_remove: function () {
1220 if (!this.$target.siblings().length) {
1221 var $parent = this.$target.parents("[data-snippet-id]:first");
1222 if($parent.find("[data-snippet-id='colmd']").length > 1) {
1225 if (!$parent.data("snippet-editor")) {
1226 this.parent.create_overlay($parent);
1228 $parent.data("snippet-editor").on_remove();
1232 this.hide_remove_button();
1235 on_resize: function (compass, beginClass, current) {
1236 if (compass === 'w') {
1237 // don't change the right border position when we change the offset (replace col size)
1238 var beginCol = Number(beginClass.match(/col-md-([0-9]+)|$/)[1] || 0);
1239 var beginOffset = Number(beginClass.match(/col-md-offset-([0-9-]+)|$/)[1] || beginClass.match(/col-lg-offset-([0-9-]+)|$/)[1] || 0);
1240 var offset = Number(this.grid.w[0][current].match(/col-md-offset-([0-9-]+)|$/)[1] || 0);
1244 var colSize = beginCol - (offset - beginOffset);
1247 offset = beginOffset + beginCol - 1;
1249 this.$target.attr("class",this.$target.attr("class").replace(/\s*(col-lg-offset-|col-md-offset-|col-md-)([0-9-]+)/g, ''));
1251 this.$target.addClass('col-md-' + (colSize > 12 ? 12 : colSize));
1253 this.$target.addClass('col-md-offset-' + offset);
1256 this._super(compass, beginClass, current);
1260 website.snippet.editorRegistry.slider = website.snippet.editorRegistry.resize.extend({
1261 drop_and_build_snippet: function() {
1262 var id = $(".carousel").length;
1263 this.id = "myCarousel" + id;
1264 this.$target.attr("id", this.id);
1265 this.$target.find(".carousel-control").attr("href", "#myCarousel" + id);
1266 this.$target.find("[data-target]").attr("data-target", "#myCarousel" + id);
1268 this.rebind_event();
1270 // rebind event to active carousel on edit mode
1271 rebind_event: function () {
1273 this.$target.find('.carousel-indicators [data-target]').off('click').on('click', function () {
1274 self.$target.carousel(+$(this).data('slide-to')); });
1276 this.$target.attr('contentEditable', 'false');
1277 this.$target.find('.oe_structure, .content>.row').attr('contentEditable', 'true');
1279 clean_for_save: function () {
1281 this.$target.find(".item").removeClass("next prev left right");
1282 if(!this.$target.find(".item.active").length) {
1283 this.$target.find(".item:first").addClass("active");
1286 start : function () {
1289 this.id = this.$target.attr("id");
1290 this.$inner = this.$target.find('.carousel-inner');
1291 this.$indicators = this.$target.find('.carousel-indicators');
1293 this.$editor.find(".js_add").on('click', function () {self.on_add_slide(); return false;});
1294 this.$editor.find(".js_remove").on('click', function () {self.on_remove_slide(); return false;});
1296 this.$target.carousel('pause');
1297 this.rebind_event();
1299 on_add_slide: function () {
1301 var cycle = this.$inner.find('.item').length;
1302 var $active = this.$inner.find('.item.active, .item.prev, .item.next').first();
1303 var index = $active.index();
1304 this.$target.find('.carousel-control, .carousel-indicators').removeClass("hidden");
1305 this.$indicators.append('<li data-target="#' + this.id + '" data-slide-to="' + cycle + '"></li>');
1307 var $clone = this.$el.find(".item.active").clone();
1310 $clone.removeClass('active').insertAfter($active);
1311 setTimeout(function() {
1312 self.$target.carousel().carousel(++index);
1313 self.rebind_event();
1317 on_remove_slide: function () {
1318 if (this.remove_process) {
1323 var cycle = this.$inner.find('.item').length - 1;
1324 var index = this.$inner.find('.item.active').index();
1327 this.remove_process = true;
1328 var $el = this.$inner.find('.item.active');
1329 self.$target.on('slid.bs.carousel', function (event) {
1331 self.$indicators.find("li:last").remove();
1332 self.$target.off('slid.bs.carousel');
1333 self.rebind_event();
1334 self.remove_process = false;
1336 self.on_remove_slide(event);
1339 setTimeout(function () {
1340 self.$target.carousel( index > 0 ? --index : cycle );
1343 this.$target.find('.carousel-control, .carousel-indicators').addClass("hidden");
1348 website.snippet.editorRegistry.carousel = website.snippet.editorRegistry.slider.extend({
1349 getSize: function () {
1350 this.grid = this._super();
1354 clean_for_save: function () {
1356 this.$target.css("background-image", "");
1357 this.$target.removeClass(this._class);
1359 load_style_options : function () {
1361 $(".snippet-style-size li[data-class='']").remove();
1363 start : function () {
1367 // set background and prepare to clean for save
1368 var add_class = function (c){
1369 if (c) self._class = (self._class || "").replace(new RegExp("[ ]+" + c.replace(" ", "|[ ]+")), '') + ' ' + c;
1370 return self._class || "";
1372 this.$target.on('snippet-style-change snippet-style-preview', function (event, style, np) {
1373 var $active = self.$target.find(".item.active");
1374 if (style['snippet-style-id'] === "size") return;
1375 if (style['snippet-style-id'] === "background") {
1376 $active.css("background-image", self.$target.css("background-image"));
1379 $active.removeClass(np.$prev.data("class"));
1382 $active.addClass(np.$next.data("class"));
1383 add_class(np.$next.data("class"));
1386 this.$target.on('slid', function () { // slide.bs.carousel
1387 var $active = self.$target.find(".item.active");
1389 .css("background-image", $active.css("background-image"))
1390 .removeClass(add_class($active.attr("class")))
1391 .addClass($active.attr("class"))
1392 .trigger("snippet-style-reset");
1394 self.$target.carousel();
1396 this.$target.trigger('slid');
1398 on_add_slide: function () {
1399 var $clone = this._super();
1401 // choose an other background
1402 var $styles = this.$target.data("snippet-style-ids").background.$el.find("li[data-class]:not(.oe_custom_bg)");
1403 var styles_index = $styles.index($styles.filter(".active")[0]);
1404 var $select = $($styles[styles_index >= $styles.length-1 ? 0 : styles_index+1]);
1405 $clone.css("background-image", $select.data("src") ? "url('"+ $select.data("src") +"')" : "");
1406 $clone.addClass($select.data("class") || "");
1410 // rebind event to active carousel on edit mode
1411 rebind_event: function () {
1413 this.$target.find('.carousel-control').off('click').on('click', function () {
1414 self.$target.carousel( $(this).data('slide')); });
1416 this.$target.find('.carousel-inner .content > div').attr('contentEditable', 'true');
1417 this.$target.find('.carousel-image').attr('attributeEditable', 'true');
1422 website.snippet.editorRegistry.parallax = website.snippet.editorRegistry.resize.extend({
1423 getSize: function () {
1424 this.grid = this._super();
1428 on_resize: function (compass, beginClass, current) {
1429 this.$target.data("snippet-view").set_values();
1431 start : function () {
1435 this.$target.on('snippet-style-change snippet-style-preview', function () {
1436 self.$target.data("snippet-view").set_values();
1438 this.$target.attr('contentEditable', 'false');
1439 this.$target.find('> div > .oe_structure').attr('contentEditable', 'true');
1441 scroll: function () {
1443 var $ul = this.$editor.find('ul[name="parallax-scroll"]');
1444 var $li = $ul.find("li");
1445 var speed = this.$target.data('scroll-background-ratio') || 0.6 ;
1446 $ul.find('[data-value="' + speed + '"]').addClass('active');
1447 $li.on('click', function (event) {
1448 $li.removeClass("active");
1449 $(this).addClass("active");
1450 var speed = $(this).data('value');
1451 self.$target.attr('data-scroll-background-ratio', speed);
1452 self.$target.data("snippet-view").set_values();
1455 this.$target.data("snippet-view").set_values();
1457 clean_for_save: function () {
1459 this.$target.find(".parallax")
1460 .css("background-position", '')
1461 .removeAttr("data-scroll-background-offset");
1466 * data-snippet-id automatically setted
1467 * Don't need to add data-snippet-id="..." into the views
1470 website.snippet.selector.push([".row > [class*='col-md-']", 'colmd']);
1471 website.snippet.selector.push(['hr', 'hr']);
1472 website.snippet.selector.push(['blockquote', 'quote']);