ui.js 19 KB

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