mootools-adapter.src.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. /**
  2. * @license Highcharts JS v3.0.6 (2013-10-04)
  3. * MooTools adapter
  4. *
  5. * (c) 2010-2013 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. if (method === 'width' || method === 'height') {
  71. return parseInt($(el).getStyle(method), 10);
  72. }
  73. },
  74. /**
  75. * Downloads a script and executes a callback when done.
  76. * @param {String} scriptLocation
  77. * @param {Function} callback
  78. */
  79. getScript: function (scriptLocation, callback) {
  80. // We cannot assume that Assets class from mootools-more is available so instead insert a script tag to download script.
  81. var head = doc.getElementsByTagName('head')[0];
  82. var script = doc.createElement('script');
  83. script.type = 'text/javascript';
  84. script.src = scriptLocation;
  85. script.onload = callback;
  86. head.appendChild(script);
  87. },
  88. /**
  89. * Animate a HTML element or SVG element wrapper
  90. * @param {Object} el
  91. * @param {Object} params
  92. * @param {Object} options jQuery-like animation options: duration, easing, callback
  93. */
  94. animate: function (el, params, options) {
  95. var isSVGElement = el.attr,
  96. effect,
  97. complete = options && options.complete;
  98. if (isSVGElement && !el.setStyle) {
  99. // add setStyle and getStyle methods for internal use in Moo
  100. el.getStyle = el.attr;
  101. el.setStyle = function () { // property value is given as array in Moo - break it down
  102. var args = arguments;
  103. this.attr.call(this, args[0], args[1][0]);
  104. };
  105. // dirty hack to trick Moo into handling el as an element wrapper
  106. el.$family = function () { return true; };
  107. }
  108. // stop running animations
  109. win.HighchartsAdapter.stop(el);
  110. // define and run the effect
  111. effect = new Fx.Morph(
  112. isSVGElement ? el : $(el),
  113. $extend({
  114. transition: Fx.Transitions.Quad.easeInOut
  115. }, options)
  116. );
  117. // Make sure that the element reference is set when animating svg elements
  118. if (isSVGElement) {
  119. effect.element = el;
  120. }
  121. // special treatment for paths
  122. if (params.d) {
  123. effect.toD = params.d;
  124. }
  125. // jQuery-like events
  126. if (complete) {
  127. effect.addEvent('complete', complete);
  128. }
  129. // run
  130. effect.start(params);
  131. // record for use in stop method
  132. el.fx = effect;
  133. },
  134. /**
  135. * MooTool's each function
  136. *
  137. */
  138. each: function (arr, fn) {
  139. return legacy ?
  140. $each(arr, fn) :
  141. Array.each(arr, fn);
  142. },
  143. /**
  144. * Map an array
  145. * @param {Array} arr
  146. * @param {Function} fn
  147. */
  148. map: function (arr, fn) {
  149. return arr.map(fn);
  150. },
  151. /**
  152. * Grep or filter an array
  153. * @param {Array} arr
  154. * @param {Function} fn
  155. */
  156. grep: function (arr, fn) {
  157. return arr.filter(fn);
  158. },
  159. /**
  160. * Return the index of an item in an array, or -1 if not matched
  161. */
  162. inArray: function (item, arr, from) {
  163. return arr ? arr.indexOf(item, from) : -1;
  164. },
  165. /**
  166. * Get the offset of an element relative to the top left corner of the web page
  167. */
  168. offset: function (el) {
  169. var offsets = el.getPosition(); // #1496
  170. return {
  171. left: offsets.x,
  172. top: offsets.y
  173. };
  174. },
  175. /**
  176. * Extends an object with Events, if its not done
  177. */
  178. extendWithEvents: function (el) {
  179. // if the addEvent method is not defined, el is a custom Highcharts object
  180. // like series or point
  181. if (!el.addEvent) {
  182. if (el.nodeName) {
  183. el = $(el); // a dynamically generated node
  184. } else {
  185. $extend(el, new Events()); // a custom object
  186. }
  187. }
  188. },
  189. /**
  190. * Add an event listener
  191. * @param {Object} el HTML element or custom object
  192. * @param {String} type Event type
  193. * @param {Function} fn Event handler
  194. */
  195. addEvent: function (el, type, fn) {
  196. if (typeof type === 'string') { // chart broke due to el being string, type function
  197. if (type === 'unload') { // Moo self destructs before custom unload events
  198. type = 'beforeunload';
  199. }
  200. win.HighchartsAdapter.extendWithEvents(el);
  201. el.addEvent(type, fn);
  202. }
  203. },
  204. removeEvent: function (el, type, fn) {
  205. if (typeof el === 'string') {
  206. // el.removeEvents below apperantly calls this method again. Do not quite understand why, so for now just bail out.
  207. return;
  208. }
  209. if (el.addEvent) { // If el doesn't have an addEvent method, there are no events to remove
  210. if (type) {
  211. if (type === 'unload') { // Moo self destructs before custom unload events
  212. type = 'beforeunload';
  213. }
  214. if (fn) {
  215. el.removeEvent(type, fn);
  216. } else if (el.removeEvents) { // #958
  217. el.removeEvents(type);
  218. }
  219. } else {
  220. el.removeEvents();
  221. }
  222. }
  223. },
  224. fireEvent: function (el, event, eventArguments, defaultFunction) {
  225. var eventArgs = {
  226. type: event,
  227. target: el
  228. };
  229. // create an event object that keeps all functions
  230. event = legacyEvent ? new Event(eventArgs) : new DOMEvent(eventArgs);
  231. event = $extend(event, eventArguments);
  232. // When running an event on the Chart.prototype, MooTools nests the target in event.event
  233. if (!event.target && event.event) {
  234. event.target = event.event.target;
  235. }
  236. // override the preventDefault function to be able to use
  237. // this for custom events
  238. event.preventDefault = function () {
  239. defaultFunction = null;
  240. };
  241. // if fireEvent is not available on the object, there hasn't been added
  242. // any events to it above
  243. if (el.fireEvent) {
  244. el.fireEvent(event.type, event);
  245. }
  246. // fire the default if it is passed and it is not prevented above
  247. if (defaultFunction) {
  248. defaultFunction(event);
  249. }
  250. },
  251. /**
  252. * Set back e.pageX and e.pageY that MooTools has abstracted away. #1165, #1346.
  253. */
  254. washMouseEvent: function (e) {
  255. if (e.page) {
  256. e.pageX = e.page.x;
  257. e.pageY = e.page.y;
  258. }
  259. return e;
  260. },
  261. /**
  262. * Stop running animations on the object
  263. */
  264. stop: function (el) {
  265. if (el.fx) {
  266. el.fx.cancel();
  267. }
  268. }
  269. };
  270. }());