source: other-projects/realistic-books/trunk/src/RealisticBook.js@ 24916

Last change on this file since 24916 was 20889, checked in by anna, 14 years ago

Add new version of Realistic Books that include highlighting and searching

  • Property svn:executable set to *
File size: 10.1 KB
Line 
1/*
2# A component of the Realistic Book software
3# from the New Zealand Digital Library Project at the
4# University of Waikato, New Zealand.
5#
6# Copyright (C) 2007 New Zealand Digital Library Project
7#
8# This program is free software; you can redistribute it and/or modify
9# it under the terms of the GNU General Public License as published by
10# the Free Software Foundation; either version 2 of the License, or
11# (at your option) any later version.
12#
13# This program is distributed in the hope that it will be useful,
14# but WITHOUT ANY WARRANTY; without even the implied warranty of
15# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16# GNU General Public License for more details.
17#
18# You should have received a copy of the GNU General Public License
19# along with this program; if not, write to the Free Software
20# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21*/
22
23/*****************************************************************
24Realistic Book application
25 Detect Flash Player 8.0.24 or more plugin
26 Specify the XHTML input and image cover URL to program
27 Embed the program to the div tag with id = bookdiv
28
29by Veronica Liesaputra --- 1 August 2007
30******************************************************************/
31
32var doc_url;// XHTML url
33var img_cover;// image cover url
34var anno_url;// XHTML annotation url
35
36//check internet browser type
37var isIE = (navigator.appVersion.indexOf("MSIE") != -1) ? true : false;
38var isWin = (navigator.appVersion.toLowerCase().indexOf("win") != -1) ? true : false;
39var isOpera = (navigator.userAgent.indexOf("Opera") != -1) ? true : false;
40var doc_width = -1;
41var doc_height = -1;
42var doc_style = "";
43var init_page = "";
44var http_request = false;
45
46//These functions will be called by the program
47function getDocURL() { return doc_url; }
48function getImgCover() { return img_cover; }
49function getStageW() { return doc_width; }
50function getStageH() { return doc_height; }
51function setDocumentTitle(doc_title) { document.title = doc_title; }
52function getStyleFile() { return doc_style; }
53function getStartOpenPage() { return init_page; }
54function getAnnoURL() { return anno_url; }
55
56//set the page where the book will be open to
57function setOpenPage(num) { init_page = num; }
58//set the style of the book
59function setBookStyle(s) {doc_style = s;}
60//set the size of the book
61function setBookSize(dw,dh) {doc_width = dw; doc_height = dh;}
62
63//overload function
64function embedBookProgram(args)
65{
66 if (init_page.length == 0) {
67 var loc = window.location.href;
68 var idx = loc.lastIndexOf("#");
69 if (idx != -1) {
70 var aftr = loc.substring(idx + 1);
71 if ((aftr.indexOf(".") == -1) && (aftr.indexOf("?") == -1))
72 init_page = aftr;
73 }
74 }
75 if (args.length == 0)
76 alert("Please type the URL for the XHTML input");
77 else
78 {
79 var size = [-1, -1];
80 var file_url = ["","",""];
81 var slen = 0;
82 var surl = 0;
83 for (var i = 0; i < args.length; i++) {
84 if (isNaN(args[i]) == true) {
85 file_url[surl] = args[i];
86 surl++;
87 } else {
88 size[slen] = args[i];
89 slen++;
90 }
91 }
92
93 embedProgram(file_url[0],file_url[1],size[0],size[1],file_url[2]);
94 }
95}
96
97/*
98 * If user has Flash player 8.0.24 or more, Embed the program to your HTML file
99 * inside the div tag with id = bookdiv Set the doc_url, img_cover, anno_url, stage_width
100 * and stage_height value Otherwise, Show user the links to get the flash player
101 */
102function embedProgram(xhtml_url, image_url, stage_height, stage_width,note_url)
103{
104 var flash_plug_html = "";
105 var flash_width = "100%";
106 if (stage_width != -1)
107 flash_width = "" + stage_width;
108 var flash_height = "100%";
109 if (stage_height != -1)
110 flash_height = "" + stage_height;
111
112 if (detectFlashPlayerVersion(8,0,24) == false)
113 {
114 flash_plug_html += 'This content requires the Adobe Flash Player 8.0.24 or greater.';
115 flash_plug_html += '<a href=http://www.macromedia.com/go/getflash/>Get Flash</a>';
116 }
117 else
118 {
119 doc_url = escape(''+xhtml_url);// just in case user forgot the quote
120 img_cover = escape(''+image_url);
121 if (note_url == "") {
122 var ind = xhtml_url.lastIndexOf(".");
123 if (ind != -1) {
124 note_url = xhtml_url.substring(0, ind) + "_annotation" + xhtml_url.substring(ind, xhtml_url.length);
125 }
126 }
127 anno_url = escape(''+note_url);
128
129 // embed inside object tag for IE
130 flash_plug_html += '<OBJECT align="middle" classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" \n';
131 flash_plug_html += ' codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0" \n';
132 flash_plug_html += ' height="' + flash_height + '" id="Book" swLiveConnect="true" align="middle"\n';
133 flash_plug_html += ' width="' + flash_width + '">\n';
134 // embed inside embed tag for netscape, mozilla
135 flash_plug_html += '<PARAM name="allowScriptAccess" value="always" />\n';
136 flash_plug_html += '<PARAM name="movie" value="RealisticBook.swf';
137 flash_plug_html += '?src_image=' + img_cover;
138 flash_plug_html += '&amp;stageW=' + doc_width;
139 flash_plug_html += '&amp;stageH=' + doc_height;
140 flash_plug_html += '&amp;doc_url=' + doc_url;
141 flash_plug_html += '&amp;anno_url=' + anno_url;
142 flash_plug_html += '" />\n';
143 flash_plug_html += '<PARAM name="quality" value="high" />\n';
144 flash_plug_html += '<PARAM name="bgcolor" value="#FFFFFF" />\n';
145 flash_plug_html += '<EMBED align="middle" \n';
146 flash_plug_html += ' allowScriptAccess="always" swLiveConnect="true" \n';
147 flash_plug_html += ' bgcolor="#FFFFFF" height="' + flash_height + '" name="Book" \n';
148 flash_plug_html += ' pluginspage="http://www.macromedia.com/go/getflashplayer" \n';
149 flash_plug_html += ' quality="high" \n';
150 flash_plug_html += ' src=\'RealisticBook.swf';
151 flash_plug_html += '?src_image=' + img_cover;
152 flash_plug_html += '&amp;stageW=' + doc_width;
153 flash_plug_html += '&amp;stageH=' + doc_height;
154 flash_plug_html += '&amp;doc_url=' + doc_url;
155 flash_plug_html += '&amp;anno_url=' + anno_url;
156 flash_plug_html += '\'\n';
157 flash_plug_html += ' type="application/x-shockwave-flash" width="' + flash_width + '" />\n';
158 flash_plug_html += '</OBJECT>\n';
159 }
160
161 var flash_div = document.getElementById('bookdiv');
162 flash_div.innerHTML = flash_plug_html;
163}
164
165function detectFlashPlayerVersion(reqMajor,reqMinor,reqRevision)
166{
167 if (navigator.plugins != null && navigator.plugins.length > 0)
168 {
169 if (navigator.plugins["Shockwave Flash 2.0"] || navigator.plugins["Shockwave Flash"])
170 {
171 var swVer2 = navigator.plugins["Shockwave Flash 2.0"] ? " 2.0" : "";
172 var flashDescription = navigator.plugins["Shockwave Flash" + swVer2].description;
173 var descArray = flashDescription.split(" ");
174 var tempArrayMajor = descArray[2].split(".");
175 var versionMajor = tempArrayMajor[0];
176 var versionMinor = tempArrayMajor[1];
177 var versionRevision = descArray[3];
178 if (versionRevision == "")
179 {
180 versionRevision = descArray[4];
181 }
182 if (versionRevision[0] == "d")
183 {
184 versionRevision = versionRevision.substring(1);
185 }
186 else if (versionRevision[0] == "r")
187 {
188 versionRevision = versionRevision.substring(1);
189 if (versionRevision.indexOf("d") > 0)
190 {
191 versionRevision = versionRevision.substring(0, versionRevision.indexOf("d"));
192 }
193 }
194
195 var isGood = isGoodPlugin(reqMajor,reqMinor,reqRevision,versionMajor,versionMinor,versionRevision);
196 return isGood;
197 }
198 }
199 else if ( isIE && isWin && !isOpera )
200 {
201 var version;
202 var axo;
203 var e;
204
205 try
206 {
207 // version will be set for 7.X or greater players
208 axo = new ActiveXObject("ShockwaveFlash.ShockwaveFlash.7");
209 version = axo.GetVariable("$version");
210
211 // Given "WIN 2,0,0,11"
212 var tempArray = version.split(" "); // ["WIN", "2,0,0,11"]
213 var tempString = tempArray[1]; // "2,0,0,11"
214 var versionArray = tempString.split(","); // ['2', '0', '0', '11']
215
216 var isGood = isGoodPlugin(reqMajor,reqMinor,reqRevision,versionArray[0],versionArray[1],versionArray[2]);
217 return isGood;
218 }
219 catch (e) {}
220 }
221
222 return false;
223}
224
225/*
226 * Check whether the current flash player plugin passed the required version
227 */
228function isGoodPlugin(reqMajor,reqMinor,reqRevision,currMajor,currMinor,currRevision)
229{
230 if (currMajor > parseFloat(reqMajor))
231 {
232 return true;
233 }
234 else if (currMajor == parseFloat(reqMajor))
235 {
236 if (currMinor > parseFloat(reqMinor))
237 {
238 return true;
239 }
240 else if (currMinor == parseFloat(reqMinor))
241 {
242 if (currRevision >= parseFloat(reqRevision))
243 {
244 return true;
245 }
246 }
247 }
248
249 return false;
250}
251
252function makePOSTRequest(url, parameters) {
253 http_request = false;
254 if (window.XMLHttpRequest) { // Mozilla, Safari,...
255 http_request = new XMLHttpRequest();
256 if (http_request.overrideMimeType) {
257 // set type accordingly to anticipated content type
258 // http_request.overrideMimeType('text/xml');
259 http_request.overrideMimeType('text/html');
260 }
261 } else if (window.ActiveXObject) { // IE
262 try {
263 http_request = new ActiveXObject("Msxml2.XMLHTTP");
264 } catch (e) {
265 try {
266 http_request = new ActiveXObject("Microsoft.XMLHTTP");
267 } catch (e) {}
268 }
269 }
270 if (!http_request) {
271 alert('Cannot create XMLHTTP instance');
272 return false;
273 }
274
275 http_request.onreadystatechange = sendStatus;
276 http_request.open('POST', url, true);
277 http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
278 http_request.setRequestHeader("Content-length", parameters.length);
279 http_request.setRequestHeader("Connection", "close");
280 http_request.send(parameters);
281}
282
283function sendStatus() {
284 if (http_request.readyState == 4) {
285 if (http_request.status == 200) {
286 alert(http_request.responseText);
287 }
288 else {
289 alert('There was a problem with the request.');
290 }
291 }
292}
293
294function saveText(msg) {
295 var postStr = 'msgFile=' + escape(encodeURIComponent(doc_url));
296 postStr += '&msgText=' + escape(encodeURIComponent(msg));
297 makePOSTRequest('send.php',postStr);
298}
299
300function saveNote(msg) {
301 var postStr = 'msgFile=' + escape(encodeURIComponent(anno_url));
302 postStr += '&msgText=' + escape(encodeURIComponent(msg));
303 makePOSTRequest('send.php',postStr);
304}
305
306window.onbeforeunload = saveBook;
307function saveBook() {
308 var flashBook = document["Book"];
309 if (navigator.appName.indexOf("Microsoft") != -1) {
310 flashBook = window["Book"];
311 }
312
313 if (flashBook != null) {
314 flashBook.saveBookNotes();
315 //flashBook.saveBookTexts();
316 }
317}
Note: See TracBrowser for help on using the repository browser.