You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

171 lines
5.9 KiB

  1. // Project: Web Reference Database (refbase) <http://www.refbase.net>
  2. // Copyright: Matthias Steffens <mailto:refbase@extracts.de> and the file's
  3. // original author(s).
  4. //
  5. // This code is distributed in the hope that it will be useful,
  6. // but WITHOUT ANY WARRANTY. Please see the GNU General Public
  7. // License for more details.
  8. //
  9. // File: ./javascript/show.js
  10. // Repository: $HeadURL$
  11. // Author(s): Matthias Steffens <mailto:refbase@extracts.de>
  12. //
  13. // Created: 27-May-07, 00:54
  14. // Modified: $Date: 2008-09-18 14:09:58 +0000 (Thu, 18 Sep 2008) $
  15. // $Author$
  16. // $Revision: 1244 $
  17. // This Javascript allows to dynamically include references from a refbase database
  18. // within your own HTML pages.
  19. // Sample HTML pages that show how to call function 'showRefs()' from within your
  20. // own HTML pages:
  21. // <http://beta.refbase.net/include_test.html>
  22. // <http://beta.refbase.net/javascript/include_test2.html>
  23. // <http://beta.refbase.net/javascript/include_test3.html>
  24. // More info is available in the refbase forums:
  25. // <http://sourceforge.net/forum/message.php?msg_id=4404553>
  26. // You may want to adopt following variables to your needs:
  27. // - variable 'authorNames' contains regex patterns for all authors whose names
  28. // shall be printed in bold face
  29. // - variable 'url' in function 'showRefs()' must contain the URL to the refbase
  30. // 'show.php' script on your server
  31. // - you may want to tweak other parameter settings in function 'showRefs' (e.g.
  32. // parameters 'showRows', 'citeStyle' or 'citeOrder')
  33. // Also, please make sure that this javascript file is executable.
  34. // AJAX-specific functions were adopted from sample code by Neil Saunders:
  35. // <http://nsaunders.wordpress.com/2007/02/20/my-first-ajax-for-bioinformatics-page/>
  36. // TODO: easier customization
  37. var xmlHTTP;
  38. // List of authors whose names will be printed in bold face (adopt to your needs):
  39. // NOTES: - the given regex patterns must match the author string formatting of the
  40. // chosen citation style (see 'citeStyle' parameter in function 'showRefs')
  41. // - each regex pattern must be enclosed in parentheses
  42. var authorNames = new Array(
  43. /(Granskog, M\.)/g,
  44. /(Mock, T\.)/g,
  45. /(Spindler, M\.)/g,
  46. /(Thomas, D\. N\.)/g
  47. );
  48. // ------------------------------------------------------------------
  49. // Create refbase query URL:
  50. // This function expects a query string of 'show.php' parameter=value pairs. More info about
  51. // the 'show.php' API: <http://linking.refbase.net/>, <http://bibliographies.refbase.net/>
  52. function showRefs(query) {
  53. if (query == "none") {
  54. document.getElementById("includerefs").innerHTML = "<div id='includeprogress'>References will be listed here.</div>";
  55. }
  56. else {
  57. var url = "show.php"; // set URL to your server's 'show.php' script
  58. url = url + "?" + query;
  59. url = url + "&client=" + "inc-refbase-1.0"; // "inc-" indicates include mechanisms
  60. url = url + "&wrapResults=" + "0"; // output only a partial document structure containing solely the search results
  61. // set defaults if some params were not given in the query:
  62. // (empty param values trigger the database defaults)
  63. if (query.search(/submit=(Display|Cite|Export|Browse)?(?=&|$)/) == -1)
  64. url = url + "&submit=Cite"; // possible values: "Cite", "List", "Display"
  65. if (query.search(/showLinks=[01]/) == -1)
  66. url = url + "&showLinks=1";
  67. if (query.search(/showRows=\d+/) == -1)
  68. url = url + "&showRows=100";
  69. if (query.search(/startRecord=\d+/) == -1)
  70. url = url + "&startRecord=1";
  71. if (query.search(/citeStyle=\w+/) == -1)
  72. url = url + "&citeStyle=APA"; // the specified citation style must have a matching entry within the 'styles' MySQL table
  73. if (query.search(/citeOrder=\w+/) == -1)
  74. url = url + "&citeOrder=year"; // possible values: "author", "year", "type", "type-year", "creation-date"
  75. if (query.search(/without=/) == -1)
  76. url = url + "&without=dups";
  77. getURL(url);
  78. }
  79. }
  80. // ------------------------------------------------------------------
  81. // Print a string (that matches a pattern in 'authorNames') in bold face:
  82. function highlightAuthors(sourceText) {
  83. for (var i = 0; i < authorNames.length; i++) {
  84. if (sourceText.search(authorNames[i]) > 0)
  85. sourceText = sourceText.replace(authorNames[i], "<b>$1</b>");
  86. }
  87. return sourceText;
  88. }
  89. // ------------------------------------------------------------------
  90. // Send an XMLHTTP request:
  91. function getURL(url) {
  92. xmlHTTP = getXMLHTTPObject();
  93. if (xmlHTTP == null) {
  94. alert ("Browser does not support HTTP Request");
  95. return;
  96. }
  97. xmlHTTP.onreadystatechange = stateChanged;
  98. xmlHTTP.open("GET", url, true);
  99. xmlHTTP.send(null);
  100. }
  101. // ------------------------------------------------------------------
  102. // Update an HTML element (with id = "includerefs") with the response text returned by the XMLHTTP request:
  103. function stateChanged() {
  104. document.getElementById("includerefs").innerHTML = "<div id='includeprogress'>Fetching references from database... " + "<img src='img/progress.gif'></div>";
  105. if (xmlHTTP.readyState == 4 || xmlHTTP.readyState == "complete") {
  106. var response = xmlHTTP.responseText;
  107. if (!response) {
  108. document.getElementById("includerefs").innerHTML = "<div id='includeprogress'>No data returned!</div>";
  109. }
  110. else {
  111. document.getElementById("includerefs").innerHTML = highlightAuthors(response);
  112. }
  113. }
  114. }
  115. // ------------------------------------------------------------------
  116. // Create an XMLHTTP object:
  117. function getXMLHTTPObject() {
  118. var xmlHTTP = null;
  119. try {
  120. // Firefox, Opera 8.0+, Safari:
  121. xmlHTTP = new XMLHttpRequest();
  122. }
  123. catch (e) {
  124. // Internet Explorer:
  125. try {
  126. xmlHTTP = new ActiveXObject("Msxml2.XMLHTTP");
  127. }
  128. catch (e) {
  129. xmlHTTP = new ActiveXObject("Microsoft.XMLHTTP");
  130. }
  131. }
  132. return xmlHTTP;
  133. }
  134. // ------------------------------------------------------------------