ui.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982
  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 = [ 'MouseOver', 'MouseOut', 'Click' ];
  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. this.dom = dom;
  92. this.onChangeCallback = null;
  93. this.dom.addEventListener( 'change', function ( event ) {
  94. if ( scope.onChangeCallback ) scope.onChangeCallback();
  95. }, false );
  96. return this;
  97. };
  98. UI.Input.prototype = Object.create( UI.Element.prototype );
  99. UI.Input.prototype.getValue = function () {
  100. return this.dom.value;
  101. };
  102. UI.Input.prototype.setValue = function ( value ) {
  103. this.dom.value = value;
  104. return this;
  105. };
  106. UI.Input.prototype.onChange = function ( callback ) {
  107. this.onChangeCallback = callback;
  108. return this;
  109. };
  110. // TextArea
  111. UI.TextArea = function () {
  112. UI.Element.call( this );
  113. var scope = this;
  114. var dom = document.createElement( 'textarea' );
  115. dom.className = 'TextArea';
  116. dom.style.padding = '2px';
  117. dom.style.marginTop = '-2px';
  118. dom.style.marginLeft = '-2px';
  119. dom.style.border = '1px solid #ccc';
  120. this.dom = dom;
  121. this.onChangeCallback = null;
  122. this.dom.addEventListener( 'keyup', function ( event ) {
  123. if ( scope.onKeyUpCallback ) scope.onKeyUpCallback();
  124. }, false );
  125. this.dom.addEventListener( 'change', function ( event ) {
  126. if ( scope.onChangeCallback ) scope.onChangeCallback();
  127. }, false );
  128. return this;
  129. };
  130. UI.TextArea.prototype = Object.create( UI.Element.prototype );
  131. UI.TextArea.prototype.getValue = function () {
  132. return this.dom.value;
  133. };
  134. UI.TextArea.prototype.setValue = function ( value ) {
  135. this.dom.value = value;
  136. return this;
  137. };
  138. UI.TextArea.prototype.onKeyUp = function ( callback ) {
  139. this.onKeyUpCallback = callback;
  140. return this;
  141. };
  142. UI.TextArea.prototype.onChange = function ( callback ) {
  143. this.onChangeCallback = callback;
  144. return this;
  145. };
  146. // Select
  147. UI.Select = function () {
  148. UI.Element.call( this );
  149. var scope = this;
  150. var dom = document.createElement( 'select' );
  151. dom.className = 'Select';
  152. dom.style.width = '64px';
  153. dom.style.height = '16px';
  154. dom.style.border = '0px';
  155. dom.style.padding = '0px';
  156. this.dom = dom;
  157. this.onChangeCallback = null;
  158. this.dom.addEventListener( 'change', function ( event ) {
  159. if ( scope.onChangeCallback ) scope.onChangeCallback();
  160. }, false );
  161. return this;
  162. };
  163. UI.Select.prototype = Object.create( UI.Element.prototype );
  164. UI.Select.prototype.setMultiple = function ( boolean ) {
  165. this.dom.multiple = boolean;
  166. return this;
  167. };
  168. UI.Select.prototype.setOptions = function ( options ) {
  169. while ( this.dom.children.length > 0 ) {
  170. this.dom.removeChild( this.dom.firstChild );
  171. }
  172. for ( var key in options ) {
  173. var option = document.createElement( 'option' );
  174. option.value = key;
  175. option.innerHTML = options[ key ];
  176. this.dom.appendChild( option );
  177. }
  178. return this;
  179. };
  180. UI.Select.prototype.getValue = function () {
  181. return this.dom.value;
  182. };
  183. UI.Select.prototype.setValue = function ( value ) {
  184. this.dom.value = value;
  185. return this;
  186. };
  187. UI.Select.prototype.onChange = function ( callback ) {
  188. this.onChangeCallback = callback;
  189. return this;
  190. };
  191. // FancySelect
  192. UI.FancySelect = function () {
  193. UI.Element.call( this );
  194. var scope = this;
  195. var dom = document.createElement( 'div' );
  196. dom.className = 'FancySelect';
  197. dom.style.background = '#fff';
  198. dom.style.border = '1px solid #ccc';
  199. dom.style.padding = '0';
  200. dom.style.cursor = 'default';
  201. dom.style.overflow = 'auto';
  202. this.dom = dom;
  203. this.onChangeCallback = null;
  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. while ( scope.dom.children.length > 0 ) {
  212. scope.dom.removeChild( scope.dom.firstChild );
  213. }
  214. scope.options = [];
  215. var generateOptionCallback = function ( element, value ) {
  216. return function ( event ) {
  217. for ( var i = 0; i < scope.options.length; i ++ ) {
  218. scope.options[ i ].style.backgroundColor = '#f0f0f0';
  219. }
  220. element.style.backgroundColor = '#f0f0f0';
  221. scope.selectedValue = value;
  222. if ( scope.onChangeCallback ) scope.onChangeCallback();
  223. }
  224. };
  225. for ( var key in options ) {
  226. var option = document.createElement( 'div' );
  227. option.style.padding = '4px';
  228. option.style.whiteSpace = 'nowrap';
  229. option.innerHTML = options[ key ];
  230. option.value = key;
  231. scope.dom.appendChild( option );
  232. scope.options.push( option );
  233. option.addEventListener( 'click', generateOptionCallback( option, key ), false );
  234. }
  235. return scope;
  236. };
  237. UI.FancySelect.prototype.getValue = function () {
  238. return this.selectedValue;
  239. };
  240. UI.FancySelect.prototype.setValue = function ( value ) {
  241. // must convert raw value into string for compatibility with UI.Select
  242. // which uses string values (initialized from options keys)
  243. var key = value ? value.toString() : value;
  244. for ( var i = 0; i < this.options.length; i ++ ) {
  245. var element = this.options[ i ];
  246. if ( element.value === key ) {
  247. element.style.backgroundColor = '#f0f0f0';
  248. } else {
  249. element.style.backgroundColor = '';
  250. }
  251. }
  252. this.selectedValue = value;
  253. return this;
  254. };
  255. UI.FancySelect.prototype.onChange = function ( callback ) {
  256. this.onChangeCallback = callback;
  257. return this;
  258. };
  259. // Checkbox
  260. UI.Checkbox = function ( boolean ) {
  261. UI.Element.call( this );
  262. var scope = this;
  263. var dom = document.createElement( 'input' );
  264. dom.className = 'Checkbox';
  265. dom.type = 'checkbox';
  266. this.dom = dom;
  267. this.setValue( boolean );
  268. this.onChangeCallback = null;
  269. this.dom.addEventListener( 'change', function ( event ) {
  270. if ( scope.onChangeCallback ) scope.onChangeCallback();
  271. }, false );
  272. return this;
  273. };
  274. UI.Checkbox.prototype = Object.create( UI.Element.prototype );
  275. UI.Checkbox.prototype.getValue = function () {
  276. return this.dom.checked;
  277. };
  278. UI.Checkbox.prototype.setValue = function ( value ) {
  279. if ( value !== undefined ) {
  280. this.dom.checked = value;
  281. }
  282. return this;
  283. };
  284. UI.Checkbox.prototype.onChange = function ( callback ) {
  285. this.onChangeCallback = callback;
  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. this.onChangeCallback = null;
  302. this.dom.addEventListener( 'change', function ( event ) {
  303. if ( scope.onChangeCallback ) scope.onChangeCallback();
  304. }, false );
  305. return this;
  306. };
  307. UI.Color.prototype = Object.create( UI.Element.prototype );
  308. UI.Color.prototype.getValue = function () {
  309. return this.dom.value;
  310. };
  311. UI.Color.prototype.getHexValue = function () {
  312. return parseInt( this.dom.value.substr( 1 ), 16 );
  313. };
  314. UI.Color.prototype.setValue = function ( value ) {
  315. this.dom.value = value;
  316. return this;
  317. };
  318. UI.Color.prototype.setHexValue = function ( hex ) {
  319. this.dom.value = "#" + ( '000000' + hex.toString( 16 ) ).slice( -6 );
  320. return this;
  321. };
  322. UI.Color.prototype.onChange = function ( callback ) {
  323. this.onChangeCallback = callback;
  324. return this;
  325. };
  326. // Number
  327. UI.Number = function ( number ) {
  328. UI.Element.call( this );
  329. var scope = this;
  330. var dom = document.createElement( 'input' );
  331. dom.className = 'Number';
  332. dom.style.color = '#0080f0';
  333. dom.style.fontSize = '12px';
  334. dom.style.backgroundColor = 'transparent';
  335. dom.style.border = '1px solid transparent';
  336. dom.style.marginTop = '-2px';
  337. dom.style.marginLegt = '-2px';
  338. dom.style.padding = '2px';
  339. dom.style.cursor = 'col-resize';
  340. dom.value = '0.00';
  341. this.min = - Infinity;
  342. this.max = Infinity;
  343. this.precision = 2;
  344. this.step = 1;
  345. this.dom = dom;
  346. this.setValue( number );
  347. this.onChangeCallback = null;
  348. var distance = 0;
  349. var onMouseDownValue = 0;
  350. var onMouseDown = function ( event ) {
  351. event.preventDefault();
  352. distance = 0;
  353. onMouseDownValue = parseFloat( dom.value );
  354. document.addEventListener( 'mousemove', onMouseMove, false );
  355. document.addEventListener( 'mouseup', onMouseUp, false );
  356. };
  357. var onMouseMove = function ( event ) {
  358. var currentValue = dom.value;
  359. var movementX = event.movementX || event.webkitMovementX || event.mozMovementX || 0;
  360. var movementY = event.movementY || event.webkitMovementY || event.mozMovementY || 0;
  361. distance += movementX - movementY;
  362. var number = onMouseDownValue + ( distance / ( event.shiftKey ? 5 : 50 ) ) * scope.step;
  363. dom.value = Math.min( scope.max, Math.max( scope.min, number ) ).toFixed( scope.precision );
  364. if ( currentValue !== dom.value && scope.onChangeCallback ) scope.onChangeCallback();
  365. };
  366. var onMouseUp = function ( event ) {
  367. document.removeEventListener( 'mousemove', onMouseMove, false );
  368. document.removeEventListener( 'mouseup', onMouseUp, false );
  369. if ( Math.abs( distance ) < 2 ) {
  370. dom.focus();
  371. dom.select();
  372. }
  373. };
  374. var onChange = function ( event ) {
  375. var number = parseFloat( dom.value );
  376. if ( isNaN( number ) === false ) {
  377. dom.value = number;
  378. if ( scope.onChangeCallback ) scope.onChangeCallback();
  379. }
  380. };
  381. var onFocus = function ( event ) {
  382. dom.style.backgroundColor = '';
  383. dom.style.borderColor = '#ccc';
  384. dom.style.cursor = '';
  385. };
  386. var onBlur = function ( event ) {
  387. dom.style.backgroundColor = 'transparent';
  388. dom.style.borderColor = 'transparent';
  389. dom.style.cursor = 'col-resize';
  390. };
  391. dom.addEventListener( 'mousedown', onMouseDown, false );
  392. dom.addEventListener( 'change', onChange, false );
  393. dom.addEventListener( 'focus', onFocus, false );
  394. dom.addEventListener( 'blur', onBlur, false );
  395. return this;
  396. };
  397. UI.Number.prototype = Object.create( UI.Element.prototype );
  398. UI.Number.prototype.getValue = function () {
  399. return parseFloat( this.dom.value );
  400. };
  401. UI.Number.prototype.setValue = function ( value ) {
  402. if ( value !== undefined ) {
  403. this.dom.value = value.toFixed( this.precision );
  404. }
  405. return this;
  406. };
  407. UI.Number.prototype.setRange = function ( min, max ) {
  408. this.min = min;
  409. this.max = max;
  410. return this;
  411. };
  412. UI.Number.prototype.setPrecision = function ( precision ) {
  413. this.precision = precision;
  414. return this;
  415. };
  416. UI.Number.prototype.onChange = function ( callback ) {
  417. this.onChangeCallback = callback;
  418. return this;
  419. };
  420. // Integer
  421. UI.Integer = function ( number ) {
  422. UI.Element.call( this );
  423. var scope = this;
  424. var dom = document.createElement( 'input' );
  425. dom.className = 'Number';
  426. dom.style.color = '#0080f0';
  427. dom.style.fontSize = '12px';
  428. dom.style.backgroundColor = 'transparent';
  429. dom.style.border = '1px solid transparent';
  430. dom.style.marginTop = '-2px';
  431. dom.style.marginLegt = '-2px';
  432. dom.style.padding = '2px';
  433. dom.style.cursor = 'col-resize';
  434. dom.value = '0.00';
  435. this.min = - Infinity;
  436. this.max = Infinity;
  437. this.step = 1;
  438. this.dom = dom;
  439. this.setValue( number );
  440. this.onChangeCallback = null;
  441. var distance = 0;
  442. var onMouseDownValue = 0;
  443. var onMouseDown = function ( event ) {
  444. event.preventDefault();
  445. distance = 0;
  446. onMouseDownValue = parseFloat( dom.value );
  447. document.addEventListener( 'mousemove', onMouseMove, false );
  448. document.addEventListener( 'mouseup', onMouseUp, false );
  449. };
  450. var onMouseMove = function ( event ) {
  451. var currentValue = dom.value;
  452. var movementX = event.movementX || event.webkitMovementX || event.mozMovementX || 0;
  453. var movementY = event.movementY || event.webkitMovementY || event.mozMovementY || 0;
  454. distance += movementX - movementY;
  455. var number = onMouseDownValue + ( distance / ( event.shiftKey ? 5 : 50 ) ) * scope.step;
  456. dom.value = Math.min( scope.max, Math.max( scope.min, number ) ) | 0;
  457. if ( currentValue !== dom.value && scope.onChangeCallback ) scope.onChangeCallback();
  458. };
  459. var onMouseUp = function ( event ) {
  460. document.removeEventListener( 'mousemove', onMouseMove, false );
  461. document.removeEventListener( 'mouseup', onMouseUp, false );
  462. if ( Math.abs( distance ) < 2 ) {
  463. dom.focus();
  464. dom.select();
  465. }
  466. };
  467. var onChange = function ( event ) {
  468. var number = parseInt( dom.value );
  469. if ( isNaN( number ) === false ) {
  470. dom.value = number;
  471. if ( scope.onChangeCallback ) scope.onChangeCallback();
  472. }
  473. };
  474. var onFocus = function ( event ) {
  475. dom.style.backgroundColor = '';
  476. dom.style.borderColor = '#ccc';
  477. dom.style.cursor = '';
  478. };
  479. var onBlur = function ( event ) {
  480. dom.style.backgroundColor = 'transparent';
  481. dom.style.borderColor = 'transparent';
  482. dom.style.cursor = 'col-resize';
  483. };
  484. dom.addEventListener( 'mousedown', onMouseDown, false );
  485. dom.addEventListener( 'change', onChange, false );
  486. dom.addEventListener( 'focus', onFocus, false );
  487. dom.addEventListener( 'blur', onBlur, false );
  488. return this;
  489. };
  490. UI.Integer.prototype = Object.create( UI.Element.prototype );
  491. UI.Integer.prototype.getValue = function () {
  492. return parseInt( this.dom.value );
  493. };
  494. UI.Integer.prototype.setValue = function ( value ) {
  495. if ( value !== undefined ) {
  496. this.dom.value = value | 0;
  497. }
  498. return this;
  499. };
  500. UI.Integer.prototype.setRange = function ( min, max ) {
  501. this.min = min;
  502. this.max = max;
  503. return this;
  504. };
  505. UI.Integer.prototype.onChange = function ( callback ) {
  506. this.onChangeCallback = callback;
  507. return this;
  508. };
  509. // Break
  510. UI.Break = function () {
  511. UI.Element.call( this );
  512. var dom = document.createElement( 'br' );
  513. dom.className = 'Break';
  514. this.dom = dom;
  515. return this;
  516. };
  517. UI.Break.prototype = Object.create( UI.Element.prototype );
  518. // HorizontalRule
  519. UI.HorizontalRule = function () {
  520. UI.Element.call( this );
  521. var dom = document.createElement( 'hr' );
  522. dom.className = 'HorizontalRule';
  523. this.dom = dom;
  524. return this;
  525. };
  526. UI.HorizontalRule.prototype = Object.create( UI.Element.prototype );
  527. // Button
  528. UI.Button = function ( value ) {
  529. UI.Element.call( this );
  530. var scope = this;
  531. var dom = document.createElement( 'button' );
  532. dom.className = 'Button';
  533. this.dom = dom;
  534. this.dom.textContent = value;
  535. return this;
  536. };
  537. UI.Button.prototype = Object.create( UI.Element.prototype );
  538. UI.Button.prototype.setLabel = function ( value ) {
  539. this.dom.textContent = value;
  540. return this;
  541. };