Skip to content

Commit

Permalink
fixed publish select - code repetition should be divided into widgets
Browse files Browse the repository at this point in the history
  • Loading branch information
willdoran committed Nov 13, 2015
1 parent 31acc02 commit 16ed880
Show file tree
Hide file tree
Showing 7 changed files with 161 additions and 86 deletions.
1 change: 1 addition & 0 deletions app/common/locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -522,6 +522,7 @@
"save_success" : "Saved <a href\"/post/{{id}}\">name</a> post",
"save_error" : "Unable to save post, please try again",
"publish_success" : "Post has been published",
"set_draft" : "Post has been set as draft",
"publish_error" : "Unable to publish post, please try again",
"unpublish_success" : "Post has been unpublished",
"unpublish_error" : "Unable to unpublish post, please try again",
Expand Down
43 changes: 25 additions & 18 deletions app/post/controllers/post-detail-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -198,21 +198,6 @@ function (
Notify.showApiErrors(errorResponse);
});
};
/*
$scope.setPublishedFor = function () {
PostEndpoint.update(post)
.$promise
.then(function () {
$translate('notify.post.visible_to', {visible_to: visible_to})
.then(function (message) {
$scope.post.sets.push(String(collectionId));
Notify.showNotificationSlider(message);
});
}, function (errorResponse) {
Notify.showApiErrors(errorResponse);
});
};
*/

$scope.removeFromCollection = function (selectedCollection) {
var collectionId = selectedCollection.id, collection = selectedCollection.name;
Expand Down Expand Up @@ -260,19 +245,40 @@ function (
});
};

