UI.js 13 KB

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