Blame view

js/app/views/IntervalUI.js 8.43 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 $
10200969   Roipoussiere   Remove whitespace...
9
 * @todo Validations
16035364   Benjamin Renard   First commit
10
11
 */

5e85f9e6   Nathanael Jourdane   Add 'durationLimi...
12
13
14
15
/**
config:
- durationLimit: The maximum value of the duration days field (9999 by default).
*/
16035364   Benjamin Renard   First commit
16
Ext.define('amdaUI.IntervalUI', {
ecced9f7   Elena.Budnik   redmine #5395 and...
17
	extend: 'Ext.container.Container',
10200969   Roipoussiere   Remove whitespace...
18

ecced9f7   Elena.Budnik   redmine #5395 and...
19
20
	alias: 'widget.intervalSelector',
	activeField : null,
10200969   Roipoussiere   Remove whitespace...
21

16035364   Benjamin Renard   First commit
22
23
24
25
	constructor: function(config) {
		this.init(config);
		this.callParent(arguments);
	},
10200969   Roipoussiere   Remove whitespace...
26

b185823c   Nathanael Jourdane   Use IntervalUI mo...
27
	/**
ecced9f7   Elena.Budnik   redmine #5395 and...
28
29
30
31
		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...
32
	*/
16035364   Benjamin Renard   First commit
33
34
35
	setInterval : function(startDate,stopDate)
	{
		// get the search form
ecced9f7   Elena.Budnik   redmine #5395 and...
36
37
38
39
40
			var form = this.findParentByType('form').getForm();
			// get start field
			var startField = form.findField('startDate');
			// get stop field
			var stopField = form.findField('stopDate');
10200969   Roipoussiere   Remove whitespace...
41

ecced9f7   Elena.Budnik   redmine #5395 and...
42
43
			if (startField != null)
			startField.setValue(startDate);
10200969   Roipoussiere   Remove whitespace...
44

ecced9f7   Elena.Budnik   redmine #5395 and...
45
46
			if (stopField != null)
			stopField.setValue(stopDate);
10200969   Roipoussiere   Remove whitespace...
47

ecced9f7   Elena.Budnik   redmine #5395 and...
48
			this.updateDuration();
16035364   Benjamin Renard   First commit
49
	},
10200969   Roipoussiere   Remove whitespace...
50

ecced9f7   Elena.Budnik   redmine #5395 and...
51
52
	/**
		Set the limits values of both startField and stopField date fields.
5e85f9e6   Nathanael Jourdane   Add 'durationLimi...
53
	*/
72195d65   Nathanael Jourdane   Automatically set...
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
	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();
	},
ecced9f7   Elena.Budnik   redmine #5395 and...
71
72
73
74
75
76
77
78
79
80
81
82
83
84
	
	setStartTime: function(startTime) {
		var form = this.findParentByType('form').getForm();
		var startField = form.findField('startDate');
		startField.setValue(startTime);
		this.updateDuration();
	},
	
	setStopTime: function(stopTime) {
		var form = this.findParentByType('form').getForm();
		var stopField = form.findField('stopDate');
		stopField.setValue(stopTime);
		this.updateDuration();
	},
72195d65   Nathanael Jourdane   Automatically set...
85

ecced9f7   Elena.Budnik   redmine #5395 and...
86
	/**
30cd92df   Roipoussiere   Fix merge conflicts
87
88
		Get the start time field value.
		- return: A Extjs Date object representing the start time (null if the date is not valid).
b185823c   Nathanael Jourdane   Use IntervalUI mo...
89
	*/
16035364   Benjamin Renard   First commit
90
91
92
	getStartTime : function()
	{
		// get the search form
ecced9f7   Elena.Budnik   redmine #5395 and...
93
94
95
			var form = this.findParentByType('form').getForm();
			// get start field
			var startField = form.findField('startDate');
10200969   Roipoussiere   Remove whitespace...
96

ecced9f7   Elena.Budnik   redmine #5395 and...
97
			return startField.getValue();
16035364   Benjamin Renard   First commit
98
	},
10200969   Roipoussiere   Remove whitespace...
99

b185823c   Nathanael Jourdane   Use IntervalUI mo...
100
101
	/**
		Get the stop time field value.
b97c59e9   Nathanael Jourdane   Provide to the us...
102
		- return: A Extjs Date object representing the stop time (null if the date is not valid).
b185823c   Nathanael Jourdane   Use IntervalUI mo...
103
	*/
16035364   Benjamin Renard   First commit
104
105
106
	getStopTime : function()
	{
		// get the search form
ecced9f7   Elena.Budnik   redmine #5395 and...
107
108
109
			var form = this.findParentByType('form').getForm();
			// get stop field
			var stopField = form.findField('stopDate');
10200969   Roipoussiere   Remove whitespace...
110

ecced9f7   Elena.Budnik   redmine #5395 and...
111
			return stopField.getValue();
16035364   Benjamin Renard   First commit
112
	},
10200969   Roipoussiere   Remove whitespace...
113

b185823c   Nathanael Jourdane   Use IntervalUI mo...
114
115
116
	/*
		#### Private methods from here ####
	*/
ecced9f7   Elena.Budnik   redmine #5395 and...
117
118
119
120
121
122
123
124
125
	updateDuration: function() {
		// get the search form
		var form = this.findParentByType('form').getForm();
		// get start value
		var start = this.getStartTime();
		// get stop value
		var stop = this.getStopTime();
		// if duration computable
		if (stop != null && start != null) {
7847200d   Elena.Budnik   rm 8051
126
127
128
            if ( stop <= start ) {
                form.findField('stopDate').markInvalid('Stop Time  must be after Start Time');
            }
ecced9f7   Elena.Budnik   redmine #5395 and...
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
			// compute offset
			var zoneOffset = stop.getTimezoneOffset() - start.getTimezoneOffset();
			// compute duration
			var diff = stop - start - zoneOffset*60000;

			var durationDays = Math.floor(diff/86400000);
			// set all duration values
			form.findField('durationDay').setValue(durationDays);
			form.findField('durationHour').setValue(Math.floor(diff/3600000 % 24));
			form.findField('durationMin').setValue(Math.floor(diff/60000 % 60));
			form.findField('durationSec').setValue(Math.floor(diff/1000 % 60));

			if (durationDays > this.durationLimit) {
					form.findField('durationDay').markInvalid('Maximum interval is ' + this.durationLimit + ' days!');
			}
		} else {
			form.findField('durationDay').setValue('');
			form.findField('durationHour').setValue('');
			form.findField('durationMin').setValue('');
			form.findField('durationSec').setValue('');
		}
	},
16035364   Benjamin Renard   First commit
151

ecced9f7   Elena.Budnik   redmine #5395 and...
152
153
154
155
156
157
158
159
160
	isValidDuration: function(){
		var form = this.findParentByType('form').getForm();
		return (
					form.findField('durationDay').isValid() &&
					form.findField('durationHour').isValid() &&
					form.findField('durationMin').isValid() &&
					form.findField('durationSec').isValid()
		);
	},
0de2b2fd   Nathanael Jourdane   fix and improve i...
161

ecced9f7   Elena.Budnik   redmine #5395 and...
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
	updateStop: function() {
		var form = this.findParentByType('form').getForm();
		var start = form.findField('startDate').getValue();

		var d = form.findField('durationDay').getValue();
		var h = form.findField('durationHour').getValue();
		var m = form.findField('durationMin').getValue();
		var s = form.findField('durationSec').getValue();
		var duration = (d?d:0)*86400 + (h?h:0)*3600 + (m?m:0)*60 + (s?s:0)
		var stop = Ext.Date.add(start, Ext.Date.SECOND, duration);
		
		if (Ext.Date.isDST(stop) && !Ext.Date.isDST(start))
			stop = Ext.Date.add(start, Ext.Date.SECOND, duration-3600);
		
		if (!Ext.Date.isDST(stop) && Ext.Date.isDST(start))
			stop = Ext.Date.add(start, Ext.Date.SECOND, duration+3600);
		
		form.findField('stopDate').setValue(stop);
	},
10200969   Roipoussiere   Remove whitespace...
181
182
183

	onChangeStartField : function(field, newValue, oldValue)
	{
ecced9f7   Elena.Budnik   redmine #5395 and...
184
185
186
187
188
189
190
191
192
193
194
		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 (this.activeField == 'start')  {
				// launch the update of duration fields
				this.updateDuration();
			}
		}
16035364   Benjamin Renard   First commit
195
	},
