2012-10-10 08:32:49 +09:00
|
|
|
Template.post_submit.helpers({
|
2014-01-30 19:12:57 +00:00
|
|
|
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(){
|
2014-06-22 08:57:20 +09:00
|
|
|
return Meteor.users.find({}, {sort: {'profile.name': 1}});
|
2012-10-23 12:24:38 +09:00
|
|
|
},
|
|
|
|
userName: function(){
|
|
|
|
return getDisplayName(this);
|
|
|
|
},
|
2014-07-03 10:28:27 +09:00
|
|
|
isSelected: function(user){
|
|
|
|
return user._id == Meteor.userId() ? "selected" : "";
|
2014-07-03 13:15:23 +09:00
|
|
|
},
|
|
|
|
showPostedAt: function () {
|
|
|
|
if(Session.get('currentPostStatus') == STATUS_APPROVED){
|
|
|
|
return 'visible'
|
|
|
|
}else{
|
|
|
|
return 'hidden'
|
|
|
|
}
|
2014-07-03 14:13:47 +09:00
|
|
|
// return (Session.get('currentPostStatus') || STATUS_APPROVED) == STATUS_APPROVED; // default to approved
|
2012-10-10 08:32:49 +09:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2012-10-23 12:24:38 +09:00
|
|
|
Template.post_submit.rendered = function(){
|
2014-07-03 13:15:23 +09:00
|
|
|
Session.set('currentPostStatus', STATUS_APPROVED);
|
2012-10-23 12:24:38 +09:00
|
|
|
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();
|
2014-07-03 13:15:23 +09:00
|
|
|
|
|
|
|
$('#postedAtDate').datepicker();
|
2013-07-19 12:17:49 +09:00
|
|
|
|
2014-05-01 19:58:56 -07:00
|
|
|
// $("#postUser").selectToAutocomplete(); // XXX
|
2013-07-19 12:17:49 +09:00
|
|
|
|
2012-10-23 12:24:38 +09:00
|
|
|
}
|
|
|
|
|
2013-11-24 16:13:53 +02:00
|
|
|
Template.post_submit.events({
|
2014-07-03 13:15:23 +09:00
|
|
|
'change input[name=status]': function (e, i) {
|
|
|
|
Session.set('currentPostStatus', e.currentTarget.value);
|
|
|
|
},
|
2012-09-29 17:48:49 +09:00
|
|
|
'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');
|
|
|
|
|
2014-07-03 13:15:23 +09:00
|
|
|
// ------------------------------ Checks ------------------------------ //
|
|
|
|
|
2012-09-20 09:13:48 +09:00
|
|
|
if(!Meteor.user()){
|
2013-11-04 18:45:03 +01:00
|
|
|
throwError(i18n.t('You must be logged in.'));
|
2012-09-20 09:13:48 +09:00
|
|
|
return false;
|
|
|
|
}
|
2012-08-31 19:41:54 -04:00
|
|
|
|
2014-07-03 13:15:23 +09:00
|
|
|
// ------------------------------ Properties ------------------------------ //
|
2012-10-10 08:32:49 +09:00
|
|
|
|
2014-07-03 13:15:23 +09:00
|
|
|
// Basic Properties
|
2012-10-10 08:32:49 +09:00
|
|
|
|
2013-01-13 08:52:35 +09:00
|
|
|
var properties = {
|
2014-07-03 13:15:23 +09:00
|
|
|
title: $('#title').val(),
|
|
|
|
body: instance.editor.exportFile(),
|
|
|
|
sticky: $('#sticky').is(':checked'),
|
|
|
|
userId: $('#postUser').val(),
|
|
|
|
status: parseInt($('input[name=status]:checked').val())
|
2013-01-13 08:52:35 +09:00
|
|
|
};
|
2014-06-22 09:55:34 +09:00
|
|
|
|
2014-07-03 13:15:23 +09:00
|
|
|
// PostedAt
|
|
|
|
|
|
|
|
var setPostedAt = false;
|
|
|
|
var postedAt = new Date(); // default to current browser date and time
|
|
|
|
var postedAtDate = $('#postedAtDate').datepicker('getDate');
|
2014-07-03 14:13:47 +09:00
|
|
|
var postedAtTime = $('#postedAtTime').val();
|
2014-07-03 13:15:23 +09:00
|
|
|
|
2014-07-03 14:13:47 +09:00
|
|
|
if($('#postedAtDate').exists() && postedAtDate != "Invalid Date"){ // if custom date is set, use it
|
2014-07-03 13:15:23 +09:00
|
|
|
postedAt = postedAtDate;
|
|
|
|
setPostedAt = true;
|
|
|
|
}
|
|
|
|
|
2014-07-03 14:13:47 +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];
|
2014-07-03 13:15:23 +09:00
|
|
|
postedAt = moment(postedAt).hour(hours).minute(minutes).toDate();
|
|
|
|
setPostedAt = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(setPostedAt) // if either custom date or time has been set, pass result to properties
|
|
|
|
properties.postedAt = postedAt
|
|
|
|
|
|
|
|
|
|
|
|
// URL
|
|
|
|
|
|
|
|
var url = $('#url').val();
|
2014-06-22 09:55:34 +09:00
|
|
|
if(!!url){
|
2013-02-20 10:27:45 +09:00
|
|
|
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;
|
|
|
|
}
|
2014-06-22 09:55:34 +09:00
|
|
|
|
2014-07-03 13:15:23 +09:00
|
|
|
// Categories
|
|
|
|
|
|
|
|
$('input[name=category]:checked').each(function() {
|
|
|
|
properties.categories.push(Categories.findOne($(this).val()));
|
|
|
|
});
|
|
|
|
|
2014-07-03 14:13:47 +09:00
|
|
|
// console.log(properties)
|
2014-07-03 13:15:23 +09:00
|
|
|
|
|
|
|
// ------------------------------ Insert ------------------------------ //
|
2013-01-13 08:52:35 +09:00
|
|
|
|
|
|
|
Meteor.call('post', properties, function(error, post) {
|
2012-10-06 13:15:55 +09:00
|
|
|
if(error){
|
|
|
|
throwError(error.reason);
|
2012-10-09 15:34:00 +09:00
|
|
|
clearSeenErrors();
|
|
|
|
$(e.target).removeClass('disabled');
|
2012-10-30 12:01:11 +09:00
|
|
|
if(error.error == 603)
|
2013-10-09 21:49:02 +09:00
|
|
|
Router.go('/posts/'+error.details);
|
2012-10-06 13:15:55 +09:00
|
|
|
}else{
|
2014-07-03 13:15:23 +09:00
|
|
|
trackEvent("new post", {'postId': post._id});
|
2012-10-24 11:04:42 +09:00
|
|
|
if(post.status === STATUS_PENDING)
|
|
|
|
throwError('Thanks, your post is awaiting approval.')
|
2014-07-03 13:15:23 +09:00
|
|
|
Router.go('/posts/'+post._id);
|
2012-10-06 13:15:55 +09:00
|
|
|
}
|
2012-08-31 19:41:54 -04:00
|
|
|
});
|
2014-07-03 13:15:23 +09:00
|
|
|
|
2013-11-24 16:13:53 +02: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...");
|
2013-01-09 22:48:23 -03:00
|
|
|
}
|
2013-01-09 23:50:26 -03:00
|
|
|
$(".get-title-link").removeClass("loading");
|
|
|
|
});
|
2012-09-07 18:17:19 +09:00
|
|
|
}else{
|
|
|
|
alert("Please fill in an URL first!");
|
2013-01-09 23:50:26 -03:00
|
|
|
$(".get-title-link").removeClass("loading");
|
2012-09-07 18:17:19 +09:00
|
|
|
}
|
|
|
|
}
|
2013-07-04 11:16:11 +09:00
|
|
|
|
2013-11-24 16:13:53 +02:00
|
|
|
});
|