From 82c749c91d472f5e85693bbbcd823ec6f3b259c5 Mon Sep 17 00:00:00 2001 From: Fabien Meghazi Date: Mon, 28 Oct 2013 17:26:23 +0100 Subject: [PATCH] [IMP] Refactor LinkDialog -> RTELinkDialog bzr revid: fme@openerp.com-20131028162623-i2a4cixspm8q1crx --- .../website/static/src/js/website.editor.js | 260 +++++++++--------- 1 file changed, 134 insertions(+), 126 deletions(-) diff --git a/addons/website/static/src/js/website.editor.js b/addons/website/static/src/js/website.editor.js index 25453bde0d6..b3c568c16fc 100644 --- a/addons/website/static/src/js/website.editor.js +++ b/addons/website/static/src/js/website.editor.js @@ -14,7 +14,7 @@ }); function link_dialog(editor) { - return new website.editor.LinkDialog(editor).appendTo(document.body); + return new website.editor.RTELinkDialog(editor).appendTo(document.body); } function image_dialog(editor) { return new website.editor.RTEImageDialog(editor).appendTo(document.body); @@ -661,6 +661,137 @@ this.pages = Object.create(null); this.text = null; }, + start: function () { + return $.when( + this.fetch_pages().done(this.proxy('fill_pages')), + this.fetch_menus().done(this.proxy('fill_menus')), + this._super() + ).done(this.proxy('bind_data')); + }, + save: function () { + var self = this, _super = this._super.bind(this); + var $e = this.$('.list-group-item.active .url-source'); + var val = $e.val(); + if (!val || !$e[0].checkValidity()) { + // FIXME: error message + $e.closest('.form-group').addClass('has-error'); + return; + } + + var done = $.when(); + if ($e.hasClass('email-address')) { + this.make_link('mailto:' + val, false, val); + } else if ($e.hasClass('existing')) { + self.make_link(val, false, this.pages[val]); + } else if ($e.hasClass('pages')) { + // Create the page, get the URL back + done = $.get(_.str.sprintf( + '/pagenew/%s?noredirect', encodeURI(val))) + .then(function (response) { + self.make_link(response, false, val); + var parent_id = self.$('.add-to-menu').val(); + if (parent_id) { + return openerp.jsonRpc('/web/dataset/call_kw', 'call', { + model: 'website.menu', + method: 'create', + args: [{ + 'name': val, + 'url': response, + 'sequence': 0, // TODO: better tree widget + 'website_id': website.id, + 'parent_id': parent_id|0, + }], + kwargs: { + context: website.get_context() + }, + }); + } + }); + } else { + this.make_link(val, this.$('input.window-new').prop('checked')); + } + done.then(_super); + }, + make_link: function (url, new_window, label) { + }, + bind_data: function () { + var href = this.element && (this.element.data( 'cke-saved-href') + || this.element.getAttribute('href')); + if (!href) { return; } + + var match, $control; + if (match = /mailto:(.+)/.exec(href)) { + $control = this.$('input.email-address').val(match[1]); + } else if (href in this.pages) { + $control = this.$('select.existing').val(href); + } else if (match = /\/page\/(.+)/.exec(href)) { + var actual_href = '/page/website.' + match[1]; + if (actual_href in this.pages) { + $control = this.$('select.existing').val(actual_href); + } + } + if (!$control) { + $control = this.$('input.url').val(href); + } + + this.changed($control); + + this.$('input#link-text').val(this.element.getText()); + this.$('input.window-new').prop( + 'checked', this.element.getAttribute('target') === '_blank'); + }, + changed: function ($e) { + this.$('.url-source').not($e).val(''); + $e.closest('.list-group-item') + .addClass('active') + .siblings().removeClass('active') + .addBack().removeClass('has-error'); + }, + fetch_pages: function () { + return openerp.jsonRpc('/web/dataset/call_kw', 'call', { + model: 'website', + method: 'list_pages', + args: [null], + kwargs: { + context: website.get_context() + }, + }); + }, + fill_pages: function (results) { + var self = this; + var pages = this.$('select.existing')[0]; + _(results).each(function (result) { + self.pages[result.url] = result.name; + + pages.options[pages.options.length] = + new Option(result.name, result.url); + }); + }, + fetch_menus: function () { + var context = website.get_context(); + return openerp.jsonRpc('/web/dataset/call_kw', 'call', { + model: 'website.menu', + method: 'get_tree', + args: [[context.website_id]], + kwargs: { + context: context + }, + }); + }, + fill_menus: function (tree) { + var self = this; + var menus = this.$('select.add-to-menu')[0]; + var process_tree = function(node) { + var name = (new Array(node.level + 1).join('|-')) + ' ' + node.name; + menus.options[menus.options.length] = new Option(name, node.id); + node.children.forEach(function (child) { + process_tree(child); + }); + }; + process_tree(tree); + }, + }); + website.editor.RTELinkDialog = website.editor.LinkDialog.extend({ start: function () { var element; if ((element = this.get_selected_link()) && element.hasAttribute('href')) { @@ -671,11 +802,7 @@ this.add_removal_button(); } - return $.when( - this.fetch_pages().done(this.proxy('fill_pages')), - this.fetch_menus().done(this.proxy('fill_menus')), - this._super() - ).done(this.proxy('bind_data')); + return this._super(); }, add_removal_button: function () { this.$('.modal-footer').prepend( @@ -741,83 +868,6 @@ }, 0); } }, - save: function () { - var self = this, _super = this._super.bind(this); - var $e = this.$('.list-group-item.active .url-source'); - var val = $e.val(); - if (!val || !$e[0].checkValidity()) { - // FIXME: error message - $e.closest('.form-group').addClass('has-error'); - return; - } - - var done = $.when(); - if ($e.hasClass('email-address')) { - this.make_link('mailto:' + val, false, val); - } else if ($e.hasClass('existing')) { - self.make_link(val, false, this.pages[val]); - } else if ($e.hasClass('pages')) { - // Create the page, get the URL back - done = $.get(_.str.sprintf( - '/pagenew/%s?noredirect', encodeURI(val))) - .then(function (response) { - self.make_link(response, false, val); - var parent_id = self.$('.add-to-menu').val(); - if (parent_id) { - return openerp.jsonRpc('/web/dataset/call_kw', 'call', { - model: 'website.menu', - method: 'create', - args: [{ - 'name': val, - 'url': response, - 'sequence': 0, // TODO: better tree widget - 'website_id': website.id, - 'parent_id': parent_id|0, - }], - kwargs: { - context: website.get_context() - }, - }); - } - }); - } else { - this.make_link(val, this.$('input.window-new').prop('checked')); - } - done.then(_super); - }, - bind_data: function () { - var href = this.element && (this.element.data( 'cke-saved-href') - || this.element.getAttribute('href')); - if (!href) { return; } - - var match, $control; - if (match = /mailto:(.+)/.exec(href)) { - $control = this.$('input.email-address').val(match[1]); - } else if (href in this.pages) { - $control = this.$('select.existing').val(href); - } else if (match = /\/page\/(.+)/.exec(href)) { - var actual_href = '/page/website.' + match[1]; - if (actual_href in this.pages) { - $control = this.$('select.existing').val(actual_href); - } - } - if (!$control) { - $control = this.$('input.url').val(href); - } - - this.changed($control); - - this.$('input#link-text').val(this.element.getText()); - this.$('input.window-new').prop( - 'checked', this.element.getAttribute('target') === '_blank'); - }, - changed: function ($e) { - this.$('.url-source').not($e).val(''); - $e.closest('.list-group-item') - .addClass('active') - .siblings().removeClass('active') - .addBack().removeClass('has-error'); - }, /** * CKEDITOR.plugins.link.getSelectedLink ignores the editor's root, * if the editor is set directly on a link it will thus not work. @@ -825,50 +875,8 @@ get_selected_link: function () { return get_selected_link(this.editor); }, - fetch_pages: function () { - return openerp.jsonRpc('/web/dataset/call_kw', 'call', { - model: 'website', - method: 'list_pages', - args: [null], - kwargs: { - context: website.get_context() - }, - }); - }, - fill_pages: function (results) { - var self = this; - var pages = this.$('select.existing')[0]; - _(results).each(function (result) { - self.pages[result.url] = result.name; - - pages.options[pages.options.length] = - new Option(result.name, result.url); - }); - }, - fetch_menus: function () { - var context = website.get_context(); - return openerp.jsonRpc('/web/dataset/call_kw', 'call', { - model: 'website.menu', - method: 'get_tree', - args: [[context.website_id]], - kwargs: { - context: context - }, - }); - }, - fill_menus: function (tree) { - var self = this; - var menus = this.$('select.add-to-menu')[0]; - var process_tree = function(node) { - var name = (new Array(node.level + 1).join('|-')) + ' ' + node.name; - menus.options[menus.options.length] = new Option(name, node.id); - node.children.forEach(function (child) { - process_tree(child); - }); - }; - process_tree(tree); - }, }); + /** * ImageDialog widget. Lets users change an image, including uploading a * new image in OpenERP or selecting the image style (if supported by