ui.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997
  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 onTouchStart( event ) {
  342. if ( event.touches.length === 1 ) {
  343. distance = 0;
  344. onMouseDownValue = scope.value;
  345. prevPointer = [ event.touches[ 0 ].pageX, event.touches[ 0 ].pageY ];
  346. document.addEventListener( 'touchmove', onTouchMove, false );
  347. document.addEventListener( 'touchend', onTouchEnd, false );
  348. }
  349. }
  350. function onTouchMove( event ) {
  351. var currentValue = scope.value;
  352. pointer = [ event.touches[ 0 ].pageX, event.touches[ 0 ].pageY ];
  353. distance += ( pointer[ 0 ] - prevPointer[ 0 ] ) - ( pointer[ 1 ] - prevPointer[ 1 ] );
  354. var value = onMouseDownValue + ( distance / ( event.shiftKey ? 5 : 50 ) ) * scope.step;
  355. value = Math.min( scope.max, Math.max( scope.min, value ) );
  356. if ( currentValue !== value ) {
  357. scope.setValue( value );
  358. dom.dispatchEvent( changeEvent );
  359. }
  360. prevPointer = [ event.touches[ 0 ].pageX, event.touches[ 0 ].pageY ];
  361. }
  362. function onTouchEnd( event ) {
  363. if ( event.touches.length === 0 ) {
  364. document.removeEventListener( 'touchmove', onTouchMove, false );
  365. document.removeEventListener( 'touchend', onTouchEnd, false );
  366. }
  367. }
  368. function onChange( event ) {
  369. scope.setValue( dom.value );
  370. }
  371. function onFocus( event ) {
  372. dom.style.backgroundColor = '';
  373. dom.style.cursor = '';
  374. }
  375. function onBlur( event ) {
  376. dom.style.backgroundColor = 'transparent';
  377. dom.style.cursor = 'col-resize';
  378. }
  379. onBlur();
  380. dom.addEventListener( 'mousedown', onMouseDown, false );
  381. dom.addEventListener( 'touchstart', onTouchStart, false );
  382. dom.addEventListener( 'change', onChange, false );
  383. dom.addEventListener( 'focus', onFocus, false );
  384. dom.addEventListener( 'blur', onBlur, false );
  385. return this;
  386. };
  387. UI.Number.prototype = Object.create( UI.Element.prototype );
  388. UI.Number.prototype.constructor = UI.Number;
  389. UI.Number.prototype.getValue = function () {
  390. return this.value;
  391. };
  392. UI.Number.prototype.setValue = function ( value ) {
  393. if ( value !== undefined ) {
  394. value = parseFloat( value );
  395. if ( value < this.min ) value = this.min;
  396. if ( value > this.max ) value = this.max;
  397. this.value = value;
  398. this.dom.value = value.toFixed( this.precision );
  399. if ( this.unit !== '' ) this.dom.value += ' ' + this.unit;
  400. }
  401. return this;
  402. };
  403. UI.Number.prototype.setPrecision = function ( precision ) {
  404. this.precision = precision;
  405. return this;
  406. };
  407. UI.Number.prototype.setStep = function ( step ) {
  408. this.step = step;
  409. return this;
  410. };
  411. UI.Number.prototype.setRange = function ( min, max ) {
  412. this.min = min;
  413. this.max = max;
  414. return this;
  415. };
  416. UI.Number.prototype.setUnit = function ( unit ) {
  417. this.unit = unit;
  418. return this;
  419. };
  420. // Integer
  421. UI.Integer = function ( number ) {
  422. UI.Element.call( this );
  423. var scope = this;
  424. var dom = document.createElement( 'input' );
  425. dom.className = 'Number';
  426. dom.value = '0';
  427. dom.addEventListener( 'keydown', function ( event ) {
  428. event.stopPropagation();
  429. }, false );
  430. this.value = 0;
  431. this.min = - Infinity;
  432. this.max = Infinity;
  433. this.step = 1;
  434. this.dom = dom;
  435. this.setValue( number );
  436. var changeEvent = document.createEvent( 'HTMLEvents' );
  437. changeEvent.initEvent( 'change', true, true );
  438. var distance = 0;
  439. var onMouseDownValue = 0;
  440. var pointer = [ 0, 0 ];
  441. var prevPointer = [ 0, 0 ];
  442. function onMouseDown( event ) {
  443. event.preventDefault();
  444. distance = 0;
  445. onMouseDownValue = scope.value;
  446. prevPointer = [ event.clientX, event.clientY ];
  447. document.addEventListener( 'mousemove', onMouseMove, false );
  448. document.addEventListener( 'mouseup', onMouseUp, false );
  449. }
  450. function onMouseMove( event ) {
  451. var currentValue = scope.value;
  452. pointer = [ event.clientX, event.clientY ];
  453. distance += ( pointer[ 0 ] - prevPointer[ 0 ] ) - ( pointer[ 1 ] - prevPointer[ 1 ] );
  454. var value = onMouseDownValue + ( distance / ( event.shiftKey ? 5 : 50 ) ) * scope.step;
  455. value = Math.min( scope.max, Math.max( scope.min, value ) ) | 0;
  456. if ( currentValue !== value ) {
  457. scope.setValue( value );
  458. dom.dispatchEvent( changeEvent );
  459. }
  460. prevPointer = [ event.clientX, event.clientY ];
  461. }
  462. function onMouseUp( event ) {
  463. document.removeEventListener( 'mousemove', onMouseMove, false );
  464. document.removeEventListener( 'mouseup', onMouseUp, false );
  465. if ( Math.abs( distance ) < 2 ) {
  466. dom.focus();
  467. dom.select();
  468. }
  469. }
  470. function onChange( event ) {
  471. scope.setValue( dom.value );
  472. }
  473. function onFocus( event ) {
  474. dom.style.backgroundColor = '';
  475. dom.style.cursor = '';
  476. }
  477. function onBlur( event ) {
  478. dom.style.backgroundColor = 'transparent';
  479. dom.style.cursor = 'col-resize';
  480. }
  481. onBlur();
  482. dom.addEventListener( 'mousedown', onMouseDown, false );
  483. dom.addEventListener( 'change', onChange, false );
  484. dom.addEventListener( 'focus', onFocus, false );
  485. dom.addEventListener( 'blur', onBlur, false );
  486. return this;
  487. };
  488. UI.Integer.prototype = Object.create( UI.Element.prototype );
  489. UI.Integer.prototype.constructor = UI.Integer;
  490. UI.Integer.prototype.getValue = function () {
  491. return this.value;
  492. };
  493. UI.Integer.prototype.setValue = function ( value ) {
  494. if ( value !== undefined ) {
  495. value = parseInt( value );
  496. this.value = value;
  497. this.dom.value = value;
  498. }
  499. return this;
  500. };
  501. UI.Integer.prototype.setStep = function ( step ) {
  502. this.step = parseInt( step );
  503. return this;
  504. };
  505. UI.Integer.prototype.setRange = function ( min, max ) {
  506. this.min = min;
  507. this.max = max;
  508. return this;
  509. };
  510. // Break
  511. UI.Break = function () {
  512. UI.Element.call( this );
  513. var dom = document.createElement( 'br' );
  514. dom.className = 'Break';
  515. this.dom = dom;
  516. return this;
  517. };
  518. UI.Break.prototype = Object.create( UI.Element.prototype );
  519. UI.Break.prototype.constructor = UI.Break;
  520. // HorizontalRule
  521. UI.HorizontalRule = function () {
  522. UI.Element.call( this );
  523. var dom = document.createElement( 'hr' );
  524. dom.className = 'HorizontalRule';
  525. this.dom = dom;
  526. return this;
  527. };
  528. UI.HorizontalRule.prototype = Object.create( UI.Element.prototype );
  529. UI.HorizontalRule.prototype.constructor = UI.HorizontalRule;
  530. // Button
  531. UI.Button = function ( value ) {
  532. UI.Element.call( this );
  533. var dom = document.createElement( 'button' );
  534. dom.className = 'Button';
  535. this.dom = dom;
  536. this.dom.textContent = value;
  537. return this;
  538. };
  539. UI.Button.prototype = Object.create( UI.Element.prototype );
  540. UI.Button.prototype.constructor = UI.Button;
  541. UI.Button.prototype.setLabel = function ( value ) {
  542. this.dom.textContent = value;
  543. return this;
  544. };