ui.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005
  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. add: function () {
  10. for ( var i = 0; i < arguments.length; i ++ ) {
  11. var argument = arguments[ i ];
  12. if ( argument instanceof UI.Element ) {
  13. this.dom.appendChild( argument.dom );
  14. } else {
  15. console.error( 'UI.Element:', argument, 'is not an instance of UI.Element.' );
  16. }
  17. }
  18. return this;
  19. },
  20. remove: function () {
  21. for ( var i = 0; i < arguments.length; i ++ ) {
  22. var argument = arguments[ i ];
  23. if ( argument instanceof UI.Element ) {
  24. this.dom.removeChild( argument.dom );
  25. } else {
  26. console.error( 'UI.Element:', argument, 'is not an instance of UI.Element.' );
  27. }
  28. }
  29. return this;
  30. },
  31. clear: function () {
  32. while ( this.dom.children.length ) {
  33. this.dom.removeChild( this.dom.lastChild );
  34. }
  35. },
  36. setId: function ( id ) {
  37. this.dom.id = id;
  38. return this;
  39. },
  40. setClass: function ( name ) {
  41. this.dom.className = name;
  42. return this;
  43. },
  44. setStyle: function ( style, array ) {
  45. for ( var i = 0; i < array.length; i ++ ) {
  46. this.dom.style[ style ] = array[ i ];
  47. }
  48. return this;
  49. },
  50. setDisabled: function ( value ) {
  51. this.dom.disabled = value;
  52. return this;
  53. },
  54. setTextContent: function ( value ) {
  55. this.dom.textContent = value;
  56. return this;
  57. }
  58. };
  59. // properties
  60. var properties = [ 'position', 'left', 'top', 'right', 'bottom', 'width', 'height', 'border', 'borderLeft',
  61. 'borderTop', 'borderRight', 'borderBottom', 'borderColor', 'display', 'overflow', 'margin', 'marginLeft', 'marginTop', 'marginRight', 'marginBottom', 'padding', 'paddingLeft', 'paddingTop', 'paddingRight', 'paddingBottom', 'color',
  62. 'background', 'backgroundColor', 'opacity', 'fontSize', 'fontWeight', 'textAlign', 'textDecoration', 'textTransform', 'cursor', 'zIndex' ];
  63. properties.forEach( function ( property ) {
  64. var method = 'set' + property.substr( 0, 1 ).toUpperCase() + property.substr( 1, property.length );
  65. UI.Element.prototype[ method ] = function () {
  66. this.setStyle( property, arguments );
  67. return this;
  68. };
  69. } );
  70. // events
  71. var events = [ 'KeyUp', 'KeyDown', 'MouseOver', 'MouseOut', 'Click', 'DblClick', 'Change' ];
  72. events.forEach( function ( event ) {
  73. var method = 'on' + event;
  74. UI.Element.prototype[ method ] = function ( callback ) {
  75. this.dom.addEventListener( event.toLowerCase(), callback.bind( this ), false );
  76. return this;
  77. };
  78. } );
  79. // Span
  80. UI.Span = function () {
  81. UI.Element.call( this );
  82. this.dom = document.createElement( 'span' );
  83. return this;
  84. };
  85. UI.Span.prototype = Object.create( UI.Element.prototype );
  86. UI.Span.prototype.constructor = UI.Span;
  87. // Div
  88. UI.Div = function () {
  89. UI.Element.call( this );
  90. this.dom = document.createElement( 'div' );
  91. return this;
  92. };
  93. UI.Div.prototype = Object.create( UI.Element.prototype );
  94. UI.Div.prototype.constructor = UI.Div;
  95. // Row
  96. UI.Row = function () {
  97. UI.Element.call( this );
  98. var dom = document.createElement( 'div' );
  99. dom.className = 'Row';
  100. this.dom = dom;
  101. return this;
  102. };
  103. UI.Row.prototype = Object.create( UI.Element.prototype );
  104. UI.Row.prototype.constructor = UI.Row;
  105. // Panel
  106. UI.Panel = function () {
  107. UI.Element.call( this );
  108. var dom = document.createElement( 'div' );
  109. dom.className = 'Panel';
  110. this.dom = dom;
  111. return this;
  112. };
  113. UI.Panel.prototype = Object.create( UI.Element.prototype );
  114. UI.Panel.prototype.constructor = UI.Panel;
  115. // Text
  116. UI.Text = function ( text ) {
  117. UI.Element.call( this );
  118. var dom = document.createElement( 'span' );
  119. dom.className = 'Text';
  120. dom.style.cursor = 'default';
  121. dom.style.display = 'inline-block';
  122. dom.style.verticalAlign = 'middle';
  123. this.dom = dom;
  124. this.setValue( text );
  125. return this;
  126. };
  127. UI.Text.prototype = Object.create( UI.Element.prototype );
  128. UI.Text.prototype.constructor = UI.Text;
  129. UI.Text.prototype.getValue = function () {
  130. return this.dom.textContent;
  131. };
  132. UI.Text.prototype.setValue = function ( value ) {
  133. if ( value !== undefined ) {
  134. this.dom.textContent = value;
  135. }
  136. return this;
  137. };
  138. // Input
  139. UI.Input = function ( text ) {
  140. UI.Element.call( this );
  141. var scope = this;
  142. var dom = document.createElement( 'input' );
  143. dom.className = 'Input';
  144. dom.style.padding = '2px';
  145. dom.style.border = '1px solid transparent';
  146. dom.addEventListener( 'keydown', function ( event ) {
  147. event.stopPropagation();
  148. }, false );
  149. this.dom = dom;
  150. this.setValue( text );
  151. return this;
  152. };
  153. UI.Input.prototype = Object.create( UI.Element.prototype );
  154. UI.Input.prototype.constructor = UI.Input;
  155. UI.Input.prototype.getValue = function () {
  156. return this.dom.value;
  157. };
  158. UI.Input.prototype.setValue = function ( value ) {
  159. this.dom.value = value;
  160. return this;
  161. };
  162. // TextArea
  163. UI.TextArea = function () {
  164. UI.Element.call( this );
  165. var scope = this;
  166. var dom = document.createElement( 'textarea' );
  167. dom.className = 'TextArea';
  168. dom.style.padding = '2px';
  169. dom.spellcheck = false;
  170. dom.addEventListener( 'keydown', function ( event ) {
  171. event.stopPropagation();
  172. if ( event.keyCode === 9 ) {
  173. event.preventDefault();
  174. var cursor = dom.selectionStart;
  175. dom.value = dom.value.substring( 0, cursor ) + '\t' + dom.value.substring( cursor );
  176. dom.selectionStart = cursor + 1;
  177. dom.selectionEnd = dom.selectionStart;
  178. }
  179. }, false );
  180. this.dom = dom;
  181. return this;
  182. };
  183. UI.TextArea.prototype = Object.create( UI.Element.prototype );
  184. UI.TextArea.prototype.constructor = UI.TextArea;
  185. UI.TextArea.prototype.getValue = function () {
  186. return this.dom.value;
  187. };
  188. UI.TextArea.prototype.setValue = function ( value ) {
  189. this.dom.value = value;
  190. return this;
  191. };
  192. // Select
  193. UI.Select = function () {
  194. UI.Element.call( this );
  195. var scope = this;
  196. var dom = document.createElement( 'select' );
  197. dom.className = 'Select';
  198. dom.style.padding = '2px';
  199. this.dom = dom;
  200. return this;
  201. };
  202. UI.Select.prototype = Object.create( UI.Element.prototype );
  203. UI.Select.prototype.constructor = UI.Select;
  204. UI.Select.prototype.setMultiple = function ( boolean ) {
  205. this.dom.multiple = boolean;
  206. return this;
  207. };
  208. UI.Select.prototype.setOptions = function ( options ) {
  209. var selected = this.dom.value;
  210. while ( this.dom.children.length > 0 ) {
  211. this.dom.removeChild( this.dom.firstChild );
  212. }
  213. for ( var key in options ) {
  214. var option = document.createElement( 'option' );
  215. option.value = key;
  216. option.innerHTML = options[ key ];
  217. this.dom.appendChild( option );
  218. }
  219. this.dom.value = selected;
  220. return this;
  221. };
  222. UI.Select.prototype.getValue = function () {
  223. return this.dom.value;
  224. };
  225. UI.Select.prototype.setValue = function ( value ) {
  226. value = String( value );
  227. if ( this.dom.value !== value ) {
  228. this.dom.value = value;
  229. }
  230. return this;
  231. };
  232. // Checkbox
  233. UI.Checkbox = function ( boolean ) {
  234. UI.Element.call( this );
  235. var scope = this;
  236. var dom = document.createElement( 'input' );
  237. dom.className = 'Checkbox';
  238. dom.type = 'checkbox';
  239. this.dom = dom;
  240. this.setValue( boolean );
  241. return this;
  242. };
  243. UI.Checkbox.prototype = Object.create( UI.Element.prototype );
  244. UI.Checkbox.prototype.constructor = UI.Checkbox;
  245. UI.Checkbox.prototype.getValue = function () {
  246. return this.dom.checked;
  247. };
  248. UI.Checkbox.prototype.setValue = function ( value ) {
  249. if ( value !== undefined ) {
  250. this.dom.checked = value;
  251. }
  252. return this;
  253. };
  254. // Color
  255. UI.Color = function () {
  256. UI.Element.call( this );
  257. var scope = this;
  258. var dom = document.createElement( 'input' );
  259. dom.className = 'Color';
  260. dom.style.width = '64px';
  261. dom.style.height = '17px';
  262. dom.style.border = '0px';
  263. dom.style.padding = '2px';
  264. dom.style.backgroundColor = 'transparent';
  265. try {
  266. dom.type = 'color';
  267. dom.value = '#ffffff';
  268. } catch ( exception ) {}
  269. this.dom = dom;
  270. return this;
  271. };
  272. UI.Color.prototype = Object.create( UI.Element.prototype );
  273. UI.Color.prototype.constructor = UI.Color;
  274. UI.Color.prototype.getValue = function () {
  275. return this.dom.value;
  276. };
  277. UI.Color.prototype.getHexValue = function () {
  278. return parseInt( this.dom.value.substr( 1 ), 16 );
  279. };
  280. UI.Color.prototype.setValue = function ( value ) {
  281. this.dom.value = value;
  282. return this;
  283. };
  284. UI.Color.prototype.setHexValue = function ( hex ) {
  285. this.dom.value = '#' + ( '000000' + hex.toString( 16 ) ).slice( - 6 );
  286. return this;
  287. };
  288. // Number
  289. UI.Number = function ( number ) {
  290. UI.Element.call( this );
  291. var scope = this;
  292. var dom = document.createElement( 'input' );
  293. dom.className = 'Number';
  294. dom.value = '0.00';
  295. dom.addEventListener( 'keydown', function ( event ) {
  296. event.stopPropagation();
  297. if ( event.keyCode === 13 ) dom.blur();
  298. }, false );
  299. this.value = 0;
  300. this.min = - Infinity;
  301. this.max = Infinity;
  302. this.precision = 2;
  303. this.step = 1;
  304. this.unit = '';
  305. this.dom = dom;
  306. this.setValue( number );
  307. var changeEvent = document.createEvent( 'HTMLEvents' );
  308. changeEvent.initEvent( 'change', true, true );
  309. var distance = 0;
  310. var onMouseDownValue = 0;
  311. var pointer = [ 0, 0 ];
  312. var prevPointer = [ 0, 0 ];
  313. function onMouseDown( event ) {
  314. event.preventDefault();
  315. distance = 0;
  316. onMouseDownValue = scope.value;
  317. prevPointer = [ event.clientX, event.clientY ];
  318. document.addEventListener( 'mousemove', onMouseMove, false );
  319. document.addEventListener( 'mouseup', onMouseUp, false );
  320. }
  321. function onMouseMove( event ) {
  322. var currentValue = scope.value;
  323. pointer = [ event.clientX, event.clientY ];
  324. distance += ( pointer[ 0 ] - prevPointer[ 0 ] ) - ( pointer[ 1 ] - prevPointer[ 1 ] );
  325. var value = onMouseDownValue + ( distance / ( event.shiftKey ? 5 : 50 ) ) * scope.step;
  326. value = Math.min( scope.max, Math.max( scope.min, value ) );
  327. if ( currentValue !== value ) {
  328. scope.setValue( value );
  329. dom.dispatchEvent( changeEvent );
  330. }
  331. prevPointer = [ event.clientX, event.clientY ];
  332. }
  333. function onMouseUp( event ) {
  334. document.removeEventListener( 'mousemove', onMouseMove, false );
  335. document.removeEventListener( 'mouseup', onMouseUp, false );
  336. if ( Math.abs( distance ) < 2 ) {
  337. dom.focus();
  338. dom.select();
  339. }
  340. }
  341. function onChange( event ) {
  342. scope.setValue( dom.value );
  343. }
  344. function onFocus( event ) {
  345. dom.style.backgroundColor = '';
  346. dom.style.cursor = '';
  347. }
  348. function onBlur( event ) {
  349. dom.style.backgroundColor = 'transparent';
  350. dom.style.cursor = 'col-resize';
  351. }
  352. onBlur();
  353. dom.addEventListener( 'mousedown', onMouseDown, false );
  354. dom.addEventListener( 'change', onChange, false );
  355. dom.addEventListener( 'focus', onFocus, false );
  356. dom.addEventListener( 'blur', onBlur, false );
  357. return this;
  358. };
  359. UI.Number.prototype = Object.create( UI.Element.prototype );
  360. UI.Number.prototype.constructor = UI.Number;
  361. UI.Number.prototype.getValue = function () {
  362. return this.value;
  363. };
  364. UI.Number.prototype.setValue = function ( value ) {
  365. if ( value !== undefined ) {
  366. value = parseFloat( value );
  367. if ( value < this.min ) value = this.min;
  368. if ( value > this.max ) value = this.max;
  369. this.value = value;
  370. this.dom.value = value.toFixed( this.precision );
  371. if ( this.unit !== '' ) this.dom.value += ' ' + this.unit;
  372. }
  373. return this;
  374. };
  375. UI.Number.prototype.setPrecision = function ( precision ) {
  376. this.precision = precision;
  377. return this;
  378. };
  379. UI.Number.prototype.setStep = function ( step ) {
  380. this.step = step;
  381. return this;
  382. };
  383. UI.Number.prototype.setRange = function ( min, max ) {
  384. this.min = min;
  385. this.max = max;
  386. return this;
  387. };
  388. UI.Number.prototype.setUnit = function ( unit ) {
  389. this.unit = unit;
  390. return this;
  391. };
  392. // Integer
  393. UI.Integer = function ( number ) {
  394. UI.Element.call( this );
  395. var scope = this;
  396. var dom = document.createElement( 'input' );
  397. dom.className = 'Number';
  398. dom.value = '0';
  399. dom.addEventListener( 'keydown', function ( event ) {
  400. event.stopPropagation();
  401. }, false );
  402. this.value = 0;
  403. this.min = - Infinity;
  404. this.max = Infinity;
  405. this.step = 1;
  406. this.dom = dom;
  407. this.setValue( number );
  408. var changeEvent = document.createEvent( 'HTMLEvents' );
  409. changeEvent.initEvent( 'change', true, true );
  410. var distance = 0;
  411. var onMouseDownValue = 0;
  412. var pointer = [ 0, 0 ];
  413. var prevPointer = [ 0, 0 ];
  414. function onMouseDown( event ) {
  415. event.preventDefault();
  416. distance = 0;
  417. onMouseDownValue = scope.value;
  418. prevPointer = [ event.clientX, event.clientY ];
  419. document.addEventListener( 'mousemove', onMouseMove, false );
  420. document.addEventListener( 'mouseup', onMouseUp, false );
  421. }
  422. function onMouseMove( event ) {
  423. var currentValue = scope.value;
  424. pointer = [ event.clientX, event.clientY ];
  425. distance += ( pointer[ 0 ] - prevPointer[ 0 ] ) - ( pointer[ 1 ] - prevPointer[ 1 ] );
  426. var value = onMouseDownValue + ( distance / ( event.shiftKey ? 5 : 50 ) ) * scope.step;
  427. value = Math.min( scope.max, Math.max( scope.min, value ) ) | 0;
  428. if ( currentValue !== value ) {
  429. scope.setValue( value );
  430. dom.dispatchEvent( changeEvent );
  431. }
  432. prevPointer = [ event.clientX, event.clientY ];
  433. }
  434. function onMouseUp( event ) {
  435. document.removeEventListener( 'mousemove', onMouseMove, false );
  436. document.removeEventListener( 'mouseup', onMouseUp, false );
  437. if ( Math.abs( distance ) < 2 ) {
  438. dom.focus();
  439. dom.select();
  440. }
  441. }
  442. function onChange( event ) {
  443. scope.setValue( dom.value );
  444. }
  445. function onFocus( event ) {
  446. dom.style.backgroundColor = '';
  447. dom.style.cursor = '';
  448. }
  449. function onBlur( event ) {
  450. dom.style.backgroundColor = 'transparent';
  451. dom.style.cursor = 'col-resize';
  452. }
  453. onBlur();
  454. dom.addEventListener( 'mousedown', onMouseDown, false );
  455. dom.addEventListener( 'change', onChange, false );
  456. dom.addEventListener( 'focus', onFocus, false );
  457. dom.addEventListener( 'blur', onBlur, false );
  458. return this;
  459. };
  460. UI.Integer.prototype = Object.create( UI.Element.prototype );
  461. UI.Integer.prototype.constructor = UI.Integer;
  462. UI.Integer.prototype.getValue = function () {
  463. return this.value;
  464. };
  465. UI.Integer.prototype.setValue = function ( value ) {
  466. if ( value !== undefined ) {
  467. value = parseInt( value );
  468. this.value = value;
  469. this.dom.value = value;
  470. }
  471. return this;
  472. };
  473. UI.Integer.prototype.setStep = function ( step ) {
  474. step = Math.max(step, Math.floor(step)); // Checks that step is an integer
  475. this.step = step;
  476. return this;
  477. };
  478. UI.Integer.prototype.setRange = function ( min, max ) {
  479. this.min = min;
  480. this.max = max;
  481. return this;
  482. };
  483. // Break
  484. UI.Break = function () {
  485. UI.Element.call( this );
  486. var dom = document.createElement( 'br' );
  487. dom.className = 'Break';
  488. this.dom = dom;
  489. return this;
  490. };
  491. UI.Break.prototype = Object.create( UI.Element.prototype );
  492. UI.Break.prototype.constructor = UI.Break;
  493. // HorizontalRule
  494. UI.HorizontalRule = function () {
  495. UI.Element.call( this );
  496. var dom = document.createElement( 'hr' );
  497. dom.className = 'HorizontalRule';
  498. this.dom = dom;
  499. return this;
  500. };
  501. UI.HorizontalRule.prototype = Object.create( UI.Element.prototype );
  502. UI.HorizontalRule.prototype.constructor = UI.HorizontalRule;
  503. // Button
  504. UI.Button = function ( value ) {
  505. UI.Element.call( this );
  506. var dom = document.createElement( 'button' );
  507. dom.className = 'Button';
  508. this.dom = dom;
  509. this.dom.textContent = value;
  510. return this;
  511. };
  512. UI.Button.prototype = Object.create( UI.Element.prototype );
  513. UI.Button.prototype.constructor = UI.Button;
  514. UI.Button.prototype.setLabel = function ( value ) {
  515. this.dom.textContent = value;
  516. return this;
  517. };
  518. // Modal
  519. UI.Modal = function ( value ) {
  520. var scope = this;
  521. var dom = document.createElement( 'div' );
  522. dom.style.position = 'absolute';
  523. dom.style.width = '100%';
  524. dom.style.height = '100%';
  525. dom.style.backgroundColor = 'rgba(0,0,0,0.5)';
  526. dom.style.display = 'none';
  527. dom.style.alignItems = 'center';
  528. dom.style.justifyContent = 'center';
  529. dom.addEventListener( 'click', function ( event ) {
  530. scope.hide();
  531. } );
  532. this.dom = dom;
  533. this.container = new UI.Panel();
  534. this.container.dom.style.width = '200px';
  535. this.container.dom.style.padding = '20px';
  536. this.container.dom.style.backgroundColor = '#ffffff';
  537. this.container.dom.style.boxShadow = '0px 5px 10px rgba(0,0,0,0.5)';
  538. this.add( this.container );
  539. return this;
  540. };
  541. UI.Modal.prototype = Object.create( UI.Element.prototype );
  542. UI.Modal.prototype.constructor = UI.Modal;
  543. UI.Modal.prototype.show = function ( content ) {
  544. this.container.clear();
  545. this.container.add( content );
  546. this.dom.style.display = 'flex';
  547. return this;
  548. };
  549. UI.Modal.prototype.hide = function () {
  550. this.dom.style.display = 'none';
  551. return this;
  552. };