Vulcan/client/views/posts/post_edit.js

139 lines
3.9 KiB
JavaScript
Raw Normal View History

2012-10-02 10:54:04 +09:00
// Template.post_edit.preserve(['#title', '#url', '#editor', '#sticky']);
// Template.post_edit.preserve({
// // 'input[id]': function (node) { return node.id; }
// '[name]': function(node) { return node.getAttribute('name');}
// });
2013-02-09 11:48:37 +09:00
Template.post_edit.helpers({
post: function(){
return Posts.findOne(Session.get('selectedPostId'));
2012-10-10 08:32:49 +09:00
},
2013-01-13 08:52:35 +09:00
created: function(){
var post= Posts.findOne(Session.get('selectedPostId'));
return moment(post.createdAt).format("MMMM Do, h:mm:ss a");
},
2012-10-10 08:32:49 +09:00
categories: function(){
2013-02-10 08:15:31 +09:00
return Categories.find().fetch();
2012-10-10 08:32:49 +09:00
},
2013-02-09 11:48:37 +09:00
hasCategory: function(){
// console.log('hasCategory?')
2012-10-10 08:32:49 +09:00
var post= Posts.findOne(Session.get('selectedPostId'));
2013-02-09 11:48:37 +09:00
return _.contains(post.categories, this.name) ? 'checked' : '';
2012-10-18 11:26:42 +09:00
},
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' : '';
},
isSelected: function(){
2013-02-09 11:48:37 +09:00
// console.log('isSelected?')
var post= Posts.findOne(Session.get('selectedPostId'));
2013-01-13 09:13:25 +09:00
return post && this._id == post.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(){
2013-02-10 08:15:31 +09:00
return Meteor.users.find().fetch();
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' : '';
2012-10-24 11:04:42 +09:00
},
});
Template.post_edit.rendered = function(){
var post= Posts.findOne(Session.get('selectedPostId'));
if(post && !this.editor){
2012-11-19 11:03:15 +09:00
this.editor= new EpicEditor(EpicEditorOptions).load();
this.editor.importFile('editor',post.body);
2012-11-19 11:03:15 +09:00
$('#submitted_date').datepicker();
}
}
2012-09-13 11:05:49 +09:00
2012-09-06 11:09:24 +09:00
Template.post_edit.events = {
'click input[type=submit]': function(e, instance){
e.preventDefault();
if(!Meteor.user()){
throwError('You must be logged in.');
return false;
}
var selectedPostId=Session.get('selectedPostId');
2013-01-13 08:52:35 +09:00
var post = Posts.findOne(selectedPostId);
2012-11-26 17:39:23 +09:00
var categories = [];
var url = $('#url').val();
2013-01-13 08:52:35 +09:00
var status = parseInt($('input[name=status]:checked').val());
2012-11-26 17:39:23 +09:00
$('input[name=category]:checked').each(function() {
categories.push(Categories.findOne($(this).val()));
2012-11-26 17:39:23 +09:00
});
2012-11-26 17:37:47 +09:00
var properties = {
headline: $('#title').val(),
2012-11-26 17:37:47 +09:00
body: instance.editor.exportFile(),
2012-11-26 17:39:23 +09:00
categories: categories,
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-01-13 09:52:40 +09:00
if(post.submitted == ''){
// this is the first time we are approving the post
2013-01-13 10:21:09 +09:00
Meteor.call('post_approve', selectedPostId);
}else if($('#submitted_date').exists()){
2013-01-13 09:52:40 +09:00
properties.submitted = parseInt(moment($('#submitted_date').val()+$('#submitted_time').val(), "MM/DD/YYYY HH:mm").valueOf());
}
2013-01-13 08:52:35 +09:00
}
2012-11-26 17:37:47 +09:00
adminProperties = {
sticky: !!$('#sticky').attr('checked'),
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);
}
Posts.update(selectedPostId,
{
2012-11-26 17:37:47 +09:00
$set: properties
}
,function(error){
if(error){
throwError(error.reason);
}else{
trackEvent("edit post", {'postId': selectedPostId});
Meteor.Router.to("/posts/"+selectedPostId);
}
}
2012-09-06 11:09:24 +09:00
);
}
2012-09-07 10:57:57 +09:00
, 'click .delete-link': function(e){
e.preventDefault();
if(confirm("Are you sure?")){
var selectedPostId=Session.get('selectedPostId');
Posts.remove(selectedPostId);
2012-11-28 08:57:44 +09:00
Meteor.Router.to("/posts/deleted");
2012-09-07 10:57:57 +09:00
}
}
};