UI.js 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630
  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. this.dom = document.createElement( 'div' );
  111. this.dom.style.position = position || 'relative';
  112. // this.dom.addEventListener( 'mousedown', function ( event ) { event.preventDefault() }, false );
  113. return this;
  114. };
  115. UI.Panel.prototype = Object.create( UI.Element.prototype );
  116. UI.Panel.prototype.add = function () {
  117. for ( var i = 0; i < arguments.length; i ++ ) {
  118. this.dom.appendChild( arguments[ i ].dom );
  119. }
  120. return this;
  121. };
  122. // Text
  123. UI.Text = function ( position ) {
  124. UI.Element.call( this );
  125. this.dom = document.createElement( 'span' );
  126. this.dom.style.position = position || 'relative';
  127. return this;
  128. };
  129. UI.Text.prototype = Object.create( UI.Element.prototype );
  130. UI.Text.prototype.setValue = function ( value ) {
  131. this.dom.textContent = value;
  132. return this;
  133. };
  134. // Select
  135. UI.Select = function ( position ) {
  136. UI.Element.call( this );
  137. var scope = this;
  138. this.dom = document.createElement( 'select' );
  139. this.dom.style.position = position || 'relative';
  140. this.dom.style.width = '64px';
  141. this.dom.style.height = '16px';
  142. this.dom.style.border = '0px';
  143. this.dom.style.padding = '0px';
  144. this.onChangeCallback = null;
  145. this.dom.addEventListener( 'change', function ( event ) {
  146. if ( scope.onChangeCallback ) scope.onChangeCallback();
  147. }, false );
  148. return this;
  149. };
  150. UI.Select.prototype = Object.create( UI.Element.prototype );
  151. UI.Select.prototype.setMultiple = function ( boolean ) {
  152. this.dom.multiple = boolean;
  153. return this;
  154. };
  155. UI.Select.prototype.setOptions = function ( options ) {
  156. while ( this.dom.children.length > 0 ) {
  157. this.dom.removeChild( this.dom.firstChild );
  158. }
  159. for ( var key in options ) {
  160. var option = document.createElement( 'option' );
  161. option.value = key;
  162. option.innerHTML = options[ key ];
  163. this.dom.appendChild( option );
  164. }
  165. return this;
  166. };
  167. UI.Select.prototype.getValue = function () {
  168. return this.dom.value;
  169. };
  170. UI.Select.prototype.setValue = function ( value ) {
  171. this.dom.value = value;
  172. return this;
  173. };
  174. UI.Select.prototype.onChange = function ( callback ) {
  175. this.onChangeCallback = callback;
  176. return this;
  177. };
  178. // Checkbox
  179. UI.Checkbox = function ( position ) {
  180. UI.Element.call( this );
  181. var scope = this;
  182. this.dom = document.createElement( 'input' );
  183. this.dom.type = 'checkbox';
  184. this.dom.style.position = position || 'relative';
  185. this.onChangeCallback = null;
  186. this.dom.addEventListener( 'change', function ( event ) {
  187. if ( scope.onChangeCallback ) scope.onChangeCallback();
  188. }, false );
  189. return this;
  190. };
  191. UI.Checkbox.prototype = Object.create( UI.Element.prototype );
  192. UI.Checkbox.prototype.getValue = function () {
  193. return this.dom.checked;
  194. };
  195. UI.Checkbox.prototype.setValue = function ( value ) {
  196. this.dom.checked = value;
  197. return this;
  198. };
  199. UI.Checkbox.prototype.onChange = function ( callback ) {
  200. this.onChangeCallback = callback;
  201. return this;
  202. };
  203. // Color
  204. UI.Color = function ( position ) {
  205. UI.Element.call( this );
  206. var scope = this;
  207. this.dom = document.createElement( 'input' );
  208. this.dom.type = 'color';
  209. this.dom.style.position = position || 'relative';
  210. this.dom.style.width = '64px';
  211. this.dom.style.height = '16px';
  212. this.dom.style.border = '0px';
  213. this.dom.style.padding = '0px';
  214. this.dom.style.backgroundColor = 'transparent';
  215. this.onChangeCallback = null;
  216. this.dom.addEventListener( 'change', function ( event ) {
  217. if ( scope.onChangeCallback ) scope.onChangeCallback();
  218. }, false );
  219. return this;
  220. };
  221. UI.Color.prototype = Object.create( UI.Element.prototype );
  222. UI.Color.prototype.getValue = function () {
  223. return this.dom.value;
  224. };
  225. UI.Color.prototype.setValue = function ( value ) {
  226. this.dom.value = value;
  227. return this;
  228. };
  229. UI.Color.prototype.onChange = function ( callback ) {
  230. this.onChangeCallback = callback;
  231. return this;
  232. };
  233. // Number
  234. UI.Number = function ( position ) {
  235. UI.Element.call( this );
  236. var scope = this;
  237. this.dom = document.createElement( 'input' );
  238. this.dom.style.position = position || 'relative';
  239. this.dom.style.marginTop = '0px';
  240. this.dom.style.color = '#0080f0';
  241. this.dom.style.fontSize = '12px';
  242. this.dom.style.textDecoration = 'underline';
  243. this.dom.style.backgroundColor = 'transparent';
  244. this.dom.style.border = '0px';
  245. this.dom.value = '0.00';
  246. this.min = - Infinity;
  247. this.max = Infinity;
  248. this.onChangeCallback = null;
  249. var onMouseDownValue, onMouseDownScreenX, onMouseDownScreenY;
  250. var onMouseDown = function ( event ) {
  251. // event.preventDefault();
  252. onMouseDownValue = parseFloat( scope.dom.value );
  253. onMouseDownScreenX = event.screenX;
  254. onMouseDownScreenY = event.screenY;
  255. document.addEventListener( 'mousemove', onMouseMove, false );
  256. document.addEventListener( 'mouseup', onMouseUp, false );
  257. }
  258. var onMouseMove = function ( event ) {
  259. var distance = ( event.screenX - onMouseDownScreenX ) - ( event.screenY - onMouseDownScreenY );
  260. var amount = event.shiftKey ? 10 : 100;
  261. var number = onMouseDownValue + ( distance / amount );
  262. scope.dom.value = Math.min( scope.max, Math.max( scope.min, number ) ).toFixed( 2 );
  263. if ( scope.onChangeCallback ) scope.onChangeCallback();
  264. }
  265. var onMouseUp = function ( event ) {
  266. document.removeEventListener( 'mousemove', onMouseMove, false );
  267. document.removeEventListener( 'mouseup', onMouseUp, false );
  268. }
  269. this.dom.addEventListener( 'mousedown', onMouseDown, false );
  270. this.dom.addEventListener( 'change', function ( event ) {
  271. var number = parseFloat( scope.dom.value );
  272. if ( number !== NaN ) {
  273. scope.dom.value = number.toFixed( 2 );
  274. if ( scope.onChangeCallback ) scope.onChangeCallback();
  275. }
  276. }, false );
  277. return this;
  278. };
  279. UI.Number.prototype = Object.create( UI.Element.prototype );
  280. UI.Number.prototype.getValue = function () {
  281. return parseFloat( this.dom.value );
  282. };
  283. UI.Number.prototype.setValue = function ( value ) {
  284. this.dom.value = value.toFixed( 2 );
  285. return this;
  286. };
  287. UI.Number.prototype.setRange = function ( min, max ) {
  288. this.min = min;
  289. this.max = max;
  290. return this;
  291. };
  292. UI.Number.prototype.onChange = function ( callback ) {
  293. this.onChangeCallback = callback;
  294. return this;
  295. };
  296. // Break
  297. UI.Break = function () {
  298. UI.Element.call( this );
  299. this.dom = document.createElement( 'br' );
  300. return this;
  301. };
  302. UI.Break.prototype = Object.create( UI.Element.prototype );
  303. // HorizontalRule
  304. UI.HorizontalRule = function ( position ) {
  305. UI.Element.call( this );
  306. this.dom = document.createElement( 'hr' );
  307. this.dom.style.position = position || 'relative';
  308. return this;
  309. };
  310. UI.HorizontalRule.prototype = Object.create( UI.Element.prototype );
  311. // Button
  312. UI.Button = function ( position ) {
  313. UI.Element.call( this );
  314. var scope = this;
  315. this.dom = document.createElement( 'button' );
  316. this.dom.style.position = position || 'relative';
  317. this.onClickCallback = null;
  318. this.dom.addEventListener( 'click', function ( event ) {
  319. scope.onClickCallback();
  320. }, false );
  321. return this;
  322. };
  323. UI.Button.prototype = Object.create( UI.Element.prototype );
  324. UI.Button.prototype.setLabel = function ( value ) {
  325. this.dom.textContent = value;
  326. return this;
  327. };
  328. UI.Button.prototype.onClick = function ( callback ) {
  329. this.onClickCallback = callback;
  330. return this;
  331. };