custom_search.js 4.6 KB

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