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