ui.js 20 KB

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