function CheckIconFor(n){return n===2?"indeterminate_check_box":n?"check_box":"check_box_outline_blank"}function SmallExpandIconFor(n){return n?"arrow_drop_up":"arrow_drop_down"}function LargeExpandIconFor(n){return n?"keyboard_arrow_up":"keyboard_arrow_down"}function getQueryVar(n){for(var i,u=window.location.search.substring(1),r=u.split("&"),t=0;t<r.length;t++)if(i=r[t].split("="),i[0]==n)return decodeURIComponent(i[1].replace(/\+/g," "));return rememberQueryString(),undefined}function setQueryVar(n,t){var s=window.location.search.substring(1),i=s.split("&"),u,f,o,e,r;for(i[0]==""&&(i=[]),u=!1,r=0;r<i.length;r++)f=i[r].split("="),f[0]==n&&(u=!0,i[r]=f[0]+"="+encodeURIComponent(t));for(u||i.push(n+"="+encodeURIComponent(t)),o=location.protocol+"//"+location.host+location.pathname,e=o+(i[0]?"?"+i[0]:""),r=1;r<i.length;r++)e+="&"+i[r];window.history.replaceState({},document.title,e);rememberQueryString()}function clearQueryString(){var n=location.protocol+"//"+location.host+location.pathname;window.history.replaceState({},document.title,n)}function rememberQueryString(){sessionStorage.queryString=window.location.search}function getOldQueryString(){var n=sessionStorage.queryString;return n||""}function forgetQueryString(){sessionStorage.queryString=undefined}function BaseFilter(n){n.expanded=ko.observable(!1);n.toggleExpand=function(){n.expanded(!n.expanded())};n.expandIcon=ko.pureComputed(function(){return LargeExpandIconFor(n.expanded())})}function CategoryFilterViewModel(n){var t=this,r,i;BaseFilter(t);t.categoryTags=ko.pureComputed(function(){var n=[];return t.categoryGroups.forEach(function(t){t.categories.forEach(function(t){t.checked()&&n.push(t.name)})}),n});t.categoryGroups=n.map(function(n){var t={name:n.Name,expanded:ko.observable(!1),categories:n.Categories.map(function(n){var t={name:n,checked:ko.observable(!1)};return t.toggleCheck=function(){t.checked(!t.checked())},t.checkIcon=ko.pureComputed(function(){return CheckIconFor(t.checked())}),t})};return t.checked=ko.pureComputed(function(){var n=!1,i=!1;return(t.categories.forEach(function(t){t.checked()?n=!0:i=!0}),n&&i)?2:n?!0:!1}),t.toggleCheck=function(){var n=t.checked();n||t.expanded(!0);t.categories.forEach(function(t){t.checked(!n)})},t.toggleExpand=function(){t.expanded(!t.expanded())},t.checkIcon=ko.pureComputed(function(){return CheckIconFor(t.checked())}),t.expandIcon=ko.pureComputed(function(){return SmallExpandIconFor(t.expanded())}),t});r=(getQueryVar("categories")||"").split(",");i=!1;t.categoryGroups.forEach(function(n){n.categories.forEach(function(n){r.includes(n.name)&&n.checked(!0)});n.checked()&&(n.expanded(!0),i=!0)});i&&t.expanded(!0);t.categoryTags.subscribe(function(n){setQueryVar("categories",n)});t.dirty=ko.pureComputed(function(){return t.categoryGroups.some(function(n){return n.checked()})})}function CommissioningAgencyFilterViewModel(){var n=this;BaseFilter(n);n.dirty=ko.observable(!1)}function FiltersViewModel(n,t){var i=this,r,u;t=t||{};r=getQueryVar("q");i.isAdmin=ko.observable(!1);i.search=ko.observable(r);i.search.subscribe(function(n){setQueryVar("q",n)});i.defaultStatuses=[];i.defaultLocation=undefined;i.defaultCategories=[];i.defaultTypesOfStudy=[];i.status=ko.observable(new StatusFilterViewModel(i.defaultStatuses,t.status));i.location=ko.observable(new LocationFilterViewModel(i.defaultLocation,t.location));i.categories=ko.observable(new CategoryFilterViewModel(i.defaultCategories,t.categories));i.typesOfStudy=ko.observable(new TypesOfStudyFilterViewModel(i.defaultTypesOfStudy,t.typesOfStudy));i.clearFilters=function(n){n!==!0&&clearQueryString();i.status(new StatusFilterViewModel(i.defaultStatuses));i.location(new LocationFilterViewModel(i.defaultLocation));i.categories(new CategoryFilterViewModel(i.defaultCategories));i.typesOfStudy(new TypesOfStudyFilterViewModel(i.defaultTypesOfStudy))};i.dirty=ko.pureComputed(function(){return i.status().dirty()||i.location().dirty()||i.categories().dirty()||i.typesOfStudy().dirty()});i.clean=ko.pureComputed(function(){return!i.dirty()});u=$("#userKey").val();$.getJSON("/api/filterdata?k="+u).done(function(n){i.defaultStatuses=n.Statuses;i.defaultCategories=n.CategoryGroups;i.defaultTypesOfStudy=n.TypesOfStudy;i.defaultLocation=n.Locations;i.isAdmin(n.IsAdmin);i.clearFilters(!0)})}function LocationFilterViewModel(n){var t=this,u,f,i,r;BaseFilter(t);t.locations=n;t.selectedTier1=ko.observable();t.selectedTier2=ko.observable();t.tier1HasValue=ko.pureComputed(function(){return t.selectedTier1()&&t.selectedTier1().Name!==""});t.tier2HasValue=ko.pureComputed(function(){return t.selectedTier2()&&t.tier1HasValue()&&t.selectedTier2().Name!==""});u=getQueryVar("tier1");f=getQueryVar("tier2");n!==undefined&&(i=n.find(function(n){return n.Name==u}),t.selectedTier1(i),i!==undefined&&(t.expanded(!0),r=i.Children.find(function(n){return n.Name==f}),t.selectedTier2(r),r!==undefined&&t.expanded(!0)));t.selectedTier1.subscribe(function(n){setQueryVar("tier1",(n||{}).Name||"")});t.selectedTier2.subscribe(function(n){setQueryVar("tier2",(n||{}).Name||"")});t.dirty=ko.pureComputed(function(){return t.tier1HasValue()})}function SearchPageViewModel(){var n=this,t;n.datasets=ko.observableArray([]);n.filter=ko.observable(new FiltersViewModel);n.loaded=ko.observable(!1);n.sortFunctions=[{Name:"Alphabetical",SortFunction:function(n,t){return n.DatabaseName&&t.DatabaseName?n.DatabaseName.localeCompare(t.DatabaseName):0}},{Name:"Order created",SortFunction:function(n,t){return n&&t?n.Id-t.Id:0}},{Name:"Date last updated",SortFunction:function(n,t){return n&&t?t.LastUpdatedTicks-n.LastUpdatedTicks:0}}];n.selectedSortFunction=ko.observable(n.sortFunctions[2].SortFunction);n.internalFilteredSortedData=ko.pureComputed(function(){var i=n.filter().search()||"",e=i.split(" "),o=i==="",r=n.filter().categories().categoryTags(),u=n.filter().typesOfStudy().types().filter(function(n){return n.checked()}).map(function(n){return n.id}),t=n.filter().location(),f=n.filter().status();return n.datasets().filter(function(n){var c=o||e.every(function(t){return!t||n.DatabaseName&&n.DatabaseName.toUpperCase().indexOf(t.toUpperCase())>-1||n.Summary&&n.Summary.toUpperCase().indexOf(t.toUpperCase())>-1||n.Keywords&&n.Keywords.some(function(n){return n.toUpperCase().indexOf(t.toUpperCase())>-1})}),i,s,h;return c?f.selectedStatus()&&f.selectedStatus().Name!==n.Status?!1:(i=t.tier1HasValue(),s=t.tier2HasValue(),(i||s)&&n.Locations.every(function(n){return i&&n.Tier1Id!==t.selectedTier1().Id||s&&n.Tier2Id!==t.selectedTier2().Id}))?!1:r.length>0&&(h=n.Categories.split("|"),!h.some(function(n){return r.indexOf(n)>-1}))?!1:u.length>0&&!n.TypesOfStudy.some(function(n){return u.indexOf(n.Id)>-1})?!1:!0:!1}).sort(n.selectedSortFunction())});n.showMore=ko.observable(!1);n.moreAvailable=ko.pureComputed(function(){return!n.showMore()&&n.count()>50});n.doShowMore=function(){n.showMore(!0)};n.internalFilteredSortedData.subscribe(function(){n.showMore(!1)});n.filteredData=ko.pureComputed(function(){var t;return t=n.showMore()?n.internalFilteredSortedData():n.internalFilteredSortedData().slice(0,50),t.forEach(function(n){var t=n.LastUpdated?moment.utc(n.LastUpdated).local().format("DD/MM/YYYY, h:mm a"):"Date not available";n.LastUpdatedText=t}),t});n.count=ko.pureComputed(function(){return n.internalFilteredSortedData().length});t=$("#userKey").val();$.getJSON("/api/datasets?k="+t).done(function(t){n.datasets(t);n.loaded(!0);setTimeout(function(){$("#search").focus()},2)})}function StatusFilterViewModel(n){var t=this,i,r;BaseFilter(t);t.statuses=n;t.selectedStatus=ko.observable();i=getQueryVar("status");r=n.find(function(n){return n.Name==i});t.selectedStatus(r);i&&t.expanded(!0);t.selectedStatus.subscribe(function(n){setQueryVar("status",(n||{}).Name||"")});t.dirty=ko.pureComputed(function(){return t.selectedStatus()!==undefined})}function TypesOfStudyFilterViewModel(n){var t=this,i;BaseFilter(t);t.types=ko.observableArray(n.map(function(n){var t={id:n.Id,name:n.Name,checked:ko.observable(!1)};return t.toggleCheck=function(){t.checked(!t.checked())},t.checkIcon=ko.pureComputed(function(){return CheckIconFor(t.checked())}),t}));t.typeTags=ko.pureComputed(function(){var n=[];return t.types().forEach(function(t){t.checked()&&n.push(t.name)}),n});i=(getQueryVar("types")||"").split(",");t.types().forEach(function(n){i.includes(n.name)&&(n.checked(!0),t.expanded(!0))});t.typeTags.subscribe(function(n){setQueryVar("types",n)});t.dirty=ko.pureComputed(function(){return t.types().some(function(n){return n.checked()})})}Array.prototype.find||(Array.prototype.find=function(n){"use strict";var t;if(this==null)throw new TypeError("Array.prototype.find called on null or undefined");if(typeof n!="function")throw new TypeError("predicate must be a function");var i=Object(this),u=i.length>>>0,f=arguments[1],r;for(t=0;t<u;t++)if(r=i[t],n.call(f,r,t,i))return r;return undefined});String.prototype.includes||(String.prototype.includes=function(n,t){"use strict";return typeof t!="number"&&(t=0),t+n.length>this.length?!1:this.indexOf(n,t)!==-1});Array.prototype.includes||(Array.prototype.includes=function(n){"use strict";var f,i,r,t,u;if(this==null)throw new TypeError("Array.prototype.includes called on null or undefined");if(f=Object(this),i=parseInt(f.length,10)||0,i===0)return!1;for(r=parseInt(arguments[1],10)||0,r>=0?t=r:(t=i+r,t<0&&(t=0));t<i;){if(u=f[t],n===u||n!==n&&u!==u)return!0;t++}return!1});ko.options.deferUpdates=!0;ko.applyBindings(new SearchPageViewModel)