ui.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964
  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.style.color = '#0080f0';
  334. dom.style.fontSize = '12px';
  335. dom.style.backgroundColor = 'transparent';
  336. dom.style.border = '1px solid transparent';
  337. dom.style.padding = '2px';
  338. dom.style.cursor = 'col-resize';
  339. dom.value = '0.00';
  340. dom.addEventListener( 'keydown', function ( event ) {
  341. event.stopPropagation();
  342. if ( event.keyCode === 13 ) dom.blur();
  343. }, false );
  344. this.min = - Infinity;
  345. this.max = Infinity;
  346. this.precision = 2;
  347. this.step = 1;
  348. this.dom = dom;
  349. this.setValue( number );
  350. var changeEvent = document.createEvent( 'HTMLEvents' );
  351. changeEvent.initEvent( 'change', true, true );
  352. var distance = 0;
  353. var onMouseDownValue = 0;
  354. var onMouseDown = function ( event ) {
  355. event.preventDefault();
  356. distance = 0;
  357. onMouseDownValue = parseFloat( dom.value );
  358. document.addEventListener( 'mousemove', onMouseMove, false );
  359. document.addEventListener( 'mouseup', onMouseUp, false );
  360. };
  361. var onMouseMove = function ( event ) {
  362. var currentValue = dom.value;
  363. var movementX = event.movementX || event.webkitMovementX || event.mozMovementX || 0;
  364. var movementY = event.movementY || event.webkitMovementY || event.mozMovementY || 0;
  365. distance += movementX - movementY;
  366. var number = onMouseDownValue + ( distance / ( event.shiftKey ? 5 : 50 ) ) * scope.step;
  367. dom.value = Math.min( scope.max, Math.max( scope.min, number ) ).toFixed( scope.precision );
  368. if ( currentValue !== dom.value ) dom.dispatchEvent( changeEvent );
  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 number = parseFloat( dom.value );
  380. if ( isNaN( number ) === false ) {
  381. dom.value = number;
  382. }
  383. };
  384. var onFocus = function ( event ) {
  385. dom.style.backgroundColor = '';
  386. dom.style.borderColor = '#ccc';
  387. dom.style.cursor = '';
  388. };
  389. var onBlur = function ( event ) {
  390. dom.style.backgroundColor = 'transparent';
  391. dom.style.borderColor = 'transparent';
  392. dom.style.cursor = 'col-resize';
  393. };
  394. dom.addEventListener( 'mousedown', onMouseDown, false );
  395. dom.addEventListener( 'change', onChange, false );
  396. dom.addEventListener( 'focus', onFocus, false );
  397. dom.addEventListener( 'blur', onBlur, false );
  398. return this;
  399. };
  400. UI.Number.prototype = Object.create( UI.Element.prototype );
  401. UI.Number.prototype.getValue = function () {
  402. return parseFloat( this.dom.value );
  403. };
  404. UI.Number.prototype.setValue = function ( value ) {
  405. if ( value !== undefined ) {
  406. this.dom.value = value.toFixed( this.precision );
  407. }
  408. return this;
  409. };
  410. UI.Number.prototype.setRange = function ( min, max ) {
  411. this.min = min;
  412. this.max = max;
  413. return this;
  414. };
  415. UI.Number.prototype.setPrecision = function ( precision ) {
  416. this.precision = precision;
  417. return this;
  418. };
  419. // Integer
  420. UI.Integer = function ( number ) {
  421. UI.Element.call( this );
  422. var scope = this;
  423. var dom = document.createElement( 'input' );
  424. dom.className = 'Number';
  425. dom.style.color = '#0080f0';
  426. dom.style.fontSize = '12px';
  427. dom.style.backgroundColor = 'transparent';
  428. dom.style.border = '1px solid transparent';
  429. dom.style.padding = '2px';
  430. dom.style.cursor = 'col-resize';
  431. dom.value = '0.00';
  432. dom.addEventListener( 'keydown', function ( event ) {
  433. event.stopPropagation();
  434. }, false );
  435. this.min = - Infinity;
  436. this.max = Infinity;
  437. this.step = 1;
  438. this.dom = dom;
  439. this.setValue( number );
  440. var changeEvent = document.createEvent( 'HTMLEvents' );
  441. changeEvent.initEvent( 'change', true, true );
  442. var distance = 0;
  443. var onMouseDownValue = 0;
  444. var onMouseDown = function ( event ) {
  445. event.preventDefault();
  446. distance = 0;
  447. onMouseDownValue = parseFloat( dom.value );
  448. document.addEventListener( 'mousemove', onMouseMove, false );
  449. document.addEventListener( 'mouseup', onMouseUp, false );
  450. };
  451. var onMouseMove = function ( event ) {
  452. var currentValue = dom.value;
  453. var movementX = event.movementX || event.webkitMovementX || event.mozMovementX || 0;
  454. var movementY = event.movementY || event.webkitMovementY || event.mozMovementY || 0;
  455. distance += movementX - movementY;
  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. };
  460. var onMouseUp = function ( event ) {
  461. document.removeEventListener( 'mousemove', onMouseMove, false );
  462. document.removeEventListener( 'mouseup', onMouseUp, false );
  463. if ( Math.abs( distance ) < 2 ) {
  464. dom.focus();
  465. dom.select();
  466. }
  467. };
  468. var onChange = function ( event ) {
  469. var number = parseInt( dom.value );
  470. if ( isNaN( number ) === false ) {
  471. dom.value = number;
  472. }
  473. };
  474. var onFocus = function ( event ) {
  475. dom.style.backgroundColor = '';
  476. dom.style.borderColor = '#ccc';
  477. dom.style.cursor = '';
  478. };
  479. var onBlur = function ( event ) {
  480. dom.style.backgroundColor = 'transparent';
  481. dom.style.borderColor = 'transparent';
  482. dom.style.cursor = 'col-resize';
  483. };
  484. dom.addEventListener( 'mousedown', onMouseDown, false );
  485. dom.addEventListener( 'change', onChange, false );
  486. dom.addEventListener( 'focus', onFocus, false );
  487. dom.addEventListener( 'blur', onBlur, false );
  488. return this;
  489. };
  490. UI.Integer.prototype = Object.create( UI.Element.prototype );
  491. UI.Integer.prototype.getValue = function () {
  492. return parseInt( this.dom.value );
  493. };
  494. UI.Integer.prototype.setValue = function ( value ) {
  495. if ( value !== undefined ) {
  496. this.dom.value = value | 0;
  497. }
  498. return this;
  499. };
  500. UI.Integer.prototype.setRange = function ( min, max ) {
  501. this.min = min;
  502. this.max = max;
  503. return this;
  504. };
  505. // Break
  506. UI.Break = function () {
  507. UI.Element.call( this );
  508. var dom = document.createElement( 'br' );
  509. dom.className = 'Break';
  510. this.dom = dom;
  511. return this;
  512. };
  513. UI.Break.prototype = Object.create( UI.Element.prototype );
  514. // HorizontalRule
  515. UI.HorizontalRule = function () {
  516. UI.Element.call( this );
  517. var dom = document.createElement( 'hr' );
  518. dom.className = 'HorizontalRule';
  519. this.dom = dom;
  520. return this;
  521. };
  522. UI.HorizontalRule.prototype = Object.create( UI.Element.prototype );
  523. // Button
  524. UI.Button = function ( value ) {
  525. UI.Element.call( this );
  526. var scope = this;
  527. var dom = document.createElement( 'button' );
  528. dom.className = 'Button';
  529. this.dom = dom;
  530. this.dom.textContent = value;
  531. return this;
  532. };
  533. UI.Button.prototype = Object.create( UI.Element.prototype );
  534. UI.Button.prototype.setLabel = function ( value ) {
  535. this.dom.textContent = value;
  536. return this;
  537. };