Subversion Repositories Integrator Subversion

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
1 espaco 1
/**
2
Core script to handle the entire theme and core functions
3
**/
4
var Layout = function() {
5
 
6
    var layoutImgPath = 'admin/layout4/img/';
7
 
8
    var layoutCssPath = 'admin/layout4/css/';
9
 
10
    var resBreakpointMd = Metronic.getResponsiveBreakpoint('md');
11
 
12
    //* BEGIN:CORE HANDLERS *//
13
    // this function handles responsive layout on screen size resize or mobile device rotate.
14
 
15
 
16
    // Handle sidebar menu links
17
    var handleSidebarMenuActiveLink = function(mode, el) {
18
        var url = location.hash.toLowerCase();    
19
 
20
        var menu = $('.page-sidebar-menu');
21
 
22
        if (mode === 'click' || mode === 'set') {
23
            el = $(el);
24
        } else if (mode === 'match') {
25
            menu.find("li > a").each(function() {
26
                var path = $(this).attr("href").toLowerCase();      
27
                // url match condition         
28
                if (path.length > 1 && url.substr(1, path.length - 1) == path.substr(1)) {
29
                    el = $(this);
30
                    return;
31
                }
32
            });
33
        }
34
 
35
        if (!el || el.size() == 0) {
36
            return;
37
        }
38
 
39
        if (el.attr('href').toLowerCase() === 'javascript:;' || el.attr('href').toLowerCase() === '#') {
40
            return;
41
        }        
42
 
43
        var slideSpeed = parseInt(menu.data("slide-speed"));
44
        var keepExpand = menu.data("keep-expanded");
45
 
46
        // disable active states
47
        menu.find('li.active').removeClass('active');
48
        menu.find('li > a > .selected').remove();
49
 
50
        if (menu.hasClass('page-sidebar-menu-hover-submenu') === false) {
51
            menu.find('li.open').each(function(){
52
                if ($(this).children('.sub-menu').size() === 0) {
53
                    $(this).removeClass('open');
54
                    $(this).find('> a > .arrow.open').removeClass('open');
55
                }                            
56
            });
57
        } else {
58
             menu.find('li.open').removeClass('open');
59
        }
60
 
61
        el.parents('li').each(function () {
62
            $(this).addClass('active');
63
            $(this).find('> a > span.arrow').addClass('open');
64
 
65
            if ($(this).parent('ul.page-sidebar-menu').size() === 1) {
66
                $(this).find('> a').append('<span class="selected"></span>');
67
            }
68
 
69
            if ($(this).children('ul.sub-menu').size() === 1) {
70
                $(this).addClass('open');
71
            }
72
        });
73
 
74
        if (mode === 'click') {
75
            if (Metronic.getViewPort().width < resBreakpointMd && $('.page-sidebar').hasClass("in")) { // close the menu on mobile view while laoding a page 
76
                $('.page-header .responsive-toggler').click();
77
            }
78
        }
79
    };
80
 
81
    // Handle sidebar menu
82
    var handleSidebarMenu = function() {
83
        $('.page-sidebar').on('click', 'li > a', function(e) {
84
 
85
            if (Metronic.getViewPort().width >= resBreakpointMd && $(this).parents('.page-sidebar-menu-hover-submenu').size() === 1) { // exit of hover sidebar menu
86
                return;
87
            }
88
 
89
            if ($(this).next().hasClass('sub-menu') === false) {
90
                if (Metronic.getViewPort().width < resBreakpointMd && $('.page-sidebar').hasClass("in")) { // close the menu on mobile view while laoding a page 
91
                    $('.page-header .responsive-toggler').click();
92
                }
93
                return;
94
            }
95
 
96
            if ($(this).next().hasClass('sub-menu always-open')) {
97
                return;
98
            }
99
 
100
            var parent = $(this).parent().parent();
101
            var the = $(this);
102
            var menu = $('.page-sidebar-menu');
103
            var sub = $(this).next();
104
 
105
            var autoScroll = menu.data("auto-scroll");
106
            var slideSpeed = parseInt(menu.data("slide-speed"));
107
            var keepExpand = menu.data("keep-expanded");
108
 
109
            if (keepExpand !== true) {
110
                parent.children('li.open').children('a').children('.arrow').removeClass('open');
111
                parent.children('li.open').children('.sub-menu:not(.always-open)').slideUp(slideSpeed);
112
                parent.children('li.open').removeClass('open');
113
            }
114
 
115
            var slideOffeset = -200;
116
 
117
            if (sub.is(":visible")) {
118
                $('.arrow', $(this)).removeClass("open");
119
                $(this).parent().removeClass("open");
120
                sub.slideUp(slideSpeed, function() {
121
                    if (autoScroll === true && $('body').hasClass('page-sidebar-closed') === false) {
122
                        if ($('body').hasClass('page-sidebar-fixed')) {
123
                            menu.slimScroll({
124
                                'scrollTo': (the.position()).top
125
                            });
126
                        } else {
127
                            Metronic.scrollTo(the, slideOffeset);
128
                        }
129
                    }
130
                });
131
            } else {
132
                $('.arrow', $(this)).addClass("open");
133
                $(this).parent().addClass("open");
134
                sub.slideDown(slideSpeed, function() {
135
                    if (autoScroll === true && $('body').hasClass('page-sidebar-closed') === false) {
136
                        if ($('body').hasClass('page-sidebar-fixed')) {
137
                            menu.slimScroll({
138
                                'scrollTo': (the.position()).top
139
                            });
140
                        } else {
141
                            Metronic.scrollTo(the, slideOffeset);
142
                        }
143
                    }
144
                });
145
            }
146
 
147
            e.preventDefault();
148
        });
149
 
150
        // handle ajax links within sidebar menu
151
        $('.page-sidebar').on('click', ' li > a.ajaxify', function(e) {
152
            e.preventDefault();
153
            Metronic.scrollTop();
154
 
155
            var url = $(this).attr("href");
156
            var menuContainer = $('.page-sidebar ul');
157
            var pageContent = $('.page-content');
158
            var pageContentBody = $('.page-content .page-content-body');
159
 
160
            menuContainer.children('li.active').removeClass('active');
161
            menuContainer.children('arrow.open').removeClass('open');
162
 
163
            $(this).parents('li').each(function() {
164
                $(this).addClass('active');
165
                $(this).children('a > span.arrow').addClass('open');
166
            });
167
            $(this).parents('li').addClass('active');
168
 
169
            if (Metronic.getViewPort().width < resBreakpointMd && $('.page-sidebar').hasClass("in")) { // close the menu on mobile view while laoding a page 
170
                $('.page-header .responsive-toggler').click();
171
            }
172
 
173
            Metronic.startPageLoading();
174
 
175
            var the = $(this);
176
 
177
            $.ajax({
178
                type: "GET",
179
                cache: false,
180
                url: url,
181
                dataType: "html",
182
                success: function(res) {
183
 
184
                    if (the.parents('li.open').size() === 0) {
185
                        $('.page-sidebar-menu > li.open > a').click();
186
                    }
187
 
188
                    Metronic.stopPageLoading();
189
                    pageContentBody.html(res);
190
                    Layout.fixContentHeight(); // fix content height
191
                    Metronic.initAjax(); // initialize core stuff
192
                },
193
                error: function(xhr, ajaxOptions, thrownError) {
194
                    Metronic.stopPageLoading();
195
                    pageContentBody.html('<h4>Could not load the requested content.</h4>');
196
                }
197
            });
198
        });
199
 
200
        // handle ajax link within main content
201
        $('.page-content').on('click', '.ajaxify', function(e) {
202
            e.preventDefault();
203
            Metronic.scrollTop();
204
 
205
            var url = $(this).attr("href");
206
            var pageContent = $('.page-content');
207
            var pageContentBody = $('.page-content .page-content-body');
208
 
209
            Metronic.startPageLoading();
210
 
211
            if (Metronic.getViewPort().width < resBreakpointMd && $('.page-sidebar').hasClass("in")) { // close the menu on mobile view while laoding a page 
212
                $('.page-header .responsive-toggler').click();
213
            }
214
 
215
            $.ajax({
216
                type: "GET",
217
                cache: false,
218
                url: url,
219
                dataType: "html",
220
                success: function(res) {
221
                    Metronic.stopPageLoading();
222
                    pageContentBody.html(res);
223
                    Layout.fixContentHeight(); // fix content height
224
                    Metronic.initAjax(); // initialize core stuff
225
                },
226
                error: function(xhr, ajaxOptions, thrownError) {
227
                    pageContentBody.html('<h4>Could not load the requested content.</h4>');
228
                    Metronic.stopPageLoading();
229
                }
230
            });
231
        });
232
 
233
        // handle scrolling to top on responsive menu toggler click when header is fixed for mobile view
234
        $(document).on('click', '.page-header-fixed-mobile .responsive-toggler', function(){
235
            Metronic.scrollTop();
236
        });      
237
    };
238
 
239
    // Helper function to calculate sidebar height for fixed sidebar layout.
240
    var _calculateFixedSidebarViewportHeight = function() {
241
        var sidebarHeight = Metronic.getViewPort().height - $('.page-header').outerHeight() - 30;
242
        if ($('body').hasClass("page-footer-fixed")) {
243
            sidebarHeight = sidebarHeight - $('.page-footer').outerHeight();
244
        }
245
 
246
        return sidebarHeight;
247
    };
248
 
249
    // Handles fixed sidebar
250
    var handleFixedSidebar = function() {
251
        var menu = $('.page-sidebar-menu');
252
 
253
        Metronic.destroySlimScroll(menu);
254
 
255
        if ($('.page-sidebar-fixed').size() === 0) {
256
            return;
257
        }
258
 
259
        if (Metronic.getViewPort().width >= resBreakpointMd) {
260
            menu.attr("data-height", _calculateFixedSidebarViewportHeight());
261
            Metronic.initSlimScroll(menu);
262
        }
263
    };
264
 
265
    // Handles sidebar toggler to close/hide the sidebar.
266
    var handleFixedSidebarHoverEffect = function () {
267
        var body = $('body');
268
        if (body.hasClass('page-sidebar-fixed')) {
269
            $('.page-sidebar').on('mouseenter', function () {
270
                if (body.hasClass('page-sidebar-closed')) {
271
                    $(this).find('.page-sidebar-menu').removeClass('page-sidebar-menu-closed');
272
                }
273
            }).on('mouseleave', function () {
274
                if (body.hasClass('page-sidebar-closed')) {
275
                    $(this).find('.page-sidebar-menu').addClass('page-sidebar-menu-closed');
276
                }
277
            });
278
        }
279
    };
280
 
281
    // Hanles sidebar toggler
282
    var handleSidebarToggler = function() {
283
        var body = $('body');
284
        if ($.cookie && $.cookie('sidebar_closed') === '1' && Metronic.getViewPort().width >= resBreakpointMd) {
285
            $('body').addClass('page-sidebar-closed');
286
            $('.page-sidebar-menu').addClass('page-sidebar-menu-closed');
287
        }
288
 
289
        // handle sidebar show/hide
290
        $('body').on('click', '.sidebar-toggler', function(e) {
291
            var sidebar = $('.page-sidebar');
292
            var sidebarMenu = $('.page-sidebar-menu');
293
            $(".sidebar-search", sidebar).removeClass("open");
294
 
295
            if (body.hasClass("page-sidebar-closed")) {
296
                body.removeClass("page-sidebar-closed");
297
                sidebarMenu.removeClass("page-sidebar-menu-closed");
298
                if ($.cookie) {
299
                    $.cookie('sidebar_closed', '0');
300
                }
301
            } else {
302
                body.addClass("page-sidebar-closed");
303
                sidebarMenu.addClass("page-sidebar-menu-closed");
304
                if (body.hasClass("page-sidebar-fixed")) {
305
                    sidebarMenu.trigger("mouseleave");
306
                }
307
                if ($.cookie) {
308
                    $.cookie('sidebar_closed', '1');
309
                }
310
            }
311
 
312
            $(window).trigger('resize');
313
        });
314
 
315
        handleFixedSidebarHoverEffect();
316
 
317
        // handle the search bar close
318
        $('.page-sidebar').on('click', '.sidebar-search .remove', function(e) {
319
            e.preventDefault();
320
            $('.sidebar-search').removeClass("open");
321
        });
322
 
323
        // handle the search query submit on enter press
324
        $('.page-sidebar .sidebar-search').on('keypress', 'input.form-control', function(e) {
325
            if (e.which == 13) {
326
                $('.sidebar-search').submit();
327
                return false; //<---- Add this line
328
            }
329
        });
330
 
331
        // handle the search submit(for sidebar search and responsive mode of the header search)
332
        $('.sidebar-search .submit').on('click', function(e) {
333
            e.preventDefault();
334
            if ($('body').hasClass("page-sidebar-closed")) {
335
                if ($('.sidebar-search').hasClass('open') === false) {
336
                    if ($('.page-sidebar-fixed').size() === 1) {
337
                        $('.page-sidebar .sidebar-toggler').click(); //trigger sidebar toggle button
338
                    }
339
                    $('.sidebar-search').addClass("open");
340
                } else {
341
                    $('.sidebar-search').submit();
342
                }
343
            } else {
344
                $('.sidebar-search').submit();
345
            }
346
        });
347
 
348
        // handle close on body click
349
        if ($('.sidebar-search').size() !== 0) {
350
            $('.sidebar-search .input-group').on('click', function(e) {
351
                e.stopPropagation();
352
            });
353
 
354
            $('body').on('click', function() {
355
                if ($('.sidebar-search').hasClass('open')) {
356
                    $('.sidebar-search').removeClass("open");
357
                }
358
            });
359
        }
360
    };
361
 
362
    // Handles the horizontal menu
363
    var handleHeader = function() {
364
        // handle search box expand/collapse        
365
        $('.page-header').on('click', '.search-form', function(e) {
366
            $(this).addClass("open");
367
            $(this).find('.form-control').focus();
368
 
369
            $('.page-header .search-form .form-control').on('blur', function(e) {
370
                $(this).closest('.search-form').removeClass("open");
371
                $(this).unbind("blur");
372
            });
373
        });
374
 
375
        // handle hor menu search form on enter press
376
        $('.page-header').on('keypress', '.hor-menu .search-form .form-control', function(e) {
377
            if (e.which == 13) {
378
                $(this).closest('.search-form').submit();
379
                return false;
380
            }
381
        });
382
 
383
        // handle header search button click
384
        $('.page-header').on('mousedown', '.search-form.open .submit', function(e) {
385
            e.preventDefault();
386
            e.stopPropagation();
387
            $(this).closest('.search-form').submit();
388
        });
389
    };
390
 
391
    // Handles the go to top button at the footer
392
    var handleGoTop = function() {
393
        var offset = 300;
394
        var duration = 500;
395
 
396
        if (navigator.userAgent.match(/iPhone|iPad|iPod/i)) { // ios supported
397
            $(window).bind("touchend touchcancel touchleave", function(e) {
398
                if ($(this).scrollTop() > offset) {
399
                    $('.scroll-to-top').fadeIn(duration);
400
                } else {
401
                    $('.scroll-to-top').fadeOut(duration);
402
                }
403
            });
404
        } else { // general 
405
            $(window).scroll(function() {
406
                if ($(this).scrollTop() > offset) {
407
                    $('.scroll-to-top').fadeIn(duration);
408
                } else {
409
                    $('.scroll-to-top').fadeOut(duration);
410
                }
411
            });
412
        }
413
 
414
        $('.scroll-to-top').click(function(e) {
415
            e.preventDefault();
416
            $('html, body').animate({
417
                scrollTop: 0
418
            }, duration);
419
            return false;
420
        });
421
    };
422
    //* END:CORE HANDLERS *//
423
 
424
    return {
425
 
426
        // Main init methods to initialize the layout
427
        // IMPORTANT!!!: Do not modify the core handlers call order.
428
 
429
        initHeader: function() {
430
            handleHeader(); // handles horizontal menu    
431
        },
432
 
433
        setSidebarMenuActiveLink: function(mode, el) {
434
            handleSidebarMenuActiveLink(mode, el);
435
        },
436
 
437
        initSidebar: function() {
438
            //layout handlers
439
            handleFixedSidebar(); // handles fixed sidebar menu
440
            handleSidebarMenu(); // handles main menu
441
            handleSidebarToggler(); // handles sidebar hide/show
442
 
443
            if (Metronic.isAngularJsApp()) {      
444
                handleSidebarMenuActiveLink('match'); // init sidebar active links 
445
            }
446
 
447
            Metronic.addResizeHandler(handleFixedSidebar); // reinitialize fixed sidebar on window resize
448
        },
449
 
450
        initContent: function() {
451
            return;
452
        },
453
 
454
        initFooter: function() {
455
            handleGoTop(); //handles scroll to top functionality in the footer
456
        },
457
 
458
        init: function () {            
459
            this.initHeader();
460
            this.initSidebar();
461
            this.initContent();
462
            this.initFooter();
463
        },
464
 
465
        //public function to fix the sidebar and content height accordingly
466
        fixContentHeight: function() {
467
            return;
468
        },
469
 
470
        initFixedSidebarHoverEffect: function() {
471
            handleFixedSidebarHoverEffect();
472
        },
473
 
474
        initFixedSidebar: function() {
475
            handleFixedSidebar();
476
        },
477
 
478
        getLayoutImgPath: function() {
479
            return Metronic.getAssetsPath() + layoutImgPath;
480
        },
481
 
482
        getLayoutCssPath: function() {
483
            return Metronic.getAssetsPath() + layoutCssPath;
484
        }
485
    };
486
 
487
}();