standalone-framework.src.js 12 KB

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