Vulcan/client/views/posts/post_edit.js

196 lines
5.1 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();
},
2014-07-03 13:15:23 +09:00
showPostedAt: function () {
if(Session.get('currentPostStatus') == STATUS_APPROVED){
return 'visible'
}else{
return 'hidden'
}
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' : '';
},
2014-07-03 13:15:23 +09:00
postedAtDate: function(){
return !!this.postedAt ? moment(this.postedAt).format("MM/DD/YYYY") : null;
2012-11-19 11:03:15 +09:00
},
2014-07-03 13:15:23 +09:00
postedAtTime: function(){
return !!this.postedAt ? moment(this.postedAt).format("HH:mm") : null;
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-07-03 13:15:23 +09:00
Session.set('currentPostStatus', this.status);
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
2014-07-03 13:15:23 +09:00
$('#postedAtDate').datepicker();
2012-11-19 11:03:15 +09:00
}
2013-07-19 12:17:49 +09:00
2014-07-03 13:15:23 +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({
2014-07-03 13:15:23 +09:00
'change input[name=status]': function (e, i) {
Session.set('currentPostStatus', e.currentTarget.value);
},
'click input[type=submit]': function(e, instance){
2013-10-09 12:39:05 +09:00
var post = this;
e.preventDefault();
if(!Meteor.user()){
throwError('You must be logged in.');
return false;
}
2014-07-03 13:15:23 +09:00
// ------------------------------ Properties ------------------------------ //
// Basic Properties
var properties = {
title: $('#title').val(),
body: instance.editor.exportFile()
};
// Categories
var categoriesArray = [];
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
});
2014-07-03 13:15:23 +09:00
properties.categoriesArray = categoriesArray;
2014-07-03 13:15:23 +09:00
// URL
2014-07-03 13:15:23 +09:00
var url = $('#url').val();
if(!!url){
2013-01-13 09:13:25 +09:00
properties.url = (url.substring(0, 7) == "http://" || url.substring(0, 8) == "https://") ? url : "http://"+url;
}
2014-07-03 13:15:23 +09:00
// ShortURL
var shortUrl = $('#short-url').val();
if(!!shortUrl)
properties.shortUrl = shortUrl;
// ------------------------------ Admin Properties ------------------------------ //
2012-11-26 17:37:47 +09:00
if(isAdmin(Meteor.user())){
2014-07-03 13:15:23 +09:00
// Basic Properties
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(),
};
2014-07-03 13:15:23 +09:00
// Status
adminProperties.status = parseInt($('input[name=status]:checked').val());
2012-11-26 17:37:47 +09:00
properties = _.extend(properties, adminProperties);
2014-07-03 13:15:23 +09:00
// PostedAt
if(adminProperties.status == STATUS_APPROVED){
var setPostedAt = false;
var postedAt = new Date(); // default to current browser date and time
var postedAtDate = $('#postedAtDate').datepicker('getDate');
var postedAtTime = $('#postedAtTime').split(':').val();
if(postedAtDate != "Invalid Date"){ // if custom date is set, use it
postedAt = postedAtDate;
setPostedAt = true;
}
if(postedAtTime.length==2){ // if custom time is set, use it
var hours = postedAtTime[0];
var minutes = postedAtTime[1];
postedAt = moment(postedAt).hour(hours).minute(minutes).toDate();
setPostedAt = true;
}
if(setPostedAt){ // if either custom date or time has been set, pass result to method
Meteor.call('setPostedAt', post); // use a method to guarantee timestamp integrity
}else{
Meteor.call('setPostedAt');
}
}
2012-11-26 17:37:47 +09:00
}
2014-06-25 14:33:41 +09:00
// console.log(properties)
2012-11-26 17:37:47 +09:00
2014-07-03 13:15:23 +09:00
// ------------------------------ Update ------------------------------ //
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
}
}
});