Blame view

js/app/views/IntervalUI.js 8.97 KB
16035364   Benjamin Renard   First commit
1
2
3
4
5
6
7
8
/**
 * Project   : AMDA-NG
 * Name      : IntervalUI.js
 * @class 	 amdaUI.IntervalUI
 * @extends  Ext.container.Container
 * @brief    common component to select interval
 * @author 	 Benjamin
 * @version  $Id: IntervalUI.js 2077 2014-02-11 11:33:36Z elena $
b185823c   Nathanael Jourdane   Use IntervalUI mo...
9
 * @todo Validations
16035364   Benjamin Renard   First commit
10
11
12
13
14
 *****************************************************************************
 * FT Id     :   Date   : Name - Description
 ******************************************************************************
 */

5e85f9e6   Nathanael Jourdane   Add 'durationLimi...
15
16
17
18
/**
config:
- durationLimit: The maximum value of the duration days field (9999 by default).
*/
16035364   Benjamin Renard   First commit
19
20
Ext.define('amdaUI.IntervalUI', {
    extend: 'Ext.container.Container',
16035364   Benjamin Renard   First commit
21
    alias: 'widget.intervalSelector',
16035364   Benjamin Renard   First commit
22
    activeField : null,
b185823c   Nathanael Jourdane   Use IntervalUI mo...
23

16035364   Benjamin Renard   First commit
24
25
26
27
	constructor: function(config) {
		this.init(config);
		this.callParent(arguments);
	},
b185823c   Nathanael Jourdane   Use IntervalUI mo...
28
29

	/**
5e85f9e6   Nathanael Jourdane   Add 'durationLimi...
30
31
32
33
	Set the start and stop date, and update the duration field.
	- startDate: A Extjs Date object representing the new start time.
	- stopDate: A Extjs Date object representing the new stop time.
	- return: None.
b185823c   Nathanael Jourdane   Use IntervalUI mo...
34
	*/
5e85f9e6   Nathanael Jourdane   Add 'durationLimi...
35
	setInterval: function(startDate, stopDate) {
16035364   Benjamin Renard   First commit
36
		// get the search form
5e85f9e6   Nathanael Jourdane   Add 'durationLimi...
37
38
39
40
41
		var form = this.findParentByType('form').getForm();
		// get start field
		var startField = form.findField('startDate');
		// get stop field
		var stopField = form.findField('stopDate');
b185823c   Nathanael Jourdane   Use IntervalUI mo...
42

5e85f9e6   Nathanael Jourdane   Add 'durationLimi...
43
44
		if (startField != null)
			startField.setValue(startDate);
b185823c   Nathanael Jourdane   Use IntervalUI mo...
45

5e85f9e6   Nathanael Jourdane   Add 'durationLimi...
46
47
		if (stopField != null)
			stopField.setValue(stopDate);
b185823c   Nathanael Jourdane   Use IntervalUI mo...
48

5e85f9e6   Nathanael Jourdane   Add 'durationLimi...
49
		this.updateDuration();
16035364   Benjamin Renard   First commit
50
	},
b185823c   Nathanael Jourdane   Use IntervalUI mo...
51

5e85f9e6   Nathanael Jourdane   Add 'durationLimi...
52
53
54
	/**
	Set the limits values of both startField and stopField date fields.
	*/
72195d65   Nathanael Jourdane   Automatically set...
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
	setLimits: function(minValue, maxValue) {
		var form = this.findParentByType('form').getForm();
		var startField = form.findField('startDate');
		var stopField = form.findField('stopDate');

		if (startField != null) {
			startField.setMinValue(minValue);
			startField.setMaxValue(maxValue);
		}

		if (stopField != null) {
			stopField.setMinValue(minValue);
			stopField.setMaxValue(maxValue);
		}

		this.updateDuration();
	},

b185823c   Nathanael Jourdane   Use IntervalUI mo...
73
	/**
5e85f9e6   Nathanael Jourdane   Add 'durationLimi...
74
	Get the start time field value.
b97c59e9   Nathanael Jourdane   Provide to the us...
75
	- return: A Extjs Date object representing the start time (null if the date is not valid).
b185823c   Nathanael Jourdane   Use IntervalUI mo...
76
	*/
5e85f9e6   Nathanael Jourdane   Add 'durationLimi...
77
	getStartTime: function() {
16035364   Benjamin Renard   First commit
78
		// get the search form
5e85f9e6   Nathanael Jourdane   Add 'durationLimi...
79
80
81
		var form = this.findParentByType('form').getForm();
		// get start field
		var startField = form.findField('startDate');
b185823c   Nathanael Jourdane   Use IntervalUI mo...
82

5e85f9e6   Nathanael Jourdane   Add 'durationLimi...
83
		return startField.getValue();
16035364   Benjamin Renard   First commit
84
	},
b185823c   Nathanael Jourdane   Use IntervalUI mo...
85
86
87

	/**
		Get the stop time field value.
b97c59e9   Nathanael Jourdane   Provide to the us...
88
		- return: A Extjs Date object representing the stop time (null if the date is not valid).
b185823c   Nathanael Jourdane   Use IntervalUI mo...
89
	*/
16035364   Benjamin Renard   First commit
90
91
92
93
94
95
	getStopTime : function()
	{
		// get the search form
        var form = this.findParentByType('form').getForm();
        // get stop field
        var stopField = form.findField('stopDate');
b185823c   Nathanael Jourdane   Use IntervalUI mo...
96
97

        return stopField.getValue();
16035364   Benjamin Renard   First commit
98
	},
16035364   Benjamin Renard   First commit
99

b185823c   Nathanael Jourdane   Use IntervalUI mo...
100
101
102
103
104
	/*
		#### Private methods from here ####
	*/

    updateDuration: function() {
16035364   Benjamin Renard   First commit
105
106
107
        // get the search form
        var form = this.findParentByType('form').getForm();
        // get start value
b185823c   Nathanael Jourdane   Use IntervalUI mo...
108
        var start = this.getStartTime();
16035364   Benjamin Renard   First commit
109
        // get stop value
b185823c   Nathanael Jourdane   Use IntervalUI mo...
110
        var stop = this.getStopTime();
16035364   Benjamin Renard   First commit
111
112
113
114
115
116
117
        // if duration computable
        if (stop != null && start != null) {

            // compute offset
            var zoneOffset = stop.getTimezoneOffset() - start.getTimezoneOffset();
            // compute duration
            var diff = stop - start - zoneOffset*60000;
b185823c   Nathanael Jourdane   Use IntervalUI mo...
118

16035364   Benjamin Renard   First commit
119
120
            var durationDays = Math.floor(diff/86400000);
            // set all duration values
5e85f9e6   Nathanael Jourdane   Add 'durationLimi...
121
            form.findField('durationDay').setValue(Ext.String.leftPad(durationDays, ('' + this.durationLimit).length, '0'));
16035364   Benjamin Renard   First commit
122
123
124
            form.findField('durationHour').setValue(Ext.String.leftPad(Math.floor(diff/3600000 % 24),2,'0'));
            form.findField('durationMin').setValue(Ext.String.leftPad(Math.floor(diff/60000 % 60),2,'0'));
            form.findField('durationSec').setValue(Ext.String.leftPad(Math.floor(diff/1000 % 60),2,'0'));
b185823c   Nathanael Jourdane   Use IntervalUI mo...
125

5e85f9e6   Nathanael Jourdane   Add 'durationLimi...
126
127
            if (durationDays > this.durationLimit) {
                form.findField('durationDay').markInvalid('Maximum interval is ' + this.durationLimit + ' days!');
16035364   Benjamin Renard   First commit
128
            }
16035364   Benjamin Renard   First commit
129
130
131
132
133
134
135
136
137
        }

    },

    isValidDuration: function(){
        // get the time form
        var form = this.findParentByType('form').getForm();
        // get global validation status for duration fields
        return (
b185823c   Nathanael Jourdane   Use IntervalUI mo...
138
                form.findField('durationDay').isValid() && form.findField('durationHour').isValid()
16035364   Benjamin Renard   First commit
139
140
141
142
143
                && form.findField('durationMin').isValid() && form.findField('durationSec').isValid()
        );// return true if all duration fields are Valid false otherwise
    },

    updateStop: function() {
16035364   Benjamin Renard   First commit
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
        // get the time form
        var form = this.findParentByType('form').getForm();
        // get duration value
        var duration =  parseInt(form.findField('durationDay').getValue(),10)*86400 +
        parseInt(form.findField('durationHour').getValue(),10)*3600 +
        parseInt(form.findField('durationMin').getValue(),10)*60  +
        parseInt(form.findField('durationSec').getValue(),10);
        // get start value
        var start = form.findField('startDate').getValue();
        // compute stop value
        var stop = Ext.Date.add(start,Ext.Date.SECOND,duration);
        // set stop value into form
        form.findField('stopDate').setValue(stop);

    },
b185823c   Nathanael Jourdane   Use IntervalUI mo...
159
160
161
162
163
164
165
166
167
168
169
170
171
172

	onChangeStartField : function(field, newValue, oldValue)
	{
        if (field.isValid())  {
        // get the search form
            var form = this.findParentByType('form').getForm();
            // set to the stop datefield the newValue as minValue
            form.findField('stopDate').setMinValue(newValue);
            // if it's a user modification
            if (oldValue != null && this.activeField == 'start')  {
                // launch the update of duration fields
                this.updateDuration();
            }
    }
16035364   Benjamin Renard   First commit
173
	},
b185823c   Nathanael Jourdane   Use IntervalUI mo...
174
175
176
177
178
179

	onChangeStopField: function(field, newValue, oldValue) {
        if (field.isValid() && oldValue != null  && this.activeField == 'stop') {
            // launch the update of duration fields
            this.updateDuration();
        }
16035364   Benjamin Renard   First commit
180
    },
b185823c   Nathanael Jourdane   Use IntervalUI mo...
181

16035364   Benjamin Renard   First commit
182
183
184
185
186
	getDateField : function(fieldName,fieldText,fieldId,onChangeField)
	{
		return {
			layout: {type: 'hbox', align: 'middle'},
            items: [
b185823c   Nathanael Jourdane   Use IntervalUI mo...
187
188
189
190
191
192
193
194
195
                {
					xtype: 'datefield',
					name: fieldName,
					format: 'Y/m/d H:i:s',
					enforceMaxLength: true,
					maxLength: 19,
					fieldLabel: fieldText,
					labelAlign: 'right',
					labelWidth: 60,
16035364   Benjamin Renard   First commit
196
197
198
199
200
201
                    listeners: {
                        change: onChangeField,
                        focus: function(field) {
                            this.activeField = fieldId;
                        },
                        scope : this
b185823c   Nathanael Jourdane   Use IntervalUI mo...
202
203
204
                    }
                }
            ]
16035364   Benjamin Renard   First commit
205
206
        };
	},
b185823c   Nathanael Jourdane   Use IntervalUI mo...
207

16035364   Benjamin Renard   First commit
208
209
	getStartField : function()
	{
b185823c   Nathanael Jourdane   Use IntervalUI mo...
210
		return this.getDateField('startDate','Start Time','start', this.onChangeStartField);
16035364   Benjamin Renard   First commit
211
	},
b185823c   Nathanael Jourdane   Use IntervalUI mo...
212

16035364   Benjamin Renard   First commit
213
214
215
216
	getStopField : function()
	{
		return this.getDateField('stopDate','Stop Time','stop',this.onChangeStopField);
	},
b185823c   Nathanael Jourdane   Use IntervalUI mo...
217

16035364   Benjamin Renard   First commit
218
219
220
221
222
	getDurationField : function()
	{
            return {
            layout: {type: 'hbox', align: 'middle'},
            height: 45,
b185823c   Nathanael Jourdane   Use IntervalUI mo...
223
224
            defaults: {
                xtype: 'textfield', labelAlign: 'top', width: 30,
16035364   Benjamin Renard   First commit
225
226
227
228
229
230
231
                allowBlank: false, maxLength:2, enforceMaxLength : true,
                hideTrigger: true,
                regex: /^[0-9]([0-9])*$/i,
                listeners: {
                    change: function(field, newValue, oldValue){
                        if (this.isValidDuration() && oldValue != null && this.activeField == 'duration')  {
                            // launch the update of stop datefield
b185823c   Nathanael Jourdane   Use IntervalUI mo...
232
233
                            this.updateStop();
                        }
16035364   Benjamin Renard   First commit
234
235
                    },
                    focus: function(field) {
b185823c   Nathanael Jourdane   Use IntervalUI mo...
236
237
                        this.activeField = 'duration';
                    },
16035364   Benjamin Renard   First commit
238
                    scope : this
b185823c   Nathanael Jourdane   Use IntervalUI mo...
239
240
                }
            },
16035364   Benjamin Renard   First commit
241
242
243
            items:[
                { xtype: 'displayfield', labelWidth: 60, labelAlign: 'right', width: 60, fieldLabel: '<br>Duration'},
                { xtype: 'component', width: 5},
5e85f9e6   Nathanael Jourdane   Add 'durationLimi...
244
                { name: 'durationDay', fieldLabel: 'Days', width: 45, maxLength: ('' + this.durationLimit).length},
16035364   Benjamin Renard   First commit
245
246
247
                { xtype: 'component', width: 5},
                { name: 'durationHour', fieldLabel: 'Hrs'},
                { xtype: 'component', width: 5},
b185823c   Nathanael Jourdane   Use IntervalUI mo...
248
                { name: 'durationMin', fieldLabel: 'Mins'},
16035364   Benjamin Renard   First commit
249
250
251
252
253
                { xtype: 'component', width: 5},
                { name: 'durationSec', fieldLabel: 'Secs'}
            ]
        };
	},
b185823c   Nathanael Jourdane   Use IntervalUI mo...
254

16035364   Benjamin Renard   First commit
255
    init : function(config) {
5e85f9e6   Nathanael Jourdane   Add 'durationLimi...
256
257
		this.durationLimit = config.durationLimit == null ? 9999 : config.durationLimit; // Set duration limit to 9999 by default

16035364   Benjamin Renard   First commit
258
        var me = this;
b185823c   Nathanael Jourdane   Use IntervalUI mo...
259

16035364   Benjamin Renard   First commit
260
261
        var myConf = {
                    border: false,
b185823c   Nathanael Jourdane   Use IntervalUI mo...
262
                    plain: true,
16035364   Benjamin Renard   First commit
263
264
265
266
267
268
269
270
271
272
                    flex: 1,
                    layout: 'anchor',
                    defaults: { height : 30, xtype : 'container'},

                    items: [
                            me.getStartField(),
                            me.getStopField(),
                            me.getDurationField()
                        ]
        };
b185823c   Nathanael Jourdane   Use IntervalUI mo...
273
        Ext.apply (this , Ext.apply (arguments, myConf));
16035364   Benjamin Renard   First commit
274
275
	}
});