fix small error in PickerGridLayerCombo
[tine20] / tine20 / Tinebase / js / widgets / grid / PickerGridLayerCombo.js
1 /*
2  * Tine 2.0
3  *
4  * @license     http://www.gnu.org/licenses/agpl.html AGPL Version 3
5  * @author      Philipp Schüle <p.schuele@metaways.de>
6  * @copyright   Copyright (c) 2016 Metaways Infosystems GmbH (http://www.metaways.de)
7  *
8  */
9 Ext.ns('Tine.widgets.grid');
10
11 /**
12  * config grid panel
13  *
14  * @namespace   Tine.widgets.grid
15  * @class       Tine.widgets.grid.PickerGridLayerCombo
16  * @extends     Tine.widgets.grid.LayerCombo
17  */
18 Tine.widgets.grid.PickerGridLayerCombo = Ext.extend(Ext.ux.form.LayerCombo, {
19
20     hideButtons: false,
21     layerAlign: 'tr-br?',
22     minLayerWidth: 400,
23     layerHeight: 300,
24     allowBlur: true,
25
26     lazyInit: true,
27
28     formConfig: {
29         labelAlign: 'left',
30         labelWidth: 30
31     },
32
33     pickerGrid: null,
34     inEditor: true,
35     gridRecordClass: null,
36
37     initComponent: function () {
38         Tine.widgets.grid.PickerGridLayerCombo.superclass.initComponent.call(this);
39         this.store = new Ext.data.SimpleStore({
40             fields: this.gridRecordClass
41         });
42
43         this.on('beforecollapse', this.onBeforeCollapse, this);
44     },
45
46     getItems: function () {
47         this.pickerGrid = new Tine.widgets.grid.PickerGridPanel({
48             recordClass: this.gridRecordClass,
49             height: this.layerHeight - 40 || 'auto',
50             onStoreChange: Ext.emptyFn,
51             store: this.store
52         });
53
54         return [this.pickerGrid];
55     },
56
57     /**
58      * cancel collapse if ctx menu is shown
59      */
60     onBeforeCollapse: function () {
61         return this.pickerGrid
62             && (!this.pickerGrid.contextMenu || this.pickerGrid.contextMenu.hidden)
63             && !this.pickerGrid.editing;
64     },
65
66     /**
67      * @param {String} value
68      * @return {Ext.form.Field} this
69      */
70     setValue: function (value) {
71         var _ = window.lodash;
72
73         if (!value) {
74             value = [];
75         }
76         this.setStoreFromArray(value);
77         if (this.rendered) {
78             var titles = _.reduce(this.store.data.items, function(result, record) {
79                 return result.concat(record.getTitle());
80             }, []);
81             this.setRawValue(titles.join(', '));
82         }
83         this.currentValue = value;
84
85         // to string overwrite, to make sure record is changed.
86         Tine.Tinebase.common.assertComparable(this.currentValue);
87         return this;
88     },
89
90     afterRender: function () {
91
92         Tine.widgets.grid.PickerGridLayerCombo.superclass.afterRender.apply(this, arguments);
93         if (this.currentValue) {
94             this.setValue(this.currentValue);
95         }
96     },
97     /**
98      * sets values to innerForm (grid)
99      */
100     setFormValue: function (value) {
101         if (!value) {
102             value = [];
103         }
104
105         this.setStoreFromArray(value);
106     },
107
108     /**
109      * retrieves values from grid
110      *
111      * @returns {*|Array}
112      */
113     getFormValue: function () {
114         return this.getFromStoreAsArray();
115     },
116
117     /**
118      * get values from store (as array)
119      *
120      * @param {Array}
121      *
122      */
123     setStoreFromArray: function(data) {
124         //this.pickerGrid.getStore().clearData();
125         this.store.removeAll();
126
127         for (var i = data.length-1; i >=0; --i) {
128             var recordData = data[i],
129                 newRecord = new this.gridRecordClass(recordData);
130             this.store.insert(0, newRecord);
131         }
132     },
133
134     /**
135      * get values from store (as array)
136      *
137      * @return {Array}
138      *
139      */
140     getFromStoreAsArray: function() {
141         var result = [];
142         this.store.each(function(record) {
143             result.push(record.data);
144         }, this);
145
146         return result;
147     }
148 });
149
150 Ext.reg('tinepickergridlayercombo', Tine.widgets.grid.PickerGridLayerCombo);