ui.js 19 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285
  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. }
  188. setMultiple( boolean ) {
  189. this.dom.multiple = boolean;
  190. return this;
  191. }
  192. setOptions( options ) {
  193. const selected = this.dom.value;
  194. while ( this.dom.children.length > 0 ) {
  195. this.dom.removeChild( this.dom.firstChild );
  196. }
  197. for ( const key in options ) {
  198. const option = document.createElement( 'option' );
  199. option.value = key;
  200. option.innerHTML = options[ key ];
  201. this.dom.appendChild( option );
  202. }
  203. this.dom.value = selected;
  204. return this;
  205. }
  206. getValue() {
  207. return this.dom.value;
  208. }
  209. setValue( value ) {
  210. value = String( value );
  211. if ( this.dom.value !== value ) {
  212. this.dom.value = value;
  213. }
  214. return this;
  215. }
  216. }
  217. class UICheckbox extends UIElement {
  218. constructor( boolean ) {
  219. super( document.createElement( 'input' ) );
  220. this.dom.className = 'Checkbox';
  221. this.dom.type = 'checkbox';
  222. this.setValue( boolean );
  223. }
  224. getValue() {
  225. return this.dom.checked;
  226. }
  227. setValue( value ) {
  228. if ( value !== undefined ) {
  229. this.dom.checked = value;
  230. }
  231. return this;
  232. }
  233. }
  234. class UIColor extends UIElement {
  235. constructor() {
  236. super( document.createElement( 'input' ) );
  237. this.dom.className = 'Color';
  238. this.dom.style.width = '32px';
  239. this.dom.style.height = '16px';
  240. this.dom.style.border = '0px';
  241. this.dom.style.padding = '2px';
  242. this.dom.style.backgroundColor = 'transparent';
  243. this.dom.setAttribute( 'autocomplete', 'off' );
  244. try {
  245. this.dom.type = 'color';
  246. this.dom.value = '#ffffff';
  247. } catch ( exception ) {}
  248. }
  249. getValue() {
  250. return this.dom.value;
  251. }
  252. getHexValue() {
  253. return parseInt( this.dom.value.substr( 1 ), 16 );
  254. }
  255. setValue( value ) {
  256. this.dom.value = value;
  257. return this;
  258. }
  259. setHexValue( hex ) {
  260. this.dom.value = '#' + ( '000000' + hex.toString( 16 ) ).slice( - 6 );
  261. return this;
  262. }
  263. }
  264. class UINumber extends UIElement {
  265. constructor( number ) {
  266. super( document.createElement( 'input' ) );
  267. this.dom.style.cursor = 'ns-resize';
  268. this.dom.className = 'Number';
  269. this.dom.value = '0.00';
  270. this.dom.setAttribute( 'autocomplete', 'off' );
  271. this.value = 0;
  272. this.min = - Infinity;
  273. this.max = Infinity;
  274. this.precision = 2;
  275. this.step = 1;
  276. this.unit = '';
  277. this.nudge = 0.01;
  278. this.setValue( number );
  279. const scope = this;
  280. const changeEvent = document.createEvent( 'HTMLEvents' );
  281. changeEvent.initEvent( 'change', true, true );
  282. let distance = 0;
  283. let onMouseDownValue = 0;
  284. const pointer = { x: 0, y: 0 };
  285. const prevPointer = { x: 0, y: 0 };
  286. function onMouseDown( event ) {
  287. event.preventDefault();
  288. distance = 0;
  289. onMouseDownValue = scope.value;
  290. prevPointer.x = event.clientX;
  291. prevPointer.y = event.clientY;
  292. document.addEventListener( 'mousemove', onMouseMove );
  293. document.addEventListener( 'mouseup', onMouseUp );
  294. }
  295. function onMouseMove( event ) {
  296. const currentValue = scope.value;
  297. pointer.x = event.clientX;
  298. pointer.y = event.clientY;
  299. distance += ( pointer.x - prevPointer.x ) - ( pointer.y - prevPointer.y );
  300. let value = onMouseDownValue + ( distance / ( event.shiftKey ? 5 : 50 ) ) * scope.step;
  301. value = Math.min( scope.max, Math.max( scope.min, value ) );
  302. if ( currentValue !== value ) {
  303. scope.setValue( value );
  304. scope.dom.dispatchEvent( changeEvent );
  305. }
  306. prevPointer.x = event.clientX;
  307. prevPointer.y = event.clientY;
  308. }
  309. function onMouseUp() {
  310. document.removeEventListener( 'mousemove', onMouseMove );
  311. document.removeEventListener( 'mouseup', onMouseUp );
  312. if ( Math.abs( distance ) < 2 ) {
  313. scope.dom.focus();
  314. scope.dom.select();
  315. }
  316. }
  317. function onTouchStart( event ) {
  318. if ( event.touches.length === 1 ) {
  319. distance = 0;
  320. onMouseDownValue = scope.value;
  321. prevPointer.x = event.touches[ 0 ].pageX;
  322. prevPointer.y = event.touches[ 0 ].pageY;
  323. document.addEventListener( 'touchmove', onTouchMove );
  324. document.addEventListener( 'touchend', onTouchEnd );
  325. }
  326. }
  327. function onTouchMove( event ) {
  328. const currentValue = scope.value;
  329. pointer.x = event.touches[ 0 ].pageX;
  330. pointer.y = event.touches[ 0 ].pageY;
  331. distance += ( pointer.x - prevPointer.x ) - ( pointer.y - prevPointer.y );
  332. let value = onMouseDownValue + ( distance / ( event.shiftKey ? 5 : 50 ) ) * scope.step;
  333. value = Math.min( scope.max, Math.max( scope.min, value ) );
  334. if ( currentValue !== value ) {
  335. scope.setValue( value );
  336. scope.dom.dispatchEvent( changeEvent );
  337. }
  338. prevPointer.x = event.touches[ 0 ].pageX;
  339. prevPointer.y = event.touches[ 0 ].pageY;
  340. }
  341. function onTouchEnd( event ) {
  342. if ( event.touches.length === 0 ) {
  343. document.removeEventListener( 'touchmove', onTouchMove );
  344. document.removeEventListener( 'touchend', onTouchEnd );
  345. }
  346. }
  347. function onChange() {
  348. scope.setValue( scope.dom.value );
  349. }
  350. function onFocus() {
  351. scope.dom.style.backgroundColor = '';
  352. scope.dom.style.cursor = '';
  353. }
  354. function onBlur() {
  355. scope.dom.style.backgroundColor = 'transparent';
  356. scope.dom.style.cursor = 'ns-resize';
  357. }
  358. function onKeyDown( event ) {
  359. event.stopPropagation();
  360. switch ( event.keyCode ) {
  361. case 13: // enter
  362. scope.dom.blur();
  363. break;
  364. case 38: // up
  365. event.preventDefault();
  366. scope.setValue( scope.getValue() + scope.nudge );
  367. scope.dom.dispatchEvent( changeEvent );
  368. break;
  369. case 40: // down
  370. event.preventDefault();
  371. scope.setValue( scope.getValue() - scope.nudge );
  372. scope.dom.dispatchEvent( changeEvent );
  373. break;
  374. }
  375. }
  376. onBlur();
  377. this.dom.addEventListener( 'keydown', onKeyDown );
  378. this.dom.addEventListener( 'mousedown', onMouseDown );
  379. this.dom.addEventListener( 'touchstart', onTouchStart, { passive: false } );
  380. this.dom.addEventListener( 'change', onChange );
  381. this.dom.addEventListener( 'focus', onFocus );
  382. this.dom.addEventListener( 'blur', onBlur );
  383. }
  384. getValue() {
  385. return this.value;
  386. }
  387. setValue( value ) {
  388. if ( value !== undefined ) {
  389. value = parseFloat( value );
  390. if ( value < this.min ) value = this.min;
  391. if ( value > this.max ) value = this.max;
  392. this.value = value;
  393. this.dom.value = value.toFixed( this.precision );
  394. if ( this.unit !== '' ) this.dom.value += ' ' + this.unit;
  395. }
  396. return this;
  397. }
  398. setPrecision( precision ) {
  399. this.precision = precision;
  400. return this;
  401. }
  402. setStep( step ) {
  403. this.step = step;
  404. return this;
  405. }
  406. setNudge( nudge ) {
  407. this.nudge = nudge;
  408. return this;
  409. }
  410. setRange( min, max ) {
  411. this.min = min;
  412. this.max = max;
  413. return this;
  414. }
  415. setUnit( unit ) {
  416. this.unit = unit;
  417. return this;
  418. }
  419. }
  420. class UIInteger extends UIElement {
  421. constructor( number ) {
  422. super( document.createElement( 'input' ) );
  423. this.dom.style.cursor = 'ns-resize';
  424. this.dom.className = 'Number';
  425. this.dom.value = '0';
  426. this.dom.setAttribute( 'autocomplete', 'off' );
  427. this.value = 0;
  428. this.min = - Infinity;
  429. this.max = Infinity;
  430. this.step = 1;
  431. this.nudge = 1;
  432. this.setValue( number );
  433. const scope = this;
  434. const changeEvent = document.createEvent( 'HTMLEvents' );
  435. changeEvent.initEvent( 'change', true, true );
  436. let distance = 0;
  437. let onMouseDownValue = 0;
  438. const pointer = { x: 0, y: 0 };
  439. const prevPointer = { x: 0, y: 0 };
  440. function onMouseDown( event ) {
  441. event.preventDefault();
  442. distance = 0;
  443. onMouseDownValue = scope.value;
  444. prevPointer.x = event.clientX;
  445. prevPointer.y = event.clientY;
  446. document.addEventListener( 'mousemove', onMouseMove );
  447. document.addEventListener( 'mouseup', onMouseUp );
  448. }
  449. function onMouseMove( event ) {
  450. const currentValue = scope.value;
  451. pointer.x = event.clientX;
  452. pointer.y = event.clientY;
  453. distance += ( pointer.x - prevPointer.x ) - ( pointer.y - prevPointer.y );
  454. let value = onMouseDownValue + ( distance / ( event.shiftKey ? 5 : 50 ) ) * scope.step;
  455. value = Math.min( scope.max, Math.max( scope.min, value ) ) | 0;
  456. if ( currentValue !== value ) {
  457. scope.setValue( value );
  458. scope.dom.dispatchEvent( changeEvent );
  459. }
  460. prevPointer.x = event.clientX;
  461. prevPointer.y = event.clientY;
  462. }
  463. function onMouseUp() {
  464. document.removeEventListener( 'mousemove', onMouseMove );
  465. document.removeEventListener( 'mouseup', onMouseUp );
  466. if ( Math.abs( distance ) < 2 ) {
  467. scope.dom.focus();
  468. scope.dom.select();
  469. }
  470. }
  471. function onChange() {
  472. scope.setValue( scope.dom.value );
  473. }
  474. function onFocus() {
  475. scope.dom.style.backgroundColor = '';
  476. scope.dom.style.cursor = '';
  477. }
  478. function onBlur() {
  479. scope.dom.style.backgroundColor = 'transparent';
  480. scope.dom.style.cursor = 'ns-resize';
  481. }
  482. function onKeyDown( event ) {
  483. event.stopPropagation();
  484. switch ( event.keyCode ) {
  485. case 13: // enter
  486. scope.dom.blur();
  487. break;
  488. case 38: // up
  489. event.preventDefault();
  490. scope.setValue( scope.getValue() + scope.nudge );
  491. scope.dom.dispatchEvent( changeEvent );
  492. break;
  493. case 40: // down
  494. event.preventDefault();
  495. scope.setValue( scope.getValue() - scope.nudge );
  496. scope.dom.dispatchEvent( changeEvent );
  497. break;
  498. }
  499. }
  500. onBlur();
  501. this.dom.addEventListener( 'keydown', onKeyDown );
  502. this.dom.addEventListener( 'mousedown', onMouseDown );
  503. this.dom.addEventListener( 'change', onChange );
  504. this.dom.addEventListener( 'focus', onFocus );
  505. this.dom.addEventListener( 'blur', onBlur );
  506. }
  507. getValue() {
  508. return this.value;
  509. }
  510. setValue( value ) {
  511. if ( value !== undefined ) {
  512. value = parseInt( value );
  513. this.value = value;
  514. this.dom.value = value;
  515. }
  516. return this;
  517. }
  518. setStep( step ) {
  519. this.step = parseInt( step );
  520. return this;
  521. }
  522. setNudge( nudge ) {
  523. this.nudge = nudge;
  524. return this;
  525. }
  526. setRange( min, max ) {
  527. this.min = min;
  528. this.max = max;
  529. return this;
  530. }
  531. }
  532. class UIBreak extends UIElement {
  533. constructor() {
  534. super( document.createElement( 'br' ) );
  535. this.dom.className = 'Break';
  536. }
  537. }
  538. class UIHorizontalRule extends UIElement {
  539. constructor() {
  540. super( document.createElement( 'hr' ) );
  541. this.dom.className = 'HorizontalRule';
  542. }
  543. }
  544. class UIButton extends UIElement {
  545. constructor( value ) {
  546. super( document.createElement( 'button' ) );
  547. this.dom.className = 'Button';
  548. this.dom.textContent = value;
  549. }
  550. }
  551. class UIProgress extends UIElement {
  552. constructor( value ) {
  553. super( document.createElement( 'progress' ) );
  554. this.dom.value = value;
  555. }
  556. setValue( value ) {
  557. this.dom.value = value;
  558. }
  559. }
  560. class UITabbedPanel extends UIDiv {
  561. constructor() {
  562. super();
  563. this.dom.className = 'TabbedPanel';
  564. this.tabs = [];
  565. this.panels = [];
  566. this.tabsDiv = new UIDiv();
  567. this.tabsDiv.setClass( 'Tabs' );
  568. this.panelsDiv = new UIDiv();
  569. this.panelsDiv.setClass( 'Panels' );
  570. this.add( this.tabsDiv );
  571. this.add( this.panelsDiv );
  572. this.selected = '';
  573. }
  574. select( id ) {
  575. let tab;
  576. let panel;
  577. const scope = this;
  578. // Deselect current selection
  579. if ( this.selected && this.selected.length ) {
  580. tab = this.tabs.find( function ( item ) {
  581. return item.dom.id === scope.selected;
  582. } );
  583. panel = this.panels.find( function ( item ) {
  584. return item.dom.id === scope.selected;
  585. } );
  586. if ( tab ) {
  587. tab.removeClass( 'selected' );
  588. }
  589. if ( panel ) {
  590. panel.setDisplay( 'none' );
  591. }
  592. }
  593. tab = this.tabs.find( function ( item ) {
  594. return item.dom.id === id;
  595. } );
  596. panel = this.panels.find( function ( item ) {
  597. return item.dom.id === id;
  598. } );
  599. if ( tab ) {
  600. tab.addClass( 'selected' );
  601. }
  602. if ( panel ) {
  603. panel.setDisplay( '' );
  604. }
  605. this.selected = id;
  606. return this;
  607. }
  608. addTab( id, label, items ) {
  609. const tab = new UITab( label, this );
  610. tab.setId( id );
  611. this.tabs.push( tab );
  612. this.tabsDiv.add( tab );
  613. const panel = new UIDiv();
  614. panel.setId( id );
  615. panel.add( items );
  616. panel.setDisplay( 'none' );
  617. this.panels.push( panel );
  618. this.panelsDiv.add( panel );
  619. this.select( id );
  620. }
  621. }
  622. class UITab extends UIText {
  623. constructor( text, parent ) {
  624. super( text );
  625. this.dom.className = 'Tab';
  626. this.parent = parent;
  627. const scope = this;
  628. this.dom.addEventListener( 'click', function () {
  629. scope.parent.select( scope.dom.id );
  630. } );
  631. }
  632. }
  633. class UIListbox extends UIDiv {
  634. constructor() {
  635. super();
  636. this.dom.className = 'Listbox';
  637. this.dom.tabIndex = 0;
  638. this.items = [];
  639. this.listitems = [];
  640. this.selectedIndex = 0;
  641. this.selectedValue = null;
  642. }
  643. setItems( items ) {
  644. if ( Array.isArray( items ) ) {
  645. this.items = items;
  646. }
  647. this.render();
  648. }
  649. render( ) {
  650. while ( this.listitems.length ) {
  651. const item = this.listitems[ 0 ];
  652. item.dom.remove();
  653. this.listitems.splice( 0, 1 );
  654. }
  655. for ( let i = 0; i < this.items.length; i ++ ) {
  656. const item = this.items[ i ];
  657. const listitem = new ListboxItem( this );
  658. listitem.setId( item.id || `Listbox-${i}` );
  659. listitem.setTextContent( item.name || item.type );
  660. this.add( listitem );
  661. }
  662. }
  663. add() {
  664. const items = Array.from( arguments );
  665. this.listitems = this.listitems.concat( items );
  666. UIElement.prototype.add.apply( this, items );
  667. }
  668. selectIndex( index ) {
  669. if ( index >= 0 && index < this.items.length ) {
  670. this.setValue( this.listitems[ index ].getId() );
  671. }
  672. this.selectedIndex = index;
  673. }
  674. getValue() {
  675. return this.selectedValue;
  676. }
  677. setValue( value ) {
  678. for ( let i = 0; i < this.listitems.length; i ++ ) {
  679. const element = this.listitems[ i ];
  680. if ( element.getId() === value ) {
  681. element.addClass( 'active' );
  682. } else {
  683. element.removeClass( 'active' );
  684. }
  685. }
  686. this.selectedValue = value;
  687. const changeEvent = document.createEvent( 'HTMLEvents' );
  688. changeEvent.initEvent( 'change', true, true );
  689. this.dom.dispatchEvent( changeEvent );
  690. }
  691. }
  692. class ListboxItem extends UIDiv {
  693. constructor( parent ) {
  694. super();
  695. this.dom.className = 'ListboxItem';
  696. this.parent = parent;
  697. const scope = this;
  698. function onClick() {
  699. if ( scope.parent ) {
  700. scope.parent.setValue( scope.getId( ) );
  701. }
  702. }
  703. this.dom.addEventListener( 'click', onClick );
  704. }
  705. }
  706. export { UIElement, UISpan, UIDiv, UIRow, UIPanel, UIText, UIInput, UITextArea, UISelect, UICheckbox, UIColor, UINumber, UIInteger, UIBreak, UIHorizontalRule, UIButton, UIProgress, UITabbedPanel, UIListbox, ListboxItem };