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