source: extensions/GrumPluginClasses/js/ui.iconSelector.js @ 23346

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

feature:2634- compatibility with Piwigo 2.4
+add some objects on js framework

  • Property svn:executable set to *
File size: 19.5 KB
Line 
1/**
2 * -----------------------------------------------------------------------------
3 * file: ui.iconSelector.js
4 * file version: 1.0.1
5 * date: 2012-06-18
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 *  Options         Type                 Default value
18 *  - images        Array<String>        []
19 *      Define the list at initialization
20 *        $(".selector").iconSelector({images:['url1', 'url2', ..., 'urlN']});
21 *
22 *      Set/get the images list after initialisation
23 *        $(".selector").iconSelector('images', ['url1', 'url2', ..., 'urlN']);
24 *        $(".selector").iconSelector('images');
25 *
26 *  - numCols       Integer              1
27 *  - numRows       Integer              8
28 *      Define the number of cols/rows displayed
29 *      If number of icon is greater than numCols*numRows, a scrollbar allows to
30 *      scroll on the list
31 *        $(".selector").iconSelector({numCols:4, numRows:4});
32 *
33 *      Set/get the numCols/numRows after initialisation
34 *        $(".selector").iconSelector('numCols', 4);
35 *        $(".selector").iconSelector('numCols');
36 *
37 *  - cellWidth     Integer              32
38 *  - cellHeight    Integer              32
39 *      Define the width/height of cells in the list
40 *      If number of icon is greater than numCols*numRows, a scrollbar allows to
41 *      scroll on the list
42 *        $(".selector").iconSelector({cellWidth:40, cellHeight:20});
43 *
44 *      Set/get the cellWidth/cellHeight after initialisation
45 *        $(".selector").iconSelector('cellHeight', 20);
46 *        $(".selector").iconSelector('cellHeight');
47 *
48 *  - value         String               (first value of image list)
49 *      Define the current selected image
50 *      can also take special values ':first' or ':last'
51 *        $(".selector").iconSelector({value:'urlX'});
52 *
53 *      Set/get the selected icon after initialisation
54 *        $(".selector").iconSelector('value', 'urlX');
55 *        $(".selector").iconSelector('value');
56 *
57 *
58 *  Events
59 *  - popup
60 *      Triggered when the selection list is opened/closed
61 *      One parameter is given (selection list is visible or not)
62 *        $(".selector").iconSelector({popup: function (event, visible) { ... } } );
63 *
64 *      To bind on the event :
65 *        $(".selector").bind('iconSelectorPopup', function (event, visible) { ... } );
66 *
67 *  - change
68 *      Triggered when the selected in has changed
69 *      One parameter is given (the selected value)
70 *        $(".selector").iconSelector({change: function (event, value) { ... } } );
71 *
72 *      To bind on the event :
73 *        $(".selector").bind('iconSelectorChange', function (event, value) { ... } );
74 *
75 *
76 *  Styles
77 *  .ui-icon-selector               : CSS class for the main object
78 *  .ui-icon-selector-list          : CSS class for the icon list container
79 *  .ui-icon-selector-icon          : CSS class for icons
80 *  .ui-icon-selector-selected-icon : CSS class for the selected icon
81 *
82 *
83 * :: HISTORY ::
84 *
85 * | release | date       |
86 * | 1.0.0   | 2010/10/10 | first release
87 * |         |            |
88 * | 1.0.1   | 2012/06/18 | * imoprove memory managment
89 * |         |            |
90 * |         |            |
91 * |         |            |
92 * |         |            |
93 * |         |            |
94 *
95 */
96
97
98
99(
100  function($)
101  {
102    /*
103     * plugin 'public' functions
104     */
105    var publicMethods =
106    {
107      init : function (opt)
108        {
109          return this.each(function()
110            {
111              // default values for the plugin
112              var $this=$(this),
113                  data = $this.data('options'),
114                  objects = $this.data('objects'),
115                  properties = $this.data('properties'),
116                  options =
117                    {
118                      images:[],
119                      numCols:1,
120                      numRows:8,
121                      cellWidth:32,
122                      cellHeight:32,
123                      popup:null,
124                      change:null
125                    };
126
127              // if options given, merge it
128              if(opt) $.extend(options, opt);
129
130              $this.data('options', options);
131
132              if(!properties)
133              {
134                $this.data('properties',
135                  {
136                    index:-1,
137                    initialized:false,
138                    selectorVisible:false
139                  }
140                );
141                properties=$this.data('properties');
142              }
143
144              if(!objects)
145              {
146                objects =
147                  {
148                    container:$('<div/>',
149                        {
150                          'class':' ui-icon-selector '
151                        }
152                    ).bind('click.iconSelector',
153                        function ()
154                        {
155                          privateMethods.displaySelector($this, !$this.data('properties').selectorVisible);
156                        }
157                      ),
158                    listContainer:$('<div/>',
159                        {
160                          html: "",
161                          'class':' ui-icon-selector-list ',
162                          css: {
163                            overflow:"auto",
164                            width:'0px',
165                            height:'0px',
166                            display:'none',
167                            position:'absolute'
168                          }
169                        }
170                    ).bind('mouseleave.iconSelector',
171                        function ()
172                        {
173                          privateMethods.displaySelector($this, false);
174                        }
175                      ),
176                    list:$('<ul/>',
177                      {
178                        css: {
179                          listStyle:'none',
180                          padding:'0px',
181                          margin:'0px'
182                        }
183                      }
184                    )
185                  };
186
187                $this
188                  .html('')
189                  .append(objects.container)
190                  .append(objects.listContainer.append(objects.list));
191
192
193                $this.data('objects', objects);
194              }
195
196              privateMethods.setImages($this, options.images);
197              privateMethods.setCellWidth($this, options.cellWidth);
198              privateMethods.setCellHeight($this, options.cellHeight);
199              privateMethods.setNumCols($this, options.numCols);
200              privateMethods.setNumRows($this, options.numRows);
201              privateMethods.setEventPopup($this, options.popup);
202              privateMethods.setEventChange($this, options.change);
203              if(options.images.length>0) privateMethods.setValue($this, options.images[0]);
204
205              properties.initialized=true;
206            }
207          );
208        }, // init
209      destroy : function ()
210        {
211          return this.each(
212            function()
213            {
214              // default values for the plugin
215              var $this=$(this),
216                  objects = $this.data('objects');
217              objects.container.unbind().remove();
218              objects.list.children().unbind();
219              objects.listContainer.remove();
220              $this
221                .unbind('.iconSelector')
222                .removeData()
223                .css(
224                  {
225                    width:'',
226                    height:'',
227                    backgroundImage:''
228                  }
229                );
230              delete $this;
231            }
232          );
233        }, // destroy
234      images : function (list)
235        {
236          if(list)
237          {
238            // set images list values
239            return this.each(function()
240              {
241                privateMethods.setImages($(this), list);
242              }
243            );
244          }
245          else
246          {
247            // return images list values
248            var options = this.data('options');
249
250            if(options)
251            {
252              return(options.images);
253            }
254            else
255            {
256              return([]);
257            }
258          }
259        }, // images
260      numCols: function (value)
261        {
262          if(value)
263          {
264            // set numCols values
265            return this.each(function()
266              {
267                privateMethods.setCols($(this), value);
268              }
269            );
270          }
271          else
272          {
273            // return images list values
274            var options = this.data('options');
275
276            if(options)
277            {
278              return(options.numCols);
279            }
280            else
281            {
282              return(0);
283            }
284          }
285        }, // numCols
286      numRows: function (value)
287        {
288          if(value)
289          {
290            // set numRows values
291            return this.each(function()
292              {
293                privateMethods.setRows($(this), value);
294              }
295            );
296          }
297          else
298          {
299            // return images list values
300            var options = this.data('options');
301
302            if(options)
303            {
304              return(options.numRows);
305            }
306            else
307            {
308              return(0);
309            }
310          }
311        }, // numRows
312      cellWidth: function (value)
313        {
314          if(value)
315          {
316            // set cell width values
317            return this.each(function()
318              {
319                privateMethods.setCellWidth($(this), value);
320              }
321            );
322          }
323          else
324          {
325            // return images list values
326            var options = this.data('options');
327
328            if(options)
329            {
330              return(options.cellWidth);
331            }
332            else
333            {
334              return(0);
335            }
336          }
337        }, // cellWidth
338      cellHeight: function (value)
339        {
340          if(value)
341          {
342            // set cell width values
343            return this.each(function()
344              {
345                privateMethods.setCellHeight($(this), value);
346              }
347            );
348          }
349          else
350          {
351            // return images list values
352            var options = this.data('options');
353
354            if(options)
355            {
356              return(options.cellHeight);
357            }
358            else
359            {
360              return(0);
361            }
362          }
363        }, // cellHeight
364      value: function (value)
365        {
366          if(value)
367          {
368            // set selected value
369            return this.each(function()
370              {
371                privateMethods.setValue($(this), value);
372              }
373            );
374          }
375          else
376          {
377            // return the selected value
378            var options=this.data('options'),
379                properties=this.data('properties');
380
381            if(options && properties && properties.index>-1 && properties.index<options.images.length)
382            {
383              return(options.images[properties.index]);
384            }
385            else
386            {
387              return('');
388            }
389          }
390        }, // value
391      popup: function (value)
392        {
393          if(value && $.isFunction(value))
394          {
395            // set selected value
396            return this.each(function()
397              {
398                privateMethods.setEventPopup($(this), value);
399              }
400            );
401          }
402          else
403          {
404            // return the selected value
405            var options=this.data('options');
406
407            if(options)
408            {
409              return(options.popup);
410            }
411            else
412            {
413              return(null);
414            }
415          }
416        }, // popup
417      change: function (value)
418        {
419          if(value && $.isFunction(value))
420          {
421            // set selected value
422            return this.each(function()
423              {
424                privateMethods.setEventChange($(this), value);
425              }
426            );
427          }
428          else
429          {
430            // return the selected value
431            var options=this.data('options');
432
433            if(options)
434            {
435              return(options.change);
436            }
437            else
438            {
439              return(null);
440            }
441          }
442        } // popup
443
444    }; // methods
445
446
447    /*
448     * plugin 'private' methods
449     */
450    var privateMethods =
451    {
452      updateListArea : function (object)
453        {
454          var options=object.data('options'),
455              objects=object.data('objects'),
456              icon=objects.list.children().first(),
457              width=icon.outerWidth()*options.numCols,
458              height=icon.outerHeight()*options.numRows;
459
460          objects.listContainer.css(
461            {
462              width:width+'px',
463              height:height+'px'
464            }
465          );
466
467          delta = width-objects.listContainer.get(0).clientWidth;
468          // adjust size if scrollbars are present
469          if(delta>0)
470          {
471            objects.listContainer.css('width', (width+delta)+'px');
472          }
473        },
474      setImages : function (object, value)
475        {
476          var options=object.data('options'),
477              objects=object.data('objects'),
478              properties=object.data('properties');
479          options.images=value;
480
481          objects.list.children().unbind();
482          objects.list.html('');
483          for(var i=0;i<options.images.length;i++)
484          {
485            liClass=' ui-icon-selector-icon ';
486            if(i==properties.index)
487            {
488              liClass+=' ui-icon-selector-selected-icon ';
489            }
490            objects.list.append(
491              $('<li indexValue="'+i+'" class="'+liClass+'" style="display:inline-block;width:'+options.cellWidth+'px;height:'+options.cellHeight+'px;background-image:url('+options.images[i]+');"></li>')
492                .bind('click',
493                  {object:object},
494                  function (event)
495                  {
496                    privateMethods.setValueByIndex(event.data.object, $(this).attr('indexValue'), true);
497                    privateMethods.displaySelector(event.data.object, false);
498                  }
499                )
500            );
501          }
502
503          return(options.images);
504        },
505      setNumCols : function (object, value)
506        {
507          var options=object.data('options'),
508              properties=object.data('properties');
509          if((!properties.initialized || options.numCols!=value) && value>0)
510          {
511            options.numCols=value;
512          }
513          return(options.numCols);
514        },
515      setNumRows : function (object, value)
516        {
517          var options=object.data('options'),
518              properties=object.data('properties');
519          if((!properties.initialized || options.numRows!=value) && value>0)
520          {
521            options.numRows=value;
522          }
523          return(options.numRows);
524        },
525      setCellWidth : function (object, value)
526        {
527          var options=object.data('options'),
528              properties=object.data('properties'),
529              objects=object.data('objects');
530          if((!properties.initialized || options.cellWidth!=value) && value>=0)
531          {
532            options.cellWidth=value;
533            objects.container.css('width', value+'px');
534          }
535          return(options.cellWidth);
536        },
537      setCellHeight : function (object, value)
538        {
539          var options=object.data('options'),
540              properties=object.data('properties'),
541              objects=object.data('objects');
542          if((!properties.initialized || options.cellHeight!=value) && value>=0)
543          {
544            options.cellHeight=value;
545            objects.container.css('height', value+'px');
546          }
547          return(options.cellHeight);
548        },
549      setValue : function (object, value)
550        {
551          var options=object.data('options'),
552              properties=object.data('properties'),
553              index=-1;
554
555          switch(value)
556          {
557            case ':first':
558              if(options.images.length>0) index=0;
559              break;
560            case ':last':
561              index=options.images.length-1;
562              break;
563            default:
564              index=$.inArray(value, options.images);
565              break;
566          }
567
568          if((!properties.initialized || properties.index!=index) && index>-1)
569          {
570            privateMethods.setValueByIndex(object, index, false);
571          }
572          return(options.images[properties.index]);
573        },
574      setValueByIndex : function (object, value, trigger)
575        {
576          var options=object.data('options'),
577              properties=object.data('properties'),
578              objects=object.data('objects');
579          if((!properties.initialized || properties.index!=value) && value>-1 && value<options.images.length)
580          {
581            objects.list.children('.ui-icon-selector-selected-icon').removeClass('ui-icon-selector-selected-icon');
582            objects.list.children('[indexValue="'+value+'"]').addClass('ui-icon-selector-selected-icon');
583            properties.index=value;
584            objects.container.css('background-image', 'url('+options.images[properties.index]+')');
585            if(trigger && options.change) object.trigger('iconSelectorChange', [properties.index]);
586          }
587          return(options.images[properties.index]);
588        },
589      displaySelector : function (object, value)
590        {
591          var options=object.data('options'),
592              properties=object.data('properties'),
593              objects=object.data('objects');
594          if(properties.selectorVisible!=value)
595          {
596            properties.selectorVisible=value;
597
598            if(properties.selectorVisible)
599            {
600              objects.listContainer
601                .css('display', 'block')
602                .scrollTop(objects.listContainer.scrollTop()+objects.list.children('[indexValue="'+properties.index+'"]').position().top);
603              privateMethods.updateListArea(object);
604            }
605            else
606            {
607              objects.listContainer.css('display', 'none');
608            }
609            if(options.popup) object.trigger('iconSelectorPopup', [properties.selectorVisible]);
610          }
611          return(properties.selectorVisible);
612        },
613      setEventPopup : function (object, value)
614        {
615          var options=object.data('options');
616          options.popup=value;
617          object.unbind('iconSelectorPopup');
618          if(value) object.bind('iconSelectorPopup', options.popup);
619          return(options.popup);
620        },
621      setEventChange : function (object, value)
622        {
623          var options=object.data('options');
624          options.change=value;
625          object.unbind('iconSelectorChange');
626          if(value) object.bind('iconSelectorChange', options.change);
627          return(options.change);
628        }
629    };
630
631
632    $.fn.iconSelector = function(method)
633    {
634      if(publicMethods[method])
635      {
636        return publicMethods[method].apply( this, Array.prototype.slice.call( arguments, 1 ));
637      }
638      else if(typeof method === 'object' || ! method)
639      {
640        return publicMethods.init.apply(this, arguments);
641      }
642      else
643      {
644        $.error( 'Method ' +  method + ' does not exist on jQuery.iconSelector' );
645      }
646    } // $.fn.iconSelector
647
648  }
649)(jQuery);
650
651
Note: See TracBrowser for help on using the repository browser.