UI.js 13 KB

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