Path: blob/master/calctut/calctut-res/zoom.js
2467 views
// --------------------------------------------------------------------1// Javascript Magnifier v 0.972// Written by Dino Termini - [email protected] - May 9, 20033// This script is freeware (GPL) but if you use it, please let me know!4//5// Portions of code by zoomIN, zoomOUT6// Author: Nguyen Duong Minh (Obie) - [email protected]7// WWW: http://ObieWebsite.SourceForge.net8// License: GNU (GPL)9//10// Portions of code by Webreference Javascript Cookie Functions11// Jupirmedia Corporation12// http://www.internet.com13// --------------------------------------------------------------------14//15// Please refer to DEMO.htm file for details and usage16//17// --------------------------------------------------------------------1819// Configuration parameters20// ------------------------21// Measure unit in pixel (px) or points (pt)22// measureUnit = "pt"23measureUnit = "px"2425// Minimum size allowed for SIZE attribute (like in <FONT SIZE="1"> )26minSize = 1;2728// Minimum size allowed for STYLE attribute (like in <FONT STYLE="font-size: 10px"> )29minStyleSize = 10;3031// Maximum size allowed for SIZE attribute32maxSize = 6;3334// Maximum size allowed for STYLE attribute35maxStyleSize = 30;3637// Start size for tags with no SIZE attribute defined38startSize = 1;3940// Start size for tags with no font-size STYLE or CLASS attribute defined41startStyleSize = 14;4243// Increasing and decreasing step44stepSize = 1;4546// Increasing step for STYLE definition (measure previously declared will be used)47stepStyleSize = 2;4849// To set your own hotkeys, use key generator tool page included50// Keys to zooming in (with and without CAPS lock). Default: "+"51var keyin = 61;52var keyinCAPS = 43;5354// Keys to zooming out (with and without CAPS lock). Default: "-"55var keyout = 45;56var keyoutCAPS = 95;5758// Keys for "hard" zooming in (with and without CAPS lock). Default: ">"59var keyinIe = 46;60var keyinIeCAPS = 62;6162// Keys for "hard" zooming out (with and without CAPS lock). Default: "<"63var keyoutIe = 44;64var keyoutIeCAPS = 60;6566// "Hard" zoom factor67var zoomFactor = 1.1;6869// Max zoom allowed70var maxZoom = 4.096;7172// Min zoom allowed73var minZoom = 0.625;7475// Initial decrease zoom76var startDecZoom = 0.7;7778// Initial increase zoom79var startIncZoom = 1.3;8081// Cookie expiry (default one year, actually 365 days)82// 365 days in a year83// 24 hours in a day84// 60 minutes in an hour85// 60 seconds in a minute86// 1000 milliseconds in a second87userExpiry = 365 * 24 * 60 * 60 * 1000;8889// Enable or disable alert messages90alertEnabled = false;9192// Allow input fields resize (text, buttons, and so on)93allowInputResize = false;9495// End of configuration parameters. Please do not edit below this line96// --------------------------------------------------------------------------------9798// Input values:99// name - name of the cookie100// value - value of the cookie101// [expires] - expiration date of the cookie (defaults to end of current session)102// [path] - path for which the cookie is valid (defaults to path of calling document)103// [domain] - domain for which the cookie is valid (defaults to domain of calling document)104// [secure] - Boolean value indicating if the cookie transmission requires a secure transmission105// * an argument defaults when it is assigned null as a placeholder106// * a null placeholder is not required for trailing omitted arguments107function setCookie(name, value, expires, path, domain, secure) {108return;109// Check whether cookies enabled110document.cookie = "Enabled=true";111var cookieValid = document.cookie;112113// if retrieving the VALUE we just set actually works114// then we know cookies enabled115if (cookieValid.indexOf("Enabled=true") != -1) {116var curCookie = name + "=" + escape(value) +117((expires) ? "; expires=" + expires.toGMTString() : "") +118((path) ? "; path=" + path : "") +119((domain) ? "; domain=" + domain : "") +120((secure) ? "; secure" : "");121122document.cookie = curCookie;123return(true);124}125else {126return(false);127}128}129130// Input value:131// name - name of the desired cookie132// * return string containing value of specified cookie or null if cookie does not exist133function getCookie(name) {134var dc = document.cookie;135var prefix = name + "=";136var begin = dc.indexOf("; " + prefix);137if (begin == -1) {138begin = dc.indexOf(prefix);139if (begin != 0) return null;140} else141begin += 2;142var end = document.cookie.indexOf(";", begin);143if (end == -1)144end = dc.length;145return unescape(dc.substring(begin + prefix.length, end));146}147148// Input values:149// name - name of the cookie150// [path] - path of the cookie (must be same as path used to create cookie)151// [domain] - domain of the cookie (must be same as domain used to create cookie)152// * path and domain default if assigned null or omitted if no explicit argument proceeds153function deleteCookie(name, path, domain) {154if (getCookie(name)) {155document.cookie = name + "=" +156((path) ? "; path=" + path : "") +157((domain) ? "; domain=" + domain : "") +158"; expires=Thu, 01-Jan-70 00:00:01 GMT";159}160}161162// Input value:163// date - any instance of the Date object164// * hand all instances of the Date object to this function for "repairs"165function fixDate(date) {166var base = new Date(0);167var skew = base.getTime();168if (skew > 0)169date.setTime(date.getTime() - skew);170}171172function searchTags(childTree, level) {173var retArray = new Array();174var tmpArray = new Array();175var j = 0;176var childName = "";177for (var i=0; i<childTree.length; i++) {178childName = childTree[i].nodeName;179if (childTree[i].hasChildNodes()) {180if ((childTree[i].childNodes.length == 1) && (childTree[i].childNodes[0].nodeName == "#text"))181retArray[j++] = childTree[i];182else {183tmpArray = searchTags(childTree[i].childNodes, level+1);184for (var k=0;k<tmpArray.length; k++)185retArray[j++] = tmpArray[k];186retArray[j++] = childTree[i];187}188}189else190retArray[j++] = childTree[i];191}192return(retArray);193}194195function changeFontSize(stepSize, stepStyleSize, useCookie) {196if(stepSize == 0)197return;198if (document.body) {199var myObj = searchTags(document.body.childNodes, 0);200var myCookieSize = parseInt(getCookie("incrSize"));201var myCookieStyleSize = parseInt(getCookie("incrStyleSize"));202var myStepSize = stepSize;203var myStepStyleSize = stepStyleSize;204205var now = new Date();206207// Fix the bug in Navigator 2.0, Macintosh208fixDate(now);209210if (isNaN(myCookieSize)) myCookieSize = 0;211if (isNaN(myCookieStyleSize)) myCookieStyleSize = 0;212213// For debug purpose only214// if (!confirm("COOKIE: SIZE ["+myCookieSize+"] STYLESIZE ["+myCookieStyleSize+"]")) return(0);215216// Check valid increment/decrement sizes or useCookie parameter217if (useCookie) {218myStepSize = myCookieSize;219myStepStyleSize = myCookieStyleSize;220}221222now.setTime(now.getTime() + userExpiry);223myObjNumChilds = myObj.length;224var when = false;225for (i=0; i<myObjNumChilds; i++) {226if(!when) {227if(myObj[i].className && myObj[i].className == "narrow txt")228when = true;229else230continue;231}232myObjName = myObj[i].nodeName;233234// Only some tags will be parsed235if (myObjName != "#text" && myObjName != "HTML" &&236myObjName != "HEAD" && myObjName != "TITLE" &&237myObjName != "STYLE" && myObjName != "SCRIPT" &&238myObjName != "BR" && myObjName != "TBODY" &&239myObjName != "#comment" && myObjName != "FORM") {240241// Skip INPUT fields, if required242if (!allowInputResize && myObjName == "INPUT" || myObjName == "SELECT" || myObjName == "OPTION") continue;243244if(myObj[i].className && myObj[i].className.indexOf("noresize") != -1) continue;245246size = parseInt(myObj[i].getAttribute("size"));247248// Internet Explorer uses a different DOM implementation249if (myObj[i].currentStyle)250styleSize = parseInt(myObj[i].currentStyle.fontSize);251else252styleSize = parseInt(window.getComputedStyle(myObj[i], null).fontSize);253254// For debug purpose only. Note: can be very annoying255// if (!confirm("TAG ["+myObjName+"] SIZE ["+size+"] STYLESIZE ["+styleSize+"]")) return(0);256257if (isNaN(size) || (size < minSize) || (size > maxSize))258size = startSize;259260if (isNaN(styleSize) || (styleSize < minStyleSize) || (styleSize > maxStyleSize))261styleSize = startStyleSize;262263if ( ((size > minSize) && (size < maxSize)) ||264((size == minSize) && (stepSize > 0)) ||265((size == maxSize) && (stepSize < 0)) || useCookie) {266myObj[i].setAttribute("size", size+myStepSize);267}268269if ( ((styleSize > minStyleSize) && (styleSize < maxStyleSize)) ||270((styleSize == minStyleSize) && (stepStyleSize > 0)) ||271((styleSize == maxStyleSize) && (stepStyleSize < 0)) || useCookie) {272newStyleSize = styleSize+myStepStyleSize;273myObj[i].style.fontSize = newStyleSize+measureUnit;274}275} // End if condition ("only some tags")276} // End main for cycle277278// Set the cookies279if (!useCookie) {280cookieIsSet = setCookie("incrSize", myStepSize+myCookieSize, now);281cookieIsSet = setCookie("incrStyleSize", myStepStyleSize+myCookieStyleSize, now);282if (alertEnabled && !cookieIsSet) {283alert("Per mantenere in memoria la dimensione scelta, abilita i cookie nel browser");284}285}286287} // End if condition ("document.body exists")288} // End function declaration289290function increaseFontSize() {291if (document.body) {292changeFontSize(stepSize, stepStyleSize, false);293calctut.fontPlus();294}295else {296if (alertEnabled) {297alert("Spiacente, il tuo browser non supporta questa funzione");298}299}300}301302function decreaseFontSize() {303if (document.body) {304myStepSize = -stepSize;305myStepStyleSize = -stepStyleSize;306changeFontSize(myStepSize, myStepStyleSize, false);307calctut.fontMinus();308}309else {310if (alertEnabled) {311alert("Spiacente, il tuo browser non supporta questa funzione");312}313}314}315316function zoomin() {317if (window.parent.document.body.style.zoom < maxZoom) {318if (window.parent.document.body.style.zoom > 0) {319window.parent.document.body.style.zoom *= zoomFactor;320}321else {322window.parent.document.body.style.zoom = startIncZoom;323}324}325else {326if (alertEnabled) {327alert("Warning: Max size reached");328}329}330}331332function zoomout() {333if ( (window.parent.document.body.style.zoom > minZoom) ||334(window.parent.document.body.style.zoom == 0) ) {335if (window.parent.document.body.style.zoom > 0) {336window.parent.document.body.style.zoom /= zoomFactor;337}338else {339window.parent.document.body.style.zoom = startDecZoom;340}341}342else {343if (alertEnabled) {344alert("Warning: Min size reached");345}346}347}348349function checkzoom(e) {350351if (document.all) {352myEvent = event.keyCode;353}354else {355myEvent = e.which;356}357358switch(myEvent) {359case keyinIe:360case keyinIeCAPS:361zoomin();362break;363364case keyoutIe:365case keyoutIeCAPS:366zoomout();367break;368369case keyin:370case keyinCAPS:371increaseFontSize();372break;373374case keyout:375case keyoutCAPS:376decreaseFontSize();377break;378379default:380break;381}382}383384if (document.layers) {385document.captureEvents(Event.KEYPRESS);386}387388document.onkeypress = checkzoom;389390391