From ae8630a73522a51c462fdc613b21d13d7fc2ac35 Mon Sep 17 00:00:00 2001 From: Fabien Meghazi Date: Tue, 20 Aug 2013 23:34:50 +0200 Subject: [PATCH] [IMP] website's framework setup and template loading bzr revid: fme@openerp.com-20130820213450-r71yyp42lw45plem --- addons/website/static/src/js/website.js | 115 ++++++++++++++---------- addons/website/views/views.xml | 19 ++-- 2 files changed, 78 insertions(+), 56 deletions(-) diff --git a/addons/website/static/src/js/website.js b/addons/website/static/src/js/website.js index 6feb8227f70..c3d8dd1e5f3 100644 --- a/addons/website/static/src/js/website.js +++ b/addons/website/static/src/js/website.js @@ -1,17 +1,19 @@ -$(function(){ +(function() { + "use strict"; - // Ugly. I'll clean this monday - if ($('html').attr('data-editable') !== '1'){ - return; - } + var website = {}; + // The following line can be removed in 2017 + openerp.website = website; - // TODO fme: put everything in openerp.website scope and load templates on - // next tick or document ready - // Also check with xmo if jquery.keypress.js is still needed. + var templates = [ + '/website/static/src/xml/website.xml' + ]; /* ----- TEMPLATE LOADING ---- */ - - function loadTemplates(templates) { + website.add_template = function(template) { + templates.push(template); + }; + website.load_templates = function(templates) { var def = $.Deferred(); var count = templates.length; templates.forEach(function(t) { @@ -27,41 +29,16 @@ $(function(){ }); }); return def; - } + }; - var templates = [ - '/website/static/src/xml/website.xml' - ]; - - loadTemplates(templates).then(function(){ - var editor = new EditorBar(); + website.init_editor = function () { + var editor = new website.EditorBar(); editor.prependTo($('body')); $('body').css('padding-top', '50px'); // Not working properly: editor.$el.outerHeight()); - // TODO: Create an openerp.Widget out of this - }); - - /* ----- PUBLISHING STUFF ---- */ - - $(document).on('click', '.js_publish, .js_unpublish', function (e) { - e.preventDefault(); - var $link = $(this).parent(); - $link.find('.js_publish, .js_unpublish').addClass("hidden"); - var $unp = $link.find(".js_unpublish"); - var $p = $link.find(".js_publish"); - $.post('/website/publish', {'id': $link.data('id'), 'object': $link.data('object')}, function (result) { - if (+result) { - $p.addClass("hidden"); - $unp.removeClass("hidden"); - } else { - $p.removeClass("hidden"); - $unp.addClass("hidden"); - } - }); - }); + }; /* ----- TOP EDITOR BAR FOR ADMIN ---- */ - - var EditorBar = openerp.Widget.extend({ + website.EditorBar = openerp.Widget.extend({ template: 'Website.EditorBar', events: { 'click button[data-action=edit]': 'edit', @@ -116,10 +93,10 @@ $(function(){ snippet: this.$('button[data-action=snippet]'), }; - this.rte = new RTE(this); + this.rte = new website.RTE(this); this.rte.on('change', this, this.proxy('rte_changed')); - this.snippets = new Snippets(); + this.snippets = new website.Snippets(); this.snippets.appendTo($("body")); window.snippets = this.snippets; @@ -218,8 +195,7 @@ $(function(){ }); /* ----- RICH TEXT EDITOR ---- */ - - var RTE = openerp.Widget.extend({ + website.RTE = openerp.Widget.extend({ tagName: 'li', id: 'oe_rte_toolbar', className: 'oe_right oe_rte_toolbar', @@ -323,8 +299,7 @@ $(function(){ }); /* ----- SNIPPET SELECTOR ---- */ - - var Snippets = openerp.Widget.extend({ + website.Snippets = openerp.Widget.extend({ template: 'website.snippets', init: function () { this._super.apply(this, arguments); @@ -464,5 +439,51 @@ $(function(){ }, }); -}); + var all_ready = null; + var dom_ready = $.Deferred(); + $(dom_ready.resolve); + + /** + * Returns a deferred resolved when the templates are loaded + * and the Widgets can be instanciated. + */ + website.ready = function() { + if (!all_ready) { + all_ready = dom_ready.then(function () { + // TODO: load translations + return website.load_templates(templates); + }); + } + return all_ready; + }; + + dom_ready.then(function () { + website.is_editable = $('html').attr('data-editable') === '1'; + + if (website.is_editable) { + website.ready().then(website.init_editor); + } + + /* ----- PUBLISHING STUFF ---- */ + $(document).on('click', '.js_publish, .js_unpublish', function (e) { + e.preventDefault(); + var $link = $(this).parent(); + $link.find('.js_publish, .js_unpublish').addClass("hidden"); + var $unp = $link.find(".js_unpublish"); + var $p = $link.find(".js_publish"); + $.post('/website/publish', {'id': $link.data('id'), 'object': $link.data('object')}, function (result) { + if (+result) { + $p.addClass("hidden"); + $unp.removeClass("hidden"); + } else { + $p.removeClass("hidden"); + $unp.addClass("hidden"); + } + }); + }); + + }); + + return website; +})(); diff --git a/addons/website/views/views.xml b/addons/website/views/views.xml index 87ba3d49e03..a2b42cf2f84 100644 --- a/addons/website/views/views.xml +++ b/addons/website/views/views.xml @@ -27,21 +27,22 @@ - - - + + + + + + - -