纽威
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

316 lines
7.7 KiB

3 years ago
  1. /**
  2. * @license Highcharts JS v3.0.10 (2014-03-10)
  3. * MooTools adapter
  4. *
  5. * (c) 2010-2014 Torstein Honsi
  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(document.id(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. el.getComputedStyle = function () {
  108. return el.element.getComputedStyle.apply(el.element, arguments);
  109. };
  110. }
  111. // stop running animations
  112. win.HighchartsAdapter.stop(el);
  113. // define and run the effect
  114. effect = new Fx.Morph(
  115. isSVGElement ? el : document.id(el),
  116. $extend({
  117. transition: Fx.Transitions.Quad.easeInOut
  118. }, options)
  119. );
  120. // Make sure that the element reference is set when animating svg elements
  121. if (isSVGElement) {
  122. effect.element = el;
  123. }
  124. // special treatment for paths
  125. if (params.d) {
  126. effect.toD = params.d;
  127. }
  128. // jQuery-like events
  129. if (complete) {
  130. effect.addEvent('complete', complete);
  131. }
  132. // run
  133. effect.start(params);
  134. // record for use in stop method
  135. el.fx = effect;
  136. },
  137. /**
  138. * MooTool's each function
  139. *
  140. */
  141. each: function (arr, fn) {
  142. return legacy ?
  143. $each(arr, fn) :
  144. Array.each(arr, fn);
  145. },
  146. /**
  147. * Map an array
  148. * @param {Array} arr
  149. * @param {Function} fn
  150. */
  151. map: function (arr, fn) {
  152. return arr.map(fn);
  153. },
  154. /**
  155. * Grep or filter an array
  156. * @param {Array} arr
  157. * @param {Function} fn
  158. */
  159. grep: function (arr, fn) {
  160. return arr.filter(fn);
  161. },
  162. /**
  163. * Return the index of an item in an array, or -1 if not matched
  164. */
  165. inArray: function (item, arr, from) {
  166. return arr ? arr.indexOf(item, from) : -1;
  167. },
  168. /**
  169. * Get the offset of an element relative to the top left corner of the web page
  170. */
  171. offset: function (el) {
  172. var offsets = el.getPosition(); // #1496
  173. return {
  174. left: offsets.x,
  175. top: offsets.y
  176. };
  177. },
  178. /**
  179. * Extends an object with Events, if its not done
  180. */
  181. extendWithEvents: function (el) {
  182. // if the addEvent method is not defined, el is a custom Highcharts object
  183. // like series or point
  184. if (!el.addEvent) {
  185. if (el.nodeName) {
  186. el = document.id(el); // a dynamically generated node
  187. } else {
  188. $extend(el, new Events()); // a custom object
  189. }
  190. }
  191. },
  192. /**
  193. * Add an event listener
  194. * @param {Object} el HTML element or custom object
  195. * @param {String} type Event type
  196. * @param {Function} fn Event handler
  197. */
  198. addEvent: function (el, type, fn) {
  199. if (typeof type === 'string') { // chart broke due to el being string, type function
  200. if (type === 'unload') { // Moo self destructs before custom unload events
  201. type = 'beforeunload';
  202. }
  203. win.HighchartsAdapter.extendWithEvents(el);
  204. el.addEvent(type, fn);
  205. }
  206. },
  207. removeEvent: function (el, type, fn) {
  208. if (typeof el === 'string') {
  209. // el.removeEvents below apperantly calls this method again. Do not quite understand why, so for now just bail out.
  210. return;
  211. }
  212. if (el.addEvent) { // If el doesn't have an addEvent method, there are no events to remove
  213. if (type) {
  214. if (type === 'unload') { // Moo self destructs before custom unload events
  215. type = 'beforeunload';
  216. }
  217. if (fn) {
  218. el.removeEvent(type, fn);
  219. } else if (el.removeEvents) { // #958
  220. el.removeEvents(type);
  221. }
  222. } else {
  223. el.removeEvents();
  224. }
  225. }
  226. },
  227. fireEvent: function (el, event, eventArguments, defaultFunction) {
  228. var eventArgs = {
  229. type: event,
  230. target: el
  231. };
  232. // create an event object that keeps all functions
  233. event = legacyEvent ? new Event(eventArgs) : new DOMEvent(eventArgs);
  234. event = $extend(event, eventArguments);
  235. // When running an event on the Chart.prototype, MooTools nests the target in event.event
  236. if (!event.target && event.event) {
  237. event.target = event.event.target;
  238. }
  239. // override the preventDefault function to be able to use
  240. // this for custom events
  241. event.preventDefault = function () {
  242. defaultFunction = null;
  243. };
  244. // if fireEvent is not available on the object, there hasn't been added
  245. // any events to it above
  246. if (el.fireEvent) {
  247. el.fireEvent(event.type, event);
  248. }
  249. // fire the default if it is passed and it is not prevented above
  250. if (defaultFunction) {
  251. defaultFunction(event);
  252. }
  253. },
  254. /**
  255. * Set back e.pageX and e.pageY that MooTools has abstracted away. #1165, #1346.
  256. */
  257. washMouseEvent: function (e) {
  258. if (e.page) {
  259. e.pageX = e.page.x;
  260. e.pageY = e.page.y;
  261. }
  262. return e;
  263. },
  264. /**
  265. * Stop running animations on the object
  266. */
  267. stop: function (el) {
  268. if (el.fx) {
  269. el.fx.cancel();
  270. }
  271. }
  272. };
  273. }());