diff options
| author | IEL <iel@odoo.com> | 2020-11-10 14:57:27 +0000 |
|---|---|---|
| committer | Ivan Yelizariev <iel@odoo.com> | 2020-11-20 13:01:43 +0000 |
| commit | 710784da1f02d45cbe898da426ba7e2ac63dc711 (patch) | |
| tree | 724e336eb15714db025a8342a1cdef28d9f916a7 | |
| parent | 7ae0e00738a0bcedb4b36049ce4364f46c7d899e (diff) | |
[FIX] web_editor, website_form: speed up and robustify form editing
STEPS:
* open website page with a form (e.g. /contactus)
* click edit
* slow down connection speed
* click fast between form input lines or change order of the lines
BEFORE:
* 7 svg files are requested every click
* get_authorized_fields is called every click
* random error "'classList' of undefined" in SelectUserValueWidget::close
* random similar errors when a method is called before initialization
AFTER:
* svg and get_authorized_fields are requested once
* random errors are no reproduced or it's hard to do it
* performance of switching between inputs doesn't depend on connection
speed (though, still not instant)
WHY:
* about changing in SelectUserValueWidget::close: posible reason why
this.menuTogglerEl may be undefined is because method start is async
* the other updates just implement caching
---
opw-2368928
closes odoo/odoo#61598
Signed-off-by: Quentin Smetz (qsm) <qsm@odoo.com>
| -rw-r--r-- | addons/web_editor/static/src/js/editor/snippets.options.js | 30 | ||||
| -rw-r--r-- | addons/website_form/static/src/snippets/s_website_form/options.js | 21 |
2 files changed, 36 insertions, 15 deletions
diff --git a/addons/web_editor/static/src/js/editor/snippets.options.js b/addons/web_editor/static/src/js/editor/snippets.options.js index 3b1360c8951..635360677fc 100644 --- a/addons/web_editor/static/src/js/editor/snippets.options.js +++ b/addons/web_editor/static/src/js/editor/snippets.options.js @@ -82,17 +82,25 @@ function _buildTitleElement(title) { * @param {string} src * @returns {HTMLElement} */ +const _buildImgElementCache = {}; async function _buildImgElement(src) { - if (src.split('.').pop() === 'svg') { - const response = await window.fetch(src); - const text = await response.text(); - const parser = new window.DOMParser(); - const xmlDoc = parser.parseFromString(text, 'text/xml'); - return xmlDoc.getElementsByTagName('svg')[0]; + if (!(src in _buildImgElementCache)) { + _buildImgElementCache[src] = (async () => { + if (src.split('.').pop() === 'svg') { + const response = await window.fetch(src); + const text = await response.text(); + const parser = new window.DOMParser(); + const xmlDoc = parser.parseFromString(text, 'text/xml'); + return xmlDoc.getElementsByTagName('svg')[0]; + } else { + const imgEl = document.createElement('img'); + imgEl.src = src; + return imgEl; + } + })(); } - const imgEl = document.createElement('img'); - imgEl.src = src; - return imgEl; + const node = await _buildImgElementCache[src]; + return node.cloneNode(true); } /** * Build the correct DOM for a we-row element. @@ -868,7 +876,9 @@ const SelectUserValueWidget = BaseSelectionUserValueWidget.extend({ */ close: function () { this._super(...arguments); - this.menuTogglerEl.classList.remove('active'); + if (this.menuTogglerEl) { + this.menuTogglerEl.classList.remove('active'); + } }, /** * @override diff --git a/addons/website_form/static/src/snippets/s_website_form/options.js b/addons/website_form/static/src/snippets/s_website_form/options.js index 0c202314ec2..ef542a7004a 100644 --- a/addons/website_form/static/src/snippets/s_website_form/options.js +++ b/addons/website_form/static/src/snippets/s_website_form/options.js @@ -714,6 +714,8 @@ options.registry.WebsiteFormEditor = FormEditor.extend({ }, }); +const authorizedFieldsCache = {}; + options.registry.WebsiteFieldEditor = FieldEditor.extend({ events: _.extend({}, FieldEditor.prototype.events, { 'click we-button.o_we_select_remove_option': '_onRemoveItemClick', @@ -736,11 +738,20 @@ options.registry.WebsiteFieldEditor = FieldEditor.extend({ willStart: async function () { const _super = this._super.bind(this); // Get the authorized existing fields for the form model - this.existingFields = await this._rpc({ - model: "ir.model", - method: "get_authorized_fields", - args: [this.formEl.dataset.model_name], - }).then(fields => { + const model = this.formEl.dataset.model_name; + let getFields; + if (model in authorizedFieldsCache) { + getFields = authorizedFieldsCache[model]; + } else { + getFields = this._rpc({ + model: "ir.model", + method: "get_authorized_fields", + args: [model], + }); + authorizedFieldsCache[model] = getFields; + } + + this.existingFields = await getFields.then(fields => { this.fields = _.each(fields, function (field, fieldName) { field.name = fieldName; field.domain = field.domain || []; |
