2013-02-27 07:38:38 +00:00
|
|
|
/**
|
|
|
|
|
* Pin Form for Pinry
|
|
|
|
|
* Descrip: This is for creation new pins on everything, the bookmarklet, on the
|
|
|
|
|
* site and even editing pins in some limited situations.
|
|
|
|
|
* Authors: Pinry Contributors
|
2013-03-03 14:30:06 +00:00
|
|
|
* Updated: March 3rd, 2013
|
2013-02-27 07:38:38 +00:00
|
|
|
* Require: jQuery, Pinry JavaScript Helpers
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
2013-02-26 01:54:02 +00:00
|
|
|
$(window).load(function() {
|
2013-02-27 22:44:56 +00:00
|
|
|
var uploadedImage = false;
|
|
|
|
|
|
2013-02-27 07:38:38 +00:00
|
|
|
// Start Helper Functions
|
|
|
|
|
function getFormData() {
|
|
|
|
|
return {
|
|
|
|
|
submitter: currentUser,
|
|
|
|
|
url: $('#pin-form-image-url').val(),
|
|
|
|
|
description: $('#pin-form-description').val(),
|
|
|
|
|
tags: cleanTags($('#pin-form-tags').val())
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-02-26 05:23:31 +00:00
|
|
|
|
2013-02-27 07:38:38 +00:00
|
|
|
function createPinPreviewFromForm() {
|
|
|
|
|
var context = {pins: [{
|
2013-02-26 05:23:31 +00:00
|
|
|
submitter: currentUser,
|
2013-02-27 07:38:38 +00:00
|
|
|
image: {thumbnail: {image: $('#pin-form-image-url').val()}},
|
2013-02-26 05:23:31 +00:00
|
|
|
description: $('#pin-form-description').val(),
|
2013-02-27 06:03:05 +00:00
|
|
|
tags: cleanTags($('#pin-form-tags').val())
|
2013-02-27 07:38:38 +00:00
|
|
|
}]},
|
|
|
|
|
html = renderTemplate('#pins-template', context),
|
|
|
|
|
preview = $('#pin-form-image-preview');
|
|
|
|
|
preview.html(html);
|
|
|
|
|
preview.find('.pin').width(200);
|
|
|
|
|
preview.find('.pin .text').width(140);
|
2013-03-04 00:50:59 +00:00
|
|
|
preview.find('.pin').fadeIn(300);
|
2013-03-05 22:02:35 +00:00
|
|
|
if (getFormData().url == "")
|
|
|
|
|
preview.find('.image-wrapper').height(278);
|
2013-03-04 00:50:59 +00:00
|
|
|
preview.find('.image-wrapper img').fadeIn(300);
|
2013-03-05 22:02:35 +00:00
|
|
|
console.log(preview.find('.pin').height());
|
|
|
|
|
setTimeout(function() {
|
|
|
|
|
if (preview.find('.pin').height() > 305) {
|
|
|
|
|
$('#pin-form .modal-body').animate({
|
|
|
|
|
'height': preview.find('.pin').height()
|
|
|
|
|
}, 300);
|
|
|
|
|
}
|
|
|
|
|
}, 300);
|
2013-02-26 05:23:31 +00:00
|
|
|
}
|
|
|
|
|
|
2013-02-27 07:38:38 +00:00
|
|
|
function dismissModal(modal) {
|
|
|
|
|
modal.modal('hide');
|
|
|
|
|
setTimeout(function() {
|
|
|
|
|
modal.remove();
|
|
|
|
|
}, 200);
|
2013-02-26 05:23:31 +00:00
|
|
|
}
|
2013-02-27 07:38:38 +00:00
|
|
|
// End Helper Functions
|
2013-02-26 05:23:31 +00:00
|
|
|
|
2013-02-26 23:20:50 +00:00
|
|
|
|
2013-02-27 07:38:38 +00:00
|
|
|
// Start View Functions
|
|
|
|
|
function createPinForm() {
|
|
|
|
|
$('body').append(renderTemplate('#pin-form-template', ''));
|
|
|
|
|
var modal = $('#pin-form'),
|
|
|
|
|
formFields = [$('#pin-form-image-url'), $('#pin-form-description'),
|
|
|
|
|
$('#pin-form-tags')],
|
|
|
|
|
pinFromUrl = getUrlParameter('pin-image-url');
|
|
|
|
|
modal.modal('show');
|
2013-02-27 22:44:56 +00:00
|
|
|
// Auto update preview on field changes
|
2013-03-05 22:02:35 +00:00
|
|
|
var timer;
|
2013-02-27 07:38:38 +00:00
|
|
|
for (var i in formFields) {
|
|
|
|
|
formFields[i].bind('propertychange keyup input paste', function() {
|
2013-03-05 22:02:35 +00:00
|
|
|
clearTimeout(timer);
|
|
|
|
|
timer = setTimeout(function() {
|
|
|
|
|
createPinPreviewFromForm()
|
|
|
|
|
}, 700);
|
2013-02-27 22:44:56 +00:00
|
|
|
if (!uploadedImage)
|
2013-03-04 00:50:59 +00:00
|
|
|
$('#pin-form-image-upload').parent().parent().fadeOut(300);
|
2013-02-27 07:38:38 +00:00
|
|
|
});
|
2013-02-26 23:20:50 +00:00
|
|
|
}
|
2013-02-27 22:44:56 +00:00
|
|
|
// Drag and Drop Upload
|
|
|
|
|
$('#pin-form-image-upload').fineUploader({
|
|
|
|
|
request: {
|
2013-03-02 12:01:07 -08:00
|
|
|
endpoint: '/pins/create-image/',
|
2013-02-27 22:44:56 +00:00
|
|
|
paramsInBody: true,
|
|
|
|
|
multiple: false,
|
|
|
|
|
validation: {
|
|
|
|
|
allowedExtensions: ['jpeg', 'jpg', 'png', 'gif']
|
|
|
|
|
},
|
|
|
|
|
text: {
|
|
|
|
|
uploadButton: 'Click or Drop'
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}).on('complete', function(e, id, name, data) {
|
2013-03-04 00:50:59 +00:00
|
|
|
$('#pin-form-image-url').parent().parent().fadeOut(300);
|
2013-02-27 22:44:56 +00:00
|
|
|
$('.qq-upload-button').css('display', 'none');
|
|
|
|
|
var promise = getImageData(data.success.id);
|
|
|
|
|
uploadedImage = data.success.id;
|
|
|
|
|
promise.success(function(image) {
|
|
|
|
|
$('#pin-form-image-url').val(image.thumbnail.image);
|
|
|
|
|
createPinPreviewFromForm();
|
|
|
|
|
});
|
2013-03-05 21:25:08 +00:00
|
|
|
promise.error(function() {
|
|
|
|
|
message('Problem uploading image.', 'alert alert-error');
|
|
|
|
|
});
|
2013-02-27 22:44:56 +00:00
|
|
|
});
|
|
|
|
|
// If bookmarklet submit
|
2013-02-27 07:38:38 +00:00
|
|
|
if (pinFromUrl) {
|
2013-03-03 13:05:38 +00:00
|
|
|
$('#pin-form-image-upload').parent().parent().css('display', 'none');
|
2013-02-27 07:38:38 +00:00
|
|
|
$('#pin-form-image-url').val(pinFromUrl);
|
|
|
|
|
$('.navbar').css('display', 'none');
|
|
|
|
|
modal.css({
|
|
|
|
|
'margin-top': -35,
|
|
|
|
|
'margin-left': -281
|
|
|
|
|
});
|
2013-02-26 23:20:50 +00:00
|
|
|
}
|
2013-02-27 22:44:56 +00:00
|
|
|
// Submit pin on post click
|
2013-02-26 01:54:02 +00:00
|
|
|
$('#pin-form-submit').click(function(e) {
|
2013-02-27 07:38:38 +00:00
|
|
|
e.preventDefault();
|
2013-02-27 08:03:23 +00:00
|
|
|
$(this).off('click');
|
|
|
|
|
$(this).addClass('disabled');
|
2013-02-27 07:38:38 +00:00
|
|
|
var data = {
|
2013-02-27 22:44:56 +00:00
|
|
|
submitter: '/api/v1/user/'+currentUser.id+'/',
|
|
|
|
|
description: $('#pin-form-description').val(),
|
|
|
|
|
tags: cleanTags($('#pin-form-tags').val())
|
|
|
|
|
};
|
|
|
|
|
if (uploadedImage) data.image = '/api/v1/image/'+uploadedImage+'/';
|
|
|
|
|
else data.url = $('#pin-form-image-url').val();
|
|
|
|
|
var promise = postPinData(data);
|
2013-02-27 08:03:23 +00:00
|
|
|
promise.success(function(pin) {
|
2013-02-27 07:38:38 +00:00
|
|
|
if (pinFromUrl) return window.close();
|
2013-03-03 14:30:06 +00:00
|
|
|
pin.editable = true;
|
2013-02-27 08:03:23 +00:00
|
|
|
pin = renderTemplate('#pins-template', {pins: [pin]});
|
|
|
|
|
$('#pins').prepend(pin);
|
2013-02-27 07:38:38 +00:00
|
|
|
dismissModal(modal);
|
2013-02-27 22:44:56 +00:00
|
|
|
uploadedImage = false;
|
2013-02-26 01:54:02 +00:00
|
|
|
});
|
2013-03-05 21:25:08 +00:00
|
|
|
promise.error(function() {
|
|
|
|
|
message('Problem saving image.', 'alert alert-error');
|
|
|
|
|
});
|
2013-02-26 01:54:02 +00:00
|
|
|
});
|
|
|
|
|
$('#pin-form-close').click(function() {
|
2013-02-27 07:38:38 +00:00
|
|
|
if (pinFromUrl) return window.close();
|
|
|
|
|
dismissModal(modal);
|
2013-02-26 01:54:02 +00:00
|
|
|
});
|
2013-02-27 07:38:38 +00:00
|
|
|
createPinPreviewFromForm();
|
2013-02-26 01:54:02 +00:00
|
|
|
}
|
2013-02-27 07:38:38 +00:00
|
|
|
// End View Functions
|
|
|
|
|
|
2013-02-26 01:54:02 +00:00
|
|
|
|
2013-02-27 07:38:38 +00:00
|
|
|
// Start Init
|
|
|
|
|
window.pinForm = function() {
|
|
|
|
|
createPinForm();
|
|
|
|
|
}
|
2013-02-26 23:20:50 +00:00
|
|
|
|
2013-02-27 07:38:38 +00:00
|
|
|
if (getUrlParameter('pin-image-url')) {
|
2013-02-26 01:54:02 +00:00
|
|
|
createPinForm();
|
2013-02-27 07:38:38 +00:00
|
|
|
}
|
|
|
|
|
// End Init
|
2013-02-26 01:54:02 +00:00
|
|
|
});
|