advanced-search.js 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. /* NOTE this script requires the ieat-util.js script to function */
  2. var as_types = ["1","4","2","3"];
  3. var as_type_rules = new Array();
  4. as_type_rules["1"] = ["1","3","4"];
  5. as_type_rules["2"] = ["5","6"];
  6. as_type_rules["3"] = ["1","2"];
  7. as_type_rules["4"] = ["1","3","4"];
  8. var as_choice_rule = new Array();
  9. as_choice_rule["1"] = [
  10. ["1","1"],
  11. ["2","2"],
  12. ["3","3"],
  13. ["4","4"],
  14. ["5","5"]
  15. ];
  16. as_choice_rule["4"] = [
  17. ["1","1"],
  18. ["2","2"],
  19. ["3","3"],
  20. ["4","4"],
  21. ["5","5"]
  22. ];
  23. var as_text_choice_rule = new Array();
  24. //as_text_choice_rule["4"] = true;
  25. var as_help = new Array();
  26. as_help["1"] = '/help.do?key=search.averagerating';
  27. as_help["2"] = '/help.do?key=search.text';
  28. as_help["3"] = '/help.do?key=search.owner';
  29. /**
  30. * Initialize an advanced search form row.
  31. *
  32. * @param rowNum the form row number to initialize
  33. */
  34. function initAdvancedSearchRow(rowNum) {
  35. var form = getAdvancedSearchForm();
  36. // init the search type select
  37. var typeSelect = getEmptySelect(form,"criteria.advanced["+rowNum+"].type");
  38. typeSelect.onchange = handleAdvancedSearchTypeChange;
  39. typeSelect.appendChild(document.createElement("option"));
  40. for ( var i = 0; i < as_types.length; i++ ) {
  41. var newOption = document.createElement("option");
  42. newOption.text = getMessage("msg_as_type_" +as_types[i]);
  43. newOption.value = as_types[i];
  44. typeSelect.appendChild(newOption);
  45. }
  46. // clear out the other selects
  47. advancedSearchClearOp(form,rowNum);
  48. // uncheck the "required" checkbox
  49. var requiredCheckbox = form.elements["criteria.advanced["+rowNum+"].required"];
  50. requiredCheckbox.checked = false;
  51. // hide the choice elements
  52. var choiceSelect = form.elements["criteria.advanced["+rowNum+"].choice"];
  53. choiceSelect.style.display = 'none';
  54. var queryText = form.elements["criteria.advanced["+rowNum+"].query"];
  55. queryText.style.display = 'none';
  56. var opSelect = form.elements["criteria.advanced["+rowNum+"].op"];
  57. // make sure nothing between opSelect and typeSelect, which clears out
  58. // any existing help link
  59. while ( opSelect.previousSibling != typeSelect ) {
  60. opSelect.parentNode.removeChild(opSelect.previousSibling);
  61. }
  62. if ( advancedCriteria[rowNum] ) {
  63. // initialize the form using the advancedCriteria array data, which is arranged:
  64. // 0: type
  65. // 1: op
  66. // 2: choice
  67. // 3: query
  68. // 4: required
  69. for ( var i = 0; i < typeSelect.options.length; i++ ) {
  70. if ( typeSelect.options[i].value == advancedCriteria[rowNum][0] ) {
  71. typeSelect.options[i].selected = true;
  72. break;
  73. }
  74. }
  75. var event = new Object();
  76. event.target = typeSelect;
  77. handleAdvancedSearchTypeChange(event);
  78. for ( var i = 0; i < opSelect.options.length; i++ ) {
  79. if ( opSelect.options[i].value == advancedCriteria[rowNum][1] ) {
  80. opSelect.options[i].selected = true;
  81. break;
  82. }
  83. }
  84. for ( var i = 0; i < choiceSelect.length; i++ ) {
  85. if ( choiceSelect.options[i].value == advancedCriteria[rowNum][2] ) {
  86. choiceSelect.options[i].selected = true;
  87. break;
  88. }
  89. }
  90. queryText.value = advancedCriteria[rowNum][3];
  91. if ( advancedCriteria[rowNum][4] ) {
  92. requiredCheckbox.checked = true;
  93. }
  94. }
  95. }
  96. function advancedSearchClearOp(form,rowNum) {
  97. getEmptySelect(form,"criteria.advanced["+rowNum+"].op");
  98. advancedSearchClearChoice(form,rowNum);
  99. }
  100. function advancedSearchClearChoice(form,rowNum) {
  101. var choiceSelect = getEmptySelect(form,"criteria.advanced["+rowNum+"].choice");
  102. choiceSelect.style.display = "none";
  103. var textText = form.elements["criteria.advanced["+rowNum+"].query"];
  104. textText.value = "";
  105. textText.style.display = "none";
  106. }
  107. function handleAdvancedSearchTypeChange(event) {
  108. var typeSelect = event.target;
  109. var rowNum = typeSelect.name.match(/\d+/)[0];
  110. var selectedValue = typeSelect.options[typeSelect.selectedIndex].value;
  111. //alert("got value: " +selectedValue +" for row num " +rowNum);
  112. if ( !selectedValue ) {
  113. // clear out other selects, etc
  114. advancedSearchClearOp(typeSelect.form,rowNum);
  115. } else {
  116. // clear out choice data
  117. advancedSearchClearChoice(typeSelect.form,rowNum);
  118. // populate op select
  119. var opSelect = getEmptySelect(typeSelect.form,"criteria.advanced["+rowNum+"].op");
  120. var as_ops = as_type_rules[selectedValue];
  121. //opSelect.appendChild(document.createElement("option"));
  122. for ( var i = 0; i < as_ops.length; i++ ) {
  123. var newOption = document.createElement("option");
  124. newOption.text = getMessage("msg_as_op_" +as_ops[i]);
  125. newOption.value = as_ops[i];
  126. opSelect.appendChild(newOption);
  127. }
  128. // make sure nothing between opSelect and typeSelect, which clears out
  129. // any existing help link
  130. while ( opSelect.previousSibling != typeSelect ) {
  131. opSelect.parentNode.removeChild(opSelect.previousSibling);
  132. }
  133. // insert help link if available
  134. if ( as_help[selectedValue] ) {
  135. var helpLink = document.createElement("a");
  136. var sup = document.createElement("sup");
  137. sup.appendChild(document.createTextNode("?"));
  138. helpLink.appendChild(sup);
  139. var linkUrl = webContext+as_help[selectedValue];
  140. helpLink.setAttribute("class","help");
  141. helpLink.href = linkUrl;
  142. helpLink.onclick = popupWin(linkUrl,"width=480,height=175,location=0");
  143. opSelect.parentNode.insertBefore(helpLink,opSelect);
  144. }
  145. // populate the choice select or display the text input
  146. var choice_data = as_choice_rule[selectedValue];
  147. var choiceSelect = typeSelect.form.elements["criteria.advanced["+rowNum+"].choice"];
  148. var textText = typeSelect.form.elements["criteria.advanced["+rowNum+"].query"];
  149. if ( choice_data ) {
  150. for ( var i = 0; i < choice_data.length; i++ ) {
  151. var newOption = document.createElement("option");
  152. newOption.value = choice_data[i][0];
  153. newOption.text = choice_data[i][1];
  154. choiceSelect.appendChild(newOption);
  155. }
  156. choiceSelect.style.display = "inline";
  157. textText.style.display = as_text_choice_rule[selectedValue] ? "inline" : "none";
  158. } else {
  159. choiceSelect.style.display = "none";
  160. textText.style.display = "inline";
  161. }
  162. }
  163. }
  164. function addAdvancedSearchCriteria() {
  165. // get the search criteria table
  166. var row = document.getElementById('criteria_row');
  167. // get the add row to insert the cloned row into
  168. var addRow = document.getElementById('criteria_add_row');
  169. // clone the ingredient row
  170. var newRow = row.cloneNode(true);
  171. // remove 'id' attribute
  172. newRow.removeAttribute('id');
  173. // make the 'minus' button appear
  174. var minus = findNodeWithAttributeValue(newRow,'class','minus');
  175. if ( minus && minus.style ) {
  176. minus.style.display = 'block';
  177. }
  178. // find all 'name' attributes, and increment index values
  179. replaceAttribute(newRow,'name','advanced\\[\\d+\\]',
  180. 'advanced[' +numAdvancedCriteria +']');
  181. replaceAttribute(newRow,'href','javascript:removeAdvancedSearchCriteria\\(\\d+\\)',
  182. 'javascript:removeAdvancedSearchCriteria(' +numAdvancedCriteria +')');
  183. // insert into table
  184. addRow.parentNode.insertBefore(newRow,addRow);
  185. initAdvancedSearchRow(numAdvancedCriteria);
  186. numAdvancedCriteria++;
  187. }
  188. function removeAdvancedSearchCriteria(criteriaIndex) {
  189. // loop through all rows in criteria table, removing the
  190. // row in question and then re-naming any subsequent rows
  191. // so index values decrease
  192. if ( criteriaIndex < 1 ) {
  193. alert(getMessage('msg_noDeleteFirstCriteria'));
  194. return;
  195. }
  196. // forward to proper row
  197. var rowIdx = 0;
  198. var criteriaRow = document.getElementById('criteria_row');
  199. if ( !criteriaRow ) {
  200. alert("Could not get criteria_row!");
  201. return;
  202. }
  203. while ( criteriaRow != null && rowIdx < criteriaIndex ) {
  204. criteriaRow = criteriaRow.nextSibling;
  205. if ( criteriaRow.nodeName.toLowerCase() == 'tr' ) {
  206. rowIdx++;
  207. }
  208. }
  209. var currRow = criteriaRow.nextSibling;
  210. for ( var currIndex = criteriaIndex; currIndex < (numAdvancedCriteria - 1) && currRow != null
  211. && currRow.nextSibling != null; currRow = currRow.nextSibling )
  212. {
  213. if ( currRow.nodeName.toLowerCase() != 'tr' ) continue;
  214. replaceAttribute(currRow,'name','advanced\\[\\d+\\]',
  215. 'advanced[' +currIndex +']');
  216. replaceAttribute(currRow,'href','javascript:removeAdvancedSearchCriteria\\(\\d+\\)',
  217. 'javascript:removeAdvancedSearchCriteria(' +currIndex +')');
  218. currIndex++;
  219. }
  220. // now remove criteriaRow from table
  221. if ( criteriaRow.parentNode.removeChild(criteriaRow) ) {
  222. numAdvancedCriteria--;
  223. } else {
  224. alert("Unable to remove criteria node!");
  225. }
  226. }
  227. function getAdvancedSearchForm() {
  228. var form = document.getElementById("advancedSearchForm");
  229. if ( !form ) {
  230. alert("Could not get advanced search form.");
  231. }
  232. return form;
  233. }
  234. function getEmptySelect(form, name) {
  235. var typeSelect = form.elements[name];
  236. if ( !typeSelect ) {
  237. alert("Could not get advanced search select '" +name +"'");
  238. return;
  239. }
  240. // remove any options
  241. if ( typeSelect.options.length > 0 ) {
  242. removeChildren(typeSelect);
  243. }
  244. return typeSelect;
  245. }