Skip to content

Instantly share code, notes, and snippets.

@lukewhyte
Created May 14, 2016 00:29
Show Gist options
  • Select an option

  • Save lukewhyte/85c3dad0aafcf0c51e9ba362e1e6f82d to your computer and use it in GitHub Desktop.

Select an option

Save lukewhyte/85c3dad0aafcf0c51e9ba362e1e6f82d to your computer and use it in GitHub Desktop.
Handles retrieval, pagination and filtering of a list of data sources returned from an API
/**
* used to populate the source List... (so we can add sources to reports)
*/
$scope.filterSources = {
filterVal: '',
privatePublic: 'all',
resetPagination: false,
defaultConfigObj: {
offset: 0,
limit: 100,
clientOnly: false,
categoryString: "",
tagString: "",
queryString: '',
privateOnly: false
},
filter: function (value, pageNum) {
var that = this,
def = $q.defer(), // to be resolved in this.service
val = value || '';
$scope.currentSourcePage = pageNum || 1;
$scope.sourceLimit = 25;
var config = angular.extend({}, this.defaultConfigObj, { // set the config obj to send to API
offset: ($scope.currentSourcePage - 1) * $scope.sourceLimit,
limit: $scope.sourceLimit,
privateOnly: this.privatePublic === 'private',
queryString: val
});
this.service(config, def); // make API call
def.promise.then(function (data) { // after successful call, apply additional filters
$scope.sourceList = that.privatePublic === 'all' ? data.payload.sourceDtoList :
_.filter(data.payload.sourceDtoList, function (source) {
if (that.privatePublic === 'private') {
return source.privateSource;
} else {
return !source.privateSource;
}
});
$scope.totalSourceItems = data.payload.totalSourceCount; // used by pagination
if (that.resetPagination) { // if the list has been refreshed, reset the pagination
$scope.sourcePagination = false;
that.resetPagination = !that.resetPagination;
$timeout(function () { // conflict with the angular pagination plugin requires droping this to the bottom of the event chain
$scope.sourcePagination = $scope.totalSourceItems > $scope.sourceLimit;
}, 0);
} else {
$scope.sourcePagination = $scope.totalSourceItems > $scope.sourceLimit;
}
});
},
service: function (config, def) {
return sourceService.list(config, def.resolve, function (data) {
console.log("Got Failed with Status : " + data.status);
});
},
togglePrivPub: function () {
this.resetPagination = true;
this.get(this.filterVal);
},
getDelayed: function (value, pageNum) {
var that = this;
this.filterVal = value || '';
clearTimeout(this.filterSourcesTimeout);
this.filterSourcesTimeout = $timeout(function () { // a timeout that waits until user has stopped typing to make API request
that.resetPagination = true;
that.filter(value, pageNum);
}, 1000);
},
get: function (value, pageNum) {
this.filterVal = value || '';
this.filter(value, pageNum);
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment