PNA.fi koodi

ruoka.js 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /**
  2. * Copyright 2010 Timo Siiranen.
  3. * Public Domain
  4. *
  5. * Copyright 2018 Toni Fadjukoff. All Rights Reserved.
  6. *
  7. * Licensed under the Apache License, Version 2.0 (the "License");
  8. * you may not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS,
  15. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. */
  19. function is_in_list(list, num) {
  20. for (i = 0; i < list.length; i++) {
  21. if (list[i] == num)
  22. return true;
  23. }
  24. return false;
  25. }
  26. function any_allergies_set()
  27. {
  28. for (a = 0; a < allergies.length; a++) {
  29. var e = document.getElementById("allergy_" + allergies[a]);
  30. if (e.checked)
  31. return true;
  32. }
  33. return false;
  34. }
  35. function unhighlight() {
  36. for (f = 0; f < food_count; f++) {
  37. var e = document.getElementById("f"+f);
  38. e.style.background = "#ffffff";
  39. }
  40. }
  41. function fix_hrefs() {
  42. // change all links to submit-buttons
  43. for (f = 0; f < document.links.length; f++) {
  44. var href = document.links[f].href;
  45. var i = href.lastIndexOf('/') + 1;
  46. if (href[i] >= '1' && href[i] <= '7' && href[i+1] == '.') {
  47. document.links[f].onclick = function() {
  48. document.forms[0].action = this.href;
  49. document.forms[0].submit();
  50. return false;
  51. }
  52. }
  53. }
  54. }
  55. function highlight() {
  56. fix_hrefs();
  57. if (!any_allergies_set()) {
  58. unhighlight();
  59. return;
  60. }
  61. for (f = 0; f < food_count; f++) {
  62. var eatable = 1;
  63. for (a = 0; a < allergies.length; a++) {
  64. var e = document.getElementById("allergy_" + allergies[a]);
  65. if (e.checked) {
  66. if (is_in_list(eatable_foods[allergies[a]], f)) {
  67. // still (maybe) eatable
  68. } else if (is_in_list(maybe_eatable_foods[allergies[a]], f)) {
  69. // just maybe eatable
  70. eatable = 0;
  71. } else {
  72. eatable = -1;
  73. }
  74. }
  75. }
  76. var e = document.getElementById("f"+f);
  77. if (eatable < 0) {
  78. e.style.background = "#ffffff";
  79. //e.style.display = "none";
  80. } else {
  81. //e.style.display = "list-item";
  82. e.style.background = eatable > 0 ? "#ffff00" : "#cccccc";
  83. }
  84. }
  85. }
  86. function url_param(name)
  87. {
  88. // From http://www.netlobo.com/url_query_string_javascript.html
  89. name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  90. var regexS = "[\\?&]"+name+"=([^&#]*)";
  91. var regex = new RegExp( regexS );
  92. var results = regex.exec( window.location.href );
  93. if( results == null )
  94. return "";
  95. else
  96. return results[1];
  97. }
  98. function set_allergies()
  99. {
  100. for (a = 0; a < allergies.length; a++) {
  101. var e = document.getElementById("allergy_" + allergies[a]);
  102. if (url_param("allergy_" + allergies[a]) != "")
  103. e.checked = true;
  104. }
  105. highlight();
  106. }
  107. var NO_WARNING_SETTING = "pna-no-warning";
  108. function shouldShowWarning() {
  109. if (window.localStorage) {
  110. return !localStorage[NO_WARNING_SETTING];
  111. } else {
  112. return document.cookie.indexOf(NO_WARNING_SETTING) == -1;
  113. }
  114. }
  115. function noMoreWarning() {
  116. if (window.localStorage) {
  117. localStorage[NO_WARNING_SETTING] = "true";
  118. } else {
  119. document.cookie += NO_WARNING_SETTING + "=true; expires=Tue, 19 Jan 2038 03:14:07 GMT";
  120. }
  121. }
  122. function show_warning() {
  123. var notice = document.getElementById("notice");
  124. if (shouldShowWarning()) {
  125. var input = document.createElement("input");
  126. input.type="submit";
  127. input.value="Älä näytä tätä enää";
  128. input.onclick = function() {
  129. notice.parentNode.removeChild(notice);
  130. noMoreWarning();
  131. return false;
  132. }
  133. notice.appendChild(input)
  134. } else {
  135. notice.parentNode.removeChild(notice);
  136. }
  137. }