UI.js 14 KB

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