UI.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. var UI = {};
  2. UI.Element = function () {};
  3. UI.Element.prototype = {
  4. setX: function ( value ) {
  5. this.dom.style.left = value;
  6. return this;
  7. },
  8. setY: function ( value ) {
  9. this.dom.style.top = value;
  10. return this;
  11. },
  12. setWidth: function ( value ) {
  13. this.dom.style.width = value;
  14. return this;
  15. },
  16. setHeight: function ( value ) {
  17. this.dom.style.height = value;
  18. return this;
  19. },
  20. // border
  21. setBorder: function ( value ) {
  22. this.dom.style.border = value;
  23. return this;
  24. },
  25. setBorderTop: function ( value ) {
  26. this.dom.style.borderTop = value;
  27. return this;
  28. },
  29. setBorderBottom: function ( value ) {
  30. this.dom.style.borderBottom = value;
  31. return this;
  32. },
  33. setBorderLeft: function ( value ) {
  34. this.dom.style.borderLeft = value;
  35. return this;
  36. },
  37. setBorderRight: function ( value ) {
  38. this.dom.style.borderRight = value;
  39. return this;
  40. },
  41. // margin
  42. setMargin: function ( value ) {
  43. this.dom.style.margin = value;
  44. return this;
  45. },
  46. setMarginTop: function ( value ) {
  47. this.dom.style.marginTop = value;
  48. return this;
  49. },
  50. setMarginBottom: function ( value ) {
  51. this.dom.style.marginBottom = value;
  52. return this;
  53. },
  54. setMarginLeft: function ( value ) {
  55. this.dom.style.marginLeft = value;
  56. return this;
  57. },
  58. setMarginRight: function ( value ) {
  59. this.dom.style.marginRight = value;
  60. return this;
  61. },
  62. // padding
  63. setPadding: function ( value ) {
  64. this.dom.style.padding = value;
  65. return this;
  66. },
  67. //
  68. setFontWeight: function ( value ) {
  69. this.dom.style.fontWeight = value;
  70. return this;
  71. },
  72. setColor: function ( value ) {
  73. this.dom.style.color = value;
  74. return this;
  75. },
  76. setBackgroundColor: function ( value ) {
  77. this.dom.style.backgroundColor = value;
  78. return this;
  79. }
  80. }
  81. // Panel
  82. UI.Panel = function ( position ) {
  83. UI.Element.call( this );
  84. this.dom = document.createElement( 'div' );
  85. this.dom.style.position = position || 'relative';
  86. this.dom.addEventListener( 'mousedown', function ( event ) { event.preventDefault() }, false );
  87. return this;
  88. };
  89. UI.Panel.prototype = new UI.Element();
  90. UI.Panel.prototype.constructor = UI.Panel;
  91. UI.Panel.prototype.add = function () {
  92. for ( var i = 0; i < arguments.length; i ++ ) {
  93. this.dom.appendChild( arguments[ i ].dom );
  94. }
  95. return this;
  96. };
  97. // Text
  98. UI.Text = function ( position ) {
  99. UI.Element.call( this );
  100. this.dom = document.createElement( 'span' );
  101. this.dom.style.position = position || 'relative';
  102. return this;
  103. };
  104. UI.Text.prototype = new UI.Element();
  105. UI.Text.prototype.constructor = UI.Text;
  106. UI.Text.prototype.setText = function ( value ) {
  107. this.dom.innerText = value;
  108. return this;
  109. };
  110. // IntNumber
  111. UI.IntNumber = function ( position ) {
  112. UI.Element.call( this );
  113. this.dom = document.createElement( 'span' );
  114. this.dom.style.position = position || 'relative';
  115. this.dom.innerText = '0.00';
  116. this.dom.style.marginTop = '2px';
  117. this.dom.style.color = '#0080f0';
  118. this.dom.style.fontSize = '12px';
  119. this.dom.style.textDecoration = 'underline';
  120. var scope = this;
  121. var onMouseDownValue, onMouseDownScreenX, onMouseDownScreenY;
  122. var onMouseDown = function ( event ) {
  123. event.preventDefault();
  124. onMouseDownValue = parseFloat( scope.dom.innerText );
  125. onMouseDownScreenX = event.screenX;
  126. onMouseDownScreenY = event.screenY;
  127. document.addEventListener( 'mousemove', onMouseMove, false );
  128. document.addEventListener( 'mouseup', onMouseUp, false );
  129. }
  130. var onMouseMove = function ( event ) {
  131. var dx = event.screenX - onMouseDownScreenX;
  132. var dy = event.screenY - onMouseDownScreenY;
  133. scope.dom.innerText = ( onMouseDownValue - ( dx - dy ) ).toFixed( 0 );
  134. }
  135. var onMouseUp = function ( event ) {
  136. document.removeEventListener( 'mousemove', onMouseMove, false );
  137. document.removeEventListener( 'mouseup', onMouseUp, false );
  138. }
  139. this.dom.addEventListener( 'mousedown', onMouseDown, false );
  140. return this;
  141. };
  142. UI.IntNumber.prototype = new UI.Element();
  143. UI.IntNumber.prototype.constructor = UI.IntNumber;
  144. UI.IntNumber.prototype.setNumber = function ( value ) {
  145. this.dom.innerText = value.toFixed( 0 );
  146. return this;
  147. };
  148. // FloatNumber
  149. UI.FloatNumber = function ( position ) {
  150. UI.Element.call( this );
  151. this.dom = document.createElement( 'span' );
  152. this.dom.style.position = position || 'relative';
  153. this.dom.innerText = '0.00';
  154. this.dom.style.marginTop = '2px';
  155. this.dom.style.color = '#0080f0';
  156. this.dom.style.fontSize = '12px';
  157. this.dom.style.textDecoration = 'underline';
  158. var scope = this;
  159. var onMouseDownValue, onMouseDownScreenX, onMouseDownScreenY;
  160. var onMouseDown = function ( event ) {
  161. event.preventDefault();
  162. onMouseDownValue = parseFloat( scope.dom.innerText );
  163. onMouseDownScreenX = event.screenX;
  164. onMouseDownScreenY = event.screenY;
  165. document.addEventListener( 'mousemove', onMouseMove, false );
  166. document.addEventListener( 'mouseup', onMouseUp, false );
  167. }
  168. var onMouseMove = function ( event ) {
  169. var dx = event.screenX - onMouseDownScreenX;
  170. var dy = event.screenY - onMouseDownScreenY;
  171. scope.dom.innerText = ( onMouseDownValue + ( dx - dy ) / 100 ).toFixed( 2 );
  172. }
  173. var onMouseUp = function ( event ) {
  174. document.removeEventListener( 'mousemove', onMouseMove, false );
  175. document.removeEventListener( 'mouseup', onMouseUp, false );
  176. }
  177. this.dom.addEventListener( 'mousedown', onMouseDown, false );
  178. return this;
  179. };
  180. UI.FloatNumber.prototype = new UI.Element();
  181. UI.FloatNumber.prototype.constructor = UI.FloatNumber;
  182. UI.FloatNumber.prototype.setNumber = function ( value ) {
  183. this.dom.innerText = value.toFixed( 2 );
  184. return this;
  185. };
  186. // Break
  187. UI.Break = function () {
  188. UI.Element.call( this );
  189. this.dom = document.createElement( 'br' );
  190. return this;
  191. };
  192. UI.Break.prototype = new UI.Element();
  193. UI.Break.prototype.constructor = UI.Break;
  194. // HorizontalRule
  195. UI.HorizontalRule = function ( position ) {
  196. UI.Element.call( this );
  197. this.dom = document.createElement( 'hr' );
  198. this.dom.style.position = position || 'relative';
  199. return this;
  200. };
  201. UI.HorizontalRule.prototype = new UI.Element();
  202. UI.HorizontalRule.prototype.constructor = UI.HorizontalRule;
  203. // Button
  204. UI.Button = function ( position ) {
  205. UI.Element.call( this );
  206. this.dom = document.createElement( 'button' );
  207. this.dom.style.position = position || 'relative';
  208. return this;
  209. };
  210. UI.Button.prototype = new UI.Element();
  211. UI.Button.prototype.constructor = UI.Button;
  212. UI.Button.prototype.setText = function ( value ) {
  213. this.dom.innerText = value;
  214. return this;
  215. };