Skip to content

Instantly share code, notes, and snippets.

@eric-norcross
Last active September 5, 2016 18:37
Show Gist options
  • Save eric-norcross/781670be49d6a1703edde0c092fbcf07 to your computer and use it in GitHub Desktop.
Save eric-norcross/781670be49d6a1703edde0c092fbcf07 to your computer and use it in GitHub Desktop.
Rails 4 / Turbolinks / Cocoon / Cloudinary Initialization
var uploadsStarted = [];
var uploadsCompleted = [];
$(document).on("fileuploadadd", function(event, data) {
console.log("file upload add");
var submitButton = $("input[type=submit]");
uploadsStarted.push(event.target);
submitButton.attr("disabled", "disabled");
submitButton.addClass("disabled")
submitButton.attr("value", ("Please wait for " + (uploadsStarted.length - uploadsCompleted.length) + " image(s) to finish uploading."));
})
$(document).on("fileuploadfail", function(event, data) {
console.error("file upload fail", data);
alert("An error occurred while attempting to upload the image. Please refresh the page and try again. If this error persists, contact the administrator.");
})
$(document).on("cloudinarydone", function(event, data) {
console.log("file upload done");
var submitButton = $("input[type=submit]");
uploadsCompleted.push(event.target);
if (uploadsStarted.length == uploadsCompleted.length) {
submitButton.removeAttr('disabled');
submitButton.removeClass("disabled");
submitButton.attr("value", "Save");
} else {
submitButton.attr("value", ("Please wait for " + (uploadsStarted.length - uploadsCompleted.length) + " image(s) to finish uploading."));
}
})
// $(document).ready equivalent for Trubolinks 5 usage
$(document).on('turbolinks:load', function() {
console.log("trubolinks:load - jQuery Ready!");
$(function() {
cloudinaryUploaderInit();
});
// Support for Cocoon callbacks
$('.collection')
.on('cocoon:before-insert', function() {
console.log("cocoon:before-insert");
// $("#owner_from_list").hide();
// $("#owner a.add_fields").hide();
})
.on('cocoon:after-insert', function() {
console.log("cocoon:after-insert");
/* ... do something ... */
cloudinaryUploaderInit();
})
.on("cocoon:before-remove", function() {
console.log("cocoon:before-remove");
// $("#owner_from_list").show();
// $("#owner a.add_fields").show();
})
.on("cocoon:after-remove", function() {
console.log("cocoon:after-remove");
/* e.g. recalculate order of child items */
});
});
var cloudinaryUploaderInit = function() {
console.log("cloudinaryUploaderInit");
if ($.fn.cloudinary_fileupload !== undefined) {
console.log("`$.fn.cloudinary_fileupload` is defined");
console.log($("input[type=file]"));
if ($("input.cloudinary-fileupload[type=file]").length) {
console.log("`input.cloudinary-fileupload[type=file]` found; Creating Cloudinary File Upload Input");
$("input.cloudinary-fileupload[type=file]").cloudinary_fileupload();
console.log("AFTER - input.cloudinary-fileupload[type=file]", $("input.cloudinary-fileupload[type=file]"));
} else {
console.log("`input.cloudinary-fileupload[type=file]` Not found.");
}
} else {
console.log("`cloudinary_fileupload` is undefined");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment