ui.js 17 KB

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