|
| 1 | +/** |
| 2 | + * Ushahidi Angular Collection Selector directive |
| 3 | + * Drop in directive for managing collections addition for posts |
| 4 | + */ |
| 5 | + |
| 6 | +module.exports = [ |
| 7 | +function ( |
| 8 | +) { |
| 9 | + var controller = [ |
| 10 | + '$scope', |
| 11 | + '$rootScope', |
| 12 | + '$translate', |
| 13 | + 'Notify', |
| 14 | + 'CollectionEndpoint', |
| 15 | + '_', |
| 16 | + function ( |
| 17 | + $scope, |
| 18 | + $rootScope, |
| 19 | + $translate, |
| 20 | + Notify, |
| 21 | + CollectionEndpoint, |
| 22 | + _ |
| 23 | + ) { |
| 24 | + $scope.showNewCollectionInput = false; |
| 25 | + $scope.newCollection = ''; |
| 26 | + $scope.editableCollections = []; |
| 27 | + |
| 28 | + $scope.refreshCollections = function () { |
| 29 | + CollectionEndpoint.editableByMe().$promise.then(function (results) { |
| 30 | + $scope.editableCollections = results; |
| 31 | + }); |
| 32 | + }; |
| 33 | + |
| 34 | + $scope.refreshCollections(); |
| 35 | + |
| 36 | + $scope.$on('event:collection-selector:update', function () { |
| 37 | + $scope.refrehCollections(); |
| 38 | + }); |
| 39 | + |
| 40 | + $scope.postInCollection = function (collection) { |
| 41 | + return _.contains($scope.post.sets, String(collection.id)); |
| 42 | + }; |
| 43 | + |
| 44 | + $scope.toggleCreateCollection = function () { |
| 45 | + $scope.showNewCollectionInput = !$scope.showNewCollectionInput; |
| 46 | + }; |
| 47 | + |
| 48 | + $scope.toggleCollection = function (selectedCollection) { |
| 49 | + if (_.contains($scope.post.sets, String(selectedCollection.id))) { |
| 50 | + $scope.removeFromCollection(selectedCollection); |
| 51 | + } else { |
| 52 | + $scope.addToCollection(selectedCollection); |
| 53 | + } |
| 54 | + }; |
| 55 | + |
| 56 | + $scope.addToCollection = function (selectedCollection) { |
| 57 | + var collectionId = selectedCollection.id, collection = selectedCollection.name; |
| 58 | + |
| 59 | + CollectionEndpoint.addPost({'collectionId': collectionId, 'id': $scope.post.id}) |
| 60 | + .$promise.then(function () { |
| 61 | + $translate('notify.collection.add_to_collection', {collection: collection}) |
| 62 | + .then(function (message) { |
| 63 | + $scope.post.sets.push(String(collectionId)); |
| 64 | + Notify.showNotificationSlider(message); |
| 65 | + }); |
| 66 | + }, function (errorResponse) { |
| 67 | + Notify.showApiErrors(errorResponse); |
| 68 | + }); |
| 69 | + }; |
| 70 | + |
| 71 | + $scope.removeFromCollection = function (selectedCollection) { |
| 72 | + var collectionId = selectedCollection.id, collection = selectedCollection.name; |
| 73 | + |
| 74 | + CollectionEndpoint.removePost({'collectionId': collectionId, 'id': $scope.post.id}) |
| 75 | + .$promise |
| 76 | + .then(function () { |
| 77 | + $translate('notify.collection.removed_from_collection', {collection: collection}) |
| 78 | + .then(function (message) { |
| 79 | + $scope.post.sets = _.without($scope.post.sets, String(collectionId)); |
| 80 | + Notify.showNotificationSlider(message); |
| 81 | + }); |
| 82 | + }, function (errorResponse) { |
| 83 | + Notify.showApiErrors(errorResponse); |
| 84 | + }); |
| 85 | + }; |
| 86 | + /* |
| 87 | + scope.searchCollections = function (query) { |
| 88 | + CollectionEndpoint.query(query) |
| 89 | + .$promise |
| 90 | + .then(function (result) { |
| 91 | + }, function (errorResponse) { |
| 92 | + Notify.showApiErrors(errorResponse); |
| 93 | + }); |
| 94 | + }; |
| 95 | +
|
| 96 | + scope.clearSearch = function() { |
| 97 | + scope.editableCollection = scope.editableCollectionCopy; |
| 98 | + }; |
| 99 | + */ |
| 100 | + $scope.createNewCollection = function (collectionName) { |
| 101 | + var collection = { |
| 102 | + 'name': collectionName, |
| 103 | + 'user_id': $rootScope.currentUser.userId |
| 104 | + }; |
| 105 | + CollectionEndpoint.save(collection) |
| 106 | + .$promise |
| 107 | + .then(function (collection) { |
| 108 | + $scope.toggleCreateCollection(); |
| 109 | + $scope.newCollection = ''; |
| 110 | + $scope.addToCollection(collection); |
| 111 | + |
| 112 | + // Where collection selectors can appear multiple times |
| 113 | + // it is necessary to force a collection refresh when an update occurs |
| 114 | + // on anyone collection selector - to ensure that newly created collections |
| 115 | + // are available in all lists on the page. |
| 116 | + // TODO: add caching for collections to reduce requests. |
| 117 | + $rootScope.$broadcast('event:collection:update'); |
| 118 | + }, function (errorResponse) { |
| 119 | + Notify.showApiErrors(errorResponse); |
| 120 | + }); |
| 121 | + }; |
| 122 | + |
| 123 | + }]; |
| 124 | + return { |
| 125 | + restrict: 'E', |
| 126 | + templateUrl: 'templates/collection-selector/collection-selector.html', |
| 127 | + scope: { |
| 128 | + post: '=' |
| 129 | + }, |
| 130 | + controller: controller |
| 131 | + }; |
| 132 | +}]; |
0 commit comments