2019-02-16 10:05:51 +01:00
|
|
|
/*
|
|
|
|
* bootstrap-uploadprogress
|
|
|
|
* github: https://github.com/jakobadam/bootstrap-uploadprogress
|
|
|
|
*
|
|
|
|
* Copyright (c) 2015 Jakob Aarøe Dam
|
|
|
|
* Version 1.0.0
|
|
|
|
* Licensed under the MIT license.
|
|
|
|
*/
|
2019-02-23 18:28:25 +01:00
|
|
|
(function($) {
|
2019-02-16 10:05:51 +01:00
|
|
|
"use strict";
|
|
|
|
|
|
|
|
$.support.xhrFileUpload = !!(window.FileReader && window.ProgressEvent);
|
|
|
|
$.support.xhrFormData = !!window.FormData;
|
|
|
|
|
2019-02-23 18:28:25 +01:00
|
|
|
if (!$.support.xhrFileUpload || !$.support.xhrFormData) {
|
2019-02-16 10:05:51 +01:00
|
|
|
// skip decorating form
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-02-23 18:28:25 +01:00
|
|
|
var template = "<div class=\"modal fade\" id=\"file-progress-modal\">" +
|
|
|
|
"<div class=\"modal-dialog\">" +
|
|
|
|
" <div class=\"modal-content\">" +
|
|
|
|
" <div class=\"modal-header\">" +
|
|
|
|
" <button type=\"button\" class=\"close\" data-dismiss=\"modal\" aria-label=\"Close\"><span aria-hidden=\"true\">×</span></button>" +
|
|
|
|
" <h4 class=\"modal-title\">Uploading</h4>" +
|
|
|
|
" </div>" +
|
|
|
|
" <div class=\"modal-body\">" +
|
|
|
|
" <div class=\"modal-message\"></div>" +
|
|
|
|
" <div class=\"progress\">" +
|
|
|
|
" <div class=\"progress-bar progress-bar-striped active\" role=\"progressbar\" aria-valuenow=\"0\" aria-valuemin=\"0\"" +
|
|
|
|
" aria-valuemax=\"100\" style=\"width: 0%;min-width: 2em;\">" +
|
|
|
|
" 0%" +
|
|
|
|
" </div>" +
|
|
|
|
" </div>" +
|
|
|
|
" </div>" +
|
|
|
|
" <div class=\"modal-footer\" style=\"display:none\">" +
|
|
|
|
" <button type=\"button\" class=\"btn btn-default\" data-dismiss=\"modal\">Close</button>" +
|
|
|
|
" </div>" +
|
|
|
|
" </div>" +
|
|
|
|
" </div>" +
|
|
|
|
"</div>";
|
|
|
|
|
|
|
|
var UploadProgress = function(element, options) {
|
2019-02-16 10:05:51 +01:00
|
|
|
this.options = options;
|
|
|
|
this.$element = $(element);
|
|
|
|
};
|
|
|
|
|
|
|
|
UploadProgress.prototype = {
|
|
|
|
|
|
|
|
constructor: function() {
|
|
|
|
this.$form = this.$element;
|
2019-02-23 18:28:25 +01:00
|
|
|
this.$form.on("submit", $.proxy(this.submit, this));
|
2019-02-16 10:05:51 +01:00
|
|
|
this.$modal = $(this.options.template);
|
2019-02-23 18:28:25 +01:00
|
|
|
this.$modalTitle = this.$modal.find(".modal-title");
|
|
|
|
this.$modalFooter = this.$modal.find(".modal-footer");
|
|
|
|
this.$modalBar = this.$modal.find(".progress-bar");
|
2019-02-16 10:05:51 +01:00
|
|
|
|
2019-02-23 18:28:25 +01:00
|
|
|
// Translate texts
|
|
|
|
this.$modalTitle.text(this.options.modalTitle)
|
|
|
|
this.$modalFooter.children("button").text(this.options.modalFooter)
|
|
|
|
|
|
|
|
this.$modal.on("hidden.bs.modal", $.proxy(this.reset, this));
|
2019-02-16 10:05:51 +01:00
|
|
|
},
|
|
|
|
|
2019-02-23 18:28:25 +01:00
|
|
|
reset: function() {
|
|
|
|
this.$modalTitle.text(this.options.modalTitle)
|
|
|
|
this.$modalFooter.hide();
|
|
|
|
this.$modalBar.addClass("progress-bar-success");
|
|
|
|
this.$modalBar.removeClass("progress-bar-danger");
|
|
|
|
if (this.xhr) {
|
2019-02-16 10:05:51 +01:00
|
|
|
this.xhr.abort();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
submit: function(e) {
|
|
|
|
e.preventDefault();
|
|
|
|
|
|
|
|
this.$modal.modal({
|
2019-02-23 18:28:25 +01:00
|
|
|
backdrop: "static",
|
2019-02-16 10:05:51 +01:00
|
|
|
keyboard: false
|
|
|
|
});
|
|
|
|
|
|
|
|
// We need the native XMLHttpRequest for the progress event
|
|
|
|
var xhr = new XMLHttpRequest();
|
|
|
|
this.xhr = xhr;
|
|
|
|
|
2019-02-23 18:28:25 +01:00
|
|
|
xhr.addEventListener("load", $.proxy(this.success, this, xhr));
|
|
|
|
xhr.addEventListener("error", $.proxy(this.error, this, xhr));
|
2019-02-16 10:05:51 +01:00
|
|
|
|
2019-02-23 18:28:25 +01:00
|
|
|
xhr.upload.addEventListener("progress", $.proxy(this.progress, this));
|
2019-02-16 10:05:51 +01:00
|
|
|
|
|
|
|
var form = this.$form;
|
2019-02-23 18:28:25 +01:00
|
|
|
|
|
|
|
xhr.open(form.attr("method"), form.attr("action"));
|
|
|
|
xhr.setRequestHeader("X-REQUESTED-WITH", "XMLHttpRequest");
|
2019-02-16 10:05:51 +01:00
|
|
|
|
|
|
|
var data = new FormData(form.get(0));
|
|
|
|
xhr.send(data);
|
|
|
|
},
|
|
|
|
|
|
|
|
success: function(xhr) {
|
2019-02-23 18:28:25 +01:00
|
|
|
if (xhr.status === 0 || xhr.status >= 400) {
|
2019-02-16 10:05:51 +01:00
|
|
|
// HTTP 500 ends up here!?!
|
|
|
|
return this.error(xhr);
|
|
|
|
}
|
|
|
|
this.set_progress(100);
|
|
|
|
var url;
|
2019-02-23 18:28:25 +01:00
|
|
|
var contentType = xhr.getResponseHeader("Content-Type");
|
2019-02-16 10:05:51 +01:00
|
|
|
|
|
|
|
// make it possible to return the redirect URL in
|
|
|
|
// a JSON response
|
2019-02-23 18:28:25 +01:00
|
|
|
if (contentType.indexOf("application/json") !== -1) {
|
2019-02-16 10:05:51 +01:00
|
|
|
var response = $.parseJSON(xhr.responseText);
|
|
|
|
url = response.location;
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
url = this.options.redirect_url;
|
|
|
|
}
|
|
|
|
window.location.href = url;
|
|
|
|
},
|
|
|
|
|
|
|
|
// handle form error
|
|
|
|
// we replace the form with the returned one
|
2019-02-23 18:28:25 +01:00
|
|
|
error: function(xhr) {
|
|
|
|
this.$modalTitle.text(this.options.modalTitleFailed);
|
2019-02-16 10:05:51 +01:00
|
|
|
|
2019-02-23 18:28:25 +01:00
|
|
|
this.$modalBar.removeClass("progress-bar-success");
|
|
|
|
this.$modalBar.addClass("progress-bar-danger");
|
|
|
|
this.$modalFooter.show();
|
2019-02-16 10:05:51 +01:00
|
|
|
|
2019-02-23 18:28:25 +01:00
|
|
|
var contentType = xhr.getResponseHeader("Content-Type");
|
2019-02-16 10:05:51 +01:00
|
|
|
// Replace the contents of the form, with the returned html
|
2019-02-23 18:28:25 +01:00
|
|
|
if (xhr.status === 422) {
|
|
|
|
var newHtml = $.parseHTML(xhr.responseText);
|
|
|
|
this.replace_form(newHtml);
|
|
|
|
this.$modal.modal("hide");
|
2019-02-16 10:05:51 +01:00
|
|
|
}
|
|
|
|
// Write the error response to the document.
|
|
|
|
else{
|
2019-02-23 18:28:25 +01:00
|
|
|
var responseText = xhr.responseText;
|
|
|
|
// Handle no response error
|
|
|
|
if (contentType) {
|
|
|
|
if (contentType.indexOf("text/plain") !== -1) {
|
|
|
|
responseText = "<pre>" + responseText + "</pre>";
|
|
|
|
}
|
|
|
|
document.write(xhr.responseText);
|
2019-02-16 10:05:51 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
set_progress: function(percent){
|
2019-02-23 18:28:25 +01:00
|
|
|
var txt = percent + "%";
|
2019-02-16 10:05:51 +01:00
|
|
|
if (percent == 100) {
|
2019-02-23 18:28:25 +01:00
|
|
|
txt = this.options.uploadedMsg;
|
2019-02-16 10:05:51 +01:00
|
|
|
}
|
2019-02-23 18:28:25 +01:00
|
|
|
this.$modalBar.attr("aria-valuenow", percent);
|
|
|
|
this.$modalBar.text(txt);
|
|
|
|
this.$modalBar.css("width", percent + "%");
|
2019-02-16 10:05:51 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
progress: function(/*ProgressEvent*/e){
|
|
|
|
var percent = Math.round((e.loaded / e.total) * 100);
|
|
|
|
this.set_progress(percent);
|
|
|
|
},
|
|
|
|
|
|
|
|
// replace_form replaces the contents of the current form
|
|
|
|
// with the form in the html argument.
|
|
|
|
// We use the id of the current form to find the new form in the html
|
2019-02-23 18:28:25 +01:00
|
|
|
replace_form: function(html) {
|
|
|
|
var newForm;
|
|
|
|
var formId = this.$form.attr("id");
|
|
|
|
if(formId !== undefined){
|
|
|
|
newForm = $(html).find("#" + formId);
|
2019-02-16 10:05:51 +01:00
|
|
|
}
|
|
|
|
else{
|
2019-02-23 18:28:25 +01:00
|
|
|
newForm = $(html).find("form");
|
2019-02-16 10:05:51 +01:00
|
|
|
}
|
|
|
|
// add the filestyle again
|
2019-02-23 18:28:25 +01:00
|
|
|
newForm.find(":file").filestyle({buttonBefore: true});
|
|
|
|
this.$form.html(newForm.children());
|
2019-02-16 10:05:51 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
$.fn.uploadprogress = function(options, value){
|
2019-02-23 18:28:25 +01:00
|
|
|
return this.each(function() {
|
2019-02-16 10:05:51 +01:00
|
|
|
var _options = $.extend({}, $.fn.uploadprogress.defaults, options);
|
|
|
|
var file_progress = new UploadProgress(this, _options);
|
|
|
|
file_progress.constructor();
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
$.fn.uploadprogress.defaults = {
|
|
|
|
template: template,
|
2019-02-23 18:28:25 +01:00
|
|
|
uploadedMsg: "Upload done, processing, please wait...",
|
|
|
|
modalTitle: "Uploading",
|
|
|
|
modalFooter: "Close",
|
|
|
|
modalTitleFailed: "Upload failed"
|
2019-02-16 10:05:51 +01:00
|
|
|
//redirect_url: ...
|
|
|
|
// need to customize stuff? Add here, and change code accordingly.
|
|
|
|
};
|
|
|
|
|
|
|
|
})(window.jQuery);
|