source: main/trunk/greenstone3/web/interfaces/default/js/document_scripts.js

Last change on this file was 37480, checked in by davidb, 14 months ago

Changes to get Greenbug back up and running. Theis commit targets two areas: we now need to explicitly specify 'o=xml' in our calls to general action with 'sa' as things like 'XSLTGetTemplateListFromFile'; we now also need to set the AJAX calls explicitly so they return string-type as their response (even if fundamentally the message is XML for example), which is done with dataType: 'text' in the jquery-based AJAX call. We are now using jquery v3.x and it looks like the newer versions of the library auto-sense the return time, where as the older jquery we were using did not to this, hence the reason for the change. In the long run it would be better to keep the returned response in XML, and use browser API to access XML to get to the data we want, however the decision for now was to explicitly set things so it is handled as a string, thereby allowing all our currently written code to operate as it used to.

  • Property svn:executable set to *
File size: 38.7 KB
Line 
1/** Javascript file for viewing documents */
2
3/** NOTE, this file uses inline templates which look for httpPath, assocfilepath, Source, Thumb, Title metadata. These need to be added into any xsl file that uses this javascript (using <gsf:metadata name="xx" hidden="true"/> if they are not already being collected. */
4
5var _imageZoomEnabled = false;
6// Choose which types of filtering you want: sectionnum, or sectiontitle or both
7var _filter_on_types = ["sectiontitle", "sectionnum"];
8// if we have only one type, do we display the option? true/false
9var _display_single_filter_on = true;
10// are titles numeric match?
11var _filter_title_numeric = false;
12
13var _linkCellMap = new Array();
14var _onCells = new Array();
15
16var curHLNum = null;
17
18/*Array to store init states of metadata fields and text*/
19var editableInitStates = new Array();
20/*Array to store last states of metadata fields and text*/
21var editableLastStates = new Array();
22
23// REDEFINING map-scripts::_scrollThroughDefault to be FALSE for document view
24_scrollThroughDefault = false; // TODO: should this even be there in doc view? And if so, what should it scroll through, all markers/shapes in coll or only those in doc?
25
26/********************
27* EXPANSION SCRIPTS *
28********************/
29
30
31function getTextForSection(sectionID, callback)
32{
33 if(!callback)
34 {
35 console.log("Cannot get text as the callback function is not defined");
36 }
37
38 var template = "";
39 template += '<xsl:template match="/">';
40 template += '<text>';
41 template += '<xsl:for-each select="/page/pageResponse/document//documentNode[@nodeID = \'' + sectionID + '\']">';
42 template += '<xsl:call-template name="sectionContent"/>';
43 template += '</xsl:for-each>';
44 template += '</text>';
45 template += '</xsl:template>';
46
47 template = makeURLComponentSafe(template);
48
49 var hlCheckBox = document.getElementById("highlightOption");
50
51 var hl = "";
52 if(hlCheckBox)
53 {
54 if(hlCheckBox.checked)
55 {
56 hl = "on";
57 }
58 else
59 {
60 hl = "off";
61 }
62 }
63
64 var url = gs.xsltParams.library_name + "/collection/" + gs.cgiParams.c + "/document/" + sectionID + "?ed=1&hl=" + hl + "&ilt=" + template;
65 if (gs.cgiParams.p_s && gs.cgiParams.p_s.length > 0) {
66 url += "&p.s=" + gs.cgiParams.p_s;
67 }
68 if (gs.cgiParams.ck && gs.cgiParams.ck.length > 0) {
69 url += "&ck=" + gs.cgiParams.ck;
70 }
71
72 $.ajax(url, { dataType: "text" })
73 .done(function(response)
74 {
75 if(response)
76 {
77 var textStart = response.indexOf(">", response.indexOf(">") + 1) + 1;
78 var textEnd = response.lastIndexOf("<");
79
80 if(textStart == 0 || textEnd == -1 || textEnd <= textStart)
81 {
82 callback("");
83 }
84
85 var text = response.substring(textStart, textEnd);
86 callback(text);
87 }
88 else
89 {
90 callback(null);
91 }
92 })
93 .fail(function()
94 {
95 callback(null);
96 });
97}
98
99function getSubSectionsForSection(sectionID, callback)
100{
101 if(!callback)
102 {
103 console.log("Cannot get sub sections as the callback function is not defined");
104 }
105
106 var template = "";
107 template += '<xsl:template match="/">';
108 template += '<sections>';
109 template += '<xsl:for-each select="/page/pageResponse/document//documentNode[@nodeID = \'' + sectionID + '\']/documentNode">';
110 template += '<xsl:call-template name="wrapDocumentNodes"/>';
111 template += '</xsl:for-each>';
112 template += '</sections>';
113 template += '</xsl:template>';
114
115 template = makeURLComponentSafe(template);
116 var url = gs.xsltParams.library_name + "/collection/" + gs.cgiParams.c + "/document/" + sectionID + "?ilt=" + template;
117
118 if (gs.cgiParams.p_s && gs.cgiParams.p_s.length > 0) {
119 url += "&p.s=" + gs.cgiParams.p_s;
120 }
121
122 if(gs.documentMetadata.docType == "paged")
123 {
124 url += "&dt=hierarchy";
125 }
126
127 $.ajax(url, { dataType: "text" })
128 .done(function(response)
129 {
130 if(response)
131 {
132 var sectionsStart = response.indexOf(">", response.indexOf(">") + 1) + 1;
133 var sectionsEnd = response.lastIndexOf("<");
134
135 if(sectionsStart == 0 || sectionsEnd == -1 || sectionsEnd <= sectionsStart)
136 {
137 callback(" ");
138 return;
139 }
140 else {
141 var sections = response.substring(sectionsStart, sectionsEnd);
142 callback(sections);
143 }
144 }
145 else
146 {
147 callback(null);
148 }
149 })
150 .fail(function()
151 {
152 callback(null);
153 });
154}
155
156
157function toggleSection(sectionID, callback, tocDisabled)
158{
159 var docElem = gs.jqGet("doc" + sectionID);
160 var tocElem = gs.jqGet("toc" + sectionID);
161
162 var tocToggleElem = gs.jqGet("ttoggle" + sectionID);
163 var docToggleElem = gs.jqGet("dtoggle" + sectionID);
164
165 if(docElem.css("display") == "none")
166 {
167 if(tocToggleElem.length && !tocDisabled)
168 {
169 tocToggleElem.attr("src", gs.imageURLs.collapse);
170 }
171
172 if(tocElem.length && !tocDisabled)
173 {
174 tocElem.css("display", "block");
175 }
176
177 if(docElem.hasClass("noText"))
178 {
179 getTextForSection(sectionID, function(text)
180 {
181 if(text)
182 {
183 var nodeID = sectionID.replace(/\./g, "_");
184 if(text.search("wrap" + nodeID) != -1)
185 {
186 $("#zoomOptions").css("display", "");
187 $("#pagedImageOptions").css("display", "");
188 }
189 getSubSectionsForSection(sectionID, function(sections)
190 {
191 if(sections)
192 {
193 var textElem = gs.jqGet("doc" + sectionID);
194 textElem.html(text + sections);
195
196 docElem.removeClass("noText");
197 docElem.css("display", "block");
198 docToggleElem.attr("src", gs.imageURLs.collapse);
199
200 if(callback)
201 {
202 callback(true);
203 }
204
205 if(gs.jqGet("viewSelection").length)
206 {
207 changeView();
208 }
209 }
210 else
211 {
212 docToggleElem.attr("src", gs.imageURLs.expand);
213 if(callback)
214 {
215 callback(false);
216 }
217 }
218 });
219 }
220 else
221 {
222 docToggleElem.attr("src", gs.imageURLs.expand);
223 if(callback)
224 {
225 callback(false);
226 }
227 }
228 });
229
230 docToggleElem.attr("src", gs.imageURLs.loading);
231 }
232 else
233 {
234 docToggleElem.attr("src", gs.imageURLs.collapse);
235 docElem.css("display", "block");
236
237 if(callback)
238 {
239 callback(true);
240 }
241 }
242 }
243 else
244 {
245 docElem.css("display", "none");
246
247 //Use the page image if this is a leaf node and the chapter image if it not
248 docToggleElem.attr("src", gs.imageURLs.expand);
249
250 if(tocToggleElem.length)
251 {
252 tocToggleElem.attr("src", gs.imageURLs.expand);
253 }
254
255 if(tocElem.length)
256 {
257 tocElem.css("display", "none");
258 }
259
260 if(callback)
261 {
262 callback(true);
263 }
264 }
265}
266
267function scrollToTop()
268{
269 $('html, body').stop().animate({scrollTop: 0}, 1000);
270}
271
272function scrollNextHightlight()
273{
274 if (curHLNum == null)
275 {
276 curHLNum = 0;
277 } else
278 {
279 curHLNum++;
280 }
281 var hlNode = $("span[class$='ermHighlight']:eq(" + curHLNum + ")");
282 if (hlNode.length == 0)
283 {
284 curHLNum = 0;
285 hlNode = $("span[class$='ermHighlight']:eq(" + curHLNum + ")");
286 }
287 if (hlNode.length != 0)
288 {
289 var topVal = $(hlNode).offset().top - 50;
290 $('html, body').stop().animate({scrollTop: topVal}, 1000);
291 }
292}
293
294function scrollPrevHightlight()
295{
296 if (curHLNum == null)
297 {
298 curHLNum = $("span[class$='ermHighlight']").length - 1;
299 } else
300 {
301 curHLNum--;
302 }
303 var hlNode = $("span[class$='ermHighlight']:eq(" + curHLNum + ")");
304 if (hlNode.length == 0)
305 {
306 curHLNum = $("span[class$='ermHighlight']").length - 1;
307 hlNode = $("span[class$='ermHighlight']:eq(" + curHLNum + ")");
308 }
309 if (hlNode.length != 0)
310 {
311 var topVal = $(hlNode).offset().top - 50;
312 $('html, body').stop().animate({scrollTop: topVal}, 1000);
313 }
314}
315
316function focusSection(sectionID, level, tocDisabled)
317{
318 expandAndExecute(sectionID, level, tocDisabled, function()
319 {
320 var topVal = $(document.getElementById("doc" + sectionID)).offset().top - 50;
321 $('html, body').stop().animate({scrollTop: topVal}, 1000);
322 });
323}
324function focusAnchor(sectionID, level, tocDisabled, anchor)
325{
326 expandAndExecute(sectionID, level, tocDisabled, function()
327 {
328 var target = document.getElementById(anchor);
329 if (!target){
330 target = document.getElementsByName(anchor)[0];
331 }
332 var topVal = $(target).offset().top - 50;
333 $('html, body').stop().animate({scrollTop: topVal}, 1000);
334 window.location.hash = anchor;
335 });
336}
337function expandAndExecute(sectionID, level, tocDisabled, executeAfter)
338{
339 if(!level)
340 {
341 level = 0;
342 }
343 var parts = sectionID.split(".");
344 if(level >= parts.length)
345 {
346 if (executeAfter) {
347 executeAfter();
348 }
349 document.getElementById("gs_content").style.cursor = "default";
350 return;
351 }
352
353 var idToExpand = "";
354 for(var i = 0; i < level + 1; i++)
355 {
356 if(i > 0)
357 {
358 idToExpand += ".";
359 }
360
361 idToExpand += parts[i];
362 }
363
364 if(!isSectionExpanded(idToExpand))
365 {
366 document.getElementById("gs_content").style.cursor = "progress";
367 toggleSection(idToExpand, function(success)
368 {
369 if(success)
370 {
371 expandAndExecute(sectionID, level + 1, tocDisabled, executeAfter);
372 }
373 }, tocDisabled);
374 }
375 else
376 {
377 expandAndExecute(sectionID, level + 1, tocDisabled, executeAfter);
378 }
379}
380function expandOrCollapseAll(expand)
381{
382 var divs = $("div");
383 var startCounter = 0;
384 var endCounter = 0;
385
386 for(var i = 0; i < divs.length; i++)
387 {
388 if($(divs[i]).attr("id") && $(divs[i]).attr("id").search(/^doc/) != -1)
389 {
390 var id = $(divs[i]).attr("id").replace(/^doc(.*)/, "$1");
391 if(isSectionExpanded(id) != expand)
392 {
393 //Don't collapse the top level
394 if(!expand && id.indexOf(".") == -1)
395 {
396 continue;
397 }
398 startCounter++;
399
400 var toggleFunction = function(tid)
401 {
402 toggleSection(tid, function(success)
403 {
404 if(success)
405 {
406 endCounter++;
407 }
408 else
409 {
410 setTimeout(function(){toggleFunction(tid)}, 500);
411 }
412 });
413 }
414 toggleFunction(id);
415 }
416 }
417 }
418
419 if(startCounter != 0)
420 {
421 var checkFunction = function()
422 {
423 if(startCounter == endCounter)
424 {
425 expandOrCollapseAll(expand);
426 }
427 else
428 {
429 setTimeout(checkFunction, 500);
430 }
431 }
432 checkFunction();
433 }
434}
435
436function loadTopLevelPage(callbackFunction, customURL)
437{
438 var url;
439 if(customURL)
440 {
441 url = customURL;
442 }
443 else
444 {
445 url = gs.xsltParams.library_name + "?a=d&c=" + gs.cgiParams.c + "&excerptid=gs-document";
446 if(gs.cgiParams.d && gs.cgiParams.d.length > 0)
447 {
448 url += "&d=" + gs.cgiParams.d.replace(/([^.]*)\..*/, "$1");
449 // in case we are doing search term highlighting, want p.s here too (e/g/ p.s=TextQuery)
450 if (gs.cgiParams.p_s && gs.cgiParams.p_s.length > 0) {
451 url += "&p.s=" + gs.cgiParams.p_s;
452 }
453
454 }
455 else if(gs.cgiParams.href && gs.cgiParams.href.length > 0)
456 {
457 url += "&d=&alb=1&rl=1&href=" + gs.cgiParams.href;
458 }
459 }
460
461 $.ajax(url, { dataType: "text" })
462 .done(function(response)
463 {
464 if(response)
465 {
466 var targetElem = $("#gs-document");
467 var docStart = response.indexOf(">") + 1;
468 var docEnd = response.lastIndexOf("<");
469 var doc = response.substring(docStart, docEnd);
470
471 targetElem.html(doc);
472
473 if(callbackFunction)
474 {
475 callbackFunction();
476 }
477 }
478 })
479 .fail(function()
480 {
481 setTimeout(function(){loadTopLevelPage(callbackFunction, customURL);}, 1000);
482 });
483}
484
485function retrieveFullTableOfContentsSuccess(newTOCElem)
486{
487 var tocStart = newTOCElem.indexOf(">") + 1;
488 var tocEnd = newTOCElem.lastIndexOf("<");
489
490 var newTOC = newTOCElem.substring(tocStart, tocEnd);
491
492 //Add the "Expand document"/"Collapse document" links
493 //newTOC = "<table style=\"width:100%; text-align:center;\"><tr><td><a href=\"javascript:expandOrCollapseAll(true);\">"+gs.text.doc.expand_doc+"</a></td><td><a href=\"javascript:expandOrCollapseAll(false);\">"+gs.text.doc.collapse_doc+"</a></td></tr></table>" + newTOC;
494 //newTOC = "<table style=\"width:100%; text-align:center;\"><tr><td><a href=\""+window.location.href+"?ed=1\">"+gs.text.doc.expand_doc+"</a></td><td><a href=\""+window.location.href+"?ed=0\">"+gs.text.doc.collapse_doc+"</a></td></tr></table>" + newTOC;
495
496 //Collapse the TOC
497 newTOC = newTOC.replace(/display:block/g, "display:none");
498 newTOC = newTOC.replace(/display:none/, "display:block");
499 newTOC = newTOC.replace(/images\/collapse/g, "images/expand");
500
501 var tocElem = $("#tableOfContents");
502 tocElem.html(newTOC);
503
504 gs.variables.tocLoaded = true;
505}
506
507function retrieveFullTableOfContentsSuccessClientSideXSLT(newTOCElem)
508{
509 $('#client-side-xslt-ajax').remove();
510 retrieveFullTableOfContentsSuccess(newTOCElem)
511}
512
513function retrieveFullTableOfContents()
514{
515 var url = gs.xsltParams.library_name + "/collection/" + gs.cgiParams.c + "?excerptid=tableOfContents&ec=1";
516 if(gs.cgiParams.d && gs.cgiParams.d.length > 0)
517 {
518 url += "&a=d&d=" + gs.cgiParams.d;
519 }
520 else if(gs.cgiParams.href && gs.cgiParams.href.length > 0)
521 {
522 url += "&a=d&d=&alb=1&rl=1&href=" + gs.cgiParams.href;
523 }
524 // later on we want this arg p.s so we can keep search term highlighting for expand document link
525 if (gs.cgiParams.p_s && gs.cgiParams.p_s.length > 0) {
526 url += "&p.s=" + gs.cgiParams.p_s;
527 }
528 if (gs.cgiParams.ck && gs.cgiParams.ck.length > 0) {
529 url += "&ck=" + gs.cgiParams.ck;
530 }
531
532 if (gs.xsltParams.use_client_side_xslt == "true") { // note xsltParams are of type string, so test needs to be in quotes
533 url += "&callback=retrieveFullTableOfContentsSuccessClientSideXSLT"; // used in client-side-xslt.js, in combination with 'excerptid'
534 $('<iframe src="'+url+'" id="client-side-xslt-ajax" tabindex="-1" style="position: absolute; width: 0px; height: 0px; border: none;"></iframe>').appendTo('body');
535 }
536 else {
537 $.ajax(url, { dataType: "text" })
538 .done(retrieveFullTableOfContentsSuccess)
539 .fail(function() {
540 setTimeout(retrieveFullTableOfContents, 1000);
541 });
542 }
543}
544
545function isSectionExpanded(sectionID)
546{
547 var docElem = gs.jqGet("doc" + sectionID);
548
549 if(docElem.css("display") === "block")
550 {
551 return true;
552 }
553 return false;
554}
555
556function minimizeSidebar()
557{
558 var toc = $("#contentsArea");
559 var maxLink = $("#sidebarMaximizeButton");
560 var minLink = $("#sidebarMinimizeButton");
561
562 if(toc.length)
563 {
564 toc.css("display", "none");
565 }
566
567 maxLink.css("display", "block");
568 minLink.css("display", "none");
569}
570
571function maximizeSidebar()
572{
573 var coverImage = $("#coverImage");
574 var toc = $("#contentsArea");
575 var maxLink = $("#sidebarMaximizeButton");
576 var minLink = $("#sidebarMinimizeButton");
577
578 if(coverImage.length)
579 {
580 coverImage.css("display", "block");
581 }
582
583 if(toc.length)
584 {
585 toc.css("display", "block");
586 }
587
588 maxLink.css("display", "none");
589 minLink.css("display", "block");
590}
591
592function extractFilteredPagesToOwnDocument()
593{
594 var oids = new Array();
595 var filtered = $(".pageSliderCol:visible a").each(function()
596 {
597 var hrefString = $(this).attr("href");
598 var oidStart = hrefString.indexOf(".") + 1;
599 var oidFinish = hrefString.indexOf("'", oidStart + 1);
600
601 oids.push(hrefString.substring(oidStart, oidFinish));
602 });
603
604 var sectionString = "[";
605 for(var i = 0; i < oids.length; i++)
606 {
607 sectionString += "\"" + oids[i] + "\"";
608 if(i < oids.length - 1)
609 {
610 sectionString += ",";
611 }
612 }
613 sectionString += "]";
614
615 var url = "cgi-bin/document-extract.pl?a=extract-archives-doc&c=" + gs.cgiParams.c + "&d=" + gs.cgiParams.d + "&json-sections=" + sectionString + "&site=" + gs.xsltParams.site_name;// + "&json-metadata=[{"metaname":"dc.Title","metavalue":"All Black Rugy Success","metamode":"accumulate"]"
616 $("#extractDocButton").attr("disabled", "disabled").html("Extracting document...");
617 $.ajax(url)
618 .done(function(response)
619 {
620 $("#extractDocButton").html("Building collection...");
621 gs.functions.buildCollections([gs.cgiParams.c], function()
622 {
623 $("#extractDocButton").prop("disabled", false).html("Extract these pages to document");
624 });
625 })
626 .fail(function()
627 {
628 $("#extractDocButton").prop("disabled", false).html("Extract these pages to document");
629 });
630}
631
632/**********************
633* PAGED-IMAGE SCRIPTS *
634**********************/
635
636function changeView()
637{
638 var viewList = $("#viewSelection");
639 var currentVal = viewList.val();
640
641 var view;
642 if(currentVal == "Image view")
643 {
644 setImageVisible(true);
645 setTextVisible(false);
646 view = "image";
647 }
648 else if(currentVal == "Text view")
649 {
650 setImageVisible(false);
651 setTextVisible(true);
652 view = "text";
653 }
654 else
655 {
656 setImageVisible(true);
657 setTextVisible(true);
658 view = "";
659 }
660
661 var url = gs.xsltParams.library_name + "?a=d&view=" + view + "&c=" + gs.cgiParams.c;
662 $.ajax(url);
663}
664
665function setImageVisible(visible)
666{
667 $("div").each(function()
668 {
669 if($(this).attr("id") && $(this).attr("id").search(/^image/) != -1)
670 {
671 $(this).css("display", (visible ? "block" : "none"));
672 }
673 });
674}
675
676function setTextVisible(visible)
677{
678 $("div").each(function()
679 {
680 if($(this).attr("id") && $(this).attr("id").search(/^text/) != -1)
681 {
682 $(this).css("display", (visible ? "block" : "none"));
683 }
684 });
685}
686
687function retrieveTableOfContentsAndTitles()
688{
689 var ilt = "";
690 ilt += '<xsl:template match="/">';
691 ilt += '<xsl:for-each select="/page/pageResponse/document/documentNode">';
692 ilt += '<xsl:call-template name="documentNodeTOC"/>';
693 ilt += '</xsl:for-each>';
694 ilt += '</xsl:template>';
695
696 ilt = makeURLComponentSafe(ilt);
697
698 var url = gs.xsltParams.library_name + "?a=d&ec=1&c=" + gs.cgiParams.c + "&d=" + gs.cgiParams.d + "&ilt=" + ilt;
699
700 if (gs.cgiParams.p_s && gs.cgiParams.p_s.length > 0) {
701 url += "&p.s=" + gs.cgiParams.p_s;
702 }
703
704 $.ajax(url, { dataType: "text" })
705 .done(function(response)
706 {
707 var tableOfContents = $("#tableOfContents");
708 tableOfContents.append(response);
709 replaceLinksWithSlider();
710
711 var loading = $("#tocLoadingImage");
712 loading.remove();
713 })
714 .fail(function()
715 {
716 setTimeout(function(){retrieveTableOfContentsAndTitles();}, 1000);
717 });
718}
719
720
721function replaceLinksWithSlider()
722{
723 var tableOfContents = $("#tableOfContents");
724 var leafSections = new Array();
725 var liElems = tableOfContents.find("li").each(function()
726 {
727 var section = $(this);
728 var add = true;
729 for(var j = 0; j < leafSections.length; j++)
730 {
731 if(leafSections[j] == undefined){continue;}
732
733 var leaf = $(leafSections[j]);
734 if(leaf.attr("id").search(section.attr("id")) != -1)
735 {
736 add = false;
737 }
738
739 if(section.attr("id").search(leaf.attr("id")) != -1)
740 {
741 delete leafSections[j];
742 }
743 }
744
745 if(add)
746 {
747 leafSections.push(section);
748 }
749 });
750
751 for(var i = 0 ; i < leafSections.length; i++)
752 {
753 if(leafSections[i] == undefined){continue;}
754
755 leafSections[i].css("display", "none");
756 var links = leafSections[i].find("a");
757
758 var widget = new SliderWidget(links);
759 leafSections[i].before(widget.getElem());
760 }
761
762 //Disable all TOC toggles
763 var imgs = $("img").each(function()
764 {
765 var currentImage = $(this);
766 if(currentImage.attr("id") && currentImage.attr("id").search(/^ttoggle/) != -1)
767 {
768 currentImage.attr("onclick", "");
769 currentImage.on("click", function()
770 {
771 var sliderDiv = currentImage.parents("table").first().next();
772 if(sliderDiv.is(":visible"))
773 {
774 sliderDiv.hide();
775 }
776 else
777 {
778 sliderDiv.show();
779 }
780 });
781 }
782 else if(currentImage.attr("id") && currentImage.attr("id").search(/^dtoggle/) != -1)
783 {
784 currentImage.attr("onclick", currentImage.attr("onclick").replace(/\)/, ", null, true)"));
785 }
786 });
787}
788
789
790function SliderWidget(_links)
791{
792 //****************
793 //MEMBER VARIABLES
794 //****************
795
796 //The container for the widget
797 var _mainDiv = $("<div>");
798 _mainDiv.attr("class", "ui-widget-content pageSlider");
799
800 //The table of images
801 var _linkTable = $("<table>");
802 _mainDiv.append(_linkTable);
803
804 //The image row of the table
805 var _linkRow = $("<tr>");
806 _linkTable.append(_linkRow);
807
808 //The list of titles we can search through
809 var _titles = new Array();
810
811 //Keep track of the slider position
812 var _prevScroll = 0;
813
814 //****************
815 //PUBLIC FUNCTIONS
816 //****************
817
818 //Function that returns the widget element
819 this.getElem = function()
820 {
821 return _mainDiv;
822 }
823
824 //*****************
825 //PRIVATE FUNCTIONS
826 //*****************
827
828 // _filter_on_types can be "sectionnum", "sectiontitle"
829 var setUpFilterButtons = function() {
830
831 var button_div = $("#filterOnButtons");
832 button_div.onclick = doFiltering;
833 button_div.html("radio");
834 if (_filter_on_types.length == 0) {
835 _filter_on_types = ["sectionnum", "sectiontitle"];
836 }
837 else if (_filter_on_types.length == 1) {
838 if (_filter_on_types[0] == "sectionnum") {
839 button_div.html("(<input type='radio' name='filterOn' value='num' checked>"+gs.text.doc.filter.pagenum+"</input>)");
840 } else {
841 button_div.html("(<input type='radio' name='filterOn' value='title' checked>"+gs.text.doc.filter.title+"</input>)");
842 }
843 if (_display_single_filter_on == false) {
844 button_div.css("display","none");
845 }
846
847 } else {
848 // should be both options
849 button_div.html("<input type='radio' name='filterOn' value='num' checked>"+gs.text.doc.filter.pagenum+"</input><input type='radio' name='filterOn' value='title'>"+gs.text.doc.filter.title+"</input>");
850 }
851}
852
853 var doFiltering = function () {
854 if (typeof _titles == "undefined") {
855 return;
856 }
857
858 var filter_string = $("#filterText").val();
859 var filter_type = $('input[name="filterOn"]:checked').val();
860
861 var index = 2; // section num
862 var numeric_match = true;
863 if (filter_type == "title") {
864 index = 3;
865 if (_filter_title_numeric != true) {
866 numeric_match = false;
867 }
868
869 }
870 var values = filter_string.split(",");
871
872 var matchingTitles = new Array();
873
874 for (var l = 0; l < values.length; l++)
875 {
876 var currentValue = values[l].replace(/^ +/g, "").replace(/ +$/g, "");
877 if (numeric_match) {
878 var isRange = (currentValue.search(/^\d+-\d+$/) != -1);
879 if (isRange) {
880 var firstNumber = Number(currentValue.replace(/(\d+)-\d+/, "$1"));
881 var secondNumber = Number(currentValue.replace(/\d+-(\d+)/, "$1"));
882 if(firstNumber <= secondNumber)
883 {
884 for(var i = firstNumber; i <= secondNumber; i++)
885 {
886 var numString = i + "";
887 for(var j = 0; j < _titles.length; j++) {
888 var currentTitle = _titles[j];
889 if(currentTitle[index] == numString) {
890 matchingTitles.push(currentTitle);
891 break; // assume no titles are the same
892 }
893 }
894 }
895 }
896 } // if isRange
897 else {
898 for(var j = 0; j < _titles.length; j++) {
899 if (_titles[j][index]==currentValue) {
900 matchingTitles.push(_titles[j]);
901 break; // assume no titles are the same
902 }
903 }
904
905 }
906
907 } else { // not numeric match.
908 // need to do a search
909 for(var i = 0; i < _titles.length; i++)
910 {
911 var currentTitle = _titles[i];
912 if(currentTitle[index].toLowerCase().search(currentValue.toLowerCase().replace(/\./g, "\\.")) != -1)
913 {
914 matchingTitles.push(currentTitle);
915 }
916 }
917 }
918 } // for each value from filter string
919
920 // set all to hide...
921 for(var i = 0; i < _titles.length; i++)
922 {
923 $(_titles[i][1].cell).css("display", "none");
924 }
925
926 // .. then display the matching ones
927 for(var i = 0; i < matchingTitles.length; i++)
928 {
929 $(matchingTitles[i][1].cell).css("display", "table-cell");
930 }
931 } // end doFiltering() function
932
933 var setUpFilterBox = function()
934 {
935 var filter = $("#filterText");
936
937 filter.keyup(function()
938 {
939 doFiltering();
940 });
941 }
942
943 var getImage = function(page, attemptNumber)
944 {
945 var href = page.getAttribute("href");
946 var startHREF = href.indexOf("'") + 1;
947 var endHREF = href.indexOf("'", startHREF);
948 var nodeID = href.substring(startHREF, endHREF);
949 href = gs.xsltParams.library_name + "/collection/" + gs.cgiParams.c + "/document/" + nodeID;
950
951 var template = '';
952 template += '<xsl:template match="/">';
953 template += '<html>';
954 template += '<img>';
955 template += '<xsl:attribute name="src">';
956 template += "<xsl:value-of disable-output-escaping=\"yes\" select=\"/page/pageResponse/collection/metadataList/metadata[@name = 'httpPath']\"/>";
957 template += '<xsl:text>/index/assoc/</xsl:text>';
958 template += "<xsl:value-of disable-output-escaping=\"yes\" select=\"/page/pageResponse/document/metadataList/metadata[@name = 'assocfilepath']\"/>";
959 template += '<xsl:text>/</xsl:text>';
960 template += "<xsl:value-of disable-output-escaping=\"yes\" select=\"/page/pageResponse/document//documentNode[@nodeID = '" + nodeID + "']/metadataList/metadata[@name = 'Thumb']\"/>";
961 template += '</xsl:attribute>';
962 template += '</img>';
963 template += '<p>';
964 template += "<xsl:value-of disable-output-escaping=\"yes\" select=\"/page/pageResponse/document/documentNode/metadataList/metadata[@name = 'Title']\"/>";
965 template += '</p>';
966 template += '</html>';
967 template += '</xsl:template>';
968 template = makeURLComponentSafe(template);
969 var url = href + "?noText=1&ilt=" + template;
970
971 $.ajax(url, { dataType: "text" })
972 .done(function(text)
973 {
974 var hrefStart = text.indexOf("src=\"") + 5;
975 if(hrefStart == -1)
976 {
977 page.isLoading = false;
978 page.noImage = true;
979 $(page.image).attr("src", gs.imageURLs.blank);
980 return;
981 }
982 var hrefEnd = text.indexOf("\"", hrefStart);
983 var href = text.substring(hrefStart, hrefEnd);
984
985 var image = $("<img>");
986 image.on("load", function()
987 {
988 $(page.link).html("");
989 $(page.link).append(image);
990 page.isLoading = false;
991 page.imageLoaded = true;
992 });
993 image.on("error", function()
994 {
995 if(!attemptNumber || attemptNumber < 3)
996 {
997 setTimeout(function(){getImage(page, ((!attemptNumber) ? 1 : attemptNumber + 1));}, 500);
998 }
999 else
1000 {
1001 page.isLoading = false;
1002 page.noImage = true;
1003 image.attr("src", gs.imageURLs.blank);
1004 }
1005 });
1006 image.attr("src", href);
1007
1008 var titleStart = text.indexOf("<p>") + 3;
1009 var titleEnd = text.indexOf("</p>");
1010 var title = text.substring(titleStart, titleEnd);
1011 })
1012 .fail(function()
1013 {
1014 page.failed = true;
1015 if(!attemptNumber || attemptNumber < 3)
1016 {
1017 setTimeout(function(){getImage(page, ((!attemptNumber) ? 1 : attemptNumber + 1));}, 500);
1018 }
1019 else
1020 {
1021 var image = $("<img>", {"src": gs.imageURLs.blank});
1022 $(page.link).html("");
1023 $(page.link).append(image);
1024 page.isLoading = false;
1025 page.noImage = true;
1026 }
1027 });
1028 }
1029
1030 var startCheckFunction = function()
1031 {
1032 var checkFunction = function(forced)
1033 {
1034 //Don't bother checking if we haven't scrolled very far
1035 if(Math.abs(_mainDiv.scrollLeft() - _prevScroll) > 100 || forced)
1036 {
1037 _prevScroll = _mainDiv.scrollLeft();
1038 _checking = true;
1039 var widgetLeft = _mainDiv.offset().left;
1040 var widgetRight = widgetLeft + _mainDiv.width();
1041
1042 var visiblePages = new Array();
1043 for(var i = 0; i < _links.length; i++)
1044 {
1045 var current = _links[i].cell;
1046 var currentLeft = current.offset().left;
1047 var currentRight = currentLeft + current.width();
1048
1049 if(currentRight > widgetLeft && currentLeft < widgetRight)
1050 {
1051 visiblePages.push(_links[i]);
1052 }
1053 }
1054
1055 for(var i = 0; i < visiblePages.length; i++)
1056 {
1057 var page = visiblePages[i];
1058 if(!page || page.imageLoaded || page.noImage || page.isLoading)
1059 {
1060 continue;
1061 }
1062
1063 page.isLoading = true;
1064 getImage(page);
1065 }
1066 _checking = false;
1067 }
1068 }
1069
1070 setTimeout(checkFunction, 250);
1071 setInterval(function(){checkFunction(true)}, 2000);
1072 _mainDiv.scroll(checkFunction);
1073 }
1074
1075 //***********
1076 //CONSTRUCTOR
1077 //***********
1078
1079 for(var i = 0; i < _links.length; i++)
1080 {
1081 var col = $("<td>");
1082 _linkRow.append(col);
1083 col.addClass("pageSliderCol");
1084 _links[i].cell = col;
1085
1086 var link = $("<a>");
1087 col.append(link);
1088 _links[i].link = link;
1089 var href = $(_links[i]).attr("href");
1090 link.attr("href", href.replace(/\)/, ", 0, true)"));
1091
1092 if(!_linkCellMap[href])
1093 {
1094 _linkCellMap[href] = new Array();
1095 }
1096 _linkCellMap[href].push(_links[i]);
1097
1098 var loadingText = $("<p>Loading image</p>");
1099 link.append(loadingText);
1100
1101 var image = $("<img>");
1102 link.append(image);
1103 image.attr("src", gs.imageURLs.loading);
1104 _links[i].image = image;
1105
1106 var title = $(_links[i]).html();
1107 var t_section = "";
1108 var t_title = "";
1109 if (title.search(/tocSectionNumber/) != -1)
1110 {
1111 var matching_regex = /<span class=\"tocSectionNumber\">([0-9]+)<\/span>[\s\S]*<span class=\"tocSectionTitle\">(.+)<\/span>$/mg;
1112 var matches_array = matching_regex.exec(title);
1113 if (matches_array != null && matches_array.length == 3) {
1114 t_section = matches_array[1];
1115 t_title = matches_array[2];
1116 }
1117 }
1118
1119 _titles.push([title, _links[i], t_section, t_title]);
1120
1121 col.append($("<br>"));
1122 col.append(title);
1123 }
1124
1125 setUpFilterBox();
1126 setUpFilterButtons();
1127 startCheckFunction();
1128 }
1129
1130/***********************
1131* HIGHLIGHTING SCRIPTS *
1132***********************/
1133function swapHighlight(imageClicked)
1134{
1135 var hlCheckbox = $("#highlightOption");
1136 if(imageClicked)
1137 {
1138 // toggle the state of the checkbox
1139 $(hlCheckbox).prop("checked", !$(hlCheckbox).prop("checked"));
1140 }
1141 var from;
1142 var to;
1143 if(hlCheckbox.prop("checked"))
1144 {
1145 from = "noTermHighlight";
1146 to = "termHighlight";
1147 }
1148 else
1149 {
1150 from = "termHighlight";
1151 to = "noTermHighlight";
1152 }
1153
1154 var spans = $("span").each(function()
1155 {
1156 if($(this).hasClass(from))
1157 {
1158 $(this).removeClass(from);
1159 $(this).addClass(to);
1160 }
1161 });
1162}
1163
1164/**************************
1165* REALISTIC BOOKS SCRIPTS *
1166**************************/
1167
1168function bookInit()
1169{
1170 loadBook();
1171 hideText();
1172 showBook();
1173 swapLinkJavascript(false);
1174}
1175
1176function hideText()
1177{
1178 $("#gs-document-text").css("visibility", "hidden");
1179}
1180
1181function showText()
1182{
1183 $("#gs-document-text").css("visibility", "visible");
1184}
1185
1186function hideBook()
1187{
1188 $("#bookDiv, #bookObject, #bookEmbed").css({"visibility": "hidden", "height": "0px"});
1189}
1190
1191function showBook()
1192{
1193 $("#bookDiv, #bookObject, #bookEmbed").css({"visibility": "visible", "height": "600px"});
1194}
1195
1196function swapLinkJavascript(rbOn)
1197{
1198 var option = $("#rbOption");
1199 var optionImage = $("#rbOptionImage");
1200
1201 if(rbOn)
1202 {
1203 option.attr("onclick", "hideText(); showBook(); swapLinkJavascript(false);");
1204 optionImage.attr("onclick", "hideText(); showBook(); swapLinkJavascript(false);");
1205 $(option).prop("checked", false);
1206 }
1207 else
1208 {
1209 option.attr("onclick", "hideBook(); showText(); swapLinkJavascript(true);");
1210 optionImage.attr("onclick", "hideBook(); showText(); swapLinkJavascript(true);");
1211 $(option).prop("checked", true);
1212 }
1213}
1214
1215function loadBook()
1216{
1217 var doc_url = document.URL;
1218 doc_url = doc_url.replace(/(&|\?)book=[a-z]+/gi,'');
1219 doc_url += '&book=flashxml';
1220
1221 var img_cover = gs.collectionMetadata.httpPath + '/index/assoc/' + gs.documentMetadata.assocfilepath + '/cover.jpg';
1222
1223 var flash_plug_html = ""
1224 flash_plug_html += '<OBJECT align="middle" classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" \n';
1225 flash_plug_html += ' height="600px" id="bookObject" swLiveConnect="true" \n';
1226 flash_plug_html += ' codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0" \n';
1227 flash_plug_html += ' width="70%">\n';
1228 flash_plug_html += ' <PARAM name="allowScriptAccess" value="always" />\n';
1229 flash_plug_html += ' <PARAM name="movie" value="Book.swf';
1230 flash_plug_html += '?src_image=' + escape(img_cover);
1231 flash_plug_html += '&doc_url=' + escape(doc_url);
1232 flash_plug_html += '" />\n';
1233 flash_plug_html += ' <PARAM name="quality" value="high" />\n';
1234 flash_plug_html += ' <PARAM name="bgcolor" value="#FFFFFF" />\n';
1235 flash_plug_html += ' <EMBED align="middle" \n';
1236 flash_plug_html += ' allowScriptAccess="always" swLiveConnect="true" \n';
1237 flash_plug_html += ' bgcolor="#FFFFFF" height="600px" name="Book" \n';
1238 flash_plug_html += ' pluginspage="http://www.macromedia.com/go/getflashplayer" \n';
1239 flash_plug_html += ' quality="high" id="bookEmbed"\n';
1240 flash_plug_html += ' src="Book.swf';
1241 flash_plug_html += '?src_image=' + escape(img_cover);
1242 flash_plug_html += '&doc_url=' + escape(doc_url);
1243 flash_plug_html += '"\n';
1244 flash_plug_html += ' type="application/x-shockwave-flash" width="70%" />\n';
1245 flash_plug_html += '</OBJECT>\n';
1246 $("#bookdiv").html(flash_plug_html);
1247}
1248
1249
1250
1251
1252/********************
1253* SLIDESHOW SCRIPTS *
1254********************/
1255
1256function showSlideShow()
1257{
1258
1259 if(!($("#gs-slideshow").length))
1260 {
1261 var slideshowDiv = $("<div>", {id:"gs-slideshow", style:"height:100%;"});
1262 var loadingImage = $("<img>", {src:gs.imageURLs.loading});
1263 slideshowDiv.append(loadingImage);
1264
1265 $.blockUI({message: $(slideshowDiv), css:{top: "5%", left: "5%", width: "90%", height: "90%", overflow: "auto", cursor: "auto"}});
1266
1267 retrieveImagesForSlideShow(function(imageIDArray)
1268 {
1269 loadingImage.hide();
1270 if(imageIDArray && imageIDArray.length > 0)
1271 {
1272 var imageURLs = new Array();
1273 for(var i = 0; i < imageIDArray.length; i++)
1274 {
1275 if(imageIDArray[i].source && imageIDArray[i].source.search(/.*\.(gif|jpg|jpeg|png)$/) != -1)
1276 {
1277 imageURLs.push(gs.collectionMetadata.httpPath + "/index/assoc/" + gs.documentMetadata.assocfilepath + "/" + imageIDArray[i].source);
1278 }
1279 }
1280 new SlideShowWidget(slideshowDiv, imageURLs, imageIDArray);
1281 }
1282 });
1283 }
1284 else
1285 {
1286 $("#gs-slideshow").show();
1287 }
1288}
1289
1290function retrieveImagesForSlideShow(callback)
1291{
1292 var template = "";
1293 template += '<xsl:template match="/">';
1294 template += '<images>[';
1295 template += '<xsl:for-each select="//documentNode">';
1296 // ****
1297 // Used to be "Source" but now changed to "Image"
1298 // This is because Source might not be a web friendly image format (e.g. TIFF original)
1299 // Image gives us a better chance at success, as least with Image, the collectionConfig.xml file can specify a format that is web friendly
1300 // **** **** Is it worth changing the "source" and .source JS field to be "image" and .image??? **** ****
1301 template += '<xsl:text disable-output-escaping="yes">{"source":"</xsl:text><gsf:metadata name="Image"/><xsl:text disable-output-escaping="yes">",</xsl:text>';
1302 template += '<xsl:text disable-output-escaping="yes">"id":"</xsl:text><xsl:value-of select="@nodeID"/><xsl:text disable-output-escaping="yes">"}</xsl:text>';
1303 template += '<xsl:if test="position() != count(//documentNode)">,</xsl:if>';
1304 template += '</xsl:for-each>';
1305 template += ']</images>';
1306 template += '</xsl:template>';
1307 template = makeURLComponentSafe(template);
1308 var url = gs.xsltParams.library_name + "/collection/" + gs.cgiParams.c + "/document/" + gs.cgiParams.d + "?ed=1&ilt=" + template;
1309
1310 $.ajax(url, {dataType: "text"})
1311 .done(function(data) {
1312 var startIndex = data.indexOf(">", data.indexOf(">") + 1) + 1;
1313 var endIndex = data.lastIndexOf("<");
1314 var arrayString = data.substring(startIndex, endIndex);
1315 var imageIDArray = eval(arrayString);
1316
1317 callback(imageIDArray);
1318 });
1319}
1320
1321function SlideShowWidget(mainDiv, images, idArray)
1322{
1323 var _inTransition = false;
1324 var _images = new Array();
1325 var _mainDiv = mainDiv;
1326 var _imageDiv = $("<div>", {id:"ssImageDiv", style:"height:95%; overflow:auto;"});
1327 var _navDiv = $("<div>", {style:"height:5%;"});
1328 var _nextButton = $("<img>", {src:gs.imageURLs.next, style:"float:right; cursor:pointer;"});
1329 var _prevButton = $("<img>", {src:gs.imageURLs.prev, style:"float:left; cursor:pointer; display:none;"});
1330 var _closeLink = $("<a href=\"javascript:$.unblockUI()\">Close Slideshow</a>");
1331 var _clearDiv = $("<div>", {style:"clear:both;"});
1332 var _currentIndex = 0;
1333
1334 _navDiv.append(_nextButton);
1335 _navDiv.append(_closeLink);
1336 _navDiv.append(_prevButton);
1337 _navDiv.append(_clearDiv);
1338 _mainDiv.append(_navDiv);
1339 _mainDiv.append(_imageDiv);
1340
1341 for(var i = 0; i < images.length; i++)
1342 {
1343 _images.push($("<img>", {src:images[i], "class":"slideshowImage"}));
1344 }
1345
1346 if(_images.length < 2)
1347 {
1348 _nextButton.css("display", "none");
1349 }
1350
1351 _imageDiv.append(_images[0]);
1352
1353 this.nextImage = function()
1354 {
1355 if(!_inTransition)
1356 {
1357 _inTransition = true;
1358 if((_currentIndex + 1) < _images.length)
1359 {
1360 _prevButton.css("display", "");
1361 if(_currentIndex + 1 == _images.length - 1)
1362 {
1363 _nextButton.css("display", "none");
1364 }
1365
1366 _imageDiv.fadeOut(500, function()
1367 {
1368 _imageDiv.empty();
1369 _imageDiv.append(_images[_currentIndex + 1]);
1370 _currentIndex++;
1371 _imageDiv.fadeIn(500, function()
1372 {
1373 _inTransition = false;
1374 });
1375 });
1376 }
1377 else
1378 {
1379 _inTransition = false;
1380 }
1381 }
1382 }
1383
1384 this.prevImage = function()
1385 {
1386 if(!_inTransition)
1387 {
1388 _inTransition = true;
1389 if((_currentIndex - 1) >= 0)
1390 {
1391 _nextButton.css("display", "");
1392 if(_currentIndex - 1 == 0)
1393 {
1394 _prevButton.css("display", "none");
1395 }
1396
1397 _imageDiv.fadeOut(500, function()
1398 {
1399 _imageDiv.empty();
1400 _imageDiv.append(_images[_currentIndex - 1]);
1401 _currentIndex--;
1402 _imageDiv.fadeIn(500, function()
1403 {
1404 _inTransition = false;
1405 });
1406 });
1407 }
1408 else
1409 {
1410 _inTransition = false;
1411 }
1412 }
1413 }
1414
1415 var getRootFilenameFromURL = function(url)
1416 {
1417 var urlSegments = url.split("/");
1418 var filename = urlSegments[urlSegments.length - 1];
1419 return filename.replace(/_thumb\..*$/, "");
1420 }
1421
1422 var setLink = function(currentLink, index)
1423 {
1424 $(currentLink).on("click", function()
1425 {
1426 _inTransition = true;
1427 _currentIndex = index;
1428 _imageDiv.fadeOut(500, function()
1429 {
1430 _imageDiv.empty();
1431 _imageDiv.append(_images[_currentIndex]);
1432 _imageDiv.fadeIn(500, function()
1433 {
1434 _inTransition = false;
1435 });
1436 });
1437 });
1438 }
1439
1440 var sliderLinks = $(".pageSliderCol a");
1441 for(var i = 0; i < sliderLinks.length; i++)
1442 {
1443 var currentLink = sliderLinks[i];
1444 var id = $(currentLink).attr("href").split("'")[1];
1445
1446 for(var j = 0; j < idArray.length; j++)
1447 {
1448 if(idArray[j].id == id)
1449 {
1450 var image = idArray[j].source;
1451
1452 for(var l = 0; l < images.length; l++)
1453 {
1454 var filename = getRootFilenameFromURL(images[l]);
1455 if (filename == image)
1456 {
1457 setLink(currentLink, l);
1458 break;
1459 }
1460 }
1461
1462 break;
1463 }
1464 }
1465 }
1466
1467 _nextButton.on("click", this.nextImage);
1468 _prevButton.on("click", this.prevImage);
1469}
Note: See TracBrowser for help on using the repository browser.