|
1 | 1 | import $ from 'jquery'; |
2 | 2 | import { AJAX } from './ajax.js'; |
3 | 3 | import { Functions } from './functions.js'; |
| 4 | +import { Navigation } from './navigation.js'; |
| 5 | +import { CommonParams } from './common.js'; |
| 6 | + |
| 7 | +/* global themeImagePath */ // templates/javascript/variables.twig |
4 | 8 |
|
5 | 9 | /** |
6 | 10 | * Functions used in the import tab |
@@ -58,6 +62,7 @@ AJAX.registerTeardown('import.js', function () { |
58 | 62 | $('#input_import_file').off('change').off('focus'); |
59 | 63 | $('#select_local_import_file').off('focus'); |
60 | 64 | $('#text_csv_enclosed').add('#text_csv_escaped').off('keyup'); |
| 65 | + $('#importmain #buttonGo').off('click'); |
61 | 66 | }); |
62 | 67 |
|
63 | 68 | AJAX.registerOnload('import.js', function () { |
@@ -155,4 +160,133 @@ AJAX.registerOnload('import.js', function () { |
155 | 160 | } |
156 | 161 | return true; |
157 | 162 | }); |
| 163 | + |
| 164 | + $('#importmain #buttonGo').on('click', function () { |
| 165 | + const uploadProgressInfo = document.getElementById('upload_progress_info'); |
| 166 | + const uploadId = uploadProgressInfo.dataset.uploadId; |
| 167 | + const handler = uploadProgressInfo.dataset.handler; |
| 168 | + |
| 169 | + $('#upload_form_form').css('display', 'none'); |
| 170 | + |
| 171 | + const clockImage = '<img src="' + themeImagePath + 'ajax_clock_small.gif" width="16" height="16" alt="ajax clock">'; |
| 172 | + |
| 173 | + if (handler !== 'PhpMyAdmin\\Plugins\\Import\\Upload\\UploadNoplugin') { |
| 174 | + var finished = false; |
| 175 | + var percent = 0.0; |
| 176 | + var total = 0; |
| 177 | + var complete = 0; |
| 178 | + var originalTitle = parent && parent.document ? parent.document.title : false; |
| 179 | + var importStart; |
| 180 | + |
| 181 | + var performUpload = function () { |
| 182 | + $.getJSON( |
| 183 | + 'index.php?route=/import-status', |
| 184 | + { 'id': uploadId, 'import_status': 1, 'server': CommonParams.get('server') }, |
| 185 | + function (response) { |
| 186 | + finished = response.finished; |
| 187 | + percent = response.percent; |
| 188 | + total = response.total; |
| 189 | + complete = response.complete; |
| 190 | + |
| 191 | + if (total === 0 && complete === 0 && percent === 0) { |
| 192 | + $('#upload_form_status_info').html(clockImage + ' ' + window.Messages.uploadProgressMaximumAllowedSize); |
| 193 | + $('#upload_form_status').css('display', 'none'); |
| 194 | + } else { |
| 195 | + var now = new Date(); |
| 196 | + now = Date.UTC( |
| 197 | + now.getFullYear(), |
| 198 | + now.getMonth(), |
| 199 | + now.getDate(), |
| 200 | + now.getHours(), |
| 201 | + now.getMinutes(), |
| 202 | + now.getSeconds() |
| 203 | + ) + now.getMilliseconds() - 1000; |
| 204 | + var statusText = Functions.sprintf( |
| 205 | + window.Messages.uploadProgressStatusText, |
| 206 | + Functions.formatBytes( |
| 207 | + complete, 1, window.Messages.strDecimalSeparator |
| 208 | + ), |
| 209 | + Functions.formatBytes( |
| 210 | + total, 1, window.Messages.strDecimalSeparator |
| 211 | + ) |
| 212 | + ); |
| 213 | + |
| 214 | + if ($('#importmain').is(':visible')) { |
| 215 | + // Show progress UI |
| 216 | + $('#importmain').hide(); |
| 217 | + const uploadProgressDiv = '<div class="upload_progress">' |
| 218 | + + '<div class="upload_progress_bar_outer">' |
| 219 | + + '<div class="percentage"></div>' |
| 220 | + + '<div id="status" class="upload_progress_bar_inner">' |
| 221 | + + '<div class="percentage"></div></div></div>' |
| 222 | + + '<div>' + clockImage + ' ' + window.Messages.uploadProgressUploading + '</div>' |
| 223 | + + '<div id="statustext"></div></div>'; |
| 224 | + $('#import_form_status').html(uploadProgressDiv).show(); |
| 225 | + importStart = now; |
| 226 | + } else if (percent > 9 || complete > 2000000) { |
| 227 | + // Calculate estimated time |
| 228 | + var usedTime = now - importStart; |
| 229 | + var seconds = parseInt(((total - complete) / complete) * usedTime / 1000); |
| 230 | + var speed = Functions.sprintf( |
| 231 | + window.Messages.uploadProgressPerSecond, |
| 232 | + Functions.formatBytes(complete / usedTime * 1000, 1, window.Messages.strDecimalSeparator) |
| 233 | + ); |
| 234 | + |
| 235 | + var minutes = parseInt(seconds / 60); |
| 236 | + seconds %= 60; |
| 237 | + var estimatedTime; |
| 238 | + if (minutes > 0) { |
| 239 | + estimatedTime = window.Messages.uploadProgressRemainingMin |
| 240 | + .replace('%MIN', minutes) |
| 241 | + .replace('%SEC', seconds); |
| 242 | + } else { |
| 243 | + estimatedTime = window.Messages.uploadProgressRemainingSec |
| 244 | + .replace('%SEC', seconds); |
| 245 | + } |
| 246 | + |
| 247 | + statusText += '<br>' + speed + '<br><br>' + estimatedTime; |
| 248 | + } |
| 249 | + |
| 250 | + var percentString = Math.round(percent) + '%'; |
| 251 | + $('#status').animate({ width: percentString }, 150); |
| 252 | + $('.percentage').text(percentString); |
| 253 | + |
| 254 | + // Show percent in window title |
| 255 | + if (originalTitle !== false) { |
| 256 | + parent.document.title |
| 257 | + = percentString + ' - ' + originalTitle; |
| 258 | + } else { |
| 259 | + document.title |
| 260 | + = percentString + ' - ' + originalTitle; |
| 261 | + } |
| 262 | + $('#statustext').html(statusText); |
| 263 | + } |
| 264 | + |
| 265 | + if (finished) { |
| 266 | + if (originalTitle !== false) { |
| 267 | + parent.document.title = originalTitle; |
| 268 | + } else { |
| 269 | + document.title = originalTitle; |
| 270 | + } |
| 271 | + $('#importmain').hide(); |
| 272 | + // Loads the message, either success or mysql error |
| 273 | + $('#import_form_status') |
| 274 | + .html(clockImage + ' ' + window.Messages.uploadProgressBeingProcessed) |
| 275 | + .show(); |
| 276 | + $('#import_form_status').load( |
| 277 | + 'index.php?route=/import-status&message=1&import_status=1&server=' + CommonParams.get('server') |
| 278 | + ); |
| 279 | + Navigation.reload(); |
| 280 | + } else { |
| 281 | + setTimeout(performUpload, 1000); |
| 282 | + } |
| 283 | + }); |
| 284 | + }; |
| 285 | + setTimeout(performUpload, 1000); |
| 286 | + } else { |
| 287 | + // No plugin available |
| 288 | + $('#upload_form_status_info').html(clockImage + ' ' + window.Messages.uploadProgressNoDetails); |
| 289 | + $('#upload_form_status').css('display', 'none'); |
| 290 | + } |
| 291 | + }); |
158 | 292 | }); |
0 commit comments