1 | /** |
---|
2 | * ----------------------------------------------------------------------------- |
---|
3 | * file: ui.inputFilterBox.js |
---|
4 | * file version: 1.0.0 |
---|
5 | * date: 2012-06-14 |
---|
6 | * |
---|
7 | * A jQuery plugin provided by the piwigo's plugin "GrumPluginClasses" |
---|
8 | * |
---|
9 | * ----------------------------------------------------------------------------- |
---|
10 | * Author : Grum |
---|
11 | * email : grum@piwigo.com |
---|
12 | * website : http://photos.grum.fr |
---|
13 | * |
---|
14 | * << May the Little SpaceFrog be with you ! >> |
---|
15 | * ----------------------------------------------------------------------------- |
---|
16 | * |
---|
17 | * |
---|
18 | * |
---|
19 | * |
---|
20 | * :: HISTORY :: |
---|
21 | * |
---|
22 | * | release | date | |
---|
23 | * | 1.0.0 | 2012/06/14 | first release |
---|
24 | * | | | |
---|
25 | * | | | |
---|
26 | * | | | |
---|
27 | * | | | |
---|
28 | * | | | |
---|
29 | * | | | |
---|
30 | * |
---|
31 | */ |
---|
32 | |
---|
33 | |
---|
34 | var inputFilterBoxLang={ |
---|
35 | 'Operator':'Operator', |
---|
36 | 'Value':'Value', |
---|
37 | 'MinValue':'Minimum value', |
---|
38 | 'MaxValue':'Maximum value', |
---|
39 | '>':'Greater than', |
---|
40 | '<':'Less than', |
---|
41 | '>=':'Greater or equal to', |
---|
42 | '<=':'Less or equal to', |
---|
43 | '=':'Equal to', |
---|
44 | '!=':'Different than', |
---|
45 | 'between':'Between', |
---|
46 | 'and':'and', |
---|
47 | 'like':'Like', |
---|
48 | 'not like':'Not like', |
---|
49 | 'invalidParam':'Filter is not valid' |
---|
50 | }; |
---|
51 | |
---|
52 | ( |
---|
53 | function($) |
---|
54 | { |
---|
55 | /* |
---|
56 | * plugin 'public' functions |
---|
57 | */ |
---|
58 | var publicMethods = |
---|
59 | { |
---|
60 | init : function (opt) |
---|
61 | { |
---|
62 | return this.each(function() |
---|
63 | { |
---|
64 | // default values for the plugin |
---|
65 | var $this=$(this), |
---|
66 | data = $this.data('options'), |
---|
67 | objects = $this.data('objects'), |
---|
68 | properties = $this.data('properties'), |
---|
69 | options = |
---|
70 | { |
---|
71 | dataType:'', // numeric, string, dataset-url, dataset-values, date, time, datetime |
---|
72 | filterOperators:[], //allowed filter types |
---|
73 | defaultOperator:'=', //default filter applied |
---|
74 | defaultValue:{ //default value |
---|
75 | value:null, |
---|
76 | minValue:null, |
---|
77 | maxValue:null |
---|
78 | }, |
---|
79 | numericData:{ |
---|
80 | numDec:0, |
---|
81 | minValue:'none', |
---|
82 | maxValue:'none' |
---|
83 | }, |
---|
84 | stringData:{ |
---|
85 | regExp:'', |
---|
86 | maxChar:0 |
---|
87 | }, |
---|
88 | datasetData:{ |
---|
89 | serverUrl:'', |
---|
90 | values:[], |
---|
91 | listMaxHeight:200 |
---|
92 | }, |
---|
93 | datetimeData:{ |
---|
94 | value:'', |
---|
95 | minValue:'', |
---|
96 | maxValue:'', |
---|
97 | timeFormat:'hh:mm' |
---|
98 | } |
---|
99 | }; |
---|
100 | |
---|
101 | // if options given, merge it |
---|
102 | // if(opt) $.extend(options, opt); ==> options are set by setters |
---|
103 | |
---|
104 | $this.data('options', options); |
---|
105 | |
---|
106 | |
---|
107 | if(!properties) |
---|
108 | { |
---|
109 | $this.data('properties', |
---|
110 | { |
---|
111 | datasetValues:[], //available values for a dataset |
---|
112 | filter:{ |
---|
113 | operator:'', |
---|
114 | value:null, |
---|
115 | minValue:null, |
---|
116 | maxValue:null |
---|
117 | } |
---|
118 | } |
---|
119 | ); |
---|
120 | properties=$this.data('properties'); |
---|
121 | } |
---|
122 | |
---|
123 | if(!objects) |
---|
124 | { |
---|
125 | objects = |
---|
126 | { |
---|
127 | container:$('<div/>', |
---|
128 | { |
---|
129 | 'class':'ui-inputFilterBox' |
---|
130 | } |
---|
131 | ), |
---|
132 | operatorBox:$('<div/>', |
---|
133 | { |
---|
134 | 'class':'ui-inputFilterBox-operatorBox' |
---|
135 | } |
---|
136 | ), |
---|
137 | valueBox1:$('<div/>', |
---|
138 | { |
---|
139 | 'class':'ui-inputFilterBox-valueBox' |
---|
140 | } |
---|
141 | ), |
---|
142 | valueBox2:$('<div/>', |
---|
143 | { |
---|
144 | 'class':'ui-inputFilterBox-valueBox' |
---|
145 | } |
---|
146 | ), |
---|
147 | operator:$('<div/>', {'class':'ui-inputFilterBox-operator'}), |
---|
148 | value0:$('<div/>', {'class':'ui-inputFilterBox-value'}), |
---|
149 | value1:$('<div/>', {'class':'ui-inputFilterBox-value'}), |
---|
150 | value2:$('<div/>', {'class':'ui-inputFilterBox-value'}) |
---|
151 | }; |
---|
152 | |
---|
153 | $this |
---|
154 | .html('') |
---|
155 | .append( |
---|
156 | objects.container |
---|
157 | .append(objects.operatorBox) |
---|
158 | .append(objects.valueBox1) |
---|
159 | .append(objects.valueBox2) |
---|
160 | ); |
---|
161 | |
---|
162 | $this.data('objects', objects); |
---|
163 | } |
---|
164 | |
---|
165 | privateMethods.setOptions($this, opt); |
---|
166 | } |
---|
167 | ); |
---|
168 | }, // init |
---|
169 | destroy : function () |
---|
170 | { |
---|
171 | return this.each( |
---|
172 | function() |
---|
173 | { |
---|
174 | // default values for the plugin |
---|
175 | var $this=$(this), |
---|
176 | options = $this.data('options'), |
---|
177 | objects = $this.data('objects'); |
---|
178 | switch(options.dataType) |
---|
179 | { |
---|
180 | case 'numeric': |
---|
181 | objects.operator.inputList('destroy').remove(); |
---|
182 | objects.value0.inputNum('destroy').remove(); |
---|
183 | objects.value1.inputNum('destroy').remove(); |
---|
184 | objects.value2.inputNum('destroy').remove(); |
---|
185 | break; |
---|
186 | case 'string': |
---|
187 | objects.operator.inputList('destroy').remove(); |
---|
188 | objects.value0.inputText('destroy').remove(); |
---|
189 | objects.value1.inputText('destroy').remove(); |
---|
190 | objects.value2.inputText('destroy').remove(); |
---|
191 | break; |
---|
192 | case 'date': |
---|
193 | case 'datetime': |
---|
194 | objects.operator.inputList('destroy').remove(); |
---|
195 | objects.value0.inputDate('destroy').remove(); |
---|
196 | objects.value1.inputDate('destroy').remove(); |
---|
197 | objects.value2.inputDate('destroy').remove(); |
---|
198 | break; |
---|
199 | case 'dataset-url': |
---|
200 | case 'dataset-values': |
---|
201 | objects.value0.inputList('destroy').remove(); |
---|
202 | break; |
---|
203 | } |
---|
204 | |
---|
205 | objects.valueBox2.remove(); |
---|
206 | objects.valueBox1.remove(); |
---|
207 | objects.operatorBox.remove(); |
---|
208 | objects.container.remove(); |
---|
209 | |
---|
210 | $this |
---|
211 | .removeData() |
---|
212 | .unbind() |
---|
213 | .css( |
---|
214 | { |
---|
215 | width:'', |
---|
216 | height:'' |
---|
217 | } |
---|
218 | ); |
---|
219 | delete $this; |
---|
220 | } |
---|
221 | ); |
---|
222 | }, // destroy |
---|
223 | |
---|
224 | options: function (value) |
---|
225 | { |
---|
226 | return( |
---|
227 | this.each( |
---|
228 | function() |
---|
229 | { |
---|
230 | privateMethods.setOptions($(this), value); |
---|
231 | } |
---|
232 | ) |
---|
233 | ); |
---|
234 | }, // options |
---|
235 | |
---|
236 | filter: function () |
---|
237 | { |
---|
238 | var properties=this.data('properties'); |
---|
239 | return(properties.filter); |
---|
240 | }, //filter |
---|
241 | |
---|
242 | isValid: function () |
---|
243 | { |
---|
244 | return(privateMethods.isValid($(this))); |
---|
245 | } //isValid |
---|
246 | |
---|
247 | }; // methods |
---|
248 | |
---|
249 | |
---|
250 | /* |
---|
251 | * plugin 'private' methods |
---|
252 | */ |
---|
253 | var privateMethods = |
---|
254 | { |
---|
255 | setOptions : function (object, value) |
---|
256 | { |
---|
257 | var properties=object.data('properties'), |
---|
258 | options=object.data('options'); |
---|
259 | |
---|
260 | if(!$.isPlainObject(value)) return(false); |
---|
261 | |
---|
262 | properties.initialized=false; |
---|
263 | |
---|
264 | privateMethods.setFilter(object, (value!=null)?value:options); |
---|
265 | privateMethods.initialiseFilter(object); |
---|
266 | privateMethods.buildInterface(object); |
---|
267 | |
---|
268 | properties.initialized=true; |
---|
269 | }, |
---|
270 | |
---|
271 | setFilter : function (object, value) |
---|
272 | { |
---|
273 | var options=object.data('options'); |
---|
274 | |
---|
275 | options.dataType=value.dataType; |
---|
276 | |
---|
277 | switch(value.dataType) |
---|
278 | { |
---|
279 | case 'numeric': |
---|
280 | case 'date': |
---|
281 | case 'time': |
---|
282 | case 'datetime': |
---|
283 | for(var i=0;i<value.filterOperators.length;i++) |
---|
284 | { |
---|
285 | if(value.filterOperators[i]=='>' || |
---|
286 | value.filterOperators[i]=='<' || |
---|
287 | value.filterOperators[i]=='>=' || |
---|
288 | value.filterOperators[i]=='<=' || |
---|
289 | value.filterOperators[i]=='=' || |
---|
290 | value.filterOperators[i]=='!=' || |
---|
291 | value.filterOperators[i]=='between') options.filterOperators.push(value.filterOperators[i]); |
---|
292 | } |
---|
293 | break; |
---|
294 | case 'string': |
---|
295 | for(var i=0;i<value.filterOperators.length;i++) |
---|
296 | { |
---|
297 | if(value.filterOperators[i]=='>' || |
---|
298 | value.filterOperators[i]=='<' || |
---|
299 | value.filterOperators[i]=='>=' || |
---|
300 | value.filterOperators[i]=='<=' || |
---|
301 | value.filterOperators[i]=='=' || |
---|
302 | value.filterOperators[i]=='!=' || |
---|
303 | value.filterOperators[i]=='like' || |
---|
304 | value.filterOperators[i]=='not like' || |
---|
305 | value.filterOperators[i]=='between') options.filterOperators.push(value.filterOperators[i]); |
---|
306 | } |
---|
307 | break; |
---|
308 | case 'dataset-url': |
---|
309 | case 'dataset-values': |
---|
310 | for(var i=0;i<value.filterOperators.length;i++) |
---|
311 | { |
---|
312 | if(value.filterOperators[i]=='=' || |
---|
313 | value.filterOperators[i]=='in') options.filterOperators.push(value.filterOperators[i]); |
---|
314 | } |
---|
315 | break; |
---|
316 | } |
---|
317 | |
---|
318 | options.defaultValue=value.defaultValue; |
---|
319 | options.defaultOperator=value.defaultOperator; |
---|
320 | |
---|
321 | switch(value.dataType) |
---|
322 | { |
---|
323 | case 'numeric': |
---|
324 | if(value.data.numDec) |
---|
325 | options.numericData.numDec=value.data.numDec; |
---|
326 | if(value.data.minValue) |
---|
327 | options.numericData.minValue=value.data.minValue; |
---|
328 | if(value.data.maxValue) |
---|
329 | options.numericData.maxValue=value.data.maxValue; |
---|
330 | break; |
---|
331 | case 'string': |
---|
332 | if(value.data.regExp) |
---|
333 | options.stringData.regExp=value.data.regExp; |
---|
334 | if(value.data.maxChar) |
---|
335 | options.stringData.maxChar=value.data.maxChar; |
---|
336 | break; |
---|
337 | case 'date': |
---|
338 | case 'time': |
---|
339 | case 'datetime': |
---|
340 | if(value.data.value) |
---|
341 | options.datetimeData.value=value.data.value; |
---|
342 | if(value.data.minValue) |
---|
343 | options.datetimeData.minValue=value.data.minValue; |
---|
344 | if(value.data.maxValue) |
---|
345 | options.datetimeData.maxValue=value.data.maxValue; |
---|
346 | if(value.data.timeFormat) |
---|
347 | options.datetimeData.timeFormat=value.data.timeFormat; |
---|
348 | break; |
---|
349 | case 'dataset-url': |
---|
350 | options.datasetData.serverUrl=value.data.serverUrl; |
---|
351 | if(value.data.listMaxHeight) |
---|
352 | options.datasetData.listMaxHeight=value.data.listMaxHeight; |
---|
353 | break; |
---|
354 | case 'dataset-values': |
---|
355 | options.datasetData.values=value.data.values; |
---|
356 | if(value.data.listMaxHeight) |
---|
357 | options.datasetData.listMaxHeight=value.data.listMaxHeight; |
---|
358 | break; |
---|
359 | } |
---|
360 | |
---|
361 | return(options.filter); |
---|
362 | }, //setFilter |
---|
363 | |
---|
364 | /* |
---|
365 | * initialise the returned filter |
---|
366 | */ |
---|
367 | initialiseFilter : function (object) |
---|
368 | { |
---|
369 | var options=object.data('options'), |
---|
370 | properties=object.data('properties'); |
---|
371 | |
---|
372 | properties.filter.value=options.defaultValue.value; |
---|
373 | properties.filter.minValue=options.defaultValue.minValue; |
---|
374 | properties.filter.maxValue=options.defaultValue.maxValue; |
---|
375 | |
---|
376 | if(options.dataType=='dataset-values') |
---|
377 | properties.datasetValues=options.datasetData.values; |
---|
378 | |
---|
379 | if(options.dataType=='dataset-values' || |
---|
380 | options.dataType=='dataset-values') |
---|
381 | { |
---|
382 | properties.filter.operator=''; |
---|
383 | } |
---|
384 | else |
---|
385 | { |
---|
386 | properties.filter.operator=options.defaultOperator; |
---|
387 | } |
---|
388 | }, //buildFilter |
---|
389 | |
---|
390 | buildInterface : function (object) |
---|
391 | { |
---|
392 | var options=object.data('options'), |
---|
393 | objects=object.data('objects'), |
---|
394 | properties=object.data('properties'); |
---|
395 | |
---|
396 | if(options.dataType=='numeric' || |
---|
397 | options.dataType=='string' || |
---|
398 | options.dataType=='date' || |
---|
399 | options.dataType=='time' || |
---|
400 | options.dataType=='datetime') |
---|
401 | { |
---|
402 | var operatorList=[]; |
---|
403 | |
---|
404 | for(var i=0;i<options.filterOperators.length;i++) |
---|
405 | { |
---|
406 | operatorList.push( |
---|
407 | { |
---|
408 | value:options.filterOperators[i], |
---|
409 | cols:[inputFilterBoxLang[options.filterOperators[i]]] |
---|
410 | } |
---|
411 | ); |
---|
412 | } |
---|
413 | |
---|
414 | objects.operator |
---|
415 | .inputList( |
---|
416 | { |
---|
417 | value:options.defaultOperator, |
---|
418 | colsWidth:[], |
---|
419 | colsDisplayed:[0], |
---|
420 | items:operatorList, |
---|
421 | change:function (event, value) |
---|
422 | { |
---|
423 | privateMethods.changeOperator(object, value); |
---|
424 | } |
---|
425 | } |
---|
426 | ); |
---|
427 | |
---|
428 | objects.operatorBox |
---|
429 | .append($('<span/>').html(inputFilterBoxLang['Operator'])) |
---|
430 | .append(objects.operator); |
---|
431 | } // operator: numeric, string, date, time, datetime |
---|
432 | |
---|
433 | if(options.dataType=='numeric') |
---|
434 | { |
---|
435 | objects.value0 |
---|
436 | .inputNum( |
---|
437 | { |
---|
438 | numDec:options.numericData.numDec, |
---|
439 | minValue:options.numericData.minValue, |
---|
440 | maxValue:options.numericData.maxValue, |
---|
441 | value:options.defaultValue.value, |
---|
442 | change:function (event, value) |
---|
443 | { |
---|
444 | privateMethods.checkNumericValue(object); |
---|
445 | } |
---|
446 | } |
---|
447 | ).addClass('ui-inputFilterBox-numValue'); |
---|
448 | objects.value1 |
---|
449 | .inputNum( |
---|
450 | { |
---|
451 | numDec:options.numericData.numDec, |
---|
452 | minValue:options.numericData.minValue, |
---|
453 | maxValue:options.numericData.maxValue, |
---|
454 | value:options.defaultValue.minValue, |
---|
455 | change:function (event, value) |
---|
456 | { |
---|
457 | privateMethods.checkNumericBetweenValue(object); |
---|
458 | } |
---|
459 | } |
---|
460 | ).addClass('ui-inputFilterBox-numValue'); |
---|
461 | objects.value2 |
---|
462 | .inputNum( |
---|
463 | { |
---|
464 | numDec:options.numericData.numDec, |
---|
465 | minValue:options.numericData.minValue, |
---|
466 | maxValue:options.numericData.maxValue, |
---|
467 | value:options.defaultValue.maxValue, |
---|
468 | change:function (event, value) |
---|
469 | { |
---|
470 | privateMethods.checkNumericBetweenValue(object) |
---|
471 | } |
---|
472 | } |
---|
473 | ).addClass('ui-inputFilterBox-numValue'); |
---|
474 | privateMethods.checkNumericValue(object); |
---|
475 | privateMethods.checkNumericBetweenValue(object); |
---|
476 | } //numeric |
---|
477 | |
---|
478 | if(options.dataType=='string') |
---|
479 | { |
---|
480 | objects.value0 |
---|
481 | .inputText( |
---|
482 | { |
---|
483 | regExp:options.stringData.regExp, |
---|
484 | maxChar:options.stringData.maxChar, |
---|
485 | value:options.defaultValue.value, |
---|
486 | change:function (event, value) |
---|
487 | { |
---|
488 | privateMethods.checkStringValue(object); |
---|
489 | } |
---|
490 | } |
---|
491 | ).addClass('ui-inputFilterBox-stringValue'); |
---|
492 | objects.value1 |
---|
493 | .inputText( |
---|
494 | { |
---|
495 | regExp:options.stringData.regExp, |
---|
496 | maxChar:options.stringData.maxChar, |
---|
497 | value:options.defaultValue.minValue, |
---|
498 | change:function (event, value) |
---|
499 | { |
---|
500 | privateMethods.checkStringBetweenValue(object); |
---|
501 | } |
---|
502 | } |
---|
503 | ).addClass('ui-inputFilterBox-stringValue'); |
---|
504 | objects.value2 |
---|
505 | .inputText( |
---|
506 | { |
---|
507 | regExp:options.stringData.regExp, |
---|
508 | maxChar:options.stringData.maxChar, |
---|
509 | value:options.defaultValue.maxValue, |
---|
510 | change:function (event, value) |
---|
511 | { |
---|
512 | privateMethods.checkStringBetweenValue(object) |
---|
513 | } |
---|
514 | } |
---|
515 | ).addClass('ui-inputFilterBox-stringValue'); |
---|
516 | privateMethods.checkStringValue(object); |
---|
517 | privateMethods.checkStringBetweenValue(object); |
---|
518 | } //string |
---|
519 | |
---|
520 | if(options.dataType=='date' || |
---|
521 | options.dataType=='datetime') |
---|
522 | { |
---|
523 | objects.value0 |
---|
524 | .inputDate( |
---|
525 | { |
---|
526 | dateType:options.dataType, |
---|
527 | value:options.defaultValue.value, |
---|
528 | timepicker:{ |
---|
529 | timeFormat:options.datetimeData.timeFormat |
---|
530 | }, |
---|
531 | datepicker:{ |
---|
532 | minDate:options.datetimeData.minValue, |
---|
533 | maxDate:options.datetimeData.maxValue, |
---|
534 | defaultDate:options.defaultValue.value, |
---|
535 | dateFormat:'yy-mm-dd' |
---|
536 | }, |
---|
537 | change:function (event, value) |
---|
538 | { |
---|
539 | privateMethods.checkDateValue(object); |
---|
540 | } |
---|
541 | } |
---|
542 | ).addClass('ui-inputFilterBox-dateValue'); |
---|
543 | |
---|
544 | objects.value1 |
---|
545 | .inputDate( |
---|
546 | { |
---|
547 | dateType:options.dataType, |
---|
548 | value:options.defaultValue.minValue, |
---|
549 | timepicker:{ |
---|
550 | timeFormat:options.datetimeData.timeFormat |
---|
551 | }, |
---|
552 | datepicker:{ |
---|
553 | minDate:options.datetimeData.minValue, |
---|
554 | maxDate:options.datetimeData.maxValue, |
---|
555 | defaultDate:options.defaultValue.minValue, |
---|
556 | dateFormat:'yy-mm-dd' |
---|
557 | }, |
---|
558 | change:function (event, value) |
---|
559 | { |
---|
560 | privateMethods.checkDateBetweenValue(object); |
---|
561 | } |
---|
562 | } |
---|
563 | ).addClass('ui-inputFilterBox-dateValue'); |
---|
564 | |
---|
565 | objects.value2 |
---|
566 | .inputDate( |
---|
567 | { |
---|
568 | dateType:options.dataType, |
---|
569 | value:options.defaultValue.maxValue, |
---|
570 | timepicker:{ |
---|
571 | timeFormat:options.datetimeData.timeFormat |
---|
572 | }, |
---|
573 | datepicker:{ |
---|
574 | minDate:options.datetimeData.minValue, |
---|
575 | maxDate:options.datetimeData.maxValue, |
---|
576 | defaultDate:options.defaultValue.maxValue, |
---|
577 | dateFormat:'yy-mm-dd' |
---|
578 | }, |
---|
579 | change:function (event, value) |
---|
580 | { |
---|
581 | privateMethods.checkDateBetweenValue(object); |
---|
582 | } |
---|
583 | } |
---|
584 | ).addClass('ui-inputFilterBox-dateValue'); |
---|
585 | |
---|
586 | privateMethods.checkDateValue(object); |
---|
587 | privateMethods.checkDateBetweenValue(object); |
---|
588 | } //date, datetime |
---|
589 | |
---|
590 | |
---|
591 | if(options.dataType=='numeric' || |
---|
592 | options.dataType=='string' || |
---|
593 | options.dataType=='date' || |
---|
594 | options.dataType=='datetime') |
---|
595 | { |
---|
596 | objects.valueBox1 |
---|
597 | .append($('<span/>').html(inputFilterBoxLang['Value'])) |
---|
598 | .append(objects.value0); |
---|
599 | |
---|
600 | objects.valueBox2 |
---|
601 | .append($('<span/>').html(inputFilterBoxLang['MinValue'])) |
---|
602 | .append(objects.value1) |
---|
603 | .append($('<span/>').html(inputFilterBoxLang['MaxValue'])) |
---|
604 | .append(objects.value2); |
---|
605 | } // numeric+string+date+datetime |
---|
606 | |
---|
607 | |
---|
608 | if((options.dataType=='numeric' || |
---|
609 | options.dataType=='string' || |
---|
610 | options.dataType=='date' || |
---|
611 | options.dataType=='time' || |
---|
612 | options.dataType=='datetime') && |
---|
613 | options.defaultOperator!=null && options.defaultOperator!='') |
---|
614 | privateMethods.changeOperator(object, options.defaultOperator); |
---|
615 | |
---|
616 | if(options.dataType=='dataset-values' || |
---|
617 | options.dataType=='dataset-url') |
---|
618 | { |
---|
619 | var itemsList=null, |
---|
620 | serverUrl=null; |
---|
621 | |
---|
622 | if(options.dataType=='dataset-values') |
---|
623 | { |
---|
624 | itemsList=[]; |
---|
625 | |
---|
626 | for(var i=0;i<options.datasetData.values.length;i++) |
---|
627 | { |
---|
628 | itemsList.push( |
---|
629 | { |
---|
630 | value:options.datasetData.values[i], |
---|
631 | cols:options.datasetData.values[i] |
---|
632 | } |
---|
633 | ); |
---|
634 | } |
---|
635 | } |
---|
636 | else |
---|
637 | { |
---|
638 | serverUrl=options.datasetData.serverUrl; |
---|
639 | } |
---|
640 | |
---|
641 | objects.value0 |
---|
642 | .inputList( |
---|
643 | { |
---|
644 | value:options.defaultValue.value, |
---|
645 | multiple:($.inArray('in', options.filterOperators)>-1), |
---|
646 | colsWidth:[], |
---|
647 | colsCss:['col1', 'col2', 'col3'], |
---|
648 | items:itemsList, |
---|
649 | serverUrl:serverUrl, |
---|
650 | listMaxHeight:options.datasetData.listMaxHeight, |
---|
651 | change:function (event, value) |
---|
652 | { |
---|
653 | privateMethods.checkDatasetValue(object); |
---|
654 | } |
---|
655 | } |
---|
656 | ); |
---|
657 | |
---|
658 | objects.valueBox1 |
---|
659 | .append($('<span/>').html(inputFilterBoxLang['Value'])) |
---|
660 | .append(objects.value0) |
---|
661 | .css('display', 'block'); |
---|
662 | |
---|
663 | objects.operatorBox.css('display', 'none'); |
---|
664 | } //dataset-url + dataset-values |
---|
665 | }, //buildInterface |
---|
666 | |
---|
667 | changeOperator : function (object, value) |
---|
668 | { |
---|
669 | var objects=object.data('objects') |
---|
670 | properties=object.data('properties'); |
---|
671 | |
---|
672 | properties.filter.operator=value; |
---|
673 | |
---|
674 | if(value=='between') |
---|
675 | { |
---|
676 | objects.valueBox1.css('display', 'none'); |
---|
677 | objects.valueBox2.css('display', 'block'); |
---|
678 | } |
---|
679 | else |
---|
680 | { |
---|
681 | objects.valueBox2.css('display', 'none'); |
---|
682 | objects.valueBox1.css('display', 'block'); |
---|
683 | } |
---|
684 | }, //changeOperator |
---|
685 | |
---|
686 | checkNumericValue : function (object) |
---|
687 | { |
---|
688 | var objects=object.data('objects'), |
---|
689 | properties=object.data('properties'); |
---|
690 | |
---|
691 | properties.filter.value=objects.value0.inputNum('value'); |
---|
692 | }, //checkNumericValue |
---|
693 | |
---|
694 | checkNumericBetweenValue : function (object) |
---|
695 | { |
---|
696 | var objects=object.data('objects'), |
---|
697 | properties=object.data('properties'), |
---|
698 | minValue=null, |
---|
699 | maxValue=null; |
---|
700 | |
---|
701 | minValue=objects.value1.inputNum('value'); |
---|
702 | maxValue=objects.value2.inputNum('value'); |
---|
703 | |
---|
704 | properties.filter.minValue=minValue; |
---|
705 | properties.filter.maxValue=maxValue; |
---|
706 | |
---|
707 | if(minValue>maxValue) |
---|
708 | { |
---|
709 | objects.value1.inputNum('isValid', false); |
---|
710 | objects.value2.inputNum('isValid', false); |
---|
711 | } |
---|
712 | else |
---|
713 | { |
---|
714 | objects.value1.inputNum('isValid', 'check'); |
---|
715 | objects.value2.inputNum('isValid', 'check'); |
---|
716 | } |
---|
717 | }, //checkNumericMinMaxValue |
---|
718 | |
---|
719 | checkStringValue : function (object) |
---|
720 | { |
---|
721 | var objects=object.data('objects'), |
---|
722 | properties=object.data('properties'), |
---|
723 | value=''; |
---|
724 | |
---|
725 | value=objects.value0.inputText('value'); |
---|
726 | |
---|
727 | properties.filter.value=value; |
---|
728 | |
---|
729 | if(value=='') |
---|
730 | { |
---|
731 | objects.value0.inputText('isValid', false); |
---|
732 | } |
---|
733 | else |
---|
734 | { |
---|
735 | objects.value0.inputText('isValid', 'check'); |
---|
736 | } |
---|
737 | }, //checkStringValue |
---|
738 | |
---|
739 | checkStringBetweenValue : function (object) |
---|
740 | { |
---|
741 | var objects=object.data('objects'), |
---|
742 | properties=object.data('properties'), |
---|
743 | minValue=null, |
---|
744 | maxValue=null; |
---|
745 | |
---|
746 | minValue=objects.value1.inputText('value'); |
---|
747 | maxValue=objects.value2.inputText('value'); |
---|
748 | |
---|
749 | properties.filter.minValue=minValue; |
---|
750 | properties.filter.maxValue=maxValue; |
---|
751 | |
---|
752 | if(minValue=='') |
---|
753 | objects.value1.inputText('isValid', false); |
---|
754 | |
---|
755 | if(maxValue=='') |
---|
756 | objects.value2.inputText('isValid', false); |
---|
757 | |
---|
758 | if(minValue>maxValue) |
---|
759 | { |
---|
760 | objects.value1.inputText('isValid', false); |
---|
761 | objects.value2.inputText('isValid', false); |
---|
762 | } |
---|
763 | else if(minValue!='' && maxValue!='') |
---|
764 | { |
---|
765 | objects.value1.inputText('isValid', 'check'); |
---|
766 | objects.value2.inputText('isValid', 'check'); |
---|
767 | } |
---|
768 | }, //checkStringBetweenValue |
---|
769 | |
---|
770 | |
---|
771 | checkDatasetValue : function (object) |
---|
772 | { |
---|
773 | var objects=object.data('objects'), |
---|
774 | properties=object.data('properties'), |
---|
775 | value=''; |
---|
776 | |
---|
777 | value=objects.value0.inputList('value'); |
---|
778 | |
---|
779 | properties.filter.value=value; |
---|
780 | |
---|
781 | if(objects.value0.inputList('properties', 'multiple')) |
---|
782 | { |
---|
783 | if(value.length==0) |
---|
784 | { |
---|
785 | objects.value0.inputList('isValid', false); |
---|
786 | } |
---|
787 | else |
---|
788 | { |
---|
789 | objects.value0.inputList('isValid', true); |
---|
790 | } |
---|
791 | } |
---|
792 | else |
---|
793 | { |
---|
794 | if(value=='') |
---|
795 | { |
---|
796 | objects.value0.inputList('isValid', false); |
---|
797 | } |
---|
798 | else |
---|
799 | { |
---|
800 | objects.value0.inputList('isValid', true); |
---|
801 | } |
---|
802 | } |
---|
803 | }, //checkDatasetValue |
---|
804 | |
---|
805 | checkDateValue : function (object) |
---|
806 | { |
---|
807 | var options=object.data('options'), |
---|
808 | objects=object.data('objects'), |
---|
809 | properties=object.data('properties'); |
---|
810 | |
---|
811 | properties.filter.value=objects.value0.inputDate('value'); |
---|
812 | }, //checkDateValue |
---|
813 | |
---|
814 | checkDateBetweenValue : function (object) |
---|
815 | { |
---|
816 | var options=object.data('options'), |
---|
817 | objects=object.data('objects'), |
---|
818 | properties=object.data('properties'), |
---|
819 | minValue=null, |
---|
820 | maxValue=null; |
---|
821 | |
---|
822 | minValue=objects.value1.inputDate('value'); |
---|
823 | maxValue=objects.value2.inputDate('value'); |
---|
824 | |
---|
825 | properties.filter.minValue=minValue; |
---|
826 | properties.filter.maxValue=maxValue; |
---|
827 | |
---|
828 | if(minValue>maxValue) |
---|
829 | { |
---|
830 | objects.value1.inputDate('isValid', false); |
---|
831 | objects.value2.inputDate('isValid', false); |
---|
832 | } |
---|
833 | else |
---|
834 | { |
---|
835 | objects.value1.inputDate('isValid', 'check'); |
---|
836 | objects.value2.inputDate('isValid', 'check'); |
---|
837 | } |
---|
838 | |
---|
839 | }, //checkDateBetweenValue |
---|
840 | |
---|
841 | setValid: function (object, inputObject, isValid) |
---|
842 | { |
---|
843 | if(isValid) |
---|
844 | { |
---|
845 | inputObject.removeClass('ui-error'); |
---|
846 | } |
---|
847 | else |
---|
848 | { |
---|
849 | inputObject.addClass('ui-error'); |
---|
850 | } |
---|
851 | }, //setValid |
---|
852 | |
---|
853 | |
---|
854 | isValid:function(object) |
---|
855 | { |
---|
856 | var objects=object.data('objects'), |
---|
857 | properties=object.data('properties'), |
---|
858 | options=object.data('options'); |
---|
859 | |
---|
860 | switch(options.dataType) |
---|
861 | { |
---|
862 | case 'numeric': |
---|
863 | if(properties.filter.operator=='between') |
---|
864 | { |
---|
865 | return(objects.value1.inputNum('isValid') && objects.value2.inputNum('isValid')); |
---|
866 | } |
---|
867 | else |
---|
868 | { |
---|
869 | return(objects.value0.inputNum('isValid')); |
---|
870 | } |
---|
871 | break; |
---|
872 | case 'string': |
---|
873 | if(properties.filter.operator=='between') |
---|
874 | { |
---|
875 | return(objects.value1.inputText('isValid') && objects.value2.inputText('isValid')); |
---|
876 | } |
---|
877 | else |
---|
878 | { |
---|
879 | return(objects.value0.inputText('isValid')); |
---|
880 | } |
---|
881 | break; |
---|
882 | case 'date': |
---|
883 | case 'time': |
---|
884 | case 'datetime': |
---|
885 | if(properties.filter.operator=='between') |
---|
886 | { |
---|
887 | return(objects.value1.inputDate('isValid') && objects.value2.inputDate('isValid')); |
---|
888 | } |
---|
889 | else |
---|
890 | { |
---|
891 | return(objects.value0.inputDate('isValid')); |
---|
892 | } |
---|
893 | break; |
---|
894 | case 'dataset-url': |
---|
895 | case 'dataset-values': |
---|
896 | return(objects.value0.inputText('isValid')); |
---|
897 | break; |
---|
898 | } |
---|
899 | } //isValid |
---|
900 | |
---|
901 | |
---|
902 | }; |
---|
903 | |
---|
904 | |
---|
905 | $.fn.inputFilterBox = function(method) |
---|
906 | { |
---|
907 | if(publicMethods[method]) |
---|
908 | { |
---|
909 | return publicMethods[method].apply( this, Array.prototype.slice.call( arguments, 1 )); |
---|
910 | } |
---|
911 | else if(typeof method === 'object' || ! method) |
---|
912 | { |
---|
913 | return publicMethods.init.apply(this, arguments); |
---|
914 | } |
---|
915 | else |
---|
916 | { |
---|
917 | $.error( 'Method ' + method + ' does not exist on jQuery.inputFilterBox' ); |
---|
918 | } |
---|
919 | } // $.fn.inputFilterBox |
---|
920 | |
---|
921 | } |
---|
922 | )(jQuery); |
---|
923 | |
---|
924 | /** |
---|
925 | * check if the given object can be considered as a valid filter |
---|
926 | * |
---|
927 | * @param Object filter: object to check |
---|
928 | * @return Boolean: return true if object is a valid filter, otherwise return false |
---|
929 | */ |
---|
930 | $.isValidFilter = function (filter) |
---|
931 | { |
---|
932 | var validFilterTypes=[]; |
---|
933 | |
---|
934 | if(!$.isPlainObject(filter)) return(false); |
---|
935 | if(filter.dataType==null || |
---|
936 | !$.isArray(filter.filterOperators) || |
---|
937 | filter.filterOperators.length==0) return(false); |
---|
938 | |
---|
939 | if(filter.data!==null) |
---|
940 | { |
---|
941 | switch(filter.dataType) |
---|
942 | { |
---|
943 | case 'numeric': |
---|
944 | if((filter.data.numDec!=null && !$.isNumeric(filter.data.numDec)) || |
---|
945 | (filter.data.minValue!=null && !$.isNumeric(filter.data.minValue)) || |
---|
946 | (filter.data.maxValue!=null && !$.isNumeric(filter.data.maxValue))) return(false); |
---|
947 | if(filter.data.numDec<0 || |
---|
948 | filter.data.minValue > filter.data.maxValue) return(false); |
---|
949 | break; |
---|
950 | case 'datetime': |
---|
951 | if(filter.data.timeFormat!=null && |
---|
952 | !(filter.data.timeFormat=='hh:mm' || filter.data.timeFormat=='hh:mm:ss')) return(false); |
---|
953 | case 'date': |
---|
954 | /* |
---|
955 | * faire un check de la validité des valeur min&max |
---|
956 | if((filter.data.minValue!=null && filter.data.minValue!='') || |
---|
957 | (filter.data.maxValue!=null && filter.data.minValue!='')) return(false); |
---|
958 | */ |
---|
959 | if(filter.data.minValue > filter.data.maxValue) return(false); |
---|
960 | break; |
---|
961 | case 'string': |
---|
962 | if(filter.data.maxChar!=null && !$.isNumeric(filter.data.maxChar)) return(false); |
---|
963 | if(filter.data.maxChar<0) return(false); |
---|
964 | break; |
---|
965 | case 'dataset-url': |
---|
966 | if(filter.data.serverUrl==null || |
---|
967 | filter.data.serverUrl=='') return(false); |
---|
968 | if(filter.listMaxHeight!=null && |
---|
969 | !$.isNumeric(filter.listMaxHeight) && |
---|
970 | filter.listMaxHeight<0) return(false); |
---|
971 | break; |
---|
972 | case 'dataset-values': |
---|
973 | if(!$.isArray(filter.data.values) || |
---|
974 | filter.data.values.length==0) return(false); |
---|
975 | if(filter.listMaxHeight!=null && |
---|
976 | !$.isNumeric(filter.listMaxHeight) && |
---|
977 | filter.listMaxHeight<0) return(false); |
---|
978 | break; |
---|
979 | } |
---|
980 | } |
---|
981 | |
---|
982 | switch(filter.dataType) |
---|
983 | { |
---|
984 | case 'numeric': |
---|
985 | case 'date': |
---|
986 | case 'time': |
---|
987 | case 'datetime': |
---|
988 | for(var i=0;i<filter.filterOperators.length;i++) |
---|
989 | { |
---|
990 | if(filter.filterOperators[i]=='>' || |
---|
991 | filter.filterOperators[i]=='<' || |
---|
992 | filter.filterOperators[i]=='>=' || |
---|
993 | filter.filterOperators[i]=='<=' || |
---|
994 | filter.filterOperators[i]=='=' || |
---|
995 | filter.filterOperators[i]=='!=' || |
---|
996 | filter.filterOperators[i]=='between') validFilterTypes.push(filter.filterOperators[i]); |
---|
997 | } |
---|
998 | break; |
---|
999 | case 'string': |
---|
1000 | for(var i=0;i<filter.filterOperators.length;i++) |
---|
1001 | { |
---|
1002 | if(filter.filterOperators[i]=='>' || |
---|
1003 | filter.filterOperators[i]=='<' || |
---|
1004 | filter.filterOperators[i]=='>=' || |
---|
1005 | filter.filterOperators[i]=='<=' || |
---|
1006 | filter.filterOperators[i]=='=' || |
---|
1007 | filter.filterOperators[i]=='!=' || |
---|
1008 | filter.filterOperators[i]=='like' || |
---|
1009 | filter.filterOperators[i]=='not like' || |
---|
1010 | filter.filterOperators[i]=='between') validFilterTypes.push(filter.filterOperators[i]); |
---|
1011 | } |
---|
1012 | break; |
---|
1013 | case 'dataset-values': |
---|
1014 | case 'dataset-url': |
---|
1015 | for(var i=0;i<filter.filterOperators.length;i++) |
---|
1016 | { |
---|
1017 | if(filter.filterOperators[i]=='=' || |
---|
1018 | filter.filterOperators[i]=='in') validFilterTypes.push(filter.filterOperators[i]); |
---|
1019 | } |
---|
1020 | if(validFilterTypes.length>1) return(false); // only one filter type at once for dataset filters |
---|
1021 | break; |
---|
1022 | default: |
---|
1023 | return(false); |
---|
1024 | } |
---|
1025 | if(validFilterTypes.length==0) return(false); |
---|
1026 | |
---|
1027 | return(true); |
---|
1028 | } |
---|
1029 | |
---|
1030 | /** |
---|
1031 | * open a modal dialog box to set filter properties |
---|
1032 | */ |
---|
1033 | $.inputDialogFilterBox = function(opt) |
---|
1034 | { |
---|
1035 | var options= |
---|
1036 | { |
---|
1037 | width:350, |
---|
1038 | height:170, |
---|
1039 | title:'Filter box', |
---|
1040 | buttons: |
---|
1041 | { |
---|
1042 | ok:'Ok', |
---|
1043 | cancel:'Cancel', |
---|
1044 | erase:null |
---|
1045 | }, |
---|
1046 | filter: |
---|
1047 | { |
---|
1048 | dataType:'' |
---|
1049 | }, |
---|
1050 | errorMessage:inputFilterBoxLang['invalidParam'], |
---|
1051 | change:null |
---|
1052 | }, |
---|
1053 | objects= |
---|
1054 | { |
---|
1055 | dialogBox:$('<div/>'), |
---|
1056 | filterBox:$('<div/>', {'class':'filterBox'} ) |
---|
1057 | }, |
---|
1058 | |
---|
1059 | setOptions = function (opt) |
---|
1060 | { |
---|
1061 | if(opt.width!=null && opt.width>0) options.width=opt.width; |
---|
1062 | if(opt.height!=null && opt.height>0) options.height=opt.height; |
---|
1063 | if(opt.errorMessage!=null && opt.errorMessage!='') options.errorMessage=opt.errorMessage; |
---|
1064 | if(opt.title) options.title=opt.title; |
---|
1065 | if($.isValidFilter(opt.filter)) options.filter=opt.filter; |
---|
1066 | if(opt.buttons && opt.buttons.ok) options.buttons.ok=opt.buttons.ok; |
---|
1067 | if(opt.buttons && opt.buttons.cancel) options.buttons.cancel=opt.buttons.cancel; |
---|
1068 | if(opt.buttons && opt.buttons.erase) options.buttons.erase=opt.buttons.erase; |
---|
1069 | if(opt.change && $.isFunction(opt.change)) options.change=opt.change; |
---|
1070 | }, |
---|
1071 | |
---|
1072 | initDialog = function () |
---|
1073 | { |
---|
1074 | var dialogOpt={}, |
---|
1075 | dialogButtons={}; |
---|
1076 | |
---|
1077 | dialogButtons[options.buttons.ok] = function (event) |
---|
1078 | { |
---|
1079 | if(objects.filterBox.inputFilterBox('isValid')) |
---|
1080 | { |
---|
1081 | if(options.change) |
---|
1082 | { |
---|
1083 | options.change(event, objects.filterBox.inputFilterBox('filter') ); |
---|
1084 | } |
---|
1085 | $(this).dialog('close'); |
---|
1086 | } |
---|
1087 | else |
---|
1088 | { |
---|
1089 | alert(options.errorMessage); |
---|
1090 | } |
---|
1091 | }; |
---|
1092 | |
---|
1093 | dialogButtons[options.buttons.cancel] = function (event) |
---|
1094 | { |
---|
1095 | $(this).dialog('close'); |
---|
1096 | }; |
---|
1097 | |
---|
1098 | if(options.buttons.erase!=null) |
---|
1099 | dialogButtons[options.buttons.erase]=function (event) |
---|
1100 | { |
---|
1101 | if(options.change) |
---|
1102 | { |
---|
1103 | options.change(event, null); |
---|
1104 | } |
---|
1105 | $(this).dialog('close'); |
---|
1106 | }; |
---|
1107 | |
---|
1108 | dialogOpt= |
---|
1109 | { |
---|
1110 | width:options.width, |
---|
1111 | height:options.height, |
---|
1112 | closeText:'x', |
---|
1113 | dialogClass:'ui-inputDialogFilterBox', |
---|
1114 | modal:true, |
---|
1115 | resizable:true, |
---|
1116 | title:options.title, |
---|
1117 | buttons:dialogButtons, |
---|
1118 | open: open= function () |
---|
1119 | { |
---|
1120 | objects.filterBox |
---|
1121 | .inputFilterBox(options.filter); |
---|
1122 | }, |
---|
1123 | close: function () |
---|
1124 | { |
---|
1125 | objects.filterBox.inputFilterBox('destroy').remove(); |
---|
1126 | $(this).dialog('destroy').remove(); |
---|
1127 | } |
---|
1128 | }; |
---|
1129 | |
---|
1130 | objects.dialogBox |
---|
1131 | .append(objects.filterBox) |
---|
1132 | .dialog(dialogOpt); |
---|
1133 | }; |
---|
1134 | |
---|
1135 | setOptions(opt); |
---|
1136 | if(!$.isValidFilter(options.filter)) |
---|
1137 | { |
---|
1138 | alert('[Technical error] '+inputFilterBoxLang['invalidParam']); |
---|
1139 | } |
---|
1140 | else |
---|
1141 | { |
---|
1142 | initDialog(); |
---|
1143 | } |
---|
1144 | |
---|
1145 | } // $.fn.inputFilterBox |
---|