source: branches/testa-single-bookmark/js/remotex.js @ 36

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

hide unused menu by css

File size: 3.9 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 ) {
19        console.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 ) {
25        console.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) {
31        console.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 get_output_url(num, func) {
38console.log("get_output_url")
39        console.log("remotex get_output_url num=" + num )
40        let param_array = [['cmd', 'get_output_url'], ['num', num ] ]
41        await this.fetchx( this.filename , param_array, func )
42    }
43
44    async fetchx( filename, param_array, func) {
45console.log("fetchx func" + func)
46        let params = this.make_url_params( param_array )
47        fetch(`${filename}?${params}`)
48            .then((response) => response.text())
49            .then((text) => { console.log(func) ; if( func !== undefined ){func(text)} else { console.log("func=undefined")} })
50            .catch((error) => console.log(error));
51    }
52
53    upload_to_host(part) {
54        if( Globalx[part].key_indicate_file === true ){
55            let textarea = $( Globalx[part].textarea_sel )
56            upload_content(Globalx.num, part, Globalx[part].key, textarea.val())
57        }
58    }
59
60    upload_content(num, part, path, content) {
61        let form = document.createElement('form');
62        let req_content = document.createElement('input');
63        let req_path = document.createElement('input');
64        let req_cmd = document.createElement('input');
65        let req_num = document.createElement('input');
66
67        document.body.appendChild(form);
68        req_content.type = 'hidden';
69        req_content.name = 'mytext';
70        req_content.value = content;
71       
72        req_path.type = 'hidden';
73        req_path.name = 'path';
74        req_path.value = path;
75       
76        req_cmd.type = 'hidden';
77        req_cmd.name = 'cmd';
78        req_cmd.value = 'upload_content';
79       
80        req_num.type = 'hidden';
81        req_num.name = 'num';
82        req_num.value = num + '';
83       
84        form.appendChild(req_content);
85        form.appendChild(req_path);
86        form.appendChild(req_cmd);
87        form.appendChild(req_num);
88       
89        // FormDataオブジェクトを作成する
90        var form_data = new FormData(form);
91
92        fetch('php/content.php', {
93            method: 'POST',
94            mode: 'same-origin', /* 'no-cors' 'cors' 'same-origin' */
95            body: form_data
96        })
97            .then((response) => response.text())
98            .then((data) => console.log(data))
99            .catch((error) => console.log(error));
100    }
101
102    handleDownload(e) {
103        let dl = document.getElementById(`${e.data.part}-down-download`)
104
105        if( Globalx[ e.data.part ].key_indicate_file === true ){
106            this.get_content( e.data.num, e.data.part , Globalx[ e.data.part ].key , (content) => {
107                let blob = new Blob([ content ], { "type" : "text/plain" });
108
109                if (window.navigator.msSaveBlob) {
110                    window.navigator.msSaveBlob(blob, Globalx[ e.data.part ].item_name);
111                   
112                    // msSaveOrOpenBlobの場合はファイルを保存せずに開ける
113                    window.navigator.msSaveOrOpenBlob(blob, Globalx[ e.data.part ].item_name);
114                } else {
115                    dl.download = Globalx[ e.data.part ].item_name
116                    dl.href = window.URL.createObjectURL(blob);
117                }
118            })
119        }
120        else{
121            e.preventDefault()
122            dl.href = ""
123            dl.download = ""
124        }
125    }
126}
127
128function handleDownload(e) {
129    Globalx.remotex.handleDownload(e)
130}
Note: See TracBrowser for help on using the repository browser.