Dotclear

Changeset 317:1997218a39ba for admin/js


Ignore:
Timestamp:
05/25/11 17:50:34 (14 years ago)
Author:
Tomtom33 <tbouron@…>
Branch:
wysiwyg
Message:

Improvements of dcToolBarManager object:

  • Added events manager
  • Improved JS scripts loading
Location:
admin/js
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • admin/js/_post.js

    r310 r317  
    8686          // Init toolbars 
    8787          $('#post_excerpt,#post_content').dctoolbarmanager({ 
     88               id: 'tinymce', 
    8889               mode: $(formatField).val(), 
    8990               context: 'post' 
     
    131132               hide: $('#post_excerpt').val() == '' 
    132133          }); 
    133            
    134           $('#post_content').dctoolbarmanager('draw'); 
    135134           
    136135          // Replace attachment remove links by a POST form submit 
  • admin/js/dcToolBarManager.js

    r310 r317  
    33          init: function(options) { 
    44               var settings = { 
    5                     mode: 'xhtml', 
    6                     context: null 
    7                }; 
     5                    id: null, 
     6                    mode: null, 
     7               }; 
     8               $.extend(settings,options); 
     9                
    810               return this.each(function(){ 
    911                    var _this = this; 
    10                     if (options) { 
    11                          $.extend(settings,options); 
    12                     } 
    1312                    $.each(settings, function(k,v) { 
    1413                         $.data(_this,k,v); 
    1514                    }); 
    16                     dcToolBarManager._load(settings.mode,this); 
     15                    dcToolBarManager._init(settings.mode,_this); 
    1716               }); 
    1817           
     
    2019          draw: function() { 
    2120               return this.each(function(){ 
     21                    if ($(this).data('toolbar') == null) { 
     22                         throw 'Toolbar should be initialize before render it'; 
     23                    } 
    2224                    dcToolBarManager._draw($(this).data('mode'),this); 
    2325               }); 
     
    3032                         $.data(this,'mode',mode); 
    3133                         dcToolBarManager._destroy($(this).data('mode'),this); 
    32                          dcToolBarManager._load(mode,this); 
    33                          dcToolBarManager._draw(mode,this); 
     34                         dcToolBarManager._init(mode,this); 
    3435                    } 
    3536               }); 
     
    3839      
    3940     $.fn.dctoolbarmanager = function(method) { 
    40           if (methods[method]) { 
    41                return methods[method].apply(this,Array.prototype.slice.call(arguments,1)); 
    42           } else if (typeof method === 'object' || !method) { 
    43                return methods.init.apply(this,arguments); 
    44           } else { 
    45                $.error('Method ' + method + ' does not exist on jQuery.dctoolbar'); 
    46           } 
     41          //try { 
     42               if (methods[method]) { 
     43                    return methods[method].apply(this,Array.prototype.slice.call(arguments,1)); 
     44               } else if (typeof method === 'object' || !method) { 
     45                    return methods.init.apply(this,arguments); 
     46               } else { 
     47                    throw 'Method ' + method + ' does not exist on jQuery.dctoolbarmanager'; 
     48               } 
     49          /*} catch (e) { 
     50               $.error('Error happend on jQuery.dctoolbarmanager: ' + e); 
     51          }*/ 
    4752     }; 
    4853})(jQuery); 
     
    5055function dcToolBarManager() { 
    5156     this.setToolBar = function(options) { 
    52           var settings = { 
    53                id: 'generic', 
    54                js_urls: [], 
    55                css_urls: [], 
    56                preinit: function() {}, 
    57                init: function() {}, 
    58                load: function() {}, 
    59                draw: function() {}, 
    60                destroy: function() {} 
    61           }; 
    62            
    63           $.extend(settings,options); 
    64            
    65           this.toolbars[settings.id] = { 
    66                id: settings.id, 
    67                js_urls: settings.js_urls, 
    68                css_urls: settings.css_urls, 
    69                preinit: settings.preinit, 
    70                init: settings.init, 
    71                load: settings.load, 
    72                draw: settings.draw, 
    73                destroy: settings.destroy, 
    74                loaded: false 
    75           }; 
    76            
    77           this.fn[settings.id] = new Array(); 
    78            
    79           this._init(settings.id); 
     57          try { 
     58               var toolbar = { 
     59                    id: null, 
     60                    mode: null, 
     61                    js: [], 
     62                    css: [], 
     63                    onPreInit: function() {}, 
     64                    onInit: function() {}, 
     65                    onDraw: function() {}, 
     66                    onDestroy: function() {} 
     67               }; 
     68                
     69               $.extend(toolbar,options); 
     70                
     71               if (toolbar.id == null || toolbar.id == '') { 
     72                    throw 'Invalid toolbar id'; 
     73               } 
     74                
     75               if (toolbar.mode == null || toolbar.mode == '') { 
     76                    throw 'Invalid toolbar mode'; 
     77               } 
     78                
     79               //var js = $.makeArray(toolbar.js); 
     80               if (!$.isArray(toolbar.js)) { 
     81                    throw 'Invalid format for JS scripts'; 
     82               } 
     83               //var css = $.makeArray(toolbar.css); 
     84               if (!$.isArray(toolbar.css)) { 
     85                    throw 'Invalid format for CSS scripts'; 
     86               } 
     87                
     88               // Add toolbar 
     89               this.toolbars[toolbar.mode] = { 
     90                    id: toolbar.id, 
     91                    js: toolbar.js, 
     92                    css: toolbar.css, 
     93                    loaded: false, 
     94                    init: false 
     95               }; 
     96                
     97               // Add events 
     98               this.events[toolbar.mode] = { 
     99                    onPreInit: [], 
     100                    onInit: [], 
     101                    onDraw: [], 
     102                    onDestroy: [] 
     103               }; 
     104               this.bind('onPreInit',toolbar.mode,toolbar.onPreInit); 
     105               this.bind('onInit',toolbar.mode,toolbar.onInit); 
     106               this.bind('onDraw',toolbar.mode,toolbar.onDraw); 
     107               this.bind('onDestroy',toolbar.mode,toolbar.onDestroy); 
     108                
     109               // Pre-init toolbar 
     110               this._preInit(toolbar.mode); 
     111          } catch (e) { 
     112               $.error('Toolbar signature error: ' + e.message); 
     113          } 
    80114     }; 
    81 }; 
     115      
     116     this.bind = function(event,mode,callback) { 
     117          if (!this.events.hasOwnProperty(mode)) { 
     118               throw 'No event available for toolbar [%s]'.replace('%s',id); 
     119          } 
     120           
     121          var events = this.events[mode]; 
     122           
     123          if (!events.hasOwnProperty(event)) { 
     124               throw 'Event [%1$s] does not exist for toolbar [%2$s]'.replace('%1$s',event).replace('%2$s',mode); 
     125          } 
     126           
     127          if (typeof(callback) === 'function') { 
     128               events[event].push(callback); 
     129          } 
     130     }, 
     131      
     132     this.call = function(event,mode) { 
     133          if (!this.events.hasOwnProperty(mode)) { 
     134               return; 
     135          } 
     136           
     137          var events = this.events[mode]; 
     138           
     139          if (!events.hasOwnProperty(event)) { 
     140               return; 
     141          } 
     142           
     143          var _arguments = arguments; 
     144           
     145          $.each(events[event],function(i,fn) { 
     146               fn.apply(this,Array.prototype.slice.call(_arguments,2)); 
     147          }); 
     148     } 
     149} 
    82150 
    83151dcToolBarManager.prototype = { 
    84152     toolbars: {}, 
    85      fn: {}, 
    86      msg: { 
    87           toolbar_does_not_exists: 'Toolbar [%s] does not exists' 
    88      }, 
    89       
    90      _init: function(mode) { 
    91           try { 
    92                var _this = this; 
    93                var results = []; 
    94                 
    95                var t = this.toolbars[mode]; 
    96                 
    97                if (t.loaded) { 
    98                     return; 
    99                } 
    100                 
    101                var n = t.js_urls.length; 
    102                 
    103                // Pre-initialization 
    104                t.preinit(); 
    105                 
    106                // Loading JS scripts 
    107                $.each(t.js_urls, function(i,url) { 
    108                     $('head').append($('<script>').attr({ 
    109                          type: 'text/javascript', 
    110                          src: url 
    111                     })); 
    112                     if(! --n) { 
    113                          t.loaded = true; 
    114                          t.init(); 
    115                     } 
    116                }); 
    117                 
    118                // Loading CSS scripts 
    119                $.each(t.css_urls, function(j,css) { 
    120                     $('head').append($('<link/>').attr({ 
    121                          rel: 'stylesheet', 
    122                          type: 'text/css', 
    123                          href: css 
    124                     })); 
    125                }); 
    126           } catch (e) { 
    127                $.error('Error during toolbar [' + id + '] initialization: ' + e.message); 
    128           } 
    129      }, 
    130       
    131      _load: function(mode,elm) { 
    132           if (!this.toolbars[mode]) { 
    133                throw this.msg.toolbar_does_not_exists.replace('%s',mode); 
    134           } 
    135           var t = this.toolbars[mode]; 
    136            
    137           $.each(this.fn[mode],function(i,callback) { 
    138                callback(); 
     153     events: {}, 
     154     msg: {}, 
     155      
     156     _preInit: function(mode) { 
     157          if (!this.toolbars.hasOwnProperty(mode)) { 
     158               throw 'Toolbar [%s] does not exist'.replace('%s',mode); 
     159          } 
     160           
     161          this.call('onPreInit',mode); 
     162           
     163          var t = this.toolbars[mode]; 
     164          var n = t.js.length; 
     165           
     166          // Loading JS scripts 
     167          $.each(t.js, function(i,url) { 
     168               $.ajax({ 
     169                    async: false, 
     170                    url: url, 
     171                    dataType: 'script' 
     172               }); 
    139173          }); 
    140           t.load(elm); 
     174           
     175          // Loading CSS scripts 
     176          $.each(t.css, function(j,css) { 
     177               $('head').append($('<link/>').attr({ 
     178                    rel: 'stylesheet', 
     179                    type: 'text/css', 
     180                    href: css 
     181               })); 
     182          }); 
     183           
     184          t.loaded = true; 
     185     }, 
     186      
     187     _init: function(mode,elm) { 
     188          if (!this.toolbars.hasOwnProperty(mode)) { 
     189               throw 'Toolbar [%s] does not exist'.replace('%s',mode); 
     190          } 
     191           
     192          var _this = this; 
     193          var t = this.toolbars[mode]; 
     194           
     195          var _this = this; 
     196          var t = this.toolbars[mode]; 
     197           
     198          if (t.loaded) { 
     199               if (!t.init) { 
     200                    // Init toolbar 
     201                    this.call('onInit',mode,elm); 
     202                    t.init = true; 
     203               } 
     204               // Draw toolbar 
     205               this._draw(mode,elm); 
     206               return; 
     207          } 
     208           
     209          setTimeout(function() { _this._init.apply(_this,[mode,elm]); },1); 
    141210     }, 
    142211      
    143212     _draw: function(mode,elm) { 
    144           var t = this.toolbars[mode]; 
    145           t.draw(elm); 
     213          if (!this.toolbars.hasOwnProperty(mode)) { 
     214               throw 'Toolbar [%s] does not exist'.replace('%s',mode); 
     215          } 
     216           
     217          var t = this.toolbars[mode]; 
     218           
     219          // Draw toolbar 
     220          this.call('onDraw',mode,elm); 
    146221     }, 
    147222      
    148223     _destroy: function(mode,elm) { 
    149           var t = this.toolbars[mode]; 
    150           t.destroy(elm); 
     224          if (!this.toolbars.hasOwnProperty(mode)) { 
     225               throw 'Toolbar [%s] does not exist'.replace('%s',mode); 
     226          } 
     227           
     228          var t = this.toolbars[mode]; 
     229           
     230          // Destroy toolbar 
     231          this.call('onDestroy',mode,elm); 
    151232     } 
    152233} 
Note: See TracChangeset for help on using the changeset viewer.

Sites map