DefaultTreeGrid.js 6.58 KB
Ext.define('amdaUI.DefaultTreeGrid', {
    extend: 'Ext.tree.Panel',
    
    requires: [
        'Ext.data.*',
        'Ext.grid.*',
        'Ext.tree.*',
        'amdaModel.DefaultValuesModel',
        'amdaUI.ColorPicker'
    ],    
    xtype: 'tree-grid',
    
    reserveScrollbar: true,
    
    height: 300,
    useArrows: true,
    rootVisible: false,
    multiSelect: true,
    singleExpand: true,

    constructor: function() {
		this.init();
		this.callParent();
	},

    isHexCode: function(str) {
        return /^#[0-9a-fA-F]{6}$/.test(str);
    },
    init: function() {
        var me = this;
        me.cellEditing = Ext.create('Ext.grid.plugin.CellEditing', {clicksToEdit: 1,
            listeners:{
                beforeedit:function(editor, context,e){
                    if(!context.record.data.leaf)
                    {
                        return false;
                    }
                    var value = context.record.get(context.field);
                    if (me.isHexCode(value)) {
                        var colorPicker = Ext.create('amdaUI.ColorPicker', {
                            pickerField: context.field,
                            value: value,
                            listeners: {
                                select: function(picker, color) {
                                    context.record.set(context.field, '#' + color);
                                    me.getView().refresh();
                                }
                            }
                        });
                        editor.field = colorPicker;
                    }
                },
                afteredit:function(editor, context,e){
                    console.log(context.record.get('default'));
                }
            }
        });

        Ext.apply(this, {
            store: new Ext.data.TreeStore({
                model: amdaModel.DefaultValuesModel,
                proxy: {
                    type: 'direct',
                    directFn: AmdaAction.getDefaultValueTree   
				},
                folderSort: true
            }),
            plugins: [me.cellEditing],
            columns: [
                {
                    xtype: 'treecolumn', //this is so we know which column will show the tree
                    text: 'Parameters',
                    flex: 2,
                    sortable: true,
                    dataIndex: 'parameter',
                    renderer: function(value, metaData, record) {
                        if (record.get('modified')) {
                            return '<b style="color: blue;">'+value+'</b>';
                        }
                        return value;
                    }
                },{
                    xtype: 'gridcolumn',
                    getEditor: function(record) {
                        var grid = this.up().grid,
                            cellediting = grid.findPlugin('cellediting'),
                            editors = cellediting.editors,
                            editor = editors.getByKey(this.id),
                            fieldType;
                        
                        if (editor) {
                            // Do this to avoid memory leaks
                            editors.remove(editor);
                        }
                        var parentNode = record;
                        var parameter ="";
                        while (parentNode.parentNode) {
                            parentNode = parentNode.parentNode;
                            if(parentNode.get('parameter'))
                                parameter = parentNode.get('parameter')+'.'.concat(parameter);
                        }
                        option = amdaDefaultOptions[parameter+record.get('parameter')];
                        value = record.get('value');
                        store = null;
                        if(me.isHexCode(value)){
                            fieldType = 'colorpicker'
                        }
                        else if(value === 'true' || value == 'false'){
                            fieldType = 'combobox'
                            store= [[true, 'true'], [false, 'false']];
                        }
                        else{
                            fieldType = isNaN(parseFloat(record.get('value'))) ? 'textfield' : 'numberfield';
                        }

                        if (option && option.type){
                            fieldType = option.type;
                            var myStore = Ext.create('Ext.data.Store', {
                                fields: ['key', 'value'],
                                data: option.store
                            });
                            store = myStore;

                        }
                        return {
                            xtype: fieldType,
                            store: store,
                            editable: false, queryMode: 'local',
                            displayField: 'value',
                            valueField: 'key',
                            value: (fieldType=='combobox') ? value : null,
                            allowBlank: false
                        };
                    },
                    text: 'Values',
                    flex: 1,
                    sortable: true,
                    dataIndex: 'value',
                    align: 'center',
                    renderer: function(value, meta) {
                        if (me.isHexCode(value)){
                            value = value.substring(1, 7);
                            return '<div style="background-color:#' + value + '; margin-left: auto; \
                                margin-right: auto;width:15px;height:15px;"></div>';
                        }
                        else{
                            return value;
                        }
                    },
                },{
                    text: 'Reset',
                    width: 55,
                    menuDisabled: true,
                    xtype: 'actioncolumn',
                    tooltip: 'Reset to the default value',
                    align: 'center',
                    icon: 'js/resources/images/16x16/arrow_circle_double.png',
                    handler: function(grid, rowIndex, colIndex, actionItem, event, record, row) {
                        record.set('value', record.get('default'));
                    },
                    // Only leaf level tasks may be edited
                    isDisabled: function(view, rowIdx, colIdx, item, record) {
                        return !record.data.leaf || !record.data.modified;
                    }
                }
            ],
        });
    }
});