UI.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  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 ( node ) {
  92. this.dom.appendChild( node.dom );
  93. return this;
  94. };
  95. // Text
  96. UI.Text = function ( position ) {
  97. UI.Element.call( this );
  98. this.dom = document.createElement( 'span' );
  99. this.dom.style.position = position || 'relative';
  100. return this;
  101. };
  102. UI.Text.prototype = new UI.Element();
  103. UI.Text.prototype.constructor = UI.Text;
  104. UI.Text.prototype.setText = function ( value ) {
  105. this.dom.innerText = value;
  106. return this;
  107. };
  108. // IntNumber
  109. UI.IntNumber = function ( position ) {
  110. UI.Element.call( this );
  111. this.dom = document.createElement( 'span' );
  112. this.dom.style.position = position || 'relative';
  113. this.dom.innerText = '0.00';
  114. this.dom.style.marginTop = '2px';
  115. this.dom.style.color = '#0080f0';
  116. this.dom.style.fontSize = '12px';
  117. this.dom.style.textDecoration = 'underline';
  118. var scope = this;
  119. var onMouseDownValue, onMouseDownScreenX, onMouseDownScreenY;
  120. var onMouseDown = function ( event ) {
  121. event.preventDefault();
  122. onMouseDownValue = parseFloat( scope.dom.innerText );
  123. onMouseDownScreenX = event.screenX;
  124. onMouseDownScreenY = event.screenY;
  125. document.addEventListener( 'mousemove', onMouseMove, false );
  126. document.addEventListener( 'mouseup', onMouseUp, false );
  127. }
  128. var onMouseMove = function ( event ) {
  129. var dx = event.screenX - onMouseDownScreenX;
  130. var dy = event.screenY - onMouseDownScreenY;
  131. scope.dom.innerText = ( onMouseDownValue - ( dx - dy ) ).toFixed( 0 );
  132. }
  133. var onMouseUp = function ( event ) {
  134. document.removeEventListener( 'mousemove', onMouseMove, false );
  135. document.removeEventListener( 'mouseup', onMouseUp, false );
  136. }
  137. this.dom.addEventListener( 'mousedown', onMouseDown, false );
  138. return this;
  139. };
  140. UI.IntNumber.prototype = new UI.Element();
  141. UI.IntNumber.prototype.constructor = UI.IntNumber;
  142. UI.IntNumber.prototype.setNumber = function ( value ) {
  143. this.dom.innerText = value.toFixed( 0 );
  144. return this;
  145. };
  146. // FloatNumber
  147. UI.FloatNumber = function ( position ) {
  148. UI.Element.call( this );
  149. this.dom = document.createElement( 'span' );
  150. this.dom.style.position = position || 'relative';
  151. this.dom.innerText = '0.00';
  152. this.dom.style.marginTop = '2px';
  153. this.dom.style.color = '#0080f0';
  154. this.dom.style.fontSize = '12px';
  155. this.dom.style.textDecoration = 'underline';
  156. var scope = this;
  157. var onMouseDownValue, onMouseDownScreenX, onMouseDownScreenY;
  158. var onMouseDown = function ( event ) {
  159. event.preventDefault();
  160. onMouseDownValue = parseFloat( scope.dom.innerText );
  161. onMouseDownScreenX = event.screenX;
  162. onMouseDownScreenY = event.screenY;
  163. document.addEventListener( 'mousemove', onMouseMove, false );
  164. document.addEventListener( 'mouseup', onMouseUp, false );
  165. }
  166. var onMouseMove = function ( event ) {
  167. var dx = event.screenX - onMouseDownScreenX;
  168. var dy = event.screenY - onMouseDownScreenY;
  169. scope.dom.innerText = ( onMouseDownValue + ( dx - dy ) / 100 ).toFixed( 2 );
  170. }
  171. var onMouseUp = function ( event ) {
  172. document.removeEventListener( 'mousemove', onMouseMove, false );
  173. document.removeEventListener( 'mouseup', onMouseUp, false );
  174. }
  175. this.dom.addEventListener( 'mousedown', onMouseDown, false );
  176. return this;
  177. };
  178. UI.FloatNumber.prototype = new UI.Element();
  179. UI.FloatNumber.prototype.constructor = UI.FloatNumber;
  180. UI.FloatNumber.prototype.setNumber = function ( value ) {
  181. this.dom.innerText = value.toFixed( 2 );
  182. return this;
  183. };
  184. // Break
  185. UI.Break = function () {
  186. UI.Element.call( this );
  187. this.dom = document.createElement( 'br' );
  188. return this;
  189. };
  190. UI.Break.prototype = new UI.Element();
  191. UI.Break.prototype.constructor = UI.Break;
  192. // HorizontalRule
  193. UI.HorizontalRule = function ( position ) {
  194. UI.Element.call( this );
  195. this.dom = document.createElement( 'hr' );
  196. this.dom.style.position = position || 'relative';
  197. return this;
  198. };
  199. UI.HorizontalRule.prototype = new UI.Element();
  200. UI.HorizontalRule.prototype.constructor = UI.HorizontalRule;
  201. // Button
  202. UI.Button = function ( position ) {
  203. UI.Element.call( this );
  204. this.dom = document.createElement( 'button' );
  205. this.dom.style.position = position || 'relative';
  206. return this;
  207. };
  208. UI.Button.prototype = new UI.Element();
  209. UI.Button.prototype.constructor = UI.Button;
  210. UI.Button.prototype.setText = function ( value ) {
  211. this.dom.innerText = value;
  212. return this;
  213. };