UI.js 7.7 KB

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