ui.js 15 KB

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