Commit 97140203 authored by Romain Courteaud's avatar Romain Courteaud

[erp5_web_renderjs_ui] SortEditor: reduce number of event listeners

parent f87a4346
...@@ -13,8 +13,6 @@ ...@@ -13,8 +13,6 @@
<!-- custom script --> <!-- custom script -->
<script src="gadget_erp5_sort_editor.js"></script> <script src="gadget_erp5_sort_editor.js"></script>
<script id="sort-item-template" type="text/x-handlebars-template"> <script id="sort-item-template" type="text/x-handlebars-template">
<button type="submit" class="ui-icon ui-btn ui-btn-inline ui-icon-minus ui-icon-shadow"></button> <button type="submit" class="ui-icon ui-btn ui-btn-inline ui-icon-minus ui-icon-shadow"></button>
<div class="sort_item ui-controlgroup-controls" > <div class="sort_item ui-controlgroup-controls" >
...@@ -41,35 +39,27 @@ ...@@ -41,35 +39,27 @@
</div> </div>
</script> </script>
<script id="sort-template" type="text/x-handlebars-template"> <script id="sort-template" type="text/x-handlebars-template">
<div class="ui-panel-inner"> <div class="ui-panel-inner">
<div data-role="header" role="banner" class="ui-header ui-bar-inherit"> <div data-role="header" role="banner" class="ui-header ui-bar-inherit">
<div class="ui-controlgroup ui-controlgroup-horizontal ui-btn-left"> <div class="ui-controlgroup ui-controlgroup-horizontal ui-btn-right">
<div class="ui-controlgroup-controls"> <div class="ui-controlgroup-controls">
<form class="delete"> <button data-rel="save" data-i18n="Submit" type="submit" class="submit responsive ui-last-child ui-btn ui-btn-icon-left ui-icon-check">Submit</button>
<button data-i18n="Close" data-rel="close" type="submit" class="close responsive ui-first-child ui-btn ui-btn-icon-left ui-icon-times">Close</button>
</form>
</div> </div>
</div> </div>
<h1 class="ui-title" role="heading" data-i18n="Sort Editor" aria-level="1">Sort Editor</h1> <h1 class="ui-title" role="heading" data-i18n="Sort Editor" aria-level="1">Sort Editor</h1>
<div class="ui-controlgroup ui-controlgroup-horizontal ui-btn-right">
<div class="ui-controlgroup ui-controlgroup-horizontal ui-btn-left">
<div class="ui-controlgroup-controls"> <div class="ui-controlgroup-controls">
<form class="submit"> <button data-i18n="Close" data-rel="close" type="submit" class="close responsive ui-first-child ui-btn ui-btn-icon-left ui-icon-times">Close</button>
<button data-rel="save" data-i18n="Submit" type="submit" class="submit responsive ui-last-child ui-btn ui-btn-icon-left ui-icon-check">Submit</button>
</form>
</div> </div>
</div> </div>
</div> </div>
<section class="ui-body-c ui-content-section"> <section class="ui-body-c ui-content-section">
<div class="sort_item_container ui-controlgroup ui-corner_all"> <div class="sort_item_container ui-controlgroup ui-corner_all"></div>
</div>
<form class="plus">
<button type="submit" class="plus ui-btn-c ui-override-theme ui-btn ui-icon-plus ui-btn-icon-left ui-corner-all">Add Criteria</button> <button type="submit" class="plus ui-btn-c ui-override-theme ui-btn ui-icon-plus ui-btn-icon-left ui-corner-all">Add Criteria</button>
</form>
</section> </section>
</div> </div>
</script> </script>
......
...@@ -234,7 +234,7 @@ ...@@ -234,7 +234,7 @@
</item> </item>
<item> <item>
<key> <string>serial</string> </key> <key> <string>serial</string> </key>
<value> <string>952.34318.26924.28552</string> </value> <value> <string>955.38127.32735.11195</string> </value>
</item> </item>
<item> <item>
<key> <string>state</string> </key> <key> <string>state</string> </key>
...@@ -252,7 +252,7 @@ ...@@ -252,7 +252,7 @@
</tuple> </tuple>
<state> <state>
<tuple> <tuple>
<float>1468413387.26</float> <float>1480342874.92</float>
<string>UTC</string> <string>UTC</string>
</tuple> </tuple>
</state> </state>
......
/*jslint indent: 2, maxerr: 3, nomen: true */ /*jslint indent: 2, maxerr: 3, nomen: true */
/*global window, document, rJS, RSVP, Handlebars, loopEventListener*/ /*global window, document, rJS, RSVP, Handlebars*/
(function (window, document, rJS, RSVP, Handlebars, loopEventListener) { (function (window, document, rJS, RSVP, Handlebars) {
"use strict"; "use strict";
var gadget_klass = rJS(window), var gadget_klass = rJS(window),
sort_item_source = gadget_klass.__template_element template_element = gadget_klass.__template_element,
sort_item_template = Handlebars.compile(template_element
.getElementById("sort-item-template") .getElementById("sort-item-template")
.innerHTML, .innerHTML),
sort_item_template = Handlebars.compile(sort_item_source), sort_template = Handlebars.compile(template_element
sort_source = gadget_klass.__template_element
.getElementById("sort-template") .getElementById("sort-template")
.innerHTML, .innerHTML);
sort_template = Handlebars.compile(sort_source);
Handlebars.registerHelper('equal', function (left_value, right_value, options) { Handlebars.registerHelper('equal', function (left_value, right_value, options) {
if (arguments.length < 3) { if (arguments.length < 3) {
...@@ -24,17 +24,16 @@ ...@@ -24,17 +24,16 @@
function createSortItemTemplate(gadget, sort_value) { function createSortItemTemplate(gadget, sort_value) {
var sort_column_list = gadget.props.sort_column_list, var sort_column_list = gadget.state.sort_column_list,
sort_value_list = sort_value || [], sort_value_list = sort_value || [],
option_list = [], option_list = [],
i; i;
for (i = 0; i < sort_column_list.length; i += 1) { for (i = 0; i < sort_column_list.length; i += 1) {
option_list.push({ option_list.push({
"text": sort_column_list[i][1], text: sort_column_list[i][1],
"value": sort_column_list[i][0], value: sort_column_list[i][0],
"selected_option": sort_value_list[0] selected_option: sort_value_list[0]
}); });
} }
...@@ -44,97 +43,88 @@ ...@@ -44,97 +43,88 @@
})); }));
} }
gadget_klass
rJS(window)
/////////////////////////////////////////////////////////////////
// ready
/////////////////////////////////////////////////////////////////
// Init local properties
.ready(function (g) {
g.props = {};
})
.ready(function (g) {
return g.getElement()
.push(function (element) {
g.props.element = element;
});
})
////////////////////////////////////////////// //////////////////////////////////////////////
// acquired method // acquired method
////////////////////////////////////////////// //////////////////////////////////////////////
.declareAcquiredMethod("translateHtml", "translateHtml") .declareAcquiredMethod("translateHtml", "translateHtml")
.declareAcquiredMethod("redirect", "redirect") .declareAcquiredMethod("redirect", "redirect")
.declareAcquiredMethod("trigger", "trigger") .declareAcquiredMethod("trigger", "trigger")
//////////////////////////////////////////////
// initialize the gadget content .onStateChange(function () {
//////////////////////////////////////////////
.declareMethod('render', function (options) {
var gadget = this;
gadget.props.sort_column_list = options.sort_column_list || [];
gadget.props.key = options.key;
gadget.props.sort_list = options.sort_list;
return new RSVP.Queue()
.push(function () {
var tmp = sort_template();
return gadget.translateHtml(tmp);
})
.push(function (translated_html) {
var tmp = document.createElement("div");
tmp.innerHTML = translated_html;
gadget.props.element.querySelector(".container").appendChild(tmp);
});
})
//////////////////////////////////////////////
.declareService(function () {
var gadget = this, var gadget = this,
i, div = document.createElement("div"),
list = []; container = gadget.element.querySelector(".container");
return new RSVP.Queue()
.push(function () { return gadget.translateHtml(sort_template())
for (i = 0; i < gadget.props.sort_list.length; i += 1) { .push(function (translated_html) {
if (gadget.props.sort_list[i]) { var i,
list.push(createSortItemTemplate(gadget, gadget.props.sort_list[i])); promise_list = [];
div.innerHTML = translated_html;
for (i = 0; i < gadget.state.sort_list.length; i += 1) {
if (gadget.state.sort_list[i]) {
promise_list.push(createSortItemTemplate(gadget, gadget.state.sort_list[i]));
} }
} }
return RSVP.all(list); return RSVP.all(promise_list);
}) })
.push(function (all_result) { .push(function (result_list) {
var div, var i,
container = gadget.props.element.querySelector(".sort_item_container"); subdiv,
for (i = 0; i < all_result.length; i += 1) { filter_item_container = div.querySelector('.sort_item_container');
div = document.createElement("div");
div.innerHTML = all_result[i]; for (i = 0; i < result_list.length; i += 1) {
container.appendChild(div); subdiv = document.createElement("div");
subdiv.innerHTML = result_list[i];
filter_item_container.appendChild(subdiv);
}
while (container.firstChild) {
container.removeChild(container.firstChild);
} }
container.appendChild(div);
}); });
}) })
.declareService(function () {
var gadget = this, .declareMethod('render', function (options) {
container = gadget.props.element.querySelector(".sort_item_container"); return this.changeState({
return loopEventListener( sort_column_list: options.sort_column_list || [],
gadget.props.element.querySelector(".sort_editor"), key: options.key,
"submit", sort_list: options.sort_list
false, });
function () {
var focused = document.activeElement;
container.removeChild(focused.parentElement);
}
);
}) })
.declareService(function () {
.onEvent('click', function (evt) {
var gadget = this; var gadget = this;
return loopEventListener(
gadget.props.element.querySelector(".submit"), if (evt.target.classList.contains('close')) {
"submit", evt.preventDefault();
false, return this.trigger();
function () { }
var sort_list = gadget.props.element.querySelectorAll(".sort_item"),
if (evt.target.classList.contains('plus')) {
evt.preventDefault();
return createSortItemTemplate(gadget)
.push(function (template) {
var tmp = document.createElement("div"),
container = gadget.element.querySelector(".sort_item_container");
tmp.innerHTML = template;
container.appendChild(tmp);
});
}
if (evt.target.classList.contains('ui-icon-minus')) {
evt.preventDefault();
evt.target.parentElement.parentElement.removeChild(evt.target.parentElement);
}
}, false, false)
.onEvent('submit', function () {
var gadget = this,
sort_list = gadget.element.querySelectorAll(".sort_item"),
sort_query = [], sort_query = [],
select_list, select_list,
sort_item, sort_item,
...@@ -148,47 +138,14 @@ ...@@ -148,47 +138,14 @@
select_list[1][select_list[1].selectedIndex].value]; select_list[1][select_list[1].selectedIndex].value];
} }
if (i === 0) { if (i === 0) {
options[gadget.props.key] = undefined; options[gadget.state.key] = undefined;
} else { } else {
options[gadget.props.key] = sort_query; options[gadget.state.key] = sort_query;
} }
return gadget.redirect({ return gadget.redirect({
command: 'store_and_change', command: 'store_and_change',
options: options options: options
}); });
}
);
})
.declareService(function () {
var gadget = this;
return loopEventListener(
gadget.props.element.querySelector(".plus"),
"submit",
false,
function () {
return new RSVP.Queue()
.push(function () {
return createSortItemTemplate(gadget);
})
.push(function (template) {
var tmp = document.createElement("div"),
container = gadget.props.element.querySelector(".sort_item_container");
tmp.innerHTML = template;
container.appendChild(tmp);
});
}
);
})
.declareService(function () {
var gadget = this;
return loopEventListener(
gadget.props.element.querySelector(".delete"),
"submit",
false,
function () {
return gadget.trigger();
}
);
}); });
}(window, document, rJS, RSVP, Handlebars, loopEventListener)); }(window, document, rJS, RSVP, Handlebars));
\ No newline at end of file \ 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>952.21277.40593.7202</string> </value> <value> <string>955.38160.33114.47854</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>1468403615.53</float> <float>1480344058.06</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