mootools-adapter.src.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. /**
  2. * @license Highcharts JS v2.1.4 (2011-03-30)
  3. * MooTools adapter
  4. *
  5. * (c) 2010 Torstein Hønsi
  6. *
  7. * License: www.highcharts.com/license
  8. */
  9. // JSLint options:
  10. /*global Highcharts, Fx, $, $extend, $each, $merge, Events, Event */
  11. (function() {
  12. var win = window,
  13. legacy = !!win.$merge
  14. $extend = win.$extend || function() {
  15. return Object.append.apply(Object, arguments)
  16. };
  17. win.HighchartsAdapter = {
  18. /**
  19. * Initialize the adapter. This is run once as Highcharts is first run.
  20. */
  21. init: function() {
  22. var fxProto = Fx.prototype,
  23. fxStart = fxProto.start,
  24. morphProto = Fx.Morph.prototype,
  25. morphCompute = morphProto.compute;
  26. // override Fx.start to allow animation of SVG element wrappers
  27. fxProto.start = function(from, to) {
  28. var fx = this,
  29. elem = fx.element;
  30. // special for animating paths
  31. if (from.d) {
  32. //this.fromD = this.element.d.split(' ');
  33. fx.paths = Highcharts.pathAnim.init(
  34. elem,
  35. elem.d,
  36. fx.toD
  37. );
  38. }
  39. fxStart.apply(fx, arguments);
  40. };
  41. // override Fx.step to allow animation of SVG element wrappers
  42. morphProto.compute = function(from, to, delta) {
  43. var fx = this,
  44. paths = fx.paths;
  45. if (paths) {
  46. fx.element.attr(
  47. 'd',
  48. Highcharts.pathAnim.step(paths[0], paths[1], delta, fx.toD)
  49. );
  50. } else {
  51. return morphCompute.apply(fx, arguments);
  52. }
  53. };
  54. },
  55. /**
  56. * Animate a HTML element or SVG element wrapper
  57. * @param {Object} el
  58. * @param {Object} params
  59. * @param {Object} options jQuery-like animation options: duration, easing, callback
  60. */
  61. animate: function (el, params, options) {
  62. var isSVGElement = el.attr,
  63. effect,
  64. complete = options && options.complete;
  65. if (isSVGElement && !el.setStyle) {
  66. // add setStyle and getStyle methods for internal use in Moo
  67. el.getStyle = el.attr;
  68. el.setStyle = function() { // property value is given as array in Moo - break it down
  69. var args = arguments;
  70. el.attr.call(el, args[0], args[1][0]);
  71. }
  72. // dirty hack to trick Moo into handling el as an element wrapper
  73. el.$family = el.uid = true;
  74. }
  75. // stop running animations
  76. HighchartsAdapter.stop(el);
  77. // define and run the effect
  78. effect = new Fx.Morph(
  79. isSVGElement ? el : $(el),
  80. $extend({
  81. transition: Fx.Transitions.Quad.easeInOut
  82. }, options)
  83. );
  84. // special treatment for paths
  85. if (params.d) {
  86. effect.toD = params.d;
  87. }
  88. // jQuery-like events
  89. if (complete) {
  90. effect.addEvent('complete', complete);
  91. }
  92. // run
  93. effect.start(params);
  94. // record for use in stop method
  95. el.fx = effect;
  96. },
  97. /**
  98. * MooTool's each function
  99. *
  100. */
  101. each: function(arr, fn) {
  102. return legacy ?
  103. $each(arr, fn) :
  104. arr.each(fn);
  105. },
  106. /**
  107. * Map an array
  108. * @param {Array} arr
  109. * @param {Function} fn
  110. */
  111. map: function (arr, fn){
  112. return arr.map(fn);
  113. },
  114. /**
  115. * Grep or filter an array
  116. * @param {Array} arr
  117. * @param {Function} fn
  118. */
  119. grep: function(arr, fn) {
  120. return arr.filter(fn);
  121. },
  122. /**
  123. * Deep merge two objects and return a third
  124. */
  125. merge: function() {
  126. var args = arguments,
  127. args13 = [{}], // MooTools 1.3+
  128. i = args.length,
  129. ret;
  130. if (legacy) {
  131. ret = $merge.apply(null, args);
  132. } else {
  133. while (i--) {
  134. args13[i + 1] = args[i];
  135. }
  136. ret = Object.merge.apply(Object, args13);
  137. }
  138. return ret;
  139. },
  140. /**
  141. * Hyphenate a string, like minWidth becomes min-width
  142. * @param {Object} str
  143. */
  144. hyphenate: function (str){
  145. return str.hyphenate();
  146. },
  147. /**
  148. * Add an event listener
  149. * @param {Object} el HTML element or custom object
  150. * @param {String} type Event type
  151. * @param {Function} fn Event handler
  152. */
  153. addEvent: function (el, type, fn) {
  154. if (typeof type == 'string') { // chart broke due to el being string, type function
  155. if (type == 'unload') { // Moo self destructs before custom unload events
  156. type = 'beforeunload';
  157. }
  158. // if the addEvent method is not defined, el is a custom Highcharts object
  159. // like series or point
  160. if (!el.addEvent) {
  161. if (el.nodeName) {
  162. el = $(el); // a dynamically generated node
  163. } else {
  164. $extend(el, new Events()); // a custom object
  165. }
  166. }
  167. el.addEvent(type, fn);
  168. }
  169. },
  170. removeEvent: function(el, type, fn) {
  171. if (type) {
  172. if (type == 'unload') { // Moo self destructs before custom unload events
  173. type = 'beforeunload';
  174. }
  175. el.removeEvent(type, fn);
  176. }
  177. },
  178. fireEvent: function(el, event, eventArguments, defaultFunction) {
  179. // create an event object that keeps all functions
  180. event = new Event({
  181. type: event,
  182. target: el
  183. });
  184. event = $extend(event, eventArguments);
  185. // override the preventDefault function to be able to use
  186. // this for custom events
  187. event.preventDefault = function() {
  188. defaultFunction = null;
  189. };
  190. // if fireEvent is not available on the object, there hasn't been added
  191. // any events to it above
  192. if (el.fireEvent) {
  193. el.fireEvent(event.type, event);
  194. }
  195. // fire the default if it is passed and it is not prevented above
  196. if (defaultFunction) {
  197. defaultFunction(event);
  198. }
  199. },
  200. /**
  201. * Stop running animations on the object
  202. */
  203. stop: function (el) {
  204. if (el.fx) {
  205. el.fx.cancel();
  206. }
  207. }
  208. }
  209. })();