ui.js 19 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148
  1. /**
  2. * @author mrdoob / http://mrdoob.com/
  3. */
  4. var UI = {};
  5. UI.Element = function ( dom ) {
  6. this.dom = dom;
  7. };
  8. UI.Element.prototype = {
  9. add: function () {
  10. for ( var i = 0; i < arguments.length; i ++ ) {
  11. var argument = arguments[ i ];
  12. if ( argument instanceof UI.Element ) {
  13. this.dom.appendChild( argument.dom );
  14. } else {
  15. console.error( 'UI.Element:', argument, 'is not an instance of UI.Element.' );
  16. }
  17. }
  18. return this;
  19. },
  20. remove: function () {
  21. for ( var i = 0; i < arguments.length; i ++ ) {
  22. var argument = arguments[ i ];
  23. if ( argument instanceof UI.Element ) {
  24. this.dom.removeChild( argument.dom );
  25. } else {
  26. console.error( 'UI.Element:', argument, 'is not an instance of UI.Element.' );
  27. }
  28. }
  29. return this;
  30. },
  31. clear: function () {
  32. while ( this.dom.children.length ) {
  33. this.dom.removeChild( this.dom.lastChild );
  34. }
  35. },
  36. setId: function ( id ) {
  37. this.dom.id = id;
  38. return this;
  39. },
  40. getId: function ( id ) {
  41. return this.dom.id;
  42. },
  43. setClass: function ( name ) {
  44. this.dom.className = name;
  45. return this;
  46. },
  47. setStyle: function ( style, array ) {
  48. for ( var i = 0; i < array.length; i ++ ) {
  49. this.dom.style[ style ] = array[ i ];
  50. }
  51. return this;
  52. },
  53. setDisabled: function ( value ) {
  54. this.dom.disabled = value;
  55. return this;
  56. },
  57. setTextContent: function ( value ) {
  58. this.dom.textContent = value;
  59. return this;
  60. }
  61. };
  62. // properties
  63. var properties = [ 'position', 'left', 'top', 'right', 'bottom', 'width', 'height', 'border', 'borderLeft',
  64. 'borderTop', 'borderRight', 'borderBottom', 'borderColor', 'display', 'overflow', 'margin', 'marginLeft', 'marginTop', 'marginRight', 'marginBottom', 'padding', 'paddingLeft', 'paddingTop', 'paddingRight', 'paddingBottom', 'color',
  65. 'background', 'backgroundColor', 'opacity', 'fontSize', 'fontWeight', 'textAlign', 'textDecoration', 'textTransform', 'cursor', 'zIndex' ];
  66. properties.forEach( function ( property ) {
  67. var method = 'set' + property.substr( 0, 1 ).toUpperCase() + property.substr( 1, property.length );
  68. UI.Element.prototype[ method ] = function () {
  69. this.setStyle( property, arguments );
  70. return this;
  71. };
  72. } );
  73. // events
  74. var events = [ 'KeyUp', 'KeyDown', 'MouseOver', 'MouseOut', 'Click', 'DblClick', 'Change' ];
  75. events.forEach( function ( event ) {
  76. var method = 'on' + event;
  77. UI.Element.prototype[ method ] = function ( callback ) {
  78. this.dom.addEventListener( event.toLowerCase(), callback.bind( this ), false );
  79. return this;
  80. };
  81. } );
  82. // Span
  83. UI.Span = function () {
  84. UI.Element.call( this );
  85. this.dom = document.createElement( 'span' );
  86. return this;
  87. };
  88. UI.Span.prototype = Object.create( UI.Element.prototype );
  89. UI.Span.prototype.constructor = UI.Span;
  90. // Div
  91. UI.Div = function () {
  92. UI.Element.call( this );
  93. this.dom = document.createElement( 'div' );
  94. return this;
  95. };
  96. UI.Div.prototype = Object.create( UI.Element.prototype );
  97. UI.Div.prototype.constructor = UI.Div;
  98. // Row
  99. UI.Row = function () {
  100. UI.Element.call( this );
  101. var dom = document.createElement( 'div' );
  102. dom.className = 'Row';
  103. this.dom = dom;
  104. return this;
  105. };
  106. UI.Row.prototype = Object.create( UI.Element.prototype );
  107. UI.Row.prototype.constructor = UI.Row;
  108. // Panel
  109. UI.Panel = function () {
  110. UI.Element.call( this );
  111. var dom = document.createElement( 'div' );
  112. dom.className = 'Panel';
  113. this.dom = dom;
  114. return this;
  115. };
  116. UI.Panel.prototype = Object.create( UI.Element.prototype );
  117. UI.Panel.prototype.constructor = UI.Panel;
  118. // Text
  119. UI.Text = function ( text ) {
  120. UI.Element.call( this );
  121. var dom = document.createElement( 'span' );
  122. dom.className = 'Text';
  123. dom.style.cursor = 'default';
  124. dom.style.display = 'inline-block';
  125. dom.style.verticalAlign = 'middle';
  126. this.dom = dom;
  127. this.setValue( text );
  128. return this;
  129. };
  130. UI.Text.prototype = Object.create( UI.Element.prototype );
  131. UI.Text.prototype.constructor = UI.Text;
  132. UI.Text.prototype.getValue = function () {
  133. return this.dom.textContent;
  134. };
  135. UI.Text.prototype.setValue = function ( value ) {
  136. if ( value !== undefined ) {
  137. this.dom.textContent = value;
  138. }
  139. return this;
  140. };
  141. // Input
  142. UI.Input = function ( text ) {
  143. UI.Element.call( this );
  144. var scope = this;
  145. var dom = document.createElement( 'input' );
  146. dom.className = 'Input';
  147. dom.style.padding = '2px';
  148. dom.style.border = '1px solid transparent';
  149. dom.addEventListener( 'keydown', function ( event ) {
  150. event.stopPropagation();
  151. }, false );
  152. this.dom = dom;
  153. this.setValue( text );
  154. return this;
  155. };
  156. UI.Input.prototype = Object.create( UI.Element.prototype );
  157. UI.Input.prototype.constructor = UI.Input;
  158. UI.Input.prototype.getValue = function () {
  159. return this.dom.value;
  160. };
  161. UI.Input.prototype.setValue = function ( value ) {
  162. this.dom.value = value;
  163. return this;
  164. };
  165. // TextArea
  166. UI.TextArea = function () {
  167. UI.Element.call( this );
  168. var scope = this;
  169. var dom = document.createElement( 'textarea' );
  170. dom.className = 'TextArea';
  171. dom.style.padding = '2px';
  172. dom.spellcheck = false;
  173. dom.addEventListener( 'keydown', function ( event ) {
  174. event.stopPropagation();
  175. if ( event.keyCode === 9 ) {
  176. event.preventDefault();
  177. var cursor = dom.selectionStart;
  178. dom.value = dom.value.substring( 0, cursor ) + '\t' + dom.value.substring( cursor );
  179. dom.selectionStart = cursor + 1;
  180. dom.selectionEnd = dom.selectionStart;
  181. }
  182. }, false );
  183. this.dom = dom;
  184. return this;
  185. };
  186. UI.TextArea.prototype = Object.create( UI.Element.prototype );
  187. UI.TextArea.prototype.constructor = UI.TextArea;
  188. UI.TextArea.prototype.getValue = function () {
  189. return this.dom.value;
  190. };
  191. UI.TextArea.prototype.setValue = function ( value ) {
  192. this.dom.value = value;
  193. return this;
  194. };
  195. // Select
  196. UI.Select = function () {
  197. UI.Element.call( this );
  198. var scope = this;
  199. var dom = document.createElement( 'select' );
  200. dom.className = 'Select';
  201. dom.style.padding = '2px';
  202. this.dom = dom;
  203. return this;
  204. };
  205. UI.Select.prototype = Object.create( UI.Element.prototype );
  206. UI.Select.prototype.constructor = UI.Select;
  207. UI.Select.prototype.setMultiple = function ( boolean ) {
  208. this.dom.multiple = boolean;
  209. return this;
  210. };
  211. UI.Select.prototype.setOptions = function ( options ) {
  212. var selected = this.dom.value;
  213. while ( this.dom.children.length > 0 ) {
  214. this.dom.removeChild( this.dom.firstChild );
  215. }
  216. for ( var key in options ) {
  217. var option = document.createElement( 'option' );
  218. option.value = key;
  219. option.innerHTML = options[ key ];
  220. this.dom.appendChild( option );
  221. }
  222. this.dom.value = selected;
  223. return this;
  224. };
  225. UI.Select.prototype.getValue = function () {
  226. return this.dom.value;
  227. };
  228. UI.Select.prototype.setValue = function ( value ) {
  229. value = String( value );
  230. if ( this.dom.value !== value ) {
  231. this.dom.value = value;
  232. }
  233. return this;
  234. };
  235. // Checkbox
  236. UI.Checkbox = function ( boolean ) {
  237. UI.Element.call( this );
  238. var scope = 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. UI.Checkbox.prototype = Object.create( UI.Element.prototype );
  247. UI.Checkbox.prototype.constructor = UI.Checkbox;
  248. UI.Checkbox.prototype.getValue = function () {
  249. return this.dom.checked;
  250. };
  251. UI.Checkbox.prototype.setValue = function ( value ) {
  252. if ( value !== undefined ) {
  253. this.dom.checked = value;
  254. }
  255. return this;
  256. };
  257. // Color
  258. UI.Color = function () {
  259. UI.Element.call( this );
  260. var scope = this;
  261. var dom = document.createElement( 'input' );
  262. dom.className = 'Color';
  263. dom.style.width = '64px';
  264. dom.style.height = '17px';
  265. dom.style.border = '0px';
  266. dom.style.padding = '2px';
  267. dom.style.backgroundColor = 'transparent';
  268. try {
  269. dom.type = 'color';
  270. dom.value = '#ffffff';
  271. } catch ( exception ) {}
  272. this.dom = dom;
  273. return this;
  274. };
  275. UI.Color.prototype = Object.create( UI.Element.prototype );
  276. UI.Color.prototype.constructor = UI.Color;
  277. UI.Color.prototype.getValue = function () {
  278. return this.dom.value;
  279. };
  280. UI.Color.prototype.getHexValue = function () {
  281. return parseInt( this.dom.value.substr( 1 ), 16 );
  282. };
  283. UI.Color.prototype.setValue = function ( value ) {
  284. this.dom.value = value;
  285. return this;
  286. };
  287. UI.Color.prototype.setHexValue = function ( hex ) {
  288. this.dom.value = '#' + ( '000000' + hex.toString( 16 ) ).slice( - 6 );
  289. return this;
  290. };
  291. // Number
  292. UI.Number = function ( number ) {
  293. UI.Element.call( this );
  294. var scope = this;
  295. var dom = document.createElement( 'input' );
  296. dom.className = 'Number';
  297. dom.value = '0.00';
  298. dom.addEventListener( 'keydown', function ( event ) {
  299. event.stopPropagation();
  300. if ( event.keyCode === 13 ) dom.blur();
  301. }, false );
  302. this.value = 0;
  303. this.min = - Infinity;
  304. this.max = Infinity;
  305. this.precision = 2;
  306. this.step = 1;
  307. this.unit = '';
  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( event ) {
  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( event ) {
  372. scope.setValue( dom.value );
  373. }
  374. function onFocus( event ) {
  375. dom.style.backgroundColor = '';
  376. dom.style.cursor = '';
  377. }
  378. function onBlur( event ) {
  379. dom.style.backgroundColor = 'transparent';
  380. dom.style.cursor = 'col-resize';
  381. }
  382. onBlur();
  383. dom.addEventListener( 'mousedown', onMouseDown, false );
  384. dom.addEventListener( 'touchstart', onTouchStart, false );
  385. dom.addEventListener( 'change', onChange, false );
  386. dom.addEventListener( 'focus', onFocus, false );
  387. dom.addEventListener( 'blur', onBlur, false );
  388. return this;
  389. };
  390. UI.Number.prototype = Object.create( UI.Element.prototype );
  391. UI.Number.prototype.constructor = UI.Number;
  392. UI.Number.prototype.getValue = function () {
  393. return this.value;
  394. };
  395. UI.Number.prototype.setValue = function ( value ) {
  396. if ( value !== undefined ) {
  397. value = parseFloat( value );
  398. if ( value < this.min ) value = this.min;
  399. if ( value > this.max ) value = this.max;
  400. this.value = value;
  401. this.dom.value = value.toFixed( this.precision );
  402. if ( this.unit !== '' ) this.dom.value += ' ' + this.unit;
  403. }
  404. return this;
  405. };
  406. UI.Number.prototype.setPrecision = function ( precision ) {
  407. this.precision = precision;
  408. return this;
  409. };
  410. UI.Number.prototype.setStep = function ( step ) {
  411. this.step = step;
  412. return this;
  413. };
  414. UI.Number.prototype.setRange = function ( min, max ) {
  415. this.min = min;
  416. this.max = max;
  417. return this;
  418. };
  419. UI.Number.prototype.setUnit = function ( unit ) {
  420. this.unit = unit;
  421. return this;
  422. };
  423. // Integer
  424. UI.Integer = function ( number ) {
  425. UI.Element.call( this );
  426. var scope = this;
  427. var dom = document.createElement( 'input' );
  428. dom.className = 'Number';
  429. dom.value = '0';
  430. dom.addEventListener( 'keydown', function ( event ) {
  431. event.stopPropagation();
  432. }, false );
  433. this.value = 0;
  434. this.min = - Infinity;
  435. this.max = Infinity;
  436. this.step = 1;
  437. this.dom = dom;
  438. this.setValue( number );
  439. var changeEvent = document.createEvent( 'HTMLEvents' );
  440. changeEvent.initEvent( 'change', true, true );
  441. var distance = 0;
  442. var onMouseDownValue = 0;
  443. var pointer = [ 0, 0 ];
  444. var prevPointer = [ 0, 0 ];
  445. function onMouseDown( event ) {
  446. event.preventDefault();
  447. distance = 0;
  448. onMouseDownValue = scope.value;
  449. prevPointer = [ event.clientX, event.clientY ];
  450. document.addEventListener( 'mousemove', onMouseMove, false );
  451. document.addEventListener( 'mouseup', onMouseUp, false );
  452. }
  453. function onMouseMove( event ) {
  454. var currentValue = scope.value;
  455. pointer = [ event.clientX, event.clientY ];
  456. distance += ( pointer[ 0 ] - prevPointer[ 0 ] ) - ( pointer[ 1 ] - prevPointer[ 1 ] );
  457. var value = onMouseDownValue + ( distance / ( event.shiftKey ? 5 : 50 ) ) * scope.step;
  458. value = Math.min( scope.max, Math.max( scope.min, value ) ) | 0;
  459. if ( currentValue !== value ) {
  460. scope.setValue( value );
  461. dom.dispatchEvent( changeEvent );
  462. }
  463. prevPointer = [ event.clientX, event.clientY ];
  464. }
  465. function onMouseUp( event ) {
  466. document.removeEventListener( 'mousemove', onMouseMove, false );
  467. document.removeEventListener( 'mouseup', onMouseUp, false );
  468. if ( Math.abs( distance ) < 2 ) {
  469. dom.focus();
  470. dom.select();
  471. }
  472. }
  473. function onChange( event ) {
  474. scope.setValue( dom.value );
  475. }
  476. function onFocus( event ) {
  477. dom.style.backgroundColor = '';
  478. dom.style.cursor = '';
  479. }
  480. function onBlur( event ) {
  481. dom.style.backgroundColor = 'transparent';
  482. dom.style.cursor = 'col-resize';
  483. }
  484. onBlur();
  485. dom.addEventListener( 'mousedown', onMouseDown, false );
  486. dom.addEventListener( 'change', onChange, false );
  487. dom.addEventListener( 'focus', onFocus, false );
  488. dom.addEventListener( 'blur', onBlur, false );
  489. return this;
  490. };
  491. UI.Integer.prototype = Object.create( UI.Element.prototype );
  492. UI.Integer.prototype.constructor = UI.Integer;
  493. UI.Integer.prototype.getValue = function () {
  494. return this.value;
  495. };
  496. UI.Integer.prototype.setValue = function ( value ) {
  497. if ( value !== undefined ) {
  498. value = parseInt( value );
  499. this.value = value;
  500. this.dom.value = value;
  501. }
  502. return this;
  503. };
  504. UI.Integer.prototype.setStep = function ( step ) {
  505. this.step = parseInt( step );
  506. return this;
  507. };
  508. UI.Integer.prototype.setRange = function ( min, max ) {
  509. this.min = min;
  510. this.max = max;
  511. return this;
  512. };
  513. // Break
  514. UI.Break = function () {
  515. UI.Element.call( this );
  516. var dom = document.createElement( 'br' );
  517. dom.className = 'Break';
  518. this.dom = dom;
  519. return this;
  520. };
  521. UI.Break.prototype = Object.create( UI.Element.prototype );
  522. UI.Break.prototype.constructor = UI.Break;
  523. // HorizontalRule
  524. UI.HorizontalRule = function () {
  525. UI.Element.call( this );
  526. var dom = document.createElement( 'hr' );
  527. dom.className = 'HorizontalRule';
  528. this.dom = dom;
  529. return this;
  530. };
  531. UI.HorizontalRule.prototype = Object.create( UI.Element.prototype );
  532. UI.HorizontalRule.prototype.constructor = UI.HorizontalRule;
  533. // Button
  534. UI.Button = function ( value ) {
  535. UI.Element.call( this );
  536. var dom = document.createElement( 'button' );
  537. dom.className = 'Button';
  538. this.dom = dom;
  539. this.dom.textContent = value;
  540. return this;
  541. };
  542. UI.Button.prototype = Object.create( UI.Element.prototype );
  543. UI.Button.prototype.constructor = UI.Button;
  544. UI.Button.prototype.setLabel = function ( value ) {
  545. this.dom.textContent = value;
  546. return this;
  547. };
  548. // Listbox
  549. UI.Listbox = function ( ) {
  550. UI.Element.call( this );
  551. var dom = document.createElement( 'div' );
  552. dom.className = 'Listbox';
  553. dom.tabIndex = 0;
  554. this.dom = dom;
  555. this.items = [];
  556. this.listitems = [];
  557. this.selectedIndex = 0;
  558. this.selectedValue = null;
  559. return this;
  560. }
  561. UI.Listbox.prototype = Object.create( UI.Element.prototype );
  562. UI.Listbox.prototype.constructor = UI.ListboxItem;
  563. UI.Listbox.prototype.setItems = function ( items ) {
  564. if ( Array.isArray( items ) ) {
  565. this.items = items;
  566. }
  567. this.render( );
  568. }
  569. UI.Listbox.prototype.render = function ( ) {
  570. while( this.listitems.length ) {
  571. var item = this.listitems[0];
  572. item.dom.remove();
  573. this.listitems.splice(0, 1);
  574. }
  575. for ( var i = 0; i < this.items.length; i ++ ) {
  576. var item = this.items[i];
  577. var listitem = new UI.Listbox.ListboxItem( this );
  578. listitem.setId( item.id || `Listbox-${i}` );
  579. listitem.setTextContent( item.name || item.type );
  580. this.add( listitem );
  581. }
  582. }
  583. // Assuming user passes valid list items
  584. UI.Listbox.prototype.add = function ( ) {
  585. var items = Array.from( arguments );
  586. this.listitems = this.listitems.concat( items );
  587. UI.Element.prototype.add.apply( this, items );
  588. }
  589. UI.Listbox.prototype.selectIndex = function ( index ) {
  590. if ( index >= 0 && index < this.items.length ) {
  591. this.setValue( this.listitems[ index ].getId( ) );
  592. }
  593. this.selectIndex = index;
  594. }
  595. UI.Listbox.prototype.getValue = function ( index ) {
  596. return this.selectedValue;
  597. }
  598. UI.Listbox.prototype.setValue = function ( value ) {
  599. for ( var i = 0; i < this.listitems.length; i ++ ) {
  600. var element = this.listitems[ i ];
  601. if ( element.getId( ) === value ) {
  602. element.addClass( 'active' );
  603. } else {
  604. element.removeClass( 'active' );
  605. }
  606. }
  607. this.selectedValue = value;
  608. var changeEvent = document.createEvent( 'HTMLEvents' );
  609. changeEvent.initEvent( 'change', true, true );
  610. this.dom.dispatchEvent( changeEvent );
  611. }
  612. // Listbox Item
  613. UI.Listbox.ListboxItem = function ( parent ) {
  614. UI.Element.call( this );
  615. var dom = document.createElement( 'div' );
  616. dom.className = 'ListboxItem';
  617. this.parent = parent;
  618. this.dom = dom;
  619. var scope = this;
  620. function onClick ( ) {
  621. if( scope.parent ) {
  622. scope.parent.setValue( scope.getId( ) );
  623. }
  624. }
  625. dom.addEventListener( 'click', onClick, false );
  626. return this;
  627. }
  628. UI.Listbox.ListboxItem.prototype = Object.create( UI.Element.prototype );
  629. UI.Listbox.ListboxItem.prototype.constructor = UI.Listbox.ListboxItem;