Taskpanel.js 2.82 KB
Ext.define('MyMA.controller.Taskpanel', {
    extend: 'Ext.app.Controller',
	stores: ['Programs'],
	refs: [{
		selector: 'taskpanel',
		ref: 'taskPanel'
	}],
	
    init: function() {
        this.control({
            'taskpanel > button': {
                toggle: this.programState
            }
        });
    },


	/**
	 * Add program to the task panel
	 * @param  data
	 */
	addProgram: function(data) {
		var data = data || {
			name: null
		},
            store = this.getStore('Programs'),
            record, widget;
        
		if(!(record = store.getProccess({
			property: 'name',
			value: data.name
		})))
        {
			try {
				var widget = Ext.widget(data.name),
                    progId = (store.max('id') || 0) + 1;
			}
			catch(e) {
				return false;
			}
			
			var Button = this.getTaskPanel().add({
				xtype: 'button',
				ui: 'program-button',
				height: 28,
				text: data.title || widget.title,
				enableToggle: true,
				pressed: true,
				programId: progId
			});
			
			record = store.add({
                id: progId,
				title: data.title || widget.title,
                name: data.name,
                state: 'show',
                item: widget.getId(),
                control: Button.getId()
            })[0];
			
			this.getController('Program').registerControl(data.name);
			
			widget.animateTarget = Button.getId();
			widget.setTitle(data.title);
			widget.show();
		}
		else {
			this.getTaskPanel().items.get(record.get('control')).toggle(true);
			if((widget = Ext.getCmp(record.get('item')))) {
				widget.show();
			}
		}

        return widget ? widget : null;
	},


	/**
	 * Get reference function
	 * @param {Object} Button
	 */
	callRef: function(selector) {
		var me = this;
		
		try {
			for (var i = 0, ln = this.refs.length; i < ln; i++) {
				if (this.refs[i]['selector'] == selector) {
					return me['get' + Ext.String.capitalize(this.refs[i]['ref'])]();
				}
			}
		}
		catch(e) { }
		
		return null;
	},
	
	
    programState: function(Button) {
        var store = this.getStore('Programs'),
            record, widget;
		
		if(!(record = store.getProccess({
			property: 'id', 
			value: Button.programId
        })))
		{
			return false;
		}
        
		if ((widget = Ext.getCmp(record.get('item')))) {
		    if(record.get('state') == 'show' && Ext.WindowManager.getActive() != widget) {
		        widget.toFront();
		        Ext.getCmp(record.get('control')).toggle(true, true);
		    }
		    else {
                widget[record.get('state') == 'show' ? 'hide' : 'show']();
			}
		}
    },
	
	
	/**
	 * Close all process those are registed in the Programs storage
	 */
	closeAll: function() {
	    var widgets = this.getStore('Programs').collect('item');
	    
	    Ext.Array.each(widgets, function(item) {
	        if ((widget = Ext.getCmp(item))) {
                widget.close();
            }
	    });
	}
});