Merge pull request #1818 from dibdot/lxc_fix
[project/luci.git] / themes / luci-theme-material / htdocs / luci-static / material / js / script.js
1 /**
2  *  Material is a clean HTML5 theme for LuCI. It is based on luci-theme-bootstrap and MUI
3  *
4  *  luci-theme-material
5  *      Copyright 2015 Lutty Yang <lutty@wcan.in>
6  *
7  *  Have a bug? Please create an issue here on GitHub!
8  *      https://github.com/LuttyYang/luci-theme-material/issues
9  *
10  *  luci-theme-bootstrap:
11  *      Copyright 2008 Steven Barth <steven@midlink.org>
12  *      Copyright 2008 Jo-Philipp Wich <jow@openwrt.org>
13  *      Copyright 2012 David Menting <david@nut-bolt.nl>
14  *
15  *  MUI:
16  *      https://github.com/muicss/mui
17  *
18  *  Licensed to the public under the Apache License 2.0
19  */
20 (function ($) {
21     $(".main > .loading").fadeOut();
22
23     /**
24      * trim text, Remove spaces, wrap
25      * @param text
26      * @returns {string}
27      */
28     function trimText(text) {
29         return text.replace(/[ \t\n\r]+/g, " ");
30     }
31
32
33     var lastNode = undefined;
34     var mainNodeName = undefined;
35
36     var nodeUrl = "";
37     (function(node){
38         if (node[0] == "admin"){
39             luciLocation = [node[1], node[2]];
40         }else{
41             luciLocation = node;
42         }
43
44         for(var i in luciLocation){
45             nodeUrl += luciLocation[i];
46             if (i != luciLocation.length - 1){
47                 nodeUrl += "/";
48             }
49         }
50     })(luciLocation);
51
52     /**
53      * get the current node by Burl (primary)
54      * @returns {boolean} success?
55      */
56     function getCurrentNodeByUrl() {
57         var ret = false;
58         if (!$('body').hasClass('logged-in')) {
59             luciLocation = ["Main", "Login"];
60             return true;
61         }
62
63         $(".main > .main-left > .nav > .slide > .menu").each(function () {
64             var ulNode = $(this);
65             ulNode.next().find("a").each(function () {
66                 var that = $(this);
67                 var href = that.attr("href");
68
69                 if (href.indexOf(nodeUrl) != -1) {
70                     ulNode.click();
71                     ulNode.next(".slide-menu").stop(true, true);
72                     lastNode = that.parent();
73                     lastNode.addClass("active");
74                     ret = true;
75                     return true;
76                 }
77             });
78         });
79         return ret;
80     }
81
82     /**
83      * menu click
84      */
85     $(".main > .main-left > .nav > .slide > .menu").click(function () {
86         var ul = $(this).next(".slide-menu");
87         var menu = $(this);
88         if (!ul.is(":visible")) {
89             menu.addClass("active");
90             ul.addClass("active");
91             ul.stop(true).slideDown("fast");
92         } else {
93             ul.stop(true).slideUp("fast", function () {
94                 menu.removeClass("active");
95                 ul.removeClass("active");
96             });
97         }
98         return false;
99     });
100
101     /**
102      * hook menu click and add the hash
103      */
104     $(".main > .main-left > .nav > .slide > .slide-menu > li > a").click(function () {
105         if (lastNode != undefined) lastNode.removeClass("active");
106         $(this).parent().addClass("active");
107         $(".main > .loading").fadeIn("fast");
108         return true;
109     });
110
111     /**
112      * fix menu click
113      */
114     $(".main > .main-left > .nav > .slide > .slide-menu > li").click(function () {
115         if (lastNode != undefined) lastNode.removeClass("active");
116         $(this).addClass("active");
117         $(".main > .loading").fadeIn("fast");
118         window.location = $($(this).find("a")[0]).attr("href");
119         return false;
120     });
121
122     /**
123      * get current node and open it
124      */
125     if (getCurrentNodeByUrl()) {
126         mainNodeName = "node-" + luciLocation[0] + "-" + luciLocation[1];
127         mainNodeName = mainNodeName.replace(/[ \t\n\r\/]+/g, "_").toLowerCase();
128         $("body").addClass(mainNodeName);
129     }
130     $(".cbi-button-up").val("");
131     $(".cbi-button-down").val("");
132
133
134     /**
135      * hook other "A Label" and add hash to it.
136      */
137     $("#maincontent > .container").find("a").each(function () {
138         var that = $(this);
139         var onclick = that.attr("onclick");
140         if (onclick == undefined || onclick == "") {
141             that.click(function () {
142                 var href = that.attr("href");
143                 if (href.indexOf("#") == -1) {
144                     $(".main > .loading").fadeIn("fast");
145                     return true;
146                 }
147             });
148         }
149     });
150
151     /**
152      * Sidebar expand
153      */
154     var showSide = false;
155     $(".showSide").click(function () {
156         if (showSide) {
157             $(".darkMask").stop(true).fadeOut("fast");
158             $(".main-left").stop(true).animate({
159                 width: "0"
160             }, "fast");
161             $(".main-right").css("overflow-y", "auto");
162             showSide = false;
163         } else {
164             $(".darkMask").stop(true).fadeIn("fast");
165             $(".main-left").stop(true).animate({
166                 width: "15rem"
167             }, "fast");
168             $(".main-right").css("overflow-y", "hidden");
169             showSide = true;
170         }
171     });
172
173
174     $(".darkMask").click(function () {
175         if (showSide) {
176             showSide = false;
177             $(".darkMask").stop(true).fadeOut("fast");
178             $(".main-left").stop(true).animate({
179                 width: "0"
180             }, "fast");
181             $(".main-right").css("overflow-y", "auto");
182         }
183     });
184
185     $(window).resize(function () {
186         if ($(window).width() > 921) {
187             $(".main-left").css("width", "");
188             $(".darkMask").stop(true);
189             $(".darkMask").css("display", "none");
190             showSide = false;
191         }
192     });
193
194     /**
195      * fix legend position
196      */
197     $("legend").each(function () {
198         var that = $(this);
199         that.after("<span class='panel-title'>" + that.text() + "</span>");
200     });
201
202     $(".cbi-section-table-titles, .cbi-section-table-descr, .cbi-section-descr").each(function () {
203         var that = $(this);
204         if (that.text().trim() == ""){
205             that.css("display", "none");
206         }
207     });
208
209
210     $(".main-right").focus();
211     $(".main-right").blur();
212     $("input").attr("size", "0");
213
214     if (mainNodeName != undefined) {
215         console.log(mainNodeName);
216         switch (mainNodeName) {
217             case "node-status-system_log":
218             case "node-status-kernel_log":
219                 $("#syslog").focus(function () {
220                     $("#syslog").blur();
221                     $(".main-right").focus();
222                     $(".main-right").blur();
223                 });
224                 break;
225             case "node-status-firewall":
226                 var button = $(".node-status-firewall > .main fieldset li > a");
227                 button.addClass("cbi-button cbi-button-reset a-to-btn");
228                 break;
229             case "node-system-reboot":
230                 var button = $(".node-system-reboot > .main > .main-right p > a");
231                 button.addClass("cbi-button cbi-input-reset a-to-btn");
232                 break;
233         }
234     }
235
236 })(jQuery);