source: extensions/GrumPluginClasses/js/ui.inputExportBox.js @ 19961

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

version 3.5.4 - fix minor bug & add new functions to framework

  • Property svn:executable set to *
File size: 28.0 KB
Line 
1/**
2 * -----------------------------------------------------------------------------
3 * file: ui.inputExportBox.js
4 * file version: 1.0.0
5 * date: 2012-09-02
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://www.grum.fr
13 *   PWG user : http://forum.piwigo.org/profile.php?id=3706
14 *
15 *   << May the Little SpaceFrog be with you ! >>
16 * -----------------------------------------------------------------------------
17 *
18 *
19 *
20 *
21 * :: HISTORY ::
22 *
23 * | release | date       |
24 * | 1.0.0   | 2012/09/02 | first release
25 * |         |            |
26 * |         |            |
27 * |         |            |
28 * |         |            |
29 * |         |            |
30 * |         |            |
31 *
32 */
33
34var inputExportBoxLang={
35    'export':'Export',
36    'name':'Name',
37    'format':'Format',
38    'compression':'Compression'
39  };
40
41
42(
43  function($)
44  {
45    /*
46     * plugin 'public' functions
47     */
48    var publicMethods =
49    {
50      init : function (opt)
51        {
52          return this.each(function()
53            {
54              // default values for the plugin
55              var $this=$(this),
56                  timeStamp=new Date(),
57                  data = $this.data('options'),
58                  objects = $this.data('objects'),
59                  properties = $this.data('properties'),
60                  options =
61                    {
62                      chooseFormat:true,
63                      chooseCompression:true,
64                      chooseName:true,
65                      formatList:[],
66                      compressionList:[],
67                      name:'',
68                      format:'',
69                      compression:''
70                    };
71
72              // if options given, merge it
73              // if(opt) $.extend(options, opt); ==> options are set by setters
74
75              $this.data('options', options);
76
77
78              if(!properties)
79              {
80                $this.data('properties',
81                  {
82                    isValid:false
83                  }
84                );
85                properties=$this.data('properties');
86              }
87
88              if(!objects)
89              {
90                objects =
91                  {
92                    content:$('<table/>',
93                        {
94                          'class':'ui-inputExportBox-content'
95                        }
96                      ),
97                    nameRow:$('<tr/>',
98                        {
99                          'class':'ui-inputExportBox-contentRow'
100                        }
101                      ),
102                    nameInput:$('<div/>',
103                        {
104                          'class':'ui-inputExportBox-inputName'
105                        }
106                      ),
107                    formatRow:$('<tr/>',
108                        {
109                          'class':'ui-inputExportBox-contentRow'
110                        }
111                      ),
112                    formatInput:$('<div/>',
113                        {
114                          'class':'ui-inputExportBox-inputFormat'
115                        }
116                      ),
117                    compressionRow:$('<tr/>',
118                        {
119                          'class':'ui-inputExportBox-contentRow'
120                        }
121                      ),
122                    compressionInput:$('<div/>',
123                        {
124                          'class':'ui-inputExportBox-inputCompression'
125                        }
126                      )
127                  };
128
129                $this
130                  .html('')
131                  .addClass('ui-inputExportBox')
132                  .append(objects.content
133                            .append(objects.nameRow
134                                      .append($('<td/>', {'html':inputExportBoxLang['name']}))
135                                      .append($('<td/>').append(objects.nameInput))
136                                   )
137                            .append(objects.formatRow
138                                      .append($('<td/>', {'html':inputExportBoxLang['format']}))
139                                      .append($('<td/>').append(objects.formatInput))
140                                   )
141                            .append(objects.compressionRow
142                                      .append($('<td/>', {'html':inputExportBoxLang['compression']}))
143                                      .append($('<td/>').append(objects.compressionInput))
144                                   )
145                         );
146
147                objects.nameInput.inputText(
148                  {
149                    regExp:'/^([a-z0-9_\.]|-)+$/i',
150                    change: function (event, value)
151                      {
152                        options.name=value;
153                        properties.isValid=$(this).inputText('isValid');
154                      }
155                  }
156                );
157                objects.formatInput.inputList(
158                  {
159                    listMaxWidth:500,
160                    listMaxHeight:200,
161                    multiple:false,
162                    colsWidth:[],
163                    colsDisplayed:[0,1],
164                    colsCss:["ui-inputExportBox-formatInput-value","ui-inputExportBox-formatInput-info"],
165                    change: function (event, value)
166                      {
167                        options.format=value;
168                      }
169                  }
170                );
171                objects.compressionInput.inputList(
172                  {
173                    listMaxWidth:500,
174                    listMaxHeight:200,
175                    multiple:false,
176                    colsWidth:[],
177                    colsDisplayed:[0,1],
178                    colsCss:["ui-inputExportBox-compressionInput-value","ui-inputExportBox-compressionInput-info"],
179                    change: function (event, value)
180                      {
181                        options.compression=value;
182                      }
183                  }
184                );
185
186                $this.data('objects', objects);
187              }
188
189              privateMethods.setOptions($this, opt);
190            }
191          );
192        }, // init
193      destroy : function ()
194        {
195          return this.each(
196            function()
197            {
198              // default values for the plugin
199              var $this=$(this),
200                  objects = $this.data('objects');
201              objects.compressionInput.inputList('destroy').remove();
202              objects.compressionRow.remove();
203              objects.formatInput.inputList('destroy').remove();
204              objects.formatRow.remove();
205              objects.nameInput.remove();
206              objects.nameRow.remove();
207              objects.content.remove();
208
209              delete objects.compressionInput;
210              delete objects.compressionRow;
211              delete objects.formatInput;
212              delete objects.formatRow;
213              delete objects.nameInput;
214              delete objects.nameRow;
215              delete objects.content;
216
217              $this.removeClass('ui-inputExportBox');
218            }
219          );
220        }, // destroy
221
222      options: function (value)
223        {
224          if(value!=null)
225          {
226            return(
227              this.each(
228                function()
229                {
230                  privateMethods.setOptions($(this), value);
231                }
232              )
233            );
234          }
235          else
236          {
237            var options=this.data('options');
238            return(options);
239          }
240        }, // options
241
242      // set or return the value for chooseName option
243      chooseName: function (value)
244        {
245          if(value!=null)
246          {
247            // set selected value
248            return(
249              this.each(
250                function()
251                {
252                  privateMethods.setChooseName($(this), value);
253                }
254              )
255            );
256          }
257          else
258          {
259            var options=this.data('options');
260            return(options.chooseName);
261          }
262        }, // chooseName
263
264      // set or return the value for chooseFormat option
265      chooseFormat: function (value)
266        {
267          if(value!=null)
268          {
269            // set selected value
270            return(
271              this.each(
272                function()
273                {
274                  privateMethods.setChooseFormat($(this), value);
275                }
276              )
277            );
278          }
279          else
280          {
281            var options=this.data('options');
282            return(options.chooseFormat);
283          }
284        }, // chooseFormat
285
286      // set or return the value for chooseCompression option
287      chooseCompression: function (value)
288        {
289          if(value!=null)
290          {
291            // set selected value
292            return(
293              this.each(
294                function()
295                {
296                  privateMethods.setChooseCompression($(this), value);
297                }
298              )
299            );
300          }
301          else
302          {
303            var options=this.data('options');
304            return(options.chooseCompression);
305          }
306        }, // chooseCompression
307
308      // set or return the available formats
309      formatList: function (value)
310        {
311          if(value!=null)
312          {
313            // set selected value
314            return(
315              this.each(
316                function()
317                {
318                  privateMethods.setFormatList($(this), value);
319                }
320              )
321            );
322          }
323          else
324          {
325            var options=this.data('options');
326            return(options.formatList);
327          }
328        }, // formatList
329
330      // set or return the available compression methods
331      compressionList: function (value)
332        {
333          if(value!=null)
334          {
335            // set selected value
336            return(
337              this.each(
338                function()
339                {
340                  privateMethods.setCompressionList($(this), value);
341                }
342              )
343            );
344          }
345          else
346          {
347            var options=this.data('options');
348            return(options.compressionList);
349          }
350        }, // formatList
351
352
353      // set or return the name
354      name: function (value)
355        {
356          if(value!=null)
357          {
358            // set selected value
359            return(
360              this.each(
361                function()
362                {
363                  privateMethods.setName($(this), value);
364                }
365              )
366            );
367          }
368          else
369          {
370            var options=this.data('options');
371            return(options.name);
372          }
373        }, // name
374
375
376      // set or return the selected format
377      format: function (value)
378        {
379          if(value!=null)
380          {
381            // set selected value
382            return(
383              this.each(
384                function()
385                {
386                  privateMethods.setFormat($(this), value);
387                }
388              )
389            );
390          }
391          else
392          {
393            var options=this.data('options');
394            return(options.format);
395          }
396        }, // format
397
398      // set or return the selected compression
399      compression: function (value)
400        {
401          if(value!=null)
402          {
403            // set selected value
404            return(
405              this.each(
406                function()
407                {
408                  privateMethods.setCompression($(this), value);
409                }
410              )
411            );
412          }
413          else
414          {
415            var options=this.data('options');
416            return(options.compression);
417          }
418        }, // compression
419
420      // return true if export box is filled
421      isValid: function ()
422        {
423          var properties=this.data('properties');
424          privateMethods.checkValid($(this));
425          return(properties.isValid);
426        },
427
428      // add a new format to the list
429      addFormat: function (value)
430        {
431          if(value!=null)
432          {
433            // set selected value
434            return(
435              this.each(
436                function()
437                {
438                  privateMethods.addFormat($(this), value);
439                }
440              )
441            );
442          }
443        }, // addFormat
444
445      // remove a format from the list
446      removeFormat: function (value)
447        {
448          if(value!=null)
449          {
450            // set selected value
451            return(
452              this.each(
453                function()
454                {
455                  privateMethods.removeFormat($(this), value);
456                }
457              )
458            );
459          }
460        }, // removeFormat
461
462      // add a compression type to the list
463      addCompression: function (value)
464        {
465          if(value!=null)
466          {
467            // set selected value
468            return(
469              this.each(
470                function()
471                {
472                  privateMethods.addCompression($(this), value);
473                }
474              )
475            );
476          }
477        }, // addCompression
478
479      // set or return the name
480      removeCompression: function (value)
481        {
482          if(value!=null)
483          {
484            // set selected value
485            return(
486              this.each(
487                function()
488                {
489                  privateMethods.removeCompression($(this), value);
490                }
491              )
492            );
493          }
494        } // removeCompression
495
496    }; // methods
497
498
499    /*
500     * plugin 'private' methods
501     */
502    var privateMethods =
503    {
504      setOptions : function (object, value)
505        {
506          var properties=object.data('properties'),
507              options=object.data('options'),
508              objects=object.data('objects');
509
510          if(!$.isPlainObject(value)) return(false);
511
512          properties.initialized=false;
513
514          privateMethods.setName(object, (value.name!=null)?value.name:options.name);
515          privateMethods.setCompressionList(object, (value.compressionList!=null)?value.compressionList:options.compressionList);
516          privateMethods.setFormatList(object, (value.formatList!=null)?value.formatList:options.formatList);
517          privateMethods.setCompression(object, (value.compression!=null)?value.compression:options.compression);
518          privateMethods.setFormat(object, (value.format!=null)?value.format:options.format);
519          privateMethods.setChooseName(object, (value.chooseName!=null)?value.chooseName:options.chooseName);
520          privateMethods.setChooseCompression(object, (value.chooseCompression!=null)?value.chooseCompression:options.chooseCompression);
521          privateMethods.setChooseFormat(object, (value.chooseFormat!=null)?value.chooseFormat:options.chooseFormat);
522
523          if((options.format==null || options.format=='') && options.chooseFormat)
524            options.format=objects.formatInput.inputList('value', ':first').inputList('value');
525
526          if((options.compression==null || options.compression=='') && options.chooseCompression)
527            options.compression=objects.compressionInput.inputList('value', ':first').inputList('value');
528
529
530          properties.initialized=true;
531        },
532
533
534      checkValid: function (object)
535        {
536          var options=object.data('options'),
537              properties=object.data('properties'),
538              objects=object.data('objects');
539
540          properties.isValid=objects.nameInput.inputText('isValid', 'check').inputText('isValid');
541        },
542
543      setChooseCompression: function (object, value)
544        {
545          var options=object.data('options'),
546              properties=object.data('properties'),
547              objects=object.data('objects');
548
549          if((!properties.initialized || value!=options.chooseCompression) && (value==true || value==false))
550          {
551            options.chooseCompression=value;
552            objects.compressionRow.css('display', options.chooseCompression?'table-row':'none');
553          }
554          return(options.chooseCompression);
555        }, // setChooseCompression
556
557      setChooseFormat: function (object, value)
558        {
559          var options=object.data('options'),
560              properties=object.data('properties'),
561              objects=object.data('objects');
562
563          if((!properties.initialized || value!=options.chooseFormat) && (value==true || value==false))
564          {
565            options.chooseFormat=value;
566            objects.formatRow.css('display', options.chooseFormat?'table-row':'none');
567          }
568          return(options.chooseFormat);
569        }, // setChooseFormat
570
571      setChooseName: function (object, value)
572        {
573          var options=object.data('options'),
574              properties=object.data('properties'),
575              objects=object.data('objects');
576
577          if((!properties.initialized || value!=options.chooseName) && (value==true || value==false))
578          {
579            options.chooseName=value;
580            objects.formatRow.css('display', options.chooseName?'table-row':'none');
581          }
582          return(options.chooseName);
583        }, // setChooseName
584
585      setName: function (object, value)
586        {
587          var options=object.data('options'),
588              properties=object.data('properties'),
589              objects=object.data('objects');
590
591          if(!properties.initialized || value!=options.name)
592          {
593            options.name=value;
594            objects.nameInput.inputText('value', options.name);
595          }
596          return(options.name);
597        }, // setName
598
599      setFormat: function (object, value)
600        {
601          var options=object.data('options'),
602              properties=object.data('properties'),
603              objects=object.data('objects');
604
605          if(!properties.initialized || value!=null)
606          {
607            options.format=objects.formatInput.inputList('value', value).inputList('value');
608          }
609          return(options.format);
610        }, // setFormat
611
612      setCompression: function (object, value)
613        {
614          var options=object.data('options'),
615              properties=object.data('properties'),
616              objects=object.data('objects');
617
618          if(!properties.initialized || value!=null)
619          {
620            options.compression=objects.compressionInput.inputList('value', value).inputList('value');
621          }
622          return(options.compression);
623        }, // setCompression
624
625
626      setCompressionList: function (object, value)
627        {
628          var options=object.data('options'),
629              properties=object.data('properties'),
630              objects=object.data('objects');
631
632          if(!properties.initialized || $.isArray(value))
633          {
634            privateMethods.removeCompression(object, 'all');
635            for(var i=0;i<value.length;i++)
636            {
637              privateMethods.addCompression(object, value[i]);
638            }
639          }
640          return(options.compressionList);
641        }, // setCompressionList
642
643      setFormatList: function (object, value)
644        {
645          var options=object.data('options'),
646              properties=object.data('properties'),
647              objects=object.data('objects');
648
649          if(!properties.initialized || $.isArray(value))
650          {
651            privateMethods.removeFormat(object, 'all');
652            for(var i=0;i<value.length;i++)
653            {
654              privateMethods.addFormat(object, value[i]);
655            }
656          }
657          return(options.formatList);
658        }, // setFormatList
659
660      addCompression: function (object, value)
661        {
662          var options=object.data('options'),
663              properties=object.data('properties'),
664              objects=object.data('objects');
665
666          if($.isPlainObject(value) &&
667             value.id!=null &&
668             value.id!='' &&
669             value.text!=null &&
670             value.text!=''
671            )
672          {
673            options.compressionList.push(
674              {
675                id:value.id,
676                text:value.text,
677                infos:(value.infos!=null)?value.infos:''
678              }
679            );
680          }
681          else if(value=='all')
682          {
683            options.compressionList=[];
684          }
685          privateMethods.applyListValues(object, 'compressionList');
686        }, // addCompression
687
688      removeCompression: function (object, value)
689        {
690          var options=object.data('options'),
691              properties=object.data('properties'),
692              objects=object.data('objects');
693
694          if(value!=null && value!='')
695          {
696            for(var i=0;i<options.compressionList.length;i++)
697            {
698              if(options.compressionList[i].id==value)
699              {
700                options.compressionList.splice(i, 1);
701                privateMethods.applyListValues(object, 'compressionList');
702                return(true);
703              }
704            }
705          }
706          return(false);
707        }, // removeCompression
708
709      addFormat: function (object, value)
710        {
711          var options=object.data('options'),
712              properties=object.data('properties'),
713              objects=object.data('objects');
714
715          if($.isPlainObject(value) &&
716             value.id!=null &&
717             value.id!='' &&
718             value.text!=null &&
719             value.text!=''
720            )
721          {
722            options.formatList.push(
723              {
724                id:value.id,
725                text:value.text,
726                infos:(value.infos!=null)?value.infos:''
727              }
728            );
729          }
730          else if(value=='all')
731          {
732            options.formatList=[];
733          }
734          privateMethods.applyListValues(object, 'formatList');
735        }, // addFormat
736
737      removeFormat: function (object, value)
738        {
739          var options=object.data('options'),
740              properties=object.data('properties'),
741              objects=object.data('objects');
742
743          if(value!=null && value!='')
744          {
745            for(var i=0;i<options.formatList.length;i++)
746            {
747              if(options.formatList[i].id==value)
748              {
749                options.formatList.splice(i, 1);
750                privateMethods.applyListValues(object, 'formatList');
751                return(true);
752              }
753            }
754          }
755          return(false);
756        }, // removeFormat
757
758      applyListValues: function (object, list)
759        {
760          var options=object.data('options'),
761              properties=object.data('properties'),
762              objects=object.data('objects'),
763              target=null,
764              source=null,
765              values=[];
766
767          switch(list)
768          {
769            case 'compressionList':
770              target=objects.compressionInput;
771              source=options.compressionList;
772              break;
773            case 'formatList':
774              target=objects.formatInput;
775              source=options.formatList;
776              break;
777          }
778
779          for(var i=0;i<source.length;i++)
780          {
781            values.push(
782              {
783                value:source[i].id,
784                cols:[source[i].text, source[i].infos]
785              }
786            );
787          }
788          target.inputList('items', values);
789        } // applyListValues
790
791    };
792
793    $.fn.inputExportBox = function(method)
794    {
795      if(publicMethods[method])
796      {
797        return publicMethods[method].apply( this, Array.prototype.slice.call( arguments, 1 ));
798      }
799      else if(typeof method === 'object' || ! method)
800      {
801        return publicMethods.init.apply(this, arguments);
802      }
803      else
804      {
805        $.error( 'Method ' +  method + ' does not exist on jQuery.inputExportBox' );
806      }
807    } // $.fn.inputExportBox
808
809  }
810)(jQuery);
811
812
813/**
814 * open a modal dialog box for export
815 */
816$.dialogExport = function (opt)
817{
818  var options=
819        {
820          width:350,
821          height:150,
822          title:'Export',
823          buttons:
824            {
825              ok:'Ok',
826              cancel:'Cancel'
827            },
828          chooseFormat:true,
829          chooseCompression:true,
830          chooseName:true,
831          formatList:[],
832          compressionList:[],
833          name:'',
834          format:'',
835          compression:'',
836          validExport:null,
837          cancelExport:null
838        },
839      objects=
840        {
841          dialogBox:$('<div/>'),
842          iExport:$('<div/>')
843        },
844
845  setOptions = function (opt)
846    {
847      if(opt.width!=null && opt.width>0) options.width=opt.width;
848      if(opt.height!=null && opt.height>0) options.height=opt.height;
849      if(opt.title) options.title=opt.title;
850      if(opt.buttons && opt.buttons.ok) options.buttons.ok=opt.buttons.ok;
851      if(opt.buttons && opt.buttons.cancel) options.buttons.cancel=opt.buttons.cancel;
852      if(opt.chooseFormat!=null && (opt.chooseFormat==true || opt.chooseFormat==false)) options.chooseFormat=opt.chooseFormat;
853      if(opt.chooseCompression!=null && (opt.chooseCompression==true || opt.chooseCompression==false)) options.chooseCompression=opt.chooseCompression;
854      if(opt.chooseName!=null && (opt.chooseName==true || opt.chooseName==false)) options.chooseName=opt.chooseName;
855      if(opt.formatList && $.isArray(opt.formatList)) options.formatList=opt.formatList;
856      if(opt.compressionList && $.isArray(opt.compressionList)) options.compressionList=opt.compressionList;
857      if(opt.name) options.name=opt.name;
858      if(opt.format) options.format=opt.format;
859      if(opt.compression) options.compression=opt.compression;
860
861      if(opt.validExport && $.isFunction(opt.validExport)) options.validExport=opt.validExport;
862      if(opt.cancelExport && $.isFunction(opt.cancelExport)) options.cancelExport=opt.cancelExport;
863    },
864
865  initDialog = function ()
866    {
867      var dialogOpt={},
868          dialogButtons={};
869
870      dialogButtons[options.buttons.ok] = function (event)
871        {
872          if(objects.iExport.inputExportBox('isValid'))
873          {
874            if(options.validExport)
875              options.validExport(event,
876                {
877                  name:objects.iExport.inputExportBox('name'),
878                  format:objects.iExport.inputExportBox('format'),
879                  compression:objects.iExport.inputExportBox('compression')
880                }
881              );
882
883            $(this).dialog('close');
884          }
885        };
886
887      dialogButtons[options.buttons.cancel] = function (event)
888        {
889          if(options.cancelExport)
890            options.cancelExport(event,
891              {
892                name:objects.iExport.inputExportBox('name'),
893                format:objects.iExport.inputExportBox('format'),
894                compression:objects.iExport.inputExportBox('compression')
895              }
896            );
897
898          $(this).dialog('close');
899        };
900
901      dialogOpt=
902          {
903            width:options.width,
904            height:options.height,
905            closeOnEscape:false,
906            closeText:'',
907            dialogClass:'ui-dialogExport',
908            modal:true,
909            resizable:false,
910            title:options.title,
911            buttons:dialogButtons,
912            open: open= function ()
913                    {
914                      objects.iExport
915                        .inputExportBox(
916                          {
917                            name:options.name,
918                            format:options.format,
919                            compression:options.compression,
920                            chooseCompression:options.chooseCompression,
921                            chooseFormat:options.chooseFormat,
922                            chooseName:options.chooseName,
923                            formatList:options.formatList,
924                            compressionList:options.compressionList
925                          }
926                        );
927                    },
928            close: function ()
929                    {
930                      objects.iExport.inputExportBox('destroy').remove();
931                      $(this).dialog('destroy').remove();
932                    }
933          };
934
935      objects.dialogBox
936        .append(objects.iExport)
937        .dialog(dialogOpt);
938    };
939
940  setOptions(opt);
941  initDialog();
942
943  return(objects.iExport);
944} // $.dialogExport
Note: See TracBrowser for help on using the repository browser.