ui.js 19 KB

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