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.

6267 lines
190 KiB

11 years ago
11 years ago
11 years ago
  1. /**
  2. * vis.js
  3. * https://github.com/almende/vis
  4. *
  5. * A dynamic, browser-based visualization library.
  6. *
  7. * @version 0.0.1
  8. * @date 2013-04-16
  9. *
  10. * @license
  11. * Copyright (C) 2011-2013 Almende B.V, http://almende.com
  12. *
  13. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  14. * use this file except in compliance with the License. You may obtain a copy
  15. * of the License at
  16. *
  17. * http://www.apache.org/licenses/LICENSE-2.0
  18. *
  19. * Unless required by applicable law or agreed to in writing, software
  20. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  21. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  22. * License for the specific language governing permissions and limitations under
  23. * the License.
  24. */
  25. // create namespace
  26. var util = {};
  27. /**
  28. * Test whether given object is a number
  29. * @param {*} object
  30. * @return {Boolean} isNumber
  31. */
  32. util.isNumber = function isNumber(object) {
  33. return (object instanceof Number || typeof object == 'number');
  34. };
  35. /**
  36. * Test whether given object is a string
  37. * @param {*} object
  38. * @return {Boolean} isString
  39. */
  40. util.isString = function isString(object) {
  41. return (object instanceof String || typeof object == 'string');
  42. };
  43. /**
  44. * Test whether given object is a Date, or a String containing a Date
  45. * @param {Date | String} object
  46. * @return {Boolean} isDate
  47. */
  48. util.isDate = function isDate(object) {
  49. if (object instanceof Date) {
  50. return true;
  51. }
  52. else if (util.isString(object)) {
  53. // test whether this string contains a date
  54. var match = ASPDateRegex.exec(object);
  55. if (match) {
  56. return true;
  57. }
  58. else if (!isNaN(Date.parse(object))) {
  59. return true;
  60. }
  61. }
  62. return false;
  63. };
  64. /**
  65. * Test whether given object is an instance of google.visualization.DataTable
  66. * @param {*} object
  67. * @return {Boolean} isDataTable
  68. */
  69. util.isDataTable = function isDataTable(object) {
  70. return (typeof (google) !== 'undefined') &&
  71. (google.visualization) &&
  72. (google.visualization.DataTable) &&
  73. (object instanceof google.visualization.DataTable);
  74. };
  75. /**
  76. * Create a semi UUID
  77. * source: http://stackoverflow.com/a/105074/1262753
  78. * @return {String} uuid
  79. */
  80. util.randomUUID = function randomUUID () {
  81. var S4 = function () {
  82. return Math.floor(
  83. Math.random() * 0x10000 /* 65536 */
  84. ).toString(16);
  85. };
  86. return (
  87. S4() + S4() + '-' +
  88. S4() + '-' +
  89. S4() + '-' +
  90. S4() + '-' +
  91. S4() + S4() + S4()
  92. );
  93. };
  94. /**
  95. * Extend object a with the properties of object b
  96. * @param {Object} a
  97. * @param {Object} b
  98. * @return {Object} a
  99. */
  100. util.extend = function (a, b) {
  101. for (var prop in b) {
  102. if (b.hasOwnProperty(prop)) {
  103. a[prop] = b[prop];
  104. }
  105. }
  106. return a;
  107. };
  108. /**
  109. * Cast an object to another type
  110. * @param {Boolean | Number | String | Date | Null | undefined} object
  111. * @param {String | function | undefined} type Name of the type or a cast
  112. * function. Available types:
  113. * 'Boolean', 'Number', 'String',
  114. * 'Date', ISODate', 'ASPDate'.
  115. * @return {*} object
  116. * @throws Error
  117. */
  118. util.cast = function cast(object, type) {
  119. if (object === undefined) {
  120. return undefined;
  121. }
  122. if (object === null) {
  123. return null;
  124. }
  125. if (!type) {
  126. return object;
  127. }
  128. if (typeof type == 'function') {
  129. return type(object);
  130. }
  131. //noinspection FallthroughInSwitchStatementJS
  132. switch (type) {
  133. case 'boolean':
  134. case 'Boolean':
  135. return Boolean(object);
  136. case 'number':
  137. case 'Number':
  138. return Number(object);
  139. case 'string':
  140. case 'String':
  141. return String(object);
  142. case 'Date':
  143. if (util.isNumber(object)) {
  144. return new Date(object);
  145. }
  146. if (object instanceof Date) {
  147. return new Date(object.valueOf());
  148. }
  149. if (util.isString(object)) {
  150. // parse ASP.Net Date pattern,
  151. // for example '/Date(1198908717056)/' or '/Date(1198908717056-0700)/'
  152. // code from http://momentjs.com/
  153. var match = ASPDateRegex.exec(object);
  154. if (match) {
  155. return new Date(Number(match[1]));
  156. }
  157. else {
  158. return new Date(object);
  159. }
  160. }
  161. else {
  162. throw new Error(
  163. 'Cannot cast object of type ' + util.getType(object) +
  164. ' to type Date');
  165. }
  166. case 'ISODate':
  167. if (object instanceof Date) {
  168. return object.toISOString();
  169. }
  170. else if (util.isNumber(object) || util.isString(Object)) {
  171. return (new Date(object)).toISOString()
  172. }
  173. else {
  174. throw new Error(
  175. 'Cannot cast object of type ' + util.getType(object) +
  176. ' to type ISODate');
  177. }
  178. case 'ASPDate':
  179. if (object instanceof Date) {
  180. return '/Date(' + object.valueOf() + ')/';
  181. }
  182. else if (util.isNumber(object) || util.isString(Object)) {
  183. return '/Date(' + (new Date(object)).valueOf() + ')/';
  184. }
  185. else {
  186. throw new Error(
  187. 'Cannot cast object of type ' + util.getType(object) +
  188. ' to type ASPDate');
  189. }
  190. default:
  191. throw new Error('Cannot cast object of type ' + util.getType(object) +
  192. ' to type "' + type + '"');
  193. }
  194. };
  195. var ASPDateRegex = /^\/?Date\((\-?\d+)/i;
  196. /**
  197. * Get the type of an object
  198. * @param {*} object
  199. * @return {String} type
  200. */
  201. util.getType = function getType(object) {
  202. var type = typeof object;
  203. if (type == 'object') {
  204. if (object == null) {
  205. return 'null';
  206. }
  207. if (object && object.constructor && object.constructor.name) {
  208. return object.constructor.name;
  209. }
  210. }
  211. return type;
  212. };
  213. /**
  214. * Retrieve the absolute left value of a DOM element
  215. * @param {Element} elem A dom element, for example a div
  216. * @return {number} left The absolute left position of this element
  217. * in the browser page.
  218. */
  219. util.getAbsoluteLeft = function getAbsoluteLeft (elem) {
  220. var doc = document.documentElement;
  221. var body = document.body;
  222. var left = elem.offsetLeft;
  223. var e = elem.offsetParent;
  224. while (e != null && e != body && e != doc) {
  225. left += e.offsetLeft;
  226. left -= e.scrollLeft;
  227. e = e.offsetParent;
  228. }
  229. return left;
  230. };
  231. /**
  232. * Retrieve the absolute top value of a DOM element
  233. * @param {Element} elem A dom element, for example a div
  234. * @return {number} top The absolute top position of this element
  235. * in the browser page.
  236. */
  237. util.getAbsoluteTop = function getAbsoluteTop (elem) {
  238. var doc = document.documentElement;
  239. var body = document.body;
  240. var top = elem.offsetTop;
  241. var e = elem.offsetParent;
  242. while (e != null && e != body && e != doc) {
  243. top += e.offsetTop;
  244. top -= e.scrollTop;
  245. e = e.offsetParent;
  246. }
  247. return top;
  248. };
  249. /**
  250. * Get the absolute, vertical mouse position from an event.
  251. * @param {Event} event
  252. * @return {Number} pageY
  253. */
  254. util.getPageY = function getPageY (event) {
  255. if ('pageY' in event) {
  256. return event.pageY;
  257. }
  258. else {
  259. var clientY;
  260. if (('targetTouches' in event) && event.targetTouches.length) {
  261. clientY = event.targetTouches[0].clientY;
  262. }
  263. else {
  264. clientY = event.clientY;
  265. }
  266. var doc = document.documentElement;
  267. var body = document.body;
  268. return clientY +
  269. ( doc && doc.scrollTop || body && body.scrollTop || 0 ) -
  270. ( doc && doc.clientTop || body && body.clientTop || 0 );
  271. }
  272. };
  273. /**
  274. * Get the absolute, horizontal mouse position from an event.
  275. * @param {Event} event
  276. * @return {Number} pageX
  277. */
  278. util.getPageX = function getPageX (event) {
  279. if ('pageY' in event) {
  280. return event.pageX;
  281. }
  282. else {
  283. var clientX;
  284. if (('targetTouches' in event) && event.targetTouches.length) {
  285. clientX = event.targetTouches[0].clientX;
  286. }
  287. else {
  288. clientX = event.clientX;
  289. }
  290. var doc = document.documentElement;
  291. var body = document.body;
  292. return clientX +
  293. ( doc && doc.scrollLeft || body && body.scrollLeft || 0 ) -
  294. ( doc && doc.clientLeft || body && body.clientLeft || 0 );
  295. }
  296. };
  297. /**
  298. * add a className to the given elements style
  299. * @param {Element} elem
  300. * @param {String} className
  301. */
  302. util.addClassName = function addClassName(elem, className) {
  303. var classes = elem.className.split(' ');
  304. if (classes.indexOf(className) == -1) {
  305. classes.push(className); // add the class to the array
  306. elem.className = classes.join(' ');
  307. }
  308. };
  309. /**
  310. * add a className to the given elements style
  311. * @param {Element} elem
  312. * @param {String} className
  313. */
  314. util.removeClassName = function removeClassname(elem, className) {
  315. var classes = elem.className.split(' ');
  316. var index = classes.indexOf(className);
  317. if (index != -1) {
  318. classes.splice(index, 1); // remove the class from the array
  319. elem.className = classes.join(' ');
  320. }
  321. };
  322. /**
  323. * For each method for both arrays and objects.
  324. * In case of an array, the built-in Array.forEach() is applied.
  325. * In case of an Object, the method loops over all properties of the object.
  326. * @param {Object | Array} object An Object or Array
  327. * @param {function} callback Callback method, called for each item in
  328. * the object or array with three parameters:
  329. * callback(value, index, object)
  330. */
  331. util.forEach = function forEach (object, callback) {
  332. if (object instanceof Array) {
  333. // array
  334. object.forEach(callback);
  335. }
  336. else {
  337. // object
  338. for (var key in object) {
  339. if (object.hasOwnProperty(key)) {
  340. callback(object[key], key, object);
  341. }
  342. }
  343. }
  344. };
  345. /**
  346. * Update a property in an object
  347. * @param {Object} object
  348. * @param {String} key
  349. * @param {*} value
  350. * @return {Boolean} changed
  351. */
  352. util.updateProperty = function updateProp (object, key, value) {
  353. if (object[key] !== value) {
  354. object[key] = value;
  355. return true;
  356. }
  357. else {
  358. return false;
  359. }
  360. };
  361. /**
  362. * Add and event listener. Works for all browsers
  363. * @param {Element} element An html element
  364. * @param {string} action The action, for example "click",
  365. * without the prefix "on"
  366. * @param {function} listener The callback function to be executed
  367. * @param {boolean} [useCapture]
  368. */
  369. util.addEventListener = function addEventListener(element, action, listener, useCapture) {
  370. if (element.addEventListener) {
  371. if (useCapture === undefined)
  372. useCapture = false;
  373. if (action === "mousewheel" && navigator.userAgent.indexOf("Firefox") >= 0) {
  374. action = "DOMMouseScroll"; // For Firefox
  375. }
  376. element.addEventListener(action, listener, useCapture);
  377. } else {
  378. element.attachEvent("on" + action, listener); // IE browsers
  379. }
  380. };
  381. /**
  382. * Remove an event listener from an element
  383. * @param {Element} element An html dom element
  384. * @param {string} action The name of the event, for example "mousedown"
  385. * @param {function} listener The listener function
  386. * @param {boolean} [useCapture]
  387. */
  388. util.removeEventListener = function removeEventListener(element, action, listener, useCapture) {
  389. if (element.removeEventListener) {
  390. // non-IE browsers
  391. if (useCapture === undefined)
  392. useCapture = false;
  393. if (action === "mousewheel" && navigator.userAgent.indexOf("Firefox") >= 0) {
  394. action = "DOMMouseScroll"; // For Firefox
  395. }
  396. element.removeEventListener(action, listener, useCapture);
  397. } else {
  398. // IE browsers
  399. element.detachEvent("on" + action, listener);
  400. }
  401. };
  402. /**
  403. * Get HTML element which is the target of the event
  404. * @param {Event} event
  405. * @return {Element} target element
  406. */
  407. util.getTarget = function getTarget(event) {
  408. // code from http://www.quirksmode.org/js/events_properties.html
  409. if (!event) {
  410. event = window.event;
  411. }
  412. var target;
  413. if (event.target) {
  414. target = event.target;
  415. }
  416. else if (event.srcElement) {
  417. target = event.srcElement;
  418. }
  419. if (target.nodeType != undefined && target.nodeType == 3) {
  420. // defeat Safari bug
  421. target = target.parentNode;
  422. }
  423. return target;
  424. };
  425. /**
  426. * Stop event propagation
  427. */
  428. util.stopPropagation = function stopPropagation(event) {
  429. if (!event)
  430. event = window.event;
  431. if (event.stopPropagation) {
  432. event.stopPropagation(); // non-IE browsers
  433. }
  434. else {
  435. event.cancelBubble = true; // IE browsers
  436. }
  437. };
  438. /**
  439. * Cancels the event if it is cancelable, without stopping further propagation of the event.
  440. */
  441. util.preventDefault = function preventDefault (event) {
  442. if (!event)
  443. event = window.event;
  444. if (event.preventDefault) {
  445. event.preventDefault(); // non-IE browsers
  446. }
  447. else {
  448. event.returnValue = false; // IE browsers
  449. }
  450. };
  451. util.option = {};
  452. /**
  453. * Cast a value as boolean
  454. * @param {Boolean | function | undefined} value
  455. * @param {Boolean} [defaultValue]
  456. * @returns {Boolean} bool
  457. */
  458. util.option.asBoolean = function (value, defaultValue) {
  459. if (typeof value == 'function') {
  460. value = value();
  461. }
  462. if (value != null) {
  463. return (value != false);
  464. }
  465. return defaultValue || null;
  466. };
  467. /**
  468. * Cast a value as string
  469. * @param {String | function | undefined} value
  470. * @param {String} [defaultValue]
  471. * @returns {String} str
  472. */
  473. util.option.asString = function (value, defaultValue) {
  474. if (typeof value == 'function') {
  475. value = value();
  476. }
  477. if (value != null) {
  478. return String(value);
  479. }
  480. return defaultValue || null;
  481. };
  482. /**
  483. * Cast a size or location in pixels or a percentage
  484. * @param {String | Number | function | undefined} value
  485. * @param {String} [defaultValue]
  486. * @returns {String} size
  487. */
  488. util.option.asSize = function (value, defaultValue) {
  489. if (typeof value == 'function') {
  490. value = value();
  491. }
  492. if (util.isString(value)) {
  493. return value;
  494. }
  495. else if (util.isNumber(value)) {
  496. return value + 'px';
  497. }
  498. else {
  499. return defaultValue || null;
  500. }
  501. };
  502. /**
  503. * Cast a value as DOM element
  504. * @param {HTMLElement | function | undefined} value
  505. * @param {HTMLElement} [defaultValue]
  506. * @returns {HTMLElement | null} dom
  507. */
  508. util.option.asElement = function (value, defaultValue) {
  509. if (typeof value == 'function') {
  510. value = value();
  511. }
  512. return value || defaultValue || null;
  513. };
  514. // Internet Explorer 8 and older does not support Array.indexOf, so we define
  515. // it here in that case.
  516. // http://soledadpenades.com/2007/05/17/arrayindexof-in-internet-explorer/
  517. if(!Array.prototype.indexOf) {
  518. Array.prototype.indexOf = function(obj){
  519. for(var i = 0; i < this.length; i++){
  520. if(this[i] == obj){
  521. return i;
  522. }
  523. }
  524. return -1;
  525. };
  526. try {
  527. console.log("Warning: Ancient browser detected. Please update your browser");
  528. }
  529. catch (err) {
  530. }
  531. }
  532. // Internet Explorer 8 and older does not support Array.forEach, so we define
  533. // it here in that case.
  534. // https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Array/forEach
  535. if (!Array.prototype.forEach) {
  536. Array.prototype.forEach = function(fn, scope) {
  537. for(var i = 0, len = this.length; i < len; ++i) {
  538. fn.call(scope || this, this[i], i, this);
  539. }
  540. }
  541. }
  542. // Internet Explorer 8 and older does not support Array.map, so we define it
  543. // here in that case.
  544. // https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Array/map
  545. // Production steps of ECMA-262, Edition 5, 15.4.4.19
  546. // Reference: http://es5.github.com/#x15.4.4.19
  547. if (!Array.prototype.map) {
  548. Array.prototype.map = function(callback, thisArg) {
  549. var T, A, k;
  550. if (this == null) {
  551. throw new TypeError(" this is null or not defined");
  552. }
  553. // 1. Let O be the result of calling ToObject passing the |this| value as the argument.
  554. var O = Object(this);
  555. // 2. Let lenValue be the result of calling the Get internal method of O with the argument "length".
  556. // 3. Let len be ToUint32(lenValue).
  557. var len = O.length >>> 0;
  558. // 4. If IsCallable(callback) is false, throw a TypeError exception.
  559. // See: http://es5.github.com/#x9.11
  560. if (typeof callback !== "function") {
  561. throw new TypeError(callback + " is not a function");
  562. }
  563. // 5. If thisArg was supplied, let T be thisArg; else let T be undefined.
  564. if (thisArg) {
  565. T = thisArg;
  566. }
  567. // 6. Let A be a new array created as if by the expression new Array(len) where Array is
  568. // the standard built-in constructor with that name and len is the value of len.
  569. A = new Array(len);
  570. // 7. Let k be 0
  571. k = 0;
  572. // 8. Repeat, while k < len
  573. while(k < len) {
  574. var kValue, mappedValue;
  575. // a. Let Pk be ToString(k).
  576. // This is implicit for LHS operands of the in operator
  577. // b. Let kPresent be the result of calling the HasProperty internal method of O with argument Pk.
  578. // This step can be combined with c
  579. // c. If kPresent is true, then
  580. if (k in O) {
  581. // i. Let kValue be the result of calling the Get internal method of O with argument Pk.
  582. kValue = O[ k ];
  583. // ii. Let mappedValue be the result of calling the Call internal method of callback
  584. // with T as the this value and argument list containing kValue, k, and O.
  585. mappedValue = callback.call(T, kValue, k, O);
  586. // iii. Call the DefineOwnProperty internal method of A with arguments
  587. // Pk, Property Descriptor {Value: mappedValue, : true, Enumerable: true, Configurable: true},
  588. // and false.
  589. // In browsers that support Object.defineProperty, use the following:
  590. // Object.defineProperty(A, Pk, { value: mappedValue, writable: true, enumerable: true, configurable: true });
  591. // For best browser support, use the following:
  592. A[ k ] = mappedValue;
  593. }
  594. // d. Increase k by 1.
  595. k++;
  596. }
  597. // 9. return A
  598. return A;
  599. };
  600. }
  601. // Internet Explorer 8 and older does not support Array.filter, so we define it
  602. // here in that case.
  603. // https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Array/filter
  604. if (!Array.prototype.filter) {
  605. Array.prototype.filter = function(fun /*, thisp */) {
  606. "use strict";
  607. if (this == null) {
  608. throw new TypeError();
  609. }
  610. var t = Object(this);
  611. var len = t.length >>> 0;
  612. if (typeof fun != "function") {
  613. throw new TypeError();
  614. }
  615. var res = [];
  616. var thisp = arguments[1];
  617. for (var i = 0; i < len; i++) {
  618. if (i in t) {
  619. var val = t[i]; // in case fun mutates this
  620. if (fun.call(thisp, val, i, t))
  621. res.push(val);
  622. }
  623. }
  624. return res;
  625. };
  626. }
  627. // Internet Explorer 8 and older does not support Object.keys, so we define it
  628. // here in that case.
  629. // https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Object/keys
  630. if (!Object.keys) {
  631. Object.keys = (function () {
  632. var hasOwnProperty = Object.prototype.hasOwnProperty,
  633. hasDontEnumBug = !({toString: null}).propertyIsEnumerable('toString'),
  634. dontEnums = [
  635. 'toString',
  636. 'toLocaleString',
  637. 'valueOf',
  638. 'hasOwnProperty',
  639. 'isPrototypeOf',
  640. 'propertyIsEnumerable',
  641. 'constructor'
  642. ],
  643. dontEnumsLength = dontEnums.length;
  644. return function (obj) {
  645. if (typeof obj !== 'object' && typeof obj !== 'function' || obj === null) {
  646. throw new TypeError('Object.keys called on non-object');
  647. }
  648. var result = [];
  649. for (var prop in obj) {
  650. if (hasOwnProperty.call(obj, prop)) result.push(prop);
  651. }
  652. if (hasDontEnumBug) {
  653. for (var i=0; i < dontEnumsLength; i++) {
  654. if (hasOwnProperty.call(obj, dontEnums[i])) result.push(dontEnums[i]);
  655. }
  656. }
  657. return result;
  658. }
  659. })()
  660. }
  661. /**
  662. * Event listener (singleton)
  663. */
  664. var events = {
  665. 'listeners': [],
  666. /**
  667. * Find a single listener by its object
  668. * @param {Object} object
  669. * @return {Number} index -1 when not found
  670. */
  671. 'indexOf': function (object) {
  672. var listeners = this.listeners;
  673. for (var i = 0, iMax = this.listeners.length; i < iMax; i++) {
  674. var listener = listeners[i];
  675. if (listener && listener.object == object) {
  676. return i;
  677. }
  678. }
  679. return -1;
  680. },
  681. /**
  682. * Add an event listener
  683. * @param {Object} object
  684. * @param {String} event The name of an event, for example 'select'
  685. * @param {function} callback The callback method, called when the
  686. * event takes place
  687. */
  688. 'addListener': function (object, event, callback) {
  689. var index = this.indexOf(object);
  690. var listener = this.listeners[index];
  691. if (!listener) {
  692. listener = {
  693. 'object': object,
  694. 'events': {}
  695. };
  696. this.listeners.push(listener);
  697. }
  698. var callbacks = listener.events[event];
  699. if (!callbacks) {
  700. callbacks = [];
  701. listener.events[event] = callbacks;
  702. }
  703. // add the callback if it does not yet exist
  704. if (callbacks.indexOf(callback) == -1) {
  705. callbacks.push(callback);
  706. }
  707. },
  708. /**
  709. * Remove an event listener
  710. * @param {Object} object
  711. * @param {String} event The name of an event, for example 'select'
  712. * @param {function} callback The registered callback method
  713. */
  714. 'removeListener': function (object, event, callback) {
  715. var index = this.indexOf(object);
  716. var listener = this.listeners[index];
  717. if (listener) {
  718. var callbacks = listener.events[event];
  719. if (callbacks) {
  720. index = callbacks.indexOf(callback);
  721. if (index != -1) {
  722. callbacks.splice(index, 1);
  723. }
  724. // remove the array when empty
  725. if (callbacks.length == 0) {
  726. delete listener.events[event];
  727. }
  728. }
  729. // count the number of registered events. remove listener when empty
  730. var count = 0;
  731. var events = listener.events;
  732. for (var e in events) {
  733. if (events.hasOwnProperty(e)) {
  734. count++;
  735. }
  736. }
  737. if (count == 0) {
  738. delete this.listeners[index];
  739. }
  740. }
  741. },
  742. /**
  743. * Remove all registered event listeners
  744. */
  745. 'removeAllListeners': function () {
  746. this.listeners = [];
  747. },
  748. /**
  749. * Trigger an event. All registered event handlers will be called
  750. * @param {Object} object
  751. * @param {String} event
  752. * @param {Object} properties (optional)
  753. */
  754. 'trigger': function (object, event, properties) {
  755. var index = this.indexOf(object);
  756. var listener = this.listeners[index];
  757. if (listener) {
  758. var callbacks = listener.events[event];
  759. if (callbacks) {
  760. for (var i = 0, iMax = callbacks.length; i < iMax; i++) {
  761. callbacks[i](properties);
  762. }
  763. }
  764. }
  765. }
  766. };
  767. /**
  768. * @constructor TimeStep
  769. * The class TimeStep is an iterator for dates. You provide a start date and an
  770. * end date. The class itself determines the best scale (step size) based on the
  771. * provided start Date, end Date, and minimumStep.
  772. *
  773. * If minimumStep is provided, the step size is chosen as close as possible
  774. * to the minimumStep but larger than minimumStep. If minimumStep is not
  775. * provided, the scale is set to 1 DAY.
  776. * The minimumStep should correspond with the onscreen size of about 6 characters
  777. *
  778. * Alternatively, you can set a scale by hand.
  779. * After creation, you can initialize the class by executing first(). Then you
  780. * can iterate from the start date to the end date via next(). You can check if
  781. * the end date is reached with the function hasNext(). After each step, you can
  782. * retrieve the current date via getCurrent().
  783. * The TimeStep has scales ranging from milliseconds, seconds, minutes, hours,
  784. * days, to years.
  785. *
  786. * Version: 1.2
  787. *
  788. * @param {Date} [start] The start date, for example new Date(2010, 9, 21)
  789. * or new Date(2010, 9, 21, 23, 45, 00)
  790. * @param {Date} [end] The end date
  791. * @param {Number} [minimumStep] Optional. Minimum step size in milliseconds
  792. */
  793. TimeStep = function(start, end, minimumStep) {
  794. // variables
  795. this.current = new Date();
  796. this._start = new Date();
  797. this._end = new Date();
  798. this.autoScale = true;
  799. this.scale = TimeStep.SCALE.DAY;
  800. this.step = 1;
  801. // initialize the range
  802. this.setRange(start, end, minimumStep);
  803. };
  804. /// enum scale
  805. TimeStep.SCALE = {
  806. MILLISECOND: 1,
  807. SECOND: 2,
  808. MINUTE: 3,
  809. HOUR: 4,
  810. DAY: 5,
  811. WEEKDAY: 6,
  812. MONTH: 7,
  813. YEAR: 8
  814. };
  815. /**
  816. * Set a new range
  817. * If minimumStep is provided, the step size is chosen as close as possible
  818. * to the minimumStep but larger than minimumStep. If minimumStep is not
  819. * provided, the scale is set to 1 DAY.
  820. * The minimumStep should correspond with the onscreen size of about 6 characters
  821. * @param {Date} start The start date and time.
  822. * @param {Date} end The end date and time.
  823. * @param {int} [minimumStep] Optional. Minimum step size in milliseconds
  824. */
  825. TimeStep.prototype.setRange = function(start, end, minimumStep) {
  826. if (!(start instanceof Date) || !(end instanceof Date)) {
  827. //throw "No legal start or end date in method setRange";
  828. return;
  829. }
  830. this._start = (start != undefined) ? new Date(start.valueOf()) : new Date();
  831. this._end = (end != undefined) ? new Date(end.valueOf()) : new Date();
  832. if (this.autoScale) {
  833. this.setMinimumStep(minimumStep);
  834. }
  835. };
  836. /**
  837. * Set the range iterator to the start date.
  838. */
  839. TimeStep.prototype.first = function() {
  840. this.current = new Date(this._start.valueOf());
  841. this.roundToMinor();
  842. };
  843. /**
  844. * Round the current date to the first minor date value
  845. * This must be executed once when the current date is set to start Date
  846. */
  847. TimeStep.prototype.roundToMinor = function() {
  848. // round to floor
  849. // IMPORTANT: we have no breaks in this switch! (this is no bug)
  850. //noinspection FallthroughInSwitchStatementJS
  851. switch (this.scale) {
  852. case TimeStep.SCALE.YEAR:
  853. this.current.setFullYear(this.step * Math.floor(this.current.getFullYear() / this.step));
  854. this.current.setMonth(0);
  855. case TimeStep.SCALE.MONTH: this.current.setDate(1);
  856. case TimeStep.SCALE.DAY: // intentional fall through
  857. case TimeStep.SCALE.WEEKDAY: this.current.setHours(0);
  858. case TimeStep.SCALE.HOUR: this.current.setMinutes(0);
  859. case TimeStep.SCALE.MINUTE: this.current.setSeconds(0);
  860. case TimeStep.SCALE.SECOND: this.current.setMilliseconds(0);
  861. //case TimeStep.SCALE.MILLISECOND: // nothing to do for milliseconds
  862. }
  863. if (this.step != 1) {
  864. // round down to the first minor value that is a multiple of the current step size
  865. switch (this.scale) {
  866. case TimeStep.SCALE.MILLISECOND: this.current.setMilliseconds(this.current.getMilliseconds() - this.current.getMilliseconds() % this.step); break;
  867. case TimeStep.SCALE.SECOND: this.current.setSeconds(this.current.getSeconds() - this.current.getSeconds() % this.step); break;
  868. case TimeStep.SCALE.MINUTE: this.current.setMinutes(this.current.getMinutes() - this.current.getMinutes() % this.step); break;
  869. case TimeStep.SCALE.HOUR: this.current.setHours(this.current.getHours() - this.current.getHours() % this.step); break;
  870. case TimeStep.SCALE.WEEKDAY: // intentional fall through
  871. case TimeStep.SCALE.DAY: this.current.setDate((this.current.getDate()-1) - (this.current.getDate()-1) % this.step + 1); break;
  872. case TimeStep.SCALE.MONTH: this.current.setMonth(this.current.getMonth() - this.current.getMonth() % this.step); break;
  873. case TimeStep.SCALE.YEAR: this.current.setFullYear(this.current.getFullYear() - this.current.getFullYear() % this.step); break;
  874. default: break;
  875. }
  876. }
  877. };
  878. /**
  879. * Check if the there is a next step
  880. * @return {boolean} true if the current date has not passed the end date
  881. */
  882. TimeStep.prototype.hasNext = function () {
  883. return (this.current.valueOf() <= this._end.valueOf());
  884. };
  885. /**
  886. * Do the next step
  887. */
  888. TimeStep.prototype.next = function() {
  889. var prev = this.current.valueOf();
  890. // Two cases, needed to prevent issues with switching daylight savings
  891. // (end of March and end of October)
  892. if (this.current.getMonth() < 6) {
  893. switch (this.scale) {
  894. case TimeStep.SCALE.MILLISECOND:
  895. this.current = new Date(this.current.valueOf() + this.step); break;
  896. case TimeStep.SCALE.SECOND: this.current = new Date(this.current.valueOf() + this.step * 1000); break;
  897. case TimeStep.SCALE.MINUTE: this.current = new Date(this.current.valueOf() + this.step * 1000 * 60); break;
  898. case TimeStep.SCALE.HOUR:
  899. this.current = new Date(this.current.valueOf() + this.step * 1000 * 60 * 60);
  900. // in case of skipping an hour for daylight savings, adjust the hour again (else you get: 0h 5h 9h ... instead of 0h 4h 8h ...)
  901. var h = this.current.getHours();
  902. this.current.setHours(h - (h % this.step));
  903. break;
  904. case TimeStep.SCALE.WEEKDAY: // intentional fall through
  905. case TimeStep.SCALE.DAY: this.current.setDate(this.current.getDate() + this.step); break;
  906. case TimeStep.SCALE.MONTH: this.current.setMonth(this.current.getMonth() + this.step); break;
  907. case TimeStep.SCALE.YEAR: this.current.setFullYear(this.current.getFullYear() + this.step); break;
  908. default: break;
  909. }
  910. }
  911. else {
  912. switch (this.scale) {
  913. case TimeStep.SCALE.MILLISECOND: this.current = new Date(this.current.valueOf() + this.step); break;
  914. case TimeStep.SCALE.SECOND: this.current.setSeconds(this.current.getSeconds() + this.step); break;
  915. case TimeStep.SCALE.MINUTE: this.current.setMinutes(this.current.getMinutes() + this.step); break;
  916. case TimeStep.SCALE.HOUR: this.current.setHours(this.current.getHours() + this.step); break;
  917. case TimeStep.SCALE.WEEKDAY: // intentional fall through
  918. case TimeStep.SCALE.DAY: this.current.setDate(this.current.getDate() + this.step); break;
  919. case TimeStep.SCALE.MONTH: this.current.setMonth(this.current.getMonth() + this.step); break;
  920. case TimeStep.SCALE.YEAR: this.current.setFullYear(this.current.getFullYear() + this.step); break;
  921. default: break;
  922. }
  923. }
  924. if (this.step != 1) {
  925. // round down to the correct major value
  926. switch (this.scale) {
  927. case TimeStep.SCALE.MILLISECOND: if(this.current.getMilliseconds() < this.step) this.current.setMilliseconds(0); break;
  928. case TimeStep.SCALE.SECOND: if(this.current.getSeconds() < this.step) this.current.setSeconds(0); break;
  929. case TimeStep.SCALE.MINUTE: if(this.current.getMinutes() < this.step) this.current.setMinutes(0); break;
  930. case TimeStep.SCALE.HOUR: if(this.current.getHours() < this.step) this.current.setHours(0); break;
  931. case TimeStep.SCALE.WEEKDAY: // intentional fall through
  932. case TimeStep.SCALE.DAY: if(this.current.getDate() < this.step+1) this.current.setDate(1); break;
  933. case TimeStep.SCALE.MONTH: if(this.current.getMonth() < this.step) this.current.setMonth(0); break;
  934. case TimeStep.SCALE.YEAR: break; // nothing to do for year
  935. default: break;
  936. }
  937. }
  938. // safety mechanism: if current time is still unchanged, move to the end
  939. if (this.current.valueOf() == prev) {
  940. this.current = new Date(this._end.valueOf());
  941. }
  942. };
  943. /**
  944. * Get the current datetime
  945. * @return {Date} current The current date
  946. */
  947. TimeStep.prototype.getCurrent = function() {
  948. return this.current;
  949. };
  950. /**
  951. * Set a custom scale. Autoscaling will be disabled.
  952. * For example setScale(SCALE.MINUTES, 5) will result
  953. * in minor steps of 5 minutes, and major steps of an hour.
  954. *
  955. * @param {TimeStep.SCALE} newScale
  956. * A scale. Choose from SCALE.MILLISECOND,
  957. * SCALE.SECOND, SCALE.MINUTE, SCALE.HOUR,
  958. * SCALE.WEEKDAY, SCALE.DAY, SCALE.MONTH,
  959. * SCALE.YEAR.
  960. * @param {Number} newStep A step size, by default 1. Choose for
  961. * example 1, 2, 5, or 10.
  962. */
  963. TimeStep.prototype.setScale = function(newScale, newStep) {
  964. this.scale = newScale;
  965. if (newStep > 0) {
  966. this.step = newStep;
  967. }
  968. this.autoScale = false;
  969. };
  970. /**
  971. * Enable or disable autoscaling
  972. * @param {boolean} enable If true, autoascaling is set true
  973. */
  974. TimeStep.prototype.setAutoScale = function (enable) {
  975. this.autoScale = enable;
  976. };
  977. /**
  978. * Automatically determine the scale that bests fits the provided minimum step
  979. * @param {Number} minimumStep The minimum step size in milliseconds
  980. */
  981. TimeStep.prototype.setMinimumStep = function(minimumStep) {
  982. if (minimumStep == undefined) {
  983. return;
  984. }
  985. var stepYear = (1000 * 60 * 60 * 24 * 30 * 12);
  986. var stepMonth = (1000 * 60 * 60 * 24 * 30);
  987. var stepDay = (1000 * 60 * 60 * 24);
  988. var stepHour = (1000 * 60 * 60);
  989. var stepMinute = (1000 * 60);
  990. var stepSecond = (1000);
  991. var stepMillisecond= (1);
  992. // find the smallest step that is larger than the provided minimumStep
  993. if (stepYear*1000 > minimumStep) {this.scale = TimeStep.SCALE.YEAR; this.step = 1000;}
  994. if (stepYear*500 > minimumStep) {this.scale = TimeStep.SCALE.YEAR; this.step = 500;}
  995. if (stepYear*100 > minimumStep) {this.scale = TimeStep.SCALE.YEAR; this.step = 100;}
  996. if (stepYear*50 > minimumStep) {this.scale = TimeStep.SCALE.YEAR; this.step = 50;}
  997. if (stepYear*10 > minimumStep) {this.scale = TimeStep.SCALE.YEAR; this.step = 10;}
  998. if (stepYear*5 > minimumStep) {this.scale = TimeStep.SCALE.YEAR; this.step = 5;}
  999. if (stepYear > minimumStep) {this.scale = TimeStep.SCALE.YEAR; this.step = 1;}
  1000. if (stepMonth*3 > minimumStep) {this.scale = TimeStep.SCALE.MONTH; this.step = 3;}
  1001. if (stepMonth > minimumStep) {this.scale = TimeStep.SCALE.MONTH; this.step = 1;}
  1002. if (stepDay*5 > minimumStep) {this.scale = TimeStep.SCALE.DAY; this.step = 5;}
  1003. if (stepDay*2 > minimumStep) {this.scale = TimeStep.SCALE.DAY; this.step = 2;}
  1004. if (stepDay > minimumStep) {this.scale = TimeStep.SCALE.DAY; this.step = 1;}
  1005. if (stepDay/2 > minimumStep) {this.scale = TimeStep.SCALE.WEEKDAY; this.step = 1;}
  1006. if (stepHour*4 > minimumStep) {this.scale = TimeStep.SCALE.HOUR; this.step = 4;}
  1007. if (stepHour > minimumStep) {this.scale = TimeStep.SCALE.HOUR; this.step = 1;}
  1008. if (stepMinute*15 > minimumStep) {this.scale = TimeStep.SCALE.MINUTE; this.step = 15;}
  1009. if (stepMinute*10 > minimumStep) {this.scale = TimeStep.SCALE.MINUTE; this.step = 10;}
  1010. if (stepMinute*5 > minimumStep) {this.scale = TimeStep.SCALE.MINUTE; this.step = 5;}
  1011. if (stepMinute > minimumStep) {this.scale = TimeStep.SCALE.MINUTE; this.step = 1;}
  1012. if (stepSecond*15 > minimumStep) {this.scale = TimeStep.SCALE.SECOND; this.step = 15;}
  1013. if (stepSecond*10 > minimumStep) {this.scale = TimeStep.SCALE.SECOND; this.step = 10;}
  1014. if (stepSecond*5 > minimumStep) {this.scale = TimeStep.SCALE.SECOND; this.step = 5;}
  1015. if (stepSecond > minimumStep) {this.scale = TimeStep.SCALE.SECOND; this.step = 1;}
  1016. if (stepMillisecond*200 > minimumStep) {this.scale = TimeStep.SCALE.MILLISECOND; this.step = 200;}
  1017. if (stepMillisecond*100 > minimumStep) {this.scale = TimeStep.SCALE.MILLISECOND; this.step = 100;}
  1018. if (stepMillisecond*50 > minimumStep) {this.scale = TimeStep.SCALE.MILLISECOND; this.step = 50;}
  1019. if (stepMillisecond*10 > minimumStep) {this.scale = TimeStep.SCALE.MILLISECOND; this.step = 10;}
  1020. if (stepMillisecond*5 > minimumStep) {this.scale = TimeStep.SCALE.MILLISECOND; this.step = 5;}
  1021. if (stepMillisecond > minimumStep) {this.scale = TimeStep.SCALE.MILLISECOND; this.step = 1;}
  1022. };
  1023. /**
  1024. * Snap a date to a rounded value. The snap intervals are dependent on the
  1025. * current scale and step.
  1026. * @param {Date} date the date to be snapped
  1027. */
  1028. TimeStep.prototype.snap = function(date) {
  1029. if (this.scale == TimeStep.SCALE.YEAR) {
  1030. var year = date.getFullYear() + Math.round(date.getMonth() / 12);
  1031. date.setFullYear(Math.round(year / this.step) * this.step);
  1032. date.setMonth(0);
  1033. date.setDate(0);
  1034. date.setHours(0);
  1035. date.setMinutes(0);
  1036. date.setSeconds(0);
  1037. date.setMilliseconds(0);
  1038. }
  1039. else if (this.scale == TimeStep.SCALE.MONTH) {
  1040. if (date.getDate() > 15) {
  1041. date.setDate(1);
  1042. date.setMonth(date.getMonth() + 1);
  1043. // important: first set Date to 1, after that change the month.
  1044. }
  1045. else {
  1046. date.setDate(1);
  1047. }
  1048. date.setHours(0);
  1049. date.setMinutes(0);
  1050. date.setSeconds(0);
  1051. date.setMilliseconds(0);
  1052. }
  1053. else if (this.scale == TimeStep.SCALE.DAY ||
  1054. this.scale == TimeStep.SCALE.WEEKDAY) {
  1055. //noinspection FallthroughInSwitchStatementJS
  1056. switch (this.step) {
  1057. case 5:
  1058. case 2:
  1059. date.setHours(Math.round(date.getHours() / 24) * 24); break;
  1060. default:
  1061. date.setHours(Math.round(date.getHours() / 12) * 12); break;
  1062. }
  1063. date.setMinutes(0);
  1064. date.setSeconds(0);
  1065. date.setMilliseconds(0);
  1066. }
  1067. else if (this.scale == TimeStep.SCALE.HOUR) {
  1068. switch (this.step) {
  1069. case 4:
  1070. date.setMinutes(Math.round(date.getMinutes() / 60) * 60); break;
  1071. default:
  1072. date.setMinutes(Math.round(date.getMinutes() / 30) * 30); break;
  1073. }
  1074. date.setSeconds(0);
  1075. date.setMilliseconds(0);
  1076. } else if (this.scale == TimeStep.SCALE.MINUTE) {
  1077. //noinspection FallthroughInSwitchStatementJS
  1078. switch (this.step) {
  1079. case 15:
  1080. case 10:
  1081. date.setMinutes(Math.round(date.getMinutes() / 5) * 5);
  1082. date.setSeconds(0);
  1083. break;
  1084. case 5:
  1085. date.setSeconds(Math.round(date.getSeconds() / 60) * 60); break;
  1086. default:
  1087. date.setSeconds(Math.round(date.getSeconds() / 30) * 30); break;
  1088. }
  1089. date.setMilliseconds(0);
  1090. }
  1091. else if (this.scale == TimeStep.SCALE.SECOND) {
  1092. //noinspection FallthroughInSwitchStatementJS
  1093. switch (this.step) {
  1094. case 15:
  1095. case 10:
  1096. date.setSeconds(Math.round(date.getSeconds() / 5) * 5);
  1097. date.setMilliseconds(0);
  1098. break;
  1099. case 5:
  1100. date.setMilliseconds(Math.round(date.getMilliseconds() / 1000) * 1000); break;
  1101. default:
  1102. date.setMilliseconds(Math.round(date.getMilliseconds() / 500) * 500); break;
  1103. }
  1104. }
  1105. else if (this.scale == TimeStep.SCALE.MILLISECOND) {
  1106. var step = this.step > 5 ? this.step / 2 : 1;
  1107. date.setMilliseconds(Math.round(date.getMilliseconds() / step) * step);
  1108. }
  1109. };
  1110. /**
  1111. * Check if the current value is a major value (for example when the step
  1112. * is DAY, a major value is each first day of the MONTH)
  1113. * @return {boolean} true if current date is major, else false.
  1114. */
  1115. TimeStep.prototype.isMajor = function() {
  1116. switch (this.scale) {
  1117. case TimeStep.SCALE.MILLISECOND:
  1118. return (this.current.getMilliseconds() == 0);
  1119. case TimeStep.SCALE.SECOND:
  1120. return (this.current.getSeconds() == 0);
  1121. case TimeStep.SCALE.MINUTE:
  1122. return (this.current.getHours() == 0) && (this.current.getMinutes() == 0);
  1123. // Note: this is no bug. Major label is equal for both minute and hour scale
  1124. case TimeStep.SCALE.HOUR:
  1125. return (this.current.getHours() == 0);
  1126. case TimeStep.SCALE.WEEKDAY: // intentional fall through
  1127. case TimeStep.SCALE.DAY:
  1128. return (this.current.getDate() == 1);
  1129. case TimeStep.SCALE.MONTH:
  1130. return (this.current.getMonth() == 0);
  1131. case TimeStep.SCALE.YEAR:
  1132. return false;
  1133. default:
  1134. return false;
  1135. }
  1136. };
  1137. /**
  1138. * Returns formatted text for the minor axislabel, depending on the current
  1139. * date and the scale. For example when scale is MINUTE, the current time is
  1140. * formatted as "hh:mm".
  1141. * @param {Date} [date] custom date. if not provided, current date is taken
  1142. */
  1143. TimeStep.prototype.getLabelMinor = function(date) {
  1144. if (date == undefined) {
  1145. date = this.current;
  1146. }
  1147. switch (this.scale) {
  1148. case TimeStep.SCALE.MILLISECOND: return moment(date).format('SSS');
  1149. case TimeStep.SCALE.SECOND: return moment(date).format('s');
  1150. case TimeStep.SCALE.MINUTE: return moment(date).format('HH:mm');
  1151. case TimeStep.SCALE.HOUR: return moment(date).format('HH:mm');
  1152. case TimeStep.SCALE.WEEKDAY: return moment(date).format('ddd D');
  1153. case TimeStep.SCALE.DAY: return moment(date).format('D');
  1154. case TimeStep.SCALE.MONTH: return moment(date).format('MMM');
  1155. case TimeStep.SCALE.YEAR: return moment(date).format('YYYY');
  1156. default: return '';
  1157. }
  1158. };
  1159. /**
  1160. * Returns formatted text for the major axislabel, depending on the current
  1161. * date and the scale. For example when scale is MINUTE, the major scale is
  1162. * hours, and the hour will be formatted as "hh".
  1163. * @param {Date} [date] custom date. if not provided, current date is taken
  1164. */
  1165. TimeStep.prototype.getLabelMajor = function(date) {
  1166. if (date == undefined) {
  1167. date = this.current;
  1168. }
  1169. //noinspection FallthroughInSwitchStatementJS
  1170. switch (this.scale) {
  1171. case TimeStep.SCALE.MILLISECOND:return moment(date).format('HH:mm:ss');
  1172. case TimeStep.SCALE.SECOND: return moment(date).format('D MMMM HH:mm');
  1173. case TimeStep.SCALE.MINUTE:
  1174. case TimeStep.SCALE.HOUR: return moment(date).format('ddd D MMMM');
  1175. case TimeStep.SCALE.WEEKDAY:
  1176. case TimeStep.SCALE.DAY: return moment(date).format('MMMM YYYY');
  1177. case TimeStep.SCALE.MONTH: return moment(date).format('YYYY');
  1178. case TimeStep.SCALE.YEAR: return '';
  1179. default: return '';
  1180. }
  1181. };
  1182. /**
  1183. * DataSet
  1184. *
  1185. * Usage:
  1186. * var dataSet = new DataSet({
  1187. * fieldId: '_id',
  1188. * fieldTypes: {
  1189. * // ...
  1190. * }
  1191. * });
  1192. *
  1193. * dataSet.add(item);
  1194. * dataSet.add(data);
  1195. * dataSet.update(item);
  1196. * dataSet.update(data);
  1197. * dataSet.remove(id);
  1198. * dataSet.remove(ids);
  1199. * var data = dataSet.get();
  1200. * var data = dataSet.get(id);
  1201. * var data = dataSet.get(ids);
  1202. * var data = dataSet.get(ids, options, data);
  1203. * dataSet.clear();
  1204. *
  1205. * A data set can:
  1206. * - add/remove/update data
  1207. * - gives triggers upon changes in the data
  1208. * - can import/export data in various data formats
  1209. * @param {Object} [options] Available options:
  1210. * {String} fieldId Field name of the id in the
  1211. * items, 'id' by default.
  1212. * {Object.<String, String} fieldTypes
  1213. * A map with field names as key,
  1214. * and the field type as value.
  1215. */
  1216. function DataSet (options) {
  1217. var me = this;
  1218. this.options = options || {};
  1219. this.data = {}; // map with data indexed by id
  1220. this.fieldId = this.options.fieldId || 'id'; // name of the field containing id
  1221. this.fieldTypes = {}; // field types by field name
  1222. if (this.options.fieldTypes) {
  1223. util.forEach(this.options.fieldTypes, function (value, field) {
  1224. if (value == 'Date' || value == 'ISODate' || value == 'ASPDate') {
  1225. me.fieldTypes[field] = 'Date';
  1226. }
  1227. else {
  1228. me.fieldTypes[field] = value;
  1229. }
  1230. });
  1231. }
  1232. // event subscribers
  1233. this.subscribers = {};
  1234. this.internalIds = {}; // internally generated id's
  1235. }
  1236. /**
  1237. * Subscribe to an event, add an event listener
  1238. * @param {String} event Event name. Available events: 'put', 'update',
  1239. * 'remove'
  1240. * @param {function} callback Callback method. Called with three parameters:
  1241. * {String} event
  1242. * {Object | null} params
  1243. * {String} senderId
  1244. * @param {String} [id] Optional id for the sender, used to filter
  1245. * events triggered by the sender itself.
  1246. */
  1247. DataSet.prototype.subscribe = function (event, callback, id) {
  1248. var subscribers = this.subscribers[event];
  1249. if (!subscribers) {
  1250. subscribers = [];
  1251. this.subscribers[event] = subscribers;
  1252. }
  1253. subscribers.push({
  1254. id: id ? String(id) : null,
  1255. callback: callback
  1256. });
  1257. };
  1258. /**
  1259. * Unsubscribe from an event, remove an event listener
  1260. * @param {String} event
  1261. * @param {function} callback
  1262. */
  1263. DataSet.prototype.unsubscribe = function (event, callback) {
  1264. var subscribers = this.subscribers[event];
  1265. if (subscribers) {
  1266. this.subscribers[event] = subscribers.filter(function (listener) {
  1267. return (listener.callback != callback);
  1268. });
  1269. }
  1270. };
  1271. /**
  1272. * Trigger an event
  1273. * @param {String} event
  1274. * @param {Object | null} params
  1275. * @param {String} [senderId] Optional id of the sender. The event will
  1276. * be triggered for all subscribers except the
  1277. * sender itself.
  1278. * @private
  1279. */
  1280. DataSet.prototype._trigger = function (event, params, senderId) {
  1281. if (event == '*') {
  1282. throw new Error('Cannot trigger event *');
  1283. }
  1284. var subscribers = [];
  1285. if (event in this.subscribers) {
  1286. subscribers = subscribers.concat(this.subscribers[event]);
  1287. }
  1288. if ('*' in this.subscribers) {
  1289. subscribers = subscribers.concat(this.subscribers['*']);
  1290. }
  1291. subscribers.forEach(function (listener) {
  1292. if (listener.id != senderId && listener.callback) {
  1293. listener.callback(event, params, senderId || null);
  1294. }
  1295. });
  1296. };
  1297. /**
  1298. * Add data. Existing items with the same id will be overwritten.
  1299. * @param {Object | Array | DataTable} data
  1300. * @param {String} [senderId] Optional sender id, used to trigger events for
  1301. * all but this sender's event subscribers.
  1302. */
  1303. DataSet.prototype.add = function (data, senderId) {
  1304. var items = [],
  1305. id,
  1306. me = this;
  1307. if (data instanceof Array) {
  1308. // Array
  1309. data.forEach(function (item) {
  1310. var id = me._addItem(item);
  1311. items.push(id);
  1312. });
  1313. }
  1314. else if (util.isDataTable(data)) {
  1315. // Google DataTable
  1316. var columns = this._getColumnNames(data);
  1317. for (var row = 0, rows = data.getNumberOfRows(); row < rows; row++) {
  1318. var item = {};
  1319. columns.forEach(function (field, col) {
  1320. item[field] = data.getValue(row, col);
  1321. });
  1322. id = me._addItem(item);
  1323. items.push(id);
  1324. }
  1325. }
  1326. else if (data instanceof Object) {
  1327. // Single item
  1328. id = me._addItem(data);
  1329. items.push(id);
  1330. }
  1331. else {
  1332. throw new Error('Unknown dataType');
  1333. }
  1334. this._trigger('add', {items: items}, senderId);
  1335. };
  1336. /**
  1337. * Update existing items. Items with the same id will be merged
  1338. * @param {Object | Array | DataTable} data
  1339. * @param {String} [senderId] Optional sender id, used to trigger events for
  1340. * all but this sender's event subscribers.
  1341. */
  1342. DataSet.prototype.update = function (data, senderId) {
  1343. var items = [],
  1344. id,
  1345. me = this;
  1346. if (data instanceof Array) {
  1347. // Array
  1348. data.forEach(function (item) {
  1349. var id = me._updateItem(item);
  1350. items.push(id);
  1351. });
  1352. }
  1353. else if (util.isDataTable(data)) {
  1354. // Google DataTable
  1355. var columns = this._getColumnNames(data);
  1356. for (var row = 0, rows = data.getNumberOfRows(); row < rows; row++) {
  1357. var item = {};
  1358. columns.forEach(function (field, col) {
  1359. item[field] = data.getValue(row, col);
  1360. });
  1361. id = me._updateItem(item);
  1362. items.push(id);
  1363. }
  1364. }
  1365. else if (data instanceof Object) {
  1366. // Single item
  1367. id = me._updateItem(data);
  1368. items.push(id);
  1369. }
  1370. else {
  1371. throw new Error('Unknown dataType');
  1372. }
  1373. this._trigger('update', {items: items}, senderId);
  1374. };
  1375. /**
  1376. * Get a data item or multiple items
  1377. * @param {String | Number | Array | Object} [ids] Id of a single item, or an
  1378. * array with multiple id's, or
  1379. * undefined or an Object with options
  1380. * to retrieve all data.
  1381. * @param {Object} [options] Available options:
  1382. * {String} [type]
  1383. * 'DataTable' or 'Array' (default)
  1384. * {Object.<String, String>} [fieldTypes]
  1385. * {String[]} [fields] filter fields
  1386. * @param {Array | DataTable} [data] If provided, items will be appended
  1387. * to this array or table. Required
  1388. * in case of Google DataTable
  1389. * @return {Array | Object | DataTable | null} data
  1390. * @throws Error
  1391. */
  1392. DataSet.prototype.get = function (ids, options, data) {
  1393. var me = this;
  1394. // shift arguments when first argument contains the options
  1395. if (util.getType(ids) == 'Object') {
  1396. data = options;
  1397. options = ids;
  1398. ids = undefined;
  1399. }
  1400. // merge field types
  1401. var fieldTypes = {};
  1402. if (this.options && this.options.fieldTypes) {
  1403. util.forEach(this.options.fieldTypes, function (value, field) {
  1404. fieldTypes[field] = value;
  1405. });
  1406. }
  1407. if (options && options.fieldTypes) {
  1408. util.forEach(options.fieldTypes, function (value, field) {
  1409. fieldTypes[field] = value;
  1410. });
  1411. }
  1412. var fields = options ? options.fields : undefined;
  1413. // determine the return type
  1414. var type;
  1415. if (options && options.type) {
  1416. type = (options.type == 'DataTable') ? 'DataTable' : 'Array';
  1417. if (data && (type != util.getType(data))) {
  1418. throw new Error('Type of parameter "data" (' + util.getType(data) + ') ' +
  1419. 'does not correspond with specified options.type (' + options.type + ')');
  1420. }
  1421. if (type == 'DataTable' && !util.isDataTable(data)) {
  1422. throw new Error('Parameter "data" must be a DataTable ' +
  1423. 'when options.type is "DataTable"');
  1424. }
  1425. }
  1426. else if (data) {
  1427. type = (util.getType(data) == 'DataTable') ? 'DataTable' : 'Array';
  1428. }
  1429. else {
  1430. type = 'Array';
  1431. }
  1432. if (type == 'DataTable') {
  1433. // return a Google DataTable
  1434. var columns = this._getColumnNames(data);
  1435. if (ids == undefined) {
  1436. // return all data
  1437. util.forEach(this.data, function (item) {
  1438. me._appendRow(data, columns, me._castItem(item));
  1439. });
  1440. }
  1441. else if (util.isNumber(ids) || util.isString(ids)) {
  1442. var item = me._castItem(me.data[ids], fieldTypes, fields);
  1443. this._appendRow(data, columns, item);
  1444. }
  1445. else if (ids instanceof Array) {
  1446. ids.forEach(function (id) {
  1447. var item = me._castItem(me.data[id], fieldTypes, fields);
  1448. me._appendRow(data, columns, item);
  1449. });
  1450. }
  1451. else {
  1452. throw new TypeError('Parameter "ids" must be ' +
  1453. 'undefined, a String, Number, or Array');
  1454. }
  1455. }
  1456. else {
  1457. // return an array
  1458. data = data || [];
  1459. if (ids == undefined) {
  1460. // return all data
  1461. util.forEach(this.data, function (item) {
  1462. data.push(me._castItem(item, fieldTypes, fields));
  1463. });
  1464. }
  1465. else if (util.isNumber(ids) || util.isString(ids)) {
  1466. // return a single item
  1467. return this._castItem(me.data[ids], fieldTypes, fields);
  1468. }
  1469. else if (ids instanceof Array) {
  1470. ids.forEach(function (id) {
  1471. data.push(me._castItem(me.data[id], fieldTypes, fields));
  1472. });
  1473. }
  1474. else {
  1475. throw new TypeError('Parameter "ids" must be ' +
  1476. 'undefined, a String, Number, or Array');
  1477. }
  1478. }
  1479. return data;
  1480. };
  1481. /**
  1482. * Remove an object by pointer or by id
  1483. * @param {String | Number | Object | Array} id Object or id, or an array with
  1484. * objects or ids to be removed
  1485. * @param {String} [senderId] Optional sender id, used to trigger events for
  1486. * all but this sender's event subscribers.
  1487. */
  1488. DataSet.prototype.remove = function (id, senderId) {
  1489. var items = [],
  1490. me = this;
  1491. if (util.isNumber(id) || util.isString(id)) {
  1492. delete this.data[id];
  1493. delete this.internalIds[id];
  1494. items.push(id);
  1495. }
  1496. else if (id instanceof Array) {
  1497. id.forEach(function (id) {
  1498. me.remove(id);
  1499. });
  1500. items = items.concat(id);
  1501. }
  1502. else if (id instanceof Object) {
  1503. // search for the object
  1504. for (var i in this.data) {
  1505. if (this.data.hasOwnProperty(i)) {
  1506. if (this.data[i] == id) {
  1507. delete this.data[i];
  1508. delete this.internalIds[i];
  1509. items.push(i);
  1510. }
  1511. }
  1512. }
  1513. }
  1514. this._trigger('remove', {items: items}, senderId);
  1515. };
  1516. /**
  1517. * Clear the data
  1518. * @param {String} [senderId] Optional sender id, used to trigger events for
  1519. * all but this sender's event subscribers.
  1520. */
  1521. DataSet.prototype.clear = function (senderId) {
  1522. var items = Object.keys(this.data);
  1523. this.data = [];
  1524. this.internalIds = {};
  1525. this._trigger('remove', {items: items}, senderId);
  1526. };
  1527. /**
  1528. * Add a single item
  1529. * @param {Object} item
  1530. * @return {String} id
  1531. * @private
  1532. */
  1533. DataSet.prototype._addItem = function (item) {
  1534. var id = item[this.fieldId];
  1535. if (id == undefined) {
  1536. // generate an id
  1537. id = util.randomUUID();
  1538. item[this.fieldId] = id;
  1539. this.internalIds[id] = item;
  1540. }
  1541. var d = {};
  1542. for (var field in item) {
  1543. if (item.hasOwnProperty(field)) {
  1544. var type = this.fieldTypes[field]; // type may be undefined
  1545. d[field] = util.cast(item[field], type);
  1546. }
  1547. }
  1548. this.data[id] = d;
  1549. //TODO: fail when an item with this id already exists?
  1550. return id;
  1551. };
  1552. /**
  1553. * Cast and filter the fields of an item
  1554. * @param {Object | undefined} item
  1555. * @param {Object.<String, String>} [fieldTypes]
  1556. * @param {String[]} [fields]
  1557. * @return {Object | null} castedItem
  1558. * @private
  1559. */
  1560. DataSet.prototype._castItem = function (item, fieldTypes, fields) {
  1561. var clone,
  1562. fieldId = this.fieldId,
  1563. internalIds = this.internalIds;
  1564. if (item) {
  1565. clone = {};
  1566. fieldTypes = fieldTypes || {};
  1567. if (fields) {
  1568. // output filtered fields
  1569. util.forEach(item, function (value, field) {
  1570. if (fields.indexOf(field) != -1) {
  1571. clone[field] = util.cast(value, fieldTypes[field]);
  1572. }
  1573. });
  1574. }
  1575. else {
  1576. // output all fields, except internal ids
  1577. util.forEach(item, function (value, field) {
  1578. if (field != fieldId || !(value in internalIds)) {
  1579. clone[field] = util.cast(value, fieldTypes[field]);
  1580. }
  1581. });
  1582. }
  1583. }
  1584. else {
  1585. clone = null;
  1586. }
  1587. return clone;
  1588. };
  1589. /**
  1590. * Update a single item: merge with existing item
  1591. * @param {Object} item
  1592. * @return {String} id
  1593. * @private
  1594. */
  1595. DataSet.prototype._updateItem = function (item) {
  1596. var id = item[this.fieldId];
  1597. if (id == undefined) {
  1598. throw new Error('Item has no id (item: ' + JSON.stringify(item) + ')');
  1599. }
  1600. var d = this.data[id];
  1601. if (d) {
  1602. // merge with current item
  1603. for (var field in item) {
  1604. if (item.hasOwnProperty(field)) {
  1605. var type = this.fieldTypes[field]; // type may be undefined
  1606. d[field] = util.cast(item[field], type);
  1607. }
  1608. }
  1609. }
  1610. else {
  1611. // create new item
  1612. this._addItem(item);
  1613. }
  1614. return id;
  1615. };
  1616. /**
  1617. * Get an array with the column names of a Google DataTable
  1618. * @param {DataTable} dataTable
  1619. * @return {Array} columnNames
  1620. * @private
  1621. */
  1622. DataSet.prototype._getColumnNames = function (dataTable) {
  1623. var columns = [];
  1624. for (var col = 0, cols = dataTable.getNumberOfColumns(); col < cols; col++) {
  1625. columns[col] = dataTable.getColumnId(col) || dataTable.getColumnLabel(col);
  1626. }
  1627. return columns;
  1628. };
  1629. /**
  1630. * Append an item as a row to the dataTable
  1631. * @param dataTable
  1632. * @param columns
  1633. * @param item
  1634. * @private
  1635. */
  1636. DataSet.prototype._appendRow = function (dataTable, columns, item) {
  1637. var row = dataTable.addRow();
  1638. columns.forEach(function (field, col) {
  1639. dataTable.setValue(row, col, item[field]);
  1640. });
  1641. };
  1642. /**
  1643. * @constructor Stack
  1644. * Stacks items on top of each other.
  1645. * @param {ItemSet} parent
  1646. * @param {Object} [options]
  1647. */
  1648. function Stack (parent, options) {
  1649. this.parent = parent;
  1650. this.options = {
  1651. order: function (a, b) {
  1652. return (b.width - a.width) || (a.left - b.left);
  1653. }
  1654. };
  1655. this.ordered = []; // ordered items
  1656. this.setOptions(options);
  1657. }
  1658. /**
  1659. * Set options for the stack
  1660. * @param {Object} options Available options:
  1661. * {ItemSet} parent
  1662. * {Number} margin
  1663. * {function} order Stacking order
  1664. */
  1665. Stack.prototype.setOptions = function (options) {
  1666. util.extend(this.options, options);
  1667. // TODO: register on data changes at the connected parent itemset, and update the changed part only and immediately
  1668. };
  1669. /**
  1670. * Stack the items such that they don't overlap. The items will have a minimal
  1671. * distance equal to options.margin.item.
  1672. */
  1673. Stack.prototype.update = function() {
  1674. this._order();
  1675. this._stack();
  1676. };
  1677. /**
  1678. * Order the items. The items are ordered by width first, and by left position
  1679. * second.
  1680. * If a custom order function has been provided via the options, then this will
  1681. * be used.
  1682. * @private
  1683. */
  1684. Stack.prototype._order = function() {
  1685. var items = this.parent.items;
  1686. if (!items) {
  1687. throw new Error('Cannot stack items: parent does not contain items');
  1688. }
  1689. // TODO: store the sorted items, to have less work later on
  1690. var ordered = [];
  1691. var index = 0;
  1692. util.forEach(items, function (item, id) {
  1693. ordered[index] = item;
  1694. index++;
  1695. });
  1696. //if a customer stack order function exists, use it.
  1697. var order = this.options.order;
  1698. if (!(typeof this.options.order === 'function')) {
  1699. throw new Error('Option order must be a function');
  1700. }
  1701. ordered.sort(order);
  1702. this.ordered = ordered;
  1703. };
  1704. /**
  1705. * Adjust vertical positions of the events such that they don't overlap each
  1706. * other.
  1707. * @private
  1708. */
  1709. Stack.prototype._stack = function() {
  1710. var i,
  1711. iMax,
  1712. ordered = this.ordered,
  1713. options = this.options,
  1714. axisOnTop = (options.orientation == 'top'),
  1715. margin = options.margin && options.margin.item || 0;
  1716. // calculate new, non-overlapping positions
  1717. for (i = 0, iMax = ordered.length; i < iMax; i++) {
  1718. var item = ordered[i];
  1719. var collidingItem = null;
  1720. do {
  1721. // TODO: optimize checking for overlap. when there is a gap without items,
  1722. // you only need to check for items from the next item on, not from zero
  1723. collidingItem = this.checkOverlap(ordered, i, 0, i - 1, margin);
  1724. if (collidingItem != null) {
  1725. // There is a collision. Reposition the event above the colliding element
  1726. if (axisOnTop) {
  1727. item.top = collidingItem.top + collidingItem.height + margin;
  1728. }
  1729. else {
  1730. item.top = collidingItem.top - item.height - margin;
  1731. }
  1732. }
  1733. } while (collidingItem);
  1734. }
  1735. };
  1736. /**
  1737. * Check if the destiny position of given item overlaps with any
  1738. * of the other items from index itemStart to itemEnd.
  1739. * @param {Array} items Array with items
  1740. * @param {int} itemIndex Number of the item to be checked for overlap
  1741. * @param {int} itemStart First item to be checked.
  1742. * @param {int} itemEnd Last item to be checked.
  1743. * @return {Object | null} colliding item, or undefined when no collisions
  1744. * @param {Number} margin A minimum required margin.
  1745. * If margin is provided, the two items will be
  1746. * marked colliding when they overlap or
  1747. * when the margin between the two is smaller than
  1748. * the requested margin.
  1749. */
  1750. Stack.prototype.checkOverlap = function(items, itemIndex, itemStart, itemEnd, margin) {
  1751. var collision = this.collision;
  1752. // we loop from end to start, as we suppose that the chance of a
  1753. // collision is larger for items at the end, so check these first.
  1754. var a = items[itemIndex];
  1755. for (var i = itemEnd; i >= itemStart; i--) {
  1756. var b = items[i];
  1757. if (collision(a, b, margin)) {
  1758. if (i != itemIndex) {
  1759. return b;
  1760. }
  1761. }
  1762. }
  1763. return null;
  1764. };
  1765. /**
  1766. * Test if the two provided items collide
  1767. * The items must have parameters left, width, top, and height.
  1768. * @param {Component} a The first item
  1769. * @param {Component} b The second item
  1770. * @param {Number} margin A minimum required margin.
  1771. * If margin is provided, the two items will be
  1772. * marked colliding when they overlap or
  1773. * when the margin between the two is smaller than
  1774. * the requested margin.
  1775. * @return {boolean} true if a and b collide, else false
  1776. */
  1777. Stack.prototype.collision = function(a, b, margin) {
  1778. return ((a.left - margin) < (b.left + b.width) &&
  1779. (a.left + a.width + margin) > b.left &&
  1780. (a.top - margin) < (b.top + b.height) &&
  1781. (a.top + a.height + margin) > b.top);
  1782. };
  1783. /**
  1784. * @constructor Range
  1785. * A Range controls a numeric range with a start and end value.
  1786. * The Range adjusts the range based on mouse events or programmatic changes,
  1787. * and triggers events when the range is changing or has been changed.
  1788. * @param {Object} [options] See description at Range.setOptions
  1789. * @extends Controller
  1790. */
  1791. function Range(options) {
  1792. this.id = util.randomUUID();
  1793. this.start = 0; // Number
  1794. this.end = 0; // Number
  1795. this.options = {
  1796. min: null,
  1797. max: null,
  1798. zoomMin: null,
  1799. zoomMax: null
  1800. };
  1801. this.setOptions(options);
  1802. this.listeners = [];
  1803. }
  1804. /**
  1805. * Set options for the range controller
  1806. * @param {Object} options Available options:
  1807. * {Number} start Set start value of the range
  1808. * {Number} end Set end value of the range
  1809. * {Number} min Minimum value for start
  1810. * {Number} max Maximum value for end
  1811. * {Number} zoomMin Set a minimum value for
  1812. * (end - start).
  1813. * {Number} zoomMax Set a maximum value for
  1814. * (end - start).
  1815. */
  1816. Range.prototype.setOptions = function (options) {
  1817. util.extend(this.options, options);
  1818. if (options.start != null || options.end != null) {
  1819. this.setRange(options.start, options.end);
  1820. }
  1821. };
  1822. /**
  1823. * Add listeners for mouse and touch events to the component
  1824. * @param {Component} component
  1825. * @param {String} event Available events: 'move', 'zoom'
  1826. * @param {String} direction Available directions: 'horizontal', 'vertical'
  1827. */
  1828. Range.prototype.subscribe = function (component, event, direction) {
  1829. var me = this;
  1830. var listener;
  1831. if (direction != 'horizontal' && direction != 'vertical') {
  1832. throw new TypeError('Unknown direction "' + direction + '". ' +
  1833. 'Choose "horizontal" or "vertical".');
  1834. }
  1835. //noinspection FallthroughInSwitchStatementJS
  1836. if (event == 'move') {
  1837. listener = {
  1838. component: component,
  1839. event: event,
  1840. direction: direction,
  1841. callback: function (event) {
  1842. me._onMouseDown(event, listener);
  1843. },
  1844. params: {}
  1845. };
  1846. component.on('mousedown', listener.callback);
  1847. me.listeners.push(listener);
  1848. }
  1849. else if (event == 'zoom') {
  1850. listener = {
  1851. component: component,
  1852. event: event,
  1853. direction: direction,
  1854. callback: function (event) {
  1855. me._onMouseWheel(event, listener);
  1856. },
  1857. params: {}
  1858. };
  1859. component.on('mousewheel', listener.callback);
  1860. me.listeners.push(listener);
  1861. }
  1862. else {
  1863. throw new TypeError('Unknown event "' + event + '". ' +
  1864. 'Choose "move" or "zoom".');
  1865. }
  1866. };
  1867. /**
  1868. * Event handler
  1869. * @param {String} event name of the event, for example 'click', 'mousemove'
  1870. * @param {function} callback callback handler, invoked with the raw HTML Event
  1871. * as parameter.
  1872. */
  1873. Range.prototype.on = function (event, callback) {
  1874. events.addListener(this, event, callback);
  1875. };
  1876. /**
  1877. * Trigger an event
  1878. * @param {String} event name of the event, available events: 'rangechange',
  1879. * 'rangechanged'
  1880. * @private
  1881. */
  1882. Range.prototype._trigger = function (event) {
  1883. events.trigger(this, event, {
  1884. start: this.start,
  1885. end: this.end
  1886. });
  1887. };
  1888. /**
  1889. * Set a new start and end range
  1890. * @param {Number} start
  1891. * @param {Number} end
  1892. */
  1893. Range.prototype.setRange = function(start, end) {
  1894. var changed = this._applyRange(start, end);
  1895. if (changed) {
  1896. this._trigger('rangechange');
  1897. this._trigger('rangechanged');
  1898. }
  1899. };
  1900. /**
  1901. * Set a new start and end range. This method is the same as setRange, but
  1902. * does not trigger a range change and range changed event, and it returns
  1903. * true when the range is changed
  1904. * @param {Number} start
  1905. * @param {Number} end
  1906. * @return {Boolean} changed
  1907. * @private
  1908. */
  1909. Range.prototype._applyRange = function(start, end) {
  1910. var newStart = util.cast(start, 'Number');
  1911. var newEnd = util.cast(end, 'Number');
  1912. var diff;
  1913. // check for valid number
  1914. if (newStart == null || isNaN(newStart)) {
  1915. throw new Error('Invalid start "' + start + '"');
  1916. }
  1917. if (newEnd == null || isNaN(newEnd)) {
  1918. throw new Error('Invalid end "' + end + '"');
  1919. }
  1920. // prevent start < end
  1921. if (newEnd < newStart) {
  1922. newEnd = newStart;
  1923. }
  1924. // prevent start < min
  1925. if (this.options.min != null) {
  1926. var min = this.options.min.valueOf();
  1927. if (newStart < min) {
  1928. diff = (min - newStart);
  1929. newStart += diff;
  1930. newEnd += diff;
  1931. }
  1932. }
  1933. // prevent end > max
  1934. if (this.options.max != null) {
  1935. var max = this.options.max.valueOf();
  1936. if (newEnd > max) {
  1937. diff = (newEnd - max);
  1938. newStart -= diff;
  1939. newEnd -= diff;
  1940. }
  1941. }
  1942. // prevent (end-start) > zoomMin
  1943. if (this.options.zoomMin != null) {
  1944. var zoomMin = this.options.zoomMin.valueOf();
  1945. if (zoomMin < 0) {
  1946. zoomMin = 0;
  1947. }
  1948. if ((newEnd - newStart) < zoomMin) {
  1949. if ((this.end - this.start) > zoomMin) {
  1950. // zoom to the minimum
  1951. diff = (zoomMin - (newEnd - newStart));
  1952. newStart -= diff / 2;
  1953. newEnd += diff / 2;
  1954. }
  1955. else {
  1956. // ingore this action, we are already zoomed to the minimum
  1957. newStart = this.start;
  1958. newEnd = this.end;
  1959. }
  1960. }
  1961. }
  1962. // prevent (end-start) > zoomMin
  1963. if (this.options.zoomMax != null) {
  1964. var zoomMax = this.options.zoomMax.valueOf();
  1965. if (zoomMax < 0) {
  1966. zoomMax = 0;
  1967. }
  1968. if ((newEnd - newStart) > zoomMax) {
  1969. if ((this.end - this.start) < zoomMax) {
  1970. // zoom to the maximum
  1971. diff = ((newEnd - newStart) - zoomMax);
  1972. newStart += diff / 2;
  1973. newEnd -= diff / 2;
  1974. }
  1975. else {
  1976. // ingore this action, we are already zoomed to the maximum
  1977. newStart = this.start;
  1978. newEnd = this.end;
  1979. }
  1980. }
  1981. }
  1982. var changed = (this.start != newStart || this.end != newEnd);
  1983. this.start = newStart;
  1984. this.end = newEnd;
  1985. return changed;
  1986. };
  1987. /**
  1988. * Retrieve the current range.
  1989. * @return {Object} An object with start and end properties
  1990. */
  1991. Range.prototype.getRange = function() {
  1992. return {
  1993. start: this.start,
  1994. end: this.end
  1995. };
  1996. };
  1997. /**
  1998. * Calculate the conversion offset and factor for current range, based on
  1999. * the provided width
  2000. * @param {Number} width
  2001. * @returns {{offset: number, factor: number}} conversion
  2002. */
  2003. Range.prototype.conversion = function (width) {
  2004. var start = this.start;
  2005. var end = this.end;
  2006. return Range.conversion(this.start, this.end, width);
  2007. };
  2008. /**
  2009. * Static method to calculate the conversion offset and factor for a range,
  2010. * based on the provided start, end, and width
  2011. * @param {Number} start
  2012. * @param {Number} end
  2013. * @param {Number} width
  2014. * @returns {{offset: number, factor: number}} conversion
  2015. */
  2016. Range.conversion = function (start, end, width) {
  2017. if (width != 0 && (end - start != 0)) {
  2018. return {
  2019. offset: start,
  2020. factor: width / (end - start)
  2021. }
  2022. }
  2023. else {
  2024. return {
  2025. offset: 0,
  2026. factor: 1
  2027. };
  2028. }
  2029. };
  2030. /**
  2031. * Start moving horizontally or vertically
  2032. * @param {Event} event
  2033. * @param {Object} listener Listener containing the component and params
  2034. * @private
  2035. */
  2036. Range.prototype._onMouseDown = function(event, listener) {
  2037. event = event || window.event;
  2038. var params = listener.params;
  2039. // only react on left mouse button down
  2040. var leftButtonDown = event.which ? (event.which == 1) : (event.button == 1);
  2041. if (!leftButtonDown) {
  2042. return;
  2043. }
  2044. // get mouse position
  2045. params.mouseX = util.getPageX(event);
  2046. params.mouseY = util.getPageY(event);
  2047. params.previousLeft = 0;
  2048. params.previousOffset = 0;
  2049. params.moved = false;
  2050. params.start = this.start;
  2051. params.end = this.end;
  2052. var frame = listener.component.frame;
  2053. if (frame) {
  2054. frame.style.cursor = 'move';
  2055. }
  2056. // add event listeners to handle moving the contents
  2057. // we store the function onmousemove and onmouseup in the timeaxis,
  2058. // so we can remove the eventlisteners lateron in the function onmouseup
  2059. var me = this;
  2060. if (!params.onMouseMove) {
  2061. params.onMouseMove = function (event) {
  2062. me._onMouseMove(event, listener);
  2063. };
  2064. util.addEventListener(document, "mousemove", params.onMouseMove);
  2065. }
  2066. if (!params.onMouseUp) {
  2067. params.onMouseUp = function (event) {
  2068. me._onMouseUp(event, listener);
  2069. };
  2070. util.addEventListener(document, "mouseup", params.onMouseUp);
  2071. }
  2072. util.preventDefault(event);
  2073. };
  2074. /**
  2075. * Perform moving operating.
  2076. * This function activated from within the funcion TimeAxis._onMouseDown().
  2077. * @param {Event} event
  2078. * @param {Object} listener
  2079. * @private
  2080. */
  2081. Range.prototype._onMouseMove = function (event, listener) {
  2082. event = event || window.event;
  2083. var params = listener.params;
  2084. // calculate change in mouse position
  2085. var mouseX = util.getPageX(event);
  2086. var mouseY = util.getPageY(event);
  2087. if (params.mouseX == undefined) {
  2088. params.mouseX = mouseX;
  2089. }
  2090. if (params.mouseY == undefined) {
  2091. params.mouseY = mouseY;
  2092. }
  2093. var diffX = mouseX - params.mouseX;
  2094. var diffY = mouseY - params.mouseY;
  2095. var diff = (listener.direction == 'horizontal') ? diffX : diffY;
  2096. // if mouse movement is big enough, register it as a "moved" event
  2097. if (Math.abs(diff) >= 1) {
  2098. params.moved = true;
  2099. }
  2100. var interval = (params.end - params.start);
  2101. var width = (listener.direction == 'horizontal') ?
  2102. listener.component.width : listener.component.height;
  2103. var diffRange = -diff / width * interval;
  2104. this._applyRange(params.start + diffRange, params.end + diffRange);
  2105. // fire a rangechange event
  2106. this._trigger('rangechange');
  2107. util.preventDefault(event);
  2108. };
  2109. /**
  2110. * Stop moving operating.
  2111. * This function activated from within the function Range._onMouseDown().
  2112. * @param {event} event
  2113. * @param {Object} listener
  2114. * @private
  2115. */
  2116. Range.prototype._onMouseUp = function (event, listener) {
  2117. event = event || window.event;
  2118. var params = listener.params;
  2119. if (listener.component.frame) {
  2120. listener.component.frame.style.cursor = 'auto';
  2121. }
  2122. // remove event listeners here, important for Safari
  2123. if (params.onMouseMove) {
  2124. util.removeEventListener(document, "mousemove", params.onMouseMove);
  2125. params.onMouseMove = null;
  2126. }
  2127. if (params.onMouseUp) {
  2128. util.removeEventListener(document, "mouseup", params.onMouseUp);
  2129. params.onMouseUp = null;
  2130. }
  2131. //util.preventDefault(event);
  2132. if (params.moved) {
  2133. // fire a rangechanged event
  2134. this._trigger('rangechanged');
  2135. }
  2136. };
  2137. /**
  2138. * Event handler for mouse wheel event, used to zoom
  2139. * Code from http://adomas.org/javascript-mouse-wheel/
  2140. * @param {Event} event
  2141. * @param {Object} listener
  2142. * @private
  2143. */
  2144. Range.prototype._onMouseWheel = function(event, listener) {
  2145. event = event || window.event;
  2146. // retrieve delta
  2147. var delta = 0;
  2148. if (event.wheelDelta) { /* IE/Opera. */
  2149. delta = event.wheelDelta / 120;
  2150. } else if (event.detail) { /* Mozilla case. */
  2151. // In Mozilla, sign of delta is different than in IE.
  2152. // Also, delta is multiple of 3.
  2153. delta = -event.detail / 3;
  2154. }
  2155. // If delta is nonzero, handle it.
  2156. // Basically, delta is now positive if wheel was scrolled up,
  2157. // and negative, if wheel was scrolled down.
  2158. if (delta) {
  2159. var me = this;
  2160. var zoom = function () {
  2161. // perform the zoom action. Delta is normally 1 or -1
  2162. var zoomFactor = delta / 5.0;
  2163. var zoomAround = null;
  2164. var frame = listener.component.frame;
  2165. if (frame) {
  2166. var size, conversion;
  2167. if (listener.direction == 'horizontal') {
  2168. size = listener.component.width;
  2169. conversion = me.conversion(size);
  2170. var frameLeft = util.getAbsoluteLeft(frame);
  2171. var mouseX = util.getPageX(event);
  2172. zoomAround = (mouseX - frameLeft) / conversion.factor + conversion.offset;
  2173. }
  2174. else {
  2175. size = listener.component.height;
  2176. conversion = me.conversion(size);
  2177. var frameTop = util.getAbsoluteTop(frame);
  2178. var mouseY = util.getPageY(event);
  2179. zoomAround = ((frameTop + size - mouseY) - frameTop) / conversion.factor + conversion.offset;
  2180. }
  2181. }
  2182. me.zoom(zoomFactor, zoomAround);
  2183. };
  2184. zoom();
  2185. }
  2186. // Prevent default actions caused by mouse wheel.
  2187. // That might be ugly, but we handle scrolls somehow
  2188. // anyway, so don't bother here...
  2189. util.preventDefault(event);
  2190. };
  2191. /**
  2192. * Zoom the range the given zoomfactor in or out. Start and end date will
  2193. * be adjusted, and the timeline will be redrawn. You can optionally give a
  2194. * date around which to zoom.
  2195. * For example, try zoomfactor = 0.1 or -0.1
  2196. * @param {Number} zoomFactor Zooming amount. Positive value will zoom in,
  2197. * negative value will zoom out
  2198. * @param {Number} zoomAround Value around which will be zoomed. Optional
  2199. */
  2200. Range.prototype.zoom = function(zoomFactor, zoomAround) {
  2201. // if zoomAroundDate is not provided, take it half between start Date and end Date
  2202. if (zoomAround == null) {
  2203. zoomAround = (this.start + this.end) / 2;
  2204. }
  2205. // prevent zoom factor larger than 1 or smaller than -1 (larger than 1 will
  2206. // result in a start>=end )
  2207. if (zoomFactor >= 1) {
  2208. zoomFactor = 0.9;
  2209. }
  2210. if (zoomFactor <= -1) {
  2211. zoomFactor = -0.9;
  2212. }
  2213. // adjust a negative factor such that zooming in with 0.1 equals zooming
  2214. // out with a factor -0.1
  2215. if (zoomFactor < 0) {
  2216. zoomFactor = zoomFactor / (1 + zoomFactor);
  2217. }
  2218. // zoom start and end relative to the zoomAround value
  2219. var startDiff = (this.start - zoomAround);
  2220. var endDiff = (this.end - zoomAround);
  2221. // calculate new start and end
  2222. var newStart = this.start - startDiff * zoomFactor;
  2223. var newEnd = this.end - endDiff * zoomFactor;
  2224. this.setRange(newStart, newEnd);
  2225. };
  2226. /**
  2227. * Move the range with a given factor to the left or right. Start and end
  2228. * value will be adjusted. For example, try moveFactor = 0.1 or -0.1
  2229. * @param {Number} moveFactor Moving amount. Positive value will move right,
  2230. * negative value will move left
  2231. */
  2232. Range.prototype.move = function(moveFactor) {
  2233. // zoom start Date and end Date relative to the zoomAroundDate
  2234. var diff = (this.end - this.start);
  2235. // apply new values
  2236. var newStart = this.start + diff * moveFactor;
  2237. var newEnd = this.end + diff * moveFactor;
  2238. // TODO: reckon with min and max range
  2239. this.start = newStart;
  2240. this.end = newEnd;
  2241. };
  2242. /**
  2243. * @constructor Controller
  2244. *
  2245. * A Controller controls the reflows and repaints of all visual components
  2246. */
  2247. function Controller () {
  2248. this.id = util.randomUUID();
  2249. this.components = {};
  2250. this.repaintTimer = undefined;
  2251. this.reflowTimer = undefined;
  2252. }
  2253. /**
  2254. * Add a component to the controller
  2255. * @param {Component | Controller} component
  2256. */
  2257. Controller.prototype.add = function (component) {
  2258. // validate the component
  2259. if (component.id == undefined) {
  2260. throw new Error('Component has no field id');
  2261. }
  2262. if (!(component instanceof Component) && !(component instanceof Controller)) {
  2263. throw new TypeError('Component must be an instance of ' +
  2264. 'prototype Component or Controller');
  2265. }
  2266. // add the component
  2267. component.controller = this;
  2268. this.components[component.id] = component;
  2269. };
  2270. /**
  2271. * Request a reflow. The controller will schedule a reflow
  2272. */
  2273. Controller.prototype.requestReflow = function () {
  2274. if (!this.reflowTimer) {
  2275. var me = this;
  2276. this.reflowTimer = setTimeout(function () {
  2277. me.reflowTimer = undefined;
  2278. me.reflow();
  2279. }, 0);
  2280. }
  2281. };
  2282. /**
  2283. * Request a repaint. The controller will schedule a repaint
  2284. */
  2285. Controller.prototype.requestRepaint = function () {
  2286. if (!this.repaintTimer) {
  2287. var me = this;
  2288. this.repaintTimer = setTimeout(function () {
  2289. me.repaintTimer = undefined;
  2290. me.repaint();
  2291. }, 0);
  2292. }
  2293. };
  2294. /**
  2295. * Repaint all components
  2296. */
  2297. Controller.prototype.repaint = function () {
  2298. var changed = false;
  2299. // cancel any running repaint request
  2300. if (this.repaintTimer) {
  2301. clearTimeout(this.repaintTimer);
  2302. this.repaintTimer = undefined;
  2303. }
  2304. var done = {};
  2305. function repaint(component, id) {
  2306. if (!(id in done)) {
  2307. // first repaint the components on which this component is dependent
  2308. if (component.depends) {
  2309. component.depends.forEach(function (dep) {
  2310. repaint(dep, dep.id);
  2311. });
  2312. }
  2313. if (component.parent) {
  2314. repaint(component.parent, component.parent.id);
  2315. }
  2316. // repaint the component itself and mark as done
  2317. changed = component.repaint() || changed;
  2318. done[id] = true;
  2319. }
  2320. }
  2321. util.forEach(this.components, repaint);
  2322. // immediately reflow when needed
  2323. if (changed) {
  2324. this.reflow();
  2325. }
  2326. // TODO: limit the number of nested reflows/repaints, prevent loop
  2327. };
  2328. /**
  2329. * Reflow all components
  2330. */
  2331. Controller.prototype.reflow = function () {
  2332. var resized = false;
  2333. // cancel any running repaint request
  2334. if (this.reflowTimer) {
  2335. clearTimeout(this.reflowTimer);
  2336. this.reflowTimer = undefined;
  2337. }
  2338. var done = {};
  2339. function reflow(component, id) {
  2340. if (!(id in done)) {
  2341. // first reflow the components on which this component is dependent
  2342. if (component.depends) {
  2343. component.depends.forEach(function (dep) {
  2344. reflow(dep, dep.id);
  2345. });
  2346. }
  2347. if (component.parent) {
  2348. reflow(component.parent, component.parent.id);
  2349. }
  2350. // reflow the component itself and mark as done
  2351. resized = component.reflow() || resized;
  2352. done[id] = true;
  2353. }
  2354. }
  2355. util.forEach(this.components, reflow);
  2356. // immediately repaint when needed
  2357. if (resized) {
  2358. this.repaint();
  2359. }
  2360. // TODO: limit the number of nested reflows/repaints, prevent loop
  2361. };
  2362. /**
  2363. * Prototype for visual components
  2364. */
  2365. function Component () {
  2366. this.id = null;
  2367. this.parent = null;
  2368. this.depends = null;
  2369. this.controller = null;
  2370. this.options = null;
  2371. this.frame = null; // main DOM element
  2372. this.top = 0;
  2373. this.left = 0;
  2374. this.width = 0;
  2375. this.height = 0;
  2376. }
  2377. /**
  2378. * Set parameters for the frame. Parameters will be merged in current parameter
  2379. * set.
  2380. * @param {Object} options Available parameters:
  2381. * {String | function} [className]
  2382. * {String | Number | function} [left]
  2383. * {String | Number | function} [top]
  2384. * {String | Number | function} [width]
  2385. * {String | Number | function} [height]
  2386. */
  2387. Component.prototype.setOptions = function(options) {
  2388. if (options) {
  2389. util.extend(this.options, options);
  2390. }
  2391. if (this.controller) {
  2392. this.requestRepaint();
  2393. this.requestReflow();
  2394. }
  2395. };
  2396. /**
  2397. * Get the container element of the component, which can be used by a child to
  2398. * add its own widgets. Not all components do have a container for childs, in
  2399. * that case null is returned.
  2400. * @returns {HTMLElement | null} container
  2401. */
  2402. Component.prototype.getContainer = function () {
  2403. // should be implemented by the component
  2404. return null;
  2405. };
  2406. /**
  2407. * Get the frame element of the component, the outer HTML DOM element.
  2408. * @returns {HTMLElement | null} frame
  2409. */
  2410. Component.prototype.getFrame = function () {
  2411. return this.frame;
  2412. };
  2413. /**
  2414. * Repaint the component
  2415. * @return {Boolean} changed
  2416. */
  2417. Component.prototype.repaint = function () {
  2418. // should be implemented by the component
  2419. return false;
  2420. };
  2421. /**
  2422. * Reflow the component
  2423. * @return {Boolean} resized
  2424. */
  2425. Component.prototype.reflow = function () {
  2426. // should be implemented by the component
  2427. return false;
  2428. };
  2429. /**
  2430. * Request a repaint. The controller will schedule a repaint
  2431. */
  2432. Component.prototype.requestRepaint = function () {
  2433. if (this.controller) {
  2434. this.controller.requestRepaint();
  2435. }
  2436. else {
  2437. throw new Error('Cannot request a repaint: no controller configured');
  2438. // TODO: just do a repaint when no parent is configured?
  2439. }
  2440. };
  2441. /**
  2442. * Request a reflow. The controller will schedule a reflow
  2443. */
  2444. Component.prototype.requestReflow = function () {
  2445. if (this.controller) {
  2446. this.controller.requestReflow();
  2447. }
  2448. else {
  2449. throw new Error('Cannot request a reflow: no controller configured');
  2450. // TODO: just do a reflow when no parent is configured?
  2451. }
  2452. };
  2453. /**
  2454. * Event handler
  2455. * @param {String} event name of the event, for example 'click', 'mousemove'
  2456. * @param {function} callback callback handler, invoked with the raw HTML Event
  2457. * as parameter.
  2458. */
  2459. Component.prototype.on = function (event, callback) {
  2460. // TODO: rethink the way of event delegation
  2461. if (this.parent) {
  2462. this.parent.on(event, callback);
  2463. }
  2464. else {
  2465. throw new Error('Cannot attach event: no root panel found');
  2466. }
  2467. };
  2468. /**
  2469. * A panel can contain components
  2470. * @param {Component} [parent]
  2471. * @param {Component[]} [depends] Components on which this components depends
  2472. * (except for the parent)
  2473. * @param {Object} [options] Available parameters:
  2474. * {String | Number | function} [left]
  2475. * {String | Number | function} [top]
  2476. * {String | Number | function} [width]
  2477. * {String | Number | function} [height]
  2478. * {String | function} [className]
  2479. * @constructor Panel
  2480. * @extends Component
  2481. */
  2482. function Panel(parent, depends, options) {
  2483. this.id = util.randomUUID();
  2484. this.parent = parent;
  2485. this.depends = depends;
  2486. this.options = {};
  2487. this.setOptions(options);
  2488. }
  2489. Panel.prototype = new Component();
  2490. /**
  2491. * Get the container element of the panel, which can be used by a child to
  2492. * add its own widgets.
  2493. * @returns {HTMLElement} container
  2494. */
  2495. Panel.prototype.getContainer = function () {
  2496. return this.frame;
  2497. };
  2498. /**
  2499. * Repaint the component
  2500. * @return {Boolean} changed
  2501. */
  2502. Panel.prototype.repaint = function () {
  2503. var changed = 0,
  2504. update = util.updateProperty,
  2505. asSize = util.option.asSize,
  2506. options = this.options,
  2507. frame = this.frame;
  2508. if (!frame) {
  2509. frame = document.createElement('div');
  2510. frame.className = 'panel';
  2511. if (options.className) {
  2512. if (typeof options.className == 'function') {
  2513. util.addClassName(frame, String(options.className()));
  2514. }
  2515. else {
  2516. util.addClassName(frame, String(options.className));
  2517. }
  2518. }
  2519. this.frame = frame;
  2520. changed += 1;
  2521. }
  2522. if (!frame.parentNode) {
  2523. if (!this.parent) {
  2524. throw new Error('Cannot repaint panel: no parent attached');
  2525. }
  2526. var parentContainer = this.parent.getContainer();
  2527. if (!parentContainer) {
  2528. throw new Error('Cannot repaint panel: parent has no container element');
  2529. }
  2530. parentContainer.appendChild(frame);
  2531. changed += 1;
  2532. }
  2533. changed += update(frame.style, 'top', asSize(options.top, '0px'));
  2534. changed += update(frame.style, 'left', asSize(options.left, '0px'));
  2535. changed += update(frame.style, 'width', asSize(options.width, '100%'));
  2536. changed += update(frame.style, 'height', asSize(options.height, '100%'));
  2537. return (changed > 0);
  2538. };
  2539. /**
  2540. * Reflow the component
  2541. * @return {Boolean} resized
  2542. */
  2543. Panel.prototype.reflow = function () {
  2544. var changed = 0,
  2545. update = util.updateProperty,
  2546. frame = this.frame;
  2547. if (frame) {
  2548. changed += update(this, 'top', frame.offsetTop);
  2549. changed += update(this, 'left', frame.offsetLeft);
  2550. changed += update(this, 'width', frame.offsetWidth);
  2551. changed += update(this, 'height', frame.offsetHeight);
  2552. }
  2553. else {
  2554. changed += 1;
  2555. }
  2556. return (changed > 0);
  2557. };
  2558. /**
  2559. * A root panel can hold components. The root panel must be initialized with
  2560. * a DOM element as container.
  2561. * @param {HTMLElement} container
  2562. * @param {Object} [options] Available parameters: see RootPanel.setOptions.
  2563. * @constructor RootPanel
  2564. * @extends Panel
  2565. */
  2566. function RootPanel(container, options) {
  2567. this.id = util.randomUUID();
  2568. this.container = container;
  2569. this.options = {
  2570. autoResize: true
  2571. };
  2572. this.listeners = {}; // event listeners
  2573. this.setOptions(options);
  2574. }
  2575. RootPanel.prototype = new Panel();
  2576. /**
  2577. * Set options. Will extend the current options.
  2578. * @param {Object} [options] Available parameters:
  2579. * {String | function} [className]
  2580. * {String | Number | function} [left]
  2581. * {String | Number | function} [top]
  2582. * {String | Number | function} [width]
  2583. * {String | Number | function} [height]
  2584. * {String | Number | function} [height]
  2585. * {Boolean | function} [autoResize]
  2586. */
  2587. RootPanel.prototype.setOptions = function (options) {
  2588. util.extend(this.options, options);
  2589. if (this.options.autoResize) {
  2590. this._watch();
  2591. }
  2592. else {
  2593. this._unwatch();
  2594. }
  2595. };
  2596. /**
  2597. * Repaint the component
  2598. * @return {Boolean} changed
  2599. */
  2600. RootPanel.prototype.repaint = function () {
  2601. var changed = 0,
  2602. update = util.updateProperty,
  2603. asSize = util.option.asSize,
  2604. options = this.options,
  2605. frame = this.frame;
  2606. if (!frame) {
  2607. frame = document.createElement('div');
  2608. frame.className = 'graph panel';
  2609. if (options.className) {
  2610. util.addClassName(frame, util.option.asString(options.className));
  2611. }
  2612. this.frame = frame;
  2613. changed += 1;
  2614. }
  2615. if (!frame.parentNode) {
  2616. if (!this.container) {
  2617. throw new Error('Cannot repaint root panel: no container attached');
  2618. }
  2619. this.container.appendChild(frame);
  2620. changed += 1;
  2621. }
  2622. changed += update(frame.style, 'top', asSize(options.top, '0px'));
  2623. changed += update(frame.style, 'left', asSize(options.left, '0px'));
  2624. changed += update(frame.style, 'width', asSize(options.width, '100%'));
  2625. changed += update(frame.style, 'height', asSize(options.height, '100%'));
  2626. this._updateEventEmitters();
  2627. return (changed > 0);
  2628. };
  2629. /**
  2630. * Reflow the component
  2631. * @return {Boolean} resized
  2632. */
  2633. RootPanel.prototype.reflow = function () {
  2634. var changed = 0,
  2635. update = util.updateProperty,
  2636. frame = this.frame;
  2637. if (frame) {
  2638. changed += update(this, 'top', frame.offsetTop);
  2639. changed += update(this, 'left', frame.offsetLeft);
  2640. changed += update(this, 'width', frame.offsetWidth);
  2641. changed += update(this, 'height', frame.offsetHeight);
  2642. }
  2643. else {
  2644. changed += 1;
  2645. }
  2646. return (changed > 0);
  2647. };
  2648. /**
  2649. * Watch for changes in the size of the frame. On resize, the Panel will
  2650. * automatically redraw itself.
  2651. * @private
  2652. */
  2653. RootPanel.prototype._watch = function () {
  2654. var me = this;
  2655. this._unwatch();
  2656. var checkSize = function () {
  2657. if (!me.options.autoResize) {
  2658. // stop watching when the option autoResize is changed to false
  2659. me._unwatch();
  2660. return;
  2661. }
  2662. if (me.frame) {
  2663. // check whether the frame is resized
  2664. if ((me.frame.clientWidth != me.width) ||
  2665. (me.frame.clientHeight != me.height)) {
  2666. me.requestReflow();
  2667. }
  2668. }
  2669. };
  2670. // TODO: automatically cleanup the event listener when the frame is deleted
  2671. util.addEventListener(window, 'resize', checkSize);
  2672. this.watchTimer = setInterval(checkSize, 1000);
  2673. };
  2674. /**
  2675. * Stop watching for a resize of the frame.
  2676. * @private
  2677. */
  2678. RootPanel.prototype._unwatch = function () {
  2679. if (this.watchTimer) {
  2680. clearInterval(this.watchTimer);
  2681. this.watchTimer = undefined;
  2682. }
  2683. // TODO: remove event listener on window.resize
  2684. };
  2685. /**
  2686. * Event handler
  2687. * @param {String} event name of the event, for example 'click', 'mousemove'
  2688. * @param {function} callback callback handler, invoked with the raw HTML Event
  2689. * as parameter.
  2690. */
  2691. RootPanel.prototype.on = function (event, callback) {
  2692. // register the listener at this component
  2693. var arr = this.listeners[event];
  2694. if (!arr) {
  2695. arr = [];
  2696. this.listeners[event] = arr;
  2697. }
  2698. arr.push(callback);
  2699. this._updateEventEmitters();
  2700. };
  2701. /**
  2702. * Update the event listeners for all event emitters
  2703. * @private
  2704. */
  2705. RootPanel.prototype._updateEventEmitters = function () {
  2706. if (this.listeners) {
  2707. var me = this;
  2708. util.forEach(this.listeners, function (listeners, event) {
  2709. if (!me.emitters) {
  2710. me.emitters = {};
  2711. }
  2712. if (!(event in me.emitters)) {
  2713. // create event
  2714. var frame = me.frame;
  2715. if (frame) {
  2716. //console.log('Created a listener for event ' + event + ' on component ' + me.id); // TODO: cleanup logging
  2717. var callback = function(event) {
  2718. listeners.forEach(function (listener) {
  2719. // TODO: filter on event target!
  2720. listener(event);
  2721. });
  2722. };
  2723. me.emitters[event] = callback;
  2724. util.addEventListener(frame, event, callback);
  2725. }
  2726. }
  2727. });
  2728. // TODO: be able to delete event listeners
  2729. // TODO: be able to move event listeners to a parent when available
  2730. }
  2731. };
  2732. /**
  2733. * A horizontal time axis
  2734. * @param {Component} parent
  2735. * @param {Component[]} [depends] Components on which this components depends
  2736. * (except for the parent)
  2737. * @param {Object} [options] See TimeAxis.setOptions for the available
  2738. * options.
  2739. * @constructor TimeAxis
  2740. * @extends Component
  2741. */
  2742. function TimeAxis (parent, depends, options) {
  2743. this.id = util.randomUUID();
  2744. this.parent = parent;
  2745. this.depends = depends;
  2746. this.dom = {
  2747. majorLines: [],
  2748. majorTexts: [],
  2749. minorLines: [],
  2750. minorTexts: [],
  2751. redundant: {
  2752. majorLines: [],
  2753. majorTexts: [],
  2754. minorLines: [],
  2755. minorTexts: []
  2756. }
  2757. };
  2758. this.props = {
  2759. range: {
  2760. start: 0,
  2761. end: 0,
  2762. minimumStep: 0
  2763. }
  2764. };
  2765. this.options = {
  2766. orientation: 'bottom', // supported: 'top', 'bottom'
  2767. // TODO: implement timeaxis orientations 'left' and 'right'
  2768. showMinorLabels: true,
  2769. showMajorLabels: true
  2770. };
  2771. this.conversion = null;
  2772. this.range = null;
  2773. this.setOptions(options);
  2774. }
  2775. TimeAxis.prototype = new Component();
  2776. // TODO: comment options
  2777. TimeAxis.prototype.setOptions = function (options) {
  2778. util.extend(this.options, options);
  2779. };
  2780. /**
  2781. * Set a range (start and end)
  2782. * @param {Range | Object} range A Range or an object containing start and end.
  2783. */
  2784. TimeAxis.prototype.setRange = function (range) {
  2785. if (!(range instanceof Range) && (!range || !range.start || !range.end)) {
  2786. throw new TypeError('Range must be an instance of Range, ' +
  2787. 'or an object containing start and end.');
  2788. }
  2789. this.range = range;
  2790. };
  2791. /**
  2792. * Convert a position on screen (pixels) to a datetime
  2793. * @param {int} x Position on the screen in pixels
  2794. * @return {Date} time The datetime the corresponds with given position x
  2795. */
  2796. TimeAxis.prototype.toTime = function(x) {
  2797. var conversion = this.conversion;
  2798. return new Date(x / conversion.factor + conversion.offset);
  2799. };
  2800. /**
  2801. * Convert a datetime (Date object) into a position on the screen
  2802. * @param {Date} time A date
  2803. * @return {int} x The position on the screen in pixels which corresponds
  2804. * with the given date.
  2805. * @private
  2806. */
  2807. TimeAxis.prototype.toScreen = function(time) {
  2808. var conversion = this.conversion;
  2809. return (time.valueOf() - conversion.offset) * conversion.factor;
  2810. };
  2811. /**
  2812. * Repaint the component
  2813. * @return {Boolean} changed
  2814. */
  2815. TimeAxis.prototype.repaint = function () {
  2816. var changed = 0,
  2817. update = util.updateProperty,
  2818. asSize = util.option.asSize,
  2819. options = this.options,
  2820. props = this.props,
  2821. step = this.step;
  2822. var frame = this.frame;
  2823. if (!frame) {
  2824. frame = document.createElement('div');
  2825. this.frame = frame;
  2826. changed += 1;
  2827. }
  2828. frame.className = 'axis ' + options.orientation;
  2829. // TODO: custom className?
  2830. if (!frame.parentNode) {
  2831. if (!this.parent) {
  2832. throw new Error('Cannot repaint time axis: no parent attached');
  2833. }
  2834. var parentContainer = this.parent.getContainer();
  2835. if (!parentContainer) {
  2836. throw new Error('Cannot repaint time axis: parent has no container element');
  2837. }
  2838. parentContainer.appendChild(frame);
  2839. changed += 1;
  2840. }
  2841. var parent = frame.parentNode;
  2842. if (parent) {
  2843. var beforeChild = frame.nextSibling;
  2844. parent.removeChild(frame); // take frame offline while updating (is almost twice as fast)
  2845. var orientation = options.orientation;
  2846. var defaultTop = (orientation == 'bottom') ? (this.props.parentHeight - this.height) + 'px' : '0px';
  2847. changed += update(frame.style, 'top', asSize(options.top, defaultTop));
  2848. changed += update(frame.style, 'left', asSize(options.left, '0px'));
  2849. changed += update(frame.style, 'width', asSize(options.width, '100%'));
  2850. changed += update(frame.style, 'height', asSize(options.height, this.height + 'px'));
  2851. // get characters width and height
  2852. this._repaintMeasureChars();
  2853. if (this.step) {
  2854. this._repaintStart();
  2855. step.first();
  2856. var xFirstMajorLabel = undefined;
  2857. var max = 0;
  2858. while (step.hasNext() && max < 1000) {
  2859. max++;
  2860. var cur = step.getCurrent(),
  2861. x = this.toScreen(cur),
  2862. isMajor = step.isMajor();
  2863. // TODO: lines must have a width, such that we can create css backgrounds
  2864. if (options.showMinorLabels) {
  2865. this._repaintMinorText(x, step.getLabelMinor());
  2866. }
  2867. if (isMajor && options.showMajorLabels) {
  2868. if (x > 0) {
  2869. if (xFirstMajorLabel == undefined) {
  2870. xFirstMajorLabel = x;
  2871. }
  2872. this._repaintMajorText(x, step.getLabelMajor());
  2873. }
  2874. this._repaintMajorLine(x);
  2875. }
  2876. else {
  2877. this._repaintMinorLine(x);
  2878. }
  2879. step.next();
  2880. }
  2881. // create a major label on the left when needed
  2882. if (options.showMajorLabels) {
  2883. var leftTime = this.toTime(0),
  2884. leftText = step.getLabelMajor(leftTime),
  2885. widthText = leftText.length * (props.majorCharWidth || 10) + 10; // upper bound estimation
  2886. if (xFirstMajorLabel == undefined || widthText < xFirstMajorLabel) {
  2887. this._repaintMajorText(0, leftText);
  2888. }
  2889. }
  2890. this._repaintEnd();
  2891. }
  2892. this._repaintLine();
  2893. // put frame online again
  2894. if (beforeChild) {
  2895. parent.insertBefore(frame, beforeChild);
  2896. }
  2897. else {
  2898. parent.appendChild(frame)
  2899. }
  2900. }
  2901. return (changed > 0);
  2902. };
  2903. /**
  2904. * Start a repaint. Move all DOM elements to a redundant list, where they
  2905. * can be picked for re-use, or can be cleaned up in the end
  2906. * @private
  2907. */
  2908. TimeAxis.prototype._repaintStart = function () {
  2909. var dom = this.dom,
  2910. redundant = dom.redundant;
  2911. redundant.majorLines = dom.majorLines;
  2912. redundant.majorTexts = dom.majorTexts;
  2913. redundant.minorLines = dom.minorLines;
  2914. redundant.minorTexts = dom.minorTexts;
  2915. dom.majorLines = [];
  2916. dom.majorTexts = [];
  2917. dom.minorLines = [];
  2918. dom.minorTexts = [];
  2919. };
  2920. /**
  2921. * End a repaint. Cleanup leftover DOM elements in the redundant list
  2922. * @private
  2923. */
  2924. TimeAxis.prototype._repaintEnd = function () {
  2925. util.forEach(this.dom.redundant, function (arr) {
  2926. while (arr.length) {
  2927. var elem = arr.pop();
  2928. if (elem && elem.parentNode) {
  2929. elem.parentNode.removeChild(elem);
  2930. }
  2931. }
  2932. });
  2933. };
  2934. /**
  2935. * Create a minor label for the axis at position x
  2936. * @param {Number} x
  2937. * @param {String} text
  2938. * @private
  2939. */
  2940. TimeAxis.prototype._repaintMinorText = function (x, text) {
  2941. // reuse redundant label
  2942. var label = this.dom.redundant.minorTexts.shift();
  2943. if (!label) {
  2944. // create new label
  2945. var content = document.createTextNode('');
  2946. label = document.createElement('div');
  2947. label.appendChild(content);
  2948. label.className = 'text minor';
  2949. this.frame.appendChild(label);
  2950. }
  2951. this.dom.minorTexts.push(label);
  2952. label.childNodes[0].nodeValue = text;
  2953. label.style.left = x + 'px';
  2954. label.style.top = this.props.minorLabelTop + 'px';
  2955. //label.title = title; // TODO: this is a heavy operation
  2956. };
  2957. /**
  2958. * Create a Major label for the axis at position x
  2959. * @param {Number} x
  2960. * @param {String} text
  2961. * @private
  2962. */
  2963. TimeAxis.prototype._repaintMajorText = function (x, text) {
  2964. // reuse redundant label
  2965. var label = this.dom.redundant.majorTexts.shift();
  2966. if (!label) {
  2967. // create label
  2968. var content = document.createTextNode(text);
  2969. label = document.createElement('div');
  2970. label.className = 'text major';
  2971. label.appendChild(content);
  2972. this.frame.appendChild(label);
  2973. }
  2974. this.dom.majorTexts.push(label);
  2975. label.childNodes[0].nodeValue = text;
  2976. label.style.top = this.props.majorLabelTop + 'px';
  2977. label.style.left = x + 'px';
  2978. //label.title = title; // TODO: this is a heavy operation
  2979. };
  2980. /**
  2981. * Create a minor line for the axis at position x
  2982. * @param {Number} x
  2983. * @private
  2984. */
  2985. TimeAxis.prototype._repaintMinorLine = function (x) {
  2986. // reuse redundant line
  2987. var line = this.dom.redundant.minorLines.shift();
  2988. if (!line) {
  2989. // create vertical line
  2990. line = document.createElement('div');
  2991. line.className = 'grid vertical minor';
  2992. this.frame.appendChild(line);
  2993. }
  2994. this.dom.minorLines.push(line);
  2995. var props = this.props;
  2996. line.style.top = props.minorLineTop + 'px';
  2997. line.style.height = props.minorLineHeight + 'px';
  2998. line.style.left = (x - props.minorLineWidth / 2) + 'px';
  2999. };
  3000. /**
  3001. * Create a Major line for the axis at position x
  3002. * @param {Number} x
  3003. * @private
  3004. */
  3005. TimeAxis.prototype._repaintMajorLine = function (x) {
  3006. // reuse redundant line
  3007. var line = this.dom.redundant.majorLines.shift();
  3008. if (!line) {
  3009. // create vertical line
  3010. line = document.createElement('DIV');
  3011. line.className = 'grid vertical major';
  3012. this.frame.appendChild(line);
  3013. }
  3014. this.dom.majorLines.push(line);
  3015. var props = this.props;
  3016. line.style.top = props.majorLineTop + 'px';
  3017. line.style.left = (x - props.majorLineWidth / 2) + 'px';
  3018. line.style.height = props.majorLineHeight + 'px';
  3019. };
  3020. /**
  3021. * Repaint the horizontal line for the axis
  3022. * @private
  3023. */
  3024. TimeAxis.prototype._repaintLine = function() {
  3025. var line = this.dom.line,
  3026. frame = this.frame,
  3027. options = this.options;
  3028. // line before all axis elements
  3029. if (options.showMinorLabels || options.showMajorLabels) {
  3030. if (line) {
  3031. // put this line at the end of all childs
  3032. frame.removeChild(line);
  3033. frame.appendChild(line);
  3034. }
  3035. else {
  3036. // create the axis line
  3037. line = document.createElement('div');
  3038. line.className = 'grid horizontal major';
  3039. frame.appendChild(line);
  3040. this.dom.line = line;
  3041. }
  3042. line.style.top = this.props.lineTop + 'px';
  3043. }
  3044. else {
  3045. if (line && axis.parentElement) {
  3046. frame.removeChild(axis.line);
  3047. delete this.dom.line;
  3048. }
  3049. }
  3050. };
  3051. /**
  3052. * Create characters used to determine the size of text on the axis
  3053. * @private
  3054. */
  3055. TimeAxis.prototype._repaintMeasureChars = function () {
  3056. // calculate the width and height of a single character
  3057. // this is used to calculate the step size, and also the positioning of the
  3058. // axis
  3059. var dom = this.dom,
  3060. text;
  3061. if (!dom.characterMinor) {
  3062. text = document.createTextNode('0');
  3063. var measureCharMinor = document.createElement('DIV');
  3064. measureCharMinor.className = 'text minor measure';
  3065. measureCharMinor.appendChild(text);
  3066. this.frame.appendChild(measureCharMinor);
  3067. dom.measureCharMinor = measureCharMinor;
  3068. }
  3069. if (!dom.characterMajor) {
  3070. text = document.createTextNode('0');
  3071. var measureCharMajor = document.createElement('DIV');
  3072. measureCharMajor.className = 'text major measure';
  3073. measureCharMajor.appendChild(text);
  3074. this.frame.appendChild(measureCharMajor);
  3075. dom.measureCharMajor = measureCharMajor;
  3076. }
  3077. };
  3078. /**
  3079. * Reflow the component
  3080. * @return {Boolean} resized
  3081. */
  3082. TimeAxis.prototype.reflow = function () {
  3083. var changed = 0,
  3084. update = util.updateProperty,
  3085. frame = this.frame,
  3086. range = this.range;
  3087. if (!range) {
  3088. throw new Error('Cannot repaint time axis: no range configured');
  3089. }
  3090. if (frame) {
  3091. changed += update(this, 'top', frame.offsetTop);
  3092. changed += update(this, 'left', frame.offsetLeft);
  3093. // calculate size of a character
  3094. var props = this.props,
  3095. showMinorLabels = this.options.showMinorLabels,
  3096. showMajorLabels = this.options.showMajorLabels,
  3097. measureCharMinor = this.dom.measureCharMinor,
  3098. measureCharMajor = this.dom.measureCharMajor;
  3099. if (measureCharMinor) {
  3100. props.minorCharHeight = measureCharMinor.clientHeight;
  3101. props.minorCharWidth = measureCharMinor.clientWidth;
  3102. }
  3103. if (measureCharMajor) {
  3104. props.majorCharHeight = measureCharMajor.clientHeight;
  3105. props.majorCharWidth = measureCharMajor.clientWidth;
  3106. }
  3107. var parentHeight = frame.parentNode ? frame.parentNode.offsetHeight : 0;
  3108. if (parentHeight != props.parentHeight) {
  3109. props.parentHeight = parentHeight;
  3110. changed += 1;
  3111. }
  3112. switch (this.options.orientation) {
  3113. case 'bottom':
  3114. props.minorLabelHeight = showMinorLabels ? props.minorCharHeight : 0;
  3115. props.majorLabelHeight = showMajorLabels ? props.majorCharHeight : 0;
  3116. props.minorLabelTop = 0;
  3117. props.majorLabelTop = props.minorLabelTop + props.minorLabelHeight;
  3118. props.minorLineTop = -this.top;
  3119. props.minorLineHeight = this.top + props.majorLabelHeight;
  3120. props.minorLineWidth = 1; // TODO: really calculate width
  3121. props.majorLineTop = -this.top;
  3122. props.majorLineHeight = this.top + props.minorLabelHeight + props.majorLabelHeight;
  3123. props.majorLineWidth = 1; // TODO: really calculate width
  3124. props.lineTop = 0;
  3125. break;
  3126. case 'top':
  3127. props.minorLabelHeight = showMinorLabels ? props.minorCharHeight : 0;
  3128. props.majorLabelHeight = showMajorLabels ? props.majorCharHeight : 0;
  3129. props.majorLabelTop = 0;
  3130. props.minorLabelTop = props.majorLabelTop + props.majorLabelHeight;
  3131. props.minorLineTop = props.minorLabelTop;
  3132. props.minorLineHeight = parentHeight - props.majorLabelHeight - this.top;
  3133. props.minorLineWidth = 1; // TODO: really calculate width
  3134. props.majorLineTop = 0;
  3135. props.majorLineHeight = parentHeight - this.top;
  3136. props.majorLineWidth = 1; // TODO: really calculate width
  3137. props.lineTop = props.majorLabelHeight + props.minorLabelHeight;
  3138. break;
  3139. default:
  3140. throw new Error('Unkown orientation "' + this.options.orientation + '"');
  3141. }
  3142. var height = props.minorLabelHeight + props.majorLabelHeight;
  3143. changed += update(this, 'width', frame.offsetWidth);
  3144. changed += update(this, 'height', height);
  3145. // calculate range and step
  3146. this._updateConversion();
  3147. var start = util.cast(range.start, 'Date'),
  3148. end = util.cast(range.end, 'Date'),
  3149. minimumStep = this.toTime((props.minorCharWidth || 10) * 5) - this.toTime(0);
  3150. this.step = new TimeStep(start, end, minimumStep);
  3151. changed += update(props.range, 'start', start.valueOf());
  3152. changed += update(props.range, 'end', end.valueOf());
  3153. changed += update(props.range, 'minimumStep', minimumStep.valueOf());
  3154. }
  3155. return (changed > 0);
  3156. };
  3157. /**
  3158. * Calculate the factor and offset to convert a position on screen to the
  3159. * corresponding date and vice versa.
  3160. * After the method _updateConversion is executed once, the methods toTime
  3161. * and toScreen can be used.
  3162. * @private
  3163. */
  3164. TimeAxis.prototype._updateConversion = function() {
  3165. var range = this.range;
  3166. if (!range) {
  3167. throw new Error('No range configured');
  3168. }
  3169. if (range.conversion) {
  3170. this.conversion = range.conversion(this.width);
  3171. }
  3172. else {
  3173. this.conversion = Range.conversion(range.start, range.end, this.width);
  3174. }
  3175. };
  3176. /**
  3177. * An ItemSet holds a set of items and ranges which can be displayed in a
  3178. * range. The width is determined by the parent of the ItemSet, and the height
  3179. * is determined by the size of the items.
  3180. * @param {Component} parent
  3181. * @param {Component[]} [depends] Components on which this components depends
  3182. * (except for the parent)
  3183. * @param {Object} [options] See ItemSet.setOptions for the available
  3184. * options.
  3185. * @constructor ItemSet
  3186. * @extends Panel
  3187. */
  3188. function ItemSet(parent, depends, options) {
  3189. this.id = util.randomUUID();
  3190. this.parent = parent;
  3191. this.depends = depends;
  3192. // one options object is shared by this itemset and all its items
  3193. this.options = {
  3194. style: 'box',
  3195. align: 'center',
  3196. orientation: 'bottom',
  3197. margin: {
  3198. axis: 20,
  3199. item: 10
  3200. },
  3201. padding: 5
  3202. };
  3203. var me = this;
  3204. this.data = null; // DataSet
  3205. this.range = null; // Range or Object {start: number, end: number}
  3206. this.listeners = {
  3207. 'add': function (event, params) {
  3208. me._onAdd(params.items);
  3209. },
  3210. 'update': function (event, params) {
  3211. me._onUpdate(params.items);
  3212. },
  3213. 'remove': function (event, params) {
  3214. me._onRemove(params.items);
  3215. }
  3216. };
  3217. this.items = {};
  3218. this.queue = {}; // queue with items to be added/updated/removed
  3219. this.stack = new Stack(this);
  3220. this.conversion = null;
  3221. this.setOptions(options);
  3222. }
  3223. ItemSet.prototype = new Panel();
  3224. /**
  3225. * Set options for the ItemSet. Existing options will be extended/overwritten.
  3226. * @param {Object} [options] The following options are available:
  3227. * {String | function} [className]
  3228. * class name for the itemset
  3229. * {String} [style]
  3230. * Default style for the items. Choose from 'box'
  3231. * (default), 'point', or 'range'. The default
  3232. * Style can be overwritten by individual items.
  3233. * {String} align
  3234. * Alignment for the items, only applicable for
  3235. * ItemBox. Choose 'center' (default), 'left', or
  3236. * 'right'.
  3237. * {String} orientation
  3238. * Orientation of the item set. Choose 'top' or
  3239. * 'bottom' (default).
  3240. * {Number} margin.axis
  3241. * Margin between the axis and the items in pixels.
  3242. * Default is 20.
  3243. * {Number} margin.item
  3244. * Margin between items in pixels. Default is 10.
  3245. * {Number} padding
  3246. * Padding of the contents of an item in pixels.
  3247. * Must correspond with the items css. Default is 5.
  3248. */
  3249. ItemSet.prototype.setOptions = function (options) {
  3250. util.extend(this.options, options);
  3251. // TODO: ItemSet should also attach event listeners for rangechange and rangechanged, like timeaxis
  3252. this.stack.setOptions(this.options);
  3253. };
  3254. /**
  3255. * Set range (start and end).
  3256. * @param {Range | Object} range A Range or an object containing start and end.
  3257. */
  3258. ItemSet.prototype.setRange = function (range) {
  3259. if (!(range instanceof Range) && (!range || !range.start || !range.end)) {
  3260. throw new TypeError('Range must be an instance of Range, ' +
  3261. 'or an object containing start and end.');
  3262. }
  3263. this.range = range;
  3264. };
  3265. /**
  3266. * Repaint the component
  3267. * @return {Boolean} changed
  3268. */
  3269. ItemSet.prototype.repaint = function () {
  3270. var changed = 0,
  3271. update = util.updateProperty,
  3272. asSize = util.option.asSize,
  3273. options = this.options,
  3274. frame = this.frame;
  3275. if (!frame) {
  3276. frame = document.createElement('div');
  3277. frame.className = 'itemset';
  3278. if (options.className) {
  3279. util.addClassName(frame, util.option.asString(options.className));
  3280. }
  3281. this.frame = frame;
  3282. changed += 1;
  3283. }
  3284. if (!frame.parentNode) {
  3285. if (!this.parent) {
  3286. throw new Error('Cannot repaint itemset: no parent attached');
  3287. }
  3288. var parentContainer = this.parent.getContainer();
  3289. if (!parentContainer) {
  3290. throw new Error('Cannot repaint itemset: parent has no container element');
  3291. }
  3292. parentContainer.appendChild(frame);
  3293. changed += 1;
  3294. }
  3295. changed += update(frame.style, 'height', asSize(options.height, this.height + 'px'));
  3296. changed += update(frame.style, 'top', asSize(options.top, '0px'));
  3297. changed += update(frame.style, 'left', asSize(options.left, '0px'));
  3298. changed += update(frame.style, 'width', asSize(options.width, '100%'));
  3299. this._updateConversion();
  3300. var me = this,
  3301. queue = this.queue,
  3302. data = this.data,
  3303. items = this.items,
  3304. dataOptions = {
  3305. fields: ['id', 'start', 'end', 'content', 'type']
  3306. };
  3307. // TODO: copy options from the itemset itself?
  3308. // TODO: make orientation dynamically changable for the items
  3309. // show/hide added/changed/removed items
  3310. Object.keys(queue).forEach(function (id) {
  3311. var entry = queue[id];
  3312. var item = entry.item;
  3313. //noinspection FallthroughInSwitchStatementJS
  3314. switch (entry.action) {
  3315. case 'add':
  3316. case 'update':
  3317. var itemData = data.get(id, dataOptions);
  3318. var type = itemData.type ||
  3319. (itemData.start && itemData.end && 'range') ||
  3320. 'box';
  3321. var constructor = itemTypes[type];
  3322. // TODO: how to handle items with invalid data? hide them and give a warning? or throw an error?
  3323. if (item) {
  3324. // update item
  3325. if (!constructor || !(item instanceof constructor)) {
  3326. // item type has changed, delete the item
  3327. item.visible = false;
  3328. changed += item.repaint();
  3329. item = null;
  3330. }
  3331. else {
  3332. item.data = itemData; // TODO: create a method item.setData ?
  3333. changed += item.repaint();
  3334. }
  3335. }
  3336. if (!item) {
  3337. // create item
  3338. if (constructor) {
  3339. item = new constructor(me, itemData, options);
  3340. changed += item.repaint();
  3341. }
  3342. else {
  3343. throw new TypeError('Unknown item type "' + type + '"');
  3344. }
  3345. }
  3346. // update lists
  3347. items[id] = item;
  3348. delete queue[id];
  3349. break;
  3350. case 'remove':
  3351. if (item) {
  3352. // TODO: remove dom of the item
  3353. item.visible = false;
  3354. changed += item.repaint();
  3355. }
  3356. // update lists
  3357. delete items[id];
  3358. delete queue[id];
  3359. break;
  3360. default:
  3361. console.log('Error: unknown action "' + entry.action + '"');
  3362. }
  3363. });
  3364. // reposition all items
  3365. util.forEach(this.items, function (item) {
  3366. item.reposition();
  3367. });
  3368. return (changed > 0);
  3369. };
  3370. /**
  3371. * Reflow the component
  3372. * @return {Boolean} resized
  3373. */
  3374. ItemSet.prototype.reflow = function () {
  3375. var changed = 0,
  3376. options = this.options,
  3377. update = util.updateProperty,
  3378. frame = this.frame;
  3379. if (frame) {
  3380. this._updateConversion();
  3381. util.forEach(this.items, function (item) {
  3382. changed += item.reflow();
  3383. });
  3384. // TODO: stack.update should be triggered via an event, in stack itself
  3385. // TODO: only update the stack when there are changed items
  3386. this.stack.update();
  3387. if (options.height != null) {
  3388. changed += update(this, 'height', frame.offsetHeight);
  3389. }
  3390. else {
  3391. // height is not specified, determine the height from the height and positioned items
  3392. var frameHeight = this.height;
  3393. var maxHeight = 0;
  3394. if (options.orientation == 'top') {
  3395. util.forEach(this.items, function (item) {
  3396. maxHeight = Math.max(maxHeight, item.top + item.height);
  3397. });
  3398. }
  3399. else {
  3400. // orientation == 'bottom'
  3401. util.forEach(this.items, function (item) {
  3402. maxHeight = Math.max(maxHeight, frameHeight - item.top);
  3403. });
  3404. }
  3405. changed += update(this, 'height', maxHeight + options.margin.axis);
  3406. }
  3407. // calculate height from items
  3408. changed += update(this, 'top', frame.offsetTop);
  3409. changed += update(this, 'left', frame.offsetLeft);
  3410. changed += update(this, 'width', frame.offsetWidth);
  3411. }
  3412. else {
  3413. changed += 1;
  3414. }
  3415. return (changed > 0);
  3416. };
  3417. /**
  3418. * Set data
  3419. * @param {DataSet | Array | DataTable} data
  3420. */
  3421. ItemSet.prototype.setData = function(data) {
  3422. // unsubscribe from current dataset
  3423. var current = this.data;
  3424. if (current) {
  3425. util.forEach(this.listeners, function (callback, event) {
  3426. current.unsubscribe(event, callback);
  3427. });
  3428. }
  3429. if (data instanceof DataSet) {
  3430. this.data = data;
  3431. }
  3432. else {
  3433. this.data = new DataSet({
  3434. fieldTypes: {
  3435. start: 'Date',
  3436. end: 'Date'
  3437. }
  3438. });
  3439. this.data.add(data);
  3440. }
  3441. var id = this.id;
  3442. var me = this;
  3443. util.forEach(this.listeners, function (callback, event) {
  3444. me.data.subscribe(event, callback, id);
  3445. });
  3446. var dataItems = this.data.get({filter: ['id']});
  3447. var ids = [];
  3448. util.forEach(dataItems, function (dataItem, index) {
  3449. ids[index] = dataItem.id;
  3450. });
  3451. this._onAdd(ids);
  3452. };
  3453. /**
  3454. * Handle updated items
  3455. * @param {Number[]} ids
  3456. * @private
  3457. */
  3458. ItemSet.prototype._onUpdate = function(ids) {
  3459. this._toQueue(ids, 'update');
  3460. };
  3461. /**
  3462. * Handle changed items
  3463. * @param {Number[]} ids
  3464. * @private
  3465. */
  3466. ItemSet.prototype._onAdd = function(ids) {
  3467. this._toQueue(ids, 'add');
  3468. };
  3469. /**
  3470. * Handle removed items
  3471. * @param {Number[]} ids
  3472. * @private
  3473. */
  3474. ItemSet.prototype._onRemove = function(ids) {
  3475. this._toQueue(ids, 'remove');
  3476. };
  3477. /**
  3478. * Put items in the queue to be added/updated/remove
  3479. * @param {Number[]} ids
  3480. * @param {String} action can be 'add', 'update', 'remove'
  3481. */
  3482. ItemSet.prototype._toQueue = function (ids, action) {
  3483. var items = this.items;
  3484. var queue = this.queue;
  3485. ids.forEach(function (id) {
  3486. var entry = queue[id];
  3487. if (entry) {
  3488. // already queued, update the action of the entry
  3489. entry.action = action;
  3490. }
  3491. else {
  3492. // not yet queued, add an entry to the queue
  3493. queue[id] = {
  3494. item: items[id] || null,
  3495. action: action
  3496. };
  3497. }
  3498. });
  3499. if (this.controller) {
  3500. //this.requestReflow();
  3501. this.requestRepaint();
  3502. }
  3503. };
  3504. /**
  3505. * Calculate the factor and offset to convert a position on screen to the
  3506. * corresponding date and vice versa.
  3507. * After the method _updateConversion is executed once, the methods toTime
  3508. * and toScreen can be used.
  3509. * @private
  3510. */
  3511. ItemSet.prototype._updateConversion = function() {
  3512. var range = this.range;
  3513. if (!range) {
  3514. throw new Error('No range configured');
  3515. }
  3516. if (range.conversion) {
  3517. this.conversion = range.conversion(this.width);
  3518. }
  3519. else {
  3520. this.conversion = Range.conversion(range.start, range.end, this.width);
  3521. }
  3522. };
  3523. /**
  3524. * Convert a position on screen (pixels) to a datetime
  3525. * Before this method can be used, the method _updateConversion must be
  3526. * executed once.
  3527. * @param {int} x Position on the screen in pixels
  3528. * @return {Date} time The datetime the corresponds with given position x
  3529. */
  3530. ItemSet.prototype.toTime = function(x) {
  3531. var conversion = this.conversion;
  3532. return new Date(x / conversion.factor + conversion.offset);
  3533. };
  3534. /**
  3535. * Convert a datetime (Date object) into a position on the screen
  3536. * Before this method can be used, the method _updateConversion must be
  3537. * executed once.
  3538. * @param {Date} time A date
  3539. * @return {int} x The position on the screen in pixels which corresponds
  3540. * with the given date.
  3541. */
  3542. ItemSet.prototype.toScreen = function(time) {
  3543. var conversion = this.conversion;
  3544. return (time.valueOf() - conversion.offset) * conversion.factor;
  3545. };
  3546. /**
  3547. * @constructor Item
  3548. * @param {ItemSet} parent
  3549. * @param {Object} data Object containing (optional) parameters type,
  3550. * start, end, content, group, className.
  3551. * @param {Object} [options] Options to set initial property values
  3552. * // TODO: describe available options
  3553. */
  3554. function Item (parent, data, options) {
  3555. this.parent = parent;
  3556. this.data = data;
  3557. this.selected = false;
  3558. this.visible = true;
  3559. this.dom = null;
  3560. this.options = options;
  3561. }
  3562. Item.prototype = new Component();
  3563. /**
  3564. * Select current item
  3565. */
  3566. Item.prototype.select = function () {
  3567. this.selected = true;
  3568. };
  3569. /**
  3570. * Unselect current item
  3571. */
  3572. Item.prototype.unselect = function () {
  3573. this.selected = false;
  3574. };
  3575. // create a namespace for all item types
  3576. var itemTypes = {};
  3577. /**
  3578. * @constructor ItemBox
  3579. * @extends Item
  3580. * @param {ItemSet} parent
  3581. * @param {Object} data Object containing parameters start
  3582. * content, className.
  3583. * @param {Object} [options] Options to set initial property values
  3584. * // TODO: describe available options
  3585. */
  3586. function ItemBox (parent, data, options) {
  3587. this.props = {
  3588. dot: {
  3589. left: 0,
  3590. top: 0,
  3591. width: 0,
  3592. height: 0
  3593. },
  3594. line: {
  3595. top: 0,
  3596. left: 0,
  3597. width: 0,
  3598. height: 0
  3599. }
  3600. };
  3601. Item.call(this, parent, data, options);
  3602. }
  3603. ItemBox.prototype = new Item (null, null);
  3604. // register the ItemBox in the item types
  3605. itemTypes['box'] = ItemBox;
  3606. /**
  3607. * Select the item
  3608. * @override
  3609. */
  3610. ItemBox.prototype.select = function () {
  3611. this.selected = true;
  3612. // TODO: select and unselect
  3613. };
  3614. /**
  3615. * Unselect the item
  3616. * @override
  3617. */
  3618. ItemBox.prototype.unselect = function () {
  3619. this.selected = false;
  3620. // TODO: select and unselect
  3621. };
  3622. /**
  3623. * Repaint the item
  3624. * @return {Boolean} changed
  3625. */
  3626. ItemBox.prototype.repaint = function () {
  3627. // TODO: make an efficient repaint
  3628. var changed = false;
  3629. var dom = this.dom;
  3630. if (this.visible) {
  3631. if (!dom) {
  3632. this._create();
  3633. changed = true;
  3634. }
  3635. dom = this.dom;
  3636. if (dom) {
  3637. if (!this.options && !this.parent) {
  3638. throw new Error('Cannot repaint item: no parent attached');
  3639. }
  3640. var parentContainer = this.parent.getContainer();
  3641. if (!parentContainer) {
  3642. throw new Error('Cannot repaint time axis: parent has no container element');
  3643. }
  3644. if (!dom.box.parentNode) {
  3645. parentContainer.appendChild(dom.box);
  3646. changed = true;
  3647. }
  3648. if (!dom.line.parentNode) {
  3649. parentContainer.appendChild(dom.line);
  3650. changed = true;
  3651. }
  3652. if (!dom.dot.parentNode) {
  3653. parentContainer.appendChild(dom.dot);
  3654. changed = true;
  3655. }
  3656. // update contents
  3657. if (this.data.content != this.content) {
  3658. this.content = this.data.content;
  3659. if (this.content instanceof Element) {
  3660. dom.content.innerHTML = '';
  3661. dom.content.appendChild(this.content);
  3662. }
  3663. else if (this.data.content != undefined) {
  3664. dom.content.innerHTML = this.content;
  3665. }
  3666. else {
  3667. throw new Error('Property "content" missing in item ' + this.data.id);
  3668. }
  3669. changed = true;
  3670. }
  3671. // update class
  3672. var className = (this.data.className? ' ' + this.data.className : '') +
  3673. (this.selected ? ' selected' : '');
  3674. if (this.className != className) {
  3675. this.className = className;
  3676. dom.box.className = 'item box' + className;
  3677. dom.line.className = 'item line' + className;
  3678. dom.dot.className = 'item dot' + className;
  3679. changed = true;
  3680. }
  3681. }
  3682. }
  3683. else {
  3684. // hide when visible
  3685. if (dom) {
  3686. if (dom.box.parentNode) {
  3687. dom.box.parentNode.removeChild(dom.box);
  3688. changed = true;
  3689. }
  3690. if (dom.line.parentNode) {
  3691. dom.line.parentNode.removeChild(dom.line);
  3692. changed = true;
  3693. }
  3694. if (dom.dot.parentNode) {
  3695. dom.dot.parentNode.removeChild(dom.dot);
  3696. changed = true;
  3697. }
  3698. }
  3699. }
  3700. return changed;
  3701. };
  3702. /**
  3703. * Reflow the item: calculate its actual size and position from the DOM
  3704. * @return {boolean} resized returns true if the axis is resized
  3705. * @override
  3706. */
  3707. ItemBox.prototype.reflow = function () {
  3708. if (this.data.start == undefined) {
  3709. throw new Error('Property "start" missing in item ' + this.data.id);
  3710. }
  3711. var update = util.updateProperty,
  3712. dom = this.dom,
  3713. props = this.props,
  3714. options = this.options,
  3715. start = this.parent.toScreen(this.data.start),
  3716. align = options && options.align,
  3717. orientation = options.orientation,
  3718. changed = 0,
  3719. top,
  3720. left;
  3721. if (dom) {
  3722. changed += update(props.dot, 'height', dom.dot.offsetHeight);
  3723. changed += update(props.dot, 'width', dom.dot.offsetWidth);
  3724. changed += update(props.line, 'width', dom.line.offsetWidth);
  3725. changed += update(props.line, 'width', dom.line.offsetWidth);
  3726. changed += update(this, 'width', dom.box.offsetWidth);
  3727. changed += update(this, 'height', dom.box.offsetHeight);
  3728. if (align == 'right') {
  3729. left = start - this.width;
  3730. }
  3731. else if (align == 'left') {
  3732. left = start;
  3733. }
  3734. else {
  3735. // default or 'center'
  3736. left = start - this.width / 2;
  3737. }
  3738. changed += update(this, 'left', left);
  3739. changed += update(props.line, 'left', start - props.line.width / 2);
  3740. changed += update(props.dot, 'left', start - props.dot.width / 2);
  3741. if (orientation == 'top') {
  3742. top = options.margin.axis;
  3743. changed += update(this, 'top', top);
  3744. changed += update(props.line, 'top', 0);
  3745. changed += update(props.line, 'height', top);
  3746. changed += update(props.dot, 'top', -props.dot.height / 2);
  3747. }
  3748. else {
  3749. // default or 'bottom'
  3750. var parentHeight = this.parent.height;
  3751. top = parentHeight - this.height - options.margin.axis;
  3752. changed += update(this, 'top', top);
  3753. changed += update(props.line, 'top', top + this.height);
  3754. changed += update(props.line, 'height', Math.max(options.margin.axis, 0));
  3755. changed += update(props.dot, 'top', parentHeight - props.dot.height / 2);
  3756. }
  3757. }
  3758. else {
  3759. changed += 1;
  3760. }
  3761. return (changed > 0);
  3762. };
  3763. /**
  3764. * Create an items DOM
  3765. * @private
  3766. */
  3767. ItemBox.prototype._create = function () {
  3768. var dom = this.dom;
  3769. if (!dom) {
  3770. this.dom = dom = {};
  3771. // create the box
  3772. dom.box = document.createElement('DIV');
  3773. // className is updated in repaint()
  3774. // contents box (inside the background box). used for making margins
  3775. dom.content = document.createElement('DIV');
  3776. dom.content.className = 'content';
  3777. dom.box.appendChild(dom.content);
  3778. // line to axis
  3779. dom.line = document.createElement('DIV');
  3780. dom.line.className = 'line';
  3781. // dot on axis
  3782. dom.dot = document.createElement('DIV');
  3783. dom.dot.className = 'dot';
  3784. }
  3785. };
  3786. /**
  3787. * Reposition the item, recalculate its left, top, and width, using the current
  3788. * range and size of the items itemset
  3789. * @override
  3790. */
  3791. ItemBox.prototype.reposition = function () {
  3792. var dom = this.dom,
  3793. props = this.props,
  3794. orientation = this.options.orientation;
  3795. if (dom) {
  3796. var box = dom.box,
  3797. line = dom.line,
  3798. dot = dom.dot;
  3799. box.style.left = this.left + 'px';
  3800. box.style.top = this.top + 'px';
  3801. line.style.left = props.line.left + 'px';
  3802. if (orientation == 'top') {
  3803. line.style.top = 0 + 'px';
  3804. line.style.height = this.top + 'px';
  3805. }
  3806. else {
  3807. // orientation 'bottom'
  3808. line.style.top = props.line.top + 'px';
  3809. line.style.top = (this.top + this.height) + 'px';
  3810. line.style.height = (props.dot.top - this.top - this.height) + 'px';
  3811. }
  3812. dot.style.left = props.dot.left + 'px';
  3813. dot.style.top = props.dot.top + 'px';
  3814. }
  3815. };
  3816. /**
  3817. * @constructor ItemPoint
  3818. * @extends Item
  3819. * @param {ItemSet} parent
  3820. * @param {Object} data Object containing parameters start
  3821. * content, className.
  3822. * @param {Object} [options] Options to set initial property values
  3823. * // TODO: describe available options
  3824. */
  3825. function ItemPoint (parent, data, options) {
  3826. this.props = {
  3827. dot: {
  3828. top: 0,
  3829. width: 0,
  3830. height: 0
  3831. },
  3832. content: {
  3833. height: 0,
  3834. marginLeft: 0
  3835. }
  3836. };
  3837. Item.call(this, parent, data, options);
  3838. }
  3839. ItemPoint.prototype = new Item (null, null);
  3840. // register the ItemPoint in the item types
  3841. itemTypes['point'] = ItemPoint;
  3842. /**
  3843. * Select the item
  3844. * @override
  3845. */
  3846. ItemPoint.prototype.select = function () {
  3847. this.selected = true;
  3848. // TODO: select and unselect
  3849. };
  3850. /**
  3851. * Unselect the item
  3852. * @override
  3853. */
  3854. ItemPoint.prototype.unselect = function () {
  3855. this.selected = false;
  3856. // TODO: select and unselect
  3857. };
  3858. /**
  3859. * Repaint the item
  3860. * @return {Boolean} changed
  3861. */
  3862. ItemPoint.prototype.repaint = function () {
  3863. // TODO: make an efficient repaint
  3864. var changed = false;
  3865. var dom = this.dom;
  3866. if (this.visible) {
  3867. if (!dom) {
  3868. this._create();
  3869. changed = true;
  3870. }
  3871. dom = this.dom;
  3872. if (dom) {
  3873. if (!this.options && !this.options.parent) {
  3874. throw new Error('Cannot repaint item: no parent attached');
  3875. }
  3876. var parentContainer = this.parent.getContainer();
  3877. if (!parentContainer) {
  3878. throw new Error('Cannot repaint time axis: parent has no container element');
  3879. }
  3880. if (!dom.point.parentNode) {
  3881. parentContainer.appendChild(dom.point);
  3882. changed = true;
  3883. }
  3884. // update contents
  3885. if (this.data.content != this.content) {
  3886. this.content = this.data.content;
  3887. if (this.content instanceof Element) {
  3888. dom.content.innerHTML = '';
  3889. dom.content.appendChild(this.content);
  3890. }
  3891. else if (this.data.content != undefined) {
  3892. dom.content.innerHTML = this.content;
  3893. }
  3894. else {
  3895. throw new Error('Property "content" missing in item ' + this.data.id);
  3896. }
  3897. changed = true;
  3898. }
  3899. // update class
  3900. var className = (this.data.className? ' ' + this.data.className : '') +
  3901. (this.selected ? ' selected' : '');
  3902. if (this.className != className) {
  3903. this.className = className;
  3904. dom.point.className = 'item point' + className;
  3905. changed = true;
  3906. }
  3907. }
  3908. }
  3909. else {
  3910. // hide when visible
  3911. if (dom) {
  3912. if (dom.point.parentNode) {
  3913. dom.point.parentNode.removeChild(dom.point);
  3914. changed = true;
  3915. }
  3916. }
  3917. }
  3918. return changed;
  3919. };
  3920. /**
  3921. * Reflow the item: calculate its actual size from the DOM
  3922. * @return {boolean} resized returns true if the axis is resized
  3923. * @override
  3924. */
  3925. ItemPoint.prototype.reflow = function () {
  3926. if (this.data.start == undefined) {
  3927. throw new Error('Property "start" missing in item ' + this.data.id);
  3928. }
  3929. var update = util.updateProperty,
  3930. dom = this.dom,
  3931. props = this.props,
  3932. options = this.options,
  3933. orientation = options.orientation,
  3934. start = this.parent.toScreen(this.data.start),
  3935. changed = 0,
  3936. top;
  3937. if (dom) {
  3938. changed += update(this, 'width', dom.point.offsetWidth);
  3939. changed += update(this, 'height', dom.point.offsetHeight);
  3940. changed += update(props.dot, 'width', dom.dot.offsetWidth);
  3941. changed += update(props.dot, 'height', dom.dot.offsetHeight);
  3942. changed += update(props.content, 'height', dom.content.offsetHeight);
  3943. if (orientation == 'top') {
  3944. top = options.margin.axis;
  3945. }
  3946. else {
  3947. // default or 'bottom'
  3948. var parentHeight = this.parent.height;
  3949. top = parentHeight - this.height - options.margin.axis;
  3950. }
  3951. changed += update(this, 'top', top);
  3952. changed += update(this, 'left', start - props.dot.width / 2);
  3953. changed += update(props.content, 'marginLeft', 1.5 * props.dot.width);
  3954. //changed += update(props.content, 'marginRight', 0.5 * props.dot.width); // TODO
  3955. changed += update(props.dot, 'top', (this.height - props.dot.height) / 2);
  3956. }
  3957. else {
  3958. changed += 1;
  3959. }
  3960. return (changed > 0);
  3961. };
  3962. /**
  3963. * Create an items DOM
  3964. * @private
  3965. */
  3966. ItemPoint.prototype._create = function () {
  3967. var dom = this.dom;
  3968. if (!dom) {
  3969. this.dom = dom = {};
  3970. // background box
  3971. dom.point = document.createElement('div');
  3972. // className is updated in repaint()
  3973. // contents box, right from the dot
  3974. dom.content = document.createElement('div');
  3975. dom.content.className = 'content';
  3976. dom.point.appendChild(dom.content);
  3977. // dot at start
  3978. dom.dot = document.createElement('div');
  3979. dom.dot.className = 'dot';
  3980. dom.point.appendChild(dom.dot);
  3981. }
  3982. };
  3983. /**
  3984. * Reposition the item, recalculate its left, top, and width, using the current
  3985. * range and size of the items itemset
  3986. * @override
  3987. */
  3988. ItemPoint.prototype.reposition = function () {
  3989. var dom = this.dom,
  3990. props = this.props;
  3991. if (dom) {
  3992. dom.point.style.top = this.top + 'px';
  3993. dom.point.style.left = this.left + 'px';
  3994. dom.content.style.marginLeft = props.content.marginLeft + 'px';
  3995. //dom.content.style.marginRight = props.content.marginRight + 'px'; // TODO
  3996. dom.dot.style.top = props.dot.top + 'px';
  3997. }
  3998. };
  3999. /**
  4000. * @constructor ItemRange
  4001. * @extends Item
  4002. * @param {ItemSet} parent
  4003. * @param {Object} data Object containing parameters start, end
  4004. * content, className.
  4005. * @param {Object} [options] Options to set initial property values
  4006. * // TODO: describe available options
  4007. */
  4008. function ItemRange (parent, data, options) {
  4009. this.props = {
  4010. content: {
  4011. left: 0,
  4012. width: 0
  4013. }
  4014. };
  4015. Item.call(this, parent, data, options);
  4016. }
  4017. ItemRange.prototype = new Item (null, null);
  4018. // register the ItemBox in the item types
  4019. itemTypes['range'] = ItemRange;
  4020. /**
  4021. * Select the item
  4022. * @override
  4023. */
  4024. ItemRange.prototype.select = function () {
  4025. this.selected = true;
  4026. // TODO: select and unselect
  4027. };
  4028. /**
  4029. * Unselect the item
  4030. * @override
  4031. */
  4032. ItemRange.prototype.unselect = function () {
  4033. this.selected = false;
  4034. // TODO: select and unselect
  4035. };
  4036. /**
  4037. * Repaint the item
  4038. * @return {Boolean} changed
  4039. */
  4040. ItemRange.prototype.repaint = function () {
  4041. // TODO: make an efficient repaint
  4042. var changed = false;
  4043. var dom = this.dom;
  4044. if (this.visible) {
  4045. if (!dom) {
  4046. this._create();
  4047. changed = true;
  4048. }
  4049. dom = this.dom;
  4050. if (dom) {
  4051. if (!this.options && !this.options.parent) {
  4052. throw new Error('Cannot repaint item: no parent attached');
  4053. }
  4054. var parentContainer = this.parent.getContainer();
  4055. if (!parentContainer) {
  4056. throw new Error('Cannot repaint time axis: parent has no container element');
  4057. }
  4058. if (!dom.box.parentNode) {
  4059. parentContainer.appendChild(dom.box);
  4060. changed = true;
  4061. }
  4062. // update content
  4063. if (this.data.content != this.content) {
  4064. this.content = this.data.content;
  4065. if (this.content instanceof Element) {
  4066. dom.content.innerHTML = '';
  4067. dom.content.appendChild(this.content);
  4068. }
  4069. else if (this.data.content != undefined) {
  4070. dom.content.innerHTML = this.content;
  4071. }
  4072. else {
  4073. throw new Error('Property "content" missing in item ' + this.data.id);
  4074. }
  4075. changed = true;
  4076. }
  4077. // update class
  4078. var className = this.data.className ? ('' + this.data.className) : '';
  4079. if (this.className != className) {
  4080. this.className = className;
  4081. dom.box.className = 'item range' + className;
  4082. changed = true;
  4083. }
  4084. }
  4085. }
  4086. else {
  4087. // hide when visible
  4088. if (dom) {
  4089. if (dom.box.parentNode) {
  4090. dom.box.parentNode.removeChild(dom.box);
  4091. changed = true;
  4092. }
  4093. }
  4094. }
  4095. return changed;
  4096. };
  4097. /**
  4098. * Reflow the item: calculate its actual size from the DOM
  4099. * @return {boolean} resized returns true if the axis is resized
  4100. * @override
  4101. */
  4102. ItemRange.prototype.reflow = function () {
  4103. if (this.data.start == undefined) {
  4104. throw new Error('Property "start" missing in item ' + this.data.id);
  4105. }
  4106. if (this.data.end == undefined) {
  4107. throw new Error('Property "end" missing in item ' + this.data.id);
  4108. }
  4109. var dom = this.dom,
  4110. props = this.props,
  4111. options = this.options,
  4112. parent = this.parent,
  4113. start = parent.toScreen(this.data.start),
  4114. end = parent.toScreen(this.data.end),
  4115. changed = 0;
  4116. if (dom) {
  4117. var update = util.updateProperty,
  4118. box = dom.box,
  4119. parentWidth = parent.width,
  4120. orientation = options.orientation,
  4121. contentLeft,
  4122. top;
  4123. changed += update(props.content, 'width', dom.content.offsetWidth);
  4124. changed += update(this, 'height', box.offsetHeight);
  4125. // limit the width of the this, as browsers cannot draw very wide divs
  4126. if (start < -parentWidth) {
  4127. start = -parentWidth;
  4128. }
  4129. if (end > 2 * parentWidth) {
  4130. end = 2 * parentWidth;
  4131. }
  4132. // when range exceeds left of the window, position the contents at the left of the visible area
  4133. if (start < 0) {
  4134. contentLeft = Math.min(-start,
  4135. (end - start - props.content.width - 2 * options.padding));
  4136. // TODO: remove the need for options.padding. it's terrible.
  4137. }
  4138. else {
  4139. contentLeft = 0;
  4140. }
  4141. changed += update(props.content, 'left', contentLeft);
  4142. if (orientation == 'top') {
  4143. top = options.margin.axis;
  4144. changed += update(this, 'top', top);
  4145. }
  4146. else {
  4147. // default or 'bottom'
  4148. top = parent.height - this.height - options.margin.axis;
  4149. changed += update(this, 'top', top);
  4150. }
  4151. changed += update(this, 'left', start);
  4152. changed += update(this, 'width', Math.max(end - start, 1)); // TODO: reckon with border width;
  4153. }
  4154. else {
  4155. changed += 1;
  4156. }
  4157. return (changed > 0);
  4158. };
  4159. /**
  4160. * Create an items DOM
  4161. * @private
  4162. */
  4163. ItemRange.prototype._create = function () {
  4164. var dom = this.dom;
  4165. if (!dom) {
  4166. this.dom = dom = {};
  4167. // background box
  4168. dom.box = document.createElement('div');
  4169. // className is updated in repaint()
  4170. // contents box
  4171. dom.content = document.createElement('div');
  4172. dom.content.className = 'content';
  4173. dom.box.appendChild(dom.content);
  4174. }
  4175. };
  4176. /**
  4177. * Reposition the item, recalculate its left, top, and width, using the current
  4178. * range and size of the items itemset
  4179. * @override
  4180. */
  4181. ItemRange.prototype.reposition = function () {
  4182. var dom = this.dom,
  4183. props = this.props;
  4184. if (dom) {
  4185. dom.box.style.top = this.top + 'px';
  4186. dom.box.style.left = this.left + 'px';
  4187. dom.box.style.width = this.width + 'px';
  4188. dom.content.style.left = props.content.left + 'px';
  4189. }
  4190. };
  4191. /**
  4192. * Create a timeline visualization
  4193. * @param {HTMLElement} container
  4194. * @param {DataSet | Array | DataTable} [data]
  4195. * @param {Object} [options] See Timeline.setOptions for the available options.
  4196. * @constructor
  4197. */
  4198. function Timeline (container, data, options) {
  4199. var me = this;
  4200. this.options = {
  4201. orientation: 'bottom',
  4202. zoomMin: 10, // milliseconds
  4203. zoomMax: 1000 * 60 * 60 * 24 * 365 * 10000, // milliseconds
  4204. moveable: true,
  4205. zoomable: true
  4206. };
  4207. // controller
  4208. this.controller = new Controller();
  4209. // main panel
  4210. if (!container) {
  4211. throw new Error('No container element provided');
  4212. }
  4213. this.main = new RootPanel(container, {
  4214. autoResize: false,
  4215. height: function () {
  4216. return me.timeaxis.height + me.itemset.height;
  4217. }
  4218. });
  4219. this.controller.add(this.main);
  4220. // range
  4221. var now = moment().minutes(0).seconds(0).milliseconds(0);
  4222. var start = options.start && options.start.valueOf() || now.clone().add('days', -3).valueOf();
  4223. var end = options.end && options.end.valueOf() || moment(start).clone().add('days', 7).valueOf();
  4224. // TODO: if start and end are not provided, calculate range from the dataset
  4225. this.range = new Range({
  4226. start: start,
  4227. end: end
  4228. });
  4229. // TODO: reckon with options moveable and zoomable
  4230. this.range.subscribe(this.main, 'move', 'horizontal');
  4231. this.range.subscribe(this.main, 'zoom', 'horizontal');
  4232. this.range.on('rangechange', function () {
  4233. // TODO: fix the delay in reflow/repaint, does not feel snappy
  4234. me.controller.requestReflow();
  4235. });
  4236. this.range.on('rangechanged', function () {
  4237. me.controller.requestReflow();
  4238. });
  4239. // TODO: put the listeners in setOptions, be able to dynamically change with options moveable and zoomable
  4240. // time axis
  4241. this.timeaxis = new TimeAxis(this.main, null, {
  4242. orientation: this.options.orientation,
  4243. range: this.range
  4244. });
  4245. this.timeaxis.setRange(this.range);
  4246. this.controller.add(this.timeaxis);
  4247. // items panel
  4248. this.itemset = new ItemSet(this.main, [this.timeaxis], {
  4249. orientation: this.options.orientation,
  4250. range: this.range,
  4251. data: data
  4252. });
  4253. this.itemset.setRange(this.range);
  4254. if (data) {
  4255. this.setData(data);
  4256. }
  4257. this.controller.add(this.itemset);
  4258. this.setOptions(options);
  4259. }
  4260. /**
  4261. * Set options
  4262. * @param {Object} options TODO: describe the available options
  4263. */
  4264. Timeline.prototype.setOptions = function (options) {
  4265. util.extend(this.options, options);
  4266. // update options the timeaxis
  4267. this.timeaxis.setOptions(this.options);
  4268. // update options for the range
  4269. this.range.setOptions(this.options);
  4270. // update options the itemset
  4271. var top,
  4272. me = this;
  4273. if (this.options.orientation == 'top') {
  4274. top = function () {
  4275. return me.timeaxis.height;
  4276. }
  4277. }
  4278. else {
  4279. top = function () {
  4280. return me.main.height - me.timeaxis.height - me.itemset.height;
  4281. }
  4282. }
  4283. this.itemset.setOptions({
  4284. orientation: this.options.orientation,
  4285. top: top
  4286. });
  4287. this.controller.repaint();
  4288. };
  4289. /**
  4290. * Set data
  4291. * @param {DataSet | Array | DataTable} data
  4292. */
  4293. Timeline.prototype.setData = function(data) {
  4294. this.itemset.setData(data);
  4295. };
  4296. // moment.js
  4297. // version : 2.0.0
  4298. // author : Tim Wood
  4299. // license : MIT
  4300. // momentjs.com
  4301. (function (undefined) {
  4302. /************************************
  4303. Constants
  4304. ************************************/
  4305. var moment,
  4306. VERSION = "2.0.0",
  4307. round = Math.round, i,
  4308. // internal storage for language config files
  4309. languages = {},
  4310. // check for nodeJS
  4311. hasModule = (typeof module !== 'undefined' && module.exports),
  4312. // ASP.NET json date format regex
  4313. aspNetJsonRegex = /^\/?Date\((\-?\d+)/i,
  4314. // format tokens
  4315. formattingTokens = /(\[[^\[]*\])|(\\)?(Mo|MM?M?M?|Do|DDDo|DD?D?D?|ddd?d?|do?|w[o|w]?|W[o|W]?|YYYYY|YYYY|YY|a|A|hh?|HH?|mm?|ss?|SS?S?|X|zz?|ZZ?|.)/g,
  4316. localFormattingTokens = /(\[[^\[]*\])|(\\)?(LT|LL?L?L?|l{1,4})/g,
  4317. // parsing tokens
  4318. parseMultipleFormatChunker = /([0-9a-zA-Z\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]+)/gi,
  4319. // parsing token regexes
  4320. parseTokenOneOrTwoDigits = /\d\d?/, // 0 - 99
  4321. parseTokenOneToThreeDigits = /\d{1,3}/, // 0 - 999
  4322. parseTokenThreeDigits = /\d{3}/, // 000 - 999
  4323. parseTokenFourDigits = /\d{1,4}/, // 0 - 9999
  4324. parseTokenSixDigits = /[+\-]?\d{1,6}/, // -999,999 - 999,999
  4325. parseTokenWord = /[0-9]*[a-z\u00A0-\u05FF\u0700-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]+|[\u0600-\u06FF]+\s*?[\u0600-\u06FF]+/i, // any word (or two) characters or numbers including two word month in arabic.
  4326. parseTokenTimezone = /Z|[\+\-]\d\d:?\d\d/i, // +00:00 -00:00 +0000 -0000 or Z
  4327. parseTokenT = /T/i, // T (ISO seperator)
  4328. parseTokenTimestampMs = /[\+\-]?\d+(\.\d{1,3})?/, // 123456789 123456789.123
  4329. // preliminary iso regex
  4330. // 0000-00-00 + T + 00 or 00:00 or 00:00:00 or 00:00:00.000 + +00:00 or +0000
  4331. isoRegex = /^\s*\d{4}-\d\d-\d\d((T| )(\d\d(:\d\d(:\d\d(\.\d\d?\d?)?)?)?)?([\+\-]\d\d:?\d\d)?)?/,
  4332. isoFormat = 'YYYY-MM-DDTHH:mm:ssZ',
  4333. // iso time formats and regexes
  4334. isoTimes = [
  4335. ['HH:mm:ss.S', /(T| )\d\d:\d\d:\d\d\.\d{1,3}/],
  4336. ['HH:mm:ss', /(T| )\d\d:\d\d:\d\d/],
  4337. ['HH:mm', /(T| )\d\d:\d\d/],
  4338. ['HH', /(T| )\d\d/]
  4339. ],
  4340. // timezone chunker "+10:00" > ["10", "00"] or "-1530" > ["-15", "30"]
  4341. parseTimezoneChunker = /([\+\-]|\d\d)/gi,
  4342. // getter and setter names
  4343. proxyGettersAndSetters = 'Month|Date|Hours|Minutes|Seconds|Milliseconds'.split('|'),
  4344. unitMillisecondFactors = {
  4345. 'Milliseconds' : 1,
  4346. 'Seconds' : 1e3,
  4347. 'Minutes' : 6e4,
  4348. 'Hours' : 36e5,
  4349. 'Days' : 864e5,
  4350. 'Months' : 2592e6,
  4351. 'Years' : 31536e6
  4352. },
  4353. // format function strings
  4354. formatFunctions = {},
  4355. // tokens to ordinalize and pad
  4356. ordinalizeTokens = 'DDD w W M D d'.split(' '),
  4357. paddedTokens = 'M D H h m s w W'.split(' '),
  4358. formatTokenFunctions = {
  4359. M : function () {
  4360. return this.month() + 1;
  4361. },
  4362. MMM : function (format) {
  4363. return this.lang().monthsShort(this, format);
  4364. },
  4365. MMMM : function (format) {
  4366. return this.lang().months(this, format);
  4367. },
  4368. D : function () {
  4369. return this.date();
  4370. },
  4371. DDD : function () {
  4372. return this.dayOfYear();
  4373. },
  4374. d : function () {
  4375. return this.day();
  4376. },
  4377. dd : function (format) {
  4378. return this.lang().weekdaysMin(this, format);
  4379. },
  4380. ddd : function (format) {
  4381. return this.lang().weekdaysShort(this, format);
  4382. },
  4383. dddd : function (format) {
  4384. return this.lang().weekdays(this, format);
  4385. },
  4386. w : function () {
  4387. return this.week();
  4388. },
  4389. W : function () {
  4390. return this.isoWeek();
  4391. },
  4392. YY : function () {
  4393. return leftZeroFill(this.year() % 100, 2);
  4394. },
  4395. YYYY : function () {
  4396. return leftZeroFill(this.year(), 4);
  4397. },
  4398. YYYYY : function () {
  4399. return leftZeroFill(this.year(), 5);
  4400. },
  4401. a : function () {
  4402. return this.lang().meridiem(this.hours(), this.minutes(), true);
  4403. },
  4404. A : function () {
  4405. return this.lang().meridiem(this.hours(), this.minutes(), false);
  4406. },
  4407. H : function () {
  4408. return this.hours();
  4409. },
  4410. h : function () {
  4411. return this.hours() % 12 || 12;
  4412. },
  4413. m : function () {
  4414. return this.minutes();
  4415. },
  4416. s : function () {
  4417. return this.seconds();
  4418. },
  4419. S : function () {
  4420. return ~~(this.milliseconds() / 100);
  4421. },
  4422. SS : function () {
  4423. return leftZeroFill(~~(this.milliseconds() / 10), 2);
  4424. },
  4425. SSS : function () {
  4426. return leftZeroFill(this.milliseconds(), 3);
  4427. },
  4428. Z : function () {
  4429. var a = -this.zone(),
  4430. b = "+";
  4431. if (a < 0) {
  4432. a = -a;
  4433. b = "-";
  4434. }
  4435. return b + leftZeroFill(~~(a / 60), 2) + ":" + leftZeroFill(~~a % 60, 2);
  4436. },
  4437. ZZ : function () {
  4438. var a = -this.zone(),
  4439. b = "+";
  4440. if (a < 0) {
  4441. a = -a;
  4442. b = "-";
  4443. }
  4444. return b + leftZeroFill(~~(10 * a / 6), 4);
  4445. },
  4446. X : function () {
  4447. return this.unix();
  4448. }
  4449. };
  4450. function padToken(func, count) {
  4451. return function (a) {
  4452. return leftZeroFill(func.call(this, a), count);
  4453. };
  4454. }
  4455. function ordinalizeToken(func) {
  4456. return function (a) {
  4457. return this.lang().ordinal(func.call(this, a));
  4458. };
  4459. }
  4460. while (ordinalizeTokens.length) {
  4461. i = ordinalizeTokens.pop();
  4462. formatTokenFunctions[i + 'o'] = ordinalizeToken(formatTokenFunctions[i]);
  4463. }
  4464. while (paddedTokens.length) {
  4465. i = paddedTokens.pop();
  4466. formatTokenFunctions[i + i] = padToken(formatTokenFunctions[i], 2);
  4467. }
  4468. formatTokenFunctions.DDDD = padToken(formatTokenFunctions.DDD, 3);
  4469. /************************************
  4470. Constructors
  4471. ************************************/
  4472. function Language() {
  4473. }
  4474. // Moment prototype object
  4475. function Moment(config) {
  4476. extend(this, config);
  4477. }
  4478. // Duration Constructor
  4479. function Duration(duration) {
  4480. var data = this._data = {},
  4481. years = duration.years || duration.year || duration.y || 0,
  4482. months = duration.months || duration.month || duration.M || 0,
  4483. weeks = duration.weeks || duration.week || duration.w || 0,
  4484. days = duration.days || duration.day || duration.d || 0,
  4485. hours = duration.hours || duration.hour || duration.h || 0,
  4486. minutes = duration.minutes || duration.minute || duration.m || 0,
  4487. seconds = duration.seconds || duration.second || duration.s || 0,
  4488. milliseconds = duration.milliseconds || duration.millisecond || duration.ms || 0;
  4489. // representation for dateAddRemove
  4490. this._milliseconds = milliseconds +
  4491. seconds * 1e3 + // 1000
  4492. minutes * 6e4 + // 1000 * 60
  4493. hours * 36e5; // 1000 * 60 * 60
  4494. // Because of dateAddRemove treats 24 hours as different from a
  4495. // day when working around DST, we need to store them separately
  4496. this._days = days +
  4497. weeks * 7;
  4498. // It is impossible translate months into days without knowing
  4499. // which months you are are talking about, so we have to store
  4500. // it separately.
  4501. this._months = months +
  4502. years * 12;
  4503. // The following code bubbles up values, see the tests for
  4504. // examples of what that means.
  4505. data.milliseconds = milliseconds % 1000;
  4506. seconds += absRound(milliseconds / 1000);
  4507. data.seconds = seconds % 60;
  4508. minutes += absRound(seconds / 60);
  4509. data.minutes = minutes % 60;
  4510. hours += absRound(minutes / 60);
  4511. data.hours = hours % 24;
  4512. days += absRound(hours / 24);
  4513. days += weeks * 7;
  4514. data.days = days % 30;
  4515. months += absRound(days / 30);
  4516. data.months = months % 12;
  4517. years += absRound(months / 12);
  4518. data.years = years;
  4519. }
  4520. /************************************
  4521. Helpers
  4522. ************************************/
  4523. function extend(a, b) {
  4524. for (var i in b) {
  4525. if (b.hasOwnProperty(i)) {
  4526. a[i] = b[i];
  4527. }
  4528. }
  4529. return a;
  4530. }
  4531. function absRound(number) {
  4532. if (number < 0) {
  4533. return Math.ceil(number);
  4534. } else {
  4535. return Math.floor(number);
  4536. }
  4537. }
  4538. // left zero fill a number
  4539. // see http://jsperf.com/left-zero-filling for performance comparison
  4540. function leftZeroFill(number, targetLength) {
  4541. var output = number + '';
  4542. while (output.length < targetLength) {
  4543. output = '0' + output;
  4544. }
  4545. return output;
  4546. }
  4547. // helper function for _.addTime and _.subtractTime
  4548. function addOrSubtractDurationFromMoment(mom, duration, isAdding) {
  4549. var ms = duration._milliseconds,
  4550. d = duration._days,
  4551. M = duration._months,
  4552. currentDate;
  4553. if (ms) {
  4554. mom._d.setTime(+mom + ms * isAdding);
  4555. }
  4556. if (d) {
  4557. mom.date(mom.date() + d * isAdding);
  4558. }
  4559. if (M) {
  4560. currentDate = mom.date();
  4561. mom.date(1)
  4562. .month(mom.month() + M * isAdding)
  4563. .date(Math.min(currentDate, mom.daysInMonth()));
  4564. }
  4565. }
  4566. // check if is an array
  4567. function isArray(input) {
  4568. return Object.prototype.toString.call(input) === '[object Array]';
  4569. }
  4570. // compare two arrays, return the number of differences
  4571. function compareArrays(array1, array2) {
  4572. var len = Math.min(array1.length, array2.length),
  4573. lengthDiff = Math.abs(array1.length - array2.length),
  4574. diffs = 0,
  4575. i;
  4576. for (i = 0; i < len; i++) {
  4577. if (~~array1[i] !== ~~array2[i]) {
  4578. diffs++;
  4579. }
  4580. }
  4581. return diffs + lengthDiff;
  4582. }
  4583. /************************************
  4584. Languages
  4585. ************************************/
  4586. Language.prototype = {
  4587. set : function (config) {
  4588. var prop, i;
  4589. for (i in config) {
  4590. prop = config[i];
  4591. if (typeof prop === 'function') {
  4592. this[i] = prop;
  4593. } else {
  4594. this['_' + i] = prop;
  4595. }
  4596. }
  4597. },
  4598. _months : "January_February_March_April_May_June_July_August_September_October_November_December".split("_"),
  4599. months : function (m) {
  4600. return this._months[m.month()];
  4601. },
  4602. _monthsShort : "Jan_Feb_Mar_Apr_May_Jun_Jul_Aug_Sep_Oct_Nov_Dec".split("_"),
  4603. monthsShort : function (m) {
  4604. return this._monthsShort[m.month()];
  4605. },
  4606. monthsParse : function (monthName) {
  4607. var i, mom, regex, output;
  4608. if (!this._monthsParse) {
  4609. this._monthsParse = [];
  4610. }
  4611. for (i = 0; i < 12; i++) {
  4612. // make the regex if we don't have it already
  4613. if (!this._monthsParse[i]) {
  4614. mom = moment([2000, i]);
  4615. regex = '^' + this.months(mom, '') + '|^' + this.monthsShort(mom, '');
  4616. this._monthsParse[i] = new RegExp(regex.replace('.', ''), 'i');
  4617. }
  4618. // test the regex
  4619. if (this._monthsParse[i].test(monthName)) {
  4620. return i;
  4621. }
  4622. }
  4623. },
  4624. _weekdays : "Sunday_Monday_Tuesday_Wednesday_Thursday_Friday_Saturday".split("_"),
  4625. weekdays : function (m) {
  4626. return this._weekdays[m.day()];
  4627. },
  4628. _weekdaysShort : "Sun_Mon_Tue_Wed_Thu_Fri_Sat".split("_"),
  4629. weekdaysShort : function (m) {
  4630. return this._weekdaysShort[m.day()];
  4631. },
  4632. _weekdaysMin : "Su_Mo_Tu_We_Th_Fr_Sa".split("_"),
  4633. weekdaysMin : function (m) {
  4634. return this._weekdaysMin[m.day()];
  4635. },
  4636. _longDateFormat : {
  4637. LT : "h:mm A",
  4638. L : "MM/DD/YYYY",
  4639. LL : "MMMM D YYYY",
  4640. LLL : "MMMM D YYYY LT",
  4641. LLLL : "dddd, MMMM D YYYY LT"
  4642. },
  4643. longDateFormat : function (key) {
  4644. var output = this._longDateFormat[key];
  4645. if (!output && this._longDateFormat[key.toUpperCase()]) {
  4646. output = this._longDateFormat[key.toUpperCase()].replace(/MMMM|MM|DD|dddd/g, function (val) {
  4647. return val.slice(1);
  4648. });
  4649. this._longDateFormat[key] = output;
  4650. }
  4651. return output;
  4652. },
  4653. meridiem : function (hours, minutes, isLower) {
  4654. if (hours > 11) {
  4655. return isLower ? 'pm' : 'PM';
  4656. } else {
  4657. return isLower ? 'am' : 'AM';
  4658. }
  4659. },
  4660. _calendar : {
  4661. sameDay : '[Today at] LT',
  4662. nextDay : '[Tomorrow at] LT',
  4663. nextWeek : 'dddd [at] LT',
  4664. lastDay : '[Yesterday at] LT',
  4665. lastWeek : '[last] dddd [at] LT',
  4666. sameElse : 'L'
  4667. },
  4668. calendar : function (key, mom) {
  4669. var output = this._calendar[key];
  4670. return typeof output === 'function' ? output.apply(mom) : output;
  4671. },
  4672. _relativeTime : {
  4673. future : "in %s",
  4674. past : "%s ago",
  4675. s : "a few seconds",
  4676. m : "a minute",
  4677. mm : "%d minutes",
  4678. h : "an hour",
  4679. hh : "%d hours",
  4680. d : "a day",
  4681. dd : "%d days",
  4682. M : "a month",
  4683. MM : "%d months",
  4684. y : "a year",
  4685. yy : "%d years"
  4686. },
  4687. relativeTime : function (number, withoutSuffix, string, isFuture) {
  4688. var output = this._relativeTime[string];
  4689. return (typeof output === 'function') ?
  4690. output(number, withoutSuffix, string, isFuture) :
  4691. output.replace(/%d/i, number);
  4692. },
  4693. pastFuture : function (diff, output) {
  4694. var format = this._relativeTime[diff > 0 ? 'future' : 'past'];
  4695. return typeof format === 'function' ? format(output) : format.replace(/%s/i, output);
  4696. },
  4697. ordinal : function (number) {
  4698. return this._ordinal.replace("%d", number);
  4699. },
  4700. _ordinal : "%d",
  4701. preparse : function (string) {
  4702. return string;
  4703. },
  4704. postformat : function (string) {
  4705. return string;
  4706. },
  4707. week : function (mom) {
  4708. return weekOfYear(mom, this._week.dow, this._week.doy);
  4709. },
  4710. _week : {
  4711. dow : 0, // Sunday is the first day of the week.
  4712. doy : 6 // The week that contains Jan 1st is the first week of the year.
  4713. }
  4714. };
  4715. // Loads a language definition into the `languages` cache. The function
  4716. // takes a key and optionally values. If not in the browser and no values
  4717. // are provided, it will load the language file module. As a convenience,
  4718. // this function also returns the language values.
  4719. function loadLang(key, values) {
  4720. values.abbr = key;
  4721. if (!languages[key]) {
  4722. languages[key] = new Language();
  4723. }
  4724. languages[key].set(values);
  4725. return languages[key];
  4726. }
  4727. // Determines which language definition to use and returns it.
  4728. //
  4729. // With no parameters, it will return the global language. If you
  4730. // pass in a language key, such as 'en', it will return the
  4731. // definition for 'en', so long as 'en' has already been loaded using
  4732. // moment.lang.
  4733. function getLangDefinition(key) {
  4734. if (!key) {
  4735. return moment.fn._lang;
  4736. }
  4737. if (!languages[key] && hasModule) {
  4738. require('./lang/' + key);
  4739. }
  4740. return languages[key];
  4741. }
  4742. /************************************
  4743. Formatting
  4744. ************************************/
  4745. function removeFormattingTokens(input) {
  4746. if (input.match(/\[.*\]/)) {
  4747. return input.replace(/^\[|\]$/g, "");
  4748. }
  4749. return input.replace(/\\/g, "");
  4750. }
  4751. function makeFormatFunction(format) {
  4752. var array = format.match(formattingTokens), i, length;
  4753. for (i = 0, length = array.length; i < length; i++) {
  4754. if (formatTokenFunctions[array[i]]) {
  4755. array[i] = formatTokenFunctions[array[i]];
  4756. } else {
  4757. array[i] = removeFormattingTokens(array[i]);
  4758. }
  4759. }
  4760. return function (mom) {
  4761. var output = "";
  4762. for (i = 0; i < length; i++) {
  4763. output += typeof array[i].call === 'function' ? array[i].call(mom, format) : array[i];
  4764. }
  4765. return output;
  4766. };
  4767. }
  4768. // format date using native date object
  4769. function formatMoment(m, format) {
  4770. var i = 5;
  4771. function replaceLongDateFormatTokens(input) {
  4772. return m.lang().longDateFormat(input) || input;
  4773. }
  4774. while (i-- && localFormattingTokens.test(format)) {
  4775. format = format.replace(localFormattingTokens, replaceLongDateFormatTokens);
  4776. }
  4777. if (!formatFunctions[format]) {
  4778. formatFunctions[format] = makeFormatFunction(format);
  4779. }
  4780. return formatFunctions[format](m);
  4781. }
  4782. /************************************
  4783. Parsing
  4784. ************************************/
  4785. // get the regex to find the next token
  4786. function getParseRegexForToken(token) {
  4787. switch (token) {
  4788. case 'DDDD':
  4789. return parseTokenThreeDigits;
  4790. case 'YYYY':
  4791. return parseTokenFourDigits;
  4792. case 'YYYYY':
  4793. return parseTokenSixDigits;
  4794. case 'S':
  4795. case 'SS':
  4796. case 'SSS':
  4797. case 'DDD':
  4798. return parseTokenOneToThreeDigits;
  4799. case 'MMM':
  4800. case 'MMMM':
  4801. case 'dd':
  4802. case 'ddd':
  4803. case 'dddd':
  4804. case 'a':
  4805. case 'A':
  4806. return parseTokenWord;
  4807. case 'X':
  4808. return parseTokenTimestampMs;
  4809. case 'Z':
  4810. case 'ZZ':
  4811. return parseTokenTimezone;
  4812. case 'T':
  4813. return parseTokenT;
  4814. case 'MM':
  4815. case 'DD':
  4816. case 'YY':
  4817. case 'HH':
  4818. case 'hh':
  4819. case 'mm':
  4820. case 'ss':
  4821. case 'M':
  4822. case 'D':
  4823. case 'd':
  4824. case 'H':
  4825. case 'h':
  4826. case 'm':
  4827. case 's':
  4828. return parseTokenOneOrTwoDigits;
  4829. default :
  4830. return new RegExp(token.replace('\\', ''));
  4831. }
  4832. }
  4833. // function to convert string input to date
  4834. function addTimeToArrayFromToken(token, input, config) {
  4835. var a, b,
  4836. datePartArray = config._a;
  4837. switch (token) {
  4838. // MONTH
  4839. case 'M' : // fall through to MM
  4840. case 'MM' :
  4841. datePartArray[1] = (input == null) ? 0 : ~~input - 1;
  4842. break;
  4843. case 'MMM' : // fall through to MMMM
  4844. case 'MMMM' :
  4845. a = getLangDefinition(config._l).monthsParse(input);
  4846. // if we didn't find a month name, mark the date as invalid.
  4847. if (a != null) {
  4848. datePartArray[1] = a;
  4849. } else {
  4850. config._isValid = false;
  4851. }
  4852. break;
  4853. // DAY OF MONTH
  4854. case 'D' : // fall through to DDDD
  4855. case 'DD' : // fall through to DDDD
  4856. case 'DDD' : // fall through to DDDD
  4857. case 'DDDD' :
  4858. if (input != null) {
  4859. datePartArray[2] = ~~input;
  4860. }
  4861. break;
  4862. // YEAR
  4863. case 'YY' :
  4864. datePartArray[0] = ~~input + (~~input > 68 ? 1900 : 2000);
  4865. break;
  4866. case 'YYYY' :
  4867. case 'YYYYY' :
  4868. datePartArray[0] = ~~input;
  4869. break;
  4870. // AM / PM
  4871. case 'a' : // fall through to A
  4872. case 'A' :
  4873. config._isPm = ((input + '').toLowerCase() === 'pm');
  4874. break;
  4875. // 24 HOUR
  4876. case 'H' : // fall through to hh
  4877. case 'HH' : // fall through to hh
  4878. case 'h' : // fall through to hh
  4879. case 'hh' :
  4880. datePartArray[3] = ~~input;
  4881. break;
  4882. // MINUTE
  4883. case 'm' : // fall through to mm
  4884. case 'mm' :
  4885. datePartArray[4] = ~~input;
  4886. break;
  4887. // SECOND
  4888. case 's' : // fall through to ss
  4889. case 'ss' :
  4890. datePartArray[5] = ~~input;
  4891. break;
  4892. // MILLISECOND
  4893. case 'S' :
  4894. case 'SS' :
  4895. case 'SSS' :
  4896. datePartArray[6] = ~~ (('0.' + input) * 1000);
  4897. break;
  4898. // UNIX TIMESTAMP WITH MS
  4899. case 'X':
  4900. config._d = new Date(parseFloat(input) * 1000);
  4901. break;
  4902. // TIMEZONE
  4903. case 'Z' : // fall through to ZZ
  4904. case 'ZZ' :
  4905. config._useUTC = true;
  4906. a = (input + '').match(parseTimezoneChunker);
  4907. if (a && a[1]) {
  4908. config._tzh = ~~a[1];
  4909. }
  4910. if (a && a[2]) {
  4911. config._tzm = ~~a[2];
  4912. }
  4913. // reverse offsets
  4914. if (a && a[0] === '+') {
  4915. config._tzh = -config._tzh;
  4916. config._tzm = -config._tzm;
  4917. }
  4918. break;
  4919. }
  4920. // if the input is null, the date is not valid
  4921. if (input == null) {
  4922. config._isValid = false;
  4923. }
  4924. }
  4925. // convert an array to a date.
  4926. // the array should mirror the parameters below
  4927. // note: all values past the year are optional and will default to the lowest possible value.
  4928. // [year, month, day , hour, minute, second, millisecond]
  4929. function dateFromArray(config) {
  4930. var i, date, input = [];
  4931. if (config._d) {
  4932. return;
  4933. }
  4934. for (i = 0; i < 7; i++) {
  4935. config._a[i] = input[i] = (config._a[i] == null) ? (i === 2 ? 1 : 0) : config._a[i];
  4936. }
  4937. // add the offsets to the time to be parsed so that we can have a clean array for checking isValid
  4938. input[3] += config._tzh || 0;
  4939. input[4] += config._tzm || 0;
  4940. date = new Date(0);
  4941. if (config._useUTC) {
  4942. date.setUTCFullYear(input[0], input[1], input[2]);
  4943. date.setUTCHours(input[3], input[4], input[5], input[6]);
  4944. } else {
  4945. date.setFullYear(input[0], input[1], input[2]);
  4946. date.setHours(input[3], input[4], input[5], input[6]);
  4947. }
  4948. config._d = date;
  4949. }
  4950. // date from string and format string
  4951. function makeDateFromStringAndFormat(config) {
  4952. // This array is used to make a Date, either with `new Date` or `Date.UTC`
  4953. var tokens = config._f.match(formattingTokens),
  4954. string = config._i,
  4955. i, parsedInput;
  4956. config._a = [];
  4957. for (i = 0; i < tokens.length; i++) {
  4958. parsedInput = (getParseRegexForToken(tokens[i]).exec(string) || [])[0];
  4959. if (parsedInput) {
  4960. string = string.slice(string.indexOf(parsedInput) + parsedInput.length);
  4961. }
  4962. // don't parse if its not a known token
  4963. if (formatTokenFunctions[tokens[i]]) {
  4964. addTimeToArrayFromToken(tokens[i], parsedInput, config);
  4965. }
  4966. }
  4967. // handle am pm
  4968. if (config._isPm && config._a[3] < 12) {
  4969. config._a[3] += 12;
  4970. }
  4971. // if is 12 am, change hours to 0
  4972. if (config._isPm === false && config._a[3] === 12) {
  4973. config._a[3] = 0;
  4974. }
  4975. // return
  4976. dateFromArray(config);
  4977. }
  4978. // date from string and array of format strings
  4979. function makeDateFromStringAndArray(config) {
  4980. var tempConfig,
  4981. tempMoment,
  4982. bestMoment,
  4983. scoreToBeat = 99,
  4984. i,
  4985. currentDate,
  4986. currentScore;
  4987. while (config._f.length) {
  4988. tempConfig = extend({}, config);
  4989. tempConfig._f = config._f.pop();
  4990. makeDateFromStringAndFormat(tempConfig);
  4991. tempMoment = new Moment(tempConfig);
  4992. if (tempMoment.isValid()) {
  4993. bestMoment = tempMoment;
  4994. break;
  4995. }
  4996. currentScore = compareArrays(tempConfig._a, tempMoment.toArray());
  4997. if (currentScore < scoreToBeat) {
  4998. scoreToBeat = currentScore;
  4999. bestMoment = tempMoment;
  5000. }
  5001. }
  5002. extend(config, bestMoment);
  5003. }
  5004. // date from iso format
  5005. function makeDateFromString(config) {
  5006. var i,
  5007. string = config._i;
  5008. if (isoRegex.exec(string)) {
  5009. config._f = 'YYYY-MM-DDT';
  5010. for (i = 0; i < 4; i++) {
  5011. if (isoTimes[i][1].exec(string)) {
  5012. config._f += isoTimes[i][0];
  5013. break;
  5014. }
  5015. }
  5016. if (parseTokenTimezone.exec(string)) {
  5017. config._f += " Z";
  5018. }
  5019. makeDateFromStringAndFormat(config);
  5020. } else {
  5021. config._d = new Date(string);
  5022. }
  5023. }
  5024. function makeDateFromInput(config) {
  5025. var input = config._i,
  5026. matched = aspNetJsonRegex.exec(input);
  5027. if (input === undefined) {
  5028. config._d = new Date();
  5029. } else if (matched) {
  5030. config._d = new Date(+matched[1]);
  5031. } else if (typeof input === 'string') {
  5032. makeDateFromString(config);
  5033. } else if (isArray(input)) {
  5034. config._a = input.slice(0);
  5035. dateFromArray(config);
  5036. } else {
  5037. config._d = input instanceof Date ? new Date(+input) : new Date(input);
  5038. }
  5039. }
  5040. /************************************
  5041. Relative Time
  5042. ************************************/
  5043. // helper function for moment.fn.from, moment.fn.fromNow, and moment.duration.fn.humanize
  5044. function substituteTimeAgo(string, number, withoutSuffix, isFuture, lang) {
  5045. return lang.relativeTime(number || 1, !!withoutSuffix, string, isFuture);
  5046. }
  5047. function relativeTime(milliseconds, withoutSuffix, lang) {
  5048. var seconds = round(Math.abs(milliseconds) / 1000),
  5049. minutes = round(seconds / 60),
  5050. hours = round(minutes / 60),
  5051. days = round(hours / 24),
  5052. years = round(days / 365),
  5053. args = seconds < 45 && ['s', seconds] ||
  5054. minutes === 1 && ['m'] ||
  5055. minutes < 45 && ['mm', minutes] ||
  5056. hours === 1 && ['h'] ||
  5057. hours < 22 && ['hh', hours] ||
  5058. days === 1 && ['d'] ||
  5059. days <= 25 && ['dd', days] ||
  5060. days <= 45 && ['M'] ||
  5061. days < 345 && ['MM', round(days / 30)] ||
  5062. years === 1 && ['y'] || ['yy', years];
  5063. args[2] = withoutSuffix;
  5064. args[3] = milliseconds > 0;
  5065. args[4] = lang;
  5066. return substituteTimeAgo.apply({}, args);
  5067. }
  5068. /************************************
  5069. Week of Year
  5070. ************************************/
  5071. // firstDayOfWeek 0 = sun, 6 = sat
  5072. // the day of the week that starts the week
  5073. // (usually sunday or monday)
  5074. // firstDayOfWeekOfYear 0 = sun, 6 = sat
  5075. // the first week is the week that contains the first
  5076. // of this day of the week
  5077. // (eg. ISO weeks use thursday (4))
  5078. function weekOfYear(mom, firstDayOfWeek, firstDayOfWeekOfYear) {
  5079. var end = firstDayOfWeekOfYear - firstDayOfWeek,
  5080. daysToDayOfWeek = firstDayOfWeekOfYear - mom.day();
  5081. if (daysToDayOfWeek > end) {
  5082. daysToDayOfWeek -= 7;
  5083. }
  5084. if (daysToDayOfWeek < end - 7) {
  5085. daysToDayOfWeek += 7;
  5086. }
  5087. return Math.ceil(moment(mom).add('d', daysToDayOfWeek).dayOfYear() / 7);
  5088. }
  5089. /************************************
  5090. Top Level Functions
  5091. ************************************/
  5092. function makeMoment(config) {
  5093. var input = config._i,
  5094. format = config._f;
  5095. if (input === null || input === '') {
  5096. return null;
  5097. }
  5098. if (typeof input === 'string') {
  5099. config._i = input = getLangDefinition().preparse(input);
  5100. }
  5101. if (moment.isMoment(input)) {
  5102. config = extend({}, input);
  5103. config._d = new Date(+input._d);
  5104. } else if (format) {
  5105. if (isArray(format)) {
  5106. makeDateFromStringAndArray(config);
  5107. } else {
  5108. makeDateFromStringAndFormat(config);
  5109. }
  5110. } else {
  5111. makeDateFromInput(config);
  5112. }
  5113. return new Moment(config);
  5114. }
  5115. moment = function (input, format, lang) {
  5116. return makeMoment({
  5117. _i : input,
  5118. _f : format,
  5119. _l : lang,
  5120. _isUTC : false
  5121. });
  5122. };
  5123. // creating with utc
  5124. moment.utc = function (input, format, lang) {
  5125. return makeMoment({
  5126. _useUTC : true,
  5127. _isUTC : true,
  5128. _l : lang,
  5129. _i : input,
  5130. _f : format
  5131. });
  5132. };
  5133. // creating with unix timestamp (in seconds)
  5134. moment.unix = function (input) {
  5135. return moment(input * 1000);
  5136. };
  5137. // duration
  5138. moment.duration = function (input, key) {
  5139. var isDuration = moment.isDuration(input),
  5140. isNumber = (typeof input === 'number'),
  5141. duration = (isDuration ? input._data : (isNumber ? {} : input)),
  5142. ret;
  5143. if (isNumber) {
  5144. if (key) {
  5145. duration[key] = input;
  5146. } else {
  5147. duration.milliseconds = input;
  5148. }
  5149. }
  5150. ret = new Duration(duration);
  5151. if (isDuration && input.hasOwnProperty('_lang')) {
  5152. ret._lang = input._lang;
  5153. }
  5154. return ret;
  5155. };
  5156. // version number
  5157. moment.version = VERSION;
  5158. // default format
  5159. moment.defaultFormat = isoFormat;
  5160. // This function will load languages and then set the global language. If
  5161. // no arguments are passed in, it will simply return the current global
  5162. // language key.
  5163. moment.lang = function (key, values) {
  5164. var i;
  5165. if (!key) {
  5166. return moment.fn._lang._abbr;
  5167. }
  5168. if (values) {
  5169. loadLang(key, values);
  5170. } else if (!languages[key]) {
  5171. getLangDefinition(key);
  5172. }
  5173. moment.duration.fn._lang = moment.fn._lang = getLangDefinition(key);
  5174. };
  5175. // returns language data
  5176. moment.langData = function (key) {
  5177. if (key && key._lang && key._lang._abbr) {
  5178. key = key._lang._abbr;
  5179. }
  5180. return getLangDefinition(key);
  5181. };
  5182. // compare moment object
  5183. moment.isMoment = function (obj) {
  5184. return obj instanceof Moment;
  5185. };
  5186. // for typechecking Duration objects
  5187. moment.isDuration = function (obj) {
  5188. return obj instanceof Duration;
  5189. };
  5190. /************************************
  5191. Moment Prototype
  5192. ************************************/
  5193. moment.fn = Moment.prototype = {
  5194. clone : function () {
  5195. return moment(this);
  5196. },
  5197. valueOf : function () {
  5198. return +this._d;
  5199. },
  5200. unix : function () {
  5201. return Math.floor(+this._d / 1000);
  5202. },
  5203. toString : function () {
  5204. return this.format("ddd MMM DD YYYY HH:mm:ss [GMT]ZZ");
  5205. },
  5206. toDate : function () {
  5207. return this._d;
  5208. },
  5209. toJSON : function () {
  5210. return moment.utc(this).format('YYYY-MM-DD[T]HH:mm:ss.SSS[Z]');
  5211. },
  5212. toArray : function () {
  5213. var m = this;
  5214. return [
  5215. m.year(),
  5216. m.month(),
  5217. m.date(),
  5218. m.hours(),
  5219. m.minutes(),
  5220. m.seconds(),
  5221. m.milliseconds()
  5222. ];
  5223. },
  5224. isValid : function () {
  5225. if (this._isValid == null) {
  5226. if (this._a) {
  5227. this._isValid = !compareArrays(this._a, (this._isUTC ? moment.utc(this._a) : moment(this._a)).toArray());
  5228. } else {
  5229. this._isValid = !isNaN(this._d.getTime());
  5230. }
  5231. }
  5232. return !!this._isValid;
  5233. },
  5234. utc : function () {
  5235. this._isUTC = true;
  5236. return this;
  5237. },
  5238. local : function () {
  5239. this._isUTC = false;
  5240. return this;
  5241. },
  5242. format : function (inputString) {
  5243. var output = formatMoment(this, inputString || moment.defaultFormat);
  5244. return this.lang().postformat(output);
  5245. },
  5246. add : function (input, val) {
  5247. var dur;
  5248. // switch args to support add('s', 1) and add(1, 's')
  5249. if (typeof input === 'string') {
  5250. dur = moment.duration(+val, input);
  5251. } else {
  5252. dur = moment.duration(input, val);
  5253. }
  5254. addOrSubtractDurationFromMoment(this, dur, 1);
  5255. return this;
  5256. },
  5257. subtract : function (input, val) {
  5258. var dur;
  5259. // switch args to support subtract('s', 1) and subtract(1, 's')
  5260. if (typeof input === 'string') {
  5261. dur = moment.duration(+val, input);
  5262. } else {
  5263. dur = moment.duration(input, val);
  5264. }
  5265. addOrSubtractDurationFromMoment(this, dur, -1);
  5266. return this;
  5267. },
  5268. diff : function (input, units, asFloat) {
  5269. var that = this._isUTC ? moment(input).utc() : moment(input).local(),
  5270. zoneDiff = (this.zone() - that.zone()) * 6e4,
  5271. diff, output;
  5272. if (units) {
  5273. // standardize on singular form
  5274. units = units.replace(/s$/, '');
  5275. }
  5276. if (units === 'year' || units === 'month') {
  5277. diff = (this.daysInMonth() + that.daysInMonth()) * 432e5; // 24 * 60 * 60 * 1000 / 2
  5278. output = ((this.year() - that.year()) * 12) + (this.month() - that.month());
  5279. output += ((this - moment(this).startOf('month')) - (that - moment(that).startOf('month'))) / diff;
  5280. if (units === 'year') {
  5281. output = output / 12;
  5282. }
  5283. } else {
  5284. diff = (this - that) - zoneDiff;
  5285. output = units === 'second' ? diff / 1e3 : // 1000
  5286. units === 'minute' ? diff / 6e4 : // 1000 * 60
  5287. units === 'hour' ? diff / 36e5 : // 1000 * 60 * 60
  5288. units === 'day' ? diff / 864e5 : // 1000 * 60 * 60 * 24
  5289. units === 'week' ? diff / 6048e5 : // 1000 * 60 * 60 * 24 * 7
  5290. diff;
  5291. }
  5292. return asFloat ? output : absRound(output);
  5293. },
  5294. from : function (time, withoutSuffix) {
  5295. return moment.duration(this.diff(time)).lang(this.lang()._abbr).humanize(!withoutSuffix);
  5296. },
  5297. fromNow : function (withoutSuffix) {
  5298. return this.from(moment(), withoutSuffix);
  5299. },
  5300. calendar : function () {
  5301. var diff = this.diff(moment().startOf('day'), 'days', true),
  5302. format = diff < -6 ? 'sameElse' :
  5303. diff < -1 ? 'lastWeek' :
  5304. diff < 0 ? 'lastDay' :
  5305. diff < 1 ? 'sameDay' :
  5306. diff < 2 ? 'nextDay' :
  5307. diff < 7 ? 'nextWeek' : 'sameElse';
  5308. return this.format(this.lang().calendar(format, this));
  5309. },
  5310. isLeapYear : function () {
  5311. var year = this.year();
  5312. return (year % 4 === 0 && year % 100 !== 0) || year % 400 === 0;
  5313. },
  5314. isDST : function () {
  5315. return (this.zone() < moment([this.year()]).zone() ||
  5316. this.zone() < moment([this.year(), 5]).zone());
  5317. },
  5318. day : function (input) {
  5319. var day = this._isUTC ? this._d.getUTCDay() : this._d.getDay();
  5320. return input == null ? day :
  5321. this.add({ d : input - day });
  5322. },
  5323. startOf: function (units) {
  5324. units = units.replace(/s$/, '');
  5325. // the following switch intentionally omits break keywords
  5326. // to utilize falling through the cases.
  5327. switch (units) {
  5328. case 'year':
  5329. this.month(0);
  5330. /* falls through */
  5331. case 'month':
  5332. this.date(1);
  5333. /* falls through */
  5334. case 'week':
  5335. case 'day':
  5336. this.hours(0);
  5337. /* falls through */
  5338. case 'hour':
  5339. this.minutes(0);
  5340. /* falls through */
  5341. case 'minute':
  5342. this.seconds(0);
  5343. /* falls through */
  5344. case 'second':
  5345. this.milliseconds(0);
  5346. /* falls through */
  5347. }
  5348. // weeks are a special case
  5349. if (units === 'week') {
  5350. this.day(0);
  5351. }
  5352. return this;
  5353. },
  5354. endOf: function (units) {
  5355. return this.startOf(units).add(units.replace(/s?$/, 's'), 1).subtract('ms', 1);
  5356. },
  5357. isAfter: function (input, units) {
  5358. units = typeof units !== 'undefined' ? units : 'millisecond';
  5359. return +this.clone().startOf(units) > +moment(input).startOf(units);
  5360. },
  5361. isBefore: function (input, units) {
  5362. units = typeof units !== 'undefined' ? units : 'millisecond';
  5363. return +this.clone().startOf(units) < +moment(input).startOf(units);
  5364. },
  5365. isSame: function (input, units) {
  5366. units = typeof units !== 'undefined' ? units : 'millisecond';
  5367. return +this.clone().startOf(units) === +moment(input).startOf(units);
  5368. },
  5369. zone : function () {
  5370. return this._isUTC ? 0 : this._d.getTimezoneOffset();
  5371. },
  5372. daysInMonth : function () {
  5373. return moment.utc([this.year(), this.month() + 1, 0]).date();
  5374. },
  5375. dayOfYear : function (input) {
  5376. var dayOfYear = round((moment(this).startOf('day') - moment(this).startOf('year')) / 864e5) + 1;
  5377. return input == null ? dayOfYear : this.add("d", (input - dayOfYear));
  5378. },
  5379. isoWeek : function (input) {
  5380. var week = weekOfYear(this, 1, 4);
  5381. return input == null ? week : this.add("d", (input - week) * 7);
  5382. },
  5383. week : function (input) {
  5384. var week = this.lang().week(this);
  5385. return input == null ? week : this.add("d", (input - week) * 7);
  5386. },
  5387. // If passed a language key, it will set the language for this
  5388. // instance. Otherwise, it will return the language configuration
  5389. // variables for this instance.
  5390. lang : function (key) {
  5391. if (key === undefined) {
  5392. return this._lang;
  5393. } else {
  5394. this._lang = getLangDefinition(key);
  5395. return this;
  5396. }
  5397. }
  5398. };
  5399. // helper for adding shortcuts
  5400. function makeGetterAndSetter(name, key) {
  5401. moment.fn[name] = moment.fn[name + 's'] = function (input) {
  5402. var utc = this._isUTC ? 'UTC' : '';
  5403. if (input != null) {
  5404. this._d['set' + utc + key](input);
  5405. return this;
  5406. } else {
  5407. return this._d['get' + utc + key]();
  5408. }
  5409. };
  5410. }
  5411. // loop through and add shortcuts (Month, Date, Hours, Minutes, Seconds, Milliseconds)
  5412. for (i = 0; i < proxyGettersAndSetters.length; i ++) {
  5413. makeGetterAndSetter(proxyGettersAndSetters[i].toLowerCase().replace(/s$/, ''), proxyGettersAndSetters[i]);
  5414. }
  5415. // add shortcut for year (uses different syntax than the getter/setter 'year' == 'FullYear')
  5416. makeGetterAndSetter('year', 'FullYear');
  5417. // add plural methods
  5418. moment.fn.days = moment.fn.day;
  5419. moment.fn.weeks = moment.fn.week;
  5420. moment.fn.isoWeeks = moment.fn.isoWeek;
  5421. /************************************
  5422. Duration Prototype
  5423. ************************************/
  5424. moment.duration.fn = Duration.prototype = {
  5425. weeks : function () {
  5426. return absRound(this.days() / 7);
  5427. },
  5428. valueOf : function () {
  5429. return this._milliseconds +
  5430. this._days * 864e5 +
  5431. this._months * 2592e6;
  5432. },
  5433. humanize : function (withSuffix) {
  5434. var difference = +this,
  5435. output = relativeTime(difference, !withSuffix, this.lang());
  5436. if (withSuffix) {
  5437. output = this.lang().pastFuture(difference, output);
  5438. }
  5439. return this.lang().postformat(output);
  5440. },
  5441. lang : moment.fn.lang
  5442. };
  5443. function makeDurationGetter(name) {
  5444. moment.duration.fn[name] = function () {
  5445. return this._data[name];
  5446. };
  5447. }
  5448. function makeDurationAsGetter(name, factor) {
  5449. moment.duration.fn['as' + name] = function () {
  5450. return +this / factor;
  5451. };
  5452. }
  5453. for (i in unitMillisecondFactors) {
  5454. if (unitMillisecondFactors.hasOwnProperty(i)) {
  5455. makeDurationAsGetter(i, unitMillisecondFactors[i]);
  5456. makeDurationGetter(i.toLowerCase());
  5457. }
  5458. }
  5459. makeDurationAsGetter('Weeks', 6048e5);
  5460. /************************************
  5461. Default Lang
  5462. ************************************/
  5463. // Set default language, other languages will inherit from English.
  5464. moment.lang('en', {
  5465. ordinal : function (number) {
  5466. var b = number % 10,
  5467. output = (~~ (number % 100 / 10) === 1) ? 'th' :
  5468. (b === 1) ? 'st' :
  5469. (b === 2) ? 'nd' :
  5470. (b === 3) ? 'rd' : 'th';
  5471. return number + output;
  5472. }
  5473. });
  5474. /************************************
  5475. Exposing Moment
  5476. ************************************/
  5477. // CommonJS module is defined
  5478. if (hasModule) {
  5479. module.exports = moment;
  5480. }
  5481. /*global ender:false */
  5482. if (typeof ender === 'undefined') {
  5483. // here, `this` means `window` in the browser, or `global` on the server
  5484. // add `moment` as a global object via a string identifier,
  5485. // for Closure Compiler "advanced" mode
  5486. this['moment'] = moment;
  5487. }
  5488. /*global define:false */
  5489. if (typeof define === "function" && define.amd) {
  5490. define("moment", [], function () {
  5491. return moment;
  5492. });
  5493. }
  5494. }).call(this);