ui.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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,cond) {
  51. if( port === undefined) port = "ui";
  52. if( time_offset === undefined ) time_offset = 0.0;
  53. if( cond === undefined) cond = function(e) {return true;};
  54. var args = [];
  55. switch(event) {
  56. case ui.EVENTS.KEY_PRESS:
  57. args = function(e) {return [e.keyCode, e.view]};
  58. source = source.document;
  59. break;
  60. case ui.EVENTS.MOUSE_CLICK:
  61. case ui.EVENTS.MOUSE_MOVE:
  62. case ui.EVENTS.MOUSE_PRESS:
  63. cond = function(e) {return e.button == 0;}
  64. case ui.EVENTS.MOUSE_RELEASE:
  65. cond = function(e) {return e.button == 0;}
  66. case ui.EVENTS.MOUSE_RIGHT_CLICK:
  67. args = function(e) {return [e.clientX, e.clientY, e.button]};
  68. if( source instanceof svg.element_wrapper )
  69. source = source.element;
  70. break;
  71. case ui.EVENTS.WINDOW_CLOSE:
  72. args = function() {return [source]};
  73. break;
  74. default:
  75. throw 'Unsupported event';
  76. }
  77. source.addEventListener(
  78. event,
  79. (function (target, raise_name, port, time_offset, cond) {
  80. return function(e) {
  81. if (cond(e)) {
  82. e.preventDefault();
  83. e.stopPropagation();
  84. target.addInput(new Event(raise_name, port, args(e)), time_offset);
  85. return false;
  86. }};}
  87. )(target, raise_name, port, time_offset, cond));
  88. };
  89. ui.close_window = function(_window) {
  90. _window.close();
  91. };
  92. ui.log = function(value) {
  93. if( typeof(value) == 'object' )
  94. console.log(JSON.stringify(value));
  95. else
  96. console.log(value);
  97. };
  98. ui.new_window = function(width,height) {
  99. // return window.open('',ui.__nextWindowId++,'width='+width+',height='+height);
  100. return window;
  101. };
  102. ui.println = function(value,target) {
  103. if( target === undefined ) target = window.document.body;
  104. if( typeof(value) == 'object' )
  105. target.innerHTML += JSON.stringify(value);
  106. else
  107. target.innerHTML += value;
  108. target.innerHTML += '<br/>'
  109. };
  110. ui.wrap_element = function(element) {
  111. return {element:element};
  112. };