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

Last change on this file since 32767 was 32767, checked in by kjdon, 5 years ago

tomcat 8 change - it didn't like unescaped chars in the inline templates. so we need to do more esacping

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