UI.js 13 KB

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