ui.js 20 KB

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