ui.js 16 KB

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