UI.js 14 KB

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