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

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

am going to call expandAndExecute with null executeAfter function, so test it before calling it

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