UI.js 7.3 KB

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