[ADD] display groups in a most dreadful manner, need to cleanup the generation functi...
authorXavier Morel <xmo@openerp.com>
Tue, 10 May 2011 11:07:23 +0000 (13:07 +0200)
committerXavier Morel <xmo@openerp.com>
Tue, 10 May 2011 11:07:23 +0000 (13:07 +0200)
bzr revid: xmo@openerp.com-20110510110723-ic13ty3rs92zg9z9

addons/base/static/src/js/data.js
addons/base/static/src/js/list.js

index bb190e0..c3683ed 100644 (file)
@@ -37,7 +37,19 @@ openerp.base.DataGroup =  openerp.base.Controller.extend( /** @lends openerp.bas
                 group_by_fields: this.group_by
             }, function () { }).then(function (response) {
                 self.groups = response.result;
-                d.resolveWith(self, [response.result]);
+                // read_group results are annoying: they use the name of the
+                // field grouped on to hold the value and the count, so no
+                // generic access to those values is possible.
+                // Alias them to `value` and `length`.
+                d.resolveWith(self, [_(response.result).map(function (group) {
+                    var field_name = self.group_by[0];
+                    return _.extend({}, group, {
+                        // provide field used for grouping
+                        grouped_on: field_name,
+                        length: group[field_name + '_count'],
+                        value: group[field_name]
+                    });
+                })]);
             }, function () {
                 d.rejectWith.apply(d, self, [arguments]);
             });
@@ -52,9 +64,14 @@ openerp.base.DataGroup =  openerp.base.Controller.extend( /** @lends openerp.bas
      * :js:func:`~openerp.base.DataGroup.list` beforehand will likely result
      * in an error.
      *
+     * The resulting :js:class:`~openerp.base.DataGroup` or
+     * :js:class:`~openerp.base.DataSet` will be provided through the relevant
+     * callback function. In both functions, the current DataGroup will be
+     * provided as context (``this``)
+     *
      * @param {Number} index the index of the group to open in the datagroup's collection
-     * @param {Function} ifDataSet executed if the item results in a DataSet, provided with the dataset as parameter and as context
-     * @param {Function} ifDataGroup executed if the item results in a DataSet, provided with the datagroup as parameter and as context
+     * @param {Function} ifDataSet executed if the item results in a DataSet, provided with the new dataset as parameter
+     * @param {Function} ifDataGroup executed if the item results in a DataSet, provided with the new datagroup as parameter
      */
     get: function (index, ifDataSet, ifDataGroup) {
         var group = this.groups[index];
index f4a2e48..87a59f0 100644 (file)
@@ -61,7 +61,11 @@ openerp.base.ListView = openerp.base.Controller.extend(
             columns: this.columns,
             rows: this.rows
         });
-        $(this.list).bind({
+        this.groups = new openerp.base.ListView.Groups({
+            options: this.options,
+            columns: this.columns
+        });
+        $([this.list, this.groups]).bind({
             'selected': function (e, selection) {
                 self.$element.find('#oe-list-delete')
                     .toggle(!!selection.length);
@@ -291,16 +295,10 @@ openerp.base.ListView = openerp.base.Controller.extend(
             self.dataset.context = results.context;
             self.dataset.domain = results.domain;
             if (results.group_by.length) {
-                var group = new openerp.base.DataGroup(
+                self.groups.datagroup = new openerp.base.DataGroup(
                         self.session, results.group_by, self.dataset);
-                group.list().then(function (lst) {
-                    console.log(this, lst);
-                    this.get(0, function () {
-                        console.log('set', this);
-                    }, function () {
-                        console.log('group', this);
-                    });
-                });
+                self.$element.html(self.groups.render());
+                return;
             }
             return self.do_reload();
         });
@@ -476,7 +474,55 @@ openerp.base.ListView.List = Class.extend(
     // drag and drop
     // editable?
 });
-
+openerp.base.ListView.Groups = Class.extend(
+    /** @lends openerp.base.ListView.Groups# */{
+    /**
+     * Grouped display for the ListView. Handles basic DOM events and interacts
+     * with the :js:class:`~openerp.base.DataGroup` bound to it.
+     *
+     * Provides events similar to those of
+     * :js:class:`~openerp.base.ListView.List`
+     */
+    init: function (opts) {
+        this.options = opts.options;
+        this.columns = opts.columns;
+        this.datagroup = {};
+    },
+    make_level: function (datagroup) {
+        var self = this, $root = $('<dl>');
+        datagroup.list().then(function (list) {
+            _(list).each(function (group, index) {
+                var $title = $('<dt>')
+                    .text(group.grouped_on + ': ' + group.value + ' (' + group.length + ')')
+                    .appendTo($root);
+                $title.click(function () {
+                    datagroup.get(index, function (new_dataset) {
+                        var $content = $('<ul>').appendTo(
+                            $('<dd>').insertAfter($title));
+                        new_dataset.read_slice([], null, null, function (records) {
+                            _(records).each(function (record) {
+                                $('<li>')
+                                    .appendTo($content)
+                                    .text(_(record).map(function (value, key) {
+                                        return key + ': ' + value;
+                                }).join(', '));
+                            });
+                        });
+                    }, function (new_datagroup) {
+                        console.log(new_datagroup);
+                        $('<dd>')
+                            .insertAfter($title)
+                            .append(self.make_level(new_datagroup));
+                    });
+                });
+            });
+        });
+        return $root;
+    },
+    render: function () {
+        return this.make_level(this.datagroup);
+    }
+});
 openerp.base.TreeView = openerp.base.Controller.extend({
 });