UI.js 14 KB

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