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

Last change on this file since 32157 was 32157, checked in by Georgiy Litvinov, 6 years ago

Added navigation for search terms

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