ui.js 16 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004
  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. this.step = parseInt( step );
  475. return this;
  476. };
  477. UI.Integer.prototype.setRange = function ( min, max ) {
  478. this.min = min;
  479. this.max = max;
  480. return this;
  481. };
  482. // Break
  483. UI.Break = function () {
  484. UI.Element.call( this );
  485. var dom = document.createElement( 'br' );
  486. dom.className = 'Break';
  487. this.dom = dom;
  488. return this;
  489. };
  490. UI.Break.prototype = Object.create( UI.Element.prototype );
  491. UI.Break.prototype.constructor = UI.Break;
  492. // HorizontalRule
  493. UI.HorizontalRule = function () {
  494. UI.Element.call( this );
  495. var dom = document.createElement( 'hr' );
  496. dom.className = 'HorizontalRule';
  497. this.dom = dom;
  498. return this;
  499. };
  500. UI.HorizontalRule.prototype = Object.create( UI.Element.prototype );
  501. UI.HorizontalRule.prototype.constructor = UI.HorizontalRule;
  502. // Button
  503. UI.Button = function ( value ) {
  504. UI.Element.call( this );
  505. var dom = document.createElement( 'button' );
  506. dom.className = 'Button';
  507. this.dom = dom;
  508. this.dom.textContent = value;
  509. return this;
  510. };
  511. UI.Button.prototype = Object.create( UI.Element.prototype );
  512. UI.Button.prototype.constructor = UI.Button;
  513. UI.Button.prototype.setLabel = function ( value ) {
  514. this.dom.textContent = value;
  515. return this;
  516. };
  517. // Modal
  518. UI.Modal = function ( value ) {
  519. var scope = this;
  520. var dom = document.createElement( 'div' );
  521. dom.style.position = 'absolute';
  522. dom.style.width = '100%';
  523. dom.style.height = '100%';
  524. dom.style.backgroundColor = 'rgba(0,0,0,0.5)';
  525. dom.style.display = 'none';
  526. dom.style.alignItems = 'center';
  527. dom.style.justifyContent = 'center';
  528. dom.addEventListener( 'click', function ( event ) {
  529. scope.hide();
  530. } );
  531. this.dom = dom;
  532. this.container = new UI.Panel();
  533. this.container.dom.style.width = '200px';
  534. this.container.dom.style.padding = '20px';
  535. this.container.dom.style.backgroundColor = '#ffffff';
  536. this.container.dom.style.boxShadow = '0px 5px 10px rgba(0,0,0,0.5)';
  537. this.add( this.container );
  538. return this;
  539. };
  540. UI.Modal.prototype = Object.create( UI.Element.prototype );
  541. UI.Modal.prototype.constructor = UI.Modal;
  542. UI.Modal.prototype.show = function ( content ) {
  543. this.container.clear();
  544. this.container.add( content );
  545. this.dom.style.display = 'flex';
  546. return this;
  547. };
  548. UI.Modal.prototype.hide = function () {
  549. this.dom.style.display = 'none';
  550. return this;
  551. };