Commit adfb08e4 authored by Romain Courteaud's avatar Romain Courteaud

[erp5_web_renderjs_ui] Label, string fields: reduce number of async code

parent a3c6df2f
...@@ -74,18 +74,13 @@ ...@@ -74,18 +74,13 @@
label_text: '', label_text: '',
error_text: '', error_text: '',
label: true, // the label element is already present in the HTML template label: true, // the label element is already present in the HTML template
css_class: '' css_class: '',
}) first_call: false
.ready(function () {
return this.changeState({
container_element: this.element.querySelector('div'), // matches the closest div
label_element: this.element.querySelector('label')
});
}) })
.declareMethod('render', function (options) { .declareMethod('render', function (options) {
var state_dict = { var state_dict = {
first_call: true,
label_text: options.field_json.title || '', label_text: options.field_json.title || '',
label: options.label, label: options.label,
field_url: getFieldTypeGadgetUrl(options.field_type), field_url: getFieldTypeGadgetUrl(options.field_type),
...@@ -107,7 +102,16 @@ ...@@ -107,7 +102,16 @@
var gadget = this, var gadget = this,
span, span,
css_class, css_class,
i; i,
queue,
new_div;
if (modification_dict.hasOwnProperty('first_call')) {
gadget.props = {
container_element: gadget.element.querySelector('div'),
label_element: gadget.element.querySelector('label')
};
}
if (gadget.state.hidden && !modification_dict.error_text) { if (gadget.state.hidden && !modification_dict.error_text) {
this.element.hidden = true; this.element.hidden = true;
...@@ -116,9 +120,9 @@ ...@@ -116,9 +120,9 @@
} }
if (modification_dict.hasOwnProperty('label_text')) { if (modification_dict.hasOwnProperty('label_text')) {
this.state.label_element.textContent = this.state.label_text; this.props.label_element.textContent = this.state.label_text;
} }
this.state.label_element.setAttribute('for', gadget.state.scope); this.props.label_element.setAttribute('for', gadget.state.scope);
if (modification_dict.hasOwnProperty('css_class') && this.state.css_class) { if (modification_dict.hasOwnProperty('css_class') && this.state.css_class) {
css_class = this.state.css_class.split(' '); css_class = this.state.css_class.split(' ');
...@@ -129,7 +133,7 @@ ...@@ -129,7 +133,7 @@
if (modification_dict.hasOwnProperty('error_text')) { if (modification_dict.hasOwnProperty('error_text')) {
// first remove old errors // first remove old errors
span = this.state.container_element.lastElementChild; span = this.props.container_element.lastElementChild;
if ((span !== null) && (span.tagName.toLowerCase() !== 'span')) { if ((span !== null) && (span.tagName.toLowerCase() !== 'span')) {
span = null; span = null;
} }
...@@ -138,42 +142,42 @@ ...@@ -138,42 +142,42 @@
if (span === null) { if (span === null) {
span = document.createElement('span'); span = document.createElement('span');
span.textContent = this.state.error_text; span.textContent = this.state.error_text;
this.state.container_element.appendChild(span); this.props.container_element.appendChild(span);
} else { } else {
span.textContent = this.state.error_text; span.textContent = this.state.error_text;
} }
} else if (span !== null) { } else if (span !== null) {
this.state.container_element.removeChild(span); this.props.container_element.removeChild(span);
} }
} }
// Remove/add label_element from DOM // Remove/add label_element from DOM
if (modification_dict.hasOwnProperty('label')) { if (modification_dict.hasOwnProperty('label')) {
if (this.state.label === true) { if (this.state.label === true) {
this.state.container_element.insertBefore(this.state.label_element, this.state.container_element.firstChild); this.props.container_element.insertBefore(this.props.label_element, this.props.container_element.firstChild);
} else { } else {
this.state.container_element.removeChild(this.state.label_element); this.props.container_element.removeChild(this.props.label_element);
} }
} }
if (modification_dict.hasOwnProperty('options')) { if (modification_dict.hasOwnProperty('options')) {
if (this.state.field_url) { if (this.state.field_url) {
return new RSVP.Queue() if (modification_dict.hasOwnProperty('field_url')) {
.push(function () { //if (!modification_dict.hasOwnProperty('first_call')) {
if (modification_dict.hasOwnProperty('field_url')) { gadget.props.container_element.removeChild(
return gadget.declareGadget(gadget.state.field_url, { gadget.props.container_element.querySelector('div')
scope: SCOPE );
}) //}
.push(function (field_gadget) { new_div = document.createElement('div');
gadget.state.container_element.removeChild( gadget.props.container_element.appendChild(new_div);
gadget.state.container_element.querySelector('div') queue = gadget.declareGadget(gadget.state.field_url, {
); scope: SCOPE,
gadget.state.container_element.appendChild(field_gadget.element); element: new_div
return field_gadget; });
}); } else {
} queue = gadget.getDeclaredGadget(SCOPE);
return gadget.getDeclaredGadget(SCOPE); }
}) return queue
.push(function (field_gadget) { .push(function (field_gadget) {
return field_gadget.render(gadget.state.options); return field_gadget.render(gadget.state.options);
}); });
...@@ -224,7 +228,7 @@ ...@@ -224,7 +228,7 @@
}) })
.declareJob('deferErrorTextRender', function (error_text) { .declareJob('deferErrorTextRender', function (error_text) {
return this.changeState({error_text: error_text}); return this.changeState({first_call: true, error_text: error_text});
}); });
}(window, document, rJS, RSVP)); }(window, document, rJS, RSVP));
\ No newline at end of file
...@@ -230,7 +230,7 @@ ...@@ -230,7 +230,7 @@
</item> </item>
<item> <item>
<key> <string>serial</string> </key> <key> <string>serial</string> </key>
<value> <string>966.41667.10868.48810</string> </value> <value> <string>967.36537.34665.7628</string> </value>
</item> </item>
<item> <item>
<key> <string>state</string> </key> <key> <string>state</string> </key>
...@@ -248,7 +248,7 @@ ...@@ -248,7 +248,7 @@
</tuple> </tuple>
<state> <state>
<tuple> <tuple>
<float>1523002550.46</float> <float>1526396024.69</float>
<string>UTC</string> <string>UTC</string>
</tuple> </tuple>
</state> </state>
......
...@@ -31,22 +31,20 @@ ...@@ -31,22 +31,20 @@
var element = this.element, var element = this.element,
gadget = this, gadget = this,
url, url,
result; result,
new_div;
if (modification_dict.hasOwnProperty('editable')) { if (modification_dict.hasOwnProperty('editable')) {
if (gadget.state.editable) { if (gadget.state.editable) {
url = 'gadget_html5_input.html'; url = 'gadget_html5_input.html';
} else { } else {
url = 'gadget_html5_element.html'; url = 'gadget_html5_element.html';
} }
result = this.declareGadget(url, {scope: 'sub'}) while (element.firstChild) {
.push(function (input) { element.removeChild(element.firstChild);
// Clear first to DOM, append after to reduce flickering/manip }
while (element.firstChild) { new_div = document.createElement('div');
element.removeChild(element.firstChild); element.appendChild(new_div);
} result = this.declareGadget(url, {scope: 'sub', element: new_div});
element.appendChild(input.element);
return input;
});
} else { } else {
result = this.getDeclaredGadget('sub'); result = this.getDeclaredGadget('sub');
} }
......
...@@ -230,7 +230,7 @@ ...@@ -230,7 +230,7 @@
</item> </item>
<item> <item>
<key> <string>serial</string> </key> <key> <string>serial</string> </key>
<value> <string>965.12118.35525.1655</string> </value> <value> <string>965.41976.11439.18449</string> </value>
</item> </item>
<item> <item>
<key> <string>state</string> </key> <key> <string>state</string> </key>
...@@ -248,7 +248,7 @@ ...@@ -248,7 +248,7 @@
</tuple> </tuple>
<state> <state>
<tuple> <tuple>
<float>1517929158.68</float> <float>1526396915.47</float>
<string>UTC</string> <string>UTC</string>
</tuple> </tuple>
</state> </state>
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment