ui.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326
  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. }
  622. if ( panel ) {
  623. panel.setDisplay( '' );
  624. }
  625. this.selected = id;
  626. return this;
  627. }
  628. addTab( id, label, items ) {
  629. const tab = new UITab( label, this );
  630. tab.setId( id );
  631. this.tabs.push( tab );
  632. this.tabsDiv.add( tab );
  633. const panel = new UIDiv();
  634. panel.setId( id );
  635. panel.add( items );
  636. panel.setDisplay( 'none' );
  637. this.panels.push( panel );
  638. this.panelsDiv.add( panel );
  639. this.select( id );
  640. }
  641. }
  642. class UITab extends UIText {
  643. constructor( text, parent ) {
  644. super( text );
  645. this.dom.className = 'Tab';
  646. this.parent = parent;
  647. const scope = this;
  648. this.dom.addEventListener( 'click', function () {
  649. scope.parent.select( scope.dom.id );
  650. } );
  651. }
  652. }
  653. class UIListbox extends UIDiv {
  654. constructor() {
  655. super();
  656. this.dom.className = 'Listbox';
  657. this.dom.tabIndex = 0;
  658. this.items = [];
  659. this.listitems = [];
  660. this.selectedIndex = 0;
  661. this.selectedValue = null;
  662. }
  663. setItems( items ) {
  664. if ( Array.isArray( items ) ) {
  665. this.items = items;
  666. }
  667. this.render();
  668. }
  669. render( ) {
  670. while ( this.listitems.length ) {
  671. const item = this.listitems[ 0 ];
  672. item.dom.remove();
  673. this.listitems.splice( 0, 1 );
  674. }
  675. for ( let i = 0; i < this.items.length; i ++ ) {
  676. const item = this.items[ i ];
  677. const listitem = new ListboxItem( this );
  678. listitem.setId( item.id || `Listbox-${i}` );
  679. listitem.setTextContent( item.name || item.type );
  680. this.add( listitem );
  681. }
  682. }
  683. add() {
  684. const items = Array.from( arguments );
  685. this.listitems = this.listitems.concat( items );
  686. UIElement.prototype.add.apply( this, items );
  687. }
  688. selectIndex( index ) {
  689. if ( index >= 0 && index < this.items.length ) {
  690. this.setValue( this.listitems[ index ].getId() );
  691. }
  692. this.selectedIndex = index;
  693. }
  694. getValue() {
  695. return this.selectedValue;
  696. }
  697. setValue( value ) {
  698. for ( let i = 0; i < this.listitems.length; i ++ ) {
  699. const element = this.listitems[ i ];
  700. if ( element.getId() === value ) {
  701. element.addClass( 'active' );
  702. } else {
  703. element.removeClass( 'active' );
  704. }
  705. }
  706. this.selectedValue = value;
  707. const changeEvent = new Event( 'change', { bubbles: true, cancelable: true } );
  708. this.dom.dispatchEvent( changeEvent );
  709. }
  710. }
  711. class ListboxItem extends UIDiv {
  712. constructor( parent ) {
  713. super();
  714. this.dom.className = 'ListboxItem';
  715. this.parent = parent;
  716. const scope = this;
  717. function onClick() {
  718. if ( scope.parent ) {
  719. scope.parent.setValue( scope.getId( ) );
  720. }
  721. }
  722. this.dom.addEventListener( 'click', onClick );
  723. }
  724. }
  725. export { UIElement, UISpan, UIDiv, UIRow, UIPanel, UIText, UIInput, UITextArea, UISelect, UICheckbox, UIColor, UINumber, UIInteger, UIBreak, UIHorizontalRule, UIButton, UIProgress, UITabbedPanel, UIListbox, ListboxItem };