UI.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744
  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. setOverflow: function () {
  103. this.setStyle( 'overflow', arguments );
  104. return this;
  105. }
  106. }
  107. // Panel
  108. UI.Panel = function ( position ) {
  109. UI.Element.call( this );
  110. var dom = document.createElement( 'div' );
  111. dom.style.position = position || 'relative';
  112. dom.style.marginBottom = '10px';
  113. this.dom = dom;
  114. // this.dom.addEventListener( 'mousedown', function ( event ) { event.preventDefault() }, false );
  115. return this;
  116. };
  117. UI.Panel.prototype = Object.create( UI.Element.prototype );
  118. UI.Panel.prototype.add = function () {
  119. for ( var i = 0; i < arguments.length; i ++ ) {
  120. this.dom.appendChild( arguments[ i ].dom );
  121. }
  122. return this;
  123. };
  124. // Text
  125. UI.Text = function ( position ) {
  126. UI.Element.call( this );
  127. var dom = document.createElement( 'span' );
  128. dom.style.position = position || 'relative';
  129. this.dom = dom;
  130. return this;
  131. };
  132. UI.Text.prototype = Object.create( UI.Element.prototype );
  133. UI.Text.prototype.setValue = function ( value ) {
  134. this.dom.textContent = value;
  135. return this;
  136. };
  137. // Input
  138. UI.Input = function ( position ) {
  139. UI.Element.call( this );
  140. var scope = this;
  141. var dom = document.createElement( 'input' );
  142. dom.style.position = position || 'relative';
  143. dom.style.marginTop = '-2px';
  144. dom.style.marginLeft = '-2px';
  145. this.dom = dom;
  146. this.onChangeCallback = null;
  147. this.dom.addEventListener( 'change', function ( event ) {
  148. if ( scope.onChangeCallback ) scope.onChangeCallback();
  149. }, false );
  150. return this;
  151. };
  152. UI.Input.prototype = Object.create( UI.Element.prototype );
  153. UI.Input.prototype.getValue = function () {
  154. return this.dom.value;
  155. };
  156. UI.Input.prototype.setValue = function ( value ) {
  157. this.dom.value = value;
  158. return this;
  159. };
  160. UI.Input.prototype.onChange = function ( callback ) {
  161. this.onChangeCallback = callback;
  162. return this;
  163. };
  164. // Select
  165. UI.Select = function ( position ) {
  166. UI.Element.call( this );
  167. var scope = this;
  168. var dom = document.createElement( 'select' );
  169. dom.style.position = position || 'relative';
  170. dom.style.width = '64px';
  171. dom.style.height = '16px';
  172. dom.style.border = '0px';
  173. dom.style.padding = '0px';
  174. this.dom = dom;
  175. this.onChangeCallback = null;
  176. this.dom.addEventListener( 'change', function ( event ) {
  177. if ( scope.onChangeCallback ) scope.onChangeCallback();
  178. }, false );
  179. return this;
  180. };
  181. UI.Select.prototype = Object.create( UI.Element.prototype );
  182. UI.Select.prototype.setMultiple = function ( boolean ) {
  183. this.dom.multiple = boolean;
  184. return this;
  185. };
  186. UI.Select.prototype.setOptions = function ( options ) {
  187. while ( this.dom.children.length > 0 ) {
  188. this.dom.removeChild( this.dom.firstChild );
  189. }
  190. for ( var key in options ) {
  191. var option = document.createElement( 'option' );
  192. option.value = key;
  193. option.innerHTML = options[ key ];
  194. this.dom.appendChild( option );
  195. }
  196. return this;
  197. };
  198. UI.Select.prototype.getValue = function () {
  199. return this.dom.value;
  200. };
  201. UI.Select.prototype.setValue = function ( value ) {
  202. this.dom.value = value;
  203. return this;
  204. };
  205. UI.Select.prototype.onChange = function ( callback ) {
  206. this.onChangeCallback = callback;
  207. return this;
  208. };
  209. // Checkbox
  210. UI.Checkbox = function ( position ) {
  211. UI.Element.call( this );
  212. var scope = this;
  213. var dom = document.createElement( 'input' );
  214. dom.type = 'checkbox';
  215. dom.style.position = position || 'relative';
  216. this.dom = dom;
  217. this.onChangeCallback = null;
  218. this.dom.addEventListener( 'change', function ( event ) {
  219. if ( scope.onChangeCallback ) scope.onChangeCallback();
  220. }, false );
  221. return this;
  222. };
  223. UI.Checkbox.prototype = Object.create( UI.Element.prototype );
  224. UI.Checkbox.prototype.getValue = function () {
  225. return this.dom.checked;
  226. };
  227. UI.Checkbox.prototype.setValue = function ( value ) {
  228. this.dom.checked = value;
  229. return this;
  230. };
  231. UI.Checkbox.prototype.onChange = function ( callback ) {
  232. this.onChangeCallback = callback;
  233. return this;
  234. };
  235. // Color
  236. UI.Color = function ( position ) {
  237. UI.Element.call( this );
  238. var scope = this;
  239. var dom = document.createElement( 'input' );
  240. dom.type = 'color';
  241. dom.style.position = position || 'relative';
  242. dom.style.width = '64px';
  243. dom.style.height = '16px';
  244. dom.style.border = '0px';
  245. dom.style.padding = '0px';
  246. dom.style.backgroundColor = 'transparent';
  247. this.dom = dom;
  248. this.onChangeCallback = null;
  249. this.dom.addEventListener( 'change', function ( event ) {
  250. if ( scope.onChangeCallback ) scope.onChangeCallback();
  251. }, false );
  252. return this;
  253. };
  254. UI.Color.prototype = Object.create( UI.Element.prototype );
  255. UI.Color.prototype.getValue = function () {
  256. return this.dom.value;
  257. };
  258. UI.Color.prototype.setValue = function ( value ) {
  259. this.dom.value = value;
  260. return this;
  261. };
  262. UI.Color.prototype.onChange = function ( callback ) {
  263. this.onChangeCallback = callback;
  264. return this;
  265. };
  266. // Number
  267. UI.Number = function ( position ) {
  268. UI.Element.call( this );
  269. var scope = this;
  270. var dom = document.createElement( 'input' );
  271. dom.style.position = position || 'relative';
  272. dom.style.color = '#0080f0';
  273. dom.style.fontSize = '12px';
  274. dom.style.cursor = 'col-resize';
  275. dom.style.backgroundColor = 'transparent';
  276. dom.style.borderColor = 'transparent';
  277. dom.style.marginTop = '-2px';
  278. dom.style.marginLegt = '-2px';
  279. dom.value = '0.00';
  280. this.dom = dom;
  281. this.min = - Infinity;
  282. this.max = Infinity;
  283. this.onChangeCallback = null;
  284. var distance = 0;
  285. var onMouseDownValue = 0;
  286. var onMouseDown = function ( event ) {
  287. event.preventDefault();
  288. distance = 0;
  289. onMouseDownValue = parseFloat( dom.value );
  290. document.addEventListener( 'mousemove', onMouseMove, false );
  291. document.addEventListener( 'mouseup', onMouseUp, false );
  292. };
  293. var onMouseMove = function ( event ) {
  294. var movementX = event.movementX || event.webkitMovementX || event.mozMovementX || 0;
  295. var movementY = event.movementY || event.webkitMovementY || event.mozMovementY || 0;
  296. distance += movementX - movementY;
  297. var number = onMouseDownValue + ( distance / ( event.shiftKey ? 10 : 100 ) );
  298. dom.value = Math.min( scope.max, Math.max( scope.min, number ) ).toFixed( 2 );
  299. if ( scope.onChangeCallback ) scope.onChangeCallback();
  300. };
  301. var onMouseUp = function ( event ) {
  302. document.removeEventListener( 'mousemove', onMouseMove, false );
  303. document.removeEventListener( 'mouseup', onMouseUp, false );
  304. if ( Math.abs( distance ) < 2 ) {
  305. dom.focus();
  306. dom.select();
  307. }
  308. };
  309. var onChange = function ( event ) {
  310. var number = parseFloat( dom.value );
  311. if ( isNaN( number ) === false ) {
  312. dom.value = number;
  313. if ( scope.onChangeCallback ) scope.onChangeCallback();
  314. }
  315. };
  316. var onFocus = function ( event ) {
  317. dom.style.backgroundColor = '';
  318. dom.style.borderColor = '';
  319. };
  320. var onBlur = function ( event ) {
  321. dom.style.backgroundColor = 'transparent';
  322. dom.style.borderColor = 'transparent';
  323. };
  324. var onKeyUp = function ( event ) {
  325. if ( event.keyCode == 13 ) {
  326. onBlur();
  327. }
  328. };
  329. dom.addEventListener( 'mousedown', onMouseDown, false );
  330. dom.addEventListener( 'change', onChange, false );
  331. dom.addEventListener( 'focus', onFocus, false );
  332. dom.addEventListener( 'blur', onBlur, false );
  333. dom.addEventListener( 'keyup', onKeyUp, false );
  334. return this;
  335. };
  336. UI.Number.prototype = Object.create( UI.Element.prototype );
  337. UI.Number.prototype.getValue = function () {
  338. return parseFloat( this.dom.value );
  339. };
  340. UI.Number.prototype.setValue = function ( value ) {
  341. this.dom.value = value.toFixed( 2 );
  342. return this;
  343. };
  344. UI.Number.prototype.setRange = function ( min, max ) {
  345. this.min = min;
  346. this.max = max;
  347. return this;
  348. };
  349. UI.Number.prototype.onChange = function ( callback ) {
  350. this.onChangeCallback = callback;
  351. return this;
  352. };
  353. // Break
  354. UI.Break = function () {
  355. UI.Element.call( this );
  356. var dom = document.createElement( 'br' );
  357. this.dom = dom;
  358. return this;
  359. };
  360. UI.Break.prototype = Object.create( UI.Element.prototype );
  361. // HorizontalRule
  362. UI.HorizontalRule = function ( position ) {
  363. UI.Element.call( this );
  364. var dom = document.createElement( 'hr' );
  365. dom.style.position = position || 'relative';
  366. this.dom = dom;
  367. return this;
  368. };
  369. UI.HorizontalRule.prototype = Object.create( UI.Element.prototype );
  370. // Button
  371. UI.Button = function ( position ) {
  372. UI.Element.call( this );
  373. var scope = this;
  374. var dom = document.createElement( 'button' );
  375. dom.style.position = position || 'relative';
  376. this.dom = dom;
  377. this.onClickCallback = null;
  378. this.dom.addEventListener( 'click', function ( event ) {
  379. scope.onClickCallback();
  380. }, false );
  381. return this;
  382. };
  383. UI.Button.prototype = Object.create( UI.Element.prototype );
  384. UI.Button.prototype.setLabel = function ( value ) {
  385. this.dom.textContent = value;
  386. return this;
  387. };
  388. UI.Button.prototype.onClick = function ( callback ) {
  389. this.onClickCallback = callback;
  390. return this;
  391. };
粤ICP备19079148号