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.

158 lines
5.8 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. init: function (viewInfo, params) {
  35. this._super.apply(this, arguments);
  36. var self = this;
  37. this.timeline = false;
  38. this.arch = viewInfo.arch;
  39. var attrs = this.arch.attrs;
  40. this.fields = viewInfo.fields;
  41. this.modelName = this.controllerParams.modelName;
  42. this.action = params.action;
  43. var fieldNames = this.fields.display_name ? ['display_name'] : [];
  44. var mapping = {};
  45. var fieldsToGather = [
  46. "date_start",
  47. "date_stop",
  48. "default_group_by",
  49. "progress",
  50. "date_delay",
  51. ];
  52. fieldsToGather.push(attrs.default_group_by);
  53. _.each(fieldsToGather, function (field) {
  54. if (attrs[field]) {
  55. var fieldName = attrs[field];
  56. mapping[field] = fieldName;
  57. fieldNames.push(fieldName);
  58. }
  59. });
  60. this.parse_colors();
  61. for (var i=0; i<this.colors.length; i++) {
  62. fieldNames.push(this.colors[i].field);
  63. }
  64. this.permissions = {};
  65. this.grouped_by = false;
  66. this.date_start = attrs.date_start;
  67. this.date_stop = attrs.date_stop;
  68. this.date_delay = attrs.date_delay;
  69. this.no_period = this.date_start === this.date_stop;
  70. this.zoomKey = attrs.zoomKey || '';
  71. this.mode = attrs.mode || attrs.default_window || 'fit';
  72. this.current_window = {
  73. start: new moment(),
  74. end: new moment().add(24, 'hours')
  75. };
  76. if (!isNullOrUndef(attrs.quick_create_instance)) {
  77. self.quick_create_instance = 'instance.' + attrs.quick_create_instance;
  78. }
  79. this.options = {
  80. groupOrder: this.group_order,
  81. orientation: 'both',
  82. selectable: true,
  83. showCurrentTime: true,
  84. zoomKey: this.zoomKey
  85. };
  86. if (isNullOrUndef(attrs.event_open_popup) || !_.str.toBoolElse(attrs.event_open_popup, true)) {
  87. this.open_popup_action = false;
  88. } else {
  89. this.open_popup_action = attrs.event_open_popup;
  90. }
  91. this.rendererParams.mode = this.mode;
  92. this.rendererParams.model = this.modelName;
  93. this.rendererParams.options = this.options;
  94. this.rendererParams.permissions = this.permissions;
  95. this.rendererParams.current_window = this.current_window;
  96. this.rendererParams.timeline = this.timeline;
  97. this.rendererParams.date_start = this.date_start;
  98. this.rendererParams.date_stop = this.date_stop;
  99. this.rendererParams.date_delay = this.date_delay;
  100. this.rendererParams.colors = this.colors;
  101. this.rendererParams.fieldNames = fieldNames;
  102. this.rendererParams.view = this;
  103. this.loadParams.modelName = this.modelName;
  104. this.loadParams.fieldNames = fieldNames;
  105. this.controllerParams.open_popup_action = this.open_popup_action;
  106. this.controllerParams.date_start = this.date_start;
  107. this.controllerParams.date_stop = this.date_stop;
  108. this.controllerParams.date_delay = this.date_delay;
  109. this.controllerParams.actionContext = this.action.context;
  110. return this;
  111. },
  112. group_order: function (grp1, grp2) {
  113. // display non grouped elements first
  114. if (grp1.id === -1) {
  115. return -1;
  116. }
  117. if (grp2.id === -1) {
  118. return +1;
  119. }
  120. return grp1.content - grp2.content;
  121. },
  122. parse_colors: function () {
  123. if (this.arch.attrs.colors) {
  124. this.colors = _(this.arch.attrs.colors.split(';')).chain().compact().map(function (color_pair) {
  125. var pair = color_pair.split(':'), color = pair[0], expr = pair[1];
  126. var temp = py.parse(py.tokenize(expr));
  127. return {
  128. 'color': color,
  129. 'field': temp.expressions[0].value,
  130. 'opt': temp.operators[0],
  131. 'value': temp.expressions[1].value
  132. };
  133. }).value();
  134. }
  135. },
  136. });
  137. view_registry.add('timeline', TimelineView);
  138. return TimelineView;
  139. });