source: branches/testa-x/js/remotex.js @ 101

Last change on this file since 101 was 29, checked in by anonymous, 6 years ago

add js

File size: 2.3 KB
Line 
1//"php/content.php"
2class Remotex {
3    constructor( filename ){
4        this.filename = filename
5//      this.num = num
6//      this.part = part
7    }
8
9    make_url_params( arrayx ){
10        let params = new URLSearchParams();
11        arrayx.reduce(function(acc, element, index, array) {
12            acc.append(element[0], element[1]);
13            return acc;
14        }, params)
15        return params;
16    }
17
18    async get_filelist_from_remote( num, part, func ) {
19console.log("remotex get_filelist_from_remote num=" + num + " part=" + part )
20        let param_array = [['cmd', 'get_filelistx'], ['num', num ], ['part', part ]]
21        await this.fetchx( this.filename , param_array, func )
22    }
23
24    async update_filelist( num, part, func ) {
25console.log("remotex update_filelist num=" + num + " part=" + part )
26        let param_array = [['cmd', 'update_filelistx'], ['num', num ], ['part', part ]]
27        await this.fetchx( this.filename , param_array, func )
28    }
29
30    async get_content(num, part, path , func) {
31console.log("remotex get_content num=" + num + " part=" + part )
32        console.log("part=" + part)
33        let param_array = [['cmd', 'get_content'], ['num', num ], ['part', part ], ['path', path]]
34        await this.fetchx( this.filename , param_array, func )
35    }
36
37    async fetchx( filename, param_array, func) {
38        let params = this.make_url_params( param_array )
39        fetch(`${filename}?${params}`)
40            .then((response) => response.text())
41            .then((text) => { console.log(func) ; func(text)})
42            .catch((error) => console.log(error));
43    }
44
45    handleDownload(e) {
46        let dl = document.getElementById("data-down-download")
47
48        if( Globalx[ e.data.part ].key_indicate_file === true ){
49            this.get_content( e.data.num, e.data.part , Globalx[ e.data.part ].key , (content) => {
50                let blob = new Blob([ content ], { "type" : "text/plain" });
51
52                if (window.navigator.msSaveBlob) {
53                    window.navigator.msSaveBlob(blob, Globalx[ e.data.part ].item_name);
54                   
55                    // msSaveOrOpenBlobの場合はファイルを保存せずに開ける
56                    window.navigator.msSaveOrOpenBlob(blob, Globalx[ e.data.part ].item_name);
57                } else {
58                    dl.download = Globalx[ e.data.part ].item_name
59                    dl.href = window.URL.createObjectURL(blob);
60                }
61            })
62        }
63        else{
64            e.preventDefault()
65            dl.href = ""
66            dl.download = ""
67        }
68    }
69}
70
71function handleDownload(e) {
72    Globalx.remotex.handleDownload(e)
73}
Note: See TracBrowser for help on using the repository browser.