ui.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894
  1. var UI = {};
  2. UI.Element = function () {};
  3. UI.Element.prototype = {
  4. setClass: function ( name ) {
  5. this.dom.className = name;
  6. return this;
  7. },
  8. setStyle: function ( style, array ) {
  9. for ( var i = 0; i < array.length; i ++ ) {
  10. this.dom.style[ style ] = array[ i ];
  11. }
  12. },
  13. setDisabled: function ( value ) {
  14. this.dom.disabled = value;
  15. return this;
  16. },
  17. setTextContent: function ( value ) {
  18. this.dom.textContent = value;
  19. return this;
  20. }
  21. }
  22. // properties
  23. var properties = [ 'position', 'left', 'top', 'right', 'bottom', 'width', 'height', 'border', 'borderLeft',
  24. 'borderTop', 'borderRight', 'borderBottom', 'borderColor', 'display', 'overflow', 'margin', 'marginLeft', 'marginTop', 'marginRight', 'marginBottom', 'padding', 'paddingLeft', 'paddingTop', 'paddingRight', 'paddingBottom', 'color',
  25. 'backgroundColor', 'opacity', 'fontSize', 'fontWeight', 'textTransform', 'cursor' ];
  26. properties.forEach( function ( property ) {
  27. var method = 'set' + property.substr( 0, 1 ).toUpperCase() + property.substr( 1, property.length );
  28. UI.Element.prototype[ method ] = function () {
  29. this.setStyle( property, arguments );
  30. return this;
  31. };
  32. } );
  33. // events
  34. var events = [ 'KeyUp', 'KeyDown', 'MouseOver', 'MouseOut', 'Click', 'Change' ];
  35. events.forEach( function ( event ) {
  36. var method = 'on' + event;
  37. UI.Element.prototype[ method ] = function ( callback ) {
  38. this.dom.addEventListener( event.toLowerCase(), callback, false );
  39. return this;
  40. };
  41. } );
  42. // Panel
  43. UI.Panel = function () {
  44. UI.Element.call( this );
  45. var dom = document.createElement( 'div' );
  46. dom.className = 'Panel';
  47. dom.style.userSelect = 'none';
  48. dom.style.WebkitUserSelect = 'none';
  49. dom.style.MozUserSelect = 'none';
  50. this.dom = dom;
  51. return this;
  52. };
  53. UI.Panel.prototype = Object.create( UI.Element.prototype );
  54. UI.Panel.prototype.add = function () {
  55. for ( var i = 0; i < arguments.length; i ++ ) {
  56. this.dom.appendChild( arguments[ i ].dom );
  57. }
  58. return this;
  59. };
  60. UI.Panel.prototype.remove = function () {
  61. for ( var i = 0; i < arguments.length; i ++ ) {
  62. this.dom.removeChild( arguments[ i ].dom );
  63. }
  64. return this;
  65. };
  66. // Text
  67. UI.Text = function ( text ) {
  68. UI.Element.call( this );
  69. var dom = document.createElement( 'span' );
  70. dom.className = 'Text';
  71. dom.style.cursor = 'default';
  72. dom.style.display = 'inline-block';
  73. dom.style.verticalAlign = 'top';
  74. this.dom = dom;
  75. this.setValue( text );
  76. return this;
  77. };
  78. UI.Text.prototype = Object.create( UI.Element.prototype );
  79. UI.Text.prototype.setValue = function ( value ) {
  80. if ( value !== undefined ) {
  81. this.dom.textContent = value;
  82. }
  83. return this;
  84. };
  85. // Input
  86. UI.Input = function () {
  87. UI.Element.call( this );
  88. var scope = this;
  89. var dom = document.createElement( 'input' );
  90. dom.className = 'Input';
  91. dom.style.padding = '2px';
  92. dom.style.marginTop = '-2px';
  93. dom.style.marginLeft = '-2px';
  94. dom.style.border = '1px solid #ccc';
  95. dom.addEventListener( 'keydown', function ( event ) {
  96. event.stopPropagation();
  97. }, false );
  98. this.dom = dom;
  99. return this;
  100. };
  101. UI.Input.prototype = Object.create( UI.Element.prototype );
  102. UI.Input.prototype.getValue = function () {
  103. return this.dom.value;
  104. };
  105. UI.Input.prototype.setValue = function ( value ) {
  106. this.dom.value = value;
  107. return this;
  108. };
  109. // TextArea
  110. UI.TextArea = function () {
  111. UI.Element.call( this );
  112. var scope = this;
  113. var dom = document.createElement( 'textarea' );
  114. dom.className = 'TextArea';
  115. dom.style.padding = '2px';
  116. dom.style.marginTop = '-2px';
  117. dom.style.marginLeft = '-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. this.dom = dom;
  184. this.options = [];
  185. this.selectedValue = null;
  186. return this;
  187. };
  188. UI.FancySelect.prototype = Object.create( UI.Element.prototype );
  189. UI.FancySelect.prototype.setOptions = function ( options ) {
  190. var scope = this;
  191. var changeEvent = document.createEvent( 'HTMLEvents' );
  192. changeEvent.initEvent( 'change', true, true );
  193. while ( scope.dom.children.length > 0 ) {
  194. scope.dom.removeChild( scope.dom.firstChild );
  195. }
  196. scope.options = [];
  197. for ( var key in options ) {
  198. var option = document.createElement( 'div' );
  199. option.style.padding = '4px';
  200. option.style.whiteSpace = 'nowrap';
  201. option.innerHTML = options[ key ];
  202. option.value = key;
  203. scope.dom.appendChild( option );
  204. scope.options.push( option );
  205. option.addEventListener( 'click', function ( event ) {
  206. scope.setValue( this.value );
  207. scope.dom.dispatchEvent( changeEvent );
  208. }, false );
  209. }
  210. return scope;
  211. };
  212. UI.FancySelect.prototype.getValue = function () {
  213. return this.selectedValue;
  214. };
  215. UI.FancySelect.prototype.setValue = function ( value ) {
  216. if ( typeof value === 'number' ) value = value.toString();
  217. for ( var i = 0; i < this.options.length; i ++ ) {
  218. var element = this.options[ i ];
  219. if ( element.value === value ) {
  220. element.style.backgroundColor = '#f0f0f0';
  221. // scroll into view
  222. var y = i * element.clientHeight;
  223. var scrollTop = this.dom.scrollTop;
  224. var domHeight = this.dom.clientHeight;
  225. if ( y < scrollTop || y > scrollTop + domHeight ) {
  226. this.dom.scrollTop = y;
  227. }
  228. } else {
  229. element.style.backgroundColor = '';
  230. }
  231. }
  232. this.selectedValue = value;
  233. return this;
  234. };
  235. // Checkbox
  236. UI.Checkbox = function ( boolean ) {
  237. UI.Element.call( this );
  238. var scope = this;
  239. var dom = document.createElement( 'input' );
  240. dom.className = 'Checkbox';
  241. dom.type = 'checkbox';
  242. this.dom = dom;
  243. this.setValue( boolean );
  244. return this;
  245. };
  246. UI.Checkbox.prototype = Object.create( UI.Element.prototype );
  247. UI.Checkbox.prototype.getValue = function () {
  248. return this.dom.checked;
  249. };
  250. UI.Checkbox.prototype.setValue = function ( value ) {
  251. if ( value !== undefined ) {
  252. this.dom.checked = value;
  253. }
  254. return this;
  255. };
  256. // Color
  257. UI.Color = function () {
  258. UI.Element.call( this );
  259. var scope = this;
  260. var dom = document.createElement( 'input' );
  261. dom.className = 'Color';
  262. dom.style.width = '64px';
  263. dom.style.height = '16px';
  264. dom.style.border = '0px';
  265. dom.style.padding = '0px';
  266. dom.style.backgroundColor = 'transparent';
  267. try { dom.type = 'color'; } catch ( exception ) {}
  268. this.dom = dom;
  269. return this;
  270. };
  271. UI.Color.prototype = Object.create( UI.Element.prototype );
  272. UI.Color.prototype.getValue = function () {
  273. return this.dom.value;
  274. };
  275. UI.Color.prototype.getHexValue = function () {
  276. return parseInt( this.dom.value.substr( 1 ), 16 );
  277. };
  278. UI.Color.prototype.setValue = function ( value ) {
  279. this.dom.value = value;
  280. return this;
  281. };
  282. UI.Color.prototype.setHexValue = function ( hex ) {
  283. this.dom.value = "#" + ( '000000' + hex.toString( 16 ) ).slice( -6 );
  284. return this;
  285. };
  286. // Number
  287. UI.Number = function ( number ) {
  288. UI.Element.call( this );
  289. var scope = this;
  290. var dom = document.createElement( 'input' );
  291. dom.className = 'Number';
  292. dom.style.color = '#0080f0';
  293. dom.style.fontSize = '12px';
  294. dom.style.backgroundColor = 'transparent';
  295. dom.style.border = '1px solid transparent';
  296. dom.style.marginTop = '-2px';
  297. dom.style.marginLegt = '-2px';
  298. dom.style.padding = '2px';
  299. dom.style.cursor = 'col-resize';
  300. dom.value = '0.00';
  301. dom.addEventListener( 'keydown', function ( event ) {
  302. event.stopPropagation();
  303. }, false );
  304. this.min = - Infinity;
  305. this.max = Infinity;
  306. this.precision = 2;
  307. this.step = 1;
  308. this.dom = dom;
  309. this.setValue( number );
  310. var changeEvent = document.createEvent( 'HTMLEvents' );
  311. changeEvent.initEvent( 'change', true, true );
  312. var distance = 0;
  313. var onMouseDownValue = 0;
  314. var onMouseDown = function ( event ) {
  315. event.preventDefault();
  316. distance = 0;
  317. onMouseDownValue = parseFloat( dom.value );
  318. document.addEventListener( 'mousemove', onMouseMove, false );
  319. document.addEventListener( 'mouseup', onMouseUp, false );
  320. };
  321. var onMouseMove = function ( event ) {
  322. var currentValue = dom.value;
  323. var movementX = event.movementX || event.webkitMovementX || event.mozMovementX || 0;
  324. var movementY = event.movementY || event.webkitMovementY || event.mozMovementY || 0;
  325. distance += movementX - movementY;
  326. var number = onMouseDownValue + ( distance / ( event.shiftKey ? 5 : 50 ) ) * scope.step;
  327. dom.value = Math.min( scope.max, Math.max( scope.min, number ) ).toFixed( scope.precision );
  328. if ( currentValue !== dom.value ) dom.dispatchEvent( changeEvent );
  329. };
  330. var onMouseUp = function ( event ) {
  331. document.removeEventListener( 'mousemove', onMouseMove, false );
  332. document.removeEventListener( 'mouseup', onMouseUp, false );
  333. if ( Math.abs( distance ) < 2 ) {
  334. dom.focus();
  335. dom.select();
  336. }
  337. };
  338. var onChange = function ( event ) {
  339. var number = parseFloat( dom.value );
  340. if ( isNaN( number ) === false ) {
  341. dom.value = number;
  342. }
  343. };
  344. var onFocus = function ( event ) {
  345. dom.style.backgroundColor = '';
  346. dom.style.borderColor = '#ccc';
  347. dom.style.cursor = '';
  348. };
  349. var onBlur = function ( event ) {
  350. dom.style.backgroundColor = 'transparent';
  351. dom.style.borderColor = 'transparent';
  352. dom.style.cursor = 'col-resize';
  353. };
  354. dom.addEventListener( 'mousedown', onMouseDown, false );
  355. dom.addEventListener( 'change', onChange, false );
  356. dom.addEventListener( 'focus', onFocus, false );
  357. dom.addEventListener( 'blur', onBlur, false );
  358. return this;
  359. };
  360. UI.Number.prototype = Object.create( UI.Element.prototype );
  361. UI.Number.prototype.getValue = function () {
  362. return parseFloat( this.dom.value );
  363. };
  364. UI.Number.prototype.setValue = function ( value ) {
  365. if ( value !== undefined ) {
  366. this.dom.value = value.toFixed( this.precision );
  367. }
  368. return this;
  369. };
  370. UI.Number.prototype.setRange = function ( min, max ) {
  371. this.min = min;
  372. this.max = max;
  373. return this;
  374. };
  375. UI.Number.prototype.setPrecision = function ( precision ) {
  376. this.precision = precision;
  377. return this;
  378. };
  379. // Integer
  380. UI.Integer = function ( number ) {
  381. UI.Element.call( this );
  382. var scope = this;
  383. var dom = document.createElement( 'input' );
  384. dom.className = 'Number';
  385. dom.style.color = '#0080f0';
  386. dom.style.fontSize = '12px';
  387. dom.style.backgroundColor = 'transparent';
  388. dom.style.border = '1px solid transparent';
  389. dom.style.marginTop = '-2px';
  390. dom.style.marginLegt = '-2px';
  391. dom.style.padding = '2px';
  392. dom.style.cursor = 'col-resize';
  393. dom.value = '0.00';
  394. dom.addEventListener( 'keydown', function ( event ) {
  395. event.stopPropagation();
  396. }, false );
  397. this.min = - Infinity;
  398. this.max = Infinity;
  399. this.step = 1;
  400. this.dom = dom;
  401. this.setValue( number );
  402. var changeEvent = document.createEvent( 'HTMLEvents' );
  403. changeEvent.initEvent( 'change', true, true );
  404. var distance = 0;
  405. var onMouseDownValue = 0;
  406. var onMouseDown = function ( event ) {
  407. event.preventDefault();
  408. distance = 0;
  409. onMouseDownValue = parseFloat( dom.value );
  410. document.addEventListener( 'mousemove', onMouseMove, false );
  411. document.addEventListener( 'mouseup', onMouseUp, false );
  412. };
  413. var onMouseMove = function ( event ) {
  414. var currentValue = dom.value;
  415. var movementX = event.movementX || event.webkitMovementX || event.mozMovementX || 0;
  416. var movementY = event.movementY || event.webkitMovementY || event.mozMovementY || 0;
  417. distance += movementX - movementY;
  418. var number = onMouseDownValue + ( distance / ( event.shiftKey ? 5 : 50 ) ) * scope.step;
  419. dom.value = Math.min( scope.max, Math.max( scope.min, number ) ) | 0;
  420. if ( currentValue !== dom.value ) dom.dispatchEvent( changeEvent );
  421. };
  422. var onMouseUp = function ( event ) {
  423. document.removeEventListener( 'mousemove', onMouseMove, false );
  424. document.removeEventListener( 'mouseup', onMouseUp, false );
  425. if ( Math.abs( distance ) < 2 ) {
  426. dom.focus();
  427. dom.select();
  428. }
  429. };
  430. var onChange = function ( event ) {
  431. var number = parseInt( dom.value );
  432. if ( isNaN( number ) === false ) {
  433. dom.value = number;
  434. }
  435. };
  436. var onFocus = function ( event ) {
  437. dom.style.backgroundColor = '';
  438. dom.style.borderColor = '#ccc';
  439. dom.style.cursor = '';
  440. };
  441. var onBlur = function ( event ) {
  442. dom.style.backgroundColor = 'transparent';
  443. dom.style.borderColor = 'transparent';
  444. dom.style.cursor = 'col-resize';
  445. };
  446. dom.addEventListener( 'mousedown', onMouseDown, false );
  447. dom.addEventListener( 'change', onChange, false );
  448. dom.addEventListener( 'focus', onFocus, false );
  449. dom.addEventListener( 'blur', onBlur, false );
  450. return this;
  451. };
  452. UI.Integer.prototype = Object.create( UI.Element.prototype );
  453. UI.Integer.prototype.getValue = function () {
  454. return parseInt( this.dom.value );
  455. };
  456. UI.Integer.prototype.setValue = function ( value ) {
  457. if ( value !== undefined ) {
  458. this.dom.value = value | 0;
  459. }
  460. return this;
  461. };
  462. UI.Integer.prototype.setRange = function ( min, max ) {
  463. this.min = min;
  464. this.max = max;
  465. return this;
  466. };
  467. // Break
  468. UI.Break = function () {
  469. UI.Element.call( this );
  470. var dom = document.createElement( 'br' );
  471. dom.className = 'Break';
  472. this.dom = dom;
  473. return this;
  474. };
  475. UI.Break.prototype = Object.create( UI.Element.prototype );
  476. // HorizontalRule
  477. UI.HorizontalRule = function () {
  478. UI.Element.call( this );
  479. var dom = document.createElement( 'hr' );
  480. dom.className = 'HorizontalRule';
  481. this.dom = dom;
  482. return this;
  483. };
  484. UI.HorizontalRule.prototype = Object.create( UI.Element.prototype );
  485. // Button
  486. UI.Button = function ( value ) {
  487. UI.Element.call( this );
  488. var scope = this;
  489. var dom = document.createElement( 'button' );
  490. dom.className = 'Button';
  491. this.dom = dom;
  492. this.dom.textContent = value;
  493. return this;
  494. };
  495. UI.Button.prototype = Object.create( UI.Element.prototype );
  496. UI.Button.prototype.setLabel = function ( value ) {
  497. this.dom.textContent = value;
  498. return this;
  499. };