fix path error
[project/luci.git] / themes / luci-theme-material / htdocs / luci-static / material / js / script.js
1 /**
2  * Created by WebStorm on 15-9-13.
3  */
4 (function ($) {
5     var b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/",
6         a256 = '',
7         r64 = [256],
8         r256 = [256],
9         i = 0;
10     var UTF8 = {
11         /**
12          * Encode multi-byte Unicode string into utf-8 multiple single-byte characters
13          * (BMP / basic multilingual plane only)
14          *
15          * Chars in range U+0080 - U+07FF are encoded in 2 chars, U+0800 - U+FFFF in 3 chars
16          *
17          * @param {String} strUni Unicode string to be encoded as UTF-8
18          * @returns {String} encoded string
19          */
20         encode: function (strUni) {
21             // use regular expressions & String.replace callback function for better efficiency
22             // than procedural approaches
23             var strUtf = strUni.replace(/[\u0080-\u07ff]/g, // U+0080 - U+07FF => 2 bytes 110yyyyy, 10zzzzzz
24                 function (c) {
25                     var cc = c.charCodeAt(0);
26                     return String.fromCharCode(0xc0 | cc >> 6, 0x80 | cc & 0x3f);
27                 })
28                 .replace(/[\u0800-\uffff]/g, // U+0800 - U+FFFF => 3 bytes 1110xxxx, 10yyyyyy, 10zzzzzz
29                 function (c) {
30                     var cc = c.charCodeAt(0);
31                     return String.fromCharCode(0xe0 | cc >> 12, 0x80 | cc >> 6 & 0x3F, 0x80 | cc & 0x3f);
32                 });
33             return strUtf;
34         },
35         /**
36          * Decode utf-8 encoded string back into multi-byte Unicode characters
37          *
38          * @param {String} strUtf UTF-8 string to be decoded back to Unicode
39          * @returns {String} decoded string
40          */
41         decode: function (strUtf) {
42             // note: decode 3-byte chars first as decoded 2-byte strings could appear to be 3-byte char!
43             var strUni = strUtf.replace(/[\u00e0-\u00ef][\u0080-\u00bf][\u0080-\u00bf]/g, // 3-byte chars
44                 function (c) { // (note parentheses for precence)
45                     var cc = ((c.charCodeAt(0) & 0x0f) << 12) | ((c.charCodeAt(1) & 0x3f) << 6) | (c.charCodeAt(2) & 0x3f);
46                     return String.fromCharCode(cc);
47                 })
48                 .replace(/[\u00c0-\u00df][\u0080-\u00bf]/g, // 2-byte chars
49                 function (c) { // (note parentheses for precence)
50                     var cc = (c.charCodeAt(0) & 0x1f) << 6 | c.charCodeAt(1) & 0x3f;
51                     return String.fromCharCode(cc);
52                 });
53             return strUni;
54         }
55     };
56     while (i < 256) {
57         var c = String.fromCharCode(i);
58         a256 += c;
59         r256[i] = i;
60         r64[i] = b64.indexOf(c);
61         ++i;
62     }
63     function code(s, discard, alpha, beta, w1, w2) {
64         s = String(s);
65         var buffer = 0,
66             i = 0,
67             length = s.length,
68             result = '',
69             bitsInBuffer = 0;
70         while (i < length) {
71             var c = s.charCodeAt(i);
72             c = c < 256 ? alpha[c] : -1;
73             buffer = (buffer << w1) + c;
74             bitsInBuffer += w1;
75             while (bitsInBuffer >= w2) {
76                 bitsInBuffer -= w2;
77                 var tmp = buffer >> bitsInBuffer;
78                 result += beta.charAt(tmp);
79                 buffer ^= tmp << bitsInBuffer;
80             }
81             ++i;
82         }
83         if (!discard && bitsInBuffer > 0) result += beta.charAt(buffer << (w2 - bitsInBuffer));
84         return result;
85     }
86
87     var Plugin = $.base64 = function (dir, input, encode) {
88         return input ? Plugin[dir](input, encode) : dir ? null : this;
89     };
90     Plugin.btoa = Plugin.encode = function (plain, utf8encode) {
91         plain = Plugin.raw === false || Plugin.utf8encode || utf8encode ? UTF8.encode(plain) : plain;
92         plain = code(plain, false, r256, b64, 8, 6);
93         return plain + '===='.slice((plain.length % 4) || 4);
94     };
95     Plugin.atob = Plugin.decode = function (coded, utf8decode) {
96         coded = String(coded).split('=');
97         var i = coded.length;
98         do {
99             --i;
100             coded[i] = code(coded[i], true, r64, a256, 6, 8);
101         } while (i > 0);
102         coded = coded.join('');
103         return Plugin.raw === false || Plugin.utf8decode || utf8decode ? UTF8.decode(coded) : coded;
104     };
105 }(jQuery));
106
107
108 function trimText(text) {
109     return text.replace(/[ \t\n\r]+/g, " ");
110 }
111
112 (function ($) {
113     var tree = undefined;
114     var lastNode = undefined;
115
116     function getCurrentNodeByHash() {
117         var ret = false;
118         var hash = window.location.hash;
119         if (hash.substr(0, 6) == "#tree-") {
120             hash = $.base64.decode(hash.substr(6));
121             tree = hash.split("|");
122             $(".main > .main-left > .nav > .slide > .menu").each(function () {
123                 var that = $(this);
124                 if (trimText(that.data("title")) == tree[0]) {
125                     that.click();
126                     that.next().find("a").each(function () {
127                         var that = $(this);
128                         if (trimText(that.data("title")) == tree[1]) {
129                             lastNode = that.parent();
130                             lastNode.addClass("active");
131                             ret = true;
132                             return true;
133                         }
134                     });
135                 }
136             });
137         }
138         return ret;
139     }
140
141     function getCurrentNodeByUrl() {
142         var ret = false;
143         var getUrlNode = function (href){
144             var linkPos = href.indexOf(";");
145             if (linkPos == -1){
146                 return "login";
147             }else{
148                 linkPos = href.indexOf("/", linkPos);
149                 if (linkPos == -1){
150                     return "overview";
151                 }else{
152                     var link = href.substr(linkPos);
153                     if (link == "/")
154                         return "overview";
155                     else
156                         return link;
157                 }
158             }
159         };
160
161         var currentNode = getUrlNode(window.location.pathname);
162
163         if (currentNode == "login"){
164             tree = ["Main", "Login"];
165             return false;
166         }else if(currentNode == "overview"){
167             tree = ["Status", "Overview"];
168             lastNode = $($($(".main > .main-left > .nav > .slide > .menu")[0]).next().find("a")[0]).parent();
169             return false;
170         }
171
172         $(".main > .main-left > .nav > .slide > .menu").each(function () {
173             var ulNode = $(this);
174             ulNode.next().find("a").each(function () {
175                 var that = $(this);
176                 var href = that.attr("href");
177
178                 if (currentNode.indexOf(getUrlNode(href)) != -1){
179                     ulNode.click();
180                     lastNode = that.parent();
181                     tree = [trimText(ulNode.data("title")), trimText(that.data("title"))];
182                     lastNode.addClass("active");
183                     ret = true;
184                     return true;
185                 }
186             });
187         });
188         return ret;
189     }
190
191     $(".main > .main-left > .nav > .slide > .menu").click(function () {
192         var ul = $(this).next(".slide-menu");
193         var menu = $(this);
194         if (!ul.is(":visible")) {
195             menu.addClass("active");
196             ul.addClass("active");
197             ul.stop(true).slideDown();
198         } else {
199             ul.slideUp(function () {
200                 menu.removeClass("active");
201                 ul.removeClass("active");
202             });
203         }
204     });
205
206     $(".main > .main-left > .nav > .slide > .slide-menu > li > a").click(function () {
207         var href = $(this).attr("href");
208         var tree = trimText($(this).parent().parent().prev().data("title")) + "|" + trimText($(this).data("title"));
209         tree = $.base64.encode(tree);
210         window.location = href + "#tree-" + tree;
211         if (lastNode != undefined) lastNode.removeClass("active");
212         $(this).parent().addClass("active");
213         return false;
214     });
215
216     $(".main > .main-left > .nav > .slide > .slide-menu > li").click(function () {
217         $(this).find("a").click();
218     });
219
220
221     if (!getCurrentNodeByUrl()){
222         getCurrentNodeByHash();
223         if (tree[0] == "Status" && tree[1] == "Overview"){
224             //overview
225             lastNode.addClass("active");
226             $($(".main > .main-left > .nav > .slide > .menu")[0]).click();
227         }
228     }
229     var mainNodeName = "node-"+ tree[0] + "-" + tree[1];
230     $("body").addClass(mainNodeName.replace(/[ \t\n\r\/]+/g,"_").toLowerCase());
231     $(".cbi-button-up").val("");
232     $(".cbi-button-down").val("");
233
234
235     $("#maincontent > .container").find("a").each(function () {
236         var that = $(this);
237         var onclick = that.attr("onclick");
238         if (onclick == undefined || onclick == ""){
239             that.click(function () {
240                 var href = that.attr("href");
241                 if (tree != undefined && href.indexOf("Text") == -1) {
242                     window.location = href + "#tree-" +  $.base64.encode(tree[0] + "|" + tree[1]);
243                     return false;
244                 }else{
245                     return true;
246                 }
247             });
248         }
249     });
250
251     var showSide = false;
252     $(".showSide").click(function () {
253         if (showSide){
254             $(".darkMask").stop(true).fadeOut();
255             $(".main-left").stop(true).animate({
256                 width: "0"
257             });
258             showSide = false;
259         }else{
260             $(".darkMask").stop(true).fadeIn();
261             $(".main-left").stop(true).animate({
262                 width: "15rem"
263             });
264             showSide = true;
265         }
266     });
267
268
269     $(".darkMask").click(function () {
270         if (showSide){
271             showSide = false;
272             $(".darkMask").stop(true).fadeOut();
273             $(".main-left").stop(true).animate({
274                 width: "0"
275             });
276         }
277     });
278
279     $(window).resize(function() {
280         if ($(window).width() > 921) {
281             $(".main-left").css("width", "");
282             $(".darkMask").stop(true);
283             $(".darkMask").css("display", "none");
284             showSide = false;
285         }
286     });
287
288     $("legend").each(function () {
289         var that = $(this);
290         that.after("<span class='panel-title'>" + that.text() + "</span>");
291     });
292
293     window.scrollTo(0,1);
294 })(jQuery);