ui.js 20 KB

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