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

Last change on this file since 33016 was 33016, checked in by ak19, 5 years ago

Getting facet searching working again in tomcat 8, where it had broken due to reserved and unsafe chars in the URL. The fix required using the recent makeURLSafe() and makeURLComponentSafe() methods in facet-scripts.js. This meant that these functions and their helper functions needed to be moved into their own script file, newly introducing utility_scripts.js, rather than remain in document_scripts.js since doc_scripts.js is not included on the query.xsl page which includes facet-scripts.js

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