materiobaseajax module

first ajax geed results
+ 2 different custom autocomplete func (not used for now)
	- deselect
	- searchapi

Signed-off-by: bachy <git@g-u-i.net>
This commit is contained in:
bachy 2012-07-23 23:02:09 +02:00
parent ad7809415c
commit fb30fe966e
7 changed files with 280 additions and 9 deletions

5
materiobaseajax-ck.js Normal file
View File

@ -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<n;t++){for(var r=0,i=e.length;r<i;r++)if(e[r]==this[t])continue e;e[e.length]=this[t]}return e};Array.prototype.shuffle=function(){var e=this.length,t,n;if(e==0)return;while(--e){t=Math.floor(Math.random()*(e+1));n=this[e];this[e]=this[t];this[t]=n}};Array.prototype.contains=function(e){var t=this.length;while(t--)if(this[t]===e)return!0;return!1};Array.prototype.remove=function(){var e,t=arguments,n=t.length,r;while(n&&this.length){e=t[--n];while((r=this.indexOf(e))!=-1)this.splice(r,1)}return this};Array.prototype.indexOf||(Array.prototype.indexOf=function(e,t){t=t||0;var n=this.length;while(t<n){if(this[t]===e)return t;++t}return-1});(function(e){function t(){}e("#search-api-page-search-form-grid").bind("submit",function(t){trace("search submited",t);var n=e(this).find('input[name*="keys"]').val(),r=e(this).find('input[name="id"]').val();trace("keys",n);trace("form_id",r);e.getJSON("/materiobaseajax/search/"+r+"/"+n,function(t){trace("json",t);t.return&&e("#content").html(t.return)});return!1})})(jQuery);

29
materiobaseajax.info Normal file
View File

@ -0,0 +1,29 @@
name = Materiobaseajax
description = "The description of this module"
; Core version (required)
core = 7.x
; Package name (see http://drupal.org/node/542202 for a list of names)
package = Materio
; PHP version requirement (optional)
; php = 5.2
; Loadable code files
files[] = materiobaseajax.module
; files[] = materiobaseajax.admin.inc
; files[] = materiobaseajax.class.inc
; Module dependencies
; dependencies[] = mymodule
; dependencies[] = theirmodule (1.2)
; dependencies[] = anothermodule (>=2.4)
; dependencies[] = views (3.x)
; Configuration page
; configure = admin/config/materiobaseajax
; For further information about configuration options, see
; - http://drupal.org/node/542202

37
materiobaseajax.js Normal file
View File

@ -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);

74
materiobaseajax.module Normal file
View File

@ -0,0 +1,74 @@
<?php
/**
* @file
* This is the file description for Materiobaseajax module.
*
* In this more verbose, multi-line description, you can specify what this
* file does exactly. Make sure to wrap your documentation in column 78 so
* that the file can be displayed nicely in default-sized consoles.
*/
/**
* Implements hook_init().
*/
function materiobaseajax_init() {
drupal_add_js(drupal_get_path('module', 'materiobaseajax').'/materiobaseajax-ck.js');
}
/**
* Implements hook_menu().
*/
function materiobaseajax_menu() {
$items = array();
$items['materiobaseajax/search'] = array(
'title' => '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;
}

22
materiobaseajax.pages.inc Normal file
View File

@ -0,0 +1,22 @@
<?php
function materiobaseajax_search_ajax($id, $keys){
$page = search_api_page_load($id);
$return = menu_execute_active_handler($page->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);
}

View File

@ -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){
}

View File

@ -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);
}