UI.js 13 KB

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