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

Last change on this file since 32502 was 32502, checked in by kjdon, 6 years ago

can't assume TextQuery is previous service as it may have been eg AdvancedFieldQuery

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