UI.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  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. this.onChangedCallback = null;
  121. var scope = this;
  122. var onMouseDownValue, onMouseDownScreenX, onMouseDownScreenY;
  123. var onMouseDown = function ( event ) {
  124. event.preventDefault();
  125. onMouseDownValue = parseInt( scope.dom.innerText );
  126. onMouseDownScreenX = event.screenX;
  127. onMouseDownScreenY = event.screenY;
  128. document.addEventListener( 'mousemove', onMouseMove, false );
  129. document.addEventListener( 'mouseup', onMouseUp, false );
  130. }
  131. var onMouseMove = function ( event ) {
  132. var dx = event.screenX - onMouseDownScreenX;
  133. var dy = event.screenY - onMouseDownScreenY;
  134. scope.dom.innerText = ( onMouseDownValue + ( dx - dy ) / ( event.shiftKey ? 10 : 100 ) ).toFixed( 0 );
  135. scope.onChangedCallback();
  136. }
  137. var onMouseUp = function ( event ) {
  138. document.removeEventListener( 'mousemove', onMouseMove, false );
  139. document.removeEventListener( 'mouseup', onMouseUp, false );
  140. }
  141. this.dom.addEventListener( 'mousedown', onMouseDown, false );
  142. return this;
  143. };
  144. UI.IntNumber.prototype = new UI.Element();
  145. UI.IntNumber.prototype.constructor = UI.IntNumber;
  146. UI.IntNumber.prototype.getValue = function () {
  147. return parseInt( this.dom.innerText );
  148. };
  149. UI.IntNumber.prototype.setValue = function ( value ) {
  150. this.dom.innerText = value.toFixed( 0 );
  151. return this;
  152. };
  153. UI.IntNumber.prototype.onChanged = function ( callback ) {
  154. this.onChangedCallback = callback;
  155. return this;
  156. };
  157. // FloatNumber
  158. UI.FloatNumber = function ( position ) {
  159. UI.Element.call( this );
  160. this.dom = document.createElement( 'span' );
  161. this.dom.style.position = position || 'relative';
  162. this.dom.innerText = '0.00';
  163. this.dom.style.marginTop = '2px';
  164. this.dom.style.color = '#0080f0';
  165. this.dom.style.fontSize = '12px';
  166. this.dom.style.textDecoration = 'underline';
  167. this.onChangedCallback = null;
  168. var scope = this;
  169. var onMouseDownValue, onMouseDownScreenX, onMouseDownScreenY;
  170. var onMouseDown = function ( event ) {
  171. event.preventDefault();
  172. onMouseDownValue = parseFloat( scope.dom.innerText );
  173. onMouseDownScreenX = event.screenX;
  174. onMouseDownScreenY = event.screenY;
  175. document.addEventListener( 'mousemove', onMouseMove, false );
  176. document.addEventListener( 'mouseup', onMouseUp, false );
  177. }
  178. var onMouseMove = function ( event ) {
  179. var dx = event.screenX - onMouseDownScreenX;
  180. var dy = event.screenY - onMouseDownScreenY;
  181. scope.dom.innerText = ( onMouseDownValue + ( dx - dy ) / ( event.shiftKey ? 10 : 100 ) ).toFixed( 2 );
  182. scope.onChangedCallback();
  183. }
  184. var onMouseUp = function ( event ) {
  185. document.removeEventListener( 'mousemove', onMouseMove, false );
  186. document.removeEventListener( 'mouseup', onMouseUp, false );
  187. }
  188. this.dom.addEventListener( 'mousedown', onMouseDown, false );
  189. return this;
  190. };
  191. UI.FloatNumber.prototype = new UI.Element();
  192. UI.FloatNumber.prototype.constructor = UI.FloatNumber;
  193. UI.FloatNumber.prototype.getValue = function () {
  194. return parseFloat( this.dom.innerText );
  195. };
  196. UI.FloatNumber.prototype.setValue = function ( value ) {
  197. this.dom.innerText = value.toFixed( 2 );
  198. return this;
  199. };
  200. UI.FloatNumber.prototype.onChanged = function ( callback ) {
  201. this.onChangedCallback = callback;
  202. return this;
  203. };
  204. // Break
  205. UI.Break = function () {
  206. UI.Element.call( this );
  207. this.dom = document.createElement( 'br' );
  208. return this;
  209. };
  210. UI.Break.prototype = new UI.Element();
  211. UI.Break.prototype.constructor = UI.Break;
  212. // HorizontalRule
  213. UI.HorizontalRule = function ( position ) {
  214. UI.Element.call( this );
  215. this.dom = document.createElement( 'hr' );
  216. this.dom.style.position = position || 'relative';
  217. return this;
  218. };
  219. UI.HorizontalRule.prototype = new UI.Element();
  220. UI.HorizontalRule.prototype.constructor = UI.HorizontalRule;
  221. // Button
  222. UI.Button = function ( position ) {
  223. UI.Element.call( this );
  224. this.dom = document.createElement( 'button' );
  225. this.dom.style.position = position || 'relative';
  226. return this;
  227. };
  228. UI.Button.prototype = new UI.Element();
  229. UI.Button.prototype.constructor = UI.Button;
  230. UI.Button.prototype.setText = function ( value ) {
  231. this.dom.innerText = value;
  232. return this;
  233. };