ui.js 16 KB

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