ui.js 19 KB

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