diff --git a/bemade_hierarchical_tree_view/rng/hlist.rng b/bemade_hierarchical_tree_view/rng/hlist.rng index 05fa01e..1e0ccb3 100644 --- a/bemade_hierarchical_tree_view/rng/hlist.rng +++ b/bemade_hierarchical_tree_view/rng/hlist.rng @@ -9,6 +9,7 @@ + diff --git a/bemade_hierarchical_tree_view/static/src/js/hlist_model.js b/bemade_hierarchical_tree_view/static/src/js/hlist_model.js index 90bd9a9..26c819d 100644 --- a/bemade_hierarchical_tree_view/static/src/js/hlist_model.js +++ b/bemade_hierarchical_tree_view/static/src/js/hlist_model.js @@ -8,72 +8,11 @@ var HListModel = ListModel.extend({ * @param {string} params.parentField */ init: function (parent, params) { + var self = this; this._super.apply(this, arguments); - this.parentField = params.parentField; - }, - /** - * - * @override - * @private - */ - _readGroup: function (list, options) { - var self = this; - return this._super(list, options).then(function (result) { - return self._readHierarchy(list).then(_.constant(result)); - }); - }, - /** - * Fetches hierarchy specific fields on the parent field relation and stores them - * in the column datapoint in a special key 'hierarchyData'. - * Data for the hierarchy are fetched in batch for all hierarchies, to avoid - * multiple calls. - * - * @param {Object} list - * @private - * @returns {Promise} - */ - _readHierarchy (list) { - const self = this; - const parentFieldName = self.parentField; - const parentField = list.fields[parentFieldName]; - /* Each record should have only one parent */ - if (parentField.type !== 'many2one') { - return Promise.resolve(); - } - const hierarchyIds = _.reduce(list.data, function (hierarchyIds, id) { - const resId = self.get(id, { raw: true }).res_id; - if (resId) { - hierarchyIds.push(resId); - } - return hierarchyIds; - }, []); - let prom; - if (hierarchyIds.length) { - prom = this._rpc({ - model: list.model, - method: 'read', - args: [hierarchyIds, self.viewFields], - context: list.context, - }); - } - return Promise.resolve(prom).then(function (result) { - _.each(list.data, function(id) { - let dp = self.localData[id]; - let hierarchyData = result && _.findWhere(result, { - id: dp.res_id, - }); - let hierarchyDp = self._makeDataPoint({ - context: dp.context, - data: hierarchyData, - fields: list.fields, - fieldsInfo: list.fieldsInfo, - modelName: list.model, - parentID: dp.id, - res_id: dp.res_id, - viewType: 'list', - }); - }); - }); + self.parentField = params.parentField; + self.loadParams.domain = _.union(self.loadParams.domain, + [[self.parentField, '=', false]]) }, }) diff --git a/bemade_hierarchical_tree_view/static/src/js/hlist_renderer.js b/bemade_hierarchical_tree_view/static/src/js/hlist_renderer.js index 9f76705..5c2110a 100644 --- a/bemade_hierarchical_tree_view/static/src/js/hlist_renderer.js +++ b/bemade_hierarchical_tree_view/static/src/js/hlist_renderer.js @@ -2,37 +2,57 @@ import {ListRenderer} from 'web.ListRenderer' +const toggleClass = 'o_hlist_toggle'; +const toggleIconClass = 'fa-sitemap'; + var HierarchicalListRenderer = ListRenderer.extend({ className: 'o_hlist_view', events: _.extend({}, ListRenderer.prototype.events, { - 'click .o_hierarchy_parent': '_onToggleHierarchyGroup', + 'click o_hlist_toggle': '_onToggleHierarchyGroup', }), + /** + * @param {string} params.parentField + * @param {string} params.childrenField + */ init: function (parent, state, params) { this._super.apply(this, arguments); - }, - _renderBody: function () { - this._super.apply(this); - }, - _renderHierarchy(data, hierarchyLevel) { + this.parentField = params.parentField; + this.childrenField = params.childrenField; + }, // TODO: add header column on left side for toggle + /** + * @override + * @private + * @param {Object} record + * @returns {jQueryElement} a element + */ + _renderRow: function (record) { var self = this; - hierarchyLevel = hierarchyLevel || 0; - var result = []; - var $tbody = $(''); - _.each(data, function (hierarchyGroup) { - if (!$tbody) { - $tbody = $(''); + var $tr = this._super.apply(this, arguments) + if (this.childrenField in record.data) { + let numChildren = record.data[this.childrenField].length + if (numChildren > 0) { + $td = $('', {class: toggleClass}).append( + $('', {class: toggleIconClass, text: numChildren}) + ) + } else { + $tr.prepend($('')) } - $tbody.append(self._renderHierarchyRow(hierarchyGroup, hierarchyLevel)); - if (hierarchyGroup.data.length) { - result.push($tbody); - result = result.concat(self._renderHierarchyParent(hierarchyGroup, - hierarchyLevel)); - $tbody = null; - } - }) + } + if (record.data.child_ids && record.data.child_ids.length) { + $tr.prepend(this._renderHierarchyToggle(record)); + } else { + $tr.prepend($('')) + } }, - _renderHierarchyParent(hierarchyGroup, hierarchyLevel) { + /** + * + * @param record + * @private + * @returns {jQueryElement} a element + */ + _renderHierarchyToggle: function (record) { + let numChildren = record.data[this.childrenField].length } }) \ No newline at end of file diff --git a/bemade_hierarchical_tree_view/static/src/js/hlist_view.js b/bemade_hierarchical_tree_view/static/src/js/hlist_view.js index 0ec0497..f5b535d 100644 --- a/bemade_hierarchical_tree_view/static/src/js/hlist_view.js +++ b/bemade_hierarchical_tree_view/static/src/js/hlist_view.js @@ -30,7 +30,8 @@ var HListView = ListView.extend({ * @param {Object} viewInfo * @param {Object} params * @param {boolean} params.hasActionMenus - * @param {string} [params.parentField] + * @param {string} params.parentField + * @param {string} params.childrenField * @param {boolean} [params.hasSelectors=true] */ init: function(viewInfo, params) { @@ -41,8 +42,13 @@ var HListView = ListView.extend({ _updateMVCParams: function() { this._super.apply(this, arguments); var attrs = this.arch.attrs; - this.loadParams.parentField = attrs.parentField; - this.modelParams.parentField = attrs.parentField; + + this.loadParams.parentField = attrs.parent_field; + + this.modelParams.parentField = attrs.parent_field; + + this.rendererParams.parentField = this.modelParams.parentField + this.rendererParams.childrenField = attrs.childrenField } });