ui.js 19 KB

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