ui.js 20 KB

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