10200969   Roipoussiere   Remove whitespace...
196

ecced9f7   Elena.Budnik   redmine #5395 and...
197
198
199
200
201
202
203
	onChangeStopField: function(field, newValue, oldValue)
	{
		if (field.isValid() && this.activeField == 'stop') {
			// launch the update of duration fields
			this.updateDuration();
		}
	},
10200969   Roipoussiere   Remove whitespace...
204

16035364   Benjamin Renard   First commit
205
206
207
	getDateField : function(fieldName,fieldText,fieldId,onChangeField)
	{
		return {
ecced9f7   Elena.Budnik   redmine #5395 and...
208
				xtype: 'datefield',
adcbf81b   Elena.Budnik   redmine 6828 - de...
209
			//	margin : '10 0 5 5', // (top, right, bottom, left).
4715a800   Elena.Budnik   redmine 6828 - la...
210
211
				name: fieldName, 
			//	width : 220,
ecced9f7   Elena.Budnik   redmine #5395 and...
212
				emptyText: 'YYYY/MM/DD hh:mm:ss',
4210d375   Elena.Budnik   redmine 6828 - In...
213
				tip: 'Date formatted as YYYY/MM/DD hh:mm:ss',
ecced9f7   Elena.Budnik   redmine #5395 and...
214
				format: 'Y/m/d H:i:s',
4210d375   Elena.Budnik   redmine 6828 - In...
215
216
			 	enforceMaxLength: true,
			 	maxLength: 19,
ecced9f7   Elena.Budnik   redmine #5395 and...
217
218
219
220
221
222
223
224
225
226
227
228
229
				fieldLabel: fieldText,
				labelAlign: 'left',
				labelWidth: 60,
				listeners: {
					change: onChangeField,
					focus: function(field) {
							this.activeField = fieldId;
					},
					render: function(c) {
							Ext.create('Ext.tip.ToolTip', { target: c.getEl(), html: c.tip });
					},
					scope : this
				}
ecced9f7   Elena.Budnik   redmine #5395 and...
230
		};
16035364   Benjamin Renard   First commit
231
	},
