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 groups_array = [];
278 return $.when.apply(null, _.map(groups, function (group, index) {
279 var def = $.when([]);
280 var dataset = new instance.web.DataSetSearch(self, self.dataset.model,
281 new instance.web.CompoundContext(self.dataset.get_context(), group.model.context()), group.model.domain());
282 if (group.attributes.length >= 1) {
283 def = dataset.read_slice(self.fields_keys.concat(['__last_update']), { 'limit': self.limit });
285 return def.then(function(records) {
286 self.nb_records += records.length;
287 self.dataset.ids.push.apply(self.dataset.ids, dataset.ids);
288 groups_array[index] = new instance.web_kanban.KanbanGroup(self, records, group, dataset);
290 })).then(function () {
291 if(!self.nb_records) {
294 if (self.dataset.index >= self.nb_records){
295 self.dataset.index = self.dataset.size() ? 0 : null;
297 return self.do_add_groups(groups_array);
301 do_process_dataset: function() {
303 this.$el.find('table:first').show();
304 this.$el.removeClass('oe_kanban_grouped').addClass('oe_kanban_ungrouped');
305 this.add_group_mutex.exec(function() {
306 var def = $.Deferred();
307 self.do_clear_groups();
308 self.dataset.read_slice(self.fields_keys.concat(['__last_update']), { 'limit': self.limit }).done(function(records) {
309 var kgroup = new instance.web_kanban.KanbanGroup(self, records, null, self.dataset);
310 if (!_.isEmpty(self.dataset.ids) && (self.dataset.index === null || self.dataset.index >= self.dataset.ids.length)) {
311 self.dataset.index = 0;
312 } else if (_.isEmpty(self.dataset.ids)){
313 self.dataset.index = null;
315 self.do_add_groups([kgroup]).done(function() {
316 if (_.isEmpty(records)) {
321 }).done(null, function() {
327 do_reload: function() {
328 this.do_search(this.search_domain, this.search_context, this.search_group_by);
330 do_clear_groups: function() {
331 var groups = this.groups.slice(0);
333 _.each(groups, function(group) {
337 do_add_groups: function(groups) {
339 var $parent = this.$el.parent();
341 _.each(groups, function(group) {
342 self.groups[group.undefined_title ? 'unshift' : 'push'](group);
344 var $last_td = self.$el.find('.oe_kanban_groups_headers td:last');
345 var groups_started = _.map(this.groups, function(group) {
346 if (!group.is_started) {
347 group.on("add_record", self, function () {
348 self.remove_no_result();
350 return group.insertBefore($last_td);
353 return $.when.apply(null, groups_started).done(function () {
354 self.on_groups_started();
355 self.$el.appendTo($parent);
356 _.each(self.groups, function(group) {
357 group.compute_cards_auto_height();
361 on_groups_started: function() {
363 if (this.group_by || this.fields_keys.indexOf("sequence") !== -1) {
364 // Kanban cards drag'n'drop
365 var prev_widget, is_folded, record, $columns;
367 $columns = this.$el.find('.oe_kanban_column .oe_kanban_column_cards, .oe_kanban_column .oe_kanban_folded_column_cards');
369 $columns = this.$el.find('.oe_kanban_column_cards');
372 handle : '.oe_kanban_draghandle',
373 start: function(event, ui) {
374 self.currently_dragging.index = ui.item.parent().children('.oe_kanban_record').index(ui.item);
375 self.currently_dragging.group = prev_widget = ui.item.parents('.oe_kanban_column:first').data('widget');
376 ui.item.find('*').on('click.prevent', function(ev) {
379 record = ui.item.data('widget');
380 record.$el.bind('mouseup',function(ev,ui){
384 record.$el.unbind('mouseup');
386 ui.placeholder.height(ui.item.height());
388 over: function(event, ui) {
389 var parent = $(event.target).parent();
390 prev_widget.highlight(false);
391 is_folded = parent.hasClass('oe_kanban_group_folded');
393 var widget = parent.data('widget');
394 widget.highlight(true);
395 prev_widget = widget;
399 stop: function(event, ui) {
400 prev_widget.highlight(false);
401 var old_index = self.currently_dragging.index;
402 var new_index = ui.item.parent().children('.oe_kanban_record').index(ui.item);
403 var old_group = self.currently_dragging.group;
404 var new_group = ui.item.parents('.oe_kanban_column:first').data('widget');
405 if (!(old_group.title === new_group.title && old_group.value === new_group.value && old_index == new_index)) {
406 self.on_record_moved(record, old_group, old_index, new_group, new_index);
408 setTimeout(function() {
409 // A bit hacky but could not find a better solution for Firefox (problem not present in chrome)
410 // http://stackoverflow.com/questions/274843/preventing-javascript-click-event-with-scriptaculous-drag-and-drop
411 ui.item.find('*').off('click.prevent');
416 // Keep connectWith out of the sortable initialization for performance sake:
417 // http://www.planbox.com/blog/development/coding/jquery-ui-sortable-slow-to-bind.html
418 $columns.sortable({ connectWith: $columns });
420 // Kanban groups drag'n'drop
422 if (this.grouped_by_m2o) {
423 this.$('.oe_kanban_groups_headers').sortable({
424 items: '.oe_kanban_group_header',
429 start: function(event, ui) {
430 start_index = ui.item.index();
431 self.$('.oe_kanban_record, .oe_kanban_quick_create').css({ visibility: 'hidden' });
433 stop: function(event, ui) {
434 var stop_index = ui.item.index();
435 if (start_index !== stop_index) {
436 var $start_column = self.$('.oe_kanban_groups_records .oe_kanban_column').eq(start_index);
437 var $stop_column = self.$('.oe_kanban_groups_records .oe_kanban_column').eq(stop_index);
438 var method = (start_index > stop_index) ? 'insertBefore' : 'insertAfter';
439 $start_column[method]($stop_column);
440 var tmp_group = self.groups.splice(start_index, 1)[0];
441 self.groups.splice(stop_index, 0, tmp_group);
442 var new_sequence = _.pluck(self.groups, 'value');
443 (new instance.web.DataSet(self, self.group_by_field.relation)).resequence(new_sequence).done(function(r) {
445 console.error("Kanban: could not resequence model '%s'. Probably no 'sequence' field.", self.group_by_field.relation);
449 self.$('.oe_kanban_record, .oe_kanban_quick_create').css({ visibility: 'visible' });
454 this.$el.find('.oe_kanban_draghandle').removeClass('oe_kanban_draghandle');
456 this.postprocess_m2m_tags();
458 on_record_moved : function(record, old_group, old_index, new_group, new_index) {
460 record.$el.find('[title]').tooltip('destroy');
461 $(old_group.$el).add(new_group.$el).find('.oe_kanban_aggregates, .oe_kanban_group_length').hide();
462 if (old_group === new_group) {
463 new_group.records.splice(old_index, 1);
464 new_group.records.splice(new_index, 0, record);
465 new_group.do_save_sequences();
467 old_group.records.splice(old_index, 1);
468 new_group.records.splice(new_index, 0, record);
469 record.group = new_group;
471 data[this.group_by] = new_group.value;
472 this.dataset.write(record.id, data, {}).done(function() {
474 new_group.do_save_sequences();
475 if (new_group.state.folded) {
476 new_group.do_action_toggle_fold();
477 record.prependTo(new_group.$records.find('.oe_kanban_column_cards'));
479 }).fail(function(error, evt) {
480 evt.preventDefault();
481 alert(_t("An error has occured while moving the record to this group: ") + error.data.message);
482 self.do_reload(); // TODO: use draggable + sortable in order to cancel the dragging when the rcp fails
487 do_show: function() {
489 this.$buttons.show();
491 this.do_push_state({});
492 return this._super();
494 do_hide: function () {
496 this.$buttons.hide();
498 return this._super();
500 open_record: function(id, editable) {
501 if (this.dataset.select_id(id)) {
502 this.do_switch_view('form', null, { mode: editable ? "edit" : undefined });
504 this.do_warn("Kanban: could not find id#" + id);
507 no_result: function() {
509 if (this.groups.group_by
510 || !this.options.action
511 || (!this.options.action.help && !this.options.action.get_empty_list_help)) {
514 this.$el.css("position", "relative");
515 $(QWeb.render('KanbanView.nocontent', { content : this.options.action.get_empty_list_help || this.options.action.help})).insertBefore(this.$('table:first'));
516 this.$el.find('.oe_view_nocontent').click(function() {
517 self.$buttons.openerpBounce();
520 remove_no_result: function() {
521 this.$el.css("position", "");
522 this.$el.find('.oe_view_nocontent').remove();
526 * postprocessing of fields type many2many
527 * make the rpc request for all ids/model and insert value inside .oe_tags fields
529 postprocess_m2m_tags: function() {
531 if (!this.many2manys.length) {
535 this.groups.forEach(function(group) {
536 group.records.forEach(function(record) {
537 self.many2manys.forEach(function(name) {
538 var field = record.record[name];
539 var $el = record.$('.oe_form_field.oe_tags[name=' + name + ']').empty();
540 if (!relations[field.relation]) {
541 relations[field.relation] = { ids: [], elements: {}};
543 var rel = relations[field.relation];
544 field.raw_value.forEach(function(id) {
546 if (!rel.elements[id]) {
547 rel.elements[id] = [];
549 rel.elements[id].push($el[0]);
554 _.each(relations, function(rel, rel_name) {
555 var dataset = new instance.web.DataSetSearch(self, rel_name, self.dataset.get_context());
556 dataset.name_get(_.uniq(rel.ids)).done(function(result) {
557 result.forEach(function(nameget) {
558 $(rel.elements[nameget[0]]).append('<span class="oe_tag">' + _.str.escapeHTML(nameget[1]) + '</span>');
566 function get_class(name) {
567 return new instance.web.Registry({'tmp' : name}).get_object("tmp");
570 instance.web_kanban.KanbanGroup = instance.web.Widget.extend({
571 template: 'KanbanView.group_header',
572 init: function (parent, records, group, dataset) {
575 this.$has_been_started = $.Deferred();
578 this.dataset = dataset;
579 this.dataset_offset = 0;
580 this.aggregates = {};
581 this.value = this.title = null;
583 this.value = group.get('value');
584 this.title = group.get('value');
585 if (this.value instanceof Array) {
586 this.title = this.value[1];
587 this.value = this.value[0];
589 var field = this.view.group_by_field;
590 if (!_.isEmpty(field)) {
592 this.title = instance.web.format_value(group.get('value'), field, false);
595 _.each(this.view.aggregates, function(value, key) {
596 self.aggregates[value] = instance.web.format_value(group.get('aggregates')[key], {type: 'float'});
600 if (this.title === false) {
601 this.title = _t('Undefined');
602 this.undefined_title = true;
604 var key = this.view.group_by + '-' + this.value;
605 if (!this.view.state.groups[key]) {
606 this.view.state.groups[key] = {
607 folded: group ? group.get('folded') : false
610 this.state = this.view.state.groups[key];
611 this.$records = null;
614 this.$has_been_started.done(function() {
615 self.do_add_records(records);
620 if (! self.view.group_by) {
621 self.$el.addClass("oe_kanban_no_group");
622 self.quick = new (get_class(self.view.quick_create_class))(this, self.dataset, {}, false)
623 .on('added', self, self.proxy('quick_created'));
624 self.quick.replace($(".oe_kanban_no_group_qc_placeholder"));
626 this.$records = $(QWeb.render('KanbanView.group_records_container', { widget : this}));
627 this.$records.insertBefore(this.view.$el.find('.oe_kanban_groups_records td:last'));
629 this.$el.on('click', '.oe_kanban_group_dropdown li a', function(ev) {
630 var fn = 'do_action_' + $(ev.target).data().action;
631 if (typeof(self[fn]) === 'function') {
632 self[fn]($(ev.target));
636 this.$el.find('.oe_kanban_add').click(function () {
637 if (self.view.quick) {
638 self.view.quick.trigger('close');
643 self.view.$el.find('.oe_view_nocontent').hide();
645 ctx['default_' + self.view.group_by] = self.value;
646 self.quick = new (get_class(self.view.quick_create_class))(this, self.dataset, ctx, true)
647 .on('added', self, self.proxy('quick_created'))
648 .on('close', self, function() {
649 self.view.$el.find('.oe_view_nocontent').show();
650 this.quick.destroy();
651 delete self.view.quick;
654 self.quick.appendTo($(".oe_kanban_group_list_header", self.$records));
656 self.view.quick = self.quick;
658 // Add bounce effect on image '+' of kanban header when click on empty space of kanban grouped column.
659 this.$records.on('click', '.oe_kanban_show_more', this.do_show_more);
660 if (this.state.folded) {
661 this.do_toggle_fold();
663 this.$el.data('widget', this);
664 this.$records.data('widget', this);
665 this.$has_been_started.resolve();
666 var add_btn = this.$el.find('.oe_kanban_add');
667 add_btn.tooltip({delay: { show: 500, hide:1000 }});
668 this.$records.find(".oe_kanban_column_cards").click(function (ev) {
669 if (ev.target == ev.currentTarget) {
670 if (!self.state.folded) {
671 add_btn.openerpBounce();
675 this.is_started = true;
676 var def_tooltip = this.fetch_tooltip();
677 return $.when(def_tooltip);
679 fetch_tooltip: function() {
682 var field_name = this.view.group_by;
683 var field = this.view.group_by_field;
684 var field_desc = null;
685 var recurse = function(node) {
686 if (node.tag === "field" && node.attrs.name === field_name) {
690 _.each(node.children, function(child) {
691 if (field_desc === null)
695 recurse(this.view.fields_view.arch);
698 var options = instance.web.py_eval(field_desc.attrs.options || '{}')
699 if (! options.tooltip_on_group_by)
704 return (new instance.web.Model(field.relation)).query([options.tooltip_on_group_by])
705 .filter([["id", "=", this.value]]).first().then(function(res) {
706 self.tooltip = res[options.tooltip_on_group_by];
707 self.$(".oe_kanban_group_title_text").attr("title", self.tooltip || self.title || "").tooltip();
711 compute_cards_auto_height: function() {
712 // oe_kanban_no_auto_height is an empty class used to disable this feature
713 if (!this.view.group_by) {
716 _.each(this.records, function(r) {
717 var $e = r.$el.children(':first:not(.oe_kanban_no_auto_height)').css('min-height', 0);
720 min_height = Math.max(min_height, $e.outerHeight());
723 $(els).css('min-height', min_height);
726 destroy: function() {
729 this.$records.remove();
732 do_show_more: function(evt) {
734 var ids = self.view.dataset.ids.splice(0);
735 return this.dataset.read_slice(this.view.fields_keys.concat(['__last_update']), {
736 'limit': self.view.limit,
737 'offset': self.dataset_offset += self.view.limit
738 }).then(function(records) {
739 self.view.dataset.ids = ids.concat(self.dataset.ids);
740 self.do_add_records(records);
741 self.compute_cards_auto_height();
742 self.view.postprocess_m2m_tags();
746 do_add_records: function(records, prepend) {
748 var $list_header = this.$records.find('.oe_kanban_group_list_header');
749 var $show_more = this.$records.find('.oe_kanban_show_more');
750 var $cards = this.$records.find('.oe_kanban_column_cards');
752 _.each(records, function(record) {
753 var rec = new instance.web_kanban.KanbanRecord(self, record);
755 rec.appendTo($cards);
756 self.records.push(rec);
758 rec.prependTo($cards);
759 self.records.unshift(rec);
762 if ($show_more.length) {
763 var size = this.dataset.size();
764 $show_more.toggle(this.records.length < size).find('.oe_kanban_remaining').text(size - this.records.length);
767 remove_record: function(id, remove_from_dataset) {
768 for (var i = 0; i < this.records.length; i++) {
769 if (this.records[i]['id'] === id) {
770 this.records.splice(i, 1);
775 do_toggle_fold: function(compute_width) {
776 this.$el.add(this.$records).toggleClass('oe_kanban_group_folded');
777 this.state.folded = this.$el.is('.oe_kanban_group_folded');
778 this.$("ul.oe_kanban_group_dropdown li a[data-action=toggle_fold]").text((this.state.folded) ? _t("Unfold") : _t("Fold"));
780 do_action_toggle_fold: function() {
781 this.do_toggle_fold();
783 do_action_edit: function() {
787 name: _t("Edit column"),
788 res_model: self.view.group_by_field.relation,
789 views: [[false, 'form']],
790 type: 'ir.actions.act_window',
793 action_buttons: true,
796 var am = instance.webclient.action_manager;
797 var form = am.dialog_widget.views.form.controller;
798 form.on("on_button_cancel", am.dialog, function() { return am.dialog.$dialog_box.modal('hide'); });
799 form.on('record_saved', self, function() {
800 am.dialog.$dialog_box.modal('hide');
801 self.view.do_reload();
804 do_action_delete: function() {
806 if (confirm(_t("Are you sure to remove this column ?"))) {
807 (new instance.web.DataSet(self, self.view.group_by_field.relation)).unlink([self.value]).done(function(r) {
808 self.view.do_reload();
812 do_save_sequences: function() {
814 if (_.indexOf(this.view.fields_keys, 'sequence') > -1) {
815 var new_sequence = _.pluck(this.records, 'id');
816 self.view.dataset.resequence(new_sequence);
820 * Handles a newly created record
822 * @param {id} id of the newly created record
824 quick_created: function (record) {
825 var id = record, self = this;
826 self.view.remove_no_result();
827 self.trigger("add_record");
828 this.dataset.read_ids([id], this.view.fields_keys)
829 .done(function (records) {
830 self.view.dataset.ids.push(id);
831 self.do_add_records(records, true);
834 highlight: function(show){
836 this.$el.addClass('oe_kanban_column_higlight');
837 this.$records.addClass('oe_kanban_column_higlight');
839 this.$el.removeClass('oe_kanban_column_higlight');
840 this.$records.removeClass('oe_kanban_column_higlight');
845 instance.web_kanban.KanbanRecord = instance.web.Widget.extend({
846 template: 'KanbanView.record',
847 init: function (parent, record) {
850 this.view = parent.view;
852 this.set_record(record);
853 if (!this.view.state.records[this.id]) {
854 this.view.state.records[this.id] = {
858 this.state = this.view.state.records[this.id];
861 set_record: function(record) {
865 _.each(record, function(v, k) {
870 this.record = this.transform_record(record);
877 init_content: function() {
879 self.sub_widgets = [];
880 this.$("[data-field_id]").each(function() {
881 self.add_widget($(this));
883 this.$el.data('widget', this);
886 transform_record: function(record) {
889 _.each(record, function(value, name) {
890 var r = _.clone(self.view.fields_view.fields[name] || {});
891 if ((r.type === 'date' || r.type === 'datetime') && value) {
892 r.raw_value = instance.web.auto_str_to_date(value);
896 r.value = instance.web.format_value(value, r);
897 new_record[name] = r;
901 renderElement: function() {
902 this.qweb_context = {
906 read_only_mode: this.view.options.read_only_mode,
908 for (var p in this) {
909 if (_.str.startsWith(p, 'kanban_')) {
910 this.qweb_context[p] = _.bind(this[p], this);
913 var $el = instance.web.qweb.render(this.template, {
915 'content': this.view.qweb.render('kanban-box', this.qweb_context)
917 this.replaceElement($el);
918 this.replace_fields();
920 replace_fields: function() {
922 this.$("field").each(function() {
923 var $field = $(this);
924 var $nfield = $("<span></span");
925 var id = _.uniqueId("kanbanfield");
926 self.fields[id] = $field;
927 $nfield.attr("data-field_id", id);
928 $field.replaceWith($nfield);
931 add_widget: function($node) {
932 var $orig = this.fields[$node.data("field_id")];
933 var field = this.record[$orig.attr("name")];
934 var type = field.type;
935 type = $orig.attr("widget") ? $orig.attr("widget") : type;
936 var obj = instance.web_kanban.fields_registry.get_object(type);
937 var widget = new obj(this, field, $orig);
938 this.sub_widgets.push(widget);
939 widget.replace($node);
941 bind_events: function() {
943 this.setup_color_picker();
944 this.$el.find('[title]').each(function(){
946 delay: { show: 500, hide: 0},
948 var template = $(this).attr('tooltip');
949 if (!self.view.qweb.has_template(template)) {
952 return self.view.qweb.render(template, self.qweb_context);
957 // If no draghandle is found, make the whole card as draghandle (provided one can edit)
958 if (!this.$el.find('.oe_kanban_draghandle').length) {
959 this.$el.children(':first')
960 .toggleClass('oe_kanban_draghandle', this.view.is_action_enabled('edit'));
963 this.$el.find('.oe_kanban_action').click(function(ev) {
965 var $action = $(this),
966 type = $action.data('type') || 'button',
967 method = 'do_action_' + (type === 'action' ? 'object' : type);
968 if ((type === 'edit' || type === 'delete') && ! self.view.is_action_enabled(type)) {
969 self.view.open_record(self.id, true);
970 } else if (_.str.startsWith(type, 'switch_')) {
971 self.view.do_switch_view(type.substr(7));
972 } else if (typeof self[method] === 'function') {
973 self[method]($action);
975 self.do_warn("Kanban: no action for type : " + type);
979 if (this.$el.find('.oe_kanban_global_click,.oe_kanban_global_click_edit').length) {
980 this.$el.on('click', function(ev) {
981 if (!ev.isTrigger && !$._data(ev.target, 'events')) {
983 var elem = ev.target;
987 var events = $._data(elem, 'events');
988 if (elem == ev.currentTarget) {
993 if (events && events.click) {
994 // do not trigger global click if one child has a click event registered
998 if (trigger && events && events.click) {
999 _.each(events.click, function(click_event) {
1000 if (click_event.selector) {
1001 // For each parent of original target, check if a
1002 // delegated click is bound to any previously found children
1003 _.each(children, function(child) {
1004 if ($(child).is(click_event.selector)) {
1011 elem = elem.parentElement;
1014 self.on_card_clicked(ev);
1020 /* actions when user click on the block with a specific class
1021 * open on normal view : oe_kanban_global_click
1022 * open on form/edit view : oe_kanban_global_click_edit
1024 on_card_clicked: function(ev) {
1025 if(this.$el.find('.oe_kanban_global_click_edit').size()>0)
1026 this.do_action_edit();
1028 this.do_action_open();
1030 setup_color_picker: function() {
1032 var $el = this.$el.find('ul.oe_kanban_colorpicker');
1034 $el.html(QWeb.render('KanbanColorPicker', {
1037 $el.on('click', 'a', function(ev) {
1038 ev.preventDefault();
1039 var color_field = $(this).parents('.oe_kanban_colorpicker').first().data('field') || 'color';
1041 data[color_field] = $(this).data('color');
1042 self.view.dataset.write(self.id, data, {}).done(function() {
1043 self.record[color_field] = $(this).data('color');
1049 do_action_delete: function($action) {
1052 return $.when(self.view.dataset.unlink([self.id])).done(function() {
1053 self.group.remove_record(self.id);
1057 if (this.view.options.confirm_on_delete) {
1058 if (confirm(_t("Are you sure you want to delete this record ?"))) {
1064 do_action_edit: function($action) {
1065 this.view.open_record(this.id, true);
1067 do_action_open: function($action) {
1068 this.view.open_record(this.id);
1070 do_action_object: function ($action) {
1071 var button_attrs = $action.data();
1072 this.view.do_execute_action(button_attrs, this.view.dataset, this.id, this.do_reload);
1074 do_reload: function() {
1076 this.view.dataset.read_ids([this.id], this.view.fields_keys.concat(['__last_update'])).done(function(records) {
1077 _.each(self.sub_widgets, function(el) {
1080 self.sub_widgets = [];
1081 if (records.length) {
1082 self.set_record(records[0]);
1083 self.renderElement();
1084 self.init_content();
1085 self.group.compute_cards_auto_height();
1086 self.view.postprocess_m2m_tags();
1092 kanban_getcolor: function(variable) {
1094 switch (typeof(variable)) {
1096 for (var i=0, ii=variable.length; i<ii; i++) {
1097 index += variable.charCodeAt(i);
1101 index = Math.round(variable);
1106 var color = (index % this.view.number_of_color_schemes);
1109 kanban_color: function(variable) {
1110 var color = this.kanban_getcolor(variable);
1111 return color === '' ? '' : 'oe_kanban_color_' + color;
1113 kanban_image: function(model, field, id, cache, options) {
1114 options = options || {};
1116 if (this.record[field] && this.record[field].value && !instance.web.form.is_bin_size(this.record[field].value)) {
1117 url = 'data:image/png;base64,' + this.record[field].value;
1118 } else if (this.record[field] && ! this.record[field].value) {
1119 url = "/web/static/src/img/placeholder.png";
1121 id = JSON.stringify(id);
1122 if (options.preview_image)
1123 field = options.preview_image;
1124 url = this.session.url('/web/binary/image', {model: model, field: field, id: id});
1125 if (cache !== undefined) {
1126 // Set the cache duration in seconds.
1127 url += '&cache=' + parseInt(cache, 10);
1132 kanban_text_ellipsis: function(s, size) {
1136 } else if (s.length <= size) {
1139 return s.substr(0, size) + '...';
1142 kanban_compute_domain: function(domain) {
1143 return instance.web.form.compute_domain(domain, this.values);
1148 * Quick creation view.
1150 * Triggers a single event "added" with a single parameter "name", which is the
1151 * name entered by the user
1156 instance.web_kanban.QuickCreate = instance.web.Widget.extend({
1157 template: 'KanbanView.quick_create',
1160 * close_btn: If true, the widget will display a "Close" button able to trigger
1163 init: function(parent, dataset, context, buttons) {
1164 this._super(parent);
1165 this._dataset = dataset;
1166 this._buttons = buttons || false;
1167 this._context = context || {};
1169 start: function () {
1171 self.$input = this.$el.find('input');
1172 self.$input.keyup(function(event){
1173 if(event.keyCode == 13){
1177 $(".oe_kanban_quick_create").focusout(function (e) {
1178 var val = self.$el.find('input').val();
1179 if (/^\s*$/.test(val)) { self.trigger('close'); }
1180 e.stopImmediatePropagation();
1182 $(".oe_kanban_quick_create_add", this.$el).click(function () {
1186 $(".oe_kanban_quick_create_close", this.$el).click(function (ev) {
1187 ev.preventDefault();
1188 self.trigger('close');
1190 self.$input.keyup(function(e) {
1191 if (e.keyCode == 27 && self._buttons) {
1192 self.trigger('close');
1197 this.$el.find('input').focus();
1200 * Handles user event from nested quick creation view
1202 quick_add: function () {
1204 var val = this.$input.val();
1205 if (/^\s*$/.test(val)) { this.$el.remove(); return; }
1207 'name_create', [val, new instance.web.CompoundContext(
1208 this._dataset.get_context(), this._context)])
1209 .then(function(record) {
1210 self.$input.val("");
1211 self.trigger('added', record[0]);
1212 }, function(error, event) {
1213 event.preventDefault();
1214 return self.slow_create();
1217 slow_create: function() {
1219 var pop = new instance.web.form.SelectCreatePopup(this);
1221 self._dataset.model,
1223 title: _t("Create: ") + (this.string || this.name),
1224 initial_view: "form",
1225 disable_multiple_selection: true
1228 {"default_name": self.$input.val()}
1230 pop.on("elements_selected", self, function(element_ids) {
1231 self.$input.val("");
1232 self.trigger('added', element_ids[0]);
1238 * Interface to be implemented by kanban fields.
1241 instance.web_kanban.FieldInterface = {
1244 - parent: The widget's parent.
1245 - field: A dictionary giving details about the field, including the current field's value in the
1247 - $node: The field <field> tag as it appears in the view, encapsulated in a jQuery object.
1249 init: function(parent, field, $node) {},
1253 * Abstract class for classes implementing FieldInterface.
1256 * - value: useful property to hold the value of the field. By default, the constructor
1257 * sets value property.
1260 instance.web_kanban.AbstractField = instance.web.Widget.extend(instance.web_kanban.FieldInterface, {
1262 Constructor that saves the field and $node parameters and sets the "value" property.
1264 init: function(parent, field, $node) {
1265 this._super(parent);
1268 this.options = instance.web.py_eval(this.$node.attr("options") || '{}');
1269 this.set("value", field.raw_value);
1273 instance.web_kanban.Priority = instance.web_kanban.AbstractField.extend({
1274 init: function(parent, field, $node) {
1275 this._super.apply(this, arguments);
1276 this.name = $node.attr('name')
1277 this.parent = parent;
1279 prepare_priority: function() {
1281 var selection = this.field.selection || [];
1282 var init_value = selection && selection[0][0] || 0;
1283 var data = _.map(selection.slice(1), function(element, index) {
1285 'value': element[0],
1287 'click_value': element[0],
1289 if (index == 0 && self.get('value') == element[0]) {
1290 value['click_value'] = init_value;
1296 renderElement: function() {
1298 this.record_id = self.parent.id;
1299 this.priorities = self.prepare_priority();
1300 this.$el = $(QWeb.render("Priority", {'widget': this}));
1301 this.$el.find('li').click(self.do_action.bind(self));
1303 do_action: function(e) {
1305 var li = $(e.target).closest( "li" );
1308 value[self.name] = String(li.data('value'));
1309 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));
1312 reload_record: function() {
1317 instance.web_kanban.KanbanSelection = instance.web_kanban.AbstractField.extend({
1318 init: function(parent, field, $node) {
1319 this._super.apply(this, arguments);
1320 this.name = $node.attr('name')
1321 this.parent = parent;
1323 prepare_dropdown_selection: function() {
1325 _.map(this.field.selection || [], function(res) {
1329 'state_name': res[1],
1331 if (res[0] == 'normal') { value['state_class'] = 'oe_kanban_status'; }
1332 else if (res[0] == 'done') { value['state_class'] = 'oe_kanban_status oe_kanban_status_green'; }
1333 else { value['state_class'] = 'oe_kanban_status oe_kanban_status_red'; }
1338 renderElement: function() {
1340 this.record_id = self.parent.id;
1341 this.states = self.prepare_dropdown_selection();;
1342 this.$el = $(QWeb.render("KanbanSelection", {'widget': self}));
1343 this.$el.find('li').click(self.do_action.bind(self));
1345 do_action: function(e) {
1347 var li = $(e.target).closest( "li" );
1350 value[self.name] = String(li.data('value'));
1351 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));
1354 reload_record: function() {
1359 instance.web_kanban.fields_registry = new instance.web.Registry({});
1360 instance.web_kanban.fields_registry.add('priority','instance.web_kanban.Priority');
1361 instance.web_kanban.fields_registry.add('kanban_state_selection','instance.web_kanban.KanbanSelection');
1364 // vim:et fdc=0 fdl=0 foldnestmax=3 fdm=syntax: