ui.js 22 KB

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