Blame view

js/app/views/IntervalUI.js 8.19 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
15
16
17
 *****************************************************************************
 * FT Id     :   Date   : Name - Description
 ******************************************************************************
 */


Ext.define('amdaUI.IntervalUI', {
    extend: 'Ext.container.Container',
16035364   Benjamin Renard   First commit
18
    alias: 'widget.intervalSelector',
16035364   Benjamin Renard   First commit
19
    activeField : null,
b185823c   Nathanael Jourdane   Use IntervalUI mo...
20

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

	/**
		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.
	*/
	setInterval : function(startDate, stopDate)
16035364   Benjamin Renard   First commit
33
34
35
36
37
38
39
	{
		// get the search form
        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...
40

16035364   Benjamin Renard   First commit
41
42
        if (startField != null)
        	startField.setValue(startDate);
b185823c   Nathanael Jourdane   Use IntervalUI mo...
43

16035364   Benjamin Renard   First commit
44
45
        if (stopField != null)
        	stopField.setValue(stopDate);
b185823c   Nathanael Jourdane   Use IntervalUI mo...
46

16035364   Benjamin Renard   First commit
47
48
        this.updateDuration();
	},
b185823c   Nathanael Jourdane   Use IntervalUI mo...
49
50
51
52
53

	/**
		Get the start time field value.
		- return: A Extjs Date object representing the start time.
	*/
16035364   Benjamin Renard   First commit
54
55
56
57
58
59
	getStartTime : function()
	{
		// get the search form
        var form = this.findParentByType('form').getForm();
        // get start field
        var startField = form.findField('startDate');
b185823c   Nathanael Jourdane   Use IntervalUI mo...
60

16035364   Benjamin Renard   First commit
61
62
        return startField.getValue();
	},
b185823c   Nathanael Jourdane   Use IntervalUI mo...
63
64
65
66
67

	/**
		Get the stop time field value.
		- return: A Extjs Date object representing the stop time.
	*/
16035364   Benjamin Renard   First commit
68
69
70
71
72
73
	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...
74
75

        return stopField.getValue();
16035364   Benjamin Renard   First commit
76
	},
16035364   Benjamin Renard   First commit
77

b185823c   Nathanael Jourdane   Use IntervalUI mo...
78
79
80
81
82
	/*
		#### Private methods from here ####
	*/

    updateDuration: function() {
16035364   Benjamin Renard   First commit
83
84
85
        // get the search form
        var form = this.findParentByType('form').getForm();
        // get start value
b185823c   Nathanael Jourdane   Use IntervalUI mo...
86
        var start = this.getStartTime();
16035364   Benjamin Renard   First commit
87
        // get stop value
b185823c   Nathanael Jourdane   Use IntervalUI mo...
88
        var stop = this.getStopTime();
16035364   Benjamin Renard   First commit
89
90
91
92
93
94
95
        // 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...
96

16035364   Benjamin Renard   First commit
97
98
            var durationDays = Math.floor(diff/86400000);
            // set all duration values
b185823c   Nathanael Jourdane   Use IntervalUI mo...
99
            form.findField('durationDay').setValue(Ext.String.leftPad(durationDays,4,'0'));
16035364   Benjamin Renard   First commit
100
101
102
            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...
103
104

            if (durationDays > 9999) {
16035364   Benjamin Renard   First commit
105
106
                form.findField('durationDay').markInvalid('Maximum interval is 9999 days!');
            }
16035364   Benjamin Renard   First commit
107
108
109
110
111
112
113
114
115
        }

    },

    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...
116
                form.findField('durationDay').isValid() && form.findField('durationHour').isValid()
16035364   Benjamin Renard   First commit
117
118
119
120
121
                && 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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
        // 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...
137
138
139
140
141
142
143
144
145
146
147
148
149
150

	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
151
	},
b185823c   Nathanael Jourdane   Use IntervalUI mo...
152
153
154
155
156
157

	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
