ui.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964
  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.value = number;
  384. }
  385. };
  386. var onFocus = function ( event ) {
  387. dom.style.backgroundColor = '';
  388. dom.style.borderColor = '#ccc';
  389. dom.style.cursor = '';
  390. };
  391. var onBlur = function ( event ) {
  392. dom.style.backgroundColor = 'transparent';
  393. dom.style.borderColor = 'transparent';
  394. dom.style.cursor = 'col-resize';
  395. };
  396. dom.addEventListener( 'mousedown', onMouseDown, false );
  397. dom.addEventListener( 'change', onChange, false );
  398. dom.addEventListener( 'focus', onFocus, false );
  399. dom.addEventListener( 'blur', onBlur, false );
  400. return this;
  401. };
  402. UI.Number.prototype = Object.create( UI.Element.prototype );
  403. UI.Number.prototype.getValue = function () {
  404. return parseFloat( this.dom.value );
  405. };
  406. UI.Number.prototype.setValue = function ( value ) {
  407. if ( value !== undefined ) {
  408. this.dom.value = value.toFixed( this.precision );
  409. }
  410. return this;
  411. };
  412. UI.Number.prototype.setRange = function ( min, max ) {
  413. this.min = min;
  414. this.max = max;
  415. return this;
  416. };
  417. UI.Number.prototype.setPrecision = function ( precision ) {
  418. this.precision = precision;
  419. return this;
  420. };
  421. // Integer
  422. UI.Integer = function ( number ) {
  423. UI.Element.call( this );
  424. var scope = this;
  425. var dom = document.createElement( 'input' );
  426. dom.className = 'Number';
  427. dom.style.color = '#0080f0';
  428. dom.style.fontSize = '12px';
  429. dom.style.backgroundColor = 'transparent';
  430. dom.style.border = '1px solid transparent';
  431. dom.style.marginTop = '-2px';
  432. dom.style.marginLegt = '-2px';
  433. dom.style.padding = '2px';
  434. dom.style.cursor = 'col-resize';
  435. dom.value = '0.00';
  436. dom.addEventListener( 'keydown', function ( event ) {
  437. event.stopPropagation();
  438. }, false );
  439. this.min = - Infinity;
  440. this.max = Infinity;
  441. this.step = 1;
  442. this.dom = dom;
  443. this.setValue( number );
  444. var changeEvent = document.createEvent( 'HTMLEvents' );
  445. changeEvent.initEvent( 'change', true, true );
  446. var distance = 0;
  447. var onMouseDownValue = 0;
  448. var onMouseDown = function ( event ) {
  449. event.preventDefault();
  450. distance = 0;
  451. onMouseDownValue = parseFloat( dom.value );
  452. document.addEventListener( 'mousemove', onMouseMove, false );
  453. document.addEventListener( 'mouseup', onMouseUp, false );
  454. };
  455. var onMouseMove = function ( event ) {
  456. var currentValue = dom.value;
  457. var movementX = event.movementX || event.webkitMovementX || event.mozMovementX || 0;
  458. var movementY = event.movementY || event.webkitMovementY || event.mozMovementY || 0;
  459. distance += movementX - movementY;
  460. var number = onMouseDownValue + ( distance / ( event.shiftKey ? 5 : 50 ) ) * scope.step;
  461. dom.value = Math.min( scope.max, Math.max( scope.min, number ) ) | 0;
  462. if ( currentValue !== dom.value ) dom.dispatchEvent( changeEvent );
  463. };
  464. var onMouseUp = function ( event ) {
  465. document.removeEventListener( 'mousemove', onMouseMove, false );
  466. document.removeEventListener( 'mouseup', onMouseUp, false );
  467. if ( Math.abs( distance ) < 2 ) {
  468. dom.focus();
  469. dom.select();
  470. }
  471. };
  472. var onChange = function ( event ) {
  473. var number = parseInt( dom.value );
  474. if ( isNaN( number ) === false ) {
  475. dom.value = number;
  476. }
  477. };
  478. var onFocus = function ( event ) {
  479. dom.style.backgroundColor = '';
  480. dom.style.borderColor = '#ccc';
  481. dom.style.cursor = '';
  482. };
  483. var onBlur = function ( event ) {
  484. dom.style.backgroundColor = 'transparent';
  485. dom.style.borderColor = 'transparent';
  486. dom.style.cursor = 'col-resize';
  487. };
  488. dom.addEventListener( 'mousedown', onMouseDown, false );
  489. dom.addEventListener( 'change', onChange, false );
  490. dom.addEventListener( 'focus', onFocus, false );
  491. dom.addEventListener( 'blur', onBlur, false );
  492. return this;
  493. };
  494. UI.Integer.prototype = Object.create( UI.Element.prototype );
  495. UI.Integer.prototype.getValue = function () {
  496. return parseInt( this.dom.value );
  497. };
  498. UI.Integer.prototype.setValue = function ( value ) {
  499. if ( value !== undefined ) {
  500. this.dom.value = value | 0;
  501. }
  502. return this;
  503. };
  504. UI.Integer.prototype.setRange = function ( min, max ) {
  505. this.min = min;
  506. this.max = max;
  507. return this;
  508. };
  509. // Break
  510. UI.Break = function () {
  511. UI.Element.call( this );
  512. var dom = document.createElement( 'br' );
  513. dom.className = 'Break';
  514. this.dom = dom;
  515. return this;
  516. };
  517. UI.Break.prototype = Object.create( UI.Element.prototype );
  518. // HorizontalRule
  519. UI.HorizontalRule = function () {
  520. UI.Element.call( this );
  521. var dom = document.createElement( 'hr' );
  522. dom.className = 'HorizontalRule';
  523. this.dom = dom;
  524. return this;
  525. };
  526. UI.HorizontalRule.prototype = Object.create( UI.Element.prototype );
  527. // Button
  528. UI.Button = function ( value ) {
  529. UI.Element.call( this );
  530. var scope = this;
  531. var dom = document.createElement( 'button' );
  532. dom.className = 'Button';
  533. this.dom = dom;
  534. this.dom.textContent = value;
  535. return this;
  536. };
  537. UI.Button.prototype = Object.create( UI.Element.prototype );
  538. UI.Button.prototype.setLabel = function ( value ) {
  539. this.dom.textContent = value;
  540. return this;
  541. };