ui.js 20 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202
  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. Sortable.create( dom, {
  282. onUpdate: function ( event ) {
  283. console.log( event );
  284. }
  285. } );
  286. // Broadcast for object selection after arrow navigation
  287. var changeEvent = document.createEvent('HTMLEvents');
  288. changeEvent.initEvent( 'change', true, true );
  289. // Prevent native scroll behavior
  290. dom.addEventListener( 'keydown', function (event) {
  291. switch ( event.keyCode ) {
  292. case 38: // up
  293. case 40: // down
  294. event.preventDefault();
  295. event.stopPropagation();
  296. break;
  297. }
  298. }, false);
  299. // Keybindings to support arrow navigation
  300. dom.addEventListener( 'keyup', function (event) {
  301. switch ( event.keyCode ) {
  302. case 38: // up
  303. case 40: // down
  304. scope.selectedIndex += ( event.keyCode == 38 ) ? -1 : 1;
  305. if ( scope.selectedIndex >= 0 && scope.selectedIndex < scope.options.length ) {
  306. // Highlight selected dom elem and scroll parent if needed
  307. scope.setValue( scope.options[ scope.selectedIndex ].value );
  308. scope.dom.dispatchEvent( changeEvent );
  309. }
  310. break;
  311. }
  312. }, false);
  313. this.dom = dom;
  314. this.options = [];
  315. this.selectedIndex = -1;
  316. this.selectedValue = null;
  317. return this;
  318. };
  319. UI.FancySelect.prototype = Object.create( UI.Element.prototype );
  320. UI.FancySelect.prototype.constructor = UI.FancySelect;
  321. UI.FancySelect.prototype.setOptions = function ( options ) {
  322. var scope = this;
  323. var changeEvent = document.createEvent( 'HTMLEvents' );
  324. changeEvent.initEvent( 'change', true, true );
  325. while ( scope.dom.children.length > 0 ) {
  326. scope.dom.removeChild( scope.dom.firstChild );
  327. }
  328. scope.options = [];
  329. for ( var i = 0; i < options.length; i ++ ) {
  330. var option = options[ i ];
  331. var div = document.createElement( 'div' );
  332. div.className = 'option';
  333. div.innerHTML = option.html;
  334. div.value = option.value;
  335. scope.dom.appendChild( div );
  336. scope.options.push( div );
  337. div.addEventListener( 'click', function ( event ) {
  338. scope.setValue( this.value );
  339. scope.dom.dispatchEvent( changeEvent );
  340. }, false );
  341. }
  342. return scope;
  343. };
  344. UI.FancySelect.prototype.getValue = function () {
  345. return this.selectedValue;
  346. };
  347. UI.FancySelect.prototype.setValue = function ( value ) {
  348. for ( var i = 0; i < this.options.length; i ++ ) {
  349. var element = this.options[ i ];
  350. if ( element.value === value ) {
  351. element.classList.add( 'active' );
  352. // scroll into view
  353. var y = element.offsetTop - this.dom.offsetTop;
  354. var bottomY = y + element.offsetHeight;
  355. var minScroll = bottomY - this.dom.offsetHeight;
  356. if ( this.dom.scrollTop > y ) {
  357. this.dom.scrollTop = y
  358. } else if ( this.dom.scrollTop < minScroll ) {
  359. this.dom.scrollTop = minScroll;
  360. }
  361. this.selectedIndex = i;
  362. } else {
  363. element.classList.remove( 'active' );
  364. }
  365. }
  366. this.selectedValue = value;
  367. return this;
  368. };
  369. // Checkbox
  370. UI.Checkbox = function ( boolean ) {
  371. UI.Element.call( this );
  372. var scope = this;
  373. var dom = document.createElement( 'input' );
  374. dom.className = 'Checkbox';
  375. dom.type = 'checkbox';
  376. this.dom = dom;
  377. this.setValue( boolean );
  378. return this;
  379. };
  380. UI.Checkbox.prototype = Object.create( UI.Element.prototype );
  381. UI.Checkbox.prototype.constructor = UI.Checkbox;
  382. UI.Checkbox.prototype.getValue = function () {
  383. return this.dom.checked;
  384. };
  385. UI.Checkbox.prototype.setValue = function ( value ) {
  386. if ( value !== undefined ) {
  387. this.dom.checked = value;
  388. }
  389. return this;
  390. };
  391. // Color
  392. UI.Color = function () {
  393. UI.Element.call( this );
  394. var scope = this;
  395. var dom = document.createElement( 'input' );
  396. dom.className = 'Color';
  397. dom.style.width = '64px';
  398. dom.style.height = '16px';
  399. dom.style.border = '0px';
  400. dom.style.padding = '0px';
  401. dom.style.backgroundColor = 'transparent';
  402. try {
  403. dom.type = 'color';
  404. dom.value = '#ffffff';
  405. } catch ( exception ) {}
  406. this.dom = dom;
  407. return this;
  408. };
  409. UI.Color.prototype = Object.create( UI.Element.prototype );
  410. UI.Color.prototype.constructor = UI.Color;
  411. UI.Color.prototype.getValue = function () {
  412. return this.dom.value;
  413. };
  414. UI.Color.prototype.getHexValue = function () {
  415. return parseInt( this.dom.value.substr( 1 ), 16 );
  416. };
  417. UI.Color.prototype.setValue = function ( value ) {
  418. this.dom.value = value;
  419. return this;
  420. };
  421. UI.Color.prototype.setHexValue = function ( hex ) {
  422. this.dom.value = '#' + ( '000000' + hex.toString( 16 ) ).slice( -6 );
  423. return this;
  424. };
  425. // Number
  426. UI.Number = function ( number ) {
  427. UI.Element.call( this );
  428. var scope = this;
  429. var dom = document.createElement( 'input' );
  430. dom.className = 'Number';
  431. dom.value = '0.00';
  432. dom.addEventListener( 'keydown', function ( event ) {
  433. event.stopPropagation();
  434. if ( event.keyCode === 13 ) dom.blur();
  435. }, false );
  436. this.min = - Infinity;
  437. this.max = Infinity;
  438. this.precision = 2;
  439. this.step = 1;
  440. this.dom = dom;
  441. this.setValue( number );
  442. var changeEvent = document.createEvent( 'HTMLEvents' );
  443. changeEvent.initEvent( 'change', true, true );
  444. var distance = 0;
  445. var onMouseDownValue = 0;
  446. var pointer = new THREE.Vector2();
  447. var prevPointer = new THREE.Vector2();
  448. var onMouseDown = function ( event ) {
  449. event.preventDefault();
  450. distance = 0;
  451. onMouseDownValue = parseFloat( dom.value );
  452. prevPointer.set( event.clientX, event.clientY );
  453. document.addEventListener( 'mousemove', onMouseMove, false );
  454. document.addEventListener( 'mouseup', onMouseUp, false );
  455. };
  456. var onMouseMove = function ( event ) {
  457. var currentValue = dom.value;
  458. pointer.set( event.clientX, event.clientY );
  459. distance += ( pointer.x - prevPointer.x ) - ( pointer.y - prevPointer.y );
  460. var number = onMouseDownValue + ( distance / ( event.shiftKey ? 5 : 50 ) ) * scope.step;
  461. dom.value = Math.min( scope.max, Math.max( scope.min, number ) ).toFixed( scope.precision );
  462. if ( currentValue !== dom.value ) dom.dispatchEvent( changeEvent );
  463. prevPointer.set( event.clientX, event.clientY );
  464. };
  465. var onMouseUp = function ( 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. var onChange = function ( event ) {
  474. var value = 0;
  475. try {
  476. value = eval( dom.value );
  477. } catch ( error ) {
  478. console.error( error.message );
  479. }
  480. dom.value = parseFloat( value );
  481. };
  482. var onFocus = function ( event ) {
  483. dom.style.backgroundColor = '';
  484. dom.style.borderColor = '#ccc';
  485. dom.style.cursor = '';
  486. };
  487. var onBlur = function ( event ) {
  488. dom.style.backgroundColor = 'transparent';
  489. dom.style.borderColor = 'transparent';
  490. dom.style.cursor = 'col-resize';
  491. };
  492. dom.addEventListener( 'mousedown', onMouseDown, false );
  493. dom.addEventListener( 'change', onChange, false );
  494. dom.addEventListener( 'focus', onFocus, false );
  495. dom.addEventListener( 'blur', onBlur, false );
  496. return this;
  497. };
  498. UI.Number.prototype = Object.create( UI.Element.prototype );
  499. UI.Number.prototype.constructor = UI.Number;
  500. UI.Number.prototype.getValue = function () {
  501. return parseFloat( this.dom.value );
  502. };
  503. UI.Number.prototype.setValue = function ( value ) {
  504. if ( value !== undefined ) {
  505. this.dom.value = value.toFixed( this.precision );
  506. }
  507. return this;
  508. };
  509. UI.Number.prototype.setRange = function ( min, max ) {
  510. this.min = min;
  511. this.max = max;
  512. return this;
  513. };
  514. UI.Number.prototype.setPrecision = function ( precision ) {
  515. this.precision = precision;
  516. return this;
  517. };
  518. // Integer
  519. UI.Integer = function ( number ) {
  520. UI.Element.call( this );
  521. var scope = this;
  522. var dom = document.createElement( 'input' );
  523. dom.className = 'Number';
  524. dom.value = '0.00';
  525. dom.addEventListener( 'keydown', function ( event ) {
  526. event.stopPropagation();
  527. }, false );
  528. this.min = - Infinity;
  529. this.max = Infinity;
  530. this.step = 1;
  531. this.dom = dom;
  532. this.setValue( number );
  533. var changeEvent = document.createEvent( 'HTMLEvents' );
  534. changeEvent.initEvent( 'change', true, true );
  535. var distance = 0;
  536. var onMouseDownValue = 0;
  537. var pointer = new THREE.Vector2();
  538. var prevPointer = new THREE.Vector2();
  539. var onMouseDown = function ( event ) {
  540. event.preventDefault();
  541. distance = 0;
  542. onMouseDownValue = parseFloat( dom.value );
  543. prevPointer.set( event.clientX, event.clientY );
  544. document.addEventListener( 'mousemove', onMouseMove, false );
  545. document.addEventListener( 'mouseup', onMouseUp, false );
  546. };
  547. var onMouseMove = function ( event ) {
  548. var currentValue = dom.value;
  549. pointer.set( event.clientX, event.clientY );
  550. distance += ( pointer.x - prevPointer.x ) - ( pointer.y - prevPointer.y );
  551. var number = onMouseDownValue + ( distance / ( event.shiftKey ? 5 : 50 ) ) * scope.step;
  552. dom.value = Math.min( scope.max, Math.max( scope.min, number ) ) | 0;
  553. if ( currentValue !== dom.value ) dom.dispatchEvent( changeEvent );
  554. prevPointer.set( event.clientX, event.clientY );
  555. };
  556. var onMouseUp = function ( event ) {
  557. document.removeEventListener( 'mousemove', onMouseMove, false );
  558. document.removeEventListener( 'mouseup', onMouseUp, false );
  559. if ( Math.abs( distance ) < 2 ) {
  560. dom.focus();
  561. dom.select();
  562. }
  563. };
  564. var onChange = function ( event ) {
  565. var value = 0;
  566. try {
  567. value = eval( dom.value );
  568. } catch ( error ) {
  569. console.error( error.message );
  570. }
  571. dom.value = parseInt( value );
  572. };
  573. var onFocus = function ( event ) {
  574. dom.style.backgroundColor = '';
  575. dom.style.borderColor = '#ccc';
  576. dom.style.cursor = '';
  577. };
  578. var onBlur = function ( event ) {
  579. dom.style.backgroundColor = 'transparent';
  580. dom.style.borderColor = 'transparent';
  581. dom.style.cursor = 'col-resize';
  582. };
  583. dom.addEventListener( 'mousedown', onMouseDown, false );
  584. dom.addEventListener( 'change', onChange, false );
  585. dom.addEventListener( 'focus', onFocus, false );
  586. dom.addEventListener( 'blur', onBlur, false );
  587. return this;
  588. };
  589. UI.Integer.prototype = Object.create( UI.Element.prototype );
  590. UI.Integer.prototype.constructor = UI.Integer;
  591. UI.Integer.prototype.getValue = function () {
  592. return parseInt( this.dom.value );
  593. };
  594. UI.Integer.prototype.setValue = function ( value ) {
  595. if ( value !== undefined ) {
  596. this.dom.value = value | 0;
  597. }
  598. return this;
  599. };
  600. UI.Integer.prototype.setRange = function ( min, max ) {
  601. this.min = min;
  602. this.max = max;
  603. return this;
  604. };
  605. // Break
  606. UI.Break = function () {
  607. UI.Element.call( this );
  608. var dom = document.createElement( 'br' );
  609. dom.className = 'Break';
  610. this.dom = dom;
  611. return this;
  612. };
  613. UI.Break.prototype = Object.create( UI.Element.prototype );
  614. UI.Break.prototype.constructor = UI.Break;
  615. // HorizontalRule
  616. UI.HorizontalRule = function () {
  617. UI.Element.call( this );
  618. var dom = document.createElement( 'hr' );
  619. dom.className = 'HorizontalRule';
  620. this.dom = dom;
  621. return this;
  622. };
  623. UI.HorizontalRule.prototype = Object.create( UI.Element.prototype );
  624. UI.HorizontalRule.prototype.constructor = UI.HorizontalRule;
  625. // Button
  626. UI.Button = function ( value ) {
  627. UI.Element.call( this );
  628. var scope = this;
  629. var dom = document.createElement( 'button' );
  630. dom.className = 'Button';
  631. this.dom = dom;
  632. this.dom.textContent = value;
  633. return this;
  634. };
  635. UI.Button.prototype = Object.create( UI.Element.prototype );
  636. UI.Button.prototype.constructor = UI.Button;
  637. UI.Button.prototype.setLabel = function ( value ) {
  638. this.dom.textContent = value;
  639. return this;
  640. };
  641. // Dialog
  642. UI.Dialog = function ( value ) {
  643. var scope = this;
  644. var dom = document.createElement( 'dialog' );
  645. if ( dom.showModal === undefined ) {
  646. // fallback
  647. dom = document.createElement( 'div' );
  648. dom.style.display = 'none';
  649. dom.showModal = function () {
  650. dom.style.position = 'absolute';
  651. dom.style.left = '100px';
  652. dom.style.top = '100px';
  653. dom.style.zIndex = 1;
  654. dom.style.display = '';
  655. };
  656. }
  657. dom.className = 'Dialog';
  658. this.dom = dom;
  659. return this;
  660. };
  661. UI.Dialog.prototype = Object.create( UI.Panel.prototype );
  662. UI.Dialog.prototype.constructor = UI.Dialog;
  663. UI.Dialog.prototype.showModal = function () {
  664. this.dom.showModal();
  665. return this;
  666. };