File size: 3,750 Bytes
45e5708
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/*

Copyright 2017 Ziadin Givan



Licensed under the Apache License, Version 2.0 (the "License");

you may not use this file except in compliance with the License.

You may obtain a copy of the License at



   http://www.apache.org/licenses/LICENSE-2.0



Unless required by applicable law or agreed to in writing, software

distributed under the License is distributed on an "AS IS" BASIS,

WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

See the License for the specific language governing permissions and

limitations under the License.



https://github.com/givanz/Vvvebjs

*/

Vvveb.Gui.download =
function () {
    let assets = [];
    
    function addUrl(url, href, binary) {
        assets.push({url, href, binary});
    }

    let html = Vvveb.Builder.frameHtml;

    //stylesheets
    html.querySelectorAll("link[href$='.css']", html).forEach(function(e, i) {
        addUrl(e.href, e.getAttribute("href"), false);
    });

    //javascripts
     html.querySelectorAll("script[src$='.js']", html).forEach(function(e, i) {
        addUrl(e.src, e.getAttribute("src"), false);
    });
    
    //images
     html.querySelectorAll("img[src]", html).forEach(function(e, i) {
        addUrl(e.src, e.getAttribute("src"), true);
    });


    let zip = new JSZip();
    let promises = [];
    
    for (i in assets) {
        let asset = assets[i];
        let url = asset.url;
        let href = asset.href;
        let binary = asset.binary;
        
        let filename = href.substring(href.lastIndexOf('/')+1);
        let path = href.substring(0, href.lastIndexOf('/')).replace(/\.\.\//g, "");
        if (href.indexOf("://") > 0) {
			//ignore path for external assets
			path = "";
		}

        promises.push(new Promise((resolve, reject) => {

          let request = new XMLHttpRequest();
          request.open('GET', url);
          if (binary) {
            request.responseType = 'blob';
          } else {
            request.responseType = 'text';
          }

          request.onload = function() {
            if (request.status === 200) {
              resolve({url, href, filename, path, binary, data:request.response, status:request.status});
            } else {
              //reject(Error('Error code:' + request.statusText));
              console.error('Error code:' + request.statusText);
              resolve({status:request.status});
            }
          };

          request.onerror = function() {
              reject(Error('There was a network error.'));
          };

          // Send the request
          try {
			request.send();          
		 } catch (error) {
			  console.error(error);
		 }
     }));
    }
    
    Promise.all(promises).then((data) => {
        let html = Vvveb.Builder.getHtml();
        
        for (i in data) {
            let file = data[i];
            let folder = zip;
            
            if (file.status == 200) {
				if (file.path) {
					file.path = file.path.replace(/^\//, "");
					folder = zip.folder(file.path);
				} else {
					folder = zip;
				}
				
				let url =  (file.path ? file.path + "/" : "") + file.filename.trim().replace(/^\//, "");
				html = html.replace(file.href, url);
								
				folder.file(file.filename, file.data, {base64: file.binary});
			}
        }
        
        zip.file(Vvveb.FileManager.getCurrentFileName() ?? "index.html", html);
        zip.generateAsync({type:"blob"})
        .then(function(content) {
            saveAs(content, Vvveb.FileManager.getPageData("title") ?? Vvveb.FileManager.getCurrentPage());
        });
    }).catch((error) => {
        console.log(error)
  })
};