Merge pull request #2350 from odoo-dev/master-momentjs-csn
[odoo/odoo.git] / addons / web_kanban / static / src / js / kanban.js
1 openerp.web_kanban = function (instance) {
2
3 var _t = instance.web._t,
4    _lt = instance.web._lt;
5 var QWeb = instance.web.qweb;
6 instance.web.views.add('kanban', 'instance.web_kanban.KanbanView');
7
8 instance.web_kanban.KanbanView = instance.web.View.extend({
9     template: "KanbanView",
10     display_name: _lt('Kanban'),
11     default_nr_columns: 1,
12     view_type: "kanban",
13     quick_create_class: "instance.web_kanban.QuickCreate",
14     number_of_color_schemes: 10,
15     init: function (parent, dataset, view_id, options) {
16         this._super(parent, dataset, view_id, options);
17         var self = this;
18         _.defaults(this.options, {
19             "quick_creatable": true,
20             "creatable": true,
21             "create_text": undefined,
22             "read_only_mode": false,
23             "confirm_on_delete": true,
24         });
25         this.fields_view = {};
26         this.fields_keys = [];
27         this.group_by = null;
28         this.group_by_field = {};
29         this.grouped_by_m2o = false;
30         this.many2manys = [];
31         this.state = {
32             groups : {},
33             records : {}
34         };
35         this.groups = [];
36         this.aggregates = {};
37         this.group_operators = ['avg', 'max', 'min', 'sum', 'count'];
38         this.qweb = new QWeb2.Engine();
39         this.qweb.debug = instance.session.debug;
40         this.qweb.default_dict = _.clone(QWeb.default_dict);
41         this.has_been_loaded = $.Deferred();
42         this.search_domain = this.search_context = this.search_group_by = null;
43         this.currently_dragging = {};
44         this.limit = options.limit || 40;
45         this.add_group_mutex = new $.Mutex();
46         if (!this.options.$buttons || !this.options.$buttons.length) {
47             this.options.$buttons = false;
48         }
49     },
50     view_loading: function(r) {
51         return this.load_kanban(r);
52     },
53     start: function() {
54         var self = this;
55         this._super.apply(this, arguments);
56         this.$el.on('click', '.oe_kanban_dummy_cell', function() {
57             if (self.$buttons) {
58                 self.$buttons.find('.oe_kanban_add_column').openerpBounce();
59             }
60         });
61     },
62     destroy: function() {
63         this._super.apply(this, arguments);
64         $('html').off('click.kanban');
65     },
66     load_kanban: function(data) {
67         this.fields_view = data;
68
69         // use default order if defined in xml description
70         var default_order = this.fields_view.arch.attrs.default_order,
71             unsorted = !this.dataset._sort.length;
72         if (unsorted && default_order) {
73             this.dataset.set_sort(default_order.split(','));
74         }
75         this.$el.addClass(this.fields_view.arch.attrs['class']);
76         this.$buttons = $(QWeb.render("KanbanView.buttons", {'widget': this}));
77         if (this.options.$buttons) {
78             this.$buttons.appendTo(this.options.$buttons);
79         } else {
80             this.$('.oe_kanban_buttons').replaceWith(this.$buttons);
81         }
82         this.$buttons
83             .on('click', 'button.oe_kanban_button_new', this.do_add_record)
84             .on('click', '.oe_kanban_add_column', this.do_add_group);
85         this.$groups = this.$el.find('.oe_kanban_groups tr');
86         this.fields_keys = _.keys(this.fields_view.fields);
87         this.add_qweb_template();
88         this.has_been_loaded.resolve();
89         this.trigger('kanban_view_loaded', data);
90         return $.when();
91     },
92     _is_quick_create_enabled: function() {
93         if (!this.options.quick_creatable || !this.is_action_enabled('create'))
94             return false;
95         if (this.fields_view.arch.attrs.quick_create !== undefined)
96             return JSON.parse(this.fields_view.arch.attrs.quick_create);
97         return !! this.group_by;
98     },
99     is_action_enabled: function(action) {
100         if (action === 'create' && !this.options.creatable)
101             return false;
102         return this._super(action);
103     },
104     /*  add_qweb_template
105     *   select the nodes into the xml and send to extract_aggregates the nodes with TagName="field"
106     */
107     add_qweb_template: function() {
108         for (var i=0, ii=this.fields_view.arch.children.length; i < ii; i++) {
109             var child = this.fields_view.arch.children[i];
110             if (child.tag === "templates") {
111                 this.transform_qweb_template(child);
112                 this.qweb.add_template(instance.web.json_node_to_xml(child));
113                 break;
114             } else if (child.tag === 'field') {
115                 this.extract_aggregates(child);
116             }
117         }
118     },
119     /*  extract_aggregates
120     *   extract the agggregates from the nodes (TagName="field")
121     */
122     extract_aggregates: function(node) {
123         for (var j = 0, jj = this.group_operators.length; j < jj;  j++) {
124             if (node.attrs[this.group_operators[j]]) {
125                 this.aggregates[node.attrs.name] = node.attrs[this.group_operators[j]];
126                 break;
127             }
128         }
129     },
130     transform_qweb_template: function(node) {
131         var qweb_add_if = function(node, condition) {
132             if (node.attrs[QWeb.prefix + '-if']) {
133                 condition = _.str.sprintf("(%s) and (%s)", node.attrs[QWeb.prefix + '-if'], condition);
134             }
135             node.attrs[QWeb.prefix + '-if'] = condition;
136         };
137         // Process modifiers
138         if (node.tag && node.attrs.modifiers) {
139             var modifiers = JSON.parse(node.attrs.modifiers || '{}');
140             if (modifiers.invisible) {
141                 qweb_add_if(node, _.str.sprintf("!kanban_compute_domain(%s)", JSON.stringify(modifiers.invisible)));
142             }
143         }
144         switch (node.tag) {
145             case 'field':
146                 var ftype = this.fields_view.fields[node.attrs.name].type;
147                 ftype = node.attrs.widget ? node.attrs.widget : ftype;
148                 if (ftype === 'many2many') {
149                     if (_.indexOf(this.many2manys, node.attrs.name) < 0) {
150                         this.many2manys.push(node.attrs.name);
151                     }
152                     node.tag = 'div';
153                     node.attrs['class'] = (node.attrs['class'] || '') + ' oe_form_field oe_tags';
154                 } else if (instance.web_kanban.fields_registry.contains(ftype)) {
155                     // do nothing, the kanban record will handle it
156                 } else {
157                     node.tag = QWeb.prefix;
158                     node.attrs[QWeb.prefix + '-esc'] = 'record.' + node.attrs['name'] + '.value';
159                 }
160                 break;
161             case 'button':
162             case 'a':
163                 var type = node.attrs.type || '';
164                 if (_.indexOf('action,object,edit,open,delete,url'.split(','), type) !== -1) {
165                     _.each(node.attrs, function(v, k) {
166                         if (_.indexOf('icon,type,name,args,string,context,states,kanban_states'.split(','), k) != -1) {
167                             node.attrs['data-' + k] = v;
168                             delete(node.attrs[k]);
169                         }
170                     });
171                     if (node.attrs['data-string']) {
172                         node.attrs.title = node.attrs['data-string'];
173                     }
174                     if (node.attrs['data-icon']) {
175                         node.children = [{
176                             tag: 'img',
177                             attrs: {
178                                 src: instance.session.prefix + '/web/static/src/img/icons/' + node.attrs['data-icon'] + '.png',
179                                 width: '16',
180                                 height: '16'
181                             }
182                         }];
183                     }
184                     if (node.tag == 'a' && node.attrs['data-type'] != "url") {
185                         node.attrs.href = '#';
186                     } else {
187                         node.attrs.type = 'button';
188                     }
189                     node.attrs['class'] = (node.attrs['class'] || '') + ' oe_kanban_action oe_kanban_action_' + node.tag;
190                 }
191                 break;
192         }
193         if (node.children) {
194             for (var i = 0, ii = node.children.length; i < ii; i++) {
195                 this.transform_qweb_template(node.children[i]);
196             }
197         }
198     },
199     do_add_record: function() {
200         this.dataset.index = null;
201         this.do_switch_view('form');
202     },
203     do_add_group: function() {
204         var self = this;
205         self.do_action({
206             name: _t("Add column"),
207             res_model: self.group_by_field.relation,
208             views: [[false, 'form']],
209             type: 'ir.actions.act_window',
210             target: "new",
211             context: self.dataset.get_context(),
212             flags: {
213                 action_buttons: true,
214             }
215         });
216         var am = instance.webclient.action_manager;
217         var form = am.dialog_widget.views.form.controller;
218         form.on("on_button_cancel", am.dialog, am.dialog.close);
219         form.on('record_created', self, function(r) {
220             (new instance.web.DataSet(self, self.group_by_field.relation)).name_get([r]).done(function(new_record) {
221                 am.dialog.close();
222                 var domain = self.dataset.domain.slice(0);
223                 domain.push([self.group_by, '=', new_record[0][0]]);
224                 var dataset = new instance.web.DataSetSearch(self, self.dataset.model, self.dataset.get_context(), domain);
225                 var datagroup = {
226                     get: function(key) {
227                         return this[key];
228                     },
229                     value: new_record[0],
230                     length: 0,
231                     aggregates: {},
232                 };
233                 var new_group = new instance.web_kanban.KanbanGroup(self, [], datagroup, dataset);
234                 self.do_add_groups([new_group]).done(function() {
235                     $(window).scrollTo(self.groups.slice(-1)[0].$el, { axis: 'x' });
236                 });
237             });
238         });
239     },
240     do_search: function(domain, context, group_by) {
241         var self = this;
242         this.search_domain = domain;
243         this.search_context = context;
244         this.search_group_by = group_by;
245         return $.when(this.has_been_loaded).then(function() {
246             self.group_by = group_by.length ? group_by[0] : self.fields_view.arch.attrs.default_group_by;
247             self.group_by_field = self.fields_view.fields[self.group_by] || {};
248             self.grouped_by_m2o = (self.group_by_field.type === 'many2one');
249             self.$buttons.find('.oe_alternative').toggle(self.grouped_by_m2o);
250             self.$el.toggleClass('oe_kanban_grouped_by_m2o', self.grouped_by_m2o);
251             var grouping_fields = self.group_by ? [self.group_by].concat(_.keys(self.aggregates)) : undefined;
252             if (!_.isEmpty(grouping_fields)) {
253                 // ensure group_by fields are read.
254                 self.fields_keys = _.unique(self.fields_keys.concat(grouping_fields));
255             }
256             var grouping = new instance.web.Model(self.dataset.model, context, domain).query(self.fields_keys).group_by(grouping_fields);
257             return self.alive($.when(grouping)).then(function(groups) {
258                 self.remove_no_result();
259                 if (groups) {
260                     return self.do_process_groups(groups);
261                 } else {
262                     return self.do_process_dataset();
263                 }
264             });
265         });
266     },
267     do_process_groups: function(groups) {
268         var self = this;
269         this.$el.find('table:first').show();
270         this.$el.removeClass('oe_kanban_ungrouped').addClass('oe_kanban_grouped');
271         return this.add_group_mutex.exec(function() {
272             self.do_clear_groups();
273             self.dataset.ids = [];
274             if (!groups.length) {
275                 self.no_result();
276                 return $.when();
277             }
278             self.nb_records = 0;
279             var remaining = groups.length - 1,
280                 groups_array = [];
281             return $.when.apply(null, _.map(groups, function (group, index) {
282                 var def = $.when([]);
283                 var dataset = new instance.web.DataSetSearch(self, self.dataset.model,
284                     new instance.web.CompoundContext(self.dataset.get_context(), group.model.context()), group.model.domain());
285                 if (group.attributes.length >= 1) {
286                     def = dataset.read_slice(self.fields_keys.concat(['__last_update']), { 'limit': self.limit });
287                 }
288                 return def.then(function(records) {
289                         self.nb_records += records.length;
290                         self.dataset.ids.push.apply(self.dataset.ids, dataset.ids);
291                         groups_array[index] = new instance.web_kanban.KanbanGroup(self, records, group, dataset);
292                         if (self.dataset.index >= records.length){
293                             self.dataset.index = self.dataset.size() ? 0 : null;
294                         }
295                         if (!remaining--) {
296                             return self.do_add_groups(groups_array);
297                         }
298                 });
299             })).then(function () {
300                 if(!self.nb_records) {
301                     self.no_result();
302                 }
303                 self.trigger('kanban_groups_processed');
304             });
305         });
306     },
307     do_process_dataset: function() {
308         var self = this;
309         this.$el.find('table:first').show();
310         this.$el.removeClass('oe_kanban_grouped').addClass('oe_kanban_ungrouped');
311         var def = $.Deferred();
312         this.add_group_mutex.exec(function() {
313             self.do_clear_groups();
314             self.dataset.read_slice(self.fields_keys.concat(['__last_update']), { 'limit': self.limit }).done(function(records) {
315                 var kgroup = new instance.web_kanban.KanbanGroup(self, records, null, self.dataset);
316                 if (!_.isEmpty(self.dataset.ids) && (self.dataset.index === null || self.dataset.index >= self.dataset.ids.length)) {
317                     self.dataset.index = 0;
318                 } else if (_.isEmpty(self.dataset.ids)){
319                     self.dataset.index = null;
320                 }
321                 self.do_add_groups([kgroup]).done(function() {
322                     if (_.isEmpty(records)) {
323                         self.no_result();
324                     }
325                     self.trigger('kanban_dataset_processed');
326                     def.resolve();
327                 });
328             }).done(null, function() {
329                 def.reject();
330             });
331         });
332         return def;
333     },
334     do_reload: function() {
335         this.do_search(this.search_domain, this.search_context, this.search_group_by);
336     },
337     do_clear_groups: function() {
338         var groups = this.groups.slice(0);
339         this.groups = [];
340         _.each(groups, function(group) {
341             group.destroy();
342         });
343     },
344     do_add_groups: function(groups) {
345         var self = this;
346         var $parent = this.$el.parent();
347         this.$el.detach();
348         _.each(groups, function(group) {
349             self.groups[group.undefined_title ? 'unshift' : 'push'](group);
350         });
351         var $last_td = self.$el.find('.oe_kanban_groups_headers td:last');
352         var groups_started = _.map(this.groups, function(group) {
353             if (!group.is_started) {
354                 group.on("add_record", self, function () {
355                     self.remove_no_result();
356                 });
357                 return group.insertBefore($last_td);
358             }
359         });
360         return $.when.apply(null, groups_started).done(function () {
361             self.on_groups_started();
362             self.$el.appendTo($parent);
363             _.each(self.groups, function(group) {
364                 group.compute_cards_auto_height();
365             });
366         });
367     },
368     on_groups_started: function() {
369         var self = this;
370         if (this.group_by || this.fields_keys.indexOf("sequence") !== -1) {
371             // Kanban cards drag'n'drop
372             var prev_widget, is_folded, record, $columns;
373             if (this.group_by) {
374                 $columns = this.$el.find('.oe_kanban_column .oe_kanban_column_cards, .oe_kanban_column .oe_kanban_folded_column_cards');
375             } else {
376                 $columns = this.$el.find('.oe_kanban_column_cards');
377             }
378             $columns.sortable({
379                 handle : '.oe_kanban_draghandle',
380                 start: function(event, ui) {
381                     self.currently_dragging.index = ui.item.parent().children('.oe_kanban_record').index(ui.item);
382                     self.currently_dragging.group = prev_widget = ui.item.parents('.oe_kanban_column:first').data('widget');
383                     ui.item.find('*').on('click.prevent', function(ev) {
384                         return false;
385                     });
386                     record = ui.item.data('widget');
387                     record.$el.bind('mouseup',function(ev,ui){
388                         if (is_folded) {
389                             record.$el.hide();
390                         }
391                         record.$el.unbind('mouseup');
392                     })
393                     ui.placeholder.height(ui.item.height());
394                 },
395                 over: function(event, ui) {
396                     var parent = $(event.target).parent();
397                     prev_widget.highlight(false);
398                     is_folded = parent.hasClass('oe_kanban_group_folded'); 
399                     if (is_folded) {
400                         var widget = parent.data('widget');
401                         widget.highlight(true);
402                         prev_widget = widget;
403                     }
404                  },
405                 revert: 150,
406                 stop: function(event, ui) {
407                     prev_widget.highlight(false);
408                     var old_index = self.currently_dragging.index;
409                     var new_index = ui.item.parent().children('.oe_kanban_record').index(ui.item);
410                     var old_group = self.currently_dragging.group;
411                     var new_group = ui.item.parents('.oe_kanban_column:first').data('widget');
412                     if (!(old_group.title === new_group.title && old_group.value === new_group.value && old_index == new_index)) {
413                         self.on_record_moved(record, old_group, old_index, new_group, new_index);
414                     }
415                     setTimeout(function() {
416                         // A bit hacky but could not find a better solution for Firefox (problem not present in chrome)
417                         // http://stackoverflow.com/questions/274843/preventing-javascript-click-event-with-scriptaculous-drag-and-drop
418                         ui.item.find('*').off('click.prevent');
419                     }, 0);
420                 },
421                 scroll: false
422             });
423             // Keep connectWith out of the sortable initialization for performance sake:
424             // http://www.planbox.com/blog/development/coding/jquery-ui-sortable-slow-to-bind.html
425             $columns.sortable({ connectWith: $columns });
426
427             // Kanban groups drag'n'drop
428             var start_index;
429             if (this.grouped_by_m2o) {
430                 this.$('.oe_kanban_groups_headers').sortable({
431                     items: '.oe_kanban_group_header',
432                     helper: 'clone',
433                     axis: 'x',
434                     opacity: 0.5,
435                     scroll: false,
436                     start: function(event, ui) {
437                         start_index = ui.item.index();
438                         self.$('.oe_kanban_record, .oe_kanban_quick_create').css({ visibility: 'hidden' });
439                     },
440                     stop: function(event, ui) {
441                         var stop_index = ui.item.index();
442                         if (start_index !== stop_index) {
443                             var $start_column = self.$('.oe_kanban_groups_records .oe_kanban_column').eq(start_index);
444                             var $stop_column = self.$('.oe_kanban_groups_records .oe_kanban_column').eq(stop_index);
445                             var method = (start_index > stop_index) ? 'insertBefore' : 'insertAfter';
446                             $start_column[method]($stop_column);
447                             var tmp_group = self.groups.splice(start_index, 1)[0];
448                             self.groups.splice(stop_index, 0, tmp_group);
449                             var new_sequence = _.pluck(self.groups, 'value');
450                             (new instance.web.DataSet(self, self.group_by_field.relation)).resequence(new_sequence).done(function(r) {
451                                 if (r === false) {
452                                     console.error("Kanban: could not resequence model '%s'. Probably no 'sequence' field.", self.group_by_field.relation);
453                                 }
454                             });
455                         }
456                         self.$('.oe_kanban_record, .oe_kanban_quick_create').css({ visibility: 'visible' });
457                     }
458                 });
459             }
460         } else {
461             this.$el.find('.oe_kanban_draghandle').removeClass('oe_kanban_draghandle');
462         }
463         this.postprocess_m2m_tags();
464     },
465     on_record_moved : function(record, old_group, old_index, new_group, new_index) {
466         var self = this;
467         record.$el.find('[title]').tooltip('destroy');
468         $(old_group.$el).add(new_group.$el).find('.oe_kanban_aggregates, .oe_kanban_group_length').hide();
469         if (old_group === new_group) {
470             new_group.records.splice(old_index, 1);
471             new_group.records.splice(new_index, 0, record);
472             new_group.do_save_sequences();
473         } else {
474             old_group.records.splice(old_index, 1);
475             new_group.records.splice(new_index, 0, record);
476             record.group = new_group;
477             var data = {};
478             data[this.group_by] = new_group.value;
479             this.dataset.write(record.id, data, {}).done(function() {
480                 record.do_reload();
481                 new_group.do_save_sequences();
482                 if (new_group.state.folded) {
483                     new_group.do_action_toggle_fold();
484                     record.prependTo(new_group.$records.find('.oe_kanban_column_cards'));
485                 }
486             }).fail(function(error, evt) {
487                 evt.preventDefault();
488                 alert(_t("An error has occured while moving the record to this group: ") + error.data.message);
489                 self.do_reload(); // TODO: use draggable + sortable in order to cancel the dragging when the rcp fails
490             });
491         }
492     },
493
494     do_show: function() {
495         if (this.options.$buttons) {
496             this.$buttons.show();
497         }
498         this.do_push_state({});
499         return this._super();
500     },
501     do_hide: function () {
502         if (this.$buttons) {
503             this.$buttons.hide();
504         }
505         return this._super();
506     },
507     open_record: function(id, editable) {
508         if (this.dataset.select_id(id)) {
509             this.do_switch_view('form', null, { mode: editable ? "edit" : undefined });
510         } else {
511             this.do_warn("Kanban: could not find id#" + id);
512         }
513     },
514     no_result: function() {
515         var self = this;
516         if (this.groups.group_by
517             || !this.options.action
518             || (!this.options.action.help && !this.options.action.get_empty_list_help)) {
519             return;
520         }
521         this.$el.css("position", "relative");
522         $(QWeb.render('KanbanView.nocontent', { content : this.options.action.get_empty_list_help || this.options.action.help})).insertBefore(this.$('table:first'));
523         this.$el.find('.oe_view_nocontent').click(function() {
524             self.$buttons.openerpBounce();
525         });
526     },
527     remove_no_result: function() {
528         this.$el.css("position", "");
529         this.$el.find('.oe_view_nocontent').remove();
530     },
531
532     /*
533     *  postprocessing of fields type many2many
534     *  make the rpc request for all ids/model and insert value inside .oe_tags fields
535     */
536     postprocess_m2m_tags: function() {
537         var self = this;
538         if (!this.many2manys.length) {
539             return;
540         }
541         var relations = {};
542         this.groups.forEach(function(group) {
543             group.records.forEach(function(record) {
544                 self.many2manys.forEach(function(name) {
545                     var field = record.record[name];
546                     var $el = record.$('.oe_form_field.oe_tags[name=' + name + ']').empty();
547                     if (!relations[field.relation]) {
548                         relations[field.relation] = { ids: [], elements: {}};
549                     }
550                     var rel = relations[field.relation];
551                     field.raw_value.forEach(function(id) {
552                         rel.ids.push(id);
553                         if (!rel.elements[id]) {
554                             rel.elements[id] = [];
555                         }
556                         rel.elements[id].push($el[0]);
557                     });
558                 });
559             });
560         });
561        _.each(relations, function(rel, rel_name) {
562             var dataset = new instance.web.DataSetSearch(self, rel_name, self.dataset.get_context());
563             dataset.name_get(_.uniq(rel.ids)).done(function(result) {
564                 result.forEach(function(nameget) {
565                     $(rel.elements[nameget[0]]).append('<span class="oe_tag">' + _.str.escapeHTML(nameget[1]) + '</span>');
566                 });
567             });
568         });
569     }
570 });
571
572
573 function get_class(name) {
574     return new instance.web.Registry({'tmp' : name}).get_object("tmp");
575 }
576
577 instance.web_kanban.KanbanGroup = instance.web.Widget.extend({
578     template: 'KanbanView.group_header',
579     init: function (parent, records, group, dataset) {
580         var self = this;
581         this._super(parent);
582         this.$has_been_started = $.Deferred();
583         this.view = parent;
584         this.group = group;
585         this.dataset = dataset;
586         this.dataset_offset = 0;
587         this.aggregates = {};
588         this.value = this.title = null;
589         if (this.group) {
590             this.value = group.get('value');
591             this.title = group.get('value');
592             if (this.value instanceof Array) {
593                 this.title = this.value[1];
594                 this.value = this.value[0];
595             }
596             var field = this.view.group_by_field;
597             if (!_.isEmpty(field)) {
598                 try {
599                     this.title = instance.web.format_value(group.get('value'), field, false);
600                 } catch(e) {}
601             }
602             _.each(this.view.aggregates, function(value, key) {
603                 self.aggregates[value] = instance.web.format_value(group.get('aggregates')[key], {type: 'float'});
604             });
605         }
606
607         if (this.title === false) {
608             this.title = _t('Undefined');
609             this.undefined_title = true;
610         }
611         var key = this.view.group_by + '-' + this.value;
612         if (!this.view.state.groups[key]) {
613             this.view.state.groups[key] = {
614                 folded: group ? group.get('folded') : false
615             };
616         }
617         this.state = this.view.state.groups[key];
618         this.$records = null;
619
620         this.records = [];
621         this.$has_been_started.done(function() {
622             self.do_add_records(records);
623         });
624     },
625     start: function() {
626         var self = this;
627         if (! self.view.group_by) {
628             self.$el.addClass("oe_kanban_no_group");
629             self.quick = new (get_class(self.view.quick_create_class))(this, self.dataset, {}, false)
630                 .on('added', self, self.proxy('quick_created'));
631             self.quick.replace($(".oe_kanban_no_group_qc_placeholder"));
632         }
633         this.$records = $(QWeb.render('KanbanView.group_records_container', { widget : this}));
634         this.$records.insertBefore(this.view.$el.find('.oe_kanban_groups_records td:last'));
635
636         this.$el.on('click', '.oe_kanban_group_dropdown li a', function(ev) {
637             var fn = 'do_action_' + $(ev.target).data().action;
638             if (typeof(self[fn]) === 'function') {
639                 self[fn]($(ev.target));
640             }
641         });
642
643         this.$el.find('.oe_kanban_add').click(function () {
644             if (self.view.quick) {
645                 self.view.quick.trigger('close');
646             }
647             if (self.quick) {
648                 return false;
649             }
650             self.view.$el.find('.oe_view_nocontent').hide();
651             var ctx = {};
652             ctx['default_' + self.view.group_by] = self.value;
653             self.quick = new (get_class(self.view.quick_create_class))(this, self.dataset, ctx, true)
654                 .on('added', self, self.proxy('quick_created'))
655                 .on('close', self, function() {
656                     self.view.$el.find('.oe_view_nocontent').show();
657                     this.quick.destroy();
658                     delete self.view.quick;
659                     delete this.quick;
660                 });
661             self.quick.appendTo($(".oe_kanban_group_list_header", self.$records));
662             self.quick.focus();
663             self.view.quick = self.quick;
664         });
665         // Add bounce effect on image '+' of kanban header when click on empty space of kanban grouped column.
666         this.$records.on('click', '.oe_kanban_show_more', this.do_show_more);
667         if (this.state.folded) {
668             this.do_toggle_fold();
669         }
670         this.$el.data('widget', this);
671         this.$records.data('widget', this);
672         this.$has_been_started.resolve();
673         var add_btn = this.$el.find('.oe_kanban_add');
674         add_btn.tooltip({delay: { show: 500, hide:1000 }});
675         this.$records.find(".oe_kanban_column_cards").click(function (ev) {
676             if (ev.target == ev.currentTarget) {
677                 if (!self.state.folded) {
678                     add_btn.openerpBounce();
679                 }
680             }
681         });
682         this.is_started = true;
683         var def_tooltip = this.fetch_tooltip();
684         return $.when(def_tooltip);
685     },
686     fetch_tooltip: function() {
687         if (! this.group)
688             return;
689         var field_name = this.view.group_by;
690         var field = this.view.group_by_field;
691         var field_desc = null;
692         var recurse = function(node) {
693             if (node.tag === "field" && node.attrs.name === field_name) {
694                 field_desc = node;
695                 return;
696             }
697             _.each(node.children, function(child) {
698                 if (field_desc === null)
699                     recurse(child);
700             });
701         };
702         recurse(this.view.fields_view.arch);
703         if (! field_desc)
704             return;
705         var options = instance.web.py_eval(field_desc.attrs.options || '{}')
706         if (! options.tooltip_on_group_by)
707             return;
708
709         var self = this;
710         if (this.value) {
711             return (new instance.web.Model(field.relation)).query([options.tooltip_on_group_by])
712                     .filter([["id", "=", this.value]]).first().then(function(res) {
713                 self.tooltip = res[options.tooltip_on_group_by];
714                 self.$(".oe_kanban_group_title_text").attr("title", self.tooltip || self.title || "").tooltip();
715             });
716         }
717     },
718     compute_cards_auto_height: function() {
719         // oe_kanban_no_auto_height is an empty class used to disable this feature
720         if (!this.view.group_by) {
721             var min_height = 0;
722             var els = [];
723             _.each(this.records, function(r) {
724                 var $e = r.$el.children(':first:not(.oe_kanban_no_auto_height)').css('min-height', 0);
725                 if ($e.length) {
726                     els.push($e[0]);
727                     min_height = Math.max(min_height, $e.outerHeight());
728                 }
729             });
730             $(els).css('min-height', min_height);
731         }
732     },
733     destroy: function() {
734         this._super();
735         if (this.$records) {
736             this.$records.remove();
737         }
738     },
739     do_show_more: function(evt) {
740         var self = this;
741         var ids = self.view.dataset.ids.splice(0);
742         return this.dataset.read_slice(this.view.fields_keys.concat(['__last_update']), {
743             'limit': self.view.limit,
744             'offset': self.dataset_offset += self.view.limit
745         }).then(function(records) {
746             self.view.dataset.ids = ids.concat(self.dataset.ids);
747             self.do_add_records(records);
748             self.compute_cards_auto_height();
749             self.view.postprocess_m2m_tags();
750             return records;
751         });
752     },
753     do_add_records: function(records, prepend) {
754         var self = this;
755         var $list_header = this.$records.find('.oe_kanban_group_list_header');
756         var $show_more = this.$records.find('.oe_kanban_show_more');
757         var $cards = this.$records.find('.oe_kanban_column_cards');
758
759         _.each(records, function(record) {
760             var rec = new instance.web_kanban.KanbanRecord(self, record);
761             if (!prepend) {
762                 rec.appendTo($cards);
763                 self.records.push(rec);
764             } else {
765                 rec.prependTo($cards);
766                 self.records.unshift(rec);
767             }
768         });
769         if ($show_more.length) {
770             var size = this.dataset.size();
771             $show_more.toggle(this.records.length < size).find('.oe_kanban_remaining').text(size - this.records.length);
772         }
773     },
774     remove_record: function(id, remove_from_dataset) {
775         for (var i = 0; i < this.records.length; i++) {
776             if (this.records[i]['id'] === id) {
777                 this.records.splice(i, 1);
778                 i--;
779             }
780         }
781     },
782     do_toggle_fold: function(compute_width) {
783         this.$el.add(this.$records).toggleClass('oe_kanban_group_folded');
784         this.state.folded = this.$el.is('.oe_kanban_group_folded');
785         this.$("ul.oe_kanban_group_dropdown li a[data-action=toggle_fold]").text((this.state.folded) ? _t("Unfold") : _t("Fold"));
786     },
787     do_action_toggle_fold: function() {
788         this.do_toggle_fold();
789     },
790     do_action_edit: function() {
791         var self = this;
792         self.do_action({
793             res_id: this.value,
794             name: _t("Edit column"),
795             res_model: self.view.group_by_field.relation,
796             views: [[false, 'form']],
797             type: 'ir.actions.act_window',
798             target: "new",
799             flags: {
800                 action_buttons: true,
801             }
802         });
803         var am = instance.webclient.action_manager;
804         var form = am.dialog_widget.views.form.controller;
805         form.on("on_button_cancel", am.dialog, function() { return am.dialog.$dialog_box.modal('hide'); });
806         form.on('record_saved', self, function() {
807             am.dialog.$dialog_box.modal('hide');
808             self.view.do_reload();
809         });
810     },
811     do_action_delete: function() {
812         var self = this;
813         if (confirm(_t("Are you sure to remove this column ?"))) {
814             (new instance.web.DataSet(self, self.view.group_by_field.relation)).unlink([self.value]).done(function(r) {
815                 self.view.do_reload();
816             });
817         }
818     },
819     do_save_sequences: function() {
820         var self = this;
821         if (_.indexOf(this.view.fields_keys, 'sequence') > -1) {
822             var new_sequence = _.pluck(this.records, 'id');
823             self.view.dataset.resequence(new_sequence);
824         }
825     },
826     /**
827      * Handles a newly created record
828      *
829      * @param {id} id of the newly created record
830      */
831     quick_created: function (record) {
832         var id = record, self = this;
833         self.view.remove_no_result();
834         self.trigger("add_record");
835         this.dataset.read_ids([id], this.view.fields_keys)
836             .done(function (records) {
837                 self.view.dataset.ids.push(id);
838                 self.do_add_records(records, true);
839             });
840     },
841     highlight: function(show){
842         if(show){
843             this.$el.addClass('oe_kanban_column_higlight');
844             this.$records.addClass('oe_kanban_column_higlight');
845         }else{
846             this.$el.removeClass('oe_kanban_column_higlight');
847             this.$records.removeClass('oe_kanban_column_higlight');
848         }
849     }
850 });
851
852 instance.web_kanban.KanbanRecord = instance.web.Widget.extend({
853     template: 'KanbanView.record',
854     init: function (parent, record) {
855         this._super(parent);
856         this.group = parent;
857         this.view = parent.view;
858         this.id = null;
859         this.set_record(record);
860         if (!this.view.state.records[this.id]) {
861             this.view.state.records[this.id] = {
862                 folded: false
863             };
864         }
865         this.state = this.view.state.records[this.id];
866         this.fields = {};
867     },
868     set_record: function(record) {
869         var self = this;
870         this.id = record.id;
871         this.values = {};
872         _.each(record, function(v, k) {
873             self.values[k] = {
874                 value: v
875             };
876         });
877         this.record = this.transform_record(record);
878     },
879     start: function() {
880         var self = this;
881         this._super();
882         this.init_content();
883     },
884     init_content: function() {
885         var self = this;
886         self.sub_widgets = [];
887         this.$("[data-field_id]").each(function() {
888             self.add_widget($(this));
889         });
890         this.$el.data('widget', this);
891         this.bind_events();
892     },
893     transform_record: function(record) {
894         var self = this,
895             new_record = {};
896         _.each(record, function(value, name) {
897             var r = _.clone(self.view.fields_view.fields[name] || {});
898             if ((r.type === 'date' || r.type === 'datetime') && value) {
899                 r.raw_value = instance.web.auto_str_to_date(value);
900             } else {
901                 r.raw_value = value;
902             }
903             r.value = instance.web.format_value(value, r);
904             new_record[name] = r;
905         });
906         return new_record;
907     },
908     renderElement: function() {
909         this.qweb_context = {
910             instance: instance,
911             record: this.record,
912             widget: this,
913             read_only_mode: this.view.options.read_only_mode,
914         };
915         for (var p in this) {
916             if (_.str.startsWith(p, 'kanban_')) {
917                 this.qweb_context[p] = _.bind(this[p], this);
918             }
919         }
920         var $el = instance.web.qweb.render(this.template, {
921             'widget': this,
922             'content': this.view.qweb.render('kanban-box', this.qweb_context)
923         });
924         this.replaceElement($el);
925         this.replace_fields();
926     },
927     replace_fields: function() {
928         var self = this;
929         this.$("field").each(function() {
930             var $field = $(this);
931             var $nfield = $("<span></span");
932             var id = _.uniqueId("kanbanfield");
933             self.fields[id] = $field;
934             $nfield.attr("data-field_id", id);
935             $field.replaceWith($nfield);
936         });
937     },
938     add_widget: function($node) {
939         var $orig = this.fields[$node.data("field_id")];
940         var field = this.record[$orig.attr("name")];
941         var type = field.type;
942         type = $orig.attr("widget") ? $orig.attr("widget") : type;
943         var obj = instance.web_kanban.fields_registry.get_object(type);
944         var widget = new obj(this, field, $orig);
945         this.sub_widgets.push(widget);
946         widget.replace($node);
947     },
948     bind_events: function() {
949         var self = this;
950         this.setup_color_picker();
951         this.$el.find('[title]').each(function(){
952             $(this).tooltip({
953                 delay: { show: 500, hide: 0},
954                 title: function() {
955                     var template = $(this).attr('tooltip');
956                     if (!self.view.qweb.has_template(template)) {
957                         return false;
958                     }
959                     return self.view.qweb.render(template, self.qweb_context);
960                 },
961             });
962         });
963
964         // If no draghandle is found, make the whole card as draghandle (provided one can edit)
965         if (!this.$el.find('.oe_kanban_draghandle').length) {
966             this.$el.children(':first')
967                 .toggleClass('oe_kanban_draghandle', this.view.is_action_enabled('edit'));
968         }
969
970         this.$el.find('.oe_kanban_action').click(function(ev) {
971             ev.preventDefault();
972             var $action = $(this),
973                 type = $action.data('type') || 'button',
974                 method = 'do_action_' + (type === 'action' ? 'object' : type);
975             if ((type === 'edit' || type === 'delete') && ! self.view.is_action_enabled(type)) {
976                 self.view.open_record(self.id, true);
977             } else if (_.str.startsWith(type, 'switch_')) {
978                 self.view.do_switch_view(type.substr(7));
979             } else if (typeof self[method] === 'function') {
980                 self[method]($action);
981             } else {
982                 self.do_warn("Kanban: no action for type : " + type);
983             }
984         });
985
986         if (this.$el.find('.oe_kanban_global_click,.oe_kanban_global_click_edit').length) {
987             this.$el.on('click', function(ev) {
988                 if (!ev.isTrigger && !$._data(ev.target, 'events')) {
989                     var trigger = true;
990                     var elem = ev.target;
991                     var ischild = true;
992                     var children = [];
993                     while (elem) {
994                         var events = $._data(elem, 'events');
995                         if (elem == ev.currentTarget) {
996                             ischild = false;
997                         }
998                         if (ischild) {
999                             children.push(elem);
1000                             if (events && events.click) {
1001                                 // do not trigger global click if one child has a click event registered
1002                                 trigger = false;
1003                             }
1004                         }
1005                         if (trigger && events && events.click) {
1006                             _.each(events.click, function(click_event) {
1007                                 if (click_event.selector) {
1008                                     // For each parent of original target, check if a
1009                                     // delegated click is bound to any previously found children
1010                                     _.each(children, function(child) {
1011                                         if ($(child).is(click_event.selector)) {
1012                                             trigger = false;
1013                                         }
1014                                     });
1015                                 }
1016                             });
1017                         }
1018                         elem = elem.parentElement;
1019                     }
1020                     if (trigger) {
1021                         self.on_card_clicked(ev);
1022                     }
1023                 }
1024             });
1025         }
1026     },
1027     /* actions when user click on the block with a specific class
1028      *  open on normal view : oe_kanban_global_click
1029      *  open on form/edit view : oe_kanban_global_click_edit
1030      */
1031     on_card_clicked: function(ev) {
1032         if (this.$el.find('.oe_kanban_global_click').size() > 0 && this.$el.find('.oe_kanban_global_click').data('routing')) {
1033             instance.web.redirect(this.$el.find('.oe_kanban_global_click').data('routing') + "/" + this.id);
1034         }
1035         else if (this.$el.find('.oe_kanban_global_click_edit').size()>0)
1036             this.do_action_edit();
1037         else
1038             this.do_action_open();
1039     },
1040     setup_color_picker: function() {
1041         var self = this;
1042         var $el = this.$el.find('ul.oe_kanban_colorpicker');
1043         if ($el.length) {
1044             $el.html(QWeb.render('KanbanColorPicker', {
1045                 widget: this
1046             }));
1047             $el.on('click', 'a', function(ev) {
1048                 ev.preventDefault();
1049                 var color_field = $(this).parents('.oe_kanban_colorpicker').first().data('field') || 'color';
1050                 var data = {};
1051                 data[color_field] = $(this).data('color');
1052                 self.view.dataset.write(self.id, data, {}).done(function() {
1053                     self.record[color_field] = $(this).data('color');
1054                     self.do_reload();
1055                 });
1056             });
1057         }
1058     },
1059     do_action_delete: function($action) {
1060         var self = this;
1061         function do_it() {
1062             return $.when(self.view.dataset.unlink([self.id])).done(function() {
1063                 self.group.remove_record(self.id);
1064                 self.destroy();
1065             });
1066         }
1067         if (this.view.options.confirm_on_delete) {
1068             if (confirm(_t("Are you sure you want to delete this record ?"))) {
1069                 return do_it();
1070             }
1071         } else
1072             return do_it();
1073     },
1074     do_action_edit: function($action) {
1075         this.view.open_record(this.id, true);
1076     },
1077     do_action_open: function($action) {
1078         this.view.open_record(this.id);
1079     },
1080     do_action_object: function ($action) {
1081         var button_attrs = $action.data();
1082         this.view.do_execute_action(button_attrs, this.view.dataset, this.id, this.do_reload);
1083     },
1084     do_action_url: function($action) {
1085         return instance.web.redirect($action.attr("href"));
1086      },
1087     do_reload: function() {
1088         var self = this;
1089         this.view.dataset.read_ids([this.id], this.view.fields_keys.concat(['__last_update'])).done(function(records) {
1090              _.each(self.sub_widgets, function(el) {
1091                  el.destroy();
1092              });
1093              self.sub_widgets = [];
1094             if (records.length) {
1095                 self.set_record(records[0]);
1096                 self.renderElement();
1097                 self.init_content();
1098                 self.group.compute_cards_auto_height();
1099                 self.view.postprocess_m2m_tags();
1100             } else {
1101                 self.destroy();
1102             }
1103         });
1104     },
1105     kanban_getcolor: function(variable) {
1106         var index = 0;
1107         switch (typeof(variable)) {
1108             case 'string':
1109                 for (var i=0, ii=variable.length; i<ii; i++) {
1110                     index += variable.charCodeAt(i);
1111                 }
1112                 break;
1113             case 'number':
1114                 index = Math.round(variable);
1115                 break;
1116             default:
1117                 return '';
1118         }
1119         var color = (index % this.view.number_of_color_schemes);
1120         return color;
1121     },
1122     kanban_color: function(variable) {
1123         var color = this.kanban_getcolor(variable);
1124         return color === '' ? '' : 'oe_kanban_color_' + color;
1125     },
1126     kanban_image: function(model, field, id, cache, options) {
1127         options = options || {};
1128         var url;
1129         if (this.record[field] && this.record[field].value && !instance.web.form.is_bin_size(this.record[field].value)) {
1130             url = 'data:image/png;base64,' + this.record[field].value;
1131         } else if (this.record[field] && ! this.record[field].value) {
1132             url = "/web/static/src/img/placeholder.png";
1133         } else {
1134             id = JSON.stringify(id);
1135             if (options.preview_image)
1136                 field = options.preview_image;
1137             url = this.session.url('/web/binary/image', {model: model, field: field, id: id});
1138             if (cache !== undefined) {
1139                 // Set the cache duration in seconds.
1140                 url += '&cache=' + parseInt(cache, 10);
1141             }
1142         }
1143         return url;
1144     },
1145     kanban_text_ellipsis: function(s, size) {
1146         size = size || 160;
1147         if (!s) {
1148             return '';
1149         } else if (s.length <= size) {
1150             return s;
1151         } else {
1152             return s.substr(0, size) + '...';
1153         }
1154     },
1155     kanban_compute_domain: function(domain) {
1156         return instance.web.form.compute_domain(domain, this.values);
1157     }
1158 });
1159
1160 /**
1161  * Quick creation view.
1162  *
1163  * Triggers a single event "added" with a single parameter "name", which is the
1164  * name entered by the user
1165  *
1166  * @class
1167  * @type {*}
1168  */
1169 instance.web_kanban.QuickCreate = instance.web.Widget.extend({
1170     template: 'KanbanView.quick_create',
1171
1172     /**
1173      * close_btn: If true, the widget will display a "Close" button able to trigger
1174      * a "close" event.
1175      */
1176     init: function(parent, dataset, context, buttons) {
1177         this._super(parent);
1178         this._dataset = dataset;
1179         this._buttons = buttons || false;
1180         this._context = context || {};
1181     },
1182     start: function () {
1183         var self = this;
1184         self.$input = this.$el.find('input');
1185         self.$input.keyup(function(event){
1186             if(event.keyCode == 13){
1187                 self.quick_add();
1188             }
1189         });
1190         $(".oe_kanban_quick_create").focusout(function (e) {
1191             var val = self.$el.find('input').val();
1192             if (/^\s*$/.test(val)) { self.trigger('close'); }
1193             e.stopImmediatePropagation();
1194         });
1195         $(".oe_kanban_quick_create_add", this.$el).click(function () {
1196             self.quick_add();
1197             self.focus();
1198         });
1199         $(".oe_kanban_quick_create_close", this.$el).click(function (ev) {
1200             ev.preventDefault();
1201             self.trigger('close');
1202         });
1203         self.$input.keyup(function(e) {
1204             if (e.keyCode == 27 && self._buttons) {
1205                 self.trigger('close');
1206             }
1207         });
1208     },
1209     focus: function() {
1210         this.$el.find('input').focus();
1211     },
1212     /**
1213      * Handles user event from nested quick creation view
1214      */
1215     quick_add: function () {
1216         var self = this;
1217         var val = this.$input.val();
1218         if (/^\s*$/.test(val)) { this.$el.remove(); return; }
1219         this._dataset.call(
1220             'name_create', [val, new instance.web.CompoundContext(
1221                     this._dataset.get_context(), this._context)])
1222             .then(function(record) {
1223                 self.$input.val("");
1224                 self.trigger('added', record[0]);
1225             }, function(error, event) {
1226                 event.preventDefault();
1227                 return self.slow_create();
1228             });
1229     },
1230     slow_create: function() {
1231         var self = this;
1232         var pop = new instance.web.form.SelectCreatePopup(this);
1233         pop.select_element(
1234             self._dataset.model,
1235             {
1236                 title: _t("Create: ") + (this.string || this.name),
1237                 initial_view: "form",
1238                 disable_multiple_selection: true
1239             },
1240             [],
1241             {"default_name": self.$input.val()}
1242         );
1243         pop.on("elements_selected", self, function(element_ids) {
1244             self.$input.val("");
1245             self.trigger('added', element_ids[0]);
1246         });
1247     }
1248 });
1249
1250 /**
1251  * Interface to be implemented by kanban fields.
1252  *
1253  */
1254 instance.web_kanban.FieldInterface = {
1255     /**
1256         Constructor.
1257         - parent: The widget's parent.
1258         - field: A dictionary giving details about the field, including the current field's value in the
1259             raw_value field.
1260         - $node: The field <field> tag as it appears in the view, encapsulated in a jQuery object.
1261     */
1262     init: function(parent, field, $node) {},
1263 };
1264
1265 /**
1266  * Abstract class for classes implementing FieldInterface.
1267  *
1268  * Properties:
1269  *     - value: useful property to hold the value of the field. By default, the constructor
1270  *     sets value property.
1271  *
1272  */
1273 instance.web_kanban.AbstractField = instance.web.Widget.extend(instance.web_kanban.FieldInterface, {
1274     /**
1275         Constructor that saves the field and $node parameters and sets the "value" property.
1276     */
1277     init: function(parent, field, $node) {
1278         this._super(parent);
1279         this.field = field;
1280         this.$node = $node;
1281         this.options = instance.web.py_eval(this.$node.attr("options") || '{}');
1282         this.set("value", field.raw_value);
1283     },
1284 });
1285
1286 instance.web_kanban.Priority = instance.web_kanban.AbstractField.extend({
1287     init: function(parent, field, $node) {
1288         this._super.apply(this, arguments);
1289         this.name = $node.attr('name')
1290         this.parent = parent;
1291     },
1292     prepare_priority: function() {
1293         var self = this;
1294         var selection = this.field.selection || [];
1295         var init_value = selection && selection[0][0] || 0;
1296         var data = _.map(selection.slice(1), function(element, index) {
1297             var value = {
1298                 'value': element[0],
1299                 'name': element[1],
1300                 'click_value': element[0],
1301             }
1302             if (index == 0 && self.get('value') == element[0]) {
1303                 value['click_value'] = init_value;
1304             }
1305             return value;
1306         });
1307         return data;
1308     },
1309     renderElement: function() {
1310         var self = this;
1311         this.record_id = self.parent.id;
1312         this.priorities = self.prepare_priority();
1313         this.$el = $(QWeb.render("Priority", {'widget': this}));
1314         this.$el.find('li').click(self.do_action.bind(self));
1315     },
1316     do_action: function(e) {
1317         var self = this;
1318         var li = $(e.target).closest( "li" );
1319         if (li.length) {
1320             var value = {};
1321             value[self.name] = String(li.data('value'));
1322             return self.parent.view.dataset._model.call('write', [[self.record_id], value, self.parent.view.dataset.get_context()]).done(self.reload_record.bind(self.parent));
1323         }
1324     },
1325     reload_record: function() {
1326         this.do_reload();
1327     },
1328 });
1329
1330 instance.web_kanban.KanbanSelection = instance.web_kanban.AbstractField.extend({
1331     init: function(parent, field, $node) {
1332         this._super.apply(this, arguments);
1333         this.name = $node.attr('name')
1334         this.parent = parent;
1335     },
1336     prepare_dropdown_selection: function() {
1337         var data = [];
1338         _.map(this.field.selection || [], function(res) {
1339             var value = {
1340                 'name': res[0],
1341                 'tooltip': res[1],
1342                 'state_name': res[1],
1343             }
1344             if (res[0] == 'normal') { value['state_class'] = 'oe_kanban_status'; }
1345             else if (res[0] == 'done') { value['state_class'] = 'oe_kanban_status oe_kanban_status_green'; }
1346             else { value['state_class'] = 'oe_kanban_status oe_kanban_status_red'; }
1347             data.push(value);
1348         });
1349         return data;
1350     },
1351     renderElement: function() {
1352         var self = this;
1353         this.record_id = self.parent.id;
1354         this.states = self.prepare_dropdown_selection();;
1355         this.$el = $(QWeb.render("KanbanSelection", {'widget': self}));
1356         this.$el.find('li').click(self.do_action.bind(self));
1357     },
1358     do_action: function(e) {
1359         var self = this;
1360         var li = $(e.target).closest( "li" );
1361         if (li.length) {
1362             var value = {};
1363             value[self.name] = String(li.data('value'));
1364             return self.parent.view.dataset._model.call('write', [[self.record_id], value, self.parent.view.dataset.get_context()]).done(self.reload_record.bind(self.parent));
1365         }
1366     },
1367     reload_record: function() {
1368         this.do_reload();
1369     },
1370 });
1371
1372 instance.web_kanban.fields_registry = new instance.web.Registry({});
1373 instance.web_kanban.fields_registry.add('priority','instance.web_kanban.Priority');
1374 instance.web_kanban.fields_registry.add('kanban_state_selection','instance.web_kanban.KanbanSelection');
1375 };
1376
1377 // vim:et fdc=0 fdl=0 foldnestmax=3 fdm=syntax: