ui.js 19 KB

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