Vulcan/client/views/posts/post_submit.js

98 lines
2.7 KiB
JavaScript
Raw Normal View History

2012-10-10 08:32:49 +09:00
Template.post_submit.helpers({
categories: function(){
return Categories.find();
2012-10-23 12:24:38 +09:00
},
users: function(){
return Meteor.users.find();
},
userName: function(){
return getDisplayName(this);
},
isSelected: function(){
var post=Posts.findOne(Session.get('selectedPostId'));
return post && this._id == post.userId;
2012-10-10 08:32:49 +09:00
}
});
2012-10-23 12:24:38 +09:00
Template.post_submit.rendered = function(){
Session.set('selectedPostId', null);
}
Template.post_submit.rendered = function(){
2012-11-19 12:03:06 +09:00
if(!this.editor && $('#editor').exists())
2012-11-01 10:27:34 +09:00
this.editor= new EpicEditor(EpicEditorOptions).load();
2012-10-23 12:24:38 +09:00
$('#submitted').datepicker().on('changeDate', function(ev){
$('#submitted_hidden').val(moment(ev.date).valueOf());
});
}
2012-09-06 11:09:24 +09:00
Template.post_submit.events = {
'click input[type=submit]': function(e, instance){
2012-09-07 18:17:19 +09:00
e.preventDefault();
2012-10-01 18:48:46 +09:00
$(e.target).addClass('disabled');
if(!Meteor.user()){
throwError('You must be logged in.');
return false;
}
2012-08-31 19:41:54 -04:00
var title= $('#title').val();
var url = $('#url').val();
var body = instance.editor.exportFile();
2012-10-10 08:32:49 +09:00
var categories=[];
2012-10-23 12:24:38 +09:00
var sticky=!!$('#sticky').attr('checked');
var submitted = $('#submitted_hidden').val();
var userId = $('#postUser').val();
var status = parseInt($('input[name=status]:checked').val());
2012-10-10 08:32:49 +09:00
$('input[name=category]:checked').each(function() {
categories.push($(this).val());
});
Meteor.call('post', {
headline: title
, body: body
, url: url
, categories: categories
, sticky: sticky
, submitted: submitted
, userId: userId
, status: status
2012-10-24 11:04:42 +09:00
}, function(error, post) {
if(error){
console.log(error);
throwError(error.reason);
2012-10-09 15:34:00 +09:00
clearSeenErrors();
$(e.target).removeClass('disabled');
if(error.error == 603)
Meteor.Router.to('/posts/'+error.details);
}else{
2012-10-24 11:04:42 +09:00
trackEvent("new post", {'postId': post.postId});
if(post.status === STATUS_PENDING)
throwError('Thanks, your post is awaiting approval.')
Meteor.Router.to('/posts/'+post.postId);
}
2012-08-31 19:41:54 -04:00
});
}
2012-09-07 18:17:19 +09:00
,'click .get-title-link': function(e){
e.preventDefault();
var url=$("#url").val();
$(".get-title-link").addClass("loading");
if(url){
$.get(url, function(response){
if ((suggestedTitle=((/<title>(.*?)<\/title>/m).exec(response.responseText))) != null){
$("#title").val(suggestedTitle[1]);
}else{
alert("Sorry, couldn't find a title...");
}
$(".get-title-link").removeClass("loading");
});
2012-09-07 18:17:19 +09:00
}else{
alert("Please fill in an URL first!");
$(".get-title-link").removeClass("loading");
2012-09-07 18:17:19 +09:00
}
}
2012-10-23 12:24:38 +09:00
};