ui.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968
  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', '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. this.dom = dom;
  116. this.setValue( text );
  117. return this;
  118. };
  119. UI.Text.prototype = Object.create( UI.Element.prototype );
  120. UI.Text.prototype.setValue = function ( value ) {
  121. if ( value !== undefined ) {
  122. this.dom.textContent = value;
  123. }
  124. return this;
  125. };
  126. // Input
  127. UI.Input = function () {
  128. UI.Element.call( this );
  129. var scope = this;
  130. var dom = document.createElement( 'input' );
  131. dom.className = 'Input';
  132. dom.style.padding = '2px';
  133. dom.style.marginTop = '-2px';
  134. dom.style.marginLeft = '-2px';
  135. dom.style.border = '1px solid #ccc';
  136. dom.addEventListener( 'keydown', function ( event ) {
  137. event.stopPropagation();
  138. }, false );
  139. this.dom = dom;
  140. return this;
  141. };
  142. UI.Input.prototype = Object.create( UI.Element.prototype );
  143. UI.Input.prototype.getValue = function () {
  144. return this.dom.value;
  145. };
  146. UI.Input.prototype.setValue = function ( value ) {
  147. this.dom.value = value;
  148. return this;
  149. };
  150. // TextArea
  151. UI.TextArea = function () {
  152. UI.Element.call( this );
  153. var scope = this;
  154. var dom = document.createElement( 'textarea' );
  155. dom.className = 'TextArea';
  156. dom.style.padding = '2px';
  157. dom.style.marginTop = '-2px';
  158. dom.style.marginLeft = '-2px';
  159. dom.style.border = '1px solid #ccc';
  160. dom.addEventListener( 'keydown', function ( event ) {
  161. event.stopPropagation();
  162. }, false );
  163. this.dom = dom;
  164. return this;
  165. };
  166. UI.TextArea.prototype = Object.create( UI.Element.prototype );
  167. UI.TextArea.prototype.getValue = function () {
  168. return this.dom.value;
  169. };
  170. UI.TextArea.prototype.setValue = function ( value ) {
  171. this.dom.value = value;
  172. return this;
  173. };
  174. // Select
  175. UI.Select = function () {
  176. UI.Element.call( this );
  177. var scope = this;
  178. var dom = document.createElement( 'select' );
  179. dom.className = 'Select';
  180. dom.style.width = '64px';
  181. dom.style.height = '16px';
  182. dom.style.border = '0px';
  183. dom.style.padding = '0px';
  184. this.dom = dom;
  185. return this;
  186. };
  187. UI.Select.prototype = Object.create( UI.Element.prototype );
  188. UI.Select.prototype.setMultiple = function ( boolean ) {
  189. this.dom.multiple = boolean;
  190. return this;
  191. };
  192. UI.Select.prototype.setOptions = function ( options ) {
  193. var selected = this.dom.value;
  194. while ( this.dom.children.length > 0 ) {
  195. this.dom.removeChild( this.dom.firstChild );
  196. }
  197. for ( var key in options ) {
  198. var option = document.createElement( 'option' );
  199. option.value = key;
  200. option.innerHTML = options[ key ];
  201. this.dom.appendChild( option );
  202. }
  203. this.dom.value = selected;
  204. return this;
  205. };
  206. UI.Select.prototype.getValue = function () {
  207. return this.dom.value;
  208. };
  209. UI.Select.prototype.setValue = function ( value ) {
  210. this.dom.value = value;
  211. return this;
  212. };
  213. // FancySelect
  214. UI.FancySelect = function () {
  215. UI.Element.call( this );
  216. var scope = this;
  217. var dom = document.createElement( 'div' );
  218. dom.className = 'FancySelect';
  219. dom.style.background = '#fff';
  220. dom.style.border = '1px solid #ccc';
  221. dom.style.padding = '0';
  222. dom.style.cursor = 'default';
  223. dom.style.overflow = 'auto';
  224. this.dom = dom;
  225. this.options = [];
  226. this.selectedKeys = null;
  227. return this;
  228. };
  229. UI.FancySelect.prototype = Object.create( UI.Element.prototype );
  230. UI.FancySelect.prototype.setOptions = function ( options ) {
  231. var scope = this;
  232. var changeEvent = document.createEvent( 'HTMLEvents' );
  233. changeEvent.initEvent( 'change', true, true );
  234. while ( scope.dom.children.length > 0 ) {
  235. scope.dom.removeChild( scope.dom.firstChild );
  236. }
  237. scope.options = [];
  238. var generateOptionCallback = function ( element, key ) {
  239. return function ( event ) {
  240. scope.selectedKeys = [key];
  241. scope.dom.dispatchEvent( changeEvent );
  242. }
  243. };
  244. for ( var key in options ) {
  245. var option = document.createElement( 'div' );
  246. option.style.padding = '4px';
  247. option.style.whiteSpace = 'nowrap';
  248. option.innerHTML = options[ key ];
  249. option.value = key;
  250. scope.dom.appendChild( option );
  251. scope.options.push( option );
  252. option.addEventListener( 'click', generateOptionCallback( option, key ), false );
  253. }
  254. return scope;
  255. };
  256. UI.FancySelect.prototype.getValue = function () {
  257. return this.selectedKeys;
  258. };
  259. UI.FancySelect.prototype.setValue = function ( keys ) {
  260. // must convert raw keys into string for compatibility with UI.Select
  261. // which uses string keys (initialized from options keys)
  262. keys = ( keys instanceof Array ) ? keys : [keys];
  263. for ( var i = 0; i < this.options.length; i ++ ) {
  264. this.options[ i ].style.backgroundColor = '';
  265. }
  266. for ( var i in keys ) {
  267. var key = keys[ i ] ? keys[ i ].toString() : keys[ i ];
  268. for ( var j = 0; j < this.options.length; j ++ ) {
  269. if ( this.options[ j ].value === key ) {
  270. this.options[ j ].style.backgroundColor = '#f0f0f0';
  271. }
  272. }
  273. }
  274. this.selectedKeys = keys;
  275. return this;
  276. };
  277. // Checkbox
  278. UI.Checkbox = function ( boolean ) {
  279. UI.Element.call( this );
  280. var scope = this;
  281. var dom = document.createElement( 'input' );
  282. dom.className = 'Checkbox';
  283. dom.type = 'checkbox';
  284. this.dom = dom;
  285. this.setValue( boolean );
  286. return this;
  287. };
  288. UI.Checkbox.prototype = Object.create( UI.Element.prototype );
  289. UI.Checkbox.prototype.getValue = function () {
  290. return this.dom.checked;
  291. };
  292. UI.Checkbox.prototype.setValue = function ( value ) {
  293. if ( value !== undefined ) {
  294. this.dom.checked = value;
  295. }
  296. return this;
  297. };
  298. // Color
  299. UI.Color = function () {
  300. UI.Element.call( this );
  301. var scope = this;
  302. var dom = document.createElement( 'input' );
  303. dom.className = 'Color';
  304. dom.style.width = '64px';
  305. dom.style.height = '16px';
  306. dom.style.border = '0px';
  307. dom.style.padding = '0px';
  308. dom.style.backgroundColor = 'transparent';
  309. try { dom.type = 'color'; } catch ( exception ) {}
  310. this.dom = dom;
  311. return this;
  312. };
  313. UI.Color.prototype = Object.create( UI.Element.prototype );
  314. UI.Color.prototype.getValue = function () {
  315. return this.dom.value;
  316. };
  317. UI.Color.prototype.getHexValue = function () {
  318. return parseInt( this.dom.value.substr( 1 ), 16 );
  319. };
  320. UI.Color.prototype.setValue = function ( value ) {
  321. this.dom.value = value;
  322. return this;
  323. };
  324. UI.Color.prototype.setHexValue = function ( hex ) {
  325. this.dom.value = "#" + ( '000000' + hex.toString( 16 ) ).slice( -6 );
  326. return this;
  327. };
  328. // Number
  329. UI.Number = function ( number ) {
  330. UI.Element.call( this );
  331. var scope = this;
  332. var dom = document.createElement( 'input' );
  333. dom.className = 'Number';
  334. dom.style.color = '#0080f0';
  335. dom.style.fontSize = '12px';
  336. dom.style.backgroundColor = 'transparent';
  337. dom.style.border = '1px solid transparent';
  338. dom.style.marginTop = '-2px';
  339. dom.style.marginLegt = '-2px';
  340. dom.style.padding = '2px';
  341. dom.style.cursor = 'col-resize';
  342. dom.value = '0.00';
  343. dom.addEventListener( 'keydown', function ( event ) {
  344. event.stopPropagation();
  345. }, false );
  346. this.min = - Infinity;
  347. this.max = Infinity;
  348. this.precision = 2;
  349. this.step = 1;
  350. this.dom = dom;
  351. this.setValue( number );
  352. var changeEvent = document.createEvent( 'HTMLEvents' );
  353. changeEvent.initEvent( 'change', true, true );
  354. var distance = 0;
  355. var onMouseDownValue = 0;
  356. var onMouseDown = function ( event ) {
  357. event.preventDefault();
  358. distance = 0;
  359. onMouseDownValue = parseFloat( dom.value );
  360. document.addEventListener( 'mousemove', onMouseMove, false );
  361. document.addEventListener( 'mouseup', onMouseUp, false );
  362. };
  363. var onMouseMove = function ( event ) {
  364. var currentValue = dom.value;
  365. var movementX = event.movementX || event.webkitMovementX || event.mozMovementX || 0;
  366. var movementY = event.movementY || event.webkitMovementY || event.mozMovementY || 0;
  367. distance += movementX - movementY;
  368. var number = onMouseDownValue + ( distance / ( event.shiftKey ? 5 : 50 ) ) * scope.step;
  369. dom.value = Math.min( scope.max, Math.max( scope.min, number ) ).toFixed( scope.precision );
  370. if ( currentValue !== dom.value ) dom.dispatchEvent( changeEvent );
  371. };
  372. var onMouseUp = function ( event ) {
  373. document.removeEventListener( 'mousemove', onMouseMove, false );
  374. document.removeEventListener( 'mouseup', onMouseUp, false );
  375. if ( Math.abs( distance ) < 2 ) {
  376. dom.focus();
  377. dom.select();
  378. }
  379. };
  380. var onChange = function ( event ) {
  381. var number = parseFloat( dom.value );
  382. if ( isNaN( number ) === false ) {
  383. dom.oldValue = dom.newValue;
  384. dom.newValue = number;
  385. dom.value = number;
  386. }
  387. };
  388. var onFocus = function ( event ) {
  389. dom.style.backgroundColor = '';
  390. dom.style.borderColor = '#ccc';
  391. dom.style.cursor = '';
  392. };
  393. var onBlur = function ( event ) {
  394. dom.style.backgroundColor = 'transparent';
  395. dom.style.borderColor = 'transparent';
  396. dom.style.cursor = 'col-resize';
  397. };
  398. dom.addEventListener( 'mousedown', onMouseDown, false );
  399. dom.addEventListener( 'change', onChange, false );
  400. dom.addEventListener( 'focus', onFocus, false );
  401. dom.addEventListener( 'blur', onBlur, false );
  402. return this;
  403. };
  404. UI.Number.prototype = Object.create( UI.Element.prototype );
  405. UI.Number.prototype.getValue = function () {
  406. return parseFloat( this.dom.value );
  407. };
  408. UI.Number.prototype.setValue = function ( value ) {
  409. if ( value !== undefined ) {
  410. this.dom.value = value.toFixed( this.precision );
  411. }
  412. return this;
  413. };
  414. UI.Number.prototype.setRange = function ( min, max ) {
  415. this.min = min;
  416. this.max = max;
  417. return this;
  418. };
  419. UI.Number.prototype.setPrecision = function ( precision ) {
  420. this.precision = precision;
  421. return this;
  422. };
  423. // Integer
  424. UI.Integer = function ( number ) {
  425. UI.Element.call( this );
  426. var scope = this;
  427. var dom = document.createElement( 'input' );
  428. dom.className = 'Number';
  429. dom.style.color = '#0080f0';
  430. dom.style.fontSize = '12px';
  431. dom.style.backgroundColor = 'transparent';
  432. dom.style.border = '1px solid transparent';
  433. dom.style.marginTop = '-2px';
  434. dom.style.marginLegt = '-2px';
  435. dom.style.padding = '2px';
  436. dom.style.cursor = 'col-resize';
  437. dom.value = '0.00';
  438. dom.addEventListener( 'keydown', function ( event ) {
  439. event.stopPropagation();
  440. }, false );
  441. this.min = - Infinity;
  442. this.max = Infinity;
  443. this.step = 1;
  444. this.dom = dom;
  445. this.setValue( number );
  446. var changeEvent = document.createEvent( 'HTMLEvents' );
  447. changeEvent.initEvent( 'change', true, true );
  448. var distance = 0;
  449. var onMouseDownValue = 0;
  450. var onMouseDown = function ( event ) {
  451. event.preventDefault();
  452. distance = 0;
  453. onMouseDownValue = parseFloat( dom.value );
  454. document.addEventListener( 'mousemove', onMouseMove, false );
  455. document.addEventListener( 'mouseup', onMouseUp, false );
  456. };
  457. var onMouseMove = function ( event ) {
  458. var currentValue = dom.value;
  459. var movementX = event.movementX || event.webkitMovementX || event.mozMovementX || 0;
  460. var movementY = event.movementY || event.webkitMovementY || event.mozMovementY || 0;
  461. distance += movementX - movementY;
  462. var number = onMouseDownValue + ( distance / ( event.shiftKey ? 5 : 50 ) ) * scope.step;
  463. dom.value = Math.min( scope.max, Math.max( scope.min, number ) ) | 0;
  464. if ( currentValue !== dom.value ) dom.dispatchEvent( changeEvent );
  465. };
  466. var onMouseUp = function ( event ) {
  467. document.removeEventListener( 'mousemove', onMouseMove, false );
  468. document.removeEventListener( 'mouseup', onMouseUp, false );
  469. if ( Math.abs( distance ) < 2 ) {
  470. dom.focus();
  471. dom.select();
  472. }
  473. };
  474. var onChange = function ( event ) {
  475. var number = parseInt( dom.value );
  476. if ( isNaN( number ) === false ) {
  477. dom.value = number;
  478. }
  479. };
  480. var onFocus = function ( event ) {
  481. dom.style.backgroundColor = '';
  482. dom.style.borderColor = '#ccc';
  483. dom.style.cursor = '';
  484. };
  485. var onBlur = function ( event ) {
  486. dom.style.backgroundColor = 'transparent';
  487. dom.style.borderColor = 'transparent';
  488. dom.style.cursor = 'col-resize';
  489. };
  490. dom.addEventListener( 'mousedown', onMouseDown, false );
  491. dom.addEventListener( 'change', onChange, false );
  492. dom.addEventListener( 'focus', onFocus, false );
  493. dom.addEventListener( 'blur', onBlur, false );
  494. return this;
  495. };
  496. UI.Integer.prototype = Object.create( UI.Element.prototype );
  497. UI.Integer.prototype.getValue = function () {
  498. return parseInt( this.dom.value );
  499. };
  500. UI.Integer.prototype.setValue = function ( value ) {
  501. if ( value !== undefined ) {
  502. this.dom.value = value | 0;
  503. }
  504. return this;
  505. };
  506. UI.Integer.prototype.setRange = function ( min, max ) {
  507. this.min = min;
  508. this.max = max;
  509. return this;
  510. };
  511. // Break
  512. UI.Break = function () {
  513. UI.Element.call( this );
  514. var dom = document.createElement( 'br' );
  515. dom.className = 'Break';
  516. this.dom = dom;
  517. return this;
  518. };
  519. UI.Break.prototype = Object.create( UI.Element.prototype );
  520. // HorizontalRule
  521. UI.HorizontalRule = function () {
  522. UI.Element.call( this );
  523. var dom = document.createElement( 'hr' );
  524. dom.className = 'HorizontalRule';
  525. this.dom = dom;
  526. return this;
  527. };
  528. UI.HorizontalRule.prototype = Object.create( UI.Element.prototype );
  529. // Button
  530. UI.Button = function ( value ) {
  531. UI.Element.call( this );
  532. var scope = this;
  533. var dom = document.createElement( 'button' );
  534. dom.className = 'Button';
  535. this.dom = dom;
  536. this.dom.textContent = value;
  537. return this;
  538. };
  539. UI.Button.prototype = Object.create( UI.Element.prototype );
  540. UI.Button.prototype.setLabel = function ( value ) {
  541. this.dom.textContent = value;
  542. return this;
  543. };