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

Last change on this file since 31748 was 31748, checked in by kjdon, 7 years ago

trying to make document editing javascript more easy to follow. document_scripts now contains no editing functionality. Content editing (metadata and text) is now in documentedit_scripts.js, which also uses documentedit_scripts_util.js. documentmaker_scripts is for the functionality where sections are moved around?? not sure, as we have never got it working. this also uses documentedit_scripts_util.js (which was just renamed from documentmaker_scripts_util.js)

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