Vulcan/client/views/posts/post_submit.js

156 lines
4.7 KiB
JavaScript
Raw Normal View History

2014-07-05 11:24:28 +09:00
Template[getTemplate('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(){
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);
},
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
}
});
2014-07-05 11:24:28 +09:00
Template[getTemplate('post_submit')].rendered = function(){
2014-08-01 09:07:58 +09:00
// run all post submit rendered callbacks
var instance = this;
postSubmitRenderedCallbacks.forEach(function(callback) {
callback(instance);
});
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
2014-09-16 15:13:42 -04:00
};
2012-10-23 12:24:38 +09:00
2014-07-05 11:24:28 +09:00
Template[getTemplate('post_submit')].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){
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 ------------------------------ //
if(!Meteor.user()){
throwError(i18n.t('you_must_be_logged_in'));
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-07-03 13:15:23 +09:00
// PostedAt
2014-09-16 15:13:42 -04:00
var $postedAtDate = $('#postedAtDate');
var $postedAtTime = $('#postedAtTime');
2014-07-03 13:15:23 +09:00
var setPostedAt = false;
var postedAt = new Date(); // default to current browser date and time
2014-09-16 15:13:42 -04:00
var postedAtDate = $postedAtDate.datepicker('getDate');
var postedAtTime = $postedAtTime.val();
2014-07-03 13:15:23 +09:00
2014-09-16 15:13:42 -04: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-09-16 15:13:42 -04:00
if ($postedAtTime.exists() && postedAtTime.split(':').length==2){ // if custom time is set, use it
2014-07-03 14:13:47 +09:00
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
2014-09-16 15:13:42 -04:00
properties.postedAt = postedAt;
2014-07-03 13:15:23 +09:00
// URL
var url = $('#url').val();
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-07-05 18:06:28 +09:00
// ------------------------------ Callbacks ------------------------------ //
2014-07-03 13:15:23 +09:00
2014-07-05 18:06:28 +09:00
// run all post submit client callbacks on properties object successively
properties = postSubmitClientCallbacks.reduce(function(result, currentFunction) {
return currentFunction(result);
}, properties);
2014-07-03 13:15:23 +09:00
2014-07-03 14:13:47 +09:00
// console.log(properties)
2014-07-03 13:15:23 +09:00
// ------------------------------ Insert ------------------------------ //
if (properties) {
Meteor.call('post', properties, function(error, post) {
if(error){
throwError(error.reason);
clearSeenErrors();
$(e.target).removeClass('disabled');
if(error.error == 603)
Router.go('/posts/'+error.details);
}else{
trackEvent("new post", {'postId': post._id});
if(post.status === STATUS_PENDING)
2014-09-16 15:13:42 -04:00
throwError('Thanks, your post is awaiting approval.');
Router.go('/posts/'+post._id);
}
});
} else {
$(e.target).removeClass('disabled');
}
2014-07-03 13:15:23 +09:00
},
'click .get-title-link': function(e){
2012-09-07 18:17:19 +09:00
e.preventDefault();
var url=$("#url").val();
2014-09-16 15:13:42 -04:00
var $getTitleLink = $(".get-title-link");
$getTitleLink.addClass("loading");
2012-09-07 18:17:19 +09:00
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...");
}
2014-09-16 15:13:42 -04:00
$getTitleLink.removeClass("loading");
});
2012-09-07 18:17:19 +09:00
}else{
alert("Please fill in an URL first!");
2014-09-16 15:13:42 -04:00
$getTitleLink.removeClass("loading");
2012-09-07 18:17:19 +09:00
}
}
});