custom_search.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. (function($) {
  2. Drupal.behaviors.custom_search = {
  3. attach: function(context) {
  4. if (!Drupal.settings.custom_search.solr) {
  5. // Check if the search box is not empty on submit
  6. $('form.search-form', context).submit(function(){
  7. var box = $(this).find('input.custom-search-box');
  8. if (box.val() != undefined && box.val() == '') {
  9. $(this).find('input.custom-search-box').addClass('error');
  10. return false;
  11. }
  12. // If basic search is hidden, copy or value to the keys
  13. if ($(this).find('#edit-keys').parents('div.element-invisible').attr('class') == 'element-invisible') {
  14. $(this).find('#edit-keys').val($(this).find('#edit-or').val());
  15. $(this).find('#edit-or').val('');
  16. }
  17. return true;
  18. });
  19. }
  20. // Search from target
  21. $('form.search-form').attr('target', Drupal.settings.custom_search.form_target);
  22. // Displays Popup.
  23. $('form.search-form input.custom-search-box', context).bind('click focus', function(e){
  24. $this = $(this);
  25. $parentForm = $this.parents('form');
  26. // check if there's something in the popup and displays it
  27. var popup = $parentForm.find('fieldset.custom_search-popup');
  28. if (popup.find('input,select').length && !popup.hasClass('opened')) popup.fadeIn().addClass('opened');
  29. e.stopPropagation();
  30. });
  31. $(document).bind('click focus', function(){
  32. $('fieldset.custom_search-popup').hide().removeClass('opened');
  33. });
  34. // Handle checkboxes
  35. $('.custom-search-selector input:checkbox', context).each(function(){
  36. var el = $(this);
  37. if (el.val() == 'c-all') {
  38. el.change(function(){
  39. $(this).parents('.custom-search-selector').find('input:checkbox[value!=c-all]').attr('checked', false);
  40. });
  41. }
  42. else {
  43. if (el.val().substr(0,2) == 'c-') {
  44. el.change(function(){
  45. $('.custom-search-selector input:checkbox').each(function(){
  46. if ($(this).val().substr(0,2) == 'o-') $(this).attr('checked', false);
  47. });
  48. $(this).parents('.custom-search-selector').find('input:checkbox[value=c-all]').attr('checked', false);
  49. });
  50. } else {
  51. el.change(function(){
  52. $(this).parents('.custom-search-selector').find('input:checkbox[value!='+el.val()+']').attr('checked', false);
  53. });
  54. }
  55. }
  56. });
  57. // Reselect types and terms in advanced search
  58. var edit_keys = $('#edit-keys').val();
  59. if(edit_keys) {
  60. // types
  61. var pos = edit_keys.indexOf('type:');
  62. if (pos) {
  63. var pos2 = edit_keys.indexOf(' ',pos);
  64. if (pos2==-1) pos2 = edit_keys.length;
  65. var types = edit_keys.substring(pos+5,pos2);
  66. types = types.split(',');
  67. for (var i=0; i<types.length; i++) {
  68. $('.search-form input:checkbox[value='+types[i]+']').attr('checked', true);
  69. }
  70. }
  71. // terms
  72. var pos = edit_keys.indexOf('term:');
  73. if (pos) {
  74. var pos2 = edit_keys.indexOf(' ',pos);
  75. if (pos2==-1) pos2 = edit_keys.length;
  76. var terms = edit_keys.substring(pos+5,pos2);
  77. terms = terms.split(',');
  78. for (var i=0; i<terms.length; i++) {
  79. $('#edit-term option[value='+terms[i]+']').attr('selected', true);
  80. }
  81. }
  82. // languages
  83. var pos = edit_keys.indexOf('language:');
  84. if (pos) {
  85. var pos2 = edit_keys.indexOf(' ',pos);
  86. if (pos2==-1) pos2 = edit_keys.length;
  87. var languages = edit_keys.substring(pos+9,pos2);
  88. languages = languages.split(',');
  89. for (var i=0; i<languages.length; i++) {
  90. $('.search-advanced #edit-language-'+languages[i]).attr('checked', true);
  91. }
  92. }
  93. }
  94. var popup = $('fieldset.custom_search-popup:not(.custom_search-processed)', context).addClass("custom_search-processed");
  95. popup.click(function(e){
  96. e.stopPropagation();
  97. })
  98. popup.append('<a class="custom_search-popup-close" href="#">' + Drupal.t('Close') + '</a>');
  99. $('a.custom_search-popup-close').click(function(e){
  100. $('fieldset.custom_search-popup.opened').hide().removeClass('opened');
  101. e.preventDefault();
  102. });
  103. }
  104. }
  105. })(jQuery);