Skip to content

Commit

Permalink
fixed select all
Browse files Browse the repository at this point in the history
  • Loading branch information
willdoran committed Nov 13, 2015
1 parent 5b0c359 commit 31acc02
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 37 deletions.
6 changes: 5 additions & 1 deletion app/post/directives/post-preview-directive.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ function (
scope: {
post: '=',
canSelect: '=',
editableCollections: '='
editableCollections: '=',
selectedItems: '='
},
templateUrl: 'templates/posts/preview.html',
link: function (scope) {
Expand All @@ -70,6 +71,9 @@ function (
scope.getRoleDisplayName = RoleHelper.getRole;

scope.editableByMeCopy = [];
scope.updateSelectedItems = function () {
$rootScope.$broadcast('event:post:selection', scope.post);
};

// Ensure completes stages array is numeric
scope.post.completed_stages = scope.post.completed_stages.map(function (stageId) {
Expand Down
41 changes: 11 additions & 30 deletions app/post/directives/views/post-view-list-directive.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,20 +51,24 @@ function (
$scope.editableCollections = CollectionEndpoint.editableByMe();
};

$scope.$on('event:post:selection', function (event, post) {
(post.selected ? $scope.selectedItems++ : $scope.selectedItems--);
(post.selected ? $scope.selectedPosts.push(post) : $scope.selectedPosts = _.without($scope.selectedPosts, _.findWhere($scope.selectedPosts, {id: post.id})));
});

$scope.$on('event:collection:update', function () {
refreshCollections();
});

refreshCollections();

$scope.deleteSelectedPosts = function () {

$translate('notify.post.destroy_confirm').then(function (message) {
Notify.showConfirm(message).then(function () {
// ask server to delete selected posts
// and refetch posts from server
var deletePostsPromises = _.map(
$scope.selectedItems,
$scope.selectedPosts,
function (post) {
return PostEndpoint.delete({ id: post.id }).$promise;
});
Expand All @@ -74,32 +78,6 @@ function (
});
};

$scope.addSelectedPostsToCollection = function (selectedCollection) {
if ($scope.selectedItems.length === 0) {
return;
}
var collectionId = selectedCollection.id,
collection = selectedCollection.name,
// Add each post to the collection and return a promise
promises = _.map($scope.selectedItems, function (post) {
return CollectionEndpoint.addPost({'collectionId': collectionId, 'id': post.id}).$promise;
});

// Show a single notification when all selected posts have been added to the collection
$q.all(promises).then(function () {
$translate('notify.collection.bulk_add_to_collection', {
count: $scope.selectedItems.length,
collection: collection
}).then(function (message) {
Notify.showSingleAlert(message);
});
// Deselect posts
_.forEach($scope.selectedItems, function (post) {
post.selected = false;
});
}, handleResponseErrors);
};

$scope.itemsPerPageChanged = function (count) {
$scope.itemsPerPage = count;
getPostsForPagination();
Expand All @@ -115,18 +93,20 @@ function (
$event && $event.preventDefault();
_.forEach($scope.posts, function (post) {
post.selected = false;
$scope.selectedItems--;
});
};

$scope.selectAllPosts = function ($event) {
$event && $event.preventDefault();
_.forEach($scope.posts, function (post) {
post.selected = true;
$scope.selectedItems++;
});
};

$scope.allSelectedOnCurrentPage = function ($event) {
return $scope.selectedItems.length === $scope.posts.length;
return $scope.selectedItems === $scope.posts.length;
};

$scope.hasFilters = function () {
Expand All @@ -139,7 +119,8 @@ function (
// --- start: initialization
$scope.pageChanged = getPostsForPagination;
$scope.currentPage = 1;
$scope.selectedItems = [];
$scope.selectedItems = 0;
$scope.selectedPosts = [];
$scope.itemsPerPageOptions = [10, 20, 50];
$scope.itemsPerPage = $scope.itemsPerPageOptions[0];

Expand Down
2 changes: 1 addition & 1 deletion server/www/templates/posts/preview.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<div class="single-post-listing">
<div class="select-post" ng-if="canSelect">
<input type="checkbox" value="{post.id}">
<input type="checkbox" value="{post.id}" ng-change="updateSelectedItems()" ng-model="post.selected">
</div>

<div class="post-summary !has-img">
Expand Down
6 changes: 1 addition & 5 deletions server/www/templates/views/list.html
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,7 @@
ng-repeat="post in posts"
can-select="userHasBulkActionPermissions()"
editable-collections="editableCollections"
selection-model
selection-model-type="checkbox"
selection-model-mode="multiple-additive"
selection-model-selected-items="selectedItems"
selection-model-cleanup-strategy="deselect"
selected-items="selectedItems"
post="post"
>
</post-preview>
Expand Down

0 comments on commit 31acc02

Please sign in to comment.