ui.js 19 KB

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