PageRenderTime 46ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 1ms

/files/highcharts/2.3.2/adapters/mootools-adapter.src.js

https://gitlab.com/Mirros/jsdelivr
JavaScript | 327 lines | 186 code | 44 blank | 97 comment | 34 complexity | 9c8ff54e8fc0fb376f9ffaf75cd151b5 MD5 | raw file
  1. /**
  2. * @license Highcharts JS v2.3.2 (2012-08-31)
  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. 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. el.attr.call(el, 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.indexOf(item, from);
  164. },
  165. /**
  166. * Deep merge two objects and return a third
  167. */
  168. merge: function () {
  169. var args = arguments,
  170. args13 = [{}], // MooTools 1.3+
  171. i = args.length,
  172. ret;
  173. if (legacy) {
  174. ret = $merge.apply(null, args);
  175. } else {
  176. while (i--) {
  177. // Boolean argumens should not be merged.
  178. // JQuery explicitly skips this, so we do it here as well.
  179. if (typeof args[i] !== 'boolean') {
  180. args13[i + 1] = args[i];
  181. }
  182. }
  183. ret = Object.merge.apply(Object, args13);
  184. }
  185. return ret;
  186. },
  187. /**
  188. * Get the offset of an element relative to the top left corner of the web page
  189. */
  190. offset: function (el) {
  191. var offsets = $(el).getOffsets();
  192. return {
  193. left: offsets.x,
  194. top: offsets.y
  195. };
  196. },
  197. /**
  198. * Extends an object with Events, if its not done
  199. */
  200. extendWithEvents: function (el) {
  201. // if the addEvent method is not defined, el is a custom Highcharts object
  202. // like series or point
  203. if (!el.addEvent) {
  204. if (el.nodeName) {
  205. el = $(el); // a dynamically generated node
  206. } else {
  207. $extend(el, new Events()); // a custom object
  208. }
  209. }
  210. },
  211. /**
  212. * Add an event listener
  213. * @param {Object} el HTML element or custom object
  214. * @param {String} type Event type
  215. * @param {Function} fn Event handler
  216. */
  217. addEvent: function (el, type, fn) {
  218. if (typeof type === 'string') { // chart broke due to el being string, type function
  219. if (type === 'unload') { // Moo self destructs before custom unload events
  220. type = 'beforeunload';
  221. }
  222. win.HighchartsAdapter.extendWithEvents(el);
  223. el.addEvent(type, fn);
  224. }
  225. },
  226. removeEvent: function (el, type, fn) {
  227. if (typeof el === 'string') {
  228. // el.removeEvents below apperantly calls this method again. Do not quite understand why, so for now just bail out.
  229. return;
  230. }
  231. win.HighchartsAdapter.extendWithEvents(el);
  232. if (type) {
  233. if (type === 'unload') { // Moo self destructs before custom unload events
  234. type = 'beforeunload';
  235. }
  236. if (fn) {
  237. el.removeEvent(type, fn);
  238. } else if (el.removeEvents) { // #958
  239. el.removeEvents(type);
  240. }
  241. } else {
  242. el.removeEvents();
  243. }
  244. },
  245. fireEvent: function (el, event, eventArguments, defaultFunction) {
  246. var eventArgs = {
  247. type: event,
  248. target: el
  249. };
  250. // create an event object that keeps all functions
  251. event = legacyEvent ? new Event(eventArgs) : new DOMEvent(eventArgs);
  252. event = $extend(event, eventArguments);
  253. // override the preventDefault function to be able to use
  254. // this for custom events
  255. event.preventDefault = function () {
  256. defaultFunction = null;
  257. };
  258. // if fireEvent is not available on the object, there hasn't been added
  259. // any events to it above
  260. if (el.fireEvent) {
  261. el.fireEvent(event.type, event);
  262. }
  263. // fire the default if it is passed and it is not prevented above
  264. if (defaultFunction) {
  265. defaultFunction(event);
  266. }
  267. },
  268. /**
  269. * Set back e.pageX and e.pageY that MooTools has abstracted away
  270. */
  271. washMouseEvent: function (e) {
  272. return e.event || e;
  273. },
  274. /**
  275. * Stop running animations on the object
  276. */
  277. stop: function (el) {
  278. if (el.fx) {
  279. el.fx.cancel();
  280. }
  281. }
  282. };
  283. }());