source: extensions/GrumPluginClasses/js/ui.inputPath.js @ 17562

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

bug:2723
+ improve some GPC framework functionnalities

File size: 16.9 KB
Line 
1/**
2 * -----------------------------------------------------------------------------
3 * file: ui.inputPath.js
4 * file version: 1.0.0
5 * date: 2012-07-24
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 *
14 *   << May the Little SpaceFrog be with you ! >>
15 * -----------------------------------------------------------------------------
16 *
17 *
18 *
19 *
20 * :: HISTORY ::
21 *
22 * | release | date       |
23 * | 1.0.0   | 2012/07/24 | first release
24 * |         |            |
25 * |         |            |
26 * |         |            |
27 * |         |            |
28 * |         |            |
29 *
30 */
31
32
33
34(
35  function($)
36  {
37    /*
38     * plugin 'public' functions
39     */
40    var publicMethods =
41    {
42      init : function (opt)
43        {
44          return this.each(
45            function()
46            {
47              // default values for the plugin
48              var $this=$(this),
49                  timeStamp=new Date(),
50                  data = $this.data('options'),
51                  //objects = $this.data('objects'), // no objects! they're all created dynamically
52                  properties = $this.data('properties'),
53                  options =
54                    {
55                      firstLevelClickable:1, // all item with a level stricly lower than this value are not clickable
56                      autoSetLevel:true, // if true, when item is clicked, level is set to item level
57                      separator:'/',
58                      click:null
59                    };
60
61              // if options given, merge it
62              // if(opt) $.extend(options, opt); ==> options are set by setters
63
64              $this.data('options', options);
65
66              if(!properties)
67              {
68                $this.data('properties',
69                  {
70                    objectId:'ip'+Math.ceil(timeStamp.getTime()*Math.random()),
71                    level:0,
72                    items:[]
73                  }
74                );
75                properties=$this.data('properties');
76              }
77
78              $this
79                .html('')
80                .addClass('ui-inputPath');
81
82              privateMethods.setOptions($this, opt);
83            }
84          );
85        }, // init
86      destroy : function ()
87        {
88          return this.each(
89            function()
90            {
91              // default values for the plugin
92              var $this=$(this),
93                  objects = $this.data('objects');
94              for(var i=0;i<objects.length;i++)
95              {
96                objects[i].unbind('click.inputPath').remove();
97              }
98              $this
99                .removeData()
100                .css(
101                  {
102                    width:'',
103                    height:''
104                  }
105                );
106              delete $this;
107            }
108          );
109        }, // destroy
110
111      options: function (value)
112        {
113          return(
114            this.each(
115              function()
116              {
117                privateMethods.setOptions($(this), value);
118              }
119            )
120          );
121        }, // options
122
123      push: function (value)
124        {
125          return(
126            this.each(
127              function()
128              {
129                privateMethods.push($(this), value);
130              }
131            )
132          );
133        }, // push
134
135      pop: function ()
136        {
137          return(
138            this.each(
139              function()
140              {
141                privateMethods.pop($(this));
142              }
143            )
144          );
145        }, // pop
146
147      separator: function (value)
148        {
149          if(value!=null)
150          {
151            // set separator
152            return(
153              this.each(
154                function()
155                {
156                  privateMethods.setSeparator($(this), value);
157                }
158              )
159            );
160          }
161          else
162          {
163            // return the current separator
164            var options=this.data('options');
165
166            if(properties)
167            {
168              return(options.separator);
169            }
170            else
171            {
172              return(null);
173            }
174          }
175        }, // separator
176
177      level: function (value)
178        {
179          if(value!=null)
180          {
181            // set selected value
182            return(
183              this.each(
184                function()
185                {
186                  privateMethods.setLevel($(this), value);
187                }
188              )
189            );
190          }
191          else
192          {
193            // return the selected value
194            var properties=this.data('properties');
195
196            if(properties)
197            {
198              return(properties.level);
199            }
200            else
201            {
202              return(null);
203            }
204          }
205        }, // level
206
207      firstLevelClickable : function (value)
208        {
209          if(value!=null)
210          {
211            // set selected value
212            return(
213              this.each(
214                function()
215                {
216                  privateMethods.setFirstLevelClickable($(this), value);
217                }
218              )
219            );
220          }
221          else
222          {
223            // return the selected value
224            var options=this.data('options');
225
226            if(options)
227            {
228              return(options.firstLevelClickable);
229            }
230            else
231            {
232              return(null);
233            }
234          }
235        }, // level
236
237      itemId: function (value)
238        {
239          if(value!=null && $.isPlainObject(value))
240          {
241            // set selected value
242            return(
243              this.each(
244                function()
245                {
246                  privateMethods.setItemId($(this), value);
247                }
248              )
249            );
250          }
251          else
252          {
253            // return the item from is id
254            return(privateMethods.getItemId($(this), value));
255          }
256        }, // itemId
257
258
259      itemLevel: function (value)
260        {
261          if(value!=null && $.isPlainObject(value))
262          {
263            // set selected value
264            return(
265              this.each(
266                function()
267                {
268                  privateMethods.setItemLevel($(this), value);
269                }
270              )
271            );
272          }
273          else
274          {
275            // return the item from the level value
276            return(privateMethods.getItemLevel($(this), value));
277          }
278        }, // itemId
279
280
281      click: function (value)
282        {
283          if(value!=null && $.isFunction(value))
284          {
285            // set selected value
286            return(
287              this.each(
288                function()
289                {
290                  privateMethods.setEventClick($(this), value);
291                }
292              )
293            );
294          }
295          else
296          {
297            // return the selected value
298            var options=this.data('options');
299
300            if(options)
301            {
302              return(options.click);
303            }
304            else
305            {
306              return(null);
307            }
308          }
309        } // click
310
311    }; // methods
312
313
314    /*
315     * plugin 'private' methods
316     */
317    var privateMethods =
318    {
319      setOptions : function (object, value)
320        {
321          var properties=object.data('properties'),
322              options=object.data('options');
323
324          if(!$.isPlainObject(value)) return(false);
325
326          properties.initialized=false;
327
328          privateMethods.setFirstLevelClickable(object, (value.firstLevelClickable!=null)?value.firstLevelClickable:options.firstLevelClickable);
329          privateMethods.setSeparator(object, (value.separator!=null)?value.separator:options.separator);
330          privateMethods.setAutoSetLevel(object, (value.autoSetLevel!=null)?value.autoSetLevel:options.autoSetLevel);
331
332          privateMethods.setEventClick(object, (value.click!=null)?value.click:options.click);
333
334          properties.initialized=true;
335        },
336
337
338      setAutoSetLevel : function (object, value)
339        {
340          var options=object.data('options'),
341              properties=object.data('properties');
342
343          if((!properties.initialized || value!=options.autoSetLevel) &&
344              (value==true || value==false)
345            )
346          {
347            options.autoSetLevel=value;
348          }
349
350          return(options.autoSetLevel);
351        }, //setAutoSetLevel
352
353      setSeparator : function (object, value)
354        {
355          var options=object.data('options'),
356              properties=object.data('properties');
357
358          if(!properties.initialized || value!=options.separator)
359          {
360            options.separator=value;
361            object.find('.ui-inputPath-separator').html(options.separator);
362          }
363
364          return(options.separator);
365        }, //setSeparator
366
367      push : function (object, value)
368        {
369          var cssClass='ui-inputPath-itemSelectable',
370              objSep=null,
371              objItem=null,
372              item={id:'', label:'', url:'', level:0},
373              properties=object.data('properties'),
374              options=object.data('options');
375
376          if($.isPlainObject(value) &&
377             value.id!=null &&
378             value.id!='' &&
379             value.label!=null &&
380             value.label!='' &&
381             privateMethods.getItemId(object, value.id)==null) // don't accept the same Id twice
382          {
383            item.id=value.id;
384            item.label=value.label;
385            if(value.url!=null) item.url=value.url;
386
387            properties.level++;
388            item.level=properties.level;
389
390            if(properties.level>1)
391            {
392              // add a separator before the item only if the item is not the first item
393              objSep=$('<span/>',
394                        {
395                          'class':'ui-inputPath-separator',
396                          'id':properties.objectId+'-sep-'+properties.level
397                        }
398                      )
399                      .html(options.separator);
400              object.append(objSep);
401            }
402
403            if(properties.level<options.firstLevelClickable)
404            {
405              object.find('.ui-inputPath-item')
406                      .addClass('ui-inputPath-itemNotSelectable')
407                      .removeClass('ui-inputPath-item');
408            }
409            else
410            {
411              object.find('.ui-inputPath-item')
412                      .addClass('ui-inputPath-itemSelectable')
413                      .removeClass('ui-inputPath-item')
414                      .bind('click.inputPath', object, privateMethods.bindClick);
415            }
416
417            objItem=$('<span/>',
418                        {
419                          'class':'ui-inputPath-item',
420                          'id':properties.objectId+'-item-'+properties.level,
421                          'ip-level':properties.level
422                        }
423                      )
424                      .html(item.label);
425
426            object.append(objItem);
427
428            properties.items.push(item);
429
430            return(true);
431          }
432          return(false);
433        }, // push
434
435      pop : function (object)
436        {
437          var objSep=null,
438              objItem=null
439              properties=object.data('properties');
440
441          if(properties.level>0) // level=0 means no items...
442          {
443            $('#'+properties.objectId+'-item-'+properties.level).remove();
444            $('#'+properties.objectId+'-sep-'+properties.level).remove();
445
446            properties.items.pop();
447            properties.level--;
448
449            $('#'+properties.objectId+'-item-'+properties.level)
450                .addClass('ui-inputPath-item')
451                .removeClass('ui-inputPath-itemSelectable ui-inputPath-itemNotSelectable')
452                .unbind('click.inputPath');
453            return(true);
454          }
455          return(false);
456        }, // pop
457
458      setLevel : function (object, value)
459        {
460          var properties=object.data('properties');
461
462          if(value>=0 && value<properties.level)
463          {
464            while(properties.level>value)
465            {
466              privateMethods.pop(object);
467            }
468          }
469          return(properties.level)
470        }, // setLevel
471
472      setFirstLevelClickable : function (object, value)
473        {
474          var options=object.data('options'),
475              properties=object.data('properties');
476
477          if(!properties.initialized || value>0 && value!=options.firstLevelClickable)
478          {
479            options.firstLevelClickable=value;
480
481            object.find('.ui-inputPath-itemSelectable').each(
482              function (index)
483              {
484                if($(this).attr('ip-level')<options.firstLevelClickable)
485                {
486                  $(this)
487                    .addClass('ui-inputPath-itemNotSelectable')
488                    .removeClass('ui-inputPath-itemSelectable')
489                    .unbind('click.inputPath');
490                }
491              }
492            );
493            object.find('.ui-inputPath-itemNotSelectable').each(
494              function (index)
495              {
496                if($(this).attr('ip-level')>=options.firstLevelClickable)
497                {
498                  $(this)
499                    .addClass('ui-inputPath-itemSelectable')
500                    .removeClass('ui-inputPath-itemNotSelectable')
501                    .bind('click.inputPath', object, privateMethods.bindClick);
502                }
503              }
504            );
505          }
506          return(options.firstLevelClickable)
507        }, // setLevel
508
509      setItemId : function (object, value)
510        {
511          var properties=object.data('properties');
512
513          if(value!=null &&
514             $.isPlainObject(value) &&
515             value.id!=null &&
516             value.id!='' &&
517             value.label!=null &&
518             value.label!=''
519          )
520
521          for(var i=0;i<properties.items.length;i++)
522          {
523            if(properties.items[i].id==value.id)
524            {
525              properties.items[i].label=value.label;
526              if(value.url!=null) properties.items[i].url=value.url;
527
528              return(properties.items[i]);
529            }
530          }
531          return(null);
532        }, // setItemId
533
534      getItemId : function (object, value)
535        {
536          var properties=object.data('properties');
537
538          if(value!=null)
539          {
540            for(var i=0;i<properties.items.length;i++)
541            {
542              if(properties.items[i].id==value)
543                return(properties.items[i]);
544            }
545          }
546          return(null);
547        }, // getItemId
548
549      setItemLevel : function (object, value)
550        {
551          var properties=object.data('properties');
552
553          if(value!=null &&
554             $.isPlainObject(value) &&
555             value.level!=null &&
556             value.level>0 &&
557             value.label!=null &&
558             value.label!=''
559          )
560
561          for(var i=0;i<properties.items.length;i++)
562          {
563            if(properties.items[i].level==value.level)
564            {
565              properties.items[i].label=value.label;
566              if(value.url!=null) properties.items[i].url=value.url;
567
568              $('#'+properties.objectId+'-item-'+properties.items[i].level).html(value.label);
569
570              return(properties.items[i]);
571            }
572          }
573          return(null);
574        }, // setItemLevel
575
576      getItemLevel : function (object, value)
577        {
578          var properties=object.data('properties');
579
580          if(value!=null)
581          {
582            for(var i=0;i<properties.items.length;i++)
583            {
584              if(properties.items[i].level==value)
585                return(properties.items[i]);
586            }
587          }
588          return(null);
589        }, // getItemLevel
590
591      setEventClick : function (object, value)
592        {
593          var options=object.data('options');
594
595          options.click=value;
596          object.unbind('inputPathClick');
597          if(value) object.bind('inputPathClick', options.click);
598          return(options.click);
599        }, // setEventClick
600
601      bindClick : function (event)
602        {
603          // event.data = object
604          var options=event.data.data('options');
605
606          if(options.click)
607          {
608            var item=privateMethods.getItemLevel(event.data, $(this).attr('ip-level'));
609            event.data.trigger('inputPathClick', item);
610            if(options.autoSetLevel)
611              privateMethods.setLevel(event.data, item.level);
612          }
613        }
614
615    };
616
617
618    $.fn.inputPath = function(method)
619    {
620      if(publicMethods[method])
621      {
622        return publicMethods[method].apply( this, Array.prototype.slice.call( arguments, 1 ));
623      }
624      else if(typeof method === 'object' || ! method)
625      {
626        return publicMethods.init.apply(this, arguments);
627      }
628      else
629      {
630        $.error( 'Method ' +  method + ' does not exist on jQuery.inputPath' );
631      }
632    } // $.fn.inputPath
633
634  }
635)(jQuery);
636
637
Note: See TracBrowser for help on using the repository browser.