158
    },
b185823c   Nathanael Jourdane   Use IntervalUI mo...
159

16035364   Benjamin Renard   First commit
160
161
162
163
164
	getDateField : function(fieldName,fieldText,fieldId,onChangeField)
	{
		return {
			layout: {type: 'hbox', align: 'middle'},
            items: [
b185823c   Nathanael Jourdane   Use IntervalUI mo...
165
166
167
168
169
170
171
172
173
                {
					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
174
175
176
177
178
179
                    listeners: {
                        change: onChangeField,
                        focus: function(field) {
                            this.activeField = fieldId;
                        },
                        scope : this
b185823c   Nathanael Jourdane   Use IntervalUI mo...
180
181
182
                    }
                }
            ]
16035364   Benjamin Renard   First commit
183
184
        };
	},
b185823c   Nathanael Jourdane   Use IntervalUI mo...
185

16035364   Benjamin Renard   First commit
186
187
	getStartField : function()
	{
b185823c   Nathanael Jourdane   Use IntervalUI mo...
188
		return this.getDateField('startDate','Start Time','start', this.onChangeStartField);
16035364   Benjamin Renard   First commit
189
	},
b185823c   Nathanael Jourdane   Use IntervalUI mo...
190

16035364   Benjamin Renard   First commit
191
192
193
194
	getStopField : function()
	{
		return this.getDateField('stopDate','Stop Time','stop',this.onChangeStopField);
	},
b185823c   Nathanael Jourdane   Use IntervalUI mo...
195

16035364   Benjamin Renard   First commit
196
197
198
199
200
	getDurationField : function()
	{
            return {
            layout: {type: 'hbox', align: 'middle'},
            height: 45,
b185823c   Nathanael Jourdane   Use IntervalUI mo...
201
202
            defaults: {
                xtype: 'textfield', labelAlign: 'top', width: 30,
16035364   Benjamin Renard   First commit
203
204
205
206
207
208
209
                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...
210
211
                            this.updateStop();
                        }
16035364   Benjamin Renard   First commit
212
213
                    },
                    focus: function(field) {
b185823c   Nathanael Jourdane   Use IntervalUI mo...
214
215
                        this.activeField = 'duration';
                    },
16035364   Benjamin Renard   First commit
216
                    scope : this
b185823c   Nathanael Jourdane   Use IntervalUI mo...
217
218
                }
            },
16035364   Benjamin Renard   First commit
219
220
221
222
223
224
225
            items:[
                { xtype: 'displayfield', labelWidth: 60, labelAlign: 'right', width: 60, fieldLabel: '<br>Duration'},
                { xtype: 'component', width: 5},
                { name: 'durationDay', fieldLabel: 'Days', width: 45, maxLength: 4},
                { xtype: 'component', width: 5},
                { name: 'durationHour', fieldLabel: 'Hrs'},
                { xtype: 'component', width: 5},
b185823c   Nathanael Jourdane   Use IntervalUI mo...
226
                { name: 'durationMin', fieldLabel: 'Mins'},
16035364   Benjamin Renard   First commit
227
228
229
230
231
                { xtype: 'component', width: 5},
                { name: 'durationSec', fieldLabel: 'Secs'}
            ]
        };
	},
b185823c   Nathanael Jourdane   Use IntervalUI mo...
232

16035364   Benjamin Renard   First commit
233
    init : function(config) {
16035364   Benjamin Renard   First commit
234
        var me = this;
b185823c   Nathanael Jourdane   Use IntervalUI mo...
235

16035364   Benjamin Renard   First commit
236
237
        var myConf = {
                    border: false,
b185823c   Nathanael Jourdane   Use IntervalUI mo...
238
                    plain: true,
16035364   Benjamin Renard   First commit
239
240
241
242
243
244
245
246
247
248
                    flex: 1,
                    layout: 'anchor',
                    defaults: { height : 30, xtype : 'container'},

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