-
Notifications
You must be signed in to change notification settings - Fork 189
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
54 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,43 +1,69 @@ | ||
// jQuery.fileUpload - handles attachment uploads (gem: jquery-fileupload-rails) | ||
|
||
function fileUploadInit() { | ||
$('[data-behavior~=jquery-upload]').fileupload({ | ||
autoUpload: true, | ||
dropZone: function(){ return $('[data-behavior~=drop-zone]') }, | ||
pasteZone: function(){ return $('[data-behavior~=drop-zone]') }, | ||
singleFileUploads: true, | ||
destroy: function(e, data) { | ||
if (confirm('Are you sure?\n\nProceeding will delete this attachment from the associated node.')) { | ||
$.blueimp.fileupload.prototype.options.destroy.call(this, e, data); | ||
} | ||
} | ||
}).on('fileuploadadd', function (e, data) { | ||
// data.$textarea is added to the input field owned by the editor and is | ||
// only present when the image button is clicked. Copying, or dragging does | ||
// not append the attribute. | ||
var $textarea = (e.originalEvent !== undefined) ? $(e.originalEvent.target) : data.$textarea; | ||
$('[data-behavior~=jquery-upload]') | ||
.fileupload({ | ||
autoUpload: true, | ||
dropZone: function () { | ||
return $('[data-behavior~=drop-zone]'); | ||
}, | ||
pasteZone: function () { | ||
return $('[data-behavior~=drop-zone]'); | ||
}, | ||
singleFileUploads: true, | ||
destroy: function (e, data) { | ||
if ( | ||
confirm( | ||
'Are you sure?\n\nProceeding will delete this attachment from the associated node.' | ||
) | ||
) { | ||
$.blueimp.fileupload.prototype.options.destroy.call(this, e, data); | ||
} | ||
}, | ||
downloadTemplateId: | ||
$('[data-behavior~=jquery-upload]').data('template-download') === false | ||
? null | ||
: 'template-download', | ||
uploadTemplateId: | ||
$('[data-behavior~=jquery-upload]').data('template-upload') === false | ||
? null | ||
: 'template-upload', | ||
}) | ||
.on('fileuploadadd', function (e, data) { | ||
// data.$textarea is added to the input field owned by the editor and is | ||
// only present when the image button is clicked. Copying, or dragging does | ||
// not append the attribute. | ||
var $textarea = | ||
e.originalEvent !== undefined | ||
? $(e.originalEvent.target) | ||
: data.$textarea; | ||
|
||
if ($textarea.is($('[data-behavior~=rich-toolbar]'))) { | ||
var editorToolbar = $textarea.data('editorToolbar'); | ||
data.replaceImagePlaceholder = editorToolbar.replaceImagePlaceholder.bind(editorToolbar); | ||
if ($textarea.is($('[data-behavior~=rich-toolbar]'))) { | ||
var editorToolbar = $textarea.data('editorToolbar'); | ||
data.replaceImagePlaceholder = | ||
editorToolbar.replaceImagePlaceholder.bind(editorToolbar); | ||
|
||
$.each(data.files, editorToolbar.insertImagePlaceholder.bind(editorToolbar)); | ||
} | ||
}).on('fileuploaddone', function (e, data) { | ||
if (data.replaceImagePlaceholder !== undefined) { | ||
$.each(data.files, data.replaceImagePlaceholder.bind(null, data)); | ||
} | ||
}); | ||
$.each( | ||
data.files, | ||
editorToolbar.insertImagePlaceholder.bind(editorToolbar) | ||
); | ||
} | ||
}) | ||
.on('fileuploaddone', function (e, data) { | ||
if (data.replaceImagePlaceholder !== undefined) { | ||
$.each(data.files, data.replaceImagePlaceholder.bind(null, data)); | ||
} | ||
}); | ||
} | ||
|
||
document.addEventListener("turbolinks:load", function() { | ||
document.addEventListener('turbolinks:load', function () { | ||
// Bind fileUpload on page load. | ||
fileUploadInit(); | ||
}); | ||
|
||
// Un-bind fileUpload on page unload. | ||
document.addEventListener('turbolinks:before-cache', function() { | ||
$('[data-behavior~=jquery-upload]').each(function() { | ||
document.addEventListener('turbolinks:before-cache', function () { | ||
$('[data-behavior~=jquery-upload]').each(function () { | ||
$(this).fileupload('destroy'); | ||
}); | ||
}); |