ui.js 15 KB

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