drag_drop_import.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. "use strict";
  2. /* This script handles PMA Drag Drop Import, loaded only when configuration is enabled.*/
  3. /**
  4. * Class to handle PMA Drag and Drop Import
  5. * feature
  6. */
  7. var DragDropImport = {
  8. /**
  9. * @var int, count of total uploads in this view
  10. */
  11. uploadCount: 0,
  12. /**
  13. * @var int, count of live uploads
  14. */
  15. liveUploadCount: 0,
  16. /**
  17. * @var string array, allowed extensions
  18. */
  19. allowedExtensions: ['sql', 'xml', 'ldi', 'mediawiki', 'shp'],
  20. /**
  21. * @var string array, allowed extensions for compressed files
  22. */
  23. allowedCompressedExtensions: ['gz', 'bz2', 'zip'],
  24. /**
  25. * @var obj array to store message returned by /import-status
  26. */
  27. importStatus: [],
  28. /**
  29. * Checks if any dropped file has valid extension or not
  30. *
  31. * @param file filename
  32. *
  33. * @return string, extension for valid extension, '' otherwise
  34. */
  35. getExtension: function getExtension(file) {
  36. var arr = file.split('.');
  37. var ext = arr[arr.length - 1]; // check if compressed
  38. if (jQuery.inArray(ext.toLowerCase(), DragDropImport.allowedCompressedExtensions) !== -1) {
  39. ext = arr[arr.length - 2];
  40. } // Now check for extension
  41. if (jQuery.inArray(ext.toLowerCase(), DragDropImport.allowedExtensions) !== -1) {
  42. return ext;
  43. }
  44. return '';
  45. },
  46. /**
  47. * Shows upload progress for different sql uploads
  48. *
  49. * @param: hash (string), hash for specific file upload
  50. * @param: percent (float), file upload percentage
  51. *
  52. * @return void
  53. */
  54. setProgress: function setProgress(hash, percent) {
  55. $('.pma_sql_import_status div li[data-hash="' + hash + '"]').children('progress').val(percent);
  56. },
  57. /**
  58. * Function to upload the file asynchronously
  59. *
  60. * @param formData FormData object for a specific file
  61. * @param hash hash of the current file upload
  62. *
  63. * @return void
  64. */
  65. sendFileToServer: function sendFileToServer(formData, hash) {
  66. var jqXHR = $.ajax({
  67. xhr: function xhr() {
  68. var xhrobj = $.ajaxSettings.xhr();
  69. if (xhrobj.upload) {
  70. xhrobj.upload.addEventListener('progress', function (event) {
  71. var percent = 0;
  72. var position = event.loaded || event.position;
  73. var total = event.total;
  74. if (event.lengthComputable) {
  75. percent = Math.ceil(position / total * 100);
  76. } // Set progress
  77. DragDropImport.setProgress(hash, percent);
  78. }, false);
  79. }
  80. return xhrobj;
  81. },
  82. url: 'index.php?route=/import',
  83. type: 'POST',
  84. contentType: false,
  85. processData: false,
  86. cache: false,
  87. data: formData,
  88. success: function success(data) {
  89. DragDropImport.importFinished(hash, false, data.success);
  90. if (!data.success) {
  91. DragDropImport.importStatus[DragDropImport.importStatus.length] = {
  92. hash: hash,
  93. message: data.error
  94. };
  95. }
  96. }
  97. }); // -- provide link to cancel the upload
  98. $('.pma_sql_import_status div li[data-hash="' + hash + '"] span.filesize').html('<span hash="' + hash + '" class="pma_drop_file_status" task="cancel">' + Messages.dropImportMessageCancel + '</span>'); // -- add event listener to this link to abort upload operation
  99. $('.pma_sql_import_status div li[data-hash="' + hash + '"] span.filesize span.pma_drop_file_status').on('click', function () {
  100. if ($(this).attr('task') === 'cancel') {
  101. jqXHR.abort();
  102. $(this).html('<span>' + Messages.dropImportMessageAborted + '</span>');
  103. DragDropImport.importFinished(hash, true, false);
  104. } else if ($(this).children('span').html() === Messages.dropImportMessageFailed) {
  105. // -- view information
  106. var $this = $(this);
  107. $.each(DragDropImport.importStatus, function (key, value) {
  108. if (value.hash === hash) {
  109. $('.pma_drop_result:visible').remove();
  110. var filename = $this.parent('span').attr('data-filename');
  111. $('body').append('<div class="pma_drop_result"><h2>' + Messages.dropImportImportResultHeader + ' - ' + filename + '<span class="close">x</span></h2>' + value.message + '</div>');
  112. $('.pma_drop_result').draggable(); // to make this dialog draggable
  113. }
  114. });
  115. }
  116. });
  117. },
  118. /**
  119. * Triggered when an object is dragged into the PMA UI
  120. *
  121. * @param event obj
  122. *
  123. * @return void
  124. */
  125. dragEnter: function dragEnter(event) {
  126. // We don't want to prevent users from using
  127. // browser's default drag-drop feature on some page(s)
  128. if ($('.noDragDrop').length !== 0) {
  129. return;
  130. }
  131. event.stopPropagation();
  132. event.preventDefault();
  133. if (!DragDropImport.hasFiles(event)) {
  134. return;
  135. }
  136. if (CommonParams.get('db') === '') {
  137. $('.pma_drop_handler').html(Messages.dropImportSelectDB);
  138. } else {
  139. $('.pma_drop_handler').html(Messages.dropImportDropFiles);
  140. }
  141. $('.pma_drop_handler').fadeIn();
  142. },
  143. /**
  144. * Check if dragged element contains Files
  145. *
  146. * @param event the event object
  147. *
  148. * @return bool
  149. */
  150. hasFiles: function hasFiles(event) {
  151. return !(typeof event.originalEvent.dataTransfer.types === 'undefined' || $.inArray('Files', event.originalEvent.dataTransfer.types) < 0 || $.inArray('application/x-moz-nativeimage', event.originalEvent.dataTransfer.types) >= 0);
  152. },
  153. /**
  154. * Triggered when dragged file is being dragged over PMA UI
  155. *
  156. * @param event obj
  157. *
  158. * @return void
  159. */
  160. dragOver: function dragOver(event) {
  161. // We don't want to prevent users from using
  162. // browser's default drag-drop feature on some page(s)
  163. if ($('.noDragDrop').length !== 0) {
  164. return;
  165. }
  166. event.stopPropagation();
  167. event.preventDefault();
  168. if (!DragDropImport.hasFiles(event)) {
  169. return;
  170. }
  171. $('.pma_drop_handler').fadeIn();
  172. },
  173. /**
  174. * Triggered when dragged objects are left
  175. *
  176. * @param event obj
  177. *
  178. * @return void
  179. */
  180. dragLeave: function dragLeave(event) {
  181. // We don't want to prevent users from using
  182. // browser's default drag-drop feature on some page(s)
  183. if ($('.noDragDrop').length !== 0) {
  184. return;
  185. }
  186. event.stopPropagation();
  187. event.preventDefault();
  188. var $dropHandler = $('.pma_drop_handler');
  189. $dropHandler.clearQueue().stop();
  190. $dropHandler.fadeOut();
  191. $dropHandler.html(Messages.dropImportDropFiles);
  192. },
  193. /**
  194. * Called when upload has finished
  195. *
  196. * @param string, unique hash for a certain upload
  197. * @param bool, true if upload was aborted
  198. * @param bool, status of sql upload, as sent by server
  199. *
  200. * @return void
  201. */
  202. importFinished: function importFinished(hash, aborted, status) {
  203. $('.pma_sql_import_status div li[data-hash="' + hash + '"]').children('progress').hide();
  204. var icon = 'icon ic_s_success'; // -- provide link to view upload status
  205. if (!aborted) {
  206. if (status) {
  207. $('.pma_sql_import_status div li[data-hash="' + hash + '"] span.filesize span.pma_drop_file_status').html('<span>' + Messages.dropImportMessageSuccess + '</a>');
  208. } else {
  209. $('.pma_sql_import_status div li[data-hash="' + hash + '"] span.filesize span.pma_drop_file_status').html('<span class="underline">' + Messages.dropImportMessageFailed + '</a>');
  210. icon = 'icon ic_s_error';
  211. }
  212. } else {
  213. icon = 'icon ic_s_notice';
  214. }
  215. $('.pma_sql_import_status div li[data-hash="' + hash + '"] span.filesize span.pma_drop_file_status').attr('task', 'info'); // Set icon
  216. $('.pma_sql_import_status div li[data-hash="' + hash + '"]').prepend('<img src="./themes/dot.gif" title="finished" class="' + icon + '"> '); // Decrease liveUploadCount by one
  217. $('.pma_import_count').html(--DragDropImport.liveUploadCount);
  218. if (!DragDropImport.liveUploadCount) {
  219. $('.pma_sql_import_status h2 .close').fadeIn();
  220. }
  221. },
  222. /**
  223. * Triggered when dragged objects are dropped to UI
  224. * From this function, the AJAX Upload operation is initiated
  225. *
  226. * @param event object
  227. *
  228. * @return void
  229. */
  230. drop: function drop(event) {
  231. // We don't want to prevent users from using
  232. // browser's default drag-drop feature on some page(s)
  233. if ($('.noDragDrop').length !== 0) {
  234. return;
  235. }
  236. var dbname = CommonParams.get('db');
  237. var server = CommonParams.get('server'); // if no database is selected -- no
  238. if (dbname !== '') {
  239. var files = event.originalEvent.dataTransfer.files;
  240. if (!files || files.length === 0) {
  241. // No files actually transferred
  242. $('.pma_drop_handler').fadeOut();
  243. event.stopPropagation();
  244. event.preventDefault();
  245. return;
  246. }
  247. $('.pma_sql_import_status').slideDown();
  248. for (var i = 0; i < files.length; i++) {
  249. var ext = DragDropImport.getExtension(files[i].name);
  250. var hash = AJAX.hash(++DragDropImport.uploadCount);
  251. var $sqlImportStatusDiv = $('.pma_sql_import_status div');
  252. $sqlImportStatusDiv.append('<li data-hash="' + hash + '">' + (ext !== '' ? '' : '<img src="./themes/dot.gif" title="invalid format" class="icon ic_s_notice"> ') + Functions.escapeHtml(files[i].name) + '<span class="filesize" data-filename="' + Functions.escapeHtml(files[i].name) + '">' + (files[i].size / 1024).toFixed(2) + ' kb</span></li>'); // scroll the UI to bottom
  253. $sqlImportStatusDiv.scrollTop($sqlImportStatusDiv.scrollTop() + 50); // 50 hardcoded for now
  254. if (ext !== '') {
  255. // Increment liveUploadCount by one
  256. $('.pma_import_count').html(++DragDropImport.liveUploadCount);
  257. $('.pma_sql_import_status h2 .close').fadeOut();
  258. $('.pma_sql_import_status div li[data-hash="' + hash + '"]').append('<br><progress max="100" value="2"></progress>'); // uploading
  259. var fd = new FormData();
  260. fd.append('import_file', files[i]);
  261. fd.append('noplugin', Math.random().toString(36).substring(2, 12));
  262. fd.append('db', dbname);
  263. fd.append('server', server);
  264. fd.append('token', CommonParams.get('token'));
  265. fd.append('import_type', 'database'); // todo: method to find the value below
  266. fd.append('MAX_FILE_SIZE', '4194304'); // todo: method to find the value below
  267. fd.append('charset_of_file', 'utf-8'); // todo: method to find the value below
  268. fd.append('allow_interrupt', 'yes');
  269. fd.append('skip_queries', '0');
  270. fd.append('format', ext);
  271. fd.append('sql_compatibility', 'NONE');
  272. fd.append('sql_no_auto_value_on_zero', 'something');
  273. fd.append('ajax_request', 'true');
  274. fd.append('hash', hash); // init uploading
  275. DragDropImport.sendFileToServer(fd, hash);
  276. } else if (!DragDropImport.liveUploadCount) {
  277. $('.pma_sql_import_status h2 .close').fadeIn();
  278. }
  279. }
  280. }
  281. $('.pma_drop_handler').fadeOut();
  282. event.stopPropagation();
  283. event.preventDefault();
  284. }
  285. };
  286. /**
  287. * Called when some user drags, dragover, leave
  288. * a file to the PMA UI
  289. * @param object Event data
  290. * @return void
  291. */
  292. $(document).on('dragenter', DragDropImport.dragEnter);
  293. $(document).on('dragover', DragDropImport.dragOver);
  294. $(document).on('dragleave', '.pma_drop_handler', DragDropImport.dragLeave); // when file is dropped to PMA UI
  295. $(document).on('drop', 'body', DragDropImport.drop); // minimizing-maximizing the sql ajax upload status
  296. $(document).on('click', '.pma_sql_import_status h2 .minimize', function () {
  297. if ($(this).attr('toggle') === 'off') {
  298. $('.pma_sql_import_status div').css('height', '270px');
  299. $(this).attr('toggle', 'on');
  300. $(this).html('-'); // to minimize
  301. } else {
  302. $('.pma_sql_import_status div').css('height', '0px');
  303. $(this).attr('toggle', 'off');
  304. $(this).html('+'); // to maximise
  305. }
  306. }); // closing sql ajax upload status
  307. $(document).on('click', '.pma_sql_import_status h2 .close', function () {
  308. $('.pma_sql_import_status').fadeOut(function () {
  309. $('.pma_sql_import_status div').html('');
  310. DragDropImport.importStatus = []; // clear the message array
  311. });
  312. }); // Closing the import result box
  313. $(document).on('click', '.pma_drop_result h2 .close', function () {
  314. $(this).parent('h2').parent('div').remove();
  315. });