source: TI03-DataExtractor/trunk/pydxc/jsFunctions.py @ 1160

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/TI03-DataExtractor/trunk/pydxc/jsFunctions.py@1160
Revision 1160, 12.1 KB checked in by astephen, 13 years ago (diff)

A number of minor bug-fixes including full time validation in client.

Line 
1#   Copyright (C) 2004 CCLRC & NERC( Natural Environment Research Council ).
2#   This software may be distributed under the terms of the
3#   Q Public License, version 1.0 or later. http://ndg.nerc.ac.uk/public_docs/QPublic_license.txt
4
5"""
6jsFunctions.py
7===========
8
9Holder module for Javascript functions that will be inserted into the header
10of relevant web pages for checking purposes.
11
12"""
13
14# js_dateCheckFunctionGroup - a string containing a list of javascript
15# functions for checking date/time fields on a web form. These are
16# inserted into HTML output by the UserInterface class in the userInterface
17# module.
18
19js_timeValidationFunctions="""
20<script language="javascript">
21
22
23/* Set of time validation functions
24   --------------------------------
25
26Rules for each set of low and high time fields are:
27
281. high>=start
292. start<=end
303. end<=max
314. start>=min
325. Cannot do check every change because this would not allow certain selections (as we've found with some datasets). Therefore allow validate via a button or link for each of the time selections.
336. Allow a validateSelections() and force it before submitting.
347. if end<start: end=start
358. if end>max: end=max
369. if start<min: start=min
3710. Add also a "Set both to start" and "Set both to end" buttons or links.
3811. -360<=longitude<=360
3912. -90<=latitude<=90
40
41Decided not to risk using built-in javascript Date() object just in case
42it couldn't handle historical years.
43*/
44
45/* The following will be converted by python to something like:
46var minNames=["axis_1.1.1.1", "axis_2.1.1.1"];
47var maxNames=minNames;
48var minValues=[[1999,4,3,12,0,0], [2004,6,6,0,0,0]];
49var maxValues=[[2006,6,6,6,0,0], [2005,5,5,0,0,0]];
50*/
51
52var minNames=%s;
53var maxNames=minNames;
54var minValues=%s;
55var maxValues=%s;
56
57function getByIndex(arrayObj, item) {
58    // returns the index of item in arrayObj
59    for (var i=0; i<arrayObj.length; i++) {
60        if (item==arrayObj[i]) {
61            return i;
62        }
63    }
64    return -1;
65}
66
67function isEarlierThan(t1, t2) {
68    // returns true if t1 is earlier than t2
69    for (var i=0; i<t1.length; i++) {
70        if (t1[i]<t2[i]) {
71            return true;
72        } else if (t1[i]>t2[i]) {
73            return false;
74        }
75    }
76    return false;
77}
78
79function isLaterThan(t1, t2) {
80    // returns true if t1 is later than t2
81    for (var i=0; i<t1.length; i++) {
82        if (t1[i]>t2[i]) {
83            return true;
84        } else if (t1[i]<t2[i]) {
85            return false;
86        }
87    }
88    return false;
89}
90
91function setTimeSelections(itemName, timeArray) {
92    // Sets the values in the select item to those in timeArray
93    var mainForm=document.forms["extract_info"];
94    var t=timeArray;
95    var timeComps=["year", "month", "day", "hour", "minute", "second"];
96    for (var i=0; i<t.length; i++) {
97        var item=mainForm[itemName+timeComps[i]];
98        var valueToSelect=timeArray[i];
99       
100        for (var x=0; x<item.length ; x++) {
101            if (parseInt(item[x].text)==valueToSelect) {
102                item[x].selected=true;
103            }
104        }
105    }
106}
107
108function validateTimeSelections(arg) {
109        // validates the specified time selections
110        var mainForm=document.forms["extract_info"];
111        var axis=arg;
112        var unchanged=true;
113        // select items named as follows...NAME="axis_1.1.1.1_high.time.day"
114        var axisLow=arg+"_low.time.";
115        var axisHigh=arg+"_high.time.";
116        var timeComps=["year", "month", "day", "hour", "minute", "second"];
117        var lowTime=new Array(6);
118        var highTime=new Array(6);
119       
120        for (var i=0; i<timeComps.length; i++) {
121            lowTime[i]=parseInt(mainForm[axisLow+timeComps[i]].value);
122            highTime[i]=parseInt(mainForm[axisHigh+timeComps[i]].value);
123        }
124
125        var maxTime=maxValues[getByIndex(maxNames, arg)];
126        var minTime=minValues[getByIndex(minNames, arg)];
127
128        if (isEarlierThan(lowTime, minTime)==true) {  lowTime=minTime; unchanged=false; }
129        if (isEarlierThan(highTime, minTime)==true) { highTime=minTime; unchanged=false; }
130        if (isLaterThan(lowTime, maxTime)==true) {    lowTime=maxTime; unchanged=false; }
131        if (isLaterThan(highTime, maxTime)==true) {   highTime=maxTime; unchanged=false; }
132        if (isEarlierThan(highTime, lowTime)==true) { highTime=lowTime; unchanged=false; }
133
134        setTimeSelections(axisHigh, highTime);
135        setTimeSelections(axisLow, lowTime);
136       
137        // Return true if nothing changed and false if changes made
138        return unchanged;
139}
140
141function validateAllTimeSelections() {
142    // validates all time selections
143    var mainForm=document.forms["extract_info"];
144    var axisNames=minNames;
145    for (var i=0; i<axisNames.length; i++) {
146        axName=axisNames[i];
147        if (validateTimeSelections(axName)==false) {
148            alert("Axis '"+axName+"' held an invalid entry and has been modified. Please check and re-submit.");
149            return false;
150        }
151    }
152    return true;
153}
154
155function validateSelections(arg) {
156    // Validates the specified selections
157    // arg="don't submit";
158
159    if (arg=="don't submit") { return false; }
160    if (arg=="time") {
161        return validateTimeSelections("all");
162    }
163    return false;
164}
165
166</script>
167"""
168
169js_selectAllCheckBoxes="""
170<SCRIPT LANGUAGE="Javascript">
171
172// Set up global variables for all functions
173var frm=document.forms["extract_info"];
174
175function selectAllCheckBoxes() {
176    for (var i=0; i<frm.elements.length; i++) {
177        if (frm.elements[i].type=="checkbox") {
178            frm.elements[i].checked=true;
179        }
180    }
181}
182
183</SCRIPT>
184"""
185
186js_deSelectAllCheckBoxes="""
187<SCRIPT LANGUAGE="Javascript">
188
189// Set up global variables for all functions
190var frm=document.forms["extract_info"];
191
192function deSelectAllCheckBoxes() {
193    for (var i=0; i<frm.elements.length; i++) {
194        if (frm.elements[i].type=="checkbox") {
195            frm.elements[i].checked=false;
196        }
197    }
198}
199
200</SCRIPT>
201"""
202
203js_dateCheckFunctionGroup_DEPRECATED="""
204<SCRIPT LANGUAGE="Javascript">
205
206// Set up global variables for all functions
207var frm=document.forms["extract_info"];
208var dateMinimum_1 = new Array(%s, %s, %s, %s, %s, %s);
209var dateMaximum_1 = new Array(%s, %s, %s, %s, %s, %s);
210var dateMinimum_2 = new Array(%s, %s, %s, %s, %s, %s);
211var dateMaximum_2 = new Array(%s, %s, %s, %s, %s, %s);
212
213function getFormFields(arg) {
214    // getFormFields - returns an array of form fields to manipulate based on the argument given.
215    if (arg == "start_1") {
216        var fields = new Array(frm.startYear_1, frm.startMonth_1, frm.startDay_1, frm.startHour_1, frm.startMinute_1, frm.startSecond_1);
217    } else if (arg == "end_1") {
218        var fields = new Array(frm.endYear_1, frm.endMonth_1, frm.endDay_1, frm.endHour_1, frm.endMinute_1, frm.endSecond_1);
219    } else if (arg == "start_2") {
220        var fields = new Array(frm.startYear_2, frm.startMonth_2, frm.startDay_2, frm.startHour_2, frm.startMinute_2, frm.startSecond_2);
221    } else if (arg == "end_2") {
222        var fields = new Array(frm.endYear_2, frm.endMonth_2, frm.endDay_2, frm.endHour_2, frm.endMinute_2, frm.endSecond_2);
223    }
224    return fields
225}
226
227function getOppositeFields(arg) {
228    // getOppositeFields - returns an array of form fields opposite to the argument argument given.
229    if (arg == "end_1") {
230        var fields = new Array(frm.startYear_1, frm.startMonth_1, frm.startDay_1, frm.startHour_1, frm.startMinute_1, frm.startSecond_1);
231    } else if (arg == "start_1") {
232        var fields = new Array(frm.endYear_1, frm.endMonth_1, frm.endDay_1, frm.endHour_1, frm.endMinute_1, frm.endSecond_1);
233    } else if (arg == "end_2") {
234        var fields = new Array(frm.startYear_2, frm.startMonth_2, frm.startDay_2, frm.startHour_2, frm.startMinute_2, frm.startSecond_2);
235    } else if (arg == "start_2") {
236        var fields = new Array(frm.endYear_2, frm.endMonth_2, frm.endDay_2, frm.endHour_2, frm.endMinute_2, frm.endSecond_2);
237    }
238    return fields
239}
240
241function matchFields(fields1, fields2) {
242    // matchFields - sets one set of form fields equal to another
243    for (i=0; i<6; i++) {
244        fields2[i].selectedIndex = fields1[i].selectedIndex;
245    }
246}
247
248function getDateMinimum(arg) {
249    // getDateMinimum function - to return the minimum date for the appropriate fields
250    if ((arg=="start_1") || (arg=="end_1")) {
251        var dateMinimum = dateMinimum_1;
252    } else if ((arg=="start_2") || (arg=="end_2")) {
253        var dateMinimum = dateMinimum_2;
254    }
255    return dateMinimum;
256}
257
258function getDateMaximum(arg) {
259    // getDateMaximum function - to return the maximum date for the appropriate fields
260    if ((arg=="start_1") || (arg=="end_1")) {
261        var dateMaximum = dateMaximum_1;
262    } else if ((arg=="start_2") || (arg=="end_2")) {
263        var dateMaximum = dateMaximum_2;
264    }
265    return dateMaximum;
266}
267
268function resetDate(arg) {
269    // resetDate function - resets the date field selected to either max or min possible.
270    var form_fields = getFormFields(arg);
271    var dateMinimum = getDateMinimum(arg);
272    for (i=0; i<6; i++) {
273        form_fields[i].selectedIndex = dateMinimum[i];
274    }
275}
276
277
278function resetAllDates(n) {
279    // resetAllDates function - resets all date fields to minimum allowed.
280    resetDate("start_1");
281    resetDate("end_1");
282    if (n>1) {
283        resetDate("start_2");
284        resetDate("end_2");
285    }
286}
287
288
289function checkDate(arg) {
290    // checkDate function - checks start date is not below minimum.
291    var form_fields = getFormFields(arg);
292    var dateMinimum = getDateMinimum(arg);
293    var dateMaximum = getDateMaximum(arg);
294     
295    if (form_fields[0].selectedIndex < dateMinimum[0]) {
296        resetDate(arg);
297    } else if (form_fields[0].selectedIndex == dateMinimum[0]) {
298        if (form_fields[1].selectedIndex < dateMinimum[1]) {
299            resetDate(arg);
300        } else if (form_fields[1].selectedIndex == dateMinimum[1]) {
301            if (form_fields[2].selectedIndex < dateMinimum[2]) {
302                resetDate(arg);
303            } else if (form_fields[2].selectedIndex == dateMinimum[2]) {
304                if (form_fields[3].selectedIndex < dateMinimum[3]) {
305                    resetDate(arg);
306                } else if (form_fields[3].selectedIndex == dateMinimum[3]) {
307                    if (form_fields[4].selectedIndex < dateMinimum[4]) {
308                        resetDate(arg);
309                    } else if (form_fields[4].selectedIndex == dateMinimum[4]) {
310                        if (form_fields[5].selectedIndex < dateMinimum[5]) {
311                            resetDate(arg);
312                        }
313                    }
314                }
315            }
316        }
317    } else if (form_fields[0].selectedIndex == dateMaximum[0]) {
318        if (form_fields[1].selectedIndex > dateMaximum[1]) {
319            resetDate(arg);
320        } else if (form_fields[1].selectedIndex == dateMaximum[1]) {
321            if (form_fields[2].selectedIndex > dateMaximum[2]) {
322                resetDate(arg);
323            } else if (form_fields[2].selectedIndex == dateMaximum[2]) {
324                if (form_fields[3].selectedIndex > dateMaximum[3]) {
325                    resetDate(arg);
326                } else if (form_fields[3].selectedIndex == dateMaximum[3]) {
327                    if (form_fields[4].selectedIndex > dateMaximum[4]) {
328                        resetDate(arg);
329                    } else if (form_fields[4].selectedIndex == dateMaximum[4]) {
330                        if (form_fields[5].selectedIndex > dateMaximum[5]) {
331                            resetDate(arg);
332                        }
333                    }
334                }
335            }
336        }   
337    }
338    // Now check the order of the fields to make sure start is not after end
339    var opp_fields  = getOppositeFields(arg);
340    for (i=0; i<6; i++) {
341        if ((arg=="start_1") || (arg=="start_2")) {
342            if (form_fields[i].selectedIndex > opp_fields[i].selectedIndex) {
343                matchFields(form_fields, opp_fields);
344                break;
345            } else if (form_fields[i].selectedIndex < opp_fields[i].selectedIndex) {
346                break;
347            }
348        }
349        if ((arg=="end_1") || (arg=="end_2")) {
350            if (form_fields[i].selectedIndex < opp_fields[i].selectedIndex) {
351                matchFields(form_fields, opp_fields);
352                break;
353            } else if (form_fields[i].selectedIndex > opp_fields[i].selectedIndex) {
354                break;
355            }
356        }
357    }
358}
359
360</SCRIPT>
361"""
362
Note: See TracBrowser for help on using the repository browser.