Vulcan/client/views/posts/post_edit.js

210 lines
5.7 KiB
JavaScript
Raw Normal View History

2014-07-05 11:24:28 +09:00
Template[getTemplate('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 () {
2014-07-03 14:13:47 +09:00
if((Session.get('currentPostStatus') || this.status) == STATUS_APPROVED){
2014-07-03 13:15:23 +09:00
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');
}
});
2014-07-05 11:24:28 +09:00
Template[getTemplate('post_edit')].rendered = function(){
2014-08-01 09:07:58 +09:00
// run all post edit rendered callbacks
var instance = this;
postEditRenderedCallbacks.forEach(function(callback) {
callback(instance);
});
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
2014-07-05 11:24:28 +09:00
Template[getTemplate('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();
$(e.target).addClass('disabled');
if(!Meteor.user()){
throwError('You must be logged in.');
return false;
}
2014-07-03 13:15:23 +09:00
// ------------------------------ Properties ------------------------------ //
// Basic Properties
var properties = {
2014-07-05 13:07:28 +09:00
title: $('#title').val(),
body: instance.editor.exportFile(),
categories: []
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){
2014-07-03 16:17:36 +09:00
var setPostedAt = false;
var postedAt = new Date(); // default to current browser date and time
var postedAtDate = $('#postedAtDate').datepicker('getDate');
var postedAtTime = $('#postedAtTime').val();
if($('#postedAtDate').exists() && postedAtDate != "Invalid Date"){ // if custom date is set, use it
postedAt = postedAtDate;
setPostedAt = true;
}
2014-07-03 13:15:23 +09:00
2014-07-03 16:17:36 +09:00
if($('#postedAtTime').exists() && postedAtTime.split(':').length==2){ // if custom time is set, use it
var hours = postedAtTime.split(':')[0];
var minutes = postedAtTime.split(':')[1];
postedAt = moment(postedAt).hour(hours).minute(minutes).toDate();
setPostedAt = true;
}
2014-07-03 13:15:23 +09:00
if(setPostedAt){ // if either custom date or time has been set, pass result to method
2014-07-03 16:17:36 +09:00
Meteor.call('setPostedAt', post, postedAt); // use a method to guarantee timestamp integrity
2014-07-03 13:15:23 +09:00
}else{
2014-07-03 16:17:36 +09:00
Meteor.call('setPostedAt', post);
2014-07-03 13:15:23 +09:00
}
}
2012-11-26 17:37:47 +09:00
}
2014-07-05 18:06:28 +09:00
// ------------------------------ Callbacks ------------------------------ //
// run all post edit client callbacks on properties object successively
properties = postEditClientCallbacks.reduce(function(result, currentFunction) {
return currentFunction(result);
}, properties);
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 ------------------------------ //
if (properties) {
Posts.update(post._id,{
$set: properties
}, function(error){
if(error){
console.log(error);
throwError(error.reason);
clearSeenErrors();
$(e.target).removeClass('disabled');
}else{
trackEvent("edit post", {'postId': post._id});
Router.go("/posts/"+post._id);
}
});
} else {
$(e.target).removeClass('disabled');
}
},
'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
}
}
});