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;
65 this.$el.addClass(this.fields_view.arch.attrs['class']);
66 this.$buttons = $(QWeb.render("KanbanView.buttons", {'widget': this}));
67 if (this.options.$buttons) {
68 this.$buttons.appendTo(this.options.$buttons);
70 this.$el.find('.oe_kanban_buttons').replaceWith(this.$buttons);
73 .on('click', 'button.oe_kanban_button_new', this.do_add_record)
74 .on('click', '.oe_kanban_add_column', this.do_add_group);
75 this.$groups = this.$el.find('.oe_kanban_groups tr');
76 this.fields_keys = _.keys(this.fields_view.fields);
77 this.add_qweb_template();
78 this.has_been_loaded.resolve();
79 this.trigger('kanban_view_loaded', data);
82 _is_quick_create_enabled: function() {
83 if (!this.options.quick_creatable || !this.is_action_enabled('create'))
85 if (this.fields_view.arch.attrs.quick_create !== undefined)
86 return JSON.parse(this.fields_view.arch.attrs.quick_create);
87 return !! this.group_by;
89 is_action_enabled: function(action) {
90 if (action === 'create' && !this.options.creatable)
92 return this._super(action);
95 * select the nodes into the xml and send to extract_aggregates the nodes with TagName="field"
97 add_qweb_template: function() {
98 for (var i=0, ii=this.fields_view.arch.children.length; i < ii; i++) {
99 var child = this.fields_view.arch.children[i];
100 if (child.tag === "templates") {
101 this.transform_qweb_template(child);
102 this.qweb.add_template(instance.web.json_node_to_xml(child));
104 } else if (child.tag === 'field') {
105 this.extract_aggregates(child);
109 /* extract_aggregates
110 * extract the agggregates from the nodes (TagName="field")
112 extract_aggregates: function(node) {
113 for (var j = 0, jj = this.group_operators.length; j < jj; j++) {
114 if (node.attrs[this.group_operators[j]]) {
115 this.aggregates[node.attrs.name] = node.attrs[this.group_operators[j]];
120 transform_qweb_template: function(node) {
121 var qweb_add_if = function(node, condition) {
122 if (node.attrs[QWeb.prefix + '-if']) {
123 condition = _.str.sprintf("(%s) and (%s)", node.attrs[QWeb.prefix + '-if'], condition);
125 node.attrs[QWeb.prefix + '-if'] = condition;
128 if (node.tag && node.attrs.modifiers) {
129 var modifiers = JSON.parse(node.attrs.modifiers || '{}');
130 if (modifiers.invisible) {
131 qweb_add_if(node, _.str.sprintf("!kanban_compute_domain(%s)", JSON.stringify(modifiers.invisible)));
136 if (this.fields_view.fields[node.attrs.name].type === 'many2many') {
137 this.many2manys.push(node.attrs.name);
139 node.attrs['class'] = (node.attrs['class'] || '') + ' oe_form_field oe_tags';
141 node.tag = QWeb.prefix;
142 node.attrs[QWeb.prefix + '-esc'] = 'record.' + node.attrs['name'] + '.value';
147 var type = node.attrs.type || '';
148 if (_.indexOf('action,object,edit,open,delete'.split(','), type) !== -1) {
149 _.each(node.attrs, function(v, k) {
150 if (_.indexOf('icon,type,name,args,string,context,states,kanban_states'.split(','), k) != -1) {
151 node.attrs['data-' + k] = v;
152 delete(node.attrs[k]);
155 if (node.attrs['data-string']) {
156 node.attrs.title = node.attrs['data-string'];
158 if (node.attrs['data-icon']) {
162 src: instance.session.prefix + '/web/static/src/img/icons/' + node.attrs['data-icon'] + '.png',
168 if (node.tag == 'a') {
169 node.attrs.href = '#';
171 node.attrs.type = 'button';
173 node.attrs['class'] = (node.attrs['class'] || '') + ' oe_kanban_action oe_kanban_action_' + node.tag;
178 for (var i = 0, ii = node.children.length; i < ii; i++) {
179 this.transform_qweb_template(node.children[i]);
183 do_add_record: function() {
184 this.dataset.index = null;
185 this.do_switch_view('form');
187 do_add_group: function() {
190 name: _t("Add column"),
191 res_model: self.group_by_field.relation,
192 views: [[false, 'form']],
193 type: 'ir.actions.act_window',
195 context: self.dataset.get_context(),
197 action_buttons: true,
200 var am = instance.webclient.action_manager;
201 var form = am.dialog_widget.views.form.controller;
202 form.on("on_button_cancel", am.dialog, am.dialog.close);
203 form.on('record_created', self, function(r) {
204 (new instance.web.DataSet(self, self.group_by_field.relation)).name_get([r]).done(function(new_record) {
206 var domain = self.dataset.domain.slice(0);
207 domain.push([self.group_by, '=', new_record[0][0]]);
208 var dataset = new instance.web.DataSetSearch(self, self.dataset.model, self.dataset.get_context(), domain);
213 value: new_record[0],
217 var new_group = new instance.web_kanban.KanbanGroup(self, [], datagroup, dataset);
218 self.do_add_groups([new_group]).done(function() {
219 $(window).scrollTo(self.groups.slice(-1)[0].$el, { axis: 'x' });
224 do_search: function(domain, context, group_by) {
226 this.$el.find('.oe_view_nocontent').remove();
227 this.search_domain = domain;
228 this.search_context = context;
229 this.search_group_by = group_by;
230 $.when(this.has_been_loaded).done(function() {
231 self.group_by = group_by.length ? group_by[0] : self.fields_view.arch.attrs.default_group_by;
232 self.group_by_field = self.fields_view.fields[self.group_by] || {};
233 self.grouped_by_m2o = (self.group_by_field.type === 'many2one');
234 self.$buttons.find('.oe_alternative').toggle(self.grouped_by_m2o);
235 self.$el.toggleClass('oe_kanban_grouped_by_m2o', self.grouped_by_m2o);
236 var grouping = new instance.web.Model(self.dataset.model, context, domain).query().group_by(self.group_by);
237 $.when(grouping).done(function(groups) {
239 self.do_process_groups(groups);
241 self.do_process_dataset();
246 do_process_groups: function(groups) {
248 this.$el.removeClass('oe_kanban_ungrouped').addClass('oe_kanban_grouped');
249 this.add_group_mutex.exec(function() {
250 self.dataset.ids = [];
251 var remaining = groups.length - 1,
253 return $.when.apply(null, _.map(groups, function (group, index) {
254 self.do_clear_groups();
255 var dataset = new instance.web.DataSetSearch(self, self.dataset.model,
256 new instance.web.CompoundContext(self.dataset.get_context(), group.model.context()), group.model.domain());
257 return dataset.read_slice(self.fields_keys.concat(['__last_update']), { 'limit': self.limit })
258 .then(function(records) {
259 self.dataset.ids.push.apply(self.dataset.ids, dataset.ids);
260 groups_array[index] = new instance.web_kanban.KanbanGroup(self, records, group, dataset);
262 self.dataset.index = self.dataset.size() ? 0 : null;
263 return self.do_add_groups(groups_array);
269 do_process_dataset: function() {
271 this.$el.removeClass('oe_kanban_grouped').addClass('oe_kanban_ungrouped');
272 this.add_group_mutex.exec(function() {
273 var def = $.Deferred();
274 self.dataset.read_slice(self.fields_keys.concat(['__last_update']), { 'limit': self.limit }).done(function(records) {
275 self.do_clear_groups();
276 var kgroup = new instance.web_kanban.KanbanGroup(self, records, null, self.dataset);
277 self.do_add_groups([kgroup]).done(function() {
278 if (_.isEmpty(records)) {
283 }).done(null, function() {
289 do_reload: function() {
290 this.do_search(this.search_domain, this.search_context, this.search_group_by);
292 do_clear_groups: function() {
293 var groups = this.groups.slice(0);
295 _.each(groups, function(group) {
299 do_add_groups: function(groups) {
301 var $parent = this.$el.parent();
303 _.each(groups, function(group) {
304 self.groups[group.undefined_title ? 'unshift' : 'push'](group);
306 var $last_td = self.$el.find('.oe_kanban_groups_headers td:last');
307 var groups_started = _.map(this.groups, function(group) {
308 if (!group.is_started) {
309 return group.insertBefore($last_td);
312 return $.when.apply(null, groups_started).done(function () {
313 self.on_groups_started();
314 self.$el.appendTo($parent);
315 _.each(self.groups, function(group) {
316 group.compute_cards_auto_height();
320 on_groups_started: function() {
322 this.compute_groups_width();
324 // Kanban cards drag'n'drop
325 var $columns = this.$el.find('.oe_kanban_column .oe_kanban_column_cards');
327 handle : '.oe_kanban_draghandle',
328 start: function(event, ui) {
329 self.currently_dragging.index = ui.item.parent().children('.oe_kanban_record').index(ui.item);
330 self.currently_dragging.group = ui.item.parents('.oe_kanban_column:first').data('widget');
331 ui.item.find('*').on('click.prevent', function(ev) {
334 ui.placeholder.height(ui.item.height());
337 stop: function(event, ui) {
338 var record = ui.item.data('widget');
339 var old_index = self.currently_dragging.index;
340 var new_index = ui.item.parent().children('.oe_kanban_record').index(ui.item);
341 var old_group = self.currently_dragging.group;
342 var new_group = ui.item.parents('.oe_kanban_column:first').data('widget');
343 if (!(old_group.title === new_group.title && old_group.value === new_group.value && old_index == new_index)) {
344 self.on_record_moved(record, old_group, old_index, new_group, new_index);
346 setTimeout(function() {
347 // A bit hacky but could not find a better solution for Firefox (problem not present in chrome)
348 // http://stackoverflow.com/questions/274843/preventing-javascript-click-event-with-scriptaculous-drag-and-drop
349 ui.item.find('*').off('click.prevent');
354 // Keep connectWith out of the sortable initialization for performance sake:
355 // http://www.planbox.com/blog/development/coding/jquery-ui-sortable-slow-to-bind.html
356 $columns.sortable({ connectWith: $columns });
358 // Kanban groups drag'n'drop
360 if (this.grouped_by_m2o) {
361 this.$('.oe_kanban_groups_headers').sortable({
362 items: '.oe_kanban_group_header',
367 start: function(event, ui) {
368 start_index = ui.item.index();
369 self.$('.oe_kanban_record, .oe_kanban_quick_create').css({ visibility: 'hidden' });
371 stop: function(event, ui) {
372 var stop_index = ui.item.index();
373 if (start_index !== stop_index) {
374 var $start_column = $('.oe_kanban_groups_records .oe_kanban_column').eq(start_index);
375 var $stop_column = $('.oe_kanban_groups_records .oe_kanban_column').eq(stop_index);
376 var method = (start_index > stop_index) ? 'insertBefore' : 'insertAfter';
377 $start_column[method]($stop_column);
378 var tmp_group = self.groups.splice(start_index, 1)[0];
379 self.groups.splice(stop_index, 0, tmp_group);
380 var new_sequence = _.pluck(self.groups, 'value');
381 (new instance.web.DataSet(self, self.group_by_field.relation)).resequence(new_sequence).done(function(r) {
383 console.error("Kanban: could not resequence model '%s'. Probably no 'sequence' field.", self.group_by_field.relation);
387 self.$('.oe_kanban_record, .oe_kanban_quick_create').css({ visibility: 'visible' });
392 this.$el.find('.oe_kanban_draghandle').removeClass('oe_kanban_draghandle');
394 this.postprocess_m2m_tags();
396 on_record_moved : function(record, old_group, old_index, new_group, new_index) {
399 $(old_group.$el).add(new_group.$el).find('.oe_kanban_aggregates, .oe_kanban_group_length').hide();
400 if (old_group === new_group) {
401 new_group.records.splice(old_index, 1);
402 new_group.records.splice(new_index, 0, record);
403 new_group.do_save_sequences();
405 old_group.records.splice(old_index, 1);
406 new_group.records.splice(new_index, 0, record);
407 record.group = new_group;
409 data[this.group_by] = new_group.value;
410 this.dataset.write(record.id, data, {}).done(function() {
412 new_group.do_save_sequences();
413 }).fail(function(error, evt) {
414 evt.preventDefault();
415 alert(_t("An error has occured while moving the record to this group."));
416 self.do_reload(); // TODO: use draggable + sortable in order to cancel the dragging when the rcp fails
420 compute_groups_width: function() {
423 _.each(this.groups, function(group) {
424 unfolded += group.state.folded ? 0 : 1;
425 group.$el.children(':first').css('width', '');
427 _.each(this.groups, function(group) {
428 if (!group.state.folded) {
429 if (182*unfolded>=self.$el.width()) {
430 group.$el.children(':first').css('width', "170px");
431 } else if (262*unfolded<self.$el.width()) {
432 group.$el.children(':first').css('width', "250px");
434 // -12 because of padding 6 between cards
435 // -1 because of the border of the latest dummy column
436 group.$el.children(':first').css('width', Math.floor((self.$el.width()-1)/unfolded)-12 + 'px');
442 do_show: function() {
444 this.$buttons.show();
446 this.do_push_state({});
447 return this._super();
449 do_hide: function () {
451 this.$buttons.hide();
453 return this._super();
455 open_record: function(id, editable) {
456 if (this.dataset.select_id(id)) {
457 this.do_switch_view('form', null, { mode: editable ? "edit" : undefined });
459 this.do_warn("Kanban: could not find id#" + id);
462 no_result: function() {
463 if (this.groups.group_by
464 || !this.options.action
465 || !this.options.action.help) {
468 this.$el.find('.oe_view_nocontent').remove();
470 $('<div class="oe_view_nocontent">').html(this.options.action.help)
472 var create_nocontent = this.$buttons;
473 this.$el.find('.oe_view_nocontent').click(function() {
474 create_nocontent.openerpBounce();
479 * postprocessing of fields type many2many
480 * make the rpc request for all ids/model and insert value inside .oe_tags fields
482 postprocess_m2m_tags: function() {
484 if (!this.many2manys.length) {
488 this.groups.forEach(function(group) {
489 group.records.forEach(function(record) {
490 self.many2manys.forEach(function(name) {
491 var field = record.record[name];
492 var $el = record.$('.oe_form_field.oe_tags[name=' + name + ']').empty();
493 if (!relations[field.relation]) {
494 relations[field.relation] = { ids: [], elements: {}};
496 var rel = relations[field.relation];
497 field.raw_value.forEach(function(id) {
499 if (!rel.elements[id]) {
500 rel.elements[id] = [];
502 rel.elements[id].push($el[0]);
507 _.each(relations, function(rel, rel_name) {
508 var dataset = new instance.web.DataSetSearch(self, rel_name, self.dataset.get_context());
509 dataset.name_get(_.uniq(rel.ids)).done(function(result) {
510 result.forEach(function(nameget) {
511 $(rel.elements[nameget[0]]).append('<span class="oe_tag">' + _.str.escapeHTML(nameget[1]) + '</span>');
519 function get_class(name) {
520 return new instance.web.Registry({'tmp' : name}).get_object("tmp");
523 instance.web_kanban.KanbanGroup = instance.web.Widget.extend({
524 template: 'KanbanView.group_header',
525 init: function (parent, records, group, dataset) {
528 this.$has_been_started = $.Deferred();
531 this.dataset = dataset;
532 this.dataset_offset = 0;
533 this.aggregates = {};
534 this.value = this.title = null;
536 this.value = group.get('value');
537 this.title = group.get('value');
538 if (this.value instanceof Array) {
539 this.title = this.value[1];
540 this.value = this.value[0];
542 var field = this.view.group_by_field;
543 if (!_.isEmpty(field)) {
545 this.title = instance.web.format_value(group.get('value'), field, false);
548 _.each(this.view.aggregates, function(value, key) {
549 self.aggregates[value] = group.get('aggregates')[key];
553 if (this.title === false) {
554 this.title = _t('Undefined');
555 this.undefined_title = true;
557 var key = this.view.group_by + '-' + this.value;
558 if (!this.view.state.groups[key]) {
559 this.view.state.groups[key] = {
560 folded: group ? group.get('folded') : false
563 this.state = this.view.state.groups[key];
564 this.$records = null;
567 this.$has_been_started.done(function() {
568 self.do_add_records(records);
574 if (! self.view.group_by) {
575 self.$el.addClass("oe_kanban_no_group");
576 self.quick = new (get_class(self.view.quick_create_class))(this, self.dataset, {}, false)
577 .on('added', self, self.proxy('quick_created'));
578 self.quick.replace($(".oe_kanban_no_group_qc_placeholder"));
580 this.$records = $(QWeb.render('KanbanView.group_records_container', { widget : this}));
581 this.$records.insertBefore(this.view.$el.find('.oe_kanban_groups_records td:last'));
583 this.$el.on('click', '.oe_kanban_group_dropdown li a', function(ev) {
584 var fn = 'do_action_' + $(ev.target).data().action;
585 if (typeof(self[fn]) === 'function') {
586 self[fn]($(ev.target));
590 this.$el.find('.oe_kanban_add').click(function () {
592 return self.quick.trigger('close');
595 ctx['default_' + self.view.group_by] = self.value;
596 self.quick = new (get_class(self.view.quick_create_class))(this, self.dataset, ctx, true)
597 .on('added', self, self.proxy('quick_created'))
598 .on('close', self, function() {
599 this.quick.destroy();
602 self.quick.appendTo($(".oe_kanban_group_list_header", self.$records));
605 // Add bounce effect on image '+' of kanban header when click on empty space of kanban grouped column.
606 this.$records.on('click', '.oe_kanban_show_more', this.do_show_more);
607 if (this.state.folded) {
608 this.do_toggle_fold();
610 this.$el.data('widget', this);
611 this.$records.data('widget', this);
612 this.$has_been_started.resolve();
613 var add_btn = this.$el.find('.oe_kanban_add');
614 add_btn.tipsy({delayIn: 500, delayOut: 1000});
615 this.$records.find(".oe_kanban_column_cards").click(function (ev) {
616 if (ev.target == ev.currentTarget) {
617 if (!self.state.folded) {
618 add_btn.openerpBounce();
622 this.is_started = true;
625 compute_cards_auto_height: function() {
626 // oe_kanban_no_auto_height is an empty class used to disable this feature
627 if (!this.view.group_by) {
630 _.each(this.records, function(r) {
631 var $e = r.$el.children(':first:not(.oe_kanban_no_auto_height)').css('min-height', 0);
634 min_height = Math.max(min_height, $e.outerHeight());
637 $(els).css('min-height', min_height);
640 destroy: function() {
643 this.$records.remove();
646 do_show_more: function(evt) {
648 var ids = self.view.dataset.ids.splice(0);
649 return this.dataset.read_slice(this.view.fields_keys.concat(['__last_update']), {
650 'limit': self.view.limit,
651 'offset': self.dataset_offset += self.view.limit
652 }).then(function(records) {
653 self.view.dataset.ids = ids.concat(self.view.dataset.ids);
654 self.do_add_records(records);
655 self.compute_cards_auto_height();
659 do_add_records: function(records, prepend) {
661 var $list_header = this.$records.find('.oe_kanban_group_list_header');
662 var $show_more = this.$records.find('.oe_kanban_show_more');
663 var $cards = this.$records.find('.oe_kanban_column_cards');
665 _.each(records, function(record) {
666 var rec = new instance.web_kanban.KanbanRecord(self, record);
668 rec.appendTo($cards);
669 self.records.push(rec);
671 rec.prependTo($cards);
672 self.records.unshift(rec);
675 if ($show_more.length) {
676 var size = this.dataset.size();
677 $show_more.toggle(this.records.length < size).find('.oe_kanban_remaining').text(size - this.records.length);
680 remove_record: function(id, remove_from_dataset) {
681 for (var i = 0; i < this.records.length; i++) {
682 if (this.records[i]['id'] === id) {
683 this.records.splice(i, 1);
688 do_toggle_fold: function(compute_width) {
689 this.$el.add(this.$records).toggleClass('oe_kanban_group_folded');
690 this.state.folded = this.$el.is('.oe_kanban_group_folded');
691 this.$("ul.oe_kanban_group_dropdown li a[data-action=toggle_fold]").text((this.state.folded) ? _t("Unfold") : _t("Fold"));
693 do_action_toggle_fold: function() {
694 this.do_toggle_fold();
695 this.view.compute_groups_width();
697 do_action_edit: function() {
701 name: _t("Edit column"),
702 res_model: self.view.group_by_field.relation,
703 views: [[false, 'form']],
704 type: 'ir.actions.act_window',
707 action_buttons: true,
710 var am = instance.webclient.action_manager;
711 var form = am.dialog_widget.views.form.controller;
712 form.on("on_button_cancel", am.dialog, am.dialog.close);
713 form.on('record_saved', self, function() {
715 self.view.do_reload();
718 do_action_delete: function() {
720 if (confirm(_t("Are you sure to remove this column ?"))) {
721 (new instance.web.DataSet(self, self.view.group_by_field.relation)).unlink([self.value]).done(function(r) {
722 self.view.do_reload();
726 do_save_sequences: function() {
728 if (_.indexOf(this.view.fields_keys, 'sequence') > -1) {
729 var new_sequence = _.pluck(this.records, 'id');
730 self.view.dataset.resequence(new_sequence);
734 * Handles a newly created record
736 * @param {id} id of the newly created record
738 quick_created: function (record) {
739 var id = record, self = this;
740 this.dataset.read_ids([id], this.view.fields_keys)
741 .done(function (records) {
742 self.view.dataset.ids.push(id);
743 self.do_add_records(records, true);
748 instance.web_kanban.KanbanRecord = instance.web.Widget.extend({
749 template: 'KanbanView.record',
750 init: function (parent, record) {
753 this.view = parent.view;
755 this.set_record(record);
756 if (!this.view.state.records[this.id]) {
757 this.view.state.records[this.id] = {
761 this.state = this.view.state.records[this.id];
763 set_record: function(record) {
767 _.each(record, function(v, k) {
772 this.record = this.transform_record(record);
776 this.$el.data('widget', this);
779 transform_record: function(record) {
782 _.each(record, function(value, name) {
783 var r = _.clone(self.view.fields_view.fields[name] || {});
784 if ((r.type === 'date' || r.type === 'datetime') && value) {
785 r.raw_value = instance.web.auto_str_to_date(value);
789 r.value = instance.web.format_value(value, r);
790 new_record[name] = r;
794 renderElement: function() {
795 this.qweb_context = {
799 read_only_mode: this.view.options.read_only_mode,
801 for (var p in this) {
802 if (_.str.startsWith(p, 'kanban_')) {
803 this.qweb_context[p] = _.bind(this[p], this);
806 var $el = instance.web.qweb.render(this.template, {
808 'content': this.view.qweb.render('kanban-box', this.qweb_context)
810 this.replaceElement($el);
812 bind_events: function() {
814 this.setup_color_picker();
815 this.$el.find('[tooltip]').tipsy({
820 var template = $(this).attr('tooltip');
821 if (!self.view.qweb.has_template(template)) {
824 return self.view.qweb.render(template, self.qweb_context);
832 // If no draghandle is found, make the whole card as draghandle (provided one can edit)
833 if (!this.$el.find('.oe_kanban_draghandle').length) {
834 this.$el.children(':first')
835 .toggleClass('oe_kanban_draghandle', this.view.is_action_enabled('edit'));
838 this.$el.find('.oe_kanban_action').click(function(ev) {
840 var $action = $(this),
841 type = $action.data('type') || 'button',
842 method = 'do_action_' + (type === 'action' ? 'object' : type);
843 if ((type === 'edit' || type === 'delete') && ! self.view.is_action_enabled(type)) {
844 self.view.open_record(self.id, true);
845 } else if (_.str.startsWith(type, 'switch_')) {
846 self.view.do_switch_view(type.substr(7));
847 } else if (typeof self[method] === 'function') {
848 self[method]($action);
850 self.do_warn("Kanban: no action for type : " + type);
854 if (this.$el.find('.oe_kanban_global_click,.oe_kanban_global_click_edit').length) {
855 this.$el.on('click', function(ev) {
856 if (!ev.isTrigger && !$._data(ev.target, 'events')) {
858 var elem = ev.target;
862 var events = $._data(elem, 'events');
863 if (elem == ev.currentTarget) {
868 if (events && events.click) {
869 // do not trigger global click if one child has a click event registered
873 if (trigger && events && events.click) {
874 _.each(events.click, function(click_event) {
875 if (click_event.selector) {
876 // For each parent of original target, check if a
877 // delegated click is bound to any previously found children
878 _.each(children, function(child) {
879 if ($(child).is(click_event.selector)) {
886 elem = elem.parentElement;
889 self.on_card_clicked(ev);
895 /* actions when user click on the block with a specific class
896 * open on normal view : oe_kanban_global_click
897 * open on form/edit view : oe_kanban_global_click_edit
899 on_card_clicked: function(ev) {
900 if(this.$el.find('.oe_kanban_global_click_edit').size()>0)
901 this.do_action_edit();
903 this.do_action_open();
905 setup_color_picker: function() {
907 var $el = this.$el.find('ul.oe_kanban_colorpicker');
909 $el.html(QWeb.render('KanbanColorPicker', {
912 $el.on('click', 'a', function(ev) {
914 var color_field = $(this).parents('.oe_kanban_colorpicker').first().data('field') || 'color';
916 data[color_field] = $(this).data('color');
917 self.view.dataset.write(self.id, data, {}).done(function() {
918 self.record[color_field] = $(this).data('color');
924 do_action_delete: function($action) {
927 return $.when(self.view.dataset.unlink([self.id])).done(function() {
928 self.group.remove_record(self.id);
932 if (this.view.options.confirm_on_delete) {
933 if (confirm(_t("Are you sure you want to delete this record ?"))) {
939 do_action_edit: function($action) {
940 this.view.open_record(this.id, true);
942 do_action_open: function($action) {
943 this.view.open_record(this.id);
945 do_action_object: function ($action) {
946 var button_attrs = $action.data();
947 this.view.do_execute_action(button_attrs, this.view.dataset, this.id, this.do_reload);
949 do_reload: function() {
951 this.view.dataset.read_ids([this.id], this.view.fields_keys.concat(['__last_update'])).done(function(records) {
952 if (records.length) {
953 self.set_record(records[0]);
954 self.renderElement();
955 self.$el.data('widget', self);
957 self.group.compute_cards_auto_height();
958 self.view.postprocess_m2m_tags();
964 kanban_getcolor: function(variable) {
966 switch (typeof(variable)) {
968 for (var i=0, ii=variable.length; i<ii; i++) {
969 index += variable.charCodeAt(i);
973 index = Math.round(variable);
978 var color = (index % this.view.number_of_color_schemes);
981 kanban_color: function(variable) {
982 var color = this.kanban_getcolor(variable);
983 return color === '' ? '' : 'oe_kanban_color_' + color;
985 kanban_gravatar: function(email, size) {
987 email = _.str.trim(email || '').toLowerCase();
988 var default_ = _.str.isBlank(email) ? 'mm' : 'identicon';
989 var email_md5 = $.md5(email);
990 return 'http://www.gravatar.com/avatar/' + email_md5 + '.png?s=' + size + '&d=' + default_;
992 kanban_image: function(model, field, id, cache, options) {
993 options = options || {};
995 if (this.record[field] && this.record[field].value && !instance.web.form.is_bin_size(this.record[field].value)) {
996 url = 'data:image/png;base64,' + this.record[field].value;
997 } else if (this.record[field] && ! this.record[field].value) {
998 url = "/web/static/src/img/placeholder.png";
1000 id = JSON.stringify(id);
1001 if (options.preview_image)
1002 field = options.preview_image;
1003 url = this.session.url('/web/binary/image', {model: model, field: field, id: id});
1004 if (cache !== undefined) {
1005 // Set the cache duration in seconds.
1006 url += '&cache=' + parseInt(cache, 10);
1011 kanban_text_ellipsis: function(s, size) {
1015 } else if (s.length <= size) {
1018 return s.substr(0, size) + '...';
1021 kanban_compute_domain: function(domain) {
1022 return instance.web.form.compute_domain(domain, this.values);
1027 * Quick creation view.
1029 * Triggers a single event "added" with a single parameter "name", which is the
1030 * name entered by the user
1035 instance.web_kanban.QuickCreate = instance.web.Widget.extend({
1036 template: 'KanbanView.quick_create',
1039 * close_btn: If true, the widget will display a "Close" button able to trigger
1042 init: function(parent, dataset, context, buttons) {
1043 this._super(parent);
1044 this._dataset = dataset;
1045 this._buttons = buttons || false;
1046 this._context = context || {};
1048 start: function () {
1050 self.$input = this.$el.find('input');
1051 self.$input.keyup(function(event){
1052 if(event.keyCode == 13){
1056 $(".oe_kanban_quick_create_add", this.$el).click(function () {
1060 $(".oe_kanban_quick_create_close", this.$el).click(function (ev) {
1061 ev.preventDefault();
1062 self.trigger('close');
1064 self.$input.keyup(function(e) {
1065 if (e.keyCode == 27 && self._buttons) {
1066 self.trigger('close');
1071 this.$el.find('input').focus();
1074 * Handles user event from nested quick creation view
1076 quick_add: function () {
1078 var val = this.$input.val();
1079 if (/^\s*$/.test(val)) { return; }
1081 'name_create', [val, new instance.web.CompoundContext(
1082 this._dataset.get_context(), this._context)])
1083 .then(function(record) {
1084 self.$input.val("");
1085 self.trigger('added', record[0]);
1086 }, function(error, event) {
1087 event.preventDefault();
1088 return self.slow_create();
1091 slow_create: function() {
1093 var pop = new instance.web.form.SelectCreatePopup(this);
1095 self._dataset.model,
1097 title: _t("Create: ") + (this.string || this.name),
1098 initial_view: "form",
1099 disable_multiple_selection: true
1102 {"default_name": self.$input.val()}
1104 pop.on("elements_selected", self, function(element_ids) {
1105 self.$input.val("");
1106 self.trigger('added', element_ids[0]);
1112 // vim:et fdc=0 fdl=0 foldnestmax=3 fdm=syntax: