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