ui.js 22 KB

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