Vulcan/packages/telescope-embedly/lib/client/autoform-postthumbnail.js
Sacha Greif ff8bf40694 Merge branch 'telescope-master-semantic-messages' of https://github.com/AdmitHub/Telescope into AdmitHub-telescope-master-semantic-messages
Conflicts:
	client/helpers/handlebars.js
	client/views/comments/comment_edit.js
	client/views/comments/comment_form.js
	client/views/comments/comment_item.js
	client/views/posts/modules/post_upvote.js
	client/views/posts/post_edit.js
	client/views/posts/post_submit.js
	client/views/users/user_edit.js
	client/views/users/user_email.js
	lib/router.js
	packages/telescope-tags/lib/client/views/category_item.js
2014-12-06 17:34:08 +09:00

63 lines
1.9 KiB
JavaScript

AutoForm.addInputType("bootstrap-postthumbnail", {
template: "afPostThumbnail"
});
Template.afPostThumbnail.helpers({
atts: function addFormControlAtts() {
var atts = _.clone(this.atts);
// Add bootstrap class
atts = AutoForm.Utility.addClass(atts, "form-control");
return atts;
},
style: function () {
var thumbnailWidth = getSetting('thumbnailWidth', 200);
var thumbnailHeight = getSetting('thumbnailHeight', 125);
return "width: "+thumbnailWidth+"px; height: "+thumbnailHeight+"px;"
}
});
Template.afPostThumbnail.rendered = function () {
var $img = this.$('.post-thumbnail-preview');
var $thumbnailUrlField = this.$('[name="thumbnailUrl"]');
// note: the following fields are *not* in the current template
var $urlField = $('[name="url"]');
var $titleField = $('[name="title"]');
var $bodyField = $('[name="body"]');
var $thumbnailContainer = $('.post-thumbnail-container');
$urlField.change(function (e) {
var url = $urlField.val();
if (!!url) {
$thumbnailContainer.addClass('loading');
clearSeenErrors();
console.log('getting embedly data for '+url);
Meteor.call('getEmbedlyData', url, function (error, data) {
if (error) {
console.log(error)
flashMessage(error.reason, 'error');
$thumbnailContainer.removeClass('loading');
return
}
if (data) {
// set thumbnail and fill in thumbnailUrl field
$img.attr('src', data.thumbnailUrl);
$thumbnailUrlField.val(data.thumbnailUrl);
// remove loading class
$thumbnailContainer.removeClass('loading');
if (!$titleField.val()) // if title field is empty, fill in title
$titleField.val(data.title);
if (!$bodyField.val()) // if body field is empty, fill in body
$bodyField.val(data.description);
}
});
}
});
}