|
| 1 | +odoo.define("base_custom_info.CustomInfoRenderer", function(require) { |
| 2 | + "use strict"; |
| 3 | + |
| 4 | + var BasicRenderer = require("web.BasicRenderer"); |
| 5 | + var field_registry = require("web.field_registry"); |
| 6 | + var core = require("web.core"); |
| 7 | + var qweb = core.qweb; |
| 8 | + |
| 9 | + var CustomInfoRenderer = BasicRenderer.extend({ |
| 10 | + init: function(parent, state, params) { |
| 11 | + params = _.defaults({}, params, { |
| 12 | + viewType: "custom_info", |
| 13 | + }); |
| 14 | + this._super(parent, state, params); |
| 15 | + if ( |
| 16 | + parent !== undefined && |
| 17 | + parent.mode === "edit" && |
| 18 | + params.mode === undefined |
| 19 | + ) { |
| 20 | + this.mode = "edit"; |
| 21 | + } |
| 22 | + this.recordWidgets = []; |
| 23 | + }, |
| 24 | + _getWidgetOptions: function(data) { |
| 25 | + var mode = this.mode; |
| 26 | + if (data.data.readonly) { |
| 27 | + mode = "readonly"; |
| 28 | + } |
| 29 | + var options = { |
| 30 | + attrs: { |
| 31 | + options: {}, |
| 32 | + modifiers: {}, |
| 33 | + }, |
| 34 | + mode: mode, |
| 35 | + viewType: this.viewType, |
| 36 | + }; |
| 37 | + if (data.data.required) { |
| 38 | + options.attrs.modifiers.required = true; |
| 39 | + } |
| 40 | + if (data.data.widget === "many2one") { |
| 41 | + options.attrs.options.no_create_edit = true; |
| 42 | + options.attrs.options.no_open = true; |
| 43 | + } |
| 44 | + return options; |
| 45 | + }, |
| 46 | + _renderView: function() { |
| 47 | + var self = this; |
| 48 | + var $table = $(qweb.render("base_custom_info.table")); |
| 49 | + var $body = $table.find("tbody"); |
| 50 | + this.$el.empty(); |
| 51 | + this.recordWidgets = []; |
| 52 | + $table.appendTo(this.$el); |
| 53 | + _.each(this.state.data, function(data) { |
| 54 | + var element = $( |
| 55 | + qweb.render("base_custom_info.item", { |
| 56 | + widget: self, |
| 57 | + data: data, |
| 58 | + }) |
| 59 | + ); |
| 60 | + var Widget = field_registry.get(data.data.widget); |
| 61 | + if (Widget !== undefined) { |
| 62 | + self._renderCustomInfoWidget(Widget, element, data); |
| 63 | + } |
| 64 | + element.appendTo($body); |
| 65 | + }); |
| 66 | + return this._super(); |
| 67 | + }, |
| 68 | + _renderCustomInfoWidget: function(Widget, element, data) { |
| 69 | + var options = this._getWidgetOptions(data); |
| 70 | + var widget = new Widget( |
| 71 | + this, |
| 72 | + "value_" + data.data.field_type, |
| 73 | + data, |
| 74 | + options |
| 75 | + ); |
| 76 | + this.recordWidgets.push(widget); |
| 77 | + this._registerModifiers(widget, data, element, _.pick(options, "mode")); |
| 78 | + var node = element.find(".result_data"); |
| 79 | + widget.appendTo(node); |
| 80 | + }, |
| 81 | + _onNavigationMove: function(ev) { |
| 82 | + var currentIndex = -1; |
| 83 | + if (ev.data.direction === "next") { |
| 84 | + currentIndex = this.recordWidgets.indexOf(ev.data.target || ev.target); |
| 85 | + if (currentIndex + 1 >= (this.recordWidgets || []).length) { |
| 86 | + return; |
| 87 | + } |
| 88 | + ev.stopPropagation(); |
| 89 | + this._activateNextCustomInfoWidget(currentIndex); |
| 90 | + } else if (ev.data.direction === "previous") { |
| 91 | + currentIndex = this.recordWidgets.indexOf(ev.data.target); |
| 92 | + if (currentIndex <= 0) { |
| 93 | + return; |
| 94 | + } |
| 95 | + ev.stopPropagation(); |
| 96 | + this._activatePreviousCustomInfoWidget(currentIndex); |
| 97 | + } |
| 98 | + }, |
| 99 | + _activateNextCustomInfoWidget: function(currentIndex) { |
| 100 | + currentIndex = (currentIndex + 1) % (this.recordWidgets || []).length; |
| 101 | + var activatedIndex = this._activateCustomInfoWidget(currentIndex, {inc: 1}); |
| 102 | + if (activatedIndex === -1) { |
| 103 | + // No widget have been activated, we should go to the edit/save buttons |
| 104 | + this.trigger_up("focus_control_button"); |
| 105 | + this.lastActivatedFieldIndex = -1; |
| 106 | + } else { |
| 107 | + this.lastActivatedFieldIndex = activatedIndex; |
| 108 | + } |
| 109 | + return this.lastActivatedFieldIndex; |
| 110 | + }, |
| 111 | + _activatePreviousCustomInfoWidget: function(currentIndex) { |
| 112 | + currentIndex = currentIndex |
| 113 | + ? currentIndex - 1 |
| 114 | + : (this.recordWidgets || []).length - 1; |
| 115 | + return this._activateCustomInfoWidget(currentIndex, {inc: -1}); |
| 116 | + }, |
| 117 | + _activateCustomInfoWidget: function(currentIndex, options) { |
| 118 | + options = options || {}; |
| 119 | + _.defaults(options, {inc: 1, wrap: false}); |
| 120 | + currentIndex = Math.max(0, currentIndex); // Do not allow negative currentIndex |
| 121 | + |
| 122 | + for (var i = 0; i < this.recordWidgets.length; i++) { |
| 123 | + var activated = this.recordWidgets[currentIndex].activate({ |
| 124 | + event: options.event, |
| 125 | + noAutomaticCreate: options.noAutomaticCreate || false, |
| 126 | + }); |
| 127 | + if (activated) { |
| 128 | + return currentIndex; |
| 129 | + } |
| 130 | + |
| 131 | + currentIndex += options.inc; |
| 132 | + if (currentIndex >= this.recordWidgets.length) { |
| 133 | + if (options.wrap) { |
| 134 | + currentIndex -= this.recordWidgets.length; |
| 135 | + } else { |
| 136 | + return -1; |
| 137 | + } |
| 138 | + } else if (currentIndex < 0) { |
| 139 | + if (options.wrap) { |
| 140 | + currentIndex += this.recordWidgets.length; |
| 141 | + } else { |
| 142 | + return -1; |
| 143 | + } |
| 144 | + } |
| 145 | + } |
| 146 | + return -1; |
| 147 | + }, |
| 148 | + }); |
| 149 | + |
| 150 | + return CustomInfoRenderer; |
| 151 | +}); |
0 commit comments