PromptArgsWin.js 5.33 KB
/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

Ext.define('amdaUI.PromptArgsWin', {
    extend: 'Ext.window.Window',

    fieldsForm: null,
    applyCallback: null,
    argslistStore: null,

    constructor: function(config) {
        this.init(config);
        this.callParent(arguments);
    },
    setStroreList:function(storeList){
      this.argslistStore =  storeList;
    },

    promptArgs : function(prompts, fn, scope) {

        var me = this;
        if (fn)
            this.applyCallback = Ext.Function.bind(fn, scope);
        else
            this.applyCallback = null;
        this.fieldsForm.removeAll();
        prompts.each(function(promptObj, index) {
            switch (promptObj.get('type')) {
                case 'float':
                    me.addFloatField(promptObj.get('prompt'), index, promptObj.get('default'));
                    break;
                case 'list':
                    me.addComboField(promptObj.get('prompt'), promptObj.get('subtype'), index, promptObj.get('default'));
                    break;
                case 'boolean':
                    me.addBooleanField(promptObj.get('prompt'), index, promptObj.get('default'));
                    break;
                case 'string':
                    me.addStringField(promptObj.get('prompt'), index, promptObj.get('default'));
                    break;
                default:
                    console.log('Unknown arg type: ' + promptObj.get('type'));
                    break;
            }
        });
        this.show();
    },

    addFloatField: function(label, index, defaultVal) {
        var field = this.fieldsForm.add({
            xtype: 'numberfield',
            name: 'arg' + index,
            fieldLabel: label,
            decimalPrecision : 20,
            allowBlank: false,
            hideTrigger: true,
            keyNavEnabled: false,
            mouseWheelEnabled: false
        });
	if (defaultVal != "") {
		defaultVal = parseFloat(defaultVal);
		if (!isNaN(defaultVal)) {
			field.setValue(defaultVal);
		}
	}
    },

    addComboField: function(label, listtype, index, defaultVal, argslistStore) {
        //var argslistStore = amdaUI.CalculatorUI.argslistStore;
        if (!this.argslistStore) {
            return;
        }

        var valuesStore = null;
        this.argslistStore.each(function(arglist) {
            if (arglist.get('id') == listtype) {
                valuesStore = arglist.valuesStore;
            }
        });
        if (!valuesStore) {
            return;
        }


	var listData = [];
	valuesStore.each(function (item) {
		listData.push({'key': item.get('key'), 'value': item.get('value'), 'info': item.get('info')});
	});

	var comboStore = Ext.create('Ext.data.Store', {
            fields: ['key', 'value', 'info'],
            data : listData
        });

        var field = this.fieldsForm.add({
             xtype: 'combo',
             name: 'arg' + index,
             fieldLabel: label,
             store: comboStore,
             queryMode: 'local',
             displayField: 'value',
             valueField: 'key',
             editable: false,
             listConfig: {
                 getInnerTpl: function() {
                     return '<div data-qtip="{info}">{value} </div>';
                 }
             }
        });

        if (defaultVal != "") {
            field.setValue(defaultVal);
        }
    },

    addBooleanField: function(label, index, defaultVal) {
        var field = this.fieldsForm.add({
            xtype: 'checkbox',
            name: 'arg' + index,
            boxLabel: label,
            inputValue: '1',
            uncheckedValue: '0'
        });

        if (defaultVal != "") {
            field.setValue(defaultVal != '0');
        }
    },

    addStringField: function(label, index, defaultVal) {
        var field = this.fieldsForm.add({
            xtype: 'textfield',
            name: 'arg' + index,
            fieldLabel: label
        });

        if (defaultVal != "") {
            field.setValue(defaultVal);
        }
    },

    init : function(config) {
        this.fieldsForm = Ext.create('Ext.form.Panel', {
            layout: {
                type: 'vbox',
                pack: 'start',
                align: 'stretch',
                defaultMargins: {top: 5, right: 5, bottom: 0, left: 5}
            }
        });

        var myConf = {
            layout: 'fit',
            title: 'Function arguments',
            modal: true,
            closeAction: 'hide',
            width: 300,
            height: 200, 
            items: [
                this.fieldsForm
            ],
            buttons: [
                {
                    text: 'Apply',
                    scope : this,
                    handler: function(bt,event) {
                        if (this.applyCallback)
                            this.applyCallback(this.fieldsForm.getValues());
                        this.close();
                    },
                },
                {
                     text: 'Cancel',
                     scope : this,
                     handler: function(bt,event) {
                         this.close();
                     }
                }
            ] 
        };
        Ext.apply(this, Ext.apply(arguments, myConf));
    }
});