ui.js 22 KB

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