Implemented URL query parsing for initial token /opa/?token=abcde
[src/app-framework-demo.git] / afb-client / bower_components / jszip / documentation / examples / downloader.js
1 jQuery(function ($) {
2     "use strict";
3
4     /**
5      * Reset the message.
6      */
7     function resetMessage () {
8         $("#result")
9         .removeClass()
10         .text("");
11     }
12     /**
13      * show a successful message.
14      * @param {String} text the text to show.
15      */
16     function showMessage(text) {
17         resetMessage();
18         $("#result")
19         .addClass("alert alert-success")
20         .text(text);
21     }
22     /**
23      * show an error message.
24      * @param {String} text the text to show.
25      */
26     function showError(text) {
27         resetMessage();
28         $("#result")
29         .addClass("alert alert-danger")
30         .text(text);
31     }
32
33     /**
34      * Fetch the content, add it to the JSZip object
35      * and use a jQuery deferred to hold the result.
36      * @param {String} url the url of the content to fetch.
37      * @param {String} filename the filename to use in the JSZip object.
38      * @param {JSZip} zip the JSZip instance.
39      * @return {jQuery.Deferred} the deferred containing the data.
40      */
41     function deferredAddZip(url, filename, zip) {
42         var deferred = $.Deferred();
43         JSZipUtils.getBinaryContent(url, function (err, data) {
44             if(err) {
45                 deferred.reject(err);
46             } else {
47                 zip.file(filename, data, {binary:true});
48                 deferred.resolve(data);
49             }
50         });
51         return deferred;
52     }
53
54     if(!JSZip.support.blob) {
55         showError("This demo works only with a recent browser !");
56         return;
57     }
58
59     var $form = $("#download_form").on("submit", function () {
60
61         resetMessage();
62
63         var zip = new JSZip();
64         var deferreds = [];
65
66         // find every checked item
67         $(this).find(":checked").each(function () {
68             var $this = $(this);
69             var url = $this.data("url");
70             var filename = url.replace(/.*\//g, "");
71             deferreds.push(deferredAddZip(url, filename, zip));
72         });
73
74         // when everything has been downloaded, we can trigger the dl
75         $.when.apply($, deferreds).done(function () {
76             var blob = zip.generate({type:"blob"});
77
78             // see FileSaver.js
79             saveAs(blob, "example.zip");
80
81             showMessage("done !");
82         }).fail(function (err) {
83             showError(err);
84         });
85         return false;
86     });
87 });
88
89 // vim: set shiftwidth=4 softtabstop=4: