Dotclear

source: admin/js/dcToolBarManager.js @ 310:0346fd2599eb

Revision 310:0346fd2599eb, 3.2 KB checked in by Tomtom33 <tbouron@…>, 14 years ago (diff)

Updated tag plugin for the new toolbar integration

Line 
1(function($){
2     var methods = {
3          init: function(options) {
4               var settings = {
5                    mode: 'xhtml',
6                    context: null
7               };
8               return this.each(function(){
9                    var _this = this;
10                    if (options) {
11                         $.extend(settings,options);
12                    }
13                    $.each(settings, function(k,v) {
14                         $.data(_this,k,v);
15                    });
16                    dcToolBarManager._load(settings.mode,this);
17               });
18         
19          },
20          draw: function() {
21               return this.each(function(){
22                    dcToolBarManager._draw($(this).data('mode'),this);
23               });
24          },
25          switch: function(options) {
26               mode = options ? options : 'xhtml';
27               
28               return this.each(function(){
29                    if ($(this).data('mode') != mode) {
30                         $.data(this,'mode',mode);
31                         dcToolBarManager._destroy($(this).data('mode'),this);
32                         dcToolBarManager._load(mode,this);
33                         dcToolBarManager._draw(mode,this);
34                    }
35               });
36          }
37     };
38     
39     $.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          }
47     };
48})(jQuery);
49
50function dcToolBarManager() {
51     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);
80     };
81};
82
83dcToolBarManager.prototype = {
84     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();
139          });
140          t.load(elm);
141     },
142     
143     _draw: function(mode,elm) {
144          var t = this.toolbars[mode];
145          t.draw(elm);
146     },
147     
148     _destroy: function(mode,elm) {
149          var t = this.toolbars[mode];
150          t.destroy(elm);
151     }
152}
Note: See TracBrowser for help on using the repository browser.

Sites map