$scope.publishRole;
$scope.publishPostTo = function () {
// first check if stages required have been marked complete
var requiredStages = _.where($scope.stages, {required: true}),
errors = [];

_.each(requiredStages, function (stage) {
// if this stage isn't complete, add to errors
if (_.indexOf($scope.post.completed_stages, stage.id) === -1) {
errors.push($filter('translate')('post.modify.incomplete_step', { stage: stage.label }));
}
});

if (errors.length) {
Notify.showAlerts(errors);
return;
}

if ($scope.publishRole) {
$scope.post.published_to = [$scope.publishRole];
if($scope.publishRole === 'draft') {
$scope.post.status = 'draft';
} else {
$scope.post.status = 'published';
$scope.post.published_to = [$scope.publishRole];
}
} else {
$scope.post.status = 'published';
$scope.post.published_to = [];
}

PostEndpoint.update($scope.post).
$promise
.then(function () {
$translate('notify.post.publish_success')
var message = post.status == 'draft' ? 'notify.post.set_draft' : 'notify.post.publish_success';
$translate(message)
.then(function (message) {
Notify.showNotificationSlider(message);
});
Expand All @@ -292,6 +298,7 @@ function (

return '';
};
$scope.publishRole = $scope.postIsPublishedTo();

}];

144 changes: 98 additions & 46 deletions app/post/directives/post-preview-directive.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module.exports = [
'$q',
'$rootScope',
'CollectionEndpoint',
'PostEndpoint',
'TagEndpoint',
'UserEndpoint',
'FormEndpoint',
Expand All @@ -15,6 +16,7 @@ function (
$q,
$rootScope,
CollectionEndpoint,
PostEndpoint,
TagEndpoint,
UserEndpoint,
FormEndpoint,
Expand Down Expand Up @@ -61,127 +63,177 @@ function (
scope: {
post: '=',
canSelect: '=',
editableCollections: '=',
selectedItems: '='
editableCollections: '='
},
templateUrl: 'templates/posts/preview.html',
link: function (scope) {
scope.showNewCollectionInput = false;
scope.newCollection = '';
scope.getRoleDisplayName = RoleHelper.getRole;

scope.editableByMeCopy = [];
scope.updateSelectedItems = function () {
$rootScope.$broadcast('event:post:selection', scope.post);
link: function ($scope) {
$scope.showNewCollectionInput = false;
$scope.newCollection = '';
$scope.getRoleDisplayName = RoleHelper.getRole;
$scope.availableRoles = RoleHelper.roles();

$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) {
$scope.post.completed_stages = $scope.post.completed_stages.map(function (stageId) {
return parseInt(stageId);
});

scope.toggleCreateCollection = function () {
scope.showNewCollectionInput = !scope.showNewCollectionInput
$scope.toggleCreateCollection = function () {
$scope.showNewCollectionInput = !$scope.showNewCollectionInput
};

// Replace tags with full tag object
scope.post.tags = scope.post.tags.map(function (tag) {
$scope.post.tags = $scope.post.tags.map(function (tag) {
return TagEndpoint.get({id: tag.id, ignore403: true});
});

// Replace form with full object
if (scope.post.form) {
FormEndpoint.get({id: scope.post.form.id}, function (form) {
scope.post.form = form;
if ($scope.post.form) {
FormEndpoint.get({id: $scope.post.form.id}, function (form) {
$scope.post.form = form;
});
}

scope.publishedFor = function () {
if (!_.isEmpty(scope.post.published_to)) {
return RoleHelper.getRole(scope.post.published_to[0]);
}

return 'Everyone';
};

// TODO all collection code should be moved into a separate standalone widget
scope.postInCollection = function (collection) {
return _.contains(scope.post.sets, String(collection.id));
$scope.postInCollection = function (collection) {
return _.contains($scope.post.sets, String(collection.id));
};

scope.toggleCollection = function (selectedCollection) {
if (_.contains(scope.post.sets, String(selectedCollection.id))) {
scope.removeFromCollection(selectedCollection);
$scope.toggleCollection = function (selectedCollection) {
if (_.contains($scope.post.sets, String(selectedCollection.id))) {
$scope.removeFromCollection(selectedCollection);
} else {
scope.addToCollection(selectedCollection);
$scope.addToCollection(selectedCollection);
}
};

scope.addToCollection = function (selectedCollection) {
$scope.addToCollection = function (selectedCollection) {
var collectionId = selectedCollection.id, collection = selectedCollection.name;

CollectionEndpoint.addPost({'collectionId': collectionId, 'id': scope.post.id})
CollectionEndpoint.addPost({'collectionId': collectionId, 'id': $scope.post.id})
.$promise.then(function () {
$translate('notify.collection.add_to_collection', {collection: collection})
.then(function (message) {
scope.post.sets.push(String(collectionId));
$scope.post.sets.push(String(collectionId));
Notify.showNotificationSlider(message);
});
}, function (errorResponse) {
Notify.showApiErrors(errorResponse);
});
};

scope.removeFromCollection = function (selectedCollection) {
$scope.removeFromCollection = function (selectedCollection) {
var collectionId = selectedCollection.id, collection = selectedCollection.name;

CollectionEndpoint.removePost({'collectionId': collectionId, 'id': scope.post.id})
CollectionEndpoint.removePost({'collectionId': collectionId, 'id': $scope.post.id})
.$promise
.then(function () {
$translate('notify.collection.removed_from_collection', {collection: collection})
.then(function (message) {
scope.post.sets = _.without(scope.post.sets, String(collectionId));
$scope.post.sets = _.without($scope.post.sets, String(collectionId));
Notify.showNotificationSlider(message);
});
}, function (errorResponse) {
Notify.showApiErrors(errorResponse);
});
};
$scope.publishPostTo = function () {

// first check if stages required have been marked complete
var requiredStages = _.where($scope.stages, {required: true}),
errors = [];

_.each(requiredStages, function (stage) {
// if this stage isn't complete, add to errors
if (_.indexOf($scope.post.completed_stages, stage.id) === -1) {
errors.push($filter('translate')('post.modify.incomplete_step', { stage: stage.label }));
}
});

if (errors.length) {
Notify.showAlerts(errors);
return;
}

if ($scope.publishRole) {
if($scope.publishRole === 'draft') {
$scope.post.status = 'draft';
} else {
$scope.post.status = 'published';
$scope.post.published_to = [$scope.publishRole];
}
} else {
$scope.post.status = 'published';
$scope.post.published_to = [];
}

PostEndpoint.update($scope.post).
$promise
.then(function (post) {
var message = post.status == 'draft' ? 'notify.post.set_draft' : 'notify.post.publish_success';
$translate(message)
.then(function (message) {
Notify.showNotificationSlider(message);
});
}, function (errorResponse) {
Notify.showApiErrors(errorResponse);
});
};

$scope.postIsPublishedTo = function () {
if ($scope.post.status === 'draft') {
return 'draft';
}

if (!_.isEmpty($scope.post.published_to)) {
return $scope.post.published_to[0];
}

return '';
};

$scope.publishRole = $scope.postIsPublishedTo();
/*
scope.searchCollections = function (query) {
$scope.searchCollections = function (query) {
CollectionEndpoint.query(query)
.$promise
.then(function (result) {
$scope.editableCollectionsLocal = results;
}, function (errorResponse) {
Notify.showApiErrors(errorResponse);
});
};
scope.clearSearch = function() {
scope.editableCollection = scope.editableCollectionCopy;
$scope.clearSearch = function() {
$rootScope.$broadcast('event:collection:update');
$scope.editableCollectionsLocal = $scope.editableCollections;
};
*/
scope.createNewCollection = function (collectionName) {
$scope.createNewCollection = function (collectionName) {
var collection = {
'name': collectionName,
'user_id': $rootScope.currentUser.userId
};
CollectionEndpoint.save(collection)
.$promise
.then(function (collection) {
scope.addToCollection(collection);
$scope.addToCollection(collection);
$rootScope.$broadcast('event:collection:update');
scope.newCollection = '';
scope.toggleCreateCollection();
$scope.newCollection = '';
$scope.toggleCreateCollection();
refreshCollections();
}, function (errorReponse) {
Notify.showApiErrors(errorResponse);
});
};

// determine which stage the post is at
getCurrentStage(scope.post).then(function (currentStage) {
scope.currentStage = currentStage;
getCurrentStage($scope.post).then(function (currentStage) {
$scope.currentStage = currentStage;
});
}
};
Expand Down
1 change: 1 addition & 0 deletions app/post/directives/views/post-view-list-directive.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ function (
});

refreshCollections();

$scope.deleteSelectedPosts = function () {

$translate('notify.post.destroy_confirm').then(function (message) {
Expand Down
20 changes: 9 additions & 11 deletions server/www/templates/posts/detail.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,26 +13,24 @@ <h1 class="post-title">
ng-model="publishRole"
>
<option
ng-selected="postIsPublishedTo() === 'draft'"
value="draft"
translate>
post.publish_for_you
</option>
</option>
<option
ng-repeat="role in availableRoles"
data-icon="fa-user"
value="{{role.name}}"
>
{{role.display_name}}
</option>
<option
ng-selected="postIsPublishedTo() === ''"
data-icon="fa-globe"
value=""
translate>
post.publish_for_everyone
</option>
<option
ng-repeat="role in availableRoles track by role.name"
ng-selected="postIsPublishedTo() === role.name"
data-icon="fa-user"
value="{{role.name}}"
>
{{role.display_name}}
</option>

</select>
</div>
<label><span class="nodisplay">Who</span> <span translate>post.can_see_this_post</span></label>
Expand Down

0 comments on commit 16ed880

Please sign in to comment.