ui.js 19 KB

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