Filter LVWP App 2010
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function(module,$) { | |
"use strict"; | |
window.pd = window.pd || {}; | |
pd.MeetingsViewModel = function() { | |
/* observable state */ | |
this.FilterTextIm = ko.observable(''); | |
this.FilterText = ko.computed(this.FilterTextIm) | |
.extend({ throttle: 400 }); | |
this.LibraryTitle = ko.observable(''); | |
this.LibrarySubTitle = ko.observable(''); | |
this.FilterYears = new ko.observableArray([]); | |
this.FilterMonths = new ko.observableArray(['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec']); | |
this.SelectedYear = ko.observable(''); | |
this.SelectedMonth = ko.observable(''); | |
this.Working = ko.observable(true); | |
/* populate years filter as Current Year..-=4 */ | |
for(var cy = parseInt(new Date().getFullYear()), y = cy; y > (cy-4); y--) { this.FilterYears.push(y.toString()); } | |
/* model actions */ | |
this.filterByYear = function(year) { | |
this.FilterTextIm(''); // zap the filter text | |
year = (year === this.SelectedYear()) ? '' : year; | |
this.SelectedYear(year); | |
}.bind(this); | |
this.filterByMonth = function(month) { | |
this.FilterTextIm(''); // zap the filter text | |
month = (month === this.SelectedMonth()) ? '' : month; | |
this.SelectedMonth(month); | |
}.bind(this); | |
/* model behaviour */ | |
this.isSelectedYear = function (year) { | |
var y = this.SelectedYear(); | |
return (y == year); | |
}.bind(this); | |
this.isSelectedMonth = function (month) { | |
var m = this.SelectedMonth(); | |
return (m == month); | |
}.bind(this); | |
} | |
pd.FilterLibraryApp = function() { | |
var | |
viewModel = new pd.MeetingsViewModel(), | |
debug = true, | |
wpCtx = null, | |
wpEvent = null, | |
filterControlId = null, | |
listViewTableId = null, | |
_module = { | |
start: start | |
}; | |
return _module; | |
function locateListView() { | |
var fn = function() { | |
InitAllClvps(); | |
// find the CTX for the LVWP | |
for(var k in g_ctxDict) { | |
if (debug && window.console) console.log(g_ctxDict[k]); | |
if (g_ctxDict[k].ListTitle === module.LibraryTitle) { | |
wpCtx = window['ctx'+g_ctxDict[k].ctxId]; | |
break; | |
} | |
} | |
if (debug && window.console) { | |
console.log(wpCtx.clvp); | |
console.log(wpCtx.clvp.ctx); | |
console.log(wpCtx.clvp.ctx.view); | |
} | |
/* this isn't used in the 2010 version, not figured out if/how to use it yet | |
var clvp = (wpCtx && wpCtx.clvp) ? wpCtx.clvp : null; | |
wpEvent = { clvp: clvp, event: { currentCtx: { clvp: clvp } } }; | |
*/ | |
// find the LVWP and table containing the rows | |
var | |
$wpnode = $('#'+wpCtx.clvp.wpq).first(), | |
$wpTableNode = $wpnode.find("table[id^='onetidDoclibViewTbl']"); | |
listViewTableId = $wpTableNode.attr('id'); | |
// find the postback control id; e.g. ctl00$m$g_109629d4_d78b_4c9e_8ec0_90078b6e444e$ctl02 | |
var | |
$wpinput = $wpnode.find('> :first-child > input:first-child'), | |
ctrlid = $wpinput.attr('id'); | |
ctrlid = ctrlid && ctrlid.length | |
? ctrlid.replace(/^(ctl\d\d)_/gi,'$1$') | |
.replace(/_g_/g,'$g_') | |
.replace(/_(ctl\d\d)$/gi,'$$$1') | |
: ''; | |
if (debug && window.console) { console.log(">>locateListView: controlid="+ctrlid); } | |
filterControlId = ctrlid; | |
viewModel.LibraryTitle(wpCtx.ListTitle); | |
viewModel.LibrarySubTitle('All'); | |
viewModel.Working(false); | |
}; | |
EnsureScript("inplview", typeof InitAllClvps, fn); | |
} | |
function filterByText() { | |
if (!filterControlId) { | |
alert('Unable to filter: the list view webpart could not be located or the list view contains a Person/Group column!'); | |
return; | |
} | |
var filterText = this.FilterText(); | |
if (debug && window.console) { console.log(">>FilterByText: ["+filterText+"]"); } | |
var $wpTableNode = $("#"+listViewTableId); | |
$wpTableNode.find("tr.ms-itmhover").each(function() { | |
var $tr = $(this); | |
if (filterText && filterText.length) { | |
var | |
r = new RegExp(filterText,'gi'), | |
matched = false; | |
$tr.find(".ms-vb-title,.ms-vb2").each(function() { | |
var td = $(this).text(); | |
if(!r.test(td)) return true; | |
matched = true; | |
return false; | |
}); | |
if(matched) $tr.fadeIn(250); | |
else $tr.fadeOut(250); | |
} else { | |
$tr.fadeIn(250); | |
} | |
}); | |
} | |
function onFilterChange(filterType) { | |
if (!filterControlId) { | |
alert('Unable to filter: the list view webpart could not be located or the list view contains a Person/Group column!'); | |
return; | |
} | |
var | |
filterColumn = null, | |
filterValue = ''; | |
if (filterType.match(/year/gi)) { | |
filterValue = this.SelectedYear(); | |
filterColumn = 'catYear'; | |
} else if (filterType.match(/month/gi)) { | |
filterValue = this.SelectedMonth(); | |
filterColumn = 'catMonth'; | |
} | |
if (!filterValue) filterValue = '##dvt_all##'; | |
var filterCall = "__doPostBack('"+filterControlId+"','NotUTF8;__filter={"+filterColumn+"="+filterValue+"}')" | |
if (debug && window.console) { | |
console.log(">>Filter by " + filterType + " on ["+filterColumn+"] = ["+filterValue+"]"); | |
console.log(" >> " + filterCall); | |
} | |
eval(filterCall); | |
} | |
function start() { | |
ko.applyBindings(viewModel, $('#cdtm').get(0)); | |
viewModel.FilterText.subscribe(filterByText, viewModel); | |
viewModel.SelectedYear.subscribe(onFilterChange.bind(viewModel, 'year')); | |
viewModel.SelectedMonth.subscribe(onFilterChange.bind(viewModel, 'month')); | |
locateListView(); | |
} | |
}(); | |
$(pd.FilterLibraryApp.start); | |
})({ Name: 'Module', LibraryTitle: 'CDLT Meetings' },jQuery); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment