ui.js 19 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291
  1. class UIElement {
  2. constructor( dom ) {
  3. this.dom = dom;
  4. }
  5. add() {
  6. for ( let i = 0; i < arguments.length; i ++ ) {
  7. const argument = arguments[ i ];
  8. if ( argument instanceof UIElement ) {
  9. this.dom.appendChild( argument.dom );
  10. } else {
  11. console.error( 'UIElement:', argument, 'is not an instance of UIElement.' );
  12. }
  13. }
  14. return this;
  15. }
  16. remove() {
  17. for ( let i = 0; i < arguments.length; i ++ ) {
  18. const argument = arguments[ i ];
  19. if ( argument instanceof UIElement ) {
  20. this.dom.removeChild( argument.dom );
  21. } else {
  22. console.error( 'UIElement:', argument, 'is not an instance of UIElement.' );
  23. }
  24. }
  25. return this;
  26. }
  27. clear() {
  28. while ( this.dom.children.length ) {
  29. this.dom.removeChild( this.dom.lastChild );
  30. }
  31. }
  32. setId( id ) {
  33. this.dom.id = id;
  34. return this;
  35. }
  36. getId() {
  37. return this.dom.id;
  38. }
  39. setClass( name ) {
  40. this.dom.className = name;
  41. return this;
  42. }
  43. addClass( name ) {
  44. this.dom.classList.add( name );
  45. return this;
  46. }
  47. removeClass( name ) {
  48. this.dom.classList.remove( name );
  49. return this;
  50. }
  51. setStyle( style, array ) {
  52. for ( let i = 0; i < array.length; i ++ ) {
  53. this.dom.style[ style ] = array[ i ];
  54. }
  55. return this;
  56. }
  57. setDisabled( value ) {
  58. this.dom.disabled = value;
  59. return this;
  60. }
  61. setTextContent( value ) {
  62. this.dom.textContent = value;
  63. return this;
  64. }
  65. setInnerHTML( value ) {
  66. this.dom.innerHTML = value;
  67. }
  68. getIndexOfChild( element ) {
  69. return Array.prototype.indexOf.call( this.dom.children, element.dom );
  70. }
  71. }
  72. // properties
  73. const properties = [ 'position', 'left', 'top', 'right', 'bottom', 'width', 'height',
  74. 'display', 'verticalAlign', 'overflow', 'color', 'background', 'backgroundColor', 'opacity',
  75. 'border', 'borderLeft', 'borderTop', 'borderRight', 'borderBottom', 'borderColor',
  76. 'margin', 'marginLeft', 'marginTop', 'marginRight', 'marginBottom',
  77. 'padding', 'paddingLeft', 'paddingTop', 'paddingRight', 'paddingBottom',
  78. 'fontSize', 'fontWeight', 'textAlign', 'textDecoration', 'textTransform', 'cursor', 'zIndex' ];
  79. properties.forEach( function ( property ) {
  80. const method = 'set' + property.substr( 0, 1 ).toUpperCase() + property.substr( 1, property.length );
  81. UIElement.prototype[ method ] = function () {
  82. this.setStyle( property, arguments );
  83. return this;
  84. };
  85. } );
  86. // events
  87. const events = [ 'KeyUp', 'KeyDown', 'MouseOver', 'MouseOut', 'Click', 'DblClick', 'Change', 'Input' ];
  88. events.forEach( function ( event ) {
  89. const method = 'on' + event;
  90. UIElement.prototype[ method ] = function ( callback ) {
  91. this.dom.addEventListener( event.toLowerCase(), callback.bind( this ) );
  92. return this;
  93. };
  94. } );
  95. class UISpan extends UIElement {
  96. constructor() {
  97. super( document.createElement( 'span' ) );
  98. }
  99. }
  100. class UIDiv extends UIElement {
  101. constructor() {
  102. super( document.createElement( 'div' ) );
  103. }
  104. }
  105. class UIRow extends UIDiv {
  106. constructor() {
  107. super();
  108. this.dom.className = 'Row';
  109. }
  110. }
  111. class UIPanel extends UIDiv {
  112. constructor() {
  113. super();
  114. this.dom.className = 'Panel';
  115. }
  116. }
  117. class UIText extends UISpan {
  118. constructor( text ) {
  119. super();
  120. this.dom.className = 'Text';
  121. this.dom.style.cursor = 'default';
  122. this.dom.style.display = 'inline-block';
  123. this.setValue( text );
  124. }
  125. getValue() {
  126. return this.dom.textContent;
  127. }
  128. setValue( value ) {
  129. if ( value !== undefined ) {
  130. this.dom.textContent = value;
  131. }
  132. return this;
  133. }
  134. }
  135. class UIInput extends UIElement {
  136. constructor( text ) {
  137. super( document.createElement( 'input' ) );
  138. this.dom.className = 'Input';
  139. this.dom.style.padding = '2px';
  140. this.dom.style.border = '1px solid transparent';
  141. this.dom.setAttribute( 'autocomplete', 'off' );
  142. this.dom.addEventListener( 'keydown', function ( event ) {
  143. event.stopPropagation();
  144. } );
  145. this.setValue( text );
  146. }
  147. getValue() {
  148. return this.dom.value;
  149. }
  150. setValue( value ) {
  151. this.dom.value = value;
  152. return this;
  153. }
  154. }
  155. class UITextArea extends UIElement {
  156. constructor() {
  157. super( document.createElement( 'textarea' ) );
  158. this.dom.className = 'TextArea';
  159. this.dom.style.padding = '2px';
  160. this.dom.spellcheck = false;
  161. this.dom.setAttribute( 'autocomplete', 'off' );
  162. this.dom.addEventListener( 'keydown', function ( event ) {
  163. event.stopPropagation();
  164. if ( event.keyCode === 9 ) {
  165. event.preventDefault();
  166. const cursor = this.selectionStart;
  167. this.value = this.value.substring( 0, cursor ) + '\t' + this.value.substring( cursor );
  168. this.selectionStart = cursor + 1;
  169. this.selectionEnd = this.selectionStart;
  170. }
  171. } );
  172. }
  173. getValue() {
  174. return this.dom.value;
  175. }
  176. setValue( value ) {
  177. this.dom.value = value;
  178. return this;
  179. }
  180. }
  181. class UISelect extends UIElement {
  182. constructor() {
  183. super( document.createElement( 'select' ) );
  184. this.dom.className = 'Select';
  185. this.dom.style.padding = '2px';
  186. this.dom.setAttribute( 'autocomplete', 'off' );
  187. this.dom.addEventListener( 'pointerdown', function ( event ) {
  188. event.stopPropagation();
  189. } );
  190. }
  191. setMultiple( boolean ) {
  192. this.dom.multiple = boolean;
  193. return this;
  194. }
  195. setOptions( options ) {
  196. const selected = this.dom.value;
  197. while ( this.dom.children.length > 0 ) {
  198. this.dom.removeChild( this.dom.firstChild );
  199. }
  200. for ( const key in options ) {
  201. const option = document.createElement( 'option' );
  202. option.value = key;
  203. option.innerHTML = options[ key ];
  204. this.dom.appendChild( option );
  205. }
  206. this.dom.value = selected;
  207. return this;
  208. }
  209. getValue() {
  210. return this.dom.value;
  211. }
  212. setValue( value ) {
  213. value = String( value );
  214. if ( this.dom.value !== value ) {
  215. this.dom.value = value;
  216. }
  217. return this;
  218. }
  219. }
  220. class UICheckbox extends UIElement {
  221. constructor( boolean ) {
  222. super( document.createElement( 'input' ) );
  223. this.dom.className = 'Checkbox';
  224. this.dom.type = 'checkbox';
  225. this.setValue( boolean );
  226. }
  227. getValue() {
  228. return this.dom.checked;
  229. }
  230. setValue( value ) {
  231. if ( value !== undefined ) {
  232. this.dom.checked = value;
  233. }
  234. return this;
  235. }
  236. }
  237. class UIColor extends UIElement {
  238. constructor() {
  239. super( document.createElement( 'input' ) );
  240. this.dom.className = 'Color';
  241. this.dom.style.width = '32px';
  242. this.dom.style.height = '16px';
  243. this.dom.style.border = '0px';
  244. this.dom.style.padding = '2px';
  245. this.dom.style.backgroundColor = 'transparent';
  246. this.dom.setAttribute( 'autocomplete', 'off' );
  247. try {
  248. this.dom.type = 'color';
  249. this.dom.value = '#ffffff';
  250. } catch ( exception ) {}
  251. }
  252. getValue() {
  253. return this.dom.value;
  254. }
  255. getHexValue() {
  256. return parseInt( this.dom.value.substr( 1 ), 16 );
  257. }
  258. setValue( value ) {
  259. this.dom.value = value;
  260. return this;
  261. }
  262. setHexValue( hex ) {
  263. this.dom.value = '#' + ( '000000' + hex.toString( 16 ) ).slice( - 6 );
  264. return this;
  265. }
  266. }
  267. class UINumber extends UIElement {
  268. constructor( number ) {
  269. super( document.createElement( 'input' ) );
  270. this.dom.style.cursor = 'ns-resize';
  271. this.dom.className = 'Number';
  272. this.dom.value = '0.00';
  273. this.dom.setAttribute( 'autocomplete', 'off' );
  274. this.value = 0;
  275. this.min = - Infinity;
  276. this.max = Infinity;
  277. this.precision = 2;
  278. this.step = 1;
  279. this.unit = '';
  280. this.nudge = 0.01;
  281. this.setValue( number );
  282. const scope = this;
  283. const changeEvent = document.createEvent( 'HTMLEvents' );
  284. changeEvent.initEvent( 'change', true, true );
  285. let distance = 0;
  286. let onMouseDownValue = 0;
  287. const pointer = { x: 0, y: 0 };
  288. const prevPointer = { x: 0, y: 0 };
  289. function onMouseDown( event ) {
  290. event.preventDefault();
  291. distance = 0;
  292. onMouseDownValue = scope.value;
  293. prevPointer.x = event.clientX;
  294. prevPointer.y = event.clientY;
  295. document.addEventListener( 'mousemove', onMouseMove );
  296. document.addEventListener( 'mouseup', onMouseUp );
  297. }
  298. function onMouseMove( event ) {
  299. const currentValue = scope.value;
  300. pointer.x = event.clientX;
  301. pointer.y = event.clientY;
  302. distance += ( pointer.x - prevPointer.x ) - ( pointer.y - prevPointer.y );
  303. let value = onMouseDownValue + ( distance / ( event.shiftKey ? 5 : 50 ) ) * scope.step;
  304. value = Math.min( scope.max, Math.max( scope.min, value ) );
  305. if ( currentValue !== value ) {
  306. scope.setValue( value );
  307. scope.dom.dispatchEvent( changeEvent );
  308. }
  309. prevPointer.x = event.clientX;
  310. prevPointer.y = event.clientY;
  311. }
  312. function onMouseUp() {
  313. document.removeEventListener( 'mousemove', onMouseMove );
  314. document.removeEventListener( 'mouseup', onMouseUp );
  315. if ( Math.abs( distance ) < 2 ) {
  316. scope.dom.focus();
  317. scope.dom.select();
  318. }
  319. }
  320. function onTouchStart( event ) {
  321. if ( event.touches.length === 1 ) {
  322. distance = 0;
  323. onMouseDownValue = scope.value;
  324. prevPointer.x = event.touches[ 0 ].pageX;
  325. prevPointer.y = event.touches[ 0 ].pageY;
  326. document.addEventListener( 'touchmove', onTouchMove );
  327. document.addEventListener( 'touchend', onTouchEnd );
  328. }
  329. }
  330. function onTouchMove( event ) {
  331. const currentValue = scope.value;
  332. pointer.x = event.touches[ 0 ].pageX;
  333. pointer.y = event.touches[ 0 ].pageY;
  334. distance += ( pointer.x - prevPointer.x ) - ( pointer.y - prevPointer.y );
  335. let value = onMouseDownValue + ( distance / ( event.shiftKey ? 5 : 50 ) ) * scope.step;
  336. value = Math.min( scope.max, Math.max( scope.min, value ) );
  337. if ( currentValue !== value ) {
  338. scope.setValue( value );
  339. scope.dom.dispatchEvent( changeEvent );
  340. }
  341. prevPointer.x = event.touches[ 0 ].pageX;
  342. prevPointer.y = event.touches[ 0 ].pageY;
  343. }
  344. function onTouchEnd( event ) {
  345. if ( event.touches.length === 0 ) {
  346. document.removeEventListener( 'touchmove', onTouchMove );
  347. document.removeEventListener( 'touchend', onTouchEnd );
  348. }
  349. }
  350. function onChange() {
  351. scope.setValue( scope.dom.value );
  352. }
  353. function onFocus() {
  354. scope.dom.style.backgroundColor = '';
  355. scope.dom.style.cursor = '';
  356. }
  357. function onBlur() {
  358. scope.dom.style.backgroundColor = 'transparent';
  359. scope.dom.style.cursor = 'ns-resize';
  360. }
  361. function onKeyDown( event ) {
  362. event.stopPropagation();
  363. switch ( event.keyCode ) {
  364. case 13: // enter
  365. scope.dom.blur();
  366. break;
  367. case 38: // up
  368. event.preventDefault();
  369. scope.setValue( scope.getValue() + scope.nudge );
  370. scope.dom.dispatchEvent( changeEvent );
  371. break;
  372. case 40: // down
  373. event.preventDefault();
  374. scope.setValue( scope.getValue() - scope.nudge );
  375. scope.dom.dispatchEvent( changeEvent );
  376. break;
  377. }
  378. }
  379. onBlur();
  380. this.dom.addEventListener( 'keydown', onKeyDown );
  381. this.dom.addEventListener( 'mousedown', onMouseDown );
  382. this.dom.addEventListener( 'touchstart', onTouchStart, { passive: false } );
  383. this.dom.addEventListener( 'change', onChange );
  384. this.dom.addEventListener( 'focus', onFocus );
  385. this.dom.addEventListener( 'blur', onBlur );
  386. }
  387. getValue() {
  388. return this.value;
  389. }
  390. setValue( value ) {
  391. if ( value !== undefined ) {
  392. value = parseFloat( value );
  393. if ( value < this.min ) value = this.min;
  394. if ( value > this.max ) value = this.max;
  395. this.value = value;
  396. this.dom.value = value.toFixed( this.precision );
  397. if ( this.unit !== '' ) this.dom.value += ' ' + this.unit;
  398. }
  399. return this;
  400. }
  401. setPrecision( precision ) {
  402. this.precision = precision;
  403. return this;
  404. }
  405. setStep( step ) {
  406. this.step = step;
  407. return this;
  408. }
  409. setNudge( nudge ) {
  410. this.nudge = nudge;
  411. return this;
  412. }
  413. setRange( min, max ) {
  414. this.min = min;
  415. this.max = max;
  416. return this;
  417. }
  418. setUnit( unit ) {
  419. this.unit = unit;
  420. return this;
  421. }
  422. }
  423. class UIInteger extends UIElement {
  424. constructor( number ) {
  425. super( document.createElement( 'input' ) );
  426. this.dom.style.cursor = 'ns-resize';
  427. this.dom.className = 'Number';
  428. this.dom.value = '0';
  429. this.dom.setAttribute( 'autocomplete', 'off' );
  430. this.value = 0;
  431. this.min = - Infinity;
  432. this.max = Infinity;
  433. this.step = 1;
  434. this.nudge = 1;
  435. this.setValue( number );
  436. const scope = this;
  437. const changeEvent = document.createEvent( 'HTMLEvents' );
  438. changeEvent.initEvent( 'change', true, true );
  439. let distance = 0;
  440. let onMouseDownValue = 0;
  441. const pointer = { x: 0, y: 0 };
  442. const prevPointer = { x: 0, y: 0 };
  443. function onMouseDown( event ) {
  444. event.preventDefault();
  445. distance = 0;
  446. onMouseDownValue = scope.value;
  447. prevPointer.x = event.clientX;
  448. prevPointer.y = event.clientY;
  449. document.addEventListener( 'mousemove', onMouseMove );
  450. document.addEventListener( 'mouseup', onMouseUp );
  451. }
  452. function onMouseMove( event ) {
  453. const currentValue = scope.value;
  454. pointer.x = event.clientX;
  455. pointer.y = event.clientY;
  456. distance += ( pointer.x - prevPointer.x ) - ( pointer.y - prevPointer.y );
  457. let value = onMouseDownValue + ( distance / ( event.shiftKey ? 5 : 50 ) ) * scope.step;
  458. value = Math.min( scope.max, Math.max( scope.min, value ) ) | 0;
  459. if ( currentValue !== value ) {
  460. scope.setValue( value );
  461. scope.dom.dispatchEvent( changeEvent );
  462. }
  463. prevPointer.x = event.clientX;
  464. prevPointer.y = event.clientY;
  465. }
  466. function onMouseUp() {
  467. document.removeEventListener( 'mousemove', onMouseMove );
  468. document.removeEventListener( 'mouseup', onMouseUp );
  469. if ( Math.abs( distance ) < 2 ) {
  470. scope.dom.focus();
  471. scope.dom.select();
  472. }
  473. }
  474. function onChange() {
  475. scope.setValue( scope.dom.value );
  476. }
  477. function onFocus() {
  478. scope.dom.style.backgroundColor = '';
  479. scope.dom.style.cursor = '';
  480. }
  481. function onBlur() {
  482. scope.dom.style.backgroundColor = 'transparent';
  483. scope.dom.style.cursor = 'ns-resize';
  484. }
  485. function onKeyDown( event ) {
  486. event.stopPropagation();
  487. switch ( event.keyCode ) {
  488. case 13: // enter
  489. scope.dom.blur();
  490. break;
  491. case 38: // up
  492. event.preventDefault();
  493. scope.setValue( scope.getValue() + scope.nudge );
  494. scope.dom.dispatchEvent( changeEvent );
  495. break;
  496. case 40: // down
  497. event.preventDefault();
  498. scope.setValue( scope.getValue() - scope.nudge );
  499. scope.dom.dispatchEvent( changeEvent );
  500. break;
  501. }
  502. }
  503. onBlur();
  504. this.dom.addEventListener( 'keydown', onKeyDown );
  505. this.dom.addEventListener( 'mousedown', onMouseDown );
  506. this.dom.addEventListener( 'change', onChange );
  507. this.dom.addEventListener( 'focus', onFocus );
  508. this.dom.addEventListener( 'blur', onBlur );
  509. }
  510. getValue() {
  511. return this.value;
  512. }
  513. setValue( value ) {
  514. if ( value !== undefined ) {
  515. value = parseInt( value );
  516. this.value = value;
  517. this.dom.value = value;
  518. }
  519. return this;
  520. }
  521. setStep( step ) {
  522. this.step = parseInt( step );
  523. return this;
  524. }
  525. setNudge( nudge ) {
  526. this.nudge = nudge;
  527. return this;
  528. }
  529. setRange( min, max ) {
  530. this.min = min;
  531. this.max = max;
  532. return this;
  533. }
  534. }
  535. class UIBreak extends UIElement {
  536. constructor() {
  537. super( document.createElement( 'br' ) );
  538. this.dom.className = 'Break';
  539. }
  540. }
  541. class UIHorizontalRule extends UIElement {
  542. constructor() {
  543. super( document.createElement( 'hr' ) );
  544. this.dom.className = 'HorizontalRule';
  545. }
  546. }
  547. class UIButton extends UIElement {
  548. constructor( value ) {
  549. super( document.createElement( 'button' ) );
  550. this.dom.className = 'Button';
  551. this.dom.textContent = value;
  552. }
  553. }
  554. class UIProgress extends UIElement {
  555. constructor( value ) {
  556. super( document.createElement( 'progress' ) );
  557. this.dom.value = value;
  558. }
  559. setValue( value ) {
  560. this.dom.value = value;
  561. }
  562. }
  563. class UITabbedPanel extends UIDiv {
  564. constructor() {
  565. super();
  566. this.dom.className = 'TabbedPanel';
  567. this.tabs = [];
  568. this.panels = [];
  569. this.tabsDiv = new UIDiv();
  570. this.tabsDiv.setClass( 'Tabs' );
  571. this.panelsDiv = new UIDiv();
  572. this.panelsDiv.setClass( 'Panels' );
  573. this.add( this.tabsDiv );
  574. this.add( this.panelsDiv );
  575. this.selected = '';
  576. }
  577. select( id ) {
  578. let tab;
  579. let panel;
  580. const scope = this;
  581. // Deselect current selection
  582. if ( this.selected && this.selected.length ) {
  583. tab = this.tabs.find( function ( item ) {
  584. return item.dom.id === scope.selected;
  585. } );
  586. panel = this.panels.find( function ( item ) {
  587. return item.dom.id === scope.selected;
  588. } );
  589. if ( tab ) {
  590. tab.removeClass( 'selected' );
  591. }
  592. if ( panel ) {
  593. panel.setDisplay( 'none' );
  594. }
  595. }
  596. tab = this.tabs.find( function ( item ) {
  597. return item.dom.id === id;
  598. } );
  599. panel = this.panels.find( function ( item ) {
  600. return item.dom.id === id;
  601. } );
  602. if ( tab ) {
  603. tab.addClass( 'selected' );
  604. }
  605. if ( panel ) {
  606. panel.setDisplay( '' );
  607. }
  608. this.selected = id;
  609. return this;
  610. }
  611. addTab( id, label, items ) {
  612. const tab = new UITab( label, this );
  613. tab.setId( id );
  614. this.tabs.push( tab );
  615. this.tabsDiv.add( tab );
  616. const panel = new UIDiv();
  617. panel.setId( id );
  618. panel.add( items );
  619. panel.setDisplay( 'none' );
  620. this.panels.push( panel );
  621. this.panelsDiv.add( panel );
  622. this.select( id );
  623. }
  624. }
  625. class UITab extends UIText {
  626. constructor( text, parent ) {
  627. super( text );
  628. this.dom.className = 'Tab';
  629. this.parent = parent;
  630. const scope = this;
  631. this.dom.addEventListener( 'click', function () {
  632. scope.parent.select( scope.dom.id );
  633. } );
  634. }
  635. }
  636. class UIListbox extends UIDiv {
  637. constructor() {
  638. super();
  639. this.dom.className = 'Listbox';
  640. this.dom.tabIndex = 0;
  641. this.items = [];
  642. this.listitems = [];
  643. this.selectedIndex = 0;
  644. this.selectedValue = null;
  645. }
  646. setItems( items ) {
  647. if ( Array.isArray( items ) ) {
  648. this.items = items;
  649. }
  650. this.render();
  651. }
  652. render( ) {
  653. while ( this.listitems.length ) {
  654. const item = this.listitems[ 0 ];
  655. item.dom.remove();
  656. this.listitems.splice( 0, 1 );
  657. }
  658. for ( let i = 0; i < this.items.length; i ++ ) {
  659. const item = this.items[ i ];
  660. const listitem = new ListboxItem( this );
  661. listitem.setId( item.id || `Listbox-${i}` );
  662. listitem.setTextContent( item.name || item.type );
  663. this.add( listitem );
  664. }
  665. }
  666. add() {
  667. const items = Array.from( arguments );
  668. this.listitems = this.listitems.concat( items );
  669. UIElement.prototype.add.apply( this, items );
  670. }
  671. selectIndex( index ) {
  672. if ( index >= 0 && index < this.items.length ) {
  673. this.setValue( this.listitems[ index ].getId() );
  674. }
  675. this.selectedIndex = index;
  676. }
  677. getValue() {
  678. return this.selectedValue;
  679. }
  680. setValue( value ) {
  681. for ( let i = 0; i < this.listitems.length; i ++ ) {
  682. const element = this.listitems[ i ];
  683. if ( element.getId() === value ) {
  684. element.addClass( 'active' );
  685. } else {
  686. element.removeClass( 'active' );
  687. }
  688. }
  689. this.selectedValue = value;
  690. const changeEvent = document.createEvent( 'HTMLEvents' );
  691. changeEvent.initEvent( 'change', true, true );
  692. this.dom.dispatchEvent( changeEvent );
  693. }
  694. }
  695. class ListboxItem extends UIDiv {
  696. constructor( parent ) {
  697. super();
  698. this.dom.className = 'ListboxItem';
  699. this.parent = parent;
  700. const scope = this;
  701. function onClick() {
  702. if ( scope.parent ) {
  703. scope.parent.setValue( scope.getId( ) );
  704. }
  705. }
  706. this.dom.addEventListener( 'click', onClick );
  707. }
  708. }
  709. export { UIElement, UISpan, UIDiv, UIRow, UIPanel, UIText, UIInput, UITextArea, UISelect, UICheckbox, UIColor, UINumber, UIInteger, UIBreak, UIHorizontalRule, UIButton, UIProgress, UITabbedPanel, UIListbox, ListboxItem };