wp-sweep.js
95 lines
| 1 | (function ($) { |
| 2 | 'use strict'; |
| 3 | |
| 4 | $(function () { |
| 5 | var $body = $('body'), |
| 6 | sweep = function($node) { |
| 7 | var $row = $node.parents('tr'); |
| 8 | |
| 9 | // Add Active |
| 10 | $body.addClass('sweep-active'); |
| 11 | // Add Disabled |
| 12 | $node.prop('disabled', true).text(wp_sweep.text_sweeping); |
| 13 | |
| 14 | return $.get(ajaxurl, { action: $node.data('action'), sweep_name: $node.data('sweep_name'), sweep_type: $node.data('sweep_type'), '_wpnonce': $node.data('nonce') }, function(data) { |
| 15 | if(data.success) { |
| 16 | var count = parseInt(data.data.count, 10); |
| 17 | // Count Col |
| 18 | $('.sweep-count', $row).text(count.toLocaleString()); |
| 19 | // % Of Col |
| 20 | $('.sweep-percentage', $row).text(data.data.percentage); |
| 21 | // Action Col |
| 22 | if(count === 0) { |
| 23 | $node.parent('td').html(wp_sweep.text_na); |
| 24 | } |
| 25 | // Stats |
| 26 | $.each(data.data.stats, function(key, value) { |
| 27 | $('.sweep-count-type-' + key).text(parseInt(value, 10).toLocaleString()); |
| 28 | }); |
| 29 | // Message |
| 30 | $row.parents('.table-sweep').prev('.sweep-message').html('<div class="updated"><p>' + data.data.sweep + '</p></div>'); |
| 31 | // Hide Sweep Details |
| 32 | $('.sweep-details', $row).html('').hide(); |
| 33 | // Remove Active |
| 34 | $body.removeClass('sweep-active'); |
| 35 | // Remove Disabled |
| 36 | $node.prop('disabled', false).text(wp_sweep.text_sweep); |
| 37 | } |
| 38 | }); |
| 39 | }; |
| 40 | |
| 41 | $('.btn-sweep').click(function(evt) { |
| 42 | evt.preventDefault(); |
| 43 | sweep($(this)); |
| 44 | }); |
| 45 | |
| 46 | $('.btn-sweep-details').click(function(evt) { |
| 47 | evt.preventDefault(); |
| 48 | var $node = $(this); |
| 49 | |
| 50 | $.get(ajaxurl, { action: $node.data('action'), sweep_name: $node.data('sweep_name'), sweep_type: $node.data('sweep_type'), '_wpnonce': $node.data('nonce') }, function(data) { |
| 51 | if(data.success) { |
| 52 | if(data.data.length > 0) { |
| 53 | var html = ''; |
| 54 | $.each(data.data, function(i, n) { |
| 55 | html += '<li>' + n + '</li>'; |
| 56 | }); |
| 57 | $('.sweep-details', $node.parents('tr')).html('<ol>' + html + '</ol>').show(); |
| 58 | } |
| 59 | } |
| 60 | }); |
| 61 | }); |
| 62 | |
| 63 | $('.btn-sweep-all').click(function(evt) { |
| 64 | evt.preventDefault(); |
| 65 | var $node = $(this), $btn_sweep = $('.btn-sweep'), sweep_all; |
| 66 | |
| 67 | $node.prop('disabled', true).text(wp_sweep.text_sweeping); |
| 68 | |
| 69 | sweep_all = $btn_sweep.toArray().reduce(function(current, next) { |
| 70 | return current.then(function() { |
| 71 | return sweep($(next)); |
| 72 | }); |
| 73 | }, $().promise()); |
| 74 | |
| 75 | sweep_all.done(function() { |
| 76 | // Remove Active |
| 77 | $body.removeClass('sweep-active'); |
| 78 | // Remove Disabled |
| 79 | $node.prop('disabled', false).text(wp_sweep.text_sweep_all); |
| 80 | }); |
| 81 | }); |
| 82 | |
| 83 | /* |
| 84 | Page closing confirmation |
| 85 | https://developer.mozilla.org/en-US/docs/DOM/Mozilla_event_reference/beforeunload |
| 86 | */ |
| 87 | $(window).on('beforeunload', function (e) { |
| 88 | if ($body.hasClass('sweep-active')) { |
| 89 | (e || window.event).returnValue = wp_sweep.text_close_warning; // Gecko and Trident |
| 90 | return wp_sweep.text_close_warning; // Gecko and WebKit |
| 91 | } |
| 92 | }); |
| 93 | }); |
| 94 | |
| 95 | })(jQuery); |