You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

200 lines
7.1 KiB

  1. /* Odoo web_timeline
  2. * Copyright 2015 ACSONE SA/NV
  3. * Copyright 2016 Pedro M. Baeza <pedro.baeza@tecnativa.com>
  4. * License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). */
  5. _.str.toBoolElse = function (str, elseValues, trueValues, falseValues) {
  6. var ret = _.str.toBool(str, trueValues, falseValues);
  7. if (_.isUndefined(ret)) {
  8. return elseValues;
  9. }
  10. return ret;
  11. };
  12. odoo.define('web_timeline.TimelineView', function (require) {
  13. "use strict";
  14. var core = require('web.core');
  15. var view_registry = require('web.view_registry');
  16. var AbstractView = require('web.AbstractView');
  17. var TimelineRenderer = require('web_timeline.TimelineRenderer');
  18. var TimelineController = require('web_timeline.TimelineController');
  19. var TimelineModel = require('web_timeline.TimelineModel');
  20. var _lt = core._lt;
  21. function isNullOrUndef(value) {
  22. return _.isUndefined(value) || _.isNull(value);
  23. }
  24. var TimelineView = AbstractView.extend({
  25. display_name: _lt('Timeline'),
  26. icon: 'fa-clock-o',
  27. jsLibs: ['/web_timeline/static/lib/vis/vis-timeline-graph2d.min.js'],
  28. cssLibs: ['/web_timeline/static/lib/vis/vis-timeline-graph2d.min.css'],
  29. config: {
  30. Model: TimelineModel,
  31. Controller: TimelineController,
  32. Renderer: TimelineRenderer,
  33. },
  34. /**
  35. * @constructor
  36. * @override
  37. */
  38. init: function (viewInfo, params) {
  39. this._super.apply(this, arguments);
  40. var self = this;
  41. this.timeline = false;
  42. this.arch = this.rendererParams.arch;
  43. var attrs = this.arch.attrs;
  44. this.fields = viewInfo.fields;
  45. this.modelName = this.controllerParams.modelName;
  46. this.action = params.action;
  47. var fieldNames = this.fields.display_name ? ['display_name'] : [];
  48. var mapping = {};
  49. var fieldsToGather = [
  50. "date_start",
  51. "date_stop",
  52. "default_group_by",
  53. "progress",
  54. "date_delay",
  55. ];
  56. fieldsToGather.push(attrs.default_group_by);
  57. _.each(fieldsToGather, function (field) {
  58. if (attrs[field]) {
  59. var fieldName = attrs[field];
  60. mapping[field] = fieldName;
  61. fieldNames.push(fieldName);
  62. }
  63. });
  64. var archFieldNames = _.map(_.filter(this.arch.children, function(item) {
  65. return item.tag === 'field';
  66. }), function(item) {
  67. return item.attrs.name;
  68. });
  69. fieldNames = _.union(
  70. fieldNames,
  71. archFieldNames
  72. );
  73. this.parse_colors();
  74. for (var i=0; i<this.colors.length; i++) {
  75. fieldNames.push(this.colors[i].field);
  76. }
  77. if (attrs.dependency_arrow) {
  78. fieldNames.push(attrs.dependency_arrow);
  79. }
  80. this.permissions = {};
  81. this.grouped_by = false;
  82. this.date_start = attrs.date_start;
  83. this.date_stop = attrs.date_stop;
  84. this.date_delay = attrs.date_delay;
  85. this.dependency_arrow = attrs.dependency_arrow;
  86. this.no_period = this.date_start === this.date_stop;
  87. this.zoomKey = attrs.zoomKey || '';
  88. this.margin = attrs.margin || '{}';
  89. this.mode = attrs.mode || attrs.default_window || 'fit';
  90. this.min_height = attrs.min_height || 300;
  91. this.current_window = {
  92. start: new moment(),
  93. end: new moment().add(24, 'hours')
  94. };
  95. if (!isNullOrUndef(attrs.quick_create_instance)) {
  96. self.quick_create_instance = 'instance.' + attrs.quick_create_instance;
  97. }
  98. this.stack = true;
  99. if (!isNullOrUndef(attrs.stack) && !_.str.toBoolElse(attrs.stack, "true")) {
  100. this.stack = false;
  101. }
  102. this.options = {
  103. groupOrder: this.group_order,
  104. orientation: 'both',
  105. selectable: true,
  106. multiselect: true,
  107. showCurrentTime: true,
  108. stack: this.stack,
  109. margin: JSON.parse(this.margin),
  110. zoomKey: this.zoomKey
  111. };
  112. if (isNullOrUndef(attrs.event_open_popup) || !_.str.toBoolElse(attrs.event_open_popup, true)) {
  113. this.open_popup_action = false;
  114. } else {
  115. this.open_popup_action = attrs.event_open_popup;
  116. }
  117. this.rendererParams.mode = this.mode;
  118. this.rendererParams.model = this.modelName;
  119. this.rendererParams.options = this.options;
  120. this.rendererParams.permissions = this.permissions;
  121. this.rendererParams.current_window = this.current_window;
  122. this.rendererParams.timeline = this.timeline;
  123. this.rendererParams.date_start = this.date_start;
  124. this.rendererParams.date_stop = this.date_stop;
  125. this.rendererParams.date_delay = this.date_delay;
  126. this.rendererParams.colors = this.colors;
  127. this.rendererParams.fieldNames = fieldNames;
  128. this.rendererParams.view = this;
  129. this.rendererParams.min_height = this.min_height;
  130. this.rendererParams.dependency_arrow = this.dependency_arrow;
  131. this.loadParams.modelName = this.modelName;
  132. this.loadParams.fieldNames = fieldNames;
  133. this.controllerParams.open_popup_action = this.open_popup_action;
  134. this.controllerParams.date_start = this.date_start;
  135. this.controllerParams.date_stop = this.date_stop;
  136. this.controllerParams.date_delay = this.date_delay;
  137. this.controllerParams.actionContext = this.action.context;
  138. return this;
  139. },
  140. /**
  141. * Order function for groups.
  142. */
  143. group_order: function (grp1, grp2) {
  144. // display non grouped elements first
  145. if (grp1.id === -1) {
  146. return -1;
  147. }
  148. if (grp2.id === -1) {
  149. return +1;
  150. }
  151. return grp1.content.localeCompare(grp2.content);
  152. },
  153. /**
  154. * Parse the colors attribute.
  155. *
  156. * @private
  157. */
  158. parse_colors: function () {
  159. if (this.arch.attrs.colors) {
  160. this.colors = _(this.arch.attrs.colors.split(';')).chain().compact().map(function (color_pair) {
  161. var pair = color_pair.split(':'), color = pair[0], expr = pair[1];
  162. var temp = py.parse(py.tokenize(expr));
  163. return {
  164. 'color': color,
  165. 'field': temp.expressions[0].value,
  166. 'opt': temp.operators[0],
  167. 'value': temp.expressions[1].value
  168. };
  169. }).value();
  170. } else {
  171. this.colors = [];
  172. }
  173. },
  174. });
  175. view_registry.add('timeline', TimelineView);
  176. return TimelineView;
  177. });