UI.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  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 = new UI.Element();
  116. UI.Panel.prototype.constructor = UI.Panel;
  117. UI.Panel.prototype.add = function () {
  118. for ( var i = 0; i < arguments.length; i ++ ) {
  119. this.dom.appendChild( arguments[ i ].dom );
  120. }
  121. return this;
  122. };
  123. // Text
  124. UI.Text = function ( position ) {
  125. UI.Element.call( this );
  126. this.dom = document.createElement( 'span' );
  127. this.dom.style.position = position || 'relative';
  128. return this;
  129. };
  130. UI.Text.prototype = new UI.Element();
  131. UI.Text.prototype.constructor = UI.Text;
  132. UI.Text.prototype.setText = function ( value ) {
  133. this.dom.textContent = value;
  134. return this;
  135. };
  136. // Color
  137. UI.Color = function ( position ) {
  138. UI.Element.call( this );
  139. this.dom = document.createElement( 'span' );
  140. this.dom.style.position = position || 'relative';
  141. this.dom.style.width = '64px';
  142. this.dom.style.height = '16px';
  143. this.dom.style.backgroundColor = '#000000';
  144. return this;
  145. };
  146. UI.Color.prototype = new UI.Element();
  147. UI.Color.prototype.constructor = UI.Color;
  148. UI.Color.prototype.getValue = function () {
  149. return this.dom.style.backgroundColor;
  150. };
  151. UI.Color.prototype.setValue = function ( value ) {
  152. this.dom.style.backgroundColor = value;
  153. return this;
  154. };
  155. // Number
  156. UI.Number = function ( position ) {
  157. UI.Element.call( this );
  158. this.dom = document.createElement( 'input' );
  159. this.dom.style.position = position || 'relative';
  160. this.dom.style.marginTop = '0px';
  161. this.dom.style.color = '#0080f0';
  162. this.dom.style.fontSize = '12px';
  163. this.dom.style.textDecoration = 'underline';
  164. this.dom.style.backgroundColor = 'transparent';
  165. this.dom.style.border = '0px';
  166. this.dom.value = '0.00';
  167. this.min = - Infinity;
  168. this.max = Infinity;
  169. this.onChangeCallback = null;
  170. var scope = this;
  171. var onMouseDownValue, onMouseDownScreenX, onMouseDownScreenY;
  172. var onMouseDown = function ( event ) {
  173. // event.preventDefault();
  174. onMouseDownValue = parseFloat( scope.dom.value );
  175. onMouseDownScreenX = event.screenX;
  176. onMouseDownScreenY = event.screenY;
  177. document.addEventListener( 'mousemove', onMouseMove, false );
  178. document.addEventListener( 'mouseup', onMouseUp, false );
  179. }
  180. var onMouseMove = function ( event ) {
  181. var distance = ( event.screenX - onMouseDownScreenX ) - ( event.screenY - onMouseDownScreenY );
  182. var amount = event.shiftKey ? 10 : 100;
  183. var number = onMouseDownValue + ( distance / amount );
  184. scope.dom.value = Math.min( scope.max, Math.max( scope.min, number ) ).toFixed( 2 );
  185. scope.onChangeCallback();
  186. }
  187. var onMouseUp = function ( event ) {
  188. document.removeEventListener( 'mousemove', onMouseMove, false );
  189. document.removeEventListener( 'mouseup', onMouseUp, false );
  190. }
  191. this.dom.addEventListener( 'mousedown', onMouseDown, false );
  192. this.dom.addEventListener( 'change', function ( event ) {
  193. var number = parseFloat( scope.dom.value );
  194. if ( number !== NaN ) {
  195. scope.dom.value = number.toFixed( 2 );
  196. scope.onChangeCallback();
  197. }
  198. }, false );
  199. return this;
  200. };
  201. UI.Number.prototype = new UI.Element();
  202. UI.Number.prototype.constructor = UI.Number;
  203. UI.Number.prototype.getValue = function () {
  204. return parseFloat( this.dom.value );
  205. };
  206. UI.Number.prototype.setValue = function ( value ) {
  207. this.dom.value = value.toFixed( 2 );
  208. return this;
  209. };
  210. UI.Number.prototype.setMin = function ( value ) {
  211. this.min = value;
  212. return this;
  213. };
  214. UI.Number.prototype.setMax = function ( value ) {
  215. this.max = value;
  216. return this;
  217. };
  218. UI.Number.prototype.onChange = function ( callback ) {
  219. this.onChangeCallback = callback;
  220. return this;
  221. };
  222. // Break
  223. UI.Break = function () {
  224. UI.Element.call( this );
  225. this.dom = document.createElement( 'br' );
  226. return this;
  227. };
  228. UI.Break.prototype = new UI.Element();
  229. UI.Break.prototype.constructor = UI.Break;
  230. // HorizontalRule
  231. UI.HorizontalRule = function ( position ) {
  232. UI.Element.call( this );
  233. this.dom = document.createElement( 'hr' );
  234. this.dom.style.position = position || 'relative';
  235. return this;
  236. };
  237. UI.HorizontalRule.prototype = new UI.Element();
  238. UI.HorizontalRule.prototype.constructor = UI.HorizontalRule;
  239. // Button
  240. UI.Button = function ( position ) {
  241. UI.Element.call( this );
  242. this.dom = document.createElement( 'button' );
  243. this.dom.style.position = position || 'relative';
  244. this.onClickCallback = null;
  245. var scope = this;
  246. this.dom.addEventListener( 'click', function ( event ) {
  247. scope.onClickCallback();
  248. }, false );
  249. return this;
  250. };
  251. UI.Button.prototype = new UI.Element();
  252. UI.Button.prototype.constructor = UI.Button;
  253. UI.Button.prototype.setText = function ( value ) {
  254. this.dom.textContent = value;
  255. return this;
  256. };
  257. UI.Button.prototype.onClick = function ( callback ) {
  258. this.onClickCallback = callback;
  259. return this;
  260. };