1 openerp.web.form = function (openerp) {
3 var _t = openerp.web._t,
5 var QWeb = openerp.web.qweb;
7 openerp.web.views.add('form', 'openerp.web.FormView');
8 openerp.web.FormView = openerp.web.View.extend( /** @lends openerp.web.FormView# */{
10 * Indicates that this view is not searchable, and thus that no search
11 * view should be displayed (if there is one active).
16 display_name: _lt('Form'),
18 * @constructs openerp.web.FormView
19 * @extends openerp.web.View
21 * @param {openerp.web.Session} session the current openerp session
22 * @param {openerp.web.DataSet} dataset the dataset this view will work with
23 * @param {String} view_id the identifier of the OpenERP view object
24 * @param {Object} options
25 * - sidebar : [true|false]
26 * - resize_textareas : [true|false|max_height]
28 * @property {openerp.web.Registry} registry=openerp.web.form.widgets widgets registry for this form view instance
30 init: function(parent, dataset, view_id, options) {
32 this.set_default_options(options);
33 this.dataset = dataset;
34 this.model = dataset.model;
35 this.view_id = view_id || false;
36 this.fields_view = {};
38 this.widgets_counter = 0;
40 this.fields_order = [];
42 this.default_focus_field = null;
43 this.default_focus_button = null;
44 this.registry = openerp.web.form.widgets;
45 this.has_been_loaded = $.Deferred();
46 this.$form_header = null;
47 this.translatable_fields = [];
48 _.defaults(this.options, {
49 "not_interactible_on_create": false
51 this.is_initialized = $.Deferred();
52 this.mutating_mutex = new $.Mutex();
53 this.on_change_mutex = new $.Mutex();
54 this.reload_mutex = new $.Mutex();
55 this.set({"force_readonly": false});
59 return this.init_view();
61 init_view: function() {
62 if (this.embedded_view) {
63 var def = $.Deferred().then(this.on_loaded);
65 $.async_when().then(function() {def.resolve(self.embedded_view);});
68 var context = new openerp.web.CompoundContext(this.dataset.get_context());
69 return this.rpc("/web/view/load", {
71 "view_id": this.view_id,
73 toolbar: this.options.sidebar,
80 this.sidebar.attachments.destroy();
81 this.sidebar.destroy();
83 _.each(this.widgets, function(w) {
88 reposition: function ($e) {
92 on_loaded: function(data) {
95 throw new Error("No data provided.");
98 throw "Form view does not support multiple calls to on_loaded";
100 this.fields_order = [];
101 this.fields_view = data;
103 var form = new openerp.web.FormRenderingEngine(this, data);
104 form.appendTo(this.$element.find('.oe_form_content'));
106 this.$form_header = this.$element.find('.oe_form_header:first');
107 this.$form_header.find('div.oe_form_pager button[data-pager-action]').click(function() {
108 var action = $(this).data('pager-action');
109 self.on_pager_action(action);
112 this.$form_header.find('button.oe_form_button_save').click(this.on_button_save);
113 this.$form_header.find('button.oe_form_button_cancel').click(this.on_button_cancel);
115 if (!this.sidebar && this.options.sidebar && this.options.sidebar_id) {
116 this.sidebar = new openerp.web.Sidebar(this, this.options.sidebar_id);
117 this.sidebar.start();
118 this.sidebar.do_unfold();
119 this.sidebar.attachments = new openerp.web.form.SidebarAttachments(this.sidebar, this);
120 this.sidebar.add_toolbar(this.fields_view.toolbar);
121 this.set_common_sidebar_sections(this.sidebar);
123 this.sidebar.add_section(_t('Customize'), 'customize');
124 this.sidebar.add_items('customize', [{
125 label: _t('Set Default'),
127 callback: function (item) {
128 item.form.open_defaults_dialog();
132 this.has_been_loaded.resolve();
135 do_load_state: function(state, warm) {
136 if (state.id && this.datarecord.id != state.id) {
137 if (!this.dataset.get_id_index(state.id)) {
138 this.dataset.ids.push(state.id);
140 this.dataset.select_id(state.id);
147 do_show: function () {
149 this.$element.show().css('visibility', 'hidden');
150 this.$element.removeClass('oe_form_dirty');
151 return this.has_been_loaded.pipe(function() {
153 if (self.dataset.index === null) {
154 // null index means we should start a new record
155 result = self.on_button_new();
157 result = self.dataset.read_index(_.keys(self.fields_view.fields), {
158 context : { 'bin_size' : true }
159 }).pipe(self.on_record_loaded);
161 result.pipe(function() {
162 self.$element.css('visibility', 'visible');
165 self.sidebar.$element.show();
170 do_hide: function () {
173 this.sidebar.$element.hide();
176 on_record_loaded: function(record) {
177 var self = this, set_values = [];
179 this.do_warn("Form", "The record could not be found in the database.", true);
180 return $.Deferred().reject();
182 this.datarecord = record;
184 _(this.fields).each(function (field, f) {
186 var result = field.set_value(self.datarecord[f] || false);
187 set_values.push(result);
188 $.when(result).then(function() {
192 return $.when.apply(null, set_values).pipe(function() {
194 // New record: Second pass in order to trigger the onchanges
195 // respecting the fields order defined in the view
196 _.each(self.fields_order, function(field_name) {
197 if (record[field_name] !== undefined) {
198 var field = self.fields[field_name];
200 self.do_onchange(field);
204 self.on_form_changed();
205 self.is_initialized.resolve();
206 self.do_update_pager(record.id == null);
208 self.sidebar.attachments.do_update();
210 if (self.default_focus_field) {
211 self.default_focus_field.focus();
214 self.do_push_state({id:record.id});
216 self.$element.removeClass('oe_form_dirty');
219 on_form_changed: function() {
220 for (var w in this.widgets) {
222 w.process_modifiers();
229 do_notify_change: function() {
230 this.$element.addClass('oe_form_dirty');
232 on_pager_action: function(action) {
233 if (this.can_be_discarded()) {
236 this.dataset.index = 0;
239 this.dataset.previous();
245 this.dataset.index = this.dataset.ids.length - 1;
251 do_update_pager: function(hide_index) {
252 var $pager = this.$form_header.find('div.oe_form_pager');
253 var index = hide_index ? '-' : this.dataset.index + 1;
254 $pager.find('button').prop('disabled', this.dataset.ids.length < 2);
255 $pager.find('span.oe_pager_index').html(index);
256 $pager.find('span.oe_pager_count').html(this.dataset.ids.length);
258 parse_on_change: function (on_change, widget) {
260 var onchange = _.str.trim(on_change);
261 var call = onchange.match(/^\s?(.*?)\((.*?)\)\s?$/);
266 var method = call[1];
267 if (!_.str.trim(call[2])) {
268 return {method: method, args: [], context_index: null}
271 var argument_replacement = {
272 'False': function () {return false;},
273 'True': function () {return true;},
274 'None': function () {return null;},
275 'context': function (i) {
277 var ctx = new openerp.web.CompoundContext(self.dataset.get_context(), widget.build_context() ? widget.build_context() : {});
281 var parent_fields = null, context_index = null;
282 var args = _.map(call[2].split(','), function (a, i) {
283 var field = _.str.trim(a);
285 // literal constant or context
286 if (field in argument_replacement) {
287 return argument_replacement[field](i);
290 if (/^-?\d+(\.\d+)?$/.test(field)) {
291 return Number(field);
294 if (self.fields[field]) {
295 var value = self.fields[field].get_on_change_value();
296 return value == null ? false : value;
299 var splitted = field.split('.');
300 if (splitted.length > 1 && _.str.trim(splitted[0]) === "parent" && self.dataset.parent_view) {
301 if (parent_fields === null) {
302 parent_fields = self.dataset.parent_view.get_fields_values([self.dataset.child_name]);
304 var p_val = parent_fields[_.str.trim(splitted[1])];
305 if (p_val !== undefined) {
306 return p_val == null ? false : p_val;
310 var first_char = field[0], last_char = field[field.length-1];
311 if ((first_char === '"' && last_char === '"')
312 || (first_char === "'" && last_char === "'")) {
313 return field.slice(1, -1);
316 throw new Error("Could not get field with name '" + field +
317 "' for onchange '" + onchange + "'");
323 context_index: context_index
326 do_onchange: function(widget, processed) {
328 return this.on_change_mutex.exec(function() {
330 var response = {}, can_process_onchange = $.Deferred();
331 processed = processed || [];
332 processed.push(widget.name);
333 var on_change = widget.node.attrs.on_change;
335 var change_spec = self.parse_on_change(on_change, widget);
338 url: '/web/dataset/onchange',
341 can_process_onchange = self.rpc(ajax, {
342 model: self.dataset.model,
343 method: change_spec.method,
344 args: [(self.datarecord.id == null ? [] : [self.datarecord.id])].concat(change_spec.args),
345 context_id: change_spec.context_index == undefined ? null : change_spec.context_index + 1
346 }).then(function(r) {
347 _.extend(response, r);
350 console.warn("Wrong on_change format", on_change);
353 // fail if onchange failed
354 if (can_process_onchange.isRejected()) {
355 return can_process_onchange;
358 if (widget.field['change_default']) {
359 var fieldname = widget.name, value;
360 if (response.value && (fieldname in response.value)) {
361 // Use value from onchange if onchange executed
362 value = response.value[fieldname];
364 // otherwise get form value for field
365 value = self.fields[fieldname].get_on_change_value();
367 var condition = fieldname + '=' + value;
370 can_process_onchange = self.rpc({
371 url: '/web/dataset/call',
375 method: 'get_defaults',
376 args: [self.model, condition]
377 }).then(function (results) {
378 if (!results.length) { return; }
379 if (!response.value) {
382 for(var i=0; i<results.length; ++i) {
383 // [whatever, key, value]
384 var triplet = results[i];
385 response.value[triplet[1]] = triplet[2];
390 if (can_process_onchange.isRejected()) {
391 return can_process_onchange;
394 return self.on_processed_onchange(response, processed);
397 return $.Deferred().reject();
401 on_processed_onchange: function(response, processed) {
403 var result = response;
405 for (var f in result.value) {
406 if (!result.value.hasOwnProperty(f)) { continue; }
407 var field = this.fields[f];
408 // If field is not defined in the view, just ignore it
410 var value = result.value[f];
411 if (field.get_value() != value) {
412 field.set_value(value);
414 if (!_.contains(processed, field.name)) {
415 this.do_onchange(field, processed);
420 this.on_form_changed();
422 if (!_.isEmpty(result.warning)) {
423 openerp.web.dialog($(QWeb.render("CrashManagerWarning", result.warning)), {
426 {text: _t("Ok"), click: function() { $(this).dialog("close"); }}
431 function edit_domain(node) {
432 var new_domain = result.domain[node.attrs.name];
434 node.attrs.domain = new_domain;
436 _(node.children).each(edit_domain);
438 edit_domain(this.fields_view.arch);
440 return $.Deferred().resolve();
443 return $.Deferred().reject();
446 on_button_save: function() {
448 return this.do_save().then(function(result) {
449 self.do_prev_view({'created': result.created, 'default': 'page'});
452 on_button_cancel: function() {
453 if (this.can_be_discarded()) {
454 return this.do_prev_view({'default': 'page'});
457 on_button_new: function() {
459 var def = $.Deferred();
460 $.when(this.has_been_loaded).then(function() {
461 if (self.can_be_discarded()) {
462 var keys = _.keys(self.fields_view.fields);
464 self.dataset.default_get(keys).pipe(self.on_record_loaded).then(function() {
468 self.on_record_loaded({}).then(function() {
474 return def.promise();
476 can_be_discarded: function() {
477 return !this.$element.is('.oe_form_dirty') || confirm(_t("Warning, the record has been modified, your changes will be discarded."));
480 * Triggers saving the form's record. Chooses between creating a new
481 * record or saving an existing one depending on whether the record
482 * already has an id property.
484 * @param {Function} success callback on save success
485 * @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)
487 do_save: function(success, prepend_on_create) {
489 return this.mutating_mutex.exec(function() { return self.is_initialized.pipe(function() {
491 var form_invalid = false,
493 first_invalid_field = null;
494 for (var f in self.fields) {
499 if (!first_invalid_field) {
500 first_invalid_field = f;
502 } else if (f.name !== 'id' && !f.readonly && (!self.datarecord.id || f.is_dirty())) {
503 // Special case 'id' field, do not save this field
504 // on 'create' : save all non readonly fields
505 // on 'edit' : save non readonly modified fields
506 values[f.name] = f.get_value();
510 first_invalid_field.focus();
512 return $.Deferred().reject();
515 if (!self.datarecord.id) {
516 //console.log("FormView(", self, ") : About to create", values);
517 save_deferral = self.dataset.create(values).pipe(function(r) {
518 return self.on_created(r, undefined, prepend_on_create);
520 } else if (_.isEmpty(values)) {
521 //console.log("FormView(", self, ") : Nothing to save");
522 save_deferral = $.Deferred().resolve({}).promise();
524 //console.log("FormView(", self, ") : About to save", values);
525 save_deferral = self.dataset.write(self.datarecord.id, values, {}).pipe(function(r) {
526 return self.on_saved(r);
529 return save_deferral.then(success);
533 return $.Deferred().reject();
537 on_invalid: function() {
539 _.each(this.fields, function(f) {
541 msg += "<li>" + f.node.attrs.string + "</li>";
545 this.do_warn("The following fields are invalid :", msg);
547 on_saved: function(r, success) {
549 // should not happen in the server, but may happen for internal purpose
550 return $.Deferred().reject();
552 return $.when(this.reload()).pipe(function () {
553 return $.when(r).then(success); }, null);
557 * Updates the form' dataset to contain the new record:
559 * * Adds the newly created record to the current dataset (at the end by
561 * * Selects that record (sets the dataset's index to point to the new
563 * * Updates the pager and sidebar displays
566 * @param {Function} success callback to execute after having updated the dataset
567 * @param {Boolean} [prepend_on_create=false] adds the newly created record at the beginning of the dataset instead of the end
569 on_created: function(r, success, prepend_on_create) {
571 // should not happen in the server, but may happen for internal purpose
572 return $.Deferred().reject();
574 this.datarecord.id = r.result;
575 if (!prepend_on_create) {
576 this.dataset.alter_ids(this.dataset.ids.concat([this.datarecord.id]));
577 this.dataset.index = this.dataset.ids.length - 1;
579 this.dataset.alter_ids([this.datarecord.id].concat(this.dataset.ids));
580 this.dataset.index = 0;
582 this.do_update_pager();
584 this.sidebar.attachments.do_update();
586 //openerp.log("The record has been created with id #" + this.datarecord.id);
588 return $.when(_.extend(r, {created: true})).then(success);
591 on_action: function (action) {
592 console.debug('Executing action', action);
596 return this.reload_mutex.exec(function() {
597 if (self.dataset.index == null || self.dataset.index < 0) {
598 return $.when(self.on_button_new());
600 return self.dataset.read_index(_.keys(self.fields_view.fields), {
601 context : { 'bin_size' : true }
602 }).pipe(self.on_record_loaded);
606 get_fields_values: function(blacklist) {
607 blacklist = blacklist || [];
609 var ids = this.get_selected_ids();
610 values["id"] = ids.length > 0 ? ids[0] : false;
611 _.each(this.fields, function(value, key) {
612 if (_.include(blacklist, key))
614 var val = value.get_value();
619 get_selected_ids: function() {
620 var id = this.dataset.ids[this.dataset.index];
621 return id ? [id] : [];
623 recursive_save: function() {
625 return $.when(this.do_save()).pipe(function(res) {
626 if (self.dataset.parent_view)
627 return self.dataset.parent_view.recursive_save();
630 is_dirty: function() {
631 return _.any(this.fields, function (value) {
632 return value.is_dirty();
635 is_interactible_record: function() {
636 var id = this.datarecord.id;
638 if (this.options.not_interactible_on_create)
640 } else if (typeof(id) === "string") {
641 if(openerp.web.BufferedDataSet.virtual_id_regex.test(id))
646 sidebar_context: function () {
647 return this.do_save().pipe(_.bind(function() {return this.get_fields_values();}, this));
649 open_defaults_dialog: function () {
651 var fields = _.chain(this.fields)
652 .map(function (field, name) {
653 var value = field.get_value();
654 // ignore fields which are empty, invisible, readonly, o2m
659 || field.field.type === 'one2many'
660 || field.field.type === 'many2many') {
664 switch(field.field.type) {
666 displayed = _(field.values).find(function (option) {
667 return option[0] === value;
671 displayed = field.value[1] || value;
679 string: field.node_atts.string,
681 displayed: displayed,
682 // convert undefined to false
683 change_default: !!field.field.change_default
687 .sortBy(function (field) { return field.node_atts.string; })
689 var conditions = _.chain(fields)
690 .filter(function (field) { return field.change_default; })
693 var d = new openerp.web.Dialog(this, {
694 title: _t("Set Default"),
697 conditions: conditions
700 {text: _t("Close"), click: function () { d.close(); }},
701 {text: _t("Save default"), click: function () {
702 var $defaults = d.$element.find('#formview_default_fields');
703 var field_to_set = $defaults.val();
705 $defaults.parent().addClass('invalid');
708 var condition = d.$element.find('#formview_default_conditions').val(),
709 all_users = d.$element.find('#formview_default_all').is(':checked');
710 new openerp.web.DataSet(self, 'ir.values').call(
714 self.fields[field_to_set].get_value(),
718 ]).then(function () { d.close(); });
722 d.template = 'FormView.set_default';
727 openerp.web.FormRenderingEngine = openerp.web.Widget.extend({
728 init: function(parent, fvg, registry) {
730 this._super.apply(this, arguments);
733 this.fields_prefix = this.view.dataset ? this.view.dataset.model : '';
735 // TODO: I know this will save the world and all the kitten for a moment,
736 // but one day, we will have to get rid of xml2json
737 var xml = openerp.web.json_node_to_xml(fvg.arch),
738 $form = this.$form = $(xml);
740 // TODO: extract embeded views before preprocessing
741 _.each(['field', 'group', 'notebook', 'separator', 'label'], function(tag) {
742 var fn = self['process_' + tag];
743 if (registry && registry.contains(tag)) {
744 fn = registry.get_object(tag);
746 $form.find(tag).each(function() {
747 fn.call(self, $(this), $form);
753 this._super.apply(this, arguments);
754 this.$form.children().appendTo(this.$element);
755 // OpenERP views spec :
756 // - @width is obsolete ?
757 // TODO: modifiers invisible. Add a special attribute, eg: data-invisible that should be used in order to create openerp.form.InvisibleWidgetG
759 this.$element.find('field, button').each(function() {
761 key = $elem.attr('widget') || $elem[0].tagName.toLowerCase();
762 if (self.view.registry.contains(key)) {
763 var obj = self.view.registry.get_object(key);
764 var w = new (obj)(self.view, openerp.web.xml_to_json($elem[0]));
769 process_field: function($field, $form) {
770 var name = $field.attr('name'),
771 field_orm = this.fvg.fields[name],
772 field_string = $field.attr('string') || field_orm.string || '',
773 field_help = $field.attr('help') || field_orm.help || '',
774 field_colspan = parseInt($field.attr('colspan'), 10);
777 throw new Error("Field '" + name + "' specified in view could not be found.");
780 if ($field.attr('nolabel') !== '1') {
781 var $label = $form.find('label[for="' + name + '"]');
782 if (!$label.length) {
783 field_string = $label.attr('string') || $label.text() || field_string;
784 field_help = $label.attr('help') || field_help;
787 'string' : field_string,
789 }).insertBefore($field).text();
790 if (field_colspan > 1) {
791 $field.attr('colspan', field_colspan - 1);
796 'widget' : $field.attr('widget') || field_orm.type,
797 'string' : field_string,
801 process_group: function($group, $form) {
803 $new_group = $(QWeb.render('FormRenderingGroup', $group.getAttributes())),
805 if ($new_group.is('table')) {
808 $table = $new_group.find('table:first');
810 $table.addClass('oe_form_group');
812 cols = parseInt($group.attr('col') || 4, 10),
815 $group.children().each(function() {
816 var $child = $(this),
817 colspan = parseInt($child.attr('colspan') || 1, 10),
818 tagName = $child[0].tagName.toLowerCase();
819 if (tagName === 'newline') {
823 if (!$tr || row_cols < colspan) {
824 $tr = $('<tr/>').addClass('oe_form_group_row').appendTo($table);
828 var $td = $('<td/>').addClass('oe_form_group_cell').attr('colspan', colspan);
829 $tr.append($td.append($child));
831 $group.before($new_group).remove();
833 // Now compute width of cells
834 $table.find('tbody > tr').each(function() {
837 $(this).children().each(function() {
839 $child = $td.children(':first');
840 switch ($child[0].tagName.toLowerCase()) {
842 if ($child.attr('orientation') === 'vertical') {
843 $td.addClass('oe_vertical_separator').attr('width', '1');
849 if ($child.attr('for')) {
850 $td.attr('width', '1%');
856 to_compute.push($td);
859 var unit = Math.floor(total / cols);
860 _.each(to_compute, function($td, i) {
861 var width = parseInt($td.attr('colspan'), 10) * unit;
862 $td.attr('width', ((i == to_compute.length - 1) ? total : width) + '%');
867 process_notebook: function($notebook, $form) {
869 $notebook.find('> page').each(function() {
871 page_attrs = $page.getAttributes();
872 page_attrs.id = _.uniqueId('notebook_page_');
873 pages.push(page_attrs);
874 var $new_page = $(QWeb.render('FormRenderingNotebookPage', page_attrs));
875 $page.children().appendTo($new_page);
876 $page.before($new_page).remove();
878 var $new_notebook = $(QWeb.render('FormRenderingNotebook', { pages : pages }));
879 $notebook.children().appendTo($new_notebook);
880 $notebook.before($new_notebook).remove();
881 $new_notebook.tabs();
883 process_separator: function($separator, $form) {
884 var $new_separator = $(QWeb.render('FormRenderingSeparator', $separator.getAttributes()));
885 $separator.before($new_separator).remove();
887 process_label: function($label, $form) {
888 var dict = $label.getAttributes();
889 var align = parseFloat(dict.align);
890 if (isNaN(align) || align === 1) {
892 } else if (align === 0) {
898 var $new_label = $(QWeb.render('FormRenderingLabel', dict));
899 $label.before($new_label).remove();
903 openerp.web.FormDialog = openerp.web.Dialog.extend({
904 init: function(parent, options, view_id, dataset) {
905 this._super(parent, options);
906 this.dataset = dataset;
907 this.view_id = view_id;
912 this.form = new openerp.web.FormView(this, this.dataset, this.view_id, {
916 this.form.appendTo(this.$element);
917 this.form.on_created.add_last(this.on_form_dialog_saved);
918 this.form.on_saved.add_last(this.on_form_dialog_saved);
921 select_id: function(id) {
922 if (this.form.dataset.select_id(id)) {
923 return this.form.do_show();
925 this.do_warn("Could not find id in dataset");
926 return $.Deferred().reject();
929 on_form_dialog_saved: function(r) {
935 openerp.web.form = {};
937 openerp.web.form.SidebarAttachments = openerp.web.OldWidget.extend({
938 init: function(parent, form_view) {
939 var $section = parent.add_section(_t('Attachments'), 'attachments');
940 this.$div = $('<div class="oe-sidebar-attachments"></div>');
941 $section.append(this.$div);
943 this._super(parent, $section.attr('id'));
944 this.view = form_view;
946 do_update: function() {
947 if (!this.view.datarecord.id) {
948 this.on_attachments_loaded([]);
950 (new openerp.web.DataSetSearch(
951 this, 'ir.attachment', this.view.dataset.get_context(),
953 ['res_model', '=', this.view.dataset.model],
954 ['res_id', '=', this.view.datarecord.id],
955 ['type', 'in', ['binary', 'url']]
956 ])).read_slice(['name', 'url', 'type'], {}).then(this.on_attachments_loaded);
959 on_attachments_loaded: function(attachments) {
960 this.attachments = attachments;
961 this.$div.html(QWeb.render('FormView.sidebar.attachments', this));
962 this.$element.find('.oe-binary-file').change(this.on_attachment_changed);
963 this.$element.find('.oe-sidebar-attachment-delete').click(this.on_attachment_delete);
965 on_attachment_changed: function(e) {
966 window[this.element_id + '_iframe'] = this.do_update;
967 var $e = $(e.target);
968 if ($e.val() != '') {
969 this.$element.find('form.oe-binary-form').submit();
970 $e.parent().find('input[type=file]').prop('disabled', true);
971 $e.parent().find('button').prop('disabled', true).find('img, span').toggle();
974 on_attachment_delete: function(e) {
975 var self = this, $e = $(e.currentTarget);
976 var name = _.str.trim($e.parent().find('a.oe-sidebar-attachments-link').text());
977 if (confirm(_.str.sprintf(_t("Do you really want to delete the attachment %s?"), name))) {
978 this.rpc('/web/dataset/unlink', {
979 model: 'ir.attachment',
980 ids: [parseInt($e.attr('data-id'))]
982 $e.parent().remove();
983 self.do_notify("Delete an attachment", "The attachment '" + name + "' has been deleted");
989 openerp.web.form.compute_domain = function(expr, fields) {
991 for (var i = expr.length - 1; i >= 0; i--) {
993 if (ex.length == 1) {
994 var top = stack.pop();
997 stack.push(stack.pop() || top);
1000 stack.push(stack.pop() && top);
1006 throw new Error(_.str.sprintf(
1007 _t("Unknown operator %s in domain %s"),
1008 ex, JSON.stringify(expr)));
1012 var field = fields[ex[0]];
1014 throw new Error(_.str.sprintf(
1015 _t("Unknown field %s in domain %s"),
1016 ex[0], JSON.stringify(expr)));
1018 var field_value = field.get_value ? fields[ex[0]].get_value() : fields[ex[0]].value;
1022 switch (op.toLowerCase()) {
1025 stack.push(field_value == val);
1029 stack.push(field_value != val);
1032 stack.push(field_value < val);
1035 stack.push(field_value > val);
1038 stack.push(field_value <= val);
1041 stack.push(field_value >= val);
1044 if (!_.isArray(val)) val = [val];
1045 stack.push(_(val).contains(field_value));
1048 if (!_.isArray(val)) val = [val];
1049 stack.push(!_(val).contains(field_value));
1053 _t("Unsupported operator %s in domain %s"),
1054 op, JSON.stringify(expr));
1057 return _.all(stack, _.identity);
1060 openerp.web.form.Widget = openerp.web.Widget.extend(/** @lends openerp.web.form.Widget# */{
1062 * @constructs openerp.web.form.Widget
1063 * @extends openerp.web.Widget
1068 init: function(view, node) {
1071 this.modifiers = JSON.parse(this.node.attrs.modifiers || '{}');
1072 this.always_invisible = (this.modifiers.invisible && this.modifiers.invisible === true);
1076 this.invisible = this.modifiers['invisible'] === true;
1078 destroy: function() {
1079 this._super.apply(this, arguments);
1082 process_modifiers: function() {
1083 var compute_domain = openerp.web.form.compute_domain;
1084 for (var a in this.modifiers) {
1085 this[a] = compute_domain(this.modifiers[a], this.view.fields);
1088 update_dom: function() {
1089 this.$element.toggle(!this.invisible);
1091 do_attach_tooltip: function(widget, trigger, options) {
1092 widget = widget || this;
1093 trigger = trigger || this.$element;
1094 options = _.extend({
1099 var template = widget.template + '.tooltip';
1100 if (!QWeb.has_template(template)) {
1101 template = 'WidgetLabel.tooltip';
1103 return QWeb.render(template, {
1104 debug: openerp.connection.debug,
1107 gravity: $.fn.tipsy.autoBounds(50, 'nw'),
1112 trigger.tipsy(options);
1114 _build_view_fields_values: function(blacklist) {
1115 var a_dataset = this.view.dataset;
1116 var fields_values = this.view.get_fields_values(blacklist);
1117 var active_id = a_dataset.ids[a_dataset.index];
1118 _.extend(fields_values, {
1119 active_id: active_id || false,
1120 active_ids: active_id ? [active_id] : [],
1121 active_model: a_dataset.model,
1124 if (a_dataset.parent_view) {
1125 fields_values.parent = a_dataset.parent_view.get_fields_values([a_dataset.child_name]);
1127 return fields_values;
1129 _build_eval_context: function(blacklist) {
1130 var a_dataset = this.view.dataset;
1131 return new openerp.web.CompoundContext(a_dataset.get_context(), this._build_view_fields_values(blacklist));
1134 * Builds a new context usable for operations related to fields by merging
1135 * the fields'context with the action's context.
1137 build_context: function(blacklist) {
1138 // only use the model's context if there is not context on the node
1139 var v_context = this.node.attrs.context;
1141 v_context = (this.field || {}).context || {};
1144 if (v_context.__ref || true) { //TODO: remove true
1145 var fields_values = this._build_eval_context(blacklist);
1146 v_context = new openerp.web.CompoundContext(v_context).set_eval_context(fields_values);
1150 build_domain: function() {
1151 var f_domain = this.field.domain || [];
1152 var n_domain = this.node.attrs.domain || null;
1153 // if there is a domain on the node, overrides the model's domain
1154 var final_domain = n_domain !== null ? n_domain : f_domain;
1155 if (!(final_domain instanceof Array) || true) { //TODO: remove true
1156 var fields_values = this._build_eval_context();
1157 final_domain = new openerp.web.CompoundDomain(final_domain).set_eval_context(fields_values);
1159 return final_domain;
1163 openerp.web.form.WidgetButton = openerp.web.form.Widget.extend({
1164 template: 'WidgetButton',
1165 init: function(view, node) {
1166 this._super(view, node);
1167 this.force_disabled = false;
1168 this.string = (this.node.attrs.string || '').replace(/_/g, '');
1169 if (this.node.attrs.default_focus == '1') {
1170 // TODO fme: provide enter key binding to widgets
1171 this.view.default_focus_button = this;
1175 this._super.apply(this, arguments);
1176 this.$element.click(this.on_click);
1177 if (this.node.attrs.help || openerp.connection.debug) {
1178 this.do_attach_tooltip();
1181 on_click: function() {
1183 this.force_disabled = true;
1184 this.check_disable();
1185 this.execute_action().always(function() {
1186 self.force_disabled = false;
1187 self.check_disable();
1190 execute_action: function() {
1192 var exec_action = function() {
1193 if (self.node.attrs.confirm) {
1194 var def = $.Deferred();
1195 var dialog = openerp.web.dialog($('<div/>').text(self.node.attrs.confirm), {
1196 title: _t('Confirm'),
1199 {text: _t("Cancel"), click: function() {
1201 $(this).dialog("close");
1204 {text: _t("Ok"), click: function() {
1205 self.on_confirmed().then(function() {
1208 $(this).dialog("close");
1213 return def.promise();
1215 return self.on_confirmed();
1218 if (!this.node.attrs.special) {
1219 return this.view.recursive_save().pipe(exec_action);
1221 return exec_action();
1224 on_confirmed: function() {
1227 var context = this.node.attrs.context;
1228 if (context && context.__ref) {
1229 context = new openerp.web.CompoundContext(context);
1230 context.set_eval_context(this._build_eval_context());
1233 return this.view.do_execute_action(
1234 _.extend({}, this.node.attrs, {context: context}),
1235 this.view.dataset, this.view.datarecord.id, function () {
1239 update_dom: function() {
1240 this._super.apply(this, arguments);
1241 this.check_disable();
1243 check_disable: function() {
1244 var disabled = (this.readonly || this.force_disabled || !this.view.is_interactible_record());
1245 this.$element.prop('disabled', disabled);
1246 this.$element.css('color', disabled ? 'grey' : '');
1251 * Interface implemented by the form view or any other object
1252 * able to provide the features necessary for the fields to work.
1255 * - force_readonly: boolean, When it is true, all the fields should always appear
1256 * in read only mode, no matter what the value of their "readonly" property can be.
1259 openerp.web.form.FieldManagerInterface = {
1264 * Interface to be implemented by fields.
1267 * - readonly: boolean. If set to true the field should appear in readonly mode.
1273 openerp.web.form.FieldInterface = {
1275 * Constructor takes 2 arguments:
1276 * - field_manager: Implements FieldManagerInterface
1277 * - node: the "<field>" node in json form
1279 init: function(field_manager, node) {},
1281 * Called by the form view to indicate the value of the field.
1283 * set_value() may return an object that can be passed to $.when() that represents the moment when
1284 * the field has finished all operations necessary before the user can effectively use the widget.
1286 * Multiple calls to set_value() can occur at any time and must be handled correctly by the implementation,
1287 * regardless of any asynchronous operation currently running and the status of any promise that a
1288 * previous call to set_value() could have returned.
1290 * set_value() must be able, at any moment, to handle the syntax returned by the "read" method of the
1291 * osv class in the OpenERP server as well as the syntax used by the set_value() (see below). It must
1292 * also be able to handle any other format commonly used in the _defaults key on the models in the addons
1293 * as well as any format commonly returned in a on_change. It must be able to autodetect those formats as
1294 * no information is ever given to know which format is used.
1296 set_value: function(value) {},
1298 * Get the current value of the widget.
1300 * Must always return a syntaxically correct value to be passed to the "write" method of the osv class in
1301 * the OpenERP server, although it is not assumed to respect the constraints applied to the field.
1302 * For example if the field is marqued as "required", a call to get_value() can return false.
1304 * get_value() can also be called *before* a call to set_value() and, in that case, is supposed to
1305 * return a defaut value according to the type of field.
1307 * This method is always assumed to perform synchronously, it can not return a promise.
1309 * If there was no user interaction to modify the value of the field, it is always assumed that
1310 * get_value() return the same semantic value than the one passed in the last call to set_value(),
1311 * altough the syntax can be different. This can be the case for type of fields that have a different
1312 * syntax for "read" and "write" (example: m2o: set_value([0, "Administrator"]), get_value() => 0).
1314 get_value: function() {},
1318 * Abstract class for classes implementing FieldInterface.
1321 * - effective_readonly: when it is true, the widget is displayed as readonly. Vary depending
1322 * the values of the "readonly" property and the "force_readonly" property on the field manager.
1325 openerp.web.form.AbstractField = openerp.web.form.Widget.extend(/** @lends openerp.web.form.AbstractField# */{
1327 * @constructs openerp.web.form.AbstractField
1328 * @extends openerp.web.form.Widget
1330 * @param field_manager
1333 init: function(field_manager, node) {
1334 this._super(field_manager, node);
1335 this.name = this.node.attrs.name;
1336 this.value = undefined;
1337 this.view.fields[this.name] = this;
1338 this.view.fields_order.push(this.name);
1339 this.type = this.node.attrs.widget;
1340 this.field = this.view.fields_view.fields[this.name] || {};
1341 this.required = this.modifiers['required'] === true;
1342 this.invalid = this.dirty = false;
1344 // because I'm lazy to refactor right now
1345 this.on("change:readonly", this, function() {this.readonly = this.get("readonly");});
1347 // some events to make the property "effective_readonly" sync automatically with "readonly" and
1349 this.set({"readonly": this.modifiers['readonly'] === true});
1350 var test_effective_readonly = function() {
1351 this.set({"effective_readonly": this.get("readonly") || this.view.get("force_readonly")});
1353 this.view.on("change:readonly", this, test_effective_readonly);
1354 this.view.on("change:force_readonly", this, test_effective_readonly);
1355 _.bind(test_effective_readonly, this)();
1358 this.$label = this.view.$element.find('label[for="' + this.name + '"]');
1359 if (this.$label.length) {
1360 this.id_for_label = _.uniqueId(['field', this.type, this.name, ''].join('_'));
1361 this.$label.attr('for', this.id_for_label);
1368 this._super.apply(this, arguments);
1369 if (this.field.translate) {
1370 this.view.translatable_fields.push(this);
1371 this.$element.addClass('oe_form_field_translatable');
1372 this.$element.find('.oe_field_translate').click(this.on_translate);
1374 if (this.node.attrs.nolabel && openerp.connection.debug) {
1375 this.do_attach_tooltip(this, this.$element);
1378 set_value: function(value) {
1380 this.invalid = false;
1382 this.on_value_changed();
1384 set_value_from_ui: function() {
1385 this.on_value_changed();
1387 on_value_changed: function() {
1389 on_translate: function() {
1390 this.view.open_translate_dialog(this);
1392 get_value: function() {
1395 is_valid: function() {
1396 return !this.invalid;
1398 is_dirty: function() {
1399 return this.dirty && !this.readonly;
1401 get_on_change_value: function() {
1402 return this.get_value();
1404 update_dom: function(show_invalid) {
1405 this._super.apply(this, arguments);
1406 if (this.field.translate) {
1407 this.$element.find('.oe_field_translate').toggle(!!this.view.datarecord.id);
1409 if (!this.disable_utility_classes) {
1410 this.$element.toggleClass('disabled', this.readonly);
1411 this.$element.toggleClass('required', this.required);
1413 this.$element.toggleClass('invalid', !this.is_valid());
1416 // one more shit code to avoid refactoring this.readonly right now
1417 this.set({"readonly": this.readonly});
1419 on_ui_change: function() {
1422 if (this.is_valid()) {
1423 this.set_value_from_ui();
1424 this.view.do_onchange(this);
1425 this.view.on_form_changed(true);
1426 this.view.do_notify_change();
1428 this.update_dom(true);
1431 validate: function() {
1432 this.invalid = false;
1434 focus: function($element) {
1436 setTimeout(function() {
1444 get_definition_options: function() {
1445 if (!this.definition_options) {
1446 var str = this.node.attrs.options || '{}';
1447 this.definition_options = JSON.parse(str);
1449 return this.definition_options;
1454 * A mixin to apply on any field that has to completely re-render when its readonly state
1457 openerp.web.form.ReinitializeFieldMixin = {
1459 this.on("change:effective_readonly", this, function() {
1460 this.renderElement();
1462 this.render_value();
1466 bind_events: function() {},
1467 render_value: function() {},
1470 openerp.web.form.FieldChar = openerp.web.form.AbstractField.extend(_.extend({}, openerp.web.form.ReinitializeFieldMixin, {
1471 template: 'FieldChar',
1472 init: function (view, node) {
1473 this._super(view, node);
1474 this.password = this.node.attrs.password === 'True' || this.node.attrs.password === '1';
1478 openerp.web.form.ReinitializeFieldMixin.start.call(this);
1480 bind_events: function() {
1481 this.$element.find('input').change(this.on_ui_change);
1483 set_value: function(value) {
1484 this._super.apply(this, arguments);
1485 this.render_value();
1487 render_value: function() {
1488 var show_value = openerp.web.format_value(this.value, this, '');
1489 if (!this.get("effective_readonly")) {
1490 this.$element.find('input').val(show_value);
1492 if (this.password) {
1493 show_value = new Array(show_value.length + 1).join('*');
1495 this.$element.text(show_value);
1498 set_value_from_ui: function() {
1499 this.value = openerp.web.parse_value(this.$element.find('input').val(), this);
1502 validate: function() {
1503 this.invalid = false;
1505 var value = openerp.web.parse_value(this.$element.find('input').val(), this, '');
1506 this.invalid = this.required && value === '';
1508 this.invalid = true;
1511 focus: function($element) {
1512 this._super($element || this.$element.find('input:first'));
1516 openerp.web.form.FieldID = openerp.web.form.FieldChar.extend({
1520 openerp.web.form.FieldEmail = openerp.web.form.FieldChar.extend({
1521 template: 'FieldEmail',
1522 bind_events: function() {
1524 this.$element.find('button').click(this.on_button_clicked);
1526 render_value: function() {
1527 if (!this.get("effective_readonly")) {
1530 this.$element.find('a')
1531 .attr('href', 'mailto:' + this.value)
1535 on_button_clicked: function() {
1536 if (!this.value || !this.is_valid()) {
1537 this.do_warn("E-mail error", "Can't send email to invalid e-mail address");
1539 location.href = 'mailto:' + this.value;
1544 openerp.web.form.FieldUrl = openerp.web.form.FieldChar.extend({
1545 template: 'FieldUrl',
1546 bind_events: function() {
1548 this.$element.find('button').click(this.on_button_clicked);
1550 render_value: function() {
1551 if (!this.get("effective_readonly")) {
1554 var tmp = this.value;
1555 var s = /(\w+):(.+)/.exec(tmp);
1557 tmp = "http://" + this.value;
1559 this.$element.find('a').attr('href', tmp).text(tmp);
1562 on_button_clicked: function() {
1564 this.do_warn("Resource error", "This resource is empty");
1566 window.open(this.value);
1571 openerp.web.form.FieldFloat = openerp.web.form.FieldChar.extend({
1572 init: function (view, node) {
1573 this._super(view, node);
1574 if (this.node.attrs.digits) {
1575 this.parse_digits(this.node.attrs.digits);
1577 this.digits = this.field.digits;
1580 parse_digits: function (digits_attr) {
1581 // could use a Python parser instead.
1582 var match = /^\s*[\(\[](\d+),\s*(\d+)/.exec(digits_attr);
1583 return [parseInt(match[1], 10), parseInt(match[2], 10)];
1585 set_value: function(value) {
1586 if (value === false || value === undefined) {
1587 // As in GTK client, floats default to 0
1590 this._super.apply(this, [value]);
1594 openerp.web.DateTimeWidget = openerp.web.OldWidget.extend({
1595 template: "web.datetimepicker",
1596 jqueryui_object: 'datetimepicker',
1597 type_of_date: "datetime",
1598 init: function(parent) {
1599 this._super(parent);
1600 this.name = parent.name;
1604 this.$input = this.$element.find('input.oe_datepicker_master');
1605 this.$input_picker = this.$element.find('input.oe_datepicker_container');
1606 this.$input.change(this.on_change);
1608 onSelect: this.on_picker_select,
1612 showButtonPanel: true
1614 this.$element.find('img.oe_datepicker_trigger').click(function() {
1615 if (!self.readonly && !self.picker('widget').is(':visible')) {
1616 self.picker('setDate', self.value ? openerp.web.auto_str_to_date(self.value) : new Date());
1617 self.$input_picker.show();
1618 self.picker('show');
1619 self.$input_picker.hide();
1622 this.set_readonly(false);
1625 picker: function() {
1626 return $.fn[this.jqueryui_object].apply(this.$input_picker, arguments);
1628 on_picker_select: function(text, instance) {
1629 var date = this.picker('getDate');
1630 this.$input.val(date ? this.format_client(date) : '').change();
1632 set_value: function(value) {
1634 this.$input.val(value ? this.format_client(value) : '');
1636 get_value: function() {
1639 set_value_from_ui: function() {
1640 var value = this.$input.val() || false;
1641 this.value = this.parse_client(value);
1643 set_readonly: function(readonly) {
1644 this.readonly = readonly;
1645 this.$input.prop('readonly', this.readonly);
1646 this.$element.find('img.oe_datepicker_trigger').toggleClass('oe_input_icon_disabled', readonly);
1648 is_valid: function(required) {
1649 var value = this.$input.val();
1654 this.parse_client(value);
1661 parse_client: function(v) {
1662 return openerp.web.parse_value(v, {"widget": this.type_of_date});
1664 format_client: function(v) {
1665 return openerp.web.format_value(v, {"widget": this.type_of_date});
1667 on_change: function() {
1668 if (this.is_valid()) {
1669 this.set_value_from_ui();
1674 openerp.web.DateWidget = openerp.web.DateTimeWidget.extend({
1675 jqueryui_object: 'datepicker',
1676 type_of_date: "date"
1679 openerp.web.form.FieldDatetime = openerp.web.form.AbstractField.extend({
1680 template: "EmptyComponent",
1681 build_widget: function() {
1682 return new openerp.web.DateTimeWidget(this);
1686 this._super.apply(this, arguments);
1687 this.datewidget = this.build_widget();
1688 this.datewidget.on_change.add_last(this.on_ui_change);
1689 this.datewidget.appendTo(this.$element);
1691 set_value: function(value) {
1693 this.datewidget.set_value(value);
1695 get_value: function() {
1696 return this.datewidget.get_value();
1698 update_dom: function() {
1699 this._super.apply(this, arguments);
1700 this.datewidget.set_readonly(this.readonly);
1702 validate: function() {
1703 this.invalid = !this.datewidget.is_valid(this.required);
1705 focus: function($element) {
1706 this._super($element || this.datewidget.$input);
1710 openerp.web.form.FieldDate = openerp.web.form.FieldDatetime.extend({
1711 build_widget: function() {
1712 return new openerp.web.DateWidget(this);
1716 openerp.web.form.FieldText = openerp.web.form.AbstractField.extend({
1717 template: 'FieldText',
1719 this._super.apply(this, arguments);
1720 this.$textarea = this.$element.find('textarea').change(this.on_ui_change);
1721 this.resized = false;
1723 set_value: function(value) {
1724 this._super.apply(this, arguments);
1725 var show_value = openerp.web.format_value(value, this, '');
1726 this.$textarea.val(show_value);
1727 if (!this.resized && this.view.options.resize_textareas) {
1728 this.do_resize(this.view.options.resize_textareas);
1729 this.resized = true;
1732 update_dom: function() {
1733 this._super.apply(this, arguments);
1734 this.$textarea.prop('readonly', this.readonly);
1736 set_value_from_ui: function() {
1737 this.value = openerp.web.parse_value(this.$textarea.val(), this);
1740 validate: function() {
1741 this.invalid = false;
1743 var value = openerp.web.parse_value(this.$textarea.val(), this, '');
1744 this.invalid = this.required && value === '';
1746 this.invalid = true;
1749 focus: function($element) {
1750 this._super($element || this.$textarea);
1752 do_resize: function(max_height) {
1753 max_height = parseInt(max_height, 10);
1754 var $input = this.$textarea,
1755 $div = $('<div style="position: absolute; z-index: 1000; top: 0"/>').width($input.width()),
1757 $div.text($input.val());
1758 _.each('font-family,font-size,white-space'.split(','), function(style) {
1759 $div.css(style, $input.css(style));
1761 $div.appendTo($('body'));
1762 new_height = $div.height();
1763 if (new_height < 90) {
1766 if (!isNaN(max_height) && new_height > max_height) {
1767 new_height = max_height;
1770 $input.height(new_height);
1773 this.resized = false;
1777 openerp.web.form.FieldBoolean = openerp.web.form.AbstractField.extend({
1778 template: 'FieldBoolean',
1780 this._super.apply(this, arguments);
1781 this.$checkbox = $("input", this.$element);
1782 this.$element.click(this.on_ui_change);
1783 var check_readonly = function() {
1784 this.$checkbox.prop('disabled', this.get("effective_readonly"));
1786 this.on("change:effective_readonly", this, check_readonly);
1787 _.bind(check_readonly, this)();
1789 set_value: function(value) {
1790 this._super.apply(this, arguments);
1791 this.$checkbox[0].checked = value;
1793 set_value_from_ui: function() {
1794 this.value = this.$checkbox.is(':checked');
1795 this._super.apply(this, arguments);
1797 focus: function($element) {
1798 this._super($element || this.$checkbox);
1802 openerp.web.form.FieldProgressBar = openerp.web.form.AbstractField.extend({
1803 template: 'FieldProgressBar',
1805 this._super.apply(this, arguments);
1806 this.$element.progressbar({
1808 disabled: this.readonly
1811 set_value: function(value) {
1812 this._super.apply(this, arguments);
1813 var show_value = Number(value);
1814 if (isNaN(show_value)) {
1817 var formatted_value = openerp.web.format_value(show_value, { type : 'float' }, '0');
1818 this.$element.progressbar('option', 'value', show_value).find('span').html(formatted_value + '%');
1822 openerp.web.form.FieldTextXml = openerp.web.form.AbstractField.extend({
1823 // to replace view editor
1826 openerp.web.form.FieldSelection = openerp.web.form.AbstractField.extend(_.extend({}, openerp.web.form.ReinitializeFieldMixin, {
1827 template: 'FieldSelection',
1828 init: function(view, node) {
1830 this._super(view, node);
1831 this.values = _.clone(this.field.selection);
1832 _.each(this.values, function(v, i) {
1833 if (v[0] === false && v[1] === '') {
1834 self.values.splice(i, 1);
1837 this.values.unshift([false, '']);
1841 openerp.web.form.ReinitializeFieldMixin.start.call(this);
1843 bind_events: function() {
1844 // Flag indicating whether we're in an event chain containing a change
1845 // event on the select, in order to know what to do on keyup[RETURN]:
1846 // * If the user presses [RETURN] as part of changing the value of a
1847 // selection, we should just let the value change and not let the
1848 // event broadcast further (e.g. to validating the current state of
1849 // the form in editable list view, which would lead to saving the
1850 // current row or switching to the next one)
1851 // * If the user presses [RETURN] with a select closed (side-effect:
1852 // also if the user opened the select and pressed [RETURN] without
1853 // changing the selected value), takes the action as validating the
1855 var ischanging = false;
1856 this.$element.find('select')
1857 .change(this.on_ui_change)
1858 .change(function () { ischanging = true; })
1859 .click(function () { ischanging = false; })
1860 .keyup(function (e) {
1861 if (e.which !== 13 || !ischanging) { return; }
1862 e.stopPropagation();
1866 set_value: function(value) {
1867 value = value === null ? false : value;
1868 value = value instanceof Array ? value[0] : value;
1870 this.render_value();
1872 render_value: function() {
1873 if (!this.get("effective_readonly")) {
1875 for (var i = 0, ii = this.values.length; i < ii; i++) {
1876 if (this.values[i][0] === this.value) index = i;
1878 this.$element.find('select')[0].selectedIndex = index;
1881 var option = _(this.values)
1882 .detect(function (record) { return record[0] === self.value; });
1883 this.$element.text(option ? option[1] : this.values[0][1]);
1886 set_value_from_ui: function() {
1887 this.value = this.values[this.$element.find('select')[0].selectedIndex][0];
1890 validate: function() {
1891 if (this.get("effective_readonly")) {
1892 this.invalid = false;
1895 var value = this.values[this.$element.find('select')[0].selectedIndex];
1896 this.invalid = !(value && !(this.required && value[0] === false));
1898 focus: function($element) {
1899 this._super($element || this.$element.find('select:first'));
1903 // jquery autocomplete tweak to allow html
1905 var proto = $.ui.autocomplete.prototype,
1906 initSource = proto._initSource;
1908 function filter( array, term ) {
1909 var matcher = new RegExp( $.ui.autocomplete.escapeRegex(term), "i" );
1910 return $.grep( array, function(value) {
1911 return matcher.test( $( "<div>" ).html( value.label || value.value || value ).text() );
1916 _initSource: function() {
1917 if ( this.options.html && $.isArray(this.options.source) ) {
1918 this.source = function( request, response ) {
1919 response( filter( this.options.source, request.term ) );
1922 initSource.call( this );
1926 _renderItem: function( ul, item) {
1927 return $( "<li></li>" )
1928 .data( "item.autocomplete", item )
1929 .append( $( "<a></a>" )[ this.options.html ? "html" : "text" ]( item.label ) )
1935 openerp.web.form.dialog = function(content, options) {
1936 options = _.extend({
1941 var dialog = new openerp.web.Dialog(null, options, content).open();
1942 return dialog.$element;
1945 openerp.web.form.FieldMany2One = openerp.web.form.AbstractField.extend({
1946 template: 'EmptyComponent',
1947 init: function(view, node) {
1948 this._super(view, node);
1951 this.cm_id = _.uniqueId('m2o_cm_');
1952 this.last_search = [];
1953 this.tmp_value = undefined;
1957 this.render_content();
1958 this.on("change:effective_readonly", this, function() {
1959 this.render_content();
1962 render_content: function() {
1963 this.$element.html("");
1964 if (!this.get("effective_readonly"))
1965 this.render_editable();
1967 this.render_readonly();
1968 this.render_value();
1970 render_editable: function() {
1971 this.$element.html(QWeb.render("FieldMany2One", {widget: this}));
1973 this.$input = this.$element.find("input");
1974 this.$drop_down = this.$element.find(".oe-m2o-drop-down-button");
1975 this.$menu_btn = this.$element.find(".oe-m2o-cm-button");
1978 var init_context_menu_def = $.Deferred().then(function(e) {
1979 var rdataset = new openerp.web.DataSetStatic(self, "ir.values", self.build_context());
1980 rdataset.call("get", ['action', 'client_action_relate',
1981 [[self.field.relation, false]], false, rdataset.get_context()], false, 0)
1982 .then(function(result) {
1983 self.related_entries = result;
1985 var $cmenu = $("#" + self.cm_id);
1986 $cmenu.append(QWeb.render("FieldMany2One.context_menu", {widget: self}));
1988 bindings[self.cm_id + "_search"] = function() {
1989 if (self.get("effective_readonly"))
1991 self._search_create_popup("search");
1993 bindings[self.cm_id + "_create"] = function() {
1994 if (self.get("effective_readonly"))
1996 self._search_create_popup("form");
1998 bindings[self.cm_id + "_open"] = function() {
2002 var pop = new openerp.web.form.FormOpenPopup(self.view);
2004 self.field.relation,
2006 self.build_context(),
2008 title: _t("Open: ") + (self.string || self.name)
2011 pop.on_write_completed.add_last(function() {
2012 self.set_value(self.value[0]);
2015 _.each(_.range(self.related_entries.length), function(i) {
2016 bindings[self.cm_id + "_related_" + i] = function() {
2017 self.open_related(self.related_entries[i]);
2020 var cmenu = self.$menu_btn.contextMenu(self.cm_id, {'noRightClick': true,
2021 bindings: bindings, itemStyle: {"color": ""},
2022 onContextMenu: function() {
2024 $("#" + self.cm_id + " .oe_m2o_menu_item_mandatory").removeClass("oe-m2o-disabled-cm");
2026 $("#" + self.cm_id + " .oe_m2o_menu_item_mandatory").addClass("oe-m2o-disabled-cm");
2028 if (!self.get("effective_readonly")) {
2029 $("#" + self.cm_id + " .oe_m2o_menu_item_noreadonly").removeClass("oe-m2o-disabled-cm");
2031 $("#" + self.cm_id + " .oe_m2o_menu_item_noreadonly").addClass("oe-m2o-disabled-cm");
2034 }, menuStyle: {width: "200px"}
2036 $.async_when().then(function() {self.$menu_btn.trigger(e);});
2039 var ctx_callback = function(e) {init_context_menu_def.resolve(e); e.preventDefault()};
2040 this.$menu_btn.click(ctx_callback);
2042 // some behavior for input
2043 this.$input.keyup(function() {
2044 if (self.$input.val() === "") {
2045 self._change_int_value(null);
2046 } else if (self.value === null || (self.value && self.$input.val() !== self.value[1])) {
2047 self._change_int_value(undefined);
2050 this.$drop_down.click(function() {
2051 if (self.get("effective_readonly"))
2053 if (self.$input.autocomplete("widget").is(":visible")) {
2054 self.$input.autocomplete("close");
2057 self.$input.autocomplete("search", "");
2059 self.$input.autocomplete("search");
2061 self.$input.focus();
2064 var anyoneLoosesFocus = function() {
2065 if (!self.$input.is(":focus") &&
2066 !self.$input.autocomplete("widget").is(":visible") &&
2068 if (self.value === undefined && self.last_search.length > 0) {
2069 self._change_int_ext_value(self.last_search[0]);
2071 self._change_int_ext_value(null);
2075 this.$input.focusout(anyoneLoosesFocus);
2077 var isSelecting = false;
2079 this.$input.autocomplete({
2080 source: function(req, resp) { self.get_search_result(req, resp); },
2081 select: function(event, ui) {
2085 self._change_int_value([item.id, item.name]);
2086 } else if (item.action) {
2087 self._change_int_value(undefined);
2092 focus: function(e, ui) {
2096 close: anyoneLoosesFocus,
2100 this.$input.autocomplete("widget").addClass("openerp openerp2");
2101 // used to correct a bug when selecting an element by pushing 'enter' in an editable list
2102 this.$input.keyup(function(e) {
2103 if (e.which === 13) {
2105 e.stopPropagation();
2107 isSelecting = false;
2110 render_readonly: function() {
2111 this.$element.html(QWeb.render("FieldMany2One_readonly"));
2113 // autocomplete component content handling
2114 get_search_result: function(request, response) {
2115 var search_val = request.term;
2118 if (this.abort_last) {
2120 delete this.abort_last;
2122 var dataset = new openerp.web.DataSetStatic(this, this.field.relation, self.build_context());
2124 dataset.name_search(search_val, self.build_domain(), 'ilike',
2125 this.limit + 1, function(data) {
2126 self.last_search = data;
2127 // possible selections for the m2o
2128 var values = _.map(data, function(x) {
2130 label: _.str.escapeHTML(x[1]),
2137 // search more... if more results that max
2138 if (values.length > self.limit) {
2139 values = values.slice(0, self.limit);
2140 values.push({label: _t("<em>Â Â Â Search More...</em>"), action: function() {
2141 dataset.name_search(search_val, self.build_domain(), 'ilike'
2142 , false, function(data) {
2143 self._change_int_value(null);
2144 self._search_create_popup("search", data);
2149 var raw_result = _(data.result).map(function(x) {return x[1];});
2150 if (search_val.length > 0 &&
2151 !_.include(raw_result, search_val) &&
2152 (!self.value || search_val !== self.value[1])) {
2153 values.push({label: _.str.sprintf(_t('<em>Â Â Â Create "<strong>%s</strong>"</em>'),
2154 $('<span />').text(search_val).html()), action: function() {
2155 self._quick_create(search_val);
2159 values.push({label: _t("<em>Â Â Â Create and Edit...</em>"), action: function() {
2160 self._change_int_value(null);
2161 self._search_create_popup("form", undefined, {"default_name": search_val});
2166 this.abort_last = dataset.abort_last;
2168 _quick_create: function(name) {
2170 var slow_create = function () {
2171 self._change_int_value(null);
2172 self._search_create_popup("form", undefined, {"default_name": name});
2174 if (self.get_definition_options().quick_create === undefined || self.get_definition_options().quick_create) {
2175 var dataset = new openerp.web.DataSetStatic(this, this.field.relation, self.build_context());
2176 dataset.name_create(name, function(data) {
2177 self._change_int_ext_value(data);
2178 }).fail(function(error, event) {
2179 event.preventDefault();
2185 // all search/create popup handling
2186 _search_create_popup: function(view, ids, context) {
2188 var pop = new openerp.web.form.SelectCreatePopup(this);
2190 self.field.relation,
2192 title: (view === 'search' ? _t("Search: ") : _t("Create: ")) + (this.string || this.name),
2193 initial_ids: ids ? _.map(ids, function(x) {return x[0]}) : undefined,
2195 disable_multiple_selection: true
2197 self.build_domain(),
2198 new openerp.web.CompoundContext(self.build_context(), context || {})
2200 pop.on_select_elements.add(function(element_ids) {
2201 var dataset = new openerp.web.DataSetStatic(self, self.field.relation, self.build_context());
2202 dataset.name_get([element_ids[0]], function(data) {
2203 self._change_int_ext_value(data[0]);
2207 _change_int_ext_value: function(value) {
2208 this._change_int_value(value);
2209 this.render_value();
2211 render_value: function() {
2213 if (!this.get("effective_readonly")) {
2214 this.$input.val(this.value ? this.value[1] : "");
2216 self.$element.find('a')
2218 .text(this.value ? this.value[1] : '')
2219 .click(function () {
2221 type: 'ir.actions.act_window',
2222 res_model: self.field.relation,
2223 res_id: self.value[0],
2224 context: self.build_context(),
2225 views: [[false, 'page'], [false, 'form']],
2232 _change_int_value: function(value) {
2234 var back_orig_value = this.original_value;
2235 if (this.value === null || this.value) {
2236 this.original_value = this.value;
2238 if (back_orig_value === undefined) { // first use after a set_value()
2241 if (this.value !== undefined && ((back_orig_value ? back_orig_value[0] : null)
2242 !== (this.value ? this.value[0] : null))) {
2243 this.on_ui_change();
2246 set_value: function(value) {
2247 value = value || null;
2248 this.invalid = false;
2250 this.tmp_value = value;
2252 self.on_value_changed();
2253 var real_set_value = function(rval) {
2254 self.tmp_value = undefined;
2256 self.original_value = undefined;
2257 self._change_int_ext_value(rval);
2259 if (value && !(value instanceof Array)) {
2260 // name_get in a m2o does not use the context of the field
2261 var dataset = new openerp.web.DataSetStatic(this, this.field.relation, self.view.dataset.get_context());
2262 dataset.name_get([value], function(data) {
2263 real_set_value(data[0]);
2264 }).fail(function() {self.tmp_value = undefined;});
2266 $.async_when().then(function() {real_set_value(value);});
2269 get_value: function() {
2270 if (this.tmp_value !== undefined) {
2271 if (this.tmp_value instanceof Array) {
2272 return this.tmp_value[0];
2274 return this.tmp_value ? this.tmp_value : false;
2276 if (this.value === undefined)
2277 return this.original_value ? this.original_value[0] : false;
2278 return this.value ? this.value[0] : false;
2280 validate: function() {
2281 this.invalid = false;
2282 var val = this.tmp_value !== undefined ? this.tmp_value : this.value;
2284 this.invalid = this.required;
2287 open_related: function(related) {
2291 var additional_context = {
2292 active_id: self.value[0],
2293 active_ids: [self.value[0]],
2294 active_model: self.field.relation
2296 self.rpc("/web/action/load", {
2297 action_id: related[2].id,
2298 context: additional_context
2299 }, function(result) {
2300 result.result.context = _.extend(result.result.context || {}, additional_context);
2301 self.do_action(result.result);
2304 focus: function ($element) {
2305 this._super($element || this.$input);
2310 # Values: (0, 0, { fields }) create
2311 # (1, ID, { fields }) update
2312 # (2, ID) remove (delete)
2313 # (3, ID) unlink one (target id or target of relation)
2315 # (5) unlink all (only valid for one2many)
2320 'create': function (values) {
2321 return [commands.CREATE, false, values];
2323 // (1, id, {values})
2325 'update': function (id, values) {
2326 return [commands.UPDATE, id, values];
2330 'delete': function (id) {
2331 return [commands.DELETE, id, false];
2333 // (3, id[, _]) removes relation, but not linked record itself
2335 'forget': function (id) {
2336 return [commands.FORGET, id, false];
2340 'link_to': function (id) {
2341 return [commands.LINK_TO, id, false];
2345 'delete_all': function () {
2346 return [5, false, false];
2348 // (6, _, ids) replaces all linked records with provided ids
2350 'replace_with': function (ids) {
2351 return [6, false, ids];
2354 openerp.web.form.FieldOne2Many = openerp.web.form.AbstractField.extend({
2355 template: 'FieldOne2Many',
2356 multi_selection: false,
2357 init: function(view, node) {
2358 this._super(view, node);
2359 this.is_loaded = $.Deferred();
2360 this.initial_is_loaded = this.is_loaded;
2361 this.is_setted = $.Deferred();
2362 this.form_last_update = $.Deferred();
2363 this.init_form_last_update = this.form_last_update;
2364 this.disable_utility_classes = true;
2367 this._super.apply(this, arguments);
2371 this.dataset = new openerp.web.form.One2ManyDataSet(this, this.field.relation);
2372 this.dataset.o2m = this;
2373 this.dataset.parent_view = this.view;
2374 this.dataset.child_name = this.name;
2375 //this.dataset.child_name =
2376 this.dataset.on_change.add_last(function() {
2377 self.trigger_on_change();
2380 this.is_setted.then(function() {
2383 this.is_loaded.then(function() {
2384 self.on("change:effective_readonly", self, function() {
2385 self.is_loaded = self.is_loaded.pipe(function() {
2386 self.viewmanager.destroy();
2387 return $.when(self.load_views()).then(function() {
2388 self.reload_current_view();
2394 trigger_on_change: function() {
2395 var tmp = this.doing_on_change;
2396 this.doing_on_change = true;
2397 this.on_ui_change();
2398 this.doing_on_change = tmp;
2400 load_views: function() {
2403 var modes = this.node.attrs.mode;
2404 modes = !!modes ? modes.split(",") : ["tree"];
2406 _.each(modes, function(mode) {
2409 view_type: mode == "tree" ? "list" : mode,
2410 options: { sidebar : false }
2412 if (self.field.views && self.field.views[mode]) {
2413 view.embedded_view = self.field.views[mode];
2415 if(view.view_type === "list") {
2416 view.options.selectable = self.multi_selection;
2417 if (self.get("effective_readonly")) {
2418 view.options.addable = null;
2419 view.options.deletable = null;
2420 view.options.isClarkGable = false;
2422 } else if (view.view_type === "form") {
2423 if (self.get("effective_readonly")) {
2424 view.view_type = 'page';
2426 view.options.not_interactible_on_create = true;
2432 this.viewmanager = new openerp.web.ViewManager(this, this.dataset, views, {});
2433 this.viewmanager.template = 'One2Many.viewmanager';
2434 this.viewmanager.registry = openerp.web.views.extend({
2435 list: 'openerp.web.form.One2ManyListView',
2436 form: 'openerp.web.form.One2ManyFormView',
2437 page: 'openerp.web.PageView'
2439 var once = $.Deferred().then(function() {
2440 self.init_form_last_update.resolve();
2442 var def = $.Deferred().then(function() {
2443 self.initial_is_loaded.resolve();
2445 this.viewmanager.on_controller_inited.add_last(function(view_type, controller) {
2446 if (view_type == "list") {
2447 controller.o2m = self;
2448 if (self.get("effective_readonly"))
2449 controller.set_editable(false);
2450 } else if (view_type == "form" || view_type == 'page') {
2451 if (view_type == 'page' || self.get("effective_readonly")) {
2452 $(".oe_form_buttons", controller.$element).children().remove();
2454 controller.on_record_loaded.add_last(function() {
2457 controller.on_pager_action.add_first(function() {
2458 self.save_any_view();
2460 } else if (view_type == "graph") {
2461 self.reload_current_view()
2465 this.viewmanager.on_mode_switch.add_first(function(n_mode, b, c, d, e) {
2466 $.when(self.save_any_view()).then(function() {
2467 if(n_mode === "list")
2468 $.async_when().then(function() {self.reload_current_view();});
2471 this.is_setted.then(function() {
2472 $.async_when().then(function () {
2473 self.viewmanager.appendTo(self.$element);
2478 reload_current_view: function() {
2480 return self.is_loaded = self.is_loaded.pipe(function() {
2481 var active_view = self.viewmanager.active_view;
2482 var view = self.viewmanager.views[active_view].controller;
2483 if(active_view === "list") {
2484 return view.reload_content();
2485 } else if (active_view === "form" || active_view === 'page') {
2486 if (self.dataset.index === null && self.dataset.ids.length >= 1) {
2487 self.dataset.index = 0;
2489 var act = function() {
2490 return view.do_show();
2492 self.form_last_update = self.form_last_update.pipe(act, act);
2493 return self.form_last_update;
2494 } else if (active_view === "graph") {
2495 return view.do_search(self.build_domain(), self.dataset.get_context(), []);
2499 set_value: function(value) {
2500 value = value || [];
2502 this.dataset.reset_ids([]);
2503 if(value.length >= 1 && value[0] instanceof Array) {
2505 _.each(value, function(command) {
2506 var obj = {values: command[2]};
2507 switch (command[0]) {
2508 case commands.CREATE:
2509 obj['id'] = _.uniqueId(self.dataset.virtual_id_prefix);
2511 self.dataset.to_create.push(obj);
2512 self.dataset.cache.push(_.extend(_.clone(obj), {values: _.clone(command[2])}));
2515 case commands.UPDATE:
2516 obj['id'] = command[1];
2517 self.dataset.to_write.push(obj);
2518 self.dataset.cache.push(_.extend(_.clone(obj), {values: _.clone(command[2])}));
2521 case commands.DELETE:
2522 self.dataset.to_delete.push({id: command[1]});
2524 case commands.LINK_TO:
2525 ids.push(command[1]);
2527 case commands.DELETE_ALL:
2528 self.dataset.delete_all = true;
2533 this.dataset.set_ids(ids);
2534 } else if (value.length >= 1 && typeof(value[0]) === "object") {
2536 this.dataset.delete_all = true;
2537 _.each(value, function(command) {
2538 var obj = {values: command};
2539 obj['id'] = _.uniqueId(self.dataset.virtual_id_prefix);
2541 self.dataset.to_create.push(obj);
2542 self.dataset.cache.push(_.clone(obj));
2546 this.dataset.set_ids(ids);
2549 this.dataset.reset_ids(value);
2551 if (this.dataset.index === null && this.dataset.ids.length > 0) {
2552 this.dataset.index = 0;
2554 self.is_setted.resolve();
2555 return self.reload_current_view();
2557 get_value: function() {
2561 this.save_any_view();
2562 var val = this.dataset.delete_all ? [commands.delete_all()] : [];
2563 val = val.concat(_.map(this.dataset.ids, function(id) {
2564 var alter_order = _.detect(self.dataset.to_create, function(x) {return x.id === id;});
2566 return commands.create(alter_order.values);
2568 alter_order = _.detect(self.dataset.to_write, function(x) {return x.id === id;});
2570 return commands.update(alter_order.id, alter_order.values);
2572 return commands.link_to(id);
2574 return val.concat(_.map(
2575 this.dataset.to_delete, function(x) {
2576 return commands['delete'](x.id);}));
2578 save_any_view: function() {
2579 if (this.doing_on_change)
2581 return this.session.synchronized_mode(_.bind(function() {
2582 if (this.viewmanager && this.viewmanager.views && this.viewmanager.active_view &&
2583 this.viewmanager.views[this.viewmanager.active_view] &&
2584 this.viewmanager.views[this.viewmanager.active_view].controller) {
2585 var view = this.viewmanager.views[this.viewmanager.active_view].controller;
2586 if (this.viewmanager.active_view === "form") {
2587 if (!view.is_initialized.isResolved()) {
2590 var res = $.when(view.do_save());
2591 if (!res.isResolved() && !res.isRejected()) {
2592 console.warn("Asynchronous get_value() is not supported in form view.");
2595 } else if (this.viewmanager.active_view === "list") {
2596 var res = $.when(view.ensure_saved());
2597 if (!res.isResolved() && !res.isRejected()) {
2598 console.warn("Asynchronous get_value() is not supported in list view.");
2606 is_valid: function() {
2608 return this._super();
2610 validate: function() {
2611 this.invalid = false;
2612 if (!this.viewmanager.views[this.viewmanager.active_view])
2614 var view = this.viewmanager.views[this.viewmanager.active_view].controller;
2615 if (this.viewmanager.active_view === "form") {
2616 for (var f in view.fields) {
2618 if (!f.is_valid()) {
2619 this.invalid = true;
2625 is_dirty: function() {
2626 this.save_any_view();
2627 return this._super();
2631 openerp.web.form.One2ManyDataSet = openerp.web.BufferedDataSet.extend({
2632 get_context: function() {
2633 this.context = this.o2m.build_context([this.o2m.name]);
2634 return this.context;
2638 openerp.web.form.One2ManyListView = openerp.web.ListView.extend({
2639 _template: 'One2Many.listview',
2640 do_add_record: function () {
2641 if (this.options.editable) {
2642 this._super.apply(this, arguments);
2645 var pop = new openerp.web.form.SelectCreatePopup(this);
2646 pop.on_default_get.add(self.dataset.on_default_get);
2648 self.o2m.field.relation,
2650 title: _t("Create: ") + self.name,
2651 initial_view: "form",
2652 alternative_form_view: self.o2m.field.views ? self.o2m.field.views["form"] : undefined,
2653 create_function: function(data, callback, error_callback) {
2654 return self.o2m.dataset.create(data).then(function(r) {
2655 self.o2m.dataset.set_ids(self.o2m.dataset.ids.concat([r.result]));
2656 self.o2m.dataset.on_change();
2657 }).then(callback, error_callback);
2659 read_function: function() {
2660 return self.o2m.dataset.read_ids.apply(self.o2m.dataset, arguments);
2662 parent_view: self.o2m.view,
2663 child_name: self.o2m.name,
2664 form_view_options: {'not_interactible_on_create':true}
2666 self.o2m.build_domain(),
2667 self.o2m.build_context()
2669 pop.on_select_elements.add_last(function() {
2670 self.o2m.reload_current_view();
2674 do_activate_record: function(index, id) {
2676 var pop = new openerp.web.form.FormOpenPopup(self.o2m.view);
2677 pop.show_element(self.o2m.field.relation, id, self.o2m.build_context(), {
2678 title: _t("Open: ") + self.name,
2680 alternative_form_view: self.o2m.field.views ? self.o2m.field.views["form"] : undefined,
2681 parent_view: self.o2m.view,
2682 child_name: self.o2m.name,
2683 read_function: function() {
2684 return self.o2m.dataset.read_ids.apply(self.o2m.dataset, arguments);
2686 form_view_options: {'not_interactible_on_create':true},
2687 readonly: self.o2m.get("effective_readonly")
2689 pop.on_write.add(function(id, data) {
2690 self.o2m.dataset.write(id, data, {}, function(r) {
2691 self.o2m.reload_current_view();
2695 do_button_action: function (name, id, callback) {
2697 var def = $.Deferred().then(callback).then(function() {self.o2m.view.reload();});
2698 return this._super(name, id, _.bind(def.resolve, def));
2702 openerp.web.form.One2ManyFormView = openerp.web.FormView.extend({
2703 form_template: 'One2Many.formview',
2704 on_loaded: function(data) {
2707 this.$form_header.find('button.oe_form_button_create').click(function() {
2708 self.do_save().then(self.on_button_new);
2711 do_notify_change: function() {
2712 if (this.dataset.parent_view) {
2713 this.dataset.parent_view.do_notify_change();
2715 this._super.apply(this, arguments);
2721 * TODO niv: clean those deferred stuff, it could be better
2723 openerp.web.form.FieldMany2Many = openerp.web.form.AbstractField.extend({
2724 template: 'FieldMany2Many',
2725 multi_selection: false,
2726 init: function(view, node) {
2727 this._super(view, node);
2728 this.list_id = _.uniqueId("many2many");
2729 this.is_loaded = $.Deferred();
2730 this.initial_is_loaded = this.is_loaded;
2731 this.is_setted = $.Deferred();
2734 this._super.apply(this, arguments);
2738 this.dataset = new openerp.web.form.Many2ManyDataSet(this, this.field.relation);
2739 this.dataset.m2m = this;
2740 this.dataset.on_unlink.add_last(function(ids) {
2741 self.on_ui_change();
2744 this.is_setted.then(function() {
2747 this.is_loaded.then(function() {
2748 self.on("change:effective_readonly", self, function() {
2749 self.is_loaded = self.is_loaded.pipe(function() {
2750 self.list_view.destroy();
2751 return $.when(self.load_view()).then(function() {
2752 self.reload_content();
2758 set_value: function(value) {
2759 value = value || [];
2760 if (value.length >= 1 && value[0] instanceof Array) {
2761 value = value[0][2];
2764 this.dataset.set_ids(value);
2766 self.reload_content();
2767 this.is_setted.resolve();
2769 get_value: function() {
2770 return [commands.replace_with(this.dataset.ids)];
2772 validate: function() {
2773 this.invalid = false;
2775 load_view: function() {
2777 this.list_view = new openerp.web.form.Many2ManyListView(this, this.dataset, false, {
2778 'addable': self.get("effective_readonly") ? null : _t("Add"),
2779 'deletable': self.get("effective_readonly") ? false : true,
2780 'selectable': self.multi_selection,
2781 'isClarkGable': self.get("effective_readonly") ? false : true
2783 var embedded = (this.field.views || {}).tree;
2785 this.list_view.set_embedded_view(embedded);
2787 this.list_view.m2m_field = this;
2788 var loaded = $.Deferred();
2789 this.list_view.on_loaded.add_last(function() {
2790 self.initial_is_loaded.resolve();
2793 $.async_when().then(function () {
2794 self.list_view.appendTo($("#" + self.list_id));
2798 reload_content: function() {
2800 this.is_loaded = this.is_loaded.pipe(function() {
2801 return self.list_view.reload_content();
2806 openerp.web.form.Many2ManyDataSet = openerp.web.DataSetStatic.extend({
2807 get_context: function() {
2808 this.context = this.m2m.build_context();
2809 return this.context;
2815 * @extends openerp.web.ListView
2817 openerp.web.form.Many2ManyListView = openerp.web.ListView.extend(/** @lends openerp.web.form.Many2ManyListView# */{
2818 do_add_record: function () {
2819 var pop = new openerp.web.form.SelectCreatePopup(this);
2823 title: _t("Add: ") + this.name
2825 new openerp.web.CompoundDomain(this.m2m_field.build_domain(), ["!", ["id", "in", this.m2m_field.dataset.ids]]),
2826 this.m2m_field.build_context()
2829 pop.on_select_elements.add(function(element_ids) {
2830 _.each(element_ids, function(element_id) {
2831 if(! _.detect(self.dataset.ids, function(x) {return x == element_id;})) {
2832 self.dataset.set_ids([].concat(self.dataset.ids, [element_id]));
2833 self.m2m_field.on_ui_change();
2834 self.reload_content();
2839 do_activate_record: function(index, id) {
2841 var pop = new openerp.web.form.FormOpenPopup(this);
2842 pop.show_element(this.dataset.model, id, this.m2m_field.build_context(), {
2843 title: _t("Open: ") + this.name,
2844 readonly: this.getParent().get("effective_readonly")
2846 pop.on_write_completed.add_last(function() {
2847 self.reload_content();
2854 * @extends openerp.web.OldWidget
2856 openerp.web.form.SelectCreatePopup = openerp.web.OldWidget.extend(/** @lends openerp.web.form.SelectCreatePopup# */{
2857 template: "SelectCreatePopup",
2861 * - initial_view: form or search (default search)
2862 * - disable_multiple_selection
2863 * - alternative_form_view
2864 * - create_function (defaults to a naive saving behavior)
2867 * - form_view_options
2868 * - list_view_options
2871 select_element: function(model, options, domain, context) {
2874 this.domain = domain || [];
2875 this.context = context || {};
2876 this.options = _.defaults(options || {}, {"initial_view": "search", "create_function": function() {
2877 return self.create_row.apply(self, arguments);
2878 }, read_function: null});
2879 this.initial_ids = this.options.initial_ids;
2880 this.created_elements = [];
2881 this.renderElement();
2882 openerp.web.form.dialog(this.$element, {
2886 title: options.title || ""
2893 this.dataset = new openerp.web.ProxyDataSet(this, this.model,
2895 this.dataset.create_function = function() {
2896 return self.options.create_function.apply(null, arguments).then(function(r) {
2897 self.created_elements.push(r.result);
2900 this.dataset.write_function = function() {
2901 return self.write_row.apply(self, arguments);
2903 this.dataset.read_function = this.options.read_function;
2904 this.dataset.parent_view = this.options.parent_view;
2905 this.dataset.child_name = this.options.child_name;
2906 this.dataset.on_default_get.add(this.on_default_get);
2907 if (this.options.initial_view == "search") {
2908 self.rpc('/web/session/eval_domain_and_context', {
2910 contexts: [this.context]
2911 }, function (results) {
2912 var search_defaults = {};
2913 _.each(results.context, function (value, key) {
2914 var match = /^search_default_(.*)$/.exec(key);
2916 search_defaults[match[1]] = value;
2919 self.setup_search_view(search_defaults);
2925 setup_search_view: function(search_defaults) {
2927 if (this.searchview) {
2928 this.searchview.destroy();
2930 this.searchview = new openerp.web.SearchView(this,
2931 this.dataset, false, search_defaults);
2932 this.searchview.on_search.add(function(domains, contexts, groupbys) {
2933 if (self.initial_ids) {
2934 self.do_search(domains.concat([[["id", "in", self.initial_ids]], self.domain]),
2935 contexts, groupbys);
2936 self.initial_ids = undefined;
2938 self.do_search(domains.concat([self.domain]), contexts.concat(self.context), groupbys);
2941 this.searchview.on_loaded.add_last(function () {
2942 self.view_list = new openerp.web.form.SelectCreateListView(self,
2943 self.dataset, false,
2944 _.extend({'deletable': false,
2945 'selectable': !self.options.disable_multiple_selection
2946 }, self.options.list_view_options || {}));
2947 self.view_list.popup = self;
2948 self.view_list.appendTo($("#" + self.element_id + "_view_list")).pipe(function() {
2949 self.view_list.do_show();
2950 }).pipe(function() {
2951 self.searchview.do_search();
2953 self.view_list.on_loaded.add_last(function() {
2954 var $buttons = self.view_list.$element.find(".oe-actions");
2955 $buttons.prepend(QWeb.render("SelectCreatePopup.search.buttons"));
2956 var $cbutton = $buttons.find(".oe_selectcreatepopup-search-close");
2957 $cbutton.click(function() {
2960 var $sbutton = $buttons.find(".oe_selectcreatepopup-search-select");
2961 if(self.options.disable_multiple_selection) {
2964 $sbutton.click(function() {
2965 self.on_select_elements(self.selected_ids);
2970 this.searchview.appendTo($("#" + this.element_id + "_search"));
2972 do_search: function(domains, contexts, groupbys) {
2974 this.rpc('/web/session/eval_domain_and_context', {
2975 domains: domains || [],
2976 contexts: contexts || [],
2977 group_by_seq: groupbys || []
2978 }, function (results) {
2979 self.view_list.do_search(results.domain, results.context, results.group_by);
2982 create_row: function() {
2984 var wdataset = new openerp.web.DataSetSearch(this, this.model, this.context, this.domain);
2985 wdataset.parent_view = this.options.parent_view;
2986 wdataset.child_name = this.options.child_name;
2987 return wdataset.create.apply(wdataset, arguments);
2989 write_row: function() {
2991 var wdataset = new openerp.web.DataSetSearch(this, this.model, this.context, this.domain);
2992 wdataset.parent_view = this.options.parent_view;
2993 wdataset.child_name = this.options.child_name;
2994 return wdataset.write.apply(wdataset, arguments);
2996 on_select_elements: function(element_ids) {
2998 on_click_element: function(ids) {
2999 this.selected_ids = ids || [];
3000 if(this.selected_ids.length > 0) {
3001 this.$element.find(".oe_selectcreatepopup-search-select").removeAttr('disabled');
3003 this.$element.find(".oe_selectcreatepopup-search-select").attr('disabled', "disabled");
3006 new_object: function() {
3008 if (this.searchview) {
3009 this.searchview.hide();
3011 if (this.view_list) {
3012 this.view_list.$element.hide();
3014 this.dataset.index = null;
3015 this.view_form = new openerp.web.FormView(this, this.dataset, false, self.options.form_view_options);
3016 if (this.options.alternative_form_view) {
3017 this.view_form.set_embedded_view(this.options.alternative_form_view);
3019 this.view_form.appendTo(this.$element.find("#" + this.element_id + "_view_form"));
3020 this.view_form.on_loaded.add_last(function() {
3021 var $buttons = self.view_form.$element.find(".oe_form_buttons");
3022 $buttons.html(QWeb.render("SelectCreatePopup.form.buttons", {widget:self}));
3023 var $nbutton = $buttons.find(".oe_selectcreatepopup-form-save-new");
3024 $nbutton.click(function() {
3025 $.when(self.view_form.do_save()).then(function() {
3026 self.view_form.reload_mutex.exec(function() {
3027 self.view_form.on_button_new();
3031 var $nbutton = $buttons.find(".oe_selectcreatepopup-form-save");
3032 $nbutton.click(function() {
3033 $.when(self.view_form.do_save()).then(function() {
3034 self.view_form.reload_mutex.exec(function() {
3039 var $cbutton = $buttons.find(".oe_selectcreatepopup-form-close");
3040 $cbutton.click(function() {
3044 this.view_form.do_show();
3046 check_exit: function() {
3047 if (this.created_elements.length > 0) {
3048 this.on_select_elements(this.created_elements);
3052 on_default_get: function(res) {}
3055 openerp.web.form.SelectCreateListView = openerp.web.ListView.extend({
3056 do_add_record: function () {
3057 this.popup.new_object();
3059 select_record: function(index) {
3060 this.popup.on_select_elements([this.dataset.ids[index]]);
3061 this.popup.destroy();
3063 do_select: function(ids, records) {
3064 this._super(ids, records);
3065 this.popup.on_click_element(ids);
3071 * @extends openerp.web.OldWidget
3073 openerp.web.form.FormOpenPopup = openerp.web.OldWidget.extend(/** @lends openerp.web.form.FormOpenPopup# */{
3074 template: "FormOpenPopup",
3077 * - alternative_form_view
3078 * - auto_write (default true)
3082 * - form_view_options
3085 show_element: function(model, row_id, context, options) {
3087 this.row_id = row_id;
3088 this.context = context || {};
3089 this.options = _.defaults(options || {}, {"auto_write": true});
3090 this.renderElement();
3091 openerp.web.dialog(this.$element, {
3092 title: options.title || '',
3101 this.dataset = new openerp.web.form.FormOpenDataset(this, this.model, this.context);
3102 this.dataset.fop = this;
3103 this.dataset.ids = [this.row_id];
3104 this.dataset.index = 0;
3105 this.dataset.parent_view = this.options.parent_view;
3106 this.dataset.child_name = this.options.child_name;
3107 this.setup_form_view();
3109 on_write: function(id, data) {
3110 if (!this.options.auto_write)
3113 var wdataset = new openerp.web.DataSetSearch(this, this.model, this.context, this.domain);
3114 wdataset.parent_view = this.options.parent_view;
3115 wdataset.child_name = this.options.child_name;
3116 wdataset.write(id, data, {}, function(r) {
3117 self.on_write_completed();
3120 on_write_completed: function() {},
3121 setup_form_view: function() {
3123 var FormClass = this.options.readonly
3124 ? openerp.web.views.get_object('page')
3125 : openerp.web.views.get_object('form');
3126 this.view_form = new FormClass(this, this.dataset, false, self.options.form_view_options);
3127 if (this.options.alternative_form_view) {
3128 this.view_form.set_embedded_view(this.options.alternative_form_view);
3130 this.view_form.appendTo(this.$element.find("#" + this.element_id + "_view_form"));
3131 this.view_form.on_loaded.add_last(function() {
3132 var $buttons = self.view_form.$element.find(".oe_form_buttons");
3133 $buttons.html(QWeb.render("FormOpenPopup.form.buttons"));
3134 var $nbutton = $buttons.find(".oe_formopenpopup-form-save");
3135 $nbutton.click(function() {
3136 self.view_form.do_save().then(function() {
3140 var $cbutton = $buttons.find(".oe_formopenpopup-form-close");
3141 $cbutton.click(function() {
3144 if (self.options.readonly) {
3146 $cbutton.text(_t("Close"));
3148 self.view_form.do_show();
3150 this.dataset.on_write.add(this.on_write);
3154 openerp.web.form.FormOpenDataset = openerp.web.ProxyDataSet.extend({
3155 read_ids: function() {
3156 if (this.fop.options.read_function) {
3157 return this.fop.options.read_function.apply(null, arguments);
3159 return this._super.apply(this, arguments);
3164 openerp.web.form.FieldReference = openerp.web.form.AbstractField.extend({
3165 template: 'FieldReference',
3166 init: function(view, node) {
3167 this._super(view, node);
3168 this.fields_view = {
3171 selection: view.fields_view.fields[this.name].selection
3178 this.get_fields_values = view.get_fields_values;
3179 this.get_selected_ids = view.get_selected_ids;
3180 this.do_onchange = this.on_form_changed = this.do_notify_change = this.on_nop;
3181 this.dataset = this.view.dataset;
3182 this.widgets_counter = 0;
3183 this.view_id = 'reference_' + _.uniqueId();
3186 this.fields_order = [];
3187 this.selection = new openerp.web.form.FieldSelection(this, { attrs: {
3191 this.reference_ready = true;
3192 this.selection.on_value_changed.add_last(this.on_selection_changed);
3193 this.m2o = new openerp.web.form.FieldMany2One(this, { attrs: {
3197 this.m2o.on_ui_change.add_last(this.on_ui_change);
3199 on_nop: function() {
3201 on_selection_changed: function() {
3202 if (this.reference_ready) {
3203 var sel = this.selection.get_value();
3204 this.m2o.field.relation = sel;
3205 this.m2o.set_value(null);
3206 this.m2o.$element.toggle(sel !== false);
3211 // TODO: not niv compliant
3212 this.selection.$element = $(".oe_form_view_reference_selection", this.$element);
3213 this.selection.renderElement();
3214 this.selection.start();
3215 this.m2o.$element = $(".oe_form_view_reference_m2o", this.$element);
3216 this.m2o.renderElement();
3219 is_valid: function() {
3220 return this.required === false || typeof(this.get_value()) === 'string';
3222 is_dirty: function() {
3223 return this.selection.is_dirty() || this.m2o.is_dirty();
3225 set_value: function(value) {
3227 this.reference_ready = false;
3228 var vals = [], sel_val, m2o_val;
3229 if (typeof(value) === 'string') {
3230 vals = value.split(',');
3232 sel_val = vals[0] || false;
3233 m2o_val = vals[1] ? parseInt(vals[1], 10) : false;
3234 this.selection.set_value(sel_val);
3235 this.m2o.field.relation = sel_val;
3236 this.m2o.set_value(m2o_val);
3237 this.m2o.$element.toggle(sel_val !== false);
3238 this.reference_ready = true;
3240 get_value: function() {
3241 var model = this.selection.get_value(),
3242 id = this.m2o.get_value();
3243 if (typeof(model) === 'string' && typeof(id) === 'number') {
3244 return model + ',' + id;
3251 openerp.web.form.FieldBinary = openerp.web.form.AbstractField.extend({
3252 init: function(view, node) {
3253 this._super(view, node);
3254 this.iframe = this.element_id + '_iframe';
3255 this.binary_value = false;
3258 this._super.apply(this, arguments);
3259 this.$element.find('input.oe-binary-file').change(this.on_file_change);
3260 this.$element.find('button.oe-binary-file-save').click(this.on_save_as);
3261 this.$element.find('.oe-binary-file-clear').click(this.on_clear);
3263 human_filesize : function(size) {
3264 var units = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
3266 while (size >= 1024) {
3270 return size.toFixed(2) + ' ' + units[i];
3272 on_file_change: function(e) {
3273 // TODO: on modern browsers, we could directly read the file locally on client ready to be used on image cropper
3274 // http://www.html5rocks.com/tutorials/file/dndfiles/
3275 // http://deepliquid.com/projects/Jcrop/demos.php?demo=handler
3276 window[this.iframe] = this.on_file_uploaded;
3277 if ($(e.target).val() != '') {
3278 this.$element.find('form.oe-binary-form input[name=session_id]').val(this.session.session_id);
3279 this.$element.find('form.oe-binary-form').submit();
3280 this.$element.find('.oe-binary-progress').show();
3281 this.$element.find('.oe-binary').hide();
3284 on_file_uploaded: function(size, name, content_type, file_base64) {
3285 delete(window[this.iframe]);
3286 if (size === false) {
3287 this.do_warn("File Upload", "There was a problem while uploading your file");
3288 // TODO: use openerp web crashmanager
3289 console.warn("Error while uploading file : ", name);
3291 this.on_file_uploaded_and_valid.apply(this, arguments);
3292 this.on_ui_change();
3294 this.$element.find('.oe-binary-progress').hide();
3295 this.$element.find('.oe-binary').show();
3297 on_file_uploaded_and_valid: function(size, name, content_type, file_base64) {
3299 on_save_as: function() {
3301 this.session.get_file({
3302 url: '/web/binary/saveas_ajax',
3303 data: {data: JSON.stringify({
3304 model: this.view.dataset.model,
3305 id: (this.view.datarecord.id || ''),
3307 filename_field: (this.node.attrs.filename || ''),
3308 context: this.view.dataset.get_context()
3310 complete: $.unblockUI,
3311 error: openerp.webclient.crashmanager.on_rpc_error
3314 on_clear: function() {
3315 if (this.value !== false) {
3317 this.binary_value = false;
3318 this.on_ui_change();
3324 openerp.web.form.FieldBinaryFile = openerp.web.form.FieldBinary.extend({
3325 template: 'FieldBinaryFile',
3326 update_dom: function() {
3327 this._super.apply(this, arguments);
3328 this.$element.find('.oe-binary-file-set, .oe-binary-file-clear').toggle(!this.readonly);
3329 this.$element.find('input[type=text]').prop('readonly', this.readonly);
3331 set_value: function(value) {
3332 this._super.apply(this, arguments);
3334 if (this.node.attrs.filename) {
3335 show_value = this.view.datarecord[this.node.attrs.filename] || '';
3337 show_value = (value != null && value !== false) ? value : '';
3339 this.$element.find('input').eq(0).val(show_value);
3341 on_file_uploaded_and_valid: function(size, name, content_type, file_base64) {
3342 this.value = file_base64;
3343 this.binary_value = true;
3344 var show_value = name + " (" + this.human_filesize(size) + ")";
3345 this.$element.find('input').eq(0).val(show_value);
3346 this.set_filename(name);
3348 set_filename: function(value) {
3349 var filename = this.node.attrs.filename;
3350 if (this.view.fields[filename]) {
3351 this.view.fields[filename].set_value(value);
3352 this.view.fields[filename].on_ui_change();
3355 on_clear: function() {
3356 this._super.apply(this, arguments);
3357 this.$element.find('input').eq(0).val('');
3358 this.set_filename('');
3362 openerp.web.form.FieldBinaryImage = openerp.web.form.FieldBinary.extend({
3363 template: 'FieldBinaryImage',
3365 this._super.apply(this, arguments);
3366 this.$image = this.$element.find('img.oe-binary-image');
3368 update_dom: function() {
3369 this._super.apply(this, arguments);
3370 this.$element.find('.oe-binary').toggle(!this.readonly);
3372 set_value: function(value) {
3373 this._super.apply(this, arguments);
3374 this.set_image_maxwidth();
3377 if (value && value.substr(0, 10).indexOf(' ') == -1) {
3378 url = 'data:image/png;base64,' + this.value;
3380 url = '/web/binary/image?session_id=' + this.session.session_id + '&model=' +
3381 this.view.dataset.model +'&id=' + (this.view.datarecord.id || '') + '&field=' + this.name + '&t=' + (new Date().getTime());
3383 this.$image.attr('src', url);
3385 set_image_maxwidth: function() {
3386 this.$image.css('max-width', this.$element.width());
3388 on_file_change: function() {
3389 this.set_image_maxwidth();
3390 this._super.apply(this, arguments);
3392 on_file_uploaded_and_valid: function(size, name, content_type, file_base64) {
3393 this.value = file_base64;
3394 this.binary_value = true;
3395 this.$image.attr('src', 'data:' + (content_type || 'image/png') + ';base64,' + file_base64);
3397 on_clear: function() {
3398 this._super.apply(this, arguments);
3399 this.$image.attr('src', '/web/static/src/img/placeholder.png');
3403 openerp.web.form.FieldStatus = openerp.web.form.AbstractField.extend({
3404 template: "EmptyComponent",
3407 this.selected_value = null;
3411 set_value: function(value) {
3413 this.selected_value = value;
3417 render_list: function() {
3419 var shown = _.map(((this.node.attrs || {}).statusbar_visible || "").split(","),
3420 function(x) { return _.str.trim(x); });
3421 shown = _.select(shown, function(x) { return x.length > 0; });
3423 if (shown.length == 0) {
3424 this.to_show = this.field.selection;
3426 this.to_show = _.select(this.field.selection, function(x) {
3427 return _.indexOf(shown, x[0]) !== -1 || x[0] === self.selected_value;
3431 var content = openerp.web.qweb.render("FieldStatus.content", {widget: this, _:_});
3432 this.$element.html(content);
3434 var colors = JSON.parse((this.node.attrs || {}).statusbar_colors || "{}");
3435 var color = colors[this.selected_value];
3437 var elem = this.$element.find("li.oe-arrow-list-selected span");
3438 elem.css("border-color", color);
3439 if (this.check_white(color))
3440 elem.css("color", "white");
3441 elem = this.$element.find("li.oe-arrow-list-selected .oe-arrow-list-before");
3442 elem.css("border-left-color", "rgba(0,0,0,0)");
3443 elem = this.$element.find("li.oe-arrow-list-selected .oe-arrow-list-after");
3444 elem.css("border-color", "rgba(0,0,0,0)");
3445 elem.css("border-left-color", color);
3448 check_white: function(color) {
3449 var div = $("<div></div>");
3450 div.css("display", "none");
3451 div.css("color", color);
3452 div.appendTo($("body"));
3453 var ncolor = div.css("color");
3455 var res = /^\s*rgb\s*\(\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)\s*\)\s*$/.exec(ncolor);
3459 var comps = [parseInt(res[1]), parseInt(res[2]), parseInt(res[3])];
3460 var lum = comps[0] * 0.3 + comps[1] * 0.59 + comps[1] * 0.11;
3469 * Registry of form widgets, called by :js:`openerp.web.FormView`
3471 openerp.web.form.widgets = new openerp.web.Registry({
3472 'button' : 'openerp.web.form.WidgetButton',
3473 'char' : 'openerp.web.form.FieldChar',
3474 'id' : 'openerp.web.form.FieldID',
3475 'email' : 'openerp.web.form.FieldEmail',
3476 'url' : 'openerp.web.form.FieldUrl',
3477 'text' : 'openerp.web.form.FieldText',
3478 'date' : 'openerp.web.form.FieldDate',
3479 'datetime' : 'openerp.web.form.FieldDatetime',
3480 'selection' : 'openerp.web.form.FieldSelection',
3481 'many2one' : 'openerp.web.form.FieldMany2One',
3482 'many2many' : 'openerp.web.form.FieldMany2Many',
3483 'one2many' : 'openerp.web.form.FieldOne2Many',
3484 'one2many_list' : 'openerp.web.form.FieldOne2Many',
3485 'reference' : 'openerp.web.form.FieldReference',
3486 'boolean' : 'openerp.web.form.FieldBoolean',
3487 'float' : 'openerp.web.form.FieldFloat',
3488 'integer': 'openerp.web.form.FieldFloat',
3489 'float_time': 'openerp.web.form.FieldFloat',
3490 'progressbar': 'openerp.web.form.FieldProgressBar',
3491 'image': 'openerp.web.form.FieldBinaryImage',
3492 'binary': 'openerp.web.form.FieldBinaryFile',
3493 'statusbar': 'openerp.web.form.FieldStatus'
3498 // vim:et fdc=0 fdl=0 foldnestmax=3 fdm=syntax: