Vulcan/client/views/posts/post_edit.js

144 lines
3.9 KiB
JavaScript
Raw Normal View History

Template.post_edit.helpers({
2013-01-13 08:52:35 +09:00
created: function(){
2013-10-06 09:17:37 +09:00
return moment(this.createdAt).format("MMMM Do, h:mm:ss a");
2013-01-13 08:52:35 +09:00
},
2012-10-10 08:32:49 +09:00
categories: function(){
2013-02-20 10:27:45 +09:00
var post = this;
2014-06-22 13:19:47 +09:00
return Categories.find({}, {sort: {order: 1, name: 1}}).map(function(category) {
2013-04-06 13:44:52 +09:00
category.checked = _.contains(_.pluck(post.categories, '_id'), category._id) ? 'checked' : '';
2013-02-20 10:27:45 +09:00
return category;
});
2012-10-18 11:26:42 +09:00
},
categoriesEnabled: function(){
return Categories.find().count();
},
2013-01-13 08:52:35 +09:00
isApproved: function(){
2013-01-13 09:13:25 +09:00
return this.status == STATUS_APPROVED;
2013-01-13 08:52:35 +09:00
},
2013-01-13 09:13:25 +09:00
isSticky: function(){
return this.sticky ? 'checked' : '';
},
2014-05-01 19:58:56 -07:00
isSelected: function(parentPost){
return parentPost && this._id == parentPost.userId ? 'selected' : '';
},
2012-10-18 11:26:42 +09:00
submittedDate: function(){
2012-11-19 11:03:15 +09:00
return moment(this.submitted).format("MM/DD/YYYY");
},
submittedTime: function(){
return moment(this.submitted).format("HH:mm");
2012-10-23 12:24:38 +09:00
},
users: function(){
2014-06-01 11:47:30 +09:00
return Meteor.users.find({}, {sort: {'profile.name': 1}});
2012-10-23 12:24:38 +09:00
},
userName: function(){
return getDisplayName(this);
},
2013-01-13 09:13:25 +09:00
hasStatusPending: function(){
return this.status == STATUS_PENDING ? 'checked' : '';
2012-10-24 11:04:42 +09:00
},
2013-01-13 09:13:25 +09:00
hasStatusApproved: function(){
return this.status == STATUS_APPROVED ? 'checked' : '';
2012-10-24 11:04:42 +09:00
},
2013-01-13 09:13:25 +09:00
hasStatusRejected: function(){
return this.status == STATUS_REJECTED ? 'checked' : '';
2013-07-04 12:05:09 +09:00
},
shorten: function(){
return !!getSetting('bitlyToken');
}
});
Template.post_edit.rendered = function(){
2014-05-19 09:09:39 +09:00
var post = this.data.post;
if(post && !this.editor){
this.editor= new EpicEditor(EpicEditorOptions).load();
2014-05-19 09:09:39 +09:00
this.editor.importFile('editor', post.body);
2012-11-19 11:03:15 +09:00
$('#submitted_date').datepicker();
}
2013-07-19 12:17:49 +09:00
2014-05-01 19:58:56 -07:00
// $("#postUser").selectToAutocomplete(); // XXX
}
2012-09-13 11:05:49 +09:00
Template.post_edit.events({
'click input[type=submit]': function(e, instance){
2013-10-09 12:39:05 +09:00
var post = this;
var categoriesArray = [];
2013-10-09 12:39:05 +09:00
var url = $('#url').val();
var shortUrl = $('#short-url').val();
var status = parseInt($('input[name=status]:checked').val());
e.preventDefault();
if(!Meteor.user()){
throwError('You must be logged in.');
return false;
}
2012-11-26 17:39:23 +09:00
$('input[name=category]:checked').each(function() {
2013-04-06 13:44:52 +09:00
var categoryId = $(this).val();
if(category = Categories.findOne(categoryId)){
categoriesArray.push(category);
}
2012-11-26 17:39:23 +09:00
});
2012-11-26 17:37:47 +09:00
var properties = {
2014-05-16 09:19:35 +09:00
title: $('#title').val(),
shortUrl: shortUrl,
2012-11-26 17:37:47 +09:00
body: instance.editor.exportFile(),
categories: categoriesArray,
2012-11-26 17:37:47 +09:00
};
2013-01-13 09:13:25 +09:00
if(url){
properties.url = (url.substring(0, 7) == "http://" || url.substring(0, 8) == "https://") ? url : "http://"+url;
}
2012-11-26 17:37:47 +09:00
if(isAdmin(Meteor.user())){
2013-01-13 08:52:35 +09:00
if(status == STATUS_APPROVED){
2013-03-04 10:55:46 +09:00
if(!post.submitted){
2013-01-13 09:52:40 +09:00
// this is the first time we are approving the post
properties.submitted = new Date();
}else if($('#submitted_date').exists()){
properties.submitted = moment($('#submitted_date').val()+$('#submitted_time').val(), "MM/DD/YYYY HH:mm").toDate();
2013-01-13 09:52:40 +09:00
}
2013-01-13 08:52:35 +09:00
}
2012-11-26 17:37:47 +09:00
adminProperties = {
2014-06-02 09:12:48 +09:00
sticky: $('#sticky').is(':checked'),
2012-11-26 17:37:47 +09:00
userId: $('#postUser').val(),
2013-01-13 10:21:09 +09:00
status: status,
2012-11-26 17:37:47 +09:00
};
properties = _.extend(properties, adminProperties);
}
2014-06-25 14:33:41 +09:00
// console.log(properties)
2012-11-26 17:37:47 +09:00
2013-10-09 12:39:05 +09:00
Posts.update(post._id,{
$set: properties
}, function(error){
if(error){
2013-07-05 07:09:15 +09:00
console.log(error);
throwError(error.reason);
}else{
2013-10-06 09:17:37 +09:00
trackEvent("edit post", {'postId': post._id});
Router.go("/posts/"+post._id);
}
2013-10-09 12:39:05 +09:00
});
},
'click .delete-link': function(e){
2013-10-09 12:39:05 +09:00
var post = this;
2012-09-07 10:57:57 +09:00
e.preventDefault();
2013-10-09 12:39:05 +09:00
2012-09-07 10:57:57 +09:00
if(confirm("Are you sure?")){
Router.go("/");
2013-10-06 09:17:37 +09:00
Meteor.call("deletePostById", post._id, function(error) {
if (error) {
console.log(error);
throwError(error.reason);
} else {
throwError('Your post has been deleted.');
}
});
2012-09-07 10:57:57 +09:00
}
}
});