// Funnelback auto-completion jQuery plugin
// Author: Nicolas Guillaumin, Matt Sheppard
// Copyright Funnelback, 2012
// $Id: jquery.funnelback-completion.js 36837 2013-10-04 04:26:42Z nguillaumin $
(function($) {
$.fn.fbcompletion = function(settings) {
var config = {
'collection' : 'funnelback_documentation',
'show' : 10,
'sort' : 0,
'delay' : 0,
'length' : 3,
'alpha' : 0.5,
'program' : 'http://sagov-funnelback01.squiz.net/s/redirect',
'interactionLog' : 'http://sagov-funnelback01.squiz.net/s/log',
'format' : 'simple',
'enabled' : 'disabled',
'logging' : 'enabled',
'dwellLogging' : 'enabled',
'dwellLoggingTimeout' : 3000,
'selectLogging' : 'enabled',
'tmplId' : 'fb-completion-tmpl',
'profile' : '_default',
'zindex' : 1000
};
/*
* This function logs events to the interaction log, and once the logging
* has been completed, executes the given action callback.
*
* loggingEnabled - Whether logging should be performed (action will be called
* regardless of whether logging is enabled)
* mayBeAsync - Can the logging be done asyncronously?
* Must be false if executing action will load a new page (unloading the current one)
* type - The type of event to log
* paramters - Properties to log with the event
* action - Callback to be executed after logging
*/
var logger = function(loggingEnabled, mayBeAsync, type, parameters, action) {
if(loggingEnabled && config.logging === 'enabled'){
var log_url = config.interactionLog
+ '?collection=' + config.collection
+ '&type=' + encodeURIComponent(type)
// Send the local time as Ajax requests might arrive out of order
// on the server side
+ '&client_time=' + (new Date()).getTime()
+ ((config.profile !== '') ? '&profile=' + config.profile : '' );
// Fill in the specific logging parameters
for (var key in parameters) {
log_url += "&" + encodeURIComponent(key) + "=" + encodeURIComponent(parameters[key]);
}
if (mayBeAsync) {
action();
jQuery.ajax({
type: 'GET',
url: log_url,
error: function(xhr, textStatus, errorThrown) {
if (window.console) {
console.log('Interaction log error: ' + textStatus + ', ' + errorThrown);
}
}
});
} else {
jQuery.ajax({
type: 'GET',
url: log_url,
error: function(xhr, textStatus, errorThrown) {
if (window.console) {
console.log('Interaction log error: ' + textStatus + ', ' + errorThrown);
}
action();
},
success: function(data) {
action();
}
});
}
} else {
action();
}
};
if (settings) $.extend(config, settings);
if (config.enabled != 'enabled' ) {
return;
}
this.each(function() {
var targetElement = this;
var dwellTimeoutCallback;
// Compile jQuery template
var compiledTmpl;
if (jQuery().template) {
if (jQuery('#'+config.tmplId).length > 0) {
compiledTmpl = jQuery('#'+config.tmplId).template();
} else {
compiledTmpl = jQuery('').template();
}
}
$(targetElement).autocomplete( {
appendTo: ($("#fb-queryform").length > 0) ? "#fb-queryform" : "body",
source: function (request, response) {
jQuery.ajax({
type: 'GET',
url: config.program
+ '?collection=' + config.collection
+ '&partial_query=' + request.term.replace(/ /g, '+')
+ '&show=' + config.show
+ '&sort=' + config.sort
+ '&alpha=' + config.alpha
+ '&fmt=' + ((config.format == 'simple') ? 'json' : 'json++')
+ ((config.profile !== '') ? '&profile=' + config.profile : '' )
,
dataType: 'jsonp',
error: function(xhr, textStatus, errorThrown) {
if (window.console) {
console.log('Autocomplete error: ' + textStatus + ', ' + errorThrown);
}
},
success: function(data) {
var responses = new Array();
for (var i=0; i