jswindow.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. // creation args: title (string), icon(url string)
  2. // open args: width, height, left, top (all are numbers)
  3. function jswindow(args) {
  4. // specify options here
  5. var borderThickness = 2;
  6. var defaultWidth = 250;
  7. var defaultHeight = 150;
  8. var topClip = 0;
  9. var bottomClip = 24;
  10. var leftClip = 48;
  11. var rightClip = 24;
  12. // vWindow instead of this for event.target workaround
  13. var vWindow = this;
  14. function randInt(min, max) { return Math.floor(Math.random() * (max - min + 1)) + min; }
  15. function clipRight() { return parseInt(vWindow.outerWindow.style.width) + parseInt(vWindow.outerWindow.style.left) + (borderThickness * 2) > window.innerWidth; }
  16. function clipBottom() { return parseInt(vWindow.outerWindow.style.height) + parseInt(vWindow.outerWindow.style.top) + (borderThickness * 2) > window.innerHeight; }
  17. this.setTitle = function(text) {
  18. vWindow.outerWindow.bar.wname.textContent = text;
  19. }
  20. this.setIcon = function(url) {
  21. vWindow.outerWindow.bar.icon.style.backgroundImage = 'url("' + url + '")';
  22. }
  23. // open window with optional width and height (will otherwise be default) and distances from the top left (otherwise random)
  24. this.open = function(args) {
  25. vWindow.outerWindow.style.width = (args && args.width ? args.width + "px" : defaultWidth + "px");
  26. vWindow.outerWindow.style.height = (args && args.height ? args.height + "px" : defaultHeight + "px");
  27. vWindow.outerWindow.style.left = (args && args.left ? args.left + "px" : randInt(0, window.innerWidth - parseInt(vWindow.outerWindow.style.width)) + "px");
  28. vWindow.outerWindow.style.top = (args && args.top ? args.top + "px" : randInt(0, window.innerHeight - parseInt(vWindow.outerWindow.style.height)) + "px");
  29. vWindow.outerWindow.style.maxWidth = (window.innerWidth - (parseInt(vWindow.outerWindow.style.left) + (borderThickness * 2))) + "px";
  30. vWindow.outerWindow.style.maxHeight = (window.innerHeight - (parseInt(vWindow.outerWindow.style.top) + (borderThickness * 2))) + "px";
  31. document.body.appendChild(vWindow.outerWindow);
  32. }
  33. this.onclose = function() {}
  34. this.close = function() {
  35. vWindow.outerWindow.remove();
  36. vWindow.onclose();
  37. }
  38. // start constructor creation
  39. // make nodes
  40. vWindow.outerWindow = document.createElement("div");
  41. vWindow.outerWindow.classList.add("window");
  42. vWindow.outerWindow.bar = document.createElement("div");
  43. vWindow.outerWindow.bar.classList.add("windowbar");
  44. if (args && args.icon) {
  45. vWindow.outerWindow.bar.icon = document.createElement("span");
  46. vWindow.outerWindow.bar.icon.classList.add("windowicon");
  47. vWindow.outerWindow.bar.icon.style.backgroundImage = 'url("' + args.icon + '")';
  48. vWindow.outerWindow.bar.appendChild(vWindow.outerWindow.bar.icon);
  49. }
  50. vWindow.outerWindow.bar.wname = document.createElement("span");
  51. vWindow.outerWindow.bar.wname.appendChild(document.createTextNode(args && args.title ? args.title : ""));
  52. vWindow.outerWindow.bar.wname.classList.add("windowtitle");
  53. vWindow.outerWindow.bar.close = document.createElement("span");
  54. vWindow.outerWindow.bar.close.appendChild(document.createTextNode(String.fromCharCode(10006)));
  55. vWindow.outerWindow.bar.close.classList.add("windowclose");
  56. vWindow.outerWindow.bar.close.title = "Close";
  57. vWindow.outerWindow.bar.close.onclick = vWindow.close;
  58. vWindow.innerWindow = document.createElement("div");
  59. vWindow.innerWindow.classList.add("windowcontent");
  60. // icon already appended if specified
  61. vWindow.outerWindow.bar.appendChild(vWindow.outerWindow.bar.wname);
  62. vWindow.outerWindow.bar.appendChild(vWindow.outerWindow.bar.close);
  63. vWindow.outerWindow.appendChild(vWindow.outerWindow.bar);
  64. vWindow.outerWindow.appendChild(vWindow.innerWindow);
  65. // move window to front
  66. vWindow.outerWindow.addEventListener("mousedown", function(e) {
  67. var allwindows = Array.from(document.querySelectorAll("div.window"));
  68. if ((allwindows.indexOf(vWindow.outerWindow) != (allwindows.length - 1)) && e.target != vWindow.outerWindow.bar.close) document.body.appendChild(vWindow.outerWindow);
  69. }, false);
  70. // move window around
  71. var oldcursorX, oldcursorY;
  72. vWindow.outerWindow.bar.addEventListener("mousedown", function(e) {
  73. if ((e.target != vWindow.outerWindow.bar.close) && (e.button == 0)) {
  74. e.preventDefault();
  75. oldcursorX = e.clientX;
  76. oldcursorY = e.clientY;
  77. document.addEventListener("mousemove", windowDrag, false);
  78. document.addEventListener("mouseup", windowDragEnd, false);
  79. }
  80. }, false);
  81. function windowDrag(e) {
  82. e.preventDefault();
  83. vWindow.outerWindow.style.left = (vWindow.outerWindow.offsetLeft - (oldcursorX - e.clientX)) + "px";
  84. oldcursorX = e.clientX;
  85. vWindow.outerWindow.style.top = (vWindow.outerWindow.offsetTop - (oldcursorY - e.clientY)) + "px";
  86. oldcursorY = e.clientY;
  87. }
  88. // pop window back into view area, and set max dimensions if it's not clipping into bottom right
  89. function windowDragEnd() {
  90. document.removeEventListener("mousemove", windowDrag);
  91. document.removeEventListener("mouseup", windowDragEnd);
  92. vWindow.outerWindow.style.left = Math.min(Math.max(vWindow.outerWindow.offsetLeft, 0 - parseInt(vWindow.outerWindow.style.width) + leftClip), window.innerWidth - rightClip) + "px";
  93. vWindow.outerWindow.style.top = Math.min(Math.max(vWindow.outerWindow.offsetTop, topClip), window.innerHeight - bottomClip) + "px";
  94. if (!clipRight()) vWindow.outerWindow.style.maxWidth = (window.innerWidth - (parseInt(vWindow.outerWindow.style.left) + (borderThickness * 2))) + "px";
  95. if (!clipBottom()) vWindow.outerWindow.style.maxHeight = (window.innerHeight - (parseInt(vWindow.outerWindow.style.top) + (borderThickness * 2))) + "px";
  96. }
  97. // end constructor creation
  98. }