]> CyberLeo.Net >> Repos - Github/YOURLS.git/blob - js/common.js
Better encoding of text to be shared on Twitter. Fixes issue 758.
[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         closeme = ( type == 'fail' || type == 'error' ) ? true : false;         \r
33         delay = delay || ( closeme == true ? 10000 : 3500 );\r
34         $.notifyBar({\r
35                 html: '<span>'+msg+'</span>',\r
36                 delay: delay,\r
37                 animationSpeed: "normal",\r
38                 close: closeme,\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         // Split the url on '?' and get only the params (which is element 1)\r
91         url = url.split('?')[1];\r
92         // Now split those params on '&' so we can get each one individually (Ex. param_var=param_value)\r
93         url = url.split('&');\r
94         // Now we have to find the varname in that array using methods that IE likes (Curse you IE!!!)\r
95         var i=0;\r
96         for( i=0; i<url.length; i++ ){\r
97                 // So split the first param elemment on '=' and check the param_var to see if it matches varname (element 0)\r
98                 if( url[i].split('=')[0] == varname ){\r
99                         // If it matches we want to return the param_value\r
100                         return url[i].split('=')[1];\r
101                 }\r
102         }\r
103         \r
104         // If we didn't find anything then we just return the default_val\r
105         return default_val;     \r
106 }\r
107 \r
108 /**\r
109  * Jquery Cookie plugin\r
110  * Copyright (c) 2006 Klaus Hartl (stilbuero.de)\r
111  * Dual licensed under the MIT and GPL licenses:\r
112  * http://www.opensource.org/licenses/mit-license.php\r
113  * http://www.gnu.org/licenses/gpl.html\r
114  * Available at http://plugins.jquery.com/files/jquery.cookie.js.txt\r
115  */\r
116 jQuery.cookie = function(name, value, options) {\r
117     if (typeof value != 'undefined') { // name and value given, set cookie\r
118         options = options || {};\r
119         if (value === null) {\r
120             value = '';\r
121             options.expires = -1;\r
122         }\r
123         var expires = '';\r
124         if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {\r
125             var date;\r
126             if (typeof options.expires == 'number') {\r
127                 date = new Date();\r
128                 date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));\r
129             } else {\r
130                 date = options.expires;\r
131             }\r
132             expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE\r
133         }\r
134         // CAUTION: Needed to parenthesize options.path and options.domain\r
135         // in the following expressions, otherwise they evaluate to undefined\r
136         // in the packed version for some reason...\r
137         var path = options.path ? '; path=' + (options.path) : '';\r
138         var domain = options.domain ? '; domain=' + (options.domain) : '';\r
139         var secure = options.secure ? '; secure' : '';\r
140         document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');\r
141     } else { // only name given, get cookie\r
142         var cookieValue = null;\r
143         if (document.cookie && document.cookie != '') {\r
144             var cookies = document.cookie.split(';');\r
145             for (var i = 0; i < cookies.length; i++) {\r
146                 var cookie = jQuery.trim(cookies[i]);\r
147                 // Does this cookie string begin with the name we want?\r
148                 if (cookie.substring(0, name.length + 1) == (name + '=')) {\r
149                     cookieValue = decodeURIComponent(cookie.substring(name.length + 1));\r
150                     break;\r
151                 }\r
152             }\r
153         }\r
154         return cookieValue;\r
155     }\r
156 };\r
157 \r