- Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsnapcode.js
More file actions
Latest commit
208 lines (174 loc) · 6.78 KB
/
Copy pathsnapcode.js
File metadata and controls
208 lines (174 loc) · 6.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
importLZStringfrom'./lz-string.min.js';
// Get URL to SnapCode
varSNAPCODE_URL='https://reportmill.com/SnapCode/app/app09?loader=none#embed:';
if(window.location.host.includes('localhost'))
SNAPCODE_URL='http://localhost:8080?loader=none#embed:';
constMIN_HEIGHT=260;
constSnapCode={
addSnapCodeToElementForId,
addPlayButtonToElementForId
};
//
// Adds SnapCode to given element.
//
functionaddSnapCodeToElementForId(containerId)
{
// Remove any existing runner
removeExistingSnapCodeRunner();
// Get container and ensure it can host absolutely positioned children
constcontainer=document.getElementById(containerId);
if(getComputedStyle(container).position==="static")
container.style.position="relative";
// Remove play button
constplayButton=container.querySelector('#SnapCodeRunnerPlayButton');
if(playButton!==null)
container.removeChild(playButton);
// Make sure it's at least MIN_HEIGHT tall
constcontainerOriginalHeight=container.style.height;
constcontainerOriginalClientHeight=container.clientHeight;
if(containerOriginalClientHeight<MIN_HEIGHT)
container.style.height=MIN_HEIGHT+'px';
// Get java string from parent element and Base64 encode with lzwstring
constjavaStr=container.innerText;
varjavaStrLzw=LZString.compressToEncodedURIComponent(javaStr);
// Create iframe for parent element
constiframe=document.createElement("iframe");
iframe.id='SnapCodeRunner';
iframe.originalHeight=containerOriginalHeight;
iframe.originalClientHeight=containerOriginalClientHeight;
iframe.src=SNAPCODE_URL+javaStrLzw;
iframe.style.border="none";
iframe.style.position='absolute';
iframe.style.top='0';
iframe.style.left='0';
iframe.style.width='100%';
iframe.style.height='100%';
iframe.style.zIndex='9999';
iframe.style.display="block";// avoids scrollbar glitches
// Append iframe to container
container.appendChild(iframe);
// Create close button, add action and add to container
constcloseButton=getCloseButton();
closeButton.addEventListener("click",()=>{removeExistingSnapCodeRunner(containerId);});
container.appendChild(closeButton);
}
//
// Adds a SnapCode play button to element for given id.
//
functionaddPlayButtonToElementForId(containerId)
{
addFontAwesome();
// Get container and ensure it can host absolutely positioned children
constcontainer=document.getElementById(containerId);
if(getComputedStyle(container).position==="static")
container.style.position="relative";
// If height not explicitly set, explicitly set it
if(container.style.height===""){
container.style.width=container.offsetWidth+'px';
container.style.height=container.offsetHeight+'px';
container.style.boxSizing='border-box';
}
// Get play button, add action and add to container
constplayButton=getPlayButton();
playButton.addEventListener("click",()=>{addSnapCodeToElementForId(containerId);});
container.appendChild(playButton);
}
//
// Returns a play button.
//
functiongetPlayButton()
{
// Create the button
constbutton=document.createElement("button");
button.id='SnapCodeRunnerPlayButton';
button.style.fontSize="18px";
button.style.border="none";
button.style.background="none";
button.style.color="lightgray";
button.style.cursor="pointer";
button.style.transition="color 0.3s";
button.style.position="absolute";
button.style.top="10px";
button.style.right="10px";
// Add the icon
consticon=document.createElement("i");
icon.className="fa-solid fa-play";
button.appendChild(icon);
// Hover effect
button.addEventListener("mouseover",()=>{button.style.color="green";});
button.addEventListener("mouseout",()=>{button.style.color="lightgray";});
// Return
returnbutton;
}
//
// Returns a close button.
//
functiongetCloseButton()
{
// Create the button
constbutton=document.createElement("button");
button.id='SnapCodeRunnerCloseButton';
button.style.fontSize="18px";
button.style.border="none";
button.style.background="none";
button.style.color="lightgray";
button.style.cursor="pointer";
button.style.transition="color 0.3s";
button.style.position="absolute";
button.style.top="7px";
button.style.right="6px";
button.style.zIndex='99999';
// Add the icon
consticon=document.createElement("i");
icon.className="fa-solid fa-xmark";
button.appendChild(icon);
// Hover effect
button.addEventListener("mouseover",()=>{button.style.color="close";});
button.addEventListener("mouseout",()=>{button.style.color="lightgray";});
// Return
returnbutton;
}
//
// Removes existing SnapCode runner
//
functionremoveExistingSnapCodeRunner()
{
// If SnapCodePrimer still set, remove
constsnapCodePrimer=document.getElementById('SnapCodePrimer');
if(snapCodePrimer!==null)
document.body.removeChild(snapCodePrimer);
// Get old SnapCode runner (just return if not found)
constoldSnapCodeRunner=document.getElementById('SnapCodeRunner');
if(oldSnapCodeRunner===null)
return;
// If old runner was too small, restore original parent container height
if(oldSnapCodeRunner.originalClientHeight<MIN_HEIGHT)
oldSnapCodeRunner.parentNode.style.height=oldSnapCodeRunner.originalHeight;
// Remove old runner from its parent container
constcontainer=oldSnapCodeRunner.parentNode;
container.removeChild(oldSnapCodeRunner);
// Remove close button
constcloseButton=document.getElementById('SnapCodeRunnerCloseButton');
if(closeButton!==null)
container.removeChild(closeButton);
// Add play button back
addPlayButtonToElementForId(container.id);
}
//
// Adds this: <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.0/css/all.min.css">
//
functionaddFontAwesome()
{
if(window.isFontAwesome)return;window.isFontAwesome=true;
constlinkElement=document.createElement('link');
linkElement.rel='stylesheet';
linkElement.href='https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.0/css/all.min.css';
document.head.appendChild(linkElement);
// Create iframe to load SnapCode to make sure it's hot
consthiddenIframe=document.createElement('iframe');
hiddenIframe.id='SnapCodePrimer';
hiddenIframe.src=SNAPCODE_URL+LZString.compressToEncodedURIComponent('IO.print("SnapCode is primed");');
hiddenIframe.style.display='none';
document.body.appendChild(hiddenIframe);
}
exportdefaultSnapCode;