ui.js 22 KB

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