UI.js 16 KB

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