UI.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833
  1. var UI = {};
  2. UI.Element = function () {};
  3. UI.Element.prototype = {
  4. setClass: function ( name ) {
  5. this.dom.className = name;
  6. return this;
  7. },
  8. setStyle: function ( style, array ) {
  9. for ( var i = 0; i < array.length; i ++ ) {
  10. this.dom.style[ style ] = array[ i ];
  11. }
  12. },
  13. setTextContent: function ( value ) {
  14. this.dom.textContent = value;
  15. return this;
  16. }
  17. }
  18. // properties
  19. var properties = [ 'position', 'left', 'top', 'right', 'bottom', 'width', 'height', 'border', 'borderLeft',
  20. 'borderTop', 'borderRight', 'borderBottom', 'display', 'overflow', 'margin', 'marginLeft', 'marginTop', 'marginRight',
  21. 'marginBottom', 'padding', 'paddingLeft', 'paddingTop', 'paddingRight', 'paddingBottom', 'color',
  22. 'backgroundColor', 'fontSize', 'fontWeight', 'textTransform', 'cursor' ];
  23. properties.forEach( function ( property ) {
  24. var method = 'set' + property.substr( 0, 1 ).toUpperCase() + property.substr( 1, property.length );
  25. UI.Element.prototype[ method ] = function () {
  26. this.setStyle( property, arguments );
  27. return this;
  28. };
  29. } );
  30. // events
  31. var events = [ 'MouseOver', 'MouseOut', 'Click' ];
  32. events.forEach( function ( event ) {
  33. var method = 'on' + event;
  34. UI.Element.prototype[ method ] = function ( callback ) {
  35. this.dom.addEventListener( event.toLowerCase(), callback, false );
  36. return this;
  37. };
  38. } );
  39. // Panel
  40. UI.Panel = function () {
  41. UI.Element.call( this );
  42. var dom = document.createElement( 'div' );
  43. dom.className = 'Panel';
  44. dom.style.userSelect = 'none';
  45. dom.style.WebkitUserSelect = 'none';
  46. dom.style.MozUserSelect = 'none';
  47. this.dom = dom;
  48. return this;
  49. };
  50. UI.Panel.prototype = Object.create( UI.Element.prototype );
  51. UI.Panel.prototype.add = function () {
  52. for ( var i = 0; i < arguments.length; i ++ ) {
  53. this.dom.appendChild( arguments[ i ].dom );
  54. }
  55. return this;
  56. };
  57. UI.Panel.prototype.remove = function () {
  58. for ( var i = 0; i < arguments.length; i ++ ) {
  59. this.dom.removeChild( arguments[ i ].dom );
  60. }
  61. return this;
  62. };
  63. // Text
  64. UI.Text = function ( text ) {
  65. UI.Element.call( this );
  66. var dom = document.createElement( 'span' );
  67. dom.className = 'Text';
  68. dom.style.cursor = 'default';
  69. dom.style.display = 'inline-block';
  70. dom.style.verticalAlign = 'top';
  71. this.dom = dom;
  72. this.setValue( text );
  73. return this;
  74. };
  75. UI.Text.prototype = Object.create( UI.Element.prototype );
  76. UI.Text.prototype.setValue = function ( value ) {
  77. if ( value !== undefined ) {
  78. this.dom.textContent = value;
  79. }
  80. return this;
  81. };
  82. // Input
  83. UI.Input = function () {
  84. UI.Element.call( this );
  85. var scope = this;
  86. var dom = document.createElement( 'input' );
  87. dom.className = 'Input';
  88. dom.style.padding = '2px';
  89. dom.style.marginTop = '-2px';
  90. dom.style.marginLeft = '-2px';
  91. dom.style.border = '1px solid #ccc';
  92. this.dom = dom;
  93. this.onChangeCallback = null;
  94. this.dom.addEventListener( 'change', function ( event ) {
  95. if ( scope.onChangeCallback ) scope.onChangeCallback();
  96. }, false );
  97. return this;
  98. };
  99. UI.Input.prototype = Object.create( UI.Element.prototype );
  100. UI.Input.prototype.getValue = function () {
  101. return this.dom.value;
  102. };
  103. UI.Input.prototype.setValue = function ( value ) {
  104. this.dom.value = value;
  105. return this;
  106. };
  107. UI.Input.prototype.onChange = function ( callback ) {
  108. this.onChangeCallback = callback;
  109. return this;
  110. };
  111. // TextArea
  112. UI.TextArea = function () {
  113. UI.Element.call( this );
  114. var scope = this;
  115. var dom = document.createElement( 'textarea' );
  116. dom.className = 'TextArea';
  117. dom.style.padding = '2px';
  118. dom.style.marginTop = '-2px';
  119. dom.style.marginLeft = '-2px';
  120. dom.style.border = '1px solid #ccc';
  121. this.dom = dom;
  122. this.onChangeCallback = null;
  123. this.dom.addEventListener( 'change', function ( event ) {
  124. if ( scope.onChangeCallback ) scope.onChangeCallback();
  125. }, false );
  126. return this;
  127. };
  128. UI.TextArea.prototype = Object.create( UI.Element.prototype );
  129. UI.TextArea.prototype.getValue = function () {
  130. return this.dom.value;
  131. };
  132. UI.TextArea.prototype.setValue = function ( value ) {
  133. this.dom.value = value;
  134. return this;
  135. };
  136. UI.TextArea.prototype.onChange = function ( callback ) {
  137. this.onChangeCallback = callback;
  138. return this;
  139. };
  140. // Select
  141. UI.Select = function () {
  142. UI.Element.call( this );
  143. var scope = this;
  144. var dom = document.createElement( 'select' );
  145. dom.className = 'Select';
  146. dom.style.width = '64px';
  147. dom.style.height = '16px';
  148. dom.style.border = '0px';
  149. dom.style.padding = '0px';
  150. this.dom = dom;
  151. this.onChangeCallback = null;
  152. this.dom.addEventListener( 'change', function ( event ) {
  153. if ( scope.onChangeCallback ) scope.onChangeCallback();
  154. }, false );
  155. return this;
  156. };
  157. UI.Select.prototype = Object.create( UI.Element.prototype );
  158. UI.Select.prototype.setMultiple = function ( boolean ) {
  159. this.dom.multiple = boolean;
  160. return this;
  161. };
  162. UI.Select.prototype.setOptions = function ( options ) {
  163. while ( this.dom.children.length > 0 ) {
  164. this.dom.removeChild( this.dom.firstChild );
  165. }
  166. for ( var key in options ) {
  167. var option = document.createElement( 'option' );
  168. option.value = key;
  169. option.innerHTML = options[ key ];
  170. this.dom.appendChild( option );
  171. }
  172. return this;
  173. };
  174. UI.Select.prototype.getValue = function () {
  175. return this.dom.value;
  176. };
  177. UI.Select.prototype.setValue = function ( value ) {
  178. this.dom.value = value;
  179. return this;
  180. };
  181. UI.Select.prototype.onChange = function ( callback ) {
  182. this.onChangeCallback = callback;
  183. return this;
  184. };
  185. // FancySelect
  186. UI.FancySelect = function () {
  187. UI.Element.call( this );
  188. var scope = this;
  189. var dom = document.createElement( 'div' );
  190. dom.className = 'FancySelect';
  191. dom.style.background = '#fff';
  192. dom.style.border = '1px solid #ccc';
  193. dom.style.padding = '0';
  194. dom.style.cursor = 'default';
  195. dom.style.overflow = 'auto';
  196. this.dom = dom;
  197. this.onChangeCallback = null;
  198. this.options = [];
  199. this.selectedValue = null;
  200. return this;
  201. };
  202. UI.FancySelect.prototype = Object.create( UI.Element.prototype );
  203. UI.FancySelect.prototype.setOptions = function ( options ) {
  204. var scope = this;
  205. while ( scope.dom.children.length > 0 ) {
  206. scope.dom.removeChild( scope.dom.firstChild );
  207. }
  208. scope.options = [];
  209. var generateOptionCallback = function ( element, value ) {
  210. return function ( event ) {
  211. for ( var i = 0; i < scope.options.length; i ++ ) {
  212. scope.options[ i ].style.backgroundColor = '#f0f0f0';
  213. }
  214. element.style.backgroundColor = '#f0f0f0';
  215. scope.selectedValue = value;
  216. if ( scope.onChangeCallback ) scope.onChangeCallback();
  217. }
  218. };
  219. for ( var key in options ) {
  220. var option = document.createElement( 'div' );
  221. option.style.padding = '4px';
  222. option.style.whiteSpace = 'nowrap';
  223. option.innerHTML = options[ key ];
  224. option.value = key;
  225. scope.dom.appendChild( option );
  226. scope.options.push( option );
  227. option.addEventListener( 'click', generateOptionCallback( option, key ), false );
  228. }
  229. return scope;
  230. };
  231. UI.FancySelect.prototype.getValue = function () {
  232. return this.selectedValue;
  233. };
  234. UI.FancySelect.prototype.setValue = function ( value ) {
  235. // must convert raw value into string for compatibility with UI.Select
  236. // which uses string values (initialized from options keys)
  237. var key = value ? value.toString() : value;
  238. for ( var i = 0; i < this.options.length; i ++ ) {
  239. var element = this.options[ i ];
  240. if ( element.value === key ) {
  241. element.style.backgroundColor = '#f0f0f0';
  242. } else {
  243. element.style.backgroundColor = '';
  244. }
  245. }
  246. this.selectedValue = value;
  247. return this;
  248. };
  249. UI.FancySelect.prototype.onChange = function ( callback ) {
  250. this.onChangeCallback = callback;
  251. return this;
  252. };
  253. // Checkbox
  254. UI.Checkbox = function ( boolean ) {
  255. UI.Element.call( this );
  256. var scope = this;
  257. var dom = document.createElement( 'input' );
  258. dom.className = 'Checkbox';
  259. dom.type = 'checkbox';
  260. this.dom = dom;
  261. this.setValue( boolean );
  262. this.onChangeCallback = null;
  263. this.dom.addEventListener( 'change', function ( event ) {
  264. if ( scope.onChangeCallback ) scope.onChangeCallback();
  265. }, false );
  266. return this;
  267. };
  268. UI.Checkbox.prototype = Object.create( UI.Element.prototype );
  269. UI.Checkbox.prototype.getValue = function () {
  270. return this.dom.checked;
  271. };
  272. UI.Checkbox.prototype.setValue = function ( value ) {
  273. if ( value !== undefined ) {
  274. this.dom.checked = value;
  275. }
  276. return this;
  277. };
  278. UI.Checkbox.prototype.onChange = function ( callback ) {
  279. this.onChangeCallback = callback;
  280. return this;
  281. };
  282. // Color
  283. UI.Color = function () {
  284. UI.Element.call( this );
  285. var scope = this;
  286. var dom = document.createElement( 'input' );
  287. dom.className = 'Color';
  288. dom.style.width = '64px';
  289. dom.style.height = '16px';
  290. dom.style.border = '0px';
  291. dom.style.padding = '0px';
  292. dom.style.backgroundColor = 'transparent';
  293. dom.type = 'color';
  294. this.dom = dom;
  295. this.onChangeCallback = null;
  296. this.dom.addEventListener( 'change', function ( event ) {
  297. if ( scope.onChangeCallback ) scope.onChangeCallback();
  298. }, false );
  299. return this;
  300. };
  301. UI.Color.prototype = Object.create( UI.Element.prototype );
  302. UI.Color.prototype.getValue = function () {
  303. return this.dom.value;
  304. };
  305. UI.Color.prototype.getHexValue = function () {
  306. return parseInt( this.dom.value.substr( 1 ), 16 );
  307. };
  308. UI.Color.prototype.setValue = function ( value ) {
  309. this.dom.value = value;
  310. return this;
  311. };
  312. UI.Color.prototype.setHexValue = function ( hex ) {
  313. this.dom.value = "#" + ( '000000' + hex.toString( 16 ) ).slice( -6 );
  314. return this;
  315. };
  316. UI.Color.prototype.onChange = function ( callback ) {
  317. this.onChangeCallback = callback;
  318. return this;
  319. };
  320. // Number
  321. UI.Number = function ( number ) {
  322. UI.Element.call( this );
  323. var scope = this;
  324. var dom = document.createElement( 'input' );
  325. dom.className = 'Number';
  326. dom.style.color = '#0080f0';
  327. dom.style.fontSize = '12px';
  328. dom.style.backgroundColor = 'transparent';
  329. dom.style.border = '1px solid transparent';
  330. dom.style.marginTop = '-2px';
  331. dom.style.marginLegt = '-2px';
  332. dom.style.padding = '2px';
  333. dom.style.cursor = 'col-resize';
  334. dom.value = '0.00';
  335. this.dom = dom;
  336. this.setValue( number );
  337. this.min = - Infinity;
  338. this.max = Infinity;
  339. this.precision = 2;
  340. this.step = 1;
  341. this.onChangeCallback = null;
  342. var distance = 0;
  343. var onMouseDownValue = 0;
  344. var onMouseDown = function ( event ) {
  345. event.preventDefault();
  346. distance = 0;
  347. onMouseDownValue = parseFloat( dom.value );
  348. document.addEventListener( 'mousemove', onMouseMove, false );
  349. document.addEventListener( 'mouseup', onMouseUp, false );
  350. };
  351. var onMouseMove = function ( event ) {
  352. var movementX = event.movementX || event.webkitMovementX || event.mozMovementX || 0;
  353. var movementY = event.movementY || event.webkitMovementY || event.mozMovementY || 0;
  354. distance += movementX - movementY;
  355. var number = onMouseDownValue + ( distance / ( event.shiftKey ? 10 : 100 ) ) * scope.step;
  356. dom.value = Math.min( scope.max, Math.max( scope.min, number ) ).toFixed( scope.precision );
  357. if ( scope.onChangeCallback ) scope.onChangeCallback();
  358. };
  359. var onMouseUp = function ( event ) {
  360. document.removeEventListener( 'mousemove', onMouseMove, false );
  361. document.removeEventListener( 'mouseup', onMouseUp, false );
  362. if ( Math.abs( distance ) < 2 ) {
  363. dom.focus();
  364. dom.select();
  365. }
  366. };
  367. var onChange = function ( event ) {
  368. var number = parseFloat( dom.value );
  369. if ( isNaN( number ) === false ) {
  370. dom.value = number;
  371. if ( scope.onChangeCallback ) scope.onChangeCallback();
  372. }
  373. };
  374. var onFocus = function ( event ) {
  375. dom.style.backgroundColor = '';
  376. dom.style.borderColor = '#ccc';
  377. dom.style.cursor = '';
  378. };
  379. var onBlur = function ( event ) {
  380. dom.style.backgroundColor = 'transparent';
  381. dom.style.borderColor = 'transparent';
  382. dom.style.cursor = 'col-resize';
  383. };
  384. dom.addEventListener( 'mousedown', onMouseDown, false );
  385. dom.addEventListener( 'change', onChange, false );
  386. dom.addEventListener( 'focus', onFocus, false );
  387. dom.addEventListener( 'blur', onBlur, false );
  388. return this;
  389. };
  390. UI.Number.prototype = Object.create( UI.Element.prototype );
  391. UI.Number.prototype.getValue = function () {
  392. return parseFloat( this.dom.value );
  393. };
  394. UI.Number.prototype.setValue = function ( value ) {
  395. if ( value !== undefined ) {
  396. this.dom.value = value.toFixed( this.precision );
  397. }
  398. return this;
  399. };
  400. UI.Number.prototype.setRange = function ( min, max ) {
  401. this.min = min;
  402. this.max = max;
  403. return this;
  404. };
  405. UI.Number.prototype.setPrecision = function ( precision ) {
  406. this.precision = precision;
  407. if ( precision > 2 ) {
  408. this.step = Math.pow( 10, -( precision - 1 ) );
  409. }
  410. return this;
  411. };
  412. UI.Number.prototype.onChange = function ( callback ) {
  413. this.onChangeCallback = callback;
  414. return this;
  415. };
  416. // Break
  417. UI.Break = function () {
  418. UI.Element.call( this );
  419. var dom = document.createElement( 'br' );
  420. dom.className = 'Break';
  421. this.dom = dom;
  422. return this;
  423. };
  424. UI.Break.prototype = Object.create( UI.Element.prototype );
  425. // HorizontalRule
  426. UI.HorizontalRule = function () {
  427. UI.Element.call( this );
  428. var dom = document.createElement( 'hr' );
  429. dom.className = 'HorizontalRule';
  430. this.dom = dom;
  431. return this;
  432. };
  433. UI.HorizontalRule.prototype = Object.create( UI.Element.prototype );
  434. // Button
  435. UI.Button = function () {
  436. UI.Element.call( this );
  437. var scope = this;
  438. var dom = document.createElement( 'button' );
  439. dom.className = 'Button';
  440. this.dom = dom;
  441. this.onClickCallback = null;
  442. this.dom.addEventListener( 'click', function ( event ) {
  443. scope.onClickCallback();
  444. }, false );
  445. return this;
  446. };
  447. UI.Button.prototype = Object.create( UI.Element.prototype );
  448. UI.Button.prototype.setLabel = function ( value ) {
  449. this.dom.textContent = value;
  450. return this;
  451. };
  452. UI.Button.prototype.onClick = function ( callback ) {
  453. this.onClickCallback = callback;
  454. return this;
  455. };
粤ICP备19079148号