2
0

ui.js 19 KB

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