source: pro-violet-viettel/docs/template/assets/js/uncompressed/ace-extra.js @ 400

Last change on this file since 400 was 400, checked in by dungnv, 11 years ago
  • Property svn:mime-type set to text/plain
File size: 11.4 KB
Line 
1if(! ('ace' in window) ) window['ace'] = {}
2
3ace.config = {
4 cookie_expiry : 604800, //1 week duration for saved settings
5 storage_method: 2 //2 means use cookies, 1 means localStorage, 0 means localStorage if available otherwise cookies
6}
7
8ace.settings = {
9        is : function(item, status) {
10                //such as ace.settings.is('navbar', 'fixed')
11                return (ace.data.get('settings', item+'-'+status) == 1)
12        },
13        exists : function(item, status) {
14                return (ace.data.get('settings', item+'-'+status) !== null)
15        },
16        set : function(item, status) {
17                ace.data.set('settings', item+'-'+status, 1)
18        },
19        unset : function(item, status) {
20                ace.data.set('settings', item+'-'+status, -1)
21        },
22        remove : function(item, status) {
23                ace.data.remove('settings', item+'-'+status)
24        },
25
26        navbar_fixed : function(fix) {
27                fix = fix || false;
28                if(!fix && ace.settings.is('sidebar', 'fixed')) {
29                        ace.settings.sidebar_fixed(false);
30                }
31               
32                var navbar = document.getElementById('navbar');
33                if(fix) {
34                        if(!ace.hasClass(navbar , 'navbar-fixed-top'))  ace.addClass(navbar , 'navbar-fixed-top');
35                        if(!ace.hasClass(document.body , 'navbar-fixed'))  ace.addClass(document.body , 'navbar-fixed');
36                       
37                        ace.settings.set('navbar', 'fixed');
38                } else {
39                        ace.removeClass(navbar , 'navbar-fixed-top');
40                        ace.removeClass(document.body , 'navbar-fixed');
41                       
42                        ace.settings.unset('navbar', 'fixed');
43                }
44               
45                document.getElementById('ace-settings-navbar').checked = fix;
46        },
47
48
49        breadcrumbs_fixed : function(fix) {
50                fix = fix || false;
51                if(fix && !ace.settings.is('sidebar', 'fixed')) {
52                        ace.settings.sidebar_fixed(true);
53                }
54
55                var breadcrumbs = document.getElementById('breadcrumbs');
56                if(fix) {
57                        if(!ace.hasClass(breadcrumbs , 'breadcrumbs-fixed'))  ace.addClass(breadcrumbs , 'breadcrumbs-fixed');
58                        if(!ace.hasClass(document.body , 'breadcrumbs-fixed'))  ace.addClass(document.body , 'breadcrumbs-fixed');
59                       
60                        ace.settings.set('breadcrumbs', 'fixed');
61                } else {
62                        ace.removeClass(breadcrumbs , 'breadcrumbs-fixed');
63                        ace.removeClass(document.body , 'breadcrumbs-fixed');
64                       
65                        ace.settings.unset('breadcrumbs', 'fixed');
66                }
67                document.getElementById('ace-settings-breadcrumbs').checked = fix;
68        },
69
70
71        sidebar_fixed : function(fix) {
72                fix = fix || false;
73                if(!fix && ace.settings.is('breadcrumbs', 'fixed')) {
74                        ace.settings.breadcrumbs_fixed(false);
75                }
76
77                if( fix && !ace.settings.is('navbar', 'fixed') ) {
78                        ace.settings.navbar_fixed(true);
79                }
80
81                var sidebar = document.getElementById('sidebar');
82                if(fix) {
83                        if( !ace.hasClass(sidebar , 'sidebar-fixed') )  ace.addClass(sidebar , 'sidebar-fixed');
84                        ace.settings.set('sidebar', 'fixed');
85                } else {
86                        ace.removeClass(sidebar , 'sidebar-fixed');
87                        ace.settings.unset('sidebar', 'fixed');
88                }
89                document.getElementById('ace-settings-sidebar').checked = fix;
90        },
91
92        main_container_fixed : function(inside) {
93                inside = inside || false;
94
95                var main_container = document.getElementById('main-container');
96                var navbar_container = document.getElementById('navbar-container');
97                if(inside) {
98                        if( !ace.hasClass(main_container , 'container') )  ace.addClass(main_container , 'container');
99                        if( !ace.hasClass(navbar_container , 'container') )  ace.addClass(navbar_container , 'container');
100                        ace.settings.set('main-container', 'fixed');
101                } else {
102                        ace.removeClass(main_container , 'container');
103                        ace.removeClass(navbar_container , 'container');
104                        ace.settings.unset('main-container', 'fixed');
105                }
106                document.getElementById('ace-settings-add-container').checked = inside;
107               
108               
109                if(navigator.userAgent.match(/webkit/i)) {
110                        //webkit has a problem redrawing and moving around the sidebar background in realtime
111                        //so we do this, to force redraw
112                        //there will be no problems with webkit if the ".container" class is statically put inside HTML code.
113                        var sidebar = document.getElementById('sidebar')
114                        ace.toggleClass(sidebar , 'menu-min')
115                        setTimeout(function() { ace.toggleClass(sidebar , 'menu-min') } , 0)
116                }
117        },
118
119        sidebar_collapsed : function(collpase) {
120                collpase = collpase || false;
121
122                var sidebar = document.getElementById('sidebar');
123                var icon = document.getElementById('sidebar-collapse').querySelector('[class*="icon-"]');
124                var $icon1 = icon.getAttribute('data-icon1');//the icon for expanded state
125                var $icon2 = icon.getAttribute('data-icon2');//the icon for collapsed state
126
127                if(collpase) {
128                        ace.addClass(sidebar , 'menu-min');
129                        ace.removeClass(icon , $icon1);
130                        ace.addClass(icon , $icon2);
131
132                        ace.settings.set('sidebar', 'collapsed');
133                } else {
134                        ace.removeClass(sidebar , 'menu-min');
135                        ace.removeClass(icon , $icon2);
136                        ace.addClass(icon , $icon1);
137
138                        ace.settings.unset('sidebar', 'collapsed');
139                }
140
141        },
142        /**
143        select_skin : function(skin) {
144        }
145        */
146}
147
148
149//check the status of something
150ace.settings.check = function(item, val) {
151        if(! ace.settings.exists(item, val) ) return;//no such setting specified
152        var status = ace.settings.is(item, val);//is breadcrumbs-fixed? or is sidebar-collapsed? etc
153       
154        var mustHaveClass = {
155                'navbar-fixed' : 'navbar-fixed-top',
156                'sidebar-fixed' : 'sidebar-fixed',
157                'breadcrumbs-fixed' : 'breadcrumbs-fixed',
158                'sidebar-collapsed' : 'menu-min',
159                'main-container-fixed' : 'container'
160        }
161
162
163        //if an element doesn't have a specified class, but saved settings say it should, then add it
164        //for example, sidebar isn't .fixed, but user fixed it on a previous page
165        //or if an element has a specified class, but saved settings say it shouldn't, then remove it
166        //for example, sidebar by default is minimized (.menu-min hard coded), but user expanded it and now shouldn't have 'menu-min' class
167       
168        var target = document.getElementById(item);//#navbar, #sidebar, #breadcrumbs
169        if(status != ace.hasClass(target , mustHaveClass[item+'-'+val])) {
170                ace.settings[item.replace('-','_')+'_'+val](status);//call the relevant function to mage the changes
171        }
172}
173
174
175
176
177
178
179//save/retrieve data using localStorage or cookie
180//method == 1, use localStorage
181//method == 2, use cookies
182//method not specified, use localStorage if available, otherwise cookies
183ace.data_storage = function(method, undefined) {
184        var prefix = 'ace.';
185
186        var storage = null;
187        var type = 0;
188       
189        if((method == 1 || method === undefined) && 'localStorage' in window && window['localStorage'] !== null) {
190                storage = ace.storage;
191                type = 1;
192        }
193        else if(storage == null && (method == 2 || method === undefined) && 'cookie' in document && document['cookie'] !== null) {
194                storage = ace.cookie;
195                type = 2;
196        }
197
198        //var data = {}
199        this.set = function(namespace, key, value, undefined) {
200                if(!storage) return;
201               
202                if(value === undefined) {//no namespace here?
203                        value = key;
204                        key = namespace;
205
206                        if(value == null) storage.remove(prefix+key)
207                        else {
208                                if(type == 1)
209                                        storage.set(prefix+key, value)
210                                else if(type == 2)
211                                        storage.set(prefix+key, value, ace.config.cookie_expiry)
212                        }
213                }
214                else {
215                        if(type == 1) {//localStorage
216                                if(value == null) storage.remove(prefix+namespace+'.'+key)
217                                else storage.set(prefix+namespace+'.'+key, value);
218                        }
219                        else if(type == 2) {//cookie
220                                var val = storage.get(prefix+namespace);
221                                var tmp = val ? JSON.parse(val) : {};
222
223                                if(value == null) {
224                                        delete tmp[key];//remove
225                                        if(ace.sizeof(tmp) == 0) {//no other elements in this cookie, so delete it
226                                                storage.remove(prefix+namespace);
227                                                return;
228                                        }
229                                }
230                               
231                                else {
232                                        tmp[key] = value;
233                                }
234
235                                storage.set(prefix+namespace , JSON.stringify(tmp), ace.config.cookie_expiry)
236                        }
237                }
238        }
239
240        this.get = function(namespace, key, undefined) {
241                if(!storage) return null;
242               
243                if(key === undefined) {//no namespace here?
244                        key = namespace;
245                        return storage.get(prefix+key);
246                }
247                else {
248                        if(type == 1) {//localStorage
249                                return storage.get(prefix+namespace+'.'+key);
250                        }
251                        else if(type == 2) {//cookie
252                                var val = storage.get(prefix+namespace);
253                                var tmp = val ? JSON.parse(val) : {};
254                                return key in tmp ? tmp[key] : null;
255                        }
256                }
257        }
258
259       
260        this.remove = function(namespace, key, undefined) {
261                if(!storage) return;
262               
263                if(key === undefined) {
264                        key = namespace
265                        this.set(key, null);
266                }
267                else {
268                        this.set(namespace, key, null);
269                }
270        }
271}
272
273
274
275
276
277//cookie storage
278ace.cookie = {
279        // The following functions are from Cookie.js class in TinyMCE, Moxiecode, used under LGPL.
280
281        /**
282         * Get a cookie.
283         */
284        get : function(name) {
285                var cookie = document.cookie, e, p = name + "=", b;
286
287                if ( !cookie )
288                        return;
289
290                b = cookie.indexOf("; " + p);
291
292                if ( b == -1 ) {
293                        b = cookie.indexOf(p);
294
295                        if ( b != 0 )
296                                return null;
297
298                } else {
299                        b += 2;
300                }
301
302                e = cookie.indexOf(";", b);
303
304                if ( e == -1 )
305                        e = cookie.length;
306
307                return decodeURIComponent( cookie.substring(b + p.length, e) );
308        },
309
310        /**
311         * Set a cookie.
312         *
313         * The 'expires' arg can be either a JS Date() object set to the expiration date (back-compat)
314         * or the number of seconds until expiration
315         */
316        set : function(name, value, expires, path, domain, secure) {
317                var d = new Date();
318
319                if ( typeof(expires) == 'object' && expires.toGMTString ) {
320                        expires = expires.toGMTString();
321                } else if ( parseInt(expires, 10) ) {
322                        d.setTime( d.getTime() + ( parseInt(expires, 10) * 1000 ) ); // time must be in miliseconds
323                        expires = d.toGMTString();
324                } else {
325                        expires = '';
326                }
327
328                document.cookie = name + "=" + encodeURIComponent(value) +
329                        ((expires) ? "; expires=" + expires : "") +
330                        ((path) ? "; path=" + path : "") +
331                        ((domain) ? "; domain=" + domain : "") +
332                        ((secure) ? "; secure" : "");
333        },
334
335        /**
336         * Remove a cookie.
337         *
338         * This is done by setting it to an empty value and setting the expiration time in the past.
339         */
340        remove : function(name, path) {
341                this.set(name, '', -1000, path);
342        }
343};
344
345
346//local storage
347ace.storage = {
348        get: function(key) {
349                return window['localStorage'].getItem(key);
350        },
351        set: function(key, value) {
352                window['localStorage'].setItem(key , value);
353        },
354        remove: function(key) {
355                window['localStorage'].removeItem(key);
356        }
357};
358
359
360
361
362
363
364//count the number of properties in an object
365//useful for getting the number of elements in an associative array
366ace.sizeof = function(obj) {
367        var size = 0;
368        for(var key in obj) if(obj.hasOwnProperty(key)) size++;
369        return size;
370}
371
372//because jQuery may not be loaded at this stage, we use our own toggleClass
373ace.hasClass = function(elem, className) {
374        return (" " + elem.className + " ").indexOf(" " + className + " ") > -1;
375}
376ace.addClass = function(elem, className) {
377 if (!ace.hasClass(elem, className)) {
378        var currentClass = elem.className;
379        elem.className = currentClass + (currentClass.length? " " : "") + className;
380 }
381}
382ace.removeClass = function(elem, className) {ace.replaceClass(elem, className);}
383
384ace.replaceClass = function(elem, className, newClass) {
385        var classToRemove = new RegExp(("(^|\\s)" + className + "(\\s|$)"), "i");
386        elem.className = elem.className.replace(classToRemove, function (match, p1, p2) {
387                return newClass? (p1 + newClass + p2) : " ";
388        }).replace(/^\s+|\s+$/g, "");
389}
390
391ace.toggleClass = function(elem, className) {
392        if(ace.hasClass(elem, className))
393                ace.removeClass(elem, className);
394        else ace.addClass(elem, className);
395}
396
397
398
399
400//data_storage instance used inside ace.settings etc
401ace.data = new ace.data_storage(ace.config.storage_method);
Note: See TracBrowser for help on using the repository browser.