1 openerp.web_kanban = function (instance) {
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');
8 instance.web_kanban.KanbanView = instance.web.View.extend({
9 template: "KanbanView",
10 display_name: _lt('Kanban'),
11 default_nr_columns: 1,
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);
18 _.defaults(this.options, {
19 "quick_creatable": true,
21 "create_text": undefined,
22 "read_only_mode": false,
23 "confirm_on_delete": true,
25 this.fields_view = {};
26 this.fields_keys = [];
28 this.group_by_field = {};
29 this.grouped_by_m2o = false;
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();
47 view_loading: function(r) {
48 return this.load_kanban(r);
52 this._super.apply(this, arguments);
53 this.$el.on('click', '.oe_kanban_dummy_cell', function() {
55 self.$buttons.find('.oe_kanban_add_column').openerpBounce();
60 this._super.apply(this, arguments);
61 $('html').off('click.kanban');
63 load_kanban: function(data) {
64 this.fields_view = data;
66 // use default order if defined in xml description
67 var default_order = this.fields_view.arch.attrs.default_order,
68 unsorted = !this.dataset._sort.length;
69 if (unsorted && default_order) {
70 this.dataset.set_sort(default_order.split(','));
73 this.$el.addClass(this.fields_view.arch.attrs['class']);
74 this.$buttons = $(QWeb.render("KanbanView.buttons", {'widget': this}));
75 if (this.options.$buttons) {
76 this.$buttons.appendTo(this.options.$buttons);
78 this.$el.find('.oe_kanban_buttons').replaceWith(this.$buttons);
81 .on('click', 'button.oe_kanban_button_new', this.do_add_record)
82 .on('click', '.oe_kanban_add_column', this.do_add_group);
83 this.$groups = this.$el.find('.oe_kanban_groups tr');
84 this.fields_keys = _.keys(this.fields_view.fields);
85 this.add_qweb_template();
86 this.has_been_loaded.resolve();
87 this.trigger('kanban_view_loaded', data);
90 _is_quick_create_enabled: function() {
91 if (!this.options.quick_creatable || !this.is_action_enabled('create'))
93 if (this.fields_view.arch.attrs.quick_create !== undefined)
94 return JSON.parse(this.fields_view.arch.attrs.quick_create);
95 return !! this.group_by;
97 is_action_enabled: function(action) {
98 if (action === 'create' && !this.options.creatable)
100 return this._super(action);
103 * select the nodes into the xml and send to extract_aggregates the nodes with TagName="field"
105 add_qweb_template: function() {
106 for (var i=0, ii=this.fields_view.arch.children.length; i < ii; i++) {
107 var child = this.fields_view.arch.children[i];
108 if (child.tag === "templates") {
109 this.transform_qweb_template(child);
110 this.qweb.add_template(instance.web.json_node_to_xml(child));
112 } else if (child.tag === 'field') {
113 this.extract_aggregates(child);
117 /* extract_aggregates
118 * extract the agggregates from the nodes (TagName="field")
120 extract_aggregates: function(node) {
121 for (var j = 0, jj = this.group_operators.length; j < jj; j++) {
122 if (node.attrs[this.group_operators[j]]) {
123 this.aggregates[node.attrs.name] = node.attrs[this.group_operators[j]];
128 transform_qweb_template: function(node) {
129 var qweb_add_if = function(node, condition) {
130 if (node.attrs[QWeb.prefix + '-if']) {
131 condition = _.str.sprintf("(%s) and (%s)", node.attrs[QWeb.prefix + '-if'], condition);
133 node.attrs[QWeb.prefix + '-if'] = condition;
136 if (node.tag && node.attrs.modifiers) {
137 var modifiers = JSON.parse(node.attrs.modifiers || '{}');
138 if (modifiers.invisible) {
139 qweb_add_if(node, _.str.sprintf("!kanban_compute_domain(%s)", JSON.stringify(modifiers.invisible)));
144 var ftype = this.fields_view.fields[node.attrs.name].type;
145 ftype = node.attrs.widget ? node.attrs.widget : ftype;
146 if (ftype === 'many2many') {
147 if (_.indexOf(this.many2manys, node.attrs.name) < 0) {
148 this.many2manys.push(node.attrs.name);
151 node.attrs['class'] = (node.attrs['class'] || '') + ' oe_form_field oe_tags';
152 } else if (instance.web_kanban.fields_registry.contains(ftype)) {
153 // do nothing, the kanban record will handle it
155 node.tag = QWeb.prefix;
156 node.attrs[QWeb.prefix + '-esc'] = 'record.' + node.attrs['name'] + '.value';
161 var type = node.attrs.type || '';
162 if (_.indexOf('action,object,edit,open,delete'.split(','), type) !== -1) {
163 _.each(node.attrs, function(v, k) {
164 if (_.indexOf('icon,type,name,args,string,context,states,kanban_states'.split(','), k) != -1) {
165 node.attrs['data-' + k] = v;
166 delete(node.attrs[k]);
169 if (node.attrs['data-string']) {
170 node.attrs.title = node.attrs['data-string'];
172 if (node.attrs['data-icon']) {
176 src: instance.session.prefix + '/web/static/src/img/icons/' + node.attrs['data-icon'] + '.png',
182 if (node.tag == 'a') {
183 node.attrs.href = '#';
185 node.attrs.type = 'button';
187 node.attrs['class'] = (node.attrs['class'] || '') + ' oe_kanban_action oe_kanban_action_' + node.tag;
192 for (var i = 0, ii = node.children.length; i < ii; i++) {
193 this.transform_qweb_template(node.children[i]);
197 do_add_record: function() {
198 this.dataset.index = null;
199 this.do_switch_view('form');
201 do_add_group: function() {
204 name: _t("Add column"),
205 res_model: self.group_by_field.relation,
206 views: [[false, 'form']],
207 type: 'ir.actions.act_window',
209 context: self.dataset.get_context(),
211 action_buttons: true,
214 var am = instance.webclient.action_manager;
215 var form = am.dialog_widget.views.form.controller;
216 form.on("on_button_cancel", am.dialog, am.dialog.close);
217 form.on('record_created', self, function(r) {
218 (new instance.web.DataSet(self, self.group_by_field.relation)).name_get([r]).done(function(new_record) {
220 var domain = self.dataset.domain.slice(0);
221 domain.push([self.group_by, '=', new_record[0][0]]);
222 var dataset = new instance.web.DataSetSearch(self, self.dataset.model, self.dataset.get_context(), domain);
227 value: new_record[0],
231 var new_group = new instance.web_kanban.KanbanGroup(self, [], datagroup, dataset);
232 self.do_add_groups([new_group]).done(function() {
233 $(window).scrollTo(self.groups.slice(-1)[0].$el, { axis: 'x' });
238 do_search: function(domain, context, group_by) {
240 this.search_domain = domain;
241 this.search_context = context;
242 this.search_group_by = group_by;
243 return $.when(this.has_been_loaded).then(function() {
244 self.group_by = group_by.length ? group_by[0] : self.fields_view.arch.attrs.default_group_by;
245 self.group_by_field = self.fields_view.fields[self.group_by] || {};
246 self.grouped_by_m2o = (self.group_by_field.type === 'many2one');
247 self.$buttons.find('.oe_alternative').toggle(self.grouped_by_m2o);
248 self.$el.toggleClass('oe_kanban_grouped_by_m2o', self.grouped_by_m2o);
249 var grouping_fields = self.group_by ? [self.group_by].concat(_.keys(self.aggregates)) : undefined;
250 if (!_.isEmpty(grouping_fields)) {
251 // ensure group_by fields are read.
252 self.fields_keys = _.unique(self.fields_keys.concat(grouping_fields));
254 var grouping = new instance.web.Model(self.dataset.model, context, domain).query(self.fields_keys).group_by(grouping_fields);
255 return self.alive($.when(grouping)).done(function(groups) {
256 self.remove_no_result();
258 self.do_process_groups(groups);
260 self.do_process_dataset();
265 do_process_groups: function(groups) {
267 this.$el.find('table:first').show();
268 this.$el.removeClass('oe_kanban_ungrouped').addClass('oe_kanban_grouped');
269 this.add_group_mutex.exec(function() {
270 self.do_clear_groups();
271 self.dataset.ids = [];
272 if (!groups.length) {
277 var remaining = groups.length - 1,
279 return $.when.apply(null, _.map(groups, function (group, index) {
280 var def = $.when([]);
281 var dataset = new instance.web.DataSetSearch(self, self.dataset.model,
282 new instance.web.CompoundContext(self.dataset.get_context(), group.model.context()), group.model.domain());
283 if (group.attributes.length >= 1) {
284 def = dataset.read_slice(self.fields_keys.concat(['__last_update']), { 'limit': self.limit });
286 return def.then(function(records) {
287 self.nb_records += records.length;
288 self.dataset.ids.push.apply(self.dataset.ids, dataset.ids);
289 groups_array[index] = new instance.web_kanban.KanbanGroup(self, records, group, dataset);
291 self.dataset.index = self.dataset.size() ? 0 : null;
292 return self.do_add_groups(groups_array);
295 })).then(function () {
296 if(!self.nb_records) {
302 do_process_dataset: function() {
304 this.$el.find('table:first').show();
305 this.$el.removeClass('oe_kanban_grouped').addClass('oe_kanban_ungrouped');
306 this.add_group_mutex.exec(function() {
307 var def = $.Deferred();
308 self.do_clear_groups();
309 self.dataset.read_slice(self.fields_keys.concat(['__last_update']), { 'limit': self.limit }).done(function(records) {
310 var kgroup = new instance.web_kanban.KanbanGroup(self, records, null, self.dataset);
311 self.do_add_groups([kgroup]).done(function() {
312 if (_.isEmpty(records)) {
317 }).done(null, function() {
323 do_reload: function() {
324 this.do_search(this.search_domain, this.search_context, this.search_group_by);
326 do_clear_groups: function() {
327 var groups = this.groups.slice(0);
329 _.each(groups, function(group) {
333 do_add_groups: function(groups) {
335 var $parent = this.$el.parent();
337 _.each(groups, function(group) {
338 self.groups[group.undefined_title ? 'unshift' : 'push'](group);
340 var $last_td = self.$el.find('.oe_kanban_groups_headers td:last');
341 var groups_started = _.map(this.groups, function(group) {
342 if (!group.is_started) {
343 group.on("add_record", self, function () {
344 self.remove_no_result();
346 return group.insertBefore($last_td);
349 return $.when.apply(null, groups_started).done(function () {
350 self.on_groups_started();
351 self.$el.appendTo($parent);
352 _.each(self.groups, function(group) {
353 group.compute_cards_auto_height();
357 on_groups_started: function() {
359 if (this.group_by || this.fields_keys.indexOf("sequence") !== -1) {
360 // Kanban cards drag'n'drop
361 var prev_widget, is_folded, record, $columns;
363 $columns = this.$el.find('.oe_kanban_column .oe_kanban_column_cards, .oe_kanban_column .oe_kanban_folded_column_cards');
365 $columns = this.$el.find('.oe_kanban_column_cards');
368 handle : '.oe_kanban_draghandle',
369 start: function(event, ui) {
370 self.currently_dragging.index = ui.item.parent().children('.oe_kanban_record').index(ui.item);
371 self.currently_dragging.group = prev_widget = ui.item.parents('.oe_kanban_column:first').data('widget');
372 ui.item.find('*').on('click.prevent', function(ev) {
375 record = ui.item.data('widget');
376 record.$el.bind('mouseup',function(ev,ui){
380 record.$el.unbind('mouseup');
382 ui.placeholder.height(ui.item.height());
384 over: function(event, ui) {
385 var parent = $(event.target).parent();
386 prev_widget.highlight(false);
387 is_folded = parent.hasClass('oe_kanban_group_folded');
389 var widget = parent.data('widget');
390 widget.highlight(true);
391 prev_widget = widget;
395 stop: function(event, ui) {
396 prev_widget.highlight(false);
397 var old_index = self.currently_dragging.index;
398 var new_index = ui.item.parent().children('.oe_kanban_record').index(ui.item);
399 var old_group = self.currently_dragging.group;
400 var new_group = ui.item.parents('.oe_kanban_column:first').data('widget');
401 if (!(old_group.title === new_group.title && old_group.value === new_group.value && old_index == new_index)) {
402 self.on_record_moved(record, old_group, old_index, new_group, new_index);
404 setTimeout(function() {
405 // A bit hacky but could not find a better solution for Firefox (problem not present in chrome)
406 // http://stackoverflow.com/questions/274843/preventing-javascript-click-event-with-scriptaculous-drag-and-drop
407 ui.item.find('*').off('click.prevent');
412 // Keep connectWith out of the sortable initialization for performance sake:
413 // http://www.planbox.com/blog/development/coding/jquery-ui-sortable-slow-to-bind.html
414 $columns.sortable({ connectWith: $columns });
416 // Kanban groups drag'n'drop
418 if (this.grouped_by_m2o) {
419 this.$('.oe_kanban_groups_headers').sortable({
420 items: '.oe_kanban_group_header',
425 start: function(event, ui) {
426 start_index = ui.item.index();
427 self.$('.oe_kanban_record, .oe_kanban_quick_create').css({ visibility: 'hidden' });
429 stop: function(event, ui) {
430 var stop_index = ui.item.index();
431 if (start_index !== stop_index) {
432 var $start_column = $('.oe_kanban_groups_records .oe_kanban_column').eq(start_index);
433 var $stop_column = $('.oe_kanban_groups_records .oe_kanban_column').eq(stop_index);
434 var method = (start_index > stop_index) ? 'insertBefore' : 'insertAfter';
435 $start_column[method]($stop_column);
436 var tmp_group = self.groups.splice(start_index, 1)[0];
437 self.groups.splice(stop_index, 0, tmp_group);
438 var new_sequence = _.pluck(self.groups, 'value');
439 (new instance.web.DataSet(self, self.group_by_field.relation)).resequence(new_sequence).done(function(r) {
441 console.error("Kanban: could not resequence model '%s'. Probably no 'sequence' field.", self.group_by_field.relation);
445 self.$('.oe_kanban_record, .oe_kanban_quick_create').css({ visibility: 'visible' });
450 this.$el.find('.oe_kanban_draghandle').removeClass('oe_kanban_draghandle');
452 this.postprocess_m2m_tags();
454 on_record_moved : function(record, old_group, old_index, new_group, new_index) {
456 record.$el.find('[title]').tooltip('destroy');
457 $(old_group.$el).add(new_group.$el).find('.oe_kanban_aggregates, .oe_kanban_group_length').hide();
458 if (old_group === new_group) {
459 new_group.records.splice(old_index, 1);
460 new_group.records.splice(new_index, 0, record);
461 new_group.do_save_sequences();
463 old_group.records.splice(old_index, 1);
464 new_group.records.splice(new_index, 0, record);
465 record.group = new_group;
467 data[this.group_by] = new_group.value;
468 this.dataset.write(record.id, data, {}).done(function() {
470 new_group.do_save_sequences();
471 if (new_group.state.folded) {
472 new_group.do_action_toggle_fold();
473 record.prependTo(new_group.$records.find('.oe_kanban_column_cards'));
475 }).fail(function(error, evt) {
476 evt.preventDefault();
477 alert(_t("An error has occured while moving the record to this group: ") + error.data.message);
478 self.do_reload(); // TODO: use draggable + sortable in order to cancel the dragging when the rcp fails
483 do_show: function() {
485 this.$buttons.show();
487 this.do_push_state({});
488 return this._super();
490 do_hide: function () {
492 this.$buttons.hide();
494 return this._super();
496 open_record: function(id, editable) {
497 if (this.dataset.select_id(id)) {
498 this.do_switch_view('form', null, { mode: editable ? "edit" : undefined });
500 this.do_warn("Kanban: could not find id#" + id);
503 no_result: function() {
505 if (this.groups.group_by
506 || !this.options.action
507 || (!this.options.action.help && !this.options.action.get_empty_list_help)) {
510 this.$el.css("position", "relative");
511 $(QWeb.render('KanbanView.nocontent', { content : this.options.action.get_empty_list_help || this.options.action.help})).insertBefore(this.$('table:first'));
512 this.$el.find('.oe_view_nocontent').click(function() {
513 self.$buttons.openerpBounce();
516 remove_no_result: function() {
517 this.$el.css("position", "");
518 this.$el.find('.oe_view_nocontent').remove();
522 * postprocessing of fields type many2many
523 * make the rpc request for all ids/model and insert value inside .oe_tags fields
525 postprocess_m2m_tags: function() {
527 if (!this.many2manys.length) {
531 this.groups.forEach(function(group) {
532 group.records.forEach(function(record) {
533 self.many2manys.forEach(function(name) {
534 var field = record.record[name];
535 var $el = record.$('.oe_form_field.oe_tags[name=' + name + ']').empty();
536 if (!relations[field.relation]) {
537 relations[field.relation] = { ids: [], elements: {}};
539 var rel = relations[field.relation];
540 field.raw_value.forEach(function(id) {
542 if (!rel.elements[id]) {
543 rel.elements[id] = [];
545 rel.elements[id].push($el[0]);
550 _.each(relations, function(rel, rel_name) {
551 var dataset = new instance.web.DataSetSearch(self, rel_name, self.dataset.get_context());
552 dataset.name_get(_.uniq(rel.ids)).done(function(result) {
553 result.forEach(function(nameget) {
554 $(rel.elements[nameget[0]]).append('<span class="oe_tag">' + _.str.escapeHTML(nameget[1]) + '</span>');
562 function get_class(name) {
563 return new instance.web.Registry({'tmp' : name}).get_object("tmp");
566 instance.web_kanban.KanbanGroup = instance.web.Widget.extend({
567 template: 'KanbanView.group_header',
568 init: function (parent, records, group, dataset) {
571 this.$has_been_started = $.Deferred();
574 this.dataset = dataset;
575 this.dataset_offset = 0;
576 this.aggregates = {};
577 this.value = this.title = null;
579 this.value = group.get('value');
580 this.title = group.get('value');
581 if (this.value instanceof Array) {
582 this.title = this.value[1];
583 this.value = this.value[0];
585 var field = this.view.group_by_field;
586 if (!_.isEmpty(field)) {
588 this.title = instance.web.format_value(group.get('value'), field, false);
591 _.each(this.view.aggregates, function(value, key) {
592 self.aggregates[value] = instance.web.format_value(group.get('aggregates')[key], {type: 'float'});
596 if (this.title === false) {
597 this.title = _t('Undefined');
598 this.undefined_title = true;
600 var key = this.view.group_by + '-' + this.value;
601 if (!this.view.state.groups[key]) {
602 this.view.state.groups[key] = {
603 folded: group ? group.get('folded') : false
606 this.state = this.view.state.groups[key];
607 this.$records = null;
610 this.$has_been_started.done(function() {
611 self.do_add_records(records);
616 if (! self.view.group_by) {
617 self.$el.addClass("oe_kanban_no_group");
618 self.quick = new (get_class(self.view.quick_create_class))(this, self.dataset, {}, false)
619 .on('added', self, self.proxy('quick_created'));
620 self.quick.replace($(".oe_kanban_no_group_qc_placeholder"));
622 this.$records = $(QWeb.render('KanbanView.group_records_container', { widget : this}));
623 this.$records.insertBefore(this.view.$el.find('.oe_kanban_groups_records td:last'));
625 this.$el.on('click', '.oe_kanban_group_dropdown li a', function(ev) {
626 var fn = 'do_action_' + $(ev.target).data().action;
627 if (typeof(self[fn]) === 'function') {
628 self[fn]($(ev.target));
632 this.$el.find('.oe_kanban_add').click(function () {
633 if (self.view.quick) {
634 self.view.quick.trigger('close');
639 self.view.$el.find('.oe_view_nocontent').hide();
641 ctx['default_' + self.view.group_by] = self.value;
642 self.quick = new (get_class(self.view.quick_create_class))(this, self.dataset, ctx, true)
643 .on('added', self, self.proxy('quick_created'))
644 .on('close', self, function() {
645 self.view.$el.find('.oe_view_nocontent').show();
646 this.quick.destroy();
647 delete self.view.quick;
650 self.quick.appendTo($(".oe_kanban_group_list_header", self.$records));
652 self.view.quick = self.quick;
654 // Add bounce effect on image '+' of kanban header when click on empty space of kanban grouped column.
655 this.$records.on('click', '.oe_kanban_show_more', this.do_show_more);
656 if (this.state.folded) {
657 this.do_toggle_fold();
659 this.$el.data('widget', this);
660 this.$records.data('widget', this);
661 this.$has_been_started.resolve();
662 var add_btn = this.$el.find('.oe_kanban_add');
663 add_btn.tooltip({delay: { show: 500, hide:1000 }});
664 this.$records.find(".oe_kanban_column_cards").click(function (ev) {
665 if (ev.target == ev.currentTarget) {
666 if (!self.state.folded) {
667 add_btn.openerpBounce();
671 this.is_started = true;
672 var def_tooltip = this.fetch_tooltip();
673 return $.when(def_tooltip);
675 fetch_tooltip: function() {
678 var field_name = this.view.group_by;
679 var field = this.view.group_by_field;
680 var field_desc = null;
681 var recurse = function(node) {
682 if (node.tag === "field" && node.attrs.name === field_name) {
686 _.each(node.children, function(child) {
687 if (field_desc === null)
691 recurse(this.view.fields_view.arch);
694 var options = instance.web.py_eval(field_desc.attrs.options || '{}')
695 if (! options.tooltip_on_group_by)
700 return (new instance.web.Model(field.relation)).query([options.tooltip_on_group_by])
701 .filter([["id", "=", this.value]]).first().then(function(res) {
702 self.tooltip = res[options.tooltip_on_group_by];
703 self.$(".oe_kanban_group_title_text").attr("title", self.tooltip || self.title || "").tooltip();
707 compute_cards_auto_height: function() {
708 // oe_kanban_no_auto_height is an empty class used to disable this feature
709 if (!this.view.group_by) {
712 _.each(this.records, function(r) {
713 var $e = r.$el.children(':first:not(.oe_kanban_no_auto_height)').css('min-height', 0);
716 min_height = Math.max(min_height, $e.outerHeight());
719 $(els).css('min-height', min_height);
722 destroy: function() {
725 this.$records.remove();
728 do_show_more: function(evt) {
730 var ids = self.view.dataset.ids.splice(0);
731 return this.dataset.read_slice(this.view.fields_keys.concat(['__last_update']), {
732 'limit': self.view.limit,
733 'offset': self.dataset_offset += self.view.limit
734 }).then(function(records) {
735 self.view.dataset.ids = ids.concat(self.dataset.ids);
736 self.do_add_records(records);
737 self.compute_cards_auto_height();
738 self.view.postprocess_m2m_tags();
742 do_add_records: function(records, prepend) {
744 var $list_header = this.$records.find('.oe_kanban_group_list_header');
745 var $show_more = this.$records.find('.oe_kanban_show_more');
746 var $cards = this.$records.find('.oe_kanban_column_cards');
748 _.each(records, function(record) {
749 var rec = new instance.web_kanban.KanbanRecord(self, record);
751 rec.appendTo($cards);
752 self.records.push(rec);
754 rec.prependTo($cards);
755 self.records.unshift(rec);
758 if ($show_more.length) {
759 var size = this.dataset.size();
760 $show_more.toggle(this.records.length < size).find('.oe_kanban_remaining').text(size - this.records.length);
763 remove_record: function(id, remove_from_dataset) {
764 for (var i = 0; i < this.records.length; i++) {
765 if (this.records[i]['id'] === id) {
766 this.records.splice(i, 1);
771 do_toggle_fold: function(compute_width) {
772 this.$el.add(this.$records).toggleClass('oe_kanban_group_folded');
773 this.state.folded = this.$el.is('.oe_kanban_group_folded');
774 this.$("ul.oe_kanban_group_dropdown li a[data-action=toggle_fold]").text((this.state.folded) ? _t("Unfold") : _t("Fold"));
776 do_action_toggle_fold: function() {
777 this.do_toggle_fold();
779 do_action_edit: function() {
783 name: _t("Edit column"),
784 res_model: self.view.group_by_field.relation,
785 views: [[false, 'form']],
786 type: 'ir.actions.act_window',
789 action_buttons: true,
792 var am = instance.webclient.action_manager;
793 var form = am.dialog_widget.views.form.controller;
794 form.on("on_button_cancel", am.dialog, function() { return am.dialog.$dialog_box.modal('hide'); });
795 form.on('record_saved', self, function() {
796 am.dialog.$dialog_box.modal('hide');
797 self.view.do_reload();
800 do_action_delete: function() {
802 if (confirm(_t("Are you sure to remove this column ?"))) {
803 (new instance.web.DataSet(self, self.view.group_by_field.relation)).unlink([self.value]).done(function(r) {
804 self.view.do_reload();
808 do_save_sequences: function() {
810 if (_.indexOf(this.view.fields_keys, 'sequence') > -1) {
811 var new_sequence = _.pluck(this.records, 'id');
812 self.view.dataset.resequence(new_sequence);
816 * Handles a newly created record
818 * @param {id} id of the newly created record
820 quick_created: function (record) {
821 var id = record, self = this;
822 self.view.remove_no_result();
823 self.trigger("add_record");
824 this.dataset.read_ids([id], this.view.fields_keys)
825 .done(function (records) {
826 self.view.dataset.ids.push(id);
827 self.do_add_records(records, true);
830 highlight: function(show){
832 this.$el.addClass('oe_kanban_column_higlight');
833 this.$records.addClass('oe_kanban_column_higlight');
835 this.$el.removeClass('oe_kanban_column_higlight');
836 this.$records.removeClass('oe_kanban_column_higlight');
841 instance.web_kanban.KanbanRecord = instance.web.Widget.extend({
842 template: 'KanbanView.record',
843 init: function (parent, record) {
846 this.view = parent.view;
848 this.set_record(record);
849 if (!this.view.state.records[this.id]) {
850 this.view.state.records[this.id] = {
854 this.state = this.view.state.records[this.id];
857 set_record: function(record) {
861 _.each(record, function(v, k) {
866 this.record = this.transform_record(record);
873 init_content: function() {
875 self.sub_widgets = [];
876 this.$("[data-field_id]").each(function() {
877 self.add_widget($(this));
879 this.$el.data('widget', this);
882 transform_record: function(record) {
885 _.each(record, function(value, name) {
886 var r = _.clone(self.view.fields_view.fields[name] || {});
887 if ((r.type === 'date' || r.type === 'datetime') && value) {
888 r.raw_value = instance.web.auto_str_to_date(value);
892 r.value = instance.web.format_value(value, r);
893 new_record[name] = r;
897 renderElement: function() {
898 this.qweb_context = {
902 read_only_mode: this.view.options.read_only_mode,
904 for (var p in this) {
905 if (_.str.startsWith(p, 'kanban_')) {
906 this.qweb_context[p] = _.bind(this[p], this);
909 var $el = instance.web.qweb.render(this.template, {
911 'content': this.view.qweb.render('kanban-box', this.qweb_context)
913 this.replaceElement($el);
914 this.replace_fields();
916 replace_fields: function() {
918 this.$("field").each(function() {
919 var $field = $(this);
920 var $nfield = $("<span></span");
921 var id = _.uniqueId("kanbanfield");
922 self.fields[id] = $field;
923 $nfield.attr("data-field_id", id);
924 $field.replaceWith($nfield);
927 add_widget: function($node) {
928 var $orig = this.fields[$node.data("field_id")];
929 var field = this.record[$orig.attr("name")];
930 var type = field.type;
931 type = $orig.attr("widget") ? $orig.attr("widget") : type;
932 var obj = instance.web_kanban.fields_registry.get_object(type);
933 var widget = new obj(this, field, $orig);
934 this.sub_widgets.push(widget);
935 widget.replace($node);
937 bind_events: function() {
939 this.setup_color_picker();
940 this.$el.find('[title]').each(function(){
942 delay: { show: 500, hide: 0},
945 var template = $(this).attr('tooltip');
946 if (!self.view.qweb.has_template(template)) {
949 return self.view.qweb.render(template, self.qweb_context);
954 // If no draghandle is found, make the whole card as draghandle (provided one can edit)
955 if (!this.$el.find('.oe_kanban_draghandle').length) {
956 this.$el.children(':first')
957 .toggleClass('oe_kanban_draghandle', this.view.is_action_enabled('edit'));
960 this.$el.find('.oe_kanban_action').click(function(ev) {
962 var $action = $(this),
963 type = $action.data('type') || 'button',
964 method = 'do_action_' + (type === 'action' ? 'object' : type);
965 if ((type === 'edit' || type === 'delete') && ! self.view.is_action_enabled(type)) {
966 self.view.open_record(self.id, true);
967 } else if (_.str.startsWith(type, 'switch_')) {
968 self.view.do_switch_view(type.substr(7));
969 } else if (typeof self[method] === 'function') {
970 self[method]($action);
972 self.do_warn("Kanban: no action for type : " + type);
976 if (this.$el.find('.oe_kanban_global_click,.oe_kanban_global_click_edit').length) {
977 this.$el.on('click', function(ev) {
978 if (!ev.isTrigger && !$._data(ev.target, 'events')) {
980 var elem = ev.target;
984 var events = $._data(elem, 'events');
985 if (elem == ev.currentTarget) {
990 if (events && events.click) {
991 // do not trigger global click if one child has a click event registered
995 if (trigger && events && events.click) {
996 _.each(events.click, function(click_event) {
997 if (click_event.selector) {
998 // For each parent of original target, check if a
999 // delegated click is bound to any previously found children
1000 _.each(children, function(child) {
1001 if ($(child).is(click_event.selector)) {
1008 elem = elem.parentElement;
1011 self.on_card_clicked(ev);
1017 /* actions when user click on the block with a specific class
1018 * open on normal view : oe_kanban_global_click
1019 * open on form/edit view : oe_kanban_global_click_edit
1021 on_card_clicked: function(ev) {
1022 if(this.$el.find('.oe_kanban_global_click_edit').size()>0)
1023 this.do_action_edit();
1025 this.do_action_open();
1027 setup_color_picker: function() {
1029 var $el = this.$el.find('ul.oe_kanban_colorpicker');
1031 $el.html(QWeb.render('KanbanColorPicker', {
1034 $el.on('click', 'a', function(ev) {
1035 ev.preventDefault();
1036 var color_field = $(this).parents('.oe_kanban_colorpicker').first().data('field') || 'color';
1038 data[color_field] = $(this).data('color');
1039 self.view.dataset.write(self.id, data, {}).done(function() {
1040 self.record[color_field] = $(this).data('color');
1046 do_action_delete: function($action) {
1049 return $.when(self.view.dataset.unlink([self.id])).done(function() {
1050 self.group.remove_record(self.id);
1054 if (this.view.options.confirm_on_delete) {
1055 if (confirm(_t("Are you sure you want to delete this record ?"))) {
1061 do_action_edit: function($action) {
1062 this.view.open_record(this.id, true);
1064 do_action_open: function($action) {
1065 this.view.open_record(this.id);
1067 do_action_object: function ($action) {
1068 var button_attrs = $action.data();
1069 this.view.do_execute_action(button_attrs, this.view.dataset, this.id, this.do_reload);
1071 do_reload: function() {
1073 this.view.dataset.read_ids([this.id], this.view.fields_keys.concat(['__last_update'])).done(function(records) {
1074 _.each(self.sub_widgets, function(el) {
1077 self.sub_widgets = [];
1078 if (records.length) {
1079 self.set_record(records[0]);
1080 self.renderElement();
1081 self.init_content();
1082 self.group.compute_cards_auto_height();
1083 self.view.postprocess_m2m_tags();
1089 kanban_getcolor: function(variable) {
1091 switch (typeof(variable)) {
1093 for (var i=0, ii=variable.length; i<ii; i++) {
1094 index += variable.charCodeAt(i);
1098 index = Math.round(variable);
1103 var color = (index % this.view.number_of_color_schemes);
1106 kanban_color: function(variable) {
1107 var color = this.kanban_getcolor(variable);
1108 return color === '' ? '' : 'oe_kanban_color_' + color;
1110 kanban_image: function(model, field, id, cache, options) {
1111 options = options || {};
1113 if (this.record[field] && this.record[field].value && !instance.web.form.is_bin_size(this.record[field].value)) {
1114 url = 'data:image/png;base64,' + this.record[field].value;
1115 } else if (this.record[field] && ! this.record[field].value) {
1116 url = "/web/static/src/img/placeholder.png";
1118 id = JSON.stringify(id);
1119 if (options.preview_image)
1120 field = options.preview_image;
1121 url = this.session.url('/web/binary/image', {model: model, field: field, id: id});
1122 if (cache !== undefined) {
1123 // Set the cache duration in seconds.
1124 url += '&cache=' + parseInt(cache, 10);
1129 kanban_text_ellipsis: function(s, size) {
1133 } else if (s.length <= size) {
1136 return s.substr(0, size) + '...';
1139 kanban_compute_domain: function(domain) {
1140 return instance.web.form.compute_domain(domain, this.values);
1145 * Quick creation view.
1147 * Triggers a single event "added" with a single parameter "name", which is the
1148 * name entered by the user
1153 instance.web_kanban.QuickCreate = instance.web.Widget.extend({
1154 template: 'KanbanView.quick_create',
1157 * close_btn: If true, the widget will display a "Close" button able to trigger
1160 init: function(parent, dataset, context, buttons) {
1161 this._super(parent);
1162 this._dataset = dataset;
1163 this._buttons = buttons || false;
1164 this._context = context || {};
1166 start: function () {
1168 self.$input = this.$el.find('input');
1169 self.$input.keyup(function(event){
1170 if(event.keyCode == 13){
1174 $(".oe_kanban_quick_create").focusout(function (e) {
1175 var val = self.$el.find('input').val();
1176 if (/^\s*$/.test(val)) { self.trigger('close'); }
1177 e.stopImmediatePropagation();
1179 $(".oe_kanban_quick_create_add", this.$el).click(function () {
1183 $(".oe_kanban_quick_create_close", this.$el).click(function (ev) {
1184 ev.preventDefault();
1185 self.trigger('close');
1187 self.$input.keyup(function(e) {
1188 if (e.keyCode == 27 && self._buttons) {
1189 self.trigger('close');
1194 this.$el.find('input').focus();
1197 * Handles user event from nested quick creation view
1199 quick_add: function () {
1201 var val = this.$input.val();
1202 if (/^\s*$/.test(val)) { this.$el.remove(); return; }
1204 'name_create', [val, new instance.web.CompoundContext(
1205 this._dataset.get_context(), this._context)])
1206 .then(function(record) {
1207 self.$input.val("");
1208 self.trigger('added', record[0]);
1209 }, function(error, event) {
1210 event.preventDefault();
1211 return self.slow_create();
1214 slow_create: function() {
1216 var pop = new instance.web.form.SelectCreatePopup(this);
1218 self._dataset.model,
1220 title: _t("Create: ") + (this.string || this.name),
1221 initial_view: "form",
1222 disable_multiple_selection: true
1225 {"default_name": self.$input.val()}
1227 pop.on("elements_selected", self, function(element_ids) {
1228 self.$input.val("");
1229 self.trigger('added', element_ids[0]);
1235 * Interface to be implemented by kanban fields.
1238 instance.web_kanban.FieldInterface = {
1241 - parent: The widget's parent.
1242 - field: A dictionary giving details about the field, including the current field's value in the
1244 - $node: The field <field> tag as it appears in the view, encapsulated in a jQuery object.
1246 init: function(parent, field, $node) {},
1250 * Abstract class for classes implementing FieldInterface.
1253 * - value: useful property to hold the value of the field. By default, the constructor
1254 * sets value property.
1257 instance.web_kanban.AbstractField = instance.web.Widget.extend(instance.web_kanban.FieldInterface, {
1259 Constructor that saves the field and $node parameters and sets the "value" property.
1261 init: function(parent, field, $node) {
1262 this._super(parent);
1265 this.options = instance.web.py_eval(this.$node.attr("options") || '{}');
1266 this.set("value", field.raw_value);
1270 instance.web_kanban.Priority = instance.web_kanban.AbstractField.extend({
1271 init: function(parent, field, $node) {
1272 this._super.apply(this, arguments);
1273 this.name = $node.attr('name')
1274 this.parent = parent;
1276 prepare_priority: function() {
1278 var selection = this.field.selection || [];
1279 var init_value = selection && selection[0][0] || 0;
1280 var data = _.map(selection.slice(1), function(element, index) {
1282 'value': element[0],
1284 'click_value': element[0],
1286 if (index == 0 && self.get('value') == element[0]) {
1287 value['click_value'] = init_value;
1293 renderElement: function() {
1295 this.record_id = self.parent.id;
1296 this.priorities = self.prepare_priority();
1297 this.$el = $(QWeb.render("Priority", {'widget': this}));
1298 this.$el.find('li').click(self.do_action.bind(self));
1300 do_action: function(e) {
1302 var li = $(e.target).closest( "li" );
1305 value[self.name] = String(li.data('value'));
1306 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));
1309 reload_record: function() {
1314 instance.web_kanban.KanbanSelection = instance.web_kanban.AbstractField.extend({
1315 init: function(parent, field, $node) {
1316 this._super.apply(this, arguments);
1317 this.name = $node.attr('name')
1318 this.parent = parent;
1320 prepare_dropdown_selection: function() {
1322 _.map(this.field.selection || [], function(res) {
1326 'state_name': res[1],
1328 if (res[0] == 'normal') { value['state_class'] = 'oe_kanban_status'; }
1329 else if (res[0] == 'done') { value['state_class'] = 'oe_kanban_status oe_kanban_status_green'; }
1330 else { value['state_class'] = 'oe_kanban_status oe_kanban_status_red'; }
1335 renderElement: function() {
1337 this.record_id = self.parent.id;
1338 this.states = self.prepare_dropdown_selection();;
1339 this.$el = $(QWeb.render("KanbanSelection", {'widget': self}));
1340 this.$el.find('li').click(self.do_action.bind(self));
1342 do_action: function(e) {
1344 var li = $(e.target).closest( "li" );
1347 value[self.name] = String(li.data('value'));
1348 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));
1351 reload_record: function() {
1356 instance.web_kanban.fields_registry = new instance.web.Registry({});
1357 instance.web_kanban.fields_registry.add('priority','instance.web_kanban.Priority');
1358 instance.web_kanban.fields_registry.add('kanban_state_selection','instance.web_kanban.KanbanSelection');
1361 // vim:et fdc=0 fdl=0 foldnestmax=3 fdm=syntax: