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