]> CyberLeo.Net >> Repos - Github/YOURLS.git/blob - js/common.js
Implement .hide-if-*js classes
[Github/YOURLS.git] / js / common.js
1 // Handle .hide-if-no-js and .hide-if-js styles\r
2 $(document).ready(function(){\r
3         $('.hide-if-no-js').removeClass('hide-if-no-js');\r
4         $('.hide-if-js').hide();\r
5 });\r
6 \r
7 // Change an element text an revert in a smooth pulse. el is an element id like '#copybox h2'\r
8 function html_pulse( el, newtext ){\r
9         var oldtext = $(el).html();\r
10         // Fast pulse to "Copied" and revert\r
11         $(el).fadeTo(\r
12                 "normal",\r
13                 0.01,\r
14                 function(){\r
15                         $(el)\r
16                         .html( newtext )\r
17                         .css('opacity', 1)\r
18                         .fadeTo(\r
19                                 "slow", 1, // this fades from 1 to 1: just a 'sleep(1)' actually\r
20                                 function(){\r
21                                         $(el).fadeTo("normal", 0.01, function(){$(el).html( oldtext ).css('opacity', 1)});\r
22                                 }\r
23                         );\r
24                 }\r
25         );\r
26 \r
27 \r
28 }\r
29 \r
30 // Update feedback message\r
31 function feedback(msg, type, delay) {\r
32         close = ( type == 'fail' || type == 'error' ) ? true : false;           \r
33         delay = delay || ( close == true ? 10000 : 3500 );\r
34         $.notifyBar({\r
35                 html: '<span>'+msg+'</span>',\r
36                 delay: delay,\r
37                 animationSpeed: "normal",\r
38                 close: close,\r
39                 cls: type\r
40         });\r
41         return true;\r
42 }\r
43 \r
44 // Unused for now\r
45 function logout() {\r
46         $.ajax({\r
47                 type: "POST",\r
48                 url: ajaxurl,\r
49                 data: {action:'logout'},\r
50                 success: function() {\r
51                         window.parent.location.href = window.parent.location.href;\r
52                 }\r
53         });\r
54 }\r
55 \r
56 // Begin the spinning animation & disable a button\r
57 function add_loading(el) {\r
58         $(el).attr("disabled", "disabled").addClass('disabled').addClass('loading');\r
59 }\r
60 \r
61 // End spinning animation\r
62 function end_loading(el) {\r
63         $(el).removeClass('loading');\r
64 }\r
65 \r
66 // Un-disable an element\r
67 function end_disable(el) {\r
68         $(el).removeAttr("disabled").removeClass('disabled');\r
69 }\r
70 \r
71 // Trim long string\r
72 function trim_long_string( string, length) {\r
73         var newstring = string;\r
74         length = length || 60;\r
75         if ( newstring.length > length ) {\r
76                 newstring = newstring.substr(0, (length - 5) ) + '[...]';       \r
77         }\r
78         return newstring;\r
79 }\r
80 \r
81 // Get the var=xxx from a query string\r
82 function get_var_from_query( url, varname, default_val ) {\r
83         if( varname == undefined ) {\r
84                 varname = 'nonce';\r
85         }\r
86         if( default_val == undefined ) {\r
87                 default_val = '';\r
88         }\r
89         \r
90         try{\r
91                 url = url.split('?')[1].split('&').reverse().filter(function(e){\r
92                         var pair = e.split('=');\r
93                         return( pair[0] == varname );\r
94                 })[0].split('=')[1];\r
95                 // weeeeeeee\r
96                 // split the query string on '&', reverse to check last pairs first so that ?ozh=1&ozh=2 matches ozh=2 first\r
97                 // then filter on each pair to find the matching 'varname=something',\r
98                 // which is then returned in a one element array that we split on '=' and take second part. woot!\r
99         } catch(err) {\r
100                 return default_val;\r
101         }\r
102         \r
103         return url;\r
104 }\r
105 \r
106 /**\r
107  * Jquery Cookie plugin\r
108  * Copyright (c) 2006 Klaus Hartl (stilbuero.de)\r
109  * Dual licensed under the MIT and GPL licenses:\r
110  * http://www.opensource.org/licenses/mit-license.php\r
111  * http://www.gnu.org/licenses/gpl.html\r
112  * Available at http://plugins.jquery.com/files/jquery.cookie.js.txt\r
113  */\r
114 jQuery.cookie = function(name, value, options) {\r
115     if (typeof value != 'undefined') { // name and value given, set cookie\r
116         options = options || {};\r
117         if (value === null) {\r
118             value = '';\r
119             options.expires = -1;\r
120         }\r
121         var expires = '';\r
122         if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {\r
123             var date;\r
124             if (typeof options.expires == 'number') {\r
125                 date = new Date();\r
126                 date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));\r
127             } else {\r
128                 date = options.expires;\r
129             }\r
130             expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE\r
131         }\r
132         // CAUTION: Needed to parenthesize options.path and options.domain\r
133         // in the following expressions, otherwise they evaluate to undefined\r
134         // in the packed version for some reason...\r
135         var path = options.path ? '; path=' + (options.path) : '';\r
136         var domain = options.domain ? '; domain=' + (options.domain) : '';\r
137         var secure = options.secure ? '; secure' : '';\r
138         document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');\r
139     } else { // only name given, get cookie\r
140         var cookieValue = null;\r
141         if (document.cookie && document.cookie != '') {\r
142             var cookies = document.cookie.split(';');\r
143             for (var i = 0; i < cookies.length; i++) {\r
144                 var cookie = jQuery.trim(cookies[i]);\r
145                 // Does this cookie string begin with the name we want?\r
146                 if (cookie.substring(0, name.length + 1) == (name + '=')) {\r
147                     cookieValue = decodeURIComponent(cookie.substring(name.length + 1));\r
148                     break;\r
149                 }\r
150             }\r
151         }\r
152         return cookieValue;\r
153     }\r
154 };\r
155 \r