UI.js 14 KB

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