[IMP] client looks
[odoo/odoo.git] / addons / web / static / src / js / view_form.js
1 openerp.web.form = function (openerp) {
2
3 var _t = openerp.web._t;
4 var QWeb = openerp.web.qweb;
5
6 openerp.web.views.add('form', 'openerp.web.FormView');
7 openerp.web.FormView = openerp.web.View.extend( /** @lends openerp.web.FormView# */{
8     /**
9      * Indicates that this view is not searchable, and thus that no search
10      * view should be displayed (if there is one active).
11      */
12     searchable: false,
13     readonly : false,
14     form_template: "FormView",
15     identifier_prefix: 'formview-',
16     /**
17      * @constructs openerp.web.FormView
18      * @extends openerp.web.View
19      *
20      * @param {openerp.web.Session} session the current openerp session
21      * @param {openerp.web.DataSet} dataset the dataset this view will work with
22      * @param {String} view_id the identifier of the OpenERP view object
23      *
24      * @property {openerp.web.Registry} registry=openerp.web.form.widgets widgets registry for this form view instance
25      */
26     init: function(parent, dataset, view_id, options) {
27         this._super(parent);
28         this.set_default_options(options);
29         this.dataset = dataset;
30         this.model = dataset.model;
31         this.view_id = view_id || false;
32         this.fields_view = {};
33         this.widgets = {};
34         this.widgets_counter = 0;
35         this.fields = {};
36         this.datarecord = {};
37         this.show_invalid = true;
38         this.default_focus_field = null;
39         this.default_focus_button = null;
40         this.registry = this.readonly ? openerp.web.form.readonly : openerp.web.form.widgets;
41         this.has_been_loaded = $.Deferred();
42         this.$form_header = null;
43         this.translatable_fields = [];
44         _.defaults(this.options, {"always_show_new_button": true,
45             "not_interactible_on_create": false});
46         this.mutating_lock = $.Deferred();
47         this.initial_mutating_lock = this.mutating_lock;
48         this.on_change_lock = $.Deferred().resolve();
49         this.reload_lock = $.Deferred().resolve();
50     },
51     start: function() {
52         this._super();
53         return this.init_view();
54     },
55     init_view: function() {
56         if (this.embedded_view) {
57             var def = $.Deferred().then(this.on_loaded);
58             var self = this;
59             setTimeout(function() {def.resolve(self.embedded_view);}, 0);
60             return def.promise();
61         } else {
62             var context = new openerp.web.CompoundContext(this.dataset.get_context());
63             return this.rpc("/web/view/load", {
64                 "model": this.model,
65                 "view_id": this.view_id,
66                 "view_type": "form",
67                 toolbar: this.options.sidebar,
68                 context: context
69                 }, this.on_loaded);
70         }
71     },
72     stop: function() {
73         if (this.sidebar) {
74             this.sidebar.attachments.stop();
75             this.sidebar.stop();
76         }
77         _.each(this.widgets, function(w) {
78             w.stop();
79         });
80         this._super();
81     },
82     reposition: function ($e) {
83         this.$element = $e;
84         this.on_loaded();
85     },
86     on_loaded: function(data) {
87         var self = this;
88         if (data) {
89             this.fields_view = data;
90             var frame = new (this.registry.get_object('frame'))(this, this.fields_view.arch);
91
92             this.rendered = QWeb.render(this.form_template, { 'frame': frame, 'widget': this });
93         }
94         this.$element.html(this.rendered);
95         _.each(this.widgets, function(w) {
96             w.start();
97         });
98         this.$form_header = this.$element.find('.oe_form_header:first');
99         this.$form_header.find('div.oe_form_pager button[data-pager-action]').click(function() {
100             var action = $(this).data('pager-action');
101             self.on_pager_action(action);
102         });
103
104         this.$form_header.find('button.oe_form_button_save').click(this.on_button_save);
105         this.$form_header.find('button.oe_form_button_new').click(this.on_button_new);
106         this.$form_header.find('button.oe_form_button_duplicate').click(this.on_button_duplicate);
107         this.$form_header.find('button.oe_form_button_delete').click(this.on_button_delete);
108         this.$form_header.find('button.oe_form_button_toggle').click(this.on_toggle_readonly);
109
110         if (!this.sidebar && this.options.sidebar && this.options.sidebar_id) {
111             this.sidebar = new openerp.web.Sidebar(this, this.options.sidebar_id);
112             this.sidebar.start();
113             this.sidebar.do_unfold();
114             this.sidebar.attachments = new openerp.web.form.SidebarAttachments(this.sidebar, this);
115             this.sidebar.add_toolbar(this.fields_view.toolbar);
116             this.set_common_sidebar_sections(this.sidebar);
117         }
118         this.has_been_loaded.resolve();
119     },
120     on_toggle_readonly: function() {
121         var self = this;
122         self.translatable_fields = [];
123         self.widgets = {};
124         self.fields = {};
125         self.$form_header.find('button').unbind('click');
126         self.readonly = !self.readonly;
127         self.registry = self.readonly ? openerp.web.form.readonly : openerp.web.form.widgets;
128         self.on_loaded(self.fields_view);
129         return self.reload();
130     },
131     do_set_readonly: function() {
132         return this.readonly ? $.Deferred().resolve() : this.on_toggle_readonly();
133     },
134     do_set_editable: function() {
135         return !this.readonly ? $.Deferred().resolve() : this.on_toggle_readonly();
136     },
137     do_show: function () {
138         var promise;
139         if (this.dataset.index === null) {
140             // null index means we should start a new record
141             promise = this.on_button_new();
142         } else {
143             promise = this.dataset.read_index(_.keys(this.fields_view.fields)).pipe(this.on_record_loaded);
144         }
145         this.$element.show();
146         if (this.sidebar) {
147             this.sidebar.$element.show();
148         }
149         return promise;
150     },
151     do_hide: function () {
152         this.$element.hide();
153         if (this.sidebar) {
154             this.sidebar.$element.hide();
155         }
156     },
157     on_record_loaded: function(record) {
158         var self = this,
159             deferred_stack = $.Deferred.queue();
160         if (!record) {
161             throw("Form: No record received");
162         }
163         if (!record.id) {
164             this.$form_header.find('.oe_form_on_create').show();
165             this.$form_header.find('.oe_form_on_update').hide();
166             if (!this.options["always_show_new_button"]) {
167                 this.$form_header.find('button.oe_form_button_new').hide();
168             }
169         } else {
170             this.$form_header.find('.oe_form_on_create').hide();
171             this.$form_header.find('.oe_form_on_update').show();
172             this.$form_header.find('button.oe_form_button_new').show();
173         }
174         this.$form_header.find('.oe_form_on_readonly').toggle(this.readonly);
175         this.$form_header.find('.oe_form_on_editable').toggle(!this.readonly);
176         this.datarecord = record;
177
178         _(this.fields).each(function (field, f) {
179             field.reset();
180             var result = field.set_value(self.datarecord[f] || false);
181             if (result && _.isFunction(result.promise)) {
182                 deferred_stack.push(result);
183             }
184             $.when(result).then(function() {
185                 field.validate();
186             });
187         });
188         deferred_stack.push('force resolution if no fields');
189         return deferred_stack.then(function() {
190             if (!record.id) {
191                 // New record: Second pass in order to trigger the onchanges
192                 self.show_invalid = false;
193                 for (var f in record) {
194                     var field = self.fields[f];
195                     if (field) {
196                         field.dirty = true;
197                         self.do_onchange(field);
198                     }
199                 }
200             }
201             self.on_form_changed();
202             self.initial_mutating_lock.resolve();
203             self.show_invalid = true;
204             self.do_update_pager(record.id == null);
205             if (self.sidebar) {
206                 self.sidebar.attachments.do_update();
207             }
208             if (self.default_focus_field && !self.embedded_view) {
209                 self.default_focus_field.focus();
210             }
211         });
212     },
213     on_form_changed: function() {
214         for (var w in this.widgets) {
215             w = this.widgets[w];
216             w.process_modifiers();
217             w.update_dom();
218         }
219     },
220     on_pager_action: function(action) {
221         if (this.can_be_discarded()) {
222             switch (action) {
223                 case 'first':
224                     this.dataset.index = 0;
225                     break;
226                 case 'previous':
227                     this.dataset.previous();
228                     break;
229                 case 'next':
230                     this.dataset.next();
231                     break;
232                 case 'last':
233                     this.dataset.index = this.dataset.ids.length - 1;
234                     break;
235             }
236             this.reload();
237         }
238     },
239     do_update_pager: function(hide_index) {
240         var $pager = this.$form_header.find('div.oe_form_pager');
241         var index = hide_index ? '-' : this.dataset.index + 1;
242         $pager.find('span.oe_pager_index').html(index);
243         $pager.find('span.oe_pager_count').html(this.dataset.ids.length);
244     },
245     parse_on_change: function (on_change, widget) {
246         var self = this;
247         var onchange = _.str.trim(on_change);
248         var call = onchange.match(/^\s?(.*?)\((.*?)\)\s?$/);
249         if (!call) {
250             return null;
251         }
252
253         var method = call[1];
254         if (!_.str.trim(call[2])) {
255             return {method: method, args: [], context_index: null}
256         }
257
258         var argument_replacement = {
259             'False': function () {return false;},
260             'True': function () {return true;},
261             'None': function () {return null;},
262             'context': function (i) {
263                 context_index = i;
264                 var ctx = widget.build_context ? widget.build_context() : {};
265                 return ctx;
266             }
267         };
268         var parent_fields = null, context_index = null;
269         var args = _.map(call[2].split(','), function (a, i) {
270             var field = _.str.trim(a);
271
272             // literal constant or context
273             if (field in argument_replacement) {
274                 return argument_replacement[field](i);
275             }
276             // literal number
277             if (/^-?\d+(\.\d+)?$/.test(field)) {
278                 return Number(field);
279             }
280             // form field
281             if (self.fields[field]) {
282                 var value = self.fields[field].get_on_change_value();
283                 return value == null ? false : value;
284             }
285             // parent field
286             var splitted = field.split('.');
287             if (splitted.length > 1 && _.str.trim(splitted[0]) === "parent" && self.dataset.parent_view) {
288                 if (parent_fields === null) {
289                     parent_fields = self.dataset.parent_view.get_fields_values();
290                 }
291                 var p_val = parent_fields[_.str.trim(splitted[1])];
292                 if (p_val !== undefined) {
293                     return p_val == null ? false : p_val;
294                 }
295             }
296             // string literal
297             var first_char = field[0], last_char = field[field.length-1];
298             if ((first_char === '"' && last_char === '"')
299                 || (first_char === "'" && last_char === "'")) {
300                 return field.slice(1, -1);
301             }
302
303             throw new Error("Could not get field with name '" + field +
304                             "' for onchange '" + onchange + "'");
305         });
306
307         return {
308             method: method,
309             args: args,
310             context_index: context_index
311         };
312     },
313     do_onchange: function(widget, processed) {
314         var self = this;
315         var act = function() {
316             try {
317                 processed = processed || [];
318                 var on_change = widget.node.attrs.on_change;
319                 if (on_change) {
320                     var change_spec = self.parse_on_change(on_change, widget);
321                     if (change_spec) {
322                         var ajax = {
323                             url: '/web/dataset/call',
324                             async: false
325                         };
326                         return self.rpc(ajax, {
327                             model: self.dataset.model,
328                             method: change_spec.method,
329                             args: [(self.datarecord.id == null ? [] : [self.datarecord.id])].concat(change_spec.args),
330                             context_id: change_spec.context_index == undefined ? null : change_spec.context_index + 1
331                         }).pipe(function(response) {
332                             return self.on_processed_onchange(response, processed);
333                         });
334                     } else {
335                         console.warn("Wrong on_change format", on_change);
336                     }
337                 }
338             } catch(e) {
339                 console.error(e);
340                 return $.Deferred().reject();
341             }
342         };
343         this.on_change_lock = this.on_change_lock.pipe(act, act);
344         return this.on_change_lock;
345     },
346     on_processed_onchange: function(response, processed) {
347         try {
348         var result = response;
349         if (result.value) {
350             for (var f in result.value) {
351                 var field = this.fields[f];
352                 // If field is not defined in the view, just ignore it
353                 if (field) {
354                     var value = result.value[f];
355                     processed.push(field.name);
356                     if (field.get_value() != value) {
357                         field.set_value(value);
358                         field.dirty = true;
359                         if (_.indexOf(processed, field.name) < 0) {
360                             this.do_onchange(field, processed);
361                         }
362                     }
363                 }
364             }
365             this.on_form_changed();
366         }
367         if (!_.isEmpty(result.warning)) {
368             $(QWeb.render("DialogWarning", result.warning)).dialog({
369                 modal: true,
370                 buttons: {
371                     Ok: function() {
372                         $(this).dialog("close");
373                     }
374                 }
375             });
376         }
377         if (result.domain) {
378             // TODO:
379         }
380         return $.Deferred().resolve();
381         } catch(e) {
382             console.error(e);
383             return $.Deferred().reject();
384         }
385     },
386     on_button_save: function() {
387         return this.do_save().then(this.do_set_readonly);
388     },
389     on_button_new: function() {
390         var self = this;
391         var def = $.Deferred();
392         $.when(this.has_been_loaded).then(function() {
393             if (self.can_be_discarded()) {
394                 var keys = _.keys(self.fields_view.fields);
395                 $.when(self.do_set_editable()).then(function() {
396                     if (keys.length) {
397                         self.dataset.default_get(keys).pipe(self.on_record_loaded).then(function() {
398                             def.resolve();
399                         });
400                     } else {
401                         self.on_record_loaded({}).then(function() {
402                             def.resolve();
403                         });
404                     }
405                 });
406             }
407         });
408         return def.promise();
409     },
410     on_button_duplicate: function() {
411         var self = this;
412         var def = $.Deferred();
413         $.when(this.has_been_loaded).then(function() {
414             if (self.can_be_discarded()) {
415                 self.dataset.call('copy', [self.datarecord.id, {}, self.dataset.context]).then(function(new_id) {
416                     return self.on_created({ result : new_id });
417                 }).then(self.do_set_editable).then(function() {
418                     def.resolve();
419                 });
420             }
421         });
422         return def.promise();
423     },
424     on_button_delete: function() {
425         var self = this;
426         var def = $.Deferred();
427         $.when(this.has_been_loaded).then(function() {
428             if (self.can_be_discarded() && self.datarecord.id) {
429                 if (confirm(_t("Do you really want to delete this record?"))) {
430                     self.dataset.unlink([self.datarecord.id]).then(function() {
431                         self.on_pager_action('next');
432                         def.resolve();
433                     });
434                 } else {
435                     setTimeout(function () {
436                         def.reject();
437                     }, 0)
438                 }
439             }
440         });
441         return def.promise();
442     },
443     can_be_discarded: function() {
444         return !this.is_dirty() || confirm(_t("Warning, the record has been modified, your changes will be discarded."));
445     },
446     /**
447      * Triggers saving the form's record. Chooses between creating a new
448      * record or saving an existing one depending on whether the record
449      * already has an id property.
450      *
451      * @param {Function} success callback on save success
452      * @param {Boolean} [prepend_on_create=false] if ``do_save`` creates a new record, should that record be inserted at the start of the dataset (by default, records are added at the end)
453      */
454     do_save: function(success, prepend_on_create) {
455         var self = this;
456         var action = function() {
457             try {
458             if (!self.initial_mutating_lock.isResolved() && !self.initial_mutating_lock.isRejected())
459                 return;
460             var form_invalid = false,
461                 values = {},
462                 first_invalid_field = null;
463             for (var f in self.fields) {
464                 f = self.fields[f];
465                 if (!f.is_valid()) {
466                     form_invalid = true;
467                     f.update_dom();
468                     if (!first_invalid_field) {
469                         first_invalid_field = f;
470                     }
471                 } else if (f.name !== 'id' && !f.readonly && (!self.datarecord.id || f.is_dirty())) {
472                     // Special case 'id' field, do not save this field
473                     // on 'create' : save all non readonly fields
474                     // on 'edit' : save non readonly modified fields
475                     values[f.name] = f.get_value();
476                 }
477             }
478             if (form_invalid) {
479                 first_invalid_field.focus();
480                 self.on_invalid();
481                 return $.Deferred().reject();
482             } else {
483                 if (!self.datarecord.id) {
484                     openerp.log("FormView(", self, ") : About to create", values);
485                     return self.dataset.create(values).pipe(function(r) {
486                         return self.on_created(r, undefined, prepend_on_create);
487                     }).then(success);
488                 } else if (_.isEmpty(values)) {
489                     openerp.log("FormView(", self, ") : Nothing to save");
490                     if (success) {
491                         success();
492                     }
493                 } else {
494                     openerp.log("FormView(", self, ") : About to save", values);
495                     return self.dataset.write(self.datarecord.id, values, {}).pipe(function(r) {
496                         return self.on_saved(r);
497                     }).then(success);
498                 }
499             }
500             } catch (e) {
501                 console.error(e);
502                 return $.Deferred().reject();
503             }
504         };
505         this.mutating_lock = this.mutating_lock.pipe(action, action);
506         return this.mutating_lock;
507     },
508     on_invalid: function() {
509         var msg = "<ul>";
510         _.each(this.fields, function(f) {
511             if (!f.is_valid()) {
512                 msg += "<li>" + f.string + "</li>";
513             }
514         });
515         msg += "</ul>";
516         this.do_warn("The following fields are invalid :", msg);
517     },
518     on_saved: function(r, success) {
519         if (!r.result) {
520             // should not happen in the server, but may happen for internal purpose
521             return $.Deferred().reject();
522         } else {
523             return $.when(this.reload()).pipe(function () {
524                 return $.when(r).then(success); }, null);
525         }
526     },
527     /**
528      * Updates the form' dataset to contain the new record:
529      *
530      * * Adds the newly created record to the current dataset (at the end by
531      *   default)
532      * * Selects that record (sets the dataset's index to point to the new
533      *   record's id).
534      * * Updates the pager and sidebar displays
535      *
536      * @param {Object} r
537      * @param {Function} success callback to execute after having updated the dataset
538      * @param {Boolean} [prepend_on_create=false] adds the newly created record at the beginning of the dataset instead of the end
539      */
540     on_created: function(r, success, prepend_on_create) {
541         if (!r.result) {
542             // should not happen in the server, but may happen for internal purpose
543             return $.Deferred().reject();
544         } else {
545             this.datarecord.id = r.result;
546             if (!prepend_on_create) {
547                 this.dataset.ids.push(this.datarecord.id);
548                 this.dataset.index = this.dataset.ids.length - 1;
549             } else {
550                 this.dataset.ids.unshift(this.datarecord.id);
551                 this.dataset.index = 0;
552             }
553             this.do_update_pager();
554             if (this.sidebar) {
555                 this.sidebar.attachments.do_update();
556             }
557             openerp.log("The record has been created with id #" + this.datarecord.id);
558             this.reload();
559             return $.when(_.extend(r, {created: true})).then(success);
560         }
561     },
562     on_action: function (action) {
563         console.debug('Executing action', action);
564     },
565     reload: function() {
566         var self = this;
567         var act = function() {
568             if (self.dataset.index == null || self.dataset.index < 0) {
569                 return $.when(self.on_button_new());
570             } else {
571                 return self.dataset.read_index(_.keys(self.fields_view.fields)).pipe(self.on_record_loaded);
572             }
573         };
574         this.reload_lock = this.reload_lock.pipe(act, act);
575         return this.reload_lock;
576     },
577     get_fields_values: function() {
578         var values = {};
579         _.each(this.fields, function(value, key) {
580             var val = value.get_value();
581             values[key] = val;
582         });
583         return values;
584     },
585     get_selected_ids: function() {
586         var id = this.dataset.ids[this.dataset.index];
587         return id ? [id] : [];
588     },
589     recursive_save: function() {
590         var self = this;
591         return $.when(this.do_save()).pipe(function(res) {
592             if (self.dataset.parent_view)
593                 return self.dataset.parent_view.recursive_save();
594         });
595     },
596     is_dirty: function() {
597         return _.any(this.fields, function (value) {
598             return value.is_dirty();
599         });
600     },
601     is_interactible_record: function() {
602         var id = this.datarecord.id;
603         if (!id) {
604             if (this.options.not_interactible_on_create)
605                 return false;
606         } else if (typeof(id) === "string") {
607             if(openerp.web.BufferedDataSet.virtual_id_regex.test(id))
608                 return false;
609         }
610         return true;
611     },
612     sidebar_context: function () {
613         return this.do_save().pipe($.proxy(this, 'get_fields_values'));
614     }
615 });
616 openerp.web.FormDialog = openerp.web.Dialog.extend({
617     init: function(parent, options, view_id, dataset) {
618         this._super(parent, options);
619         this.dataset = dataset;
620         this.view_id = view_id;
621         return this;
622     },
623     start: function() {
624         this._super();
625         this.form = new openerp.web.FormView(this, this.dataset, this.view_id, {
626             sidebar: false,
627             pager: false
628         });
629         this.form.appendTo(this.$element);
630         this.form.on_created.add_last(this.on_form_dialog_saved);
631         this.form.on_saved.add_last(this.on_form_dialog_saved);
632         return this;
633     },
634     select_id: function(id) {
635         if (this.form.dataset.select_id(id)) {
636             return this.form.do_show();
637         } else {
638             this.do_warn("Could not find id in dataset");
639             return $.Deferred().reject();
640         }
641     },
642     on_form_dialog_saved: function(r) {
643         this.close();
644     }
645 });
646
647 /** @namespace */
648 openerp.web.form = {};
649
650 openerp.web.form.SidebarAttachments = openerp.web.Widget.extend({
651     init: function(parent, form_view) {
652         var $section = parent.add_section(_t('Attachments'), 'attachments');
653         this.$div = $('<div class="oe-sidebar-attachments"></div>');
654         $section.append(this.$div);
655
656         this._super(parent, $section.attr('id'));
657         this.view = form_view;
658     },
659     do_update: function() {
660         if (!this.view.datarecord.id) {
661             this.on_attachments_loaded([]);
662         } else {
663             (new openerp.web.DataSetSearch(
664                 this, 'ir.attachment', this.view.dataset.get_context(),
665                 [
666                     ['res_model', '=', this.view.dataset.model],
667                     ['res_id', '=', this.view.datarecord.id],
668                     ['type', 'in', ['binary', 'url']]
669                 ])).read_slice(['name', 'url', 'type'], {}, this.on_attachments_loaded);
670         }
671     },
672     on_attachments_loaded: function(attachments) {
673         this.attachments = attachments;
674         this.$div.html(QWeb.render('FormView.sidebar.attachments', this));
675         this.$element.find('.oe-binary-file').change(this.on_attachment_changed);
676         this.$element.find('.oe-sidebar-attachment-delete').click(this.on_attachment_delete);
677     },
678     on_attachment_changed: function(e) {
679         window[this.element_id + '_iframe'] = this.do_update;
680         var $e = $(e.target);
681         if ($e.val() != '') {
682             this.$element.find('form.oe-binary-form').submit();
683             $e.parent().find('input[type=file]').attr('disabled', 'true');
684             $e.parent().find('button').attr('disabled', 'true').find('img, span').toggle();
685         }
686     },
687     on_attachment_delete: function(e) {
688         var self = this, $e = $(e.currentTarget);
689         var name = _.str.trim($e.parent().find('a.oe-sidebar-attachments-link').text());
690         if (confirm("Do you really want to delete the attachment " + name + " ?")) {
691             this.rpc('/web/dataset/unlink', {
692                 model: 'ir.attachment',
693                 ids: [parseInt($e.attr('data-id'))]
694             }, function(r) {
695                 $e.parent().remove();
696                 self.do_notify("Delete an attachment", "The attachment '" + name + "' has been deleted");
697             });
698         }
699     }
700 });
701
702 openerp.web.form.compute_domain = function(expr, fields) {
703     var stack = [];
704     for (var i = expr.length - 1; i >= 0; i--) {
705         var ex = expr[i];
706         if (ex.length == 1) {
707             var top = stack.pop();
708             switch (ex) {
709                 case '|':
710                     stack.push(stack.pop() || top);
711                     continue;
712                 case '&':
713                     stack.push(stack.pop() && top);
714                     continue;
715                 case '!':
716                     stack.push(!top);
717                     continue;
718                 default:
719                     throw new Error('Unknown domain operator ' + ex);
720             }
721         }
722
723         var field = fields[ex[0]];
724         if (!field) {
725             throw new Error("Domain references unknown field : " + ex[0]);
726         }
727         var field_value = field.get_value ? fields[ex[0]].get_value() : fields[ex[0]].value;
728         var op = ex[1];
729         var val = ex[2];
730
731         switch (op.toLowerCase()) {
732             case '=':
733             case '==':
734                 stack.push(field_value == val);
735                 break;
736             case '!=':
737             case '<>':
738                 stack.push(field_value != val);
739                 break;
740             case '<':
741                 stack.push(field_value < val);
742                 break;
743             case '>':
744                 stack.push(field_value > val);
745                 break;
746             case '<=':
747                 stack.push(field_value <= val);
748                 break;
749             case '>=':
750                 stack.push(field_value >= val);
751                 break;
752             case 'in':
753                 stack.push(_(val).contains(field_value));
754                 break;
755             case 'not in':
756                 stack.push(!_(val).contains(field_value));
757                 break;
758             default:
759                 console.warn("Unsupported operator in modifiers :", op);
760         }
761     }
762     return _.all(stack, _.identity);
763 };
764
765 openerp.web.form.Widget = openerp.web.Widget.extend(/** @lends openerp.web.form.Widget# */{
766     template: 'Widget',
767     identifier_prefix: 'formview-widget-',
768     /**
769      * @constructs openerp.web.form.Widget
770      * @extends openerp.web.Widget
771      *
772      * @param view
773      * @param node
774      */
775     init: function(view, node) {
776         this.view = view;
777         this.node = node;
778         this.modifiers = JSON.parse(this.node.attrs.modifiers || '{}');
779         this.always_invisible = (this.modifiers.invisible && this.modifiers.invisible === true);
780         this.type = this.type || node.tag;
781         this.element_name = this.element_name || this.type;
782         this.element_class = [
783             'formview', this.view.view_id, this.element_name,
784             this.view.widgets_counter++].join("_");
785
786         this._super(view);
787
788         this.view.widgets[this.element_class] = this;
789         this.children = node.children;
790         this.colspan = parseInt(node.attrs.colspan || 1, 10);
791         this.decrease_max_width = 0;
792
793         this.string = this.string || node.attrs.string;
794         this.help = this.help || node.attrs.help;
795         this.invisible = this.modifiers['invisible'] === true;
796         this.classname = 'oe_form_' + this.type;
797
798         this.align = parseFloat(this.node.attrs.align);
799         if (isNaN(this.align) || this.align === 1) {
800             this.align = 'right';
801         } else if (this.align === 0) {
802             this.align = 'left';
803         } else {
804             this.align = 'center';
805         }
806
807
808         this.width = this.node.attrs.width;
809     },
810     start: function() {
811         this.$element = this.view.$element.find(
812             '.' + this.element_class.replace(/[^\r\n\f0-9A-Za-z_-]/g, "\\$&"));
813     },
814     process_modifiers: function() {
815         var compute_domain = openerp.web.form.compute_domain;
816         for (var a in this.modifiers) {
817             this[a] = compute_domain(this.modifiers[a], this.view.fields);
818         }
819     },
820     update_dom: function() {
821         this.$element.toggle(!this.invisible);
822     },
823     render: function() {
824         var template = this.template;
825         return QWeb.render(template, { "widget": this });
826     },
827     do_attach_tooltip: function(widget, trigger, options) {
828         widget = widget || this;
829         trigger = trigger || this.$element;
830         options = _.extend({
831                 delay: 1000,
832                 maxWidth: openerp.connection.debug ? '300px' : '200px',
833                 content: function() {
834                     var template = widget.template + '.tooltip';
835                     if (!QWeb.has_template(template)) {
836                         template = 'WidgetLabel.tooltip';
837                     }
838                     return QWeb.render(template, {
839                         debug: openerp.connection.debug,
840                         widget: widget
841                     });
842                 }
843             }, options || {});
844         trigger.tipTip(options);
845     },
846     _build_view_fields_values: function() {
847         var a_dataset = this.view.dataset;
848         var fields_values = this.view.get_fields_values();
849         var active_id = a_dataset.ids[a_dataset.index];
850         _.extend(fields_values, {
851             active_id: active_id || false,
852             active_ids: active_id ? [active_id] : [],
853             active_model: a_dataset.model,
854             parent: a_dataset.parent_view ? a_dataset.parent_view.get_fields_values() : {}
855         });
856         return fields_values;
857     },
858     _build_eval_context: function() {
859         var a_dataset = this.view.dataset;
860         return new openerp.web.CompoundContext(a_dataset.get_context(), this._build_view_fields_values());
861     },
862     /**
863      * Builds a new context usable for operations related to fields by merging
864      * the fields'context with the action's context.
865      */
866     build_context: function() {
867         var f_context = (this.field || {}).context || {};
868         if (!!f_context.__ref) {
869             var fields_values = this._build_eval_context();
870             f_context = new openerp.web.CompoundDomain(f_context).set_eval_context(fields_values);
871         }
872         // maybe the default_get should only be used when we do a default_get?
873         var v_contexts = _.compact([this.node.attrs.default_get || null,
874             this.node.attrs.context || null]);
875         var v_context = new openerp.web.CompoundContext();
876         _.each(v_contexts, function(x) {v_context.add(x);});
877         if (_.detect(v_contexts, function(x) {return !!x.__ref;})) {
878             var fields_values = this._build_eval_context();
879             v_context.set_eval_context(fields_values);
880         }
881         // if there is a context on the node, overrides the model's context
882         var ctx = v_contexts.length > 0 ? v_context : f_context;
883         return ctx;
884     },
885     build_domain: function() {
886         var f_domain = this.field.domain || [];
887         var n_domain = this.node.attrs.domain || null;
888         // if there is a domain on the node, overrides the model's domain
889         var final_domain = n_domain !== null ? n_domain : f_domain;
890         if (!(final_domain instanceof Array)) {
891             var fields_values = this._build_eval_context();
892             final_domain = new openerp.web.CompoundDomain(final_domain).set_eval_context(fields_values);
893         }
894         return final_domain;
895     }
896 });
897
898 openerp.web.form.WidgetFrame = openerp.web.form.Widget.extend({
899     template: 'WidgetFrame',
900     init: function(view, node) {
901         this._super(view, node);
902         this.columns = parseInt(node.attrs.col || 4, 10);
903         this.x = 0;
904         this.y = 0;
905         this.table = [];
906         this.add_row();
907         for (var i = 0; i < node.children.length; i++) {
908             var n = node.children[i];
909             if (n.tag == "newline") {
910                 this.add_row();
911             } else {
912                 this.handle_node(n);
913             }
914         }
915         this.set_row_cells_with(this.table[this.table.length - 1]);
916     },
917     add_row: function(){
918         if (this.table.length) {
919             this.set_row_cells_with(this.table[this.table.length - 1]);
920         }
921         var row = [];
922         this.table.push(row);
923         this.x = 0;
924         this.y += 1;
925         return row;
926     },
927     set_row_cells_with: function(row) {
928         var bypass = 0,
929             max_width = 100,
930             row_length = row.length;
931         for (var i = 0; i < row.length; i++) {
932             if (row[i].always_invisible) {
933                 row_length--;
934             } else {
935                 bypass += row[i].width === undefined ? 0 : 1;
936                 max_width -= row[i].decrease_max_width;
937             }
938         }
939         var size_unit = Math.round(max_width / (this.columns - bypass)),
940             colspan_sum = 0;
941         for (var i = 0; i < row.length; i++) {
942             var w = row[i];
943             if (w.always_invisible) {
944                 continue;
945             }
946             colspan_sum += w.colspan;
947             if (w.width === undefined) {
948                 var width = (i === row_length - 1 && colspan_sum === this.columns) ? max_width : Math.round(size_unit * w.colspan);
949                 max_width -= width;
950                 w.width = width + '%';
951             }
952         }
953     },
954     handle_node: function(node) {
955         var type = {};
956         if (node.tag == 'field') {
957             type = this.view.fields_view.fields[node.attrs.name] || {};
958             if (node.attrs.widget == 'statusbar' && node.attrs.nolabel !== '1') {
959                 // This way we can retain backward compatibility between addons and old clients
960                 node.attrs.colspan = (parseInt(node.attrs.colspan, 10) || 1) + 1;
961                 node.attrs.nolabel = '1';
962             }
963         }
964         var widget = new (this.view.registry.get_any(
965                 [node.attrs.widget, type.type, node.tag])) (this.view, node);
966         if (node.tag == 'field') {
967             if (!this.view.default_focus_field || node.attrs.default_focus == '1') {
968                 this.view.default_focus_field = widget;
969             }
970             if (node.attrs.nolabel != '1') {
971                 var label = new (this.view.registry.get_object('label')) (this.view, node);
972                 label["for"] = widget;
973                 this.add_widget(label, widget.colspan + 1);
974             }
975         }
976         this.add_widget(widget);
977     },
978     add_widget: function(widget, colspan) {
979         var current_row = this.table[this.table.length - 1];
980         if (!widget.always_invisible) {
981             colspan = colspan || widget.colspan;
982             if (current_row.length && (this.x + colspan) > this.columns) {
983                 current_row = this.add_row();
984             }
985             this.x += widget.colspan;
986         }
987         current_row.push(widget);
988         return widget;
989     }
990 });
991
992 openerp.web.form.WidgetGroup = openerp.web.form.WidgetFrame.extend({
993     template: 'WidgetGroup'
994 }),
995
996 openerp.web.form.WidgetNotebook = openerp.web.form.Widget.extend({
997     template: 'WidgetNotebook',
998     init: function(view, node) {
999         this._super(view, node);
1000         this.pages = [];
1001         for (var i = 0; i < node.children.length; i++) {
1002             var n = node.children[i];
1003             if (n.tag == "page") {
1004                 var page = new (this.view.registry.get_object('notebookpage'))(
1005                         this.view, n, this, this.pages.length);
1006                 this.pages.push(page);
1007             }
1008         }
1009     },
1010     start: function() {
1011         var self = this;
1012         this._super.apply(this, arguments);
1013         this.$element.find('> ul > li').each(function (index, tab_li) {
1014             var page = self.pages[index],
1015                 id = _.uniqueId(self.element_name + '-');
1016             page.element_id = id;
1017             $(tab_li).find('a').attr('href', '#' + id);
1018         });
1019         this.$element.find('> div').each(function (index, page) {
1020             page.id = self.pages[index].element_id;
1021         });
1022         this.$element.tabs();
1023         this.view.on_button_new.add_first(this.do_select_first_visible_tab);
1024         if (openerp.connection.debug) {
1025             this.do_attach_tooltip(this, this.$element.find('ul:first'), {
1026                 defaultPosition: 'top'
1027             });
1028         }
1029     },
1030     do_select_first_visible_tab: function() {
1031         for (var i = 0; i < this.pages.length; i++) {
1032             var page = this.pages[i];
1033             if (page.invisible === false) {
1034                 this.$element.tabs('select', page.index);
1035                 break;
1036             }
1037         }
1038     }
1039 });
1040
1041 openerp.web.form.WidgetNotebookPage = openerp.web.form.WidgetFrame.extend({
1042     template: 'WidgetNotebookPage',
1043     init: function(view, node, notebook, index) {
1044         this.notebook = notebook;
1045         this.index = index;
1046         this.element_name = 'page_' + index;
1047         this._super(view, node);
1048     },
1049     start: function() {
1050         this._super.apply(this, arguments);
1051         this.$element_tab = this.notebook.$element.find(
1052                 '> ul > li:eq(' + this.index + ')');
1053     },
1054     update_dom: function() {
1055         if (this.invisible && this.index === this.notebook.$element.tabs('option', 'selected')) {
1056             this.notebook.do_select_first_visible_tab();
1057         }
1058         this.$element_tab.toggle(!this.invisible);
1059         this.$element.toggle(!this.invisible);
1060     }
1061 });
1062
1063 openerp.web.form.WidgetSeparator = openerp.web.form.Widget.extend({
1064     template: 'WidgetSeparator',
1065     init: function(view, node) {
1066         this._super(view, node);
1067         this.orientation = node.attrs.orientation || 'horizontal';
1068         if (this.orientation === 'vertical') {
1069             this.width = '1';
1070         }
1071         this.classname += '_' + this.orientation;
1072     }
1073 });
1074
1075 openerp.web.form.WidgetButton = openerp.web.form.Widget.extend({
1076     template: 'WidgetButton',
1077     init: function(view, node) {
1078         this._super(view, node);
1079         this.force_disabled = false;
1080         if (this.string) {
1081             // We don't have button key bindings in the webclient
1082             this.string = this.string.replace(/_/g, '');
1083         }
1084         if (node.attrs.default_focus == '1') {
1085             // TODO fme: provide enter key binding to widgets
1086             this.view.default_focus_button = this;
1087         }
1088     },
1089     start: function() {
1090         this._super.apply(this, arguments);
1091         this.$element.find("button").click(this.on_click);
1092         if (this.help || openerp.connection.debug) {
1093             this.do_attach_tooltip();
1094         }
1095     },
1096     on_click: function() {
1097         var self = this;
1098         this.force_disabled = true;
1099         this.check_disable();
1100         this.execute_action().always(function() {
1101             self.force_disabled = false;
1102             self.check_disable();
1103         });
1104     },
1105     execute_action: function() {
1106         var self = this;
1107         var exec_action = function() {
1108             if (self.node.attrs.confirm) {
1109                 var def = $.Deferred();
1110                 var dialog = $('<div>' + self.node.attrs.confirm + '</div>').dialog({
1111                     title: 'Confirm',
1112                     modal: true,
1113                     buttons: {
1114                         Ok: function() {
1115                             self.on_confirmed().then(function() {
1116                                 def.resolve();
1117                             });
1118                             $(this).dialog("close");
1119                         },
1120                         Cancel: function() {
1121                             def.resolve();
1122                             $(this).dialog("close");
1123                         }
1124                     }
1125                 });
1126                 return def.promise();
1127             } else {
1128                 return self.on_confirmed();
1129             }
1130         };
1131         if (!this.node.attrs.special) {
1132             return this.view.recursive_save().pipe(exec_action);
1133         } else {
1134             return exec_action();
1135         }
1136     },
1137     on_confirmed: function() {
1138         var self = this;
1139
1140         var context = this.node.attrs.context;
1141         if (context && context.__ref) {
1142             context = new openerp.web.CompoundContext(context);
1143             context.set_eval_context(this._build_eval_context());
1144         }
1145
1146         return this.view.do_execute_action(
1147             _.extend({}, this.node.attrs, {context: context}),
1148             this.view.dataset, this.view.datarecord.id, function () {
1149                 self.view.reload();
1150             });
1151     },
1152     update_dom: function() {
1153         this._super();
1154         this.check_disable();
1155     },
1156     check_disable: function() {
1157         if (this.readonly || this.force_disabled || !this.view.is_interactible_record()) {
1158             this.$element.find("button").attr("disabled", "disabled");
1159             this.$element.find("button").css("color", "grey");
1160         } else {
1161             this.$element.find("button").removeAttr("disabled");
1162             this.$element.find("button").css("color", "");
1163         }
1164     }
1165 });
1166
1167 openerp.web.form.WidgetLabel = openerp.web.form.Widget.extend({
1168     template: 'WidgetLabel',
1169     init: function(view, node) {
1170         this.element_name = 'label_' + node.attrs.name;
1171
1172         this._super(view, node);
1173
1174         if (this.node.tag == 'label' && (this.align === 'left' || this.node.attrs.colspan || (this.string && this.string.length > 32))) {
1175             this.template = "WidgetParagraph";
1176             this.colspan = parseInt(this.node.attrs.colspan || 1, 10);
1177             // Widgets default to right-aligned, but paragraph defaults to
1178             // left-aligned
1179             if (isNaN(parseFloat(this.node.attrs.align))) {
1180                 this.align = 'left';
1181             }
1182         } else {
1183             this.colspan = 1;
1184             this.width = '1%';
1185             this.decrease_max_width = 1;
1186             this.nowrap = true;
1187         }
1188     },
1189     render: function () {
1190         if (this['for'] && this.type !== 'label') {
1191             return QWeb.render(this.template, {widget: this['for']});
1192         }
1193         // Actual label widgets should not have a false and have type label
1194         return QWeb.render(this.template, {widget: this});
1195     },
1196     start: function() {
1197         this._super();
1198         var self = this;
1199         if (this['for'] && (this['for'].help || openerp.connection.debug)) {
1200             this.do_attach_tooltip(self['for']);
1201         }
1202         this.$element.find("label").dblclick(function() {
1203             var widget = self['for'] || self;
1204             openerp.log(widget.element_class , widget);
1205             window.w = widget;
1206         });
1207     }
1208 });
1209
1210 openerp.web.form.Field = openerp.web.form.Widget.extend(/** @lends openerp.web.form.Field# */{
1211     /**
1212      * @constructs openerp.web.form.Field
1213      * @extends openerp.web.form.Widget
1214      *
1215      * @param view
1216      * @param node
1217      */
1218     init: function(view, node) {
1219         this.name = node.attrs.name;
1220         this.value = undefined;
1221         view.fields[this.name] = this;
1222         this.type = node.attrs.widget || view.fields_view.fields[node.attrs.name].type;
1223         this.element_name = "field_" + this.name + "_" + this.type;
1224
1225         this._super(view, node);
1226
1227         if (node.attrs.nolabel != '1' && this.colspan > 1) {
1228             this.colspan--;
1229         }
1230         this.field = view.fields_view.fields[node.attrs.name] || {};
1231         this.string = node.attrs.string || this.field.string;
1232         this.help = node.attrs.help || this.field.help;
1233         this.nolabel = (this.field.nolabel || node.attrs.nolabel) === '1';
1234         this.readonly = this.modifiers['readonly'] === true;
1235         this.required = this.modifiers['required'] === true;
1236         this.invalid = this.dirty = false;
1237
1238         this.classname = 'oe_form_field_' + this.type;
1239     },
1240     start: function() {
1241         this._super.apply(this, arguments);
1242         if (this.field.translate) {
1243             this.view.translatable_fields.push(this);
1244             this.$element.find('.oe_field_translate').click(this.on_translate);
1245         }
1246         if (this.nolabel && openerp.connection.debug) {
1247             this.do_attach_tooltip(this, this.$element, {
1248                 defaultPosition: 'top'
1249             });
1250         }
1251     },
1252     set_value: function(value) {
1253         this.value = value;
1254         this.invalid = false;
1255         this.update_dom();
1256         this.on_value_changed();
1257     },
1258     set_value_from_ui: function() {
1259         this.on_value_changed();
1260     },
1261     on_value_changed: function() {
1262     },
1263     on_translate: function() {
1264         this.view.open_translate_dialog(this);
1265     },
1266     get_value: function() {
1267         return this.value;
1268     },
1269     is_valid: function() {
1270         return !this.invalid;
1271     },
1272     is_dirty: function() {
1273         return this.dirty && !this.readonly;
1274     },
1275     get_on_change_value: function() {
1276         return this.get_value();
1277     },
1278     update_dom: function() {
1279         this._super.apply(this, arguments);
1280         if (this.field.translate) {
1281             this.$element.find('.oe_field_translate').toggle(!!this.view.datarecord.id);
1282         }
1283         if (!this.disable_utility_classes) {
1284             this.$element.toggleClass('disabled', this.readonly);
1285             this.$element.toggleClass('required', this.required);
1286             if (this.view.show_invalid) {
1287                 this.$element.toggleClass('invalid', !this.is_valid());
1288             }
1289         }
1290     },
1291     on_ui_change: function() {
1292         this.dirty = true;
1293         this.validate();
1294         if (this.is_valid()) {
1295             this.set_value_from_ui();
1296             this.view.do_onchange(this);
1297             this.view.on_form_changed();
1298         } else {
1299             this.update_dom();
1300         }
1301     },
1302     validate: function() {
1303         this.invalid = false;
1304     },
1305     focus: function() {
1306     },
1307     reset: function() {
1308         this.dirty = false;
1309     }
1310 });
1311
1312 openerp.web.form.FieldChar = openerp.web.form.Field.extend({
1313     template: 'FieldChar',
1314     init: function (view, node) {
1315         this._super(view, node);
1316         this.password = this.node.attrs.password === 'True' || this.node.attrs.password === '1';
1317     },
1318     start: function() {
1319         this._super.apply(this, arguments);
1320         this.$element.find('input').change(this.on_ui_change);
1321     },
1322     set_value: function(value) {
1323         this._super.apply(this, arguments);
1324         var show_value = openerp.web.format_value(value, this, '');
1325         this.$element.find('input').val(show_value);
1326         return show_value;
1327     },
1328     update_dom: function() {
1329         this._super.apply(this, arguments);
1330         this.$element.find('input').attr('disabled', this.readonly);
1331     },
1332     set_value_from_ui: function() {
1333         this.value = openerp.web.parse_value(this.$element.find('input').val(), this);
1334         this._super();
1335     },
1336     validate: function() {
1337         this.invalid = false;
1338         try {
1339             var value = openerp.web.parse_value(this.$element.find('input').val(), this, '');
1340             this.invalid = this.required && value === '';
1341         } catch(e) {
1342             this.invalid = true;
1343         }
1344     },
1345     focus: function() {
1346         this.$element.find('input').focus();
1347     }
1348 });
1349
1350 openerp.web.form.FieldEmail = openerp.web.form.FieldChar.extend({
1351     template: 'FieldEmail',
1352     start: function() {
1353         this._super.apply(this, arguments);
1354         this.$element.find('button').click(this.on_button_clicked);
1355     },
1356     on_button_clicked: function() {
1357         if (!this.value || !this.is_valid()) {
1358             this.do_warn("E-mail error", "Can't send email to invalid e-mail address");
1359         } else {
1360             location.href = 'mailto:' + this.value;
1361         }
1362     }
1363 });
1364
1365 openerp.web.form.FieldUrl = openerp.web.form.FieldChar.extend({
1366     template: 'FieldUrl',
1367     start: function() {
1368         this._super.apply(this, arguments);
1369         this.$element.find('button').click(this.on_button_clicked);
1370     },
1371     on_button_clicked: function() {
1372         if (!this.value) {
1373             this.do_warn("Resource error", "This resource is empty");
1374         } else {
1375             window.open(this.value);
1376         }
1377     }
1378 });
1379
1380 openerp.web.form.FieldFloat = openerp.web.form.FieldChar.extend({
1381     init: function (view, node) {
1382         this._super(view, node);
1383         if (node.attrs.digits) {
1384             this.parse_digits(node.attrs.digits);
1385         } else {
1386             this.digits = view.fields_view.fields[node.attrs.name].digits;
1387         }
1388     },
1389     parse_digits: function (digits_attr) {
1390         // could use a Python parser instead.
1391         var match = /^\s*[\(\[](\d+),\s*(\d+)/.exec(digits_attr);
1392         return [parseInt(match[1], 10), parseInt(match[2], 10)];
1393     },
1394     set_value: function(value) {
1395         if (value === false || value === undefined) {
1396             // As in GTK client, floats default to 0
1397             value = 0;
1398         }
1399         this._super.apply(this, [value]);
1400     }
1401 });
1402
1403 openerp.web.DateTimeWidget = openerp.web.Widget.extend({
1404     template: "web.datetimepicker",
1405     jqueryui_object: 'datetimepicker',
1406     type_of_date: "datetime",
1407     init: function(parent) {
1408         this._super(parent);
1409         this.name = parent.name;
1410     },
1411     start: function() {
1412         var self = this;
1413         this.$input = this.$element.find('input.oe_datepicker_master');
1414         this.$input_picker = this.$element.find('input.oe_datepicker_container');
1415         this.$input.change(this.on_change);
1416         this.picker({
1417             onSelect: this.on_picker_select,
1418             changeMonth: true,
1419             changeYear: true,
1420             showWeek: true,
1421             showButtonPanel: true
1422         });
1423         this.$element.find('img.oe_datepicker_trigger').click(function() {
1424             if (!self.readonly) {
1425                 self.picker('setDate', self.value ? openerp.web.auto_str_to_date(self.value) : new Date());
1426                 self.$input_picker.show();
1427                 self.picker('show');
1428                 self.$input_picker.hide();
1429             }
1430         });
1431         this.set_readonly(false);
1432         this.value = false;
1433     },
1434     picker: function() {
1435         return $.fn[this.jqueryui_object].apply(this.$input_picker, arguments);
1436     },
1437     on_picker_select: function(text, instance) {
1438         var date = this.picker('getDate');
1439         this.$input.val(date ? this.format_client(date) : '').change();
1440     },
1441     set_value: function(value) {
1442         this.value = value;
1443         this.$input.val(value ? this.format_client(value) : '');
1444     },
1445     get_value: function() {
1446         return this.value;
1447     },
1448     set_value_from_ui: function() {
1449         var value = this.$input.val() || false;
1450         this.value = this.parse_client(value);
1451     },
1452     set_readonly: function(readonly) {
1453         this.readonly = readonly;
1454         this.$input.attr('disabled', this.readonly);
1455         this.$element.find('img.oe_datepicker_trigger').toggleClass('oe_input_icon_disabled', readonly);
1456     },
1457     is_valid: function(required) {
1458         var value = this.$input.val();
1459         if (value === "") {
1460             return !required;
1461         } else {
1462             try {
1463                 this.parse_client(value);
1464                 return true;
1465             } catch(e) {
1466                 return false;
1467             }
1468         }
1469     },
1470     focus: function() {
1471         this.$input.focus();
1472     },
1473     parse_client: function(v) {
1474         return openerp.web.parse_value(v, {"widget": this.type_of_date});
1475     },
1476     format_client: function(v) {
1477         return openerp.web.format_value(v, {"widget": this.type_of_date});
1478     },
1479     on_change: function() {
1480         if (this.is_valid()) {
1481             this.set_value_from_ui();
1482         }
1483     }
1484 });
1485
1486 openerp.web.DateWidget = openerp.web.DateTimeWidget.extend({
1487     jqueryui_object: 'datepicker',
1488     type_of_date: "date"
1489 });
1490
1491 openerp.web.form.FieldDatetime = openerp.web.form.Field.extend({
1492     template: "EmptyComponent",
1493     build_widget: function() {
1494         return new openerp.web.DateTimeWidget(this);
1495     },
1496     start: function() {
1497         var self = this;
1498         this._super.apply(this, arguments);
1499         this.datewidget = this.build_widget();
1500         this.datewidget.on_change.add_last(this.on_ui_change);
1501         this.datewidget.appendTo(this.$element);
1502     },
1503     set_value: function(value) {
1504         this._super(value);
1505         this.datewidget.set_value(value);
1506     },
1507     get_value: function() {
1508         return this.datewidget.get_value();
1509     },
1510     update_dom: function() {
1511         this._super.apply(this, arguments);
1512         this.datewidget.set_readonly(this.readonly);
1513     },
1514     validate: function() {
1515         this.invalid = !this.datewidget.is_valid(this.required);
1516     },
1517     focus: function() {
1518         this.datewidget.focus();
1519     }
1520 });
1521
1522 openerp.web.form.FieldDate = openerp.web.form.FieldDatetime.extend({
1523     build_widget: function() {
1524         return new openerp.web.DateWidget(this);
1525     }
1526 });
1527
1528 openerp.web.form.FieldText = openerp.web.form.Field.extend({
1529     template: 'FieldText',
1530     start: function() {
1531         this._super.apply(this, arguments);
1532         this.$element.find('textarea').change(this.on_ui_change);
1533     },
1534     set_value: function(value) {
1535         this._super.apply(this, arguments);
1536         var show_value = openerp.web.format_value(value, this, '');
1537         this.$element.find('textarea').val(show_value);
1538     },
1539     update_dom: function() {
1540         this._super.apply(this, arguments);
1541         this.$element.find('textarea').attr('disabled', this.readonly);
1542     },
1543     set_value_from_ui: function() {
1544         this.value = openerp.web.parse_value(this.$element.find('textarea').val(), this);
1545         this._super();
1546     },
1547     validate: function() {
1548         this.invalid = false;
1549         try {
1550             var value = openerp.web.parse_value(this.$element.find('textarea').val(), this, '');
1551             this.invalid = this.required && value === '';
1552         } catch(e) {
1553             this.invalid = true;
1554         }
1555     },
1556     focus: function() {
1557         this.$element.find('textarea').focus();
1558     }
1559 });
1560
1561 openerp.web.form.FieldBoolean = openerp.web.form.Field.extend({
1562     template: 'FieldBoolean',
1563     start: function() {
1564         var self = this;
1565         this._super.apply(this, arguments);
1566         this.$element.find('input').click(self.on_ui_change);
1567     },
1568     set_value: function(value) {
1569         this._super.apply(this, arguments);
1570         this.$element.find('input')[0].checked = value;
1571     },
1572     set_value_from_ui: function() {
1573         this.value = this.$element.find('input').is(':checked');
1574         this._super();
1575     },
1576     update_dom: function() {
1577         this._super.apply(this, arguments);
1578         this.$element.find('input').attr('disabled', this.readonly);
1579     },
1580     focus: function() {
1581         this.$element.find('input').focus();
1582     }
1583 });
1584
1585 openerp.web.form.FieldProgressBar = openerp.web.form.Field.extend({
1586     template: 'FieldProgressBar',
1587     start: function() {
1588         this._super.apply(this, arguments);
1589         this.$element.find('div').progressbar({
1590             value: this.value,
1591             disabled: this.readonly
1592         });
1593     },
1594     set_value: function(value) {
1595         this._super.apply(this, arguments);
1596         var show_value = Number(value);
1597         if (isNaN(show_value)) {
1598             show_value = 0;
1599         }
1600         this.$element.find('div').progressbar('option', 'value', show_value).find('span').html(show_value + '%');
1601     }
1602 });
1603
1604 openerp.web.form.FieldTextXml = openerp.web.form.Field.extend({
1605 // to replace view editor
1606 });
1607
1608 openerp.web.form.FieldSelection = openerp.web.form.Field.extend({
1609     template: 'FieldSelection',
1610     init: function(view, node) {
1611         var self = this;
1612         this._super(view, node);
1613         this.values = _.clone(this.field.selection);
1614         _.each(this.values, function(v, i) {
1615             if (v[0] === false && v[1] === '') {
1616                 self.values.splice(i, 1);
1617             }
1618         });
1619         this.values.unshift([false, '']);
1620     },
1621     start: function() {
1622         // Flag indicating whether we're in an event chain containing a change
1623         // event on the select, in order to know what to do on keyup[RETURN]:
1624         // * If the user presses [RETURN] as part of changing the value of a
1625         //   selection, we should just let the value change and not let the
1626         //   event broadcast further (e.g. to validating the current state of
1627         //   the form in editable list view, which would lead to saving the
1628         //   current row or switching to the next one)
1629         // * If the user presses [RETURN] with a select closed (side-effect:
1630         //   also if the user opened the select and pressed [RETURN] without
1631         //   changing the selected value), takes the action as validating the
1632         //   row
1633         var ischanging = false;
1634         this._super.apply(this, arguments);
1635         this.$element.find('select')
1636             .change(this.on_ui_change)
1637             .change(function () { ischanging = true; })
1638             .click(function () { ischanging = false; })
1639             .keyup(function (e) {
1640                 if (e.which !== 13 || !ischanging) { return; }
1641                 e.stopPropagation();
1642                 ischanging = false;
1643             });
1644     },
1645     set_value: function(value) {
1646         value = value === null ? false : value;
1647         value = value instanceof Array ? value[0] : value;
1648         this._super(value);
1649         var index = 0;
1650         for (var i = 0, ii = this.values.length; i < ii; i++) {
1651             if (this.values[i][0] === value) index = i;
1652         }
1653         this.$element.find('select')[0].selectedIndex = index;
1654     },
1655     set_value_from_ui: function() {
1656         this.value = this.values[this.$element.find('select')[0].selectedIndex][0];
1657         this._super();
1658     },
1659     update_dom: function() {
1660         this._super.apply(this, arguments);
1661         this.$element.find('select').attr('disabled', this.readonly);
1662     },
1663     validate: function() {
1664         var value = this.values[this.$element.find('select')[0].selectedIndex];
1665         this.invalid = !(value && !(this.required && value[0] === false));
1666     },
1667     focus: function() {
1668         this.$element.find('select').focus();
1669     }
1670 });
1671
1672 // jquery autocomplete tweak to allow html
1673 (function() {
1674     var proto = $.ui.autocomplete.prototype,
1675         initSource = proto._initSource;
1676
1677     function filter( array, term ) {
1678         var matcher = new RegExp( $.ui.autocomplete.escapeRegex(term), "i" );
1679         return $.grep( array, function(value) {
1680             return matcher.test( $( "<div>" ).html( value.label || value.value || value ).text() );
1681         });
1682     }
1683
1684     $.extend( proto, {
1685         _initSource: function() {
1686             if ( this.options.html && $.isArray(this.options.source) ) {
1687                 this.source = function( request, response ) {
1688                     response( filter( this.options.source, request.term ) );
1689                 };
1690             } else {
1691                 initSource.call( this );
1692             }
1693         },
1694
1695         _renderItem: function( ul, item) {
1696             return $( "<li></li>" )
1697                 .data( "item.autocomplete", item )
1698                 .append( $( "<a></a>" )[ this.options.html ? "html" : "text" ]( item.label ) )
1699                 .appendTo( ul );
1700         }
1701     });
1702 })();
1703
1704 openerp.web.form.dialog = function(content, options) {
1705     options = _.extend({
1706         autoOpen: true,
1707         width: '90%',
1708         height: '90%',
1709         min_width: '800px',
1710         min_height: '600px'
1711     }, options || {});
1712     options.autoOpen = true;
1713     var dialog = new openerp.web.Dialog(null, options);
1714     dialog.$dialog = $(content).dialog(dialog.dialog_options);
1715     return dialog.$dialog;
1716 };
1717
1718 openerp.web.form.FieldMany2One = openerp.web.form.Field.extend({
1719     template: 'FieldMany2One',
1720     init: function(view, node) {
1721         this._super(view, node);
1722         this.limit = 7;
1723         this.value = null;
1724         this.cm_id = _.uniqueId('m2o_cm_');
1725         this.last_search = [];
1726         this.tmp_value = undefined;
1727     },
1728     start: function() {
1729         this._super();
1730         var self = this;
1731         this.$input = this.$element.find("input");
1732         this.$drop_down = this.$element.find(".oe-m2o-drop-down-button");
1733         this.$menu_btn = this.$element.find(".oe-m2o-cm-button");
1734
1735         // context menu
1736         var init_context_menu_def = $.Deferred().then(function(e) {
1737             var rdataset = new openerp.web.DataSetStatic(self, "ir.values", self.build_context());
1738             rdataset.call("get", ['action', 'client_action_relate',
1739                 [[self.field.relation, false]], false, rdataset.get_context()], false, 0)
1740                 .then(function(result) {
1741                 self.related_entries = result;
1742
1743                 var $cmenu = $("#" + self.cm_id);
1744                 $cmenu.append(QWeb.render("FieldMany2One.context_menu", {widget: self}));
1745                 var bindings = {};
1746                 bindings[self.cm_id + "_search"] = function() {
1747                     self._search_create_popup("search");
1748                 };
1749                 bindings[self.cm_id + "_create"] = function() {
1750                     self._search_create_popup("form");
1751                 };
1752                 bindings[self.cm_id + "_open"] = function() {
1753                     if (!self.value) {
1754                         return;
1755                     }
1756                     var pop = new openerp.web.form.FormOpenPopup(self.view);
1757                     pop.show_element(self.field.relation, self.value[0],self.build_context(), {});
1758                     pop.on_write_completed.add_last(function() {
1759                         self.set_value(self.value[0]);
1760                     });
1761                 };
1762                 _.each(_.range(self.related_entries.length), function(i) {
1763                     bindings[self.cm_id + "_related_" + i] = function() {
1764                         self.open_related(self.related_entries[i]);
1765                     };
1766                 });
1767                 var cmenu = self.$menu_btn.contextMenu(self.cm_id, {'leftClickToo': true,
1768                     bindings: bindings, itemStyle: {"color": ""},
1769                     onContextMenu: function() {
1770                         if(self.value) {
1771                             $("#" + self.cm_id + " .oe_m2o_menu_item_mandatory").removeClass("oe-m2o-disabled-cm");
1772                         } else {
1773                             $("#" + self.cm_id + " .oe_m2o_menu_item_mandatory").addClass("oe-m2o-disabled-cm");
1774                         }
1775                         if (!self.readonly) {
1776                             $("#" + self.cm_id + " .oe_m2o_menu_item_noreadonly").removeClass("oe-m2o-disabled-cm");
1777                         } else {
1778                             $("#" + self.cm_id + " .oe_m2o_menu_item_noreadonly").addClass("oe-m2o-disabled-cm");
1779                         }
1780                         return true;
1781                     }, menuStyle: {width: "200px"}
1782                 });
1783                 setTimeout(function() {self.$menu_btn.trigger(e);}, 0);
1784             });
1785         });
1786         var ctx_callback = function(e) {init_context_menu_def.resolve(e); e.preventDefault()};
1787         this.$menu_btn.bind('contextmenu', ctx_callback);
1788         this.$menu_btn.click(ctx_callback);
1789
1790         // some behavior for input
1791         this.$input.keyup(function() {
1792             if (self.$input.val() === "") {
1793                 self._change_int_value(null);
1794             } else if (self.value === null || (self.value && self.$input.val() !== self.value[1])) {
1795                 self._change_int_value(undefined);
1796             }
1797         });
1798         this.$drop_down.click(function() {
1799             if (self.readonly)
1800                 return;
1801             if (self.$input.autocomplete("widget").is(":visible")) {
1802                 self.$input.autocomplete("close");
1803             } else {
1804                 if (self.value) {
1805                     self.$input.autocomplete("search", "");
1806                 } else {
1807                     self.$input.autocomplete("search");
1808                 }
1809                 self.$input.focus();
1810             }
1811         });
1812         var anyoneLoosesFocus = function() {
1813             if (!self.$input.is(":focus") &&
1814                     !self.$input.autocomplete("widget").is(":visible") &&
1815                     !self.value) {
1816                 if (self.value === undefined && self.last_search.length > 0) {
1817                     self._change_int_ext_value(self.last_search[0]);
1818                 } else {
1819                     self._change_int_ext_value(null);
1820                 }
1821             }
1822         };
1823         this.$input.focusout(anyoneLoosesFocus);
1824
1825         var isSelecting = false;
1826         // autocomplete
1827         this.$input.autocomplete({
1828             source: function(req, resp) { self.get_search_result(req, resp); },
1829             select: function(event, ui) {
1830                 isSelecting = true;
1831                 var item = ui.item;
1832                 if (item.id) {
1833                     self._change_int_value([item.id, item.name]);
1834                 } else if (item.action) {
1835                     self._change_int_value(undefined);
1836                     item.action();
1837                     return false;
1838                 }
1839             },
1840             focus: function(e, ui) {
1841                 e.preventDefault();
1842             },
1843             html: true,
1844             close: anyoneLoosesFocus,
1845             minLength: 0,
1846             delay: 0
1847         });
1848         // used to correct a bug when selecting an element by pushing 'enter' in an editable list
1849         this.$input.keyup(function(e) {
1850             if (e.which === 13) {
1851                 if (isSelecting)
1852                     e.stopPropagation();
1853             }
1854             isSelecting = false;
1855         });
1856     },
1857     // autocomplete component content handling
1858     get_search_result: function(request, response) {
1859         var search_val = request.term;
1860         var self = this;
1861
1862         var dataset = new openerp.web.DataSetStatic(this, this.field.relation, self.build_context());
1863
1864         dataset.name_search(search_val, self.build_domain(), 'ilike',
1865                 this.limit + 1, function(data) {
1866             self.last_search = data;
1867             // possible selections for the m2o
1868             var values = _.map(data, function(x) {
1869                 return {label: $('<span />').text(x[1]).html(), name:x[1], id:x[0]};
1870             });
1871
1872             // search more... if more results that max
1873             if (values.length > self.limit) {
1874                 values = values.slice(0, self.limit);
1875                 values.push({label: _t("<em>   Search More...</em>"), action: function() {
1876                     dataset.name_search(search_val, self.build_domain(), 'ilike'
1877                     , false, function(data) {
1878                         self._change_int_value(null);
1879                         self._search_create_popup("search", data);
1880                     });
1881                 }});
1882             }
1883             // quick create
1884             var raw_result = _(data.result).map(function(x) {return x[1];});
1885             if (search_val.length > 0 &&
1886                 !_.include(raw_result, search_val) &&
1887                 (!self.value || search_val !== self.value[1])) {
1888                 values.push({label: _.str.sprintf(_t('<em>   Create "<strong>%s</strong>"</em>'),
1889                         $('<span />').text(search_val).html()), action: function() {
1890                     self._quick_create(search_val);
1891                 }});
1892             }
1893             // create...
1894             values.push({label: _t("<em>   Create and Edit...</em>"), action: function() {
1895                 self._change_int_value(null);
1896                 self._search_create_popup("form", undefined, {"default_name": search_val});
1897             }});
1898
1899             response(values);
1900         });
1901     },
1902     _quick_create: function(name) {
1903         var self = this;
1904         var dataset = new openerp.web.DataSetStatic(this, this.field.relation, self.build_context());
1905         dataset.name_create(name, function(data) {
1906             self._change_int_ext_value(data);
1907         }).fail(function(error, event) {
1908             event.preventDefault();
1909             self._change_int_value(null);
1910             self._search_create_popup("form", undefined, {"default_name": name});
1911         });
1912     },
1913     // all search/create popup handling
1914     _search_create_popup: function(view, ids, context) {
1915         var self = this;
1916         var pop = new openerp.web.form.SelectCreatePopup(this);
1917         pop.select_element(self.field.relation,{
1918                 initial_ids: ids ? _.map(ids, function(x) {return x[0]}) : undefined,
1919                 initial_view: view,
1920                 disable_multiple_selection: true
1921                 }, self.build_domain(),
1922                 new openerp.web.CompoundContext(self.build_context(), context || {}));
1923         pop.on_select_elements.add(function(element_ids) {
1924             var dataset = new openerp.web.DataSetStatic(self, self.field.relation, self.build_context());
1925             dataset.name_get([element_ids[0]], function(data) {
1926                 self._change_int_ext_value(data[0]);
1927             });
1928         });
1929     },
1930     _change_int_ext_value: function(value) {
1931         this._change_int_value(value);
1932         this.$input.val(this.value ? this.value[1] : "");
1933     },
1934     _change_int_value: function(value) {
1935         this.value = value;
1936         var back_orig_value = this.original_value;
1937         if (this.value === null || this.value) {
1938             this.original_value = this.value;
1939         }
1940         if (back_orig_value === undefined) { // first use after a set_value()
1941             return;
1942         }
1943         if (this.value !== undefined && ((back_orig_value ? back_orig_value[0] : null)
1944                 !== (this.value ? this.value[0] : null))) {
1945             this.on_ui_change();
1946         }
1947     },
1948     set_value: function(value) {
1949         value = value || null;
1950         this.invalid = false;
1951         var self = this;
1952         this.tmp_value = value;
1953         self.update_dom();
1954         self.on_value_changed();
1955         var real_set_value = function(rval) {
1956             self.tmp_value = undefined;
1957             self.value = rval;
1958             self.original_value = undefined;
1959             self._change_int_ext_value(rval);
1960         };
1961         if (value && !(value instanceof Array)) {
1962             var dataset = new openerp.web.DataSetStatic(this, this.field.relation, self.build_context());
1963             dataset.name_get([value], function(data) {
1964                 real_set_value(data[0]);
1965             }).fail(function() {self.tmp_value = undefined;});
1966         } else {
1967             setTimeout(function() {real_set_value(value);}, 0);
1968         }
1969     },
1970     get_value: function() {
1971         if (this.tmp_value !== undefined) {
1972             if (this.tmp_value instanceof Array) {
1973                 return this.tmp_value[0];
1974             }
1975             return this.tmp_value ? this.tmp_value : false;
1976         }
1977         if (this.value === undefined)
1978             return this.original_value ? this.original_value[0] : false;
1979         return this.value ? this.value[0] : false;
1980     },
1981     validate: function() {
1982         this.invalid = false;
1983         var val = this.tmp_value !== undefined ? this.tmp_value : this.value;
1984         if (val === null) {
1985             this.invalid = this.required;
1986         }
1987     },
1988     open_related: function(related) {
1989         var self = this;
1990         if (!self.value)
1991             return;
1992         var additional_context = {
1993                 active_id: self.value[0],
1994                 active_ids: [self.value[0]],
1995                 active_model: self.field.relation
1996         };
1997         self.rpc("/web/action/load", {
1998             action_id: related[2].id,
1999             context: additional_context
2000         }, function(result) {
2001             result.result.context = _.extend(result.result.context || {}, additional_context);
2002             self.do_action(result.result);
2003         });
2004     },
2005     focus: function () {
2006         this.$input.focus();
2007     },
2008     update_dom: function() {
2009         this._super.apply(this, arguments);
2010         this.$input.attr('disabled', this.readonly);
2011     }
2012 });
2013
2014 /*
2015 # Values: (0, 0,  { fields })    create
2016 #         (1, ID, { fields })    update
2017 #         (2, ID)                remove (delete)
2018 #         (3, ID)                unlink one (target id or target of relation)
2019 #         (4, ID)                link
2020 #         (5)                    unlink all (only valid for one2many)
2021 */
2022 var commands = {
2023     // (0, _, {values})
2024     CREATE: 0,
2025     'create': function (values) {
2026         return [commands.CREATE, false, values];
2027     },
2028     // (1, id, {values})
2029     UPDATE: 1,
2030     'update': function (id, values) {
2031         return [commands.UPDATE, id, values];
2032     },
2033     // (2, id[, _])
2034     DELETE: 2,
2035     'delete': function (id) {
2036         return [commands.DELETE, id, false];
2037     },
2038     // (3, id[, _]) removes relation, but not linked record itself
2039     FORGET: 3,
2040     'forget': function (id) {
2041         return [commands.FORGET, id, false];
2042     },
2043     // (4, id[, _])
2044     LINK_TO: 4,
2045     'link_to': function (id) {
2046         return [commands.LINK_TO, id, false];
2047     },
2048     // (5[, _[, _]])
2049     DELETE_ALL: 5,
2050     'delete_all': function () {
2051         return [5, false, false];
2052     },
2053     // (6, _, ids) replaces all linked records with provided ids
2054     REPLACE_WITH: 6,
2055     'replace_with': function (ids) {
2056         return [6, false, ids];
2057     }
2058 };
2059 openerp.web.form.FieldOne2Many = openerp.web.form.Field.extend({
2060     template: 'FieldOne2Many',
2061     multi_selection: false,
2062     init: function(view, node) {
2063         this._super(view, node);
2064         this.is_loaded = $.Deferred();
2065         this.initial_is_loaded = this.is_loaded;
2066         this.is_setted = $.Deferred();
2067         this.form_last_update = $.Deferred();
2068         this.init_form_last_update = this.form_last_update;
2069         this.disable_utility_classes = true;
2070     },
2071     start: function() {
2072         this._super.apply(this, arguments);
2073
2074         var self = this;
2075
2076         this.dataset = new openerp.web.form.One2ManyDataSet(this, this.field.relation);
2077         this.dataset.o2m = this;
2078         this.dataset.parent_view = this.view;
2079         this.dataset.on_change.add_last(function() {
2080             self.on_ui_change();
2081         });
2082
2083         this.is_setted.then(function() {
2084             self.load_views();
2085         });
2086     },
2087     is_readonly: function() {
2088         return this.readonly || this.force_readonly;
2089     },
2090     load_views: function() {
2091         var self = this;
2092         
2093         var modes = this.node.attrs.mode;
2094         modes = !!modes ? modes.split(",") : ["tree"];
2095         var views = [];
2096         _.each(modes, function(mode) {
2097             var view = {
2098                 view_id: false,
2099                 view_type: mode == "tree" ? "list" : mode,
2100                 options: { sidebar : false }
2101             };
2102             if (self.field.views && self.field.views[mode]) {
2103                 view.embedded_view = self.field.views[mode];
2104             }
2105             if(view.view_type === "list") {
2106                 view.options.selectable = self.multi_selection;
2107                 if (self.is_readonly()) {
2108                     view.options.addable = null;
2109                     view.options.deletable = null;
2110                 }
2111             } else if (view.view_type === "form") {
2112                 view.options.not_interactible_on_create = true;
2113             }
2114             views.push(view);
2115         });
2116         this.views = views;
2117         
2118         this.viewmanager = new openerp.web.ViewManager(this, this.dataset, views);
2119         this.viewmanager.registry = openerp.web.views.clone({
2120             list: 'openerp.web.form.One2ManyListView',
2121             form: 'openerp.web.FormView'
2122         });
2123         var once = $.Deferred().then(function() {
2124             self.init_form_last_update.resolve();
2125         });
2126         var def = $.Deferred().then(function() {
2127             self.initial_is_loaded.resolve();
2128         });
2129         this.viewmanager.on_controller_inited.add_last(function(view_type, controller) {
2130             if (view_type == "list") {
2131                 controller.o2m = self;
2132                 if (self.is_readonly())
2133                     controller.set_editable(false);
2134             } else if (view_type == "form") {
2135                 if (self.is_readonly()) {
2136                     controller.on_toggle_readonly();
2137                     $(controller.$element.find(".oe_form_buttons")[0]).children().remove();
2138                 }
2139                 controller.on_record_loaded.add_last(function() {
2140                     once.resolve();
2141                 });
2142                 controller.on_pager_action.add_first(function() {
2143                     self.save_any_view();
2144                 });
2145                 controller.$element.find(".oe_form_button_save").hide();
2146             } else if (view_type == "graph") {
2147                 self.reload_current_view()
2148             }
2149             def.resolve();
2150         });
2151         this.viewmanager.on_mode_switch.add_first(function(n_mode, b, c, d, e) {
2152             $.when(self.save_any_view()).then(function() {
2153                 if(n_mode === "list")
2154                     setTimeout(function() {self.reload_current_view();}, 0);
2155             });
2156         });
2157         this.is_setted.then(function() {
2158             setTimeout(function () {
2159                 self.viewmanager.appendTo(self.$element);
2160             }, 0);
2161         });
2162         return def;
2163     },
2164     reload_current_view: function() {
2165         var self = this;
2166         return self.is_loaded = self.is_loaded.pipe(function() {
2167             var view = self.viewmanager.views[self.viewmanager.active_view].controller;
2168             if(self.viewmanager.active_view === "list") {
2169                 return view.reload_content();
2170             } else if (self.viewmanager.active_view === "form") {
2171                 if (self.dataset.index === null && self.dataset.ids.length >= 1) {
2172                     self.dataset.index = 0;
2173                 }
2174                 var act = function() {
2175                     return view.do_show();
2176                 }
2177                 self.form_last_update = self.form_last_update.pipe(act, act);
2178                 return self.form_last_update;
2179             } else if (self.viewmanager.active_view === "graph") {
2180                 return view.do_search(self.build_domain(), self.dataset.get_context(), []);
2181             }
2182         });
2183     },
2184     set_value: function(value) {
2185         value = value || [];
2186         var self = this;
2187         this.dataset.reset_ids([]);
2188         if(value.length >= 1 && value[0] instanceof Array) {
2189             var ids = [];
2190             _.each(value, function(command) {
2191                 var obj = {values: command[2]};
2192                 switch (command[0]) {
2193                     case commands.CREATE:
2194                         obj['id'] = _.uniqueId(self.dataset.virtual_id_prefix);
2195                         obj.defaults = {};
2196                         self.dataset.to_create.push(obj);
2197                         self.dataset.cache.push(_.clone(obj));
2198                         ids.push(obj.id);
2199                         return;
2200                     case commands.UPDATE:
2201                         obj['id'] = command[1];
2202                         self.dataset.to_write.push(obj);
2203                         self.dataset.cache.push(_.clone(obj));
2204                         ids.push(obj.id);
2205                         return;
2206                     case commands.DELETE:
2207                         self.dataset.to_delete.push({id: command[1]});
2208                         return;
2209                     case commands.LINK_TO:
2210                         ids.push(command[1]);
2211                         return;
2212                     case commands.DELETE_ALL:
2213                         self.dataset.delete_all = true;
2214                         return;
2215                 }
2216             });
2217             this._super(ids);
2218             this.dataset.set_ids(ids);
2219         } else if (value.length >= 1 && typeof(value[0]) === "object") {
2220             var ids = [];
2221             this.dataset.delete_all = true;
2222             _.each(value, function(command) {
2223                 var obj = {values: command};
2224                 obj['id'] = _.uniqueId(self.dataset.virtual_id_prefix);
2225                 obj.defaults = {};
2226                 self.dataset.to_create.push(obj);
2227                 self.dataset.cache.push(_.clone(obj));
2228                 ids.push(obj.id);
2229             });
2230             this._super(ids);
2231             this.dataset.set_ids(ids);
2232         } else {
2233             this._super(value);
2234             this.dataset.reset_ids(value);
2235         }
2236         if (this.dataset.index === null && this.dataset.ids.length > 0) {
2237             this.dataset.index = 0;
2238         }
2239         self.is_setted.resolve();
2240         return self.reload_current_view();
2241     },
2242     get_value: function() {
2243         var self = this;
2244         if (!this.dataset)
2245             return [];
2246         var val = this.dataset.delete_all ? [commands.delete_all()] : [];
2247         val = val.concat(_.map(this.dataset.ids, function(id) {
2248             var alter_order = _.detect(self.dataset.to_create, function(x) {return x.id === id;});
2249             if (alter_order) {
2250                 return commands.create(alter_order.values);
2251             }
2252             alter_order = _.detect(self.dataset.to_write, function(x) {return x.id === id;});
2253             if (alter_order) {
2254                 return commands.update(alter_order.id, alter_order.values);
2255             }
2256             return commands.link_to(id);
2257         }));
2258         return val.concat(_.map(
2259             this.dataset.to_delete, function(x) {
2260                 return commands['delete'](x.id);}));
2261     },
2262     save_any_view: function() {
2263         if (this.viewmanager && this.viewmanager.views && this.viewmanager.active_view &&
2264             this.viewmanager.views[this.viewmanager.active_view] &&
2265             this.viewmanager.views[this.viewmanager.active_view].controller) {
2266             var view = this.viewmanager.views[this.viewmanager.active_view].controller;
2267             if (this.viewmanager.active_view === "form") {
2268                 var res = $.when(view.do_save());
2269                 // it seems line there are some cases when this happens
2270                 /*if (!res.isResolved() && !res.isRejected()) {
2271                     console.warn("Asynchronous get_value() is not supported in form view.");
2272                 }*/
2273                 return res;
2274             } else if (this.viewmanager.active_view === "list") {
2275                 var res = $.when(view.ensure_saved());
2276                 // it seems line there are some cases when this happens
2277                 /*if (!res.isResolved() && !res.isRejected()) {
2278                     console.warn("Asynchronous get_value() is not supported in list view.");
2279                 }*/
2280                 return res;
2281             }
2282         }
2283         return false;
2284     },
2285     is_valid: function() {
2286         this.validate();
2287         return this._super();
2288     },
2289     validate: function() {
2290         this.invalid = false;
2291         if (!this.viewmanager.views[this.viewmanager.active_view])
2292             return;
2293         var view = this.viewmanager.views[this.viewmanager.active_view].controller;
2294         if (this.viewmanager.active_view === "form") {
2295             for (var f in view.fields) {
2296                 f = view.fields[f];
2297                 if (!f.is_valid()) {
2298                     this.invalid = true;
2299                     return;
2300                 }
2301             }
2302         }
2303     },
2304     is_dirty: function() {
2305         this.save_any_view();
2306         return this._super();
2307     },
2308     update_dom: function() {
2309         this._super.apply(this, arguments);
2310         var self = this;
2311         if (this.previous_readonly !== this.readonly) {
2312             this.previous_readonly = this.readonly;
2313             if (this.viewmanager) {
2314                 this.is_loaded = this.is_loaded.pipe(function() {
2315                     self.viewmanager.stop();
2316                     return $.when(self.load_views()).then(function() {
2317                         self.reload_current_view();
2318                     });
2319                 });
2320             }
2321         }
2322     }
2323 });
2324
2325 openerp.web.form.One2ManyDataSet = openerp.web.BufferedDataSet.extend({
2326     get_context: function() {
2327         this.context = this.o2m.build_context();
2328         return this.context;
2329     }
2330 });
2331
2332 openerp.web.form.One2ManyListView = openerp.web.ListView.extend({
2333     do_add_record: function () {
2334         if (this.options.editable) {
2335             this._super.apply(this, arguments);
2336         } else {
2337             var self = this;
2338             var pop = new openerp.web.form.SelectCreatePopup(this);
2339             pop.on_default_get.add(self.dataset.on_default_get);
2340             pop.select_element(self.o2m.field.relation,{
2341                 initial_view: "form",
2342                 alternative_form_view: self.o2m.field.views ? self.o2m.field.views["form"] : undefined,
2343                 create_function: function(data, callback, error_callback) {
2344                     return self.o2m.dataset.create(data).then(function(r) {
2345                         self.o2m.dataset.set_ids(self.o2m.dataset.ids.concat([r.result]));
2346                         self.o2m.dataset.on_change();
2347                     }).then(callback, error_callback);
2348                 },
2349                 read_function: function() {
2350                     return self.o2m.dataset.read_ids.apply(self.o2m.dataset, arguments);
2351                 },
2352                 parent_view: self.o2m.view,
2353                 form_view_options: {'not_interactible_on_create':true}
2354             }, self.o2m.build_domain(), self.o2m.build_context());
2355             pop.on_select_elements.add_last(function() {
2356                 self.o2m.reload_current_view();
2357             });
2358         }
2359     },
2360     do_activate_record: function(index, id) {
2361         var self = this;
2362         var pop = new openerp.web.form.FormOpenPopup(self.o2m.view);
2363         pop.show_element(self.o2m.field.relation, id, self.o2m.build_context(),{
2364             auto_write: false,
2365             alternative_form_view: self.o2m.field.views ? self.o2m.field.views["form"] : undefined,
2366             parent_view: self.o2m.view,
2367             read_function: function() {
2368                 return self.o2m.dataset.read_ids.apply(self.o2m.dataset, arguments);
2369             },
2370             form_view_options: {'not_interactible_on_create':true},
2371             readonly: self.o2m.is_readonly()
2372         });
2373         pop.on_write.add(function(id, data) {
2374             self.o2m.dataset.write(id, data, {}, function(r) {
2375                 self.o2m.reload_current_view();
2376             });
2377         });
2378     }
2379 });
2380
2381 openerp.web.form.FieldMany2Many = openerp.web.form.Field.extend({
2382     template: 'FieldMany2Many',
2383     multi_selection: false,
2384     init: function(view, node) {
2385         this._super(view, node);
2386         this.list_id = _.uniqueId("many2many");
2387         this.is_loaded = $.Deferred();
2388         this.initial_is_loaded = this.is_loaded;
2389         this.is_setted = $.Deferred();
2390     },
2391     start: function() {
2392         this._super.apply(this, arguments);
2393
2394         var self = this;
2395
2396         this.dataset = new openerp.web.form.Many2ManyDataSet(this, this.field.relation);
2397         this.dataset.m2m = this;
2398         this.dataset.on_unlink.add_last(function(ids) {
2399             self.on_ui_change();
2400         });
2401         
2402         this.is_setted.then(function() {
2403             self.load_view();
2404         });
2405     },
2406     set_value: function(value) {
2407         value = value || [];
2408         if (value.length >= 1 && value[0] instanceof Array) {
2409             value = value[0][2];
2410         }
2411         this._super(value);
2412         this.dataset.set_ids(value);
2413         var self = this;
2414         self.reload_content();
2415         this.is_setted.resolve();
2416     },
2417     get_value: function() {
2418         return [commands.replace_with(this.dataset.ids)];
2419     },
2420     validate: function() {
2421         this.invalid = false;
2422     },
2423     is_readonly: function() {
2424         return this.readonly || this.force_readonly;
2425     },
2426     load_view: function() {
2427         var self = this;
2428         this.list_view = new openerp.web.form.Many2ManyListView(this, this.dataset, false, {
2429                     'addable': self.is_readonly() ? null : 'Add',
2430                     'deletable': self.is_readonly() ? false : true,
2431                     'selectable': self.multi_selection
2432             });
2433         var embedded = (this.field.views || {}).tree;
2434         if (embedded) {
2435             this.list_view.set_embedded_view(embedded);
2436         }
2437         this.list_view.m2m_field = this;
2438         var loaded = $.Deferred();
2439         this.list_view.on_loaded.add_last(function() {
2440             self.initial_is_loaded.resolve();
2441             loaded.resolve();
2442         });
2443         setTimeout(function () {
2444             self.list_view.appendTo($("#" + self.list_id));
2445         }, 0);
2446         return loaded;
2447     },
2448     reload_content: function() {
2449         var self = this;
2450         this.is_loaded = this.is_loaded.pipe(function() {
2451             return self.list_view.reload_content();
2452         });
2453     },
2454     update_dom: function() {
2455         this._super.apply(this, arguments);
2456         var self = this;
2457         if (this.previous_readonly !== this.readonly) {
2458             this.previous_readonly = this.readonly;
2459             if (this.list_view) {
2460                 this.is_loaded = this.is_loaded.pipe(function() {
2461                     self.list_view.stop();
2462                     return $.when(self.load_view()).then(function() {
2463                         self.reload_content();
2464                     });
2465                 });
2466             }
2467         }
2468     }
2469 });
2470
2471 openerp.web.form.Many2ManyDataSet = openerp.web.DataSetStatic.extend({
2472     get_context: function() {
2473         this.context = this.m2m.build_context();
2474         return this.context;
2475     }
2476 });
2477
2478 /**
2479  * @class
2480  * @extends openerp.web.ListView
2481  */
2482 openerp.web.form.Many2ManyListView = openerp.web.ListView.extend(/** @lends openerp.web.form.Many2ManyListView# */{
2483     do_add_record: function () {
2484         var pop = new openerp.web.form.SelectCreatePopup(this);
2485         pop.select_element(this.model, {},
2486             new openerp.web.CompoundDomain(this.m2m_field.build_domain(), ["!", ["id", "in", this.m2m_field.dataset.ids]]),
2487             this.m2m_field.build_context());
2488         var self = this;
2489         pop.on_select_elements.add(function(element_ids) {
2490             _.each(element_ids, function(element_id) {
2491                 if(! _.detect(self.dataset.ids, function(x) {return x == element_id;})) {
2492                     self.dataset.set_ids([].concat(self.dataset.ids, [element_id]));
2493                     self.m2m_field.on_ui_change();
2494                     self.reload_content();
2495                 }
2496             });
2497         });
2498     },
2499     do_activate_record: function(index, id) {
2500         var self = this;
2501         var pop = new openerp.web.form.FormOpenPopup(this);
2502         pop.show_element(this.dataset.model, id, this.m2m_field.build_context(), {});
2503         pop.on_write_completed.add_last(function() {
2504             self.reload_content();
2505         });
2506     }
2507 });
2508
2509 /**
2510  * @class
2511  * @extends openerp.web.OldWidget
2512  */
2513 openerp.web.form.SelectCreatePopup = openerp.web.OldWidget.extend(/** @lends openerp.web.form.SelectCreatePopup# */{
2514     identifier_prefix: "selectcreatepopup",
2515     template: "SelectCreatePopup",
2516     /**
2517      * options:
2518      * - initial_ids
2519      * - initial_view: form or search (default search)
2520      * - disable_multiple_selection
2521      * - alternative_form_view
2522      * - create_function (defaults to a naive saving behavior)
2523      * - parent_view
2524      * - form_view_options
2525      * - list_view_options
2526      * - read_function
2527      */
2528     select_element: function(model, options, domain, context) {
2529         var self = this;
2530         this.model = model;
2531         this.domain = domain || [];
2532         this.context = context || {};
2533         this.options = _.defaults(options || {}, {"initial_view": "search", "create_function": function() {
2534             return self.create_row.apply(self, arguments);
2535         }, read_function: null});
2536         this.initial_ids = this.options.initial_ids;
2537         this.created_elements = [];
2538         this.render_element();
2539         openerp.web.form.dialog(this.$element, {close:function() {
2540             self.check_exit();
2541         }});
2542         this.start();
2543     },
2544     start: function() {
2545         this._super();
2546         var self = this;
2547         this.dataset = new openerp.web.ProxyDataSet(this, this.model,
2548             this.context);
2549         this.dataset.create_function = function() {
2550             return self.options.create_function.apply(null, arguments).then(function(r) {
2551                 self.created_elements.push(r.result);
2552             });
2553         };
2554         this.dataset.write_function = function() {
2555             return self.write_row.apply(self, arguments);
2556         };
2557         this.dataset.read_function = this.options.read_function;
2558         this.dataset.parent_view = this.options.parent_view;
2559         this.dataset.on_default_get.add(this.on_default_get);
2560         if (this.options.initial_view == "search") {
2561             self.rpc('/web/session/eval_domain_and_context', {
2562                 domains: [],
2563                 contexts: [this.context]
2564             }, function (results) {
2565                 var search_defaults = {};
2566                 _.each(results.context, function (value, key) {
2567                     var match = /^search_default_(.*)$/.exec(key);
2568                     if (match) {
2569                         search_defaults[match[1]] = value;
2570                     }
2571                 });
2572                 self.setup_search_view(search_defaults);
2573             });
2574         } else { // "form"
2575             this.new_object();
2576         }
2577     },
2578     setup_search_view: function(search_defaults) {
2579         var self = this;
2580         if (this.searchview) {
2581             this.searchview.stop();
2582         }
2583         this.searchview = new openerp.web.SearchView(this,
2584                 this.dataset, false,  search_defaults);
2585         this.searchview.on_search.add(function(domains, contexts, groupbys) {
2586             if (self.initial_ids) {
2587                 self.do_search(domains.concat([[["id", "in", self.initial_ids]], self.domain]),
2588                     contexts, groupbys);
2589                 self.initial_ids = undefined;
2590             } else {
2591                 self.do_search(domains.concat([self.domain]), contexts, groupbys);
2592             }
2593         });
2594         this.searchview.on_loaded.add_last(function () {
2595             var $buttons = self.searchview.$element.find(".oe_search-view-buttons");
2596             $buttons.append(QWeb.render("SelectCreatePopup.search.buttons"));
2597             var $cbutton = $buttons.find(".oe_selectcreatepopup-search-close");
2598             $cbutton.click(function() {
2599                 self.stop();
2600             });
2601             var $sbutton = $buttons.find(".oe_selectcreatepopup-search-select");
2602             if(self.options.disable_multiple_selection) {
2603                 $sbutton.hide();
2604             }
2605             $sbutton.click(function() {
2606                 self.on_select_elements(self.selected_ids);
2607                 self.stop();
2608             });
2609             self.view_list = new openerp.web.form.SelectCreateListView(self,
2610                     self.dataset, false,
2611                     _.extend({'deletable': false,
2612                         'selectable': !self.options.disable_multiple_selection
2613                     }, self.options.list_view_options || {}));
2614             self.view_list.popup = self;
2615             self.view_list.appendTo($("#" + self.element_id + "_view_list")).pipe(function() {
2616                 self.view_list.do_show();
2617             }).pipe(function() {
2618                 self.searchview.do_search();
2619             });
2620         });
2621         this.searchview.appendTo($("#" + this.element_id + "_search"));
2622     },
2623     do_search: function(domains, contexts, groupbys) {
2624         var self = this;
2625         this.rpc('/web/session/eval_domain_and_context', {
2626             domains: domains || [],
2627             contexts: contexts || [],
2628             group_by_seq: groupbys || []
2629         }, function (results) {
2630             self.view_list.do_search(results.domain, results.context, results.group_by);
2631         });
2632     },
2633     create_row: function() {
2634         var self = this;
2635         var wdataset = new openerp.web.DataSetSearch(this, this.model, this.context, this.domain);
2636         wdataset.parent_view = this.options.parent_view;
2637         return wdataset.create.apply(wdataset, arguments);
2638     },
2639     write_row: function() {
2640         var self = this;
2641         var wdataset = new openerp.web.DataSetSearch(this, this.model, this.context, this.domain);
2642         wdataset.parent_view = this.options.parent_view;
2643         return wdataset.write.apply(wdataset, arguments);
2644     },
2645     on_select_elements: function(element_ids) {
2646     },
2647     on_click_element: function(ids) {
2648         this.selected_ids = ids || [];
2649         if(this.selected_ids.length > 0) {
2650             this.$element.find(".oe_selectcreatepopup-search-select").removeAttr('disabled');
2651         } else {
2652             this.$element.find(".oe_selectcreatepopup-search-select").attr('disabled', "disabled");
2653         }
2654     },
2655     new_object: function() {
2656         var self = this;
2657         if (this.searchview) {
2658             this.searchview.hide();
2659         }
2660         if (this.view_list) {
2661             this.view_list.$element.hide();
2662         }
2663         this.dataset.index = null;
2664         this.view_form = new openerp.web.FormView(this, this.dataset, false, self.options.form_view_options);
2665         if (this.options.alternative_form_view) {
2666             this.view_form.set_embedded_view(this.options.alternative_form_view);
2667         }
2668         this.view_form.appendTo(this.$element.find("#" + this.element_id + "_view_form"));
2669         this.view_form.on_loaded.add_last(function() {
2670             var $buttons = self.view_form.$element.find(".oe_form_buttons");
2671             $buttons.html(QWeb.render("SelectCreatePopup.form.buttons", {widget:self}));
2672             var $nbutton = $buttons.find(".oe_selectcreatepopup-form-save-new");
2673             $nbutton.click(function() {
2674                 $.when(self.view_form.do_save()).then(function() {
2675                     self.view_form.reload_lock.then(function() {
2676                         self.view_form.on_button_new();
2677                     });
2678                 });
2679             });
2680             var $nbutton = $buttons.find(".oe_selectcreatepopup-form-save");
2681             $nbutton.click(function() {
2682                 $.when(self.view_form.do_save()).then(function() {
2683                     self.view_form.reload_lock.then(function() {
2684                         self.check_exit();
2685                     });
2686                 });
2687             });
2688             var $cbutton = $buttons.find(".oe_selectcreatepopup-form-close");
2689             $cbutton.click(function() {
2690                 self.check_exit();
2691             });
2692         });
2693         this.view_form.do_show();
2694     },
2695     check_exit: function() {
2696         if (this.created_elements.length > 0) {
2697             this.on_select_elements(this.created_elements);
2698         }
2699         this.stop();
2700     },
2701     on_default_get: function(res) {}
2702 });
2703
2704 openerp.web.form.SelectCreateListView = openerp.web.ListView.extend({
2705     do_add_record: function () {
2706         this.popup.new_object();
2707     },
2708     select_record: function(index) {
2709         this.popup.on_select_elements([this.dataset.ids[index]]);
2710         this.popup.stop();
2711     },
2712     do_select: function(ids, records) {
2713         this._super(ids, records);
2714         this.popup.on_click_element(ids);
2715     }
2716 });
2717
2718 /**
2719  * @class
2720  * @extends openerp.web.OldWidget
2721  */
2722 openerp.web.form.FormOpenPopup = openerp.web.OldWidget.extend(/** @lends openerp.web.form.FormOpenPopup# */{
2723     identifier_prefix: "formopenpopup",
2724     template: "FormOpenPopup",
2725     /**
2726      * options:
2727      * - alternative_form_view
2728      * - auto_write (default true)
2729      * - read_function
2730      * - parent_view
2731      * - form_view_options
2732      * - readonly
2733      */
2734     show_element: function(model, row_id, context, options) {
2735         this.model = model;
2736         this.row_id = row_id;
2737         this.context = context || {};
2738         this.options = _.defaults(options || {}, {"auto_write": true});
2739         this.render_element();
2740         this.$element.dialog({title: '',
2741                     modal: true,
2742                     width: 960,
2743                     height: 600});
2744         this.start();
2745     },
2746     start: function() {
2747         this._super();
2748         this.dataset = new openerp.web.form.FormOpenDataset(this, this.model, this.context);
2749         this.dataset.fop = this;
2750         this.dataset.ids = [this.row_id];
2751         this.dataset.index = 0;
2752         this.dataset.parent_view = this.options.parent_view;
2753         this.setup_form_view();
2754     },
2755     on_write: function(id, data) {
2756         if (!this.options.auto_write)
2757             return;
2758         var self = this;
2759         var wdataset = new openerp.web.DataSetSearch(this, this.model, this.context, this.domain);
2760         wdataset.parent_view = this.options.parent_view;
2761         wdataset.write(id, data, {}, function(r) {
2762             self.on_write_completed();
2763         });
2764     },
2765     on_write_completed: function() {},
2766     setup_form_view: function() {
2767         var self = this;
2768         this.view_form = new openerp.web.FormView(this, this.dataset, false, self.options.form_view_options);
2769         if (this.options.alternative_form_view) {
2770             this.view_form.set_embedded_view(this.options.alternative_form_view);
2771         }
2772         this.view_form.appendTo(this.$element.find("#" + this.element_id + "_view_form"));
2773         var once = $.Deferred().then(function() {
2774             if (self.options.readonly) {
2775                 self.view_form.on_toggle_readonly();
2776             }
2777         });
2778         this.view_form.on_loaded.add_last(function() {
2779             once.resolve();
2780             var $buttons = self.view_form.$element.find(".oe_form_buttons");
2781             $buttons.html(QWeb.render("FormOpenPopup.form.buttons"));
2782             var $nbutton = $buttons.find(".oe_formopenpopup-form-save");
2783             $nbutton.click(function() {
2784                 self.view_form.do_save().then(function() {
2785                     self.stop();
2786                 });
2787             });
2788             var $cbutton = $buttons.find(".oe_formopenpopup-form-close");
2789             $cbutton.click(function() {
2790                 self.stop();
2791             });
2792             if (self.options.readonly) {
2793                 $nbutton.hide();
2794                 $cbutton.text(_t("Close"));
2795             }
2796             self.view_form.do_show();
2797         });
2798         this.dataset.on_write.add(this.on_write);
2799     }
2800 });
2801
2802 openerp.web.form.FormOpenDataset = openerp.web.ProxyDataSet.extend({
2803     read_ids: function() {
2804         if (this.fop.options.read_function) {
2805             return this.fop.options.read_function.apply(null, arguments);
2806         } else {
2807             return this._super.apply(this, arguments);
2808         }
2809     }
2810 });
2811
2812 openerp.web.form.FieldReference = openerp.web.form.Field.extend({
2813     template: 'FieldReference',
2814     init: function(view, node) {
2815         this._super(view, node);
2816         this.fields_view = {
2817             fields: {
2818                 selection: {
2819                     selection: view.fields_view.fields[this.name].selection
2820                 },
2821                 m2o: {
2822                     relation: null
2823                 }
2824             }
2825         };
2826         this.get_fields_values = view.get_fields_values;
2827         this.do_onchange = this.on_form_changed = this.on_nop;
2828         this.dataset = this.view.dataset;
2829         this.widgets_counter = 0;
2830         this.view_id = 'reference_' + _.uniqueId();
2831         this.widgets = {};
2832         this.fields = {};
2833         this.selection = new openerp.web.form.FieldSelection(this, { attrs: {
2834             name: 'selection',
2835             widget: 'selection'
2836         }});
2837         this.selection.on_value_changed.add_last(this.on_selection_changed);
2838         this.m2o = new openerp.web.form.FieldMany2One(this, { attrs: {
2839             name: 'm2o',
2840             widget: 'many2one'
2841         }});
2842     },
2843     on_nop: function() {
2844     },
2845     on_selection_changed: function() {
2846         var sel = this.selection.get_value();
2847         this.m2o.field.relation = sel;
2848         this.m2o.set_value(null);
2849         this.m2o.$element.toggle(sel !== false);
2850     },
2851     start: function() {
2852         this._super();
2853         this.selection.start();
2854         this.m2o.start();
2855     },
2856     is_valid: function() {
2857         return this.required === false || typeof(this.get_value()) === 'string';
2858     },
2859     is_dirty: function() {
2860         return this.selection.is_dirty() || this.m2o.is_dirty();
2861     },
2862     set_value: function(value) {
2863         this._super(value);
2864         if (typeof(value) === 'string') {
2865             var vals = value.split(',');
2866             this.selection.set_value(vals[0]);
2867             this.m2o.set_value(parseInt(vals[1], 10));
2868         }
2869     },
2870     get_value: function() {
2871         var model = this.selection.get_value(),
2872             id = this.m2o.get_value();
2873         if (typeof(model) === 'string' && typeof(id) === 'number') {
2874             return model + ',' + id;
2875         } else {
2876             return false;
2877         }
2878     }
2879 });
2880
2881 openerp.web.form.FieldBinary = openerp.web.form.Field.extend({
2882     init: function(view, node) {
2883         this._super(view, node);
2884         this.iframe = this.element_id + '_iframe';
2885         this.binary_value = false;
2886     },
2887     start: function() {
2888         this._super.apply(this, arguments);
2889         this.$element.find('input.oe-binary-file').change(this.on_file_change);
2890         this.$element.find('button.oe-binary-file-save').click(this.on_save_as);
2891         this.$element.find('.oe-binary-file-clear').click(this.on_clear);
2892     },
2893     human_filesize : function(size) {
2894         var units = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
2895         var i = 0;
2896         while (size >= 1024) {
2897             size /= 1024;
2898             ++i;
2899         }
2900         return size.toFixed(2) + ' ' + units[i];
2901     },
2902     on_file_change: function(e) {
2903         // TODO: on modern browsers, we could directly read the file locally on client ready to be used on image cropper
2904         // http://www.html5rocks.com/tutorials/file/dndfiles/
2905         // http://deepliquid.com/projects/Jcrop/demos.php?demo=handler
2906         window[this.iframe] = this.on_file_uploaded;
2907         if ($(e.target).val() != '') {
2908             this.$element.find('form.oe-binary-form input[name=session_id]').val(this.session.session_id);
2909             this.$element.find('form.oe-binary-form').submit();
2910             this.$element.find('.oe-binary-progress').show();
2911             this.$element.find('.oe-binary').hide();
2912         }
2913     },
2914     on_file_uploaded: function(size, name, content_type, file_base64) {
2915         delete(window[this.iframe]);
2916         if (size === false) {
2917             this.do_warn("File Upload", "There was a problem while uploading your file");
2918             // TODO: use openerp web crashmanager
2919             console.warn("Error while uploading file : ", name);
2920         } else {
2921             this.on_file_uploaded_and_valid.apply(this, arguments);
2922             this.on_ui_change();
2923         }
2924         this.$element.find('.oe-binary-progress').hide();
2925         this.$element.find('.oe-binary').show();
2926     },
2927     on_file_uploaded_and_valid: function(size, name, content_type, file_base64) {
2928     },
2929     on_save_as: function() {
2930         var url = '/web/binary/saveas?session_id=' + this.session.session_id + '&model=' +
2931             this.view.dataset.model +'&id=' + (this.view.datarecord.id || '') + '&field=' + this.name +
2932             '&fieldname=' + (this.node.attrs.filename || '') + '&t=' + (new Date().getTime());
2933         window.open(url);
2934     },
2935     on_clear: function() {
2936         if (this.value !== false) {
2937             this.value = false;
2938             this.binary_value = false;
2939             this.on_ui_change();
2940         }
2941         return false;
2942     }
2943 });
2944
2945 openerp.web.form.FieldBinaryFile = openerp.web.form.FieldBinary.extend({
2946     template: 'FieldBinaryFile',
2947     update_dom: function() {
2948         this._super.apply(this, arguments);
2949         this.$element.find('.oe-binary-file-set, .oe-binary-file-clear').toggle(!this.readonly);
2950         this.$element.find('input[type=text]').attr('disabled', this.readonly);
2951     },
2952     set_value: function(value) {
2953         this._super.apply(this, arguments);
2954         var show_value = (value != null && value !== false) ? value : '';
2955         this.$element.find('input').eq(0).val(show_value);
2956     },
2957     on_file_uploaded_and_valid: function(size, name, content_type, file_base64) {
2958         this.value = file_base64;
2959         this.binary_value = true;
2960         var show_value = this.human_filesize(size);
2961         this.$element.find('input').eq(0).val(show_value);
2962         this.set_filename(name);
2963     },
2964     set_filename: function(value) {
2965         var filename = this.node.attrs.filename;
2966         if (this.view.fields[filename]) {
2967             this.view.fields[filename].set_value(value);
2968             this.view.fields[filename].on_ui_change();
2969         }
2970     },
2971     on_clear: function() {
2972         this._super.apply(this, arguments);
2973         this.$element.find('input').eq(0).val('');
2974         this.set_filename('');
2975     }
2976 });
2977
2978 openerp.web.form.FieldBinaryImage = openerp.web.form.FieldBinary.extend({
2979     template: 'FieldBinaryImage',
2980     start: function() {
2981         this._super.apply(this, arguments);
2982         this.$image = this.$element.find('img.oe-binary-image');
2983     },
2984     update_dom: function() {
2985         this._super.apply(this, arguments);
2986         this.$element.find('.oe-binary').toggle(!this.readonly);
2987     },
2988     set_value: function(value) {
2989         this._super.apply(this, arguments);
2990         this.set_image_maxwidth();
2991         var url = '/web/binary/image?session_id=' + this.session.session_id + '&model=' +
2992             this.view.dataset.model +'&id=' + (this.view.datarecord.id || '') + '&field=' + this.name + '&t=' + (new Date().getTime());
2993         this.$image.attr('src', url);
2994     },
2995     set_image_maxwidth: function() {
2996         this.$image.css('max-width', this.$element.width());
2997     },
2998     on_file_change: function() {
2999         this.set_image_maxwidth();
3000         this._super.apply(this, arguments);
3001     },
3002     on_file_uploaded_and_valid: function(size, name, content_type, file_base64) {
3003         this.value = file_base64;
3004         this.binary_value = true;
3005         this.$image.attr('src', 'data:' + (content_type || 'image/png') + ';base64,' + file_base64);
3006     },
3007     on_clear: function() {
3008         this._super.apply(this, arguments);
3009         this.$image.attr('src', '/web/static/src/img/placeholder.png');
3010     }
3011 });
3012
3013 openerp.web.form.FieldStatus = openerp.web.form.Field.extend({
3014     template: "EmptyComponent",
3015     start: function() {
3016         this._super();
3017         this.selected_value = null;
3018
3019         this.render_list();
3020     },
3021     set_value: function(value) {
3022         this._super(value);
3023         this.selected_value = value;
3024
3025         this.render_list();
3026     },
3027     render_list: function() {
3028         var self = this;
3029         var shown = _.map(((this.node.attrs || {}).statusbar_visible || "").split(","),
3030             function(x) { return _.str.trim(x); });
3031         shown = _.select(shown, function(x) { return x.length > 0; });
3032
3033         if (shown.length == 0) {
3034             this.to_show = this.field.selection;
3035         } else {
3036             this.to_show = _.select(this.field.selection, function(x) {
3037                 return _.indexOf(shown, x[0]) !== -1 || x[0] === self.selected_value;
3038             });
3039         }
3040
3041         var content = openerp.web.qweb.render("FieldStatus.content", {widget: this, _:_});
3042         this.$element.html(content);
3043
3044         var colors = JSON.parse((this.node.attrs || {}).statusbar_colors || "{}");
3045         var color = colors[this.selected_value];
3046         if (color) {
3047             var elem = this.$element.find("li.oe-arrow-list-selected span");
3048             elem.css("border-color", color);
3049             if (this.check_white(color))
3050                 elem.css("color", "white");
3051             elem = this.$element.find("li.oe-arrow-list-selected .oe-arrow-list-before");
3052             elem.css("border-left-color", "rgba(0,0,0,0)");
3053             elem = this.$element.find("li.oe-arrow-list-selected .oe-arrow-list-after");
3054             elem.css("border-color", "rgba(0,0,0,0)");
3055             elem.css("border-left-color", color);
3056         }
3057     },
3058     check_white: function(color) {
3059         var div = $("<div></div>");
3060         div.css("display", "none");
3061         div.css("color", color);
3062         div.appendTo($("body"));
3063         var ncolor = div.css("color");
3064         div.remove();
3065         var res = /^\s*rgb\s*\(\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)\s*\)\s*$/.exec(ncolor);
3066         if (!res) {
3067             return false;
3068         }
3069         var comps = [parseInt(res[1]), parseInt(res[2]), parseInt(res[3])];
3070         var lum = comps[0] * 0.3 + comps[1] * 0.59 + comps[1] * 0.11;
3071         if (lum < 128) {
3072             return true;
3073         }
3074         return false;
3075     }
3076 });
3077
3078 openerp.web.form.FieldReadonly = openerp.web.form.Field.extend({
3079
3080 });
3081 openerp.web.form.WidgetFrameReadonly = openerp.web.form.WidgetFrame.extend({
3082     template: 'WidgetFrame.readonly'
3083 });
3084 openerp.web.form.FieldCharReadonly = openerp.web.form.FieldReadonly.extend({
3085     template: 'FieldChar.readonly',
3086     init: function(view, node) {
3087         this._super(view, node);
3088         this.password = this.node.attrs.password === 'True' || this.node.attrs.password === '1';
3089     },
3090     set_value: function (value) {
3091         this._super.apply(this, arguments);
3092         var show_value = openerp.web.format_value(value, this, '');
3093         if (this.password) {
3094             show_value = new Array(show_value.length + 1).join('*');
3095         }
3096         this.$element.find('div').text(show_value);
3097         return show_value;
3098     }
3099 });
3100 openerp.web.form.FieldURIReadonly = openerp.web.form.FieldCharReadonly.extend({
3101     template: 'FieldURI.readonly',
3102     scheme: null,
3103     set_value: function (value) {
3104         var displayed = this._super.apply(this, arguments);
3105         this.$element.find('a')
3106                 .attr('href', this.scheme + ':' + displayed)
3107                 .text(displayed);
3108     }
3109 });
3110 openerp.web.form.FieldEmailReadonly = openerp.web.form.FieldURIReadonly.extend({
3111     scheme: 'mailto'
3112 });
3113 openerp.web.form.FieldUrlReadonly = openerp.web.form.FieldURIReadonly.extend({
3114     set_value: function (value) {
3115         var s = /(\w+):(.+)/.exec(value);
3116         if (!s || !(s[1] === 'http' || s[1] === 'https')) { return; }
3117         this.scheme = s[1];
3118         this._super(s[2]);
3119     }
3120 });
3121 openerp.web.form.FieldBooleanReadonly = openerp.web.form.FieldCharReadonly.extend({
3122     set_value: function (value) {
3123         this._super(value ? '\u2611' : '\u2610');
3124     }
3125 });
3126 openerp.web.form.FieldSelectionReadonly = openerp.web.form.FieldReadonly.extend({
3127     template: 'FieldChar.readonly',
3128     init: function(view, node) {
3129         // lifted straight from r/w version
3130         var self = this;
3131         this._super(view, node);
3132         this.values = _.clone(this.field.selection);
3133         _.each(this.values, function(v, i) {
3134             if (v[0] === false && v[1] === '') {
3135                 self.values.splice(i, 1);
3136             }
3137         });
3138         this.values.unshift([false, '']);
3139     },
3140     set_value: function (value) {
3141         value = value === null ? false : value;
3142         value = value instanceof Array ? value[0] : value;
3143         var option = _(this.values)
3144             .detect(function (record) { return record[0] === value; });
3145         this._super(value);
3146         this.$element.find('div').text(option ? option[1] : this.values[0][1]);
3147     }
3148 });
3149 openerp.web.form.FieldMany2OneReadonly = openerp.web.form.FieldURIReadonly.extend({
3150     set_value: function (value) {
3151         value = value || null;
3152         this.invalid = false;
3153         var self = this;
3154         this.value = value;
3155         self.update_dom();
3156         self.on_value_changed();
3157         var real_set_value = function(rval) {
3158             self.value = rval;
3159             self.$element.find('a')
3160                  .unbind('click')
3161                  .text(rval ? rval[1] : '')
3162                  .click(function () {
3163                     self.do_action({
3164                         type: 'ir.actions.act_window',
3165                         res_model: self.field.relation,
3166                         res_id: self.value[0],
3167                         context: self.build_context(),
3168                         views: [[false, 'form']],
3169                         target: 'current'
3170                     });
3171                     return false;
3172                  });
3173         };
3174         if (value && !(value instanceof Array)) {
3175             new openerp.web.DataSetStatic(
3176                     this, this.field.relation, self.build_context())
3177                 .name_get([value], function(data) {
3178                     real_set_value(data[0]);
3179             });
3180         } else {
3181             setTimeout(function() {real_set_value(value);}, 0);
3182         }
3183     },
3184     get_value: function() {
3185         if (!this.value) {
3186             return false;
3187         } else if (this.value instanceof Array) {
3188             return this.value[0];
3189         } else {
3190             return this.value;
3191         }
3192     }
3193 });
3194
3195 /**
3196  * Registry of form widgets, called by :js:`openerp.web.FormView`
3197  */
3198 openerp.web.form.widgets = new openerp.web.Registry({
3199     'frame' : 'openerp.web.form.WidgetFrame',
3200     'group' : 'openerp.web.form.WidgetGroup',
3201     'notebook' : 'openerp.web.form.WidgetNotebook',
3202     'notebookpage' : 'openerp.web.form.WidgetNotebookPage',
3203     'separator' : 'openerp.web.form.WidgetSeparator',
3204     'label' : 'openerp.web.form.WidgetLabel',
3205     'button' : 'openerp.web.form.WidgetButton',
3206     'char' : 'openerp.web.form.FieldChar',
3207     'email' : 'openerp.web.form.FieldEmail',
3208     'url' : 'openerp.web.form.FieldUrl',
3209     'text' : 'openerp.web.form.FieldText',
3210     'text_wiki' : 'openerp.web.form.FieldText',
3211     'date' : 'openerp.web.form.FieldDate',
3212     'datetime' : 'openerp.web.form.FieldDatetime',
3213     'selection' : 'openerp.web.form.FieldSelection',
3214     'many2one' : 'openerp.web.form.FieldMany2One',
3215     'many2many' : 'openerp.web.form.FieldMany2Many',
3216     'one2many' : 'openerp.web.form.FieldOne2Many',
3217     'one2many_list' : 'openerp.web.form.FieldOne2Many',
3218     'reference' : 'openerp.web.form.FieldReference',
3219     'boolean' : 'openerp.web.form.FieldBoolean',
3220     'float' : 'openerp.web.form.FieldFloat',
3221     'integer': 'openerp.web.form.FieldFloat',
3222     'float_time': 'openerp.web.form.FieldFloat',
3223     'progressbar': 'openerp.web.form.FieldProgressBar',
3224     'image': 'openerp.web.form.FieldBinaryImage',
3225     'binary': 'openerp.web.form.FieldBinaryFile',
3226     'statusbar': 'openerp.web.form.FieldStatus'
3227 });
3228
3229 openerp.web.form.FieldMany2ManyReadonly = openerp.web.form.FieldMany2Many.extend({
3230     force_readonly: true
3231 });
3232 openerp.web.form.FieldOne2ManyReadonly = openerp.web.form.FieldOne2Many.extend({
3233     force_readonly: true
3234 });
3235 openerp.web.form.readonly = openerp.web.form.widgets.clone({
3236     'frame': 'openerp.web.form.WidgetFrameReadonly',
3237     'char': 'openerp.web.form.FieldCharReadonly',
3238     'email': 'openerp.web.form.FieldEmailReadonly',
3239     'url': 'openerp.web.form.FieldUrlReadonly',
3240     'text': 'openerp.web.form.FieldCharReadonly',
3241     'text_wiki' : 'openerp.web.form.FieldCharReadonly',
3242     'date': 'openerp.web.form.FieldCharReadonly',
3243     'datetime': 'openerp.web.form.FieldCharReadonly',
3244     'selection' : 'openerp.web.form.FieldSelectionReadonly',
3245     'many2one': 'openerp.web.form.FieldMany2OneReadonly',
3246     'many2many' : 'openerp.web.form.FieldMany2ManyReadonly',
3247     'one2many' : 'openerp.web.form.FieldOne2ManyReadonly',
3248     'one2many_list' : 'openerp.web.form.FieldOne2ManyReadonly',
3249     'boolean': 'openerp.web.form.FieldBooleanReadonly',
3250     'float': 'openerp.web.form.FieldCharReadonly',
3251     'integer': 'openerp.web.form.FieldCharReadonly',
3252     'float_time': 'openerp.web.form.FieldCharReadonly'
3253 });
3254
3255 };
3256
3257 // vim:et fdc=0 fdl=0 foldnestmax=3 fdm=syntax: