ui.js 21 KB

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