2
0

ui.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888
  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 = 'middle';
  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.border = '1px solid #ccc';
  93. dom.addEventListener( 'keydown', function ( event ) {
  94. event.stopPropagation();
  95. }, false );
  96. this.dom = dom;
  97. return this;
  98. };
  99. UI.Input.prototype = Object.create( UI.Element.prototype );
  100. UI.Input.prototype.getValue = function () {
  101. return this.dom.value;
  102. };
  103. UI.Input.prototype.setValue = function ( value ) {
  104. this.dom.value = value;
  105. return this;
  106. };
  107. // TextArea
  108. UI.TextArea = function () {
  109. UI.Element.call( this );
  110. var scope = this;
  111. var dom = document.createElement( 'textarea' );
  112. dom.className = 'TextArea';
  113. dom.style.padding = '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. var selected = this.dom.value;
  149. while ( this.dom.children.length > 0 ) {
  150. this.dom.removeChild( this.dom.firstChild );
  151. }
  152. for ( var key in options ) {
  153. var option = document.createElement( 'option' );
  154. option.value = key;
  155. option.innerHTML = options[ key ];
  156. this.dom.appendChild( option );
  157. }
  158. this.dom.value = selected;
  159. return this;
  160. };
  161. UI.Select.prototype.getValue = function () {
  162. return this.dom.value;
  163. };
  164. UI.Select.prototype.setValue = function ( value ) {
  165. this.dom.value = value;
  166. return this;
  167. };
  168. // FancySelect
  169. UI.FancySelect = function () {
  170. UI.Element.call( this );
  171. var scope = this;
  172. var dom = document.createElement( 'div' );
  173. dom.className = 'FancySelect';
  174. dom.style.background = '#fff';
  175. dom.style.border = '1px solid #ccc';
  176. dom.style.padding = '0';
  177. dom.style.cursor = 'default';
  178. dom.style.overflow = 'auto';
  179. this.dom = dom;
  180. this.options = [];
  181. this.selectedValue = null;
  182. return this;
  183. };
  184. UI.FancySelect.prototype = Object.create( UI.Element.prototype );
  185. UI.FancySelect.prototype.setOptions = function ( options ) {
  186. var scope = this;
  187. var changeEvent = document.createEvent( 'HTMLEvents' );
  188. changeEvent.initEvent( 'change', true, true );
  189. while ( scope.dom.children.length > 0 ) {
  190. scope.dom.removeChild( scope.dom.firstChild );
  191. }
  192. scope.options = [];
  193. for ( var key in options ) {
  194. var option = document.createElement( 'div' );
  195. option.style.padding = '4px';
  196. option.style.whiteSpace = 'nowrap';
  197. option.innerHTML = options[ key ];
  198. option.value = key;
  199. scope.dom.appendChild( option );
  200. scope.options.push( option );
  201. option.addEventListener( 'click', function ( event ) {
  202. scope.setValue( this.value );
  203. scope.dom.dispatchEvent( changeEvent );
  204. }, false );
  205. }
  206. return scope;
  207. };
  208. UI.FancySelect.prototype.getValue = function () {
  209. return this.selectedValue;
  210. };
  211. UI.FancySelect.prototype.setValue = function ( value ) {
  212. if ( typeof value === 'number' ) value = value.toString();
  213. for ( var i = 0; i < this.options.length; i ++ ) {
  214. var element = this.options[ i ];
  215. if ( element.value === value ) {
  216. element.style.backgroundColor = '#f0f0f0';
  217. // scroll into view
  218. var y = i * element.clientHeight;
  219. var scrollTop = this.dom.scrollTop;
  220. var domHeight = this.dom.clientHeight;
  221. if ( y < scrollTop || y > scrollTop + domHeight ) {
  222. this.dom.scrollTop = y;
  223. }
  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.padding = '2px';
  293. dom.style.cursor = 'col-resize';
  294. dom.value = '0.00';
  295. dom.addEventListener( 'keydown', function ( event ) {
  296. event.stopPropagation();
  297. if ( event.keyCode === 13 ) dom.blur();
  298. }, false );
  299. this.min = - Infinity;
  300. this.max = Infinity;
  301. this.precision = 2;
  302. this.step = 1;
  303. this.dom = dom;
  304. this.setValue( number );
  305. var changeEvent = document.createEvent( 'HTMLEvents' );
  306. changeEvent.initEvent( 'change', true, true );
  307. var distance = 0;
  308. var onMouseDownValue = 0;
  309. var onMouseDown = function ( event ) {
  310. event.preventDefault();
  311. distance = 0;
  312. onMouseDownValue = parseFloat( dom.value );
  313. document.addEventListener( 'mousemove', onMouseMove, false );
  314. document.addEventListener( 'mouseup', onMouseUp, false );
  315. };
  316. var onMouseMove = function ( event ) {
  317. var currentValue = dom.value;
  318. var movementX = event.movementX || event.webkitMovementX || event.mozMovementX || 0;
  319. var movementY = event.movementY || event.webkitMovementY || event.mozMovementY || 0;
  320. distance += movementX - movementY;
  321. var number = onMouseDownValue + ( distance / ( event.shiftKey ? 5 : 50 ) ) * scope.step;
  322. dom.value = Math.min( scope.max, Math.max( scope.min, number ) ).toFixed( scope.precision );
  323. if ( currentValue !== dom.value ) dom.dispatchEvent( changeEvent );
  324. };
  325. var onMouseUp = function ( event ) {
  326. document.removeEventListener( 'mousemove', onMouseMove, false );
  327. document.removeEventListener( 'mouseup', onMouseUp, false );
  328. if ( Math.abs( distance ) < 2 ) {
  329. dom.focus();
  330. dom.select();
  331. }
  332. };
  333. var onChange = function ( event ) {
  334. var number = parseFloat( dom.value );
  335. if ( isNaN( number ) === false ) {
  336. dom.value = number;
  337. }
  338. };
  339. var onFocus = function ( event ) {
  340. dom.style.backgroundColor = '';
  341. dom.style.borderColor = '#ccc';
  342. dom.style.cursor = '';
  343. };
  344. var onBlur = function ( event ) {
  345. dom.style.backgroundColor = 'transparent';
  346. dom.style.borderColor = 'transparent';
  347. dom.style.cursor = 'col-resize';
  348. };
  349. dom.addEventListener( 'mousedown', onMouseDown, false );
  350. dom.addEventListener( 'change', onChange, false );
  351. dom.addEventListener( 'focus', onFocus, false );
  352. dom.addEventListener( 'blur', onBlur, false );
  353. return this;
  354. };
  355. UI.Number.prototype = Object.create( UI.Element.prototype );
  356. UI.Number.prototype.getValue = function () {
  357. return parseFloat( this.dom.value );
  358. };
  359. UI.Number.prototype.setValue = function ( value ) {
  360. if ( value !== undefined ) {
  361. this.dom.value = value.toFixed( this.precision );
  362. }
  363. return this;
  364. };
  365. UI.Number.prototype.setRange = function ( min, max ) {
  366. this.min = min;
  367. this.max = max;
  368. return this;
  369. };
  370. UI.Number.prototype.setPrecision = function ( precision ) {
  371. this.precision = precision;
  372. return this;
  373. };
  374. // Integer
  375. UI.Integer = function ( number ) {
  376. UI.Element.call( this );
  377. var scope = this;
  378. var dom = document.createElement( 'input' );
  379. dom.className = 'Number';
  380. dom.style.color = '#0080f0';
  381. dom.style.fontSize = '12px';
  382. dom.style.backgroundColor = 'transparent';
  383. dom.style.border = '1px solid transparent';
  384. dom.style.padding = '2px';
  385. dom.style.cursor = 'col-resize';
  386. dom.value = '0.00';
  387. dom.addEventListener( 'keydown', function ( event ) {
  388. event.stopPropagation();
  389. }, false );
  390. this.min = - Infinity;
  391. this.max = Infinity;
  392. this.step = 1;
  393. this.dom = dom;
  394. this.setValue( number );
  395. var changeEvent = document.createEvent( 'HTMLEvents' );
  396. changeEvent.initEvent( 'change', true, true );
  397. var distance = 0;
  398. var onMouseDownValue = 0;
  399. var onMouseDown = function ( event ) {
  400. event.preventDefault();
  401. distance = 0;
  402. onMouseDownValue = parseFloat( dom.value );
  403. document.addEventListener( 'mousemove', onMouseMove, false );
  404. document.addEventListener( 'mouseup', onMouseUp, false );
  405. };
  406. var onMouseMove = function ( event ) {
  407. var currentValue = dom.value;
  408. var movementX = event.movementX || event.webkitMovementX || event.mozMovementX || 0;
  409. var movementY = event.movementY || event.webkitMovementY || event.mozMovementY || 0;
  410. distance += movementX - movementY;
  411. var number = onMouseDownValue + ( distance / ( event.shiftKey ? 5 : 50 ) ) * scope.step;
  412. dom.value = Math.min( scope.max, Math.max( scope.min, number ) ) | 0;
  413. if ( currentValue !== dom.value ) dom.dispatchEvent( changeEvent );
  414. };
  415. var onMouseUp = function ( event ) {
  416. document.removeEventListener( 'mousemove', onMouseMove, false );
  417. document.removeEventListener( 'mouseup', onMouseUp, false );
  418. if ( Math.abs( distance ) < 2 ) {
  419. dom.focus();
  420. dom.select();
  421. }
  422. };
  423. var onChange = function ( event ) {
  424. var number = parseInt( dom.value );
  425. if ( isNaN( number ) === false ) {
  426. dom.value = number;
  427. }
  428. };
  429. var onFocus = function ( event ) {
  430. dom.style.backgroundColor = '';
  431. dom.style.borderColor = '#ccc';
  432. dom.style.cursor = '';
  433. };
  434. var onBlur = function ( event ) {
  435. dom.style.backgroundColor = 'transparent';
  436. dom.style.borderColor = 'transparent';
  437. dom.style.cursor = 'col-resize';
  438. };
  439. dom.addEventListener( 'mousedown', onMouseDown, false );
  440. dom.addEventListener( 'change', onChange, false );
  441. dom.addEventListener( 'focus', onFocus, false );
  442. dom.addEventListener( 'blur', onBlur, false );
  443. return this;
  444. };
  445. UI.Integer.prototype = Object.create( UI.Element.prototype );
  446. UI.Integer.prototype.getValue = function () {
  447. return parseInt( this.dom.value );
  448. };
  449. UI.Integer.prototype.setValue = function ( value ) {
  450. if ( value !== undefined ) {
  451. this.dom.value = value | 0;
  452. }
  453. return this;
  454. };
  455. UI.Integer.prototype.setRange = function ( min, max ) {
  456. this.min = min;
  457. this.max = max;
  458. return this;
  459. };
  460. // Break
  461. UI.Break = function () {
  462. UI.Element.call( this );
  463. var dom = document.createElement( 'br' );
  464. dom.className = 'Break';
  465. this.dom = dom;
  466. return this;
  467. };
  468. UI.Break.prototype = Object.create( UI.Element.prototype );
  469. // HorizontalRule
  470. UI.HorizontalRule = function () {
  471. UI.Element.call( this );
  472. var dom = document.createElement( 'hr' );
  473. dom.className = 'HorizontalRule';
  474. this.dom = dom;
  475. return this;
  476. };
  477. UI.HorizontalRule.prototype = Object.create( UI.Element.prototype );
  478. // Button
  479. UI.Button = function ( value ) {
  480. UI.Element.call( this );
  481. var scope = this;
  482. var dom = document.createElement( 'button' );
  483. dom.className = 'Button';
  484. this.dom = dom;
  485. this.dom.textContent = value;
  486. return this;
  487. };
  488. UI.Button.prototype = Object.create( UI.Element.prototype );
  489. UI.Button.prototype.setLabel = function ( value ) {
  490. this.dom.textContent = value;
  491. return this;
  492. };