ui.js 22 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354
  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. // UITabbedPanel
  594. function UITabbedPanel( ) {
  595. UIElement.call( this );
  596. var dom = document.createElement( 'div' );
  597. this.dom = dom;
  598. this.setClass( 'TabbedPanel' );
  599. this.tabs = [];
  600. this.panels = [];
  601. this.tabsDiv = new UIDiv();
  602. this.tabsDiv.setClass( 'Tabs' );
  603. this.panelsDiv = new UIDiv();
  604. this.panelsDiv.setClass( 'Panels' );
  605. this.add( this.tabsDiv );
  606. this.add( this.panelsDiv );
  607. this.selected = '';
  608. return this;
  609. }
  610. UITabbedPanel.prototype = Object.create( UIElement.prototype );
  611. UITabbedPanel.prototype.constructor = UITabbedPanel;
  612. UITabbedPanel.prototype.select = function ( id ) {
  613. var tab;
  614. var panel;
  615. var scope = this;
  616. // Deselect current selection
  617. if ( this.selected && this.selected.length ) {
  618. tab = this.tabs.find( function ( item ) {
  619. return item.dom.id === scope.selected;
  620. } );
  621. panel = this.panels.find( function ( item ) {
  622. return item.dom.id === scope.selected;
  623. } );
  624. if ( tab ) {
  625. tab.removeClass( 'selected' );
  626. }
  627. if ( panel ) {
  628. panel.setDisplay( 'none' );
  629. }
  630. }
  631. tab = this.tabs.find( function ( item ) {
  632. return item.dom.id === id;
  633. } );
  634. panel = this.panels.find( function ( item ) {
  635. return item.dom.id === id;
  636. } );
  637. if ( tab ) {
  638. tab.addClass( 'selected' );
  639. }
  640. if ( panel ) {
  641. panel.setDisplay( '' );
  642. }
  643. this.selected = id;
  644. return this;
  645. };
  646. UITabbedPanel.prototype.addTab = function ( id, label, items ) {
  647. var tab = new UITabbedPanel.Tab( label, this );
  648. tab.setId( id );
  649. this.tabs.push( tab );
  650. this.tabsDiv.add( tab );
  651. var panel = new UIDiv();
  652. panel.setId( id );
  653. panel.add( items );
  654. panel.setDisplay( 'none' );
  655. this.panels.push( panel );
  656. this.panelsDiv.add( panel );
  657. this.select( id );
  658. };
  659. UITabbedPanel.Tab = function ( text, parent ) {
  660. UIText.call( this, text );
  661. this.parent = parent;
  662. this.setClass( 'Tab' );
  663. var scope = this;
  664. this.dom.addEventListener( 'click', function () {
  665. scope.parent.select( scope.dom.id );
  666. } );
  667. return this;
  668. };
  669. UITabbedPanel.Tab.prototype = Object.create( UIText.prototype );
  670. UITabbedPanel.Tab.prototype.constructor = UITabbedPanel.Tab;
  671. // UIListbox
  672. function UIListbox( ) {
  673. UIElement.call( this );
  674. var dom = document.createElement( 'div' );
  675. dom.className = 'Listbox';
  676. dom.tabIndex = 0;
  677. this.dom = dom;
  678. this.items = [];
  679. this.listitems = [];
  680. this.selectedIndex = 0;
  681. this.selectedValue = null;
  682. return this;
  683. }
  684. UIListbox.prototype = Object.create( UIElement.prototype );
  685. UIListbox.prototype.constructor = UIListbox;
  686. UIListbox.prototype.setItems = function ( items ) {
  687. if ( Array.isArray( items ) ) {
  688. this.items = items;
  689. }
  690. this.render();
  691. };
  692. UIListbox.prototype.render = function ( ) {
  693. while ( this.listitems.length ) {
  694. var item = this.listitems[ 0 ];
  695. item.dom.remove();
  696. this.listitems.splice( 0, 1 );
  697. }
  698. for ( var i = 0; i < this.items.length; i ++ ) {
  699. var item = this.items[ i ];
  700. var listitem = new UIListbox.ListboxItem( this );
  701. listitem.setId( item.id || `Listbox-${i}` );
  702. listitem.setTextContent( item.name || item.type );
  703. this.add( listitem );
  704. }
  705. };
  706. // Assuming user passes valid list items
  707. UIListbox.prototype.add = function () {
  708. var items = Array.from( arguments );
  709. this.listitems = this.listitems.concat( items );
  710. UIElement.prototype.add.apply( this, items );
  711. };
  712. UIListbox.prototype.selectIndex = function ( index ) {
  713. if ( index >= 0 && index < this.items.length ) {
  714. this.setValue( this.listitems[ index ].getId() );
  715. }
  716. this.selectedIndex = index;
  717. };
  718. UIListbox.prototype.getValue = function () {
  719. return this.selectedValue;
  720. };
  721. UIListbox.prototype.setValue = function ( value ) {
  722. for ( var i = 0; i < this.listitems.length; i ++ ) {
  723. var element = this.listitems[ i ];
  724. if ( element.getId() === value ) {
  725. element.addClass( 'active' );
  726. } else {
  727. element.removeClass( 'active' );
  728. }
  729. }
  730. this.selectedValue = value;
  731. var changeEvent = document.createEvent( 'HTMLEvents' );
  732. changeEvent.initEvent( 'change', true, true );
  733. this.dom.dispatchEvent( changeEvent );
  734. };
  735. // Listbox Item
  736. UIListbox.ListboxItem = function ( parent ) {
  737. UIElement.call( this );
  738. var dom = document.createElement( 'div' );
  739. dom.className = 'ListboxItem';
  740. this.parent = parent;
  741. this.dom = dom;
  742. var scope = this;
  743. function onClick() {
  744. if ( scope.parent ) {
  745. scope.parent.setValue( scope.getId( ) );
  746. }
  747. }
  748. dom.addEventListener( 'click', onClick, false );
  749. return this;
  750. };
  751. UIListbox.ListboxItem.prototype = Object.create( UIElement.prototype );
  752. UIListbox.ListboxItem.prototype.constructor = UIListbox.ListboxItem;
  753. export { UIElement, UISpan, UIDiv, UIRow, UIPanel, UIText, UIInput, UITextArea, UISelect, UICheckbox, UIColor, UINumber, UIInteger, UIBreak, UIHorizontalRule, UIButton, UITabbedPanel, UIListbox };