diff options
| author | qsm-odoo <qsm@odoo.com> | 2016-10-19 17:41:10 +0200 |
|---|---|---|
| committer | qsm-odoo <qsm@odoo.com> | 2016-11-08 14:18:28 +0100 |
| commit | 5a030db3eb77b6d80187f8ae1a64303b2f50391d (patch) | |
| tree | 24e1ed9fd9862f8a1a9d255489a0f7abb461b866 /addons/website/static/src | |
| parent | 42226de46ea0cb2ff93f8e34af323fb1d805fce0 (diff) | |
[REF] website, web_editor: refactor ace editor
Stop using ugly code for ace editor.
* Really close the ace editor by changing its display property thanks
to the dedicated do_show and do_hide method.
* Make the common part of ace editors (website, web_studio) consistent
so that the extensions are really small.
* Do not do RPC to retrieve data already retrieved (now, there is only
one unique RPC when the editor starts an one RPC per view changed
on save).
* Fix born-dead code so that the website editor starts automatically
if the URL contains its associated hash and loads the correct view
* Better handle errors (previous behavior only worked partially and
only on chrome):
- Jump to the error line and make it red.
- Do not open a Dialog with mostly incomprehensible message but
allow to open it on click of the error marker.
- Do not save any changed file if one contains an error.
- Do not allow formatting a file if it contains an error (show the
error instead).
* Do not use different routes to get the same content (remove the
/website/customize_template_get route as it returns the same
content as /web_editor/customize_template_get route). Note: in
a further commit this route will be refactored.
* In a general way, use consistent method names and behaviors /
comment the code / lint files / ...
* Open the code to extensions (so that it may later load other data
than XML).
Note: this refactoring allowed to fix bugs in previous odoo versions
but this commit message will not detail them.
Diffstat (limited to 'addons/website/static/src')
| -rw-r--r-- | addons/website/static/src/js/website.ace.js | 104 | ||||
| -rw-r--r-- | addons/website/static/src/js/website.editor.js | 2 |
2 files changed, 45 insertions, 61 deletions
diff --git a/addons/website/static/src/js/website.ace.js b/addons/website/static/src/js/website.ace.js index 630a8919a2f..4fdf8834ab1 100644 --- a/addons/website/static/src/js/website.ace.js +++ b/addons/website/static/src/js/website.ace.js @@ -1,83 +1,67 @@ odoo.define('website.ace', function (require) { 'use strict'; -var ajax = require('web.ajax'); var Widget = require('web.Widget'); var website = require('website.website'); -var AceCommon = require('web_editor.ace'); +var ViewEditor = require('web_editor.ace'); var hash = "#advanced-view-editor"; var Ace = Widget.extend({ events: { - 'click a[data-action=ace]': 'launchAce', + "click a[data-action=ace]": function (e) { + e.preventDefault(); + this.launchAce(); + }, }, - launchAce: function (e) { - var self = this; - if (!window.ace && !this.loadJS_def) { - this.loadJS_def = ajax.loadJS('/web/static/lib/ace/ace.odoo-custom.js').then(function () { - return $.when(ajax.loadJS('/web/static/lib/ace/mode-xml.js'), - ajax.loadJS('/web/static/lib/ace/theme-monokai.js')); - }); + start: function () { + if (window.location.hash.substr(0, hash.length) === hash) { + this.launchAce(); } + return this._super.apply(this, arguments); + }, + launchAce: function () { + if (this.globalEditor) { + this.globalEditor.do_show(); + } else { + var currentHash = window.location.hash; + var indexOfView = currentHash.indexOf("?view="); + var initialViewID = undefined; + if (indexOfView >= 0) { + initialViewID = parseInt(currentHash.substr(indexOfView + 6), 10); + } - if (e) { - e.preventDefault(); - } + this.globalEditor = new ViewEditor(this, $(document.documentElement).data('view-xmlid'), { + initialViewID: initialViewID + }); + this.globalEditor.appendTo(document.body); - return $.when(this.loadJS_def).then(function () { - if (self.globalEditor) { - self.globalEditor.open(); - } else { - self.globalEditor = new ViewEditor(self); - self.globalEditor.appendTo($(document.body)); - } - }); + $("a[data-action=edit]").on("click", this.globalEditor.do_hide.bind(this.globalEditor)); + } }, }); -var ViewEditor = AceCommon.ViewEditor.extend({ - loadTemplates: function () { - var self = this; - var args = { - key: $(document.documentElement).data('view-xmlid'), - full: true, - bundles: this.$('.js_include_bundles')[0].checked - }; - return ajax - .jsonRpc('/website/customize_template_get', 'call', args) - .then(function (views) { - self.loadViews.call(self, views); - self.open.call(self); - var curentHash = window.location.hash; - var indexOfView = curentHash.indexOf("?view="); - if (indexOfView >= 0) { - var viewId = parseInt(curentHash.substring(indexOfView + 6, curentHash.length), 10); - self.$('#ace-view-list').val(viewId).change(); - } else { - if (views.length >= 2) { - var mainTemplate = views[1]; - self.$('#ace-view-list').val(mainTemplate.id).trigger('change'); - } - window.location.hash = hash; - } - }); - }, - displayError: function () { - var error = this._super.apply(this, arguments); - website.error(error.title, error.message); +/** + * Extend the default view editor so that the URL hash is updated with view id. + */ +ViewEditor = ViewEditor.extend({ + displayView: function () { + this._super.apply(this, arguments); + this._updateHash(); }, - updateHash: function () { - window.location.hash = hash + "?view=" + this.selectedViewId(); - }, - reloadPage: function () { - this.updateHash(); - window.location.reload(); + saveResources: function () { + return this._super.apply(this, arguments).then((function () { + this._updateHash(); + window.location.reload(); + }).bind(this)); }, - close: function () { + do_hide: function () { + this._super.apply(this, arguments); window.location.hash = ""; - this.$el.removeClass('oe_ace_open').addClass('oe_ace_closed'); + }, + _updateHash: function () { + window.location.hash = hash + "?view=" + this.selectedViewId(); }, }); @@ -86,7 +70,7 @@ website.TopBar.include({ this.ace = new Ace(); return $.when( this._super.apply(this, arguments), - this.ace.attachTo($('#html_editor')) + this.ace.attachTo(this.$('#html_editor')) ); }, }); diff --git a/addons/website/static/src/js/website.editor.js b/addons/website/static/src/js/website.editor.js index a110e6eca3e..d7bedde67f7 100644 --- a/addons/website/static/src/js/website.editor.js +++ b/addons/website/static/src/js/website.editor.js @@ -70,7 +70,7 @@ website.TopBarCustomize = Widget.extend({ }, load_menu: function () { var $menu = this.$el.children("ul"); - ajax.jsonRpc('/website/customize_template_get', 'call', { + ajax.jsonRpc('/web_editor/customize_template_get', 'call', { key: this.view_name, full: false }).then(function (result) { |
