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.

607 lines
17 KiB

9 years ago
9 years ago
  1. var util = require('../util');
  2. import ColorPicker from './ColorPicker'
  3. /**
  4. * The way this works is for all properties of this.possible options, you can supply the property name in any form to list the options.
  5. * Boolean options are recognised as Boolean
  6. * Number options should be written as array: [default value, min value, max value, stepsize]
  7. * Colors should be written as array: ['color', '#ffffff']
  8. * Strings with should be written as array: [option1, option2, option3, ..]
  9. *
  10. * The options are matched with their counterparts in each of the modules and the values used in the configuration are
  11. *
  12. * @param parentModule | the location where parentModule.setOptions() can be called
  13. * @param defaultContainer | the default container of the module
  14. * @param configureOptions | the fully configured and predefined options set found in allOptions.js
  15. * @param pixelRatio | canvas pixel ratio
  16. */
  17. class Configurator {
  18. constructor(parentModule, defaultContainer, configureOptions, pixelRatio = 1) {
  19. this.parent = parentModule;
  20. this.changedOptions = [];
  21. this.container = defaultContainer;
  22. this.allowCreation = false;
  23. this.options = {};
  24. this.defaultOptions = {
  25. enabled: false,
  26. filter: true,
  27. container: undefined,
  28. showButton: true
  29. };
  30. util.extend(this.options, this.defaultOptions);
  31. this.configureOptions = configureOptions;
  32. this.moduleOptions = {};
  33. this.domElements = [];
  34. this.colorPicker = new ColorPicker(pixelRatio);
  35. this.wrapper = undefined;
  36. }
  37. /**
  38. * refresh all options.
  39. * Because all modules parse their options by themselves, we just use their options. We copy them here.
  40. *
  41. * @param options
  42. */
  43. setOptions(options) {
  44. if (options !== undefined) {
  45. let enabled = true;
  46. if (typeof options === 'string') {
  47. this.options.filter = options;
  48. }
  49. else if (options instanceof Array) {
  50. this.options.filter = options.join();
  51. }
  52. else if (typeof options === 'object') {
  53. if (options.container !== undefined) {
  54. this.options.container = options.container;
  55. }
  56. if (options.filter !== undefined) {
  57. this.options.filter = options.filter;
  58. }
  59. if (options.showButton !== undefined) {
  60. this.options.showButton = options.showButton;
  61. }
  62. if (options.enabled !== undefined) {
  63. enabled = options.enabled;
  64. }
  65. }
  66. else if (typeof options === 'boolean') {
  67. this.options.filter = true;
  68. enabled = options;
  69. }
  70. else if (typeof options === 'function') {
  71. this.options.filter = options;
  72. enabled = true;
  73. }
  74. if (this.options.filter === false) {
  75. enabled = false;
  76. }
  77. this.options.enabled = enabled;
  78. }
  79. this._clean();
  80. }
  81. setModuleOptions(moduleOptions) {
  82. this.moduleOptions = moduleOptions;
  83. if (this.options.enabled === true) {
  84. this._clean();
  85. if (this.options.container !== undefined) {
  86. this.container = this.options.container;
  87. }
  88. this._create();
  89. }
  90. }
  91. /**
  92. * Create all DOM elements
  93. * @private
  94. */
  95. _create() {
  96. this._clean();
  97. this.changedOptions = [];
  98. let filter = this.options.filter;
  99. let counter = 0;
  100. let show = false;
  101. for (let option in this.configureOptions) {
  102. if (this.configureOptions.hasOwnProperty(option)) {
  103. this.allowCreation = false;
  104. show = false;
  105. if (typeof filter === 'function') {
  106. show = filter(option,[]);
  107. show = show || this._handleObject(this.configureOptions[option], [option], true);
  108. }
  109. else if (filter === true || filter.indexOf(option) !== -1) {
  110. show = true;
  111. }
  112. if (show !== false) {
  113. this.allowCreation = true;
  114. // linebreak between categories
  115. if (counter > 0) {
  116. this._makeItem([]);
  117. }
  118. // a header for the category
  119. this._makeHeader(option);
  120. // get the suboptions
  121. this._handleObject(this.configureOptions[option], [option]);
  122. }
  123. counter++;
  124. }
  125. }
  126. if (this.options.showButton === true) {
  127. let generateButton = document.createElement('div');
  128. generateButton.className = 'vis-network-configuration button';
  129. generateButton.innerHTML = 'generate options';
  130. generateButton.onclick = () => {this._printOptions();};
  131. generateButton.onmouseover = () => {generateButton.className = 'vis-network-configuration button hover';};
  132. generateButton.onmouseout = () => {generateButton.className = 'vis-network-configuration button';};
  133. this.optionsContainer = document.createElement('div');
  134. this.optionsContainer.className = 'vis-network-configuration vis-option-container';
  135. this.domElements.push(this.optionsContainer);
  136. this.domElements.push(generateButton);
  137. }
  138. this._push();
  139. this.colorPicker.insertTo(this.container);
  140. }
  141. /**
  142. * draw all DOM elements on the screen
  143. * @private
  144. */
  145. _push() {
  146. this.wrapper = document.createElement('div');
  147. this.wrapper.className = 'vis-network-configuration-wrapper';
  148. this.container.appendChild(this.wrapper);
  149. for (var i = 0; i < this.domElements.length; i++) {
  150. this.wrapper.appendChild(this.domElements[i]);
  151. }
  152. }
  153. /**
  154. * delete all DOM elements
  155. * @private
  156. */
  157. _clean() {
  158. for (var i = 0; i < this.domElements.length; i++) {
  159. this.wrapper.removeChild(this.domElements[i]);
  160. }
  161. if (this.wrapper !== undefined) {
  162. this.container.removeChild(this.wrapper);
  163. this.wrapper = undefined;
  164. }
  165. this.domElements = [];
  166. }
  167. /**
  168. * get the value from the actualOptions if it exists
  169. * @param {array} path | where to look for the actual option
  170. * @returns {*}
  171. * @private
  172. */
  173. _getValue(path) {
  174. let base = this.moduleOptions;
  175. for (let i = 0; i < path.length; i++) {
  176. if (base[path[i]] !== undefined) {
  177. base = base[path[i]];
  178. }
  179. else {
  180. base = undefined;
  181. break;
  182. }
  183. }
  184. return base;
  185. }
  186. /**
  187. * all option elements are wrapped in an item
  188. * @param path
  189. * @param domElements
  190. * @private
  191. */
  192. _makeItem(path, ...domElements) {
  193. if (this.allowCreation === true) {
  194. let item = document.createElement('div');
  195. item.className = 'vis-network-configuration item s' + path.length;
  196. domElements.forEach((element) => {
  197. item.appendChild(element);
  198. });
  199. this.domElements.push(item);
  200. }
  201. }
  202. /**
  203. * header for major subjects
  204. * @param name
  205. * @private
  206. */
  207. _makeHeader(name) {
  208. let div = document.createElement('div');
  209. div.className = 'vis-network-configuration header';
  210. div.innerHTML = name;
  211. this._makeItem([],div);
  212. }
  213. /**
  214. * make a label, if it is an object label, it gets different styling.
  215. * @param name
  216. * @param path
  217. * @param objectLabel
  218. * @returns {HTMLElement}
  219. * @private
  220. */
  221. _makeLabel(name, path, objectLabel = false) {
  222. let div = document.createElement('div');
  223. div.className = 'vis-network-configuration label s' + path.length;
  224. if (objectLabel === true) {
  225. div.innerHTML = '<i><b>' + name + ':</b></i>';
  226. }
  227. else {
  228. div.innerHTML = name + ':';
  229. }
  230. return div;
  231. }
  232. /**
  233. * make a dropdown list for multiple possible string optoins
  234. * @param arr
  235. * @param value
  236. * @param path
  237. * @private
  238. */
  239. _makeDropdown(arr, value, path) {
  240. let select = document.createElement('select');
  241. select.className = 'vis-network-configuration select';
  242. let selectedValue = 0;
  243. if (value !== undefined) {
  244. if (arr.indexOf(value) !== -1) {
  245. selectedValue = arr.indexOf(value);
  246. }
  247. }
  248. for (let i = 0; i < arr.length; i++) {
  249. let option = document.createElement('option');
  250. option.value = arr[i];
  251. if (i === selectedValue) {
  252. option.selected = 'selected';
  253. }
  254. option.innerHTML = arr[i];
  255. select.appendChild(option);
  256. }
  257. let me = this;
  258. select.onchange = function () {me._update(this.value, path);};
  259. let label = this._makeLabel(path[path.length-1], path);
  260. this._makeItem(path, label, select);
  261. }
  262. /**
  263. * make a range object for numeric options
  264. * @param arr
  265. * @param value
  266. * @param path
  267. * @private
  268. */
  269. _makeRange(arr, value, path) {
  270. let defaultValue = arr[0];
  271. let min = arr[1];
  272. let max = arr[2];
  273. let step = arr[3];
  274. let range = document.createElement('input');
  275. range.type = 'range';
  276. range.className = 'vis-network-configuration range';
  277. range.min = min;
  278. range.max = max;
  279. range.step = step;
  280. if (value !== undefined) {
  281. if (value < 0 && value * 2 < min) {
  282. range.min = value*2;
  283. }
  284. else if (value * 0.1 < min) {
  285. range.min = value / 10;
  286. }
  287. if (value * 2 > max && max !== 1) {
  288. range.max = value * 2;
  289. }
  290. range.value = value;
  291. }
  292. else {
  293. range.value = defaultValue;
  294. }
  295. let input = document.createElement('input');
  296. input.className = 'vis-network-configuration rangeinput';
  297. input.value = range.value;
  298. var me = this;
  299. range.onchange = function () {input.value = this.value; me._update(Number(this.value), path);};
  300. range.oninput = function () {input.value = this.value; };
  301. let label = this._makeLabel(path[path.length-1], path);
  302. this._makeItem(path, label, range, input);
  303. }
  304. /**
  305. * make a checkbox for boolean options.
  306. * @param defaultValue
  307. * @param value
  308. * @param path
  309. * @private
  310. */
  311. _makeCheckbox(defaultValue, value, path) {
  312. var checkbox = document.createElement('input');
  313. checkbox.type = 'checkbox';
  314. checkbox.className = 'vis-network-configuration checkbox';
  315. checkbox.checked = defaultValue;
  316. if (value !== undefined) {
  317. checkbox.checked = value;
  318. if (value !== defaultValue) {
  319. if (typeof defaultValue === 'object') {
  320. if (value !== defaultValue.enabled) {
  321. this.changedOptions.push({path:path, value:value});
  322. }
  323. }
  324. else {
  325. this.changedOptions.push({path:path, value:value});
  326. }
  327. }
  328. }
  329. let me = this;
  330. checkbox.onchange = function() {me._update(this.checked, path)};
  331. let label = this._makeLabel(path[path.length-1], path);
  332. this._makeItem(path, label, checkbox);
  333. }
  334. /**
  335. * make a text input field for string options.
  336. * @param defaultValue
  337. * @param value
  338. * @param path
  339. * @private
  340. */
  341. _makeTextInput(defaultValue, value, path) {
  342. var checkbox = document.createElement('input');
  343. checkbox.type = 'text';
  344. checkbox.className = 'vis-network-configuration text';
  345. checkbox.value = value;
  346. if (value !== defaultValue) {
  347. this.changedOptions.push({path:path, value:value});
  348. }
  349. let me = this;
  350. checkbox.onchange = function() {me._update(this.value, path)};
  351. let label = this._makeLabel(path[path.length-1], path);
  352. this._makeItem(path, label, checkbox);
  353. }
  354. /**
  355. * make a color field with a color picker for color fields
  356. * @param arr
  357. * @param value
  358. * @param path
  359. * @private
  360. */
  361. _makeColorField(arr, value, path) {
  362. let defaultColor = arr[1];
  363. let div = document.createElement('div');
  364. value = value === undefined ? defaultColor : value;
  365. if (value !== 'none') {
  366. div.className = 'vis-network-configuration colorBlock';
  367. div.style.backgroundColor = value;
  368. }
  369. else {
  370. div.className = 'vis-network-configuration colorBlock none';
  371. }
  372. value = value === undefined ? defaultColor : value;
  373. div.onclick = () => {
  374. this._showColorPicker(value,div,path);
  375. };
  376. let label = this._makeLabel(path[path.length-1], path);
  377. this._makeItem(path,label, div);
  378. }
  379. /**
  380. * used by the color buttons to call the color picker.
  381. * @param event
  382. * @param value
  383. * @param div
  384. * @param path
  385. * @private
  386. */
  387. _showColorPicker(value, div, path) {
  388. let rect = div.getBoundingClientRect();
  389. let bodyRect = document.body.getBoundingClientRect();
  390. let pickerX = rect.left + rect.width + 5;
  391. let pickerY = rect.top - bodyRect.top + rect.height*0.5;
  392. this.colorPicker.show(pickerX,pickerY);
  393. this.colorPicker.setColor(value);
  394. this.colorPicker.setCallback((color) => {
  395. let colorString = 'rgba(' + color.r + ',' + color.g + ',' + color.b + ',' + color.a + ')';
  396. div.style.backgroundColor = colorString;
  397. this._update(colorString,path);
  398. })
  399. }
  400. /**
  401. * parse an object and draw the correct items
  402. * @param obj
  403. * @param path
  404. * @private
  405. */
  406. _handleObject(obj, path = [], checkOnly = false) {
  407. let show = false;
  408. let filter = this.options.filter;
  409. let visibleInSet = false;
  410. for (let subObj in obj) {
  411. if (obj.hasOwnProperty(subObj)) {
  412. show = true;
  413. let item = obj[subObj];
  414. let newPath = util.copyAndExtendArray(path, subObj);
  415. if (typeof filter === 'function') {
  416. show = filter(subObj,path);
  417. // if needed we must go deeper into the object.
  418. if (show === false) {
  419. if (!(item instanceof Array) && typeof item !== 'string' && typeof item !== 'boolean' && item instanceof Object) {
  420. this.allowCreation = false;
  421. show = this._handleObject(item, newPath, true);
  422. this.allowCreation = checkOnly === false;
  423. }
  424. }
  425. }
  426. if (show !== false) {
  427. visibleInSet = true;
  428. let value = this._getValue(newPath);
  429. if (item instanceof Array) {
  430. this._handleArray(item, value, newPath);
  431. }
  432. else if (typeof item === 'string') {
  433. this._makeTextInput(item, value, newPath);
  434. }
  435. else if (typeof item === 'boolean') {
  436. this._makeCheckbox(item, value, newPath);
  437. }
  438. else if (item instanceof Object) {
  439. // collapse the physics options that are not enabled
  440. let draw = true;
  441. if (path.indexOf('physics') !== -1) {
  442. if (this.moduleOptions.physics.solver !== subObj) {
  443. draw = false;
  444. }
  445. }
  446. if (draw === true) {
  447. // initially collapse options with an disabled enabled option.
  448. if (item.enabled !== undefined) {
  449. let enabledPath = util.copyAndExtendArray(newPath, 'enabled');
  450. let enabledValue = this._getValue(enabledPath);
  451. if (enabledValue === true) {
  452. let label = this._makeLabel(subObj, newPath, true);
  453. this._makeItem(newPath, label);
  454. visibleInSet = this._handleObject(item, newPath) || visibleInSet;
  455. }
  456. else {
  457. this._makeCheckbox(item, enabledValue, newPath);
  458. }
  459. }
  460. else {
  461. let label = this._makeLabel(subObj, newPath, true);
  462. this._makeItem(newPath, label);
  463. visibleInSet = this._handleObject(item, newPath) || visibleInSet;
  464. }
  465. }
  466. }
  467. else {
  468. console.error('dont know how to handle', item, subObj, newPath);
  469. }
  470. }
  471. }
  472. }
  473. return visibleInSet;
  474. }
  475. /**
  476. * handle the array type of option
  477. * @param optionName
  478. * @param arr
  479. * @param value
  480. * @param path
  481. * @private
  482. */
  483. _handleArray(arr, value, path) {
  484. if (typeof arr[0] === 'string' && arr[0] === 'color') {
  485. this._makeColorField(arr, value, path);
  486. if (arr[1] !== value) {this.changedOptions.push({path:path, value:value});}
  487. }
  488. else if (typeof arr[0] === 'string') {
  489. this._makeDropdown(arr, value, path);
  490. if (arr[0] !== value) {this.changedOptions.push({path:path, value:value});}
  491. }
  492. else if (typeof arr[0] === 'number') {
  493. this._makeRange(arr, value, path);
  494. if (arr[0] !== value) {this.changedOptions.push({path:path, value:Number(value)});}
  495. }
  496. }
  497. /**
  498. * called to update the network with the new settings.
  499. * @param value
  500. * @param path
  501. * @private
  502. */
  503. _update(value, path) {
  504. let options = this._constructOptions(value,path);
  505. this.parent.setOptions(options);
  506. }
  507. _constructOptions(value, path, optionsObj = {}) {
  508. let pointer = optionsObj;
  509. // when dropdown boxes can be string or boolean, we typecast it into correct types
  510. value = value === 'true' ? true : value;
  511. value = value === 'false' ? false : value;
  512. for (let i = 0; i < path.length; i++) {
  513. if (path[i] !== 'global') {
  514. if (pointer[path[i]] === undefined) {
  515. pointer[path[i]] = {};
  516. }
  517. if (i !== path.length - 1) {
  518. pointer = pointer[path[i]];
  519. }
  520. else {
  521. pointer[path[i]] = value;
  522. }
  523. }
  524. }
  525. return optionsObj;
  526. }
  527. _printOptions() {
  528. let options = {};
  529. for (var i = 0; i < this.changedOptions.length; i++) {
  530. this._constructOptions(this.changedOptions[i].value, this.changedOptions[i].path, options)
  531. }
  532. this.optionsContainer.innerHTML = '<pre>var options = ' + JSON.stringify(options, null, 2) + '</pre>';
  533. }
  534. }
  535. export default Configurator;