diff --git a/js/jquery.autocomplete.js b/js/jquery.autocomplete.js index be18b96289de7064ce5f4fd99ba17f3cf79780f1..09b889fb50f77f7b36c44477d4fcf61f5052d708 100644 --- a/js/jquery.autocomplete.js +++ b/js/jquery.autocomplete.js @@ -23,20 +23,25 @@ innerHTML += ('
' + output + '
'); }); } else { - // Case no results : - innerHTML += ('
' + item.label + '
'); + innerHTML += ('
' + item.label + '
'); } innerHTML += ''; var group = ''; if ("group" in item) { - group += ('
' + item.group.group_name + '
'); + groupId = typeof(item.group.group_id) !== 'undefined' ? item.group.group_id : ''; + groupName = typeof(item.group.group_name) !== 'undefined' ? item.group.group_name : ''; + group += ('
' + groupName + '
'); $(group).appendTo(ul); } var elem = $("
  • " ) - .data("item.autocomplete", item) - .append("" + innerHTML + "") - .appendTo(ul); + .append("" + innerHTML + ""); + if (item.value == '') { + elem = $("
  • " + item.label + "
  • " ); + } + elem.data("item.autocomplete", item) + .appendTo(ul); + Drupal.attachBehaviors(elem); return elem; }; @@ -50,61 +55,70 @@ attach: function(context) { if (Drupal.settings.search_autocomplete) { $.each(Drupal.settings.search_autocomplete, function(key, value) { - var NoResultsLabel = Drupal.settings.search_autocomplete[key].no_results; + var no_results = Drupal.settings.search_autocomplete[key].no_results; + var all_results = Drupal.settings.search_autocomplete[key].all_results; $(Drupal.settings.search_autocomplete[key].selector).bind("mouseover", function() { $(Drupal.settings.search_autocomplete[key].selector).addClass('ui-autocomplete-processed ui-theme-' + Drupal.settings.search_autocomplete[key].theme).autocomplete({ - minLength: Drupal.settings.search_autocomplete[key].minChars, - source: function(request, response) { - // External URL: - if (Drupal.settings.search_autocomplete[key].type == 'external') { - $.getJSON(Drupal.settings.search_autocomplete[key].datas, { q: request.term }, function (results) { - // Only return the number of values set in the settings. - if (!results.length && NoResultsLabel) { - results = [NoResultsLabel.replace("[search-phrase]", request.term)]; - } - response(results.slice(0, Drupal.settings.search_autocomplete[key].max_sug)); - }); - } - // Internal URL: - else if (Drupal.settings.search_autocomplete[key].type == 'internal' || Drupal.settings.search_autocomplete[key].type == 'view') { - $.getJSON(Drupal.settings.search_autocomplete[key].datas + request.term, { }, function (results) { - // Only return the number of values set in the settings. - if (!results.length && NoResultsLabel) { - results = [NoResultsLabel.replace("[search-phrase]", request.term)]; + minLength: Drupal.settings.search_autocomplete[key].minChars, + source: function(request, response) { + // External URL: + if (Drupal.settings.search_autocomplete[key].type == 'external') { + $.getJSON(Drupal.settings.search_autocomplete[key].datas, { q: request.term }, function (results) { + // Only return the number of values set in the settings. + if (!results.length && no_results) { + results = [jQuery.parseJSON(no_results.replace(/\[search-phrase\]/g, request.term))]; + } else { + results.slice(0, Drupal.settings.search_autocomplete[key].max_sug); + results.push(jQuery.parseJSON(all_results.replace(/\[search-phrase\]/g, request.term))); + } + response(results); + }); + } + // Internal URL: + else if (Drupal.settings.search_autocomplete[key].type == 'internal' || Drupal.settings.search_autocomplete[key].type == 'view') { + $.getJSON(Drupal.settings.search_autocomplete[key].datas + request.term, { }, function (results) { + // Only return the number of values set in the settings. + if (!results.length && no_results) { + results = [jQuery.parseJSON(no_results.replace(/\[search-phrase\]/g, request.term))]; + } else { + results.slice(0, Drupal.settings.search_autocomplete[key].max_sug); + results.push(jQuery.parseJSON(all_results.replace(/\[search-phrase\]/g, request.term))); + } + response(results); + }); + } + // Static resources: + else if (Drupal.settings.search_autocomplete[key].type == 'static') { + var results = $.ui.autocomplete.filter(Drupal.settings.search_autocomplete[key].datas, request.term); + if (!results.length && no_results) { + results = [jQuery.parseJSON(no_results.replace(/\[search-phrase\]/g, request.term))]; + } else { + results.slice(0, Drupal.settings.search_autocomplete[key].max_sug); + results.push(jQuery.parseJSON(all_results.replace(/\[search-phrase\]/g, request.term))); } - response(results.slice(0, Drupal.settings.search_autocomplete[key].max_sug)); - }); - } - // Static resources: - else if (Drupal.settings.search_autocomplete[key].type == 'static') { - var results = $.ui.autocomplete.filter(Drupal.settings.search_autocomplete[key].datas, request.term); - if (!results.length && NoResultsLabel) { - results = [NoResultsLabel.replace("[search-phrase]", request.term)]; - } - // Only return the number of values set in the settings. - response(results.slice(0, Drupal.settings.search_autocomplete[key].max_sug)); - } - }, - open: function(event, ui) { - $(".ui-autocomplete li.ui-menu-item:odd").addClass("ui-menu-item-odd"); - $(".ui-autocomplete li.ui-menu-item:even").addClass("ui-menu-item-even"); - }, - select: function(event, ui) { - if (ui.item.label === NoResultsLabel) { - event.preventDefault(); - } else if (Drupal.settings.search_autocomplete[key].auto_redirect == 1 && ui.item.link) { - document.location.href = ui.item.link; - } else if (Drupal.settings.search_autocomplete[key].auto_submit == 1) { - $(this).val(ui.item.label); - $(this).closest("form").submit(); - } - }, - focus: function (event, ui) { - if (ui.item.label === NoResultsLabel) { - event.preventDefault(); - } - }, - appendTo: $(Drupal.settings.search_autocomplete[key].selector).parent(), + response(results); + } + }, + open: function(event, ui) { + $(".ui-autocomplete li.ui-menu-item:odd").addClass("ui-menu-item-odd"); + $(".ui-autocomplete li.ui-menu-item:even").addClass("ui-menu-item-even"); + }, + select: function(event, ui) { + if (Drupal.settings.search_autocomplete[key].auto_redirect == 1 && ui.item.link) { + document.location.href = ui.item.link; + } else if (Drupal.settings.search_autocomplete[key].auto_submit == 1 && ui.item.value) { + $(this).val(ui.item.value); + $(this).closest("form").submit(); + } else { + event.preventDefault(); + } + }, + focus: function (event, ui) { + if (ui.item.label === no_results) { + event.preventDefault(); + } + }, + appendTo: $(Drupal.settings.search_autocomplete[key].selector).parent(), }).autocomplete("widget").attr("id", "ui-theme-" + Drupal.settings.search_autocomplete[key].theme); }); $(Drupal.settings.search_autocomplete[key].selector).trigger('mouseover'); diff --git a/search_autocomplete.form.add.inc b/search_autocomplete.form.add.inc index a553f7d1d8289960cd8484764d618d88cbe024e8..ec3875655bea2e14979cbaf5f9a2792d3b4139f4 100644 --- a/search_autocomplete.form.add.inc +++ b/search_autocomplete.form.add.inc @@ -77,11 +77,27 @@ function search_autocomplete_form_add_submit($form, &$form_state) { // Insert the new form in database. $fid = db_insert('search_autocomplete_forms') - ->fields(array( - 'title' => $values['title'], - 'selector' => $values['selector'], - )) - ->execute(); + ->fields(array( + 'title' => $values['title'], + 'selector' => $values['selector'], + 'no_results' => json_encode(array( // manually add no_results default + 'label' => 'No results found for [search-phrase].
    Click to perform full search.', + 'value' => '[search-phrase]', + 'link' => '', + 'group' => array( + 'group_id' => 'no_results', + ), + )), + 'all_results' => json_encode(array( // manually add all_results default + 'label' => 'View all results for [search-phrase].', + 'value' => '[search-phrase]', + 'link' => '', + 'group' => array( + 'group_id' => 'all_results', + ), + )), + )) + ->execute(); // Redirect to configuration page. $form_state['redirect'] = 'admin/config/search/search_autocomplete/' . $fid . '/configure'; diff --git a/search_autocomplete.form.configure.inc b/search_autocomplete.form.configure.inc index 13566b1aa4b5bc96a5cc99b964f5fd63b2830875..c76943a669f68771539cb2ffb7bcee484423ebb8 100644 --- a/search_autocomplete.form.configure.inc +++ b/search_autocomplete.form.configure.inc @@ -57,15 +57,7 @@ function search_autocomplete_form_configure($form, &$form_state) { '#maxlength' => 2, '#required' => TRUE, ); - // Check if form should be autosubmitted. - $form['search_autocomplete_how']['no_results'] = array( - '#type' => 'textfield', - '#title' => t('Behavior when no suggestions are found'), - '#description' => t('Enter a message to display when no results are found. Leave empty for no message. You can use HTML tags as well as the token [search-phrase] to replace user input.'), - '#default_value' => $item->no_results, - '#maxlength' => 50, - '#required' => FALSE, - ); + // Check if form should be autosubmitted. $form['search_autocomplete_how']['auto_submit'] = array( '#type' => 'checkbox', @@ -81,6 +73,80 @@ function search_autocomplete_form_configure($form, &$form_state) { '#default_value' => $item->auto_redirect, ); + // ### + $all_results = json_decode($item->all_results); + // "View all results" custom configurations + $form['search_autocomplete_how']['view_all_results'] = array( + '#type' => 'fieldset', + '#title' => t('Custom behavior when some suggestions are available'), + '#collapsible' => TRUE, + '#collapsed' => TRUE, + ); + // Check if form should be autosubmitted. + $form['search_autocomplete_how']['view_all_results']['all_results_label'] = array( + '#type' => 'textfield', + '#title' => t('Custom "view all results" message label'), + '#description' => t('This message is going to be displayed at the end of suggestion list when suggestions are found. Leave empty to disable this functionality. You can use HTML tags as well as the token [search-phrase] to replace user input.'), + '#default_value' => $all_results->label, + '#maxlength' => 255, + '#required' => FALSE, + ); + // Check if form should be autosubmitted. + $form['search_autocomplete_how']['view_all_results']['all_results_value'] = array( + '#type' => 'textfield', + '#title' => t('Custom "view all results" message value'), + '#description' => t('If a label is filled above, this is the value that will be picked when the message is selected. Leave empty if you don\'t when the message to be selectable. You can use the token [search-phrase] to replace user input.'), + '#default_value' => $all_results->value, + '#maxlength' => 255, + '#required' => FALSE, + ); + // Check if form should be autosubmitted. + $form['search_autocomplete_how']['view_all_results']['all_results_link'] = array( + '#type' => 'textfield', + '#title' => t('Custom "view all results" URL redirection'), + '#description' => t('If "Auto redirect" is checked and a label is given for this configuration, the user will be redirected to this URL when the message is selected. Leave empty if you rather like a standard Drupal search to be performed on the "value" given above.'), + '#default_value' => $all_results->link, + '#maxlength' => 255, + '#required' => FALSE, + ); + + // ### + $no_results = json_decode($item->no_results); + // "No resuls" custom configurations + $form['search_autocomplete_how']['no_results'] = array( + '#type' => 'fieldset', + '#title' => t('Custom behavior when no suggestions are found'), + '#collapsible' => TRUE, + '#collapsed' => TRUE, + ); + // Check if form should be autosubmitted. + $form['search_autocomplete_how']['no_results']['no_results_label'] = array( + '#type' => 'textfield', + '#title' => t('Custom "no result" message label'), + '#description' => t('This message is going to be displayed when no suggestions can be found. Leave empty to disable this functionality. You can use HTML tags as well as the token [search-phrase] to replace user input.'), + '#default_value' => $no_results->label, + '#maxlength' => 255, + '#required' => FALSE, + ); + // Check if form should be autosubmitted. + $form['search_autocomplete_how']['no_results']['no_results_value'] = array( + '#type' => 'textfield', + '#title' => t('Custom "no result" message value'), + '#description' => t('If a label is filled above, this is the value that will be picked when the message is selected. Leave empty if you don\'t when the message to be selectable. You can use the token [search-phrase] to replace user input.'), + '#default_value' => $no_results->value, + '#maxlength' => 255, + '#required' => FALSE, + ); + // Check if form should be autosubmitted. + $form['search_autocomplete_how']['no_results']['no_results_link'] = array( + '#type' => 'textfield', + '#title' => t('Custom "no result" URL redirection'), + '#description' => t('If "Auto redirect" is checked and a label is given for this configuration, the user will be redirected to this URL when the message is selected. Leave empty if you rather like a standard Drupal search to be performed on the "value" given above.'), + '#default_value' => $no_results->link, + '#maxlength' => 255, + '#required' => FALSE, + ); + // ------------------------------------------------------------------. // WHAT - What to display in Search Autocomplete suggestions. $form['search_autocomplete_what'] = array( @@ -246,6 +312,23 @@ function search_autocomplete_form_configure_submit($form, &$form_state) { $callback = 'search_autocomplete/autocomplete/' . $values['fid'] . '/'; } + $all_results = array( + 'label' => $values['all_results_label'], + 'value' => $values['all_results_value'], + 'link' => $values['all_results_link'], + 'group' => array( + 'group_id' => 'all_results', + ), + ); + $no_results = array( + 'label' => $values['no_results_label'], + 'value' => $values['no_results_value'], + 'link' => $values['no_results_link'], + 'group' => array( + 'group_id' => 'no_results', + ), + ); + // ###. // UPDATE THE FORM. // -> update form. @@ -255,7 +338,8 @@ function search_autocomplete_form_configure_submit($form, &$form_state) { 'max_sug' => $values['max_sug'], 'auto_submit' => $values['auto_submit'], 'auto_redirect' => $values['auto_redirect'], - 'no_results' => $values['no_results'], + 'all_results' => json_encode($all_results), + 'no_results' => json_encode($no_results), 'selector' => $values['selector'], 'data_source' => $values['suggestions'], 'data_callback' => $callback, @@ -312,4 +396,4 @@ function _search_autocomplete_get_all_children($fid, &$items = array(), $depth = _search_autocomplete_get_all_children($item->fid, $items, $depth); } return $items; -} +} \ No newline at end of file diff --git a/search_autocomplete.install b/search_autocomplete.install index 49025fa3adcf578e3b34a44f5bf12ff4b2a76df0..b51e0fa961c16b6610a57e049b43558fea14a79e 100644 --- a/search_autocomplete.install +++ b/search_autocomplete.install @@ -73,11 +73,14 @@ function search_autocomplete_schema() { 'default' => 10, ), 'no_results' => array( - 'description' => 'Maximum number of suggestions', - 'type' => 'varchar', - 'length' => 50, - 'not null' => FALSE, - 'default' => '-- No results found for [search-phrase] --', + 'description' => 'Display field when no suggestion available.', + 'type' => 'text', + 'size' => 'big', + ), + 'all_results' => array( + 'description' => 'Display field when some suggestion are available.', + 'type' => 'text', + 'size' => 'big', ), 'auto_submit' => array( 'description' => 'Define if form should be autosubmitted when suggestion is choosen', @@ -147,7 +150,7 @@ function search_autocomplete_install() { // ---------------- // Declare insertion statement. $insert = db_insert('search_autocomplete_forms') - ->fields(array('title', 'selector', 'weight', 'enabled', 'min_char', 'max_sug', 'auto_submit', 'auto_redirect', 'data_source', 'data_callback', 'data_static', 'data_view', 'theme')); + ->fields(array('title', 'selector', 'weight', 'enabled', 'min_char', 'max_sug', 'no_results', 'all_results', 'auto_submit', 'auto_redirect', 'data_source', 'data_callback', 'data_static', 'data_view', 'theme')); $insert->values(array( 'title' => st('Search page - Node Tab') . " (search/node/%)", 'selector' => '#search-form[action="/search/node"] #edit-keys', @@ -155,6 +158,22 @@ function search_autocomplete_install() { 'enabled' => $enabled, 'min_char' => $trigger, 'max_sug' => $limit, + 'no_results' => json_encode(array( + 'label' => 'No results found for [search-phrase].
    Click to perform full search.', + 'value' => '[search-phrase]', + 'link' => '', + 'group' => array( + 'group_id' => 'no_results', + ), + )), + 'all_results' => json_encode(array( + 'label' => 'View all results for [search-phrase].', + 'value' => '[search-phrase]', + 'link' => '', + 'group' => array( + 'group_id' => 'all_results', + ), + )), 'translite' => 1, 'auto_submit' => 1, 'auto_redirect' => 1, @@ -165,12 +184,28 @@ function search_autocomplete_install() { 'theme' => 'basic-green.css', )); $insert->values(array( - 'title' => st('Search page - User Tab') . " (search/user/%)", - 'selector' => '#search-form[action="/search/user"] #edit-keys', - 'weight' => 1, - 'enabled' => $enabled, - 'min_char' => $trigger, - 'max_sug' => $limit, + 'title' => st('Search page - User Tab') . " (search/user/%)", + 'selector' => '#search-form[action="/search/user"] #edit-keys', + 'weight' => 1, + 'enabled' => $enabled, + 'min_char' => $trigger, + 'max_sug' => $limit, + 'no_results' => json_encode(array( + 'label' => 'No results found for [search-phrase].
    Click to perform full search.', + 'value' => '[search-phrase]', + 'link' => '', + 'group' => array( + 'group_id' => 'no_results', + ), + )), + 'all_results' => json_encode(array( + 'label' => 'View all results for [search-phrase].', + 'value' => '[search-phrase]', + 'link' => '', + 'group' => array( + 'group_id' => 'all_results', + ), + )), 'translite' => 1, 'auto_submit' => 1, 'auto_redirect' => 1, @@ -181,12 +216,28 @@ function search_autocomplete_install() { 'theme' => 'user-blue.css', )); $insert->values(array( - 'title' => st('Search Block'), - 'selector' => "#edit-search-block-form--2", - 'weight' => 0, - 'enabled' => $enabled, - 'min_char' => $trigger, - 'max_sug' => $limit, + 'title' => st('Search Block'), + 'selector' => "#edit-search-block-form--2", + 'weight' => 0, + 'enabled' => $enabled, + 'min_char' => $trigger, + 'max_sug' => $limit, + 'no_results' => json_encode(array( + 'label' => 'No results found for [search-phrase].
    Click to perform full search.', + 'value' => '[search-phrase]', + 'link' => '', + 'group' => array( + 'group_id' => 'no_results', + ), + )), + 'all_results' => json_encode(array( + 'label' => 'View all results for [search-phrase].', + 'value' => '[search-phrase]', + 'link' => '', + 'group' => array( + 'group_id' => 'all_results', + ), + )), 'translite' => 1, 'auto_submit' => 1, 'auto_redirect' => 1, @@ -335,12 +386,53 @@ function search_autocomplete_update_7408(&$sandbox) { * Migrate no_results field from 4.0 to 4.1 default value */ function search_autocomplete_update_7410(&$sandbox) { - if (db_field_exists('search_autocomplete_forms', 'no_results')) { db_update('search_autocomplete_forms') // Table name no longer needs {} ->fields(array('no_results' => '-- No results found for [search-phrase] --')) ->execute(); } - return (t('Update no_results field default value has been done.')); +} + +/** + * Migrate no_results and all_resuts fields from 4.0 to 4.1 default value + */ +function search_autocomplete_update_7411(&$sandbox) { + if (!db_field_exists('search_autocomplete_forms', 'no_results')) { + $field = array( + 'description' => 'Display field when no suggestion available.', + 'type' => 'text', + 'size' => 'big', + ); + db_add_field('search_autocomplete_forms', 'no_results', $field); + } + db_update('search_autocomplete_forms') // Table name no longer needs {} + ->fields(array('no_results' => json_encode(array( + 'label' => 'No results found for [search-phrase].
    Click to perform full search.', + 'value' => '[search-phrase]', + 'link' => '', + 'group' => array( + 'group_id' => 'no_results', + ), + )))) + ->execute(); + + if (!db_field_exists('search_autocomplete_forms', 'all_results')) { + $field = array( + 'description' => 'Display field when some suggestion are available.', + 'type' => 'text', + 'size' => 'big', + ); + db_add_field('search_autocomplete_forms', 'all_results', $field); + } + db_update('search_autocomplete_forms') // Table name no longer needs {} + ->fields(array('all_results' => json_encode(array( + 'label' => 'View all results for [search-phrase].', + 'value' => '[search-phrase]', + 'link' => '', + 'group' => array( + 'group_id' => 'all_results', + ), + )))) + ->execute(); } \ No newline at end of file diff --git a/search_autocomplete.module b/search_autocomplete.module index a9a521cfd718c0dcb938c72180443c0e0e646540..0a77c71b073f49ccf26831eaa8ac305901d83d03 100644 --- a/search_autocomplete.module +++ b/search_autocomplete.module @@ -76,6 +76,7 @@ function search_autocomplete_init() { 'minChars' => $match->min_char, 'max_sug' => $match->max_sug, 'no_results' => $match->no_results, + 'all_results' => $match->all_results, 'type' => $data_source, 'datas' => $match->data_source == 'static' ? $data_static : $data_callback, 'fid' => $match->fid,