source: extensions/GrumPluginClasses/js/ui.inputFilterBox.js @ 17757

Last change on this file since 17757 was 17757, checked in by grum, 12 years ago

version 3.5.4
GPCExport

. Improve ODS formatting
. Fix bug in SQLite export options

FilterBox

. add 'not between' filter

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