ui.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /**
  2. * *REALLY* Small framework for creating/manipulating/deleting gui elements in a browser.
  3. *
  4. * TODO / TBI
  5. * . finer append_*() layout control (as opposed to current append at the end of the window)
  6. * . finer print control (as opposed to current append at the end of main window)
  7. * . replicate ui.EVENTS for style properties
  8. * . complete this list
  9. *
  10. * Author: Raphael Mannadiar
  11. * Date: 2014/08/14
  12. */
  13. ['svg'].forEach(
  14. function(dependency) {if( this[dependency] === undefined ) throw 'Missing dependency: ' + dependency;});
  15. ui = {};
  16. ui.window = window;
  17. ui.__nextWindowId = 0;
  18. ui.EVENTS = {
  19. KEY_PRESS: 'keydown',
  20. MOUSE_CLICK: 'click',
  21. MOUSE_MOVE: 'mousemove',
  22. MOUSE_PRESS: 'mousedown',
  23. MOUSE_RELEASE: 'mouseup',
  24. MOUSE_RIGHT_CLICK: 'contextmenu',
  25. WINDOW_CLOSE: 'unload'
  26. };
  27. ui.MOUSE_BUTTONS = {
  28. LEFT: 0,
  29. MIDDLE: 1,
  30. RIGHT: 2
  31. };
  32. ui.KEYCODES = {
  33. DELETE: 46
  34. };
  35. ui.append_button = function(_window,text) {
  36. var button = _window.document.createElement('button');
  37. button.innerHTML = text;
  38. _window.document.body.appendChild(button);
  39. return ui.wrap_element(button);
  40. };
  41. ui.append_canvas = function(_window,width,height,style) {
  42. var canvas = _window.document.createElementNS('http://www.w3.org/2000/svg', 'svg');
  43. canvas.setAttribute('width', width);
  44. canvas.setAttribute('height', height);
  45. for( var key in style )
  46. canvas.style[key] = style[key];
  47. _window.document.body.appendChild(canvas);
  48. return new svg.canvas_wrapper(canvas);
  49. };
  50. ui.bind_event = function(source,event,target,raise_name,port,time_offset) {
  51. if( port === undefined) port = "ui";
  52. if( time_offset === undefined ) time_offset = 0.0;
  53. var args = [];
  54. switch(event) {
  55. case ui.EVENTS.KEY_PRESS:
  56. args = function(e) {return [e.keyCode, e.view]};
  57. source = source.document;
  58. break;
  59. case ui.EVENTS.MOUSE_CLICK:
  60. case ui.EVENTS.MOUSE_MOVE:
  61. case ui.EVENTS.MOUSE_PRESS:
  62. case ui.EVENTS.MOUSE_RELEASE:
  63. case ui.EVENTS.MOUSE_RIGHT_CLICK:
  64. args = function(e) {return [e.clientX, e.clientY, e.button]};
  65. if( source instanceof svg.element_wrapper )
  66. source = source.element;
  67. break;
  68. case ui.EVENTS.WINDOW_CLOSE:
  69. args = function() {return [source]};
  70. break;
  71. default:
  72. throw 'Unsupported event';
  73. }
  74. source.addEventListener(
  75. event,
  76. (function (target, raise_name, port, time_offset) {
  77. return function(e) {
  78. e.preventDefault();
  79. e.stopPropagation();
  80. target.addInput(new Event(raise_name, port, args(e)), time_offset);};}
  81. )(target, raise_name, port, time_offset));
  82. };
  83. ui.close_window = function(_window) {
  84. _window.close();
  85. };
  86. ui.log = function(value) {
  87. if( typeof(value) == 'object' )
  88. console.log(JSON.stringify(value));
  89. else
  90. console.log(value);
  91. };
  92. ui.new_window = function(width,height) {
  93. return window.open('',ui.__nextWindowId++,'width='+width+',height='+height);
  94. };
  95. ui.println = function(value,target) {
  96. if( target === undefined ) target = window.document.body;
  97. if( typeof(value) == 'object' )
  98. target.innerHTML += JSON.stringify(value);
  99. else
  100. target.innerHTML += value;
  101. target.innerHTML += '<br/>'
  102. };
  103. ui.wrap_element = function(element) {
  104. return {element:element};
  105. };