ui.js 20 KB

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