mootools-adapter.src.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. /**
  2. * @license Highcharts JS v2.2.5 (2012-06-08)
  3. * MooTools adapter
  4. *
  5. * (c) 2010-2011 Torstein Hønsi
  6. *
  7. * License: www.highcharts.com/license
  8. */
  9. // JSLint options:
  10. /*global Fx, $, $extend, $each, $merge, Events, Event, DOMEvent */
  11. (function () {
  12. var win = window,
  13. doc = document,
  14. mooVersion = win.MooTools.version.substring(0, 3), // Get the first three characters of the version number
  15. legacy = mooVersion === '1.2' || mooVersion === '1.1', // 1.1 && 1.2 considered legacy, 1.3 is not.
  16. legacyEvent = legacy || mooVersion === '1.3', // In versions 1.1 - 1.3 the event class is named Event, in newer versions it is named DOMEvent.
  17. $extend = win.$extend || function () {
  18. return Object.append.apply(Object, arguments);
  19. };
  20. win.HighchartsAdapter = {
  21. /**
  22. * Initialize the adapter. This is run once as Highcharts is first run.
  23. * @param {Object} pathAnim The helper object to do animations across adapters.
  24. */
  25. init: function (pathAnim) {
  26. var fxProto = Fx.prototype,
  27. fxStart = fxProto.start,
  28. morphProto = Fx.Morph.prototype,
  29. morphCompute = morphProto.compute;
  30. // override Fx.start to allow animation of SVG element wrappers
  31. /*jslint unparam: true*//* allow unused parameters in fx functions */
  32. fxProto.start = function (from, to) {
  33. var fx = this,
  34. elem = fx.element;
  35. // special for animating paths
  36. if (from.d) {
  37. //this.fromD = this.element.d.split(' ');
  38. fx.paths = pathAnim.init(
  39. elem,
  40. elem.d,
  41. fx.toD
  42. );
  43. }
  44. fxStart.apply(fx, arguments);
  45. return this; // chainable
  46. };
  47. // override Fx.step to allow animation of SVG element wrappers
  48. morphProto.compute = function (from, to, delta) {
  49. var fx = this,
  50. paths = fx.paths;
  51. if (paths) {
  52. fx.element.attr(
  53. 'd',
  54. pathAnim.step(paths[0], paths[1], delta, fx.toD)
  55. );
  56. } else {
  57. return morphCompute.apply(fx, arguments);
  58. }
  59. };
  60. /*jslint unparam: false*/
  61. },
  62. /**
  63. * Run a general method on the framework, following jQuery syntax
  64. * @param {Object} el The HTML element
  65. * @param {String} method Which method to run on the wrapped element
  66. */
  67. adapterRun: function (el, method) {
  68. // This currently works for getting inner width and height. If adding
  69. // more methods later, we need a conditional implementation for each.
  70. return $(el).getStyle(method).toInt();
  71. },
  72. /**
  73. * Downloads a script and executes a callback when done.
  74. * @param {String} scriptLocation
  75. * @param {Function} callback
  76. */
  77. getScript: function (scriptLocation, callback) {
  78. // We cannot assume that Assets class from mootools-more is available so instead insert a script tag to download script.
  79. var head = doc.getElementsByTagName('head')[0];
  80. var script = doc.createElement('script');
  81. script.type = 'text/javascript';
  82. script.src = scriptLocation;
  83. script.onload = callback;
  84. head.appendChild(script);
  85. },
  86. /**
  87. * Animate a HTML element or SVG element wrapper
  88. * @param {Object} el
  89. * @param {Object} params
  90. * @param {Object} options jQuery-like animation options: duration, easing, callback
  91. */
  92. animate: function (el, params, options) {
  93. var isSVGElement = el.attr,
  94. effect,
  95. complete = options && options.complete;
  96. if (isSVGElement && !el.setStyle) {
  97. // add setStyle and getStyle methods for internal use in Moo
  98. el.getStyle = el.attr;
  99. el.setStyle = function () { // property value is given as array in Moo - break it down
  100. var args = arguments;
  101. el.attr.call(el, args[0], args[1][0]);
  102. };
  103. // dirty hack to trick Moo into handling el as an element wrapper
  104. el.$family = function () { return true; };
  105. }
  106. // stop running animations
  107. win.HighchartsAdapter.stop(el);
  108. // define and run the effect
  109. effect = new Fx.Morph(
  110. isSVGElement ? el : $(el),
  111. $extend({
  112. transition: Fx.Transitions.Quad.easeInOut
  113. }, options)
  114. );
  115. // Make sure that the element reference is set when animating svg elements
  116. if (isSVGElement) {
  117. effect.element = el;
  118. }
  119. // special treatment for paths
  120. if (params.d) {
  121. effect.toD = params.d;
  122. }
  123. // jQuery-like events
  124. if (complete) {
  125. effect.addEvent('complete', complete);
  126. }
  127. // run
  128. effect.start(params);
  129. // record for use in stop method
  130. el.fx = effect;
  131. },
  132. /**
  133. * MooTool's each function
  134. *
  135. */
  136. each: function (arr, fn) {
  137. return legacy ?
  138. $each(arr, fn) :
  139. Array.each(arr, fn);
  140. },
  141. /**
  142. * Map an array
  143. * @param {Array} arr
  144. * @param {Function} fn
  145. */
  146. map: function (arr, fn) {
  147. return arr.map(fn);
  148. },
  149. /**
  150. * Grep or filter an array
  151. * @param {Array} arr
  152. * @param {Function} fn
  153. */
  154. grep: function (arr, fn) {
  155. return arr.filter(fn);
  156. },
  157. /**
  158. * Deep merge two objects and return a third
  159. */
  160. merge: function () {
  161. var args = arguments,
  162. args13 = [{}], // MooTools 1.3+
  163. i = args.length,
  164. ret;
  165. if (legacy) {
  166. ret = $merge.apply(null, args);
  167. } else {
  168. while (i--) {
  169. // Boolean argumens should not be merged.
  170. // JQuery explicitly skips this, so we do it here as well.
  171. if (typeof args[i] !== 'boolean') {
  172. args13[i + 1] = args[i];
  173. }
  174. }
  175. ret = Object.merge.apply(Object, args13);
  176. }
  177. return ret;
  178. },
  179. /**
  180. * Get the offset of an element relative to the top left corner of the web page
  181. */
  182. offset: function (el) {
  183. var offsets = $(el).getOffsets();
  184. return {
  185. left: offsets.x,
  186. top: offsets.y
  187. };
  188. },
  189. /**
  190. * Extends an object with Events, if its not done
  191. */
  192. extendWithEvents: function (el) {
  193. // if the addEvent method is not defined, el is a custom Highcharts object
  194. // like series or point
  195. if (!el.addEvent) {
  196. if (el.nodeName) {
  197. el = $(el); // a dynamically generated node
  198. } else {
  199. $extend(el, new Events()); // a custom object
  200. }
  201. }
  202. },
  203. /**
  204. * Add an event listener
  205. * @param {Object} el HTML element or custom object
  206. * @param {String} type Event type
  207. * @param {Function} fn Event handler
  208. */
  209. addEvent: function (el, type, fn) {
  210. if (typeof type === 'string') { // chart broke due to el being string, type function
  211. if (type === 'unload') { // Moo self destructs before custom unload events
  212. type = 'beforeunload';
  213. }
  214. win.HighchartsAdapter.extendWithEvents(el);
  215. el.addEvent(type, fn);
  216. }
  217. },
  218. removeEvent: function (el, type, fn) {
  219. if (typeof el === 'string') {
  220. // el.removeEvents below apperantly calls this method again. Do not quite understand why, so for now just bail out.
  221. return;
  222. }
  223. win.HighchartsAdapter.extendWithEvents(el);
  224. if (type) {
  225. if (type === 'unload') { // Moo self destructs before custom unload events
  226. type = 'beforeunload';
  227. }
  228. if (fn) {
  229. el.removeEvent(type, fn);
  230. } else if (el.removeEvents) { // #958
  231. el.removeEvents(type);
  232. }
  233. } else {
  234. el.removeEvents();
  235. }
  236. },
  237. fireEvent: function (el, event, eventArguments, defaultFunction) {
  238. var eventArgs = {
  239. type: event,
  240. target: el
  241. };
  242. // create an event object that keeps all functions
  243. event = legacyEvent ? new Event(eventArgs) : new DOMEvent(eventArgs);
  244. event = $extend(event, eventArguments);
  245. // override the preventDefault function to be able to use
  246. // this for custom events
  247. event.preventDefault = function () {
  248. defaultFunction = null;
  249. };
  250. // if fireEvent is not available on the object, there hasn't been added
  251. // any events to it above
  252. if (el.fireEvent) {
  253. el.fireEvent(event.type, event);
  254. }
  255. // fire the default if it is passed and it is not prevented above
  256. if (defaultFunction) {
  257. defaultFunction(event);
  258. }
  259. },
  260. /**
  261. * Set back e.pageX and e.pageY that MooTools has abstracted away
  262. */
  263. washMouseEvent: function (e) {
  264. e.pageX = e.page.x;
  265. e.pageY = e.page.y;
  266. return e;
  267. },
  268. /**
  269. * Stop running animations on the object
  270. */
  271. stop: function (el) {
  272. if (el.fx) {
  273. el.fx.cancel();
  274. }
  275. }
  276. };
  277. }());