ui.js 17 KB

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