ui.js 20 KB

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