From fb30fe966ef3e134cf30a6c047a575c4ac52ee92 Mon Sep 17 00:00:00 2001 From: bachy Date: Mon, 23 Jul 2012 23:02:09 +0200 Subject: [PATCH] materiobaseajax module first ajax geed results + 2 different custom autocomplete func (not used for now) - deselect - searchapi Signed-off-by: bachy --- materiobaseajax-ck.js | 5 +++ materiobaseajax.info | 29 +++++++++++++++ materiobaseajax.js | 37 ++++++++++++++++++++ materiobaseajax.module | 74 +++++++++++++++++++++++++++++++++++++++ materiobaseajax.pages.inc | 22 ++++++++++++ materiobasemod.module | 74 ++++++++++++++++++++++++++++++++++----- materiobasemod.pages.inc | 48 ++++++++++++++++++++++++- 7 files changed, 280 insertions(+), 9 deletions(-) create mode 100644 materiobaseajax-ck.js create mode 100644 materiobaseajax.info create mode 100644 materiobaseajax.js create mode 100644 materiobaseajax.module create mode 100644 materiobaseajax.pages.inc diff --git a/materiobaseajax-ck.js b/materiobaseajax-ck.js new file mode 100644 index 00000000..5214dd84 --- /dev/null +++ b/materiobaseajax-ck.js @@ -0,0 +1,5 @@ +// * date 2011/10/19 * +/** +* misc +*/// ------ TRACE ------ +function trace(e,t){window.console&&window.console.debug&&(arguments.length===2?window.console.debug(e,t):window.console.debug(e))}function Timeout(e,t){var n=setTimeout(e,t);this.cleared=!1;this.clear=function(){this.cleared=!0;clearTimeout(n)}}function HexToRGB(e){if(e.charAt(0)=="#"){var t=e.substring(1,7),n=parseInt(t.substring(0,2),16),r=parseInt(t.substring(2,4),16),i=parseInt(t.substring(4,6),16);return"rgb("+n+", "+r+", "+i+")"}return null}function pair(e){return typeof e=="number"?e%2==0?!0:!1:null}function random(e,t){return arguments.length===1&&typeof e=="number"?Math.random()*e:arguments.length===2&&typeof e==typeof t&&typeof e=="number"?e+Math.random()*(t-e):NaN}function round(e,t){if(arguments.length==1)e=Math.round(e);else{t=t.toString();t=t.replace(/./gi,0);t=t.replace(/^./gi,1);t=parseInt(t);e=Math.round(e*t)/t}return e}function floor(e,t){if(arguments.length==1)e=Math.floor(e);else{t=t.toString();t=t.replace(/./gi,0);t=t.replace(/^./gi,1);t=parseInt(t);e=Math.floor(e*t)/t}return e}function ceil(e,t){if(arguments.length==1)e=Math.ceil(e);else{t=t.toString();t=t.replace(/./gi,0);t=t.replace(/^./gi,1);t=parseInt(t);e=Math.ceil(e*t)/t}return e}function ArraySubtract(e,t){var n=new Array;for(var r=e.length-1;r>=0;r--)t.contains(e[r])||n.push(e[r]);return n}function objectSize(e){var t=e.length?--e.length:0;for(var n in e)t++;return t}function objectGetKey(e,t){var n=0;for(var r in e){if(r==="length"||!e.hasOwnProperty(r))continue;if(n==t)return r;n++}}function objectSort(e,t){var n=new Array;for(var r=objectSize(e)-1;r>=0;r--){var i=objectGetKey(e,r);for(var s=objectSize(e[i].wit)-1;s>=0;s--){var o=objectGetKey(e[i].wit,s);if(o==t){var u=parseFloat(e[i].wit[o]);n.push({wit:u,nid:i})}}}n.sort(function(e,t){return t.wit-e.wit});var a={};for(var r=n.length-1;r>=0;r--)a[n[r].nid]=e[n[r].nid];return a}function getElementAngle(e,t,n,r){var i=n-e,s=r-t,o=Math.abs(Math.atan(s/i)*180/Math.PI);i>0&&s<0?o=90-o:i>=0&&s>=0?o+=90:i<0&&s>=0?o=180+(90-o):o+=270;return o}String.prototype.trim=function(){return this.replace(/(^\s*)|(\s*$)/gi,"").replace(/[ ]{2,}/gi," ").replace(/\n /,"\n")};Array.prototype.unique=function(){var e=new Array;e:for(var t=0,n=this.length;t=2.4) +; dependencies[] = views (3.x) + +; Configuration page +; configure = admin/config/materiobaseajax + + +; For further information about configuration options, see +; - http://drupal.org/node/542202 diff --git a/materiobaseajax.js b/materiobaseajax.js new file mode 100644 index 00000000..3e1f7208 --- /dev/null +++ b/materiobaseajax.js @@ -0,0 +1,37 @@ +// @codekit-prepend "gui.js" +(function($) { + + $('#search-api-page-search-form-grid').bind('submit', function(event) { + trace('search submited', event); + var keys = $(this).find('input[name*="keys"]').val(), + id = $(this).find('input[name="id"]').val(); + trace('keys', keys); + trace('form_id', id); + + $.getJSON('/materiobaseajax/search/'+id+'/'+keys, + // {form_id: "theform_id", keys: "thekeys"}, + function(json){ + trace('json', json); + if(json.return){ + $('#content').html(json.return); + } + }); + + return false; + }); + + // select event need a patch http://drupal.org/node/365241#comment-5374686 + + // $("input.form-text", '#search-api-page-search-form-grid').bind('autocomplete_select', function(event) { + // trace('val', $(this).val()); + // var $form = $(this).parent('form'); + // // setTimeout(function(){ + // // $form.submit(); + // // }, 10); + // }); + + function navigate(){ + + }; + +})(jQuery); \ No newline at end of file diff --git a/materiobaseajax.module b/materiobaseajax.module new file mode 100644 index 00000000..33f1e9ca --- /dev/null +++ b/materiobaseajax.module @@ -0,0 +1,74 @@ + 'Matrio base ajax', + 'page callback' => 'materiobaseajax_search_ajax', + 'page arguments' => array(2, 3), + // 'access callback' => TRUE, + 'access arguments' => array('access search_api_page'), + 'file' => 'materiobaseajax.pages.inc', + 'type' => MENU_CALLBACK, + ); + + return $items; +} + + + +/** + * Implements hook_search_api_ajax_settings(). + */ +function materiobaseajax_search_api_ajax_settings() { + $settings = array( + + // CSS id for main content (search results html) + // format: content => CSS id + 'content' => '#content', + + // CSS id's for regions containing search blocks + // check your region names in mytheme.info + // format: region_name => CSS id + 'regions' => array( + 'headerblock_left' => '#headerblock-left', + // 'sidebar_second' => '#sidebar-second', + ), + + // OPTIONAL: if you want to provide an AJAX spinner + // this paht is for a default spinner path provided with this module + // @note: see the search_api_ajax.css + 'spinner' => drupal_get_path('module', 'search_api_ajax') .'/spinner.gif', + + // OPTIONAL: if you want to use scroll-to-top functionality when paging + // scroll target div + // 'scrolltarget' => '#main-content', + // 'scrollspeed' => 1000, + + // OPTIONAL: if you want to fade search results when Ajaxing + // please set to 1 for TRUE + 'fade' => 1, + 'opacity' => 0.3, + ); + + return $settings; +} diff --git a/materiobaseajax.pages.inc b/materiobaseajax.pages.inc new file mode 100644 index 00000000..eb6a2f9a --- /dev/null +++ b/materiobaseajax.pages.inc @@ -0,0 +1,22 @@ +path . '/' . $keys, FALSE); + + if (is_array($return)) { + $return = drupal_render($return); + } + + $rep = array( + 'id'=>$id, + 'keys'=>$keys, + 'page'=>$page, + 'return'=>$return, + ); + + return drupal_json_output($rep); +} \ No newline at end of file diff --git a/materiobasemod.module b/materiobasemod.module index de3a398b..eed10ac6 100644 --- a/materiobasemod.module +++ b/materiobasemod.module @@ -26,13 +26,21 @@ function materiobasemod_permission() { */ function materiobasemod_menu() { $items = array(); - $items['materiobase/search/autocomplete'] = array( + $items['materiobase/search/autocomplete/dbselect'] = array( 'title' => 'Autocomplete materiobase search', - 'page callback' => 'materiobase_search_autocomplete', + 'page callback' => 'materiobase_search_autocomplete_dbselect', 'access arguments' => array('access content'), 'type' => MENU_CALLBACK, 'file' => 'materiobasemod.pages.inc', ); + $items['materiobase/search/autocomplete/searchapi'] = array( + 'title' => 'Autocomplete materiobase search', + 'page callback' => 'materiobase_search_autocomplete_searchapi', + 'access arguments' => array('access content'), + 'type' => MENU_CALLBACK, + 'file' => 'materiobasemod.pages.inc', + ); + return $items; } @@ -81,28 +89,79 @@ function materiobasemod_block_view($delta = '') { return $block; } +/** + * Implements hook_block_view_alter(). + */ +function materiobasemod_block_view_alter(&$data, $block) { + if ($block->module == 'search_api_page') { + $page = search_api_page_load($block->delta); + $item = menu_get_item(); + + if (isset($page->path) && $page->path == $item['path']) { + $keys = arg(count(arg(NULL, $page->path))); + if ($keys) { + $data['content']['keys_' . $page->id]['#default_value'] = $keys; + $data['content']['keys_' . $page->id]['#value'] = $keys; + } + } + } +} + +/** + * Implements hook_entity_info_alter(). + */ +function materiobasemod_entity_info_alter(&$entity_info) { + $entity_info['node']['view modes']['cardsmall'] = array( + 'label' => t('Small card for the grid'), + 'custom settings' => TRUE, + ); + + $entity_info['node']['view modes']['cardmedium'] = array( + 'label' => t('Medium card for the grid'), + 'custom settings' => TRUE, + ); + + $entity_info['node']['view modes']['cardbig'] = array( + 'label' => t('Big card for the grid'), + 'custom settings' => TRUE, + ); +} + +/** +* Implements hook_preprocess_node(). +*/ +function materiobasemod_preprocess_node(&$vars) { + $vars['theme_hook_suggestions'][] = 'node__'.$vars['view_mode']; + $vars['theme_hook_suggestions'][] = 'node__' . $vars['type'] . '__' . $vars['view_mode']; + // dsm($vars, '$vars'); +} + +function materiobasemod_preprocess_field(&$vars) { + $vars['theme_hook_suggestions'][] = 'field__' . $vars['element']['#view_mode']; + $vars['theme_hook_suggestions'][] = 'field__' . $vars['element']['#field_type'] . '__' . $vars['element']['#view_mode']; + $vars['theme_hook_suggestions'][] = 'field__' . $vars['element']['#field_name'] . '__' . $vars['element']['#view_mode']; + // dsm($vars, '$vars'); +} + /** * materiobase_search_form() */ function materiobase_search_form(){ - $form = array(); - $form['searchfield'] = array( '#type' => 'textfield', '#default_value' => '', - '#autocomplete_path' => 'materiobase/search/autocomplete', + // '#autocomplete_path' => 'materiobase/search/autocomplete/searchapi', + '#autocomplete_path' => 'materiobase/search/autocomplete/dbselect', '#size' => 30, '#maxlength' => 1024, // '#element_validate' => array('taxonomy_autocomplete_validate'), ); - $form['create'] = array( '#type' => 'submit', '#value' => t('Search'), ); - return $form; } @@ -115,4 +174,3 @@ function template_preprocess_materiobase_search_block(&$vars){ } - diff --git a/materiobasemod.pages.inc b/materiobasemod.pages.inc index b03105bc..54eccddf 100644 --- a/materiobasemod.pages.inc +++ b/materiobasemod.pages.inc @@ -6,7 +6,7 @@ * inspired by taxonomy_autocomplete() * */ -function materiobase_search_autocomplete($typed = ''){ +function materiobase_search_autocomplete_dbselect($typed = ''){ // If the request has a '/' in the search text, then the menu system will have // split it into multiple arguments, recover the intended $tags_typed. $args = func_get_args(); @@ -51,4 +51,50 @@ function materiobase_search_autocomplete($typed = ''){ drupal_json_output($term_matches); +} + +function materiobase_search_autocomplete_searchapi($typed = ''){ + // If the request has a '/' in the search text, then the menu system will have + // split it into multiple arguments, recover the intended $tags_typed. + $args = func_get_args(); + $typed = implode('/', $args); + + /* + TODO riche serach engine + \\ etc gmail like + */ + + if ($typed != '') { + + + $query = search_api_query('referencement'); + + $query_filter = $query->createFilter(); + $query_filter->condition('name', $typed); + // $query_filter->condition('type', 'article'); + + $query->filter($query_filter); + + $tags_return = $query->execute(); + + dsm($tags_return, '$tags_return'); + + $term_matches = array(); + $index = search_api_index_load('referencement'); + foreach ($index->loadItems(array_keys($tags_return['results'])) as $item) { + dsm($item, '$item'); + } + + // + // foreach ($tags_return as $tid => $name) { + // $n = $name; + // // Term names containing commas or quotes must be wrapped in quotes. + // // if (strpos($name, ',') !== FALSE || strpos($name, '"') !== FALSE) { + // // $n = '"' . str_replace('"', '""', $name) . '"'; + // // } + // $term_matches[$n] = check_plain($name); + // } + } + + drupal_json_output($term_matches); + } \ No newline at end of file