-
Notifications
You must be signed in to change notification settings - Fork 34
/
admin-lte.js
182 lines (155 loc) · 4.62 KB
/
admin-lte.js
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
var screenSizes = {
xs: 480,
sm: 768,
md: 992,
lg: 1200
};
Template.AdminLTE.onCreated(function () {
var self = this;
var skin = 'blue';
var fixed = false;
var sidebarMini = false;
if (this.data) {
skin = this.data.skin || skin;
fixed = this.data.fixed || fixed;
sidebarMini = this.data.sidebarMini || sidebarMini;
}
self.isReady = new ReactiveVar(false);
self.style = waitOnCSS(cssUrl());
self.skin = waitOnCSS(skinUrl(skin));
fixed && $('body').addClass('fixed');
sidebarMini && $('body').addClass('sidebar-mini');
self.removeClasses = function () {
fixed && $('body').removeClass('fixed');
sidebarMini && $('body').removeClass('sidebar-mini');
}
this.autorun(function () {
if (self.style.ready() && self.skin.ready()) {
self.isReady.set(true);
}
});
});
Template.AdminLTE.onDestroyed(function () {
this.removeClasses();
this.style.remove();
this.skin.remove();
});
Template.AdminLTE.helpers({
isReady: function () {
return Template.instance().isReady.get();
},
loadingTemplate: function () {
return this.loadingTemplate || 'AdminLTE_loading';
},
skin: function () {
return this.skin || 'blue';
}
});
Template.AdminLTE.events({
'click [data-toggle=offcanvas]': function (e, t) {
e.preventDefault();
//Enable sidebar push menu
if ($(window).width() > (screenSizes.sm - 1)) {
$("body").toggleClass('sidebar-collapse');
}
//Handle sidebar push menu for small screens
else {
if ($("body").hasClass('sidebar-open')) {
$("body").removeClass('sidebar-open');
$("body").removeClass('sidebar-collapse')
} else {
$("body").addClass('sidebar-open');
}
}
},
'click .content-wrapper': function (e, t) {
//Enable hide menu when clicking on the content-wrapper on small screens
if ($(window).width() <= (screenSizes.sm - 1) && $("body").hasClass("sidebar-open")) {
$("body").removeClass('sidebar-open');
}
},
'click .sidebar li a': function (e, t) {
//Get the clicked link and the next element
var $this = $(e.currentTarget);
var checkElement = $this.next();
//Check if the next element is a menu and is visible
if ((checkElement.is('.treeview-menu')) && (checkElement.is(':visible'))) {
//Close the menu
checkElement.slideUp('normal', function () {
checkElement.removeClass('menu-open');
});
checkElement.parent("li").removeClass("active");
}
//If the menu is not visible
else if ((checkElement.is('.treeview-menu')) && (!checkElement.is(':visible'))) {
//Get the parent menu
var parent = $this.parents('ul').first();
//Close all open menus within the parent
var ul = parent.find('ul:visible').slideUp('normal');
//Remove the menu-open class from the parent
ul.removeClass('menu-open');
//Get the parent li
var parent_li = $this.parent("li");
//Open the target menu and add the menu-open class
checkElement.slideDown('normal', function () {
//Add the class active to the parent li
checkElement.addClass('menu-open');
parent.find('li.active').removeClass('active');
parent_li.addClass('active');
});
}
//if this isn't a link, prevent the page from being redirected
if (checkElement.is('.treeview-menu')) {
e.preventDefault();
}
}
});
function cssUrl () {
return Meteor.absoluteUrl('packages/mfactory_admin-lte/css/AdminLTE.min.css');
}
function skinUrl (name) {
return Meteor.absoluteUrl(
'packages/mfactory_admin-lte/css/skins/skin-' + name + '.min.css');
}
function waitOnCSS (url, timeout) {
var isLoaded = new ReactiveVar(false);
timeout = timeout || 5000;
var link = document.createElement('link');
link.type = 'text/css';
link.rel = 'stylesheet';
link.href = url;
link.onload = function () {
isLoaded.set(true);
};
if (link.addEventListener) {
link.addEventListener('load', function () {
isLoaded.set(true);
}, false);
}
link.onreadystatechange = function () {
var state = link.readyState;
if (state === 'loaded' || state === 'complete') {
link.onreadystatechange = null;
isLoaded.set(true);
}
};
var cssnum = document.styleSheets.length;
var ti = setInterval(function () {
if (document.styleSheets.length > cssnum) {
isLoaded.set(true);
clearInterval(ti);
}
}, 10);
setTimeout(function () {
isLoaded.set(true);
}, timeout);
$(document.head).append(link);
return {
ready: function () {
return isLoaded.get();
},
remove: function () {
$('link[href="' + url + '"]').remove();
}
};
}