/public/javascripts/sources/jrails.js

https://github.com/dumas/score-keeper · JavaScript · 207 lines · 175 code · 6 blank · 26 comment · 57 complexity · a2f8ff74fcf5ce948ac91b82a57e2034 MD5 · raw file

  1. /*
  2. *
  3. * jRails form observer plugin
  4. * version 0.1
  5. * <aaron@ennerchi.com> | http://www.ennerchi.com
  6. *
  7. */
  8. (function($) {
  9. $.fn.extend({
  10. formElementObserver: function(frequency, callback){
  11. var field = $(this), lastValue = field.formVal();
  12. setInterval(function(){
  13. value = field.formVal();
  14. if (lastValue != value) callback.apply(this, [field[0], value]);
  15. lastValue = value;
  16. }, frequency * 1000);
  17. },
  18. formElementEventObserver: function(callback){
  19. var field = $(this), lastValue = field.formVal();
  20. event = (field.type == 'radio' || field.type == 'checkbox') ? 'click' : 'change';
  21. field.bind(event, function(){
  22. value = field.formVal();
  23. if (lastValue != value) callback.apply(this, [field[0], value]);
  24. lastValue = value;
  25. });
  26. },
  27. formObserver: function(frequency, callback){
  28. var form = $(this), lastValue = form.serialize();
  29. setInterval(function(){
  30. value = form.serialize();
  31. if (lastValue != value) callback.apply(this, [form[0], form.serialize()]);
  32. lastValue = value;
  33. }, frequency * 1000);
  34. },
  35. formEventObserver: function(callback){
  36. var form = $(this);
  37. $(form.elements).each(function(){
  38. field = $(this);
  39. field.attr('lastValue', field.formVal());
  40. event = (field.type == 'radio' || field.type == 'checkbox') ? 'click' : 'change';
  41. field.bind(event, function(){
  42. field = $(this);
  43. value = field.formVal();
  44. if (field.attr('lastValue') != value) callback.apply(this, [form[0], form.serialize()]);
  45. field.attr('lastValue', value);
  46. });
  47. });
  48. }
  49. });
  50. })(jQuery);
  51. //gets the value of the first matched form field, or sets
  52. //the value of all the matched form fields
  53. (function($) {
  54. $.fn.extend({
  55. formVal: function(newVal) {
  56. var self = this.get(0);
  57. var optVal = function(opt) {
  58. if(opt.value) return opt.value; // non-empty string, use it
  59. if(!opt.outerHTML) return ''; // not IE, we should trust value
  60. return /\svalue=""(?:\s|>)/.test(opt.outerHTML) ? '' : opt.text;
  61. };
  62. if(newVal == null) {
  63. if(this.size() < 1) return '';
  64. if(self.type == 'text') return self.value;
  65. if(self.tagName == 'TEXTAREA') return this.text();
  66. if(self.type == 'checkbox' || self.type == 'radio')
  67. return this.filter('input:checked').val() || '';
  68. if(self.tagName == 'OPTION') return this.parent().formVal();
  69. if(self.tagName == 'SELECT' && self.selectedIndex >= 0)
  70. return optVal(self.options[self.selectedIndex]);
  71. return '';
  72. }
  73. if(self.type == 'text') this.val(newVal);
  74. else if(self.tagName == 'TEXTAREA') this.text(newVal);
  75. else if(self.type == 'checkbox' || self.type == 'radio') {
  76. this.filter(':checked').removeAttr('checked');
  77. this.filter('[@value=' + newVal + ']').attr('checked', 'checked');
  78. }
  79. else if(self.tagName == 'OPTION') this.parent().formVal(newVal);
  80. else if(self.tagName == 'SELECT') {
  81. for (var i=0, l=self.options.length; i<l; ++i) {
  82. if(newVal == optVal(self.options[i])) {
  83. self.selectedIndex = i;
  84. break;
  85. }
  86. }
  87. }
  88. return this;
  89. }
  90. });
  91. })(jQuery);
  92. /*
  93. *
  94. * jRails visual effects stubs
  95. * version 0.1
  96. * <aaron@ennerchi.com> | http://www.ennerchi.com
  97. *
  98. */
  99. (function($) {
  100. $.fn.extend({
  101. Appear : function(speed, callback) {
  102. return this.fadeIn(speed, callback);
  103. },
  104. BlindDown : function(speed, callback) {
  105. this.show({ method: 'blind', direction: 'vertical' }, speed, callback);
  106. return this;
  107. },
  108. BlindUp : function(speed, callback) {
  109. this.hide({ method: 'blind', direction: 'vertical' }, speed, callback);
  110. return this;
  111. },
  112. BlindRight : function(speed, callback) {
  113. this.show({ method: 'blind', direction: 'horizontal' }, speed, callback);
  114. return this;
  115. },
  116. BlindLeft : function(speed, callback) {
  117. this.hide({ method: 'blind', direction: 'horizontal' }, speed, callback);
  118. return this;
  119. },
  120. DropOut : function(speed, callback) {
  121. this.hide({ method: 'drop', direction: 'down' }, speed, callback);
  122. return this;
  123. },
  124. DropIn : function(speed, callback) {
  125. this.show({ method: 'drop', direction: 'down' }, speed, callback);
  126. return this;
  127. },
  128. Fade : function(speed, callback) {
  129. return this.fadeOut(speed, callback);
  130. },
  131. Grow : function(speed, callback) {
  132. this.show({ method: 'scale' }, speed, callback);
  133. return this;
  134. },
  135. Highlight : function(speed, callback) {
  136. this.show({ method: 'highlight' }, speed, callback);
  137. return this;
  138. },
  139. Puff : function(speed, callback) {
  140. this.hide({ method: 'scale', mode: 'puff' }, speed, callback);
  141. return this;
  142. },
  143. Pulsate : function(speed, callback) {
  144. this.show({ method: 'pulsate' }, speed, callback);
  145. return this;
  146. },
  147. Shake : function(speed, callback) {
  148. this.show({ method: 'shake' }, speed, callback);
  149. return this;
  150. },
  151. Shrink : function(speed, callback) {
  152. this.hide({ method: 'scale' }, speed, callback);
  153. return this;
  154. },
  155. Squish : function(speed, callback) {
  156. this.hide({ method: 'scale', mode: 'squish' }, speed, callback);
  157. return this;
  158. },
  159. SlideUp : function(speed, callback) {
  160. this.hide({ method: 'slide', direction: 'up'}, speed, callback);
  161. return this;
  162. },
  163. SlideDown : function(speed, callback) {
  164. this.show({ method: 'slide', direction: 'down'}, speed, callback);
  165. return this;
  166. }
  167. });
  168. })(jQuery);
  169. /*
  170. * [ 0.2 ] Original by Jonathan Howard
  171. * [ 0.3 ] Updated for current jQuery releases,
  172. * and formatted for jQuery namespace by Charles Phillips
  173. * <charles@doublerebel.com> | http://www.doublerebel.com/scripts/jquery.pause.js
  174. *
  175. * jQuery Pause
  176. * version 0.3
  177. *
  178. */
  179. (function($) {
  180. $.fn.extend({
  181. pause: function(milli,type) {
  182. milli = milli || 1000;
  183. type = type || "fx";
  184. return this.queue(type,function(){
  185. var self = this;
  186. setTimeout(function(){
  187. $(self).dequeue();
  188. },milli);
  189. });
  190. },
  191. clearQueue: function(type) {
  192. return this.each(function(){
  193. type = type || "fx";
  194. if(this.queue && this.queue[type]) {
  195. this.queue[type].length = 0;
  196. }
  197. });
  198. },
  199. unpause: $.fn.clearQueue
  200. });
  201. })(jQuery);