PageRenderTime 59ms CodeModel.GetById 27ms RepoModel.GetById 1ms app.codeStats 0ms

/wp-content/plugins/myMail/assets/js/manage-script.js

https://bitbucket.org/seyekuyinu/highlifer
JavaScript | 503 lines | 368 code | 134 blank | 1 comment | 40 complexity | caefa1dcae3d1f28ea671605448ca377 MD5 | raw file
Possible License(s): MIT, Apache-2.0, LGPL-2.1
  1. jQuery(document).ready(function ($) {
  2. "use strict"
  3. var importstatus = $('.import-status'),
  4. exportstatus = $('.export-status'),
  5. progress = $('#progress'),
  6. progressbar = progress.find('.bar'),
  7. wpnonce = $('#mymail_nonce').val(),
  8. importdata = null,
  9. importerrors = 0,
  10. importstarttime,
  11. importidentifier,
  12. uploader_init = function() {
  13. var uploader = new plupload.Uploader(wpUploaderInit);
  14. uploader.bind('Init', function(up) {
  15. var uploaddiv = $('#plupload-upload-ui');
  16. if ( up.features.dragdrop && ! $(document.body).hasClass('mobile') ) {
  17. uploaddiv.addClass('drag-drop');
  18. $('#drag-drop-area').bind('dragover.wp-uploader', function(){ // dragenter doesn't fire right :(
  19. uploaddiv.addClass('drag-over');
  20. }).bind('dragleave.wp-uploader, drop.wp-uploader', function(){
  21. uploaddiv.removeClass('drag-over');
  22. });
  23. } else {
  24. uploaddiv.removeClass('drag-drop');
  25. $('#drag-drop-area').unbind('.wp-uploader');
  26. }
  27. if ( up.runtime == 'html4' )
  28. $('.upload-flash-bypass').hide();
  29. });
  30. uploader.bind('FilesAdded', function(up, files) {
  31. $('#media-upload-error').html('');
  32. $('#wordpress-users').fadeOut();
  33. setTimeout(function(){
  34. up.refresh();
  35. up.start();
  36. }, 1);
  37. });
  38. uploader.bind('BeforeUpload', function(up, file) {
  39. progress.show().removeClass('finished error');
  40. importstatus.html('uploading');
  41. });
  42. uploader.bind('UploadFile', function(up, file) {
  43. });
  44. uploader.bind('UploadProgress', function(up, file) {
  45. importstatus.html(sprintf(mymailL10n.uploading, file.percent+'%'));
  46. progressbar.stop().animate({'width': file.percent+'%'}, 100);
  47. });
  48. uploader.bind('Error', function(up, err) {
  49. importstatus.html(err.message);
  50. progress.addClass('error');
  51. up.refresh();
  52. });
  53. uploader.bind('FileUploaded', function(up, file, response) {
  54. response = $.parseJSON(response.response);
  55. importidentifier = response.identifier;
  56. });
  57. uploader.bind('UploadComplete', function(up, files) {
  58. importstatus.html(mymailL10n.prepare_data);
  59. progress.addClass('finished');
  60. get_import_data();
  61. });
  62. uploader.init();
  63. }
  64. if ( typeof(wpUploaderInit) == 'object' )
  65. uploader_init();
  66. $('.wrap')
  67. .on('change', '#signup', function(){
  68. $('#signupdate').prop('disabled', !$(this).is(':checked'));
  69. })
  70. .on('click', '.do-import', function(){
  71. var lists = $('#lists').serialize(),
  72. order = $('#subscriber-table').serialize();
  73. if (!/%5D=email/.test(order)) {
  74. alert(mymailL10n.select_emailcolumn);
  75. return false;
  76. }
  77. if (!lists) {
  78. alert(mymailL10n.no_lists);
  79. return false;
  80. }
  81. if (!$('input[name="status"]:checked').length) {
  82. alert(mymailL10n.select_status);
  83. return false;
  84. }
  85. if(!confirm(mymailL10n.confirm_import)) return false;
  86. var _this = $(this).prop('disabled', true),
  87. status = $('input[name="status"]:checked').val(),
  88. existing = $('input[name="existing"]:checked').val(),
  89. signup = $('#signup').is(':checked'),
  90. signupdate = $('#signupdate').val(),
  91. keepstatus = $('#keepstatus').is(':checked'),
  92. loader = $('#import-ajax-loading').css({ 'display': 'inline-block' }),
  93. identifier = $('#identifier').val(),
  94. performance = $('#performance').is(':checked');
  95. progress.show();
  96. progressbar.stop().width(0);
  97. $('.step1').slideUp();
  98. $('.step2-body').html('<br><br>').parent().show();
  99. importstarttime = new Date();
  100. do_import(0, {
  101. identifier: identifier,
  102. order: order,
  103. lists: lists,
  104. status: status,
  105. keepstatus: keepstatus,
  106. existing: existing,
  107. signupdate: signup ? signupdate : null,
  108. performance: performance
  109. });
  110. importstatus.html(sprintf(mymailL10n.import_contacts, ''));
  111. window.onbeforeunload = function(){
  112. return mymailL10n.onbeforeunloadimport;
  113. };
  114. })
  115. .on('click', '.wordpress-users-toggle', function () {
  116. $(this).parent().parent().parent().find('li input').prop('checked', $(this).prop('checked'));
  117. })
  118. .on('click', '#addlist', function () {
  119. var val = $('#new_list_name').val();
  120. if (!val) return false;
  121. $('<li><label><input name="lists[]" value="' + val + '" type="checkbox" checked> ' + val + ' </label></li>').appendTo('#lists > ul');
  122. $('#new_list_name').val('');
  123. });
  124. $('#paste-import')
  125. .on('focus', function(){
  126. $(this).val('').addClass('focus');
  127. })
  128. .on('blur', function(){
  129. $(this).removeClass('focus');
  130. var value = $.trim($(this).val());
  131. if(value){
  132. _ajax('import_subscribers_upload_handler', {
  133. data: value
  134. }, function(response){
  135. if(response.success){
  136. importidentifier = response.identifier;
  137. $('#wordpress-users').fadeOut();
  138. get_import_data();
  139. }
  140. }, function(){
  141. importstatus.html('Error');
  142. });
  143. }
  144. });
  145. $('#import_wordpress')
  146. .on('submit', function(){
  147. var data = $(this).serialize();
  148. _ajax('import_subscribers_upload_handler', {
  149. wordpressusers: data
  150. }, function(response){
  151. if(response.success){
  152. importidentifier = response.identifier;
  153. $('#wordpress-users').fadeOut();
  154. get_import_data();
  155. }
  156. }, function(){
  157. importstatus.html('Error');
  158. });
  159. return false;
  160. });
  161. $( ".export-order" ).sortable({
  162. containment: "parent"
  163. });
  164. $('#export-subscribers').on('submit', function(){
  165. var data = $(this).serialize();
  166. progress.show().removeClass('finished error');
  167. $('.step1').slideUp();
  168. $('.step2').slideDown();
  169. $('.step2-body').html(sprintf(mymailL10n.write_file, '0.00 Kb'));
  170. _ajax('export_contacts', {
  171. data: data,
  172. },function(response){
  173. if(response.success){
  174. window.onbeforeunload = function(){
  175. return mymailL10n.onbeforeunloadexport;
  176. };
  177. var limit = $('.performance').val();
  178. do_export(0, limit, response.count, data);
  179. }else{
  180. alert(response.msg);
  181. }
  182. },function(jqXHR, textStatus, errorThrown){
  183. alert(textStatus);
  184. });
  185. return false;
  186. });
  187. $('#delete-subscribers').on('submit', function(){
  188. var input = prompt(mymailL10n.confirm_delete, '');
  189. if(!input) return false;
  190. if('delete' == input.toLowerCase()){
  191. var data = $(this).serialize();
  192. progress.show().removeClass('finished error');
  193. $('.step1').slideUp();
  194. progressbar.stop().animate({'width': '99%'}, 25000);
  195. _ajax('delete_contacts', {
  196. data: data,
  197. },function(response){
  198. if(response.success){
  199. progressbar.stop().animate({'width': '100%'}, 200, function(){
  200. $('.delete-status').html(response.msg);
  201. progress.addClass('finished');
  202. });
  203. }else{
  204. progressbar.stop();
  205. $('.delete-status').html(response.msg);
  206. progress.addClass('error');
  207. }
  208. },function(jqXHR, textStatus, errorThrown){
  209. progressbar.stop();
  210. $('.delete-status').html('['+jqXHR.status+'] '+errorThrown);
  211. progress.addClass('error');
  212. });
  213. }
  214. return false;
  215. });
  216. $('input.selectall').on('change', function(){
  217. var _this = $(this),
  218. name = _this.attr('name');
  219. $('input[name="'+name+'"]').prop('checked', _this.prop('checked'));
  220. });
  221. function do_export(offset, limit, count, data) {
  222. var t = new Date().getTime(),
  223. percentage = (Math.min(1, (limit*offset)/count)*100);
  224. exportstatus.html(sprintf(mymailL10n.prepare_download, ''));
  225. _ajax('do_export',{
  226. limit: limit,
  227. offset: offset,
  228. data: data
  229. }, function(response){
  230. var finished = percentage >= 100 && response.finished;
  231. if(response.success){
  232. if(!finished) do_export(offset+1, limit, count, data);
  233. progressbar.stop().animate({'width': (percentage)+'%'}, {
  234. duration: finished ? 100 : (new Date().getTime()-t)*0.9,
  235. easing: 'swing',
  236. queue:false,
  237. step: function(percentage){
  238. exportstatus.html(sprintf(mymailL10n.prepare_download, Math.ceil(percentage)+'%'));
  239. },
  240. complete: function(){
  241. exportstatus.html(sprintf(mymailL10n.prepare_download, Math.ceil(percentage)+'%'));
  242. if(finished){
  243. window.onbeforeunload = null;
  244. progress.addClass('finished');
  245. $('.step2-body').html(mymailL10n.download_finished);
  246. exportstatus.html(mymailL10n.downloading);
  247. if(response.filename) setTimeout( function() { document.location = response.filename }, 1000);
  248. }else{
  249. $('.step2-body').html(sprintf(mymailL10n.write_file, response.total));
  250. }
  251. }
  252. });
  253. }else{
  254. }
  255. }, function(jqXHR, textStatus, errorThrown){
  256. });
  257. }
  258. function do_import(id, options) {
  259. //if(id > importdata.parts) return;
  260. var t = new Date().getTime(),
  261. percentage = 0;
  262. if(!id) id = 0;
  263. _ajax('do_import',{
  264. id:id,
  265. options: options
  266. }, function(response){
  267. percentage = (Math.min(1, (response.imported+response.errors)/response.total)*100);
  268. $('.step2-body').html('<p>'+get_stats(response.f_imported, response.f_errors, response.f_total, percentage, response.memoryusage)+'</p>');
  269. importerrors = 0;
  270. var finished = percentage >= 100;
  271. if(response.success){
  272. if(!finished) do_import(id+1, options);
  273. progressbar.stop().animate({'width': (percentage)+'%'}, {
  274. duration: finished ? 100 : (new Date().getTime()-t)*0.9,
  275. easing: 'swing',
  276. queue:false,
  277. step: function(percentage){
  278. importstatus.html(sprintf(mymailL10n.import_contacts, Math.ceil(percentage)+'%'));
  279. },
  280. complete: function(){
  281. importstatus.html(sprintf(mymailL10n.import_contacts, Math.ceil(percentage)+'%'));
  282. if(finished){
  283. window.onbeforeunload = null;
  284. progress.addClass('finished');
  285. $('.step2-body').html(response.html).slideDown();
  286. }
  287. }
  288. });
  289. }else{
  290. upload_error_handler(percentage, id, options);
  291. }
  292. }, function(jqXHR, textStatus, errorThrown){
  293. upload_error_handler(percentage, id, options);
  294. });
  295. }
  296. function get_import_data(){
  297. progress.removeClass('finished error');
  298. _ajax('get_import_data', { identifier : importidentifier }, function(response){
  299. progress.hide().removeClass('finished');
  300. $('.step1').slideUp();
  301. $('.step2-body').html(response.html).parent().show();
  302. $('input.datepicker').datepicker({
  303. dateFormat: 'yy-mm-dd',
  304. showAnim: 'fadeIn',
  305. onClose: function () {
  306. }
  307. });
  308. importstatus.html('');
  309. importdata = response.data;
  310. });
  311. }
  312. function upload_error_handler(percentage, id, options){
  313. importerrors++;
  314. if(importerrors >= 5){
  315. alert(mymailL10n.error_importing);
  316. window.onbeforeunload = null;
  317. return;
  318. }
  319. var i = importerrors*5,
  320. str = '',
  321. errorint = setInterval(function(){
  322. if(i <= 0) {
  323. clearInterval(errorint);
  324. progress.removeClass('paused');
  325. do_import(id, options);
  326. str = Math.round(percentage)+'%';
  327. }else{
  328. progress.addClass('paused');
  329. str = '<span class="error">'+sprintf(mymailL10n.continues_in, (i--))+'</span>';
  330. }
  331. importstatus.html(sprintf(mymailL10n.import_contacts, str));
  332. }, 1000);
  333. }
  334. function get_stats(imported, errors, total, percentage, memoryusage) {
  335. var timepast = new Date().getTime()-importstarttime.getTime(),
  336. timeleft = Math.ceil(((100 - percentage) * (timepast/percentage))/60000);
  337. return sprintf(mymailL10n.current_stats, '<strong>'+imported+'</strong>', '<strong>'+total+'</strong>', '<strong>'+errors+'</strong>', '<strong>'+memoryusage+'</strong>')+'<br>'+
  338. sprintf(mymailL10n.estimate_time, timeleft);
  339. }
  340. function _ajax(action, data, callback, errorCallback){
  341. if($.isFunction(data)){
  342. if($.isFunction(callback)){
  343. errorCallback = callback;
  344. }
  345. callback = data;
  346. data = {};
  347. }
  348. $.ajax({
  349. type: 'POST',
  350. url: ajaxurl,
  351. data: $.extend({action: 'mymail_'+action, _wpnonce:wpnonce}, data),
  352. success: function(data, textStatus, jqXHR){
  353. callback && callback.call(this, data, textStatus, jqXHR);
  354. },
  355. error: function(jqXHR, textStatus, errorThrown){
  356. if(textStatus == 'error' && !errorThrown) return;
  357. if(console) console.error($.trim(jqXHR.responseText));
  358. errorCallback && errorCallback.call(this, jqXHR, textStatus, errorThrown);
  359. },
  360. dataType: "JSON"
  361. });
  362. }
  363. function sprintf() {
  364. var a = Array.prototype.slice.call(arguments),
  365. str = a.shift();
  366. while (a.length) str = str.replace('%s', a.shift());
  367. return str;
  368. }
  369. });