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.

14010 lines
419 KiB

11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
  1. /**
  2. * vis.js
  3. * https://github.com/almende/vis
  4. *
  5. * A dynamic, browser-based visualization library.
  6. *
  7. * @version 0.0.8
  8. * @date 2013-06-03
  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. (function(e){if("function"==typeof bootstrap)bootstrap("vis",e);else if("object"==typeof exports)module.exports=e();else if("function"==typeof define&&define.amd)define(e);else if("undefined"!=typeof ses){if(!ses.ok())return;ses.makeVis=e}else"undefined"!=typeof window?window.vis=e():global.vis=e()})(function(){var define,ses,bootstrap,module,exports;
  26. return (function(e,t,n){function i(n,s){if(!t[n]){if(!e[n]){var o=typeof require=="function"&&require;if(!s&&o)return o(n,!0);if(r)return r(n,!0);throw new Error("Cannot find module '"+n+"'")}var u=t[n]={exports:{}};e[n][0].call(u.exports,function(t){var r=e[n][1][t];return i(r?r:t)},u,u.exports)}return t[n].exports}var r=typeof require=="function"&&require;for(var s=0;s<n.length;s++)i(n[s]);return i})({1:[function(require,module,exports){
  27. (function(){/**
  28. * vis.js module imports
  29. */
  30. var moment = require('moment');
  31. /**
  32. * utility functions
  33. */
  34. var util = {};
  35. /**
  36. * Test whether given object is a number
  37. * @param {*} object
  38. * @return {Boolean} isNumber
  39. */
  40. util.isNumber = function isNumber(object) {
  41. return (object instanceof Number || typeof object == 'number');
  42. };
  43. /**
  44. * Test whether given object is a string
  45. * @param {*} object
  46. * @return {Boolean} isString
  47. */
  48. util.isString = function isString(object) {
  49. return (object instanceof String || typeof object == 'string');
  50. };
  51. /**
  52. * Test whether given object is a Date, or a String containing a Date
  53. * @param {Date | String} object
  54. * @return {Boolean} isDate
  55. */
  56. util.isDate = function isDate(object) {
  57. if (object instanceof Date) {
  58. return true;
  59. }
  60. else if (util.isString(object)) {
  61. // test whether this string contains a date
  62. var match = ASPDateRegex.exec(object);
  63. if (match) {
  64. return true;
  65. }
  66. else if (!isNaN(Date.parse(object))) {
  67. return true;
  68. }
  69. }
  70. return false;
  71. };
  72. /**
  73. * Test whether given object is an instance of google.visualization.DataTable
  74. * @param {*} object
  75. * @return {Boolean} isDataTable
  76. */
  77. util.isDataTable = function isDataTable(object) {
  78. return (typeof (google) !== 'undefined') &&
  79. (google.visualization) &&
  80. (google.visualization.DataTable) &&
  81. (object instanceof google.visualization.DataTable);
  82. };
  83. /**
  84. * Create a semi UUID
  85. * source: http://stackoverflow.com/a/105074/1262753
  86. * @return {String} uuid
  87. */
  88. util.randomUUID = function randomUUID () {
  89. var S4 = function () {
  90. return Math.floor(
  91. Math.random() * 0x10000 /* 65536 */
  92. ).toString(16);
  93. };
  94. return (
  95. S4() + S4() + '-' +
  96. S4() + '-' +
  97. S4() + '-' +
  98. S4() + '-' +
  99. S4() + S4() + S4()
  100. );
  101. };
  102. /**
  103. * Extend object a with the properties of object b or a series of objects
  104. * Only properties with defined values are copied
  105. * @param {Object} a
  106. * @param {... Object} b
  107. * @return {Object} a
  108. */
  109. util.extend = function (a, b) {
  110. for (var i = 1, len = arguments.length; i < len; i++) {
  111. var other = arguments[i];
  112. for (var prop in other) {
  113. if (other.hasOwnProperty(prop) && other[prop] !== undefined) {
  114. a[prop] = other[prop];
  115. }
  116. }
  117. }
  118. return a;
  119. };
  120. /**
  121. * Cast an object to another type
  122. * @param {Boolean | Number | String | Date | Moment | Null | undefined} object
  123. * @param {String | undefined} type Name of the type. Available types:
  124. * 'Boolean', 'Number', 'String',
  125. * 'Date', 'Moment', ISODate', 'ASPDate'.
  126. * @return {*} object
  127. * @throws Error
  128. */
  129. util.cast = function cast(object, type) {
  130. var match;
  131. if (object === undefined) {
  132. return undefined;
  133. }
  134. if (object === null) {
  135. return null;
  136. }
  137. if (!type) {
  138. return object;
  139. }
  140. if (!(typeof type === 'string') && !(type instanceof String)) {
  141. throw new Error('Type must be a string');
  142. }
  143. //noinspection FallthroughInSwitchStatementJS
  144. switch (type) {
  145. case 'boolean':
  146. case 'Boolean':
  147. return Boolean(object);
  148. case 'number':
  149. case 'Number':
  150. return Number(object);
  151. case 'string':
  152. case 'String':
  153. return String(object);
  154. case 'Date':
  155. if (util.isNumber(object)) {
  156. return new Date(object);
  157. }
  158. if (object instanceof Date) {
  159. return new Date(object.valueOf());
  160. }
  161. else if (moment.isMoment(object)) {
  162. return new Date(object.valueOf());
  163. }
  164. if (util.isString(object)) {
  165. // parse ASP.Net Date pattern,
  166. // for example '/Date(1198908717056)/' or '/Date(1198908717056-0700)/'
  167. // code from http://momentjs.com/
  168. match = ASPDateRegex.exec(object);
  169. if (match) {
  170. return new Date(Number(match[1])); // parse number
  171. }
  172. else {
  173. return moment(object).toDate(); // parse string
  174. }
  175. }
  176. else {
  177. throw new Error(
  178. 'Cannot cast object of type ' + util.getType(object) +
  179. ' to type Date');
  180. }
  181. case 'Moment':
  182. if (util.isNumber(object)) {
  183. return moment(object);
  184. }
  185. if (object instanceof Date) {
  186. return moment(object.valueOf());
  187. }
  188. else if (moment.isMoment(object)) {
  189. return moment.clone();
  190. }
  191. if (util.isString(object)) {
  192. // parse ASP.Net Date pattern,
  193. // for example '/Date(1198908717056)/' or '/Date(1198908717056-0700)/'
  194. // code from http://momentjs.com/
  195. match = ASPDateRegex.exec(object);
  196. if (match) {
  197. return moment(Number(match[1])); // parse number
  198. }
  199. else {
  200. return moment(object); // parse string
  201. }
  202. }
  203. else {
  204. throw new Error(
  205. 'Cannot cast object of type ' + util.getType(object) +
  206. ' to type Date');
  207. }
  208. case 'ISODate':
  209. if (object instanceof Date) {
  210. return object.toISOString();
  211. }
  212. else if (moment.isMoment(object)) {
  213. return object.toDate().toISOString();
  214. }
  215. else if (util.isNumber(object) || util.isString(object)) {
  216. return moment(object).toDate().toISOString();
  217. }
  218. else {
  219. throw new Error(
  220. 'Cannot cast object of type ' + util.getType(object) +
  221. ' to type ISODate');
  222. }
  223. case 'ASPDate':
  224. if (object instanceof Date) {
  225. return '/Date(' + object.valueOf() + ')/';
  226. }
  227. else if (util.isNumber(object) || util.isString(object)) {
  228. return '/Date(' + moment(object).valueOf() + ')/';
  229. }
  230. else {
  231. throw new Error(
  232. 'Cannot cast object of type ' + util.getType(object) +
  233. ' to type ASPDate');
  234. }
  235. default:
  236. throw new Error('Cannot cast object of type ' + util.getType(object) +
  237. ' to type "' + type + '"');
  238. }
  239. };
  240. var ASPDateRegex = /^\/?Date\((\-?\d+)/i;
  241. /**
  242. * Get the type of an object, for example util.getType([]) returns 'Array'
  243. * @param {*} object
  244. * @return {String} type
  245. */
  246. util.getType = function getType(object) {
  247. var type = typeof object;
  248. if (type == 'object') {
  249. if (object == null) {
  250. return 'null';
  251. }
  252. if (object instanceof Boolean) {
  253. return 'Boolean';
  254. }
  255. if (object instanceof Number) {
  256. return 'Number';
  257. }
  258. if (object instanceof String) {
  259. return 'String';
  260. }
  261. if (object instanceof Array) {
  262. return 'Array';
  263. }
  264. if (object instanceof Date) {
  265. return 'Date';
  266. }
  267. return 'Object';
  268. }
  269. else if (type == 'number') {
  270. return 'Number';
  271. }
  272. else if (type == 'boolean') {
  273. return 'Boolean';
  274. }
  275. else if (type == 'string') {
  276. return 'String';
  277. }
  278. return type;
  279. };
  280. /**
  281. * Retrieve the absolute left value of a DOM element
  282. * @param {Element} elem A dom element, for example a div
  283. * @return {number} left The absolute left position of this element
  284. * in the browser page.
  285. */
  286. util.getAbsoluteLeft = function getAbsoluteLeft (elem) {
  287. var doc = document.documentElement;
  288. var body = document.body;
  289. var left = elem.offsetLeft;
  290. var e = elem.offsetParent;
  291. while (e != null && e != body && e != doc) {
  292. left += e.offsetLeft;
  293. left -= e.scrollLeft;
  294. e = e.offsetParent;
  295. }
  296. return left;
  297. };
  298. /**
  299. * Retrieve the absolute top value of a DOM element
  300. * @param {Element} elem A dom element, for example a div
  301. * @return {number} top The absolute top position of this element
  302. * in the browser page.
  303. */
  304. util.getAbsoluteTop = function getAbsoluteTop (elem) {
  305. var doc = document.documentElement;
  306. var body = document.body;
  307. var top = elem.offsetTop;
  308. var e = elem.offsetParent;
  309. while (e != null && e != body && e != doc) {
  310. top += e.offsetTop;
  311. top -= e.scrollTop;
  312. e = e.offsetParent;
  313. }
  314. return top;
  315. };
  316. /**
  317. * Get the absolute, vertical mouse position from an event.
  318. * @param {Event} event
  319. * @return {Number} pageY
  320. */
  321. util.getPageY = function getPageY (event) {
  322. if ('pageY' in event) {
  323. return event.pageY;
  324. }
  325. else {
  326. var clientY;
  327. if (('targetTouches' in event) && event.targetTouches.length) {
  328. clientY = event.targetTouches[0].clientY;
  329. }
  330. else {
  331. clientY = event.clientY;
  332. }
  333. var doc = document.documentElement;
  334. var body = document.body;
  335. return clientY +
  336. ( doc && doc.scrollTop || body && body.scrollTop || 0 ) -
  337. ( doc && doc.clientTop || body && body.clientTop || 0 );
  338. }
  339. };
  340. /**
  341. * Get the absolute, horizontal mouse position from an event.
  342. * @param {Event} event
  343. * @return {Number} pageX
  344. */
  345. util.getPageX = function getPageX (event) {
  346. if ('pageY' in event) {
  347. return event.pageX;
  348. }
  349. else {
  350. var clientX;
  351. if (('targetTouches' in event) && event.targetTouches.length) {
  352. clientX = event.targetTouches[0].clientX;
  353. }
  354. else {
  355. clientX = event.clientX;
  356. }
  357. var doc = document.documentElement;
  358. var body = document.body;
  359. return clientX +
  360. ( doc && doc.scrollLeft || body && body.scrollLeft || 0 ) -
  361. ( doc && doc.clientLeft || body && body.clientLeft || 0 );
  362. }
  363. };
  364. /**
  365. * add a className to the given elements style
  366. * @param {Element} elem
  367. * @param {String} className
  368. */
  369. util.addClassName = function addClassName(elem, className) {
  370. var classes = elem.className.split(' ');
  371. if (classes.indexOf(className) == -1) {
  372. classes.push(className); // add the class to the array
  373. elem.className = classes.join(' ');
  374. }
  375. };
  376. /**
  377. * add a className to the given elements style
  378. * @param {Element} elem
  379. * @param {String} className
  380. */
  381. util.removeClassName = function removeClassname(elem, className) {
  382. var classes = elem.className.split(' ');
  383. var index = classes.indexOf(className);
  384. if (index != -1) {
  385. classes.splice(index, 1); // remove the class from the array
  386. elem.className = classes.join(' ');
  387. }
  388. };
  389. /**
  390. * For each method for both arrays and objects.
  391. * In case of an array, the built-in Array.forEach() is applied.
  392. * In case of an Object, the method loops over all properties of the object.
  393. * @param {Object | Array} object An Object or Array
  394. * @param {function} callback Callback method, called for each item in
  395. * the object or array with three parameters:
  396. * callback(value, index, object)
  397. */
  398. util.forEach = function forEach (object, callback) {
  399. var i,
  400. len;
  401. if (object instanceof Array) {
  402. // array
  403. for (i = 0, len = object.length; i < len; i++) {
  404. callback(object[i], i, object);
  405. }
  406. }
  407. else {
  408. // object
  409. for (i in object) {
  410. if (object.hasOwnProperty(i)) {
  411. callback(object[i], i, object);
  412. }
  413. }
  414. }
  415. };
  416. /**
  417. * Update a property in an object
  418. * @param {Object} object
  419. * @param {String} key
  420. * @param {*} value
  421. * @return {Boolean} changed
  422. */
  423. util.updateProperty = function updateProp (object, key, value) {
  424. if (object[key] !== value) {
  425. object[key] = value;
  426. return true;
  427. }
  428. else {
  429. return false;
  430. }
  431. };
  432. /**
  433. * Add and event listener. Works for all browsers
  434. * @param {Element} element An html element
  435. * @param {string} action The action, for example "click",
  436. * without the prefix "on"
  437. * @param {function} listener The callback function to be executed
  438. * @param {boolean} [useCapture]
  439. */
  440. util.addEventListener = function addEventListener(element, action, listener, useCapture) {
  441. if (element.addEventListener) {
  442. if (useCapture === undefined)
  443. useCapture = false;
  444. if (action === "mousewheel" && navigator.userAgent.indexOf("Firefox") >= 0) {
  445. action = "DOMMouseScroll"; // For Firefox
  446. }
  447. element.addEventListener(action, listener, useCapture);
  448. } else {
  449. element.attachEvent("on" + action, listener); // IE browsers
  450. }
  451. };
  452. /**
  453. * Remove an event listener from an element
  454. * @param {Element} element An html dom element
  455. * @param {string} action The name of the event, for example "mousedown"
  456. * @param {function} listener The listener function
  457. * @param {boolean} [useCapture]
  458. */
  459. util.removeEventListener = function removeEventListener(element, action, listener, useCapture) {
  460. if (element.removeEventListener) {
  461. // non-IE browsers
  462. if (useCapture === undefined)
  463. useCapture = false;
  464. if (action === "mousewheel" && navigator.userAgent.indexOf("Firefox") >= 0) {
  465. action = "DOMMouseScroll"; // For Firefox
  466. }
  467. element.removeEventListener(action, listener, useCapture);
  468. } else {
  469. // IE browsers
  470. element.detachEvent("on" + action, listener);
  471. }
  472. };
  473. /**
  474. * Get HTML element which is the target of the event
  475. * @param {Event} event
  476. * @return {Element} target element
  477. */
  478. util.getTarget = function getTarget(event) {
  479. // code from http://www.quirksmode.org/js/events_properties.html
  480. if (!event) {
  481. event = window.event;
  482. }
  483. var target;
  484. if (event.target) {
  485. target = event.target;
  486. }
  487. else if (event.srcElement) {
  488. target = event.srcElement;
  489. }
  490. if (target.nodeType != undefined && target.nodeType == 3) {
  491. // defeat Safari bug
  492. target = target.parentNode;
  493. }
  494. return target;
  495. };
  496. /**
  497. * Stop event propagation
  498. */
  499. util.stopPropagation = function stopPropagation(event) {
  500. if (!event)
  501. event = window.event;
  502. if (event.stopPropagation) {
  503. event.stopPropagation(); // non-IE browsers
  504. }
  505. else {
  506. event.cancelBubble = true; // IE browsers
  507. }
  508. };
  509. /**
  510. * Cancels the event if it is cancelable, without stopping further propagation of the event.
  511. */
  512. util.preventDefault = function preventDefault (event) {
  513. if (!event)
  514. event = window.event;
  515. if (event.preventDefault) {
  516. event.preventDefault(); // non-IE browsers
  517. }
  518. else {
  519. event.returnValue = false; // IE browsers
  520. }
  521. };
  522. util.option = {};
  523. /**
  524. * Cast a value as boolean
  525. * @param {Boolean | function | undefined} value
  526. * @param {Boolean} [defaultValue]
  527. * @returns {Boolean} bool
  528. */
  529. util.option.asBoolean = function (value, defaultValue) {
  530. if (typeof value == 'function') {
  531. value = value();
  532. }
  533. if (value != null) {
  534. return (value != false);
  535. }
  536. return defaultValue || null;
  537. };
  538. /**
  539. * Cast a value as number
  540. * @param {Boolean | function | undefined} value
  541. * @param {Number} [defaultValue]
  542. * @returns {Number} number
  543. */
  544. util.option.asNumber = function (value, defaultValue) {
  545. if (typeof value == 'function') {
  546. value = value();
  547. }
  548. if (value != null) {
  549. return Number(value) || defaultValue || null;
  550. }
  551. return defaultValue || null;
  552. };
  553. /**
  554. * Cast a value as string
  555. * @param {String | function | undefined} value
  556. * @param {String} [defaultValue]
  557. * @returns {String} str
  558. */
  559. util.option.asString = function (value, defaultValue) {
  560. if (typeof value == 'function') {
  561. value = value();
  562. }
  563. if (value != null) {
  564. return String(value);
  565. }
  566. return defaultValue || null;
  567. };
  568. /**
  569. * Cast a size or location in pixels or a percentage
  570. * @param {String | Number | function | undefined} value
  571. * @param {String} [defaultValue]
  572. * @returns {String} size
  573. */
  574. util.option.asSize = function (value, defaultValue) {
  575. if (typeof value == 'function') {
  576. value = value();
  577. }
  578. if (util.isString(value)) {
  579. return value;
  580. }
  581. else if (util.isNumber(value)) {
  582. return value + 'px';
  583. }
  584. else {
  585. return defaultValue || null;
  586. }
  587. };
  588. /**
  589. * Cast a value as DOM element
  590. * @param {HTMLElement | function | undefined} value
  591. * @param {HTMLElement} [defaultValue]
  592. * @returns {HTMLElement | null} dom
  593. */
  594. util.option.asElement = function (value, defaultValue) {
  595. if (typeof value == 'function') {
  596. value = value();
  597. }
  598. return value || defaultValue || null;
  599. };
  600. /**
  601. * load css from text
  602. * @param {String} css Text containing css
  603. */
  604. util.loadCss = function (css) {
  605. if (typeof document === 'undefined') {
  606. return;
  607. }
  608. // get the script location, and built the css file name from the js file name
  609. // http://stackoverflow.com/a/2161748/1262753
  610. // var scripts = document.getElementsByTagName('script');
  611. // var jsFile = scripts[scripts.length-1].src.split('?')[0];
  612. // var cssFile = jsFile.substring(0, jsFile.length - 2) + 'css';
  613. // inject css
  614. // http://stackoverflow.com/questions/524696/how-to-create-a-style-tag-with-javascript
  615. var style = document.createElement('style');
  616. style.type = 'text/css';
  617. if (style.styleSheet){
  618. style.styleSheet.cssText = css;
  619. } else {
  620. style.appendChild(document.createTextNode(css));
  621. }
  622. document.getElementsByTagName('head')[0].appendChild(style);
  623. };
  624. // Internet Explorer 8 and older does not support Array.indexOf, so we define
  625. // it here in that case.
  626. // http://soledadpenades.com/2007/05/17/arrayindexof-in-internet-explorer/
  627. if(!Array.prototype.indexOf) {
  628. Array.prototype.indexOf = function(obj){
  629. for(var i = 0; i < this.length; i++){
  630. if(this[i] == obj){
  631. return i;
  632. }
  633. }
  634. return -1;
  635. };
  636. try {
  637. console.log("Warning: Ancient browser detected. Please update your browser");
  638. }
  639. catch (err) {
  640. }
  641. }
  642. // Internet Explorer 8 and older does not support Array.forEach, so we define
  643. // it here in that case.
  644. // https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Array/forEach
  645. if (!Array.prototype.forEach) {
  646. Array.prototype.forEach = function(fn, scope) {
  647. for(var i = 0, len = this.length; i < len; ++i) {
  648. fn.call(scope || this, this[i], i, this);
  649. }
  650. }
  651. }
  652. // Internet Explorer 8 and older does not support Array.map, so we define it
  653. // here in that case.
  654. // https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Array/map
  655. // Production steps of ECMA-262, Edition 5, 15.4.4.19
  656. // Reference: http://es5.github.com/#x15.4.4.19
  657. if (!Array.prototype.map) {
  658. Array.prototype.map = function(callback, thisArg) {
  659. var T, A, k;
  660. if (this == null) {
  661. throw new TypeError(" this is null or not defined");
  662. }
  663. // 1. Let O be the result of calling ToObject passing the |this| value as the argument.
  664. var O = Object(this);
  665. // 2. Let lenValue be the result of calling the Get internal method of O with the argument "length".
  666. // 3. Let len be ToUint32(lenValue).
  667. var len = O.length >>> 0;
  668. // 4. If IsCallable(callback) is false, throw a TypeError exception.
  669. // See: http://es5.github.com/#x9.11
  670. if (typeof callback !== "function") {
  671. throw new TypeError(callback + " is not a function");
  672. }
  673. // 5. If thisArg was supplied, let T be thisArg; else let T be undefined.
  674. if (thisArg) {
  675. T = thisArg;
  676. }
  677. // 6. Let A be a new array created as if by the expression new Array(len) where Array is
  678. // the standard built-in constructor with that name and len is the value of len.
  679. A = new Array(len);
  680. // 7. Let k be 0
  681. k = 0;
  682. // 8. Repeat, while k < len
  683. while(k < len) {
  684. var kValue, mappedValue;
  685. // a. Let Pk be ToString(k).
  686. // This is implicit for LHS operands of the in operator
  687. // b. Let kPresent be the result of calling the HasProperty internal method of O with argument Pk.
  688. // This step can be combined with c
  689. // c. If kPresent is true, then
  690. if (k in O) {
  691. // i. Let kValue be the result of calling the Get internal method of O with argument Pk.
  692. kValue = O[ k ];
  693. // ii. Let mappedValue be the result of calling the Call internal method of callback
  694. // with T as the this value and argument list containing kValue, k, and O.
  695. mappedValue = callback.call(T, kValue, k, O);
  696. // iii. Call the DefineOwnProperty internal method of A with arguments
  697. // Pk, Property Descriptor {Value: mappedValue, : true, Enumerable: true, Configurable: true},
  698. // and false.
  699. // In browsers that support Object.defineProperty, use the following:
  700. // Object.defineProperty(A, Pk, { value: mappedValue, writable: true, enumerable: true, configurable: true });
  701. // For best browser support, use the following:
  702. A[ k ] = mappedValue;
  703. }
  704. // d. Increase k by 1.
  705. k++;
  706. }
  707. // 9. return A
  708. return A;
  709. };
  710. }
  711. // Internet Explorer 8 and older does not support Array.filter, so we define it
  712. // here in that case.
  713. // https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Array/filter
  714. if (!Array.prototype.filter) {
  715. Array.prototype.filter = function(fun /*, thisp */) {
  716. "use strict";
  717. if (this == null) {
  718. throw new TypeError();
  719. }
  720. var t = Object(this);
  721. var len = t.length >>> 0;
  722. if (typeof fun != "function") {
  723. throw new TypeError();
  724. }
  725. var res = [];
  726. var thisp = arguments[1];
  727. for (var i = 0; i < len; i++) {
  728. if (i in t) {
  729. var val = t[i]; // in case fun mutates this
  730. if (fun.call(thisp, val, i, t))
  731. res.push(val);
  732. }
  733. }
  734. return res;
  735. };
  736. }
  737. // Internet Explorer 8 and older does not support Object.keys, so we define it
  738. // here in that case.
  739. // https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Object/keys
  740. if (!Object.keys) {
  741. Object.keys = (function () {
  742. var hasOwnProperty = Object.prototype.hasOwnProperty,
  743. hasDontEnumBug = !({toString: null}).propertyIsEnumerable('toString'),
  744. dontEnums = [
  745. 'toString',
  746. 'toLocaleString',
  747. 'valueOf',
  748. 'hasOwnProperty',
  749. 'isPrototypeOf',
  750. 'propertyIsEnumerable',
  751. 'constructor'
  752. ],
  753. dontEnumsLength = dontEnums.length;
  754. return function (obj) {
  755. if (typeof obj !== 'object' && typeof obj !== 'function' || obj === null) {
  756. throw new TypeError('Object.keys called on non-object');
  757. }
  758. var result = [];
  759. for (var prop in obj) {
  760. if (hasOwnProperty.call(obj, prop)) result.push(prop);
  761. }
  762. if (hasDontEnumBug) {
  763. for (var i=0; i < dontEnumsLength; i++) {
  764. if (hasOwnProperty.call(obj, dontEnums[i])) result.push(dontEnums[i]);
  765. }
  766. }
  767. return result;
  768. }
  769. })()
  770. }
  771. // Internet Explorer 8 and older does not support Array.isArray,
  772. // so we define it here in that case.
  773. // https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Array/isArray
  774. if(!Array.isArray) {
  775. Array.isArray = function (vArg) {
  776. return Object.prototype.toString.call(vArg) === "[object Array]";
  777. };
  778. }
  779. // Internet Explorer 8 and older does not support Function.bind,
  780. // so we define it here in that case.
  781. // https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Function/bind
  782. if (!Function.prototype.bind) {
  783. Function.prototype.bind = function (oThis) {
  784. if (typeof this !== "function") {
  785. // closest thing possible to the ECMAScript 5 internal IsCallable function
  786. throw new TypeError("Function.prototype.bind - what is trying to be bound is not callable");
  787. }
  788. var aArgs = Array.prototype.slice.call(arguments, 1),
  789. fToBind = this,
  790. fNOP = function () {},
  791. fBound = function () {
  792. return fToBind.apply(this instanceof fNOP && oThis
  793. ? this
  794. : oThis,
  795. aArgs.concat(Array.prototype.slice.call(arguments)));
  796. };
  797. fNOP.prototype = this.prototype;
  798. fBound.prototype = new fNOP();
  799. return fBound;
  800. };
  801. }
  802. // https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Object/create
  803. if (!Object.create) {
  804. Object.create = function (o) {
  805. if (arguments.length > 1) {
  806. throw new Error('Object.create implementation only accepts the first parameter.');
  807. }
  808. function F() {}
  809. F.prototype = o;
  810. return new F();
  811. };
  812. }
  813. /**
  814. * Event listener (singleton)
  815. */
  816. // TODO: replace usage of the event listener for the EventBus
  817. var events = {
  818. 'listeners': [],
  819. /**
  820. * Find a single listener by its object
  821. * @param {Object} object
  822. * @return {Number} index -1 when not found
  823. */
  824. 'indexOf': function (object) {
  825. var listeners = this.listeners;
  826. for (var i = 0, iMax = this.listeners.length; i < iMax; i++) {
  827. var listener = listeners[i];
  828. if (listener && listener.object == object) {
  829. return i;
  830. }
  831. }
  832. return -1;
  833. },
  834. /**
  835. * Add an event listener
  836. * @param {Object} object
  837. * @param {String} event The name of an event, for example 'select'
  838. * @param {function} callback The callback method, called when the
  839. * event takes place
  840. */
  841. 'addListener': function (object, event, callback) {
  842. var index = this.indexOf(object);
  843. var listener = this.listeners[index];
  844. if (!listener) {
  845. listener = {
  846. 'object': object,
  847. 'events': {}
  848. };
  849. this.listeners.push(listener);
  850. }
  851. var callbacks = listener.events[event];
  852. if (!callbacks) {
  853. callbacks = [];
  854. listener.events[event] = callbacks;
  855. }
  856. // add the callback if it does not yet exist
  857. if (callbacks.indexOf(callback) == -1) {
  858. callbacks.push(callback);
  859. }
  860. },
  861. /**
  862. * Remove an event listener
  863. * @param {Object} object
  864. * @param {String} event The name of an event, for example 'select'
  865. * @param {function} callback The registered callback method
  866. */
  867. 'removeListener': function (object, event, callback) {
  868. var index = this.indexOf(object);
  869. var listener = this.listeners[index];
  870. if (listener) {
  871. var callbacks = listener.events[event];
  872. if (callbacks) {
  873. index = callbacks.indexOf(callback);
  874. if (index != -1) {
  875. callbacks.splice(index, 1);
  876. }
  877. // remove the array when empty
  878. if (callbacks.length == 0) {
  879. delete listener.events[event];
  880. }
  881. }
  882. // count the number of registered events. remove listener when empty
  883. var count = 0;
  884. var events = listener.events;
  885. for (var e in events) {
  886. if (events.hasOwnProperty(e)) {
  887. count++;
  888. }
  889. }
  890. if (count == 0) {
  891. delete this.listeners[index];
  892. }
  893. }
  894. },
  895. /**
  896. * Remove all registered event listeners
  897. */
  898. 'removeAllListeners': function () {
  899. this.listeners = [];
  900. },
  901. /**
  902. * Trigger an event. All registered event handlers will be called
  903. * @param {Object} object
  904. * @param {String} event
  905. * @param {Object} properties (optional)
  906. */
  907. 'trigger': function (object, event, properties) {
  908. var index = this.indexOf(object);
  909. var listener = this.listeners[index];
  910. if (listener) {
  911. var callbacks = listener.events[event];
  912. if (callbacks) {
  913. for (var i = 0, iMax = callbacks.length; i < iMax; i++) {
  914. callbacks[i](properties);
  915. }
  916. }
  917. }
  918. }
  919. };
  920. /**
  921. * @constructor TimeStep
  922. * The class TimeStep is an iterator for dates. You provide a start date and an
  923. * end date. The class itself determines the best scale (step size) based on the
  924. * provided start Date, end Date, and minimumStep.
  925. *
  926. * If minimumStep is provided, the step size is chosen as close as possible
  927. * to the minimumStep but larger than minimumStep. If minimumStep is not
  928. * provided, the scale is set to 1 DAY.
  929. * The minimumStep should correspond with the onscreen size of about 6 characters
  930. *
  931. * Alternatively, you can set a scale by hand.
  932. * After creation, you can initialize the class by executing first(). Then you
  933. * can iterate from the start date to the end date via next(). You can check if
  934. * the end date is reached with the function hasNext(). After each step, you can
  935. * retrieve the current date via getCurrent().
  936. * The TimeStep has scales ranging from milliseconds, seconds, minutes, hours,
  937. * days, to years.
  938. *
  939. * Version: 1.2
  940. *
  941. * @param {Date} [start] The start date, for example new Date(2010, 9, 21)
  942. * or new Date(2010, 9, 21, 23, 45, 00)
  943. * @param {Date} [end] The end date
  944. * @param {Number} [minimumStep] Optional. Minimum step size in milliseconds
  945. */
  946. TimeStep = function(start, end, minimumStep) {
  947. // variables
  948. this.current = new Date();
  949. this._start = new Date();
  950. this._end = new Date();
  951. this.autoScale = true;
  952. this.scale = TimeStep.SCALE.DAY;
  953. this.step = 1;
  954. // initialize the range
  955. this.setRange(start, end, minimumStep);
  956. };
  957. /// enum scale
  958. TimeStep.SCALE = {
  959. MILLISECOND: 1,
  960. SECOND: 2,
  961. MINUTE: 3,
  962. HOUR: 4,
  963. DAY: 5,
  964. WEEKDAY: 6,
  965. MONTH: 7,
  966. YEAR: 8
  967. };
  968. /**
  969. * Set a new range
  970. * If minimumStep is provided, the step size is chosen as close as possible
  971. * to the minimumStep but larger than minimumStep. If minimumStep is not
  972. * provided, the scale is set to 1 DAY.
  973. * The minimumStep should correspond with the onscreen size of about 6 characters
  974. * @param {Date} [start] The start date and time.
  975. * @param {Date} [end] The end date and time.
  976. * @param {int} [minimumStep] Optional. Minimum step size in milliseconds
  977. */
  978. TimeStep.prototype.setRange = function(start, end, minimumStep) {
  979. if (!(start instanceof Date) || !(end instanceof Date)) {
  980. //throw "No legal start or end date in method setRange";
  981. return;
  982. }
  983. this._start = (start != undefined) ? new Date(start.valueOf()) : new Date();
  984. this._end = (end != undefined) ? new Date(end.valueOf()) : new Date();
  985. if (this.autoScale) {
  986. this.setMinimumStep(minimumStep);
  987. }
  988. };
  989. /**
  990. * Set the range iterator to the start date.
  991. */
  992. TimeStep.prototype.first = function() {
  993. this.current = new Date(this._start.valueOf());
  994. this.roundToMinor();
  995. };
  996. /**
  997. * Round the current date to the first minor date value
  998. * This must be executed once when the current date is set to start Date
  999. */
  1000. TimeStep.prototype.roundToMinor = function() {
  1001. // round to floor
  1002. // IMPORTANT: we have no breaks in this switch! (this is no bug)
  1003. //noinspection FallthroughInSwitchStatementJS
  1004. switch (this.scale) {
  1005. case TimeStep.SCALE.YEAR:
  1006. this.current.setFullYear(this.step * Math.floor(this.current.getFullYear() / this.step));
  1007. this.current.setMonth(0);
  1008. case TimeStep.SCALE.MONTH: this.current.setDate(1);
  1009. case TimeStep.SCALE.DAY: // intentional fall through
  1010. case TimeStep.SCALE.WEEKDAY: this.current.setHours(0);
  1011. case TimeStep.SCALE.HOUR: this.current.setMinutes(0);
  1012. case TimeStep.SCALE.MINUTE: this.current.setSeconds(0);
  1013. case TimeStep.SCALE.SECOND: this.current.setMilliseconds(0);
  1014. //case TimeStep.SCALE.MILLISECOND: // nothing to do for milliseconds
  1015. }
  1016. if (this.step != 1) {
  1017. // round down to the first minor value that is a multiple of the current step size
  1018. switch (this.scale) {
  1019. case TimeStep.SCALE.MILLISECOND: this.current.setMilliseconds(this.current.getMilliseconds() - this.current.getMilliseconds() % this.step); break;
  1020. case TimeStep.SCALE.SECOND: this.current.setSeconds(this.current.getSeconds() - this.current.getSeconds() % this.step); break;
  1021. case TimeStep.SCALE.MINUTE: this.current.setMinutes(this.current.getMinutes() - this.current.getMinutes() % this.step); break;
  1022. case TimeStep.SCALE.HOUR: this.current.setHours(this.current.getHours() - this.current.getHours() % this.step); break;
  1023. case TimeStep.SCALE.WEEKDAY: // intentional fall through
  1024. case TimeStep.SCALE.DAY: this.current.setDate((this.current.getDate()-1) - (this.current.getDate()-1) % this.step + 1); break;
  1025. case TimeStep.SCALE.MONTH: this.current.setMonth(this.current.getMonth() - this.current.getMonth() % this.step); break;
  1026. case TimeStep.SCALE.YEAR: this.current.setFullYear(this.current.getFullYear() - this.current.getFullYear() % this.step); break;
  1027. default: break;
  1028. }
  1029. }
  1030. };
  1031. /**
  1032. * Check if the there is a next step
  1033. * @return {boolean} true if the current date has not passed the end date
  1034. */
  1035. TimeStep.prototype.hasNext = function () {
  1036. return (this.current.valueOf() <= this._end.valueOf());
  1037. };
  1038. /**
  1039. * Do the next step
  1040. */
  1041. TimeStep.prototype.next = function() {
  1042. var prev = this.current.valueOf();
  1043. // Two cases, needed to prevent issues with switching daylight savings
  1044. // (end of March and end of October)
  1045. if (this.current.getMonth() < 6) {
  1046. switch (this.scale) {
  1047. case TimeStep.SCALE.MILLISECOND:
  1048. this.current = new Date(this.current.valueOf() + this.step); break;
  1049. case TimeStep.SCALE.SECOND: this.current = new Date(this.current.valueOf() + this.step * 1000); break;
  1050. case TimeStep.SCALE.MINUTE: this.current = new Date(this.current.valueOf() + this.step * 1000 * 60); break;
  1051. case TimeStep.SCALE.HOUR:
  1052. this.current = new Date(this.current.valueOf() + this.step * 1000 * 60 * 60);
  1053. // in case of skipping an hour for daylight savings, adjust the hour again (else you get: 0h 5h 9h ... instead of 0h 4h 8h ...)
  1054. var h = this.current.getHours();
  1055. this.current.setHours(h - (h % this.step));
  1056. break;
  1057. case TimeStep.SCALE.WEEKDAY: // intentional fall through
  1058. case TimeStep.SCALE.DAY: this.current.setDate(this.current.getDate() + this.step); break;
  1059. case TimeStep.SCALE.MONTH: this.current.setMonth(this.current.getMonth() + this.step); break;
  1060. case TimeStep.SCALE.YEAR: this.current.setFullYear(this.current.getFullYear() + this.step); break;
  1061. default: break;
  1062. }
  1063. }
  1064. else {
  1065. switch (this.scale) {
  1066. case TimeStep.SCALE.MILLISECOND: this.current = new Date(this.current.valueOf() + this.step); break;
  1067. case TimeStep.SCALE.SECOND: this.current.setSeconds(this.current.getSeconds() + this.step); break;
  1068. case TimeStep.SCALE.MINUTE: this.current.setMinutes(this.current.getMinutes() + this.step); break;
  1069. case TimeStep.SCALE.HOUR: this.current.setHours(this.current.getHours() + this.step); break;
  1070. case TimeStep.SCALE.WEEKDAY: // intentional fall through
  1071. case TimeStep.SCALE.DAY: this.current.setDate(this.current.getDate() + this.step); break;
  1072. case TimeStep.SCALE.MONTH: this.current.setMonth(this.current.getMonth() + this.step); break;
  1073. case TimeStep.SCALE.YEAR: this.current.setFullYear(this.current.getFullYear() + this.step); break;
  1074. default: break;
  1075. }
  1076. }
  1077. if (this.step != 1) {
  1078. // round down to the correct major value
  1079. switch (this.scale) {
  1080. case TimeStep.SCALE.MILLISECOND: if(this.current.getMilliseconds() < this.step) this.current.setMilliseconds(0); break;
  1081. case TimeStep.SCALE.SECOND: if(this.current.getSeconds() < this.step) this.current.setSeconds(0); break;
  1082. case TimeStep.SCALE.MINUTE: if(this.current.getMinutes() < this.step) this.current.setMinutes(0); break;
  1083. case TimeStep.SCALE.HOUR: if(this.current.getHours() < this.step) this.current.setHours(0); break;
  1084. case TimeStep.SCALE.WEEKDAY: // intentional fall through
  1085. case TimeStep.SCALE.DAY: if(this.current.getDate() < this.step+1) this.current.setDate(1); break;
  1086. case TimeStep.SCALE.MONTH: if(this.current.getMonth() < this.step) this.current.setMonth(0); break;
  1087. case TimeStep.SCALE.YEAR: break; // nothing to do for year
  1088. default: break;
  1089. }
  1090. }
  1091. // safety mechanism: if current time is still unchanged, move to the end
  1092. if (this.current.valueOf() == prev) {
  1093. this.current = new Date(this._end.valueOf());
  1094. }
  1095. };
  1096. /**
  1097. * Get the current datetime
  1098. * @return {Date} current The current date
  1099. */
  1100. TimeStep.prototype.getCurrent = function() {
  1101. return this.current;
  1102. };
  1103. /**
  1104. * Set a custom scale. Autoscaling will be disabled.
  1105. * For example setScale(SCALE.MINUTES, 5) will result
  1106. * in minor steps of 5 minutes, and major steps of an hour.
  1107. *
  1108. * @param {TimeStep.SCALE} newScale
  1109. * A scale. Choose from SCALE.MILLISECOND,
  1110. * SCALE.SECOND, SCALE.MINUTE, SCALE.HOUR,
  1111. * SCALE.WEEKDAY, SCALE.DAY, SCALE.MONTH,
  1112. * SCALE.YEAR.
  1113. * @param {Number} newStep A step size, by default 1. Choose for
  1114. * example 1, 2, 5, or 10.
  1115. */
  1116. TimeStep.prototype.setScale = function(newScale, newStep) {
  1117. this.scale = newScale;
  1118. if (newStep > 0) {
  1119. this.step = newStep;
  1120. }
  1121. this.autoScale = false;
  1122. };
  1123. /**
  1124. * Enable or disable autoscaling
  1125. * @param {boolean} enable If true, autoascaling is set true
  1126. */
  1127. TimeStep.prototype.setAutoScale = function (enable) {
  1128. this.autoScale = enable;
  1129. };
  1130. /**
  1131. * Automatically determine the scale that bests fits the provided minimum step
  1132. * @param {Number} [minimumStep] The minimum step size in milliseconds
  1133. */
  1134. TimeStep.prototype.setMinimumStep = function(minimumStep) {
  1135. if (minimumStep == undefined) {
  1136. return;
  1137. }
  1138. var stepYear = (1000 * 60 * 60 * 24 * 30 * 12);
  1139. var stepMonth = (1000 * 60 * 60 * 24 * 30);
  1140. var stepDay = (1000 * 60 * 60 * 24);
  1141. var stepHour = (1000 * 60 * 60);
  1142. var stepMinute = (1000 * 60);
  1143. var stepSecond = (1000);
  1144. var stepMillisecond= (1);
  1145. // find the smallest step that is larger than the provided minimumStep
  1146. if (stepYear*1000 > minimumStep) {this.scale = TimeStep.SCALE.YEAR; this.step = 1000;}
  1147. if (stepYear*500 > minimumStep) {this.scale = TimeStep.SCALE.YEAR; this.step = 500;}
  1148. if (stepYear*100 > minimumStep) {this.scale = TimeStep.SCALE.YEAR; this.step = 100;}
  1149. if (stepYear*50 > minimumStep) {this.scale = TimeStep.SCALE.YEAR; this.step = 50;}
  1150. if (stepYear*10 > minimumStep) {this.scale = TimeStep.SCALE.YEAR; this.step = 10;}
  1151. if (stepYear*5 > minimumStep) {this.scale = TimeStep.SCALE.YEAR; this.step = 5;}
  1152. if (stepYear > minimumStep) {this.scale = TimeStep.SCALE.YEAR; this.step = 1;}
  1153. if (stepMonth*3 > minimumStep) {this.scale = TimeStep.SCALE.MONTH; this.step = 3;}
  1154. if (stepMonth > minimumStep) {this.scale = TimeStep.SCALE.MONTH; this.step = 1;}
  1155. if (stepDay*5 > minimumStep) {this.scale = TimeStep.SCALE.DAY; this.step = 5;}
  1156. if (stepDay*2 > minimumStep) {this.scale = TimeStep.SCALE.DAY; this.step = 2;}
  1157. if (stepDay > minimumStep) {this.scale = TimeStep.SCALE.DAY; this.step = 1;}
  1158. if (stepDay/2 > minimumStep) {this.scale = TimeStep.SCALE.WEEKDAY; this.step = 1;}
  1159. if (stepHour*4 > minimumStep) {this.scale = TimeStep.SCALE.HOUR; this.step = 4;}
  1160. if (stepHour > minimumStep) {this.scale = TimeStep.SCALE.HOUR; this.step = 1;}
  1161. if (stepMinute*15 > minimumStep) {this.scale = TimeStep.SCALE.MINUTE; this.step = 15;}
  1162. if (stepMinute*10 > minimumStep) {this.scale = TimeStep.SCALE.MINUTE; this.step = 10;}
  1163. if (stepMinute*5 > minimumStep) {this.scale = TimeStep.SCALE.MINUTE; this.step = 5;}
  1164. if (stepMinute > minimumStep) {this.scale = TimeStep.SCALE.MINUTE; this.step = 1;}
  1165. if (stepSecond*15 > minimumStep) {this.scale = TimeStep.SCALE.SECOND; this.step = 15;}
  1166. if (stepSecond*10 > minimumStep) {this.scale = TimeStep.SCALE.SECOND; this.step = 10;}
  1167. if (stepSecond*5 > minimumStep) {this.scale = TimeStep.SCALE.SECOND; this.step = 5;}
  1168. if (stepSecond > minimumStep) {this.scale = TimeStep.SCALE.SECOND; this.step = 1;}
  1169. if (stepMillisecond*200 > minimumStep) {this.scale = TimeStep.SCALE.MILLISECOND; this.step = 200;}
  1170. if (stepMillisecond*100 > minimumStep) {this.scale = TimeStep.SCALE.MILLISECOND; this.step = 100;}
  1171. if (stepMillisecond*50 > minimumStep) {this.scale = TimeStep.SCALE.MILLISECOND; this.step = 50;}
  1172. if (stepMillisecond*10 > minimumStep) {this.scale = TimeStep.SCALE.MILLISECOND; this.step = 10;}
  1173. if (stepMillisecond*5 > minimumStep) {this.scale = TimeStep.SCALE.MILLISECOND; this.step = 5;}
  1174. if (stepMillisecond > minimumStep) {this.scale = TimeStep.SCALE.MILLISECOND; this.step = 1;}
  1175. };
  1176. /**
  1177. * Snap a date to a rounded value. The snap intervals are dependent on the
  1178. * current scale and step.
  1179. * @param {Date} date the date to be snapped
  1180. */
  1181. TimeStep.prototype.snap = function(date) {
  1182. if (this.scale == TimeStep.SCALE.YEAR) {
  1183. var year = date.getFullYear() + Math.round(date.getMonth() / 12);
  1184. date.setFullYear(Math.round(year / this.step) * this.step);
  1185. date.setMonth(0);
  1186. date.setDate(0);
  1187. date.setHours(0);
  1188. date.setMinutes(0);
  1189. date.setSeconds(0);
  1190. date.setMilliseconds(0);
  1191. }
  1192. else if (this.scale == TimeStep.SCALE.MONTH) {
  1193. if (date.getDate() > 15) {
  1194. date.setDate(1);
  1195. date.setMonth(date.getMonth() + 1);
  1196. // important: first set Date to 1, after that change the month.
  1197. }
  1198. else {
  1199. date.setDate(1);
  1200. }
  1201. date.setHours(0);
  1202. date.setMinutes(0);
  1203. date.setSeconds(0);
  1204. date.setMilliseconds(0);
  1205. }
  1206. else if (this.scale == TimeStep.SCALE.DAY ||
  1207. this.scale == TimeStep.SCALE.WEEKDAY) {
  1208. //noinspection FallthroughInSwitchStatementJS
  1209. switch (this.step) {
  1210. case 5:
  1211. case 2:
  1212. date.setHours(Math.round(date.getHours() / 24) * 24); break;
  1213. default:
  1214. date.setHours(Math.round(date.getHours() / 12) * 12); break;
  1215. }
  1216. date.setMinutes(0);
  1217. date.setSeconds(0);
  1218. date.setMilliseconds(0);
  1219. }
  1220. else if (this.scale == TimeStep.SCALE.HOUR) {
  1221. switch (this.step) {
  1222. case 4:
  1223. date.setMinutes(Math.round(date.getMinutes() / 60) * 60); break;
  1224. default:
  1225. date.setMinutes(Math.round(date.getMinutes() / 30) * 30); break;
  1226. }
  1227. date.setSeconds(0);
  1228. date.setMilliseconds(0);
  1229. } else if (this.scale == TimeStep.SCALE.MINUTE) {
  1230. //noinspection FallthroughInSwitchStatementJS
  1231. switch (this.step) {
  1232. case 15:
  1233. case 10:
  1234. date.setMinutes(Math.round(date.getMinutes() / 5) * 5);
  1235. date.setSeconds(0);
  1236. break;
  1237. case 5:
  1238. date.setSeconds(Math.round(date.getSeconds() / 60) * 60); break;
  1239. default:
  1240. date.setSeconds(Math.round(date.getSeconds() / 30) * 30); break;
  1241. }
  1242. date.setMilliseconds(0);
  1243. }
  1244. else if (this.scale == TimeStep.SCALE.SECOND) {
  1245. //noinspection FallthroughInSwitchStatementJS
  1246. switch (this.step) {
  1247. case 15:
  1248. case 10:
  1249. date.setSeconds(Math.round(date.getSeconds() / 5) * 5);
  1250. date.setMilliseconds(0);
  1251. break;
  1252. case 5:
  1253. date.setMilliseconds(Math.round(date.getMilliseconds() / 1000) * 1000); break;
  1254. default:
  1255. date.setMilliseconds(Math.round(date.getMilliseconds() / 500) * 500); break;
  1256. }
  1257. }
  1258. else if (this.scale == TimeStep.SCALE.MILLISECOND) {
  1259. var step = this.step > 5 ? this.step / 2 : 1;
  1260. date.setMilliseconds(Math.round(date.getMilliseconds() / step) * step);
  1261. }
  1262. };
  1263. /**
  1264. * Check if the current value is a major value (for example when the step
  1265. * is DAY, a major value is each first day of the MONTH)
  1266. * @return {boolean} true if current date is major, else false.
  1267. */
  1268. TimeStep.prototype.isMajor = function() {
  1269. switch (this.scale) {
  1270. case TimeStep.SCALE.MILLISECOND:
  1271. return (this.current.getMilliseconds() == 0);
  1272. case TimeStep.SCALE.SECOND:
  1273. return (this.current.getSeconds() == 0);
  1274. case TimeStep.SCALE.MINUTE:
  1275. return (this.current.getHours() == 0) && (this.current.getMinutes() == 0);
  1276. // Note: this is no bug. Major label is equal for both minute and hour scale
  1277. case TimeStep.SCALE.HOUR:
  1278. return (this.current.getHours() == 0);
  1279. case TimeStep.SCALE.WEEKDAY: // intentional fall through
  1280. case TimeStep.SCALE.DAY:
  1281. return (this.current.getDate() == 1);
  1282. case TimeStep.SCALE.MONTH:
  1283. return (this.current.getMonth() == 0);
  1284. case TimeStep.SCALE.YEAR:
  1285. return false;
  1286. default:
  1287. return false;
  1288. }
  1289. };
  1290. /**
  1291. * Returns formatted text for the minor axislabel, depending on the current
  1292. * date and the scale. For example when scale is MINUTE, the current time is
  1293. * formatted as "hh:mm".
  1294. * @param {Date} [date] custom date. if not provided, current date is taken
  1295. */
  1296. TimeStep.prototype.getLabelMinor = function(date) {
  1297. if (date == undefined) {
  1298. date = this.current;
  1299. }
  1300. switch (this.scale) {
  1301. case TimeStep.SCALE.MILLISECOND: return moment(date).format('SSS');
  1302. case TimeStep.SCALE.SECOND: return moment(date).format('s');
  1303. case TimeStep.SCALE.MINUTE: return moment(date).format('HH:mm');
  1304. case TimeStep.SCALE.HOUR: return moment(date).format('HH:mm');
  1305. case TimeStep.SCALE.WEEKDAY: return moment(date).format('ddd D');
  1306. case TimeStep.SCALE.DAY: return moment(date).format('D');
  1307. case TimeStep.SCALE.MONTH: return moment(date).format('MMM');
  1308. case TimeStep.SCALE.YEAR: return moment(date).format('YYYY');
  1309. default: return '';
  1310. }
  1311. };
  1312. /**
  1313. * Returns formatted text for the major axis label, depending on the current
  1314. * date and the scale. For example when scale is MINUTE, the major scale is
  1315. * hours, and the hour will be formatted as "hh".
  1316. * @param {Date} [date] custom date. if not provided, current date is taken
  1317. */
  1318. TimeStep.prototype.getLabelMajor = function(date) {
  1319. if (date == undefined) {
  1320. date = this.current;
  1321. }
  1322. //noinspection FallthroughInSwitchStatementJS
  1323. switch (this.scale) {
  1324. case TimeStep.SCALE.MILLISECOND:return moment(date).format('HH:mm:ss');
  1325. case TimeStep.SCALE.SECOND: return moment(date).format('D MMMM HH:mm');
  1326. case TimeStep.SCALE.MINUTE:
  1327. case TimeStep.SCALE.HOUR: return moment(date).format('ddd D MMMM');
  1328. case TimeStep.SCALE.WEEKDAY:
  1329. case TimeStep.SCALE.DAY: return moment(date).format('MMMM YYYY');
  1330. case TimeStep.SCALE.MONTH: return moment(date).format('YYYY');
  1331. case TimeStep.SCALE.YEAR: return '';
  1332. default: return '';
  1333. }
  1334. };
  1335. /**
  1336. * DataSet
  1337. *
  1338. * Usage:
  1339. * var dataSet = new DataSet({
  1340. * fieldId: '_id',
  1341. * fieldTypes: {
  1342. * // ...
  1343. * }
  1344. * });
  1345. *
  1346. * dataSet.add(item);
  1347. * dataSet.add(data);
  1348. * dataSet.update(item);
  1349. * dataSet.update(data);
  1350. * dataSet.remove(id);
  1351. * dataSet.remove(ids);
  1352. * var data = dataSet.get();
  1353. * var data = dataSet.get(id);
  1354. * var data = dataSet.get(ids);
  1355. * var data = dataSet.get(ids, options, data);
  1356. * dataSet.clear();
  1357. *
  1358. * A data set can:
  1359. * - add/remove/update data
  1360. * - gives triggers upon changes in the data
  1361. * - can import/export data in various data formats
  1362. *
  1363. * @param {Object} [options] Available options:
  1364. * {String} fieldId Field name of the id in the
  1365. * items, 'id' by default.
  1366. * {Object.<String, String} fieldTypes
  1367. * A map with field names as key,
  1368. * and the field type as value.
  1369. * @constructor DataSet
  1370. */
  1371. function DataSet (options) {
  1372. this.id = util.randomUUID();
  1373. this.options = options || {};
  1374. this.data = {}; // map with data indexed by id
  1375. this.fieldId = this.options.fieldId || 'id'; // name of the field containing id
  1376. this.fieldTypes = {}; // field types by field name
  1377. if (this.options.fieldTypes) {
  1378. for (var field in this.options.fieldTypes) {
  1379. if (this.options.fieldTypes.hasOwnProperty(field)) {
  1380. var value = this.options.fieldTypes[field];
  1381. if (value == 'Date' || value == 'ISODate' || value == 'ASPDate') {
  1382. this.fieldTypes[field] = 'Date';
  1383. }
  1384. else {
  1385. this.fieldTypes[field] = value;
  1386. }
  1387. }
  1388. }
  1389. }
  1390. // event subscribers
  1391. this.subscribers = {};
  1392. this.internalIds = {}; // internally generated id's
  1393. }
  1394. /**
  1395. * Subscribe to an event, add an event listener
  1396. * @param {String} event Event name. Available events: 'put', 'update',
  1397. * 'remove'
  1398. * @param {function} callback Callback method. Called with three parameters:
  1399. * {String} event
  1400. * {Object | null} params
  1401. * {String} senderId
  1402. * @param {String} [id] Optional id for the sender, used to filter
  1403. * events triggered by the sender itself.
  1404. */
  1405. DataSet.prototype.subscribe = function (event, callback, id) {
  1406. var subscribers = this.subscribers[event];
  1407. if (!subscribers) {
  1408. subscribers = [];
  1409. this.subscribers[event] = subscribers;
  1410. }
  1411. subscribers.push({
  1412. id: id ? String(id) : null,
  1413. callback: callback
  1414. });
  1415. };
  1416. /**
  1417. * Unsubscribe from an event, remove an event listener
  1418. * @param {String} event
  1419. * @param {function} callback
  1420. */
  1421. DataSet.prototype.unsubscribe = function (event, callback) {
  1422. var subscribers = this.subscribers[event];
  1423. if (subscribers) {
  1424. this.subscribers[event] = subscribers.filter(function (listener) {
  1425. return (listener.callback != callback);
  1426. });
  1427. }
  1428. };
  1429. /**
  1430. * Trigger an event
  1431. * @param {String} event
  1432. * @param {Object | null} params
  1433. * @param {String} [senderId] Optional id of the sender.
  1434. * @private
  1435. */
  1436. DataSet.prototype._trigger = function (event, params, senderId) {
  1437. if (event == '*') {
  1438. throw new Error('Cannot trigger event *');
  1439. }
  1440. var subscribers = [];
  1441. if (event in this.subscribers) {
  1442. subscribers = subscribers.concat(this.subscribers[event]);
  1443. }
  1444. if ('*' in this.subscribers) {
  1445. subscribers = subscribers.concat(this.subscribers['*']);
  1446. }
  1447. for (var i = 0; i < subscribers.length; i++) {
  1448. var subscriber = subscribers[i];
  1449. if (subscriber.callback) {
  1450. subscriber.callback(event, params, senderId || null);
  1451. }
  1452. }
  1453. };
  1454. /**
  1455. * Add data.
  1456. * Adding an item will fail when there already is an item with the same id.
  1457. * @param {Object | Array | DataTable} data
  1458. * @param {String} [senderId] Optional sender id
  1459. */
  1460. DataSet.prototype.add = function (data, senderId) {
  1461. var addedItems = [],
  1462. id,
  1463. me = this;
  1464. if (data instanceof Array) {
  1465. // Array
  1466. for (var i = 0, len = data.length; i < len; i++) {
  1467. id = me._addItem(data[i]);
  1468. addedItems.push(id);
  1469. }
  1470. }
  1471. else if (util.isDataTable(data)) {
  1472. // Google DataTable
  1473. var columns = this._getColumnNames(data);
  1474. for (var row = 0, rows = data.getNumberOfRows(); row < rows; row++) {
  1475. var item = {};
  1476. for (var col = 0, cols = columns.length; col < cols; col++) {
  1477. var field = columns[col];
  1478. item[field] = data.getValue(row, col);
  1479. }
  1480. id = me._addItem(item);
  1481. addedItems.push(id);
  1482. }
  1483. }
  1484. else if (data instanceof Object) {
  1485. // Single item
  1486. id = me._addItem(data);
  1487. addedItems.push(id);
  1488. }
  1489. else {
  1490. throw new Error('Unknown dataType');
  1491. }
  1492. if (addedItems.length) {
  1493. this._trigger('add', {items: addedItems}, senderId);
  1494. }
  1495. };
  1496. /**
  1497. * Update existing items. When an item does not exist, it will be created
  1498. * @param {Object | Array | DataTable} data
  1499. * @param {String} [senderId] Optional sender id
  1500. */
  1501. DataSet.prototype.update = function (data, senderId) {
  1502. var addedItems = [],
  1503. updatedItems = [],
  1504. me = this,
  1505. fieldId = me.fieldId;
  1506. var addOrUpdate = function (item) {
  1507. var id = item[fieldId];
  1508. if (me.data[id]) {
  1509. // update item
  1510. id = me._updateItem(item);
  1511. updatedItems.push(id);
  1512. }
  1513. else {
  1514. // add new item
  1515. id = me._addItem(item);
  1516. addedItems.push(id);
  1517. }
  1518. };
  1519. if (data instanceof Array) {
  1520. // Array
  1521. for (var i = 0, len = data.length; i < len; i++) {
  1522. addOrUpdate(data[i]);
  1523. }
  1524. }
  1525. else if (util.isDataTable(data)) {
  1526. // Google DataTable
  1527. var columns = this._getColumnNames(data);
  1528. for (var row = 0, rows = data.getNumberOfRows(); row < rows; row++) {
  1529. var item = {};
  1530. for (var col = 0, cols = columns.length; col < cols; col++) {
  1531. var field = columns[col];
  1532. item[field] = data.getValue(row, col);
  1533. }
  1534. addOrUpdate(item);
  1535. }
  1536. }
  1537. else if (data instanceof Object) {
  1538. // Single item
  1539. addOrUpdate(data);
  1540. }
  1541. else {
  1542. throw new Error('Unknown dataType');
  1543. }
  1544. if (addedItems.length) {
  1545. this._trigger('add', {items: addedItems}, senderId);
  1546. }
  1547. if (updatedItems.length) {
  1548. this._trigger('update', {items: updatedItems}, senderId);
  1549. }
  1550. };
  1551. /**
  1552. * Get a data item or multiple items.
  1553. *
  1554. * Usage:
  1555. *
  1556. * get()
  1557. * get(options: Object)
  1558. * get(options: Object, data: Array | DataTable)
  1559. *
  1560. * get(id: Number | String)
  1561. * get(id: Number | String, options: Object)
  1562. * get(id: Number | String, options: Object, data: Array | DataTable)
  1563. *
  1564. * get(ids: Number[] | String[])
  1565. * get(ids: Number[] | String[], options: Object)
  1566. * get(ids: Number[] | String[], options: Object, data: Array | DataTable)
  1567. *
  1568. * Where:
  1569. *
  1570. * {Number | String} id The id of an item
  1571. * {Number[] | String{}} ids An array with ids of items
  1572. * {Object} options An Object with options. Available options:
  1573. * {String} [type] Type of data to be returned. Can
  1574. * be 'DataTable' or 'Array' (default)
  1575. * {Object.<String, String>} [fieldTypes]
  1576. * {String[]} [fields] field names to be returned
  1577. * {function} [filter] filter items
  1578. * {String | function} [order] Order the items by
  1579. * a field name or custom sort function.
  1580. * {Array | DataTable} [data] If provided, items will be appended to this
  1581. * array or table. Required in case of Google
  1582. * DataTable.
  1583. *
  1584. * @throws Error
  1585. */
  1586. DataSet.prototype.get = function (args) {
  1587. var me = this;
  1588. // parse the arguments
  1589. var id, ids, options, data;
  1590. var firstType = util.getType(arguments[0]);
  1591. if (firstType == 'String' || firstType == 'Number') {
  1592. // get(id [, options] [, data])
  1593. id = arguments[0];
  1594. options = arguments[1];
  1595. data = arguments[2];
  1596. }
  1597. else if (firstType == 'Array') {
  1598. // get(ids [, options] [, data])
  1599. ids = arguments[0];
  1600. options = arguments[1];
  1601. data = arguments[2];
  1602. }
  1603. else {
  1604. // get([, options] [, data])
  1605. options = arguments[0];
  1606. data = arguments[1];
  1607. }
  1608. // determine the return type
  1609. var type;
  1610. if (options && options.type) {
  1611. type = (options.type == 'DataTable') ? 'DataTable' : 'Array';
  1612. if (data && (type != util.getType(data))) {
  1613. throw new Error('Type of parameter "data" (' + util.getType(data) + ') ' +
  1614. 'does not correspond with specified options.type (' + options.type + ')');
  1615. }
  1616. if (type == 'DataTable' && !util.isDataTable(data)) {
  1617. throw new Error('Parameter "data" must be a DataTable ' +
  1618. 'when options.type is "DataTable"');
  1619. }
  1620. }
  1621. else if (data) {
  1622. type = (util.getType(data) == 'DataTable') ? 'DataTable' : 'Array';
  1623. }
  1624. else {
  1625. type = 'Array';
  1626. }
  1627. // build options
  1628. var fieldTypes = options && options.fieldTypes || this.options.fieldTypes;
  1629. var filter = options && options.filter;
  1630. var items = [], item, itemId, i, len;
  1631. // cast items
  1632. if (id != undefined) {
  1633. // return a single item
  1634. item = me._getItem(id, fieldTypes);
  1635. if (filter && !filter(item)) {
  1636. item = null;
  1637. }
  1638. }
  1639. else if (ids != undefined) {
  1640. // return a subset of items
  1641. for (i = 0, len = ids.length; i < len; i++) {
  1642. item = me._getItem(ids[i], fieldTypes);
  1643. if (!filter || filter(item)) {
  1644. items.push(item);
  1645. }
  1646. }
  1647. }
  1648. else {
  1649. // return all items
  1650. for (itemId in this.data) {
  1651. if (this.data.hasOwnProperty(itemId)) {
  1652. item = me._getItem(itemId, fieldTypes);
  1653. if (!filter || filter(item)) {
  1654. items.push(item);
  1655. }
  1656. }
  1657. }
  1658. }
  1659. // order the results
  1660. if (options && options.order && id == undefined) {
  1661. this._sort(items, options.order);
  1662. }
  1663. // filter fields of the items
  1664. if (options && options.fields) {
  1665. var fields = options.fields;
  1666. if (id != undefined) {
  1667. item = this._filterFields(item, fields);
  1668. }
  1669. else {
  1670. for (i = 0, len = items.length; i < len; i++) {
  1671. items[i] = this._filterFields(items[i], fields);
  1672. }
  1673. }
  1674. }
  1675. // return the results
  1676. if (type == 'DataTable') {
  1677. var columns = this._getColumnNames(data);
  1678. if (id != undefined) {
  1679. // append a single item to the data table
  1680. me._appendRow(data, columns, item);
  1681. }
  1682. else {
  1683. // copy the items to the provided data table
  1684. for (i = 0, len = items.length; i < len; i++) {
  1685. me._appendRow(data, columns, items[i]);
  1686. }
  1687. }
  1688. return data;
  1689. }
  1690. else {
  1691. // return an array
  1692. if (id != undefined) {
  1693. // a single item
  1694. return item;
  1695. }
  1696. else {
  1697. // multiple items
  1698. if (data) {
  1699. // copy the items to the provided array
  1700. for (i = 0, len = items.length; i < len; i++) {
  1701. data.push(items[i]);
  1702. }
  1703. return data;
  1704. }
  1705. else {
  1706. // just return our array
  1707. return items;
  1708. }
  1709. }
  1710. }
  1711. };
  1712. /**
  1713. * Get ids of all items or from a filtered set of items.
  1714. * @param {Object} [options] An Object with options. Available options:
  1715. * {function} [filter] filter items
  1716. * {String | function} [order] Order the items by
  1717. * a field name or custom sort function.
  1718. * @return {Array} ids
  1719. */
  1720. DataSet.prototype.getIds = function (options) {
  1721. var data = this.data,
  1722. filter = options && options.filter,
  1723. order = options && options.order,
  1724. fieldTypes = options && options.fieldTypes || this.options.fieldTypes,
  1725. i,
  1726. len,
  1727. id,
  1728. item,
  1729. items,
  1730. ids = [];
  1731. if (filter) {
  1732. // get filtered items
  1733. if (order) {
  1734. // create ordered list
  1735. items = [];
  1736. for (id in data) {
  1737. if (data.hasOwnProperty(id)) {
  1738. item = this._getItem(id, fieldTypes);
  1739. if (filter(item)) {
  1740. items.push(item);
  1741. }
  1742. }
  1743. }
  1744. this._sort(items, order);
  1745. for (i = 0, len = items.length; i < len; i++) {
  1746. ids[i] = items[i][this.fieldId];
  1747. }
  1748. }
  1749. else {
  1750. // create unordered list
  1751. for (id in data) {
  1752. if (data.hasOwnProperty(id)) {
  1753. item = this._getItem(id, fieldTypes);
  1754. if (filter(item)) {
  1755. ids.push(item[this.fieldId]);
  1756. }
  1757. }
  1758. }
  1759. }
  1760. }
  1761. else {
  1762. // get all items
  1763. if (order) {
  1764. // create an ordered list
  1765. items = [];
  1766. for (id in data) {
  1767. if (data.hasOwnProperty(id)) {
  1768. items.push(data[id]);
  1769. }
  1770. }
  1771. this._sort(items, order);
  1772. for (i = 0, len = items.length; i < len; i++) {
  1773. ids[i] = items[i][this.fieldId];
  1774. }
  1775. }
  1776. else {
  1777. // create unordered list
  1778. for (id in data) {
  1779. if (data.hasOwnProperty(id)) {
  1780. item = data[id];
  1781. ids.push(item[this.fieldId]);
  1782. }
  1783. }
  1784. }
  1785. }
  1786. return ids;
  1787. };
  1788. /**
  1789. * Execute a callback function for every item in the dataset.
  1790. * The order of the items is not determined.
  1791. * @param {function} callback
  1792. * @param {Object} [options] Available options:
  1793. * {Object.<String, String>} [fieldTypes]
  1794. * {String[]} [fields] filter fields
  1795. * {function} [filter] filter items
  1796. * {String | function} [order] Order the items by
  1797. * a field name or custom sort function.
  1798. */
  1799. DataSet.prototype.forEach = function (callback, options) {
  1800. var filter = options && options.filter,
  1801. fieldTypes = options && options.fieldTypes || this.options.fieldTypes,
  1802. data = this.data,
  1803. item,
  1804. id;
  1805. if (options && options.order) {
  1806. // execute forEach on ordered list
  1807. var items = this.get(options);
  1808. for (var i = 0, len = items.length; i < len; i++) {
  1809. item = items[i];
  1810. id = item[this.fieldId];
  1811. callback(item, id);
  1812. }
  1813. }
  1814. else {
  1815. // unordered
  1816. for (id in data) {
  1817. if (data.hasOwnProperty(id)) {
  1818. item = this._getItem(id, fieldTypes);
  1819. if (!filter || filter(item)) {
  1820. callback(item, id);
  1821. }
  1822. }
  1823. }
  1824. }
  1825. };
  1826. /**
  1827. * Map every item in the dataset.
  1828. * @param {function} callback
  1829. * @param {Object} [options] Available options:
  1830. * {Object.<String, String>} [fieldTypes]
  1831. * {String[]} [fields] filter fields
  1832. * {function} [filter] filter items
  1833. * {String | function} [order] Order the items by
  1834. * a field name or custom sort function.
  1835. * @return {Object[]} mappedItems
  1836. */
  1837. DataSet.prototype.map = function (callback, options) {
  1838. var filter = options && options.filter,
  1839. fieldTypes = options && options.fieldTypes || this.options.fieldTypes,
  1840. mappedItems = [],
  1841. data = this.data,
  1842. item;
  1843. // cast and filter items
  1844. for (var id in data) {
  1845. if (data.hasOwnProperty(id)) {
  1846. item = this._getItem(id, fieldTypes);
  1847. if (!filter || filter(item)) {
  1848. mappedItems.push(callback(item, id));
  1849. }
  1850. }
  1851. }
  1852. // order items
  1853. if (options && options.order) {
  1854. this._sort(mappedItems, options.order);
  1855. }
  1856. return mappedItems;
  1857. };
  1858. /**
  1859. * Filter the fields of an item
  1860. * @param {Object} item
  1861. * @param {String[]} fields Field names
  1862. * @return {Object} filteredItem
  1863. * @private
  1864. */
  1865. DataSet.prototype._filterFields = function (item, fields) {
  1866. var filteredItem = {};
  1867. for (var field in item) {
  1868. if (item.hasOwnProperty(field) && (fields.indexOf(field) != -1)) {
  1869. filteredItem[field] = item[field];
  1870. }
  1871. }
  1872. return filteredItem;
  1873. };
  1874. /**
  1875. * Sort the provided array with items
  1876. * @param {Object[]} items
  1877. * @param {String | function} order A field name or custom sort function.
  1878. * @private
  1879. */
  1880. DataSet.prototype._sort = function (items, order) {
  1881. if (util.isString(order)) {
  1882. // order by provided field name
  1883. var name = order; // field name
  1884. items.sort(function (a, b) {
  1885. var av = a[name];
  1886. var bv = b[name];
  1887. return (av > bv) ? 1 : ((av < bv) ? -1 : 0);
  1888. });
  1889. }
  1890. else if (typeof order === 'function') {
  1891. // order by sort function
  1892. items.sort(order);
  1893. }
  1894. // TODO: extend order by an Object {field:String, direction:String}
  1895. // where direction can be 'asc' or 'desc'
  1896. else {
  1897. throw new TypeError('Order must be a function or a string');
  1898. }
  1899. };
  1900. /**
  1901. * Remove an object by pointer or by id
  1902. * @param {String | Number | Object | Array} id Object or id, or an array with
  1903. * objects or ids to be removed
  1904. * @param {String} [senderId] Optional sender id
  1905. */
  1906. DataSet.prototype.remove = function (id, senderId) {
  1907. var removedItems = [],
  1908. i, len;
  1909. if (util.isNumber(id) || util.isString(id)) {
  1910. delete this.data[id];
  1911. delete this.internalIds[id];
  1912. removedItems.push(id);
  1913. }
  1914. else if (id instanceof Array) {
  1915. for (i = 0, len = id.length; i < len; i++) {
  1916. this.remove(id[i]);
  1917. }
  1918. removedItems = items.concat(id);
  1919. }
  1920. else if (id instanceof Object) {
  1921. // search for the object
  1922. for (i in this.data) {
  1923. if (this.data.hasOwnProperty(i)) {
  1924. if (this.data[i] == id) {
  1925. delete this.data[i];
  1926. delete this.internalIds[i];
  1927. removedItems.push(i);
  1928. }
  1929. }
  1930. }
  1931. }
  1932. if (removedItems.length) {
  1933. this._trigger('remove', {items: removedItems}, senderId);
  1934. }
  1935. };
  1936. /**
  1937. * Clear the data
  1938. * @param {String} [senderId] Optional sender id
  1939. */
  1940. DataSet.prototype.clear = function (senderId) {
  1941. var ids = Object.keys(this.data);
  1942. this.data = {};
  1943. this.internalIds = {};
  1944. this._trigger('remove', {items: ids}, senderId);
  1945. };
  1946. /**
  1947. * Find the item with maximum value of a specified field
  1948. * @param {String} field
  1949. * @return {Object | null} item Item containing max value, or null if no items
  1950. */
  1951. DataSet.prototype.max = function (field) {
  1952. var data = this.data,
  1953. max = null,
  1954. maxField = null;
  1955. for (var id in data) {
  1956. if (data.hasOwnProperty(id)) {
  1957. var item = data[id];
  1958. var itemField = item[field];
  1959. if (itemField != null && (!max || itemField > maxField)) {
  1960. max = item;
  1961. maxField = itemField;
  1962. }
  1963. }
  1964. }
  1965. return max;
  1966. };
  1967. /**
  1968. * Find the item with minimum value of a specified field
  1969. * @param {String} field
  1970. * @return {Object | null} item Item containing max value, or null if no items
  1971. */
  1972. DataSet.prototype.min = function (field) {
  1973. var data = this.data,
  1974. min = null,
  1975. minField = null;
  1976. for (var id in data) {
  1977. if (data.hasOwnProperty(id)) {
  1978. var item = data[id];
  1979. var itemField = item[field];
  1980. if (itemField != null && (!min || itemField < minField)) {
  1981. min = item;
  1982. minField = itemField;
  1983. }
  1984. }
  1985. }
  1986. return min;
  1987. };
  1988. /**
  1989. * Find all distinct values of a specified field
  1990. * @param {String} field
  1991. * @return {Array} values Array containing all distinct values. If the data
  1992. * items do not contain the specified field, an array
  1993. * containing a single value undefined is returned.
  1994. * The returned array is unordered.
  1995. */
  1996. DataSet.prototype.distinct = function (field) {
  1997. var data = this.data,
  1998. values = [],
  1999. fieldType = this.options.fieldTypes[field],
  2000. count = 0;
  2001. for (var prop in data) {
  2002. if (data.hasOwnProperty(prop)) {
  2003. var item = data[prop];
  2004. var value = util.cast(item[field], fieldType);
  2005. var exists = false;
  2006. for (var i = 0; i < count; i++) {
  2007. if (values[i] == value) {
  2008. exists = true;
  2009. break;
  2010. }
  2011. }
  2012. if (!exists) {
  2013. values[count] = value;
  2014. count++;
  2015. }
  2016. }
  2017. }
  2018. return values;
  2019. };
  2020. /**
  2021. * Add a single item. Will fail when an item with the same id already exists.
  2022. * @param {Object} item
  2023. * @return {String} id
  2024. * @private
  2025. */
  2026. DataSet.prototype._addItem = function (item) {
  2027. var id = item[this.fieldId];
  2028. if (id != undefined) {
  2029. // check whether this id is already taken
  2030. if (this.data[id]) {
  2031. // item already exists
  2032. throw new Error('Cannot add item: item with id ' + id + ' already exists');
  2033. }
  2034. }
  2035. else {
  2036. // generate an id
  2037. id = util.randomUUID();
  2038. item[this.fieldId] = id;
  2039. this.internalIds[id] = item;
  2040. }
  2041. var d = {};
  2042. for (var field in item) {
  2043. if (item.hasOwnProperty(field)) {
  2044. var type = this.fieldTypes[field]; // type may be undefined
  2045. d[field] = util.cast(item[field], type);
  2046. }
  2047. }
  2048. this.data[id] = d;
  2049. return id;
  2050. };
  2051. /**
  2052. * Get an item. Fields can be casted to a specific type
  2053. * @param {String} id
  2054. * @param {Object.<String, String>} [fieldTypes] Cast field types
  2055. * @return {Object | null} item
  2056. * @private
  2057. */
  2058. DataSet.prototype._getItem = function (id, fieldTypes) {
  2059. var field, value;
  2060. // get the item from the dataset
  2061. var raw = this.data[id];
  2062. if (!raw) {
  2063. return null;
  2064. }
  2065. // cast the items field types
  2066. var casted = {},
  2067. fieldId = this.fieldId,
  2068. internalIds = this.internalIds;
  2069. if (fieldTypes) {
  2070. for (field in raw) {
  2071. if (raw.hasOwnProperty(field)) {
  2072. value = raw[field];
  2073. // output all fields, except internal ids
  2074. if ((field != fieldId) || !(value in internalIds)) {
  2075. casted[field] = util.cast(value, fieldTypes[field]);
  2076. }
  2077. }
  2078. }
  2079. }
  2080. else {
  2081. // no field types specified, no casting needed
  2082. for (field in raw) {
  2083. if (raw.hasOwnProperty(field)) {
  2084. value = raw[field];
  2085. // output all fields, except internal ids
  2086. if ((field != fieldId) || !(value in internalIds)) {
  2087. casted[field] = value;
  2088. }
  2089. }
  2090. }
  2091. }
  2092. return casted;
  2093. };
  2094. /**
  2095. * Update a single item: merge with existing item.
  2096. * Will fail when the item has no id, or when there does not exist an item
  2097. * with the same id.
  2098. * @param {Object} item
  2099. * @return {String} id
  2100. * @private
  2101. */
  2102. DataSet.prototype._updateItem = function (item) {
  2103. var id = item[this.fieldId];
  2104. if (id == undefined) {
  2105. throw new Error('Cannot update item: item has no id (item: ' + JSON.stringify(item) + ')');
  2106. }
  2107. var d = this.data[id];
  2108. if (!d) {
  2109. // item doesn't exist
  2110. throw new Error('Cannot update item: no item with id ' + id + ' found');
  2111. }
  2112. // merge with current item
  2113. for (var field in item) {
  2114. if (item.hasOwnProperty(field)) {
  2115. var type = this.fieldTypes[field]; // type may be undefined
  2116. d[field] = util.cast(item[field], type);
  2117. }
  2118. }
  2119. return id;
  2120. };
  2121. /**
  2122. * Get an array with the column names of a Google DataTable
  2123. * @param {DataTable} dataTable
  2124. * @return {String[]} columnNames
  2125. * @private
  2126. */
  2127. DataSet.prototype._getColumnNames = function (dataTable) {
  2128. var columns = [];
  2129. for (var col = 0, cols = dataTable.getNumberOfColumns(); col < cols; col++) {
  2130. columns[col] = dataTable.getColumnId(col) || dataTable.getColumnLabel(col);
  2131. }
  2132. return columns;
  2133. };
  2134. /**
  2135. * Append an item as a row to the dataTable
  2136. * @param dataTable
  2137. * @param columns
  2138. * @param item
  2139. * @private
  2140. */
  2141. DataSet.prototype._appendRow = function (dataTable, columns, item) {
  2142. var row = dataTable.addRow();
  2143. for (var col = 0, cols = columns.length; col < cols; col++) {
  2144. var field = columns[col];
  2145. dataTable.setValue(row, col, item[field]);
  2146. }
  2147. };
  2148. /**
  2149. * DataView
  2150. *
  2151. * a dataview offers a filtered view on a dataset or an other dataview.
  2152. *
  2153. * @param {DataSet | DataView} data
  2154. * @param {Object} [options] Available options: see method get
  2155. *
  2156. * @constructor DataView
  2157. */
  2158. function DataView (data, options) {
  2159. this.id = util.randomUUID();
  2160. this.data = null;
  2161. this.ids = {}; // ids of the items currently in memory (just contains a boolean true)
  2162. this.options = options || {};
  2163. this.fieldId = 'id'; // name of the field containing id
  2164. this.subscribers = {}; // event subscribers
  2165. var me = this;
  2166. this.listener = function () {
  2167. me._onEvent.apply(me, arguments);
  2168. };
  2169. this.setData(data);
  2170. }
  2171. /**
  2172. * Set a data source for the view
  2173. * @param {DataSet | DataView} data
  2174. */
  2175. DataView.prototype.setData = function (data) {
  2176. var ids, dataItems, i, len;
  2177. if (this.data) {
  2178. // unsubscribe from current dataset
  2179. if (this.data.unsubscribe) {
  2180. this.data.unsubscribe('*', this.listener);
  2181. }
  2182. // trigger a remove of all items in memory
  2183. ids = [];
  2184. for (var id in this.ids) {
  2185. if (this.ids.hasOwnProperty(id)) {
  2186. ids.push(id);
  2187. }
  2188. }
  2189. this.ids = {};
  2190. this._trigger('remove', {items: ids});
  2191. }
  2192. this.data = data;
  2193. if (this.data) {
  2194. // update fieldId
  2195. this.fieldId = this.options.fieldId ||
  2196. (this.data && this.data.options && this.data.options.fieldId) ||
  2197. 'id';
  2198. // trigger an add of all added items
  2199. ids = this.data.getIds({filter: this.options && this.options.filter});
  2200. for (i = 0, len = ids.length; i < len; i++) {
  2201. id = ids[i];
  2202. this.ids[id] = true;
  2203. }
  2204. this._trigger('add', {items: ids});
  2205. // subscribe to new dataset
  2206. if (this.data.subscribe) {
  2207. this.data.subscribe('*', this.listener);
  2208. }
  2209. }
  2210. };
  2211. /**
  2212. * Get data from the data view
  2213. *
  2214. * Usage:
  2215. *
  2216. * get()
  2217. * get(options: Object)
  2218. * get(options: Object, data: Array | DataTable)
  2219. *
  2220. * get(id: Number)
  2221. * get(id: Number, options: Object)
  2222. * get(id: Number, options: Object, data: Array | DataTable)
  2223. *
  2224. * get(ids: Number[])
  2225. * get(ids: Number[], options: Object)
  2226. * get(ids: Number[], options: Object, data: Array | DataTable)
  2227. *
  2228. * Where:
  2229. *
  2230. * {Number | String} id The id of an item
  2231. * {Number[] | String{}} ids An array with ids of items
  2232. * {Object} options An Object with options. Available options:
  2233. * {String} [type] Type of data to be returned. Can
  2234. * be 'DataTable' or 'Array' (default)
  2235. * {Object.<String, String>} [fieldTypes]
  2236. * {String[]} [fields] field names to be returned
  2237. * {function} [filter] filter items
  2238. * {String | function} [order] Order the items by
  2239. * a field name or custom sort function.
  2240. * {Array | DataTable} [data] If provided, items will be appended to this
  2241. * array or table. Required in case of Google
  2242. * DataTable.
  2243. * @param args
  2244. */
  2245. DataView.prototype.get = function (args) {
  2246. var me = this;
  2247. // parse the arguments
  2248. var ids, options, data;
  2249. var firstType = util.getType(arguments[0]);
  2250. if (firstType == 'String' || firstType == 'Number' || firstType == 'Array') {
  2251. // get(id(s) [, options] [, data])
  2252. ids = arguments[0]; // can be a single id or an array with ids
  2253. options = arguments[1];
  2254. data = arguments[2];
  2255. }
  2256. else {
  2257. // get([, options] [, data])
  2258. options = arguments[0];
  2259. data = arguments[1];
  2260. }
  2261. // extend the options with the default options and provided options
  2262. var viewOptions = util.extend({}, this.options, options);
  2263. // create a combined filter method when needed
  2264. if (this.options.filter && options && options.filter) {
  2265. viewOptions.filter = function (item) {
  2266. return me.options.filter(item) && options.filter(item);
  2267. }
  2268. }
  2269. // build up the call to the linked data set
  2270. var getArguments = [];
  2271. if (ids != undefined) {
  2272. getArguments.push(ids);
  2273. }
  2274. getArguments.push(viewOptions);
  2275. getArguments.push(data);
  2276. return this.data && this.data.get.apply(this.data, getArguments);
  2277. };
  2278. /**
  2279. * Get ids of all items or from a filtered set of items.
  2280. * @param {Object} [options] An Object with options. Available options:
  2281. * {function} [filter] filter items
  2282. * {String | function} [order] Order the items by
  2283. * a field name or custom sort function.
  2284. * @return {Array} ids
  2285. */
  2286. DataView.prototype.getIds = function (options) {
  2287. var ids;
  2288. if (this.data) {
  2289. var defaultFilter = this.options.filter;
  2290. var filter;
  2291. if (options && options.filter) {
  2292. if (defaultFilter) {
  2293. filter = function (item) {
  2294. return defaultFilter(item) && options.filter(item);
  2295. }
  2296. }
  2297. else {
  2298. filter = options.filter;
  2299. }
  2300. }
  2301. else {
  2302. filter = defaultFilter;
  2303. }
  2304. ids = this.data.getIds({
  2305. filter: filter,
  2306. order: options && options.order
  2307. });
  2308. }
  2309. else {
  2310. ids = [];
  2311. }
  2312. return ids;
  2313. };
  2314. /**
  2315. * Event listener. Will propagate all events from the connected data set to
  2316. * the subscribers of the DataView, but will filter the items and only trigger
  2317. * when there are changes in the filtered data set.
  2318. * @param {String} event
  2319. * @param {Object | null} params
  2320. * @param {String} senderId
  2321. * @private
  2322. */
  2323. DataView.prototype._onEvent = function (event, params, senderId) {
  2324. var i, len, id, item,
  2325. ids = params && params.items,
  2326. data = this.data,
  2327. added = [],
  2328. updated = [],
  2329. removed = [];
  2330. if (ids && data) {
  2331. switch (event) {
  2332. case 'add':
  2333. // filter the ids of the added items
  2334. for (i = 0, len = ids.length; i < len; i++) {
  2335. id = ids[i];
  2336. item = this.get(id);
  2337. if (item) {
  2338. this.ids[id] = true;
  2339. added.push(id);
  2340. }
  2341. }
  2342. break;
  2343. case 'update':
  2344. // determine the event from the views viewpoint: an updated
  2345. // item can be added, updated, or removed from this view.
  2346. for (i = 0, len = ids.length; i < len; i++) {
  2347. id = ids[i];
  2348. item = this.get(id);
  2349. if (item) {
  2350. if (this.ids[id]) {
  2351. updated.push(id);
  2352. }
  2353. else {
  2354. this.ids[id] = true;
  2355. added.push(id);
  2356. }
  2357. }
  2358. else {
  2359. if (this.ids[id]) {
  2360. delete this.ids[id];
  2361. removed.push(id);
  2362. }
  2363. else {
  2364. // nothing interesting for me :-(
  2365. }
  2366. }
  2367. }
  2368. break;
  2369. case 'remove':
  2370. // filter the ids of the removed items
  2371. for (i = 0, len = ids.length; i < len; i++) {
  2372. id = ids[i];
  2373. if (this.ids[id]) {
  2374. delete this.ids[id];
  2375. removed.push(id);
  2376. }
  2377. }
  2378. break;
  2379. }
  2380. if (added.length) {
  2381. this._trigger('add', {items: added}, senderId);
  2382. }
  2383. if (updated.length) {
  2384. this._trigger('update', {items: updated}, senderId);
  2385. }
  2386. if (removed.length) {
  2387. this._trigger('remove', {items: removed}, senderId);
  2388. }
  2389. }
  2390. };
  2391. // copy subscription functionality from DataSet
  2392. DataView.prototype.subscribe = DataSet.prototype.subscribe;
  2393. DataView.prototype.unsubscribe = DataSet.prototype.unsubscribe;
  2394. DataView.prototype._trigger = DataSet.prototype._trigger;
  2395. /**
  2396. * @constructor Stack
  2397. * Stacks items on top of each other.
  2398. * @param {ItemSet} parent
  2399. * @param {Object} [options]
  2400. */
  2401. function Stack (parent, options) {
  2402. this.parent = parent;
  2403. this.options = options || {};
  2404. this.defaultOptions = {
  2405. order: function (a, b) {
  2406. //return (b.width - a.width) || (a.left - b.left); // TODO: cleanup
  2407. // Order: ranges over non-ranges, ranged ordered by width, and
  2408. // lastly ordered by start.
  2409. if (a instanceof ItemRange) {
  2410. if (b instanceof ItemRange) {
  2411. var aInt = (a.data.end - a.data.start);
  2412. var bInt = (b.data.end - b.data.start);
  2413. return (aInt - bInt) || (a.data.start - b.data.start);
  2414. }
  2415. else {
  2416. return -1;
  2417. }
  2418. }
  2419. else {
  2420. if (b instanceof ItemRange) {
  2421. return 1;
  2422. }
  2423. else {
  2424. return (a.data.start - b.data.start);
  2425. }
  2426. }
  2427. },
  2428. margin: {
  2429. item: 10
  2430. }
  2431. };
  2432. this.ordered = []; // ordered items
  2433. }
  2434. /**
  2435. * Set options for the stack
  2436. * @param {Object} options Available options:
  2437. * {ItemSet} parent
  2438. * {Number} margin
  2439. * {function} order Stacking order
  2440. */
  2441. Stack.prototype.setOptions = function setOptions (options) {
  2442. util.extend(this.options, options);
  2443. // TODO: register on data changes at the connected parent itemset, and update the changed part only and immediately
  2444. };
  2445. /**
  2446. * Stack the items such that they don't overlap. The items will have a minimal
  2447. * distance equal to options.margin.item.
  2448. */
  2449. Stack.prototype.update = function update() {
  2450. this._order();
  2451. this._stack();
  2452. };
  2453. /**
  2454. * Order the items. The items are ordered by width first, and by left position
  2455. * second.
  2456. * If a custom order function has been provided via the options, then this will
  2457. * be used.
  2458. * @private
  2459. */
  2460. Stack.prototype._order = function _order () {
  2461. var items = this.parent.items;
  2462. if (!items) {
  2463. throw new Error('Cannot stack items: parent does not contain items');
  2464. }
  2465. // TODO: store the sorted items, to have less work later on
  2466. var ordered = [];
  2467. var index = 0;
  2468. // items is a map (no array)
  2469. util.forEach(items, function (item) {
  2470. if (item.visible) {
  2471. ordered[index] = item;
  2472. index++;
  2473. }
  2474. });
  2475. //if a customer stack order function exists, use it.
  2476. var order = this.options.order || this.defaultOptions.order;
  2477. if (!(typeof order === 'function')) {
  2478. throw new Error('Option order must be a function');
  2479. }
  2480. ordered.sort(order);
  2481. this.ordered = ordered;
  2482. };
  2483. /**
  2484. * Adjust vertical positions of the events such that they don't overlap each
  2485. * other.
  2486. * @private
  2487. */
  2488. Stack.prototype._stack = function _stack () {
  2489. var i,
  2490. iMax,
  2491. ordered = this.ordered,
  2492. options = this.options,
  2493. orientation = options.orientation || this.defaultOptions.orientation,
  2494. axisOnTop = (orientation == 'top'),
  2495. margin;
  2496. if (options.margin && options.margin.item !== undefined) {
  2497. margin = options.margin.item;
  2498. }
  2499. else {
  2500. margin = this.defaultOptions.margin.item
  2501. }
  2502. // calculate new, non-overlapping positions
  2503. for (i = 0, iMax = ordered.length; i < iMax; i++) {
  2504. var item = ordered[i];
  2505. var collidingItem = null;
  2506. do {
  2507. // TODO: optimize checking for overlap. when there is a gap without items,
  2508. // you only need to check for items from the next item on, not from zero
  2509. collidingItem = this.checkOverlap(ordered, i, 0, i - 1, margin);
  2510. if (collidingItem != null) {
  2511. // There is a collision. Reposition the event above the colliding element
  2512. if (axisOnTop) {
  2513. item.top = collidingItem.top + collidingItem.height + margin;
  2514. }
  2515. else {
  2516. item.top = collidingItem.top - item.height - margin;
  2517. }
  2518. }
  2519. } while (collidingItem);
  2520. }
  2521. };
  2522. /**
  2523. * Check if the destiny position of given item overlaps with any
  2524. * of the other items from index itemStart to itemEnd.
  2525. * @param {Array} items Array with items
  2526. * @param {int} itemIndex Number of the item to be checked for overlap
  2527. * @param {int} itemStart First item to be checked.
  2528. * @param {int} itemEnd Last item to be checked.
  2529. * @return {Object | null} colliding item, or undefined when no collisions
  2530. * @param {Number} margin A minimum required margin.
  2531. * If margin is provided, the two items will be
  2532. * marked colliding when they overlap or
  2533. * when the margin between the two is smaller than
  2534. * the requested margin.
  2535. */
  2536. Stack.prototype.checkOverlap = function checkOverlap (items, itemIndex,
  2537. itemStart, itemEnd, margin) {
  2538. var collision = this.collision;
  2539. // we loop from end to start, as we suppose that the chance of a
  2540. // collision is larger for items at the end, so check these first.
  2541. var a = items[itemIndex];
  2542. for (var i = itemEnd; i >= itemStart; i--) {
  2543. var b = items[i];
  2544. if (collision(a, b, margin)) {
  2545. if (i != itemIndex) {
  2546. return b;
  2547. }
  2548. }
  2549. }
  2550. return null;
  2551. };
  2552. /**
  2553. * Test if the two provided items collide
  2554. * The items must have parameters left, width, top, and height.
  2555. * @param {Component} a The first item
  2556. * @param {Component} b The second item
  2557. * @param {Number} margin A minimum required margin.
  2558. * If margin is provided, the two items will be
  2559. * marked colliding when they overlap or
  2560. * when the margin between the two is smaller than
  2561. * the requested margin.
  2562. * @return {boolean} true if a and b collide, else false
  2563. */
  2564. Stack.prototype.collision = function collision (a, b, margin) {
  2565. return ((a.left - margin) < (b.left + b.width) &&
  2566. (a.left + a.width + margin) > b.left &&
  2567. (a.top - margin) < (b.top + b.height) &&
  2568. (a.top + a.height + margin) > b.top);
  2569. };
  2570. /**
  2571. * @constructor Range
  2572. * A Range controls a numeric range with a start and end value.
  2573. * The Range adjusts the range based on mouse events or programmatic changes,
  2574. * and triggers events when the range is changing or has been changed.
  2575. * @param {Object} [options] See description at Range.setOptions
  2576. * @extends Controller
  2577. */
  2578. function Range(options) {
  2579. this.id = util.randomUUID();
  2580. this.start = 0; // Number
  2581. this.end = 0; // Number
  2582. this.options = {
  2583. min: null,
  2584. max: null,
  2585. zoomMin: null,
  2586. zoomMax: null
  2587. };
  2588. this.listeners = [];
  2589. this.setOptions(options);
  2590. }
  2591. /**
  2592. * Set options for the range controller
  2593. * @param {Object} options Available options:
  2594. * {Number} start Set start value of the range
  2595. * {Number} end Set end value of the range
  2596. * {Number} min Minimum value for start
  2597. * {Number} max Maximum value for end
  2598. * {Number} zoomMin Set a minimum value for
  2599. * (end - start).
  2600. * {Number} zoomMax Set a maximum value for
  2601. * (end - start).
  2602. */
  2603. Range.prototype.setOptions = function (options) {
  2604. util.extend(this.options, options);
  2605. if (options.start != null || options.end != null) {
  2606. this.setRange(options.start, options.end);
  2607. }
  2608. };
  2609. /**
  2610. * Add listeners for mouse and touch events to the component
  2611. * @param {Component} component
  2612. * @param {String} event Available events: 'move', 'zoom'
  2613. * @param {String} direction Available directions: 'horizontal', 'vertical'
  2614. */
  2615. Range.prototype.subscribe = function (component, event, direction) {
  2616. var me = this;
  2617. var listener;
  2618. if (direction != 'horizontal' && direction != 'vertical') {
  2619. throw new TypeError('Unknown direction "' + direction + '". ' +
  2620. 'Choose "horizontal" or "vertical".');
  2621. }
  2622. //noinspection FallthroughInSwitchStatementJS
  2623. if (event == 'move') {
  2624. listener = {
  2625. component: component,
  2626. event: event,
  2627. direction: direction,
  2628. callback: function (event) {
  2629. me._onMouseDown(event, listener);
  2630. },
  2631. params: {}
  2632. };
  2633. component.on('mousedown', listener.callback);
  2634. me.listeners.push(listener);
  2635. }
  2636. else if (event == 'zoom') {
  2637. listener = {
  2638. component: component,
  2639. event: event,
  2640. direction: direction,
  2641. callback: function (event) {
  2642. me._onMouseWheel(event, listener);
  2643. },
  2644. params: {}
  2645. };
  2646. component.on('mousewheel', listener.callback);
  2647. me.listeners.push(listener);
  2648. }
  2649. else {
  2650. throw new TypeError('Unknown event "' + event + '". ' +
  2651. 'Choose "move" or "zoom".');
  2652. }
  2653. };
  2654. /**
  2655. * Event handler
  2656. * @param {String} event name of the event, for example 'click', 'mousemove'
  2657. * @param {function} callback callback handler, invoked with the raw HTML Event
  2658. * as parameter.
  2659. */
  2660. Range.prototype.on = function (event, callback) {
  2661. events.addListener(this, event, callback);
  2662. };
  2663. /**
  2664. * Trigger an event
  2665. * @param {String} event name of the event, available events: 'rangechange',
  2666. * 'rangechanged'
  2667. * @private
  2668. */
  2669. Range.prototype._trigger = function (event) {
  2670. events.trigger(this, event, {
  2671. start: this.start,
  2672. end: this.end
  2673. });
  2674. };
  2675. /**
  2676. * Set a new start and end range
  2677. * @param {Number} start
  2678. * @param {Number} end
  2679. */
  2680. Range.prototype.setRange = function(start, end) {
  2681. var changed = this._applyRange(start, end);
  2682. if (changed) {
  2683. this._trigger('rangechange');
  2684. this._trigger('rangechanged');
  2685. }
  2686. };
  2687. /**
  2688. * Set a new start and end range. This method is the same as setRange, but
  2689. * does not trigger a range change and range changed event, and it returns
  2690. * true when the range is changed
  2691. * @param {Number} start
  2692. * @param {Number} end
  2693. * @return {Boolean} changed
  2694. * @private
  2695. */
  2696. Range.prototype._applyRange = function(start, end) {
  2697. var newStart = (start != null) ? util.cast(start, 'Number') : this.start;
  2698. var newEnd = (end != null) ? util.cast(end, 'Number') : this.end;
  2699. var diff;
  2700. // check for valid number
  2701. if (isNaN(newStart)) {
  2702. throw new Error('Invalid start "' + start + '"');
  2703. }
  2704. if (isNaN(newEnd)) {
  2705. throw new Error('Invalid end "' + end + '"');
  2706. }
  2707. // prevent start < end
  2708. if (newEnd < newStart) {
  2709. newEnd = newStart;
  2710. }
  2711. // prevent start < min
  2712. if (this.options.min != null) {
  2713. var min = this.options.min.valueOf();
  2714. if (newStart < min) {
  2715. diff = (min - newStart);
  2716. newStart += diff;
  2717. newEnd += diff;
  2718. }
  2719. }
  2720. // prevent end > max
  2721. if (this.options.max != null) {
  2722. var max = this.options.max.valueOf();
  2723. if (newEnd > max) {
  2724. diff = (newEnd - max);
  2725. newStart -= diff;
  2726. newEnd -= diff;
  2727. }
  2728. }
  2729. // prevent (end-start) > zoomMin
  2730. if (this.options.zoomMin != null) {
  2731. var zoomMin = this.options.zoomMin.valueOf();
  2732. if (zoomMin < 0) {
  2733. zoomMin = 0;
  2734. }
  2735. if ((newEnd - newStart) < zoomMin) {
  2736. if ((this.end - this.start) > zoomMin) {
  2737. // zoom to the minimum
  2738. diff = (zoomMin - (newEnd - newStart));
  2739. newStart -= diff / 2;
  2740. newEnd += diff / 2;
  2741. }
  2742. else {
  2743. // ingore this action, we are already zoomed to the minimum
  2744. newStart = this.start;
  2745. newEnd = this.end;
  2746. }
  2747. }
  2748. }
  2749. // prevent (end-start) > zoomMin
  2750. if (this.options.zoomMax != null) {
  2751. var zoomMax = this.options.zoomMax.valueOf();
  2752. if (zoomMax < 0) {
  2753. zoomMax = 0;
  2754. }
  2755. if ((newEnd - newStart) > zoomMax) {
  2756. if ((this.end - this.start) < zoomMax) {
  2757. // zoom to the maximum
  2758. diff = ((newEnd - newStart) - zoomMax);
  2759. newStart += diff / 2;
  2760. newEnd -= diff / 2;
  2761. }
  2762. else {
  2763. // ingore this action, we are already zoomed to the maximum
  2764. newStart = this.start;
  2765. newEnd = this.end;
  2766. }
  2767. }
  2768. }
  2769. var changed = (this.start != newStart || this.end != newEnd);
  2770. this.start = newStart;
  2771. this.end = newEnd;
  2772. return changed;
  2773. };
  2774. /**
  2775. * Retrieve the current range.
  2776. * @return {Object} An object with start and end properties
  2777. */
  2778. Range.prototype.getRange = function() {
  2779. return {
  2780. start: this.start,
  2781. end: this.end
  2782. };
  2783. };
  2784. /**
  2785. * Calculate the conversion offset and factor for current range, based on
  2786. * the provided width
  2787. * @param {Number} width
  2788. * @returns {{offset: number, factor: number}} conversion
  2789. */
  2790. Range.prototype.conversion = function (width) {
  2791. var start = this.start;
  2792. var end = this.end;
  2793. return Range.conversion(this.start, this.end, width);
  2794. };
  2795. /**
  2796. * Static method to calculate the conversion offset and factor for a range,
  2797. * based on the provided start, end, and width
  2798. * @param {Number} start
  2799. * @param {Number} end
  2800. * @param {Number} width
  2801. * @returns {{offset: number, factor: number}} conversion
  2802. */
  2803. Range.conversion = function (start, end, width) {
  2804. if (width != 0 && (end - start != 0)) {
  2805. return {
  2806. offset: start,
  2807. factor: width / (end - start)
  2808. }
  2809. }
  2810. else {
  2811. return {
  2812. offset: 0,
  2813. factor: 1
  2814. };
  2815. }
  2816. };
  2817. /**
  2818. * Start moving horizontally or vertically
  2819. * @param {Event} event
  2820. * @param {Object} listener Listener containing the component and params
  2821. * @private
  2822. */
  2823. Range.prototype._onMouseDown = function(event, listener) {
  2824. event = event || window.event;
  2825. var params = listener.params;
  2826. // only react on left mouse button down
  2827. var leftButtonDown = event.which ? (event.which == 1) : (event.button == 1);
  2828. if (!leftButtonDown) {
  2829. return;
  2830. }
  2831. // get mouse position
  2832. params.mouseX = util.getPageX(event);
  2833. params.mouseY = util.getPageY(event);
  2834. params.previousLeft = 0;
  2835. params.previousOffset = 0;
  2836. params.moved = false;
  2837. params.start = this.start;
  2838. params.end = this.end;
  2839. var frame = listener.component.frame;
  2840. if (frame) {
  2841. frame.style.cursor = 'move';
  2842. }
  2843. // add event listeners to handle moving the contents
  2844. // we store the function onmousemove and onmouseup in the timeaxis,
  2845. // so we can remove the eventlisteners lateron in the function onmouseup
  2846. var me = this;
  2847. if (!params.onMouseMove) {
  2848. params.onMouseMove = function (event) {
  2849. me._onMouseMove(event, listener);
  2850. };
  2851. util.addEventListener(document, "mousemove", params.onMouseMove);
  2852. }
  2853. if (!params.onMouseUp) {
  2854. params.onMouseUp = function (event) {
  2855. me._onMouseUp(event, listener);
  2856. };
  2857. util.addEventListener(document, "mouseup", params.onMouseUp);
  2858. }
  2859. util.preventDefault(event);
  2860. };
  2861. /**
  2862. * Perform moving operating.
  2863. * This function activated from within the funcion TimeAxis._onMouseDown().
  2864. * @param {Event} event
  2865. * @param {Object} listener
  2866. * @private
  2867. */
  2868. Range.prototype._onMouseMove = function (event, listener) {
  2869. event = event || window.event;
  2870. var params = listener.params;
  2871. // calculate change in mouse position
  2872. var mouseX = util.getPageX(event);
  2873. var mouseY = util.getPageY(event);
  2874. if (params.mouseX == undefined) {
  2875. params.mouseX = mouseX;
  2876. }
  2877. if (params.mouseY == undefined) {
  2878. params.mouseY = mouseY;
  2879. }
  2880. var diffX = mouseX - params.mouseX;
  2881. var diffY = mouseY - params.mouseY;
  2882. var diff = (listener.direction == 'horizontal') ? diffX : diffY;
  2883. // if mouse movement is big enough, register it as a "moved" event
  2884. if (Math.abs(diff) >= 1) {
  2885. params.moved = true;
  2886. }
  2887. var interval = (params.end - params.start);
  2888. var width = (listener.direction == 'horizontal') ?
  2889. listener.component.width : listener.component.height;
  2890. var diffRange = -diff / width * interval;
  2891. this._applyRange(params.start + diffRange, params.end + diffRange);
  2892. // fire a rangechange event
  2893. this._trigger('rangechange');
  2894. util.preventDefault(event);
  2895. };
  2896. /**
  2897. * Stop moving operating.
  2898. * This function activated from within the function Range._onMouseDown().
  2899. * @param {event} event
  2900. * @param {Object} listener
  2901. * @private
  2902. */
  2903. Range.prototype._onMouseUp = function (event, listener) {
  2904. event = event || window.event;
  2905. var params = listener.params;
  2906. if (listener.component.frame) {
  2907. listener.component.frame.style.cursor = 'auto';
  2908. }
  2909. // remove event listeners here, important for Safari
  2910. if (params.onMouseMove) {
  2911. util.removeEventListener(document, "mousemove", params.onMouseMove);
  2912. params.onMouseMove = null;
  2913. }
  2914. if (params.onMouseUp) {
  2915. util.removeEventListener(document, "mouseup", params.onMouseUp);
  2916. params.onMouseUp = null;
  2917. }
  2918. //util.preventDefault(event);
  2919. if (params.moved) {
  2920. // fire a rangechanged event
  2921. this._trigger('rangechanged');
  2922. }
  2923. };
  2924. /**
  2925. * Event handler for mouse wheel event, used to zoom
  2926. * Code from http://adomas.org/javascript-mouse-wheel/
  2927. * @param {Event} event
  2928. * @param {Object} listener
  2929. * @private
  2930. */
  2931. Range.prototype._onMouseWheel = function(event, listener) {
  2932. event = event || window.event;
  2933. // retrieve delta
  2934. var delta = 0;
  2935. if (event.wheelDelta) { /* IE/Opera. */
  2936. delta = event.wheelDelta / 120;
  2937. } else if (event.detail) { /* Mozilla case. */
  2938. // In Mozilla, sign of delta is different than in IE.
  2939. // Also, delta is multiple of 3.
  2940. delta = -event.detail / 3;
  2941. }
  2942. // If delta is nonzero, handle it.
  2943. // Basically, delta is now positive if wheel was scrolled up,
  2944. // and negative, if wheel was scrolled down.
  2945. if (delta) {
  2946. var me = this;
  2947. var zoom = function () {
  2948. // perform the zoom action. Delta is normally 1 or -1
  2949. var zoomFactor = delta / 5.0;
  2950. var zoomAround = null;
  2951. var frame = listener.component.frame;
  2952. if (frame) {
  2953. var size, conversion;
  2954. if (listener.direction == 'horizontal') {
  2955. size = listener.component.width;
  2956. conversion = me.conversion(size);
  2957. var frameLeft = util.getAbsoluteLeft(frame);
  2958. var mouseX = util.getPageX(event);
  2959. zoomAround = (mouseX - frameLeft) / conversion.factor + conversion.offset;
  2960. }
  2961. else {
  2962. size = listener.component.height;
  2963. conversion = me.conversion(size);
  2964. var frameTop = util.getAbsoluteTop(frame);
  2965. var mouseY = util.getPageY(event);
  2966. zoomAround = ((frameTop + size - mouseY) - frameTop) / conversion.factor + conversion.offset;
  2967. }
  2968. }
  2969. me.zoom(zoomFactor, zoomAround);
  2970. };
  2971. zoom();
  2972. }
  2973. // Prevent default actions caused by mouse wheel.
  2974. // That might be ugly, but we handle scrolls somehow
  2975. // anyway, so don't bother here...
  2976. util.preventDefault(event);
  2977. };
  2978. /**
  2979. * Zoom the range the given zoomfactor in or out. Start and end date will
  2980. * be adjusted, and the timeline will be redrawn. You can optionally give a
  2981. * date around which to zoom.
  2982. * For example, try zoomfactor = 0.1 or -0.1
  2983. * @param {Number} zoomFactor Zooming amount. Positive value will zoom in,
  2984. * negative value will zoom out
  2985. * @param {Number} zoomAround Value around which will be zoomed. Optional
  2986. */
  2987. Range.prototype.zoom = function(zoomFactor, zoomAround) {
  2988. // if zoomAroundDate is not provided, take it half between start Date and end Date
  2989. if (zoomAround == null) {
  2990. zoomAround = (this.start + this.end) / 2;
  2991. }
  2992. // prevent zoom factor larger than 1 or smaller than -1 (larger than 1 will
  2993. // result in a start>=end )
  2994. if (zoomFactor >= 1) {
  2995. zoomFactor = 0.9;
  2996. }
  2997. if (zoomFactor <= -1) {
  2998. zoomFactor = -0.9;
  2999. }
  3000. // adjust a negative factor such that zooming in with 0.1 equals zooming
  3001. // out with a factor -0.1
  3002. if (zoomFactor < 0) {
  3003. zoomFactor = zoomFactor / (1 + zoomFactor);
  3004. }
  3005. // zoom start and end relative to the zoomAround value
  3006. var startDiff = (this.start - zoomAround);
  3007. var endDiff = (this.end - zoomAround);
  3008. // calculate new start and end
  3009. var newStart = this.start - startDiff * zoomFactor;
  3010. var newEnd = this.end - endDiff * zoomFactor;
  3011. this.setRange(newStart, newEnd);
  3012. };
  3013. /**
  3014. * Move the range with a given factor to the left or right. Start and end
  3015. * value will be adjusted. For example, try moveFactor = 0.1 or -0.1
  3016. * @param {Number} moveFactor Moving amount. Positive value will move right,
  3017. * negative value will move left
  3018. */
  3019. Range.prototype.move = function(moveFactor) {
  3020. // zoom start Date and end Date relative to the zoomAroundDate
  3021. var diff = (this.end - this.start);
  3022. // apply new values
  3023. var newStart = this.start + diff * moveFactor;
  3024. var newEnd = this.end + diff * moveFactor;
  3025. // TODO: reckon with min and max range
  3026. this.start = newStart;
  3027. this.end = newEnd;
  3028. };
  3029. /**
  3030. * An event bus can be used to emit events, and to subscribe to events
  3031. * @constructor EventBus
  3032. */
  3033. function EventBus() {
  3034. this.subscriptions = [];
  3035. }
  3036. /**
  3037. * Subscribe to an event
  3038. * @param {String | RegExp} event The event can be a regular expression, or
  3039. * a string with wildcards, like 'server.*'.
  3040. * @param {function} callback. Callback are called with three parameters:
  3041. * {String} event, {*} [data], {*} [source]
  3042. * @param {*} [target]
  3043. * @returns {String} id A subscription id
  3044. */
  3045. EventBus.prototype.on = function (event, callback, target) {
  3046. var regexp = (event instanceof RegExp) ?
  3047. event :
  3048. new RegExp(event.replace('*', '\\w+'));
  3049. var subscription = {
  3050. id: util.randomUUID(),
  3051. event: event,
  3052. regexp: regexp,
  3053. callback: (typeof callback === 'function') ? callback : null,
  3054. target: target
  3055. };
  3056. this.subscriptions.push(subscription);
  3057. return subscription.id;
  3058. };
  3059. /**
  3060. * Unsubscribe from an event
  3061. * @param {String | Object} filter Filter for subscriptions to be removed
  3062. * Filter can be a string containing a
  3063. * subscription id, or an object containing
  3064. * one or more of the fields id, event,
  3065. * callback, and target.
  3066. */
  3067. EventBus.prototype.off = function (filter) {
  3068. var i = 0;
  3069. while (i < this.subscriptions.length) {
  3070. var subscription = this.subscriptions[i];
  3071. var match = true;
  3072. if (filter instanceof Object) {
  3073. // filter is an object. All fields must match
  3074. for (var prop in filter) {
  3075. if (filter.hasOwnProperty(prop)) {
  3076. if (filter[prop] !== subscription[prop]) {
  3077. match = false;
  3078. }
  3079. }
  3080. }
  3081. }
  3082. else {
  3083. // filter is a string, filter on id
  3084. match = (subscription.id == filter);
  3085. }
  3086. if (match) {
  3087. this.subscriptions.splice(i, 1);
  3088. }
  3089. else {
  3090. i++;
  3091. }
  3092. }
  3093. };
  3094. /**
  3095. * Emit an event
  3096. * @param {String} event
  3097. * @param {*} [data]
  3098. * @param {*} [source]
  3099. */
  3100. EventBus.prototype.emit = function (event, data, source) {
  3101. for (var i =0; i < this.subscriptions.length; i++) {
  3102. var subscription = this.subscriptions[i];
  3103. if (subscription.regexp.test(event)) {
  3104. if (subscription.callback) {
  3105. subscription.callback(event, data, source);
  3106. }
  3107. }
  3108. }
  3109. };
  3110. /**
  3111. * @constructor Controller
  3112. *
  3113. * A Controller controls the reflows and repaints of all visual components
  3114. */
  3115. function Controller () {
  3116. this.id = util.randomUUID();
  3117. this.components = {};
  3118. this.repaintTimer = undefined;
  3119. this.reflowTimer = undefined;
  3120. }
  3121. /**
  3122. * Add a component to the controller
  3123. * @param {Component} component
  3124. */
  3125. Controller.prototype.add = function add(component) {
  3126. // validate the component
  3127. if (component.id == undefined) {
  3128. throw new Error('Component has no field id');
  3129. }
  3130. if (!(component instanceof Component) && !(component instanceof Controller)) {
  3131. throw new TypeError('Component must be an instance of ' +
  3132. 'prototype Component or Controller');
  3133. }
  3134. // add the component
  3135. component.controller = this;
  3136. this.components[component.id] = component;
  3137. };
  3138. /**
  3139. * Remove a component from the controller
  3140. * @param {Component | String} component
  3141. */
  3142. Controller.prototype.remove = function remove(component) {
  3143. var id;
  3144. for (id in this.components) {
  3145. if (this.components.hasOwnProperty(id)) {
  3146. if (id == component || this.components[id] == component) {
  3147. break;
  3148. }
  3149. }
  3150. }
  3151. if (id) {
  3152. delete this.components[id];
  3153. }
  3154. };
  3155. /**
  3156. * Request a reflow. The controller will schedule a reflow
  3157. * @param {Boolean} [force] If true, an immediate reflow is forced. Default
  3158. * is false.
  3159. */
  3160. Controller.prototype.requestReflow = function requestReflow(force) {
  3161. if (force) {
  3162. this.reflow();
  3163. }
  3164. else {
  3165. if (!this.reflowTimer) {
  3166. var me = this;
  3167. this.reflowTimer = setTimeout(function () {
  3168. me.reflowTimer = undefined;
  3169. me.reflow();
  3170. }, 0);
  3171. }
  3172. }
  3173. };
  3174. /**
  3175. * Request a repaint. The controller will schedule a repaint
  3176. * @param {Boolean} [force] If true, an immediate repaint is forced. Default
  3177. * is false.
  3178. */
  3179. Controller.prototype.requestRepaint = function requestRepaint(force) {
  3180. if (force) {
  3181. this.repaint();
  3182. }
  3183. else {
  3184. if (!this.repaintTimer) {
  3185. var me = this;
  3186. this.repaintTimer = setTimeout(function () {
  3187. me.repaintTimer = undefined;
  3188. me.repaint();
  3189. }, 0);
  3190. }
  3191. }
  3192. };
  3193. /**
  3194. * Repaint all components
  3195. */
  3196. Controller.prototype.repaint = function repaint() {
  3197. var changed = false;
  3198. // cancel any running repaint request
  3199. if (this.repaintTimer) {
  3200. clearTimeout(this.repaintTimer);
  3201. this.repaintTimer = undefined;
  3202. }
  3203. var done = {};
  3204. function repaint(component, id) {
  3205. if (!(id in done)) {
  3206. // first repaint the components on which this component is dependent
  3207. if (component.depends) {
  3208. component.depends.forEach(function (dep) {
  3209. repaint(dep, dep.id);
  3210. });
  3211. }
  3212. if (component.parent) {
  3213. repaint(component.parent, component.parent.id);
  3214. }
  3215. // repaint the component itself and mark as done
  3216. changed = component.repaint() || changed;
  3217. done[id] = true;
  3218. }
  3219. }
  3220. util.forEach(this.components, repaint);
  3221. // immediately reflow when needed
  3222. if (changed) {
  3223. this.reflow();
  3224. }
  3225. // TODO: limit the number of nested reflows/repaints, prevent loop
  3226. };
  3227. /**
  3228. * Reflow all components
  3229. */
  3230. Controller.prototype.reflow = function reflow() {
  3231. var resized = false;
  3232. // cancel any running repaint request
  3233. if (this.reflowTimer) {
  3234. clearTimeout(this.reflowTimer);
  3235. this.reflowTimer = undefined;
  3236. }
  3237. var done = {};
  3238. function reflow(component, id) {
  3239. if (!(id in done)) {
  3240. // first reflow the components on which this component is dependent
  3241. if (component.depends) {
  3242. component.depends.forEach(function (dep) {
  3243. reflow(dep, dep.id);
  3244. });
  3245. }
  3246. if (component.parent) {
  3247. reflow(component.parent, component.parent.id);
  3248. }
  3249. // reflow the component itself and mark as done
  3250. resized = component.reflow() || resized;
  3251. done[id] = true;
  3252. }
  3253. }
  3254. util.forEach(this.components, reflow);
  3255. // immediately repaint when needed
  3256. if (resized) {
  3257. this.repaint();
  3258. }
  3259. // TODO: limit the number of nested reflows/repaints, prevent loop
  3260. };
  3261. /**
  3262. * Prototype for visual components
  3263. */
  3264. function Component () {
  3265. this.id = null;
  3266. this.parent = null;
  3267. this.depends = null;
  3268. this.controller = null;
  3269. this.options = null;
  3270. this.frame = null; // main DOM element
  3271. this.top = 0;
  3272. this.left = 0;
  3273. this.width = 0;
  3274. this.height = 0;
  3275. }
  3276. /**
  3277. * Set parameters for the frame. Parameters will be merged in current parameter
  3278. * set.
  3279. * @param {Object} options Available parameters:
  3280. * {String | function} [className]
  3281. * {EventBus} [eventBus]
  3282. * {String | Number | function} [left]
  3283. * {String | Number | function} [top]
  3284. * {String | Number | function} [width]
  3285. * {String | Number | function} [height]
  3286. */
  3287. Component.prototype.setOptions = function setOptions(options) {
  3288. if (options) {
  3289. util.extend(this.options, options);
  3290. if (this.controller) {
  3291. this.requestRepaint();
  3292. this.requestReflow();
  3293. }
  3294. }
  3295. };
  3296. /**
  3297. * Get an option value by name
  3298. * The function will first check this.options object, and else will check
  3299. * this.defaultOptions.
  3300. * @param {String} name
  3301. * @return {*} value
  3302. */
  3303. Component.prototype.getOption = function getOption(name) {
  3304. var value;
  3305. if (this.options) {
  3306. value = this.options[name];
  3307. }
  3308. if (value === undefined && this.defaultOptions) {
  3309. value = this.defaultOptions[name];
  3310. }
  3311. return value;
  3312. };
  3313. /**
  3314. * Get the container element of the component, which can be used by a child to
  3315. * add its own widgets. Not all components do have a container for childs, in
  3316. * that case null is returned.
  3317. * @returns {HTMLElement | null} container
  3318. */
  3319. Component.prototype.getContainer = function getContainer() {
  3320. // should be implemented by the component
  3321. return null;
  3322. };
  3323. /**
  3324. * Get the frame element of the component, the outer HTML DOM element.
  3325. * @returns {HTMLElement | null} frame
  3326. */
  3327. Component.prototype.getFrame = function getFrame() {
  3328. return this.frame;
  3329. };
  3330. /**
  3331. * Repaint the component
  3332. * @return {Boolean} changed
  3333. */
  3334. Component.prototype.repaint = function repaint() {
  3335. // should be implemented by the component
  3336. return false;
  3337. };
  3338. /**
  3339. * Reflow the component
  3340. * @return {Boolean} resized
  3341. */
  3342. Component.prototype.reflow = function reflow() {
  3343. // should be implemented by the component
  3344. return false;
  3345. };
  3346. /**
  3347. * Hide the component from the DOM
  3348. * @return {Boolean} changed
  3349. */
  3350. Component.prototype.hide = function hide() {
  3351. if (this.frame && this.frame.parentNode) {
  3352. this.frame.parentNode.removeChild(this.frame);
  3353. return true;
  3354. }
  3355. else {
  3356. return false;
  3357. }
  3358. };
  3359. /**
  3360. * Show the component in the DOM (when not already visible).
  3361. * A repaint will be executed when the component is not visible
  3362. * @return {Boolean} changed
  3363. */
  3364. Component.prototype.show = function show() {
  3365. if (!this.frame || !this.frame.parentNode) {
  3366. return this.repaint();
  3367. }
  3368. else {
  3369. return false;
  3370. }
  3371. };
  3372. /**
  3373. * Request a repaint. The controller will schedule a repaint
  3374. */
  3375. Component.prototype.requestRepaint = function requestRepaint() {
  3376. if (this.controller) {
  3377. this.controller.requestRepaint();
  3378. }
  3379. else {
  3380. throw new Error('Cannot request a repaint: no controller configured');
  3381. // TODO: just do a repaint when no parent is configured?
  3382. }
  3383. };
  3384. /**
  3385. * Request a reflow. The controller will schedule a reflow
  3386. */
  3387. Component.prototype.requestReflow = function requestReflow() {
  3388. if (this.controller) {
  3389. this.controller.requestReflow();
  3390. }
  3391. else {
  3392. throw new Error('Cannot request a reflow: no controller configured');
  3393. // TODO: just do a reflow when no parent is configured?
  3394. }
  3395. };
  3396. /**
  3397. * A panel can contain components
  3398. * @param {Component} [parent]
  3399. * @param {Component[]} [depends] Components on which this components depends
  3400. * (except for the parent)
  3401. * @param {Object} [options] Available parameters:
  3402. * {String | Number | function} [left]
  3403. * {String | Number | function} [top]
  3404. * {String | Number | function} [width]
  3405. * {String | Number | function} [height]
  3406. * {String | function} [className]
  3407. * @constructor Panel
  3408. * @extends Component
  3409. */
  3410. function Panel(parent, depends, options) {
  3411. this.id = util.randomUUID();
  3412. this.parent = parent;
  3413. this.depends = depends;
  3414. this.options = options || {};
  3415. }
  3416. Panel.prototype = new Component();
  3417. /**
  3418. * Set options. Will extend the current options.
  3419. * @param {Object} [options] Available parameters:
  3420. * {String | function} [className]
  3421. * {String | Number | function} [left]
  3422. * {String | Number | function} [top]
  3423. * {String | Number | function} [width]
  3424. * {String | Number | function} [height]
  3425. */
  3426. Panel.prototype.setOptions = Component.prototype.setOptions;
  3427. /**
  3428. * Get the container element of the panel, which can be used by a child to
  3429. * add its own widgets.
  3430. * @returns {HTMLElement} container
  3431. */
  3432. Panel.prototype.getContainer = function () {
  3433. return this.frame;
  3434. };
  3435. /**
  3436. * Repaint the component
  3437. * @return {Boolean} changed
  3438. */
  3439. Panel.prototype.repaint = function () {
  3440. var changed = 0,
  3441. update = util.updateProperty,
  3442. asSize = util.option.asSize,
  3443. options = this.options,
  3444. frame = this.frame;
  3445. if (!frame) {
  3446. frame = document.createElement('div');
  3447. frame.className = 'panel';
  3448. var className = options.className;
  3449. if (className) {
  3450. if (typeof className == 'function') {
  3451. util.addClassName(frame, String(className()));
  3452. }
  3453. else {
  3454. util.addClassName(frame, String(className));
  3455. }
  3456. }
  3457. this.frame = frame;
  3458. changed += 1;
  3459. }
  3460. if (!frame.parentNode) {
  3461. if (!this.parent) {
  3462. throw new Error('Cannot repaint panel: no parent attached');
  3463. }
  3464. var parentContainer = this.parent.getContainer();
  3465. if (!parentContainer) {
  3466. throw new Error('Cannot repaint panel: parent has no container element');
  3467. }
  3468. parentContainer.appendChild(frame);
  3469. changed += 1;
  3470. }
  3471. changed += update(frame.style, 'top', asSize(options.top, '0px'));
  3472. changed += update(frame.style, 'left', asSize(options.left, '0px'));
  3473. changed += update(frame.style, 'width', asSize(options.width, '100%'));
  3474. changed += update(frame.style, 'height', asSize(options.height, '100%'));
  3475. return (changed > 0);
  3476. };
  3477. /**
  3478. * Reflow the component
  3479. * @return {Boolean} resized
  3480. */
  3481. Panel.prototype.reflow = function () {
  3482. var changed = 0,
  3483. update = util.updateProperty,
  3484. frame = this.frame;
  3485. if (frame) {
  3486. changed += update(this, 'top', frame.offsetTop);
  3487. changed += update(this, 'left', frame.offsetLeft);
  3488. changed += update(this, 'width', frame.offsetWidth);
  3489. changed += update(this, 'height', frame.offsetHeight);
  3490. }
  3491. else {
  3492. changed += 1;
  3493. }
  3494. return (changed > 0);
  3495. };
  3496. /**
  3497. * A root panel can hold components. The root panel must be initialized with
  3498. * a DOM element as container.
  3499. * @param {HTMLElement} container
  3500. * @param {Object} [options] Available parameters: see RootPanel.setOptions.
  3501. * @constructor RootPanel
  3502. * @extends Panel
  3503. */
  3504. function RootPanel(container, options) {
  3505. this.id = util.randomUUID();
  3506. this.container = container;
  3507. this.options = options || {};
  3508. this.defaultOptions = {
  3509. autoResize: true
  3510. };
  3511. this.listeners = {}; // event listeners
  3512. }
  3513. RootPanel.prototype = new Panel();
  3514. /**
  3515. * Set options. Will extend the current options.
  3516. * @param {Object} [options] Available parameters:
  3517. * {String | function} [className]
  3518. * {String | Number | function} [left]
  3519. * {String | Number | function} [top]
  3520. * {String | Number | function} [width]
  3521. * {String | Number | function} [height]
  3522. * {Boolean | function} [autoResize]
  3523. */
  3524. RootPanel.prototype.setOptions = Component.prototype.setOptions;
  3525. /**
  3526. * Repaint the component
  3527. * @return {Boolean} changed
  3528. */
  3529. RootPanel.prototype.repaint = function () {
  3530. var changed = 0,
  3531. update = util.updateProperty,
  3532. asSize = util.option.asSize,
  3533. options = this.options,
  3534. frame = this.frame;
  3535. if (!frame) {
  3536. frame = document.createElement('div');
  3537. frame.className = 'graph panel';
  3538. var className = options.className;
  3539. if (className) {
  3540. util.addClassName(frame, util.option.asString(className));
  3541. }
  3542. this.frame = frame;
  3543. changed += 1;
  3544. }
  3545. if (!frame.parentNode) {
  3546. if (!this.container) {
  3547. throw new Error('Cannot repaint root panel: no container attached');
  3548. }
  3549. this.container.appendChild(frame);
  3550. changed += 1;
  3551. }
  3552. changed += update(frame.style, 'top', asSize(options.top, '0px'));
  3553. changed += update(frame.style, 'left', asSize(options.left, '0px'));
  3554. changed += update(frame.style, 'width', asSize(options.width, '100%'));
  3555. changed += update(frame.style, 'height', asSize(options.height, '100%'));
  3556. this._updateEventEmitters();
  3557. this._updateWatch();
  3558. return (changed > 0);
  3559. };
  3560. /**
  3561. * Reflow the component
  3562. * @return {Boolean} resized
  3563. */
  3564. RootPanel.prototype.reflow = function () {
  3565. var changed = 0,
  3566. update = util.updateProperty,
  3567. frame = this.frame;
  3568. if (frame) {
  3569. changed += update(this, 'top', frame.offsetTop);
  3570. changed += update(this, 'left', frame.offsetLeft);
  3571. changed += update(this, 'width', frame.offsetWidth);
  3572. changed += update(this, 'height', frame.offsetHeight);
  3573. }
  3574. else {
  3575. changed += 1;
  3576. }
  3577. return (changed > 0);
  3578. };
  3579. /**
  3580. * Update watching for resize, depending on the current option
  3581. * @private
  3582. */
  3583. RootPanel.prototype._updateWatch = function () {
  3584. var autoResize = this.getOption('autoResize');
  3585. if (autoResize) {
  3586. this._watch();
  3587. }
  3588. else {
  3589. this._unwatch();
  3590. }
  3591. };
  3592. /**
  3593. * Watch for changes in the size of the frame. On resize, the Panel will
  3594. * automatically redraw itself.
  3595. * @private
  3596. */
  3597. RootPanel.prototype._watch = function () {
  3598. var me = this;
  3599. this._unwatch();
  3600. var checkSize = function () {
  3601. var autoResize = me.getOption('autoResize');
  3602. if (!autoResize) {
  3603. // stop watching when the option autoResize is changed to false
  3604. me._unwatch();
  3605. return;
  3606. }
  3607. if (me.frame) {
  3608. // check whether the frame is resized
  3609. if ((me.frame.clientWidth != me.width) ||
  3610. (me.frame.clientHeight != me.height)) {
  3611. me.requestReflow();
  3612. }
  3613. }
  3614. };
  3615. // TODO: automatically cleanup the event listener when the frame is deleted
  3616. util.addEventListener(window, 'resize', checkSize);
  3617. this.watchTimer = setInterval(checkSize, 1000);
  3618. };
  3619. /**
  3620. * Stop watching for a resize of the frame.
  3621. * @private
  3622. */
  3623. RootPanel.prototype._unwatch = function () {
  3624. if (this.watchTimer) {
  3625. clearInterval(this.watchTimer);
  3626. this.watchTimer = undefined;
  3627. }
  3628. // TODO: remove event listener on window.resize
  3629. };
  3630. /**
  3631. * Event handler
  3632. * @param {String} event name of the event, for example 'click', 'mousemove'
  3633. * @param {function} callback callback handler, invoked with the raw HTML Event
  3634. * as parameter.
  3635. */
  3636. RootPanel.prototype.on = function (event, callback) {
  3637. // register the listener at this component
  3638. var arr = this.listeners[event];
  3639. if (!arr) {
  3640. arr = [];
  3641. this.listeners[event] = arr;
  3642. }
  3643. arr.push(callback);
  3644. this._updateEventEmitters();
  3645. };
  3646. /**
  3647. * Update the event listeners for all event emitters
  3648. * @private
  3649. */
  3650. RootPanel.prototype._updateEventEmitters = function () {
  3651. if (this.listeners) {
  3652. var me = this;
  3653. util.forEach(this.listeners, function (listeners, event) {
  3654. if (!me.emitters) {
  3655. me.emitters = {};
  3656. }
  3657. if (!(event in me.emitters)) {
  3658. // create event
  3659. var frame = me.frame;
  3660. if (frame) {
  3661. //console.log('Created a listener for event ' + event + ' on component ' + me.id); // TODO: cleanup logging
  3662. var callback = function(event) {
  3663. listeners.forEach(function (listener) {
  3664. // TODO: filter on event target!
  3665. listener(event);
  3666. });
  3667. };
  3668. me.emitters[event] = callback;
  3669. util.addEventListener(frame, event, callback);
  3670. }
  3671. }
  3672. });
  3673. // TODO: be able to delete event listeners
  3674. // TODO: be able to move event listeners to a parent when available
  3675. }
  3676. };
  3677. /**
  3678. * A horizontal time axis
  3679. * @param {Component} parent
  3680. * @param {Component[]} [depends] Components on which this components depends
  3681. * (except for the parent)
  3682. * @param {Object} [options] See TimeAxis.setOptions for the available
  3683. * options.
  3684. * @constructor TimeAxis
  3685. * @extends Component
  3686. */
  3687. function TimeAxis (parent, depends, options) {
  3688. this.id = util.randomUUID();
  3689. this.parent = parent;
  3690. this.depends = depends;
  3691. this.dom = {
  3692. majorLines: [],
  3693. majorTexts: [],
  3694. minorLines: [],
  3695. minorTexts: [],
  3696. redundant: {
  3697. majorLines: [],
  3698. majorTexts: [],
  3699. minorLines: [],
  3700. minorTexts: []
  3701. }
  3702. };
  3703. this.props = {
  3704. range: {
  3705. start: 0,
  3706. end: 0,
  3707. minimumStep: 0
  3708. },
  3709. lineTop: 0
  3710. };
  3711. this.options = options || {};
  3712. this.defaultOptions = {
  3713. orientation: 'bottom', // supported: 'top', 'bottom'
  3714. // TODO: implement timeaxis orientations 'left' and 'right'
  3715. showMinorLabels: true,
  3716. showMajorLabels: true
  3717. };
  3718. this.conversion = null;
  3719. this.range = null;
  3720. }
  3721. TimeAxis.prototype = new Component();
  3722. // TODO: comment options
  3723. TimeAxis.prototype.setOptions = Component.prototype.setOptions;
  3724. /**
  3725. * Set a range (start and end)
  3726. * @param {Range | Object} range A Range or an object containing start and end.
  3727. */
  3728. TimeAxis.prototype.setRange = function (range) {
  3729. if (!(range instanceof Range) && (!range || !range.start || !range.end)) {
  3730. throw new TypeError('Range must be an instance of Range, ' +
  3731. 'or an object containing start and end.');
  3732. }
  3733. this.range = range;
  3734. };
  3735. /**
  3736. * Convert a position on screen (pixels) to a datetime
  3737. * @param {int} x Position on the screen in pixels
  3738. * @return {Date} time The datetime the corresponds with given position x
  3739. */
  3740. TimeAxis.prototype.toTime = function(x) {
  3741. var conversion = this.conversion;
  3742. return new Date(x / conversion.factor + conversion.offset);
  3743. };
  3744. /**
  3745. * Convert a datetime (Date object) into a position on the screen
  3746. * @param {Date} time A date
  3747. * @return {int} x The position on the screen in pixels which corresponds
  3748. * with the given date.
  3749. * @private
  3750. */
  3751. TimeAxis.prototype.toScreen = function(time) {
  3752. var conversion = this.conversion;
  3753. return (time.valueOf() - conversion.offset) * conversion.factor;
  3754. };
  3755. /**
  3756. * Repaint the component
  3757. * @return {Boolean} changed
  3758. */
  3759. TimeAxis.prototype.repaint = function () {
  3760. var changed = 0,
  3761. update = util.updateProperty,
  3762. asSize = util.option.asSize,
  3763. options = this.options,
  3764. orientation = this.getOption('orientation'),
  3765. props = this.props,
  3766. step = this.step;
  3767. var frame = this.frame;
  3768. if (!frame) {
  3769. frame = document.createElement('div');
  3770. this.frame = frame;
  3771. changed += 1;
  3772. }
  3773. frame.className = 'axis ' + orientation;
  3774. // TODO: custom className?
  3775. if (!frame.parentNode) {
  3776. if (!this.parent) {
  3777. throw new Error('Cannot repaint time axis: no parent attached');
  3778. }
  3779. var parentContainer = this.parent.getContainer();
  3780. if (!parentContainer) {
  3781. throw new Error('Cannot repaint time axis: parent has no container element');
  3782. }
  3783. parentContainer.appendChild(frame);
  3784. changed += 1;
  3785. }
  3786. var parent = frame.parentNode;
  3787. if (parent) {
  3788. var beforeChild = frame.nextSibling;
  3789. parent.removeChild(frame); // take frame offline while updating (is almost twice as fast)
  3790. var defaultTop = (orientation == 'bottom' && this.props.parentHeight && this.height) ?
  3791. (this.props.parentHeight - this.height) + 'px' :
  3792. '0px';
  3793. changed += update(frame.style, 'top', asSize(options.top, defaultTop));
  3794. changed += update(frame.style, 'left', asSize(options.left, '0px'));
  3795. changed += update(frame.style, 'width', asSize(options.width, '100%'));
  3796. changed += update(frame.style, 'height', asSize(options.height, this.height + 'px'));
  3797. // get characters width and height
  3798. this._repaintMeasureChars();
  3799. if (this.step) {
  3800. this._repaintStart();
  3801. step.first();
  3802. var xFirstMajorLabel = undefined;
  3803. var max = 0;
  3804. while (step.hasNext() && max < 1000) {
  3805. max++;
  3806. var cur = step.getCurrent(),
  3807. x = this.toScreen(cur),
  3808. isMajor = step.isMajor();
  3809. // TODO: lines must have a width, such that we can create css backgrounds
  3810. if (this.getOption('showMinorLabels')) {
  3811. this._repaintMinorText(x, step.getLabelMinor());
  3812. }
  3813. if (isMajor && this.getOption('showMajorLabels')) {
  3814. if (x > 0) {
  3815. if (xFirstMajorLabel == undefined) {
  3816. xFirstMajorLabel = x;
  3817. }
  3818. this._repaintMajorText(x, step.getLabelMajor());
  3819. }
  3820. this._repaintMajorLine(x);
  3821. }
  3822. else {
  3823. this._repaintMinorLine(x);
  3824. }
  3825. step.next();
  3826. }
  3827. // create a major label on the left when needed
  3828. if (this.getOption('showMajorLabels')) {
  3829. var leftTime = this.toTime(0),
  3830. leftText = step.getLabelMajor(leftTime),
  3831. widthText = leftText.length * (props.majorCharWidth || 10) + 10; // upper bound estimation
  3832. if (xFirstMajorLabel == undefined || widthText < xFirstMajorLabel) {
  3833. this._repaintMajorText(0, leftText);
  3834. }
  3835. }
  3836. this._repaintEnd();
  3837. }
  3838. this._repaintLine();
  3839. // put frame online again
  3840. if (beforeChild) {
  3841. parent.insertBefore(frame, beforeChild);
  3842. }
  3843. else {
  3844. parent.appendChild(frame)
  3845. }
  3846. }
  3847. return (changed > 0);
  3848. };
  3849. /**
  3850. * Start a repaint. Move all DOM elements to a redundant list, where they
  3851. * can be picked for re-use, or can be cleaned up in the end
  3852. * @private
  3853. */
  3854. TimeAxis.prototype._repaintStart = function () {
  3855. var dom = this.dom,
  3856. redundant = dom.redundant;
  3857. redundant.majorLines = dom.majorLines;
  3858. redundant.majorTexts = dom.majorTexts;
  3859. redundant.minorLines = dom.minorLines;
  3860. redundant.minorTexts = dom.minorTexts;
  3861. dom.majorLines = [];
  3862. dom.majorTexts = [];
  3863. dom.minorLines = [];
  3864. dom.minorTexts = [];
  3865. };
  3866. /**
  3867. * End a repaint. Cleanup leftover DOM elements in the redundant list
  3868. * @private
  3869. */
  3870. TimeAxis.prototype._repaintEnd = function () {
  3871. util.forEach(this.dom.redundant, function (arr) {
  3872. while (arr.length) {
  3873. var elem = arr.pop();
  3874. if (elem && elem.parentNode) {
  3875. elem.parentNode.removeChild(elem);
  3876. }
  3877. }
  3878. });
  3879. };
  3880. /**
  3881. * Create a minor label for the axis at position x
  3882. * @param {Number} x
  3883. * @param {String} text
  3884. * @private
  3885. */
  3886. TimeAxis.prototype._repaintMinorText = function (x, text) {
  3887. // reuse redundant label
  3888. var label = this.dom.redundant.minorTexts.shift();
  3889. if (!label) {
  3890. // create new label
  3891. var content = document.createTextNode('');
  3892. label = document.createElement('div');
  3893. label.appendChild(content);
  3894. label.className = 'text minor';
  3895. this.frame.appendChild(label);
  3896. }
  3897. this.dom.minorTexts.push(label);
  3898. label.childNodes[0].nodeValue = text;
  3899. label.style.left = x + 'px';
  3900. label.style.top = this.props.minorLabelTop + 'px';
  3901. //label.title = title; // TODO: this is a heavy operation
  3902. };
  3903. /**
  3904. * Create a Major label for the axis at position x
  3905. * @param {Number} x
  3906. * @param {String} text
  3907. * @private
  3908. */
  3909. TimeAxis.prototype._repaintMajorText = function (x, text) {
  3910. // reuse redundant label
  3911. var label = this.dom.redundant.majorTexts.shift();
  3912. if (!label) {
  3913. // create label
  3914. var content = document.createTextNode(text);
  3915. label = document.createElement('div');
  3916. label.className = 'text major';
  3917. label.appendChild(content);
  3918. this.frame.appendChild(label);
  3919. }
  3920. this.dom.majorTexts.push(label);
  3921. label.childNodes[0].nodeValue = text;
  3922. label.style.top = this.props.majorLabelTop + 'px';
  3923. label.style.left = x + 'px';
  3924. //label.title = title; // TODO: this is a heavy operation
  3925. };
  3926. /**
  3927. * Create a minor line for the axis at position x
  3928. * @param {Number} x
  3929. * @private
  3930. */
  3931. TimeAxis.prototype._repaintMinorLine = function (x) {
  3932. // reuse redundant line
  3933. var line = this.dom.redundant.minorLines.shift();
  3934. if (!line) {
  3935. // create vertical line
  3936. line = document.createElement('div');
  3937. line.className = 'grid vertical minor';
  3938. this.frame.appendChild(line);
  3939. }
  3940. this.dom.minorLines.push(line);
  3941. var props = this.props;
  3942. line.style.top = props.minorLineTop + 'px';
  3943. line.style.height = props.minorLineHeight + 'px';
  3944. line.style.left = (x - props.minorLineWidth / 2) + 'px';
  3945. };
  3946. /**
  3947. * Create a Major line for the axis at position x
  3948. * @param {Number} x
  3949. * @private
  3950. */
  3951. TimeAxis.prototype._repaintMajorLine = function (x) {
  3952. // reuse redundant line
  3953. var line = this.dom.redundant.majorLines.shift();
  3954. if (!line) {
  3955. // create vertical line
  3956. line = document.createElement('DIV');
  3957. line.className = 'grid vertical major';
  3958. this.frame.appendChild(line);
  3959. }
  3960. this.dom.majorLines.push(line);
  3961. var props = this.props;
  3962. line.style.top = props.majorLineTop + 'px';
  3963. line.style.left = (x - props.majorLineWidth / 2) + 'px';
  3964. line.style.height = props.majorLineHeight + 'px';
  3965. };
  3966. /**
  3967. * Repaint the horizontal line for the axis
  3968. * @private
  3969. */
  3970. TimeAxis.prototype._repaintLine = function() {
  3971. var line = this.dom.line,
  3972. frame = this.frame,
  3973. options = this.options;
  3974. // line before all axis elements
  3975. if (this.getOption('showMinorLabels') || this.getOption('showMajorLabels')) {
  3976. if (line) {
  3977. // put this line at the end of all childs
  3978. frame.removeChild(line);
  3979. frame.appendChild(line);
  3980. }
  3981. else {
  3982. // create the axis line
  3983. line = document.createElement('div');
  3984. line.className = 'grid horizontal major';
  3985. frame.appendChild(line);
  3986. this.dom.line = line;
  3987. }
  3988. line.style.top = this.props.lineTop + 'px';
  3989. }
  3990. else {
  3991. if (line && axis.parentElement) {
  3992. frame.removeChild(axis.line);
  3993. delete this.dom.line;
  3994. }
  3995. }
  3996. };
  3997. /**
  3998. * Create characters used to determine the size of text on the axis
  3999. * @private
  4000. */
  4001. TimeAxis.prototype._repaintMeasureChars = function () {
  4002. // calculate the width and height of a single character
  4003. // this is used to calculate the step size, and also the positioning of the
  4004. // axis
  4005. var dom = this.dom,
  4006. text;
  4007. if (!dom.measureCharMinor) {
  4008. text = document.createTextNode('0');
  4009. var measureCharMinor = document.createElement('DIV');
  4010. measureCharMinor.className = 'text minor measure';
  4011. measureCharMinor.appendChild(text);
  4012. this.frame.appendChild(measureCharMinor);
  4013. dom.measureCharMinor = measureCharMinor;
  4014. }
  4015. if (!dom.measureCharMajor) {
  4016. text = document.createTextNode('0');
  4017. var measureCharMajor = document.createElement('DIV');
  4018. measureCharMajor.className = 'text major measure';
  4019. measureCharMajor.appendChild(text);
  4020. this.frame.appendChild(measureCharMajor);
  4021. dom.measureCharMajor = measureCharMajor;
  4022. }
  4023. };
  4024. /**
  4025. * Reflow the component
  4026. * @return {Boolean} resized
  4027. */
  4028. TimeAxis.prototype.reflow = function () {
  4029. var changed = 0,
  4030. update = util.updateProperty,
  4031. frame = this.frame,
  4032. range = this.range;
  4033. if (!range) {
  4034. throw new Error('Cannot repaint time axis: no range configured');
  4035. }
  4036. if (frame) {
  4037. changed += update(this, 'top', frame.offsetTop);
  4038. changed += update(this, 'left', frame.offsetLeft);
  4039. // calculate size of a character
  4040. var props = this.props,
  4041. showMinorLabels = this.getOption('showMinorLabels'),
  4042. showMajorLabels = this.getOption('showMajorLabels'),
  4043. measureCharMinor = this.dom.measureCharMinor,
  4044. measureCharMajor = this.dom.measureCharMajor;
  4045. if (measureCharMinor) {
  4046. props.minorCharHeight = measureCharMinor.clientHeight;
  4047. props.minorCharWidth = measureCharMinor.clientWidth;
  4048. }
  4049. if (measureCharMajor) {
  4050. props.majorCharHeight = measureCharMajor.clientHeight;
  4051. props.majorCharWidth = measureCharMajor.clientWidth;
  4052. }
  4053. var parentHeight = frame.parentNode ? frame.parentNode.offsetHeight : 0;
  4054. if (parentHeight != props.parentHeight) {
  4055. props.parentHeight = parentHeight;
  4056. changed += 1;
  4057. }
  4058. switch (this.getOption('orientation')) {
  4059. case 'bottom':
  4060. props.minorLabelHeight = showMinorLabels ? props.minorCharHeight : 0;
  4061. props.majorLabelHeight = showMajorLabels ? props.majorCharHeight : 0;
  4062. props.minorLabelTop = 0;
  4063. props.majorLabelTop = props.minorLabelTop + props.minorLabelHeight;
  4064. props.minorLineTop = -this.top;
  4065. props.minorLineHeight = Math.max(this.top + props.majorLabelHeight, 0);
  4066. props.minorLineWidth = 1; // TODO: really calculate width
  4067. props.majorLineTop = -this.top;
  4068. props.majorLineHeight = Math.max(this.top + props.minorLabelHeight + props.majorLabelHeight, 0);
  4069. props.majorLineWidth = 1; // TODO: really calculate width
  4070. props.lineTop = 0;
  4071. break;
  4072. case 'top':
  4073. props.minorLabelHeight = showMinorLabels ? props.minorCharHeight : 0;
  4074. props.majorLabelHeight = showMajorLabels ? props.majorCharHeight : 0;
  4075. props.majorLabelTop = 0;
  4076. props.minorLabelTop = props.majorLabelTop + props.majorLabelHeight;
  4077. props.minorLineTop = props.minorLabelTop;
  4078. props.minorLineHeight = Math.max(parentHeight - props.majorLabelHeight - this.top);
  4079. props.minorLineWidth = 1; // TODO: really calculate width
  4080. props.majorLineTop = 0;
  4081. props.majorLineHeight = Math.max(parentHeight - this.top);
  4082. props.majorLineWidth = 1; // TODO: really calculate width
  4083. props.lineTop = props.majorLabelHeight + props.minorLabelHeight;
  4084. break;
  4085. default:
  4086. throw new Error('Unkown orientation "' + this.getOption('orientation') + '"');
  4087. }
  4088. var height = props.minorLabelHeight + props.majorLabelHeight;
  4089. changed += update(this, 'width', frame.offsetWidth);
  4090. changed += update(this, 'height', height);
  4091. // calculate range and step
  4092. this._updateConversion();
  4093. var start = util.cast(range.start, 'Date'),
  4094. end = util.cast(range.end, 'Date'),
  4095. minimumStep = this.toTime((props.minorCharWidth || 10) * 5) - this.toTime(0);
  4096. this.step = new TimeStep(start, end, minimumStep);
  4097. changed += update(props.range, 'start', start.valueOf());
  4098. changed += update(props.range, 'end', end.valueOf());
  4099. changed += update(props.range, 'minimumStep', minimumStep.valueOf());
  4100. }
  4101. return (changed > 0);
  4102. };
  4103. /**
  4104. * Calculate the factor and offset to convert a position on screen to the
  4105. * corresponding date and vice versa.
  4106. * After the method _updateConversion is executed once, the methods toTime
  4107. * and toScreen can be used.
  4108. * @private
  4109. */
  4110. TimeAxis.prototype._updateConversion = function() {
  4111. var range = this.range;
  4112. if (!range) {
  4113. throw new Error('No range configured');
  4114. }
  4115. if (range.conversion) {
  4116. this.conversion = range.conversion(this.width);
  4117. }
  4118. else {
  4119. this.conversion = Range.conversion(range.start, range.end, this.width);
  4120. }
  4121. };
  4122. /**
  4123. * An ItemSet holds a set of items and ranges which can be displayed in a
  4124. * range. The width is determined by the parent of the ItemSet, and the height
  4125. * is determined by the size of the items.
  4126. * @param {Component} parent
  4127. * @param {Component[]} [depends] Components on which this components depends
  4128. * (except for the parent)
  4129. * @param {Object} [options] See ItemSet.setOptions for the available
  4130. * options.
  4131. * @constructor ItemSet
  4132. * @extends Panel
  4133. */
  4134. // TODO: improve performance by replacing all Array.forEach with a for loop
  4135. function ItemSet(parent, depends, options) {
  4136. this.id = util.randomUUID();
  4137. this.parent = parent;
  4138. this.depends = depends;
  4139. // one options object is shared by this itemset and all its items
  4140. this.options = options || {};
  4141. this.defaultOptions = {
  4142. style: 'box',
  4143. align: 'center',
  4144. orientation: 'bottom',
  4145. margin: {
  4146. axis: 20,
  4147. item: 10
  4148. },
  4149. padding: 5
  4150. };
  4151. this.dom = {};
  4152. var me = this;
  4153. this.itemsData = null; // DataSet
  4154. this.range = null; // Range or Object {start: number, end: number}
  4155. this.listeners = {
  4156. 'add': function (event, params, senderId) {
  4157. if (senderId != me.id) {
  4158. me._onAdd(params.items);
  4159. }
  4160. },
  4161. 'update': function (event, params, senderId) {
  4162. if (senderId != me.id) {
  4163. me._onUpdate(params.items);
  4164. }
  4165. },
  4166. 'remove': function (event, params, senderId) {
  4167. if (senderId != me.id) {
  4168. me._onRemove(params.items);
  4169. }
  4170. }
  4171. };
  4172. this.items = {}; // object with an Item for every data item
  4173. this.queue = {}; // queue with id/actions: 'add', 'update', 'delete'
  4174. this.stack = new Stack(this, Object.create(this.options));
  4175. this.conversion = null;
  4176. // TODO: ItemSet should also attach event listeners for rangechange and rangechanged, like timeaxis
  4177. }
  4178. ItemSet.prototype = new Panel();
  4179. // available item types will be registered here
  4180. ItemSet.types = {
  4181. box: ItemBox,
  4182. range: ItemRange,
  4183. point: ItemPoint
  4184. };
  4185. /**
  4186. * Set options for the ItemSet. Existing options will be extended/overwritten.
  4187. * @param {Object} [options] The following options are available:
  4188. * {String | function} [className]
  4189. * class name for the itemset
  4190. * {String} [style]
  4191. * Default style for the items. Choose from 'box'
  4192. * (default), 'point', or 'range'. The default
  4193. * Style can be overwritten by individual items.
  4194. * {String} align
  4195. * Alignment for the items, only applicable for
  4196. * ItemBox. Choose 'center' (default), 'left', or
  4197. * 'right'.
  4198. * {String} orientation
  4199. * Orientation of the item set. Choose 'top' or
  4200. * 'bottom' (default).
  4201. * {Number} margin.axis
  4202. * Margin between the axis and the items in pixels.
  4203. * Default is 20.
  4204. * {Number} margin.item
  4205. * Margin between items in pixels. Default is 10.
  4206. * {Number} padding
  4207. * Padding of the contents of an item in pixels.
  4208. * Must correspond with the items css. Default is 5.
  4209. */
  4210. ItemSet.prototype.setOptions = Component.prototype.setOptions;
  4211. /**
  4212. * Set range (start and end).
  4213. * @param {Range | Object} range A Range or an object containing start and end.
  4214. */
  4215. ItemSet.prototype.setRange = function setRange(range) {
  4216. if (!(range instanceof Range) && (!range || !range.start || !range.end)) {
  4217. throw new TypeError('Range must be an instance of Range, ' +
  4218. 'or an object containing start and end.');
  4219. }
  4220. this.range = range;
  4221. };
  4222. /**
  4223. * Repaint the component
  4224. * @return {Boolean} changed
  4225. */
  4226. ItemSet.prototype.repaint = function repaint() {
  4227. var changed = 0,
  4228. update = util.updateProperty,
  4229. asSize = util.option.asSize,
  4230. options = this.options,
  4231. orientation = this.getOption('orientation'),
  4232. defaultOptions = this.defaultOptions,
  4233. frame = this.frame;
  4234. if (!frame) {
  4235. frame = document.createElement('div');
  4236. frame.className = 'itemset';
  4237. var className = options.className;
  4238. if (className) {
  4239. util.addClassName(frame, util.option.asString(className));
  4240. }
  4241. // create background panel
  4242. var background = document.createElement('div');
  4243. background.className = 'background';
  4244. frame.appendChild(background);
  4245. this.dom.background = background;
  4246. // create foreground panel
  4247. var foreground = document.createElement('div');
  4248. foreground.className = 'foreground';
  4249. frame.appendChild(foreground);
  4250. this.dom.foreground = foreground;
  4251. // create axis panel
  4252. var axis = document.createElement('div');
  4253. axis.className = 'itemset-axis';
  4254. //frame.appendChild(axis);
  4255. this.dom.axis = axis;
  4256. this.frame = frame;
  4257. changed += 1;
  4258. }
  4259. if (!this.parent) {
  4260. throw new Error('Cannot repaint itemset: no parent attached');
  4261. }
  4262. var parentContainer = this.parent.getContainer();
  4263. if (!parentContainer) {
  4264. throw new Error('Cannot repaint itemset: parent has no container element');
  4265. }
  4266. if (!frame.parentNode) {
  4267. parentContainer.appendChild(frame);
  4268. changed += 1;
  4269. }
  4270. if (!this.dom.axis.parentNode) {
  4271. parentContainer.appendChild(this.dom.axis);
  4272. changed += 1;
  4273. }
  4274. // reposition frame
  4275. changed += update(frame.style, 'left', asSize(options.left, '0px'));
  4276. changed += update(frame.style, 'top', asSize(options.top, '0px'));
  4277. changed += update(frame.style, 'width', asSize(options.width, '100%'));
  4278. changed += update(frame.style, 'height', asSize(options.height, this.height + 'px'));
  4279. // reposition axis
  4280. changed += update(this.dom.axis.style, 'left', asSize(options.left, '0px'));
  4281. changed += update(this.dom.axis.style, 'width', asSize(options.width, '100%'));
  4282. if (orientation == 'bottom') {
  4283. changed += update(this.dom.axis.style, 'top', (this.height + this.top) + 'px');
  4284. }
  4285. else { // orientation == 'top'
  4286. changed += update(this.dom.axis.style, 'top', this.top + 'px');
  4287. }
  4288. this._updateConversion();
  4289. var me = this,
  4290. queue = this.queue,
  4291. itemsData = this.itemsData,
  4292. items = this.items,
  4293. dataOptions = {
  4294. fields: [(itemsData && itemsData.fieldId || 'id'), 'start', 'end', 'content', 'type']
  4295. };
  4296. // TODO: copy options from the itemset itself?
  4297. // show/hide added/changed/removed items
  4298. Object.keys(queue).forEach(function (id) {
  4299. //var entry = queue[id];
  4300. var action = queue[id];
  4301. var item = items[id];
  4302. //var item = entry.item;
  4303. //noinspection FallthroughInSwitchStatementJS
  4304. switch (action) {
  4305. case 'add':
  4306. case 'update':
  4307. var itemData = itemsData && itemsData.get(id, dataOptions);
  4308. if (itemData) {
  4309. var type = itemData.type ||
  4310. (itemData.start && itemData.end && 'range') ||
  4311. 'box';
  4312. var constructor = ItemSet.types[type];
  4313. // TODO: how to handle items with invalid data? hide them and give a warning? or throw an error?
  4314. if (item) {
  4315. // update item
  4316. if (!constructor || !(item instanceof constructor)) {
  4317. // item type has changed, hide and delete the item
  4318. changed += item.hide();
  4319. item = null;
  4320. }
  4321. else {
  4322. item.data = itemData; // TODO: create a method item.setData ?
  4323. changed++;
  4324. }
  4325. }
  4326. if (!item) {
  4327. // create item
  4328. if (constructor) {
  4329. item = new constructor(me, itemData, options, defaultOptions);
  4330. changed++;
  4331. }
  4332. else {
  4333. throw new TypeError('Unknown item type "' + type + '"');
  4334. }
  4335. }
  4336. // force a repaint (not only a reposition)
  4337. item.repaint();
  4338. items[id] = item;
  4339. }
  4340. // update queue
  4341. delete queue[id];
  4342. break;
  4343. case 'remove':
  4344. if (item) {
  4345. // remove DOM of the item
  4346. changed += item.hide();
  4347. }
  4348. // update lists
  4349. delete items[id];
  4350. delete queue[id];
  4351. break;
  4352. default:
  4353. console.log('Error: unknown action "' + action + '"');
  4354. }
  4355. });
  4356. // reposition all items. Show items only when in the visible area
  4357. util.forEach(this.items, function (item) {
  4358. if (item.visible) {
  4359. changed += item.show();
  4360. item.reposition();
  4361. }
  4362. else {
  4363. changed += item.hide();
  4364. }
  4365. });
  4366. return (changed > 0);
  4367. };
  4368. /**
  4369. * Get the foreground container element
  4370. * @return {HTMLElement} foreground
  4371. */
  4372. ItemSet.prototype.getForeground = function getForeground() {
  4373. return this.dom.foreground;
  4374. };
  4375. /**
  4376. * Get the background container element
  4377. * @return {HTMLElement} background
  4378. */
  4379. ItemSet.prototype.getBackground = function getBackground() {
  4380. return this.dom.background;
  4381. };
  4382. /**
  4383. * Get the axis container element
  4384. * @return {HTMLElement} axis
  4385. */
  4386. ItemSet.prototype.getAxis = function getAxis() {
  4387. return this.dom.axis;
  4388. };
  4389. /**
  4390. * Reflow the component
  4391. * @return {Boolean} resized
  4392. */
  4393. ItemSet.prototype.reflow = function reflow () {
  4394. var changed = 0,
  4395. options = this.options,
  4396. marginAxis = options.margin && options.margin.axis || this.defaultOptions.margin.axis,
  4397. marginItem = options.margin && options.margin.item || this.defaultOptions.margin.item,
  4398. update = util.updateProperty,
  4399. asNumber = util.option.asNumber,
  4400. asSize = util.option.asSize,
  4401. frame = this.frame;
  4402. if (frame) {
  4403. this._updateConversion();
  4404. util.forEach(this.items, function (item) {
  4405. changed += item.reflow();
  4406. });
  4407. // TODO: stack.update should be triggered via an event, in stack itself
  4408. // TODO: only update the stack when there are changed items
  4409. this.stack.update();
  4410. var maxHeight = asNumber(options.maxHeight);
  4411. var fixedHeight = (asSize(options.height) != null);
  4412. var height;
  4413. if (fixedHeight) {
  4414. height = frame.offsetHeight;
  4415. }
  4416. else {
  4417. // height is not specified, determine the height from the height and positioned items
  4418. var visibleItems = this.stack.ordered; // TODO: not so nice way to get the filtered items
  4419. if (visibleItems.length) {
  4420. var min = visibleItems[0].top;
  4421. var max = visibleItems[0].top + visibleItems[0].height;
  4422. util.forEach(visibleItems, function (item) {
  4423. min = Math.min(min, item.top);
  4424. max = Math.max(max, (item.top + item.height));
  4425. });
  4426. height = (max - min) + marginAxis + marginItem;
  4427. }
  4428. else {
  4429. height = marginAxis + marginItem;
  4430. }
  4431. }
  4432. if (maxHeight != null) {
  4433. height = Math.min(height, maxHeight);
  4434. }
  4435. changed += update(this, 'height', height);
  4436. // calculate height from items
  4437. changed += update(this, 'top', frame.offsetTop);
  4438. changed += update(this, 'left', frame.offsetLeft);
  4439. changed += update(this, 'width', frame.offsetWidth);
  4440. }
  4441. else {
  4442. changed += 1;
  4443. }
  4444. return (changed > 0);
  4445. };
  4446. /**
  4447. * Hide this component from the DOM
  4448. * @return {Boolean} changed
  4449. */
  4450. ItemSet.prototype.hide = function hide() {
  4451. var changed = false;
  4452. // remove the DOM
  4453. if (this.frame && this.frame.parentNode) {
  4454. this.frame.parentNode.removeChild(this.frame);
  4455. changed = true;
  4456. }
  4457. if (this.dom.axis && this.dom.axis.parentNode) {
  4458. this.dom.axis.parentNode.removeChild(this.dom.axis);
  4459. changed = true;
  4460. }
  4461. return changed;
  4462. };
  4463. /**
  4464. * Set items
  4465. * @param {vis.DataSet | null} items
  4466. */
  4467. ItemSet.prototype.setItems = function setItems(items) {
  4468. var me = this,
  4469. ids;
  4470. // unsubscribe from current dataset
  4471. var current = this.itemsData;
  4472. if (current) {
  4473. util.forEach(this.listeners, function (callback, event) {
  4474. current.unsubscribe(event, callback);
  4475. });
  4476. // remove all drawn items
  4477. ids = current.getIds();
  4478. this._onRemove(ids);
  4479. }
  4480. // replace the dataset
  4481. if (!items) {
  4482. this.itemsData = null;
  4483. }
  4484. else if (items instanceof DataSet || items instanceof DataView) {
  4485. this.itemsData = items;
  4486. }
  4487. else {
  4488. throw new TypeError('Data must be an instance of DataSet');
  4489. }
  4490. if (this.itemsData) {
  4491. // subscribe to new dataset
  4492. var id = this.id;
  4493. util.forEach(this.listeners, function (callback, event) {
  4494. me.itemsData.subscribe(event, callback, id);
  4495. });
  4496. // draw all new items
  4497. ids = this.itemsData.getIds();
  4498. this._onAdd(ids);
  4499. }
  4500. };
  4501. /**
  4502. * Get the current items items
  4503. * @returns {vis.DataSet | null}
  4504. */
  4505. ItemSet.prototype.getItems = function getItems() {
  4506. return this.itemsData;
  4507. };
  4508. /**
  4509. * Handle updated items
  4510. * @param {Number[]} ids
  4511. * @private
  4512. */
  4513. ItemSet.prototype._onUpdate = function _onUpdate(ids) {
  4514. this._toQueue('update', ids);
  4515. };
  4516. /**
  4517. * Handle changed items
  4518. * @param {Number[]} ids
  4519. * @private
  4520. */
  4521. ItemSet.prototype._onAdd = function _onAdd(ids) {
  4522. this._toQueue('add', ids);
  4523. };
  4524. /**
  4525. * Handle removed items
  4526. * @param {Number[]} ids
  4527. * @private
  4528. */
  4529. ItemSet.prototype._onRemove = function _onRemove(ids) {
  4530. this._toQueue('remove', ids);
  4531. };
  4532. /**
  4533. * Put items in the queue to be added/updated/remove
  4534. * @param {String} action can be 'add', 'update', 'remove'
  4535. * @param {Number[]} ids
  4536. */
  4537. ItemSet.prototype._toQueue = function _toQueue(action, ids) {
  4538. var queue = this.queue;
  4539. ids.forEach(function (id) {
  4540. queue[id] = action;
  4541. });
  4542. if (this.controller) {
  4543. //this.requestReflow();
  4544. this.requestRepaint();
  4545. }
  4546. };
  4547. /**
  4548. * Calculate the factor and offset to convert a position on screen to the
  4549. * corresponding date and vice versa.
  4550. * After the method _updateConversion is executed once, the methods toTime
  4551. * and toScreen can be used.
  4552. * @private
  4553. */
  4554. ItemSet.prototype._updateConversion = function _updateConversion() {
  4555. var range = this.range;
  4556. if (!range) {
  4557. throw new Error('No range configured');
  4558. }
  4559. if (range.conversion) {
  4560. this.conversion = range.conversion(this.width);
  4561. }
  4562. else {
  4563. this.conversion = Range.conversion(range.start, range.end, this.width);
  4564. }
  4565. };
  4566. /**
  4567. * Convert a position on screen (pixels) to a datetime
  4568. * Before this method can be used, the method _updateConversion must be
  4569. * executed once.
  4570. * @param {int} x Position on the screen in pixels
  4571. * @return {Date} time The datetime the corresponds with given position x
  4572. */
  4573. ItemSet.prototype.toTime = function toTime(x) {
  4574. var conversion = this.conversion;
  4575. return new Date(x / conversion.factor + conversion.offset);
  4576. };
  4577. /**
  4578. * Convert a datetime (Date object) into a position on the screen
  4579. * Before this method can be used, the method _updateConversion must be
  4580. * executed once.
  4581. * @param {Date} time A date
  4582. * @return {int} x The position on the screen in pixels which corresponds
  4583. * with the given date.
  4584. */
  4585. ItemSet.prototype.toScreen = function toScreen(time) {
  4586. var conversion = this.conversion;
  4587. return (time.valueOf() - conversion.offset) * conversion.factor;
  4588. };
  4589. /**
  4590. * @constructor Item
  4591. * @param {ItemSet} parent
  4592. * @param {Object} data Object containing (optional) parameters type,
  4593. * start, end, content, group, className.
  4594. * @param {Object} [options] Options to set initial property values
  4595. * @param {Object} [defaultOptions] default options
  4596. * // TODO: describe available options
  4597. */
  4598. function Item (parent, data, options, defaultOptions) {
  4599. this.parent = parent;
  4600. this.data = data;
  4601. this.dom = null;
  4602. this.options = options || {};
  4603. this.defaultOptions = defaultOptions || {};
  4604. this.selected = false;
  4605. this.visible = false;
  4606. this.top = 0;
  4607. this.left = 0;
  4608. this.width = 0;
  4609. this.height = 0;
  4610. }
  4611. /**
  4612. * Select current item
  4613. */
  4614. Item.prototype.select = function select() {
  4615. this.selected = true;
  4616. };
  4617. /**
  4618. * Unselect current item
  4619. */
  4620. Item.prototype.unselect = function unselect() {
  4621. this.selected = false;
  4622. };
  4623. /**
  4624. * Show the Item in the DOM (when not already visible)
  4625. * @return {Boolean} changed
  4626. */
  4627. Item.prototype.show = function show() {
  4628. return false;
  4629. };
  4630. /**
  4631. * Hide the Item from the DOM (when visible)
  4632. * @return {Boolean} changed
  4633. */
  4634. Item.prototype.hide = function hide() {
  4635. return false;
  4636. };
  4637. /**
  4638. * Repaint the item
  4639. * @return {Boolean} changed
  4640. */
  4641. Item.prototype.repaint = function repaint() {
  4642. // should be implemented by the item
  4643. return false;
  4644. };
  4645. /**
  4646. * Reflow the item
  4647. * @return {Boolean} resized
  4648. */
  4649. Item.prototype.reflow = function reflow() {
  4650. // should be implemented by the item
  4651. return false;
  4652. };
  4653. /**
  4654. * @constructor ItemBox
  4655. * @extends Item
  4656. * @param {ItemSet} parent
  4657. * @param {Object} data Object containing parameters start
  4658. * content, className.
  4659. * @param {Object} [options] Options to set initial property values
  4660. * @param {Object} [defaultOptions] default options
  4661. * // TODO: describe available options
  4662. */
  4663. function ItemBox (parent, data, options, defaultOptions) {
  4664. this.props = {
  4665. dot: {
  4666. left: 0,
  4667. top: 0,
  4668. width: 0,
  4669. height: 0
  4670. },
  4671. line: {
  4672. top: 0,
  4673. left: 0,
  4674. width: 0,
  4675. height: 0
  4676. }
  4677. };
  4678. Item.call(this, parent, data, options, defaultOptions);
  4679. }
  4680. ItemBox.prototype = new Item (null, null);
  4681. /**
  4682. * Select the item
  4683. * @override
  4684. */
  4685. ItemBox.prototype.select = function select() {
  4686. this.selected = true;
  4687. // TODO: select and unselect
  4688. };
  4689. /**
  4690. * Unselect the item
  4691. * @override
  4692. */
  4693. ItemBox.prototype.unselect = function unselect() {
  4694. this.selected = false;
  4695. // TODO: select and unselect
  4696. };
  4697. /**
  4698. * Repaint the item
  4699. * @return {Boolean} changed
  4700. */
  4701. ItemBox.prototype.repaint = function repaint() {
  4702. // TODO: make an efficient repaint
  4703. var changed = false;
  4704. var dom = this.dom;
  4705. if (!dom) {
  4706. this._create();
  4707. dom = this.dom;
  4708. changed = true;
  4709. }
  4710. if (dom) {
  4711. if (!this.parent) {
  4712. throw new Error('Cannot repaint item: no parent attached');
  4713. }
  4714. var foreground = this.parent.getForeground();
  4715. if (!foreground) {
  4716. throw new Error('Cannot repaint time axis: ' +
  4717. 'parent has no foreground container element');
  4718. }
  4719. var background = this.parent.getBackground();
  4720. if (!background) {
  4721. throw new Error('Cannot repaint time axis: ' +
  4722. 'parent has no background container element');
  4723. }
  4724. var axis = this.parent.getAxis();
  4725. if (!background) {
  4726. throw new Error('Cannot repaint time axis: ' +
  4727. 'parent has no axis container element');
  4728. }
  4729. if (!dom.box.parentNode) {
  4730. foreground.appendChild(dom.box);
  4731. changed = true;
  4732. }
  4733. if (!dom.line.parentNode) {
  4734. background.appendChild(dom.line);
  4735. changed = true;
  4736. }
  4737. if (!dom.dot.parentNode) {
  4738. axis.appendChild(dom.dot);
  4739. changed = true;
  4740. }
  4741. // update contents
  4742. if (this.data.content != this.content) {
  4743. this.content = this.data.content;
  4744. if (this.content instanceof Element) {
  4745. dom.content.innerHTML = '';
  4746. dom.content.appendChild(this.content);
  4747. }
  4748. else if (this.data.content != undefined) {
  4749. dom.content.innerHTML = this.content;
  4750. }
  4751. else {
  4752. throw new Error('Property "content" missing in item ' + this.data.id);
  4753. }
  4754. changed = true;
  4755. }
  4756. // update class
  4757. var className = (this.data.className? ' ' + this.data.className : '') +
  4758. (this.selected ? ' selected' : '');
  4759. if (this.className != className) {
  4760. this.className = className;
  4761. dom.box.className = 'item box' + className;
  4762. dom.line.className = 'item line' + className;
  4763. dom.dot.className = 'item dot' + className;
  4764. changed = true;
  4765. }
  4766. }
  4767. return changed;
  4768. };
  4769. /**
  4770. * Show the item in the DOM (when not already visible). The items DOM will
  4771. * be created when needed.
  4772. * @return {Boolean} changed
  4773. */
  4774. ItemBox.prototype.show = function show() {
  4775. if (!this.dom || !this.dom.box.parentNode) {
  4776. return this.repaint();
  4777. }
  4778. else {
  4779. return false;
  4780. }
  4781. };
  4782. /**
  4783. * Hide the item from the DOM (when visible)
  4784. * @return {Boolean} changed
  4785. */
  4786. ItemBox.prototype.hide = function hide() {
  4787. var changed = false,
  4788. dom = this.dom;
  4789. if (dom) {
  4790. if (dom.box.parentNode) {
  4791. dom.box.parentNode.removeChild(dom.box);
  4792. changed = true;
  4793. }
  4794. if (dom.line.parentNode) {
  4795. dom.line.parentNode.removeChild(dom.line);
  4796. }
  4797. if (dom.dot.parentNode) {
  4798. dom.dot.parentNode.removeChild(dom.dot);
  4799. }
  4800. }
  4801. return changed;
  4802. };
  4803. /**
  4804. * Reflow the item: calculate its actual size and position from the DOM
  4805. * @return {boolean} resized returns true if the axis is resized
  4806. * @override
  4807. */
  4808. ItemBox.prototype.reflow = function reflow() {
  4809. var changed = 0,
  4810. update,
  4811. dom,
  4812. props,
  4813. options,
  4814. margin,
  4815. start,
  4816. align,
  4817. orientation,
  4818. top,
  4819. left,
  4820. data,
  4821. range;
  4822. if (this.data.start == undefined) {
  4823. throw new Error('Property "start" missing in item ' + this.data.id);
  4824. }
  4825. data = this.data;
  4826. range = this.parent && this.parent.range;
  4827. if (data && range) {
  4828. // TODO: account for the width of the item. Take some margin
  4829. this.visible = (data.start > range.start) && (data.start < range.end);
  4830. }
  4831. else {
  4832. this.visible = false;
  4833. }
  4834. if (this.visible) {
  4835. dom = this.dom;
  4836. if (dom) {
  4837. update = util.updateProperty;
  4838. props = this.props;
  4839. options = this.options;
  4840. start = this.parent.toScreen(this.data.start);
  4841. align = options.align || this.defaultOptions.align;
  4842. margin = options.margin && options.margin.axis || this.defaultOptions.margin.axis;
  4843. orientation = options.orientation || this.defaultOptions.orientation;
  4844. changed += update(props.dot, 'height', dom.dot.offsetHeight);
  4845. changed += update(props.dot, 'width', dom.dot.offsetWidth);
  4846. changed += update(props.line, 'width', dom.line.offsetWidth);
  4847. changed += update(props.line, 'height', dom.line.offsetHeight);
  4848. changed += update(props.line, 'top', dom.line.offsetTop);
  4849. changed += update(this, 'width', dom.box.offsetWidth);
  4850. changed += update(this, 'height', dom.box.offsetHeight);
  4851. if (align == 'right') {
  4852. left = start - this.width;
  4853. }
  4854. else if (align == 'left') {
  4855. left = start;
  4856. }
  4857. else {
  4858. // default or 'center'
  4859. left = start - this.width / 2;
  4860. }
  4861. changed += update(this, 'left', left);
  4862. changed += update(props.line, 'left', start - props.line.width / 2);
  4863. changed += update(props.dot, 'left', start - props.dot.width / 2);
  4864. changed += update(props.dot, 'top', -props.dot.height / 2);
  4865. if (orientation == 'top') {
  4866. top = margin;
  4867. changed += update(this, 'top', top);
  4868. }
  4869. else {
  4870. // default or 'bottom'
  4871. var parentHeight = this.parent.height;
  4872. top = parentHeight - this.height - margin;
  4873. changed += update(this, 'top', top);
  4874. }
  4875. }
  4876. else {
  4877. changed += 1;
  4878. }
  4879. }
  4880. return (changed > 0);
  4881. };
  4882. /**
  4883. * Create an items DOM
  4884. * @private
  4885. */
  4886. ItemBox.prototype._create = function _create() {
  4887. var dom = this.dom;
  4888. if (!dom) {
  4889. this.dom = dom = {};
  4890. // create the box
  4891. dom.box = document.createElement('DIV');
  4892. // className is updated in repaint()
  4893. // contents box (inside the background box). used for making margins
  4894. dom.content = document.createElement('DIV');
  4895. dom.content.className = 'content';
  4896. dom.box.appendChild(dom.content);
  4897. // line to axis
  4898. dom.line = document.createElement('DIV');
  4899. dom.line.className = 'line';
  4900. // dot on axis
  4901. dom.dot = document.createElement('DIV');
  4902. dom.dot.className = 'dot';
  4903. }
  4904. };
  4905. /**
  4906. * Reposition the item, recalculate its left, top, and width, using the current
  4907. * range and size of the items itemset
  4908. * @override
  4909. */
  4910. ItemBox.prototype.reposition = function reposition() {
  4911. var dom = this.dom,
  4912. props = this.props,
  4913. orientation = this.options.orientation || this.defaultOptions.orientation;
  4914. if (dom) {
  4915. var box = dom.box,
  4916. line = dom.line,
  4917. dot = dom.dot;
  4918. box.style.left = this.left + 'px';
  4919. box.style.top = this.top + 'px';
  4920. line.style.left = props.line.left + 'px';
  4921. if (orientation == 'top') {
  4922. line.style.top = 0 + 'px';
  4923. line.style.height = this.top + 'px';
  4924. }
  4925. else {
  4926. // orientation 'bottom'
  4927. line.style.top = (this.top + this.height) + 'px';
  4928. line.style.height = Math.max(this.parent.height - this.top - this.height +
  4929. this.props.dot.height / 2, 0) + 'px';
  4930. }
  4931. dot.style.left = props.dot.left + 'px';
  4932. dot.style.top = props.dot.top + 'px';
  4933. }
  4934. };
  4935. /**
  4936. * @constructor ItemPoint
  4937. * @extends Item
  4938. * @param {ItemSet} parent
  4939. * @param {Object} data Object containing parameters start
  4940. * content, className.
  4941. * @param {Object} [options] Options to set initial property values
  4942. * @param {Object} [defaultOptions] default options
  4943. * // TODO: describe available options
  4944. */
  4945. function ItemPoint (parent, data, options, defaultOptions) {
  4946. this.props = {
  4947. dot: {
  4948. top: 0,
  4949. width: 0,
  4950. height: 0
  4951. },
  4952. content: {
  4953. height: 0,
  4954. marginLeft: 0
  4955. }
  4956. };
  4957. Item.call(this, parent, data, options, defaultOptions);
  4958. }
  4959. ItemPoint.prototype = new Item (null, null);
  4960. /**
  4961. * Select the item
  4962. * @override
  4963. */
  4964. ItemPoint.prototype.select = function select() {
  4965. this.selected = true;
  4966. // TODO: select and unselect
  4967. };
  4968. /**
  4969. * Unselect the item
  4970. * @override
  4971. */
  4972. ItemPoint.prototype.unselect = function unselect() {
  4973. this.selected = false;
  4974. // TODO: select and unselect
  4975. };
  4976. /**
  4977. * Repaint the item
  4978. * @return {Boolean} changed
  4979. */
  4980. ItemPoint.prototype.repaint = function repaint() {
  4981. // TODO: make an efficient repaint
  4982. var changed = false;
  4983. var dom = this.dom;
  4984. if (!dom) {
  4985. this._create();
  4986. dom = this.dom;
  4987. changed = true;
  4988. }
  4989. if (dom) {
  4990. if (!this.parent) {
  4991. throw new Error('Cannot repaint item: no parent attached');
  4992. }
  4993. var foreground = this.parent.getForeground();
  4994. if (!foreground) {
  4995. throw new Error('Cannot repaint time axis: ' +
  4996. 'parent has no foreground container element');
  4997. }
  4998. if (!dom.point.parentNode) {
  4999. foreground.appendChild(dom.point);
  5000. foreground.appendChild(dom.point);
  5001. changed = true;
  5002. }
  5003. // update contents
  5004. if (this.data.content != this.content) {
  5005. this.content = this.data.content;
  5006. if (this.content instanceof Element) {
  5007. dom.content.innerHTML = '';
  5008. dom.content.appendChild(this.content);
  5009. }
  5010. else if (this.data.content != undefined) {
  5011. dom.content.innerHTML = this.content;
  5012. }
  5013. else {
  5014. throw new Error('Property "content" missing in item ' + this.data.id);
  5015. }
  5016. changed = true;
  5017. }
  5018. // update class
  5019. var className = (this.data.className? ' ' + this.data.className : '') +
  5020. (this.selected ? ' selected' : '');
  5021. if (this.className != className) {
  5022. this.className = className;
  5023. dom.point.className = 'item point' + className;
  5024. changed = true;
  5025. }
  5026. }
  5027. return changed;
  5028. };
  5029. /**
  5030. * Show the item in the DOM (when not already visible). The items DOM will
  5031. * be created when needed.
  5032. * @return {Boolean} changed
  5033. */
  5034. ItemPoint.prototype.show = function show() {
  5035. if (!this.dom || !this.dom.point.parentNode) {
  5036. return this.repaint();
  5037. }
  5038. else {
  5039. return false;
  5040. }
  5041. };
  5042. /**
  5043. * Hide the item from the DOM (when visible)
  5044. * @return {Boolean} changed
  5045. */
  5046. ItemPoint.prototype.hide = function hide() {
  5047. var changed = false,
  5048. dom = this.dom;
  5049. if (dom) {
  5050. if (dom.point.parentNode) {
  5051. dom.point.parentNode.removeChild(dom.point);
  5052. changed = true;
  5053. }
  5054. }
  5055. return changed;
  5056. };
  5057. /**
  5058. * Reflow the item: calculate its actual size from the DOM
  5059. * @return {boolean} resized returns true if the axis is resized
  5060. * @override
  5061. */
  5062. ItemPoint.prototype.reflow = function reflow() {
  5063. var changed = 0,
  5064. update,
  5065. dom,
  5066. props,
  5067. options,
  5068. margin,
  5069. orientation,
  5070. start,
  5071. top,
  5072. data,
  5073. range;
  5074. if (this.data.start == undefined) {
  5075. throw new Error('Property "start" missing in item ' + this.data.id);
  5076. }
  5077. data = this.data;
  5078. range = this.parent && this.parent.range;
  5079. if (data && range) {
  5080. // TODO: account for the width of the item. Take some margin
  5081. this.visible = (data.start > range.start) && (data.start < range.end);
  5082. }
  5083. else {
  5084. this.visible = false;
  5085. }
  5086. if (this.visible) {
  5087. dom = this.dom;
  5088. if (dom) {
  5089. update = util.updateProperty;
  5090. props = this.props;
  5091. options = this.options;
  5092. orientation = options.orientation || this.defaultOptions.orientation;
  5093. margin = options.margin && options.margin.axis || this.defaultOptions.margin.axis;
  5094. start = this.parent.toScreen(this.data.start);
  5095. changed += update(this, 'width', dom.point.offsetWidth);
  5096. changed += update(this, 'height', dom.point.offsetHeight);
  5097. changed += update(props.dot, 'width', dom.dot.offsetWidth);
  5098. changed += update(props.dot, 'height', dom.dot.offsetHeight);
  5099. changed += update(props.content, 'height', dom.content.offsetHeight);
  5100. if (orientation == 'top') {
  5101. top = margin;
  5102. }
  5103. else {
  5104. // default or 'bottom'
  5105. var parentHeight = this.parent.height;
  5106. top = Math.max(parentHeight - this.height - margin, 0);
  5107. }
  5108. changed += update(this, 'top', top);
  5109. changed += update(this, 'left', start - props.dot.width / 2);
  5110. changed += update(props.content, 'marginLeft', 1.5 * props.dot.width);
  5111. //changed += update(props.content, 'marginRight', 0.5 * props.dot.width); // TODO
  5112. changed += update(props.dot, 'top', (this.height - props.dot.height) / 2);
  5113. }
  5114. else {
  5115. changed += 1;
  5116. }
  5117. }
  5118. return (changed > 0);
  5119. };
  5120. /**
  5121. * Create an items DOM
  5122. * @private
  5123. */
  5124. ItemPoint.prototype._create = function _create() {
  5125. var dom = this.dom;
  5126. if (!dom) {
  5127. this.dom = dom = {};
  5128. // background box
  5129. dom.point = document.createElement('div');
  5130. // className is updated in repaint()
  5131. // contents box, right from the dot
  5132. dom.content = document.createElement('div');
  5133. dom.content.className = 'content';
  5134. dom.point.appendChild(dom.content);
  5135. // dot at start
  5136. dom.dot = document.createElement('div');
  5137. dom.dot.className = 'dot';
  5138. dom.point.appendChild(dom.dot);
  5139. }
  5140. };
  5141. /**
  5142. * Reposition the item, recalculate its left, top, and width, using the current
  5143. * range and size of the items itemset
  5144. * @override
  5145. */
  5146. ItemPoint.prototype.reposition = function reposition() {
  5147. var dom = this.dom,
  5148. props = this.props;
  5149. if (dom) {
  5150. dom.point.style.top = this.top + 'px';
  5151. dom.point.style.left = this.left + 'px';
  5152. dom.content.style.marginLeft = props.content.marginLeft + 'px';
  5153. //dom.content.style.marginRight = props.content.marginRight + 'px'; // TODO
  5154. dom.dot.style.top = props.dot.top + 'px';
  5155. }
  5156. };
  5157. /**
  5158. * @constructor ItemRange
  5159. * @extends Item
  5160. * @param {ItemSet} parent
  5161. * @param {Object} data Object containing parameters start, end
  5162. * content, className.
  5163. * @param {Object} [options] Options to set initial property values
  5164. * @param {Object} [defaultOptions] default options
  5165. * // TODO: describe available options
  5166. */
  5167. function ItemRange (parent, data, options, defaultOptions) {
  5168. this.props = {
  5169. content: {
  5170. left: 0,
  5171. width: 0
  5172. }
  5173. };
  5174. Item.call(this, parent, data, options, defaultOptions);
  5175. }
  5176. ItemRange.prototype = new Item (null, null);
  5177. /**
  5178. * Select the item
  5179. * @override
  5180. */
  5181. ItemRange.prototype.select = function select() {
  5182. this.selected = true;
  5183. // TODO: select and unselect
  5184. };
  5185. /**
  5186. * Unselect the item
  5187. * @override
  5188. */
  5189. ItemRange.prototype.unselect = function unselect() {
  5190. this.selected = false;
  5191. // TODO: select and unselect
  5192. };
  5193. /**
  5194. * Repaint the item
  5195. * @return {Boolean} changed
  5196. */
  5197. ItemRange.prototype.repaint = function repaint() {
  5198. // TODO: make an efficient repaint
  5199. var changed = false;
  5200. var dom = this.dom;
  5201. if (!dom) {
  5202. this._create();
  5203. dom = this.dom;
  5204. changed = true;
  5205. }
  5206. if (dom) {
  5207. if (!this.parent) {
  5208. throw new Error('Cannot repaint item: no parent attached');
  5209. }
  5210. var foreground = this.parent.getForeground();
  5211. if (!foreground) {
  5212. throw new Error('Cannot repaint time axis: ' +
  5213. 'parent has no foreground container element');
  5214. }
  5215. if (!dom.box.parentNode) {
  5216. foreground.appendChild(dom.box);
  5217. changed = true;
  5218. }
  5219. // update content
  5220. if (this.data.content != this.content) {
  5221. this.content = this.data.content;
  5222. if (this.content instanceof Element) {
  5223. dom.content.innerHTML = '';
  5224. dom.content.appendChild(this.content);
  5225. }
  5226. else if (this.data.content != undefined) {
  5227. dom.content.innerHTML = this.content;
  5228. }
  5229. else {
  5230. throw new Error('Property "content" missing in item ' + this.data.id);
  5231. }
  5232. changed = true;
  5233. }
  5234. // update class
  5235. var className = this.data.className ? ('' + this.data.className) : '';
  5236. if (this.className != className) {
  5237. this.className = className;
  5238. dom.box.className = 'item range' + className;
  5239. changed = true;
  5240. }
  5241. }
  5242. return changed;
  5243. };
  5244. /**
  5245. * Show the item in the DOM (when not already visible). The items DOM will
  5246. * be created when needed.
  5247. * @return {Boolean} changed
  5248. */
  5249. ItemRange.prototype.show = function show() {
  5250. if (!this.dom || !this.dom.box.parentNode) {
  5251. return this.repaint();
  5252. }
  5253. else {
  5254. return false;
  5255. }
  5256. };
  5257. /**
  5258. * Hide the item from the DOM (when visible)
  5259. * @return {Boolean} changed
  5260. */
  5261. ItemRange.prototype.hide = function hide() {
  5262. var changed = false,
  5263. dom = this.dom;
  5264. if (dom) {
  5265. if (dom.box.parentNode) {
  5266. dom.box.parentNode.removeChild(dom.box);
  5267. changed = true;
  5268. }
  5269. }
  5270. return changed;
  5271. };
  5272. /**
  5273. * Reflow the item: calculate its actual size from the DOM
  5274. * @return {boolean} resized returns true if the axis is resized
  5275. * @override
  5276. */
  5277. ItemRange.prototype.reflow = function reflow() {
  5278. var changed = 0,
  5279. dom,
  5280. props,
  5281. options,
  5282. margin,
  5283. padding,
  5284. parent,
  5285. start,
  5286. end,
  5287. data,
  5288. range,
  5289. update,
  5290. box,
  5291. parentWidth,
  5292. contentLeft,
  5293. orientation,
  5294. top;
  5295. if (this.data.start == undefined) {
  5296. throw new Error('Property "start" missing in item ' + this.data.id);
  5297. }
  5298. if (this.data.end == undefined) {
  5299. throw new Error('Property "end" missing in item ' + this.data.id);
  5300. }
  5301. data = this.data;
  5302. range = this.parent && this.parent.range;
  5303. if (data && range) {
  5304. // TODO: account for the width of the item. Take some margin
  5305. this.visible = (data.start < range.end) && (data.end > range.start);
  5306. }
  5307. else {
  5308. this.visible = false;
  5309. }
  5310. if (this.visible) {
  5311. dom = this.dom;
  5312. if (dom) {
  5313. props = this.props;
  5314. options = this.options;
  5315. parent = this.parent;
  5316. start = parent.toScreen(this.data.start);
  5317. end = parent.toScreen(this.data.end);
  5318. update = util.updateProperty;
  5319. box = dom.box;
  5320. parentWidth = parent.width;
  5321. orientation = options.orientation || this.defaultOptions.orientation;
  5322. margin = options.margin && options.margin.axis || this.defaultOptions.margin.axis;
  5323. padding = options.padding || this.defaultOptions.padding;
  5324. changed += update(props.content, 'width', dom.content.offsetWidth);
  5325. changed += update(this, 'height', box.offsetHeight);
  5326. // limit the width of the this, as browsers cannot draw very wide divs
  5327. if (start < -parentWidth) {
  5328. start = -parentWidth;
  5329. }
  5330. if (end > 2 * parentWidth) {
  5331. end = 2 * parentWidth;
  5332. }
  5333. // when range exceeds left of the window, position the contents at the left of the visible area
  5334. if (start < 0) {
  5335. contentLeft = Math.min(-start,
  5336. (end - start - props.content.width - 2 * padding));
  5337. // TODO: remove the need for options.padding. it's terrible.
  5338. }
  5339. else {
  5340. contentLeft = 0;
  5341. }
  5342. changed += update(props.content, 'left', contentLeft);
  5343. if (orientation == 'top') {
  5344. top = margin;
  5345. changed += update(this, 'top', top);
  5346. }
  5347. else {
  5348. // default or 'bottom'
  5349. top = parent.height - this.height - margin;
  5350. changed += update(this, 'top', top);
  5351. }
  5352. changed += update(this, 'left', start);
  5353. changed += update(this, 'width', Math.max(end - start, 1)); // TODO: reckon with border width;
  5354. }
  5355. else {
  5356. changed += 1;
  5357. }
  5358. }
  5359. return (changed > 0);
  5360. };
  5361. /**
  5362. * Create an items DOM
  5363. * @private
  5364. */
  5365. ItemRange.prototype._create = function _create() {
  5366. var dom = this.dom;
  5367. if (!dom) {
  5368. this.dom = dom = {};
  5369. // background box
  5370. dom.box = document.createElement('div');
  5371. // className is updated in repaint()
  5372. // contents box
  5373. dom.content = document.createElement('div');
  5374. dom.content.className = 'content';
  5375. dom.box.appendChild(dom.content);
  5376. }
  5377. };
  5378. /**
  5379. * Reposition the item, recalculate its left, top, and width, using the current
  5380. * range and size of the items itemset
  5381. * @override
  5382. */
  5383. ItemRange.prototype.reposition = function reposition() {
  5384. var dom = this.dom,
  5385. props = this.props;
  5386. if (dom) {
  5387. dom.box.style.top = this.top + 'px';
  5388. dom.box.style.left = this.left + 'px';
  5389. dom.box.style.width = this.width + 'px';
  5390. dom.content.style.left = props.content.left + 'px';
  5391. }
  5392. };
  5393. /**
  5394. * @constructor Group
  5395. * @param {GroupSet} parent
  5396. * @param {Number | String} groupId
  5397. * @param {Object} [options] Options to set initial property values
  5398. * // TODO: describe available options
  5399. * @extends Component
  5400. */
  5401. function Group (parent, groupId, options) {
  5402. this.id = util.randomUUID();
  5403. this.parent = parent;
  5404. this.groupId = groupId;
  5405. this.itemsData = null; // DataSet
  5406. this.itemset = null; // ItemSet
  5407. this.options = options || {};
  5408. this.options.top = 0;
  5409. this.top = 0;
  5410. this.left = 0;
  5411. this.width = 0;
  5412. this.height = 0;
  5413. }
  5414. Group.prototype = new Component();
  5415. // TODO: comment
  5416. Group.prototype.setOptions = Component.prototype.setOptions;
  5417. /**
  5418. * Get the container element of the panel, which can be used by a child to
  5419. * add its own widgets.
  5420. * @returns {HTMLElement} container
  5421. */
  5422. Group.prototype.getContainer = function () {
  5423. return this.parent.getContainer();
  5424. };
  5425. /**
  5426. * Set item set for the group. The group will create a view on the itemset,
  5427. * filtered by the groups id.
  5428. * @param {DataSet | DataView} items
  5429. */
  5430. Group.prototype.setItems = function setItems(items) {
  5431. if (this.itemset) {
  5432. // remove current item set
  5433. this.itemset.hide();
  5434. this.itemset.setItems();
  5435. this.parent.controller.remove(this.itemset);
  5436. this.itemset = null;
  5437. }
  5438. if (items) {
  5439. var groupId = this.groupId;
  5440. var itemsetOptions = Object.create(this.options);
  5441. this.itemset = new ItemSet(this, null, itemsetOptions);
  5442. this.itemset.setRange(this.parent.range);
  5443. this.view = new DataView(items, {
  5444. filter: function (item) {
  5445. return item.group == groupId;
  5446. }
  5447. });
  5448. this.itemset.setItems(this.view);
  5449. this.parent.controller.add(this.itemset);
  5450. }
  5451. };
  5452. /**
  5453. * Repaint the item
  5454. * @return {Boolean} changed
  5455. */
  5456. Group.prototype.repaint = function repaint() {
  5457. return false;
  5458. };
  5459. /**
  5460. * Reflow the item
  5461. * @return {Boolean} resized
  5462. */
  5463. Group.prototype.reflow = function reflow() {
  5464. var changed = 0,
  5465. update = util.updateProperty;
  5466. changed += update(this, 'top', this.itemset ? this.itemset.top : 0);
  5467. changed += update(this, 'height', this.itemset ? this.itemset.height : 0);
  5468. return (changed > 0);
  5469. };
  5470. /**
  5471. * An GroupSet holds a set of groups
  5472. * @param {Component} parent
  5473. * @param {Component[]} [depends] Components on which this components depends
  5474. * (except for the parent)
  5475. * @param {Object} [options] See GroupSet.setOptions for the available
  5476. * options.
  5477. * @constructor GroupSet
  5478. * @extends Panel
  5479. */
  5480. function GroupSet(parent, depends, options) {
  5481. this.id = util.randomUUID();
  5482. this.parent = parent;
  5483. this.depends = depends;
  5484. this.options = options || {};
  5485. this.range = null; // Range or Object {start: number, end: number}
  5486. this.itemsData = null; // DataSet with items
  5487. this.groupsData = null; // DataSet with groups
  5488. this.groups = {}; // map with groups
  5489. // changes in groups are queued key/value map containing id/action
  5490. this.queue = {};
  5491. var me = this;
  5492. this.listeners = {
  5493. 'add': function (event, params) {
  5494. me._onAdd(params.items);
  5495. },
  5496. 'update': function (event, params) {
  5497. me._onUpdate(params.items);
  5498. },
  5499. 'remove': function (event, params) {
  5500. me._onRemove(params.items);
  5501. }
  5502. };
  5503. }
  5504. GroupSet.prototype = new Panel();
  5505. /**
  5506. * Set options for the GroupSet. Existing options will be extended/overwritten.
  5507. * @param {Object} [options] The following options are available:
  5508. * {String | function} groupsOrder
  5509. * TODO: describe options
  5510. */
  5511. GroupSet.prototype.setOptions = Component.prototype.setOptions;
  5512. GroupSet.prototype.setRange = function (range) {
  5513. // TODO: implement setRange
  5514. };
  5515. /**
  5516. * Set items
  5517. * @param {vis.DataSet | null} items
  5518. */
  5519. GroupSet.prototype.setItems = function setItems(items) {
  5520. this.itemsData = items;
  5521. for (var id in this.groups) {
  5522. if (this.groups.hasOwnProperty(id)) {
  5523. var group = this.groups[id];
  5524. group.setItems(items);
  5525. }
  5526. }
  5527. };
  5528. /**
  5529. * Get items
  5530. * @return {vis.DataSet | null} items
  5531. */
  5532. GroupSet.prototype.getItems = function getItems() {
  5533. return this.itemsData;
  5534. };
  5535. /**
  5536. * Set range (start and end).
  5537. * @param {Range | Object} range A Range or an object containing start and end.
  5538. */
  5539. GroupSet.prototype.setRange = function setRange(range) {
  5540. this.range = range;
  5541. };
  5542. /**
  5543. * Set groups
  5544. * @param {vis.DataSet} groups
  5545. */
  5546. GroupSet.prototype.setGroups = function setGroups(groups) {
  5547. var me = this,
  5548. ids;
  5549. // unsubscribe from current dataset
  5550. if (this.groupsData) {
  5551. util.forEach(this.listeners, function (callback, event) {
  5552. me.groupsData.unsubscribe(event, callback);
  5553. });
  5554. // remove all drawn groups
  5555. ids = this.groupsData.getIds();
  5556. this._onRemove(ids);
  5557. }
  5558. // replace the dataset
  5559. if (!groups) {
  5560. this.groupsData = null;
  5561. }
  5562. else if (groups instanceof DataSet) {
  5563. this.groupsData = groups;
  5564. }
  5565. else {
  5566. this.groupsData = new DataSet({
  5567. fieldTypes: {
  5568. start: 'Date',
  5569. end: 'Date'
  5570. }
  5571. });
  5572. this.groupsData.add(groups);
  5573. }
  5574. if (this.groupsData) {
  5575. // subscribe to new dataset
  5576. var id = this.id;
  5577. util.forEach(this.listeners, function (callback, event) {
  5578. me.groupsData.subscribe(event, callback, id);
  5579. });
  5580. // draw all new groups
  5581. ids = this.groupsData.getIds();
  5582. this._onAdd(ids);
  5583. }
  5584. };
  5585. /**
  5586. * Get groups
  5587. * @return {vis.DataSet | null} groups
  5588. */
  5589. GroupSet.prototype.getGroups = function getGroups() {
  5590. return this.groupsData;
  5591. };
  5592. /**
  5593. * Repaint the component
  5594. * @return {Boolean} changed
  5595. */
  5596. GroupSet.prototype.repaint = function repaint() {
  5597. var changed = 0,
  5598. update = util.updateProperty,
  5599. asSize = util.option.asSize,
  5600. options = this.options,
  5601. frame = this.frame;
  5602. if (!frame) {
  5603. frame = document.createElement('div');
  5604. frame.className = 'groupset';
  5605. var className = options.className;
  5606. if (className) {
  5607. util.addClassName(frame, util.option.asString(className));
  5608. }
  5609. this.frame = frame;
  5610. changed += 1;
  5611. }
  5612. if (!this.parent) {
  5613. throw new Error('Cannot repaint groupset: no parent attached');
  5614. }
  5615. var parentContainer = this.parent.getContainer();
  5616. if (!parentContainer) {
  5617. throw new Error('Cannot repaint groupset: parent has no container element');
  5618. }
  5619. if (!frame.parentNode) {
  5620. parentContainer.appendChild(frame);
  5621. changed += 1;
  5622. }
  5623. // reposition frame
  5624. changed += update(frame.style, 'height', asSize(options.height, this.height + 'px'));
  5625. changed += update(frame.style, 'top', asSize(options.top, '0px'));
  5626. changed += update(frame.style, 'left', asSize(options.left, '0px'));
  5627. changed += update(frame.style, 'width', asSize(options.width, '100%'));
  5628. var me = this,
  5629. queue = this.queue,
  5630. groups = this.groups,
  5631. groupsData = this.groupsData;
  5632. // show/hide added/changed/removed items
  5633. var ids = Object.keys(queue);
  5634. if (ids.length) {
  5635. ids.forEach(function (id) {
  5636. var action = queue[id];
  5637. var group = groups[id];
  5638. //noinspection FallthroughInSwitchStatementJS
  5639. switch (action) {
  5640. case 'add':
  5641. case 'update':
  5642. if (!group) {
  5643. var groupOptions = Object.create(me.options);
  5644. group = new Group(me, id, groupOptions);
  5645. group.setItems(me.itemsData); // attach items data
  5646. groups[id] = group;
  5647. me.controller.add(group);
  5648. }
  5649. // TODO: update group data
  5650. group.data = groupsData.get(id);
  5651. delete queue[id];
  5652. break;
  5653. case 'remove':
  5654. if (group) {
  5655. group.setItems(); // detach items data
  5656. delete groups[id];
  5657. me.controller.remove(group);
  5658. }
  5659. // update lists
  5660. delete queue[id];
  5661. break;
  5662. default:
  5663. console.log('Error: unknown action "' + action + '"');
  5664. }
  5665. });
  5666. // the groupset depends on each of the groups
  5667. //this.depends = this.groups; // TODO: gives a circular reference through the parent
  5668. // TODO: apply dependencies of the groupset
  5669. // update the top positions of the groups in the correct order
  5670. var orderedGroups = this.groupsData.getIds({
  5671. order: this.options.groupsOrder
  5672. });
  5673. for (var i = 0; i < orderedGroups.length; i++) {
  5674. (function (group, prevGroup) {
  5675. var top = 0;
  5676. if (prevGroup) {
  5677. top = function () {
  5678. // TODO: top must reckon with options.maxHeight
  5679. return prevGroup.top + prevGroup.height;
  5680. }
  5681. }
  5682. group.setOptions({
  5683. top: top
  5684. });
  5685. })(groups[orderedGroups[i]], groups[orderedGroups[i - 1]]);
  5686. }
  5687. changed++;
  5688. }
  5689. return (changed > 0);
  5690. };
  5691. /**
  5692. * Get container element
  5693. * @return {HTMLElement} container
  5694. */
  5695. GroupSet.prototype.getContainer = function getContainer() {
  5696. // TODO: replace later on with container element for holding itemsets
  5697. return this.frame;
  5698. };
  5699. /**
  5700. * Reflow the component
  5701. * @return {Boolean} resized
  5702. */
  5703. GroupSet.prototype.reflow = function reflow() {
  5704. var changed = 0,
  5705. options = this.options,
  5706. update = util.updateProperty,
  5707. asNumber = util.option.asNumber,
  5708. asSize = util.option.asSize,
  5709. frame = this.frame;
  5710. if (frame) {
  5711. var maxHeight = asNumber(options.maxHeight);
  5712. var fixedHeight = (asSize(options.height) != null);
  5713. var height;
  5714. if (fixedHeight) {
  5715. height = frame.offsetHeight;
  5716. }
  5717. else {
  5718. // height is not specified, calculate the sum of the height of all groups
  5719. height = 0;
  5720. for (var id in this.groups) {
  5721. if (this.groups.hasOwnProperty(id)) {
  5722. var group = this.groups[id];
  5723. height += group.height;
  5724. }
  5725. }
  5726. }
  5727. if (maxHeight != null) {
  5728. height = Math.min(height, maxHeight);
  5729. }
  5730. changed += update(this, 'height', height);
  5731. changed += update(this, 'top', frame.offsetTop);
  5732. changed += update(this, 'left', frame.offsetLeft);
  5733. changed += update(this, 'width', frame.offsetWidth);
  5734. }
  5735. return (changed > 0);
  5736. };
  5737. /**
  5738. * Hide the component from the DOM
  5739. * @return {Boolean} changed
  5740. */
  5741. GroupSet.prototype.hide = function hide() {
  5742. if (this.frame && this.frame.parentNode) {
  5743. this.frame.parentNode.removeChild(this.frame);
  5744. return true;
  5745. }
  5746. else {
  5747. return false;
  5748. }
  5749. };
  5750. /**
  5751. * Show the component in the DOM (when not already visible).
  5752. * A repaint will be executed when the component is not visible
  5753. * @return {Boolean} changed
  5754. */
  5755. GroupSet.prototype.show = function show() {
  5756. if (!this.frame || !this.frame.parentNode) {
  5757. return this.repaint();
  5758. }
  5759. else {
  5760. return false;
  5761. }
  5762. };
  5763. /**
  5764. * Handle updated groups
  5765. * @param {Number[]} ids
  5766. * @private
  5767. */
  5768. GroupSet.prototype._onUpdate = function _onUpdate(ids) {
  5769. this._toQueue(ids, 'update');
  5770. };
  5771. /**
  5772. * Handle changed groups
  5773. * @param {Number[]} ids
  5774. * @private
  5775. */
  5776. GroupSet.prototype._onAdd = function _onAdd(ids) {
  5777. this._toQueue(ids, 'add');
  5778. };
  5779. /**
  5780. * Handle removed groups
  5781. * @param {Number[]} ids
  5782. * @private
  5783. */
  5784. GroupSet.prototype._onRemove = function _onRemove(ids) {
  5785. this._toQueue(ids, 'remove');
  5786. };
  5787. /**
  5788. * Put groups in the queue to be added/updated/remove
  5789. * @param {Number[]} ids
  5790. * @param {String} action can be 'add', 'update', 'remove'
  5791. */
  5792. GroupSet.prototype._toQueue = function _toQueue(ids, action) {
  5793. var queue = this.queue;
  5794. ids.forEach(function (id) {
  5795. queue[id] = action;
  5796. });
  5797. if (this.controller) {
  5798. //this.requestReflow();
  5799. this.requestRepaint();
  5800. }
  5801. };
  5802. /**
  5803. * Create a timeline visualization
  5804. * @param {HTMLElement} container
  5805. * @param {vis.DataSet | Array | DataTable} [items]
  5806. * @param {Object} [options] See Timeline.setOptions for the available options.
  5807. * @constructor
  5808. */
  5809. function Timeline (container, items, options) {
  5810. var me = this;
  5811. this.options = util.extend({
  5812. orientation: 'bottom',
  5813. min: null,
  5814. max: null,
  5815. zoomMin: 10, // milliseconds
  5816. zoomMax: 1000 * 60 * 60 * 24 * 365 * 10000, // milliseconds
  5817. moveable: true,
  5818. zoomable: true,
  5819. showMinorLabels: true,
  5820. showMajorLabels: true,
  5821. autoResize: false
  5822. }, options);
  5823. // controller
  5824. this.controller = new Controller();
  5825. // root panel
  5826. if (!container) {
  5827. throw new Error('No container element provided');
  5828. }
  5829. var mainOptions = Object.create(this.options);
  5830. mainOptions.height = function () {
  5831. if (me.options.height) {
  5832. // fixed height
  5833. return me.options.height;
  5834. }
  5835. else {
  5836. // auto height
  5837. return me.timeaxis.height + me.content.height;
  5838. }
  5839. };
  5840. this.root = new RootPanel(container, mainOptions);
  5841. this.controller.add(this.root);
  5842. // range
  5843. var now = moment().hours(0).minutes(0).seconds(0).milliseconds(0);
  5844. this.range = new Range({
  5845. start: now.clone().add('days', -3).valueOf(),
  5846. end: now.clone().add('days', 4).valueOf()
  5847. });
  5848. // TODO: reckon with options moveable and zoomable
  5849. this.range.subscribe(this.root, 'move', 'horizontal');
  5850. this.range.subscribe(this.root, 'zoom', 'horizontal');
  5851. this.range.on('rangechange', function () {
  5852. var force = true;
  5853. me.controller.requestReflow(force);
  5854. });
  5855. this.range.on('rangechanged', function () {
  5856. var force = true;
  5857. me.controller.requestReflow(force);
  5858. });
  5859. // TODO: put the listeners in setOptions, be able to dynamically change with options moveable and zoomable
  5860. // time axis
  5861. var timeaxisOptions = Object.create(mainOptions);
  5862. timeaxisOptions.range = this.range;
  5863. this.timeaxis = new TimeAxis(this.root, [], timeaxisOptions);
  5864. this.timeaxis.setRange(this.range);
  5865. this.controller.add(this.timeaxis);
  5866. // create itemset or groupset
  5867. this.setGroups(null);
  5868. this.itemsData = null; // DataSet
  5869. this.groupsData = null; // DataSet
  5870. // set data
  5871. if (items) {
  5872. this.setItems(items);
  5873. }
  5874. }
  5875. /**
  5876. * Set options
  5877. * @param {Object} options TODO: describe the available options
  5878. */
  5879. Timeline.prototype.setOptions = function (options) {
  5880. if (options) {
  5881. util.extend(this.options, options);
  5882. }
  5883. this.controller.reflow();
  5884. this.controller.repaint();
  5885. };
  5886. /**
  5887. * Set items
  5888. * @param {vis.DataSet | Array | DataTable | null} items
  5889. */
  5890. Timeline.prototype.setItems = function(items) {
  5891. var initialLoad = (this.itemsData == null);
  5892. // convert to type DataSet when needed
  5893. var newItemSet;
  5894. if (!items) {
  5895. newItemSet = null;
  5896. }
  5897. else if (items instanceof DataSet) {
  5898. newItemSet = items;
  5899. }
  5900. if (!(items instanceof DataSet)) {
  5901. newItemSet = new DataSet({
  5902. fieldTypes: {
  5903. start: 'Date',
  5904. end: 'Date'
  5905. }
  5906. });
  5907. newItemSet.add(items);
  5908. }
  5909. // set items
  5910. this.itemsData = newItemSet;
  5911. this.content.setItems(newItemSet);
  5912. if (initialLoad && (this.options.start == undefined || this.options.end == undefined)) {
  5913. // apply the data range as range
  5914. var dataRange = this.getItemRange();
  5915. // add 5% on both sides
  5916. var min = dataRange.min;
  5917. var max = dataRange.max;
  5918. if (min != null && max != null) {
  5919. var interval = (max.valueOf() - min.valueOf());
  5920. min = new Date(min.valueOf() - interval * 0.05);
  5921. max = new Date(max.valueOf() + interval * 0.05);
  5922. }
  5923. // override specified start and/or end date
  5924. if (this.options.start != undefined) {
  5925. min = new Date(this.options.start.valueOf());
  5926. }
  5927. if (this.options.end != undefined) {
  5928. max = new Date(this.options.end.valueOf());
  5929. }
  5930. // apply range if there is a min or max available
  5931. if (min != null || max != null) {
  5932. this.range.setRange(min, max);
  5933. }
  5934. }
  5935. };
  5936. /**
  5937. * Set groups
  5938. * @param {vis.DataSet | Array | DataTable} groups
  5939. */
  5940. Timeline.prototype.setGroups = function(groups) {
  5941. var me = this;
  5942. this.groupsData = groups;
  5943. // switch content type between ItemSet or GroupSet when needed
  5944. var type = this.groupsData ? GroupSet : ItemSet;
  5945. if (!(this.content instanceof type)) {
  5946. // remove old content set
  5947. if (this.content) {
  5948. this.content.hide();
  5949. if (this.content.setItems) {
  5950. this.content.setItems(); // disconnect from items
  5951. }
  5952. if (this.content.setGroups) {
  5953. this.content.setGroups(); // disconnect from groups
  5954. }
  5955. this.controller.remove(this.content);
  5956. }
  5957. // create new content set
  5958. var options = Object.create(this.options);
  5959. util.extend(options, {
  5960. top: function () {
  5961. if (me.options.orientation == 'top') {
  5962. return me.timeaxis.height;
  5963. }
  5964. else {
  5965. return me.root.height - me.timeaxis.height - me.content.height;
  5966. }
  5967. },
  5968. height: function () {
  5969. if (me.options.height) {
  5970. return me.root.height - me.timeaxis.height;
  5971. }
  5972. else {
  5973. return null;
  5974. }
  5975. },
  5976. maxHeight: function () {
  5977. if (me.options.maxHeight) {
  5978. if (!util.isNumber(me.options.maxHeight)) {
  5979. throw new TypeError('Number expected for property maxHeight');
  5980. }
  5981. return me.options.maxHeight - me.timeaxis.height;
  5982. }
  5983. else {
  5984. return null;
  5985. }
  5986. }
  5987. });
  5988. this.content = new type(this.root, [this.timeaxis], options);
  5989. if (this.content.setRange) {
  5990. this.content.setRange(this.range);
  5991. }
  5992. if (this.content.setItems) {
  5993. this.content.setItems(this.itemsData);
  5994. }
  5995. if (this.content.setGroups) {
  5996. this.content.setGroups(this.groupsData);
  5997. }
  5998. this.controller.add(this.content);
  5999. }
  6000. };
  6001. /**
  6002. * Get the data range of the item set.
  6003. * @returns {{min: Date, max: Date}} range A range with a start and end Date.
  6004. * When no minimum is found, min==null
  6005. * When no maximum is found, max==null
  6006. */
  6007. Timeline.prototype.getItemRange = function getItemRange() {
  6008. // calculate min from start filed
  6009. var itemsData = this.itemsData,
  6010. min = null,
  6011. max = null;
  6012. if (itemsData) {
  6013. // calculate the minimum value of the field 'start'
  6014. var minItem = itemsData.min('start');
  6015. min = minItem ? minItem.start.valueOf() : null;
  6016. // calculate maximum value of fields 'start' and 'end'
  6017. var maxStartItem = itemsData.max('start');
  6018. if (maxStartItem) {
  6019. max = maxStartItem.start.valueOf();
  6020. }
  6021. var maxEndItem = itemsData.max('end');
  6022. if (maxEndItem) {
  6023. if (max == null) {
  6024. max = maxEndItem.end.valueOf();
  6025. }
  6026. else {
  6027. max = Math.max(max, maxEndItem.end.valueOf());
  6028. }
  6029. }
  6030. }
  6031. return {
  6032. min: (min != null) ? new Date(min) : null,
  6033. max: (max != null) ? new Date(max) : null
  6034. };
  6035. };
  6036. /**
  6037. * @constructor Graph
  6038. * Create a graph visualization connecting nodes via edges.
  6039. * @param {Element} container The DOM element in which the Graph will
  6040. * be created. Normally a div element.
  6041. * @param {Object} data An object containing parameters
  6042. * {Array} nodes
  6043. * {Array} edges
  6044. * @param {Object} options Options
  6045. */
  6046. function Graph (container, data, options) {
  6047. // create variables and set default values
  6048. this.containerElement = container;
  6049. this.width = "100%";
  6050. this.height = "100%";
  6051. this.refreshRate = 50; // milliseconds
  6052. this.stabilize = true; // stabilize before displaying the network
  6053. this.selectable = true;
  6054. // set constant values
  6055. this.constants = {
  6056. "nodes": {
  6057. "radiusMin": 5,
  6058. "radiusMax": 20,
  6059. "radius": 5,
  6060. "distance": 100, // px
  6061. "style": "rect",
  6062. "image": undefined,
  6063. "widthMin": 16, // px
  6064. "widthMax": 64, // px
  6065. "fontColor": "black",
  6066. "fontSize": 14, // px
  6067. //"fontFace": "verdana",
  6068. "fontFace": "arial",
  6069. "borderColor": "#2B7CE9",
  6070. "backgroundColor": "#97C2FC",
  6071. "highlightColor": "#D2E5FF",
  6072. "group": undefined
  6073. },
  6074. "edges": {
  6075. "widthMin": 1,
  6076. "widthMax": 15,
  6077. "width": 1,
  6078. "style": "line",
  6079. "color": "#343434",
  6080. "fontColor": "#343434",
  6081. "fontSize": 14, // px
  6082. "fontFace": "arial",
  6083. //"distance": 100, //px
  6084. "length": 100, // px
  6085. "dashlength": 10,
  6086. "dashgap": 5
  6087. },
  6088. "packages": {
  6089. "radius": 5,
  6090. "radiusMin": 5,
  6091. "radiusMax": 10,
  6092. "style": "dot",
  6093. "color": "#2B7CE9",
  6094. "image": undefined,
  6095. "widthMin": 16, // px
  6096. "widthMax": 64, // px
  6097. "duration": 1.0 // seconds
  6098. },
  6099. "minForce": 0.05,
  6100. "minVelocity": 0.02, // px/s
  6101. "maxIterations": 1000 // maximum number of iteration to stabilize
  6102. };
  6103. this.nodes = []; // array with Node objects
  6104. this.edges = []; // array with Edge objects
  6105. this.packages = []; // array with all Package packages
  6106. this.images = new Graph.Images(); // object with images
  6107. this.groups = new Graph.Groups(); // object with groups
  6108. // properties of the data
  6109. this.hasMovingEdges = false; // True if one or more of the edges or nodes have an animation
  6110. this.hasMovingNodes = false; // True if any of the nodes have an undefined position
  6111. this.hasMovingPackages = false; // True if there are one or more packages
  6112. this.selection = [];
  6113. this.timer = undefined;
  6114. // create a frame and canvas
  6115. this._create();
  6116. // apply options
  6117. this.setOptions(options);
  6118. // draw data
  6119. this.setData(data);
  6120. }
  6121. /**
  6122. * Main drawing logic. This is the function that needs to be called
  6123. * in the html page, to draw the Network.
  6124. *
  6125. * A data table with the events must be provided, and an options table.
  6126. * @param {Object} data Object containing parameters:
  6127. * {Array} nodes Array with nodes
  6128. * {Array} edges Array with edges
  6129. * {Options} [options] Object with options
  6130. */
  6131. Graph.prototype.setData = function(data) {
  6132. if (data.options) {
  6133. this.setOptions(data.options);
  6134. }
  6135. // set all data
  6136. this.hasTimestamps = false;
  6137. this.setNodes(data.nodes);
  6138. this.setEdges(data.edges);
  6139. this.setPackages(data.packages);
  6140. this._reposition(); // TODO: bad solution
  6141. if (this.stabilize) {
  6142. this._doStabilize();
  6143. }
  6144. this.start();
  6145. // create an onload callback method for the images
  6146. var network = this;
  6147. var callback = function () {
  6148. network._redraw();
  6149. };
  6150. this.images.setOnloadCallback(callback);
  6151. // fire the ready event
  6152. this.trigger('ready');
  6153. };
  6154. /**
  6155. * Set options
  6156. * @param {Object} options
  6157. */
  6158. Graph.prototype.setOptions = function (options) {
  6159. if (options) {
  6160. // retrieve parameter values
  6161. if (options.width != undefined) {this.width = options.width;}
  6162. if (options.height != undefined) {this.height = options.height;}
  6163. if (options.stabilize != undefined) {this.stabilize = options.stabilize;}
  6164. if (options.selectable != undefined) {this.selectable = options.selectable;}
  6165. // TODO: work out these options and document them
  6166. if (options.edges) {
  6167. for (var prop in options.edges) {
  6168. if (options.edges.hasOwnProperty(prop)) {
  6169. this.constants.edges[prop] = options.edges[prop];
  6170. }
  6171. }
  6172. if (options.edges.length != undefined &&
  6173. options.nodes && options.nodes.distance == undefined) {
  6174. this.constants.edges.length = options.edges.length;
  6175. this.constants.nodes.distance = options.edges.length * 1.25;
  6176. }
  6177. if (!options.edges.fontColor) {
  6178. this.constants.edges.fontColor = options.edges.color;
  6179. }
  6180. // Added to support dashed lines
  6181. // David Jordan
  6182. // 2012-08-08
  6183. if (options.edges.dashlength != undefined) {
  6184. this.constants.edges.dashlength = options.edges.dashlength;
  6185. }
  6186. if (options.edges.dashgap != undefined) {
  6187. this.constants.edges.dashgap = options.edges.dashgap;
  6188. }
  6189. if (options.edges.altdashlength != undefined) {
  6190. this.constants.edges.altdashlength = options.edges.altdashlength;
  6191. }
  6192. }
  6193. if (options.nodes) {
  6194. for (prop in options.nodes) {
  6195. if (options.nodes.hasOwnProperty(prop)) {
  6196. this.constants.nodes[prop] = options.nodes[prop];
  6197. }
  6198. }
  6199. /*
  6200. if (options.nodes.widthMin) this.constants.nodes.radiusMin = options.nodes.widthMin;
  6201. if (options.nodes.widthMax) this.constants.nodes.radiusMax = options.nodes.widthMax;
  6202. */
  6203. }
  6204. if (options.packages) {
  6205. for (prop in options.packages) {
  6206. if (options.packages.hasOwnProperty(prop)) {
  6207. this.constants.packages[prop] = options.packages[prop];
  6208. }
  6209. }
  6210. /*
  6211. if (options.packages.widthMin) this.constants.packages.radiusMin = options.packages.widthMin;
  6212. if (options.packages.widthMax) this.constants.packages.radiusMax = options.packages.widthMax;
  6213. */
  6214. }
  6215. if (options.groups) {
  6216. for (var groupname in options.groups) {
  6217. if (options.groups.hasOwnProperty(groupname)) {
  6218. var group = options.groups[groupname];
  6219. this.groups.add(groupname, group);
  6220. }
  6221. }
  6222. }
  6223. this._setBackgroundColor(options.backgroundColor);
  6224. }
  6225. this._setSize(this.width, this.height);
  6226. this._setTranslation(0, 0);
  6227. this._setScale(1.0);
  6228. };
  6229. /**
  6230. * fire an event
  6231. * @param {String} event The name of an event, for example "select" or "ready"
  6232. * @param {Object} params Optional object with event parameters
  6233. */
  6234. Graph.prototype.trigger = function (event, params) {
  6235. // trigger the edges event bus
  6236. events.trigger(this, event, params);
  6237. // trigger the google event bus
  6238. if (typeof google !== 'undefined' && google.visualization && google.visualization.events) {
  6239. google.visualization.events.trigger(this, event, params);
  6240. }
  6241. };
  6242. /**
  6243. * Create the main frame for the Network.
  6244. * This function is executed once when a Network object is created. The frame
  6245. * contains a canvas, and this canvas contains all objects like the axis and
  6246. * nodes.
  6247. */
  6248. Graph.prototype._create = function () {
  6249. // remove all elements from the container element.
  6250. while (this.containerElement.hasChildNodes()) {
  6251. this.containerElement.removeChild(this.containerElement.firstChild);
  6252. }
  6253. this.frame = document.createElement("div");
  6254. this.frame.className = "network-frame";
  6255. this.frame.style.position = "relative";
  6256. this.frame.style.overflow = "hidden";
  6257. // create the graph canvas (HTML canvas element)
  6258. this.frame.canvas = document.createElement( "canvas" );
  6259. this.frame.canvas.style.position = "relative";
  6260. this.frame.appendChild(this.frame.canvas);
  6261. if (!this.frame.canvas.getContext) {
  6262. var noCanvas = document.createElement( "DIV" );
  6263. noCanvas.style.color = "red";
  6264. noCanvas.style.fontWeight = "bold" ;
  6265. noCanvas.style.padding = "10px";
  6266. noCanvas.innerHTML = "Error: your browser does not support HTML canvas";
  6267. this.frame.canvas.appendChild(noCanvas);
  6268. }
  6269. // create event listeners
  6270. var me = this;
  6271. var onmousedown = function (event) {me._onMouseDown(event);};
  6272. var onmousemove = function (event) {me._onMouseMoveTitle(event);};
  6273. var onmousewheel = function (event) {me._onMouseWheel(event);};
  6274. var ontouchstart = function (event) {me._onTouchStart(event);};
  6275. Graph.addEventListener(this.frame.canvas, "mousedown", onmousedown);
  6276. Graph.addEventListener(this.frame.canvas, "mousemove", onmousemove);
  6277. Graph.addEventListener(this.frame.canvas, "mousewheel", onmousewheel);
  6278. Graph.addEventListener(this.frame.canvas, "touchstart", ontouchstart);
  6279. // add the frame to the container element
  6280. this.containerElement.appendChild(this.frame);
  6281. };
  6282. /**
  6283. * Set the background and border styling for the graph
  6284. * @param {String | Object} backgroundColor
  6285. */
  6286. Graph.prototype._setBackgroundColor = function(backgroundColor) {
  6287. var fill = "white";
  6288. var stroke = "lightgray";
  6289. var strokeWidth = 1;
  6290. if (typeof(backgroundColor) == "string") {
  6291. fill = backgroundColor;
  6292. stroke = "none";
  6293. strokeWidth = 0;
  6294. }
  6295. else if (typeof(backgroundColor) == "object") {
  6296. if (backgroundColor.fill != undefined) fill = backgroundColor.fill;
  6297. if (backgroundColor.stroke != undefined) stroke = backgroundColor.stroke;
  6298. if (backgroundColor.strokeWidth != undefined) strokeWidth = backgroundColor.strokeWidth;
  6299. }
  6300. else if (backgroundColor == undefined) {
  6301. // use use defaults
  6302. }
  6303. else {
  6304. throw "Unsupported type of backgroundColor";
  6305. }
  6306. this.frame.style.boxSizing = 'border-box';
  6307. this.frame.style.backgroundColor = fill;
  6308. this.frame.style.borderColor = stroke;
  6309. this.frame.style.borderWidth = strokeWidth + "px";
  6310. this.frame.style.borderStyle = "solid";
  6311. };
  6312. /**
  6313. * handle on mouse down event
  6314. */
  6315. Graph.prototype._onMouseDown = function (event) {
  6316. event = event || window.event;
  6317. if (!this.selectable) {
  6318. return;
  6319. }
  6320. // check if mouse is still down (may be up when focus is lost for example
  6321. // in an iframe)
  6322. if (this.leftButtonDown) {
  6323. this._onMouseUp(event);
  6324. }
  6325. // only react on left mouse button down
  6326. this.leftButtonDown = event.which ? (event.which == 1) : (event.button == 1);
  6327. if (!this.leftButtonDown && !this.touchDown) {
  6328. return;
  6329. }
  6330. // add event listeners to handle moving the contents
  6331. // we store the function onmousemove and onmouseup in the timeline, so we can
  6332. // remove the eventlisteners lateron in the function mouseUp()
  6333. var me = this;
  6334. if (!this.onmousemove) {
  6335. this.onmousemove = function (event) {me._onMouseMove(event);};
  6336. Graph.addEventListener(document, "mousemove", me.onmousemove);
  6337. }
  6338. if (!this.onmouseup) {
  6339. this.onmouseup = function (event) {me._onMouseUp(event);};
  6340. Graph.addEventListener(document, "mouseup", me.onmouseup);
  6341. }
  6342. Graph.preventDefault(event);
  6343. // store the start x and y position of the mouse
  6344. this.startMouseX = event.clientX || event.targetTouches[0].clientX;
  6345. this.startMouseY = event.clientY || event.targetTouches[0].clientY;
  6346. this.startFrameLeft = Graph._getAbsoluteLeft(this.frame.canvas);
  6347. this.startFrameTop = Graph._getAbsoluteTop(this.frame.canvas);
  6348. this.startTranslation = this._getTranslation();
  6349. this.ctrlKeyDown = event.ctrlKey;
  6350. this.shiftKeyDown = event.shiftKey;
  6351. var obj = {
  6352. "left" : this._xToCanvas(this.startMouseX - this.startFrameLeft),
  6353. "top" : this._yToCanvas(this.startMouseY - this.startFrameTop),
  6354. "right" : this._xToCanvas(this.startMouseX - this.startFrameLeft),
  6355. "bottom" : this._yToCanvas(this.startMouseY - this.startFrameTop)
  6356. };
  6357. var overlappingNodes = this._getNodesOverlappingWith(obj);
  6358. // if there are overlapping nodes, select the last one, this is the
  6359. // one which is drawn on top of the others
  6360. this.startClickedObj = (overlappingNodes.length > 0) ?
  6361. overlappingNodes[overlappingNodes.length - 1] : undefined;
  6362. if (this.startClickedObj) {
  6363. // move clicked node with the mouse
  6364. // make the clicked node temporarily fixed, and store their original state
  6365. var node = this.nodes[this.startClickedObj.row];
  6366. this.startClickedObj.xFixed = node.xFixed;
  6367. this.startClickedObj.yFixed = node.yFixed;
  6368. node.xFixed = true;
  6369. node.yFixed = true;
  6370. if (!this.ctrlKeyDown || !node.isSelected()) {
  6371. // select this node
  6372. this._selectNodes([this.startClickedObj], this.ctrlKeyDown);
  6373. }
  6374. else {
  6375. // unselect this node
  6376. this._unselectNodes([this.startClickedObj]);
  6377. }
  6378. if (!this.hasMovingNodes) {
  6379. this._redraw();
  6380. }
  6381. }
  6382. else if (this.shiftKeyDown) {
  6383. // start selection of multiple nodes
  6384. }
  6385. else {
  6386. // start moving the graph
  6387. this.moved = false;
  6388. }
  6389. };
  6390. /**
  6391. * handle on mouse move event
  6392. */
  6393. Graph.prototype._onMouseMove = function (event) {
  6394. event = event || window.event;
  6395. if (!this.selectable) {
  6396. return;
  6397. }
  6398. var mouseX = event.clientX || (event.targetTouches && event.targetTouches[0].clientX) || 0;
  6399. var mouseY = event.clientY || (event.targetTouches && event.targetTouches[0].clientY) || 0;
  6400. this.mouseX = mouseX;
  6401. this.mouseY = mouseY;
  6402. if (this.startClickedObj) {
  6403. var node = this.nodes[this.startClickedObj.row];
  6404. if (!this.startClickedObj.xFixed)
  6405. node.x = this._xToCanvas(mouseX - this.startFrameLeft);
  6406. if (!this.startClickedObj.yFixed)
  6407. node.y = this._yToCanvas(mouseY - this.startFrameTop);
  6408. // start animation if not yet running
  6409. if (!this.hasMovingNodes) {
  6410. this.hasMovingNodes = true;
  6411. this.start();
  6412. }
  6413. }
  6414. else if (this.shiftKeyDown) {
  6415. // draw a rect from start mouse location to current mouse location
  6416. if (this.frame.selRect == undefined) {
  6417. this.frame.selRect = document.createElement("DIV");
  6418. this.frame.appendChild(this.frame.selRect);
  6419. this.frame.selRect.style.position = "absolute";
  6420. this.frame.selRect.style.border = "1px dashed red";
  6421. }
  6422. var left = Math.min(this.startMouseX, mouseX) - this.startFrameLeft;
  6423. var top = Math.min(this.startMouseY, mouseY) - this.startFrameTop;
  6424. var right = Math.max(this.startMouseX, mouseX) - this.startFrameLeft;
  6425. var bottom = Math.max(this.startMouseY, mouseY) - this.startFrameTop;
  6426. this.frame.selRect.style.left = left + "px";
  6427. this.frame.selRect.style.top = top + "px";
  6428. this.frame.selRect.style.width = (right - left) + "px";
  6429. this.frame.selRect.style.height = (bottom - top) + "px";
  6430. }
  6431. else {
  6432. // move the network
  6433. var diffX = mouseX - this.startMouseX;
  6434. var diffY = mouseY - this.startMouseY;
  6435. this._setTranslation(
  6436. this.startTranslation.x + diffX,
  6437. this.startTranslation.y + diffY);
  6438. this._redraw();
  6439. this.moved = true;
  6440. }
  6441. Graph.preventDefault(event);
  6442. };
  6443. /**
  6444. * handle on mouse up event
  6445. */
  6446. Graph.prototype._onMouseUp = function (event) {
  6447. event = event || window.event;
  6448. if (!this.selectable) {
  6449. return;
  6450. }
  6451. // remove event listeners here, important for Safari
  6452. if (this.onmousemove) {
  6453. Graph.removeEventListener(document, "mousemove", this.onmousemove);
  6454. this.onmousemove = undefined;
  6455. }
  6456. if (this.onmouseup) {
  6457. Graph.removeEventListener(document, "mouseup", this.onmouseup);
  6458. this.onmouseup = undefined;
  6459. }
  6460. Graph.preventDefault(event);
  6461. // check selected nodes
  6462. var endMouseX = event.clientX || this.mouseX || 0;
  6463. var endMouseY = event.clientY || this.mouseY || 0;
  6464. var ctrlKey = event ? event.ctrlKey : window.event.ctrlKey;
  6465. if (this.startClickedObj) {
  6466. // restore the original fixed state
  6467. var node = this.nodes[this.startClickedObj.row];
  6468. node.xFixed = this.startClickedObj.xFixed;
  6469. node.yFixed = this.startClickedObj.yFixed;
  6470. }
  6471. else if (this.shiftKeyDown) {
  6472. // select nodes inside selection area
  6473. var obj = {
  6474. "left": this._xToCanvas(Math.min(this.startMouseX, endMouseX) - this.startFrameLeft),
  6475. "top": this._yToCanvas(Math.min(this.startMouseY, endMouseY) - this.startFrameTop),
  6476. "right": this._xToCanvas(Math.max(this.startMouseX, endMouseX) - this.startFrameLeft),
  6477. "bottom": this._yToCanvas(Math.max(this.startMouseY, endMouseY) - this.startFrameTop)
  6478. };
  6479. var overlappingNodes = this._getNodesOverlappingWith(obj);
  6480. this._selectNodes(overlappingNodes, ctrlKey);
  6481. this.redraw();
  6482. // remove the selection rectangle
  6483. if (this.frame.selRect) {
  6484. this.frame.removeChild(this.frame.selRect);
  6485. this.frame.selRect = undefined;
  6486. }
  6487. }
  6488. else {
  6489. if (!this.ctrlKeyDown && !this.moved) {
  6490. // remove selection
  6491. this._unselectNodes();
  6492. this._redraw();
  6493. }
  6494. }
  6495. this.leftButtonDown = false;
  6496. this.ctrlKeyDown = false;
  6497. };
  6498. /**
  6499. * Event handler for mouse wheel event, used to zoom the timeline
  6500. * Code from http://adomas.org/javascript-mouse-wheel/
  6501. * @param {event} event The event
  6502. */
  6503. Graph.prototype._onMouseWheel = function(event) {
  6504. event = event || window.event;
  6505. var mouseX = event.clientX;
  6506. var mouseY = event.clientY;
  6507. // retrieve delta
  6508. var delta = 0;
  6509. if (event.wheelDelta) { /* IE/Opera. */
  6510. delta = event.wheelDelta/120;
  6511. } else if (event.detail) { /* Mozilla case. */
  6512. // In Mozilla, sign of delta is different than in IE.
  6513. // Also, delta is multiple of 3.
  6514. delta = -event.detail/3;
  6515. }
  6516. // If delta is nonzero, handle it.
  6517. // Basically, delta is now positive if wheel was scrolled up,
  6518. // and negative, if wheel was scrolled down.
  6519. if (delta) {
  6520. // determine zoom factor, and adjust the zoom factor such that zooming in
  6521. // and zooming out correspond wich each other
  6522. var zoom = delta / 10;
  6523. if (delta < 0) {
  6524. zoom = zoom / (1 - zoom);
  6525. }
  6526. var scaleOld = this._getScale();
  6527. var scaleNew = scaleOld * (1 + zoom);
  6528. if (scaleNew < 0.01) {
  6529. scaleNew = 0.01;
  6530. }
  6531. if (scaleNew > 10) {
  6532. scaleNew = 10;
  6533. }
  6534. var frameLeft = Graph._getAbsoluteLeft(this.frame.canvas);
  6535. var frameTop = Graph._getAbsoluteTop(this.frame.canvas);
  6536. var x = mouseX - frameLeft;
  6537. var y = mouseY - frameTop;
  6538. var translation = this._getTranslation();
  6539. var scaleFrac = scaleNew / scaleOld;
  6540. var tx = (1 - scaleFrac) * x + translation.x * scaleFrac;
  6541. var ty = (1 - scaleFrac) * y + translation.y * scaleFrac;
  6542. this._setScale(scaleNew);
  6543. this._setTranslation(tx, ty);
  6544. this._redraw();
  6545. }
  6546. // Prevent default actions caused by mouse wheel.
  6547. // That might be ugly, but we handle scrolls somehow
  6548. // anyway, so don't bother here...
  6549. Graph.preventDefault(event);
  6550. };
  6551. /**
  6552. * Mouse move handler for checking whether the title moves over a node or
  6553. * package with a title.
  6554. */
  6555. Graph.prototype._onMouseMoveTitle = function (event) {
  6556. event = event || window.event;
  6557. var startMouseX = event.clientX;
  6558. var startMouseY = event.clientY;
  6559. this.startFrameLeft = this.startFrameLeft || Graph._getAbsoluteLeft(this.frame.canvas);
  6560. this.startFrameTop = this.startFrameTop || Graph._getAbsoluteTop(this.frame.canvas);
  6561. var x = startMouseX - this.startFrameLeft;
  6562. var y = startMouseY - this.startFrameTop;
  6563. // check if the previously selected node is still selected
  6564. if (this.popupNode) {
  6565. this._checkHidePopup(x, y);
  6566. }
  6567. // start a timeout that will check if the mouse is positioned above
  6568. // an element
  6569. var me = this;
  6570. var checkShow = function() {
  6571. me._checkShowPopup(x, y);
  6572. };
  6573. if (this.popupTimer) {
  6574. clearInterval(this.popupTimer); // stop any running timer
  6575. }
  6576. if (!this.leftButtonDown) {
  6577. this.popupTimer = setTimeout(checkShow, 300);
  6578. }
  6579. };
  6580. /**
  6581. * Check if there is an element on the given position in the network (
  6582. * (a node, package, or edge). If so, and if this element has a title,
  6583. * show a popup window with its title.
  6584. *
  6585. * @param {number} x
  6586. * @param {number} y
  6587. */
  6588. Graph.prototype._checkShowPopup = function (x, y) {
  6589. var obj = {
  6590. "left" : this._xToCanvas(x),
  6591. "top" : this._yToCanvas(y),
  6592. "right" : this._xToCanvas(x),
  6593. "bottom" : this._yToCanvas(y)
  6594. };
  6595. var i, len;
  6596. var lastPopupNode = this.popupNode;
  6597. if (this.popupNode == undefined) {
  6598. // search the packages for overlap
  6599. for (i = 0, len = this.packages.length; i < len; i++) {
  6600. var p = this.packages[i];
  6601. if (p.getTitle() != undefined && p.isOverlappingWith(obj)) {
  6602. this.popupNode = p;
  6603. break;
  6604. }
  6605. }
  6606. }
  6607. if (this.popupNode == undefined) {
  6608. // search the nodes for overlap, select the top one in case of multiple nodes
  6609. var nodes = this.nodes;
  6610. for (i = nodes.length - 1; i >= 0; i--) {
  6611. var node = nodes[i];
  6612. if (node.getTitle() != undefined && node.isOverlappingWith(obj)) {
  6613. this.popupNode = node;
  6614. break;
  6615. }
  6616. }
  6617. }
  6618. if (this.popupNode == undefined) {
  6619. // search the edges for overlap
  6620. var allEdges = this.edges;
  6621. for (i = 0, len = allEdges.length; i < len; i++) {
  6622. var edge = allEdges[i];
  6623. if (edge.getTitle() != undefined && edge.isOverlappingWith(obj)) {
  6624. this.popupNode = edge;
  6625. break;
  6626. }
  6627. }
  6628. }
  6629. if (this.popupNode) {
  6630. // show popup message window
  6631. if (this.popupNode != lastPopupNode) {
  6632. var me = this;
  6633. if (!me.popup) {
  6634. me.popup = new Graph.Popup(me.frame);
  6635. }
  6636. // adjust a small offset such that the mouse cursor is located in the
  6637. // bottom left location of the popup, and you can easily move over the
  6638. // popup area
  6639. me.popup.setPosition(x - 3, y - 3);
  6640. me.popup.setText(me.popupNode.getTitle());
  6641. me.popup.show();
  6642. }
  6643. }
  6644. else {
  6645. if (this.popup) {
  6646. this.popup.hide();
  6647. }
  6648. }
  6649. };
  6650. /**
  6651. * Check if the popup must be hided, which is the case when the mouse is no
  6652. * longer hovering on the object
  6653. * @param {number} x
  6654. * @param {number} y
  6655. */
  6656. Graph.prototype._checkHidePopup = function (x, y) {
  6657. var obj = {
  6658. "left" : x,
  6659. "top" : y,
  6660. "right" : x,
  6661. "bottom" : y
  6662. };
  6663. if (!this.popupNode || !this.popupNode.isOverlappingWith(obj) ) {
  6664. this.popupNode = undefined;
  6665. if (this.popup) {
  6666. this.popup.hide();
  6667. }
  6668. }
  6669. };
  6670. /**
  6671. * Event handler for touchstart event on mobile devices
  6672. */
  6673. Graph.prototype._onTouchStart = function(event) {
  6674. Graph.preventDefault(event);
  6675. if (this.touchDown) {
  6676. // if already moving, return
  6677. return;
  6678. }
  6679. this.touchDown = true;
  6680. var me = this;
  6681. if (!this.ontouchmove) {
  6682. this.ontouchmove = function (event) {me._onTouchMove(event);};
  6683. Graph.addEventListener(document, "touchmove", this.ontouchmove);
  6684. }
  6685. if (!this.ontouchend) {
  6686. this.ontouchend = function (event) {me._onTouchEnd(event);};
  6687. Graph.addEventListener(document, "touchend", this.ontouchend);
  6688. }
  6689. this._onMouseDown(event);
  6690. };
  6691. /**
  6692. * Event handler for touchmove event on mobile devices
  6693. */
  6694. Graph.prototype._onTouchMove = function(event) {
  6695. Graph.preventDefault(event);
  6696. this._onMouseMove(event);
  6697. };
  6698. /**
  6699. * Event handler for touchend event on mobile devices
  6700. */
  6701. Graph.prototype._onTouchEnd = function(event) {
  6702. Graph.preventDefault(event);
  6703. this.touchDown = false;
  6704. if (this.ontouchmove) {
  6705. Graph.removeEventListener(document, "touchmove", this.ontouchmove);
  6706. this.ontouchmove = undefined;
  6707. }
  6708. if (this.ontouchend) {
  6709. Graph.removeEventListener(document, "touchend", this.ontouchend);
  6710. this.ontouchend = undefined;
  6711. }
  6712. this._onMouseUp(event);
  6713. };
  6714. /**
  6715. * Unselect selected nodes. If no selection array is provided, all nodes
  6716. * are unselected
  6717. * @param {Object[]} selection Array with selection objects, each selection
  6718. * object has a parameter row. Optional
  6719. * @param {Boolean} triggerSelect If true (default), the select event
  6720. * is triggered when nodes are unselected
  6721. * @return {Boolean} changed True if the selection is changed
  6722. */
  6723. Graph.prototype._unselectNodes = function(selection, triggerSelect) {
  6724. var changed = false;
  6725. var i, iMax, row;
  6726. if (selection) {
  6727. // remove provided selections
  6728. for (i = 0, iMax = selection.length; i < iMax; i++) {
  6729. row = selection[i].row;
  6730. this.nodes[row].unselect();
  6731. var j = 0;
  6732. while (j < this.selection.length) {
  6733. if (this.selection[j].row == row) {
  6734. this.selection.splice(j, 1);
  6735. changed = true;
  6736. }
  6737. else {
  6738. j++;
  6739. }
  6740. }
  6741. }
  6742. }
  6743. else if (this.selection && this.selection.length) {
  6744. // remove all selections
  6745. for (i = 0, iMax = this.selection.length; i < iMax; i++) {
  6746. row = this.selection[i].row;
  6747. this.nodes[row].unselect();
  6748. changed = true;
  6749. }
  6750. this.selection = [];
  6751. }
  6752. if (changed && (triggerSelect == true || triggerSelect == undefined)) {
  6753. // fire the select event
  6754. this.trigger('select');
  6755. }
  6756. return changed;
  6757. };
  6758. /**
  6759. * select all nodes on given location x, y
  6760. * @param {Array} selection an array with selection objects. Each selection
  6761. * object has a parameter row
  6762. * @param {boolean} append If true, the new selection will be appended to the
  6763. * current selection (except for duplicate entries)
  6764. * @return {Boolean} changed True if the selection is changed
  6765. */
  6766. Graph.prototype._selectNodes = function(selection, append) {
  6767. var changed = false;
  6768. var i, iMax;
  6769. // TODO: the selectNodes method is a little messy, rework this
  6770. // check if the current selection equals the desired selection
  6771. var selectionAlreadyDone = true;
  6772. if (selection.length != this.selection.length) {
  6773. selectionAlreadyDone = false;
  6774. }
  6775. else {
  6776. for (i = 0, iMax = Math.min(selection.length, this.selection.length); i < iMax; i++) {
  6777. if (selection[i].row != this.selection[i].row) {
  6778. selectionAlreadyDone = false;
  6779. break;
  6780. }
  6781. }
  6782. }
  6783. if (selectionAlreadyDone) {
  6784. return changed;
  6785. }
  6786. if (append == undefined || append == false) {
  6787. // first deselect any selected node
  6788. var triggerSelect = false;
  6789. changed = this._unselectNodes(undefined, triggerSelect);
  6790. }
  6791. for (i = 0, iMax = selection.length; i < iMax; i++) {
  6792. // add each of the new selections, but only when they are not duplicate
  6793. var row = selection[i].row;
  6794. var isDuplicate = false;
  6795. for (var j = 0, jMax = this.selection.length; j < jMax; j++) {
  6796. if (this.selection[j].row == row) {
  6797. isDuplicate = true;
  6798. break;
  6799. }
  6800. }
  6801. if (!isDuplicate) {
  6802. this.nodes[row].select();
  6803. this.selection.push(selection[i]);
  6804. changed = true;
  6805. }
  6806. }
  6807. if (changed) {
  6808. // fire the select event
  6809. this.trigger('select');
  6810. }
  6811. return changed;
  6812. };
  6813. /**
  6814. * retrieve all nodes overlapping with given object
  6815. * @param {Object} obj An object with parameters left, top, right, bottom
  6816. * @return {Object[]} An array with selection objects containing
  6817. * the parameter row.
  6818. */
  6819. Graph.prototype._getNodesOverlappingWith = function (obj) {
  6820. var overlappingNodes = [];
  6821. for (var i = 0; i < this.nodes.length; i++) {
  6822. if (this.nodes[i].isOverlappingWith(obj)) {
  6823. var sel = {"row": i};
  6824. overlappingNodes.push(sel);
  6825. }
  6826. }
  6827. return overlappingNodes;
  6828. };
  6829. /**
  6830. * retrieve the currently selected nodes
  6831. * @return {Object[]} an array with zero or more objects. Each object
  6832. * contains the parameter row
  6833. */
  6834. Graph.prototype.getSelection = function() {
  6835. var selection = [];
  6836. for (var i = 0; i < this.selection.length; i++) {
  6837. var row = this.selection[i].row;
  6838. selection.push({"row": row});
  6839. }
  6840. return selection;
  6841. };
  6842. /**
  6843. * select zero or more nodes
  6844. * @param {object[]} selection an array with zero or more objects. Each object
  6845. * contains the parameter row
  6846. */
  6847. Graph.prototype.setSelection = function(selection) {
  6848. var i, iMax, row;
  6849. if (selection.length == undefined)
  6850. throw "Selection must be an array with objects";
  6851. // first unselect any selected node
  6852. for (i = 0, iMax = this.selection.length; i < iMax; i++) {
  6853. row = this.selection[i].row;
  6854. this.nodes[row].unselect();
  6855. }
  6856. this.selection = [];
  6857. for (i = 0, iMax = selection.length; i < iMax; i++) {
  6858. row = selection[i].row;
  6859. if (row == undefined)
  6860. throw "Parameter row missing in selection object";
  6861. if (row > this.nodes.length-1)
  6862. throw "Parameter row out of range";
  6863. var sel = {"row": row};
  6864. this.selection.push(sel);
  6865. this.nodes[row].select();
  6866. }
  6867. this.redraw();
  6868. };
  6869. /**
  6870. * Temporary method to test calculating a hub value for the nodes
  6871. * @param {number} level Maximum number edges between two nodes in order
  6872. * to call them connected. Optional, 1 by default
  6873. * @return {Number[]} connectioncount array with the connection count
  6874. * for each node
  6875. */
  6876. Graph.prototype._getConnectionCount = function(level) {
  6877. var conn = this.edges;
  6878. if (level == undefined) {
  6879. level = 1;
  6880. }
  6881. // get the nodes connected to given nodes
  6882. function getConnectedNodes(nodes) {
  6883. var connectedNodes = [];
  6884. for (var j = 0, jMax = nodes.length; j < jMax; j++) {
  6885. var node = nodes[j];
  6886. // find all nodes connected to this node
  6887. for (var i = 0, iMax = conn.length; i < iMax; i++) {
  6888. var other = null;
  6889. // check if connected
  6890. if (conn[i].from == node)
  6891. other = conn[i].to;
  6892. else if (conn[i].to == node)
  6893. other = conn[i].from;
  6894. // check if the other node is not already in the list with nodes
  6895. var k, kMax;
  6896. if (other) {
  6897. for (k = 0, kMax = nodes.length; k < kMax; k++) {
  6898. if (nodes[k] == other) {
  6899. other = null;
  6900. break;
  6901. }
  6902. }
  6903. }
  6904. if (other) {
  6905. for (k = 0, kMax = connectedNodes.length; k < kMax; k++) {
  6906. if (connectedNodes[k] == other) {
  6907. other = null;
  6908. break;
  6909. }
  6910. }
  6911. }
  6912. if (other)
  6913. connectedNodes.push(other);
  6914. }
  6915. }
  6916. return connectedNodes;
  6917. }
  6918. var connections = [];
  6919. var level0 = [];
  6920. var nodes = this.nodes;
  6921. var i, iMax;
  6922. for (i = 0, iMax = nodes.length; i < iMax; i++) {
  6923. var c = [nodes[i]];
  6924. for (var l = 0; l < level; l++) {
  6925. c = c.concat(getConnectedNodes(c));
  6926. }
  6927. connections.push(c);
  6928. }
  6929. var hubs = [];
  6930. for (i = 0, len = connections.length; i < len; i++) {
  6931. hubs.push(connections[i].length);
  6932. }
  6933. return hubs;
  6934. };
  6935. /**
  6936. * Set a new size for the network
  6937. * @param {string} width Width in pixels or percentage (for example "800px"
  6938. * or "50%")
  6939. * @param {string} height Height in pixels or percentage (for example "400px"
  6940. * or "30%")
  6941. */
  6942. Graph.prototype._setSize = function(width, height) {
  6943. this.frame.style.width = width;
  6944. this.frame.style.height = height;
  6945. this.frame.canvas.style.width = "100%";
  6946. this.frame.canvas.style.height = "100%";
  6947. this.frame.canvas.width = this.frame.canvas.clientWidth;
  6948. this.frame.canvas.height = this.frame.canvas.clientHeight;
  6949. if (this.slider) {
  6950. this.slider.redraw();
  6951. }
  6952. };
  6953. /**
  6954. * Append nodes
  6955. * Nodes with a duplicate id will be replaced
  6956. * @param {Array} nodes The data containing the nodes.
  6957. */
  6958. Graph.prototype.addNodes = function(nodes) {
  6959. var hasValues = false;
  6960. var rowCount = nodes.length;
  6961. for (var i = 0; i < rowCount; i++) {
  6962. var properties = nodes[i];
  6963. if (properties.value != undefined) {
  6964. hasValues = true;
  6965. }
  6966. if (properties.id == undefined) {
  6967. throw "Column 'id' missing in table with nodes (row " + i + ")";
  6968. }
  6969. this._createNode(properties);
  6970. }
  6971. // calculate scaling function when value is provided
  6972. if (hasValues) {
  6973. this._updateValueRange(this.nodes);
  6974. }
  6975. this.start();
  6976. };
  6977. /**
  6978. * Load all nodes by reading the data table nodesTable
  6979. * @param {Array} nodes The data containing the nodes.
  6980. */
  6981. Graph.prototype.setNodes = function(nodes) {
  6982. this.selection = [];
  6983. this.nodes = [];
  6984. this.hasMovingNodes = false;
  6985. if (!nodes) {
  6986. return;
  6987. }
  6988. this.nodesTable = nodes;
  6989. var hasValues = false;
  6990. var rowCount = nodes.length;
  6991. for (var i = 0; i < rowCount; i++) {
  6992. var properties = nodes[i];
  6993. if (properties.value != undefined) {
  6994. hasValues = true;
  6995. }
  6996. if (properties.timestamp) {
  6997. this.hasTimestamps = this.hasTimestamps || properties.timestamp;
  6998. }
  6999. if (properties.id == undefined) {
  7000. throw "Column 'id' missing in table with nodes (row " + i + ")";
  7001. }
  7002. this._createNode(properties);
  7003. }
  7004. // calculate scaling function when value is provided
  7005. if (hasValues) {
  7006. this._updateValueRange(this.nodes);
  7007. }
  7008. };
  7009. /**
  7010. * Filter the current nodes table for nodes with a timestamp older than given
  7011. * timestamp. Can only be used for nodes added via setNodes(), not via
  7012. * addNodes().
  7013. * @param {*} [timestamp] If timestamp is undefined, all nodes are shown
  7014. */
  7015. Graph.prototype._filterNodes = function(timestamp) {
  7016. if (this.nodesTable == undefined) {
  7017. return;
  7018. }
  7019. // remove existing nodes with a too new timestamp
  7020. if (timestamp !== undefined) {
  7021. var ns = this.nodes;
  7022. var n = 0;
  7023. while (n < ns.length) {
  7024. var t = ns[n].timestamp;
  7025. if (t !== undefined && t > timestamp) {
  7026. // remove this node
  7027. ns.splice(n, 1);
  7028. }
  7029. else {
  7030. n++;
  7031. }
  7032. }
  7033. }
  7034. // add all nodes with an old enough timestamp
  7035. var table = this.nodesTable;
  7036. var rowCount = table.length;
  7037. for (var i = 0; i < rowCount; i++) {
  7038. // copy all properties
  7039. var properties = table[i];
  7040. if (properties.id === undefined) {
  7041. throw "Column 'id' missing in table with nodes (row " + i + ")";
  7042. }
  7043. // check what the timestamp is
  7044. var ts = properties.timestamp ? properties.timestamp : undefined;
  7045. var visible = true;
  7046. if (ts !== undefined && timestamp !== undefined && ts > timestamp) {
  7047. visible = false;
  7048. }
  7049. if (visible) {
  7050. // create or update the node
  7051. this._createNode(properties);
  7052. }
  7053. }
  7054. this.start();
  7055. };
  7056. /**
  7057. * Create a node with the given properties
  7058. * If the new node has an id identical to an existing package, the existing
  7059. * node will be overwritten.
  7060. * The properties can contain a property "action", which can have values
  7061. * "create", "update", or "delete"
  7062. * @param {Object} properties An object with properties
  7063. */
  7064. Graph.prototype._createNode = function(properties) {
  7065. var action = properties.action ? properties.action : "update";
  7066. var id, index, newNode, oldNode;
  7067. if (action === "create") {
  7068. // create the node
  7069. newNode = new Graph.Node(properties, this.images, this.groups, this.constants);
  7070. id = properties.id;
  7071. index = (id !== undefined) ? this._findNode(id) : undefined;
  7072. if (index !== undefined) {
  7073. // replace node
  7074. oldNode = this.nodes[index];
  7075. this.nodes[index] = newNode;
  7076. // remove selection of old node
  7077. if (oldNode.selected) {
  7078. this._unselectNodes([{'row': index}], false);
  7079. }
  7080. /* TODO: implement this? -> will give performance issues, searching all edges and node...
  7081. // update edges linking to this node
  7082. var edgesTable = this.edges;
  7083. for (var i = 0, iMax = edgesTable.length; i < iMax; i++) {
  7084. var edge = edgesTable[i];
  7085. if (edge.from == oldNode) {
  7086. edge.from = newNode;
  7087. }
  7088. if (edge.to == oldNode) {
  7089. edge.to = newNode;
  7090. }
  7091. }
  7092. // update packages linking to this node
  7093. var packagesTable = this.packages;
  7094. for (var i = 0, iMax = packagesTable.length; i < iMax; i++) {
  7095. var package = packagesTable[i];
  7096. if (package.from == oldNode) {
  7097. package.from = newNode;
  7098. }
  7099. if (package.to == oldNode) {
  7100. package.to = newNode;
  7101. }
  7102. }
  7103. */
  7104. }
  7105. else {
  7106. // add new node
  7107. this.nodes.push(newNode);
  7108. }
  7109. if (!newNode.isFixed()) {
  7110. // note: no not use node.isMoving() here, as that gives the current
  7111. // velocity of the node, which is zero after creation of the node.
  7112. this.hasMovingNodes = true;
  7113. }
  7114. }
  7115. else if (action === "update") {
  7116. // update existing node, or create it when not yet existing
  7117. id = properties.id;
  7118. if (id === undefined) {
  7119. throw "Cannot update a node without id";
  7120. }
  7121. index = this._findNode(id);
  7122. if (index !== undefined) {
  7123. // update node
  7124. this.nodes[index].setProperties(properties, this.constants);
  7125. }
  7126. else {
  7127. // create node
  7128. newNode = new Graph.Node(properties, this.images, this.groups, this.constants);
  7129. this.nodes.push(newNode);
  7130. if (!newNode.isFixed()) {
  7131. // note: no not use node.isMoving() here, as that gives the current
  7132. // velocity of the node, which is zero after creation of the node.
  7133. this.hasMovingNodes = true;
  7134. }
  7135. }
  7136. }
  7137. else if (action === "delete") {
  7138. // delete existing node
  7139. id = properties.id;
  7140. if (id === undefined) {
  7141. throw "Cannot delete node without its id";
  7142. }
  7143. index = this._findNode(id);
  7144. if (index !== undefined) {
  7145. oldNode = this.nodes[index];
  7146. // remove selection of old node
  7147. if (oldNode.selected) {
  7148. this._unselectNodes([{'row': index}], false);
  7149. }
  7150. this.nodes.splice(index, 1);
  7151. }
  7152. else {
  7153. throw "Node with id " + id + " not found";
  7154. }
  7155. }
  7156. else {
  7157. throw "Unknown action " + action + ". Choose 'create', 'update', or 'delete'.";
  7158. }
  7159. };
  7160. /**
  7161. * Find a node by its id
  7162. * @param {Number} id Id of the node
  7163. * @return {Number} index Index of the node in the array this.nodes, or
  7164. * undefined when not found. *
  7165. */
  7166. Graph.prototype._findNode = function (id) {
  7167. var nodes = this.nodes;
  7168. for (var n = 0, len = nodes.length; n < len; n++) {
  7169. if (nodes[n].id === id) {
  7170. return n;
  7171. }
  7172. }
  7173. return undefined;
  7174. };
  7175. /**
  7176. * Find a node by its rowNumber
  7177. * @param {Number} row Row number of the node
  7178. * @return {Graph.Node} node     The node with the given row number, or
  7179. *                            undefined when not found.
  7180. */
  7181. Graph.prototype._findNodeByRow = function (row) {
  7182. return this.nodes[row];
  7183. };
  7184. /**
  7185. * Load edges by reading the data table
  7186. * @param {Array} edges The data containing the edges.
  7187. */
  7188. Graph.prototype.setEdges = function(edges) {
  7189. this.edges = [];
  7190. this.hasMovingEdges = false;
  7191. if (!edges) {
  7192. return;
  7193. }
  7194. this.edgesTable = edges;
  7195. var hasValues = false;
  7196. var rowCount = edges.length;
  7197. for (var i = 0; i < rowCount; i++) {
  7198. var properties = edges[i];
  7199. if (properties.from === undefined) {
  7200. throw "Column 'from' missing in table with edges (row " + i + ")";
  7201. }
  7202. if (properties.to === undefined) {
  7203. throw "Column 'to' missing in table with edges (row " + i + ")";
  7204. }
  7205. if (properties.timestamp != undefined) {
  7206. this.hasTimestamps = this.hasTimestamps || properties.timestamp;
  7207. }
  7208. if (properties.value != undefined) {
  7209. hasValues = true;
  7210. }
  7211. this._createEdge(properties);
  7212. }
  7213. // calculate scaling function when value is provided
  7214. if (hasValues) {
  7215. this._updateValueRange(this.edges);
  7216. }
  7217. };
  7218. /**
  7219. * Load edges by reading the data table
  7220. * @param {Array} edges The data containing the edges.
  7221. */
  7222. Graph.prototype.addEdges = function(edges) {
  7223. var hasValues = false;
  7224. var rowCount = edges.length;
  7225. for (var i = 0; i < rowCount; i++) {
  7226. // copy all properties
  7227. var properties = edges[i];
  7228. if (properties.from === undefined) {
  7229. throw "Column 'from' missing in table with edges (row " + i + ")";
  7230. }
  7231. if (properties.to === undefined) {
  7232. throw "Column 'to' missing in table with edges (row " + i + ")";
  7233. }
  7234. if (properties.value != undefined) {
  7235. hasValues = true;
  7236. }
  7237. this._createEdge(properties);
  7238. }
  7239. // calculate scaling function when value is provided
  7240. if (hasValues) {
  7241. this._updateValueRange(this.edges);
  7242. }
  7243. this.start();
  7244. };
  7245. /**
  7246. * Filter the current edges table for edges with a timestamp below given
  7247. * timestamp. Can only be used for edges added via setEdges(), not via
  7248. * addEdges().
  7249. * @param {*} [timestamp] If timestamp is undefined, all edges are shown
  7250. */
  7251. Graph.prototype._filterEdges = function(timestamp) {
  7252. if (this.edgesTable == undefined) {
  7253. return;
  7254. }
  7255. // remove existing packages with a too new timestamp
  7256. if (timestamp !== undefined) {
  7257. var ls = this.edges;
  7258. var l = 0;
  7259. while (l < ls.length) {
  7260. var t = ls[l].timestamp;
  7261. if (t !== undefined && t > timestamp) {
  7262. // remove this edge
  7263. ls.splice(l, 1);
  7264. }
  7265. else {
  7266. l++;
  7267. }
  7268. }
  7269. }
  7270. // add all edges with an old enough timestamp
  7271. var table = this.edgesTable;
  7272. var rowCount = table.length;
  7273. for (var i = 0; i < rowCount; i++) {
  7274. var properties = table[i];
  7275. if (properties.from === undefined) {
  7276. throw "Column 'from' missing in table with edges (row " + i + ")";
  7277. }
  7278. if (properties.to === undefined) {
  7279. throw "Column 'to' missing in table with edges (row " + i + ")";
  7280. }
  7281. // check what the timestamp is
  7282. var ts = properties.timestamp ? properties.timestamp : undefined;
  7283. var visible = true;
  7284. if (ts !== undefined && timestamp !== undefined && ts > timestamp) {
  7285. visible = false;
  7286. }
  7287. if (visible) {
  7288. // create or update the edge
  7289. this._createEdge(properties);
  7290. }
  7291. }
  7292. this.start();
  7293. };
  7294. /**
  7295. * Create a edge with the given properties
  7296. * If the new edge has an id identical to an existing edge, the existing
  7297. * edge will be overwritten or updated.
  7298. * The properties can contain a property "action", which can have values
  7299. * "create", "update", or "delete"
  7300. * @param {Object} properties An object with properties
  7301. */
  7302. Graph.prototype._createEdge = function(properties) {
  7303. var action = properties.action ? properties.action : "create";
  7304. var id, index, edge, oldEdge, newEdge;
  7305. if (action === "create") {
  7306. // create the edge, or replace it if already existing
  7307. id = properties.id;
  7308. index = (id !== undefined) ? this._findEdge(id) : undefined;
  7309. edge = new Graph.Edge(properties, this, this.constants);
  7310. if (index !== undefined) {
  7311. // replace existing edge
  7312. oldEdge = this.edges[index];
  7313. oldEdge.from.detachEdge(oldEdge);
  7314. oldEdge.to.detachEdge(oldEdge);
  7315. this.edges[index] = edge;
  7316. }
  7317. else {
  7318. // add new edge
  7319. this.edges.push(edge);
  7320. }
  7321. edge.from.attachEdge(edge);
  7322. edge.to.attachEdge(edge);
  7323. if (edge.isMoving()) {
  7324. this.hasMovingEdges = true;
  7325. }
  7326. }
  7327. else if (action === "update") {
  7328. // update existing edge, or create the edge if not existing
  7329. id = properties.id;
  7330. if (id === undefined) {
  7331. throw "Cannot update a edge without id";
  7332. }
  7333. index = this._findEdge(id);
  7334. if (index !== undefined) {
  7335. // update edge
  7336. edge = this.edges[index];
  7337. edge.from.detachEdge(edge);
  7338. edge.to.detachEdge(edge);
  7339. edge.setProperties(properties, this.constants);
  7340. edge.from.attachEdge(edge);
  7341. edge.to.attachEdge(edge);
  7342. }
  7343. else {
  7344. // add new edge
  7345. edge = new Graph.Edge(properties, this, this.constants);
  7346. edge.from.attachEdge(edge);
  7347. edge.to.attachEdge(edge);
  7348. this.edges.push(edge);
  7349. if (edge.isMoving()) {
  7350. this.hasMovingEdges = true;
  7351. }
  7352. }
  7353. }
  7354. else if (action === "delete") {
  7355. // delete existing edge
  7356. id = properties.id;
  7357. if (id === undefined) {
  7358. throw "Cannot delete edge without its id";
  7359. }
  7360. index = this._findEdge(id);
  7361. if (index !== undefined) {
  7362. oldEdge = this.edges[id];
  7363. edge.from.detachEdge(oldEdge);
  7364. edge.to.detachEdge(oldEdge);
  7365. this.edges.splice(index, 1);
  7366. }
  7367. else {
  7368. throw "Edge with id " + id + " not found";
  7369. }
  7370. }
  7371. else {
  7372. throw "Unknown action " + action + ". Choose 'create', 'update', or 'delete'.";
  7373. }
  7374. };
  7375. /**
  7376. * Update the edge to oldNode in all edges and packages.
  7377. * @param {Node} oldNode
  7378. * @param {Node} newNode
  7379. */
  7380. // TODO: start utilizing this method _updateNodeReferences
  7381. Graph.prototype._updateNodeReferences = function(oldNode, newNode) {
  7382. var arrays = [this.edges, this.packages];
  7383. for (var a = 0, aMax = arrays.length; a < aMax; a++) {
  7384. var array = arrays[a];
  7385. for (var i = 0, iMax = array.length; i < iMax; i++) {
  7386. if (array.from === oldNode) {
  7387. array.from = newNode;
  7388. }
  7389. if (array.to === oldNode) {
  7390. array.to = newNode;
  7391. }
  7392. }
  7393. }
  7394. };
  7395. /**
  7396. * Find a edge by its id
  7397. * @param {Number} id Id of the edge
  7398. * @return {Number} index Index of the edge in the array this.edges, or
  7399. * undefined when not found. *
  7400. */
  7401. Graph.prototype._findEdge = function (id) {
  7402. var edges = this.edges;
  7403. for (var n = 0, len = edges.length; n < len; n++) {
  7404. if (edges[n].id === id) {
  7405. return n;
  7406. }
  7407. }
  7408. return undefined;
  7409. };
  7410. /**
  7411. * Find a edge by its row
  7412. * @param {Number} row Row of the edge
  7413. * @return {Graph.Edge} the found edge, or undefined when not found
  7414. */
  7415. Graph.prototype._findEdgeByRow = function (row) {
  7416. return this.edges[row];
  7417. };
  7418. /**
  7419. * Append packages
  7420. * Packages with a duplicate id will be replaced
  7421. * @param {Array} packages The data containing the packages.
  7422. */
  7423. Graph.prototype.addPackages = function(packages) {
  7424. var rowCount = packages.length;
  7425. for (var i = 0; i < rowCount; i++) {
  7426. var properties = packages[i];
  7427. if (properties.from === undefined) {
  7428. throw "Column 'from' missing in table with packages (row " + i + ")";
  7429. }
  7430. if (properties.to === undefined) {
  7431. throw "Column 'to' missing in table with packages (row " + i + ")";
  7432. }
  7433. this._createPackage(properties);
  7434. }
  7435. // calculate scaling function when value is provided
  7436. this._updateValueRange(this.packages);
  7437. this.start();
  7438. };
  7439. /**
  7440. * Set a new packages table
  7441. * Packages with a duplicate id will be replaced
  7442. * @param {Array} packages The data containing the packages.
  7443. */
  7444. Graph.prototype.setPackages = function(packages) {
  7445. this.packages = [];
  7446. if (!packages) {
  7447. return;
  7448. }
  7449. this.packagesTable = packages;
  7450. var rowCount = packages.length;
  7451. for (var i = 0; i < rowCount; i++) {
  7452. var properties = packages[i];
  7453. if (properties.from === undefined) {
  7454. throw "Column 'from' missing in table with packages (row " + i + ")";
  7455. }
  7456. if (properties.to === undefined) {
  7457. throw "Column 'to' missing in table with packages (row " + i + ")";
  7458. }
  7459. if (properties.timestamp) {
  7460. this.hasTimestamps = this.hasTimestamps || properties.timestamp;
  7461. }
  7462. this._createPackage(properties);
  7463. }
  7464. // calculate scaling function when value is provided
  7465. this._updateValueRange(this.packages);
  7466. /* TODO: adjust examples and documentation for this?
  7467. this.start();
  7468. */
  7469. };
  7470. /**
  7471. * Filter the current package table for packages with a timestamp below given
  7472. * timestamp. Can only be used for packages added via setPackages(), not via
  7473. * addPackages().
  7474. * @param {*} [timestamp] If timestamp is undefined, all packages are shown
  7475. */
  7476. Graph.prototype._filterPackages = function(timestamp) {
  7477. if (this.packagesTable == undefined) {
  7478. return;
  7479. }
  7480. // remove all current packages
  7481. this.packages = [];
  7482. /* TODO: cleanup
  7483. // remove existing packages with a too new timestamp
  7484. if (timestamp !== undefined) {
  7485. var packages = this.packages;
  7486. var p = 0;
  7487. while (p < packages.length) {
  7488. var package = packages[p];
  7489. var t = package.timestamp;
  7490. if (t !== undefined && t > timestamp ) {
  7491. // remove this package
  7492. packages.splice(p, 1);
  7493. }
  7494. else {
  7495. p++;
  7496. }
  7497. }
  7498. }
  7499. */
  7500. // add all packages with an old enough timestamp
  7501. var table = this.packagesTable;
  7502. var rowCount = table.length;
  7503. for (var i = 0; i < rowCount; i++) {
  7504. var properties = table[i];
  7505. if (properties.from === undefined) {
  7506. throw "Column 'from' missing in table with packages (row " + i + ")";
  7507. }
  7508. if (properties.to === undefined) {
  7509. throw "Column 'to' missing in table with packages (row " + i + ")";
  7510. }
  7511. // check what the timestamp is
  7512. var pTimestamp = properties.timestamp ? properties.timestamp : undefined;
  7513. var visible = true;
  7514. if (pTimestamp !== undefined && timestamp !== undefined && pTimestamp > timestamp) {
  7515. visible = false;
  7516. }
  7517. if (visible === true) {
  7518. if (properties.progress == undefined) {
  7519. // when no progress is provided, we need to add our own progress
  7520. var duration = properties.duration || this.constants.packages.duration; // seconds
  7521. var diff = (timestamp.getTime() - pTimestamp.getTime()) / 1000; // seconds
  7522. if (diff < duration) {
  7523. // copy the properties, and fill in the current progress based on the
  7524. // timestamp and the duration
  7525. var original = properties;
  7526. properties = {};
  7527. for (var j in original) {
  7528. if (original.hasOwnProperty(j)) {
  7529. properties[j] = original[j];
  7530. }
  7531. }
  7532. properties.progress = diff / duration; // scale 0-1
  7533. }
  7534. else {
  7535. visible = false;
  7536. }
  7537. }
  7538. }
  7539. if (visible === true) {
  7540. // create or update the package
  7541. this._createPackage(properties);
  7542. }
  7543. }
  7544. this.start();
  7545. };
  7546. /**
  7547. * Create a package with the given properties
  7548. * If the new package has an id identical to an existing package, the existing
  7549. * package will be overwritten.
  7550. * The properties can contain a property "action", which can have values
  7551. * "create", "update", or "delete"
  7552. * @param {Object} properties An object with properties
  7553. */
  7554. Graph.prototype._createPackage = function(properties) {
  7555. var action = properties.action ? properties.action : "create";
  7556. var id, index, newPackage;
  7557. if (action === "create") {
  7558. // create the package
  7559. id = properties.id;
  7560. index = (id !== undefined) ? this._findPackage(id) : undefined;
  7561. newPackage = new Graph.Package(properties, this, this.images, this.constants);
  7562. if (index !== undefined) {
  7563. // replace existing package
  7564. this.packages[index] = newPackage;
  7565. }
  7566. else {
  7567. // add new package
  7568. this.packages.push(newPackage);
  7569. }
  7570. if (newPackage.isMoving()) {
  7571. this.hasMovingPackages = true;
  7572. }
  7573. }
  7574. else if (action === "update") {
  7575. // update a package, or create it when not existing
  7576. id = properties.id;
  7577. if (id === undefined) {
  7578. throw "Cannot update a edge without id";
  7579. }
  7580. index = this._findPackage(id);
  7581. if (index !== undefined) {
  7582. // update existing package
  7583. this.packages[index].setProperties(properties, this.constants);
  7584. }
  7585. else {
  7586. // add new package
  7587. newPackage = new Graph.Package(properties, this, this.images, this.constants);
  7588. this.packages.push(newPackage);
  7589. if (newPackage.isMoving()) {
  7590. this.hasMovingPackages = true;
  7591. }
  7592. }
  7593. }
  7594. else if (action === "delete") {
  7595. // delete existing package
  7596. id = properties.id;
  7597. if (id === undefined) {
  7598. throw "Cannot delete package without its id";
  7599. }
  7600. index = this._findPackage(id);
  7601. if (index !== undefined) {
  7602. this.packages.splice(index, 1);
  7603. }
  7604. else {
  7605. throw "Package with id " + id + " not found";
  7606. }
  7607. }
  7608. else {
  7609. throw "Unknown action " + action + ". Choose 'create', 'update', or 'delete'.";
  7610. }
  7611. };
  7612. /**
  7613. * Find a package by its id.
  7614. * @param {Number} id
  7615. * @return {Number} index Index of the package in the array this.packages,
  7616. * or undefined when not found
  7617. */
  7618. Graph.prototype._findPackage = function (id) {
  7619. var packages = this.packages;
  7620. for (var n = 0, len = packages.length; n < len; n++) {
  7621. if (packages[n].id === id) {
  7622. return n;
  7623. }
  7624. }
  7625. return undefined;
  7626. };
  7627. /**
  7628. * Find a package by its row
  7629. * @param {Number} row Row of the package
  7630. * @return {Graph.Package} the found package, or undefined when not found
  7631. */
  7632. Graph.prototype._findPackageByRow = function (row) {
  7633. return this.packages[row];
  7634. };
  7635. /**
  7636. * Update the values of all object in the given array according to the current
  7637. * value range of the objects in the array.
  7638. * @param {Array} array. An array with objects like Edges, Nodes, or Packages
  7639. * The objects must have a method getValue() and
  7640. * setValueRange(min, max).
  7641. */
  7642. Graph.prototype._updateValueRange = function(array) {
  7643. var count = array.length;
  7644. var i;
  7645. // determine the range of the node values
  7646. var valueMin = undefined;
  7647. var valueMax = undefined;
  7648. for (i = 0; i < count; i++) {
  7649. var value = array[i].getValue();
  7650. if (value !== undefined) {
  7651. valueMin = (valueMin === undefined) ? value : Math.min(value, valueMin);
  7652. valueMax = (valueMax === undefined) ? value : Math.max(value, valueMax);
  7653. }
  7654. }
  7655. // adjust the range of all nodes
  7656. if (valueMin !== undefined && valueMax !== undefined) {
  7657. for (i = 0; i < count; i++) {
  7658. array[i].setValueRange(valueMin, valueMax);
  7659. }
  7660. }
  7661. };
  7662. /**
  7663. * Set the current timestamp. All packages with a timestamp smaller or equal
  7664. * than the given timestamp will be drawn.
  7665. * @param {Date | Number} timestamp
  7666. */
  7667. Graph.prototype.setTimestamp = function(timestamp) {
  7668. this._filterNodes(timestamp);
  7669. this._filterEdges(timestamp);
  7670. this._filterPackages(timestamp);
  7671. };
  7672. /**
  7673. * Get the range of all timestamps defined in the nodes, edges and packages
  7674. * @return {Object} A range object, containing parameters start and end.
  7675. */
  7676. Graph.prototype._getRange = function() {
  7677. // range is stored as number. at the end of the method, it is converted to
  7678. // Date when needed.
  7679. var range = {
  7680. "start": undefined,
  7681. "end": undefined
  7682. };
  7683. var tables = [this.nodesTable, this.edgesTable];
  7684. for (var t = 0, tMax = tables.length; t < tMax; t++) {
  7685. var table = tables[t];
  7686. if (table !== undefined) {
  7687. for (var i = 0, iMax = table.length; i < iMax; i++) {
  7688. var timestamp = table[i].timestamp;
  7689. if (timestamp) {
  7690. // to long
  7691. if (timestamp instanceof Date) {
  7692. timestamp = timestamp.getTime();
  7693. }
  7694. // calculate new range
  7695. range.start = range.start ? Math.min(timestamp, range.start) : timestamp;
  7696. range.end = range.end ? Math.max(timestamp, range.end) : timestamp;
  7697. }
  7698. }
  7699. }
  7700. }
  7701. // calculate the range for the packagesTable by hand. In case of packages
  7702. // without a progress provided, we need to calculate the end time by hand.
  7703. if (this.packagesTable) {
  7704. var packagesTable = this.packagesTable;
  7705. for (var row = 0, len = packagesTable.length; row < len; row ++) {
  7706. var pkg = packagesTable[row],
  7707. timestamp = pkg.timestamp,
  7708. progress = pkg.progress,
  7709. duration = pkg.duration || this.constants.packages.duration;
  7710. // convert to number
  7711. if (timestamp instanceof Date) {
  7712. timestamp = timestamp.getTime();
  7713. }
  7714. if (timestamp != undefined) {
  7715. var start = timestamp,
  7716. end = progress ? timestamp : (timestamp + duration * 1000);
  7717. range.start = range.start ? Math.min(start, range.start) : start;
  7718. range.end = range.end ? Math.max(end, range.end) : end;
  7719. }
  7720. }
  7721. }
  7722. // convert to the right type: number or date
  7723. var rangeFormat = {
  7724. "start": new Date(range.start),
  7725. "end": new Date(range.end)
  7726. };
  7727. return rangeFormat;
  7728. };
  7729. /**
  7730. * Start animation.
  7731. * Only applicable when packages with a timestamp are available
  7732. */
  7733. Graph.prototype.animationStart = function() {
  7734. if (this.slider) {
  7735. this.slider.play();
  7736. }
  7737. };
  7738. /**
  7739. * Start animation.
  7740. * Only applicable when packages with a timestamp are available
  7741. */
  7742. Graph.prototype.animationStop = function() {
  7743. if (this.slider) {
  7744. this.slider.stop();
  7745. }
  7746. };
  7747. /**
  7748. * Set framerate for the animation.
  7749. * Only applicable when packages with a timestamp are available
  7750. * @param {number} framerate The framerate in frames per second
  7751. */
  7752. Graph.prototype.setAnimationFramerate = function(framerate) {
  7753. if (this.slider) {
  7754. this.slider.setFramerate(framerate);
  7755. }
  7756. }
  7757. /**
  7758. * Set the duration of playing the whole package history
  7759. * Only applicable when packages with a timestamp are available
  7760. * @param {number} duration The duration in seconds
  7761. */
  7762. Graph.prototype.setAnimationDuration = function(duration) {
  7763. if (this.slider) {
  7764. this.slider.setDuration(duration);
  7765. }
  7766. };
  7767. /**
  7768. * Set the time acceleration for playing the history.
  7769. * Only applicable when packages with a timestamp are available
  7770. * @param {number} acceleration Acceleration, for example 10 means play
  7771. * ten times as fast as real time. A value
  7772. * of 1 will play the history in real time.
  7773. */
  7774. Graph.prototype.setAnimationAcceleration = function(acceleration) {
  7775. if (this.slider) {
  7776. this.slider.setAcceleration(acceleration);
  7777. }
  7778. };
  7779. /**
  7780. * Redraw the graph with the current data
  7781. * chart will be resized too.
  7782. */
  7783. Graph.prototype.redraw = function() {
  7784. this._setSize(this.width, this.height);
  7785. this._redraw();
  7786. };
  7787. /**
  7788. * Redraw the graph with the current data
  7789. */
  7790. Graph.prototype._redraw = function() {
  7791. var ctx = this.frame.canvas.getContext("2d");
  7792. // clear the canvas
  7793. var w = this.frame.canvas.width;
  7794. var h = this.frame.canvas.height;
  7795. ctx.clearRect(0, 0, w, h);
  7796. // set scaling and translation
  7797. ctx.save();
  7798. ctx.translate(this.translation.x, this.translation.y);
  7799. ctx.scale(this.scale, this.scale);
  7800. this._drawEdges(ctx);
  7801. this._drawNodes(ctx);
  7802. this._drawPackages(ctx);
  7803. this._drawSlider();
  7804. // restore original scaling and translation
  7805. ctx.restore();
  7806. };
  7807. /**
  7808. * Set the translation of the graph
  7809. * @param {Number} offsetX Horizontal offset
  7810. * @param {Number} offsetY Vertical offset
  7811. */
  7812. Graph.prototype._setTranslation = function(offsetX, offsetY) {
  7813. if (this.translation === undefined) {
  7814. this.translation = {
  7815. "x": 0,
  7816. "y": 0
  7817. };
  7818. }
  7819. if (offsetX !== undefined) {
  7820. this.translation.x = offsetX;
  7821. }
  7822. if (offsetY !== undefined) {
  7823. this.translation.y = offsetY;
  7824. }
  7825. };
  7826. /**
  7827. * Get the translation of the graph
  7828. * @return {Object} translation An object with parameters x and y, both a number
  7829. */
  7830. Graph.prototype._getTranslation = function() {
  7831. return {
  7832. "x": this.translation.x,
  7833. "y": this.translation.y
  7834. };
  7835. };
  7836. /**
  7837. * Scale the graph
  7838. * @param {Number} scale Scaling factor 1.0 is unscaled
  7839. */
  7840. Graph.prototype._setScale = function(scale) {
  7841. this.scale = scale;
  7842. };
  7843. /**
  7844. * Get the current scale of the graph
  7845. * @return {Number} scale Scaling factor 1.0 is unscaled
  7846. */
  7847. Graph.prototype._getScale = function() {
  7848. return this.scale;
  7849. };
  7850. Graph.prototype._xToCanvas = function(x) {
  7851. return (x - this.translation.x) / this.scale;
  7852. };
  7853. Graph.prototype._canvasToX = function(x) {
  7854. return x * this.scale + this.translation.x;
  7855. };
  7856. Graph.prototype._yToCanvas = function(y) {
  7857. return (y - this.translation.y) / this.scale;
  7858. };
  7859. Graph.prototype._canvasToY = function(y) {
  7860. return y * this.scale + this.translation.y ;
  7861. };
  7862. /**
  7863. * Get a node by its id
  7864. * @param {number} id
  7865. * @return {Node} node, or null if not found
  7866. */
  7867. Graph.prototype._getNode = function(id) {
  7868. for (var i = 0; i < this.nodes.length; i++) {
  7869. if (this.nodes[i].id == id)
  7870. return this.nodes[i];
  7871. }
  7872. return null;
  7873. };
  7874. /**
  7875. * Redraw all nodes
  7876. * The 2d context of a HTML canvas can be retrieved by canvas.getContext("2d");
  7877. * @param {CanvasRenderingContext2D} ctx
  7878. */
  7879. Graph.prototype._drawNodes = function(ctx) {
  7880. // first draw the unselected nodes
  7881. var nodes = this.nodes;
  7882. var selected = [];
  7883. for (var i = 0, iMax = nodes.length; i < iMax; i++) {
  7884. if (nodes[i].isSelected()) {
  7885. selected.push(i);
  7886. }
  7887. else {
  7888. nodes[i].draw(ctx);
  7889. }
  7890. }
  7891. // draw the selected nodes on top
  7892. for (var s = 0, sMax = selected.length; s < sMax; s++) {
  7893. nodes[selected[s]].draw(ctx);
  7894. }
  7895. };
  7896. /**
  7897. * Redraw all edges
  7898. * The 2d context of a HTML canvas can be retrieved by canvas.getContext("2d");
  7899. * @param {CanvasRenderingContext2D} ctx
  7900. */
  7901. Graph.prototype._drawEdges = function(ctx) {
  7902. var edges = this.edges;
  7903. for (var i = 0, iMax = edges.length; i < iMax; i++) {
  7904. edges[i].draw(ctx);
  7905. }
  7906. };
  7907. /**
  7908. * Redraw all packages
  7909. * The 2d context of a HTML canvas can be retrieved by canvas.getContext("2d");
  7910. * @param {CanvasRenderingContext2D} ctx
  7911. */
  7912. Graph.prototype._drawPackages = function(ctx) {
  7913. var packages = this.packages;
  7914. for (var i = 0, iMax = packages.length; i < iMax; i++) {
  7915. packages[i].draw(ctx);
  7916. }
  7917. };
  7918. /**
  7919. * Redraw the filter
  7920. */
  7921. Graph.prototype._drawSlider = function() {
  7922. var sliderNode;
  7923. if (this.hasTimestamps) {
  7924. sliderNode = this.frame.slider;
  7925. if (sliderNode === undefined) {
  7926. sliderNode = document.createElement( "div" );
  7927. sliderNode.style.position = "absolute";
  7928. sliderNode.style.bottom = "0px";
  7929. sliderNode.style.left = "0px";
  7930. sliderNode.style.right = "0px";
  7931. sliderNode.style.backgroundColor = "rgba(255, 255, 255, 0.7)";
  7932. this.frame.slider = sliderNode;
  7933. this.frame.slider.style.padding = "10px";
  7934. //this.frame.filter.style.backgroundColor = "#EFEFEF";
  7935. this.frame.appendChild(sliderNode);
  7936. var range = this._getRange();
  7937. this.slider = new Graph.Slider(sliderNode);
  7938. this.slider.setLoop(false);
  7939. this.slider.setRange(range.start, range.end);
  7940. // create an event handler
  7941. var me = this;
  7942. var onchange = function () {
  7943. var timestamp = me.slider.getValue();
  7944. me.setTimestamp(timestamp);
  7945. // TODO: do only a redraw when the graph is not still moving
  7946. me.redraw();
  7947. };
  7948. this.slider.setOnChangeCallback(onchange);
  7949. onchange(); // perform the first update by hand.
  7950. }
  7951. }
  7952. else {
  7953. sliderNode = this.frame.slider;
  7954. if (sliderNode !== undefined) {
  7955. this.frame.removeChild(sliderNode);
  7956. this.frame.slider = undefined;
  7957. this.slider = undefined;
  7958. }
  7959. }
  7960. };
  7961. /**
  7962. * Recalculate the best positions for all nodes
  7963. */
  7964. Graph.prototype._reposition = function() {
  7965. // TODO: implement function reposition
  7966. /*
  7967. var w = this.frame.canvas.clientWidth;
  7968. var h = this.frame.canvas.clientHeight;
  7969. for (var i = 0; i < this.nodes.length; i++) {
  7970. if (!this.nodes[i].xFixed) this.nodes[i].x = w * Math.random();
  7971. if (!this.nodes[i].yFixed) this.nodes[i].y = h * Math.random();
  7972. }
  7973. //*/
  7974. //*
  7975. // TODO
  7976. var radius = this.constants.edges.length * 2;
  7977. var cx = this.frame.canvas.clientWidth / 2;
  7978. var cy = this.frame.canvas.clientHeight / 2;
  7979. for (var i = 0; i < this.nodes.length; i++) {
  7980. var angle = 2*Math.PI * (i / this.nodes.length);
  7981. if (!this.nodes[i].xFixed) this.nodes[i].x = cx + radius * Math.cos(angle);
  7982. if (!this.nodes[i].yFixed) this.nodes[i].y = cy + radius * Math.sin(angle);
  7983. }
  7984. //*/
  7985. /*
  7986. // TODO
  7987. var radius = this.constants.edges.length * 2;
  7988. var w = this.frame.canvas.clientWidth,
  7989. h = this.frame.canvas.clientHeight;
  7990. var cx = this.frame.canvas.clientWidth / 2;
  7991. var cy = this.frame.canvas.clientHeight / 2;
  7992. var s = Math.sqrt(this.nodes.length);
  7993. for (var i = 0; i < this.nodes.length; i++) {
  7994. //var angle = 2*Math.PI * (i / this.nodes.length);
  7995. if (!this.nodes[i].xFixed) this.nodes[i].x = w/s * (i % s);
  7996. if (!this.nodes[i].yFixed) this.nodes[i].y = h/s * (i / s);
  7997. }
  7998. //*/
  7999. /*
  8000. var cx = this.frame.canvas.clientWidth / 2;
  8001. var cy = this.frame.canvas.clientHeight / 2;
  8002. for (var i = 0; i < this.nodes.length; i++) {
  8003. this.nodes[i].x = cx;
  8004. this.nodes[i].y = cy;
  8005. }
  8006. //*/
  8007. };
  8008. /**
  8009. * Find a stable position for all nodes
  8010. */
  8011. Graph.prototype._doStabilize = function() {
  8012. var start = new Date();
  8013. // find stable position
  8014. var count = 0;
  8015. var vmin = this.constants.minVelocity;
  8016. var stable = false;
  8017. while (!stable && count < this.constants.maxIterations) {
  8018. this._calculateForces();
  8019. this._discreteStepNodes();
  8020. stable = !this.isMoving(vmin);
  8021. count++;
  8022. }
  8023. var end = new Date();
  8024. //console.log("Stabilized in " + (end-start) + " ms, " + count + " iterations" ); // TODO: cleanup
  8025. };
  8026. /**
  8027. * Calculate the external forces acting on the nodes
  8028. * Forces are caused by: edges, repulsing forces between nodes, gravity
  8029. */
  8030. Graph.prototype._calculateForces = function(nodeId) {
  8031. // create a local edge to the nodes and edges, that is faster
  8032. var nodes = this.nodes,
  8033. edges = this.edges;
  8034. // gravity, add a small constant force to pull the nodes towards the center of
  8035. // the graph
  8036. // Also, the forces are reset to zero in this loop by using _setForce instead
  8037. // of _addForce
  8038. var gravity = 0.01,
  8039. gx = this.frame.canvas.clientWidth / 2,
  8040. gy = this.frame.canvas.clientHeight / 2;
  8041. for (var n = 0; n < nodes.length; n++) {
  8042. var dx = gx - nodes[n].x,
  8043. dy = gy - nodes[n].y,
  8044. angle = Math.atan2(dy, dx),
  8045. fx = Math.cos(angle) * gravity,
  8046. fy = Math.sin(angle) * gravity;
  8047. this.nodes[n]._setForce(fx, fy);
  8048. }
  8049. // repulsing forces between nodes
  8050. var minimumDistance = this.constants.nodes.distance,
  8051. steepness = 10; // higher value gives steeper slope of the force around the given minimumDistance
  8052. for (var n = 0; n < nodes.length; n++) {
  8053. for (var n2 = n + 1; n2 < this.nodes.length; n2++) {
  8054. //var dmin = (nodes[n].width + nodes[n].height + nodes[n2].width + nodes[n2].height) / 1 || minimumDistance, // TODO: dmin
  8055. //var dmin = (nodes[n].width + nodes[n2].width)/2 || minimumDistance, // TODO: dmin
  8056. //dmin = 40 + ((nodes[n].width/2 + nodes[n2].width/2) || 0),
  8057. // calculate normally distributed force
  8058. var dx = nodes[n2].x - nodes[n].x,
  8059. dy = nodes[n2].y - nodes[n].y,
  8060. distance = Math.sqrt(dx * dx + dy * dy),
  8061. angle = Math.atan2(dy, dx),
  8062. // TODO: correct factor for repulsing force
  8063. //var repulsingforce = 2 * Math.exp(-5 * (distance * distance) / (dmin * dmin) ); // TODO: customize the repulsing force
  8064. //repulsingforce = Math.exp(-1 * (distance * distance) / (dmin * dmin) ), // TODO: customize the repulsing force
  8065. repulsingforce = 1 / (1 + Math.exp((distance / minimumDistance - 1) * steepness)), // TODO: customize the repulsing force
  8066. fx = Math.cos(angle) * repulsingforce,
  8067. fy = Math.sin(angle) * repulsingforce;
  8068. this.nodes[n]._addForce(-fx, -fy);
  8069. this.nodes[n2]._addForce(fx, fy);
  8070. }
  8071. /* TODO: re-implement repulsion of edges
  8072. for (var l = 0; l < edges.length; l++) {
  8073. var lx = edges[l].from.x+(edges[l].to.x - edges[l].from.x)/2,
  8074. ly = edges[l].from.y+(edges[l].to.y - edges[l].from.y)/2,
  8075. // calculate normally distributed force
  8076. dx = nodes[n].x - lx,
  8077. dy = nodes[n].y - ly,
  8078. distance = Math.sqrt(dx * dx + dy * dy),
  8079. angle = Math.atan2(dy, dx),
  8080. // TODO: correct factor for repulsing force
  8081. //var repulsingforce = 2 * Math.exp(-5 * (distance * distance) / (dmin * dmin) ); // TODO: customize the repulsing force
  8082. //repulsingforce = Math.exp(-1 * (distance * distance) / (dmin * dmin) ), // TODO: customize the repulsing force
  8083. repulsingforce = 1 / (1 + Math.exp((distance / (minimumDistance / 2) - 1) * steepness)), // TODO: customize the repulsing force
  8084. fx = Math.cos(angle) * repulsingforce,
  8085. fy = Math.sin(angle) * repulsingforce;
  8086. nodes[n]._addForce(fx, fy);
  8087. edges[l].from._addForce(-fx/2,-fy/2);
  8088. edges[l].to._addForce(-fx/2,-fy/2);
  8089. }
  8090. */
  8091. }
  8092. // forces caused by the edges, modelled as springs
  8093. for (var l = 0, lMax = edges.length; l < lMax; l++) {
  8094. var edge = edges[l],
  8095. dx = (edge.to.x - edge.from.x),
  8096. dy = (edge.to.y - edge.from.y),
  8097. //edgeLength = (edge.from.width + edge.from.height + edge.to.width + edge.to.height)/2 || edge.length, // TODO: dmin
  8098. //edgeLength = (edge.from.width + edge.to.width)/2 || edge.length, // TODO: dmin
  8099. //edgeLength = 20 + ((edge.from.width + edge.to.width) || 0) / 2,
  8100. edgeLength = edge.length,
  8101. length = Math.sqrt(dx * dx + dy * dy),
  8102. angle = Math.atan2(dy, dx),
  8103. springforce = edge.stiffness * (edgeLength - length),
  8104. fx = Math.cos(angle) * springforce,
  8105. fy = Math.sin(angle) * springforce;
  8106. edge.from._addForce(-fx, -fy);
  8107. edge.to._addForce(fx, fy);
  8108. }
  8109. /* TODO: re-implement repulsion of edges
  8110. // repulsing forces between edges
  8111. var minimumDistance = this.constants.edges.distance,
  8112. steepness = 10; // higher value gives steeper slope of the force around the given minimumDistance
  8113. for (var l = 0; l < edges.length; l++) {
  8114. //Keep distance from other edge centers
  8115. for (var l2 = l + 1; l2 < this.edges.length; l2++) {
  8116. //var dmin = (nodes[n].width + nodes[n].height + nodes[n2].width + nodes[n2].height) / 1 || minimumDistance, // TODO: dmin
  8117. //var dmin = (nodes[n].width + nodes[n2].width)/2 || minimumDistance, // TODO: dmin
  8118. //dmin = 40 + ((nodes[n].width/2 + nodes[n2].width/2) || 0),
  8119. var lx = edges[l].from.x+(edges[l].to.x - edges[l].from.x)/2,
  8120. ly = edges[l].from.y+(edges[l].to.y - edges[l].from.y)/2,
  8121. l2x = edges[l2].from.x+(edges[l2].to.x - edges[l2].from.x)/2,
  8122. l2y = edges[l2].from.y+(edges[l2].to.y - edges[l2].from.y)/2,
  8123. // calculate normally distributed force
  8124. dx = l2x - lx,
  8125. dy = l2y - ly,
  8126. distance = Math.sqrt(dx * dx + dy * dy),
  8127. angle = Math.atan2(dy, dx),
  8128. // TODO: correct factor for repulsing force
  8129. //var repulsingforce = 2 * Math.exp(-5 * (distance * distance) / (dmin * dmin) ); // TODO: customize the repulsing force
  8130. //repulsingforce = Math.exp(-1 * (distance * distance) / (dmin * dmin) ), // TODO: customize the repulsing force
  8131. repulsingforce = 1 / (1 + Math.exp((distance / minimumDistance - 1) * steepness)), // TODO: customize the repulsing force
  8132. fx = Math.cos(angle) * repulsingforce,
  8133. fy = Math.sin(angle) * repulsingforce;
  8134. edges[l].from._addForce(-fx, -fy);
  8135. edges[l].to._addForce(-fx, -fy);
  8136. edges[l2].from._addForce(fx, fy);
  8137. edges[l2].to._addForce(fx, fy);
  8138. }
  8139. }
  8140. */
  8141. };
  8142. /**
  8143. * Check if any of the nodes is still moving
  8144. * @param {number} vmin the minimum velocity considered as "moving"
  8145. * @return {boolean} true if moving, false if non of the nodes is moving
  8146. */
  8147. Graph.prototype.isMoving = function(vmin) {
  8148. // TODO: ismoving does not work well: should check the kinetic energy, not its velocity
  8149. var nodes = this.nodes;
  8150. for (var n = 0, nMax = nodes.length; n < nMax; n++) {
  8151. if (nodes[n].isMoving(vmin)) {
  8152. return true;
  8153. }
  8154. }
  8155. return false;
  8156. };
  8157. /**
  8158. * Perform one discrete step for all nodes
  8159. */
  8160. Graph.prototype._discreteStepNodes = function() {
  8161. var interval = this.refreshRate / 1000.0; // in seconds
  8162. var nodes = this.nodes;
  8163. for (var n = 0, nMax = nodes.length; n < nMax; n++) {
  8164. nodes[n].discreteStep(interval);
  8165. }
  8166. };
  8167. /**
  8168. * Perform one discrete step for all packages
  8169. */
  8170. Graph.prototype._discreteStepPackages = function() {
  8171. var interval = this.refreshRate / 1000.0; // in seconds
  8172. var packages = this.packages;
  8173. for (var n = 0, nMax = packages.length; n < nMax; n++) {
  8174. packages[n].discreteStep(interval);
  8175. }
  8176. };
  8177. /**
  8178. * Cleanup finished packages.
  8179. * also checks if there are moving packages
  8180. */
  8181. Graph.prototype._deleteFinishedPackages = function() {
  8182. var n = 0;
  8183. var hasMovingPackages = false;
  8184. while (n < this.packages.length) {
  8185. if (this.packages[n].isFinished()) {
  8186. this.packages.splice(n, 1);
  8187. n--;
  8188. }
  8189. else if (this.packages[n].isMoving()) {
  8190. hasMovingPackages = true;
  8191. }
  8192. n++;
  8193. }
  8194. this.hasMovingPackages = hasMovingPackages;
  8195. };
  8196. /**
  8197. * Start animating nodes, edges, and packages.
  8198. */
  8199. Graph.prototype.start = function() {
  8200. if (this.hasMovingNodes) {
  8201. this._calculateForces();
  8202. this._discreteStepNodes();
  8203. var vmin = this.constants.minVelocity;
  8204. this.hasMovingNodes = this.isMoving(vmin);
  8205. }
  8206. if (this.hasMovingPackages) {
  8207. this._discreteStepPackages();
  8208. this._deleteFinishedPackages();
  8209. }
  8210. if (this.hasMovingNodes || this.hasMovingEdges || this.hasMovingPackages) {
  8211. // start animation. only start timer if it is not already running
  8212. if (!this.timer) {
  8213. var graph = this;
  8214. this.timer = window.setTimeout(function () {
  8215. graph.timer = undefined;
  8216. graph.start();
  8217. graph._redraw();
  8218. }, this.refreshRate);
  8219. }
  8220. }
  8221. else {
  8222. this._redraw();
  8223. }
  8224. };
  8225. /**
  8226. * Stop animating nodes, edges, and packages.
  8227. */
  8228. Graph.prototype.stop = function () {
  8229. if (this.timer) {
  8230. window.clearInterval(this.timer);
  8231. this.timer = undefined;
  8232. }
  8233. };
  8234. /**--------------------------------------------------------------------------**/
  8235. /**
  8236. * Add and event listener. Works for all browsers
  8237. * @param {Element} element An html element
  8238. * @param {String} action The action, for example "click",
  8239. * without the prefix "on"
  8240. * @param {function} listener The callback function to be executed
  8241. * @param {boolean} useCapture
  8242. */
  8243. Graph.addEventListener = function (element, action, listener, useCapture) {
  8244. if (element.addEventListener) {
  8245. if (useCapture === undefined)
  8246. useCapture = false;
  8247. if (action === "mousewheel" && navigator.userAgent.indexOf("Firefox") >= 0) {
  8248. action = "DOMMouseScroll"; // For Firefox
  8249. }
  8250. element.addEventListener(action, listener, useCapture);
  8251. } else {
  8252. element.attachEvent("on" + action, listener); // IE browsers
  8253. }
  8254. };
  8255. /**
  8256. * Remove an event listener from an element
  8257. * @param {Element} element An html dom element
  8258. * @param {string} action The name of the event, for example "mousedown"
  8259. * @param {function} listener The listener function
  8260. * @param {boolean} useCapture
  8261. */
  8262. Graph.removeEventListener = function(element, action, listener, useCapture) {
  8263. if (element.removeEventListener) {
  8264. // non-IE browsers
  8265. if (useCapture === undefined)
  8266. useCapture = false;
  8267. if (action === "mousewheel" && navigator.userAgent.indexOf("Firefox") >= 0) {
  8268. action = "DOMMouseScroll"; // For Firefox
  8269. }
  8270. element.removeEventListener(action, listener, useCapture);
  8271. } else {
  8272. // IE browsers
  8273. element.detachEvent("on" + action, listener);
  8274. }
  8275. };
  8276. /**
  8277. * Stop event propagation
  8278. */
  8279. Graph.stopPropagation = function (event) {
  8280. if (!event)
  8281. event = window.event;
  8282. if (event.stopPropagation) {
  8283. event.stopPropagation(); // non-IE browsers
  8284. }
  8285. else {
  8286. event.cancelBubble = true; // IE browsers
  8287. }
  8288. };
  8289. /**
  8290. * Cancels the event if it is cancelable, without stopping further propagation of the event.
  8291. */
  8292. Graph.preventDefault = function (event) {
  8293. if (!event)
  8294. event = window.event;
  8295. if (event.preventDefault) {
  8296. event.preventDefault(); // non-IE browsers
  8297. }
  8298. else {
  8299. event.returnValue = false; // IE browsers
  8300. }
  8301. };
  8302. /**
  8303. * Retrieve the absolute left value of a DOM element
  8304. * @param {Element} elem A dom element, for example a div
  8305. * @return {number} left The absolute left position of this element
  8306. * in the browser page.
  8307. */
  8308. Graph._getAbsoluteLeft = function(elem) {
  8309. var left = 0;
  8310. while( elem != null ) {
  8311. left += elem.offsetLeft;
  8312. left -= elem.scrollLeft;
  8313. elem = elem.offsetParent;
  8314. }
  8315. if (!document.body.scrollLeft && window.pageXOffset) {
  8316. // FF
  8317. left -= window.pageXOffset;
  8318. }
  8319. return left;
  8320. };
  8321. /**
  8322. * Retrieve the absolute top value of a DOM element
  8323. * @param {Element} elem A dom element, for example a div
  8324. * @return {number} top The absolute top position of this element
  8325. * in the browser page.
  8326. */
  8327. Graph._getAbsoluteTop = function(elem) {
  8328. var top = 0;
  8329. while( elem != null ) {
  8330. top += elem.offsetTop;
  8331. top -= elem.scrollTop;
  8332. elem = elem.offsetParent;
  8333. }
  8334. if (!document.body.scrollTop && window.pageYOffset) {
  8335. // FF
  8336. top -= window.pageYOffset;
  8337. }
  8338. return top;
  8339. };
  8340. /**--------------------------------------------------------------------------**/
  8341. /**
  8342. * @class Node
  8343. * A node. A node can be connected to other nodes via one or multiple edges.
  8344. * @param {object} properties An object containing properties for the node. All
  8345. * properties are optional, except for the id.
  8346. * {number} id Id of the node. Required
  8347. * {string} text Title for the node
  8348. * {number} x Horizontal position of the node
  8349. * {number} y Vertical position of the node
  8350. * {string} style Drawing style, available:
  8351. * "database", "circle", "rect",
  8352. * "image", "text", "dot", "star",
  8353. * "triangle", "triangleDown",
  8354. * "square"
  8355. * {string} image An image url
  8356. * {string} title An title text, can be HTML
  8357. * {anytype} group A group name or number
  8358. * @param {Graph.Images} imagelist A list with images. Only needed
  8359. * when the node has an image
  8360. * @param {Graph.Groups} grouplist A list with groups. Needed for
  8361. * retrieving group properties
  8362. * @param {Object} constants An object with default values for
  8363. * example for the color
  8364. */
  8365. Graph.Node = function (properties, imagelist, grouplist, constants) {
  8366. this.selected = false;
  8367. this.edges = []; // all edges connected to this node
  8368. this.group = constants.nodes.group;
  8369. this.fontSize = constants.nodes.fontSize;
  8370. this.fontFace = constants.nodes.fontFace;
  8371. this.fontColor = constants.nodes.fontColor;
  8372. this.borderColor = constants.nodes.borderColor;
  8373. this.backgroundColor = constants.nodes.backgroundColor;
  8374. this.highlightColor = constants.nodes.highlightColor;
  8375. // set defaults for the properties
  8376. this.id = undefined;
  8377. this.style = constants.nodes.style;
  8378. this.image = constants.nodes.image;
  8379. this.x = 0;
  8380. this.y = 0;
  8381. this.xFixed = false;
  8382. this.yFixed = false;
  8383. this.radius = constants.nodes.radius;
  8384. this.radiusFixed = false;
  8385. this.radiusMin = constants.nodes.radiusMin;
  8386. this.radiusMax = constants.nodes.radiusMax;
  8387. this.imagelist = imagelist;
  8388. this.grouplist = grouplist;
  8389. this.setProperties(properties, constants);
  8390. // mass, force, velocity
  8391. this.mass = 50; // kg (mass is adjusted for the number of connected edges)
  8392. this.fx = 0.0; // external force x
  8393. this.fy = 0.0; // external force y
  8394. this.vx = 0.0; // velocity x
  8395. this.vy = 0.0; // velocity y
  8396. this.minForce = constants.minForce;
  8397. this.damping = 0.9; // damping factor
  8398. };
  8399. /**
  8400. * Attach a edge to the node
  8401. * @param {Graph.Edge} edge
  8402. */
  8403. Graph.Node.prototype.attachEdge = function(edge) {
  8404. this.edges.push(edge);
  8405. this._updateMass();
  8406. };
  8407. /**
  8408. * Detach a edge from the node
  8409. * @param {Graph.Edge} edge
  8410. */
  8411. Graph.Node.prototype.detachEdge = function(edge) {
  8412. var index = this.edges.indexOf(edge);
  8413. if (index != -1) {
  8414. this.edges.splice(index, 1);
  8415. }
  8416. this._updateMass();
  8417. };
  8418. /**
  8419. * Update the nodes mass, which is determined by the number of edges connecting
  8420. * to it (more edges -> heavier node).
  8421. * @private
  8422. */
  8423. Graph.Node.prototype._updateMass = function() {
  8424. this.mass = 50 + 20 * this.edges.length; // kg
  8425. };
  8426. /**
  8427. * Set or overwrite properties for the node
  8428. * @param {Object} properties an object with properties
  8429. * @param {Object} constants and object with default, global properties
  8430. */
  8431. Graph.Node.prototype.setProperties = function(properties, constants) {
  8432. if (!properties) {
  8433. return;
  8434. }
  8435. // basic properties
  8436. if (properties.id != undefined) {this.id = properties.id;}
  8437. if (properties.text != undefined) {this.text = properties.text;}
  8438. if (properties.title != undefined) {this.title = properties.title;}
  8439. if (properties.group != undefined) {this.group = properties.group;}
  8440. if (properties.x != undefined) {this.x = properties.x;}
  8441. if (properties.y != undefined) {this.y = properties.y;}
  8442. if (properties.value != undefined) {this.value = properties.value;}
  8443. if (properties.timestamp != undefined) {this.timestamp = properties.timestamp;}
  8444. if (this.id === undefined) {
  8445. throw "Node must have an id";
  8446. }
  8447. // copy group properties
  8448. if (this.group) {
  8449. var groupObj = this.grouplist.get(this.group);
  8450. for (var prop in groupObj) {
  8451. if (groupObj.hasOwnProperty(prop)) {
  8452. this[prop] = groupObj[prop];
  8453. }
  8454. }
  8455. }
  8456. // individual style properties
  8457. if (properties.style != undefined) {this.style = properties.style;}
  8458. if (properties.image != undefined) {this.image = properties.image;}
  8459. if (properties.radius != undefined) {this.radius = properties.radius;}
  8460. if (properties.borderColor != undefined) {this.borderColor = properties.borderColor;}
  8461. if (properties.backgroundColor != undefined){this.backgroundColor = properties.backgroundColor;}
  8462. if (properties.highlightColor != undefined) {this.highlightColor = properties.highlightColor;}
  8463. if (properties.fontColor != undefined) {this.fontColor = properties.fontColor;}
  8464. if (properties.fontSize != undefined) {this.fontSize = properties.fontSize;}
  8465. if (properties.fontFace != undefined) {this.fontFace = properties.fontFace;}
  8466. if (this.image != undefined) {
  8467. if (this.imagelist) {
  8468. this.imageObj = this.imagelist.load(this.image);
  8469. }
  8470. else {
  8471. throw "No imagelist provided";
  8472. }
  8473. }
  8474. this.xFixed = this.xFixed || (properties.x != undefined);
  8475. this.yFixed = this.yFixed || (properties.y != undefined);
  8476. this.radiusFixed = this.radiusFixed || (properties.radius != undefined);
  8477. if (this.style == 'image') {
  8478. this.radiusMin = constants.nodes.widthMin;
  8479. this.radiusMax = constants.nodes.widthMax;
  8480. }
  8481. // choose draw method depending on the style
  8482. var style = this.style;
  8483. switch (style) {
  8484. case 'database': this.draw = this._drawDatabase; this.resize = this._resizeDatabase; break;
  8485. case 'rect': this.draw = this._drawRect; this.resize = this._resizeRect; break;
  8486. case 'circle': this.draw = this._drawCircle; this.resize = this._resizeCircle; break;
  8487. // TODO: add ellipse shape
  8488. // TODO: add diamond shape
  8489. case 'image': this.draw = this._drawImage; this.resize = this._resizeImage; break;
  8490. case 'text': this.draw = this._drawText; this.resize = this._resizeText; break;
  8491. case 'dot': this.draw = this._drawDot; this.resize = this._resizeShape; break;
  8492. case 'square': this.draw = this._drawSquare; this.resize = this._resizeShape; break;
  8493. case 'triangle': this.draw = this._drawTriangle; this.resize = this._resizeShape; break;
  8494. case 'triangleDown': this.draw = this._drawTriangleDown; this.resize = this._resizeShape; break;
  8495. case 'star': this.draw = this._drawStar; this.resize = this._resizeShape; break;
  8496. default: this.draw = this._drawRect; this.resize = this._resizeRect; break;
  8497. }
  8498. // reset the size of the node, this can be changed
  8499. this._reset();
  8500. };
  8501. /**
  8502. * select this node
  8503. */
  8504. Graph.Node.prototype.select = function() {
  8505. this.selected = true;
  8506. this._reset();
  8507. };
  8508. /**
  8509. * unselect this node
  8510. */
  8511. Graph.Node.prototype.unselect = function() {
  8512. this.selected = false;
  8513. this._reset();
  8514. };
  8515. /**
  8516. * Reset the calculated size of the node, forces it to recalculate its size
  8517. */
  8518. Graph.Node.prototype._reset = function() {
  8519. this.width = undefined;
  8520. this.height = undefined;
  8521. };
  8522. /**
  8523. * get the title of this node.
  8524. * @return {string} title The title of the node, or undefined when no title
  8525. * has been set.
  8526. */
  8527. Graph.Node.prototype.getTitle = function() {
  8528. return this.title;
  8529. };
  8530. /**
  8531. * Calculate the distance to the border of the Node
  8532. * @param {CanvasRenderingContext2D} ctx
  8533. * @param {Number} angle Angle in radians
  8534. * @returns {number} distance Distance to the border in pixels
  8535. */
  8536. Graph.Node.prototype.distanceToBorder = function (ctx, angle) {
  8537. var borderWidth = 1;
  8538. if (!this.width) {
  8539. this.resize(ctx);
  8540. }
  8541. //noinspection FallthroughInSwitchStatementJS
  8542. switch (this.style) {
  8543. case 'circle':
  8544. case 'dot':
  8545. return this.radius + borderWidth;
  8546. // TODO: implement distanceToBorder for database
  8547. // TODO: implement distanceToBorder for triangle
  8548. // TODO: implement distanceToBorder for triangleDown
  8549. case 'rect':
  8550. case 'image':
  8551. case 'text':
  8552. default:
  8553. if (this.width) {
  8554. return Math.min(
  8555. Math.abs(this.width / 2 / Math.cos(angle)),
  8556. Math.abs(this.height / 2 / Math.sin(angle))) + borderWidth;
  8557. // TODO: reckon with border radius too in case of rect
  8558. }
  8559. else {
  8560. return 0;
  8561. }
  8562. }
  8563. // TODO: implement calculation of distance to border for all shapes
  8564. };
  8565. /**
  8566. * Set forces acting on the node
  8567. * @param {number} fx Force in horizontal direction
  8568. * @param {number} fy Force in vertical direction
  8569. */
  8570. Graph.Node.prototype._setForce = function(fx, fy) {
  8571. this.fx = fx;
  8572. this.fy = fy;
  8573. };
  8574. /**
  8575. * Add forces acting on the node
  8576. * @param {number} fx Force in horizontal direction
  8577. * @param {number} fy Force in vertical direction
  8578. */
  8579. Graph.Node.prototype._addForce = function(fx, fy) {
  8580. this.fx += fx;
  8581. this.fy += fy;
  8582. };
  8583. /**
  8584. * Perform one discrete step for the node
  8585. * @param {number} interval Time interval in seconds
  8586. */
  8587. Graph.Node.prototype.discreteStep = function(interval) {
  8588. if (!this.xFixed) {
  8589. var dx = -this.damping * this.vx; // damping force
  8590. var ax = (this.fx + dx) / this.mass; // acceleration
  8591. this.vx += ax / interval; // velocity
  8592. this.x += this.vx / interval; // position
  8593. }
  8594. if (!this.yFixed) {
  8595. var dy = -this.damping * this.vy; // damping force
  8596. var ay = (this.fy + dy) / this.mass; // acceleration
  8597. this.vy += ay / interval; // velocity
  8598. this.y += this.vy / interval; // position
  8599. }
  8600. };
  8601. /**
  8602. * Check if this node has a fixed x and y position
  8603. * @return {boolean} true if fixed, false if not
  8604. */
  8605. Graph.Node.prototype.isFixed = function() {
  8606. return (this.xFixed && this.yFixed);
  8607. };
  8608. /**
  8609. * Check if this node is moving
  8610. * @param {number} vmin the minimum velocity considered as "moving"
  8611. * @return {boolean} true if moving, false if it has no velocity
  8612. */
  8613. // TODO: replace this method with calculating the kinetic energy
  8614. Graph.Node.prototype.isMoving = function(vmin) {
  8615. return (Math.abs(this.vx) > vmin || Math.abs(this.vy) > vmin ||
  8616. (!this.xFixed && Math.abs(this.fx) > this.minForce) ||
  8617. (!this.yFixed && Math.abs(this.fy) > this.minForce));
  8618. };
  8619. /**
  8620. * check if this node is selecte
  8621. * @return {boolean} selected True if node is selected, else false
  8622. */
  8623. Graph.Node.prototype.isSelected = function() {
  8624. return this.selected;
  8625. };
  8626. /**
  8627. * Retrieve the value of the node. Can be undefined
  8628. * @return {Number} value
  8629. */
  8630. Graph.Node.prototype.getValue = function() {
  8631. return this.value;
  8632. };
  8633. /**
  8634. * Calculate the distance from the nodes location to the given location (x,y)
  8635. * @param {Number} x
  8636. * @param {Number} y
  8637. * @return {Number} value
  8638. */
  8639. Graph.Node.prototype.getDistance = function(x, y) {
  8640. var dx = this.x - x,
  8641. dy = this.y - y;
  8642. return Math.sqrt(dx * dx + dy * dy);
  8643. };
  8644. /**
  8645. * Adjust the value range of the node. The node will adjust it's radius
  8646. * based on its value.
  8647. * @param {Number} min
  8648. * @param {Number} max
  8649. */
  8650. Graph.Node.prototype.setValueRange = function(min, max) {
  8651. if (!this.radiusFixed && this.value !== undefined) {
  8652. var scale = (this.radiusMax - this.radiusMin) / (max - min);
  8653. this.radius = (this.value - min) * scale + this.radiusMin;
  8654. }
  8655. };
  8656. /**
  8657. * Draw this node in the given canvas
  8658. * The 2d context of a HTML canvas can be retrieved by canvas.getContext("2d");
  8659. * @param {CanvasRenderingContext2D} ctx
  8660. */
  8661. Graph.Node.prototype.draw = function(ctx) {
  8662. throw "Draw method not initialized for node";
  8663. };
  8664. /**
  8665. * Recalculate the size of this node in the given canvas
  8666. * The 2d context of a HTML canvas can be retrieved by canvas.getContext("2d");
  8667. * @param {CanvasRenderingContext2D} ctx
  8668. */
  8669. Graph.Node.prototype.resize = function(ctx) {
  8670. throw "Resize method not initialized for node";
  8671. };
  8672. /**
  8673. * Check if this object is overlapping with the provided object
  8674. * @param {Object} obj an object with parameters left, top, right, bottom
  8675. * @return {boolean} True if location is located on node
  8676. */
  8677. Graph.Node.prototype.isOverlappingWith = function(obj) {
  8678. return (this.left < obj.right &&
  8679. this.left + this.width > obj.left &&
  8680. this.top < obj.bottom &&
  8681. this.top + this.height > obj.top);
  8682. };
  8683. Graph.Node.prototype._resizeImage = function (ctx) {
  8684. // TODO: pre calculate the image size
  8685. if (!this.width) { // undefined or 0
  8686. var width, height;
  8687. if (this.value) {
  8688. var scale = this.imageObj.height / this.imageObj.width;
  8689. width = this.radius || this.imageObj.width;
  8690. height = this.radius * scale || this.imageObj.height;
  8691. }
  8692. else {
  8693. width = this.imageObj.width;
  8694. height = this.imageObj.height;
  8695. }
  8696. this.width = width;
  8697. this.height = height;
  8698. }
  8699. };
  8700. Graph.Node.prototype._drawImage = function (ctx) {
  8701. this._resizeImage(ctx);
  8702. this.left = this.x - this.width / 2;
  8703. this.top = this.y - this.height / 2;
  8704. var yText;
  8705. if (this.imageObj) {
  8706. ctx.drawImage(this.imageObj, this.left, this.top, this.width, this.height);
  8707. yText = this.y + this.height / 2;
  8708. }
  8709. else {
  8710. // image still loading... just draw the text for now
  8711. yText = this.y;
  8712. }
  8713. this._text(ctx, this.text, this.x, yText, undefined, "top");
  8714. };
  8715. Graph.Node.prototype._resizeRect = function (ctx) {
  8716. if (!this.width) {
  8717. var margin = 5;
  8718. var textSize = this.getTextSize(ctx);
  8719. this.width = textSize.width + 2 * margin;
  8720. this.height = textSize.height + 2 * margin;
  8721. }
  8722. };
  8723. Graph.Node.prototype._drawRect = function (ctx) {
  8724. this._resizeRect(ctx);
  8725. this.left = this.x - this.width / 2;
  8726. this.top = this.y - this.height / 2;
  8727. ctx.strokeStyle = this.borderColor;
  8728. ctx.fillStyle = this.selected ? this.highlightColor : this.backgroundColor;
  8729. ctx.lineWidth = this.selected ? 2.0 : 1.0;
  8730. ctx.roundRect(this.left, this.top, this.width, this.height, this.radius);
  8731. ctx.fill();
  8732. ctx.stroke();
  8733. this._text(ctx, this.text, this.x, this.y);
  8734. };
  8735. Graph.Node.prototype._resizeDatabase = function (ctx) {
  8736. if (!this.width) {
  8737. var margin = 5;
  8738. var textSize = this.getTextSize(ctx);
  8739. var size = textSize.width + 2 * margin;
  8740. this.width = size;
  8741. this.height = size;
  8742. }
  8743. };
  8744. Graph.Node.prototype._drawDatabase = function (ctx) {
  8745. this._resizeDatabase(ctx);
  8746. this.left = this.x - this.width / 2;
  8747. this.top = this.y - this.height / 2;
  8748. ctx.strokeStyle = this.borderColor;
  8749. ctx.fillStyle = this.selected ? this.highlightColor : this.backgroundColor;
  8750. ctx.lineWidth = this.selected ? 2.0 : 1.0;
  8751. ctx.database(this.x - this.width/2, this.y - this.height*0.5, this.width, this.height);
  8752. ctx.fill();
  8753. ctx.stroke();
  8754. this._text(ctx, this.text, this.x, this.y);
  8755. };
  8756. Graph.Node.prototype._resizeCircle = function (ctx) {
  8757. if (!this.width) {
  8758. var margin = 5;
  8759. var textSize = this.getTextSize(ctx);
  8760. var diameter = Math.max(textSize.width, textSize.height) + 2 * margin;
  8761. this.radius = diameter / 2;
  8762. this.width = diameter;
  8763. this.height = diameter;
  8764. }
  8765. };
  8766. Graph.Node.prototype._drawCircle = function (ctx) {
  8767. this._resizeCircle(ctx);
  8768. this.left = this.x - this.width / 2;
  8769. this.top = this.y - this.height / 2;
  8770. ctx.strokeStyle = this.borderColor;
  8771. ctx.fillStyle = this.selected ? this.highlightColor : this.backgroundColor;
  8772. ctx.lineWidth = this.selected ? 2.0 : 1.0;
  8773. ctx.circle(this.x, this.y, this.radius);
  8774. ctx.fill();
  8775. ctx.stroke();
  8776. this._text(ctx, this.text, this.x, this.y);
  8777. };
  8778. Graph.Node.prototype._drawDot = function (ctx) {
  8779. this._drawShape(ctx, 'circle');
  8780. };
  8781. Graph.Node.prototype._drawTriangle = function (ctx) {
  8782. this._drawShape(ctx, 'triangle');
  8783. };
  8784. Graph.Node.prototype._drawTriangleDown = function (ctx) {
  8785. this._drawShape(ctx, 'triangleDown');
  8786. };
  8787. Graph.Node.prototype._drawSquare = function (ctx) {
  8788. this._drawShape(ctx, 'square');
  8789. };
  8790. Graph.Node.prototype._drawStar = function (ctx) {
  8791. this._drawShape(ctx, 'star');
  8792. };
  8793. Graph.Node.prototype._resizeShape = function (ctx) {
  8794. if (!this.width) {
  8795. var size = 2 * this.radius;
  8796. this.width = size;
  8797. this.height = size;
  8798. }
  8799. };
  8800. Graph.Node.prototype._drawShape = function (ctx, shape) {
  8801. this._resizeShape(ctx);
  8802. this.left = this.x - this.width / 2;
  8803. this.top = this.y - this.height / 2;
  8804. ctx.strokeStyle = this.borderColor;
  8805. ctx.fillStyle = this.selected ? this.highlightColor : this.backgroundColor;
  8806. ctx.lineWidth = this.selected ? 2.0 : 1.0;
  8807. ctx[shape](this.x, this.y, this.radius);
  8808. ctx.fill();
  8809. ctx.stroke();
  8810. if (this.text) {
  8811. this._text(ctx, this.text, this.x, this.y + this.height / 2, undefined, 'top');
  8812. }
  8813. };
  8814. Graph.Node.prototype._resizeText = function (ctx) {
  8815. if (!this.width) {
  8816. var margin = 5;
  8817. var textSize = this.getTextSize(ctx);
  8818. this.width = textSize.width + 2 * margin;
  8819. this.height = textSize.height + 2 * margin;
  8820. }
  8821. };
  8822. Graph.Node.prototype._drawText = function (ctx) {
  8823. this._resizeText(ctx);
  8824. this.left = this.x - this.width / 2;
  8825. this.top = this.y - this.height / 2;
  8826. this._text(ctx, this.text, this.x, this.y);
  8827. };
  8828. Graph.Node.prototype._text = function (ctx, text, x, y, align, baseline) {
  8829. if (text) {
  8830. ctx.font = (this.selected ? "bold " : "") + this.fontSize + "px " + this.fontFace;
  8831. ctx.fillStyle = this.fontColor || "black";
  8832. ctx.textAlign = align || "center";
  8833. ctx.textBaseline = baseline || "middle";
  8834. var lines = text.split('\n'),
  8835. lineCount = lines.length,
  8836. fontSize = (this.fontSize + 4),
  8837. yLine = y + (1 - lineCount) / 2 * fontSize;
  8838. for (var i = 0; i < lineCount; i++) {
  8839. ctx.fillText(lines[i], x, yLine);
  8840. yLine += fontSize;
  8841. }
  8842. }
  8843. };
  8844. Graph.Node.prototype.getTextSize = function(ctx) {
  8845. if (this.text != undefined) {
  8846. ctx.font = (this.selected ? "bold " : "") + this.fontSize + "px " + this.fontFace;
  8847. var lines = this.text.split('\n'),
  8848. height = (this.fontSize + 4) * lines.length,
  8849. width = 0;
  8850. for (var i = 0, iMax = lines.length; i < iMax; i++) {
  8851. width = Math.max(width, ctx.measureText(lines[i]).width);
  8852. }
  8853. return {"width": width, "height": height};
  8854. }
  8855. else {
  8856. return {"width": 0, "height": 0};
  8857. }
  8858. };
  8859. /**--------------------------------------------------------------------------**/
  8860. /**
  8861. * @class Edge
  8862. *
  8863. * A edge connects two nodes
  8864. * @param {Object} properties Object with properties. Must contain
  8865. * At least properties from and to.
  8866. * Available properties: from (number),
  8867. * to (number), color (string),
  8868. * width (number), style (string),
  8869. * length (number), title (string)
  8870. * @param {Graph} graph A graph object, used to find and edge to
  8871. * nodes.
  8872. * @param {Object} constants An object with default values for
  8873. * example for the color
  8874. */
  8875. Graph.Edge = function (properties, graph, constants) {
  8876. if (!graph) {
  8877. throw "No graph provided";
  8878. }
  8879. this.graph = graph;
  8880. // initialize constants
  8881. this.widthMin = constants.edges.widthMin;
  8882. this.widthMax = constants.edges.widthMax;
  8883. // initialize variables
  8884. this.id = undefined;
  8885. this.style = constants.edges.style;
  8886. this.title = undefined;
  8887. this.width = constants.edges.width;
  8888. this.value = undefined;
  8889. this.length = constants.edges.length;
  8890. // Added to support dashed lines
  8891. // David Jordan
  8892. // 2012-08-08
  8893. this.dashlength = constants.edges.dashlength;
  8894. this.dashgap = constants.edges.dashgap;
  8895. this.altdashlength = constants.edges.altdashlength;
  8896. this.stiffness = undefined; // depends on the length of the edge
  8897. this.color = constants.edges.color;
  8898. this.timestamp = undefined;
  8899. this.widthFixed = false;
  8900. this.lengthFixed = false;
  8901. this.setProperties(properties, constants);
  8902. };
  8903. /**
  8904. * Set or overwrite properties for the edge
  8905. * @param {Object} properties an object with properties
  8906. * @param {Object} constants and object with default, global properties
  8907. */
  8908. Graph.Edge.prototype.setProperties = function(properties, constants) {
  8909. if (!properties) {
  8910. return;
  8911. }
  8912. if (properties.from != undefined) {this.from = this.graph._getNode(properties.from);}
  8913. if (properties.to != undefined) {this.to = this.graph._getNode(properties.to);}
  8914. if (properties.id != undefined) {this.id = properties.id;}
  8915. if (properties.style != undefined) {this.style = properties.style;}
  8916. if (properties.text != undefined) {this.text = properties.text;}
  8917. if (this.text) {
  8918. this.fontSize = constants.edges.fontSize;
  8919. this.fontFace = constants.edges.fontFace;
  8920. this.fontColor = constants.edges.fontColor;
  8921. if (properties.fontColor != undefined) {this.fontColor = properties.fontColor;}
  8922. if (properties.fontSize != undefined) {this.fontSize = properties.fontSize;}
  8923. if (properties.fontFace != undefined) {this.fontFace = properties.fontFace;}
  8924. }
  8925. if (properties.title != undefined) {this.title = properties.title;}
  8926. if (properties.width != undefined) {this.width = properties.width;}
  8927. if (properties.value != undefined) {this.value = properties.value;}
  8928. if (properties.length != undefined) {this.length = properties.length;}
  8929. // Added to support dashed lines
  8930. // David Jordan
  8931. // 2012-08-08
  8932. if (properties.dashlength != undefined) {this.dashlength = properties.dashlength;}
  8933. if (properties.dashgap != undefined) {this.dashgap = properties.dashgap;}
  8934. if (properties.altdashlength != undefined) {this.altdashlength = properties.altdashlength;}
  8935. if (properties.color != undefined) {this.color = properties.color;}
  8936. if (properties.timestamp != undefined) {this.timestamp = properties.timestamp;}
  8937. if (!this.from) {
  8938. throw "Node with id " + properties.from + " not found";
  8939. }
  8940. if (!this.to) {
  8941. throw "Node with id " + properties.to + " not found";
  8942. }
  8943. this.widthFixed = this.widthFixed || (properties.width != undefined);
  8944. this.lengthFixed = this.lengthFixed || (properties.length != undefined);
  8945. this.stiffness = 1 / this.length;
  8946. // initialize animation
  8947. if (this.style === 'arrow') {
  8948. this.arrows = [0.5];
  8949. this.animation = false;
  8950. }
  8951. else if (this.style === 'arrow-end') {
  8952. this.animation = false;
  8953. }
  8954. else if (this.style === 'moving-arrows') {
  8955. this.arrows = [];
  8956. var arrowCount = 3; // TODO: make customizable
  8957. for (var a = 0; a < arrowCount; a++) {
  8958. this.arrows.push(a / arrowCount);
  8959. }
  8960. this.animation = true;
  8961. }
  8962. else if (this.style === 'moving-dot') {
  8963. this.dot = 0.0;
  8964. this.animation = true;
  8965. }
  8966. else {
  8967. this.animation = false;
  8968. }
  8969. // set draw method based on style
  8970. switch (this.style) {
  8971. case 'line': this.draw = this._drawLine; break;
  8972. case 'arrow': this.draw = this._drawArrow; break;
  8973. case 'arrow-end': this.draw = this._drawArrowEnd; break;
  8974. case 'moving-arrows': this.draw = this._drawMovingArrows; break;
  8975. case 'moving-dot': this.draw = this._drawMovingDot; break;
  8976. case 'dash-line': this.draw = this._drawDashLine; break;
  8977. default: this.draw = this._drawLine; break;
  8978. }
  8979. };
  8980. /**
  8981. * Check if a node has an animating contents. If so, the graph needs to be
  8982. * redrawn regularly
  8983. * @return {boolean} true if this edge needs animation, else false
  8984. */
  8985. Graph.Edge.prototype.isMoving = function() {
  8986. // TODO: be able to set the interval somehow
  8987. return this.animation;
  8988. };
  8989. /**
  8990. * get the title of this edge.
  8991. * @return {string} title The title of the edge, or undefined when no title
  8992. * has been set.
  8993. */
  8994. Graph.Edge.prototype.getTitle = function() {
  8995. return this.title;
  8996. };
  8997. /**
  8998. * Retrieve the value of the edge. Can be undefined
  8999. * @return {Number} value
  9000. */
  9001. Graph.Edge.prototype.getValue = function() {
  9002. return this.value;
  9003. }
  9004. /**
  9005. * Adjust the value range of the edge. The edge will adjust it's width
  9006. * based on its value.
  9007. * @param {Number} min
  9008. * @param {Number} max
  9009. */
  9010. Graph.Edge.prototype.setValueRange = function(min, max) {
  9011. if (!this.widthFixed && this.value !== undefined) {
  9012. var factor = (this.widthMax - this.widthMin) / (max - min);
  9013. this.width = (this.value - min) * factor + this.widthMin;
  9014. }
  9015. };
  9016. /**
  9017. * Check if the length is fixed.
  9018. * @return {boolean} lengthFixed True if the length is fixed, else false
  9019. */
  9020. Graph.Edge.prototype.isLengthFixed = function() {
  9021. return this.lengthFixed;
  9022. };
  9023. /**
  9024. * Retrieve the length of the edge. Can be undefined
  9025. * @return {Number} length
  9026. */
  9027. Graph.Edge.prototype.getLength = function() {
  9028. return this.length;
  9029. };
  9030. /**
  9031. * Adjust the length of the edge. This can only be done when the length
  9032. * is not fixed (which is the case when the edge is created with a length property)
  9033. * @param {Number} length
  9034. */
  9035. Graph.Edge.prototype.setLength = function(length) {
  9036. if (!this.lengthFixed) {
  9037. this.length = length;
  9038. }
  9039. };
  9040. /**
  9041. * Retrieve the length of the edges dashes. Can be undefined
  9042. * @author David Jordan
  9043. * @date 2012-08-08
  9044. * @return {Number} dashlength
  9045. */
  9046. Graph.Edge.prototype.getDashLength = function() {
  9047. return this.dashlength;
  9048. };
  9049. /**
  9050. * Adjust the length of the edges dashes.
  9051. * @author David Jordan
  9052. * @date 2012-08-08
  9053. * @param {Number} dashlength
  9054. */
  9055. Graph.Edge.prototype.setDashLength = function(dashlength) {
  9056. this.dashlength = dashlength;
  9057. };
  9058. /**
  9059. * Retrieve the length of the edges dashes gaps. Can be undefined
  9060. * @author David Jordan
  9061. * @date 2012-08-08
  9062. * @return {Number} dashgap
  9063. */
  9064. Graph.Edge.prototype.getDashGap = function() {
  9065. return this.dashgap;
  9066. };
  9067. /**
  9068. * Adjust the length of the edges dashes gaps.
  9069. * @author David Jordan
  9070. * @date 2012-08-08
  9071. * @param {Number} dashgap
  9072. */
  9073. Graph.Edge.prototype.setDashGap = function(dashgap) {
  9074. this.dashgap = dashgap;
  9075. };
  9076. /**
  9077. * Retrieve the length of the edges alternate dashes. Can be undefined
  9078. * @author David Jordan
  9079. * @date 2012-08-08
  9080. * @return {Number} altdashlength
  9081. */
  9082. Graph.Edge.prototype.getAltDashLength = function() {
  9083. return this.altdashlength;
  9084. };
  9085. /**
  9086. * Adjust the length of the edges alternate dashes.
  9087. * @author David Jordan
  9088. * @date 2012-08-08
  9089. * @param {Number} altdashlength
  9090. */
  9091. Graph.Edge.prototype.setAltDashLength = function(altdashlength) {
  9092. this.altdashlength = altdashlength;
  9093. };
  9094. /**
  9095. * Redraw a edge
  9096. * Draw this edge in the given canvas
  9097. * The 2d context of a HTML canvas can be retrieved by canvas.getContext("2d");
  9098. * @param {CanvasRenderingContext2D} ctx
  9099. */
  9100. Graph.Edge.prototype.draw = function(ctx) {
  9101. throw "Method draw not initialized in edge";
  9102. };
  9103. /**
  9104. * Check if this object is overlapping with the provided object
  9105. * @param {Object} obj an object with parameters left, top
  9106. * @return {boolean} True if location is located on the edge
  9107. */
  9108. Graph.Edge.prototype.isOverlappingWith = function(obj) {
  9109. var distMax = 10;
  9110. var xFrom = this.from.x;
  9111. var yFrom = this.from.y;
  9112. var xTo = this.to.x;
  9113. var yTo = this.to.y;
  9114. var xObj = obj.left;
  9115. var yObj = obj.top;
  9116. var dist = Graph._dist(xFrom, yFrom, xTo, yTo, xObj, yObj);
  9117. return (dist < distMax);
  9118. };
  9119. /**
  9120. * Calculate the distance between a point (x3,y3) and a line segment from
  9121. * (x1,y1) to (x2,y2).
  9122. * http://stackoverflow.com/questions/849211/shortest-distancae-between-a-point-and-a-line-segment
  9123. * @param {number} x1
  9124. * @param {number} y1
  9125. * @param {number} x2
  9126. * @param {number} y2
  9127. * @param {number} x3
  9128. * @param {number} y3
  9129. */
  9130. Graph._dist = function (x1,y1, x2,y2, x3,y3) { // x3,y3 is the point
  9131. var px = x2-x1,
  9132. py = y2-y1,
  9133. something = px*px + py*py,
  9134. u = ((x3 - x1) * px + (y3 - y1) * py) / something;
  9135. if (u > 1) {
  9136. u = 1;
  9137. }
  9138. else if (u < 0) {
  9139. u = 0;
  9140. }
  9141. var x = x1 + u * px,
  9142. y = y1 + u * py,
  9143. dx = x - x3,
  9144. dy = y - y3;
  9145. //# Note: If the actual distance does not matter,
  9146. //# if you only want to compare what this function
  9147. //# returns to other results of this function, you
  9148. //# can just return the squared distance instead
  9149. //# (i.e. remove the sqrt) to gain a little performance
  9150. return Math.sqrt(dx*dx + dy*dy);
  9151. };
  9152. /**
  9153. * Redraw a edge as a line
  9154. * Draw this edge in the given canvas
  9155. * The 2d context of a HTML canvas can be retrieved by canvas.getContext("2d");
  9156. * @param {CanvasRenderingContext2D} ctx
  9157. */
  9158. Graph.Edge.prototype._drawLine = function(ctx) {
  9159. // set style
  9160. ctx.strokeStyle = this.color;
  9161. ctx.lineWidth = this._getLineWidth();
  9162. var point;
  9163. if (this.from != this.to) {
  9164. // draw line
  9165. this._line(ctx);
  9166. // draw text
  9167. if (this.text) {
  9168. point = this._pointOnLine(0.5);
  9169. this._text(ctx, this.text, point.x, point.y);
  9170. }
  9171. }
  9172. else {
  9173. var radius = this.length / 2 / Math.PI;
  9174. var x, y;
  9175. var node = this.from;
  9176. if (!node.width) {
  9177. node.resize(ctx);
  9178. }
  9179. if (node.width > node.height) {
  9180. x = node.x + node.width / 2;
  9181. y = node.y - radius;
  9182. }
  9183. else {
  9184. x = node.x + radius;
  9185. y = node.y - node.height / 2;
  9186. }
  9187. this._circle(ctx, x, y, radius);
  9188. point = this._pointOnCircle(x, y, radius, 0.5);
  9189. this._text(ctx, this.text, point.x, point.y);
  9190. }
  9191. };
  9192. /**
  9193. * Get the line width of the edge. Depends on width and whether one of the
  9194. * connected nodes is selected.
  9195. * @return {Number} width
  9196. * @private
  9197. */
  9198. Graph.Edge.prototype._getLineWidth = function() {
  9199. if (this.from.selected || this.to.selected) {
  9200. return Math.min(this.width * 2, this.widthMax);
  9201. }
  9202. else {
  9203. return this.width;
  9204. }
  9205. };
  9206. /**
  9207. * Draw a line between two nodes
  9208. * @param {CanvasRenderingContext2D} ctx
  9209. * @private
  9210. */
  9211. Graph.Edge.prototype._line = function (ctx) {
  9212. // draw a straight line
  9213. ctx.beginPath();
  9214. ctx.moveTo(this.from.x, this.from.y);
  9215. ctx.lineTo(this.to.x, this.to.y);
  9216. ctx.stroke();
  9217. };
  9218. /**
  9219. * Draw a line from a node to itself, a circle
  9220. * @param {CanvasRenderingContext2D} ctx
  9221. * @param {Number} x
  9222. * @param {Number} y
  9223. * @param {Number} radius
  9224. * @private
  9225. */
  9226. Graph.Edge.prototype._circle = function (ctx, x, y, radius) {
  9227. // draw a circle
  9228. ctx.beginPath();
  9229. ctx.arc(x, y, radius, 0, 2 * Math.PI, false);
  9230. ctx.stroke();
  9231. };
  9232. /**
  9233. * Draw text with white background and with the middle at (x, y)
  9234. * @param {CanvasRenderingContext2D} ctx
  9235. * @param {String} text
  9236. * @param {Number} x
  9237. * @param {Number} y
  9238. */
  9239. Graph.Edge.prototype._text = function (ctx, text, x, y) {
  9240. if (text) {
  9241. // TODO: cache the calculated size
  9242. ctx.font = ((this.from.selected || this.to.selected) ? "bold " : "") +
  9243. this.fontSize + "px " + this.fontFace;
  9244. ctx.fillStyle = 'white';
  9245. var width = ctx.measureText(this.text).width;
  9246. var height = this.fontSize;
  9247. var left = x - width / 2;
  9248. var top = y - height / 2;
  9249. ctx.fillRect(left, top, width, height);
  9250. // draw text
  9251. ctx.fillStyle = this.fontColor || "black";
  9252. ctx.textAlign = "left";
  9253. ctx.textBaseline = "top";
  9254. ctx.fillText(this.text, left, top);
  9255. }
  9256. };
  9257. /**
  9258. * Sets up the dashedLine functionality for drawing
  9259. * Original code came from http://stackoverflow.com/questions/4576724/dotted-stroke-in-canvas
  9260. * @author David Jordan
  9261. * @date 2012-08-08
  9262. */
  9263. var CP = (typeof window !== 'undefined') &&
  9264. window.CanvasRenderingContext2D &&
  9265. CanvasRenderingContext2D.prototype;
  9266. if (CP && CP.lineTo){
  9267. CP.dashedLine = function(x,y,x2,y2,dashArray){
  9268. if (!dashArray) dashArray=[10,5];
  9269. if (dashLength==0) dashLength = 0.001; // Hack for Safari
  9270. var dashCount = dashArray.length;
  9271. this.moveTo(x, y);
  9272. var dx = (x2-x), dy = (y2-y);
  9273. var slope = dy/dx;
  9274. var distRemaining = Math.sqrt( dx*dx + dy*dy );
  9275. var dashIndex=0, draw=true;
  9276. while (distRemaining>=0.1){
  9277. var dashLength = dashArray[dashIndex++%dashCount];
  9278. if (dashLength > distRemaining) dashLength = distRemaining;
  9279. var xStep = Math.sqrt( dashLength*dashLength / (1 + slope*slope) );
  9280. if (dx<0) xStep = -xStep;
  9281. x += xStep
  9282. y += slope*xStep;
  9283. this[draw ? 'lineTo' : 'moveTo'](x,y);
  9284. distRemaining -= dashLength;
  9285. draw = !draw;
  9286. }
  9287. }
  9288. }
  9289. /**
  9290. * Redraw a edge as a dashed line
  9291. * Draw this edge in the given canvas
  9292. * @author David Jordan
  9293. * @date 2012-08-08
  9294. * The 2d context of a HTML canvas can be retrieved by canvas.getContext("2d");
  9295. * @param {CanvasRenderingContext2D} ctx
  9296. */
  9297. Graph.Edge.prototype._drawDashLine = function(ctx) {
  9298. // set style
  9299. ctx.strokeStyle = this.color;
  9300. ctx.lineWidth = this._getLineWidth();
  9301. // draw dashed line
  9302. ctx.beginPath();
  9303. ctx.lineCap = 'round';
  9304. if (this.altdashlength != undefined) //If an alt dash value has been set add to the array this value
  9305. {
  9306. ctx.dashedLine(this.from.x,this.from.y,this.to.x,this.to.y,[this.dashlength,this.dashgap,this.altdashlength,this.dashgap]);
  9307. }
  9308. else if (this.dashlength != undefined && this.dashgap != undefined) //If a dash and gap value has been set add to the array this value
  9309. {
  9310. ctx.dashedLine(this.from.x,this.from.y,this.to.x,this.to.y,[this.dashlength,this.dashgap]);
  9311. }
  9312. else //If all else fails draw a line
  9313. {
  9314. ctx.moveTo(this.from.x, this.from.y);
  9315. ctx.lineTo(this.to.x, this.to.y);
  9316. }
  9317. ctx.stroke();
  9318. // draw text
  9319. if (this.text) {
  9320. var point = this._pointOnLine(0.5);
  9321. this._text(ctx, this.text, point.x, point.y);
  9322. }
  9323. };
  9324. /**
  9325. * Get a point on a line
  9326. * @param {Number} percentage. Value between 0 (line start) and 1 (line end)
  9327. * @return {Object} point
  9328. * @private
  9329. */
  9330. Graph.Edge.prototype._pointOnLine = function (percentage) {
  9331. return {
  9332. x: (1 - percentage) * this.from.x + percentage * this.to.x,
  9333. y: (1 - percentage) * this.from.y + percentage * this.to.y
  9334. }
  9335. };
  9336. /**
  9337. * Get a point on a circle
  9338. * @param {Number} x
  9339. * @param {Number} y
  9340. * @param {Number} radius
  9341. * @param {Number} percentage. Value between 0 (line start) and 1 (line end)
  9342. * @return {Object} point
  9343. * @private
  9344. */
  9345. Graph.Edge.prototype._pointOnCircle = function (x, y, radius, percentage) {
  9346. var angle = (percentage - 3/8) * 2 * Math.PI;
  9347. return {
  9348. x: x + radius * Math.cos(angle),
  9349. y: y - radius * Math.sin(angle)
  9350. }
  9351. };
  9352. /**
  9353. * Redraw a edge as a line with a moving arrow
  9354. * Draw this edge in the given canvas
  9355. * The 2d context of a HTML canvas can be retrieved by canvas.getContext("2d");
  9356. * @param {CanvasRenderingContext2D} ctx
  9357. */
  9358. Graph.Edge.prototype._drawMovingArrows = function(ctx) {
  9359. this._drawArrow(ctx);
  9360. for (var a in this.arrows) {
  9361. if (this.arrows.hasOwnProperty(a)) {
  9362. this.arrows[a] += 0.02; // TODO determine speed from interval
  9363. if (this.arrows[a] > 1.0) this.arrows[a] = 0.0;
  9364. }
  9365. }
  9366. };
  9367. /**
  9368. * Redraw a edge as a line with a moving dot
  9369. * Draw this edge in the given canvas
  9370. * The 2d context of a HTML canvas can be retrieved by canvas.getContext("2d");
  9371. * @param {CanvasRenderingContext2D} ctx
  9372. */
  9373. Graph.Edge.prototype._drawMovingDot = function(ctx) {
  9374. // set style
  9375. ctx.strokeStyle = this.color;
  9376. ctx.fillStyle = this.color;
  9377. ctx.lineWidth = this._getLineWidth();
  9378. // draw line
  9379. var point;
  9380. if (this.from != this.to) {
  9381. this._line(ctx);
  9382. // draw dot
  9383. var radius = 4 + this.width * 2;
  9384. point = this._pointOnLine(this.dot);
  9385. ctx.circle(point.x, point.y, radius);
  9386. ctx.fill();
  9387. // move the dot to the next position
  9388. this.dot += 0.05; // TODO determine speed from interval
  9389. if (this.dot > 1.0) this.dot = 0.0;
  9390. // draw text
  9391. if (this.text) {
  9392. point = this._pointOnLine(0.5);
  9393. this._text(ctx, this.text, point.x, point.y);
  9394. }
  9395. }
  9396. else {
  9397. // TODO: moving dot for a circular edge
  9398. }
  9399. };
  9400. /**
  9401. * Redraw a edge as a line with an arrow
  9402. * Draw this edge in the given canvas
  9403. * The 2d context of a HTML canvas can be retrieved by canvas.getContext("2d");
  9404. * @param {CanvasRenderingContext2D} ctx
  9405. */
  9406. Graph.Edge.prototype._drawArrow = function(ctx) {
  9407. var point;
  9408. // set style
  9409. ctx.strokeStyle = this.color;
  9410. ctx.fillStyle = this.color;
  9411. ctx.lineWidth = this._getLineWidth();
  9412. if (this.from != this.to) {
  9413. // draw line
  9414. this._line(ctx);
  9415. // draw all arrows
  9416. var angle = Math.atan2((this.to.y - this.from.y), (this.to.x - this.from.x));
  9417. var length = 10 + 5 * this.width; // TODO: make customizable?
  9418. for (var a in this.arrows) {
  9419. if (this.arrows.hasOwnProperty(a)) {
  9420. point = this._pointOnLine(this.arrows[a]);
  9421. ctx.arrow(point.x, point.y, angle, length);
  9422. ctx.fill();
  9423. ctx.stroke();
  9424. }
  9425. }
  9426. // draw text
  9427. if (this.text) {
  9428. point = this._pointOnLine(0.5);
  9429. this._text(ctx, this.text, point.x, point.y);
  9430. }
  9431. }
  9432. else {
  9433. // draw circle
  9434. var radius = this.length / 2 / Math.PI;
  9435. var x, y;
  9436. var node = this.from;
  9437. if (!node.width) {
  9438. node.resize(ctx);
  9439. }
  9440. if (node.width > node.height) {
  9441. x = node.x + node.width / 2;
  9442. y = node.y - radius;
  9443. }
  9444. else {
  9445. x = node.x + radius;
  9446. y = node.y - node.height / 2;
  9447. }
  9448. this._circle(ctx, x, y, radius);
  9449. // draw all arrows
  9450. var angle = 0.2 * Math.PI;
  9451. var length = 10 + 5 * this.width; // TODO: make customizable?
  9452. for (var a in this.arrows) {
  9453. if (this.arrows.hasOwnProperty(a)) {
  9454. point = this._pointOnCircle(x, y, radius, this.arrows[a]);
  9455. ctx.arrow(point.x, point.y, angle, length);
  9456. ctx.fill();
  9457. ctx.stroke();
  9458. }
  9459. }
  9460. // draw text
  9461. if (this.text) {
  9462. point = this._pointOnCircle(x, y, radius, 0.5);
  9463. this._text(ctx, this.text, point.x, point.y);
  9464. }
  9465. }
  9466. };
  9467. /**
  9468. * Redraw a edge as a line with an arrow
  9469. * Draw this edge in the given canvas
  9470. * The 2d context of a HTML canvas can be retrieved by canvas.getContext("2d");
  9471. * @param {CanvasRenderingContext2D} ctx
  9472. */
  9473. Graph.Edge.prototype._drawArrowEnd = function(ctx) {
  9474. // set style
  9475. ctx.strokeStyle = this.color;
  9476. ctx.fillStyle = this.color;
  9477. ctx.lineWidth = this._getLineWidth();
  9478. // draw line
  9479. var angle, length;
  9480. if (this.from != this.to) {
  9481. // calculate length and angle of the line
  9482. angle = Math.atan2((this.to.y - this.from.y), (this.to.x - this.from.x));
  9483. var dx = (this.to.x - this.from.x);
  9484. var dy = (this.to.y - this.from.y);
  9485. var lEdge = Math.sqrt(dx * dx + dy * dy);
  9486. var lFrom = this.to.distanceToBorder(ctx, angle + Math.PI);
  9487. var pFrom = (lEdge - lFrom) / lEdge;
  9488. var xFrom = (pFrom) * this.from.x + (1 - pFrom) * this.to.x;
  9489. var yFrom = (pFrom) * this.from.y + (1 - pFrom) * this.to.y;
  9490. var lTo = this.to.distanceToBorder(ctx, angle);
  9491. var pTo = (lEdge - lTo) / lEdge;
  9492. var xTo = (1 - pTo) * this.from.x + pTo * this.to.x;
  9493. var yTo = (1 - pTo) * this.from.y + pTo * this.to.y;
  9494. ctx.beginPath();
  9495. ctx.moveTo(xFrom, yFrom);
  9496. ctx.lineTo(xTo, yTo);
  9497. ctx.stroke();
  9498. // draw arrow at the end of the line
  9499. length = 10 + 5 * this.width; // TODO: make customizable?
  9500. ctx.arrow(xTo, yTo, angle, length);
  9501. ctx.fill();
  9502. ctx.stroke();
  9503. // draw text
  9504. if (this.text) {
  9505. var point = this._pointOnLine(0.5);
  9506. this._text(ctx, this.text, point.x, point.y);
  9507. }
  9508. }
  9509. else {
  9510. // draw circle
  9511. var radius = this.length / 2 / Math.PI;
  9512. var x, y, arrow;
  9513. var node = this.from;
  9514. if (!node.width) {
  9515. node.resize(ctx);
  9516. }
  9517. if (node.width > node.height) {
  9518. x = node.x + node.width / 2;
  9519. y = node.y - radius;
  9520. arrow = {
  9521. x: x,
  9522. y: node.y,
  9523. angle: 0.9 * Math.PI
  9524. };
  9525. }
  9526. else {
  9527. x = node.x + radius;
  9528. y = node.y - node.height / 2;
  9529. arrow = {
  9530. x: node.x,
  9531. y: y,
  9532. angle: 0.6 * Math.PI
  9533. };
  9534. }
  9535. ctx.beginPath();
  9536. // TODO: do not draw a circle, but an arc
  9537. // TODO: similarly, for a line without arrows, draw to the border of the nodes instead of the center
  9538. ctx.arc(x, y, radius, 0, 2 * Math.PI, false);
  9539. ctx.stroke();
  9540. // draw all arrows
  9541. length = 10 + 5 * this.width; // TODO: make customizable?
  9542. ctx.arrow(arrow.x, arrow.y, arrow.angle, length);
  9543. ctx.fill();
  9544. ctx.stroke();
  9545. // draw text
  9546. if (this.text) {
  9547. point = this._pointOnCircle(x, y, radius, 0.5);
  9548. this._text(ctx, this.text, point.x, point.y);
  9549. }
  9550. }
  9551. };
  9552. /**--------------------------------------------------------------------------**/
  9553. /**
  9554. * @class Images
  9555. * This class loades images and keeps them stored.
  9556. */
  9557. Graph.Images = function () {
  9558. this.images = {};
  9559. this.callback = undefined;
  9560. };
  9561. /**
  9562. * Set an onload callback function. This will be called each time an image
  9563. * is loaded
  9564. * @param {function} callback
  9565. */
  9566. Graph.Images.prototype.setOnloadCallback = function(callback) {
  9567. this.callback = callback;
  9568. };
  9569. /**
  9570. *
  9571. * @param {string} url Url of the image
  9572. * @return {Image} img The image object
  9573. */
  9574. Graph.Images.prototype.load = function(url) {
  9575. var img = this.images[url];
  9576. if (img == undefined) {
  9577. // create the image
  9578. var images = this;
  9579. img = new Image();
  9580. this.images[url] = img;
  9581. img.onload = function() {
  9582. if (images.callback) {
  9583. images.callback(this);
  9584. }
  9585. };
  9586. img.src = url;
  9587. }
  9588. return img;
  9589. };
  9590. /**--------------------------------------------------------------------------**/
  9591. /**
  9592. * @class Package
  9593. * This class contains one package
  9594. *
  9595. * @param {number} properties Properties for the package. Optional. Available
  9596. * properties are: id {number}, title {string},
  9597. * style {string} with available values "dot" and
  9598. * "image", radius {number}, image {string},
  9599. * color {string}, progress {number} with a value
  9600. * between 0-1, duration {number}, timestamp {number
  9601. * or Date}.
  9602. * @param {Graph} graph The graph object, used to find
  9603. * and edge to nodes.
  9604. * @param {Graph.Images} imagelist An Images object. Only needed
  9605. * when the package has style 'image'
  9606. * @param {Object} constants An object with default values for
  9607. * example for the color
  9608. */
  9609. Graph.Package = function (properties, graph, imagelist, constants) {
  9610. if (graph == undefined) {
  9611. throw "No graph provided";
  9612. }
  9613. // constants
  9614. this.radiusMin = constants.packages.radiusMin;
  9615. this.radiusMax = constants.packages.radiusMax;
  9616. this.imagelist = imagelist;
  9617. this.graph = graph;
  9618. // initialize variables
  9619. this.id = undefined;
  9620. this.from = undefined;
  9621. this.to = undefined;
  9622. this.title = undefined;
  9623. this.style = constants.packages.style;
  9624. this.radius = constants.packages.radius;
  9625. this.color = constants.packages.color;
  9626. this.image = constants.packages.image;
  9627. this.value = undefined;
  9628. this.progress = 0.0;
  9629. this.timestamp = undefined;
  9630. this.duration = constants.packages.duration;
  9631. this.autoProgress = true;
  9632. this.radiusFixed = false;
  9633. // set properties
  9634. this.setProperties(properties, constants);
  9635. };
  9636. Graph.Package.DEFAULT_DURATION = 1.0; // seconds
  9637. /**
  9638. * Set or overwrite properties for the package
  9639. * @param {Object} properties an object with properties
  9640. * @param {Object} constants and object with default, global properties
  9641. */
  9642. Graph.Package.prototype.setProperties = function(properties, constants) {
  9643. if (!properties) {
  9644. return;
  9645. }
  9646. // note that the provided properties can also be null
  9647. if (properties.from != undefined) {this.from = this.graph._getNode(properties.from);}
  9648. if (properties.to != undefined) {this.to = this.graph._getNode(properties.to);}
  9649. if (!this.from) {
  9650. throw "Node with id " + properties.from + " not found";
  9651. }
  9652. if (!this.to) {
  9653. throw "Node with id " + properties.to + " not found";
  9654. }
  9655. if (properties.id != undefined) {this.id = properties.id;}
  9656. if (properties.title != undefined) {this.title = properties.title;}
  9657. if (properties.style != undefined) {this.style = properties.style;}
  9658. if (properties.radius != undefined) {this.radius = properties.radius;}
  9659. if (properties.value != undefined) {this.value = properties.value;}
  9660. if (properties.image != undefined) {this.image = properties.image;}
  9661. if (properties.color != undefined) {this.color = properties.color;}
  9662. if (properties.dashlength != undefined) {this.dashlength = properties.dashlength;}
  9663. if (properties.dashgap != undefined) {this.dashgap = properties.dashgap;}
  9664. if (properties.altdashlength != undefined) {this.altdashlength = properties.altdashlength;}
  9665. if (properties.progress != undefined) {this.progress = properties.progress;}
  9666. if (properties.timestamp != undefined) {this.timestamp = properties.timestamp;}
  9667. if (properties.duration != undefined) {this.duration = properties.duration;}
  9668. this.radiusFixed = this.radiusFixed || (properties.radius != undefined);
  9669. this.autoProgress = (this.autoProgress == true) ? (properties.progress == undefined) : false;
  9670. if (this.style == 'image') {
  9671. this.radiusMin = constants.packages.widthMin;
  9672. this.radiusMax = constants.packages.widthMax;
  9673. }
  9674. // handle progress
  9675. if (this.progress < 0.0) {this.progress = 0.0;}
  9676. if (this.progress > 1.0) {this.progress = 1.0;}
  9677. // handle image
  9678. if (this.image != undefined) {
  9679. if (this.imagelist) {
  9680. this.imageObj = this.imagelist.load(this.image);
  9681. }
  9682. else {
  9683. throw "No imagelist provided";
  9684. }
  9685. }
  9686. // choose draw method depending on the style
  9687. switch (this.style) {
  9688. // TODO: add more styles
  9689. case 'dot': this.draw = this._drawDot; break;
  9690. case 'square': this.draw = this._drawSquare; break;
  9691. case 'triangle': this.draw = this._drawTriangle; break;
  9692. case 'triangleDown':this.draw = this._drawTriangleDown; break;
  9693. case 'star': this.draw = this._drawStar; break;
  9694. case 'image': this.draw = this._drawImage; break;
  9695. default: this.draw = this._drawDot; break;
  9696. }
  9697. };
  9698. /**
  9699. * Set a new value for the progress of the package
  9700. * @param {number} progress A value between 0 and 1
  9701. */
  9702. Graph.Package.prototype.setProgress = function (progress) {
  9703. this.progress = progress;
  9704. this.autoProgress = false;
  9705. };
  9706. /**
  9707. * Check if a package is finished, if it has reached its destination.
  9708. * If so, the package can be removed.
  9709. * Only packages with automatically animated progress can be finished
  9710. * @return {boolean} true if finished, else false.
  9711. */
  9712. Graph.Package.prototype.isFinished = function () {
  9713. return (this.autoProgress == true && this.progress >= 1.0);
  9714. };
  9715. /**
  9716. * Check if this package is moving.
  9717. * A packages moves when it has automatic progress and not yet reached its
  9718. * destination.
  9719. * @return {boolean} true if moving, else false.
  9720. */
  9721. Graph.Package.prototype.isMoving = function () {
  9722. return (this.autoProgress || this.isFinished());
  9723. };
  9724. /**
  9725. * Perform one discrete step for the package. Only applicable when the
  9726. * package has no manually set, fixed progress.
  9727. * @param {number} interval Time interval in seconds
  9728. */
  9729. Graph.Package.prototype.discreteStep = function(interval) {
  9730. if (this.autoProgress == true) {
  9731. this.progress += (parseFloat(interval) / this.duration);
  9732. if (this.progress > 1.0)
  9733. this.progress = 1.0;
  9734. }
  9735. };
  9736. /**
  9737. * Draw this package in the given canvas
  9738. * The 2d context of a HTML canvas can be retrieved by canvas.getContext("2d");
  9739. * @param {CanvasRenderingContext2D} ctx
  9740. */
  9741. Graph.Package.prototype.draw = function(ctx) {
  9742. throw "Draw method not initialized for package";
  9743. };
  9744. /**
  9745. * Check if this object is overlapping with the provided object
  9746. * @param {Object} obj an object with parameters left, top, right, bottom
  9747. * @return {boolean} True if location is located on node
  9748. */
  9749. Graph.Package.prototype.isOverlappingWith = function(obj) {
  9750. // radius minimum 10px else it is too hard to get your mouse at the exact right position
  9751. var radius = Math.max(this.radius, 10);
  9752. var pos = this._getPosition();
  9753. return (pos.x - radius < obj.right &&
  9754. pos.x + radius > obj.left &&
  9755. pos.y - radius < obj.bottom &&
  9756. pos.y + radius > obj.top);
  9757. };
  9758. /**
  9759. * Calculate the current position of the package
  9760. * @return {Object} position The object has parameters x and y.
  9761. */
  9762. Graph.Package.prototype._getPosition = function() {
  9763. return {
  9764. "x" : (1 - this.progress) * this.from.x + this.progress * this.to.x,
  9765. "y" : (1 - this.progress) * this.from.y + this.progress * this.to.y
  9766. };
  9767. };
  9768. /**
  9769. * get the title of this package.
  9770. * @return {string} title The title of the package, or undefined when no
  9771. * title has been set.
  9772. */
  9773. Graph.Package.prototype.getTitle = function() {
  9774. return this.title;
  9775. };
  9776. /**
  9777. * Retrieve the value of the package. Can be undefined
  9778. * @return {Number} value
  9779. */
  9780. Graph.Package.prototype.getValue = function() {
  9781. return this.value;
  9782. };
  9783. /**
  9784. * Calculate the distance from the packages location to the given location (x,y)
  9785. * @param {Number} x
  9786. * @param {Number} y
  9787. * @return {Number} value
  9788. */
  9789. Graph.Package.prototype.getDistance = function(x, y) {
  9790. var pos = this._getPosition(),
  9791. dx = pos.x - x,
  9792. dy = pos.y - y;
  9793. return Math.sqrt(dx * dx + dy * dy);
  9794. };
  9795. /**
  9796. * Adjust the value range of the package. The package will adjust it's radius
  9797. * based on its value.
  9798. * @param {Number} min
  9799. * @param {Number} max
  9800. */
  9801. Graph.Package.prototype.setValueRange = function(min, max) {
  9802. if (!this.radiusFixed && this.value !== undefined) {
  9803. var factor = (this.radiusMax - this.radiusMin) / (max - min);
  9804. this.radius = (this.value - min) * factor + this.radiusMin;
  9805. }
  9806. };
  9807. /**
  9808. * Redraw a package as a dot
  9809. * Draw this edge in the given canvas
  9810. * The 2d context of a HTML canvas can be retrieved by canvas.getContext("2d");
  9811. * @param {CanvasRenderingContext2D} ctx
  9812. */
  9813. /* TODO: cleanup
  9814. Graph.Package.prototype._drawDot = function(ctx) {
  9815. // set style
  9816. ctx.fillStyle = this.color;
  9817. // draw dot
  9818. var pos = this._getPosition();
  9819. ctx.circle(pos.x, pos.y, this.radius);
  9820. ctx.fill();
  9821. }
  9822. */
  9823. Graph.Package.prototype._drawDot = function (ctx) {
  9824. this._drawShape(ctx, 'circle');
  9825. };
  9826. Graph.Package.prototype._drawTriangle = function (ctx) {
  9827. this._drawShape(ctx, 'triangle');
  9828. };
  9829. Graph.Package.prototype._drawTriangleDown = function (ctx) {
  9830. this._drawShape(ctx, 'triangleDown');
  9831. };
  9832. Graph.Package.prototype._drawSquare = function (ctx) {
  9833. this._drawShape(ctx, 'square');
  9834. };
  9835. Graph.Package.prototype._drawStar = function (ctx) {
  9836. this._drawShape(ctx, 'star');
  9837. };
  9838. Graph.Package.prototype._drawShape = function (ctx, shape) {
  9839. // set style
  9840. ctx.fillStyle = this.color;
  9841. // draw shape
  9842. var pos = this._getPosition();
  9843. ctx[shape](pos.x, pos.y, this.radius);
  9844. ctx.fill();
  9845. };
  9846. /**
  9847. * Redraw a package as an image
  9848. * Draw this edge in the given canvas
  9849. * The 2d context of a HTML canvas can be retrieved by canvas.getContext("2d");
  9850. * @param {CanvasRenderingContext2D} ctx
  9851. */
  9852. Graph.Package.prototype._drawImage = function (ctx) {
  9853. if (this.imageObj) {
  9854. var width, height;
  9855. if (this.value) {
  9856. var scale = this.imageObj.height / this.imageObj.width;
  9857. width = this.radius || this.imageObj.width;
  9858. height = this.radius * scale || this.imageObj.height;
  9859. }
  9860. else {
  9861. width = this.imageObj.width;
  9862. height = this.imageObj.height;
  9863. }
  9864. var pos = this._getPosition();
  9865. ctx.drawImage(this.imageObj, pos.x - width / 2, pos.y - height / 2, width, height);
  9866. }
  9867. else {
  9868. console.log("image still loading...");
  9869. }
  9870. };
  9871. /**--------------------------------------------------------------------------**/
  9872. /**
  9873. * @class Groups
  9874. * This class can store groups and properties specific for groups.
  9875. */
  9876. Graph.Groups = function () {
  9877. this.clear();
  9878. this.defaultIndex = 0;
  9879. };
  9880. /**
  9881. * default constants for group colors
  9882. */
  9883. Graph.Groups.DEFAULT = [
  9884. {"borderColor": "#2B7CE9", "backgroundColor": "#97C2FC", "highlightColor": "#D2E5FF"}, // blue
  9885. {"borderColor": "#FFA500", "backgroundColor": "#FFFF00", "highlightColor": "#FFFFA3"}, // yellow
  9886. {"borderColor": "#FA0A10", "backgroundColor": "#FB7E81", "highlightColor": "#FFAFB1"}, // red
  9887. {"borderColor": "#41A906", "backgroundColor": "#7BE141", "highlightColor": "#A1EC76"}, // green
  9888. {"borderColor": "#E129F0", "backgroundColor": "#EB7DF4", "highlightColor": "#F0B3F5"}, // magenta
  9889. {"borderColor": "#7C29F0", "backgroundColor": "#AD85E4", "highlightColor": "#D3BDF0"}, // purple
  9890. {"borderColor": "#C37F00", "backgroundColor": "#FFA807", "highlightColor": "#FFCA66"}, // orange
  9891. {"borderColor": "#4220FB", "backgroundColor": "#6E6EFD", "highlightColor": "#9B9BFD"}, // darkblue
  9892. {"borderColor": "#FD5A77", "backgroundColor": "#FFC0CB", "highlightColor": "#FFD1D9"}, // pink
  9893. {"borderColor": "#4AD63A", "backgroundColor": "#C2FABC", "highlightColor": "#E6FFE3"} // mint
  9894. ];
  9895. /**
  9896. * Clear all groups
  9897. */
  9898. Graph.Groups.prototype.clear = function () {
  9899. this.groups = {};
  9900. this.groups.length = function()
  9901. {
  9902. var i = 0;
  9903. for ( var p in this ) {
  9904. if (this.hasOwnProperty(p)) {
  9905. i++;
  9906. }
  9907. }
  9908. return i;
  9909. }
  9910. };
  9911. /**
  9912. * get group properties of a groupname. If groupname is not found, a new group
  9913. * is added.
  9914. * @param {*} groupname Can be a number, string, Date, etc.
  9915. * @return {Object} group The created group, containing all group properties
  9916. */
  9917. Graph.Groups.prototype.get = function (groupname) {
  9918. var group = this.groups[groupname];
  9919. if (group == undefined) {
  9920. // create new group
  9921. var index = this.defaultIndex % Graph.Groups.DEFAULT.length;
  9922. this.defaultIndex++;
  9923. group = {};
  9924. group.borderColor = Graph.Groups.DEFAULT[index].borderColor;
  9925. group.backgroundColor = Graph.Groups.DEFAULT[index].backgroundColor;
  9926. group.highlightColor = Graph.Groups.DEFAULT[index].highlightColor;
  9927. this.groups[groupname] = group;
  9928. }
  9929. return group;
  9930. };
  9931. /**
  9932. * Add a custom group style
  9933. * @param {String} groupname
  9934. * @param {Object} style An object containing borderColor,
  9935. * backgroundColor, etc.
  9936. * @return {Object} group The created group object
  9937. */
  9938. Graph.Groups.prototype.add = function (groupname, style) {
  9939. this.groups[groupname] = style;
  9940. return style;
  9941. };
  9942. /**
  9943. * Check if given object is a Javascript Array
  9944. * @param {*} obj
  9945. * @return {Boolean} isArray true if the given object is an array
  9946. */
  9947. // See http://stackoverflow.com/questions/2943805/javascript-instanceof-typeof-in-gwt-jsni
  9948. Graph.isArray = function (obj) {
  9949. if (obj instanceof Array) {
  9950. return true;
  9951. }
  9952. return (Object.prototype.toString.call(obj) === '[object Array]');
  9953. };
  9954. /**--------------------------------------------------------------------------**/
  9955. /**
  9956. * @class Slider
  9957. *
  9958. * An html slider control with start/stop/prev/next buttons
  9959. * @param {Element} container The element where the slider will be created
  9960. */
  9961. Graph.Slider = function(container) {
  9962. if (container === undefined) throw "Error: No container element defined";
  9963. this.container = container;
  9964. this.frame = document.createElement("DIV");
  9965. //this.frame.style.backgroundColor = "#E5E5E5";
  9966. this.frame.style.width = "100%";
  9967. this.frame.style.position = "relative";
  9968. this.title = document.createElement("DIV");
  9969. this.title.style.margin = "2px";
  9970. this.title.style.marginBottom = "5px";
  9971. this.title.innerHTML = "";
  9972. this.container.appendChild(this.title);
  9973. this.frame.prev = document.createElement("INPUT");
  9974. this.frame.prev.type = "BUTTON";
  9975. this.frame.prev.value = "Prev";
  9976. this.frame.appendChild(this.frame.prev);
  9977. this.frame.play = document.createElement("INPUT");
  9978. this.frame.play.type = "BUTTON";
  9979. this.frame.play.value = "Play";
  9980. this.frame.appendChild(this.frame.play);
  9981. this.frame.next = document.createElement("INPUT");
  9982. this.frame.next.type = "BUTTON";
  9983. this.frame.next.value = "Next";
  9984. this.frame.appendChild(this.frame.next);
  9985. this.frame.bar = document.createElement("INPUT");
  9986. this.frame.bar.type = "BUTTON";
  9987. this.frame.bar.style.position = "absolute";
  9988. this.frame.bar.style.border = "1px solid red";
  9989. this.frame.bar.style.width = "100px";
  9990. this.frame.bar.style.height = "6px";
  9991. this.frame.bar.style.borderRadius = "2px";
  9992. this.frame.bar.style.MozBorderRadius = "2px";
  9993. this.frame.bar.style.border = "1px solid #7F7F7F";
  9994. this.frame.bar.style.backgroundColor = "#E5E5E5";
  9995. this.frame.appendChild(this.frame.bar);
  9996. this.frame.slide = document.createElement("INPUT");
  9997. this.frame.slide.type = "BUTTON";
  9998. this.frame.slide.style.margin = "0px";
  9999. this.frame.slide.value = " ";
  10000. this.frame.slide.style.position = "relative";
  10001. this.frame.slide.style.left = "-100px";
  10002. this.frame.appendChild(this.frame.slide);
  10003. // create events
  10004. var me = this;
  10005. this.frame.slide.onmousedown = function (event) {me._onMouseDown(event);};
  10006. this.frame.prev.onclick = function (event) {me.prev(event);};
  10007. this.frame.play.onclick = function (event) {me.togglePlay(event);};
  10008. this.frame.next.onclick = function (event) {me.next(event);};
  10009. this.container.appendChild(this.frame);
  10010. this.onChangeCallback = undefined;
  10011. this.playTimeout = undefined;
  10012. this.framerate = 20; // frames per second
  10013. this.duration = 10; // seconds
  10014. this.doLoop = true;
  10015. this.start = 0;
  10016. this.end = 0;
  10017. this.value = 0;
  10018. this.step = 0;
  10019. this.rangeIsDate = false;
  10020. this.redraw();
  10021. };
  10022. /**
  10023. * Retrieve the step size, depending on the range, framerate, and duration
  10024. */
  10025. Graph.Slider.prototype._updateStep = function() {
  10026. var range = (this.end - this.start);
  10027. var frameCount = this.duration * this.framerate;
  10028. this.step = range / frameCount;
  10029. };
  10030. /**
  10031. * Select the previous index
  10032. */
  10033. Graph.Slider.prototype.prev = function() {
  10034. this._setValue(this.value - this.step);
  10035. };
  10036. /**
  10037. * Select the next index
  10038. */
  10039. Graph.Slider.prototype.next = function() {
  10040. this._setValue(this.value + this.step);
  10041. };
  10042. /**
  10043. * Select the next index
  10044. */
  10045. Graph.Slider.prototype.playNext = function() {
  10046. var start = new Date();
  10047. if (!this.leftButtonDown) {
  10048. if (this.value + this.step < this.end) {
  10049. this._setValue(this.value + this.step);
  10050. }
  10051. else {
  10052. if (this.doLoop) {
  10053. this._setValue(this.start);
  10054. }
  10055. else {
  10056. this._setValue(this.end);
  10057. this.stop();
  10058. return;
  10059. }
  10060. }
  10061. }
  10062. var end = new Date();
  10063. var diff = (end - start);
  10064. // calculate how much time it to to set the index and to execute the callback
  10065. // function.
  10066. var interval = Math.max(1000 / this.framerate - diff, 0);
  10067. var me = this;
  10068. this.playTimeout = setTimeout(function() {me.playNext();}, interval);
  10069. };
  10070. /**
  10071. * Toggle start or stop playing
  10072. */
  10073. Graph.Slider.prototype.togglePlay = function() {
  10074. if (this.playTimeout === undefined) {
  10075. this.play();
  10076. } else {
  10077. this.stop();
  10078. }
  10079. };
  10080. /**
  10081. * Start playing
  10082. */
  10083. Graph.Slider.prototype.play = function() {
  10084. this.frame.play.value = "Stop";
  10085. this.playNext();
  10086. };
  10087. /**
  10088. * Stop playing
  10089. */
  10090. Graph.Slider.prototype.stop = function() {
  10091. this.frame.play.value = "Play";
  10092. clearInterval(this.playTimeout);
  10093. this.playTimeout = undefined;
  10094. };
  10095. /**
  10096. * Set a callback function which will be triggered when the value of the
  10097. * slider bar has changed.
  10098. */
  10099. Graph.Slider.prototype.setOnChangeCallback = function(callback) {
  10100. this.onChangeCallback = callback;
  10101. };
  10102. /**
  10103. * Set the interval for playing the list
  10104. * @param {number} framerate Framerate in frames per second
  10105. */
  10106. Graph.Slider.prototype.setFramerate = function(framerate) {
  10107. this.framerate = framerate;
  10108. this._updateStep();
  10109. };
  10110. /**
  10111. * Retrieve the current framerate
  10112. * @return {number} framerate in frames per second
  10113. */
  10114. Graph.Slider.prototype.getFramerate = function() {
  10115. return this.framerate;
  10116. };
  10117. /**
  10118. * Set the duration for playing
  10119. * @param {number} duration Duration in seconds
  10120. */
  10121. Graph.Slider.prototype.setDuration = function(duration) {
  10122. this.duration = duration;
  10123. this._updateStep();
  10124. };
  10125. /**
  10126. * Set the time acceleration for playing the history. Only applicable when
  10127. * the values are of type Date.
  10128. * @param {number} acceleration Acceleration, for example 10 means play
  10129. * ten times as fast as real time. A value
  10130. * of 1 will play the history in real time.
  10131. */
  10132. Graph.Slider.prototype.setAcceleration = function(acceleration) {
  10133. var durationRealtime = (this.end - this.start) / 1000; // in seconds
  10134. this.duration = durationRealtime / acceleration;
  10135. this._updateStep();
  10136. };
  10137. /**
  10138. * Set looping on or off
  10139. * @param {boolean} doLoop If true, the slider will jump to the start when
  10140. * the end is passed, and will jump to the end
  10141. * when the start is passed.
  10142. */
  10143. Graph.Slider.prototype.setLoop = function(doLoop) {
  10144. this.doLoop = doLoop;
  10145. };
  10146. /**
  10147. * Retrieve the current value of loop
  10148. * @return {boolean} doLoop If true, the slider will jump to the start when
  10149. * the end is passed, and will jump to the end
  10150. * when the start is passed.
  10151. */
  10152. Graph.Slider.prototype.getLoop = function() {
  10153. return this.doLoop;
  10154. };
  10155. /**
  10156. * Execute the onchange callback function
  10157. */
  10158. Graph.Slider.prototype.onChange = function() {
  10159. if (this.onChangeCallback !== undefined) {
  10160. this.onChangeCallback();
  10161. }
  10162. };
  10163. /**
  10164. * redraw the slider on the correct place
  10165. */
  10166. Graph.Slider.prototype.redraw = function() {
  10167. // resize the bar
  10168. var barTop = (this.frame.clientHeight/2 -
  10169. this.frame.bar.offsetHeight/2);
  10170. var barWidth = (this.frame.clientWidth -
  10171. this.frame.prev.clientWidth -
  10172. this.frame.play.clientWidth -
  10173. this.frame.next.clientWidth - 30);
  10174. this.frame.bar.style.top = barTop + "px";
  10175. this.frame.bar.style.width = barWidth + "px";
  10176. // position the slider button
  10177. this.frame.slide.title = this.getValue();
  10178. this.frame.slide.style.left = this._valueToLeft(this.value) + "px";
  10179. // set the title
  10180. this.title.innerHTML = this.getValue();
  10181. };
  10182. /**
  10183. * Set the range for the slider
  10184. * @param {Date | Number} start Start of the range
  10185. * @param {Date | Number} end End of the range
  10186. */
  10187. Graph.Slider.prototype.setRange = function(start, end) {
  10188. if (start === undefined || start === null || start === NaN) {
  10189. this.start = 0;
  10190. this.rangeIsDate = false;
  10191. }
  10192. else if (start instanceof Date) {
  10193. this.start = start.getTime();
  10194. this.rangeIsDate = true;
  10195. }
  10196. else {
  10197. this.start = start;
  10198. this.rangeIsDate = false;
  10199. }
  10200. if (end === undefined || end === null || end === NaN) {
  10201. if (this.start instanceof Date) {
  10202. this.end = new Date(this.start);
  10203. }
  10204. else {
  10205. this.end = this.start;
  10206. }
  10207. }
  10208. else if (end instanceof Date) {
  10209. this.end = end.getTime();
  10210. }
  10211. else {
  10212. this.end = end;
  10213. }
  10214. this.value = this.start;
  10215. this._updateStep();
  10216. this.redraw();
  10217. };
  10218. /**
  10219. * Set a value for the slider. The value must be between start and end
  10220. * When the range are Dates, the value will be translated to a date
  10221. * @param {Number} value
  10222. */
  10223. Graph.Slider.prototype._setValue = function(value) {
  10224. this.value = this._limitValue(value);
  10225. this.redraw();
  10226. this.onChange();
  10227. };
  10228. /**
  10229. * retrieve the current value in the correct type, Number or Date
  10230. * @return {Date | Number} value
  10231. */
  10232. Graph.Slider.prototype.getValue = function() {
  10233. if (this.rangeIsDate) {
  10234. return new Date(this.value);
  10235. }
  10236. else {
  10237. return this.value;
  10238. }
  10239. };
  10240. Graph.Slider.prototype.offset = 3;
  10241. Graph.Slider.prototype._leftToValue = function (left) {
  10242. var width = parseFloat(this.frame.bar.style.width) -
  10243. this.frame.slide.clientWidth - 10;
  10244. var x = left - this.offset;
  10245. var range = this.end - this.start;
  10246. var value = this._limitValue(x / width * range + this.start);
  10247. return value;
  10248. };
  10249. Graph.Slider.prototype._valueToLeft = function (value) {
  10250. var width = parseFloat(this.frame.bar.style.width) -
  10251. this.frame.slide.clientWidth - 10;
  10252. var x;
  10253. if (this.end > this.start) {
  10254. x = (value - this.start) / (this.end - this.start) * width;
  10255. }
  10256. else {
  10257. x = 0;
  10258. }
  10259. var left = x + this.offset;
  10260. return left;
  10261. };
  10262. Graph.Slider.prototype._limitValue = function(value) {
  10263. if (value < this.start) {
  10264. value = this.start
  10265. }
  10266. if (value > this.end) {
  10267. value = this.end;
  10268. }
  10269. return value;
  10270. };
  10271. Graph.Slider.prototype._onMouseDown = function(event) {
  10272. // only react on left mouse button down
  10273. this.leftButtonDown = event.which ? (event.which === 1) : (event.button === 1);
  10274. if (!this.leftButtonDown) return;
  10275. this.startClientX = event.clientX;
  10276. this.startSlideX = parseFloat(this.frame.slide.style.left);
  10277. this.frame.style.cursor = 'move';
  10278. // add event listeners to handle moving the contents
  10279. // we store the function onmousemove and onmouseup in the graph, so we can
  10280. // remove the eventlisteners lateron in the function mouseUp()
  10281. var me = this;
  10282. this.onmousemove = function (event) {me._onMouseMove(event);};
  10283. this.onmouseup = function (event) {me._onMouseUp(event);};
  10284. Graph.addEventListener(document, "mousemove", this.onmousemove);
  10285. Graph.addEventListener(document, "mouseup", this.onmouseup);
  10286. Graph.preventDefault(event);
  10287. };
  10288. Graph.Slider.prototype._onMouseMove = function (event) {
  10289. var diff = event.clientX - this.startClientX;
  10290. var x = this.startSlideX + diff;
  10291. var value = this._leftToValue(x);
  10292. this._setValue(value);
  10293. Graph.preventDefault(event);
  10294. };
  10295. Graph.Slider.prototype._onMouseUp = function (event) {
  10296. this.frame.style.cursor = 'auto';
  10297. this.leftButtonDown = false;
  10298. // remove event listeners
  10299. Graph.removeEventListener(document, "mousemove", this.onmousemove);
  10300. Graph.removeEventListener(document, "mouseup", this.onmouseup);
  10301. Graph.preventDefault(event);
  10302. };
  10303. /**--------------------------------------------------------------------------**/
  10304. /**
  10305. * Popup is a class to create a popup window with some text
  10306. * @param {Element} container The container object.
  10307. * @param {Number} x
  10308. * @param {Number} y
  10309. * @param {String} text
  10310. */
  10311. Graph.Popup = function (container, x, y, text) {
  10312. if (container) {
  10313. this.container = container;
  10314. }
  10315. else {
  10316. this.container = document.body;
  10317. }
  10318. this.x = 0;
  10319. this.y = 0;
  10320. this.padding = 5;
  10321. if (x !== undefined && y !== undefined ) {
  10322. this.setPosition(x, y);
  10323. }
  10324. if (text !== undefined) {
  10325. this.setText(text);
  10326. }
  10327. // create the frame
  10328. this.frame = document.createElement("div");
  10329. var style = this.frame.style;
  10330. style.position = "absolute";
  10331. style.visibility = "hidden";
  10332. style.border = "1px solid #666";
  10333. style.color = "black";
  10334. style.padding = this.padding + "px";
  10335. style.backgroundColor = "#FFFFC6";
  10336. style.borderRadius = "3px";
  10337. style.MozBorderRadius = "3px";
  10338. style.WebkitBorderRadius = "3px";
  10339. style.boxShadow = "3px 3px 10px rgba(128, 128, 128, 0.5)";
  10340. style.whiteSpace = "nowrap";
  10341. this.container.appendChild(this.frame);
  10342. };
  10343. /**
  10344. * @param {number} x Horizontal position of the popup window
  10345. * @param {number} y Vertical position of the popup window
  10346. */
  10347. Graph.Popup.prototype.setPosition = function(x, y) {
  10348. this.x = parseInt(x);
  10349. this.y = parseInt(y);
  10350. };
  10351. /**
  10352. * Set the text for the popup window. This can be HTML code
  10353. * @param {string} text
  10354. */
  10355. Graph.Popup.prototype.setText = function(text) {
  10356. this.frame.innerHTML = text;
  10357. };
  10358. /**
  10359. * Show the popup window
  10360. * @param {boolean} show Optional. Show or hide the window
  10361. */
  10362. Graph.Popup.prototype.show = function (show) {
  10363. if (show === undefined) {
  10364. show = true;
  10365. }
  10366. if (show) {
  10367. var height = this.frame.clientHeight;
  10368. var width = this.frame.clientWidth;
  10369. var maxHeight = this.frame.parentNode.clientHeight;
  10370. var maxWidth = this.frame.parentNode.clientWidth;
  10371. var top = (this.y - height);
  10372. if (top + height + this.padding > maxHeight) {
  10373. top = maxHeight - height - this.padding;
  10374. }
  10375. if (top < this.padding) {
  10376. top = this.padding;
  10377. }
  10378. var left = this.x;
  10379. if (left + width + this.padding > maxWidth) {
  10380. left = maxWidth - width - this.padding;
  10381. }
  10382. if (left < this.padding) {
  10383. left = this.padding;
  10384. }
  10385. this.frame.style.left = left + "px";
  10386. this.frame.style.top = top + "px";
  10387. this.frame.style.visibility = "visible";
  10388. }
  10389. else {
  10390. this.hide();
  10391. }
  10392. };
  10393. /**
  10394. * Hide the popup window
  10395. */
  10396. Graph.Popup.prototype.hide = function () {
  10397. this.frame.style.visibility = "hidden";
  10398. };
  10399. /**--------------------------------------------------------------------------**/
  10400. if (typeof CanvasRenderingContext2D !== 'undefined') {
  10401. /**
  10402. * Draw a circle shape
  10403. */
  10404. CanvasRenderingContext2D.prototype.circle = function(x, y, r) {
  10405. this.beginPath();
  10406. this.arc(x, y, r, 0, 2*Math.PI, false);
  10407. };
  10408. /**
  10409. * Draw a square shape
  10410. * @param {Number} x horizontal center
  10411. * @param {Number} y vertical center
  10412. * @param {Number} r size, width and height of the square
  10413. */
  10414. CanvasRenderingContext2D.prototype.square = function(x, y, r) {
  10415. this.beginPath();
  10416. this.rect(x - r, y - r, r * 2, r * 2);
  10417. };
  10418. /**
  10419. * Draw a triangle shape
  10420. * @param {Number} x horizontal center
  10421. * @param {Number} y vertical center
  10422. * @param {Number} r radius, half the length of the sides of the triangle
  10423. */
  10424. CanvasRenderingContext2D.prototype.triangle = function(x, y, r) {
  10425. // http://en.wikipedia.org/wiki/Equilateral_triangle
  10426. this.beginPath();
  10427. var s = r * 2;
  10428. var s2 = s / 2;
  10429. var ir = Math.sqrt(3) / 6 * s; // radius of inner circle
  10430. var h = Math.sqrt(s * s - s2 * s2); // height
  10431. this.moveTo(x, y - (h - ir));
  10432. this.lineTo(x + s2, y + ir);
  10433. this.lineTo(x - s2, y + ir);
  10434. this.lineTo(x, y - (h - ir));
  10435. this.closePath();
  10436. };
  10437. /**
  10438. * Draw a triangle shape in downward orientation
  10439. * @param {Number} x horizontal center
  10440. * @param {Number} y vertical center
  10441. * @param {Number} r radius
  10442. */
  10443. CanvasRenderingContext2D.prototype.triangleDown = function(x, y, r) {
  10444. // http://en.wikipedia.org/wiki/Equilateral_triangle
  10445. this.beginPath();
  10446. var s = r * 2;
  10447. var s2 = s / 2;
  10448. var ir = Math.sqrt(3) / 6 * s; // radius of inner circle
  10449. var h = Math.sqrt(s * s - s2 * s2); // height
  10450. this.moveTo(x, y + (h - ir));
  10451. this.lineTo(x + s2, y - ir);
  10452. this.lineTo(x - s2, y - ir);
  10453. this.lineTo(x, y + (h - ir));
  10454. this.closePath();
  10455. };
  10456. /**
  10457. * Draw a star shape, a star with 5 points
  10458. * @param {Number} x horizontal center
  10459. * @param {Number} y vertical center
  10460. * @param {Number} r radius, half the length of the sides of the triangle
  10461. */
  10462. CanvasRenderingContext2D.prototype.star = function(x, y, r) {
  10463. // http://www.html5canvastutorials.com/labs/html5-canvas-star-spinner/
  10464. this.beginPath();
  10465. for (var n = 0; n < 10; n++) {
  10466. var radius = (n % 2 === 0) ? r * 1.3 : r * 0.5;
  10467. this.lineTo(
  10468. x + radius * Math.sin(n * 2 * Math.PI / 10),
  10469. y - radius * Math.cos(n * 2 * Math.PI / 10)
  10470. );
  10471. }
  10472. this.closePath();
  10473. };
  10474. /**
  10475. * http://stackoverflow.com/questions/1255512/how-to-draw-a-rounded-rectangle-on-html-canvas
  10476. */
  10477. CanvasRenderingContext2D.prototype.roundRect = function(x, y, w, h, r) {
  10478. var r2d = Math.PI/180;
  10479. if( w - ( 2 * r ) < 0 ) { r = ( w / 2 ); } //ensure that the radius isn't too large for x
  10480. if( h - ( 2 * r ) < 0 ) { r = ( h / 2 ); } //ensure that the radius isn't too large for y
  10481. this.beginPath();
  10482. this.moveTo(x+r,y);
  10483. this.lineTo(x+w-r,y);
  10484. this.arc(x+w-r,y+r,r,r2d*270,r2d*360,false);
  10485. this.lineTo(x+w,y+h-r);
  10486. this.arc(x+w-r,y+h-r,r,0,r2d*90,false);
  10487. this.lineTo(x+r,y+h);
  10488. this.arc(x+r,y+h-r,r,r2d*90,r2d*180,false);
  10489. this.lineTo(x,y+r);
  10490. this.arc(x+r,y+r,r,r2d*180,r2d*270,false);
  10491. };
  10492. /**
  10493. * http://stackoverflow.com/questions/2172798/how-to-draw-an-oval-in-html5-canvas
  10494. */
  10495. CanvasRenderingContext2D.prototype.ellipse = function(x, y, w, h) {
  10496. var kappa = .5522848,
  10497. ox = (w / 2) * kappa, // control point offset horizontal
  10498. oy = (h / 2) * kappa, // control point offset vertical
  10499. xe = x + w, // x-end
  10500. ye = y + h, // y-end
  10501. xm = x + w / 2, // x-middle
  10502. ym = y + h / 2; // y-middle
  10503. this.beginPath();
  10504. this.moveTo(x, ym);
  10505. this.bezierCurveTo(x, ym - oy, xm - ox, y, xm, y);
  10506. this.bezierCurveTo(xm + ox, y, xe, ym - oy, xe, ym);
  10507. this.bezierCurveTo(xe, ym + oy, xm + ox, ye, xm, ye);
  10508. this.bezierCurveTo(xm - ox, ye, x, ym + oy, x, ym);
  10509. };
  10510. /**
  10511. * http://stackoverflow.com/questions/2172798/how-to-draw-an-oval-in-html5-canvas
  10512. */
  10513. CanvasRenderingContext2D.prototype.database = function(x, y, w, h) {
  10514. var f = 1/3;
  10515. var wEllipse = w;
  10516. var hEllipse = h * f;
  10517. var kappa = .5522848,
  10518. ox = (wEllipse / 2) * kappa, // control point offset horizontal
  10519. oy = (hEllipse / 2) * kappa, // control point offset vertical
  10520. xe = x + wEllipse, // x-end
  10521. ye = y + hEllipse, // y-end
  10522. xm = x + wEllipse / 2, // x-middle
  10523. ym = y + hEllipse / 2, // y-middle
  10524. ymb = y + (h - hEllipse/2), // y-midlle, bottom ellipse
  10525. yeb = y + h; // y-end, bottom ellipse
  10526. this.beginPath();
  10527. this.moveTo(xe, ym);
  10528. this.bezierCurveTo(xe, ym + oy, xm + ox, ye, xm, ye);
  10529. this.bezierCurveTo(xm - ox, ye, x, ym + oy, x, ym);
  10530. this.bezierCurveTo(x, ym - oy, xm - ox, y, xm, y);
  10531. this.bezierCurveTo(xm + ox, y, xe, ym - oy, xe, ym);
  10532. this.lineTo(xe, ymb);
  10533. this.bezierCurveTo(xe, ymb + oy, xm + ox, yeb, xm, yeb);
  10534. this.bezierCurveTo(xm - ox, yeb, x, ymb + oy, x, ymb);
  10535. this.lineTo(x, ym);
  10536. };
  10537. /**
  10538. * Draw an arrow point (no line)
  10539. */
  10540. CanvasRenderingContext2D.prototype.arrow = function(x, y, angle, length) {
  10541. // tail
  10542. var xt = x - length * Math.cos(angle);
  10543. var yt = y - length * Math.sin(angle);
  10544. // inner tail
  10545. // TODO: allow to customize different shapes
  10546. var xi = x - length * 0.9 * Math.cos(angle);
  10547. var yi = y - length * 0.9 * Math.sin(angle);
  10548. // left
  10549. var xl = xt + length / 3 * Math.cos(angle + 0.5 * Math.PI);
  10550. var yl = yt + length / 3 * Math.sin(angle + 0.5 * Math.PI);
  10551. // right
  10552. var xr = xt + length / 3 * Math.cos(angle - 0.5 * Math.PI);
  10553. var yr = yt + length / 3 * Math.sin(angle - 0.5 * Math.PI);
  10554. this.beginPath();
  10555. this.moveTo(x, y);
  10556. this.lineTo(xl, yl);
  10557. this.lineTo(xi, yi);
  10558. this.lineTo(xr, yr);
  10559. this.closePath();
  10560. };
  10561. // TODO: add diamond shape
  10562. }
  10563. /*----------------------------------------------------------------------------*/
  10564. // utility methods
  10565. Graph.util = {};
  10566. /**
  10567. * Parse a text source containing data in DOT language into a JSON object.
  10568. * The object contains two lists: one with nodes and one with edges.
  10569. * @param {String} data Text containing a graph in DOT-notation
  10570. * @return {Object} json An object containing two parameters:
  10571. * {Object[]} nodes
  10572. * {Object[]} edges
  10573. */
  10574. Graph.util.parseDOT = function (data) {
  10575. /**
  10576. * Test whether given character is a whitespace character
  10577. * @param {String} c
  10578. * @return {Boolean} isWhitespace
  10579. */
  10580. function isWhitespace(c) {
  10581. return c == ' ' || c == '\t' || c == '\n' || c == '\r';
  10582. }
  10583. /**
  10584. * Test whether given character is a delimeter
  10585. * @param {String} c
  10586. * @return {Boolean} isDelimeter
  10587. */
  10588. function isDelimeter(c) {
  10589. return '[]{}();,=->'.indexOf(c) != -1;
  10590. }
  10591. var i = -1; // current index in the data
  10592. var c = ''; // current character in the data
  10593. /**
  10594. * Read the next character from the data
  10595. */
  10596. function next() {
  10597. i++;
  10598. c = data[i];
  10599. }
  10600. /**
  10601. * Preview the next character in the data
  10602. * @returns {String} nextChar
  10603. */
  10604. function previewNext () {
  10605. return data[i + 1];
  10606. }
  10607. /**
  10608. * Preview the next character in the data
  10609. * @returns {String} nextChar
  10610. */
  10611. function previewPrevious () {
  10612. return data[i + 1];
  10613. }
  10614. /**
  10615. * Get a text description of the the current index in the data
  10616. * @return {String} desc
  10617. */
  10618. function pos() {
  10619. return '(char ' + i + ')';
  10620. }
  10621. /**
  10622. * Skip whitespace and comments
  10623. */
  10624. function parseWhitespace() {
  10625. // skip whitespace
  10626. while (c && isWhitespace(c)) {
  10627. next();
  10628. }
  10629. // test for comment
  10630. var cNext = data[i + 1];
  10631. var cPrev = data[i - 1];
  10632. var c2 = c + cNext;
  10633. if (c2 == '/*') {
  10634. // block comment. skip until the block is closed
  10635. while (c && !(c == '*' && data[i + 1] == '/')) {
  10636. next();
  10637. }
  10638. next();
  10639. next();
  10640. parseWhitespace();
  10641. }
  10642. else if (c2 == '//' || (c == '#' && cPrev == '\n')) {
  10643. // line comment. skip until the next return
  10644. while (c && c != '\n') {
  10645. next();
  10646. }
  10647. next();
  10648. parseWhitespace();
  10649. }
  10650. }
  10651. /**
  10652. * Parse a string
  10653. * The string may be enclosed by double quotes
  10654. * @return {String | undefined} value
  10655. */
  10656. function parseString() {
  10657. parseWhitespace();
  10658. var name = '';
  10659. if (c == '"') {
  10660. next();
  10661. while (c && c != '"') {
  10662. name += c;
  10663. next();
  10664. }
  10665. next(); // skip the closing quote
  10666. }
  10667. else {
  10668. while (c && !isWhitespace(c) && !isDelimeter(c)) {
  10669. name += c;
  10670. next();
  10671. }
  10672. // cast string to number or boolean
  10673. var number = Number(name);
  10674. if (!isNaN(number)) {
  10675. name = number;
  10676. }
  10677. else if (name == 'true') {
  10678. name = true;
  10679. }
  10680. else if (name == 'false') {
  10681. name = false;
  10682. }
  10683. else if (name == 'null') {
  10684. name = null;
  10685. }
  10686. }
  10687. return name;
  10688. }
  10689. /**
  10690. * Parse a value, can be a string, number, or boolean.
  10691. * The value may be enclosed by double quotes
  10692. * @return {String | Number | Boolean | undefined} value
  10693. */
  10694. function parseValue() {
  10695. parseWhitespace();
  10696. if (c == '"') {
  10697. return parseString();
  10698. }
  10699. else {
  10700. var value = parseString();
  10701. if (value != undefined) {
  10702. // cast string to number or boolean
  10703. var number = Number(value);
  10704. if (!isNaN(number)) {
  10705. value = number;
  10706. }
  10707. else if (value == 'true') {
  10708. value = true;
  10709. }
  10710. else if (value == 'false') {
  10711. value = false;
  10712. }
  10713. else if (value == 'null') {
  10714. value = null;
  10715. }
  10716. }
  10717. return value;
  10718. }
  10719. }
  10720. /**
  10721. * Parse a set with attributes,
  10722. * for example [label="1.000", style=solid]
  10723. * @return {Object | undefined} attr
  10724. */
  10725. function parseAttributes() {
  10726. parseWhitespace();
  10727. if (c == '[') {
  10728. next();
  10729. var attr = {};
  10730. while (c && c != ']') {
  10731. parseWhitespace();
  10732. var name = parseString();
  10733. if (!name) {
  10734. throw new SyntaxError('Attribute name expected ' + pos());
  10735. }
  10736. parseWhitespace();
  10737. if (c != '=') {
  10738. throw new SyntaxError('Equal sign = expected ' + pos());
  10739. }
  10740. next();
  10741. var value = parseValue();
  10742. if (!value) {
  10743. throw new SyntaxError('Attribute value expected ' + pos());
  10744. }
  10745. attr[name] = value;
  10746. parseWhitespace();
  10747. if (c ==',') {
  10748. next();
  10749. }
  10750. }
  10751. next();
  10752. return attr;
  10753. }
  10754. else {
  10755. return undefined;
  10756. }
  10757. }
  10758. /**
  10759. * Parse a directed or undirected arrow '->' or '--'
  10760. * @return {String | undefined} arrow
  10761. */
  10762. function parseArrow() {
  10763. parseWhitespace();
  10764. if (c == '-') {
  10765. next();
  10766. if (c == '>' || c == '-') {
  10767. var arrow = '-' + c;
  10768. next();
  10769. return arrow;
  10770. }
  10771. else {
  10772. throw new SyntaxError('Arrow "->" or "--" expected ' + pos());
  10773. }
  10774. }
  10775. return undefined;
  10776. }
  10777. /**
  10778. * Parse a line separator ';'
  10779. * @return {String | undefined} separator
  10780. */
  10781. function parseSeparator() {
  10782. parseWhitespace();
  10783. if (c == ';') {
  10784. next();
  10785. return ';';
  10786. }
  10787. return undefined;
  10788. }
  10789. /**
  10790. * Merge all properties of object b into object b
  10791. * @param {Object} a
  10792. * @param {Object} b
  10793. */
  10794. function merge (a, b) {
  10795. if (a && b) {
  10796. for (var name in b) {
  10797. if (b.hasOwnProperty(name)) {
  10798. a[name] = b[name];
  10799. }
  10800. }
  10801. }
  10802. }
  10803. var nodeMap = {};
  10804. var edgeList = [];
  10805. /**
  10806. * Register a node with attributes
  10807. * @param {String} id
  10808. * @param {Object} [attr]
  10809. */
  10810. function addNode(id, attr) {
  10811. var node = {
  10812. id: String(id),
  10813. attr: attr || {}
  10814. };
  10815. if (!nodeMap[id]) {
  10816. nodeMap[id] = node;
  10817. }
  10818. else {
  10819. merge(nodeMap[id].attr, node.attr);
  10820. }
  10821. }
  10822. /**
  10823. * Register an edge
  10824. * @param {String} from
  10825. * @param {String} to
  10826. * @param {String} type A string "->" or "--"
  10827. * @param {Object} [attr]
  10828. */
  10829. function addEdge(from, to, type, attr) {
  10830. edgeList.push({
  10831. from: String(from),
  10832. to: String(to),
  10833. type: type,
  10834. attr: attr || {}
  10835. });
  10836. }
  10837. // find the opening curly bracket
  10838. next();
  10839. while (c && c != '{') {
  10840. next();
  10841. }
  10842. if (c != '{') {
  10843. throw new SyntaxError('Invalid data. Curly bracket { expected ' + pos())
  10844. }
  10845. next();
  10846. // parse all data until a closing curly bracket is encountered
  10847. while (c && c != '}') {
  10848. // parse node id and optional node attributes
  10849. var id = parseString();
  10850. if (id == undefined) {
  10851. throw new SyntaxError('String with id expected ' + pos());
  10852. }
  10853. var attr = parseAttributes();
  10854. addNode(id, attr);
  10855. // TODO: parse global attributes "graph", "node", "edge"
  10856. // parse arrow
  10857. var type = parseArrow();
  10858. while (type) {
  10859. // parse node id
  10860. var prevId = id;
  10861. id = parseString();
  10862. if (id == undefined) {
  10863. throw new SyntaxError('String with id expected ' + pos());
  10864. }
  10865. addNode(id);
  10866. // parse edge attributes and register edge
  10867. attr = parseAttributes();
  10868. addEdge(prevId, id, type, attr);
  10869. // parse next arrow (optional)
  10870. type = parseArrow();
  10871. }
  10872. // parse separator (optional)
  10873. parseSeparator();
  10874. parseWhitespace();
  10875. }
  10876. if (c != '}') {
  10877. throw new SyntaxError('Invalid data. Curly bracket } expected');
  10878. }
  10879. // crop data between the curly brackets
  10880. var start = data.indexOf('{');
  10881. var end = data.indexOf('}', start);
  10882. var text = (start != -1 && end != -1) ? data.substring(start + 1, end) : undefined;
  10883. if (!text) {
  10884. throw new Error('Invalid data. no curly brackets containing data found');
  10885. }
  10886. // return the results
  10887. var nodeList = [];
  10888. for (id in nodeMap) {
  10889. if (nodeMap.hasOwnProperty(id)) {
  10890. nodeList.push(nodeMap[id]);
  10891. }
  10892. }
  10893. return {
  10894. nodes: nodeList,
  10895. edges: edgeList
  10896. }
  10897. };
  10898. /**
  10899. * Convert a string containing a graph in DOT language into a map containing
  10900. * with nodes and edges in the format of graph.
  10901. * @param {String} data Text containing a graph in DOT-notation
  10902. * @return {Object} graphData
  10903. */
  10904. Graph.util.DOTToGraph = function (data) {
  10905. // parse the DOT file
  10906. var dotData = Graph.util.parseDOT(data);
  10907. var graphData = {
  10908. nodes: [],
  10909. edges: [],
  10910. options: {
  10911. nodes: {},
  10912. edges: {}
  10913. }
  10914. };
  10915. /**
  10916. * Merge the properties of object b into object a, and adjust properties
  10917. * not supported by Graph (for example replace "shape" with "style"
  10918. * @param {Object} a
  10919. * @param {Object} b
  10920. * @param {Array} [ignore] Optional array with property names to be ignored
  10921. */
  10922. function merge (a, b, ignore) {
  10923. for (var prop in b) {
  10924. if (b.hasOwnProperty(prop) && (!ignore || ignore.indexOf(prop) == -1)) {
  10925. a[prop] = b[prop];
  10926. }
  10927. }
  10928. // Convert aliases to configuration settings supported by Graph
  10929. if (a.label) {
  10930. a.text = a.label;
  10931. delete a.label;
  10932. }
  10933. if (a.shape) {
  10934. a.style = a.shape;
  10935. delete a.shape;
  10936. }
  10937. }
  10938. dotData.nodes.forEach(function (node) {
  10939. if (node.id.toLowerCase() == 'graph') {
  10940. merge(graphData.options, node.attr);
  10941. }
  10942. else if (node.id.toLowerCase() == 'node') {
  10943. merge(graphData.options.nodes, node.attr);
  10944. }
  10945. else if (node.id.toLowerCase() == 'edge') {
  10946. merge(graphData.options.edges, node.attr);
  10947. }
  10948. else {
  10949. var graphNode = {};
  10950. graphNode.id = node.id;
  10951. graphNode.text = node.id;
  10952. merge(graphNode, node.attr);
  10953. graphData.nodes.push(graphNode);
  10954. }
  10955. });
  10956. dotData.edges.forEach(function (edge) {
  10957. var graphEdge = {};
  10958. graphEdge.from = edge.from;
  10959. graphEdge.to = edge.to;
  10960. graphEdge.text = edge.id;
  10961. graphEdge.style = (edge.type == '->') ? 'arrow-end' : 'line';
  10962. merge(graphEdge, edge.attr);
  10963. graphData.edges.push(graphEdge);
  10964. });
  10965. return graphData;
  10966. };
  10967. /**
  10968. * vis.js module exports
  10969. */
  10970. var vis = {
  10971. util: util,
  10972. events: events,
  10973. Controller: Controller,
  10974. DataSet: DataSet,
  10975. DataView: DataView,
  10976. Range: Range,
  10977. Stack: Stack,
  10978. TimeStep: TimeStep,
  10979. EventBus: EventBus,
  10980. components: {
  10981. items: {
  10982. Item: Item,
  10983. ItemBox: ItemBox,
  10984. ItemPoint: ItemPoint,
  10985. ItemRange: ItemRange
  10986. },
  10987. Component: Component,
  10988. Panel: Panel,
  10989. RootPanel: RootPanel,
  10990. ItemSet: ItemSet,
  10991. TimeAxis: TimeAxis
  10992. },
  10993. Timeline: Timeline,
  10994. Graph: Graph
  10995. };
  10996. /**
  10997. * CommonJS module exports
  10998. */
  10999. if (typeof exports !== 'undefined') {
  11000. exports = vis;
  11001. }
  11002. if (typeof module !== 'undefined' && typeof module.exports !== 'undefined') {
  11003. module.exports = vis;
  11004. }
  11005. /**
  11006. * AMD module exports
  11007. */
  11008. if (typeof(define) === 'function') {
  11009. define(function () {
  11010. return vis;
  11011. });
  11012. }
  11013. /**
  11014. * Window exports
  11015. */
  11016. if (typeof window !== 'undefined') {
  11017. // attach the module to the window, load as a regular javascript file
  11018. window['vis'] = vis;
  11019. }
  11020. // inject css
  11021. util.loadCss("/* vis.js stylesheet */\n\n.graph {\n position: relative;\n border: 1px solid #bfbfbf;\n}\n\n.graph .panel {\n position: absolute;\n}\n\n.graph .groupset {\n position: absolute;\n padding: 0;\n margin: 0;\n}\n\n\n.graph .itemset {\n position: absolute;\n padding: 0;\n margin: 0;\n overflow: hidden;\n}\n\n.graph .background {\n}\n\n.graph .foreground {\n}\n\n.graph .itemset-axis {\n position: absolute;\n}\n\n.graph .groupset .itemset-axis {\n border-top: 1px solid #bfbfbf;\n}\n\n/* TODO: with orientation=='bottom', this will more or less overlap with timeline axis\n.graph .groupset .itemset-axis:last-child {\n border-top: none;\n}\n*/\n\n\n.graph .item {\n position: absolute;\n color: #1A1A1A;\n border-color: #97B0F8;\n background-color: #D5DDF6;\n display: inline-block;\n}\n\n.graph .item.selected {\n border-color: #FFC200;\n background-color: #FFF785;\n z-index: 999;\n}\n\n.graph .item.cluster {\n /* TODO: use another color or pattern? */\n background: #97B0F8 url('img/cluster_bg.png');\n color: white;\n}\n.graph .item.cluster.point {\n border-color: #D5DDF6;\n}\n\n.graph .item.box {\n text-align: center;\n border-style: solid;\n border-width: 1px;\n border-radius: 5px;\n -moz-border-radius: 5px; /* For Firefox 3.6 and older */\n}\n\n.graph .item.point {\n background: none;\n}\n\n.graph .dot {\n border: 5px solid #97B0F8;\n position: absolute;\n border-radius: 5px;\n -moz-border-radius: 5px; /* For Firefox 3.6 and older */\n}\n\n.graph .item.range {\n overflow: hidden;\n border-style: solid;\n border-width: 1px;\n border-radius: 2px;\n -moz-border-radius: 2px; /* For Firefox 3.6 and older */\n}\n\n.graph .item.range .drag-left {\n cursor: w-resize;\n z-index: 1000;\n}\n\n.graph .item.range .drag-right {\n cursor: e-resize;\n z-index: 1000;\n}\n\n.graph .item.range .content {\n position: relative;\n display: inline-block;\n}\n\n.graph .item.line {\n position: absolute;\n width: 0;\n border-left-width: 1px;\n border-left-style: solid;\n}\n\n.graph .item .content {\n margin: 5px;\n white-space: nowrap;\n overflow: hidden;\n}\n\n/* TODO: better css name, 'graph' is way to generic */\n\n.graph {\n overflow: hidden;\n}\n\n.graph .axis {\n position: relative;\n}\n\n.graph .axis .text {\n position: absolute;\n color: #4d4d4d;\n padding: 3px;\n white-space: nowrap;\n}\n\n.graph .axis .text.measure {\n position: absolute;\n padding-left: 0;\n padding-right: 0;\n margin-left: 0;\n margin-right: 0;\n visibility: hidden;\n}\n\n.graph .axis .grid.vertical {\n position: absolute;\n width: 0;\n border-right: 1px solid;\n}\n\n.graph .axis .grid.horizontal {\n position: absolute;\n left: 0;\n width: 100%;\n height: 0;\n border-bottom: 1px solid;\n}\n\n.graph .axis .grid.minor {\n border-color: #e5e5e5;\n}\n\n.graph .axis .grid.major {\n border-color: #bfbfbf;\n}\n\n");
  11022. })()
  11023. },{"moment":2}],2:[function(require,module,exports){
  11024. (function(){// moment.js
  11025. // version : 2.0.0
  11026. // author : Tim Wood
  11027. // license : MIT
  11028. // momentjs.com
  11029. (function (undefined) {
  11030. /************************************
  11031. Constants
  11032. ************************************/
  11033. var moment,
  11034. VERSION = "2.0.0",
  11035. round = Math.round, i,
  11036. // internal storage for language config files
  11037. languages = {},
  11038. // check for nodeJS
  11039. hasModule = (typeof module !== 'undefined' && module.exports),
  11040. // ASP.NET json date format regex
  11041. aspNetJsonRegex = /^\/?Date\((\-?\d+)/i,
  11042. // format tokens
  11043. 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,
  11044. localFormattingTokens = /(\[[^\[]*\])|(\\)?(LT|LL?L?L?|l{1,4})/g,
  11045. // parsing tokens
  11046. parseMultipleFormatChunker = /([0-9a-zA-Z\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]+)/gi,
  11047. // parsing token regexes
  11048. parseTokenOneOrTwoDigits = /\d\d?/, // 0 - 99
  11049. parseTokenOneToThreeDigits = /\d{1,3}/, // 0 - 999
  11050. parseTokenThreeDigits = /\d{3}/, // 000 - 999
  11051. parseTokenFourDigits = /\d{1,4}/, // 0 - 9999
  11052. parseTokenSixDigits = /[+\-]?\d{1,6}/, // -999,999 - 999,999
  11053. 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.
  11054. parseTokenTimezone = /Z|[\+\-]\d\d:?\d\d/i, // +00:00 -00:00 +0000 -0000 or Z
  11055. parseTokenT = /T/i, // T (ISO seperator)
  11056. parseTokenTimestampMs = /[\+\-]?\d+(\.\d{1,3})?/, // 123456789 123456789.123
  11057. // preliminary iso regex
  11058. // 0000-00-00 + T + 00 or 00:00 or 00:00:00 or 00:00:00.000 + +00:00 or +0000
  11059. isoRegex = /^\s*\d{4}-\d\d-\d\d((T| )(\d\d(:\d\d(:\d\d(\.\d\d?\d?)?)?)?)?([\+\-]\d\d:?\d\d)?)?/,
  11060. isoFormat = 'YYYY-MM-DDTHH:mm:ssZ',
  11061. // iso time formats and regexes
  11062. isoTimes = [
  11063. ['HH:mm:ss.S', /(T| )\d\d:\d\d:\d\d\.\d{1,3}/],
  11064. ['HH:mm:ss', /(T| )\d\d:\d\d:\d\d/],
  11065. ['HH:mm', /(T| )\d\d:\d\d/],
  11066. ['HH', /(T| )\d\d/]
  11067. ],
  11068. // timezone chunker "+10:00" > ["10", "00"] or "-1530" > ["-15", "30"]
  11069. parseTimezoneChunker = /([\+\-]|\d\d)/gi,
  11070. // getter and setter names
  11071. proxyGettersAndSetters = 'Month|Date|Hours|Minutes|Seconds|Milliseconds'.split('|'),
  11072. unitMillisecondFactors = {
  11073. 'Milliseconds' : 1,
  11074. 'Seconds' : 1e3,
  11075. 'Minutes' : 6e4,
  11076. 'Hours' : 36e5,
  11077. 'Days' : 864e5,
  11078. 'Months' : 2592e6,
  11079. 'Years' : 31536e6
  11080. },
  11081. // format function strings
  11082. formatFunctions = {},
  11083. // tokens to ordinalize and pad
  11084. ordinalizeTokens = 'DDD w W M D d'.split(' '),
  11085. paddedTokens = 'M D H h m s w W'.split(' '),
  11086. formatTokenFunctions = {
  11087. M : function () {
  11088. return this.month() + 1;
  11089. },
  11090. MMM : function (format) {
  11091. return this.lang().monthsShort(this, format);
  11092. },
  11093. MMMM : function (format) {
  11094. return this.lang().months(this, format);
  11095. },
  11096. D : function () {
  11097. return this.date();
  11098. },
  11099. DDD : function () {
  11100. return this.dayOfYear();
  11101. },
  11102. d : function () {
  11103. return this.day();
  11104. },
  11105. dd : function (format) {
  11106. return this.lang().weekdaysMin(this, format);
  11107. },
  11108. ddd : function (format) {
  11109. return this.lang().weekdaysShort(this, format);
  11110. },
  11111. dddd : function (format) {
  11112. return this.lang().weekdays(this, format);
  11113. },
  11114. w : function () {
  11115. return this.week();
  11116. },
  11117. W : function () {
  11118. return this.isoWeek();
  11119. },
  11120. YY : function () {
  11121. return leftZeroFill(this.year() % 100, 2);
  11122. },
  11123. YYYY : function () {
  11124. return leftZeroFill(this.year(), 4);
  11125. },
  11126. YYYYY : function () {
  11127. return leftZeroFill(this.year(), 5);
  11128. },
  11129. a : function () {
  11130. return this.lang().meridiem(this.hours(), this.minutes(), true);
  11131. },
  11132. A : function () {
  11133. return this.lang().meridiem(this.hours(), this.minutes(), false);
  11134. },
  11135. H : function () {
  11136. return this.hours();
  11137. },
  11138. h : function () {
  11139. return this.hours() % 12 || 12;
  11140. },
  11141. m : function () {
  11142. return this.minutes();
  11143. },
  11144. s : function () {
  11145. return this.seconds();
  11146. },
  11147. S : function () {
  11148. return ~~(this.milliseconds() / 100);
  11149. },
  11150. SS : function () {
  11151. return leftZeroFill(~~(this.milliseconds() / 10), 2);
  11152. },
  11153. SSS : function () {
  11154. return leftZeroFill(this.milliseconds(), 3);
  11155. },
  11156. Z : function () {
  11157. var a = -this.zone(),
  11158. b = "+";
  11159. if (a < 0) {
  11160. a = -a;
  11161. b = "-";
  11162. }
  11163. return b + leftZeroFill(~~(a / 60), 2) + ":" + leftZeroFill(~~a % 60, 2);
  11164. },
  11165. ZZ : function () {
  11166. var a = -this.zone(),
  11167. b = "+";
  11168. if (a < 0) {
  11169. a = -a;
  11170. b = "-";
  11171. }
  11172. return b + leftZeroFill(~~(10 * a / 6), 4);
  11173. },
  11174. X : function () {
  11175. return this.unix();
  11176. }
  11177. };
  11178. function padToken(func, count) {
  11179. return function (a) {
  11180. return leftZeroFill(func.call(this, a), count);
  11181. };
  11182. }
  11183. function ordinalizeToken(func) {
  11184. return function (a) {
  11185. return this.lang().ordinal(func.call(this, a));
  11186. };
  11187. }
  11188. while (ordinalizeTokens.length) {
  11189. i = ordinalizeTokens.pop();
  11190. formatTokenFunctions[i + 'o'] = ordinalizeToken(formatTokenFunctions[i]);
  11191. }
  11192. while (paddedTokens.length) {
  11193. i = paddedTokens.pop();
  11194. formatTokenFunctions[i + i] = padToken(formatTokenFunctions[i], 2);
  11195. }
  11196. formatTokenFunctions.DDDD = padToken(formatTokenFunctions.DDD, 3);
  11197. /************************************
  11198. Constructors
  11199. ************************************/
  11200. function Language() {
  11201. }
  11202. // Moment prototype object
  11203. function Moment(config) {
  11204. extend(this, config);
  11205. }
  11206. // Duration Constructor
  11207. function Duration(duration) {
  11208. var data = this._data = {},
  11209. years = duration.years || duration.year || duration.y || 0,
  11210. months = duration.months || duration.month || duration.M || 0,
  11211. weeks = duration.weeks || duration.week || duration.w || 0,
  11212. days = duration.days || duration.day || duration.d || 0,
  11213. hours = duration.hours || duration.hour || duration.h || 0,
  11214. minutes = duration.minutes || duration.minute || duration.m || 0,
  11215. seconds = duration.seconds || duration.second || duration.s || 0,
  11216. milliseconds = duration.milliseconds || duration.millisecond || duration.ms || 0;
  11217. // representation for dateAddRemove
  11218. this._milliseconds = milliseconds +
  11219. seconds * 1e3 + // 1000
  11220. minutes * 6e4 + // 1000 * 60
  11221. hours * 36e5; // 1000 * 60 * 60
  11222. // Because of dateAddRemove treats 24 hours as different from a
  11223. // day when working around DST, we need to store them separately
  11224. this._days = days +
  11225. weeks * 7;
  11226. // It is impossible translate months into days without knowing
  11227. // which months you are are talking about, so we have to store
  11228. // it separately.
  11229. this._months = months +
  11230. years * 12;
  11231. // The following code bubbles up values, see the tests for
  11232. // examples of what that means.
  11233. data.milliseconds = milliseconds % 1000;
  11234. seconds += absRound(milliseconds / 1000);
  11235. data.seconds = seconds % 60;
  11236. minutes += absRound(seconds / 60);
  11237. data.minutes = minutes % 60;
  11238. hours += absRound(minutes / 60);
  11239. data.hours = hours % 24;
  11240. days += absRound(hours / 24);
  11241. days += weeks * 7;
  11242. data.days = days % 30;
  11243. months += absRound(days / 30);
  11244. data.months = months % 12;
  11245. years += absRound(months / 12);
  11246. data.years = years;
  11247. }
  11248. /************************************
  11249. Helpers
  11250. ************************************/
  11251. function extend(a, b) {
  11252. for (var i in b) {
  11253. if (b.hasOwnProperty(i)) {
  11254. a[i] = b[i];
  11255. }
  11256. }
  11257. return a;
  11258. }
  11259. function absRound(number) {
  11260. if (number < 0) {
  11261. return Math.ceil(number);
  11262. } else {
  11263. return Math.floor(number);
  11264. }
  11265. }
  11266. // left zero fill a number
  11267. // see http://jsperf.com/left-zero-filling for performance comparison
  11268. function leftZeroFill(number, targetLength) {
  11269. var output = number + '';
  11270. while (output.length < targetLength) {
  11271. output = '0' + output;
  11272. }
  11273. return output;
  11274. }
  11275. // helper function for _.addTime and _.subtractTime
  11276. function addOrSubtractDurationFromMoment(mom, duration, isAdding) {
  11277. var ms = duration._milliseconds,
  11278. d = duration._days,
  11279. M = duration._months,
  11280. currentDate;
  11281. if (ms) {
  11282. mom._d.setTime(+mom + ms * isAdding);
  11283. }
  11284. if (d) {
  11285. mom.date(mom.date() + d * isAdding);
  11286. }
  11287. if (M) {
  11288. currentDate = mom.date();
  11289. mom.date(1)
  11290. .month(mom.month() + M * isAdding)
  11291. .date(Math.min(currentDate, mom.daysInMonth()));
  11292. }
  11293. }
  11294. // check if is an array
  11295. function isArray(input) {
  11296. return Object.prototype.toString.call(input) === '[object Array]';
  11297. }
  11298. // compare two arrays, return the number of differences
  11299. function compareArrays(array1, array2) {
  11300. var len = Math.min(array1.length, array2.length),
  11301. lengthDiff = Math.abs(array1.length - array2.length),
  11302. diffs = 0,
  11303. i;
  11304. for (i = 0; i < len; i++) {
  11305. if (~~array1[i] !== ~~array2[i]) {
  11306. diffs++;
  11307. }
  11308. }
  11309. return diffs + lengthDiff;
  11310. }
  11311. /************************************
  11312. Languages
  11313. ************************************/
  11314. Language.prototype = {
  11315. set : function (config) {
  11316. var prop, i;
  11317. for (i in config) {
  11318. prop = config[i];
  11319. if (typeof prop === 'function') {
  11320. this[i] = prop;
  11321. } else {
  11322. this['_' + i] = prop;
  11323. }
  11324. }
  11325. },
  11326. _months : "January_February_March_April_May_June_July_August_September_October_November_December".split("_"),
  11327. months : function (m) {
  11328. return this._months[m.month()];
  11329. },
  11330. _monthsShort : "Jan_Feb_Mar_Apr_May_Jun_Jul_Aug_Sep_Oct_Nov_Dec".split("_"),
  11331. monthsShort : function (m) {
  11332. return this._monthsShort[m.month()];
  11333. },
  11334. monthsParse : function (monthName) {
  11335. var i, mom, regex, output;
  11336. if (!this._monthsParse) {
  11337. this._monthsParse = [];
  11338. }
  11339. for (i = 0; i < 12; i++) {
  11340. // make the regex if we don't have it already
  11341. if (!this._monthsParse[i]) {
  11342. mom = moment([2000, i]);
  11343. regex = '^' + this.months(mom, '') + '|^' + this.monthsShort(mom, '');
  11344. this._monthsParse[i] = new RegExp(regex.replace('.', ''), 'i');
  11345. }
  11346. // test the regex
  11347. if (this._monthsParse[i].test(monthName)) {
  11348. return i;
  11349. }
  11350. }
  11351. },
  11352. _weekdays : "Sunday_Monday_Tuesday_Wednesday_Thursday_Friday_Saturday".split("_"),
  11353. weekdays : function (m) {
  11354. return this._weekdays[m.day()];
  11355. },
  11356. _weekdaysShort : "Sun_Mon_Tue_Wed_Thu_Fri_Sat".split("_"),
  11357. weekdaysShort : function (m) {
  11358. return this._weekdaysShort[m.day()];
  11359. },
  11360. _weekdaysMin : "Su_Mo_Tu_We_Th_Fr_Sa".split("_"),
  11361. weekdaysMin : function (m) {
  11362. return this._weekdaysMin[m.day()];
  11363. },
  11364. _longDateFormat : {
  11365. LT : "h:mm A",
  11366. L : "MM/DD/YYYY",
  11367. LL : "MMMM D YYYY",
  11368. LLL : "MMMM D YYYY LT",
  11369. LLLL : "dddd, MMMM D YYYY LT"
  11370. },
  11371. longDateFormat : function (key) {
  11372. var output = this._longDateFormat[key];
  11373. if (!output && this._longDateFormat[key.toUpperCase()]) {
  11374. output = this._longDateFormat[key.toUpperCase()].replace(/MMMM|MM|DD|dddd/g, function (val) {
  11375. return val.slice(1);
  11376. });
  11377. this._longDateFormat[key] = output;
  11378. }
  11379. return output;
  11380. },
  11381. meridiem : function (hours, minutes, isLower) {
  11382. if (hours > 11) {
  11383. return isLower ? 'pm' : 'PM';
  11384. } else {
  11385. return isLower ? 'am' : 'AM';
  11386. }
  11387. },
  11388. _calendar : {
  11389. sameDay : '[Today at] LT',
  11390. nextDay : '[Tomorrow at] LT',
  11391. nextWeek : 'dddd [at] LT',
  11392. lastDay : '[Yesterday at] LT',
  11393. lastWeek : '[last] dddd [at] LT',
  11394. sameElse : 'L'
  11395. },
  11396. calendar : function (key, mom) {
  11397. var output = this._calendar[key];
  11398. return typeof output === 'function' ? output.apply(mom) : output;
  11399. },
  11400. _relativeTime : {
  11401. future : "in %s",
  11402. past : "%s ago",
  11403. s : "a few seconds",
  11404. m : "a minute",
  11405. mm : "%d minutes",
  11406. h : "an hour",
  11407. hh : "%d hours",
  11408. d : "a day",
  11409. dd : "%d days",
  11410. M : "a month",
  11411. MM : "%d months",
  11412. y : "a year",
  11413. yy : "%d years"
  11414. },
  11415. relativeTime : function (number, withoutSuffix, string, isFuture) {
  11416. var output = this._relativeTime[string];
  11417. return (typeof output === 'function') ?
  11418. output(number, withoutSuffix, string, isFuture) :
  11419. output.replace(/%d/i, number);
  11420. },
  11421. pastFuture : function (diff, output) {
  11422. var format = this._relativeTime[diff > 0 ? 'future' : 'past'];
  11423. return typeof format === 'function' ? format(output) : format.replace(/%s/i, output);
  11424. },
  11425. ordinal : function (number) {
  11426. return this._ordinal.replace("%d", number);
  11427. },
  11428. _ordinal : "%d",
  11429. preparse : function (string) {
  11430. return string;
  11431. },
  11432. postformat : function (string) {
  11433. return string;
  11434. },
  11435. week : function (mom) {
  11436. return weekOfYear(mom, this._week.dow, this._week.doy);
  11437. },
  11438. _week : {
  11439. dow : 0, // Sunday is the first day of the week.
  11440. doy : 6 // The week that contains Jan 1st is the first week of the year.
  11441. }
  11442. };
  11443. // Loads a language definition into the `languages` cache. The function
  11444. // takes a key and optionally values. If not in the browser and no values
  11445. // are provided, it will load the language file module. As a convenience,
  11446. // this function also returns the language values.
  11447. function loadLang(key, values) {
  11448. values.abbr = key;
  11449. if (!languages[key]) {
  11450. languages[key] = new Language();
  11451. }
  11452. languages[key].set(values);
  11453. return languages[key];
  11454. }
  11455. // Determines which language definition to use and returns it.
  11456. //
  11457. // With no parameters, it will return the global language. If you
  11458. // pass in a language key, such as 'en', it will return the
  11459. // definition for 'en', so long as 'en' has already been loaded using
  11460. // moment.lang.
  11461. function getLangDefinition(key) {
  11462. if (!key) {
  11463. return moment.fn._lang;
  11464. }
  11465. if (!languages[key] && hasModule) {
  11466. require('./lang/' + key);
  11467. }
  11468. return languages[key];
  11469. }
  11470. /************************************
  11471. Formatting
  11472. ************************************/
  11473. function removeFormattingTokens(input) {
  11474. if (input.match(/\[.*\]/)) {
  11475. return input.replace(/^\[|\]$/g, "");
  11476. }
  11477. return input.replace(/\\/g, "");
  11478. }
  11479. function makeFormatFunction(format) {
  11480. var array = format.match(formattingTokens), i, length;
  11481. for (i = 0, length = array.length; i < length; i++) {
  11482. if (formatTokenFunctions[array[i]]) {
  11483. array[i] = formatTokenFunctions[array[i]];
  11484. } else {
  11485. array[i] = removeFormattingTokens(array[i]);
  11486. }
  11487. }
  11488. return function (mom) {
  11489. var output = "";
  11490. for (i = 0; i < length; i++) {
  11491. output += typeof array[i].call === 'function' ? array[i].call(mom, format) : array[i];
  11492. }
  11493. return output;
  11494. };
  11495. }
  11496. // format date using native date object
  11497. function formatMoment(m, format) {
  11498. var i = 5;
  11499. function replaceLongDateFormatTokens(input) {
  11500. return m.lang().longDateFormat(input) || input;
  11501. }
  11502. while (i-- && localFormattingTokens.test(format)) {
  11503. format = format.replace(localFormattingTokens, replaceLongDateFormatTokens);
  11504. }
  11505. if (!formatFunctions[format]) {
  11506. formatFunctions[format] = makeFormatFunction(format);
  11507. }
  11508. return formatFunctions[format](m);
  11509. }
  11510. /************************************
  11511. Parsing
  11512. ************************************/
  11513. // get the regex to find the next token
  11514. function getParseRegexForToken(token) {
  11515. switch (token) {
  11516. case 'DDDD':
  11517. return parseTokenThreeDigits;
  11518. case 'YYYY':
  11519. return parseTokenFourDigits;
  11520. case 'YYYYY':
  11521. return parseTokenSixDigits;
  11522. case 'S':
  11523. case 'SS':
  11524. case 'SSS':
  11525. case 'DDD':
  11526. return parseTokenOneToThreeDigits;
  11527. case 'MMM':
  11528. case 'MMMM':
  11529. case 'dd':
  11530. case 'ddd':
  11531. case 'dddd':
  11532. case 'a':
  11533. case 'A':
  11534. return parseTokenWord;
  11535. case 'X':
  11536. return parseTokenTimestampMs;
  11537. case 'Z':
  11538. case 'ZZ':
  11539. return parseTokenTimezone;
  11540. case 'T':
  11541. return parseTokenT;
  11542. case 'MM':
  11543. case 'DD':
  11544. case 'YY':
  11545. case 'HH':
  11546. case 'hh':
  11547. case 'mm':
  11548. case 'ss':
  11549. case 'M':
  11550. case 'D':
  11551. case 'd':
  11552. case 'H':
  11553. case 'h':
  11554. case 'm':
  11555. case 's':
  11556. return parseTokenOneOrTwoDigits;
  11557. default :
  11558. return new RegExp(token.replace('\\', ''));
  11559. }
  11560. }
  11561. // function to convert string input to date
  11562. function addTimeToArrayFromToken(token, input, config) {
  11563. var a, b,
  11564. datePartArray = config._a;
  11565. switch (token) {
  11566. // MONTH
  11567. case 'M' : // fall through to MM
  11568. case 'MM' :
  11569. datePartArray[1] = (input == null) ? 0 : ~~input - 1;
  11570. break;
  11571. case 'MMM' : // fall through to MMMM
  11572. case 'MMMM' :
  11573. a = getLangDefinition(config._l).monthsParse(input);
  11574. // if we didn't find a month name, mark the date as invalid.
  11575. if (a != null) {
  11576. datePartArray[1] = a;
  11577. } else {
  11578. config._isValid = false;
  11579. }
  11580. break;
  11581. // DAY OF MONTH
  11582. case 'D' : // fall through to DDDD
  11583. case 'DD' : // fall through to DDDD
  11584. case 'DDD' : // fall through to DDDD
  11585. case 'DDDD' :
  11586. if (input != null) {
  11587. datePartArray[2] = ~~input;
  11588. }
  11589. break;
  11590. // YEAR
  11591. case 'YY' :
  11592. datePartArray[0] = ~~input + (~~input > 68 ? 1900 : 2000);
  11593. break;
  11594. case 'YYYY' :
  11595. case 'YYYYY' :
  11596. datePartArray[0] = ~~input;
  11597. break;
  11598. // AM / PM
  11599. case 'a' : // fall through to A
  11600. case 'A' :
  11601. config._isPm = ((input + '').toLowerCase() === 'pm');
  11602. break;
  11603. // 24 HOUR
  11604. case 'H' : // fall through to hh
  11605. case 'HH' : // fall through to hh
  11606. case 'h' : // fall through to hh
  11607. case 'hh' :
  11608. datePartArray[3] = ~~input;
  11609. break;
  11610. // MINUTE
  11611. case 'm' : // fall through to mm
  11612. case 'mm' :
  11613. datePartArray[4] = ~~input;
  11614. break;
  11615. // SECOND
  11616. case 's' : // fall through to ss
  11617. case 'ss' :
  11618. datePartArray[5] = ~~input;
  11619. break;
  11620. // MILLISECOND
  11621. case 'S' :
  11622. case 'SS' :
  11623. case 'SSS' :
  11624. datePartArray[6] = ~~ (('0.' + input) * 1000);
  11625. break;
  11626. // UNIX TIMESTAMP WITH MS
  11627. case 'X':
  11628. config._d = new Date(parseFloat(input) * 1000);
  11629. break;
  11630. // TIMEZONE
  11631. case 'Z' : // fall through to ZZ
  11632. case 'ZZ' :
  11633. config._useUTC = true;
  11634. a = (input + '').match(parseTimezoneChunker);
  11635. if (a && a[1]) {
  11636. config._tzh = ~~a[1];
  11637. }
  11638. if (a && a[2]) {
  11639. config._tzm = ~~a[2];
  11640. }
  11641. // reverse offsets
  11642. if (a && a[0] === '+') {
  11643. config._tzh = -config._tzh;
  11644. config._tzm = -config._tzm;
  11645. }
  11646. break;
  11647. }
  11648. // if the input is null, the date is not valid
  11649. if (input == null) {
  11650. config._isValid = false;
  11651. }
  11652. }
  11653. // convert an array to a date.
  11654. // the array should mirror the parameters below
  11655. // note: all values past the year are optional and will default to the lowest possible value.
  11656. // [year, month, day , hour, minute, second, millisecond]
  11657. function dateFromArray(config) {
  11658. var i, date, input = [];
  11659. if (config._d) {
  11660. return;
  11661. }
  11662. for (i = 0; i < 7; i++) {
  11663. config._a[i] = input[i] = (config._a[i] == null) ? (i === 2 ? 1 : 0) : config._a[i];
  11664. }
  11665. // add the offsets to the time to be parsed so that we can have a clean array for checking isValid
  11666. input[3] += config._tzh || 0;
  11667. input[4] += config._tzm || 0;
  11668. date = new Date(0);
  11669. if (config._useUTC) {
  11670. date.setUTCFullYear(input[0], input[1], input[2]);
  11671. date.setUTCHours(input[3], input[4], input[5], input[6]);
  11672. } else {
  11673. date.setFullYear(input[0], input[1], input[2]);
  11674. date.setHours(input[3], input[4], input[5], input[6]);
  11675. }
  11676. config._d = date;
  11677. }
  11678. // date from string and format string
  11679. function makeDateFromStringAndFormat(config) {
  11680. // This array is used to make a Date, either with `new Date` or `Date.UTC`
  11681. var tokens = config._f.match(formattingTokens),
  11682. string = config._i,
  11683. i, parsedInput;
  11684. config._a = [];
  11685. for (i = 0; i < tokens.length; i++) {
  11686. parsedInput = (getParseRegexForToken(tokens[i]).exec(string) || [])[0];
  11687. if (parsedInput) {
  11688. string = string.slice(string.indexOf(parsedInput) + parsedInput.length);
  11689. }
  11690. // don't parse if its not a known token
  11691. if (formatTokenFunctions[tokens[i]]) {
  11692. addTimeToArrayFromToken(tokens[i], parsedInput, config);
  11693. }
  11694. }
  11695. // handle am pm
  11696. if (config._isPm && config._a[3] < 12) {
  11697. config._a[3] += 12;
  11698. }
  11699. // if is 12 am, change hours to 0
  11700. if (config._isPm === false && config._a[3] === 12) {
  11701. config._a[3] = 0;
  11702. }
  11703. // return
  11704. dateFromArray(config);
  11705. }
  11706. // date from string and array of format strings
  11707. function makeDateFromStringAndArray(config) {
  11708. var tempConfig,
  11709. tempMoment,
  11710. bestMoment,
  11711. scoreToBeat = 99,
  11712. i,
  11713. currentDate,
  11714. currentScore;
  11715. while (config._f.length) {
  11716. tempConfig = extend({}, config);
  11717. tempConfig._f = config._f.pop();
  11718. makeDateFromStringAndFormat(tempConfig);
  11719. tempMoment = new Moment(tempConfig);
  11720. if (tempMoment.isValid()) {
  11721. bestMoment = tempMoment;
  11722. break;
  11723. }
  11724. currentScore = compareArrays(tempConfig._a, tempMoment.toArray());
  11725. if (currentScore < scoreToBeat) {
  11726. scoreToBeat = currentScore;
  11727. bestMoment = tempMoment;
  11728. }
  11729. }
  11730. extend(config, bestMoment);
  11731. }
  11732. // date from iso format
  11733. function makeDateFromString(config) {
  11734. var i,
  11735. string = config._i;
  11736. if (isoRegex.exec(string)) {
  11737. config._f = 'YYYY-MM-DDT';
  11738. for (i = 0; i < 4; i++) {
  11739. if (isoTimes[i][1].exec(string)) {
  11740. config._f += isoTimes[i][0];
  11741. break;
  11742. }
  11743. }
  11744. if (parseTokenTimezone.exec(string)) {
  11745. config._f += " Z";
  11746. }
  11747. makeDateFromStringAndFormat(config);
  11748. } else {
  11749. config._d = new Date(string);
  11750. }
  11751. }
  11752. function makeDateFromInput(config) {
  11753. var input = config._i,
  11754. matched = aspNetJsonRegex.exec(input);
  11755. if (input === undefined) {
  11756. config._d = new Date();
  11757. } else if (matched) {
  11758. config._d = new Date(+matched[1]);
  11759. } else if (typeof input === 'string') {
  11760. makeDateFromString(config);
  11761. } else if (isArray(input)) {
  11762. config._a = input.slice(0);
  11763. dateFromArray(config);
  11764. } else {
  11765. config._d = input instanceof Date ? new Date(+input) : new Date(input);
  11766. }
  11767. }
  11768. /************************************
  11769. Relative Time
  11770. ************************************/
  11771. // helper function for moment.fn.from, moment.fn.fromNow, and moment.duration.fn.humanize
  11772. function substituteTimeAgo(string, number, withoutSuffix, isFuture, lang) {
  11773. return lang.relativeTime(number || 1, !!withoutSuffix, string, isFuture);
  11774. }
  11775. function relativeTime(milliseconds, withoutSuffix, lang) {
  11776. var seconds = round(Math.abs(milliseconds) / 1000),
  11777. minutes = round(seconds / 60),
  11778. hours = round(minutes / 60),
  11779. days = round(hours / 24),
  11780. years = round(days / 365),
  11781. args = seconds < 45 && ['s', seconds] ||
  11782. minutes === 1 && ['m'] ||
  11783. minutes < 45 && ['mm', minutes] ||
  11784. hours === 1 && ['h'] ||
  11785. hours < 22 && ['hh', hours] ||
  11786. days === 1 && ['d'] ||
  11787. days <= 25 && ['dd', days] ||
  11788. days <= 45 && ['M'] ||
  11789. days < 345 && ['MM', round(days / 30)] ||
  11790. years === 1 && ['y'] || ['yy', years];
  11791. args[2] = withoutSuffix;
  11792. args[3] = milliseconds > 0;
  11793. args[4] = lang;
  11794. return substituteTimeAgo.apply({}, args);
  11795. }
  11796. /************************************
  11797. Week of Year
  11798. ************************************/
  11799. // firstDayOfWeek 0 = sun, 6 = sat
  11800. // the day of the week that starts the week
  11801. // (usually sunday or monday)
  11802. // firstDayOfWeekOfYear 0 = sun, 6 = sat
  11803. // the first week is the week that contains the first
  11804. // of this day of the week
  11805. // (eg. ISO weeks use thursday (4))
  11806. function weekOfYear(mom, firstDayOfWeek, firstDayOfWeekOfYear) {
  11807. var end = firstDayOfWeekOfYear - firstDayOfWeek,
  11808. daysToDayOfWeek = firstDayOfWeekOfYear - mom.day();
  11809. if (daysToDayOfWeek > end) {
  11810. daysToDayOfWeek -= 7;
  11811. }
  11812. if (daysToDayOfWeek < end - 7) {
  11813. daysToDayOfWeek += 7;
  11814. }
  11815. return Math.ceil(moment(mom).add('d', daysToDayOfWeek).dayOfYear() / 7);
  11816. }
  11817. /************************************
  11818. Top Level Functions
  11819. ************************************/
  11820. function makeMoment(config) {
  11821. var input = config._i,
  11822. format = config._f;
  11823. if (input === null || input === '') {
  11824. return null;
  11825. }
  11826. if (typeof input === 'string') {
  11827. config._i = input = getLangDefinition().preparse(input);
  11828. }
  11829. if (moment.isMoment(input)) {
  11830. config = extend({}, input);
  11831. config._d = new Date(+input._d);
  11832. } else if (format) {
  11833. if (isArray(format)) {
  11834. makeDateFromStringAndArray(config);
  11835. } else {
  11836. makeDateFromStringAndFormat(config);
  11837. }
  11838. } else {
  11839. makeDateFromInput(config);
  11840. }
  11841. return new Moment(config);
  11842. }
  11843. moment = function (input, format, lang) {
  11844. return makeMoment({
  11845. _i : input,
  11846. _f : format,
  11847. _l : lang,
  11848. _isUTC : false
  11849. });
  11850. };
  11851. // creating with utc
  11852. moment.utc = function (input, format, lang) {
  11853. return makeMoment({
  11854. _useUTC : true,
  11855. _isUTC : true,
  11856. _l : lang,
  11857. _i : input,
  11858. _f : format
  11859. });
  11860. };
  11861. // creating with unix timestamp (in seconds)
  11862. moment.unix = function (input) {
  11863. return moment(input * 1000);
  11864. };
  11865. // duration
  11866. moment.duration = function (input, key) {
  11867. var isDuration = moment.isDuration(input),
  11868. isNumber = (typeof input === 'number'),
  11869. duration = (isDuration ? input._data : (isNumber ? {} : input)),
  11870. ret;
  11871. if (isNumber) {
  11872. if (key) {
  11873. duration[key] = input;
  11874. } else {
  11875. duration.milliseconds = input;
  11876. }
  11877. }
  11878. ret = new Duration(duration);
  11879. if (isDuration && input.hasOwnProperty('_lang')) {
  11880. ret._lang = input._lang;
  11881. }
  11882. return ret;
  11883. };
  11884. // version number
  11885. moment.version = VERSION;
  11886. // default format
  11887. moment.defaultFormat = isoFormat;
  11888. // This function will load languages and then set the global language. If
  11889. // no arguments are passed in, it will simply return the current global
  11890. // language key.
  11891. moment.lang = function (key, values) {
  11892. var i;
  11893. if (!key) {
  11894. return moment.fn._lang._abbr;
  11895. }
  11896. if (values) {
  11897. loadLang(key, values);
  11898. } else if (!languages[key]) {
  11899. getLangDefinition(key);
  11900. }
  11901. moment.duration.fn._lang = moment.fn._lang = getLangDefinition(key);
  11902. };
  11903. // returns language data
  11904. moment.langData = function (key) {
  11905. if (key && key._lang && key._lang._abbr) {
  11906. key = key._lang._abbr;
  11907. }
  11908. return getLangDefinition(key);
  11909. };
  11910. // compare moment object
  11911. moment.isMoment = function (obj) {
  11912. return obj instanceof Moment;
  11913. };
  11914. // for typechecking Duration objects
  11915. moment.isDuration = function (obj) {
  11916. return obj instanceof Duration;
  11917. };
  11918. /************************************
  11919. Moment Prototype
  11920. ************************************/
  11921. moment.fn = Moment.prototype = {
  11922. clone : function () {
  11923. return moment(this);
  11924. },
  11925. valueOf : function () {
  11926. return +this._d;
  11927. },
  11928. unix : function () {
  11929. return Math.floor(+this._d / 1000);
  11930. },
  11931. toString : function () {
  11932. return this.format("ddd MMM DD YYYY HH:mm:ss [GMT]ZZ");
  11933. },
  11934. toDate : function () {
  11935. return this._d;
  11936. },
  11937. toJSON : function () {
  11938. return moment.utc(this).format('YYYY-MM-DD[T]HH:mm:ss.SSS[Z]');
  11939. },
  11940. toArray : function () {
  11941. var m = this;
  11942. return [
  11943. m.year(),
  11944. m.month(),
  11945. m.date(),
  11946. m.hours(),
  11947. m.minutes(),
  11948. m.seconds(),
  11949. m.milliseconds()
  11950. ];
  11951. },
  11952. isValid : function () {
  11953. if (this._isValid == null) {
  11954. if (this._a) {
  11955. this._isValid = !compareArrays(this._a, (this._isUTC ? moment.utc(this._a) : moment(this._a)).toArray());
  11956. } else {
  11957. this._isValid = !isNaN(this._d.getTime());
  11958. }
  11959. }
  11960. return !!this._isValid;
  11961. },
  11962. utc : function () {
  11963. this._isUTC = true;
  11964. return this;
  11965. },
  11966. local : function () {
  11967. this._isUTC = false;
  11968. return this;
  11969. },
  11970. format : function (inputString) {
  11971. var output = formatMoment(this, inputString || moment.defaultFormat);
  11972. return this.lang().postformat(output);
  11973. },
  11974. add : function (input, val) {
  11975. var dur;
  11976. // switch args to support add('s', 1) and add(1, 's')
  11977. if (typeof input === 'string') {
  11978. dur = moment.duration(+val, input);
  11979. } else {
  11980. dur = moment.duration(input, val);
  11981. }
  11982. addOrSubtractDurationFromMoment(this, dur, 1);
  11983. return this;
  11984. },
  11985. subtract : function (input, val) {
  11986. var dur;
  11987. // switch args to support subtract('s', 1) and subtract(1, 's')
  11988. if (typeof input === 'string') {
  11989. dur = moment.duration(+val, input);
  11990. } else {
  11991. dur = moment.duration(input, val);
  11992. }
  11993. addOrSubtractDurationFromMoment(this, dur, -1);
  11994. return this;
  11995. },
  11996. diff : function (input, units, asFloat) {
  11997. var that = this._isUTC ? moment(input).utc() : moment(input).local(),
  11998. zoneDiff = (this.zone() - that.zone()) * 6e4,
  11999. diff, output;
  12000. if (units) {
  12001. // standardize on singular form
  12002. units = units.replace(/s$/, '');
  12003. }
  12004. if (units === 'year' || units === 'month') {
  12005. diff = (this.daysInMonth() + that.daysInMonth()) * 432e5; // 24 * 60 * 60 * 1000 / 2
  12006. output = ((this.year() - that.year()) * 12) + (this.month() - that.month());
  12007. output += ((this - moment(this).startOf('month')) - (that - moment(that).startOf('month'))) / diff;
  12008. if (units === 'year') {
  12009. output = output / 12;
  12010. }
  12011. } else {
  12012. diff = (this - that) - zoneDiff;
  12013. output = units === 'second' ? diff / 1e3 : // 1000
  12014. units === 'minute' ? diff / 6e4 : // 1000 * 60
  12015. units === 'hour' ? diff / 36e5 : // 1000 * 60 * 60
  12016. units === 'day' ? diff / 864e5 : // 1000 * 60 * 60 * 24
  12017. units === 'week' ? diff / 6048e5 : // 1000 * 60 * 60 * 24 * 7
  12018. diff;
  12019. }
  12020. return asFloat ? output : absRound(output);
  12021. },
  12022. from : function (time, withoutSuffix) {
  12023. return moment.duration(this.diff(time)).lang(this.lang()._abbr).humanize(!withoutSuffix);
  12024. },
  12025. fromNow : function (withoutSuffix) {
  12026. return this.from(moment(), withoutSuffix);
  12027. },
  12028. calendar : function () {
  12029. var diff = this.diff(moment().startOf('day'), 'days', true),
  12030. format = diff < -6 ? 'sameElse' :
  12031. diff < -1 ? 'lastWeek' :
  12032. diff < 0 ? 'lastDay' :
  12033. diff < 1 ? 'sameDay' :
  12034. diff < 2 ? 'nextDay' :
  12035. diff < 7 ? 'nextWeek' : 'sameElse';
  12036. return this.format(this.lang().calendar(format, this));
  12037. },
  12038. isLeapYear : function () {
  12039. var year = this.year();
  12040. return (year % 4 === 0 && year % 100 !== 0) || year % 400 === 0;
  12041. },
  12042. isDST : function () {
  12043. return (this.zone() < moment([this.year()]).zone() ||
  12044. this.zone() < moment([this.year(), 5]).zone());
  12045. },
  12046. day : function (input) {
  12047. var day = this._isUTC ? this._d.getUTCDay() : this._d.getDay();
  12048. return input == null ? day :
  12049. this.add({ d : input - day });
  12050. },
  12051. startOf: function (units) {
  12052. units = units.replace(/s$/, '');
  12053. // the following switch intentionally omits break keywords
  12054. // to utilize falling through the cases.
  12055. switch (units) {
  12056. case 'year':
  12057. this.month(0);
  12058. /* falls through */
  12059. case 'month':
  12060. this.date(1);
  12061. /* falls through */
  12062. case 'week':
  12063. case 'day':
  12064. this.hours(0);
  12065. /* falls through */
  12066. case 'hour':
  12067. this.minutes(0);
  12068. /* falls through */
  12069. case 'minute':
  12070. this.seconds(0);
  12071. /* falls through */
  12072. case 'second':
  12073. this.milliseconds(0);
  12074. /* falls through */
  12075. }
  12076. // weeks are a special case
  12077. if (units === 'week') {
  12078. this.day(0);
  12079. }
  12080. return this;
  12081. },
  12082. endOf: function (units) {
  12083. return this.startOf(units).add(units.replace(/s?$/, 's'), 1).subtract('ms', 1);
  12084. },
  12085. isAfter: function (input, units) {
  12086. units = typeof units !== 'undefined' ? units : 'millisecond';
  12087. return +this.clone().startOf(units) > +moment(input).startOf(units);
  12088. },
  12089. isBefore: function (input, units) {
  12090. units = typeof units !== 'undefined' ? units : 'millisecond';
  12091. return +this.clone().startOf(units) < +moment(input).startOf(units);
  12092. },
  12093. isSame: function (input, units) {
  12094. units = typeof units !== 'undefined' ? units : 'millisecond';
  12095. return +this.clone().startOf(units) === +moment(input).startOf(units);
  12096. },
  12097. zone : function () {
  12098. return this._isUTC ? 0 : this._d.getTimezoneOffset();
  12099. },
  12100. daysInMonth : function () {
  12101. return moment.utc([this.year(), this.month() + 1, 0]).date();
  12102. },
  12103. dayOfYear : function (input) {
  12104. var dayOfYear = round((moment(this).startOf('day') - moment(this).startOf('year')) / 864e5) + 1;
  12105. return input == null ? dayOfYear : this.add("d", (input - dayOfYear));
  12106. },
  12107. isoWeek : function (input) {
  12108. var week = weekOfYear(this, 1, 4);
  12109. return input == null ? week : this.add("d", (input - week) * 7);
  12110. },
  12111. week : function (input) {
  12112. var week = this.lang().week(this);
  12113. return input == null ? week : this.add("d", (input - week) * 7);
  12114. },
  12115. // If passed a language key, it will set the language for this
  12116. // instance. Otherwise, it will return the language configuration
  12117. // variables for this instance.
  12118. lang : function (key) {
  12119. if (key === undefined) {
  12120. return this._lang;
  12121. } else {
  12122. this._lang = getLangDefinition(key);
  12123. return this;
  12124. }
  12125. }
  12126. };
  12127. // helper for adding shortcuts
  12128. function makeGetterAndSetter(name, key) {
  12129. moment.fn[name] = moment.fn[name + 's'] = function (input) {
  12130. var utc = this._isUTC ? 'UTC' : '';
  12131. if (input != null) {
  12132. this._d['set' + utc + key](input);
  12133. return this;
  12134. } else {
  12135. return this._d['get' + utc + key]();
  12136. }
  12137. };
  12138. }
  12139. // loop through and add shortcuts (Month, Date, Hours, Minutes, Seconds, Milliseconds)
  12140. for (i = 0; i < proxyGettersAndSetters.length; i ++) {
  12141. makeGetterAndSetter(proxyGettersAndSetters[i].toLowerCase().replace(/s$/, ''), proxyGettersAndSetters[i]);
  12142. }
  12143. // add shortcut for year (uses different syntax than the getter/setter 'year' == 'FullYear')
  12144. makeGetterAndSetter('year', 'FullYear');
  12145. // add plural methods
  12146. moment.fn.days = moment.fn.day;
  12147. moment.fn.weeks = moment.fn.week;
  12148. moment.fn.isoWeeks = moment.fn.isoWeek;
  12149. /************************************
  12150. Duration Prototype
  12151. ************************************/
  12152. moment.duration.fn = Duration.prototype = {
  12153. weeks : function () {
  12154. return absRound(this.days() / 7);
  12155. },
  12156. valueOf : function () {
  12157. return this._milliseconds +
  12158. this._days * 864e5 +
  12159. this._months * 2592e6;
  12160. },
  12161. humanize : function (withSuffix) {
  12162. var difference = +this,
  12163. output = relativeTime(difference, !withSuffix, this.lang());
  12164. if (withSuffix) {
  12165. output = this.lang().pastFuture(difference, output);
  12166. }
  12167. return this.lang().postformat(output);
  12168. },
  12169. lang : moment.fn.lang
  12170. };
  12171. function makeDurationGetter(name) {
  12172. moment.duration.fn[name] = function () {
  12173. return this._data[name];
  12174. };
  12175. }
  12176. function makeDurationAsGetter(name, factor) {
  12177. moment.duration.fn['as' + name] = function () {
  12178. return +this / factor;
  12179. };
  12180. }
  12181. for (i in unitMillisecondFactors) {
  12182. if (unitMillisecondFactors.hasOwnProperty(i)) {
  12183. makeDurationAsGetter(i, unitMillisecondFactors[i]);
  12184. makeDurationGetter(i.toLowerCase());
  12185. }
  12186. }
  12187. makeDurationAsGetter('Weeks', 6048e5);
  12188. /************************************
  12189. Default Lang
  12190. ************************************/
  12191. // Set default language, other languages will inherit from English.
  12192. moment.lang('en', {
  12193. ordinal : function (number) {
  12194. var b = number % 10,
  12195. output = (~~ (number % 100 / 10) === 1) ? 'th' :
  12196. (b === 1) ? 'st' :
  12197. (b === 2) ? 'nd' :
  12198. (b === 3) ? 'rd' : 'th';
  12199. return number + output;
  12200. }
  12201. });
  12202. /************************************
  12203. Exposing Moment
  12204. ************************************/
  12205. // CommonJS module is defined
  12206. if (hasModule) {
  12207. module.exports = moment;
  12208. }
  12209. /*global ender:false */
  12210. if (typeof ender === 'undefined') {
  12211. // here, `this` means `window` in the browser, or `global` on the server
  12212. // add `moment` as a global object via a string identifier,
  12213. // for Closure Compiler "advanced" mode
  12214. this['moment'] = moment;
  12215. }
  12216. /*global define:false */
  12217. if (typeof define === "function" && define.amd) {
  12218. define("moment", [], function () {
  12219. return moment;
  12220. });
  12221. }
  12222. }).call(this);
  12223. })()
  12224. },{}]},{},[1])(1)
  12225. });
  12226. ;