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.

583 lines
11 KiB

3 years ago
  1. /**
  2. * @license Highcharts JS v3.0.6 (2013-10-04)
  3. *
  4. * Standalone Highcharts Framework
  5. *
  6. * License: MIT License
  7. */
  8. /*global Highcharts */
  9. var HighchartsAdapter = (function () {
  10. var UNDEFINED,
  11. doc = document,
  12. emptyArray = [],
  13. timers = [],
  14. timerId,
  15. Fx;
  16. Math.easeInOutSine = function (t, b, c, d) {
  17. return -c / 2 * (Math.cos(Math.PI * t / d) - 1) + b;
  18. };
  19. /**
  20. * Extend given object with custom events
  21. */
  22. function augment(obj) {
  23. function removeOneEvent(el, type, fn) {
  24. el.removeEventListener(type, fn, false);
  25. }
  26. function IERemoveOneEvent(el, type, fn) {
  27. fn = el.HCProxiedMethods[fn.toString()];
  28. el.detachEvent('on' + type, fn);
  29. }
  30. function removeAllEvents(el, type) {
  31. var events = el.HCEvents,
  32. remove,
  33. types,
  34. len,
  35. n;
  36. if (el.removeEventListener) {
  37. remove = removeOneEvent;
  38. } else if (el.attachEvent) {
  39. remove = IERemoveOneEvent;
  40. } else {
  41. return; // break on non-DOM events
  42. }
  43. if (type) {
  44. types = {};
  45. types[type] = true;
  46. } else {
  47. types = events;
  48. }
  49. for (n in types) {
  50. if (events[n]) {
  51. len = events[n].length;
  52. while (len--) {
  53. remove(el, n, events[n][len]);
  54. }
  55. }
  56. }
  57. }
  58. if (!obj.HCExtended) {
  59. Highcharts.extend(obj, {
  60. HCExtended: true,
  61. HCEvents: {},
  62. bind: function (name, fn) {
  63. var el = this,
  64. events = this.HCEvents,
  65. wrappedFn;
  66. // handle DOM events in modern browsers
  67. if (el.addEventListener) {
  68. el.addEventListener(name, fn, false);
  69. // handle old IE implementation
  70. } else if (el.attachEvent) {
  71. wrappedFn = function (e) {
  72. fn.call(el, e);
  73. };
  74. if (!el.HCProxiedMethods) {
  75. el.HCProxiedMethods = {};
  76. }
  77. // link wrapped fn with original fn, so we can get this in removeEvent
  78. el.HCProxiedMethods[fn.toString()] = wrappedFn;
  79. el.attachEvent('on' + name, wrappedFn);
  80. }
  81. if (events[name] === UNDEFINED) {
  82. events[name] = [];
  83. }
  84. events[name].push(fn);
  85. },
  86. unbind: function (name, fn) {
  87. var events,
  88. index;
  89. if (name) {
  90. events = this.HCEvents[name] || [];
  91. if (fn) {
  92. index = HighchartsAdapter.inArray(fn, events);
  93. if (index > -1) {
  94. events.splice(index, 1);
  95. this.HCEvents[name] = events;
  96. }
  97. if (this.removeEventListener) {
  98. removeOneEvent(this, name, fn);
  99. } else if (this.attachEvent) {
  100. IERemoveOneEvent(this, name, fn);
  101. }
  102. } else {
  103. removeAllEvents(this, name);
  104. this.HCEvents[name] = [];
  105. }
  106. } else {
  107. removeAllEvents(this);
  108. this.HCEvents = {};
  109. }
  110. },
  111. trigger: function (name, args) {
  112. var events = this.HCEvents[name] || [],
  113. target = this,
  114. len = events.length,
  115. i,
  116. preventDefault,
  117. fn;
  118. // Attach a simple preventDefault function to skip default handler if called
  119. preventDefault = function () {
  120. args.defaultPrevented = true;
  121. };
  122. for (i = 0; i < len; i++) {
  123. fn = events[i];
  124. // args is never null here
  125. if (args.stopped) {
  126. return;
  127. }
  128. args.preventDefault = preventDefault;
  129. args.target = target;
  130. args.type = name; // #2297
  131. // If the event handler return false, prevent the default handler from executing
  132. if (fn.call(this, args) === false) {
  133. args.preventDefault();
  134. }
  135. }
  136. }
  137. });
  138. }
  139. return obj;
  140. }
  141. return {
  142. /**
  143. * Initialize the adapter. This is run once as Highcharts is first run.
  144. */
  145. init: function (pathAnim) {
  146. /**
  147. * Compatibility section to add support for legacy IE. This can be removed if old IE
  148. * support is not needed.
  149. */
  150. if (!doc.defaultView) {
  151. this._getStyle = function (el, prop) {
  152. var val;
  153. if (el.style[prop]) {
  154. return el.style[prop];
  155. } else {
  156. if (prop === 'opacity') {
  157. prop = 'filter';
  158. }
  159. /*jslint unparam: true*/
  160. val = el.currentStyle[prop.replace(/\-(\w)/g, function (a, b) { return b.toUpperCase(); })];
  161. if (prop === 'filter') {
  162. val = val.replace(
  163. /alpha\(opacity=([0-9]+)\)/,
  164. function (a, b) {
  165. return b / 100;
  166. }
  167. );
  168. }
  169. /*jslint unparam: false*/
  170. return val === '' ? 1 : val;
  171. }
  172. };
  173. this.adapterRun = function (elem, method) {
  174. var alias = { width: 'clientWidth', height: 'clientHeight' }[method];
  175. if (alias) {
  176. elem.style.zoom = 1;
  177. return elem[alias] - 2 * parseInt(HighchartsAdapter._getStyle(elem, 'padding'), 10);
  178. }
  179. };
  180. }
  181. if (!Array.prototype.forEach) {
  182. this.each = function (arr, fn) { // legacy
  183. var i = 0,
  184. len = arr.length;
  185. for (; i < len; i++) {
  186. if (fn.call(arr[i], arr[i], i, arr) === false) {
  187. return i;
  188. }
  189. }
  190. };
  191. }
  192. if (!Array.prototype.indexOf) {
  193. this.inArray = function (item, arr) {
  194. var len,
  195. i = 0;
  196. if (arr) {
  197. len = arr.length;
  198. for (; i < len; i++) {
  199. if (arr[i] === item) {
  200. return i;
  201. }
  202. }
  203. }
  204. return -1;
  205. };
  206. }
  207. if (!Array.prototype.filter) {
  208. this.grep = function (elements, callback) {
  209. var ret = [],
  210. i = 0,
  211. length = elements.length;
  212. for (; i < length; i++) {
  213. if (!!callback(elements[i], i)) {
  214. ret.push(elements[i]);
  215. }
  216. }
  217. return ret;
  218. };
  219. }
  220. //--- End compatibility section ---
  221. /**
  222. * Start of animation specific code
  223. */
  224. Fx = function (elem, options, prop) {
  225. this.options = options;
  226. this.elem = elem;
  227. this.prop = prop;
  228. };
  229. Fx.prototype = {
  230. update: function () {
  231. var styles,
  232. paths = this.paths,
  233. elem = this.elem,
  234. elemelem = elem.element; // if destroyed, it is null
  235. // Animating a path definition on SVGElement
  236. if (paths && elemelem) {
  237. elem.attr('d', pathAnim.step(paths[0], paths[1], this.now, this.toD));
  238. // Other animations on SVGElement
  239. } else if (elem.attr) {
  240. if (elemelem) {
  241. elem.attr(this.prop, this.now);
  242. }
  243. // HTML styles
  244. } else {
  245. styles = {};
  246. styles[elem] = this.now + this.unit;
  247. Highcharts.css(elem, styles);
  248. }
  249. if (this.options.step) {
  250. this.options.step.call(this.elem, this.now, this);
  251. }
  252. },
  253. custom: function (from, to, unit) {
  254. var self = this,
  255. t = function (gotoEnd) {
  256. return self.step(gotoEnd);
  257. },
  258. i;
  259. this.startTime = +new Date();
  260. this.start = from;
  261. this.end = to;
  262. this.unit = unit;
  263. this.now = this.start;
  264. this.pos = this.state = 0;
  265. t.elem = this.elem;
  266. if (t() && timers.push(t) === 1) {
  267. timerId = setInterval(function () {
  268. for (i = 0; i < timers.length; i++) {
  269. if (!timers[i]()) {
  270. timers.splice(i--, 1);
  271. }
  272. }
  273. if (!timers.length) {
  274. clearInterval(timerId);
  275. }
  276. }, 13);
  277. }
  278. },
  279. step: function (gotoEnd) {
  280. var t = +new Date(),
  281. ret,
  282. done,
  283. options = this.options,
  284. i;
  285. if (this.elem.stopAnimation) {
  286. ret = false;
  287. } else if (gotoEnd || t >= options.duration + this.startTime) {
  288. this.now = this.end;
  289. this.pos = this.state = 1;
  290. this.update();
  291. this.options.curAnim[this.prop] = true;
  292. done = true;
  293. for (i in options.curAnim) {
  294. if (options.curAnim[i] !== true) {
  295. done = false;
  296. }
  297. }
  298. if (done) {
  299. if (options.complete) {
  300. options.complete.call(this.elem);
  301. }
  302. }
  303. ret = false;
  304. } else {
  305. var n = t - this.startTime;
  306. this.state = n / options.duration;
  307. this.pos = options.easing(n, 0, 1, options.duration);
  308. this.now = this.start + ((this.end - this.start) * this.pos);
  309. this.update();
  310. ret = true;
  311. }
  312. return ret;
  313. }
  314. };
  315. /**
  316. * The adapter animate method
  317. */
  318. this.animate = function (el, prop, opt) {
  319. var start,
  320. unit = '',
  321. end,
  322. fx,
  323. args,
  324. name;
  325. el.stopAnimation = false; // ready for new
  326. if (typeof opt !== 'object' || opt === null) {
  327. args = arguments;
  328. opt = {
  329. duration: args[2],
  330. easing: args[3],
  331. complete: args[4]
  332. };
  333. }
  334. if (typeof opt.duration !== 'number') {
  335. opt.duration = 400;
  336. }
  337. opt.easing = Math[opt.easing] || Math.easeInOutSine;
  338. opt.curAnim = Highcharts.extend({}, prop);
  339. for (name in prop) {
  340. fx = new Fx(el, opt, name);
  341. end = null;
  342. if (name === 'd') {
  343. fx.paths = pathAnim.init(
  344. el,
  345. el.d,
  346. prop.d
  347. );
  348. fx.toD = prop.d;
  349. start = 0;
  350. end = 1;
  351. } else if (el.attr) {
  352. start = el.attr(name);
  353. } else {
  354. start = parseFloat(HighchartsAdapter._getStyle(el, name)) || 0;
  355. if (name !== 'opacity') {
  356. unit = 'px';
  357. }
  358. }
  359. if (!end) {
  360. end = parseFloat(prop[name]);
  361. }
  362. fx.custom(start, end, unit);
  363. }
  364. };
  365. },
  366. /**
  367. * Internal method to return CSS value for given element and property
  368. */
  369. _getStyle: function (el, prop) {
  370. return window.getComputedStyle(el).getPropertyValue(prop);
  371. },
  372. /**
  373. * Downloads a script and executes a callback when done.
  374. * @param {String} scriptLocation
  375. * @param {Function} callback
  376. */
  377. getScript: function (scriptLocation, callback) {
  378. // We cannot assume that Assets class from mootools-more is available so instead insert a script tag to download script.
  379. var head = doc.getElementsByTagName('head')[0],
  380. script = doc.createElement('script');
  381. script.type = 'text/javascript';
  382. script.src = scriptLocation;
  383. script.onload = callback;
  384. head.appendChild(script);
  385. },
  386. /**
  387. * Return the index of an item in an array, or -1 if not found
  388. */
  389. inArray: function (item, arr) {
  390. return arr.indexOf ? arr.indexOf(item) : emptyArray.indexOf.call(arr, item);
  391. },
  392. /**
  393. * A direct link to adapter methods
  394. */
  395. adapterRun: function (elem, method) {
  396. return parseInt(HighchartsAdapter._getStyle(elem, method), 10);
  397. },
  398. /**
  399. * Filter an array
  400. */
  401. grep: function (elements, callback) {
  402. return emptyArray.filter.call(elements, callback);
  403. },
  404. /**
  405. * Map an array
  406. */
  407. map: function (arr, fn) {
  408. var results = [], i = 0, len = arr.length;
  409. for (; i < len; i++) {
  410. results[i] = fn.call(arr[i], arr[i], i, arr);
  411. }
  412. return results;
  413. },
  414. offset: function (el) {
  415. var left = 0,
  416. top = 0;
  417. while (el) {
  418. left += el.offsetLeft;
  419. top += el.offsetTop;
  420. el = el.offsetParent;
  421. }
  422. return {
  423. left: left,
  424. top: top
  425. };
  426. },
  427. /**
  428. * Add an event listener
  429. */
  430. addEvent: function (el, type, fn) {
  431. augment(el).bind(type, fn);
  432. },
  433. /**
  434. * Remove event added with addEvent
  435. */
  436. removeEvent: function (el, type, fn) {
  437. augment(el).unbind(type, fn);
  438. },
  439. /**
  440. * Fire an event on a custom object
  441. */
  442. fireEvent: function (el, type, eventArguments, defaultFunction) {
  443. var e;
  444. if (doc.createEvent && (el.dispatchEvent || el.fireEvent)) {
  445. e = doc.createEvent('Events');
  446. e.initEvent(type, true, true);
  447. e.target = el;
  448. Highcharts.extend(e, eventArguments);
  449. if (el.dispatchEvent) {
  450. el.dispatchEvent(e);
  451. } else {
  452. el.fireEvent(type, e);
  453. }
  454. } else if (el.HCExtended === true) {
  455. eventArguments = eventArguments || {};
  456. el.trigger(type, eventArguments);
  457. }
  458. if (eventArguments && eventArguments.defaultPrevented) {
  459. defaultFunction = null;
  460. }
  461. if (defaultFunction) {
  462. defaultFunction(eventArguments);
  463. }
  464. },
  465. washMouseEvent: function (e) {
  466. return e;
  467. },
  468. /**
  469. * Stop running animation
  470. */
  471. stop: function (el) {
  472. el.stopAnimation = true;
  473. },
  474. /**
  475. * Utility for iterating over an array. Parameters are reversed compared to jQuery.
  476. * @param {Array} arr
  477. * @param {Function} fn
  478. */
  479. each: function (arr, fn) { // modern browsers
  480. return Array.prototype.forEach.call(arr, fn);
  481. }
  482. };
  483. }());