From 4dad2bb1773d2e2a97f88cc7c0a178707732f3f6 Mon Sep 17 00:00:00 2001 From: Christophe Matthieu Date: Tue, 12 Nov 2013 17:20:36 +0100 Subject: [FIX] website snippet: fix parallax; website.hr.recruitment; split snippet js bzr revid: chm@openerp.com-20131112162036-neoyem1sus473tqr --- .../static/src/js/website.snippets.editor.js | 1233 ++++++++++++++++++++ 1 file changed, 1233 insertions(+) create mode 100644 addons/website/static/src/js/website.snippets.editor.js (limited to 'addons/website/static/src/js/website.snippets.editor.js') diff --git a/addons/website/static/src/js/website.snippets.editor.js b/addons/website/static/src/js/website.snippets.editor.js new file mode 100644 index 00000000000..edeafbc6986 --- /dev/null +++ b/addons/website/static/src/js/website.snippets.editor.js @@ -0,0 +1,1233 @@ +(function () { + 'use strict'; + + var website = openerp.website; + website.add_template_file('/website/static/src/xml/website.snippets.xml'); + + website.EditorBar.include({ + start: function () { + var self = this; + $("[data-oe-model]").on('click', function (event) { + var $this = $(event.srcElement); + var tag = $this[0].tagName.toLowerCase(); + if (!(tag === 'a' || tag === "button") && !$this.parents("a, button").length) { + self.$('[data-action="edit"]').parent().effect('bounce', {distance: 18, times: 5}, 250); + } + }); + return this._super(); + }, + edit: function () { + var self = this; + $("body").off('click'); + window.snippets = this.snippets = new website.snippet.BuildingBlock(this); + this.snippets.appendTo(this.$el); + + this.on('rte:ready', this, function () { + self.snippets.$button.removeClass("hidden"); + website.snippet.stop_animation(); + website.snippet.start_animation(); + self.trigger('rte:snippets_ready'); + }); + + return this._super.apply(this, arguments); + }, + save: function () { + this.snippets.make_active(false); + + // FIXME: call clean_for_save on all snippets of the page, not only modified ones + // important for banner of parallax that changes data automatically. + this.snippets.clean_for_save(); + remove_added_snippet_id(); + this._super(); + }, + }); + + /* ----- SNIPPET SELECTOR ---- */ + + var observer = new website.Observer(function (mutations) { + if (!_(mutations).find(function (m) { + return m.type === 'childList' && m.addedNodes.length > 0; + })) { + return; + } + hack_to_add_snippet_id() + }); + + // puts $el at the same absolute position as $target + function hack_to_add_snippet_id () { + _.each(website.snippet.selector, function (val) { + $(val[0]).each(function() { + if (!$(this).is("[data-snippet-id]") && $(this).parents("[data-oe-model]").length) { + $(this).attr("data-snippet-id", val[1]); + } + }); + }); + } + function remove_added_snippet_id () { + _.each(website.snippet.selector, function (val) { + $(val[0]).each(function() { + if ($(this).data("snippet-id") === val[1]) { + $(this).removeAttr("data-snippet-id"); + } + }); + }); + } + + $(document).ready(function() { + hack_to_add_snippet_id(); + }); + + website.snippet.selector = []; + website.snippet.BuildingBlock = openerp.Widget.extend({ + template: 'website.snippets', + activeSnippets: [], + init: function (parent) { + this.parent = parent; + this._super.apply(this, arguments); + if(!$('#oe_manipulators').length){ + $("
").appendTo('body'); + } + this.$active_snipped_id = false; + hack_to_add_snippet_id(); + this.snippets = []; + + observer.observe(document.body, { + childList: true, + subtree: true, + }); + }, + dom_filter: function (dom, sibling) { + if (typeof dom === "string") { + var include = "[data-oe-model]"; + var sdom = dom.split(','); + dom = ""; + _.each(sdom, function (val) { + val = val.replace(/^\s+|\s+$/g, ''); + dom += include + " " + val + ", "; + if (!sibling) { + val = val.split(" "); + dom += val.shift() + include + val.join(" ") + ", "; + } + }); + dom = dom.replace(/,\s*$/g, ''); + return $(dom); + } else { + return (!sibling && $(dom).is("[data-oe-model]")) || $(dom).parents("[data-oe-model]").length ? $(dom) : $(""); + } + }, + start: function() { + var self = this; + + this.$button = $(openerp.qweb.render('website.snippets_button')) + .prependTo(this.parent.$("#website-top-edit ul")) + .find("button"); + + this.$button.click(function () { + self.make_active(false); + self.$el.toggleClass("hidden"); + }); + + this.fetch_snippet_templates(); + + this.bind_snippet_click_editor(); + + this.$el.addClass("hidden"); + + this.$modal = $(openerp.qweb.render('website.snippets_modal')); + this.$modal.appendTo("body"); + }, + fetch_snippet_templates: function () { + var self = this; + this.style_templates = {}; + + openerp.jsonRpc("/website/snippets", 'call', {}) + .then(function (html) { + var $html = $(html); + + var $styles = $html.find("#snippet_styles"); + $styles.find("> [data-snippet-id]").each(function () { + var $style = $(this); + var snipped_id = $style.data('snippet-id'); + self.style_templates[snipped_id] = { + 'snipped-id' : snipped_id, + 'selector': $style.data('selector'), + 'class': $style.find(".oe_snippet_class").text(), + 'label': $style.find(".oe_snippet_label").text() + }; + }); + $styles.remove(); + + self.$snippets = $html.find(".tab-content > div > div").addClass("oe_snippet"); + self.$el.append($html); + + self.make_snippet_draggable(self.$snippets); + }); + }, + cover_target: function ($el, $target){ + var pos = $target.offset(); + var mt = parseInt($target.css("margin-top") || 0); + var mb = parseInt($target.css("margin-bottom") || 0); + $el.css({ + 'position': 'absolute', + 'width': $target.outerWidth(), + 'height': $target.outerHeight() + mt + mb, + 'top': pos.top - mt, + 'left': pos.left + }); + }, + show: function () { + this.$el.removeClass("hidden"); + }, + hide: function () { + this.$el.addClass("hidden"); + }, + + bind_snippet_click_editor: function () { + var self = this; + var snipped_event_flag = false; + $("body").on('click', "[data-oe-model] [data-snippet-id], [data-oe-model][data-snippet-id]", function (event) { + if (snipped_event_flag) { + return; + } + snipped_event_flag = true; + setTimeout(function () {snipped_event_flag = false;}, 0); + var $target = $(event.currentTarget); + if (self.$active_snipped_id && self.$active_snipped_id.is($target)) { + return; + } + self.make_active($target); + }); + $("[data-oe-model]").on('click', function () { + if (!snipped_event_flag && self.$active_snipped_id && !self.$active_snipped_id.parents("[data-snippet-id]:first")) { + self.make_active(false); + } + }); + }, + snippet_blur: function ($snipped_id) { + if ($snipped_id) { + if ($snipped_id.data("snippet-editor")) { + $snipped_id.data("snippet-editor").onBlur(); + } + } + }, + snippet_focus: function ($snipped_id) { + if ($snipped_id) { + if ($snipped_id.data("snippet-editor")) { + $snipped_id.data("snippet-editor").onFocus(); + } + } + }, + clean_for_save: function () { + for (var k in this.snippets) { + if (!this.snippets.hasOwnProperty(k)) { continue; } + var editor = $(this.snippets[k]).data("snippet-editor"); + if (editor) { + editor.clean_for_save(); + } + } + }, + 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); + this.$active_snipped_id = false; + } + if ($snipped_id) { + if(_.indexOf(this.snippets, $snipped_id.get(0)) === -1) { + this.snippets.push($snipped_id.get(0)); + } + this.$active_snipped_id = $snipped_id; + this.create_overlay(this.$active_snipped_id); + this.snippet_focus($snipped_id); + } + }, + create_overlay: function ($snipped_id) { + if (typeof $snipped_id.data("snippet-editor") === 'undefined') { + var $targets = this.activate_overlay_zones($snipped_id); + if (!$targets.length) return; + var editor = website.snippet.editorRegistry[$snipped_id.data("snippet-id")] || website.snippet.editorRegistry.resize; + $snipped_id.data("snippet-editor", new editor(this, $snipped_id)); + } + this.cover_target($snipped_id.data('overlay'), $snipped_id); + }, + + path_eval: function(path){ + var obj = window; + path = path.split('.'); + do{ + obj = obj[path.shift()]; + }while(path.length && obj); + return obj; + }, + + // activate drag and drop for the snippets in the snippet toolbar + make_snippet_draggable: function($snippets){ + var self = this; + var $tumb = $snippets.find(".oe_snippet_thumbnail:first"); + var left = $tumb.outerWidth()/2; + var top = $tumb.outerHeight()/2; + var $toInsert, dropped, $snippet, action, snipped_id; + + $snippets.draggable({ + greedy: true, + helper: 'clone', + zIndex: '1000', + appendTo: 'body', + cursor: "move", + handle: ".oe_snippet_thumbnail", + cursorAt: { + 'left': left, + 'top': top + }, + start: function(){ + self.hide(); + dropped = false; + $snippet = $(this); + snipped_id = $snippet.data('snippet-id'); + action = $snippet.find('.oe_snippet_body').size() ? 'insert' : 'mutate'; + if( action === 'insert'){ + if (!$snippet.data('selector-siblings') && !$snippet.data('selector-children') && !$snippet.data('selector-vertical-children')) { + console.debug($snippet.data("snippet-id") + " have oe_snippet_body class and have not for insert action"+ + "data-selector-siblings, data-selector-children or data-selector-vertical-children tag for mutate action"); + return; + } + self.activate_insertion_zones({ + siblings: $snippet.data('selector-siblings'), + children: $snippet.data('selector-children'), + vertical_children: $snippet.data('selector-vertical-children') + }); + + $toInsert = $snippet.find('.oe_snippet_body').clone(); + $toInsert.removeClass('oe_snippet_body'); + $toInsert.attr('data-snippet-id', snipped_id); + + } else if( action === 'mutate' ){ + if (!$snippet.data('selector')) { + console.debug($snippet.data("snippet-id") + " have not oe_snippet_body class and have not data-selector tag"); + return; + } + 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); + }); + + } + + $('.oe_drop_zone').droppable({ + over: function(){ + if( action === 'insert'){ + dropped = true; + $(this).first().after($toInsert); + } + }, + out: function(){ + if( action === 'insert'){ + dropped = false; + $toInsert.detach(); + } + }, + drop: function(){ + dropped = true; + }, + }); + }, + stop: function(ev, ui){ + if (action === 'insert' && ! dropped) { + var $el = $('.oe_drop_zone').nearest({x: ui.position.left, y: ui.position.top}).first(); + if ($el) { + $el.after($toInsert); + dropped = true; + } + } + + $('.oe_drop_zone').droppable('destroy').remove(); + if (dropped) { + var $target = false; + if(action === 'insert'){ + $target = $toInsert; + + website.snippet.start_animation(); + + self.create_overlay($target); + $target.data("snippet-editor").drop_and_build_snippet($target); + + } else { + $target = $(this).data('target'); + + self.create_overlay($target); + if (website.snippet.editorRegistry[snipped_id]) { + var snippet = new website.snippet.editorRegistry[snipped_id](self, $target); + snippet.drop_and_build_snippet($target); + } + } + setTimeout(function () {self.make_active($target);},0); + } else { + $toInsert.remove(); + if (self.$modal.find('input:not(:checked)').length) { + self.$modal.modal('toggle'); + } + } + }, + }); + }, + + // 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(); + }, + + // Create element insertion drop zones. two css selectors can be provided + // selector.children -> 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.children; + var sibling_selector = selector.siblings; + var vertical_child_selector = selector.vertical_children; + + var zone_template = "
"; + + if(child_selector){ + self.dom_filter(child_selector).each(function (){ + var $zone = $(this); + $zone.find('> *:not(.oe_drop_zone):visible').after(zone_template); + $zone.prepend(zone_template); + }); + } + + if(vertical_child_selector){ + self.dom_filter(vertical_child_selector).each(function (){ + var $zone = $(this); + var $template = $(zone_template).addClass("oe_vertical"); + var nb = 0; + var $lastinsert = false; + var left = 0; + var temp_left = 0; + $zone.find('> *:not(.oe_drop_zone):visible').each(function () { + var $col = $(this); + $template.css('height', ($col.outerHeight() + parseInt($col.css("margin-top")) + parseInt($col.css("margin-bottom")))+'px'); + $lastinsert = $template.clone(); + $(this).after($lastinsert); + + temp_left = $col.position().left; + if (left === temp_left) { + $col.prev(".oe_drop_zone.oe_vertical").remove(); + $col.before($template.clone().css("clear", "left")); + } + else if (!nb) { + $col.before($template.clone()); + } + left = temp_left; + nb ++; + }); + if (!nb) { + $zone.prepend($template.css('height', $zone.outerHeight()+'px')); + } + }); + } + + if(sibling_selector){ + self.dom_filter(sibling_selector, true).each(function (){ + var $zone = $(this); + if($zone.prev('.oe_drop_zone:visible').length === 0){ + $zone.before(zone_template); + } + if($zone.next('.oe_drop_zone:visible').length === 0){ + $zone.after(zone_template); + } + }); + } + + var count; + do { + count = 0; + // var $zones = $('.oe_drop_zone + .oe_drop_zone'); // no two consecutive zones + // count += $zones.length; + // $zones.remove(); + + $zones = $('.oe_drop_zone > .oe_drop_zone:not(.oe_vertical)').remove(); // no recursive 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. + var $zones = $('.oe_drop_zone:not(.oe_vertical)'); + $zones.each(function (){ + var zone = $(this); + var prev = zone.prev(); + var next = zone.next(); + var float_prev = prev.css('float') || 'none'; + var float_next = next.css('float') || 'none'; + var disp_prev = prev.css('display') || null; + var disp_next = next.css('display') || null; + if( (float_prev === 'left' || float_prev === 'right') + && (float_next === 'left' || float_next === 'right') ){ + zone.remove(); + }else if( !( disp_prev === null + || disp_next === null + || disp_prev === 'block' + || disp_next === 'block' )){ + zone.remove(); + } + }); + }, + + // generate drop zones covering the elements selected by the selector + // we generate overlay drop zones only to get an idea of where the snippet are, the drop + activate_overlay_zones: function(selector){ + var $targets = this.dom_filter(selector || '[data-snippet-id]'); + var self = this; + + if (typeof selector !== 'string' && !$targets.length) { + console.debug( "A good node must have a [data-oe-model] attribute or must have at least one parent with [data-oe-model] attribute."); + console.debug( "Wrong node(s): ", selector); + } + + 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; + }); + + $targets.each(function () { + var $target = $(this); + if (!$target.data('overlay')) { + var $zone = $(openerp.qweb.render('website.snippet_overlay')); + $zone.appendTo('#oe_manipulators'); + $zone.data('target',$target); + $target.data('overlay',$zone); + + $target.on("DOMNodeInserted DOMNodeRemoved DOMSubtreeModified", function () { + self.cover_target($zone, $target); + }); + $('body').on("resize", function () { + self.cover_target($zone, $target); + }); + } + self.cover_target($target.data('overlay'), $target); + }); + return $targets; + } + }); + + + website.snippet.editorRegistry = {}; + website.snippet.Editor = openerp.Class.extend({ + init: function (parent, dom) { + this.parent = parent; + this.$target = $(dom); + this.$overlay = this.$target.data('overlay'); + this.snippet_id = this.$target.data("snippet-id"); + this._readXMLData(); + this.load_style_options(); + this.get_parent_block(); + this.start(); + }, + + /* + * _readXMLData + * Read data XML and set value into: + * this.$el : + * all xml data + * this.$overlay : + * Dom hover the $target who content options + * this.$editor : + * content of .oe_snippet_options + * Displayed into the overlay options on focus + */ + _readXMLData: function() { + var self = this; + this.$el = this.parent.$snippets.filter(function () { return $(this).data("snippet-id") == self.snippet_id; }).clone(); + this.$editor = this.$el.find(".oe_snippet_options"); + var $options = this.$overlay.find(".oe_overlay_options"); + this.$editor.prependTo($options.find(".oe_options ul")); + if ($options.find(".oe_options ul li").length) { + $options.find(".oe_options").removeClass("hidden"); + } + }, + + + // activate drag and drop for the snippets in the snippet toolbar + _drag_and_drop: function(){ + var self = this; + this.dropped = false; + this.$overlay.draggable({ + greedy: true, + appendTo: 'body', + cursor: "move", + handle: ".oe_snippet_move", + cursorAt: { + left: 18, + top: 14 + }, + helper: function() { + var $clone = $(this).clone().css({width: "24px", height: "24px", border: 0}); + $clone.find(".oe_overlay_options >:not(:contains(.oe_snippet_move)), .oe_handle").remove(); + $clone.find(":not(.glyphicon)").css({position: 'absolute', top: 0, left: 0}); + $clone.appendTo("body").removeClass("hidden"); + return $clone; + }, + start: _.bind(self._drag_and_drop_start, self), + stop: _.bind(self._drag_and_drop_stop, self) + }); + }, + _drag_and_drop_after_insert_dropzone: function (){}, + _drag_and_drop_active_drop_zone: function ($zones){ + var self = this; + $zones.droppable({ + over: function(){ + $(".oe_drop_zone.hide").removeClass("hide"); + $(this).addClass("hide").first().after(self.$target); + self.dropped = true; + }, + out: function(){ + $(this).removeClass("hide"); + self.$target.detach(); + self.dropped = false; + }, + }); + }, + _drag_and_drop_start: function (){ + var self = this; + self.parent.hide(); + self.parent.editor_busy = true; + self.size = { + width: self.$target.width(), + height: self.$target.height() + }; + self.$target.after("