jQuery(document).ready(function($) {
var defaults={
name:false,
facets:{},
value:{},
change:$.noop,
placeholder:'Search...',
sort:{}
};
var searchbox='
';
var facet='';
var init=0;
var doinit=function() {
$('.facetsearch .facets .facet').livequery(function() {
var r=$('');
$(this).prepend(r);
r.click(function() {
var p=$(this).parent().parent();
$(this).parent().remove();
p.change();
});
});
}
$.fn.autoGrowInput = function(o) {
o = $.extend({
maxWidth: 300,
minWidth: 20,
comfortZone: 20
}, o);
this.filter('input:text,select').each(function(){
var minWidth = o.minWidth || $(this).width(),
val = '',
input = $(this),
testSubject = $('').css({
position: 'absolute',
top: -9999,
left: -9999,
width: 'auto',
fontSize: input.css('fontSize'),
fontFamily: input.css('fontFamily'),
fontWeight: input.css('fontWeight'),
letterSpacing: input.css('letterSpacing'),
whiteSpace: 'nowrap'
}),
check = function(event) {
var v=input.val();
if (v=='') {
var ph=$(this).attr('placeholder');
if (ph)
v=ph;
}
if (input[0].tagName=='SELECT') {
var s=input[0].selectedIndex;
if (s==-1)
s=0;
v=$(input[0].options[s]).text();
}
if (!event || event.type!='visible') {
if (val === (val = v)) {return;}
}
// Enter new content into testSubject
var escaped = val.replace(/&/g, '&').replace(/\s/g,' ').replace(//g, '>');
testSubject.html(escaped);
// Calculate new width + whether to change
var testerWidth = testSubject.width();
if (input[0].tagName=='SELECT')
testerWidth+=5;
var newWidth = (testerWidth + o.comfortZone) >= minWidth ? testerWidth + o.comfortZone : minWidth;
var currentWidth = input.width();
var isValidWidthChange = (newWidth < currentWidth && newWidth >= minWidth) || (newWidth > minWidth && newWidth < o.maxWidth);
// Animate width
if (isValidWidthChange) {
input.width(newWidth);
}
};
testSubject.insertAfter(input);
$(this).bind('keyup keydown blur update autocompletechange autocompleteselect change visible', check);
check.call(this);
});
};
$('.facetsearch input:text:visible,.facetsearch select:visible').livequery(function() {
$(this).trigger('visible');
});
var autogrow=function(i) {
i.autoGrowInput({
maxWidth:1000,
minWidth:20,
comfortZone:20
});
};
$.widget( "custom.facetsearch_autocomplete", $.ui.autocomplete, {
_create: function() {
this._super();
this.widget().menu( "option", "items", "> :not(.ui-autocomplete-category)" );
},
_renderMenu: function( ul, items ) {
var that = this,
currentCategory = "";
$.each( items, function( index, item ) {
var li;
if (item.category && item.category != currentCategory ) {
ul.append( "" + item.category + "" );
currentCategory = item.category;
}
li = that._renderItemData( ul, item );
if ( item.category ) {
li.attr( "aria-label", item.category + " : " + item.label );
}
});
}
});
$.fn.facetsearch = function(options) {
$(this).each(function() {
var inited=false;
var e=$(searchbox);
if (typeof(value)=='undefined') {
var t=$(this).data('value');
if (typeof(t)=='object') {
value=t;
}
}
options = $.extend(true, {}, defaults, options);
if (!init) {
doinit();
init=1;
}
if (!options.name)
options.name=$(this).attr('name');
var id=options.name;
if (options.id)
id=options.id;
else if ($(this).attr('id'))
id=$(this).attr('id');
if (id)
e.attr('id','facet_'+id);
options.id=id;
if (ph=$(this).attr('placeholder'))
options.placeholder=ph;
if (typeof(options.hook)!='undefined') {
$(window).trigger('facetsearch_'+options.hook,[options]);
}
var olddata=$.extend(true, {},$(this).data());
$(this).after(e);
$(this).hide().val('');
var setval=function(value) {
inited=false;
ff.html('');
if (value) {
if (typeof(value)!='object')
value={text:value};
for (k in value) {
if (!value.hasOwnProperty(k))
continue;
if (k=='text') {
ft.val(value[k]);
ft.trigger('updated');
} else {
for (kk in value[k]) {
if (value[k].hasOwnProperty(kk) && options.facets && options.facets[kk])
addfacet(kk,value[k][kk]);
}
ft.trigger('updated');
}
}
if (sortable) {
if (value.sort) {
fss.val(value.sort);
fss.show();
fs.find('.facet_click_sort').hide();
autogrow(fss);
}
if (typeof(value.sort_dir)!='undefined') {
if (value.sort_dir==0 || value.sort_dir.toLowerCase()=='asc') {
fsd.val('asc');
fsa.removeClass('desc').addClass('asc');
} else{
fsd.val('desc');
fsa.removeClass('asc').addClass('desc');
}
}
}
}
inited=true;
}
$(this).addClass('facetsearch_input');
$(this).attr('disabled','disabled');
$(this).data('facet_setval',setval);//('facet_setval',setval);
var cs=e.find('.clearsearch');
var sortable=typeof(options.sort)=='object' && (typeof(olddata)!='object' || !olddata['nosort']);
if (sortable) {
var fs=$('SORT
');
var fss=fs.find('select:eq(0)');
fss.hide();
fs.click(function() {
return false;
});
fs.find('.facet_click_sort').click(function() {
fss.show()
fss[0].focus();
$(this).hide();
autogrow(fss);
});
for (k in options.sort) {
var o=$('');
o.attr('value',k);
o.text(options.sort[k]);
fss.append(o);
}
fss.attr('name',options.name+'[sort]');
var fsd=fs.find('input:eq(0)');
fsd.attr('name',options.name+'[sort_dir]');
var fsa=fs.find('.facet_sort_dir');
fsd.val('desc');
fsa.removeClass('asc').addClass('desc');
fsa.click(function() {
if ($(this).hasClass('noascdesc'))
return;
if ($(this).hasClass('asc')) {
$(this).removeClass('asc').addClass('desc');
fsd.val('desc');
} else {
$(this).removeClass('desc').addClass('asc');
fsd.val('asc');
}
return false;
});
cs.after(fs);
}
var fo=e.find('.facet_options');
var ff=e.find('.facets');
cs.click(function() {
ff.html('');
e.find('.freetext').val('');
});
//fo.hide();
var ft=e.find('.freetext')
.attr('placeholder',options.placeholder)
.focus(function() {
//fo.slideDown();
})
.blur(function() {
//fo.slideUp();
})
.attr('name',options.name+'[text]');
var searchcount=0;
autogrow(ft);
e.click(function() {
e.find('.freetext').focus();
return false;
});
for (k in options.facets) {
if (!options.facets.hasOwnProperty(k))
continue;
var d=$('');
var n=(options.facets[k].name) ? options.facets[k].name : k;
d.text(n);
d.data('key',k);
fo.append(d);
};
fo.click(function(event) {
var t=$(event.target);
if (t.hasClass('facet_options'))
return false;
addfacet(t.data('key'));
return false;
});
var addfacet=function(key,val) {
var tf=options.facets[key];
var f=$(facet);
var n=(tf.name) ? tf.name : key;
f.find('.label').text(n);
ff.append(f);
f[0].focus();
var name=options.name+'['+searchcount+']['+key+']';
searchcount++;
f.addClass('facet_'+key);
var setval=function(input,actual) {
if (typeof(actual)=='undefined') {
if (i.next().hasClass('hidden')) {
i.next().remove();
i.attr('name',i.data('name'));
}
} else {
var n=i.attr('name');
var hi=$('');
hi.attr('name',n);
i.after(hi);
hi.val(actual);
i.data('name',n)
i.removeAttr('name');
}
};
if (typeof(tf.input)=='function') {
var r=tf.input.call(f,name,val,tf); // custom input
if (r===false) {
f.remove();
return false;
}
} else {
if (tf.type && tf.type=='select')
var i=$('');
else {
var i=$('');
var type=(typeof(tf.type)=='undefined' || !tf.type) ? 'text' : tf.type;
if (type=='date' && $.fn.datePick)
type='dateinput';
i.attr('type',type);
}
if (typeof(tf.data)=='object') {
for (var tfdatak in tf.data) {
if (tf.data.hasOwnProperty(tfdatak))
i.data(tfdatak,tf.data[tfdatak]);
}
}
i.attr('name',name);
f.find('.input').append(i);
if (type=='range') {
var rd=$('');
i.after(rd);
i.parent().width(120).css({display:'inline-block',padding:'0 10px'});
i.attr('type','hidden');
var mmin=(typeof(tf.min)=='undefined') ? 0 : tf.min;
var mmax=(typeof(tf.max)=='undefined') ? 100 : tf.max;
rd.slider({
range:true,
min:mmin,
max:mmax,
values: [mmin,mmax],
slide: function( event, ui ) {
i.val(ui.values[0]+","+ui.values[1]);
rd.attr('title',ui.values[0]+" to "+ui.values[1]);
}
});
i.val(mmin+','+mmax);
}
var dooptions=false;
var opts=[];
if (tf.options) {
var o=tf.options;
if (typeof(tf.options)=='function')
o=tf.options.call();
for (k in o) {
if (o.hasOwnProperty(k)) {
if (typeof(o[k])=='object') {
var pp={};
if (o[k].label) {
pp.label=o[k].label;
if (typeof(o[k].key)!='undefined')
pp.key=o[k].key;
else
pp.key=k;
opts.push(pp);
} else {
continue;
}
} else {
opts.push({label:o[k],key:k});
}
}
}
dooptions=true;
}
if (tf.other) {
for (k in tf.other) {
if (!tf.other.hasOwnProperty(k))
continue;
for (kk in tf.other[k]) {
opts.push({label:tf.other[k][kk],key:kk});
}
}
dooptions=true;
}
if (tf.autocomplete) {
opts=tf.autocomplete;
dooptions=true;
}
if (dooptions) {
if (tf.type && tf.type=='select' && typeof(opts)=='object') {
for (k in opts) {
if (opts.hasOwnProperty(k)) {
var o=$('');
o.text(opts[k].label);
if (typeof(opts[k].key)!='undefined')
o.attr('value',opts[k].key);
i.append(o);
}
}
} else {
var closing=false;
i.autocomplete({
delay:(typeof(opts)=='object') ? 0 : 500,
source:opts,
close: function() {
closing = true;
setTimeout(function() { closing = false; }, 300);
},
change:function(event, ui) {
// FIXME: causes problem, loosing info
//if (!tf.text && ui.item==null)
// i.val('').focus();
},
select:function(event, ui) {
if (typeof(ui.item.key)!='undefined' && (typeof(tf.text)=='undefined' || !tf.text))
setval(i,ui.item.key);
else
setval(i);
i.val(ui.item.label);
i.trigger('update');
return false;
}
}).focus(function() {
//if (!closing)
// $(this).autocomplete("search","");
}).click(function() {
//$(this).autocomplete("search",i.val());
}).keyup(function() {
setval(i); // remove hidden input
});
}
}
var callback=function(val) {
if (typeof(val)=='object') {
i.val(val.label);
if (typeof(val.key)!='undefined')
setval(i,val.key);
else
setval(i);
} else {
i.val(val);
setval(i);
}
i.trigger('update');
};
if (typeof(tf.value)=='function') {
var oldval=val;
i.hide();
tf.value.call(i,val,function(v) {
i.show();
if (typeof(v)=='object')
callback(v);
else {
if (oldval!=v)
callback({label:v,key:oldval});
}
});
}
if (val) {
if (val) {
if (type=='range') {
if (i.next().hasClass('ui-slider')) {
var v=val.split(',');
var v=[v[0],(typeof(v[1])=='undefined' ? v[0] : v[1])];
i.next().slider("values",v);
i.next().attr('title',v[0]+' to '+v[1]);
}
}
i.val(val);
}
switch (type) {
case 'daterange':
case 'datemultiple':
case 'dateinput':
i.addClass('closed');
break;
}
} else if (typeof(val)=='undefined') {
switch (type) {
case 'date':
var d=new Date();
i.val(d.getFullYear()+'-'+(('0'+(d.getMonth()+1)).substr(-2,2))+'-'+(('0'+d.getDate()).substr(-2,2)));
break;
}
}
autogrow(i);
}
f.click(function() {
$(this).find('input,select,textarea').filter(':visible').eq(0).focus();
return false;
});
if (inited)
f.find('input,select,textarea').filter(':visible').eq(0).focus();
}
setval(options.value);
inited=true;
});
}
var oldval=$.fn.val;
$.fn.val=function() {
if ($(this).hasClass('facetsearch_input')) {
if (arguments.length==1) {
var set=arguments[0];
$(this).each(function() {
var d=$(this).data('facet_setval');
d(set);
});
} else {
}
return;
}
return oldval.apply(this,arguments);
};
});