ui.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945
  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. // Keybindings to support arrow navigation
  188. dom.addEventListener( 'keyup', function (event) {
  189. switch ( event.keyCode ) {
  190. case 38: // up
  191. case 40: // down
  192. var index = scope.indexOf( scope.selectedValue );
  193. index += ( event.keyCode == 38 ) ? -1 : 1;
  194. if ( index >= 0 && index < scope.options.length ) {
  195. // Select dom element
  196. scope.setValue( scope.options[index].value );
  197. // Invoke object selection logic
  198. scope.dom.dispatchEvent( changeEvent );
  199. }
  200. break;
  201. }
  202. }, false);
  203. this.dom = dom;
  204. this.options = [];
  205. this.selectedValue = null;
  206. return this;
  207. };
  208. UI.FancySelect.prototype = Object.create( UI.Element.prototype );
  209. UI.FancySelect.prototype.setOptions = function ( options ) {
  210. var scope = this;
  211. var changeEvent = document.createEvent( 'HTMLEvents' );
  212. changeEvent.initEvent( 'change', true, true );
  213. while ( scope.dom.children.length > 0 ) {
  214. scope.dom.removeChild( scope.dom.firstChild );
  215. }
  216. scope.options = [];
  217. for ( var key in options ) {
  218. var option = document.createElement( 'div' );
  219. option.style.padding = '4px';
  220. option.style.whiteSpace = 'nowrap';
  221. option.innerHTML = options[ key ];
  222. option.value = key;
  223. scope.dom.appendChild( option );
  224. scope.options.push( option );
  225. option.addEventListener( 'click', function ( event ) {
  226. scope.setValue( this.value );
  227. scope.dom.dispatchEvent( changeEvent );
  228. }, false );
  229. }
  230. return scope;
  231. };
  232. UI.FancySelect.prototype.getValue = function () {
  233. return this.selectedValue;
  234. };
  235. UI.FancySelect.prototype.setValue = function ( value ) {
  236. if ( typeof value === 'number' ) value = value.toString();
  237. for ( var i = 0; i < this.options.length; i ++ ) {
  238. var element = this.options[ i ];
  239. if ( element.value === value ) {
  240. element.style.backgroundColor = '#f0f0f0';
  241. // scroll into view
  242. var y = i * element.clientHeight;
  243. var scrollTop = this.dom.scrollTop;
  244. var domHeight = this.dom.clientHeight;
  245. if ( y < scrollTop || y > scrollTop + domHeight ) {
  246. this.dom.scrollTop = y;
  247. }
  248. } else {
  249. element.style.backgroundColor = '';
  250. }
  251. }
  252. this.selectedValue = value;
  253. return this;
  254. };
  255. UI.FancySelect.prototype.indexOf = function ( key ) {
  256. if ( typeof key === 'number' ) key = key.toString();
  257. // Iterate options and return the index of the first occurrence of the key
  258. for ( var i = 0; i < this.options.length; i++ ) {
  259. var element = this.options[i];
  260. if ( element.value === key ) {
  261. test = element;
  262. return i;
  263. }
  264. }
  265. return -1
  266. };
  267. // Checkbox
  268. UI.Checkbox = function ( boolean ) {
  269. UI.Element.call( this );
  270. var scope = this;
  271. var dom = document.createElement( 'input' );
  272. dom.className = 'Checkbox';
  273. dom.type = 'checkbox';
  274. this.dom = dom;
  275. this.setValue( boolean );
  276. return this;
  277. };
  278. UI.Checkbox.prototype = Object.create( UI.Element.prototype );
  279. UI.Checkbox.prototype.getValue = function () {
  280. return this.dom.checked;
  281. };
  282. UI.Checkbox.prototype.setValue = function ( value ) {
  283. if ( value !== undefined ) {
  284. this.dom.checked = value;
  285. }
  286. return this;
  287. };
  288. // Color
  289. UI.Color = function () {
  290. UI.Element.call( this );
  291. var scope = this;
  292. var dom = document.createElement( 'input' );
  293. dom.className = 'Color';
  294. dom.style.width = '64px';
  295. dom.style.height = '16px';
  296. dom.style.border = '0px';
  297. dom.style.padding = '0px';
  298. dom.style.backgroundColor = 'transparent';
  299. try { dom.type = 'color'; } catch ( exception ) {}
  300. this.dom = dom;
  301. return this;
  302. };
  303. UI.Color.prototype = Object.create( UI.Element.prototype );
  304. UI.Color.prototype.getValue = function () {
  305. return this.dom.value;
  306. };
  307. UI.Color.prototype.getHexValue = function () {
  308. return parseInt( this.dom.value.substr( 1 ), 16 );
  309. };
  310. UI.Color.prototype.setValue = function ( value ) {
  311. this.dom.value = value;
  312. return this;
  313. };
  314. UI.Color.prototype.setHexValue = function ( hex ) {
  315. this.dom.value = "#" + ( '000000' + hex.toString( 16 ) ).slice( -6 );
  316. return this;
  317. };
  318. // Number
  319. UI.Number = function ( number ) {
  320. UI.Element.call( this );
  321. var scope = this;
  322. var dom = document.createElement( 'input' );
  323. dom.className = 'Number';
  324. dom.style.color = '#0080f0';
  325. dom.style.fontSize = '12px';
  326. dom.style.backgroundColor = 'transparent';
  327. dom.style.border = '1px solid transparent';
  328. dom.style.padding = '2px';
  329. dom.style.cursor = 'col-resize';
  330. dom.value = '0.00';
  331. dom.addEventListener( 'keydown', function ( event ) {
  332. event.stopPropagation();
  333. if ( event.keyCode === 13 ) dom.blur();
  334. }, false );
  335. this.min = - Infinity;
  336. this.max = Infinity;
  337. this.precision = 2;
  338. this.step = 1;
  339. this.dom = dom;
  340. this.setValue( number );
  341. var changeEvent = document.createEvent( 'HTMLEvents' );
  342. changeEvent.initEvent( 'change', true, true );
  343. var distance = 0;
  344. var onMouseDownValue = 0;
  345. var onMouseDown = function ( event ) {
  346. event.preventDefault();
  347. distance = 0;
  348. onMouseDownValue = parseFloat( dom.value );
  349. document.addEventListener( 'mousemove', onMouseMove, false );
  350. document.addEventListener( 'mouseup', onMouseUp, false );
  351. };
  352. var onMouseMove = function ( event ) {
  353. var currentValue = dom.value;
  354. var movementX = event.movementX || event.webkitMovementX || event.mozMovementX || 0;
  355. var movementY = event.movementY || event.webkitMovementY || event.mozMovementY || 0;
  356. distance += movementX - movementY;
  357. var number = onMouseDownValue + ( distance / ( event.shiftKey ? 5 : 50 ) ) * scope.step;
  358. dom.value = Math.min( scope.max, Math.max( scope.min, number ) ).toFixed( scope.precision );
  359. if ( currentValue !== dom.value ) dom.dispatchEvent( changeEvent );
  360. };
  361. var onMouseUp = function ( event ) {
  362. document.removeEventListener( 'mousemove', onMouseMove, false );
  363. document.removeEventListener( 'mouseup', onMouseUp, false );
  364. if ( Math.abs( distance ) < 2 ) {
  365. dom.focus();
  366. dom.select();
  367. }
  368. };
  369. var onChange = function ( event ) {
  370. var number = parseFloat( dom.value );
  371. if ( isNaN( number ) === false ) {
  372. dom.value = number;
  373. }
  374. };
  375. var onFocus = function ( event ) {
  376. dom.style.backgroundColor = '';
  377. dom.style.borderColor = '#ccc';
  378. dom.style.cursor = '';
  379. };
  380. var onBlur = function ( event ) {
  381. dom.style.backgroundColor = 'transparent';
  382. dom.style.borderColor = 'transparent';
  383. dom.style.cursor = 'col-resize';
  384. };
  385. dom.addEventListener( 'mousedown', onMouseDown, false );
  386. dom.addEventListener( 'change', onChange, false );
  387. dom.addEventListener( 'focus', onFocus, false );
  388. dom.addEventListener( 'blur', onBlur, false );
  389. return this;
  390. };
  391. UI.Number.prototype = Object.create( UI.Element.prototype );
  392. UI.Number.prototype.getValue = function () {
  393. return parseFloat( this.dom.value );
  394. };
  395. UI.Number.prototype.setValue = function ( value ) {
  396. if ( value !== undefined ) {
  397. this.dom.value = value.toFixed( this.precision );
  398. }
  399. return this;
  400. };
  401. UI.Number.prototype.setRange = function ( min, max ) {
  402. this.min = min;
  403. this.max = max;
  404. return this;
  405. };
  406. UI.Number.prototype.setPrecision = function ( precision ) {
  407. this.precision = precision;
  408. return this;
  409. };
  410. // Integer
  411. UI.Integer = function ( number ) {
  412. UI.Element.call( this );
  413. var scope = this;
  414. var dom = document.createElement( 'input' );
  415. dom.className = 'Number';
  416. dom.style.color = '#0080f0';
  417. dom.style.fontSize = '12px';
  418. dom.style.backgroundColor = 'transparent';
  419. dom.style.border = '1px solid transparent';
  420. dom.style.padding = '2px';
  421. dom.style.cursor = 'col-resize';
  422. dom.value = '0.00';
  423. dom.addEventListener( 'keydown', function ( event ) {
  424. event.stopPropagation();
  425. }, false );
  426. this.min = - Infinity;
  427. this.max = Infinity;
  428. this.step = 1;
  429. this.dom = dom;
  430. this.setValue( number );
  431. var changeEvent = document.createEvent( 'HTMLEvents' );
  432. changeEvent.initEvent( 'change', true, true );
  433. var distance = 0;
  434. var onMouseDownValue = 0;
  435. var onMouseDown = function ( event ) {
  436. event.preventDefault();
  437. distance = 0;
  438. onMouseDownValue = parseFloat( dom.value );
  439. document.addEventListener( 'mousemove', onMouseMove, false );
  440. document.addEventListener( 'mouseup', onMouseUp, false );
  441. };
  442. var onMouseMove = function ( event ) {
  443. var currentValue = dom.value;
  444. var movementX = event.movementX || event.webkitMovementX || event.mozMovementX || 0;
  445. var movementY = event.movementY || event.webkitMovementY || event.mozMovementY || 0;
  446. distance += movementX - movementY;
  447. var number = onMouseDownValue + ( distance / ( event.shiftKey ? 5 : 50 ) ) * scope.step;
  448. dom.value = Math.min( scope.max, Math.max( scope.min, number ) ) | 0;
  449. if ( currentValue !== dom.value ) dom.dispatchEvent( changeEvent );
  450. };
  451. var onMouseUp = function ( event ) {
  452. document.removeEventListener( 'mousemove', onMouseMove, false );
  453. document.removeEventListener( 'mouseup', onMouseUp, false );
  454. if ( Math.abs( distance ) < 2 ) {
  455. dom.focus();
  456. dom.select();
  457. }
  458. };
  459. var onChange = function ( event ) {
  460. var number = parseInt( dom.value );
  461. if ( isNaN( number ) === false ) {
  462. dom.value = number;
  463. }
  464. };
  465. var onFocus = function ( event ) {
  466. dom.style.backgroundColor = '';
  467. dom.style.borderColor = '#ccc';
  468. dom.style.cursor = '';
  469. };
  470. var onBlur = function ( event ) {
  471. dom.style.backgroundColor = 'transparent';
  472. dom.style.borderColor = 'transparent';
  473. dom.style.cursor = 'col-resize';
  474. };
  475. dom.addEventListener( 'mousedown', onMouseDown, false );
  476. dom.addEventListener( 'change', onChange, false );
  477. dom.addEventListener( 'focus', onFocus, false );
  478. dom.addEventListener( 'blur', onBlur, false );
  479. return this;
  480. };
  481. UI.Integer.prototype = Object.create( UI.Element.prototype );
  482. UI.Integer.prototype.getValue = function () {
  483. return parseInt( this.dom.value );
  484. };
  485. UI.Integer.prototype.setValue = function ( value ) {
  486. if ( value !== undefined ) {
  487. this.dom.value = value | 0;
  488. }
  489. return this;
  490. };
  491. UI.Integer.prototype.setRange = function ( min, max ) {
  492. this.min = min;
  493. this.max = max;
  494. return this;
  495. };
  496. // Break
  497. UI.Break = function () {
  498. UI.Element.call( this );
  499. var dom = document.createElement( 'br' );
  500. dom.className = 'Break';
  501. this.dom = dom;
  502. return this;
  503. };
  504. UI.Break.prototype = Object.create( UI.Element.prototype );
  505. // HorizontalRule
  506. UI.HorizontalRule = function () {
  507. UI.Element.call( this );
  508. var dom = document.createElement( 'hr' );
  509. dom.className = 'HorizontalRule';
  510. this.dom = dom;
  511. return this;
  512. };
  513. UI.HorizontalRule.prototype = Object.create( UI.Element.prototype );
  514. // Button
  515. UI.Button = function ( value ) {
  516. UI.Element.call( this );
  517. var scope = this;
  518. var dom = document.createElement( 'button' );
  519. dom.className = 'Button';
  520. this.dom = dom;
  521. this.dom.textContent = value;
  522. return this;
  523. };
  524. UI.Button.prototype = Object.create( UI.Element.prototype );
  525. UI.Button.prototype.setLabel = function ( value ) {
  526. this.dom.textContent = value;
  527. return this;
  528. };