10200969   Roipoussiere   Remove whitespace...
232

16035364   Benjamin Renard   First commit
233
234
	getStartField : function()
	{
b185823c   Nathanael Jourdane   Use IntervalUI mo...
235
		return this.getDateField('startDate','Start Time','start', this.onChangeStartField);
16035364   Benjamin Renard   First commit
236
	},
10200969   Roipoussiere   Remove whitespace...
237

16035364   Benjamin Renard   First commit
238
239
	getStopField : function()
	{
ecced9f7   Elena.Budnik   redmine #5395 and...
240
		return this.getDateField('stopDate','Stop Time','stop', this.onChangeStopField);
16035364   Benjamin Renard   First commit
241
	},
10200969   Roipoussiere   Remove whitespace...
242

16035364   Benjamin Renard   First commit
243
244
	getDurationField : function()
	{
ecced9f7   Elena.Budnik   redmine #5395 and...
245
246
		return {
			layout: {type: 'hbox', align: 'middle'},
4715a800   Elena.Budnik   redmine 6828 - la...
247
		//	margin: 0,
ecced9f7   Elena.Budnik   redmine #5395 and...
248
249
250
251
252
253
254
255
256
257
258
			defaults: {
				xtype: 'numberfield',
				minValue: 0,
				maxLength: 2,
				enforceMaxLength: true,
				decimalPrecision: 0,
				allowExponential: false,
				autoStripChars: true,
				hideTrigger: true,
				labelAlign: 'left',
				width: 32,
d341347d   Elena.Budnik   Plot Mgr ameliora...
259
				margin : '0 0 0 5',
ecced9f7   Elena.Budnik   redmine #5395 and...
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
				listeners: {
					change: function(field, newValue, oldValue) {
						var form = this.findParentByType('form').getForm();
						var start = form.findField('startDate').getValue();
						if (this.isValidDuration() && start!=null && this.activeField == 'duration')  {
								this.updateStop();
						}
					},
					focus: function(field) {
						this.activeField = 'duration';
					},
					render: function(c) {
						Ext.create('Ext.tip.ToolTip', { target: c.getEl(), html: c.tip });
					},
					scope : this
				}
			},
			items:[
					{ name: 'durationDay',  emptyText: 'Days', tip: 'Days',    maxValue: 73000, maxLength: 5, fieldLabel: 'Duration', labelWidth: 60, width: 110},
					{ name: 'durationHour', emptyText: 'Hrs',  tip: 'Hours',   maxValue: 23},
					{ name: 'durationMin',  emptyText: 'Mins', tip: 'Minutes', maxValue: 59},
					{ name: 'durationSec',  emptyText: 'Secs', tip: 'Seconds', maxValue: 59}
			]
		};
16035364   Benjamin Renard   First commit
284
	},
10200969   Roipoussiere   Remove whitespace...
285

ecced9f7   Elena.Budnik   redmine #5395 and...
286
287
288
289
290
291
292
293
294
295
	init : function(config) 
	{
		this.durationLimit = config.durationLimit == null ? 73000 : config.durationLimit; // Set duration limit to 200 years by default

		var me = this;
		var myConf = {
			border: false,
			plain: true,
			flex: 1,
			layout: 'anchor',
adcbf81b   Elena.Budnik   redmine 6828 - de...
296
			defaults: { margin: '10 0 5 5', xtype : 'container'},
ecced9f7   Elena.Budnik   redmine #5395 and...
297
298
299
300
301
302
303
			items: [
				me.getStartField(),
				me.getStopField(),
				me.getDurationField()
			]
		};
		Ext.apply (this, Ext.apply(arguments, myConf));
16035364   Benjamin Renard   First commit
304
305
	}
});