Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemath
GitHub Repository: sagemath/sagemath.github.io
Path: blob/master/calctut/calctut-res/zoom.js
2467 views
1
// --------------------------------------------------------------------
2
// Javascript Magnifier v 0.97
3
// Written by Dino Termini - [email protected] - May 9, 2003
4
// This script is freeware (GPL) but if you use it, please let me know!
5
//
6
// Portions of code by zoomIN, zoomOUT
7
// Author: Nguyen Duong Minh (Obie) - [email protected]
8
// WWW: http://ObieWebsite.SourceForge.net
9
// License: GNU (GPL)
10
//
11
// Portions of code by Webreference Javascript Cookie Functions
12
// Jupirmedia Corporation
13
// http://www.internet.com
14
// --------------------------------------------------------------------
15
//
16
// Please refer to DEMO.htm file for details and usage
17
//
18
// --------------------------------------------------------------------
19
20
// Configuration parameters
21
// ------------------------
22
// Measure unit in pixel (px) or points (pt)
23
// measureUnit = "pt"
24
measureUnit = "px"
25
26
// Minimum size allowed for SIZE attribute (like in <FONT SIZE="1"> )
27
minSize = 1;
28
29
// Minimum size allowed for STYLE attribute (like in <FONT STYLE="font-size: 10px"> )
30
minStyleSize = 10;
31
32
// Maximum size allowed for SIZE attribute
33
maxSize = 6;
34
35
// Maximum size allowed for STYLE attribute
36
maxStyleSize = 30;
37
38
// Start size for tags with no SIZE attribute defined
39
startSize = 1;
40
41
// Start size for tags with no font-size STYLE or CLASS attribute defined
42
startStyleSize = 14;
43
44
// Increasing and decreasing step
45
stepSize = 1;
46
47
// Increasing step for STYLE definition (measure previously declared will be used)
48
stepStyleSize = 2;
49
50
// To set your own hotkeys, use key generator tool page included
51
// Keys to zooming in (with and without CAPS lock). Default: "+"
52
var keyin = 61;
53
var keyinCAPS = 43;
54
55
// Keys to zooming out (with and without CAPS lock). Default: "-"
56
var keyout = 45;
57
var keyoutCAPS = 95;
58
59
// Keys for "hard" zooming in (with and without CAPS lock). Default: ">"
60
var keyinIe = 46;
61
var keyinIeCAPS = 62;
62
63
// Keys for "hard" zooming out (with and without CAPS lock). Default: "<"
64
var keyoutIe = 44;
65
var keyoutIeCAPS = 60;
66
67
// "Hard" zoom factor
68
var zoomFactor = 1.1;
69
70
// Max zoom allowed
71
var maxZoom = 4.096;
72
73
// Min zoom allowed
74
var minZoom = 0.625;
75
76
// Initial decrease zoom
77
var startDecZoom = 0.7;
78
79
// Initial increase zoom
80
var startIncZoom = 1.3;
81
82
// Cookie expiry (default one year, actually 365 days)
83
// 365 days in a year
84
// 24 hours in a day
85
// 60 minutes in an hour
86
// 60 seconds in a minute
87
// 1000 milliseconds in a second
88
userExpiry = 365 * 24 * 60 * 60 * 1000;
89
90
// Enable or disable alert messages
91
alertEnabled = false;
92
93
// Allow input fields resize (text, buttons, and so on)
94
allowInputResize = false;
95
96
// End of configuration parameters. Please do not edit below this line
97
// --------------------------------------------------------------------------------
98
99
// Input values:
100
// name - name of the cookie
101
// value - value of the cookie
102
// [expires] - expiration date of the cookie (defaults to end of current session)
103
// [path] - path for which the cookie is valid (defaults to path of calling document)
104
// [domain] - domain for which the cookie is valid (defaults to domain of calling document)
105
// [secure] - Boolean value indicating if the cookie transmission requires a secure transmission
106
// * an argument defaults when it is assigned null as a placeholder
107
// * a null placeholder is not required for trailing omitted arguments
108
function setCookie(name, value, expires, path, domain, secure) {
109
return;
110
// Check whether cookies enabled
111
document.cookie = "Enabled=true";
112
var cookieValid = document.cookie;
113
114
// if retrieving the VALUE we just set actually works
115
// then we know cookies enabled
116
if (cookieValid.indexOf("Enabled=true") != -1) {
117
var curCookie = name + "=" + escape(value) +
118
((expires) ? "; expires=" + expires.toGMTString() : "") +
119
((path) ? "; path=" + path : "") +
120
((domain) ? "; domain=" + domain : "") +
121
((secure) ? "; secure" : "");
122
123
document.cookie = curCookie;
124
return(true);
125
}
126
else {
127
return(false);
128
}
129
}
130
131
// Input value:
132
// name - name of the desired cookie
133
// * return string containing value of specified cookie or null if cookie does not exist
134
function getCookie(name) {
135
var dc = document.cookie;
136
var prefix = name + "=";
137
var begin = dc.indexOf("; " + prefix);
138
if (begin == -1) {
139
begin = dc.indexOf(prefix);
140
if (begin != 0) return null;
141
} else
142
begin += 2;
143
var end = document.cookie.indexOf(";", begin);
144
if (end == -1)
145
end = dc.length;
146
return unescape(dc.substring(begin + prefix.length, end));
147
}
148
149
// Input values:
150
// name - name of the cookie
151
// [path] - path of the cookie (must be same as path used to create cookie)
152
// [domain] - domain of the cookie (must be same as domain used to create cookie)
153
// * path and domain default if assigned null or omitted if no explicit argument proceeds
154
function deleteCookie(name, path, domain) {
155
if (getCookie(name)) {
156
document.cookie = name + "=" +
157
((path) ? "; path=" + path : "") +
158
((domain) ? "; domain=" + domain : "") +
159
"; expires=Thu, 01-Jan-70 00:00:01 GMT";
160
}
161
}
162
163
// Input value:
164
// date - any instance of the Date object
165
// * hand all instances of the Date object to this function for "repairs"
166
function fixDate(date) {
167
var base = new Date(0);
168
var skew = base.getTime();
169
if (skew > 0)
170
date.setTime(date.getTime() - skew);
171
}
172
173
function searchTags(childTree, level) {
174
var retArray = new Array();
175
var tmpArray = new Array();
176
var j = 0;
177
var childName = "";
178
for (var i=0; i<childTree.length; i++) {
179
childName = childTree[i].nodeName;
180
if (childTree[i].hasChildNodes()) {
181
if ((childTree[i].childNodes.length == 1) && (childTree[i].childNodes[0].nodeName == "#text"))
182
retArray[j++] = childTree[i];
183
else {
184
tmpArray = searchTags(childTree[i].childNodes, level+1);
185
for (var k=0;k<tmpArray.length; k++)
186
retArray[j++] = tmpArray[k];
187
retArray[j++] = childTree[i];
188
}
189
}
190
else
191
retArray[j++] = childTree[i];
192
}
193
return(retArray);
194
}
195
196
function changeFontSize(stepSize, stepStyleSize, useCookie) {
197
if(stepSize == 0)
198
return;
199
if (document.body) {
200
var myObj = searchTags(document.body.childNodes, 0);
201
var myCookieSize = parseInt(getCookie("incrSize"));
202
var myCookieStyleSize = parseInt(getCookie("incrStyleSize"));
203
var myStepSize = stepSize;
204
var myStepStyleSize = stepStyleSize;
205
206
var now = new Date();
207
208
// Fix the bug in Navigator 2.0, Macintosh
209
fixDate(now);
210
211
if (isNaN(myCookieSize)) myCookieSize = 0;
212
if (isNaN(myCookieStyleSize)) myCookieStyleSize = 0;
213
214
// For debug purpose only
215
// if (!confirm("COOKIE: SIZE ["+myCookieSize+"] STYLESIZE ["+myCookieStyleSize+"]")) return(0);
216
217
// Check valid increment/decrement sizes or useCookie parameter
218
if (useCookie) {
219
myStepSize = myCookieSize;
220
myStepStyleSize = myCookieStyleSize;
221
}
222
223
now.setTime(now.getTime() + userExpiry);
224
myObjNumChilds = myObj.length;
225
var when = false;
226
for (i=0; i<myObjNumChilds; i++) {
227
if(!when) {
228
if(myObj[i].className && myObj[i].className == "narrow txt")
229
when = true;
230
else
231
continue;
232
}
233
myObjName = myObj[i].nodeName;
234
235
// Only some tags will be parsed
236
if (myObjName != "#text" && myObjName != "HTML" &&
237
myObjName != "HEAD" && myObjName != "TITLE" &&
238
myObjName != "STYLE" && myObjName != "SCRIPT" &&
239
myObjName != "BR" && myObjName != "TBODY" &&
240
myObjName != "#comment" && myObjName != "FORM") {
241
242
// Skip INPUT fields, if required
243
if (!allowInputResize && myObjName == "INPUT" || myObjName == "SELECT" || myObjName == "OPTION") continue;
244
245
if(myObj[i].className && myObj[i].className.indexOf("noresize") != -1) continue;
246
247
size = parseInt(myObj[i].getAttribute("size"));
248
249
// Internet Explorer uses a different DOM implementation
250
if (myObj[i].currentStyle)
251
styleSize = parseInt(myObj[i].currentStyle.fontSize);
252
else
253
styleSize = parseInt(window.getComputedStyle(myObj[i], null).fontSize);
254
255
// For debug purpose only. Note: can be very annoying
256
// if (!confirm("TAG ["+myObjName+"] SIZE ["+size+"] STYLESIZE ["+styleSize+"]")) return(0);
257
258
if (isNaN(size) || (size < minSize) || (size > maxSize))
259
size = startSize;
260
261
if (isNaN(styleSize) || (styleSize < minStyleSize) || (styleSize > maxStyleSize))
262
styleSize = startStyleSize;
263
264
if ( ((size > minSize) && (size < maxSize)) ||
265
((size == minSize) && (stepSize > 0)) ||
266
((size == maxSize) && (stepSize < 0)) || useCookie) {
267
myObj[i].setAttribute("size", size+myStepSize);
268
}
269
270
if ( ((styleSize > minStyleSize) && (styleSize < maxStyleSize)) ||
271
((styleSize == minStyleSize) && (stepStyleSize > 0)) ||
272
((styleSize == maxStyleSize) && (stepStyleSize < 0)) || useCookie) {
273
newStyleSize = styleSize+myStepStyleSize;
274
myObj[i].style.fontSize = newStyleSize+measureUnit;
275
}
276
} // End if condition ("only some tags")
277
} // End main for cycle
278
279
// Set the cookies
280
if (!useCookie) {
281
cookieIsSet = setCookie("incrSize", myStepSize+myCookieSize, now);
282
cookieIsSet = setCookie("incrStyleSize", myStepStyleSize+myCookieStyleSize, now);
283
if (alertEnabled && !cookieIsSet) {
284
alert("Per mantenere in memoria la dimensione scelta, abilita i cookie nel browser");
285
}
286
}
287
288
} // End if condition ("document.body exists")
289
} // End function declaration
290
291
function increaseFontSize() {
292
if (document.body) {
293
changeFontSize(stepSize, stepStyleSize, false);
294
calctut.fontPlus();
295
}
296
else {
297
if (alertEnabled) {
298
alert("Spiacente, il tuo browser non supporta questa funzione");
299
}
300
}
301
}
302
303
function decreaseFontSize() {
304
if (document.body) {
305
myStepSize = -stepSize;
306
myStepStyleSize = -stepStyleSize;
307
changeFontSize(myStepSize, myStepStyleSize, false);
308
calctut.fontMinus();
309
}
310
else {
311
if (alertEnabled) {
312
alert("Spiacente, il tuo browser non supporta questa funzione");
313
}
314
}
315
}
316
317
function zoomin() {
318
if (window.parent.document.body.style.zoom < maxZoom) {
319
if (window.parent.document.body.style.zoom > 0) {
320
window.parent.document.body.style.zoom *= zoomFactor;
321
}
322
else {
323
window.parent.document.body.style.zoom = startIncZoom;
324
}
325
}
326
else {
327
if (alertEnabled) {
328
alert("Warning: Max size reached");
329
}
330
}
331
}
332
333
function zoomout() {
334
if ( (window.parent.document.body.style.zoom > minZoom) ||
335
(window.parent.document.body.style.zoom == 0) ) {
336
if (window.parent.document.body.style.zoom > 0) {
337
window.parent.document.body.style.zoom /= zoomFactor;
338
}
339
else {
340
window.parent.document.body.style.zoom = startDecZoom;
341
}
342
}
343
else {
344
if (alertEnabled) {
345
alert("Warning: Min size reached");
346
}
347
}
348
}
349
350
function checkzoom(e) {
351
352
if (document.all) {
353
myEvent = event.keyCode;
354
}
355
else {
356
myEvent = e.which;
357
}
358
359
switch(myEvent) {
360
case keyinIe:
361
case keyinIeCAPS:
362
zoomin();
363
break;
364
365
case keyoutIe:
366
case keyoutIeCAPS:
367
zoomout();
368
break;
369
370
case keyin:
371
case keyinCAPS:
372
increaseFontSize();
373
break;
374
375
case keyout:
376
case keyoutCAPS:
377
decreaseFontSize();
378
break;
379
380
default:
381
break;
382
}
383
}
384
385
if (document.layers) {
386
document.captureEvents(Event.KEYPRESS);
387
}
388
389
document.onkeypress = checkzoom;
390
391