| 1 | //"php/content.php" |
|---|
| 2 | class Remotex { |
|---|
| 3 | constructor( filename ){ |
|---|
| 4 | this.filename = filename |
|---|
| 5 | // this.num = num |
|---|
| 6 | // this.part = part |
|---|
| 7 | } |
|---|
| 8 | |
|---|
| 9 | make_url_params( arrayx , num ){ |
|---|
| 10 | let params = new URLSearchParams(); |
|---|
| 11 | for(let i=0; i<num; i++ ){ |
|---|
| 12 | params.append(arrayx[i][0], arrayx[i][1]) |
|---|
| 13 | } |
|---|
| 14 | const pa = params.toString() |
|---|
| 15 | return pa |
|---|
| 16 | } |
|---|
| 17 | |
|---|
| 18 | async get_filelist_from_remote( num, part, func ) { |
|---|
| 19 | let param_array = [['cmd', 'get_filelistx'], ['num', num ], ['part', part ]] |
|---|
| 20 | await this.fetchx( this.filename , param_array, 3, func ) |
|---|
| 21 | } |
|---|
| 22 | |
|---|
| 23 | async update_filelist( num, part, func ) { |
|---|
| 24 | let param_array = [['cmd', 'update_filelistx'], ['num', num ], ['part', part ]] |
|---|
| 25 | await this.fetchx( this.filename , param_array, 3, func ) |
|---|
| 26 | } |
|---|
| 27 | |
|---|
| 28 | async get_content(num, part, path , func) { |
|---|
| 29 | let param_array = [['cmd', 'get_content'], ['num', num ], ['part', part ], ['path', path]] |
|---|
| 30 | await this.fetchx( this.filename , param_array, 4, func ) |
|---|
| 31 | } |
|---|
| 32 | |
|---|
| 33 | async get_output_url(num, func) { |
|---|
| 34 | let param_array = [['cmd', 'get_output_url'], ['num', num ] ] |
|---|
| 35 | await this.fetchx( this.filename , param_array, 2, func ) |
|---|
| 36 | } |
|---|
| 37 | |
|---|
| 38 | async get_output_url_listx( num, func) { |
|---|
| 39 | let param_array = [['cmd', 'get_output_url_listx'] , ['num', num]] |
|---|
| 40 | await this.fetchx( this.filename , param_array, 2, func ) |
|---|
| 41 | } |
|---|
| 42 | |
|---|
| 43 | async update_output_url_listx( num, func) { |
|---|
| 44 | let param_array = [['cmd', 'update_output_url_listx'] , ['num', num]] |
|---|
| 45 | await this.fetchx( this.filename , param_array, 2, func ) |
|---|
| 46 | } |
|---|
| 47 | |
|---|
| 48 | async fetchx( filename, param_array, num, func) { |
|---|
| 49 | let params = this.make_url_params( param_array , num ) |
|---|
| 50 | fetch(`${filename}?${params}`) |
|---|
| 51 | .then((response) => { return response.text() }) |
|---|
| 52 | .then((text) => { func(text) }) |
|---|
| 53 | .catch((error) => console.log("error=" + error)); |
|---|
| 54 | } |
|---|
| 55 | |
|---|
| 56 | upload_to_host(part) { |
|---|
| 57 | if( Globalx[part].key_indicate_file === true ){ |
|---|
| 58 | let textarea = $( Globalx[part].textarea_sel ) |
|---|
| 59 | this.upload_content(Globalx.num, part, Globalx[part].key, textarea.val()) |
|---|
| 60 | } |
|---|
| 61 | } |
|---|
| 62 | |
|---|
| 63 | async build( cmd, num, part, path, content, func = null ) { |
|---|
| 64 | await this.upload_file( cmd, num, part, path, content, func = null ) |
|---|
| 65 | } |
|---|
| 66 | |
|---|
| 67 | async upload_file(cmd, num, part, path, content, func = null) { |
|---|
| 68 | let form = document.createElement('form') |
|---|
| 69 | let req_cmd = document.createElement('input') |
|---|
| 70 | let req_num = document.createElement('input') |
|---|
| 71 | let req_part = document.createElement('input') |
|---|
| 72 | let req_path = document.createElement('input') |
|---|
| 73 | let req_content = document.createElement('input') |
|---|
| 74 | |
|---|
| 75 | document.body.appendChild(form) |
|---|
| 76 | |
|---|
| 77 | req_cmd.type = 'hidden' |
|---|
| 78 | req_cmd.name = 'cmd' |
|---|
| 79 | req_cmd.value = cmd |
|---|
| 80 | |
|---|
| 81 | req_num.type = 'hidden' |
|---|
| 82 | req_num.name = 'num' |
|---|
| 83 | req_num.value = num + '' |
|---|
| 84 | |
|---|
| 85 | req_part.type = 'hidden' |
|---|
| 86 | req_part.name = 'part' |
|---|
| 87 | req_part.value = part |
|---|
| 88 | |
|---|
| 89 | req_path.type = 'hidden' |
|---|
| 90 | req_path.name = 'path' |
|---|
| 91 | req_path.value = path |
|---|
| 92 | |
|---|
| 93 | req_content.type = 'hidden' |
|---|
| 94 | req_content.name = 'mytext' |
|---|
| 95 | req_content.value = content |
|---|
| 96 | |
|---|
| 97 | form.appendChild(req_cmd) |
|---|
| 98 | form.appendChild(req_num) |
|---|
| 99 | form.appendChild(req_part) |
|---|
| 100 | form.appendChild(req_path) |
|---|
| 101 | form.appendChild(req_content) |
|---|
| 102 | |
|---|
| 103 | // FormDataオブジェクトを作成する |
|---|
| 104 | var form_data = new FormData(form); |
|---|
| 105 | console.log("remotex.js filename=" + this.filename ) |
|---|
| 106 | fetch( this.filename , { |
|---|
| 107 | method: 'POST', |
|---|
| 108 | mode: 'same-origin', /* 'no-cors' 'cors' 'same-origin' */ |
|---|
| 109 | body: form_data |
|---|
| 110 | }) |
|---|
| 111 | .then((response) => { |
|---|
| 112 | return response.text() |
|---|
| 113 | }) |
|---|
| 114 | .then((data) => {console.log( "data=" + data) |
|---|
| 115 | if( func !== null ){ |
|---|
| 116 | func(data) |
|---|
| 117 | } |
|---|
| 118 | }) |
|---|
| 119 | .catch((error) => {console.log("error=" + error)}) |
|---|
| 120 | } |
|---|
| 121 | |
|---|
| 122 | upload_content(num, part, path, content) { |
|---|
| 123 | let form = document.createElement('form'); |
|---|
| 124 | let req_content = document.createElement('input'); |
|---|
| 125 | let req_path = document.createElement('input'); |
|---|
| 126 | let req_cmd = document.createElement('input'); |
|---|
| 127 | let req_num = document.createElement('input'); |
|---|
| 128 | let req_part = document.createElement('input'); |
|---|
| 129 | |
|---|
| 130 | document.body.appendChild(form); |
|---|
| 131 | req_content.type = 'hidden'; |
|---|
| 132 | req_content.name = 'mytext'; |
|---|
| 133 | req_content.value = content; |
|---|
| 134 | |
|---|
| 135 | req_path.type = 'hidden'; |
|---|
| 136 | req_path.name = 'path'; |
|---|
| 137 | req_path.value = path; |
|---|
| 138 | |
|---|
| 139 | req_cmd.type = 'hidden'; |
|---|
| 140 | req_cmd.name = 'cmd'; |
|---|
| 141 | req_cmd.value = 'upload_content'; |
|---|
| 142 | |
|---|
| 143 | req_num.type = 'hidden'; |
|---|
| 144 | req_num.name = 'num'; |
|---|
| 145 | req_num.value = num + ''; |
|---|
| 146 | |
|---|
| 147 | req_part.type = 'hidden'; |
|---|
| 148 | req_part.name = 'part'; |
|---|
| 149 | req_part.value = part; |
|---|
| 150 | |
|---|
| 151 | form.appendChild(req_content); |
|---|
| 152 | form.appendChild(req_path); |
|---|
| 153 | form.appendChild(req_cmd); |
|---|
| 154 | form.appendChild(req_num); |
|---|
| 155 | form.appendChild(req_part); |
|---|
| 156 | |
|---|
| 157 | // FormDataオブジェクトを作成する |
|---|
| 158 | var form_data = new FormData(form); |
|---|
| 159 | console.log("remotex.js filename=" + this.filename ) |
|---|
| 160 | fetch( this.filename , { |
|---|
| 161 | method: 'POST', |
|---|
| 162 | mode: 'same-origin', /* 'no-cors' 'cors' 'same-origin' */ |
|---|
| 163 | body: form_data |
|---|
| 164 | }) |
|---|
| 165 | .then((response) => { return response.text()}) |
|---|
| 166 | .then((data) => {console.log( "data=" + data)}) |
|---|
| 167 | .catch((error) => {console.log("error=" + error)}); |
|---|
| 168 | } |
|---|
| 169 | |
|---|
| 170 | handleDownload(e) { |
|---|
| 171 | let dl = document.getElementById(`${e.data.part}-down-download`) |
|---|
| 172 | |
|---|
| 173 | if( Globalx[ e.data.part ].key_indicate_file === true ){ |
|---|
| 174 | this.get_content( e.data.num, e.data.part , Globalx[ e.data.part ].key , (content) => { |
|---|
| 175 | let blob = new Blob([ content ], { "type" : "text/plain" }); |
|---|
| 176 | |
|---|
| 177 | if (window.navigator.msSaveBlob) { |
|---|
| 178 | window.navigator.msSaveBlob(blob, Globalx[ e.data.part ].item_name); |
|---|
| 179 | |
|---|
| 180 | // msSaveOrOpenBlobの場合はファイルを保存せずに開ける |
|---|
| 181 | window.navigator.msSaveOrOpenBlob(blob, Globalx[ e.data.part ].item_name); |
|---|
| 182 | } else { |
|---|
| 183 | dl.download = Globalx[ e.data.part ].item_name |
|---|
| 184 | dl.href = window.URL.createObjectURL(blob); |
|---|
| 185 | } |
|---|
| 186 | }) |
|---|
| 187 | } |
|---|
| 188 | else{ |
|---|
| 189 | e.preventDefault() |
|---|
| 190 | dl.href = "" |
|---|
| 191 | dl.download = "" |
|---|
| 192 | } |
|---|
| 193 | } |
|---|
| 194 | } |
|---|
| 195 | |
|---|
| 196 | function handleDownload(e) { |
|---|
| 197 | Globalx.remotex.handleDownload(e) |
|---|
| 198 | } |
|---|