Commit 56f24826 authored by Sven Franck's avatar Sven Franck

erp5_officejs_afs_directory: rewrite directory to not rely on ES6 code

parent c37b5c19
/*global window, rJS, RSVP, Handlebars, URI, console, jIO, document */ /*global window, rJS, RSVP, Handlebars, jIO, document */
/*jslint nomen: true, indent: 2, maxerr: 3 */ /*jslint indent: 2, nomen: true, maxlen: 80*/
(function (window, rJS, RSVP, Handlebars, document) {
(function (window, rJS, RSVP, Handlebars, URI, document) {
"use strict"; "use strict";
var gadget_klass = rJS(window), /////////////////////////////////////////////////////////////////
// some variables
/////////////////////////////////////////////////////////////////
var VIEW = "?page=afs_software&view=view",
gadget_klass = rJS(window),
templater = gadget_klass.__template_element, templater = gadget_klass.__template_element,
source = templater.getElementById("frontpage-template") source = templater.getElementById("frontpage-template").innerHTML,
.innerHTML,
template = Handlebars.compile(source); template = Handlebars.compile(source);
Handlebars.registerPartial( Handlebars.registerPartial(
"list-partial", "list-partial",
templater.getElementById("list-partial").innerHTML templater.getElementById("list-partial").innerHTML
); );
gadget_klass gadget_klass
/////////////////////////////////////////////////////////////////
// acquired methods
/////////////////////////////////////////////////////////////////
.declareAcquiredMethod("updateHeader", "updateHeader") .declareAcquiredMethod("updateHeader", "updateHeader")
.declareAcquiredMethod("jio_allDocs", "jio_allDocs") .declareAcquiredMethod("jio_allDocs", "jio_allDocs")
...@@ -23,9 +31,7 @@ ...@@ -23,9 +31,7 @@
///////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////
.declareMethod("render", function () { .declareMethod("render", function () {
var gadget = this, var gadget = this,
masonry_container = gadget.element.querySelector( grid = gadget.element.querySelector('.ui-masonry-container');
'.ui-masonry-container'
);
return new RSVP.Queue() return new RSVP.Queue()
.push(function () { .push(function () {
...@@ -36,67 +42,64 @@ ...@@ -36,67 +42,64 @@
}) })
]); ]);
}) })
.push(function (my_response_list) { .push(function (result_list) {
return RSVP.all([ return result_list[0].render();
gadget.jio_allDocs({
select_list: ['category_list'],
query: 'portal_type:"software"'
}),
my_response_list[0].render()
]);
}) })
.push(function (my_response_list) { .push(function (html_content) {
var softwares = my_response_list[0].data.rows, var banner = document.createRange()
obj, .createContextualFragment(html_content || "");
// get categories and flatten array of category arrays gadget.element.insertBefore(banner, grid);
categories = softwares return gadget.jio_allDocs({
.map((obj) => obj.value.category_list) select_list: ['category_list', 'title', 'publisher'],
.reduce((cur, prev) => cur.concat(prev)), query: 'portal_type:"software"'
// remove duplicates (case sensitive!)
unique_categories = Array.from(new Set(categories)),
// kudos: https://davidwalsh.name/convert-html-stings-dom-nodes
banner = document.createRange()
.createContextualFragment(my_response_list[1] || "");
gadget.element.insertBefore(banner, masonry_container);
return RSVP.all(unique_categories);
})
.push(function (categories) {
var softwares_by_category = categories.map(function (category) {
return gadget.jio_allDocs({
select_list: [
'title',
'publisher',
'logo'
],
query: 'category_list:"%' + category + '%" AND portal_type:"software"'
})
.push(function (softwares) {
softwares.data.rows.map(function (sw) {
// XXX hardcoded page and view
sw.value.href = "#/" + sw.id + "?page=afs_software&view=view";
});
return {
category: category,
softwares: softwares.data.rows
};
});
}); });
return RSVP.all(softwares_by_category);
}) })
.push(function (result) { .push(function (result_list) {
var content; var software_list = result_list.data.rows,
global_category_list = [],
config;
// list of unique list of categories
if (software_list.length > 0) {
global_category_list = software_list.reduce(function (list, dict) {
var software = dict.value;
if (software.category_list.length > 0) {
software.category_list.map(function (category) {
if (list.indexOf(category) === -1) {
list.push(category);
}
});
}
return list;
}, []);
}
// populate list with software
config = global_category_list.reduce(function (result, category) {
var category_entry = {"category": category, "software_list": []},
len = software_list.length,
match_list,
software,
i;
// reverse sort categories by number of softwares for (i = 0; i < len; i += 1) {
result.sort( (a, b) => b.softwares.length - a.softwares.length ); software = software_list[i].value;
match_list = software.category_list;
if (match_list && match_list.length > 0) {
if (match_list.indexOf(category) > -1) {
category_entry.software_list.push({
"title": software.title,
"href": "#/" + software_list[i].id + VIEW
});
}
}
}
result.push(category_entry);
return result;
}, []);
content = template(result); grid.innerHTML = template(config);
masonry_container.innerHTML = content;
}); });
}); });
}(window, rJS, RSVP, Handlebars, URI, document)); }(window, rJS, RSVP, Handlebars, document));
...@@ -228,7 +228,7 @@ ...@@ -228,7 +228,7 @@
</item> </item>
<item> <item>
<key> <string>serial</string> </key> <key> <string>serial</string> </key>
<value> <string>959.46981.52420.64187</string> </value> <value> <string>961.9243.54613.9181</string> </value>
</item> </item>
<item> <item>
<key> <string>state</string> </key> <key> <string>state</string> </key>
...@@ -246,7 +246,7 @@ ...@@ -246,7 +246,7 @@
</tuple> </tuple>
<state> <state>
<tuple> <tuple>
<float>1496170069.03</float> <float>1501772325.68</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