vis.js is a dynamic, browser-based visualization library
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.

1606 lines
42 KiB

11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
9 years ago
9 years ago
9 years ago
11 years ago
11 years ago
11 years ago
10 years ago
10 years ago
11 years ago
11 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
9 years ago
9 years ago
  1. // utility functions
  2. // first check if moment.js is already loaded in the browser window, if so,
  3. // use this instance. Else, load via commonjs.
  4. var moment = require('./module/moment');
  5. var uuid = require('./module/uuid');
  6. /**
  7. * Test whether given object is a number
  8. * @param {*} object
  9. * @return {Boolean} isNumber
  10. */
  11. exports.isNumber = function (object) {
  12. return (object instanceof Number || typeof object == 'number');
  13. };
  14. /**
  15. * Remove everything in the DOM object
  16. * @param DOMobject
  17. */
  18. exports.recursiveDOMDelete = function (DOMobject) {
  19. if (DOMobject) {
  20. while (DOMobject.hasChildNodes() === true) {
  21. exports.recursiveDOMDelete(DOMobject.firstChild);
  22. DOMobject.removeChild(DOMobject.firstChild);
  23. }
  24. }
  25. };
  26. /**
  27. * this function gives you a range between 0 and 1 based on the min and max values in the set, the total sum of all values and the current value.
  28. *
  29. * @param min
  30. * @param max
  31. * @param total
  32. * @param value
  33. * @returns {number}
  34. */
  35. exports.giveRange = function (min, max, total, value) {
  36. if (max == min) {
  37. return 0.5;
  38. }
  39. else {
  40. var scale = 1 / (max - min);
  41. return Math.max(0, (value - min) * scale);
  42. }
  43. }
  44. /**
  45. * Test whether given object is a string
  46. * @param {*} object
  47. * @return {Boolean} isString
  48. */
  49. exports.isString = function (object) {
  50. return (object instanceof String || typeof object == 'string');
  51. };
  52. /**
  53. * Test whether given object is a Date, or a String containing a Date
  54. * @param {Date | String} object
  55. * @return {Boolean} isDate
  56. */
  57. exports.isDate = function (object) {
  58. if (object instanceof Date) {
  59. return true;
  60. }
  61. else if (exports.isString(object)) {
  62. // test whether this string contains a date
  63. var match = ASPDateRegex.exec(object);
  64. if (match) {
  65. return true;
  66. }
  67. else if (!isNaN(Date.parse(object))) {
  68. return true;
  69. }
  70. }
  71. return false;
  72. };
  73. /**
  74. * Create a semi UUID
  75. * source: http://stackoverflow.com/a/105074/1262753
  76. * @return {String} uuid
  77. */
  78. exports.randomUUID = function () {
  79. return uuid.v4();
  80. };
  81. /**
  82. * assign all keys of an object that are not nested objects to a certain value (used for color objects).
  83. * @param obj
  84. * @param value
  85. */
  86. exports.assignAllKeys = function (obj, value) {
  87. for (var prop in obj) {
  88. if (obj.hasOwnProperty(prop)) {
  89. if (typeof obj[prop] !== 'object') {
  90. obj[prop] = value;
  91. }
  92. }
  93. }
  94. }
  95. /**
  96. * Fill an object with a possibly partially defined other object. Only copies values if the a object has an object requiring values.
  97. * That means an object is not created on a property if only the b object has it.
  98. * @param obj
  99. * @param value
  100. */
  101. exports.fillIfDefined = function (a, b, allowDeletion = false) {
  102. for (var prop in a) {
  103. if (b[prop] !== undefined) {
  104. if (typeof b[prop] !== 'object') {
  105. if ((b[prop] === undefined || b[prop] === null) && a[prop] !== undefined && allowDeletion === true) {
  106. delete a[prop];
  107. }
  108. else {
  109. a[prop] = b[prop];
  110. }
  111. }
  112. else {
  113. if (typeof a[prop] === 'object') {
  114. exports.fillIfDefined(a[prop], b[prop], allowDeletion);
  115. }
  116. }
  117. }
  118. }
  119. }
  120. /**
  121. * Extend object a with the properties of object b or a series of objects
  122. * Only properties with defined values are copied
  123. * @param {Object} a
  124. * @param {... Object} b
  125. * @return {Object} a
  126. */
  127. exports.protoExtend = function (a, b) {
  128. for (var i = 1; i < arguments.length; i++) {
  129. var other = arguments[i];
  130. for (var prop in other) {
  131. a[prop] = other[prop];
  132. }
  133. }
  134. return a;
  135. };
  136. /**
  137. * Extend object a with the properties of object b or a series of objects
  138. * Only properties with defined values are copied
  139. * @param {Object} a
  140. * @param {... Object} b
  141. * @return {Object} a
  142. */
  143. exports.extend = function (a, b) {
  144. for (var i = 1; i < arguments.length; i++) {
  145. var other = arguments[i];
  146. for (var prop in other) {
  147. if (other.hasOwnProperty(prop)) {
  148. a[prop] = other[prop];
  149. }
  150. }
  151. }
  152. return a;
  153. };
  154. /**
  155. * Extend object a with selected properties of object b or a series of objects
  156. * Only properties with defined values are copied
  157. * @param {Array.<String>} props
  158. * @param {Object} a
  159. * @param {Object} b
  160. * @return {Object} a
  161. */
  162. exports.selectiveExtend = function (props, a, b) {
  163. if (!Array.isArray(props)) {
  164. throw new Error('Array with property names expected as first argument');
  165. }
  166. for (var i = 2; i < arguments.length; i++) {
  167. var other = arguments[i];
  168. for (var p = 0; p < props.length; p++) {
  169. var prop = props[p];
  170. if (other && other.hasOwnProperty(prop)) {
  171. a[prop] = other[prop];
  172. }
  173. }
  174. }
  175. return a;
  176. };
  177. /**
  178. * Extend object a with selected properties of object b or a series of objects
  179. * Only properties with defined values are copied
  180. * @param {Array.<String>} props
  181. * @param {Object} a
  182. * @param {Object} b
  183. * @return {Object} a
  184. */
  185. exports.selectiveDeepExtend = function (props, a, b, allowDeletion = false) {
  186. // TODO: add support for Arrays to deepExtend
  187. if (Array.isArray(b)) {
  188. throw new TypeError('Arrays are not supported by deepExtend');
  189. }
  190. for (var i = 2; i < arguments.length; i++) {
  191. var other = arguments[i];
  192. for (var p = 0; p < props.length; p++) {
  193. var prop = props[p];
  194. if (other.hasOwnProperty(prop)) {
  195. if (b[prop] && b[prop].constructor === Object) {
  196. if (a[prop] === undefined) {
  197. a[prop] = {};
  198. }
  199. if (a[prop].constructor === Object) {
  200. exports.deepExtend(a[prop], b[prop], false, allowDeletion);
  201. }
  202. else {
  203. if ((b[prop] === null) && a[prop] !== undefined && allowDeletion === true) {
  204. delete a[prop];
  205. }
  206. else {
  207. a[prop] = b[prop];
  208. }
  209. }
  210. } else if (Array.isArray(b[prop])) {
  211. throw new TypeError('Arrays are not supported by deepExtend');
  212. } else {
  213. if ((b[prop] === null) && a[prop] !== undefined && allowDeletion === true) {
  214. delete a[prop];
  215. }
  216. else {
  217. a[prop] = b[prop];
  218. }
  219. }
  220. }
  221. }
  222. }
  223. return a;
  224. };
  225. /**
  226. * Extend object a with selected properties of object b or a series of objects
  227. * Only properties with defined values are copied
  228. * @param {Array.<String>} props
  229. * @param {Object} a
  230. * @param {Object} b
  231. * @return {Object} a
  232. */
  233. exports.selectiveNotDeepExtend = function (props, a, b, allowDeletion = false) {
  234. // TODO: add support for Arrays to deepExtend
  235. if (Array.isArray(b)) {
  236. throw new TypeError('Arrays are not supported by deepExtend');
  237. }
  238. for (var prop in b) {
  239. if (b.hasOwnProperty(prop)) {
  240. if (props.indexOf(prop) == -1) {
  241. if (b[prop] && b[prop].constructor === Object) {
  242. if (a[prop] === undefined) {
  243. a[prop] = {};
  244. }
  245. if (a[prop].constructor === Object) {
  246. exports.deepExtend(a[prop], b[prop]);
  247. }
  248. else {
  249. if ((b[prop] === null) && a[prop] !== undefined && allowDeletion === true) {
  250. delete a[prop];
  251. }
  252. else {
  253. a[prop] = b[prop];
  254. }
  255. }
  256. } else if (Array.isArray(b[prop])) {
  257. a[prop] = [];
  258. for (let i = 0; i < b[prop].length; i++) {
  259. a[prop].push(b[prop][i]);
  260. }
  261. } else {
  262. if ((b[prop] === null) && a[prop] !== undefined && allowDeletion === true) {
  263. delete a[prop];
  264. }
  265. else {
  266. a[prop] = b[prop];
  267. }
  268. }
  269. }
  270. }
  271. }
  272. return a;
  273. };
  274. /**
  275. * Deep extend an object a with the properties of object b
  276. * @param {Object} a
  277. * @param {Object} b
  278. * @param [Boolean] protoExtend --> optional parameter. If true, the prototype values will also be extended.
  279. * (ie. the options objects that inherit from others will also get the inherited options)
  280. * @param [Boolean] global --> optional parameter. If true, the values of fields that are null will not deleted
  281. * @returns {Object}
  282. */
  283. exports.deepExtend = function (a, b, protoExtend, allowDeletion) {
  284. for (var prop in b) {
  285. if (b.hasOwnProperty(prop) || protoExtend === true) {
  286. if (b[prop] && b[prop].constructor === Object) {
  287. if (a[prop] === undefined) {
  288. a[prop] = {};
  289. }
  290. if (a[prop].constructor === Object) {
  291. exports.deepExtend(a[prop], b[prop], protoExtend);
  292. }
  293. else {
  294. if ((b[prop] === null) && a[prop] !== undefined && allowDeletion === true) {
  295. delete a[prop];
  296. }
  297. else {
  298. a[prop] = b[prop];
  299. }
  300. }
  301. } else if (Array.isArray(b[prop])) {
  302. a[prop] = [];
  303. for (let i = 0; i < b[prop].length; i++) {
  304. a[prop].push(b[prop][i]);
  305. }
  306. } else {
  307. if ((b[prop] === null) && a[prop] !== undefined && allowDeletion === true) {
  308. delete a[prop];
  309. }
  310. else {
  311. a[prop] = b[prop];
  312. }
  313. }
  314. }
  315. }
  316. return a;
  317. };
  318. /**
  319. * Test whether all elements in two arrays are equal.
  320. * @param {Array} a
  321. * @param {Array} b
  322. * @return {boolean} Returns true if both arrays have the same length and same
  323. * elements.
  324. */
  325. exports.equalArray = function (a, b) {
  326. if (a.length != b.length) return false;
  327. for (var i = 0, len = a.length; i < len; i++) {
  328. if (a[i] != b[i]) return false;
  329. }
  330. return true;
  331. };
  332. /**
  333. * Convert an object to another type
  334. * @param {Boolean | Number | String | Date | Moment | Null | undefined} object
  335. * @param {String | undefined} type Name of the type. Available types:
  336. * 'Boolean', 'Number', 'String',
  337. * 'Date', 'Moment', ISODate', 'ASPDate'.
  338. * @return {*} object
  339. * @throws Error
  340. */
  341. exports.convert = function (object, type) {
  342. var match;
  343. if (object === undefined) {
  344. return undefined;
  345. }
  346. if (object === null) {
  347. return null;
  348. }
  349. if (!type) {
  350. return object;
  351. }
  352. if (!(typeof type === 'string') && !(type instanceof String)) {
  353. throw new Error('Type must be a string');
  354. }
  355. //noinspection FallthroughInSwitchStatementJS
  356. switch (type) {
  357. case 'boolean':
  358. case 'Boolean':
  359. return Boolean(object);
  360. case 'number':
  361. case 'Number':
  362. if (exports.isString(object) && !isNaN(Date.parse(object))) {
  363. return moment(object).valueOf();
  364. } else {
  365. return Number(object.valueOf());
  366. }
  367. case 'string':
  368. case 'String':
  369. return String(object);
  370. case 'Date':
  371. if (exports.isNumber(object)) {
  372. return new Date(object);
  373. }
  374. if (object instanceof Date) {
  375. return new Date(object.valueOf());
  376. }
  377. else if (moment.isMoment(object)) {
  378. return new Date(object.valueOf());
  379. }
  380. if (exports.isString(object)) {
  381. match = ASPDateRegex.exec(object);
  382. if (match) {
  383. // object is an ASP date
  384. return new Date(Number(match[1])); // parse number
  385. }
  386. else {
  387. return moment(new Date(object)).toDate(); // parse string
  388. }
  389. }
  390. else {
  391. throw new Error(
  392. 'Cannot convert object of type ' + exports.getType(object) +
  393. ' to type Date');
  394. }
  395. case 'Moment':
  396. if (exports.isNumber(object)) {
  397. return moment(object);
  398. }
  399. if (object instanceof Date) {
  400. return moment(object.valueOf());
  401. }
  402. else if (moment.isMoment(object)) {
  403. return moment(object);
  404. }
  405. if (exports.isString(object)) {
  406. match = ASPDateRegex.exec(object);
  407. if (match) {
  408. // object is an ASP date
  409. return moment(Number(match[1])); // parse number
  410. }
  411. else {
  412. return moment(object); // parse string
  413. }
  414. }
  415. else {
  416. throw new Error(
  417. 'Cannot convert object of type ' + exports.getType(object) +
  418. ' to type Date');
  419. }
  420. case 'ISODate':
  421. if (exports.isNumber(object)) {
  422. return new Date(object);
  423. }
  424. else if (object instanceof Date) {
  425. return object.toISOString();
  426. }
  427. else if (moment.isMoment(object)) {
  428. return object.toDate().toISOString();
  429. }
  430. else if (exports.isString(object)) {
  431. match = ASPDateRegex.exec(object);
  432. if (match) {
  433. // object is an ASP date
  434. return new Date(Number(match[1])).toISOString(); // parse number
  435. }
  436. else {
  437. return moment(object).format(); // ISO 8601
  438. }
  439. }
  440. else {
  441. throw new Error(
  442. 'Cannot convert object of type ' + exports.getType(object) +
  443. ' to type ISODate');
  444. }
  445. case 'ASPDate':
  446. if (exports.isNumber(object)) {
  447. return '/Date(' + object + ')/';
  448. }
  449. else if (object instanceof Date) {
  450. return '/Date(' + object.valueOf() + ')/';
  451. }
  452. else if (exports.isString(object)) {
  453. match = ASPDateRegex.exec(object);
  454. var value;
  455. if (match) {
  456. // object is an ASP date
  457. value = new Date(Number(match[1])).valueOf(); // parse number
  458. }
  459. else {
  460. value = new Date(object).valueOf(); // parse string
  461. }
  462. return '/Date(' + value + ')/';
  463. }
  464. else {
  465. throw new Error(
  466. 'Cannot convert object of type ' + exports.getType(object) +
  467. ' to type ASPDate');
  468. }
  469. default:
  470. throw new Error('Unknown type "' + type + '"');
  471. }
  472. };
  473. // parse ASP.Net Date pattern,
  474. // for example '/Date(1198908717056)/' or '/Date(1198908717056-0700)/'
  475. // code from http://momentjs.com/
  476. var ASPDateRegex = /^\/?Date\((\-?\d+)/i;
  477. /**
  478. * Get the type of an object, for example exports.getType([]) returns 'Array'
  479. * @param {*} object
  480. * @return {String} type
  481. */
  482. exports.getType = function (object) {
  483. var type = typeof object;
  484. if (type == 'object') {
  485. if (object === null) {
  486. return 'null';
  487. }
  488. if (object instanceof Boolean) {
  489. return 'Boolean';
  490. }
  491. if (object instanceof Number) {
  492. return 'Number';
  493. }
  494. if (object instanceof String) {
  495. return 'String';
  496. }
  497. if (Array.isArray(object)) {
  498. return 'Array';
  499. }
  500. if (object instanceof Date) {
  501. return 'Date';
  502. }
  503. return 'Object';
  504. }
  505. else if (type == 'number') {
  506. return 'Number';
  507. }
  508. else if (type == 'boolean') {
  509. return 'Boolean';
  510. }
  511. else if (type == 'string') {
  512. return 'String';
  513. }
  514. else if (type === undefined) {
  515. return 'undefined';
  516. }
  517. return type;
  518. };
  519. /**
  520. * Used to extend an array and copy it. This is used to propagate paths recursively.
  521. *
  522. * @param arr
  523. * @param newValue
  524. * @returns {Array}
  525. */
  526. exports.copyAndExtendArray = function (arr, newValue) {
  527. let newArr = [];
  528. for (let i = 0; i < arr.length; i++) {
  529. newArr.push(arr[i]);
  530. }
  531. newArr.push(newValue);
  532. return newArr;
  533. }
  534. /**
  535. * Used to extend an array and copy it. This is used to propagate paths recursively.
  536. *
  537. * @param arr
  538. * @param newValue
  539. * @returns {Array}
  540. */
  541. exports.copyArray = function (arr) {
  542. let newArr = [];
  543. for (let i = 0; i < arr.length; i++) {
  544. newArr.push(arr[i]);
  545. }
  546. return newArr;
  547. }
  548. /**
  549. * Retrieve the absolute left value of a DOM element
  550. * @param {Element} elem A dom element, for example a div
  551. * @return {number} left The absolute left position of this element
  552. * in the browser page.
  553. */
  554. exports.getAbsoluteLeft = function (elem) {
  555. return elem.getBoundingClientRect().left;
  556. };
  557. exports.getAbsoluteRight = function (elem) {
  558. return elem.getBoundingClientRect().right;
  559. };
  560. /**
  561. * Retrieve the absolute top value of a DOM element
  562. * @param {Element} elem A dom element, for example a div
  563. * @return {number} top The absolute top position of this element
  564. * in the browser page.
  565. */
  566. exports.getAbsoluteTop = function (elem) {
  567. return elem.getBoundingClientRect().top;
  568. };
  569. /**
  570. * add a className to the given elements style
  571. * @param {Element} elem
  572. * @param {String} className
  573. */
  574. exports.addClassName = function (elem, classNames) {
  575. var classes = elem.className.split(' ');
  576. var newClasses = classNames.split(' ');
  577. classes = classes.concat(newClasses.filter(function(className) {
  578. return classes.indexOf(className) < 0;
  579. }));
  580. elem.className = classes.join(' ');
  581. };
  582. /**
  583. * add a className to the given elements style
  584. * @param {Element} elem
  585. * @param {String} className
  586. */
  587. exports.removeClassName = function (elem, classNames) {
  588. var classes = elem.className.split(' ');
  589. var oldClasses = classNames.split(' ');
  590. classes = classes.filter(function(className) {
  591. return oldClasses.indexOf(className) < 0;
  592. });
  593. elem.className = classes.join(' ');
  594. };
  595. /**
  596. * For each method for both arrays and objects.
  597. * In case of an array, the built-in Array.forEach() is applied.
  598. * In case of an Object, the method loops over all properties of the object.
  599. * @param {Object | Array} object An Object or Array
  600. * @param {function} callback Callback method, called for each item in
  601. * the object or array with three parameters:
  602. * callback(value, index, object)
  603. */
  604. exports.forEach = function (object, callback) {
  605. var i,
  606. len;
  607. if (Array.isArray(object)) {
  608. // array
  609. for (i = 0, len = object.length; i < len; i++) {
  610. callback(object[i], i, object);
  611. }
  612. }
  613. else {
  614. // object
  615. for (i in object) {
  616. if (object.hasOwnProperty(i)) {
  617. callback(object[i], i, object);
  618. }
  619. }
  620. }
  621. };
  622. /**
  623. * Convert an object into an array: all objects properties are put into the
  624. * array. The resulting array is unordered.
  625. * @param {Object} object
  626. * @param {Array} array
  627. */
  628. exports.toArray = function (object) {
  629. var array = [];
  630. for (var prop in object) {
  631. if (object.hasOwnProperty(prop)) array.push(object[prop]);
  632. }
  633. return array;
  634. };
  635. /**
  636. * Update a property in an object
  637. * @param {Object} object
  638. * @param {String} key
  639. * @param {*} value
  640. * @return {Boolean} changed
  641. */
  642. exports.updateProperty = function (object, key, value) {
  643. if (object[key] !== value) {
  644. object[key] = value;
  645. return true;
  646. }
  647. else {
  648. return false;
  649. }
  650. };
  651. /**
  652. * Throttle the given function to be only executed once per animation frame
  653. * @param {function} fn
  654. * @returns {function} Returns the throttled function
  655. */
  656. exports.throttle = function (fn) {
  657. var scheduled = false;
  658. return function throttled () {
  659. if (!scheduled) {
  660. scheduled = true;
  661. requestAnimationFrame(function () {
  662. scheduled = false;
  663. fn();
  664. });
  665. }
  666. }
  667. };
  668. /**
  669. * Add and event listener. Works for all browsers
  670. * @param {Element} element An html element
  671. * @param {string} action The action, for example "click",
  672. * without the prefix "on"
  673. * @param {function} listener The callback function to be executed
  674. * @param {boolean} [useCapture]
  675. */
  676. exports.addEventListener = function (element, action, listener, useCapture) {
  677. if (element.addEventListener) {
  678. if (useCapture === undefined)
  679. useCapture = false;
  680. if (action === "mousewheel" && navigator.userAgent.indexOf("Firefox") >= 0) {
  681. action = "DOMMouseScroll"; // For Firefox
  682. }
  683. element.addEventListener(action, listener, useCapture);
  684. } else {
  685. element.attachEvent("on" + action, listener); // IE browsers
  686. }
  687. };
  688. /**
  689. * Remove an event listener from an element
  690. * @param {Element} element An html dom element
  691. * @param {string} action The name of the event, for example "mousedown"
  692. * @param {function} listener The listener function
  693. * @param {boolean} [useCapture]
  694. */
  695. exports.removeEventListener = function (element, action, listener, useCapture) {
  696. if (element.removeEventListener) {
  697. // non-IE browsers
  698. if (useCapture === undefined)
  699. useCapture = false;
  700. if (action === "mousewheel" && navigator.userAgent.indexOf("Firefox") >= 0) {
  701. action = "DOMMouseScroll"; // For Firefox
  702. }
  703. element.removeEventListener(action, listener, useCapture);
  704. } else {
  705. // IE browsers
  706. element.detachEvent("on" + action, listener);
  707. }
  708. };
  709. /**
  710. * Cancels the event if it is cancelable, without stopping further propagation of the event.
  711. */
  712. exports.preventDefault = function (event) {
  713. if (!event)
  714. event = window.event;
  715. if (event.preventDefault) {
  716. event.preventDefault(); // non-IE browsers
  717. }
  718. else {
  719. event.returnValue = false; // IE browsers
  720. }
  721. };
  722. /**
  723. * Get HTML element which is the target of the event
  724. * @param {Event} event
  725. * @return {Element} target element
  726. */
  727. exports.getTarget = function (event) {
  728. // code from http://www.quirksmode.org/js/events_properties.html
  729. if (!event) {
  730. event = window.event;
  731. }
  732. var target;
  733. if (event.target) {
  734. target = event.target;
  735. }
  736. else if (event.srcElement) {
  737. target = event.srcElement;
  738. }
  739. if (target.nodeType != undefined && target.nodeType == 3) {
  740. // defeat Safari bug
  741. target = target.parentNode;
  742. }
  743. return target;
  744. };
  745. /**
  746. * Check if given element contains given parent somewhere in the DOM tree
  747. * @param {Element} element
  748. * @param {Element} parent
  749. */
  750. exports.hasParent = function (element, parent) {
  751. var e = element;
  752. while (e) {
  753. if (e === parent) {
  754. return true;
  755. }
  756. e = e.parentNode;
  757. }
  758. return false;
  759. };
  760. exports.option = {};
  761. /**
  762. * Convert a value into a boolean
  763. * @param {Boolean | function | undefined} value
  764. * @param {Boolean} [defaultValue]
  765. * @returns {Boolean} bool
  766. */
  767. exports.option.asBoolean = function (value, defaultValue) {
  768. if (typeof value == 'function') {
  769. value = value();
  770. }
  771. if (value != null) {
  772. return (value != false);
  773. }
  774. return defaultValue || null;
  775. };
  776. /**
  777. * Convert a value into a number
  778. * @param {Boolean | function | undefined} value
  779. * @param {Number} [defaultValue]
  780. * @returns {Number} number
  781. */
  782. exports.option.asNumber = function (value, defaultValue) {
  783. if (typeof value == 'function') {
  784. value = value();
  785. }
  786. if (value != null) {
  787. return Number(value) || defaultValue || null;
  788. }
  789. return defaultValue || null;
  790. };
  791. /**
  792. * Convert a value into a string
  793. * @param {String | function | undefined} value
  794. * @param {String} [defaultValue]
  795. * @returns {String} str
  796. */
  797. exports.option.asString = function (value, defaultValue) {
  798. if (typeof value == 'function') {
  799. value = value();
  800. }
  801. if (value != null) {
  802. return String(value);
  803. }
  804. return defaultValue || null;
  805. };
  806. /**
  807. * Convert a size or location into a string with pixels or a percentage
  808. * @param {String | Number | function | undefined} value
  809. * @param {String} [defaultValue]
  810. * @returns {String} size
  811. */
  812. exports.option.asSize = function (value, defaultValue) {
  813. if (typeof value == 'function') {
  814. value = value();
  815. }
  816. if (exports.isString(value)) {
  817. return value;
  818. }
  819. else if (exports.isNumber(value)) {
  820. return value + 'px';
  821. }
  822. else {
  823. return defaultValue || null;
  824. }
  825. };
  826. /**
  827. * Convert a value into a DOM element
  828. * @param {HTMLElement | function | undefined} value
  829. * @param {HTMLElement} [defaultValue]
  830. * @returns {HTMLElement | null} dom
  831. */
  832. exports.option.asElement = function (value, defaultValue) {
  833. if (typeof value == 'function') {
  834. value = value();
  835. }
  836. return value || defaultValue || null;
  837. };
  838. /**
  839. * http://stackoverflow.com/questions/5623838/rgb-to-hex-and-hex-to-rgb
  840. *
  841. * @param {String} hex
  842. * @returns {{r: *, g: *, b: *}} | 255 range
  843. */
  844. exports.hexToRGB = function (hex) {
  845. // Expand shorthand form (e.g. "03F") to full form (e.g. "0033FF")
  846. var shorthandRegex = /^#?([a-f\d])([a-f\d])([a-f\d])$/i;
  847. hex = hex.replace(shorthandRegex, function (m, r, g, b) {
  848. return r + r + g + g + b + b;
  849. });
  850. var result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);
  851. return result ? {
  852. r: parseInt(result[1], 16),
  853. g: parseInt(result[2], 16),
  854. b: parseInt(result[3], 16)
  855. } : null;
  856. };
  857. /**
  858. * This function takes color in hex format or rgb() or rgba() format and overrides the opacity. Returns rgba() string.
  859. * @param color
  860. * @param opacity
  861. * @returns {*}
  862. */
  863. exports.overrideOpacity = function (color, opacity) {
  864. if (color.indexOf("rgba") != -1) {
  865. return color;
  866. }
  867. else if (color.indexOf("rgb") != -1) {
  868. var rgb = color.substr(color.indexOf("(") + 1).replace(")", "").split(",");
  869. return "rgba(" + rgb[0] + "," + rgb[1] + "," + rgb[2] + "," + opacity + ")"
  870. }
  871. else {
  872. var rgb = exports.hexToRGB(color);
  873. if (rgb == null) {
  874. return color;
  875. }
  876. else {
  877. return "rgba(" + rgb.r + "," + rgb.g + "," + rgb.b + "," + opacity + ")"
  878. }
  879. }
  880. }
  881. /**
  882. *
  883. * @param red 0 -- 255
  884. * @param green 0 -- 255
  885. * @param blue 0 -- 255
  886. * @returns {string}
  887. * @constructor
  888. */
  889. exports.RGBToHex = function (red, green, blue) {
  890. return "#" + ((1 << 24) + (red << 16) + (green << 8) + blue).toString(16).slice(1);
  891. };
  892. /**
  893. * Parse a color property into an object with border, background, and
  894. * highlight colors
  895. * @param {Object | String} color
  896. * @return {Object} colorObject
  897. */
  898. exports.parseColor = function (color) {
  899. var c;
  900. if (exports.isString(color) === true) {
  901. if (exports.isValidRGB(color) === true) {
  902. var rgb = color.substr(4).substr(0, color.length - 5).split(',').map(function (value) { return parseInt(value) });
  903. color = exports.RGBToHex(rgb[0], rgb[1], rgb[2]);
  904. }
  905. if (exports.isValidHex(color) === true) {
  906. var hsv = exports.hexToHSV(color);
  907. var lighterColorHSV = { h: hsv.h, s: hsv.s * 0.8, v: Math.min(1, hsv.v * 1.02) };
  908. var darkerColorHSV = { h: hsv.h, s: Math.min(1, hsv.s * 1.25), v: hsv.v * 0.8 };
  909. var darkerColorHex = exports.HSVToHex(darkerColorHSV.h, darkerColorHSV.s, darkerColorHSV.v);
  910. var lighterColorHex = exports.HSVToHex(lighterColorHSV.h, lighterColorHSV.s, lighterColorHSV.v);
  911. c = {
  912. background: color,
  913. border: darkerColorHex,
  914. highlight: {
  915. background: lighterColorHex,
  916. border: darkerColorHex
  917. },
  918. hover: {
  919. background: lighterColorHex,
  920. border: darkerColorHex
  921. }
  922. };
  923. }
  924. else {
  925. c = {
  926. background: color,
  927. border: color,
  928. highlight: {
  929. background: color,
  930. border: color
  931. },
  932. hover: {
  933. background: color,
  934. border: color
  935. }
  936. };
  937. }
  938. }
  939. else {
  940. c = {};
  941. c.background = color.background || undefined;
  942. c.border = color.border || undefined;
  943. if (exports.isString(color.highlight)) {
  944. c.highlight = {
  945. border: color.highlight,
  946. background: color.highlight
  947. }
  948. }
  949. else {
  950. c.highlight = {};
  951. c.highlight.background = color.highlight && color.highlight.background || undefined;
  952. c.highlight.border = color.highlight && color.highlight.border || undefined;
  953. }
  954. if (exports.isString(color.hover)) {
  955. c.hover = {
  956. border: color.hover,
  957. background: color.hover
  958. }
  959. }
  960. else {
  961. c.hover = {};
  962. c.hover.background = color.hover && color.hover.background || undefined;
  963. c.hover.border = color.hover && color.hover.border || undefined;
  964. }
  965. }
  966. return c;
  967. };
  968. /**
  969. * http://www.javascripter.net/faq/rgb2hsv.htm
  970. *
  971. * @param red
  972. * @param green
  973. * @param blue
  974. * @returns {*}
  975. * @constructor
  976. */
  977. exports.RGBToHSV = function (red, green, blue) {
  978. red = red / 255; green = green / 255; blue = blue / 255;
  979. var minRGB = Math.min(red, Math.min(green, blue));
  980. var maxRGB = Math.max(red, Math.max(green, blue));
  981. // Black-gray-white
  982. if (minRGB == maxRGB) {
  983. return { h: 0, s: 0, v: minRGB };
  984. }
  985. // Colors other than black-gray-white:
  986. var d = (red == minRGB) ? green - blue : ((blue == minRGB) ? red - green : blue - red);
  987. var h = (red == minRGB) ? 3 : ((blue == minRGB) ? 1 : 5);
  988. var hue = 60 * (h - d / (maxRGB - minRGB)) / 360;
  989. var saturation = (maxRGB - minRGB) / maxRGB;
  990. var value = maxRGB;
  991. return { h: hue, s: saturation, v: value };
  992. };
  993. var cssUtil = {
  994. // split a string with css styles into an object with key/values
  995. split: function (cssText) {
  996. var styles = {};
  997. cssText.split(';').forEach(function (style) {
  998. if (style.trim() != '') {
  999. var parts = style.split(':');
  1000. var key = parts[0].trim();
  1001. var value = parts[1].trim();
  1002. styles[key] = value;
  1003. }
  1004. });
  1005. return styles;
  1006. },
  1007. // build a css text string from an object with key/values
  1008. join: function (styles) {
  1009. return Object.keys(styles)
  1010. .map(function (key) {
  1011. return key + ': ' + styles[key];
  1012. })
  1013. .join('; ');
  1014. }
  1015. };
  1016. /**
  1017. * Append a string with css styles to an element
  1018. * @param {Element} element
  1019. * @param {String} cssText
  1020. */
  1021. exports.addCssText = function (element, cssText) {
  1022. var currentStyles = cssUtil.split(element.style.cssText);
  1023. var newStyles = cssUtil.split(cssText);
  1024. var styles = exports.extend(currentStyles, newStyles);
  1025. element.style.cssText = cssUtil.join(styles);
  1026. };
  1027. /**
  1028. * Remove a string with css styles from an element
  1029. * @param {Element} element
  1030. * @param {String} cssText
  1031. */
  1032. exports.removeCssText = function (element, cssText) {
  1033. var styles = cssUtil.split(element.style.cssText);
  1034. var removeStyles = cssUtil.split(cssText);
  1035. for (var key in removeStyles) {
  1036. if (removeStyles.hasOwnProperty(key)) {
  1037. delete styles[key];
  1038. }
  1039. }
  1040. element.style.cssText = cssUtil.join(styles);
  1041. };
  1042. /**
  1043. * https://gist.github.com/mjijackson/5311256
  1044. * @param h
  1045. * @param s
  1046. * @param v
  1047. * @returns {{r: number, g: number, b: number}}
  1048. * @constructor
  1049. */
  1050. exports.HSVToRGB = function (h, s, v) {
  1051. var r, g, b;
  1052. var i = Math.floor(h * 6);
  1053. var f = h * 6 - i;
  1054. var p = v * (1 - s);
  1055. var q = v * (1 - f * s);
  1056. var t = v * (1 - (1 - f) * s);
  1057. switch (i % 6) {
  1058. case 0: r = v, g = t, b = p; break;
  1059. case 1: r = q, g = v, b = p; break;
  1060. case 2: r = p, g = v, b = t; break;
  1061. case 3: r = p, g = q, b = v; break;
  1062. case 4: r = t, g = p, b = v; break;
  1063. case 5: r = v, g = p, b = q; break;
  1064. }
  1065. return { r: Math.floor(r * 255), g: Math.floor(g * 255), b: Math.floor(b * 255) };
  1066. };
  1067. exports.HSVToHex = function (h, s, v) {
  1068. var rgb = exports.HSVToRGB(h, s, v);
  1069. return exports.RGBToHex(rgb.r, rgb.g, rgb.b);
  1070. };
  1071. exports.hexToHSV = function (hex) {
  1072. var rgb = exports.hexToRGB(hex);
  1073. return exports.RGBToHSV(rgb.r, rgb.g, rgb.b);
  1074. };
  1075. exports.isValidHex = function (hex) {
  1076. var isOk = /(^#[0-9A-F]{6}$)|(^#[0-9A-F]{3}$)/i.test(hex);
  1077. return isOk;
  1078. };
  1079. exports.isValidRGB = function (rgb) {
  1080. rgb = rgb.replace(" ", "");
  1081. var isOk = /rgb\((\d{1,3}),(\d{1,3}),(\d{1,3})\)/i.test(rgb);
  1082. return isOk;
  1083. }
  1084. exports.isValidRGBA = function (rgba) {
  1085. rgba = rgba.replace(" ", "");
  1086. var isOk = /rgba\((\d{1,3}),(\d{1,3}),(\d{1,3}),(.{1,3})\)/i.test(rgba);
  1087. return isOk;
  1088. }
  1089. /**
  1090. * This recursively redirects the prototype of JSON objects to the referenceObject
  1091. * This is used for default options.
  1092. *
  1093. * @param referenceObject
  1094. * @returns {*}
  1095. */
  1096. exports.selectiveBridgeObject = function (fields, referenceObject) {
  1097. if (typeof referenceObject == "object") {
  1098. var objectTo = Object.create(referenceObject);
  1099. for (var i = 0; i < fields.length; i++) {
  1100. if (referenceObject.hasOwnProperty(fields[i])) {
  1101. if (typeof referenceObject[fields[i]] == "object") {
  1102. objectTo[fields[i]] = exports.bridgeObject(referenceObject[fields[i]]);
  1103. }
  1104. }
  1105. }
  1106. return objectTo;
  1107. }
  1108. else {
  1109. return null;
  1110. }
  1111. };
  1112. /**
  1113. * This recursively redirects the prototype of JSON objects to the referenceObject
  1114. * This is used for default options.
  1115. *
  1116. * @param referenceObject
  1117. * @returns {*}
  1118. */
  1119. exports.bridgeObject = function (referenceObject) {
  1120. if (typeof referenceObject == "object") {
  1121. var objectTo = Object.create(referenceObject);
  1122. if (referenceObject instanceof Element) {
  1123. // Avoid bridging DOM objects
  1124. objectTo = referenceObject;
  1125. } else {
  1126. objectTo = Object.create(referenceObject);
  1127. for (var i in referenceObject) {
  1128. if (referenceObject.hasOwnProperty(i)) {
  1129. if (typeof referenceObject[i] == "object") {
  1130. objectTo[i] = exports.bridgeObject(referenceObject[i]);
  1131. }
  1132. }
  1133. }
  1134. }
  1135. return objectTo;
  1136. }
  1137. else {
  1138. return null;
  1139. }
  1140. };
  1141. /**
  1142. * This method provides a stable sort implementation, very fast for presorted data
  1143. *
  1144. * @param a the array
  1145. * @param a order comparator
  1146. * @returns {the array}
  1147. */
  1148. exports.insertSort = function (a,compare) {
  1149. for (var i = 0; i < a.length; i++) {
  1150. var k = a[i];
  1151. for (var j = i; j > 0 && compare(k,a[j - 1])<0; j--) {
  1152. a[j] = a[j - 1];
  1153. }
  1154. a[j] = k;
  1155. }
  1156. return a;
  1157. }
  1158. /**
  1159. * This is used to set the options of subobjects in the options object.
  1160. *
  1161. * A requirement of these subobjects is that they have an 'enabled' element
  1162. * which is optional for the user but mandatory for the program.
  1163. *
  1164. * The added value here of the merge is that option 'enabled' is set as required.
  1165. *
  1166. *
  1167. * @param {object} mergeTarget | either this.options or the options used for the groups.
  1168. * @param {object} options | options
  1169. * @param {String} option | option key in the options argument
  1170. * @param {object} globalOptions | global options, passed in to determine value of option 'enabled'
  1171. */
  1172. exports.mergeOptions = function (mergeTarget, options, option, globalOptions = {}) {
  1173. // Local helpers
  1174. var isPresent = function(obj) {
  1175. return obj !== null && obj !== undefined;
  1176. }
  1177. var isObject = function(obj) {
  1178. return obj !== null && typeof obj === 'object';
  1179. }
  1180. // https://stackoverflow.com/a/34491287/1223531
  1181. var isEmpty = function(obj) {
  1182. for (var x in obj) { if (obj.hasOwnProperty(x)) return false; }
  1183. return true;
  1184. };
  1185. // Guards
  1186. if (!isObject(mergeTarget)) {
  1187. throw new Error('Parameter mergeTarget must be an object');
  1188. }
  1189. if (!isObject(options)) {
  1190. throw new Error('Parameter options must be an object');
  1191. }
  1192. if (!isPresent(option)) {
  1193. throw new Error('Parameter option must have a value');
  1194. }
  1195. if (!isObject(globalOptions)) {
  1196. throw new Error('Parameter globalOptions must be an object');
  1197. }
  1198. //
  1199. // Actual merge routine, separated from main logic
  1200. // Only a single level of options is merged. Deeper levels are ref'd. This may actually be an issue.
  1201. //
  1202. var doMerge = function(target, options, option) {
  1203. if (!isObject(target[option])) {
  1204. target[option] = {};
  1205. }
  1206. let src = options[option];
  1207. let dst = target[option];
  1208. for (var prop in src) {
  1209. if (src.hasOwnProperty(prop)) {
  1210. dst[prop] = src[prop];
  1211. }
  1212. }
  1213. };
  1214. // Local initialization
  1215. var srcOption = options[option];
  1216. var globalPassed = isObject(globalOptions) && !isEmpty(globalOptions);
  1217. var globalOption = globalPassed? globalOptions[option]: undefined;
  1218. var globalEnabled = globalOption? globalOption.enabled: undefined;
  1219. /////////////////////////////////////////
  1220. // Main routine
  1221. /////////////////////////////////////////
  1222. if (srcOption === undefined) {
  1223. return; // Nothing to do
  1224. }
  1225. if ((typeof srcOption) === 'boolean') {
  1226. if (!isObject(mergeTarget[option])) {
  1227. mergeTarget[option] = {};
  1228. }
  1229. mergeTarget[option].enabled = srcOption;
  1230. return;
  1231. }
  1232. if (srcOption === null && !isObject(mergeTarget[option])) {
  1233. // If possible, explicit copy from globals
  1234. if (isPresent(globalOption)) {
  1235. mergeTarget[option] = Object.create(globalOption);
  1236. } else {
  1237. return; // Nothing to do
  1238. }
  1239. }
  1240. if (!isObject(srcOption)) {
  1241. return;
  1242. }
  1243. //
  1244. // Ensure that 'enabled' is properly set. It is required internally
  1245. // Note that the value from options will always overwrite the existing value
  1246. //
  1247. let enabled = true; // default value
  1248. if (srcOption.enabled !== undefined) {
  1249. enabled = srcOption.enabled;
  1250. } else {
  1251. // Take from globals, if present
  1252. if (globalEnabled !== undefined) {
  1253. enabled = globalOption.enabled;
  1254. }
  1255. }
  1256. doMerge(mergeTarget, options, option);
  1257. mergeTarget[option].enabled = enabled;
  1258. }
  1259. /**
  1260. * This function does a binary search for a visible item in a sorted list. If we find a visible item, the code that uses
  1261. * this function will then iterate in both directions over this sorted list to find all visible items.
  1262. *
  1263. * @param {Item[]} orderedItems | Items ordered by start
  1264. * @param {function} comparator | -1 is lower, 0 is equal, 1 is higher
  1265. * @param {String} field
  1266. * @param {String} field2
  1267. * @returns {number}
  1268. * @private
  1269. */
  1270. exports.binarySearchCustom = function (orderedItems, comparator, field, field2) {
  1271. var maxIterations = 10000;
  1272. var iteration = 0;
  1273. var low = 0;
  1274. var high = orderedItems.length - 1;
  1275. while (low <= high && iteration < maxIterations) {
  1276. var middle = Math.floor((low + high) / 2);
  1277. var item = orderedItems[middle];
  1278. var value = (field2 === undefined) ? item[field] : item[field][field2];
  1279. var searchResult = comparator(value);
  1280. if (searchResult == 0) { // jihaa, found a visible item!
  1281. return middle;
  1282. }
  1283. else if (searchResult == -1) { // it is too small --> increase low
  1284. low = middle + 1;
  1285. }
  1286. else { // it is too big --> decrease high
  1287. high = middle - 1;
  1288. }
  1289. iteration++;
  1290. }
  1291. return -1;
  1292. };
  1293. /**
  1294. * This function does a binary search for a specific value in a sorted array. If it does not exist but is in between of
  1295. * two values, we return either the one before or the one after, depending on user input
  1296. * If it is found, we return the index, else -1.
  1297. *
  1298. * @param {Array} orderedItems
  1299. * @param {{start: number, end: number}} target
  1300. * @param {String} field
  1301. * @param {String} sidePreference 'before' or 'after'
  1302. * @param {function} comparator an optional comparator, returning -1,0,1 for <,==,>.
  1303. * @returns {number}
  1304. * @private
  1305. */
  1306. exports.binarySearchValue = function (orderedItems, target, field, sidePreference, comparator) {
  1307. var maxIterations = 10000;
  1308. var iteration = 0;
  1309. var low = 0;
  1310. var high = orderedItems.length - 1;
  1311. var prevValue, value, nextValue, middle;
  1312. var comparator = comparator != undefined ? comparator : function (a, b) {
  1313. return a == b ? 0 : a < b ? -1 : 1
  1314. };
  1315. while (low <= high && iteration < maxIterations) {
  1316. // get a new guess
  1317. middle = Math.floor(0.5 * (high + low));
  1318. prevValue = orderedItems[Math.max(0, middle - 1)][field];
  1319. value = orderedItems[middle][field];
  1320. nextValue = orderedItems[Math.min(orderedItems.length - 1, middle + 1)][field];
  1321. if (comparator(value, target) == 0) { // we found the target
  1322. return middle;
  1323. }
  1324. else if (comparator(prevValue, target) < 0 && comparator(value, target) > 0) { // target is in between of the previous and the current
  1325. return sidePreference == 'before' ? Math.max(0, middle - 1) : middle;
  1326. }
  1327. else if (comparator(value, target) < 0 && comparator(nextValue, target) > 0) { // target is in between of the current and the next
  1328. return sidePreference == 'before' ? middle : Math.min(orderedItems.length - 1, middle + 1);
  1329. }
  1330. else { // didnt find the target, we need to change our boundaries.
  1331. if (comparator(value, target) < 0) { // it is too small --> increase low
  1332. low = middle + 1;
  1333. }
  1334. else { // it is too big --> decrease high
  1335. high = middle - 1;
  1336. }
  1337. }
  1338. iteration++;
  1339. }
  1340. // didnt find anything. Return -1.
  1341. return -1;
  1342. };
  1343. /*
  1344. * Easing Functions - inspired from http://gizma.com/easing/
  1345. * only considering the t value for the range [0, 1] => [0, 1]
  1346. * https://gist.github.com/gre/1650294
  1347. */
  1348. exports.easingFunctions = {
  1349. // no easing, no acceleration
  1350. linear: function (t) {
  1351. return t
  1352. },
  1353. // accelerating from zero velocity
  1354. easeInQuad: function (t) {
  1355. return t * t
  1356. },
  1357. // decelerating to zero velocity
  1358. easeOutQuad: function (t) {
  1359. return t * (2 - t)
  1360. },
  1361. // acceleration until halfway, then deceleration
  1362. easeInOutQuad: function (t) {
  1363. return t < .5 ? 2 * t * t : -1 + (4 - 2 * t) * t
  1364. },
  1365. // accelerating from zero velocity
  1366. easeInCubic: function (t) {
  1367. return t * t * t
  1368. },
  1369. // decelerating to zero velocity
  1370. easeOutCubic: function (t) {
  1371. return (--t) * t * t + 1
  1372. },
  1373. // acceleration until halfway, then deceleration
  1374. easeInOutCubic: function (t) {
  1375. return t < .5 ? 4 * t * t * t : (t - 1) * (2 * t - 2) * (2 * t - 2) + 1
  1376. },
  1377. // accelerating from zero velocity
  1378. easeInQuart: function (t) {
  1379. return t * t * t * t
  1380. },
  1381. // decelerating to zero velocity
  1382. easeOutQuart: function (t) {
  1383. return 1 - (--t) * t * t * t
  1384. },
  1385. // acceleration until halfway, then deceleration
  1386. easeInOutQuart: function (t) {
  1387. return t < .5 ? 8 * t * t * t * t : 1 - 8 * (--t) * t * t * t
  1388. },
  1389. // accelerating from zero velocity
  1390. easeInQuint: function (t) {
  1391. return t * t * t * t * t
  1392. },
  1393. // decelerating to zero velocity
  1394. easeOutQuint: function (t) {
  1395. return 1 + (--t) * t * t * t * t
  1396. },
  1397. // acceleration until halfway, then deceleration
  1398. easeInOutQuint: function (t) {
  1399. return t < .5 ? 16 * t * t * t * t * t : 1 + 16 * (--t) * t * t * t * t
  1400. }
  1401. };
  1402. exports.getScrollBarWidth = function () {
  1403. var inner = document.createElement('p');
  1404. inner.style.width = "100%";
  1405. inner.style.height = "200px";
  1406. var outer = document.createElement('div');
  1407. outer.style.position = "absolute";
  1408. outer.style.top = "0px";
  1409. outer.style.left = "0px";
  1410. outer.style.visibility = "hidden";
  1411. outer.style.width = "200px";
  1412. outer.style.height = "150px";
  1413. outer.style.overflow = "hidden";
  1414. outer.appendChild (inner);
  1415. document.body.appendChild (outer);
  1416. var w1 = inner.offsetWidth;
  1417. outer.style.overflow = 'scroll';
  1418. var w2 = inner.offsetWidth;
  1419. if (w1 == w2) w2 = outer.clientWidth;
  1420. document.body.removeChild (outer);
  1421. return (w1 - w2);
  1422. };
  1423. exports.topMost = function (pile, accessors) {
  1424. let candidate;
  1425. if (!Array.isArray(accessors)) {
  1426. accessors = [accessors];
  1427. }
  1428. for (const member of pile) {
  1429. if (member) {
  1430. candidate = member[accessors[0]];
  1431. for (let i = 1; i < accessors.length; i++){
  1432. if (candidate) {
  1433. candidate = candidate[accessors[i]]
  1434. } else {
  1435. continue;
  1436. }
  1437. }
  1438. if (typeof candidate != 'undefined') {
  1439. break;
  1440. }
  1441. }
  1442. }
  1443. return candidate;
  1444. };