1. //(c) 2006 Nikhil Marathe (http://22bits.exofire.net). GNU GPL license.
  2. //@version 0.1
  3. /**
  4. * Adds a method to easily add properties to an objects prototype.
  5. * To extend Class with properties of AnotherClass just do
  6. * <code>Object.extend(Class.prototype, AnotherClass)<code>
  7. */
  8. Object.extend = function(dest, src) {
  9. //alert("Copying properties of " + src + " to " + dest);
  10. for(property in src)
  11. dest[property] = src[property];
  12. return dest;
  13. }
  14. Class = {
  15. create:function() {
  16. return function() {
  17. this.initialize.apply(this, arguments);
  18. }
  19. }
  20. }
  21. /**
  22. * Binds the function to the passed object.
  23. * @param {Object} The object to bind to
  24. */
  25. Function.prototype.bind = function(obj) {
  26. var __method = this;
  27. return function() {
  28. __method.apply(obj, arguments);
  29. }
  30. }
  31. function debug(data) {
  32. alert(data);
  33. }
  34. /***********************************
  35. * INBUILT OBJECT EXTENSIONS *
  36. **********************************/
  37. /**
  38. * Adds certain extra features to arrays
  39. * @version 0.1
  40. */
  41. _ArrayExtension = {
  42. /**
  43. * Calls a specified function for each element in the array.
  44. * The function is passed the current array element as the first argument.
  45. * Example:
  46. * <code>[1, 2, 3, 4, 5].each(function(num) { sqr = num * num; });<code>
  47. * @param {Function} The function to call for each element.
  48. */
  49. each:function(func) {
  50. for(var i = 0; i < this.length;i++)
  51. func(this[i]);
  52. },
  53. /**
  54. * @returns the last array element
  55. */
  56. last:function() { return this[this.length - 1]; },
  57. /**
  58. * Checks if a value is in the array.
  59. * Returns the position if found, otherwise -1.
  60. * <code>[1, 2, 3].indexOf(2) -> 1<code>
  61. * @param {Object} The object to check.
  62. * @returns the index if found or -1.
  63. */
  64. indexOf: function(object) {
  65. for(var i = 0; i < this.length; i++)
  66. if(object == this[i]) return i;
  67. return -1;
  68. }
  69. }
  70. Object.extend(Array.prototype, _ArrayExtension);
  71. /**
  72. * Extends Number.
  73. * @version 0.1
  74. */
  75. _NumExtension = {
  76. /**
  77. * Adds ruby style looping simplicity.
  78. * To do something <i>n<i> times do.
  79. * <code>(5).times(function() { doSomething; })<code>
  80. * The function is also passed the ith element.
  81. * @params {Function} The function to be called each time
  82. */
  83. times:function(func) {
  84. if(this <= 0) return;
  85. for(var i = 1; i <= this; i++)
  86. func();
  87. },
  88. /**
  89. * Returns a string representation of the number for use in colours.
  90. */
  91. toColourPart:function() {
  92. if(this<=255)return this.toHex();
  93. },
  94. /**
  95. * Returns the value of the number converted to hexadecimal
  96. * @returns A string.
  97. */
  98. toHex:function() {
  99. var hexDigits = ['0', '1', '2', '3', '4', '5', '6', '7', '8'
  100. , '9', 'a', 'b', 'c', 'd', 'e', 'e'];
  101. var hex_r = this % 16;
  102. var hex_q = (this-hex_r)/16;
  103. return hexDigits[hex_q] + hexDigits[hex_r];
  104. }
  105. }
  106. Object.extend(Number.prototype, _NumExtension)
  107. Object.extend(String.prototype, {
  108. /**
  109. * Use to convert CSS '-' seperated properties to the camelCased javascript version.
  110. * @returns The camelCased string
  111. */
  112. camelize:function() {
  113. var ar = this.split('-');
  114. var ret = ar[0];
  115. for(var i = 1; i < ar.length; i++)
  116. ret+=ar[i].charAt(0).toUpperCase() + ar[i].substring(1);
  117. return ret;
  118. },
  119. _Dec:function() {
  120. var hexDigits = ['0', '1', '2', '3', '4', '5', '6', '7', '8'
  121. , '9', 'a', 'b', 'c', 'd', 'e', 'f'];
  122. var ret = 0;
  123. for(var i = 0; i < this.length; ++i) {
  124. ret = ret * 16 + hexDigits.indexOf(this.charAt(i).toLowerCase());
  125. }
  126. return ret;
  127. },
  128. /**
  129. * Returns the hexadecimal colour code from the String.
  130. * Use this to parse CSS colours retrieved using Element.getStyle
  131. * because it handles RGB and converts it to hex.
  132. * @returns A string representing the colour
  133. */
  134. HEX:function() {
  135. var ret = '#';
  136. if(this.substr(0, 4) == 'rgb(') {
  137. colours = this.RGB();
  138. [colours.r, colours.g, colours.b].each(function(col) { ret += col.toColourPart(); });
  139. }
  140. else if(this.charAt(0) == '#') {
  141. if(this.length == 4) {
  142. sub = this.substring(1);
  143. ret += sub + sub;
  144. }
  145. else
  146. ret = this;
  147. }
  148. return (ret == '#' ? null:ret);
  149. },
  150. /**
  151. * Returns an RGB object with members
  152. * r, g, and b
  153. */
  154. RGB:function() {
  155. var rgb = {
  156. r:0,
  157. g:0,
  158. b:0
  159. };
  160. if(this.substr(0, 4) == 'rgb(') {
  161. colours = this.substring(4, this.length-1).split(',');
  162. rgb.r = parseInt(colours[0]);
  163. rgb.g = parseInt(colours[1]);
  164. rgb.b = parseInt(colours[2]);
  165. }
  166. else if(this.charAt(0) == '#') {
  167. //get a valid hex string
  168. var hex = this.HEX();
  169. //dump the #
  170. hex = hex.substring(1);
  171. rgb.r = hex.substring(0, 2)._Dec();
  172. rgb.g = hex.substring(2, 4)._Dec();
  173. rgb.b = hex.substring(4)._Dec();
  174. }
  175. return rgb;
  176. }
  177. });
  178. /**
  179. * Converts an array type object to an array.
  180. * Eg. arguments array
  181. * @param {Object} The array like object
  182. * @returns An array
  183. */
  184. function $A(iterable) {
  185. var ret = [];
  186. for(var i = 0; i < iterable.length; i++)
  187. ret.push(iterable[i]);
  188. return ret;
  189. }