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