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 if (_.indexOf(this.many2manys, node.attrs.name) < 0) {
138 this.many2manys.push(node.attrs.name);
141 node.attrs['class'] = (node.attrs['class'] || '') + ' oe_form_field oe_tags';
143 node.tag = QWeb.prefix;
144 node.attrs[QWeb.prefix + '-esc'] = 'record.' + node.attrs['name'] + '.value';
149 var type = node.attrs.type || '';
150 if (_.indexOf('action,object,edit,open,delete'.split(','), type) !== -1) {
151 _.each(node.attrs, function(v, k) {
152 if (_.indexOf('icon,type,name,args,string,context,states,kanban_states'.split(','), k) != -1) {
153 node.attrs['data-' + k] = v;
154 delete(node.attrs[k]);
157 if (node.attrs['data-string']) {
158 node.attrs.title = node.attrs['data-string'];
160 if (node.attrs['data-icon']) {
164 src: instance.session.prefix + '/web/static/src/img/icons/' + node.attrs['data-icon'] + '.png',
170 if (node.tag == 'a') {
171 node.attrs.href = '#';
173 node.attrs.type = 'button';
175 node.attrs['class'] = (node.attrs['class'] || '') + ' oe_kanban_action oe_kanban_action_' + node.tag;
180 for (var i = 0, ii = node.children.length; i < ii; i++) {
181 this.transform_qweb_template(node.children[i]);
185 do_add_record: function() {
186 this.dataset.index = null;
187 this.do_switch_view('form');
189 do_add_group: function() {
192 name: _t("Add column"),
193 res_model: self.group_by_field.relation,
194 views: [[false, 'form']],
195 type: 'ir.actions.act_window',
197 context: self.dataset.get_context(),
199 action_buttons: true,
202 var am = instance.webclient.action_manager;
203 var form = am.dialog_widget.views.form.controller;
204 form.on("on_button_cancel", am.dialog, am.dialog.close);
205 form.on('record_created', self, function(r) {
206 (new instance.web.DataSet(self, self.group_by_field.relation)).name_get([r]).done(function(new_record) {
208 var domain = self.dataset.domain.slice(0);
209 domain.push([self.group_by, '=', new_record[0][0]]);
210 var dataset = new instance.web.DataSetSearch(self, self.dataset.model, self.dataset.get_context(), domain);
215 value: new_record[0],
219 var new_group = new instance.web_kanban.KanbanGroup(self, [], datagroup, dataset);
220 self.do_add_groups([new_group]).done(function() {
221 $(window).scrollTo(self.groups.slice(-1)[0].$el, { axis: 'x' });
226 do_search: function(domain, context, group_by) {
228 this.search_domain = domain;
229 this.search_context = context;
230 this.search_group_by = group_by;
231 return $.when(this.has_been_loaded).then(function() {
232 self.group_by = group_by.length ? group_by[0] : self.fields_view.arch.attrs.default_group_by;
233 self.group_by_field = self.fields_view.fields[self.group_by] || {};
234 self.grouped_by_m2o = (self.group_by_field.type === 'many2one');
235 self.$buttons.find('.oe_alternative').toggle(self.grouped_by_m2o);
236 self.$el.toggleClass('oe_kanban_grouped_by_m2o', self.grouped_by_m2o);
237 var grouping_fields = self.group_by ? [self.group_by].concat(_.keys(self.aggregates)) : undefined;
238 var grouping = new instance.web.Model(self.dataset.model, context, domain).query().group_by(grouping_fields);
239 return self.alive($.when(grouping)).done(function(groups) {
240 self.remove_no_result();
242 self.do_process_groups(groups);
244 self.do_process_dataset();
249 do_process_groups: function(groups) {
251 this.$el.find('table:first').show();
252 this.$el.removeClass('oe_kanban_ungrouped').addClass('oe_kanban_grouped');
253 this.add_group_mutex.exec(function() {
254 self.do_clear_groups();
255 self.dataset.ids = [];
256 if (!groups.length) {
261 var remaining = groups.length - 1,
263 return $.when.apply(null, _.map(groups, function (group, index) {
264 var def = $.when([]);
265 var dataset = new instance.web.DataSetSearch(self, self.dataset.model,
266 new instance.web.CompoundContext(self.dataset.get_context(), group.model.context()), group.model.domain());
267 if (group.attributes.length >= 1) {
268 def = dataset.read_slice(self.fields_keys.concat(['__last_update']), { 'limit': self.limit });
270 return def.then(function(records) {
271 self.nb_records += records.length;
272 self.dataset.ids.push.apply(self.dataset.ids, dataset.ids);
273 groups_array[index] = new instance.web_kanban.KanbanGroup(self, records, group, dataset);
275 self.dataset.index = self.dataset.size() ? 0 : null;
276 return self.do_add_groups(groups_array);
279 })).then(function () {
280 if(!self.nb_records) {
286 do_process_dataset: function() {
288 this.$el.find('table:first').show();
289 this.$el.removeClass('oe_kanban_grouped').addClass('oe_kanban_ungrouped');
290 this.add_group_mutex.exec(function() {
291 var def = $.Deferred();
292 self.do_clear_groups();
293 self.dataset.read_slice(self.fields_keys.concat(['__last_update']), { 'limit': self.limit }).done(function(records) {
294 var kgroup = new instance.web_kanban.KanbanGroup(self, records, null, self.dataset);
295 self.do_add_groups([kgroup]).done(function() {
296 if (_.isEmpty(records)) {
301 }).done(null, function() {
307 do_reload: function() {
308 this.do_search(this.search_domain, this.search_context, this.search_group_by);
310 do_clear_groups: function() {
311 var groups = this.groups.slice(0);
313 _.each(groups, function(group) {
317 do_add_groups: function(groups) {
319 var $parent = this.$el.parent();
321 _.each(groups, function(group) {
322 self.groups[group.undefined_title ? 'unshift' : 'push'](group);
324 var $last_td = self.$el.find('.oe_kanban_groups_headers td:last');
325 var groups_started = _.map(this.groups, function(group) {
326 if (!group.is_started) {
327 group.on("add_record", self, function () {
328 self.remove_no_result();
330 return group.insertBefore($last_td);
333 return $.when.apply(null, groups_started).done(function () {
334 self.on_groups_started();
335 self.$el.appendTo($parent);
336 _.each(self.groups, function(group) {
337 group.compute_cards_auto_height();
341 on_groups_started: function() {
344 // Kanban cards drag'n'drop
345 var $columns = this.$el.find('.oe_kanban_column .oe_kanban_column_cards');
347 handle : '.oe_kanban_draghandle',
348 start: function(event, ui) {
349 self.currently_dragging.index = ui.item.parent().children('.oe_kanban_record').index(ui.item);
350 self.currently_dragging.group = ui.item.parents('.oe_kanban_column:first').data('widget');
351 ui.item.find('*').on('click.prevent', function(ev) {
354 ui.placeholder.height(ui.item.height());
357 stop: function(event, ui) {
358 var record = ui.item.data('widget');
359 var old_index = self.currently_dragging.index;
360 var new_index = ui.item.parent().children('.oe_kanban_record').index(ui.item);
361 var old_group = self.currently_dragging.group;
362 var new_group = ui.item.parents('.oe_kanban_column:first').data('widget');
363 if (!(old_group.title === new_group.title && old_group.value === new_group.value && old_index == new_index)) {
364 self.on_record_moved(record, old_group, old_index, new_group, new_index);
366 setTimeout(function() {
367 // A bit hacky but could not find a better solution for Firefox (problem not present in chrome)
368 // http://stackoverflow.com/questions/274843/preventing-javascript-click-event-with-scriptaculous-drag-and-drop
369 ui.item.find('*').off('click.prevent');
374 // Keep connectWith out of the sortable initialization for performance sake:
375 // http://www.planbox.com/blog/development/coding/jquery-ui-sortable-slow-to-bind.html
376 $columns.sortable({ connectWith: $columns });
378 // Kanban groups drag'n'drop
380 if (this.grouped_by_m2o) {
381 this.$('.oe_kanban_groups_headers').sortable({
382 items: '.oe_kanban_group_header',
387 start: function(event, ui) {
388 start_index = ui.item.index();
389 self.$('.oe_kanban_record, .oe_kanban_quick_create').css({ visibility: 'hidden' });
391 stop: function(event, ui) {
392 var stop_index = ui.item.index();
393 if (start_index !== stop_index) {
394 var $start_column = $('.oe_kanban_groups_records .oe_kanban_column').eq(start_index);
395 var $stop_column = $('.oe_kanban_groups_records .oe_kanban_column').eq(stop_index);
396 var method = (start_index > stop_index) ? 'insertBefore' : 'insertAfter';
397 $start_column[method]($stop_column);
398 var tmp_group = self.groups.splice(start_index, 1)[0];
399 self.groups.splice(stop_index, 0, tmp_group);
400 var new_sequence = _.pluck(self.groups, 'value');
401 (new instance.web.DataSet(self, self.group_by_field.relation)).resequence(new_sequence).done(function(r) {
403 console.error("Kanban: could not resequence model '%s'. Probably no 'sequence' field.", self.group_by_field.relation);
407 self.$('.oe_kanban_record, .oe_kanban_quick_create').css({ visibility: 'visible' });
412 this.$el.find('.oe_kanban_draghandle').removeClass('oe_kanban_draghandle');
414 this.postprocess_m2m_tags();
416 on_record_moved : function(record, old_group, old_index, new_group, new_index) {
419 $(old_group.$el).add(new_group.$el).find('.oe_kanban_aggregates, .oe_kanban_group_length').hide();
420 if (old_group === new_group) {
421 new_group.records.splice(old_index, 1);
422 new_group.records.splice(new_index, 0, record);
423 new_group.do_save_sequences();
425 old_group.records.splice(old_index, 1);
426 new_group.records.splice(new_index, 0, record);
427 record.group = new_group;
429 data[this.group_by] = new_group.value;
430 this.dataset.write(record.id, data, {}).done(function() {
432 new_group.do_save_sequences();
433 }).fail(function(error, evt) {
434 evt.preventDefault();
435 alert(_t("An error has occured while moving the record to this group: ") + data.message);
436 self.do_reload(); // TODO: use draggable + sortable in order to cancel the dragging when the rcp fails
441 do_show: function() {
443 this.$buttons.show();
445 this.do_push_state({});
446 return this._super();
448 do_hide: function () {
450 this.$buttons.hide();
452 return this._super();
454 open_record: function(id, editable) {
455 if (this.dataset.select_id(id)) {
456 this.do_switch_view('form', null, { mode: editable ? "edit" : undefined });
458 this.do_warn("Kanban: could not find id#" + id);
461 no_result: function() {
463 if (this.groups.group_by
464 || !this.options.action
465 || (!this.options.action.help && !this.options.action.get_empty_list_help)) {
468 this.$el.find('table:first').css("position", "absolute");
469 $(QWeb.render('KanbanView.nocontent', { content : this.options.action.get_empty_list_help || this.options.action.help})).insertAfter(this.$('table:first'));
470 this.$el.find('.oe_view_nocontent').click(function() {
471 self.$buttons.openerpBounce();
474 remove_no_result: function() {
475 this.$el.find('table:first').css("position", false);
476 this.$el.find('.oe_view_nocontent').remove();
480 * postprocessing of fields type many2many
481 * make the rpc request for all ids/model and insert value inside .oe_tags fields
483 postprocess_m2m_tags: function() {
485 if (!this.many2manys.length) {
489 this.groups.forEach(function(group) {
490 group.records.forEach(function(record) {
491 self.many2manys.forEach(function(name) {
492 var field = record.record[name];
493 var $el = record.$('.oe_form_field.oe_tags[name=' + name + ']').empty();
494 if (!relations[field.relation]) {
495 relations[field.relation] = { ids: [], elements: {}};
497 var rel = relations[field.relation];
498 field.raw_value.forEach(function(id) {
500 if (!rel.elements[id]) {
501 rel.elements[id] = [];
503 rel.elements[id].push($el[0]);
508 _.each(relations, function(rel, rel_name) {
509 var dataset = new instance.web.DataSetSearch(self, rel_name, self.dataset.get_context());
510 dataset.name_get(_.uniq(rel.ids)).done(function(result) {
511 result.forEach(function(nameget) {
512 $(rel.elements[nameget[0]]).append('<span class="oe_tag">' + _.str.escapeHTML(nameget[1]) + '</span>');
520 function get_class(name) {
521 return new instance.web.Registry({'tmp' : name}).get_object("tmp");
524 instance.web_kanban.KanbanGroup = instance.web.Widget.extend({
525 template: 'KanbanView.group_header',
526 init: function (parent, records, group, dataset) {
529 this.$has_been_started = $.Deferred();
532 this.dataset = dataset;
533 this.dataset_offset = 0;
534 this.aggregates = {};
535 this.value = this.title = null;
537 this.value = group.get('value');
538 this.title = group.get('value');
539 if (this.value instanceof Array) {
540 this.title = this.value[1];
541 this.value = this.value[0];
543 var field = this.view.group_by_field;
544 if (!_.isEmpty(field)) {
546 this.title = instance.web.format_value(group.get('value'), field, false);
549 _.each(this.view.aggregates, function(value, key) {
550 self.aggregates[value] = group.get('aggregates')[key];
554 if (this.title === false) {
555 this.title = _t('Undefined');
556 this.undefined_title = true;
558 var key = this.view.group_by + '-' + this.value;
559 if (!this.view.state.groups[key]) {
560 this.view.state.groups[key] = {
561 folded: group ? group.get('folded') : false
564 this.state = this.view.state.groups[key];
565 this.$records = null;
568 this.$has_been_started.done(function() {
569 self.do_add_records(records);
575 if (! self.view.group_by) {
576 self.$el.addClass("oe_kanban_no_group");
577 self.quick = new (get_class(self.view.quick_create_class))(this, self.dataset, {}, false)
578 .on('added', self, self.proxy('quick_created'));
579 self.quick.replace($(".oe_kanban_no_group_qc_placeholder"));
581 this.$records = $(QWeb.render('KanbanView.group_records_container', { widget : this}));
582 this.$records.insertBefore(this.view.$el.find('.oe_kanban_groups_records td:last'));
584 this.$el.on('click', '.oe_kanban_group_dropdown li a', function(ev) {
585 var fn = 'do_action_' + $(ev.target).data().action;
586 if (typeof(self[fn]) === 'function') {
587 self[fn]($(ev.target));
591 this.$el.find('.oe_kanban_add').click(function () {
592 if (self.view.quick) {
593 self.view.quick.trigger('close');
598 self.view.$el.find('.oe_view_nocontent').hide();
600 ctx['default_' + self.view.group_by] = self.value;
601 self.quick = new (get_class(self.view.quick_create_class))(this, self.dataset, ctx, true)
602 .on('added', self, self.proxy('quick_created'))
603 .on('close', self, function() {
604 self.view.$el.find('.oe_view_nocontent').show();
605 this.quick.destroy();
606 delete self.view.quick;
609 self.quick.appendTo($(".oe_kanban_group_list_header", self.$records));
611 self.view.quick = self.quick;
613 // Add bounce effect on image '+' of kanban header when click on empty space of kanban grouped column.
614 this.$records.on('click', '.oe_kanban_show_more', this.do_show_more);
615 if (this.state.folded) {
616 this.do_toggle_fold();
618 this.$el.data('widget', this);
619 this.$records.data('widget', this);
620 this.$has_been_started.resolve();
621 var add_btn = this.$el.find('.oe_kanban_add');
622 add_btn.tipsy({delayIn: 500, delayOut: 1000});
623 this.$records.find(".oe_kanban_column_cards").click(function (ev) {
624 if (ev.target == ev.currentTarget) {
625 if (!self.state.folded) {
626 add_btn.openerpBounce();
630 this.is_started = true;
633 compute_cards_auto_height: function() {
634 // oe_kanban_no_auto_height is an empty class used to disable this feature
635 if (!this.view.group_by) {
638 _.each(this.records, function(r) {
639 var $e = r.$el.children(':first:not(.oe_kanban_no_auto_height)').css('min-height', 0);
642 min_height = Math.max(min_height, $e.outerHeight());
645 $(els).css('min-height', min_height);
648 destroy: function() {
651 this.$records.remove();
654 do_show_more: function(evt) {
656 var ids = self.view.dataset.ids.splice(0);
657 return this.dataset.read_slice(this.view.fields_keys.concat(['__last_update']), {
658 'limit': self.view.limit,
659 'offset': self.dataset_offset += self.view.limit
660 }).then(function(records) {
661 self.view.dataset.ids = ids.concat(self.dataset.ids);
662 self.do_add_records(records);
663 self.compute_cards_auto_height();
664 self.view.postprocess_m2m_tags();
668 do_add_records: function(records, prepend) {
670 var $list_header = this.$records.find('.oe_kanban_group_list_header');
671 var $show_more = this.$records.find('.oe_kanban_show_more');
672 var $cards = this.$records.find('.oe_kanban_column_cards');
674 _.each(records, function(record) {
675 var rec = new instance.web_kanban.KanbanRecord(self, record);
677 rec.appendTo($cards);
678 self.records.push(rec);
680 rec.prependTo($cards);
681 self.records.unshift(rec);
684 if ($show_more.length) {
685 var size = this.dataset.size();
686 $show_more.toggle(this.records.length < size).find('.oe_kanban_remaining').text(size - this.records.length);
689 remove_record: function(id, remove_from_dataset) {
690 for (var i = 0; i < this.records.length; i++) {
691 if (this.records[i]['id'] === id) {
692 this.records.splice(i, 1);
697 do_toggle_fold: function(compute_width) {
698 this.$el.add(this.$records).toggleClass('oe_kanban_group_folded');
699 this.state.folded = this.$el.is('.oe_kanban_group_folded');
700 this.$("ul.oe_kanban_group_dropdown li a[data-action=toggle_fold]").text((this.state.folded) ? _t("Unfold") : _t("Fold"));
702 do_action_toggle_fold: function() {
703 this.do_toggle_fold();
705 do_action_edit: function() {
709 name: _t("Edit column"),
710 res_model: self.view.group_by_field.relation,
711 views: [[false, 'form']],
712 type: 'ir.actions.act_window',
715 action_buttons: true,
718 var am = instance.webclient.action_manager;
719 var form = am.dialog_widget.views.form.controller;
720 form.on("on_button_cancel", am.dialog, am.dialog.close);
721 form.on('record_saved', self, function() {
723 self.view.do_reload();
726 do_action_delete: function() {
728 if (confirm(_t("Are you sure to remove this column ?"))) {
729 (new instance.web.DataSet(self, self.view.group_by_field.relation)).unlink([self.value]).done(function(r) {
730 self.view.do_reload();
734 do_save_sequences: function() {
736 if (_.indexOf(this.view.fields_keys, 'sequence') > -1) {
737 var new_sequence = _.pluck(this.records, 'id');
738 self.view.dataset.resequence(new_sequence);
742 * Handles a newly created record
744 * @param {id} id of the newly created record
746 quick_created: function (record) {
747 var id = record, self = this;
748 self.view.remove_no_result();
749 self.trigger("add_record");
750 this.dataset.read_ids([id], this.view.fields_keys)
751 .done(function (records) {
752 self.view.dataset.ids.push(id);
753 self.do_add_records(records, true);
758 instance.web_kanban.KanbanRecord = instance.web.Widget.extend({
759 template: 'KanbanView.record',
760 init: function (parent, record) {
763 this.view = parent.view;
765 this.set_record(record);
766 if (!this.view.state.records[this.id]) {
767 this.view.state.records[this.id] = {
771 this.state = this.view.state.records[this.id];
773 set_record: function(record) {
777 _.each(record, function(v, k) {
782 this.record = this.transform_record(record);
786 this.$el.data('widget', this);
789 transform_record: function(record) {
792 _.each(record, function(value, name) {
793 var r = _.clone(self.view.fields_view.fields[name] || {});
794 if ((r.type === 'date' || r.type === 'datetime') && value) {
795 r.raw_value = instance.web.auto_str_to_date(value);
799 r.value = instance.web.format_value(value, r);
800 new_record[name] = r;
804 renderElement: function() {
805 this.qweb_context = {
809 read_only_mode: this.view.options.read_only_mode,
811 for (var p in this) {
812 if (_.str.startsWith(p, 'kanban_')) {
813 this.qweb_context[p] = _.bind(this[p], this);
816 var $el = instance.web.qweb.render(this.template, {
818 'content': this.view.qweb.render('kanban-box', this.qweb_context)
820 this.replaceElement($el);
822 bind_events: function() {
824 this.setup_color_picker();
825 this.$el.find('[tooltip]').tipsy({
830 var template = $(this).attr('tooltip');
831 if (!self.view.qweb.has_template(template)) {
834 return self.view.qweb.render(template, self.qweb_context);
842 // If no draghandle is found, make the whole card as draghandle (provided one can edit)
843 if (!this.$el.find('.oe_kanban_draghandle').length) {
844 this.$el.children(':first')
845 .toggleClass('oe_kanban_draghandle', this.view.is_action_enabled('edit'));
848 this.$el.find('.oe_kanban_action').click(function(ev) {
850 var $action = $(this),
851 type = $action.data('type') || 'button',
852 method = 'do_action_' + (type === 'action' ? 'object' : type);
853 if ((type === 'edit' || type === 'delete') && ! self.view.is_action_enabled(type)) {
854 self.view.open_record(self.id, true);
855 } else if (_.str.startsWith(type, 'switch_')) {
856 self.view.do_switch_view(type.substr(7));
857 } else if (typeof self[method] === 'function') {
858 self[method]($action);
860 self.do_warn("Kanban: no action for type : " + type);
864 if (this.$el.find('.oe_kanban_global_click,.oe_kanban_global_click_edit').length) {
865 this.$el.on('click', function(ev) {
866 if (!ev.isTrigger && !$._data(ev.target, 'events')) {
868 var elem = ev.target;
872 var events = $._data(elem, 'events');
873 if (elem == ev.currentTarget) {
878 if (events && events.click) {
879 // do not trigger global click if one child has a click event registered
883 if (trigger && events && events.click) {
884 _.each(events.click, function(click_event) {
885 if (click_event.selector) {
886 // For each parent of original target, check if a
887 // delegated click is bound to any previously found children
888 _.each(children, function(child) {
889 if ($(child).is(click_event.selector)) {
896 elem = elem.parentElement;
899 self.on_card_clicked(ev);
905 /* actions when user click on the block with a specific class
906 * open on normal view : oe_kanban_global_click
907 * open on form/edit view : oe_kanban_global_click_edit
909 on_card_clicked: function(ev) {
910 if(this.$el.find('.oe_kanban_global_click_edit').size()>0)
911 this.do_action_edit();
913 this.do_action_open();
915 setup_color_picker: function() {
917 var $el = this.$el.find('ul.oe_kanban_colorpicker');
919 $el.html(QWeb.render('KanbanColorPicker', {
922 $el.on('click', 'a', function(ev) {
924 var color_field = $(this).parents('.oe_kanban_colorpicker').first().data('field') || 'color';
926 data[color_field] = $(this).data('color');
927 self.view.dataset.write(self.id, data, {}).done(function() {
928 self.record[color_field] = $(this).data('color');
934 do_action_delete: function($action) {
937 return $.when(self.view.dataset.unlink([self.id])).done(function() {
938 self.group.remove_record(self.id);
942 if (this.view.options.confirm_on_delete) {
943 if (confirm(_t("Are you sure you want to delete this record ?"))) {
949 do_action_edit: function($action) {
950 this.view.open_record(this.id, true);
952 do_action_open: function($action) {
953 this.view.open_record(this.id);
955 do_action_object: function ($action) {
956 var button_attrs = $action.data();
957 this.view.do_execute_action(button_attrs, this.view.dataset, this.id, this.do_reload);
959 do_reload: function() {
961 this.view.dataset.read_ids([this.id], this.view.fields_keys.concat(['__last_update'])).done(function(records) {
962 if (records.length) {
963 self.set_record(records[0]);
964 self.renderElement();
965 self.$el.data('widget', self);
967 self.group.compute_cards_auto_height();
968 self.view.postprocess_m2m_tags();
974 kanban_getcolor: function(variable) {
976 switch (typeof(variable)) {
978 for (var i=0, ii=variable.length; i<ii; i++) {
979 index += variable.charCodeAt(i);
983 index = Math.round(variable);
988 var color = (index % this.view.number_of_color_schemes);
991 kanban_color: function(variable) {
992 var color = this.kanban_getcolor(variable);
993 return color === '' ? '' : 'oe_kanban_color_' + color;
995 kanban_gravatar: function(email, size) {
997 email = _.str.trim(email || '').toLowerCase();
998 var default_ = _.str.isBlank(email) ? 'mm' : 'identicon';
999 var email_md5 = $.md5(email);
1000 return 'http://www.gravatar.com/avatar/' + email_md5 + '.png?s=' + size + '&d=' + default_;
1002 kanban_image: function(model, field, id, cache, options) {
1003 options = options || {};
1005 if (this.record[field] && this.record[field].value && !instance.web.form.is_bin_size(this.record[field].value)) {
1006 url = 'data:image/png;base64,' + this.record[field].value;
1007 } else if (this.record[field] && ! this.record[field].value) {
1008 url = "/web/static/src/img/placeholder.png";
1010 id = JSON.stringify(id);
1011 if (options.preview_image)
1012 field = options.preview_image;
1013 url = this.session.url('/web/binary/image', {model: model, field: field, id: id});
1014 if (cache !== undefined) {
1015 // Set the cache duration in seconds.
1016 url += '&cache=' + parseInt(cache, 10);
1021 kanban_text_ellipsis: function(s, size) {
1025 } else if (s.length <= size) {
1028 return s.substr(0, size) + '...';
1031 kanban_compute_domain: function(domain) {
1032 return instance.web.form.compute_domain(domain, this.values);
1037 * Quick creation view.
1039 * Triggers a single event "added" with a single parameter "name", which is the
1040 * name entered by the user
1045 instance.web_kanban.QuickCreate = instance.web.Widget.extend({
1046 template: 'KanbanView.quick_create',
1049 * close_btn: If true, the widget will display a "Close" button able to trigger
1052 init: function(parent, dataset, context, buttons) {
1053 this._super(parent);
1054 this._dataset = dataset;
1055 this._buttons = buttons || false;
1056 this._context = context || {};
1058 start: function () {
1060 self.$input = this.$el.find('input');
1061 self.$input.keyup(function(event){
1062 if(event.keyCode == 13){
1066 $(".oe_kanban_quick_create_add", this.$el).click(function () {
1070 $(".oe_kanban_quick_create_close", this.$el).click(function (ev) {
1071 ev.preventDefault();
1072 self.trigger('close');
1074 self.$input.keyup(function(e) {
1075 if (e.keyCode == 27 && self._buttons) {
1076 self.trigger('close');
1081 this.$el.find('input').focus();
1084 * Handles user event from nested quick creation view
1086 quick_add: function () {
1088 var val = this.$input.val();
1089 if (/^\s*$/.test(val)) { return; }
1091 'name_create', [val, new instance.web.CompoundContext(
1092 this._dataset.get_context(), this._context)])
1093 .then(function(record) {
1094 self.$input.val("");
1095 self.trigger('added', record[0]);
1096 }, function(error, event) {
1097 event.preventDefault();
1098 return self.slow_create();
1101 slow_create: function() {
1103 var pop = new instance.web.form.SelectCreatePopup(this);
1105 self._dataset.model,
1107 title: _t("Create: ") + (this.string || this.name),
1108 initial_view: "form",
1109 disable_multiple_selection: true
1112 {"default_name": self.$input.val()}
1114 pop.on("elements_selected", self, function(element_ids) {
1115 self.$input.val("");
1116 self.trigger('added', element_ids[0]);
1122 // vim:et fdc=0 fdl=0 foldnestmax=3 fdm=syntax: