ui.js 19 KB

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