Vulcan/client/views/posts/post_submit.js

106 lines
3 KiB
JavaScript
Raw Normal View History

2012-10-10 08:32:49 +09:00
Template.post_submit.helpers({
categoriesEnabled: function(){
return Categories.find().count();
},
2012-10-10 08:32:49 +09:00
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'));
2014-04-30 16:58:11 -07:00
return (post && this._id == post.userId) ? 'selected' : '';
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);
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());
});
2013-07-19 12:17:49 +09:00
$("#postUser").selectToAutocomplete();
2012-10-23 12:24:38 +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()){
2013-11-04 18:45:03 +01:00
throwError(i18n.t('You must be logged in.'));
return false;
}
2012-08-31 19:41:54 -04:00
var title= $('#title').val();
var url = $('#url').val();
var shortUrl = $('#short-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(Categories.findOne($(this).val()));
2012-10-10 08:32:49 +09:00
});
2013-01-13 08:52:35 +09:00
var properties = {
headline: title
, body: body
, shortUrl: shortUrl
, categories: categories
, sticky: sticky
, submitted: submitted
, userId: userId
, status: status
2013-01-13 08:52:35 +09:00
};
2013-02-20 10:27:45 +09:00
if(url){
var cleanUrl = (url.substring(0, 7) == "http://" || url.substring(0, 8) == "https://") ? url : "http://"+url;
2013-01-13 08:52:35 +09:00
properties.url = cleanUrl;
}
Meteor.call('post', properties, function(error, post) {
if(error){
throwError(error.reason);
2012-10-09 15:34:00 +09:00
clearSeenErrors();
$(e.target).removeClass('disabled');
if(error.error == 603)
Router.go('/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.')
Router.go('/posts/'+post.postId);
}
2012-08-31 19:41:54 -04:00
});
},
'click .get-title-link': function(e){
2012-09-07 18:17:19 +09:00
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
}
}
});