ui.js 19 KB

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