UI.js 14 KB

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