ui.js 20 KB

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