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

Last change on this file since 33155 was 33155, checked in by davidb, 5 years ago

Moved two functions from document script to utility script so they are available to other pages as well (e.g. berrybasket tearaway on search and browse pages)

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