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
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
/**
Core script to handle the entire theme and core functions
**/
var Layout = function() {
var layoutImgPath = 'layouts/layout4/img/';
var layoutCssPath = 'layouts/layout4/css/';
var resBreakpointMd = App.getResponsiveBreakpoint('md');
//* BEGIN:CORE HANDLERS *//
// this function handles responsive layout on screen size resize or mobile device rotate.
// Set proper height for sidebar and content. The content and sidebar height must be synced always.
var handleSidebarAndContentHeight = function () {
var content = $('.page-content');
var sidebar = $('.page-sidebar');
var body = $('body');
var height;
if (body.hasClass("page-footer-fixed") === true && body.hasClass("page-sidebar-fixed") === false) {
var available_height = App.getViewPort().height - $('.page-footer').outerHeight(true) - $('.page-header').outerHeight(true);
if (content.height() < available_height) {
content.attr('style', 'min-height:' + available_height + 'px');
}
} else {
if (body.hasClass('page-sidebar-fixed')) {
height = _calculateFixedSidebarViewportHeight() - 10;
if (body.hasClass('page-footer-fixed') === false) {
height = height - $('.page-footer').outerHeight(true);
}
} else {
var headerHeight = $('.page-header').outerHeight(true);
var footerHeight = $('.page-footer').outerHeight(true);
if (App.getViewPort().width < resBreakpointMd) {
height = App.getViewPort().height - headerHeight - footerHeight;
} else {
height = sidebar.height() - 10;
}
if ((height + headerHeight + footerHeight) <= App.getViewPort().height) {
height = App.getViewPort().height - headerHeight - footerHeight - 45;
}
}
content.attr('style', 'min-height:' + height + 'px');
}
};
// Handle sidebar menu links
var handleSidebarMenuActiveLink = function(mode, el) {
var url = location.hash.toLowerCase();
var menu = $('.page-sidebar-menu');
if (mode === 'click' || mode === 'set') {
el = $(el);
} else if (mode === 'match') {
menu.find("li > a").each(function() {
var path = $(this).attr("href").toLowerCase();
// url match condition
if (path.length > 1 && url.substr(1, path.length - 1) == path.substr(1)) {
el = $(this);
return;
}
});
}
if (!el || el.size() == 0) {
return;
}
if (el.attr('href').toLowerCase() === 'javascript:;' || el.attr('href').toLowerCase() === '#') {
return;
}
var slideSpeed = parseInt(menu.data("slide-speed"));
var keepExpand = menu.data("keep-expanded");
// disable active states
menu.find('li.active').removeClass('active');
menu.find('li > a > .selected').remove();
if (menu.hasClass('page-sidebar-menu-hover-submenu') === false) {
menu.find('li.open').each(function(){
if ($(this).children('.sub-menu').size() === 0) {
$(this).removeClass('open');
$(this).find('> a > .arrow.open').removeClass('open');
}
});
} else {
menu.find('li.open').removeClass('open');
}
el.parents('li').each(function () {
$(this).addClass('active');
$(this).find('> a > span.arrow').addClass('open');
if ($(this).parent('ul.page-sidebar-menu').size() === 1) {
$(this).find('> a').append('<span class="selected"></span>');
}
if ($(this).children('ul.sub-menu').size() === 1) {
$(this).addClass('open');
}
});
if (mode === 'click') {
if (App.getViewPort().width < resBreakpointMd && $('.page-sidebar').hasClass("in")) { // close the menu on mobile view while laoding a page
$('.page-header .responsive-toggler').click();
}
}
};
// Handle sidebar menu
var handleSidebarMenu = function() {
$('.page-sidebar').on('click', 'li > a', function(e) {
if (App.getViewPort().width >= resBreakpointMd && $(this).parents('.page-sidebar-menu-hover-submenu').size() === 1) { // exit of hover sidebar menu
return;
}
if ($(this).next().hasClass('sub-menu') === false) {
if (App.getViewPort().width < resBreakpointMd && $('.page-sidebar').hasClass("in")) { // close the menu on mobile view while laoding a page
$('.page-header .responsive-toggler').click();
}
return;
}
if ($(this).next().hasClass('sub-menu always-open')) {
return;
}
var parent = $(this).parent().parent();
var the = $(this);
var menu = $('.page-sidebar-menu');
var sub = $(this).next();
var autoScroll = menu.data("auto-scroll");
var slideSpeed = parseInt(menu.data("slide-speed"));
var keepExpand = menu.data("keep-expanded");
if (keepExpand !== true) {
parent.children('li.open').children('a').children('.arrow').removeClass('open');
parent.children('li.open').children('.sub-menu:not(.always-open)').slideUp(slideSpeed);
parent.children('li.open').removeClass('open');
}
var slideOffeset = -200;
if (sub.is(":visible")) {
$('.arrow', $(this)).removeClass("open");
$(this).parent().removeClass("open");
sub.slideUp(slideSpeed, function() {
if (autoScroll === true && $('body').hasClass('page-sidebar-closed') === false) {
if ($('body').hasClass('page-sidebar-fixed')) {
menu.slimScroll({
'scrollTo': (the.position()).top
});
} else {
App.scrollTo(the, slideOffeset);
}
}
handleSidebarAndContentHeight();
});
} else {
$('.arrow', $(this)).addClass("open");
$(this).parent().addClass("open");
sub.slideDown(slideSpeed, function() {
if (autoScroll === true && $('body').hasClass('page-sidebar-closed') === false) {
if ($('body').hasClass('page-sidebar-fixed')) {
menu.slimScroll({
'scrollTo': (the.position()).top
});
} else {
App.scrollTo(the, slideOffeset);
}
}
handleSidebarAndContentHeight();
});
}
e.preventDefault();
});
// handle menu close for angularjs version
if (App.isAngularJsApp()) {
$(".page-sidebar-menu li > a").on("click", function(e) {
if (App.getViewPort().width < resBreakpointMd && $(this).next().hasClass('sub-menu') === false) {
$('.page-header .responsive-toggler').click();
}
});
}
// handle ajax links within sidebar menu
$('.page-sidebar').on('click', ' li > a.ajaxify', function(e) {
e.preventDefault();
App.scrollTop();
var url = $(this).attr("href");
var menuContainer = $('.page-sidebar ul');
var pageContent = $('.page-content');
var pageContentBody = $('.page-content .page-content-body');
menuContainer.children('li.active').removeClass('active');
menuContainer.children('arrow.open').removeClass('open');
$(this).parents('li').each(function() {
$(this).addClass('active');
$(this).children('a > span.arrow').addClass('open');
});
$(this).parents('li').addClass('active');
if (App.getViewPort().width < resBreakpointMd && $('.page-sidebar').hasClass("in")) { // close the menu on mobile view while laoding a page
$('.page-header .responsive-toggler').click();
}
App.startPageLoading();
var the = $(this);
$.ajax({
type: "GET",
cache: false,
url: url,
dataType: "html",
success: function(res) {
if (the.parents('li.open').size() === 0) {
$('.page-sidebar-menu > li.open > a').click();
}
App.stopPageLoading();
pageContentBody.html(res);
Layout.fixContentHeight(); // fix content height
App.initAjax(); // initialize core stuff
},
error: function(xhr, ajaxOptions, thrownError) {
App.stopPageLoading();
pageContentBody.html('<h4>Could not load the requested content.</h4>');
}
});
});
// handle ajax link within main content
$('.page-content').on('click', '.ajaxify', function(e) {
e.preventDefault();
App.scrollTop();
var url = $(this).attr("href");
var pageContent = $('.page-content');
var pageContentBody = $('.page-content .page-content-body');
App.startPageLoading();
if (App.getViewPort().width < resBreakpointMd && $('.page-sidebar').hasClass("in")) { // close the menu on mobile view while laoding a page
$('.page-header .responsive-toggler').click();
}
$.ajax({
type: "GET",
cache: false,
url: url,
dataType: "html",
success: function(res) {
App.stopPageLoading();
pageContentBody.html(res);
Layout.fixContentHeight(); // fix content height
App.initAjax(); // initialize core stuff
},
error: function(xhr, ajaxOptions, thrownError) {
pageContentBody.html('<h4>Could not load the requested content.</h4>');
App.stopPageLoading();
}
});
});
// handle scrolling to top on responsive menu toggler click when header is fixed for mobile view
$(document).on('click', '.page-header-fixed-mobile .responsive-toggler', function(){
App.scrollTop();
});
};
// Helper function to calculate sidebar height for fixed sidebar layout.
var _calculateFixedSidebarViewportHeight = function() {
var sidebarHeight = App.getViewPort().height - $('.page-header').outerHeight(true) - 40;
if ($('body').hasClass("page-footer-fixed")) {
sidebarHeight = sidebarHeight - $('.page-footer').outerHeight();
}
return sidebarHeight;
};
// Handles fixed sidebar
var handleFixedSidebar = function() {
var menu = $('.page-sidebar-menu');
App.destroySlimScroll(menu);
if ($('.page-sidebar-fixed').size() === 0) {
handleSidebarAndContentHeight();
return;
}
if (App.getViewPort().width >= resBreakpointMd) {
menu.attr("data-height", _calculateFixedSidebarViewportHeight());
App.initSlimScroll(menu);
handleSidebarAndContentHeight();
}
};
// Handles sidebar toggler to close/hide the sidebar.
var handleFixedSidebarHoverEffect = function () {
var body = $('body');
if (body.hasClass('page-sidebar-fixed')) {
$('.page-sidebar').on('mouseenter', function () {
if (body.hasClass('page-sidebar-closed')) {
$(this).find('.page-sidebar-menu').removeClass('page-sidebar-menu-closed');
}
}).on('mouseleave', function () {
if (body.hasClass('page-sidebar-closed')) {
$(this).find('.page-sidebar-menu').addClass('page-sidebar-menu-closed');
}
});
}
};
// Hanles sidebar toggler
var handleSidebarToggler = function() {
var body = $('body');
if ($.cookie && $.cookie('sidebar_closed') === '1' && App.getViewPort().width >= resBreakpointMd) {
$('body').addClass('page-sidebar-closed');
$('.page-sidebar-menu').addClass('page-sidebar-menu-closed');
}
// handle sidebar show/hide
$('body').on('click', '.sidebar-toggler', function(e) {
var sidebar = $('.page-sidebar');
var sidebarMenu = $('.page-sidebar-menu');
$(".sidebar-search", sidebar).removeClass("open");
if (body.hasClass("page-sidebar-closed")) {
body.removeClass("page-sidebar-closed");
sidebarMenu.removeClass("page-sidebar-menu-closed");
if ($.cookie) {
$.cookie('sidebar_closed', '0');
}
} else {
body.addClass("page-sidebar-closed");
sidebarMenu.addClass("page-sidebar-menu-closed");
if (body.hasClass("page-sidebar-fixed")) {
sidebarMenu.trigger("mouseleave");
}
if ($.cookie) {
$.cookie('sidebar_closed', '1');
}
}
$(window).trigger('resize');
});
handleFixedSidebarHoverEffect();
// handle the search bar close
$('.page-sidebar').on('click', '.sidebar-search .remove', function(e) {
e.preventDefault();
$('.sidebar-search').removeClass("open");
});
// handle the search query submit on enter press
$('.page-sidebar .sidebar-search').on('keypress', 'input.form-control', function(e) {
if (e.which == 13) {
$('.sidebar-search').submit();
return false; //<---- Add this line
}
});
// handle the search submit(for sidebar search and responsive mode of the header search)
$('.sidebar-search .submit').on('click', function(e) {
e.preventDefault();
if ($('body').hasClass("page-sidebar-closed")) {
if ($('.sidebar-search').hasClass('open') === false) {
if ($('.page-sidebar-fixed').size() === 1) {
$('.page-sidebar .sidebar-toggler').click(); //trigger sidebar toggle button
}
$('.sidebar-search').addClass("open");
} else {
$('.sidebar-search').submit();
}
} else {
$('.sidebar-search').submit();
}
});
// handle close on body click
if ($('.sidebar-search').size() !== 0) {
$('.sidebar-search .input-group').on('click', function(e) {
e.stopPropagation();
});
$('body').on('click', function() {
if ($('.sidebar-search').hasClass('open')) {
$('.sidebar-search').removeClass("open");
}
});
}
};
// Handles the horizontal menu
var handleHeader = function() {
// handle search box expand/collapse
$('.page-header').on('click', '.search-form', function(e) {
$(this).addClass("open");
$(this).find('.form-control').focus();
$('.page-header .search-form .form-control').on('blur', function(e) {
$(this).closest('.search-form').removeClass("open");
$(this).unbind("blur");
});
});
// handle hor menu search form on enter press
$('.page-header').on('keypress', '.hor-menu .search-form .form-control', function(e) {
if (e.which == 13) {
$(this).closest('.search-form').submit();
return false;
}
});
// handle header search button click
$('.page-header').on('mousedown', '.search-form.open .submit', function(e) {
e.preventDefault();
e.stopPropagation();
$(this).closest('.search-form').submit();
});
};
// Handles the go to top button at the footer
var handleGoTop = function() {
var offset = 300;
var duration = 500;
if (navigator.userAgent.match(/iPhone|iPad|iPod/i)) { // ios supported
$(window).bind("touchend touchcancel touchleave", function(e) {
if ($(this).scrollTop() > offset) {
$('.scroll-to-top').fadeIn(duration);
} else {
$('.scroll-to-top').fadeOut(duration);
}
});
} else { // general
$(window).scroll(function() {
if ($(this).scrollTop() > offset) {
$('.scroll-to-top').fadeIn(duration);
} else {
$('.scroll-to-top').fadeOut(duration);
}
});
}
$('.scroll-to-top').click(function(e) {
e.preventDefault();
$('html, body').animate({
scrollTop: 0
}, duration);
return false;
});
};
//* END:CORE HANDLERS *//
return {
// Main init methods to initialize the layout
// IMPORTANT!!!: Do not modify the core handlers call order.
initHeader: function() {
handleHeader(); // handles horizontal menu
},
setSidebarMenuActiveLink: function(mode, el) {
handleSidebarMenuActiveLink(mode, el);
},
initSidebar: function() {
//layout handlers
handleFixedSidebar(); // handles fixed sidebar menu
handleSidebarMenu(); // handles main menu
handleSidebarToggler(); // handles sidebar hide/show
if (App.isAngularJsApp()) {
handleSidebarMenuActiveLink('match'); // init sidebar active links
}
App.addResizeHandler(handleFixedSidebar); // reinitialize fixed sidebar on window resize
},
initContent: function() {
return;
},
initFooter: function() {
handleGoTop(); //handles scroll to top functionality in the footer
},
init: function () {
this.initHeader();
this.initSidebar();
this.initContent();
this.initFooter();
},
//public function to fix the sidebar and content height accordingly
fixContentHeight: function() {
return;
},
initFixedSidebarHoverEffect: function() {
handleFixedSidebarHoverEffect();
},
initFixedSidebar: function() {
handleFixedSidebar();
},
getLayoutImgPath: function() {
return App.getAssetsPath() + layoutImgPath;
},
getLayoutCssPath: function() {
return App.getAssetsPath() + layoutCssPath;
}
};
}();
if (App.isAngularJsApp() === false) {
jQuery(document).ready(function() {
Layout.init(); // init metronic core componets
});
}