ui.js 17 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043
  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 transparent';
  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.spellcheck = false;
  212. dom.addEventListener( 'keydown', function ( event ) {
  213. event.stopPropagation();
  214. if ( event.keyCode === 9 ) {
  215. event.preventDefault();
  216. var cursor = dom.selectionStart;
  217. dom.value = dom.value.substring( 0, cursor ) + '\t' + dom.value.substring( cursor );
  218. dom.selectionStart = cursor + 1;
  219. dom.selectionEnd = dom.selectionStart;
  220. }
  221. }, false );
  222. this.dom = dom;
  223. return this;
  224. };
  225. UI.TextArea.prototype = Object.create( UI.Element.prototype );
  226. UI.TextArea.prototype.constructor = UI.TextArea;
  227. UI.TextArea.prototype.getValue = function () {
  228. return this.dom.value;
  229. };
  230. UI.TextArea.prototype.setValue = function ( value ) {
  231. this.dom.value = value;
  232. return this;
  233. };
  234. // Select
  235. UI.Select = function () {
  236. UI.Element.call( this );
  237. var scope = this;
  238. var dom = document.createElement( 'select' );
  239. dom.className = 'Select';
  240. dom.style.padding = '2px';
  241. this.dom = dom;
  242. return this;
  243. };
  244. UI.Select.prototype = Object.create( UI.Element.prototype );
  245. UI.Select.prototype.constructor = UI.Select;
  246. UI.Select.prototype.setMultiple = function ( boolean ) {
  247. this.dom.multiple = boolean;
  248. return this;
  249. };
  250. UI.Select.prototype.setOptions = function ( options ) {
  251. var selected = this.dom.value;
  252. while ( this.dom.children.length > 0 ) {
  253. this.dom.removeChild( this.dom.firstChild );
  254. }
  255. for ( var key in options ) {
  256. var option = document.createElement( 'option' );
  257. option.value = key;
  258. option.innerHTML = options[ key ];
  259. this.dom.appendChild( option );
  260. }
  261. this.dom.value = selected;
  262. return this;
  263. };
  264. UI.Select.prototype.getValue = function () {
  265. return this.dom.value;
  266. };
  267. UI.Select.prototype.setValue = function ( value ) {
  268. value = String( value );
  269. if ( this.dom.value !== value ) {
  270. this.dom.value = value;
  271. }
  272. return this;
  273. };
  274. // Checkbox
  275. UI.Checkbox = function ( boolean ) {
  276. UI.Element.call( this );
  277. var scope = this;
  278. var dom = document.createElement( 'input' );
  279. dom.className = 'Checkbox';
  280. dom.type = 'checkbox';
  281. this.dom = dom;
  282. this.setValue( boolean );
  283. return this;
  284. };
  285. UI.Checkbox.prototype = Object.create( UI.Element.prototype );
  286. UI.Checkbox.prototype.constructor = UI.Checkbox;
  287. UI.Checkbox.prototype.getValue = function () {
  288. return this.dom.checked;
  289. };
  290. UI.Checkbox.prototype.setValue = function ( value ) {
  291. if ( value !== undefined ) {
  292. this.dom.checked = value;
  293. }
  294. return this;
  295. };
  296. // Color
  297. UI.Color = function () {
  298. UI.Element.call( this );
  299. var scope = this;
  300. var dom = document.createElement( 'input' );
  301. dom.className = 'Color';
  302. dom.style.width = '64px';
  303. dom.style.height = '16px';
  304. dom.style.border = '0px';
  305. dom.style.padding = '0px';
  306. dom.style.backgroundColor = 'transparent';
  307. try {
  308. dom.type = 'color';
  309. dom.value = '#ffffff';
  310. } catch ( exception ) {}
  311. this.dom = dom;
  312. return this;
  313. };
  314. UI.Color.prototype = Object.create( UI.Element.prototype );
  315. UI.Color.prototype.constructor = UI.Color;
  316. UI.Color.prototype.getValue = function () {
  317. return this.dom.value;
  318. };
  319. UI.Color.prototype.getHexValue = function () {
  320. return parseInt( this.dom.value.substr( 1 ), 16 );
  321. };
  322. UI.Color.prototype.setValue = function ( value ) {
  323. this.dom.value = value;
  324. return this;
  325. };
  326. UI.Color.prototype.setHexValue = function ( hex ) {
  327. this.dom.value = '#' + ( '000000' + hex.toString( 16 ) ).slice( -6 );
  328. return this;
  329. };
  330. // Number
  331. UI.Number = function ( number ) {
  332. UI.Element.call( this );
  333. var scope = this;
  334. var dom = document.createElement( 'input' );
  335. dom.className = 'Number';
  336. dom.value = '0.00';
  337. dom.addEventListener( 'keydown', function ( event ) {
  338. event.stopPropagation();
  339. if ( event.keyCode === 13 ) dom.blur();
  340. }, false );
  341. this.min = - Infinity;
  342. this.max = Infinity;
  343. this.precision = 2;
  344. this.step = 1;
  345. this.dom = dom;
  346. this.setValue( number );
  347. var changeEvent = document.createEvent( 'HTMLEvents' );
  348. changeEvent.initEvent( 'change', true, true );
  349. var distance = 0;
  350. var onMouseDownValue = 0;
  351. var pointer = [ 0, 0 ];
  352. var prevPointer = [ 0, 0 ];
  353. var onMouseDown = function ( event ) {
  354. event.preventDefault();
  355. distance = 0;
  356. onMouseDownValue = parseFloat( dom.value );
  357. prevPointer = [ event.clientX, event.clientY ];
  358. document.addEventListener( 'mousemove', onMouseMove, false );
  359. document.addEventListener( 'mouseup', onMouseUp, false );
  360. };
  361. var onMouseMove = function ( event ) {
  362. var currentValue = dom.value;
  363. pointer = [ event.clientX, event.clientY ];
  364. distance += ( pointer[ 0 ] - prevPointer[ 0 ] ) - ( pointer[ 1 ] - prevPointer[ 1 ] );
  365. var number = onMouseDownValue + ( distance / ( event.shiftKey ? 5 : 50 ) ) * scope.step;
  366. dom.value = Math.min( scope.max, Math.max( scope.min, number ) ).toFixed( scope.precision );
  367. if ( currentValue !== dom.value ) dom.dispatchEvent( changeEvent );
  368. prevPointer = [ event.clientX, event.clientY ];
  369. };
  370. var onMouseUp = function ( event ) {
  371. document.removeEventListener( 'mousemove', onMouseMove, false );
  372. document.removeEventListener( 'mouseup', onMouseUp, false );
  373. if ( Math.abs( distance ) < 2 ) {
  374. dom.focus();
  375. dom.select();
  376. }
  377. };
  378. var onChange = function ( event ) {
  379. var value = 0;
  380. try {
  381. value = eval( dom.value );
  382. } catch ( error ) {
  383. console.error( error.message );
  384. }
  385. dom.value = parseFloat( value );
  386. };
  387. var onFocus = function ( event ) {
  388. dom.style.backgroundColor = '';
  389. dom.style.borderColor = '#ccc';
  390. dom.style.cursor = '';
  391. };
  392. var onBlur = function ( event ) {
  393. dom.style.backgroundColor = 'transparent';
  394. dom.style.borderColor = 'transparent';
  395. dom.style.cursor = 'col-resize';
  396. };
  397. dom.addEventListener( 'mousedown', onMouseDown, false );
  398. dom.addEventListener( 'change', onChange, false );
  399. dom.addEventListener( 'focus', onFocus, false );
  400. dom.addEventListener( 'blur', onBlur, false );
  401. return this;
  402. };
  403. UI.Number.prototype = Object.create( UI.Element.prototype );
  404. UI.Number.prototype.constructor = UI.Number;
  405. UI.Number.prototype.getValue = function () {
  406. return parseFloat( this.dom.value );
  407. };
  408. UI.Number.prototype.setValue = function ( value ) {
  409. if ( value !== undefined ) {
  410. this.dom.value = value.toFixed( this.precision );
  411. }
  412. return this;
  413. };
  414. UI.Number.prototype.setRange = function ( min, max ) {
  415. this.min = min;
  416. this.max = max;
  417. return this;
  418. };
  419. UI.Number.prototype.setPrecision = function ( precision ) {
  420. this.precision = precision;
  421. return this;
  422. };
  423. // Integer
  424. UI.Integer = function ( number ) {
  425. UI.Element.call( this );
  426. var scope = this;
  427. var dom = document.createElement( 'input' );
  428. dom.className = 'Number';
  429. dom.value = '0.00';
  430. dom.addEventListener( 'keydown', function ( event ) {
  431. event.stopPropagation();
  432. }, false );
  433. this.min = - Infinity;
  434. this.max = Infinity;
  435. this.step = 1;
  436. this.dom = dom;
  437. this.setValue( number );
  438. var changeEvent = document.createEvent( 'HTMLEvents' );
  439. changeEvent.initEvent( 'change', true, true );
  440. var distance = 0;
  441. var onMouseDownValue = 0;
  442. var pointer = [ 0, 0 ];
  443. var prevPointer = [ 0, 0 ];
  444. var onMouseDown = function ( event ) {
  445. event.preventDefault();
  446. distance = 0;
  447. onMouseDownValue = parseFloat( dom.value );
  448. prevPointer = [ event.clientX, event.clientY ];
  449. document.addEventListener( 'mousemove', onMouseMove, false );
  450. document.addEventListener( 'mouseup', onMouseUp, false );
  451. };
  452. var onMouseMove = function ( event ) {
  453. var currentValue = dom.value;
  454. pointer = [ event.clientX, event.clientY ];
  455. distance += ( pointer[ 0 ] - prevPointer[ 0 ] ) - ( pointer[ 1 ] - prevPointer[ 1 ] );
  456. var number = onMouseDownValue + ( distance / ( event.shiftKey ? 5 : 50 ) ) * scope.step;
  457. dom.value = Math.min( scope.max, Math.max( scope.min, number ) ) | 0;
  458. if ( currentValue !== dom.value ) dom.dispatchEvent( changeEvent );
  459. prevPointer = [ event.clientX, event.clientY ];
  460. };
  461. var onMouseUp = function ( event ) {
  462. document.removeEventListener( 'mousemove', onMouseMove, false );
  463. document.removeEventListener( 'mouseup', onMouseUp, false );
  464. if ( Math.abs( distance ) < 2 ) {
  465. dom.focus();
  466. dom.select();
  467. }
  468. };
  469. var onChange = function ( event ) {
  470. var value = 0;
  471. try {
  472. value = eval( dom.value );
  473. } catch ( error ) {
  474. console.error( error.message );
  475. }
  476. dom.value = parseInt( value );
  477. };
  478. var onFocus = function ( event ) {
  479. dom.style.backgroundColor = '';
  480. dom.style.borderColor = '#ccc';
  481. dom.style.cursor = '';
  482. };
  483. var onBlur = function ( event ) {
  484. dom.style.backgroundColor = 'transparent';
  485. dom.style.borderColor = 'transparent';
  486. dom.style.cursor = 'col-resize';
  487. };
  488. dom.addEventListener( 'mousedown', onMouseDown, false );
  489. dom.addEventListener( 'change', onChange, false );
  490. dom.addEventListener( 'focus', onFocus, false );
  491. dom.addEventListener( 'blur', onBlur, false );
  492. return this;
  493. };
  494. UI.Integer.prototype = Object.create( UI.Element.prototype );
  495. UI.Integer.prototype.constructor = UI.Integer;
  496. UI.Integer.prototype.getValue = function () {
  497. return parseInt( this.dom.value );
  498. };
  499. UI.Integer.prototype.setValue = function ( value ) {
  500. if ( value !== undefined ) {
  501. this.dom.value = value | 0;
  502. }
  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 scope = this;
  534. var dom = document.createElement( 'button' );
  535. dom.className = 'Button';
  536. this.dom = dom;
  537. this.dom.textContent = value;
  538. return this;
  539. };
  540. UI.Button.prototype = Object.create( UI.Element.prototype );
  541. UI.Button.prototype.constructor = UI.Button;
  542. UI.Button.prototype.setLabel = function ( value ) {
  543. this.dom.textContent = value;
  544. return this;
  545. };
  546. // Dialog
  547. UI.Dialog = function ( value ) {
  548. var scope = this;
  549. var dom = document.createElement( 'dialog' );
  550. if ( dom.showModal === undefined ) {
  551. // fallback
  552. dom = document.createElement( 'div' );
  553. dom.style.display = 'none';
  554. dom.showModal = function () {
  555. dom.style.position = 'absolute';
  556. dom.style.left = '100px';
  557. dom.style.top = '100px';
  558. dom.style.zIndex = 1;
  559. dom.style.display = '';
  560. };
  561. }
  562. dom.className = 'Dialog';
  563. this.dom = dom;
  564. return this;
  565. };
  566. UI.Dialog.prototype = Object.create( UI.Panel.prototype );
  567. UI.Dialog.prototype.constructor = UI.Dialog;
  568. UI.Dialog.prototype.showModal = function () {
  569. this.dom.showModal();
  570. return this;
  571. };