ui.js 19 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301
  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. 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.keyCode ) {
  369. case 13: // enter
  370. scope.dom.blur();
  371. break;
  372. case 38: // up
  373. event.preventDefault();
  374. scope.setValue( scope.getValue() + scope.nudge );
  375. scope.dom.dispatchEvent( changeEvent );
  376. break;
  377. case 40: // down
  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. return this;
  426. }
  427. }
  428. class UIInteger extends UIElement {
  429. constructor( number ) {
  430. super( document.createElement( 'input' ) );
  431. this.dom.style.cursor = 'ns-resize';
  432. this.dom.className = 'Number';
  433. this.dom.value = '0';
  434. this.dom.setAttribute( 'autocomplete', 'off' );
  435. this.value = 0;
  436. this.min = - Infinity;
  437. this.max = Infinity;
  438. this.step = 1;
  439. this.nudge = 1;
  440. this.setValue( number );
  441. const scope = this;
  442. const changeEvent = document.createEvent( 'HTMLEvents' );
  443. changeEvent.initEvent( 'change', true, 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. event.preventDefault();
  450. distance = 0;
  451. onMouseDownValue = scope.value;
  452. prevPointer.x = event.clientX;
  453. prevPointer.y = event.clientY;
  454. document.addEventListener( 'mousemove', onMouseMove );
  455. document.addEventListener( 'mouseup', onMouseUp );
  456. }
  457. function onMouseMove( event ) {
  458. const currentValue = scope.value;
  459. pointer.x = event.clientX;
  460. pointer.y = event.clientY;
  461. distance += ( pointer.x - prevPointer.x ) - ( pointer.y - prevPointer.y );
  462. let value = onMouseDownValue + ( distance / ( event.shiftKey ? 5 : 50 ) ) * scope.step;
  463. value = Math.min( scope.max, Math.max( scope.min, value ) ) | 0;
  464. if ( currentValue !== value ) {
  465. scope.setValue( value );
  466. scope.dom.dispatchEvent( changeEvent );
  467. }
  468. prevPointer.x = event.clientX;
  469. prevPointer.y = event.clientY;
  470. }
  471. function onMouseUp() {
  472. document.removeEventListener( 'mousemove', onMouseMove );
  473. document.removeEventListener( 'mouseup', onMouseUp );
  474. if ( Math.abs( distance ) < 2 ) {
  475. scope.dom.focus();
  476. scope.dom.select();
  477. }
  478. }
  479. function onChange() {
  480. scope.setValue( scope.dom.value );
  481. }
  482. function onFocus() {
  483. scope.dom.style.backgroundColor = '';
  484. scope.dom.style.cursor = '';
  485. }
  486. function onBlur() {
  487. scope.dom.style.backgroundColor = 'transparent';
  488. scope.dom.style.cursor = 'ns-resize';
  489. }
  490. function onKeyDown( event ) {
  491. event.stopPropagation();
  492. switch ( event.keyCode ) {
  493. case 13: // enter
  494. scope.dom.blur();
  495. break;
  496. case 38: // up
  497. event.preventDefault();
  498. scope.setValue( scope.getValue() + scope.nudge );
  499. scope.dom.dispatchEvent( changeEvent );
  500. break;
  501. case 40: // down
  502. event.preventDefault();
  503. scope.setValue( scope.getValue() - scope.nudge );
  504. scope.dom.dispatchEvent( changeEvent );
  505. break;
  506. }
  507. }
  508. onBlur();
  509. this.dom.addEventListener( 'keydown', onKeyDown );
  510. this.dom.addEventListener( 'mousedown', onMouseDown );
  511. this.dom.addEventListener( 'change', onChange );
  512. this.dom.addEventListener( 'focus', onFocus );
  513. this.dom.addEventListener( 'blur', onBlur );
  514. }
  515. getValue() {
  516. return this.value;
  517. }
  518. setValue( value ) {
  519. if ( value !== undefined ) {
  520. value = parseInt( value );
  521. this.value = value;
  522. this.dom.value = value;
  523. }
  524. return this;
  525. }
  526. setStep( step ) {
  527. this.step = parseInt( step );
  528. return this;
  529. }
  530. setNudge( nudge ) {
  531. this.nudge = nudge;
  532. return this;
  533. }
  534. setRange( min, max ) {
  535. this.min = min;
  536. this.max = max;
  537. return this;
  538. }
  539. }
  540. class UIBreak extends UIElement {
  541. constructor() {
  542. super( document.createElement( 'br' ) );
  543. this.dom.className = 'Break';
  544. }
  545. }
  546. class UIHorizontalRule extends UIElement {
  547. constructor() {
  548. super( document.createElement( 'hr' ) );
  549. this.dom.className = 'HorizontalRule';
  550. }
  551. }
  552. class UIButton extends UIElement {
  553. constructor( value ) {
  554. super( document.createElement( 'button' ) );
  555. this.dom.className = 'Button';
  556. this.dom.textContent = value;
  557. }
  558. }
  559. class UIProgress extends UIElement {
  560. constructor( value ) {
  561. super( document.createElement( 'progress' ) );
  562. this.dom.value = value;
  563. }
  564. setValue( value ) {
  565. this.dom.value = value;
  566. }
  567. }
  568. class UITabbedPanel extends UIDiv {
  569. constructor() {
  570. super();
  571. this.dom.className = 'TabbedPanel';
  572. this.tabs = [];
  573. this.panels = [];
  574. this.tabsDiv = new UIDiv();
  575. this.tabsDiv.setClass( 'Tabs' );
  576. this.panelsDiv = new UIDiv();
  577. this.panelsDiv.setClass( 'Panels' );
  578. this.add( this.tabsDiv );
  579. this.add( this.panelsDiv );
  580. this.selected = '';
  581. }
  582. select( id ) {
  583. let tab;
  584. let panel;
  585. const scope = this;
  586. // Deselect current selection
  587. if ( this.selected && this.selected.length ) {
  588. tab = this.tabs.find( function ( item ) {
  589. return item.dom.id === scope.selected;
  590. } );
  591. panel = this.panels.find( function ( item ) {
  592. return item.dom.id === scope.selected;
  593. } );
  594. if ( tab ) {
  595. tab.removeClass( 'selected' );
  596. }
  597. if ( panel ) {
  598. panel.setDisplay( 'none' );
  599. }
  600. }
  601. tab = this.tabs.find( function ( item ) {
  602. return item.dom.id === id;
  603. } );
  604. panel = this.panels.find( function ( item ) {
  605. return item.dom.id === id;
  606. } );
  607. if ( tab ) {
  608. tab.addClass( 'selected' );
  609. }
  610. if ( panel ) {
  611. panel.setDisplay( '' );
  612. }
  613. this.selected = id;
  614. return this;
  615. }
  616. addTab( id, label, items ) {
  617. const tab = new UITab( label, this );
  618. tab.setId( id );
  619. this.tabs.push( tab );
  620. this.tabsDiv.add( tab );
  621. const panel = new UIDiv();
  622. panel.setId( id );
  623. panel.add( items );
  624. panel.setDisplay( 'none' );
  625. this.panels.push( panel );
  626. this.panelsDiv.add( panel );
  627. this.select( id );
  628. }
  629. }
  630. class UITab extends UIText {
  631. constructor( text, parent ) {
  632. super( text );
  633. this.dom.className = 'Tab';
  634. this.parent = parent;
  635. const scope = this;
  636. this.dom.addEventListener( 'click', function () {
  637. scope.parent.select( scope.dom.id );
  638. } );
  639. }
  640. }
  641. class UIListbox extends UIDiv {
  642. constructor() {
  643. super();
  644. this.dom.className = 'Listbox';
  645. this.dom.tabIndex = 0;
  646. this.items = [];
  647. this.listitems = [];
  648. this.selectedIndex = 0;
  649. this.selectedValue = null;
  650. }
  651. setItems( items ) {
  652. if ( Array.isArray( items ) ) {
  653. this.items = items;
  654. }
  655. this.render();
  656. }
  657. render( ) {
  658. while ( this.listitems.length ) {
  659. const item = this.listitems[ 0 ];
  660. item.dom.remove();
  661. this.listitems.splice( 0, 1 );
  662. }
  663. for ( let i = 0; i < this.items.length; i ++ ) {
  664. const item = this.items[ i ];
  665. const listitem = new ListboxItem( this );
  666. listitem.setId( item.id || `Listbox-${i}` );
  667. listitem.setTextContent( item.name || item.type );
  668. this.add( listitem );
  669. }
  670. }
  671. add() {
  672. const items = Array.from( arguments );
  673. this.listitems = this.listitems.concat( items );
  674. UIElement.prototype.add.apply( this, items );
  675. }
  676. selectIndex( index ) {
  677. if ( index >= 0 && index < this.items.length ) {
  678. this.setValue( this.listitems[ index ].getId() );
  679. }
  680. this.selectedIndex = index;
  681. }
  682. getValue() {
  683. return this.selectedValue;
  684. }
  685. setValue( value ) {
  686. for ( let i = 0; i < this.listitems.length; i ++ ) {
  687. const element = this.listitems[ i ];
  688. if ( element.getId() === value ) {
  689. element.addClass( 'active' );
  690. } else {
  691. element.removeClass( 'active' );
  692. }
  693. }
  694. this.selectedValue = value;
  695. const changeEvent = document.createEvent( 'HTMLEvents' );
  696. changeEvent.initEvent( 'change', true, 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 };