ui.js 15 KB

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