PageRenderTime 25ms CodeModel.GetById 0ms RepoModel.GetById 1ms app.codeStats 0ms

/js/common.js

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