Blame view

js/app/views/StatusGrid.js 7.18 KB
ce347c54   Erdogan Furkan   Done.
1
2
3
4
5
6

Ext.define('StatusColorEditor', {
    extend: 'Ext.grid.CellEditor',
    alias: 'widget.statuscoloreditor',

    requires: [
c85f1120   Erdogan Furkan   Merge amdadev + C...
7
        'amdaUI.ColorPicker'
ce347c54   Erdogan Furkan   Done.
8
9
10
    ],

    field: {
c85f1120   Erdogan Furkan   Merge amdadev + C...
11
        xtype: 'colorpicker'
ce347c54   Erdogan Furkan   Done.
12
13
14
15
    },
});


a19578d1   Erdogan Furkan   Status for parame...
16
Ext.define('amdaUI.StatusGrid', {
ce347c54   Erdogan Furkan   Done.
17
    extend: 'Ext.grid.Panel',
a19578d1   Erdogan Furkan   Status for parame...
18
    alias: 'widget.statusgrid',
a19578d1   Erdogan Furkan   Status for parame...
19
    type: '',
ce347c54   Erdogan Furkan   Done.
20

a19578d1   Erdogan Furkan   Status for parame...
21
22
    catFields:['value','label','color'],
    paramFields:['minVal','maxVal','label','color'],
ce347c54   Erdogan Furkan   Done.
23
    xtype:'grid',
a19578d1   Erdogan Furkan   Status for parame...
24
25
26
    cellEditing : null,
    isCat : null,
    statusStore: null,
ce347c54   Erdogan Furkan   Done.
27
28

    constructor: function(config) {
a19578d1   Erdogan Furkan   Status for parame...
29
30
31
        if(config.type == 'cat'){
            this.isCat = true;
            this.createStore(this.catFields);
ce347c54   Erdogan Furkan   Done.
32
        }
a19578d1   Erdogan Furkan   Status for parame...
33
34
35
36
37
38
39
        else{
            this.isCat = false;
            this.createStore(this.paramFields);
        }
        if(config.status != null )
            this.parseStatus(config.status);
        
ce347c54   Erdogan Furkan   Done.
40
41
42
		this.init(config);
		this.callParent();
	},
a19578d1   Erdogan Furkan   Status for parame...
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57

    createStore: function(fields){
        this.statusStore=Ext.create('Ext.data.Store', {
            fields: fields,
            data:{'items':[]},
            proxy: {
                type: 'memory',
                reader: {
                    type: 'json',
                    root: 'items'
                }
            }
        });
    },
    
ce347c54   Erdogan Furkan   Done.
58
59
    onAddClick: function(grid){
        // Create a model instance
a19578d1   Erdogan Furkan   Status for parame...
60
        if(this.isCat)
c85f1120   Erdogan Furkan   Merge amdadev + C...
61
            var rec = {'value':0, 'label':'', 'color':'#000000'};
a19578d1   Erdogan Furkan   Status for parame...
62
        else
c85f1120   Erdogan Furkan   Merge amdadev + C...
63
            var rec = {'minVal':0, 'maxVal':0, 'label':'', 'color':'#000000'};
ce347c54   Erdogan Furkan   Done.
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
        
        this.statusStore.insert(0, rec);
        this.cellEditing.startEditByPosition({
            row: 0, 
            column: 0
        });
    },
    clearStore: function(){
        this.statusStore.removeAll();
    },
    onRemoveClick: function(grid, rowIndex){
        this.statusStore.removeAt(rowIndex);
    },

    rgbToHex: function(rgbArray) {
        var hex = "";
        for (var i = 0; i < rgbArray.length; i++) {
            var hexValue = Number(rgbArray[i]).toString(16);
            if (hexValue.length < 2) {
            hexValue = "0" + hexValue;
            }
            hex += hexValue;
        }
        return hex.toUpperCase();
    },
    hexToRgb: function(hex) {
c85f1120   Erdogan Furkan   Merge amdadev + C...
90
91
92
        var r = parseInt(hex.substring(1,3), 16);
        var g = parseInt(hex.substring(3,5), 16);
        var b = parseInt(hex.substring(5,7), 16);
ce347c54   Erdogan Furkan   Done.
93
94
95
96
97
98
99
        return [r, g, b];
    },

    getStatusString: function() {
        var me = this;
        var data = this.statusStore.data.items.map(function(item) {
            var label = item.get('label');
ce347c54   Erdogan Furkan   Done.
100
            var color = item.get('color');
a19578d1   Erdogan Furkan   Status for parame...
101
102
103
104
105
            if (me.isCat)
                return item.get('value') + ' : ' + label + ' [' + me.hexToRgb(color).toString().replaceAll(",",", ") + ']';
            else
                return '['+item.get('minVal')+','+item.get('maxVal')+ '] : ' + label + ' [' + me.hexToRgb(color).toString().replaceAll(",",", ") + ']';
            
ce347c54   Erdogan Furkan   Done.
106
107
108
109
110
111
112
113
114
        });

        var outputString = data.join(' - ');
        return outputString;
    },
    
    parseStatus: function(inputStr) {
        var me = this;
        var items = inputStr.split(" - ");
ce347c54   Erdogan Furkan   Done.
115
116
117
        for (var i = 0; i < items.length; i++) {
            var parts = items[i].split(" : ");
            if(parts.length > 1){
a19578d1   Erdogan Furkan   Status for parame...
118
119
120
121
122
                var vals = {};
                if(me.isCat){
                    me.statusStore.insert(i,{
                        'value': parseInt(parts[0]),
                        'label': parts[1].split(" [")[0],
c85f1120   Erdogan Furkan   Merge amdadev + C...
123
                        'color': "#"+this.rgbToHex(parts[1].split(" [")[1].slice(0, -1).split(", "))
a19578d1   Erdogan Furkan   Status for parame...
124
125
126
127
128
129
130
131
                    });
                }
                else{
                    var minMaxVals = parts[0].split('[')[1].slice(0,-1).split(",");
                    me.statusStore.insert(i,{
                        'minVal':minMaxVals[0],
                        'maxVal':minMaxVals[1],
                        'label': parts[1].split(" [")[0],
c85f1120   Erdogan Furkan   Merge amdadev + C...
132
                        'color': "#"+this.rgbToHex(parts[1].split(" [")[1].slice(0, -1).split(", "))
a19578d1   Erdogan Furkan   Status for parame...
133
134
                    });
                }
ce347c54   Erdogan Furkan   Done.
135
136
137
            }
        } 
    },
a19578d1   Erdogan Furkan   Status for parame...
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
    getMinValColumn: function(){
        if (!this.isCat){
            return {text : 'Min Value', dataIndex:'minVal', width: 70, editor: {xtype:'numberfield', allowBlank:false,hideTrigger:true}, menuDisabled: true};
        }
    },
    getMaxValColumn: function(){
        if (!this.isCat) {
            return {text : 'Max Value', dataIndex:'maxVal', width: 70, editor:{xtype:'numberfield', allowBlank:false,hideTrigger:true},menuDisabled: true};
        }
    },
    getValueColumn: function(){
        if (this.isCat){
            return { text: 'Value',  dataIndex: 'value',width: 55, editor: {xtype:'numberfield',allowBlank:false, hideTrigger:true},menuDisabled: true};
        }
    },

    getLabelColumn: function(){
        return { text: 'Label', dataIndex: 'label', width: 55, editor: 'textfield',menuDisabled: true,};
    },
    getColorColumn: function(){
        return {
            text: 'Color',dataIndex: 'color', width: 40, menuDisabled: true,
            renderer: function(value) {
c85f1120   Erdogan Furkan   Merge amdadev + C...
161
162
                console.log(value);
                return '<div style="background-color:' + value + '; margin-left: auto; \
a19578d1   Erdogan Furkan   Status for parame...
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
                        margin-right: auto;width:15px;height:15px;"></div>';
            },
            editor: { xtype: 'statuscoloreditor' }
        };
    },

    getDeleteColumn:function(){
        return {
            xtype: 'actioncolumn', width: 25, sortable: false, menuDisabled: true,
            items: [{ iconCls: 'icon-delete', scope: this, handler: this.onRemoveClick }]
        };
    },

    getColumns: function(){
        if (this.isCat)
            return [ this.getValueColumn(), this.getLabelColumn(), this.getColorColumn(), this.getDeleteColumn()]
        else
            return [this.getMinValColumn(), this.getMaxValColumn(), this.getLabelColumn(), this.getColorColumn(), this.getDeleteColumn()]
    },
ce347c54   Erdogan Furkan   Done.
182
183

    init: function(config) {
a19578d1   Erdogan Furkan   Status for parame...
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
        var me = this;
        me.cellEditing = Ext.create('Ext.grid.plugin.CellEditing', {clicksToEdit: 1,
            listeners:{
                // Backgrounds of min and max cells become red if Max < Min
                edit:function(editor, context,e){
                    if(!me.isCat){
                        var color='';
                        if (context.record.get('maxVal') - context.record.get('minVal')< 0)
                            color= 'red';
                        else 
                            color='white';
                        for ( var i = 1; i<3; i++ )
                            Ext.get(context.row).down('td:nth(' + i + ')').setStyle('background-color',color);
                    }
                }
            }
        });
ce347c54   Erdogan Furkan   Done.
201
202
203
        Ext.apply(this,  {

            store:this.statusStore,
a19578d1   Erdogan Furkan   Status for parame...
204
            columns: this.getColumns(),
ce347c54   Erdogan Furkan   Done.
205
206
            selType: 'cellmodel',
            plugins: [this.cellEditing],
a19578d1   Erdogan Furkan   Status for parame...
207
            height: (this.isCat) ? 130 : 180,
ce347c54   Erdogan Furkan   Done.
208
209
210
211
            renderTo: Ext.getBody(),
            tbar: [
            {
                xtype:'text',
a19578d1   Erdogan Furkan   Status for parame...
212
                text :(me.isCat) ? 'Status Grid' : '',
ce347c54   Erdogan Furkan   Done.
213
214
215
216
217
218
219
220
221
222
223
            },'->',
            {
                iconCls: 'icon-add',
                text: 'New line',
                scope: this,
                handler: this.onAddClick
            }]
        });
    }

});