tablesorter/dist/js/widgets/widget-filter-formatter-select2.min.js
2015-10-31 10:20:22 -05:00

34 lines
3.1 KiB
JavaScript

/*! Widget: filter, select2 formatter function - updated 3/26/2015 (v2.21.3) */
/*
* requires: jQuery 1.7.2+, tableSorter (FORK) 2.16+, filter widget 2.16+ and select2 v3.4.6+ plugin
*/
/*jshint browser:true, jquery:true, unused:false */
/*global jQuery: false */
!function(a){"use strict";var b=a.tablesorter||{};b.filterFormatter=b.filterFormatter||{},/************************\
Select2 Filter Formatter
\************************/
b.filterFormatter.select2=function(c,d,e){var f,g,h=a.extend({
// select2 filter formatter options
cellText:"",// Text (wrapped in a label element)
match:!0,// adds 'filter-match' to header
value:"",
// include ANY select2 options below
multiple:!0,width:"100%"},e),i=c.closest("table")[0].config,j=i.widgetOptions,
// Add a hidden input to hold the range values
k=a('<input class="filter" type="hidden">').appendTo(c).bind("change"+i.namespace+"filter",function(){var a=this.value;a=a.replace(/[\/()$^]/g,"").split("|"),c.find(".select2").select2("val",a),q()}),l=i.$headerIndexed[d],m=l.hasClass(j.filter_onlyAvail),n=[],o=h.match?"":"^",p=h.match?"":"$",
// this function updates the hidden input and adds the current values to the header cell text
q=function(){var b=!1,d=c.find(".select2").select2("val")||h.value||"";
// convert array to string
a.isArray(d)&&(b=!0,d=d.join("\x00")),d=d.replace(/[-[\]{}()*+?.,\/\\^$|#\s]/g,"\\$&"),b&&(d=d.split("\x00")),k.val(a.isArray(d)&&d.length&&""!==d.join("")?"/("+o+(d||[]).join(p+"|"+o)+p+")/":"").trigger("search").end().find(".select2").select2("val",d),n.length&&n.find(".select2").select2("val",d)},
// get options from table cell content or filter_selectSource (v2.16)
r=function(){g=[],f=b.filter.getOptionSource(i.$table[0],d,m)||[],a.each(f,function(a,b){g.push({id:b,text:b})}),h.data=g};
// get filter-match class from option
// don't add default in table options if either ajax or
// data options are already defined
// add a select2 hidden input!
// update select2 from filter hidden input, in case of saved filters
// has sticky headers?
// on reset
return l.toggleClass("filter-match",h.match),h.cellText&&c.prepend("<label>"+h.cellText+"</label>"),h.ajax&&!a.isEmptyObject(h.ajax)||h.data||(r(),m&&i.$table.bind("filterEnd",function(){r(),c.add(n).find(".select2").select2(h)})),a('<input class="select2 select2-'+d+'" type="hidden" />').val(h.value).appendTo(c).select2(h).bind("change",function(){q()}),i.$table.bind("filterFomatterUpdate",function(){
// value = '/(^x$|^y$)/' => 'x,y'
var a=i.$table.data("lastSearch")[d]||"";a=a.replace(/^\/\(\^?/,"").replace(/\$\|\^/g,"|").replace(/\$?\)\/$/g,"").split("|"),c.find(".select2").select2("val",a),q(),b.filter.formatterUpdated(c,d)}),i.$table.bind("stickyHeadersInit",function(){n=i.widgetOptions.$sticky.find(".tablesorter-filter-row").children().eq(d).empty(),a('<input class="select2 select2-'+d+'" type="hidden">').val(h.value).appendTo(n).select2(h).bind("change",function(){c.find(".select2").select2("val",n.find(".select2").select2("val")),q()}),h.cellText&&n.prepend("<label>"+h.cellText+"</label>")}),i.$table.bind("filterReset",function(){c.find(".select2").select2("val",h.value||""),setTimeout(function(){q()},0)}),q(),k}}(jQuery);