[imp] improved AbstractFormPopup
authorniv-openerp <nicolas.vanhoren@openerp.com>
Wed, 16 May 2012 13:01:42 +0000 (15:01 +0200)
committerniv-openerp <nicolas.vanhoren@openerp.com>
Wed, 16 May 2012 13:01:42 +0000 (15:01 +0200)
bzr revid: nicolas.vanhoren@openerp.com-20120516130142-quyvtv0s35uvbuug

addons/web/static/src/js/data.js
addons/web/static/src/js/view_form.js

index 29e2140..fdb15d5 100644 (file)
@@ -1079,41 +1079,44 @@ instance.web.ProxyDataSet = instance.web.DataSetSearch.extend({
         this.create_function = null;
         this.write_function = null;
         this.read_function = null;
+        this.default_get_function = null;
+        this.unlink_function = null;
     },
-    read_ids: function () {
+    read_ids: function (ids, fields, options) {
         if (this.read_function) {
-            return this.read_function.apply(null, arguments);
+            return this.read_function(ids, fields, options, this._super);
         } else {
             return this._super.apply(this, arguments);
         }
     },
     default_get: function(fields, options) {
-        return this._super(fields, options).then(this.on_default_get);
+        if (this.default_get_function) {
+            return this.default_get_function(fields, options, this._super);
+        } else {
+            return this._super.apply(this, arguments);
+        }
     },
-    on_default_get: function(result) {},
     create: function(data, callback, error_callback) {
-        this.on_create(data);
         if (this.create_function) {
-            return this.create_function(data, callback, error_callback);
+            return this.create_function(data, this._super).then(callback, error_callback);
         } else {
-            return $.async_when({"result": undefined}).then(callback);
+            return this._super.apply(this, arguments);
         }
     },
-    on_create: function(data) {},
-    write: function (id, data, options, callback) {
-        this.on_write(id, data);
+    write: function (id, data, options, callback, error_callback) {
         if (this.write_function) {
-            return this.write_function(id, data, options, callback);
+            return this.write_function(id, data, options, this._super).then(callback, error_callback);
         } else {
-            return $.async_when({"result": true}).then(callback);
+            return this._super.apply(this, arguments);
         }
     },
-    on_write: function(id, data) {},
     unlink: function(ids, callback, error_callback) {
-        this.on_unlink(ids);
-        return $.async_when({"result": true}).then(callback);
+        if (this.unlink_function) {
+            return this.unlink_function(ids, this._super).then(callback, error_callback);
+        } else {
+            return this._super.apply(this, arguments);
+        }
     },
-    on_unlink: function(ids) {}
 });
 
 instance.web.CompoundContext = instance.web.Class.extend({
index 7fdcc03..246a58d 100644 (file)
@@ -2970,7 +2970,11 @@ instance.web.form.One2ManyViewManager = instance.web.ViewManager.extend({
         var pop = new instance.web.form.FormOpenPopup(self.o2m.view);
         pop.show_element(self.o2m.field.relation, id, self.o2m.build_context(), {
             title: _t("Open: ") + self.name,
-            auto_write: false,
+            write_function: function(id, data, options) {
+                return self.o2m.dataset.write(id, data, {}).then(function() {
+                    self.o2m.reload_current_view();
+                });
+            },
             alternative_form_view: self.o2m.field.views ? self.o2m.field.views["form"] : undefined,
             parent_view: self.o2m.view,
             child_name: self.o2m.name,
@@ -2980,11 +2984,6 @@ instance.web.form.One2ManyViewManager = instance.web.ViewManager.extend({
             form_view_options: {'not_interactible_on_create':true},
             readonly: self.o2m.get("effective_readonly")
         });
-        pop.on_write.add(function(id, data) {
-            self.o2m.dataset.write(id, data, {}, function(r) {
-                self.o2m.reload_current_view();
-            });
-        });
     },
 });
 
@@ -3003,7 +3002,6 @@ instance.web.form.One2ManyListView = instance.web.ListView.extend({
         } else {
             var self = this;
             var pop = new instance.web.form.SelectCreatePopup(this);
-            pop.on_default_get.add(self.dataset.on_default_get);
             pop.select_element(
                 self.o2m.field.relation,
                 {
@@ -3036,7 +3034,11 @@ instance.web.form.One2ManyListView = instance.web.ListView.extend({
         var pop = new instance.web.form.FormOpenPopup(self.o2m.view);
         pop.show_element(self.o2m.field.relation, id, self.o2m.build_context(), {
             title: _t("Open: ") + self.name,
-            auto_write: false,
+            write_function: function(id, data) {
+                return self.o2m.dataset.write(id, data, {}, function(r) {
+                    self.o2m.reload_current_view();
+                });
+            },
             alternative_form_view: self.o2m.field.views ? self.o2m.field.views["form"] : undefined,
             parent_view: self.o2m.view,
             child_name: self.o2m.name,
@@ -3046,11 +3048,6 @@ instance.web.form.One2ManyListView = instance.web.ListView.extend({
             form_view_options: {'not_interactible_on_create':true},
             readonly: self.o2m.get("effective_readonly")
         });
-        pop.on_write.add(function(id, data) {
-            self.o2m.dataset.write(id, data, {}, function(r) {
-                self.o2m.reload_current_view();
-            });
-        });
     },
     do_button_action: function (name, id, callback) {
         var self = this;
@@ -3365,6 +3362,7 @@ instance.web.form.AbstractFormPopup = instance.web.OldWidget.extend({
     template: "AbstractFormPopup.render",
     /**
      *  options:
+     *  -readonly: only applicable when not in creation mode, default to false
      */
     init_popup: function(model, row_id, domain, context, options) {
         this.row_id = row_id;
@@ -3375,6 +3373,34 @@ instance.web.form.AbstractFormPopup = instance.web.OldWidget.extend({
         _.defaults(this.options, {
         });
     },
+    init_dataset: function() {
+        var self = this;
+        this.dataset = new instance.web.ProxyDataSet(this, this.model, this.context);
+        this.dataset.read_function = this.options.read_function;
+        this.dataset.create_function = function(data, sup) {
+            var fct = self.options.create_function || sup;
+            return fct.call(this, data).then(function(r) {
+                self.created_elements.push(r.result);
+            });
+        };
+        this.dataset.write_function = function(id, data, options, sup) {
+            var fct = self.options.write_function || sup;
+            return sup.call(this, id, data, options).then(self.on_write_completed);
+        };
+        this.dataset.parent_view = this.options.parent_view;
+        this.dataset.child_name = this.options.child_name;
+    },
+    display_popup: function() {
+        this.renderElement();
+        instance.web.form.dialog(this.$element, {
+            close: function() {
+                self.check_exit();
+            },
+            title: this.options.title || ""
+        });
+        this.start();
+    },
+    on_write_completed: function() {},
     setup_form_view: function() {
         var self = this;
         var options = _.clone(self.options.form_view_options) || {};
@@ -3442,43 +3468,22 @@ instance.web.form.SelectCreatePopup = instance.web.form.AbstractFormPopup.extend
      * - read_function
      */
     select_element: function(model, options, domain, context) {
+        this.init_popup(model, null, domain, context, options);
         var self = this;
-        this.init_popup(model, null, domain, context, _.extend({}, options, {create_mode:true}));
         _.defaults(this.options, {
-            "initial_view": "search",
-            "create_function": function() {
+            initial_view: "search",
+            create_function: function() {
                 return self.create_row.apply(self, arguments);
             },
             read_function: null,
         });
         this.initial_ids = this.options.initial_ids;
         this.created_elements = [];
-        this.renderElement();
-        instance.web.form.dialog(this.$element, {
-            close: function() {
-                self.check_exit();
-            },
-            title: options.title || ""
-        });
-        this.start();
+        this.display_popup();
     },
     start: function() {
-        this._super();
         var self = this;
-        this.dataset = new instance.web.ProxyDataSet(this, this.model,
-            this.context);
-        this.dataset.create_function = function() {
-            return self.options.create_function.apply(null, arguments).then(function(r) {
-                self.created_elements.push(r.result);
-            });
-        };
-        this.dataset.write_function = function() {
-            return self.write_row.apply(self, arguments);
-        };
-        this.dataset.read_function = this.options.read_function;
-        this.dataset.parent_view = this.options.parent_view;
-        this.dataset.child_name = this.options.child_name;
-        this.dataset.on_default_get.add(this.on_default_get);
+        this.init_dataset();
         if (this.options.initial_view == "search") {
             self.rpc('/web/session/eval_domain_and_context', {
                 domains: [],
@@ -3565,13 +3570,6 @@ instance.web.form.SelectCreatePopup = instance.web.form.AbstractFormPopup.extend
         wdataset.child_name = this.options.child_name;
         return wdataset.create.apply(wdataset, arguments);
     },
-    write_row: function() {
-        var self = this;
-        var wdataset = new instance.web.DataSetSearch(this, this.model, this.context, this.domain);
-        wdataset.parent_view = this.options.parent_view;
-        wdataset.child_name = this.options.child_name;
-        return wdataset.write.apply(wdataset, arguments);
-    },
     on_select_elements: function(element_ids) {
     },
     on_click_element: function(ids) {
@@ -3596,9 +3594,8 @@ instance.web.form.SelectCreatePopup = instance.web.form.AbstractFormPopup.extend
         if (this.created_elements.length > 0) {
             this.on_select_elements(this.created_elements);
         }
-        this.destroy();
+        this._super();
     },
-    on_default_get: function(res) {}
 });
 
 instance.web.form.SelectCreateListView = instance.web.ListView.extend({
@@ -3623,50 +3620,26 @@ instance.web.form.FormOpenPopup = instance.web.form.AbstractFormPopup.extend(/**
     /**
      * options:
      * - alternative_form_view
-     * - auto_write (default true)
+     * - write_function
      * - read_function
      * - parent_view
      * - child_name
      * - form_view_options
-     * - readonly
      */
     show_element: function(model, row_id, context, options) {
-        this.init_popup(model, row_id, [], context,  _.extend({}, options, {create_mode:false}));
+        this.init_popup(model, row_id, [], context,  options);
         _.defaults(this.options, {
-            "auto_write": true,
-        });
-        this.renderElement();
-        instance.web.dialog(this.$element, {
-            title: options.title || '',
-            modal: true,
-            width: 960,
-            height: 600
+            auto_write: true,
         });
-        this.start();
+        this.display_popup();
     },
     start: function() {
         this._super();
-        this.dataset = new instance.web.ProxyDataSet(this, this.model, this.context);
-        this.dataset.read_function = this.options.read_function;
+        this.init_dataset();
         this.dataset.ids = [this.row_id];
         this.dataset.index = 0;
-        this.dataset.parent_view = this.options.parent_view;
-        this.dataset.child_name = this.options.child_name;
-        this.dataset.on_write.add(this.write_row);
         this.setup_form_view();
     },
-    write_row: function(id, data) {
-        if (!this.options.auto_write)
-            return;
-        var self = this;
-        var wdataset = new instance.web.DataSetSearch(this, this.model, this.context, this.domain);
-        wdataset.parent_view = this.options.parent_view;
-        wdataset.child_name = this.options.child_name;
-        wdataset.write(id, data, {}, function(r) {
-            self.on_write_completed();
-        });
-    },
-    on_write_completed: function() {},
 });
 
 instance.web.form.FieldReference = instance.web.form.AbstractField.extend(_.extend({}, instance.web.form.ReinitializeFieldMixin, {