UI.js 17 KB

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