2019-02-16 09:05:51 +00: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-03-05 15:31:09 +00:00
|
|
|
|
2019-02-23 17:28:25 +00:00
|
|
|
(function($) {
|
2019-02-16 09:05:51 +00:00
|
|
|
"use strict";
|
|
|
|
|
|
|
|
$.support.xhrFileUpload = !!(window.FileReader && window.ProgressEvent);
|
|
|
|
$.support.xhrFormData = !!window.FormData;
|
|
|
|
|
2019-02-23 17:28:25 +00:00
|
|
|
if (!$.support.xhrFileUpload || !$.support.xhrFormData) {
|
2019-02-16 09:05:51 +00:00
|
|
|
// skip decorating form
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-02-23 17:28:25 +00:00
|
|
|
var template = "<div class=\"modal fade\" id=\"file-progress-modal\">" +
|
2019-03-09 01:44:45 +00:00
|
|
|
"<div class=\"modal-dialog upload-modal-dialog\">" +
|
2019-02-23 17:28:25 +00:00
|
|
|
" <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 09:05:51 +00:00
|
|
|
this.options = options;
|
|
|
|
this.$element = $(element);
|
|
|
|
};
|
|
|
|
|
|
|
|
UploadProgress.prototype = {
|
|
|
|
|
|
|
|
constructor: function() {
|
|
|
|
this.$form = this.$element;
|
2019-02-23 17:28:25 +00:00
|
|
|
this.$form.on("submit", $.proxy(this.submit, this));
|
2019-02-16 09:05:51 +00:00
|
|
|
this.$modal = $(this.options.template);
|
2019-02-23 17:28:25 +00: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 09:05:51 +00:00
|
|
|
|
2019-02-23 17:28:25 +00:00
|
|
|
// Translate texts
|
|
|
|
this.$modalTitle.text(this.options.modalTitle)
|
2019-03-05 15:31:09 +00:00
|
|
|
this.$modalFooter.children("button").text(this.options.modalFooter);
|
2019-02-23 17:28:25 +00:00
|
|
|
|
|
|
|
this.$modal.on("hidden.bs.modal", $.proxy(this.reset, this));
|
2019-02-16 09:05:51 +00:00
|
|
|
},
|
|
|
|
|
2019-02-23 17:28:25 +00:00
|
|
|
reset: function() {
|
2019-03-05 15:31:09 +00:00
|
|
|
this.$modalTitle.text(this.options.modalTitle);
|
2019-02-23 17:28:25 +00:00
|
|
|
this.$modalFooter.hide();
|
|
|
|
this.$modalBar.addClass("progress-bar-success");
|
|
|
|
this.$modalBar.removeClass("progress-bar-danger");
|
|
|
|
if (this.xhr) {
|
2019-02-16 09:05:51 +00:00
|
|
|
this.xhr.abort();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
submit: function(e) {
|
|
|
|
e.preventDefault();
|
|
|
|
|
|
|
|
this.$modal.modal({
|
2019-02-23 17:28:25 +00:00
|
|
|
backdrop: "static",
|
2019-02-16 09:05:51 +00:00
|
|
|
keyboard: false
|
|
|
|
});
|
|
|
|
|
|
|
|
// We need the native XMLHttpRequest for the progress event
|
|
|
|
var xhr = new XMLHttpRequest();
|
|
|
|
this.xhr = xhr;
|
|
|
|
|
2019-02-23 17:28:25 +00:00
|
|
|
xhr.addEventListener("load", $.proxy(this.success, this, xhr));
|
|
|
|
xhr.addEventListener("error", $.proxy(this.error, this, xhr));
|
2019-02-16 09:05:51 +00:00
|
|
|
|
2019-02-23 17:28:25 +00:00
|
|
|
xhr.upload.addEventListener("progress", $.proxy(this.progress, this));
|
2019-02-16 09:05:51 +00:00
|
|
|
|
|
|
|
var form = this.$form;
|
2019-02-23 17:28:25 +00:00
|
|
|
|
|
|
|
xhr.open(form.attr("method"), form.attr("action"));
|
|
|
|
xhr.setRequestHeader("X-REQUESTED-WITH", "XMLHttpRequest");
|
2019-02-16 09:05:51 +00:00
|
|
|
|
|
|
|
var data = new FormData(form.get(0));
|
|
|
|
xhr.send(data);
|
|
|
|
},
|
|
|
|
|
|
|
|
success: function(xhr) {
|
2019-02-23 17:28:25 +00:00
|
|
|
if (xhr.status === 0 || xhr.status >= 400) {
|
2019-02-16 09:05:51 +00:00
|
|
|
// HTTP 500 ends up here!?!
|
|
|
|
return this.error(xhr);
|
|
|
|
}
|
2019-03-09 06:05:43 +00:00
|
|
|
this.setProgress(100);
|
2019-02-16 09:05:51 +00:00
|
|
|
var url;
|
2019-02-23 17:28:25 +00:00
|
|
|
var contentType = xhr.getResponseHeader("Content-Type");
|
2019-02-16 09:05:51 +00:00
|
|
|
|
|
|
|
// make it possible to return the redirect URL in
|
|
|
|
// a JSON response
|
2019-02-23 17:28:25 +00:00
|
|
|
if (contentType.indexOf("application/json") !== -1) {
|
2019-02-16 09:05:51 +00: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 17:28:25 +00:00
|
|
|
error: function(xhr) {
|
|
|
|
this.$modalTitle.text(this.options.modalTitleFailed);
|
2019-02-16 09:05:51 +00:00
|
|
|
|
2019-02-23 17:28:25 +00:00
|
|
|
this.$modalBar.removeClass("progress-bar-success");
|
|
|
|
this.$modalBar.addClass("progress-bar-danger");
|
|
|
|
this.$modalFooter.show();
|
2019-02-16 09:05:51 +00:00
|
|
|
|
2019-02-23 17:28:25 +00:00
|
|
|
var contentType = xhr.getResponseHeader("Content-Type");
|
2019-02-16 09:05:51 +00:00
|
|
|
// Replace the contents of the form, with the returned html
|
2019-02-23 17:28:25 +00:00
|
|
|
if (xhr.status === 422) {
|
|
|
|
var newHtml = $.parseHTML(xhr.responseText);
|
2019-03-05 15:31:09 +00:00
|
|
|
this.replaceForm(newHtml);
|
|
|
|
this.$modal.modal("hide");
|
2019-02-16 09:05:51 +00:00
|
|
|
}
|
|
|
|
// Write the error response to the document.
|
|
|
|
else{
|
2019-02-23 17:28:25 +00:00
|
|
|
// Handle no response error
|
|
|
|
if (contentType) {
|
2019-03-05 15:31:09 +00:00
|
|
|
var responseText = xhr.responseText;
|
2019-02-23 17:28:25 +00:00
|
|
|
if (contentType.indexOf("text/plain") !== -1) {
|
|
|
|
responseText = "<pre>" + responseText + "</pre>";
|
|
|
|
}
|
2019-03-05 15:31:09 +00:00
|
|
|
document.write(responseText);
|
2019-02-16 09:05:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2019-03-05 15:31:09 +00:00
|
|
|
setProgress: function(percent) {
|
2019-02-23 17:28:25 +00:00
|
|
|
var txt = percent + "%";
|
2019-03-05 15:31:09 +00:00
|
|
|
if (percent === 100) {
|
2019-02-23 17:28:25 +00:00
|
|
|
txt = this.options.uploadedMsg;
|
2019-02-16 09:05:51 +00:00
|
|
|
}
|
2019-02-23 17:28:25 +00:00
|
|
|
this.$modalBar.attr("aria-valuenow", percent);
|
|
|
|
this.$modalBar.text(txt);
|
|
|
|
this.$modalBar.css("width", percent + "%");
|
2019-02-16 09:05:51 +00:00
|
|
|
},
|
|
|
|
|
2019-03-05 15:31:09 +00:00
|
|
|
progress: function(/*ProgressEvent*/e) {
|
2019-02-16 09:05:51 +00:00
|
|
|
var percent = Math.round((e.loaded / e.total) * 100);
|
2019-03-05 15:31:09 +00:00
|
|
|
this.setProgress(percent);
|
2019-02-16 09:05:51 +00:00
|
|
|
},
|
|
|
|
|
2019-03-05 15:31:09 +00:00
|
|
|
// replaceForm replaces the contents of the current form
|
2019-02-16 09:05:51 +00:00
|
|
|
// with the form in the html argument.
|
|
|
|
// We use the id of the current form to find the new form in the html
|
2019-03-05 15:31:09 +00:00
|
|
|
replaceForm: function(html) {
|
2019-02-23 17:28:25 +00:00
|
|
|
var newForm;
|
|
|
|
var formId = this.$form.attr("id");
|
2019-03-05 20:28:55 +00:00
|
|
|
if ( typeof formId !== "undefined") {
|
2019-02-23 17:28:25 +00:00
|
|
|
newForm = $(html).find("#" + formId);
|
2019-03-05 15:31:09 +00:00
|
|
|
} else {
|
2019-02-23 17:28:25 +00:00
|
|
|
newForm = $(html).find("form");
|
2019-02-16 09:05:51 +00:00
|
|
|
}
|
|
|
|
// add the filestyle again
|
2019-02-23 17:28:25 +00:00
|
|
|
newForm.find(":file").filestyle({buttonBefore: true});
|
|
|
|
this.$form.html(newForm.children());
|
2019-02-16 09:05:51 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-03-05 15:31:09 +00:00
|
|
|
$.fn.uploadprogress = function(options) {
|
2019-02-23 17:28:25 +00:00
|
|
|
return this.each(function() {
|
2019-02-16 09:05:51 +00:00
|
|
|
var _options = $.extend({}, $.fn.uploadprogress.defaults, options);
|
2019-03-05 15:31:09 +00:00
|
|
|
var fileProgress = new UploadProgress(this, _options);
|
|
|
|
fileProgress.constructor();
|
2019-02-16 09:05:51 +00:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
$.fn.uploadprogress.defaults = {
|
|
|
|
template: template,
|
2019-02-23 17:28:25 +00:00
|
|
|
uploadedMsg: "Upload done, processing, please wait...",
|
|
|
|
modalTitle: "Uploading",
|
|
|
|
modalFooter: "Close",
|
|
|
|
modalTitleFailed: "Upload failed"
|
2019-02-16 09:05:51 +00:00
|
|
|
//redirect_url: ...
|
|
|
|
// need to customize stuff? Add here, and change code accordingly.
|
|
|
|
};
|
|
|
|
|
|
|
|
})(window.jQuery);
|