X-Git-Url: https://gerrit.automotivelinux.org/gerrit/gitweb?p=src%2Fapp-framework-demo.git;a=blobdiff_plain;f=afb-client%2Fbower_components%2Fjszip%2Fdocumentation%2Fexamples%2Fdownloader.js;fp=afb-client%2Fbower_components%2Fjszip%2Fdocumentation%2Fexamples%2Fdownloader.js;h=13903ccdcaafcc022a9cb4f64c47e14608eef20a;hp=0000000000000000000000000000000000000000;hb=5b1e6cc132f44262a873fa8296a2a3e1017b0278;hpb=f7d2f9ac4168ee5064580c666d508667a73cefc0 diff --git a/afb-client/bower_components/jszip/documentation/examples/downloader.js b/afb-client/bower_components/jszip/documentation/examples/downloader.js new file mode 100644 index 0000000..13903cc --- /dev/null +++ b/afb-client/bower_components/jszip/documentation/examples/downloader.js @@ -0,0 +1,89 @@ +jQuery(function ($) { + "use strict"; + + /** + * Reset the message. + */ + function resetMessage () { + $("#result") + .removeClass() + .text(""); + } + /** + * show a successful message. + * @param {String} text the text to show. + */ + function showMessage(text) { + resetMessage(); + $("#result") + .addClass("alert alert-success") + .text(text); + } + /** + * show an error message. + * @param {String} text the text to show. + */ + function showError(text) { + resetMessage(); + $("#result") + .addClass("alert alert-danger") + .text(text); + } + + /** + * Fetch the content, add it to the JSZip object + * and use a jQuery deferred to hold the result. + * @param {String} url the url of the content to fetch. + * @param {String} filename the filename to use in the JSZip object. + * @param {JSZip} zip the JSZip instance. + * @return {jQuery.Deferred} the deferred containing the data. + */ + function deferredAddZip(url, filename, zip) { + var deferred = $.Deferred(); + JSZipUtils.getBinaryContent(url, function (err, data) { + if(err) { + deferred.reject(err); + } else { + zip.file(filename, data, {binary:true}); + deferred.resolve(data); + } + }); + return deferred; + } + + if(!JSZip.support.blob) { + showError("This demo works only with a recent browser !"); + return; + } + + var $form = $("#download_form").on("submit", function () { + + resetMessage(); + + var zip = new JSZip(); + var deferreds = []; + + // find every checked item + $(this).find(":checked").each(function () { + var $this = $(this); + var url = $this.data("url"); + var filename = url.replace(/.*\//g, ""); + deferreds.push(deferredAddZip(url, filename, zip)); + }); + + // when everything has been downloaded, we can trigger the dl + $.when.apply($, deferreds).done(function () { + var blob = zip.generate({type:"blob"}); + + // see FileSaver.js + saveAs(blob, "example.zip"); + + showMessage("done !"); + }).fail(function (err) { + showError(err); + }); + return false; + }); +}); + +// vim: set shiftwidth=4 softtabstop=4: