BaseTimeSelection.js 3.55 KB
Ext.define('treps.controller.Times.BaseTimeSelection', {
	extend: 'Ext.app.Controller',

	requires: [
		'treps.controller.Times.TimesManager'
	],

	containerSelector : '',

	onBeforeSetTime: null,

	onAfterSetTime: null,

        init: function() {
                var me = this;

		var panelSelector  = this.containerSelector + ' > times_panel';
		var headerSelector = panelSelector + ' > header';
		var checkSelector  = headerSelector + ' > checkbox';
		var formatSelector = panelSelector + ' > combo[name=timeformat]';
		var timeSelector   = panelSelector + ' > textfield[name=timepattern]';

		this.ref(
			[
				{
                        		ref: 'timeDefPanel',
                        		selector: panelSelector 
                		},
				{
					ref: 'timeDefHeader',
					selector: headerSelector
				},
				{
					ref: 'timeDefCheck',
					selector: checkSelector
				},
				{
					ref: 'timeDefFormat',
					selector: formatSelector
				},
				{
					ref: 'timeDefPattern',
					selector: timeSelector
				}
			]
		);

		var c = {};
		c[checkSelector] = {
			change : me.onChangeCheckBox,
			scope : me
		};
		c[formatSelector] = {
			change : me.onChangeFormat,
			scope : me
		};
		this.control(c);
        },

	initStore: function(onReady)
	{
		var me = this;

		treps.controller.Times.TimesManager.loadTimesStore(
			function(store)
			{
				me.getTimeDefFormat().store = store;
				if (onReady != null)
					onReady.call(me, store);
                        });
	},

	onChangeCheckBox: function(check, newValue, oldValue, eOpts)
	{
		if (!newValue)
			this.setTime("");
		else
		{
			var timeformat = this.getTimeFormat();
			this.setTime(timeformat);
		}
	},

	onChangeFormat: function(combo, newValue, oldValue, eOpts)
	{
		var pattern = this.getTimeDefPattern();
		pattern.setDisabled((this.getTimeFormat() != "pattern"));
	},

	setTime: function(format)
	{
		var me = this;

		var enabled = (format != "");

		if (me.onBeforeSetTime != null)
			enabled = me.onBeforeSetTime(enabled);

		var panel = me.getTimeDefPanel();
		var check = me.getTimeDefCheck();
		var combo = me.getTimeDefFormat();
		var pattern = me.getTimeDefPattern();

		if (check != null)
		{
				check.suspendEvents();
				check.setValue(enabled);
				check.resumeEvents();
		}
		else
			panel.setDisabled(!enabled);

		combo.setFieldStyle('background-color: '+treps.Constants.TIME_COLOR+';');
		combo.setDisabled(!enabled);
		if (enabled)
			combo.setValue(format);

		if (me.onAfterSetTime != null)
			me.onAfterSetTime(enabled);
	},

	selectFirst: function()
	{
		var combo = this.getTimeDefFormat();

		var format = treps.controller.Times.TimesManager.getFirstTimeId();

		combo.setValue(format);
	},

	isDefined: function()
	{
		var me = this;

		if (me.getTimeDefPanel().isDisabled())
			return false;

		var check = me.getTimeDefCheck();

		if (check == null)
			return true;

		return check.getValue();
	},

	isValid: function()
	{
		var me = this;

		if (!this.isDefined())
			return true;

		var combo = me.getTimeDefFormat();

		if (combo.getValue() == '')
		{
			treps.Messages.showError('Please select the time format.');
			return false;
		}

		var pattern = me.getTimeDefPattern();
		if ((combo.getValue() == 'pattern') && (pattern.getValue() == ''))
		{
			treps.Messages.showError("Please define the time pattern.");
			return false;
		}

		return true;
	},

	getTimeFormat: function()
	{
		if (!this.isDefined())
			return "";
		return this.getTimeDefFormat().getValue();
	},

	getTimePattern: function()
	{
		var format = this.getTimeFormat();

		if (format != 'pattern')
			return '';
		return this.getTimeDefPattern().getValue();
	}
});