Guest User

Untitled

a guest
Apr 25th, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.87 KB | None | 0 0
  1. ;(function($) {
  2. $.fn.extend({
  3. getID: function() {
  4. return parseInt($(this).attr("id").match(/\d+/)[0]);
  5. },
  6. setInputHint: function() {
  7. if ($(this).length > 1) {
  8. $(this).each(function() {
  9. $(this).setInputHint();
  10. });
  11. } else {
  12. var el = $(this);
  13. el._default = el.val();
  14.  
  15. el.focus(function() {
  16. if(el._default != el.val()) return;
  17. el.removeClass('hint').val('');
  18. })
  19. .blur(function() {
  20. if($.trim(el.val()) != '') return;
  21. el.addClass('hint').val(el._default);
  22. })
  23. .addClass('hint');
  24. }
  25. },
  26. addSelector: function(selector) {
  27. var classes = selector.split('.'),
  28. $this = $(this);
  29. $.each(classes, function(i, c) {
  30. if (c.match(/^#/)) {
  31. $this.attr('id', c.replace('#',''));
  32. } else {
  33. $this.addClass(c);
  34. }
  35. });
  36. return $this;
  37. },
  38. findOrAppend: function(selector, element) {
  39. var $this = $(this),
  40. $el = $this.find(selector);
  41. if ($el.length > 0) return $el;
  42. // el doesnt exist so lets make it
  43. if (typeof element == 'undefined') {
  44. // figure out how to build the element from the selector
  45. var parts = selector.split(' ');
  46. $el = $("<div>");
  47. if (parts.length == 1) {
  48. // single selector so just added
  49. $el.addSelector(selector);
  50. } else {
  51. // selector with multiple depth so we just want the last selector
  52. $el.addSelector(parts.pop());
  53. // find this down the parts
  54. $this = $this.findOrAppend(parts.join(' '));
  55. }
  56. } else {
  57. // we provided a specific element
  58. $el = $(element);
  59. }
  60. // append it
  61. $this.append($el);
  62. // return it
  63. return $el;
  64. },
  65. soon: function(callback, when) {
  66. var context = this, wrapped_callback = function() {
  67. callback.apply(context);
  68. };
  69. setTimeout(wrapped_callback, when || 200);
  70. return context;
  71. },
  72. slideTo: function(speed) {
  73. var top = $(this).offset().top;
  74. $('body,html').animate({'scrollTop': top + 'px'}, speed);
  75. }
  76. });
  77.  
  78. $.extend({
  79. log: function() {
  80. if ($.data(document, 'debug.log') == true) {
  81. if (typeof window.console != 'undefined') {
  82. window.console.log.apply(window.console, arguments);
  83. } else if (typeof console != 'undefined') {
  84. console.log.apply(this, arguments);
  85. } else {
  86. // do nothing
  87. }
  88. }
  89. },
  90. // like prototype.js's bind()
  91. shove: function(fn, object) {
  92. return function() {
  93. return fn.apply(object, arguments);
  94. }
  95. },
  96.  
  97. // is val blank?
  98. blank: function(val) {
  99. return !val || val === '' || !val.match(/\w+/);
  100. },
  101.  
  102. timestamp: function() {
  103. return Math.floor(new Date().getTime()/1000);
  104. },
  105.  
  106. togglePush: function(first, second) {
  107. var index = $.inArray(second, first);
  108. index != -1 ?
  109. first.splice(index, 1) :
  110. first.push(second)
  111. return first;
  112. },
  113.  
  114. // backport param from trunk
  115. param: function( a ) {
  116. var s = [],
  117. param_traditional = jQuery.param.traditional;
  118.  
  119. function add( key, value ){
  120. // If value is a function, invoke it and return its value
  121. value = jQuery.isFunction(value) ? value() : value;
  122. s[ s.length ] = encodeURIComponent(key) + '=' + encodeURIComponent(value);
  123. }
  124.  
  125. // If an array was passed in, assume that it is an array
  126. // of form elements
  127. if ( jQuery.isArray(a) || a.jquery )
  128. // Serialize the form elements
  129. jQuery.each( a, function() {
  130. add( this.name, this.value );
  131. });
  132.  
  133. else
  134. // Encode parameters from object, recursively. If
  135. // jQuery.param.traditional is set, encode the "old" way
  136. // (the way 1.3.2 or older did it)
  137. jQuery.each( a, function buildParams( prefix, obj ) {
  138.  
  139. if ( jQuery.isArray(obj) )
  140. jQuery.each( obj, function(i,v){
  141. // Due to rails' limited request param syntax, numeric array
  142. // indices are not supported. To avoid serialization ambiguity
  143. // issues, serialized arrays can only contain scalar values. php
  144. // does not have this issue, but we should go with the lowest
  145. // common denominator
  146. add( prefix + ( param_traditional ? "" : "[]" ), v );
  147. });
  148.  
  149. else if ( typeof obj == "object" )
  150. if ( param_traditional )
  151. add( prefix, obj );
  152.  
  153. else
  154. jQuery.each( obj, function(k,v){
  155. buildParams( prefix ? prefix + "[" + k + "]" : k, v );
  156. });
  157.  
  158. else
  159. add( prefix, obj );
  160.  
  161. });
  162.  
  163. // Return the resulting serialization
  164. return s.join("&").replace('%20', "+");
  165. }
  166. });
  167.  
  168. })(jQuery);
Add Comment
Please, Sign In to add comment