| 1 |
(function (window, document, $, l, undefined) { |
| 2 |
$.fn.getAttributes = function () { |
| 3 |
var attributes = {}; |
| 4 |
|
| 5 |
if (this.length) { |
| 6 |
$.each(this[0].attributes, function (index, attr) { |
| 7 |
attributes[attr.name] = attr.value; |
| 8 |
}); |
| 9 |
} |
| 10 |
|
| 11 |
return attributes; |
| 12 |
}; |
| 13 |
|
| 14 |
$(document).ready(function () { |
| 15 |
$('#extensions-wrapper').detach().appendTo('#cmb2-metabox-upstream_extensions'); |
| 16 |
$('div[data-fieldtype="upstream_extensions_wrapper"]').remove(); |
| 17 |
|
| 18 |
$('#extensions-wrapper > .nav-tab-wrapper > a.nav-tab').on('click', function (e) { |
| 19 |
e.preventDefault(); |
| 20 |
|
| 21 |
var self = $(this); |
| 22 |
|
| 23 |
if (self.prev().length === 0) { |
| 24 |
self.next().removeClass('nav-tab-active'); |
| 25 |
self.addClass('nav-tab-active'); |
| 26 |
|
| 27 |
$('#installed-extensions-list').css('display', 'flex'); |
| 28 |
$('#non-installed-extensions-list').css('display', 'none'); |
| 29 |
} else { |
| 30 |
self.prev().removeClass('nav-tab-active'); |
| 31 |
self.addClass('nav-tab-active'); |
| 32 |
|
| 33 |
$('#installed-extensions-list').css('display', 'none'); |
| 34 |
$('#non-installed-extensions-list').css('display', 'flex'); |
| 35 |
} |
| 36 |
}); |
| 37 |
|
| 38 |
$('#installed-extensions-list').on('click', '[data-action="upstream:extension:activate"]', function (e) { |
| 39 |
e.preventDefault(); |
| 40 |
|
| 41 |
var self = $(this); |
| 42 |
if (self.attr('disabled') === 'disabled') { |
| 43 |
return; |
| 44 |
} |
| 45 |
|
| 46 |
var wrapper = $(self.parents('article[data-id]')); |
| 47 |
|
| 48 |
var licenseKeyField = $('input', wrapper); |
| 49 |
licenseKeyField.removeClass('has-error'); |
| 50 |
var licenseKey = licenseKeyField.val().trim(); |
| 51 |
if (licenseKey.length === 0) { |
| 52 |
licenseKeyField.focus(); |
| 53 |
return; |
| 54 |
} |
| 55 |
|
| 56 |
self.attr('disabled', 'disabled'); |
| 57 |
|
| 58 |
var btnAttrs = self.getAttributes(); |
| 59 |
|
| 60 |
$.ajax({ |
| 61 |
type: 'POST', |
| 62 |
url: ajaxurl, |
| 63 |
data: { |
| 64 |
action: 'upstream:extensions:license.validate', |
| 65 |
key: licenseKey, |
| 66 |
extension: wrapper.attr('data-id'), |
| 67 |
nonce: $('#upstream-extensions-nonce').val() |
| 68 |
}, |
| 69 |
beforeSend: function (jqXHR, settings) { |
| 70 |
self.attr('disabled', 'disabled'); |
| 71 |
$('input', wrapper).attr('disabled', 'disabled'); |
| 72 |
self.replaceWith($('<div></div>', btnAttrs)); |
| 73 |
|
| 74 |
$('[data-action="upstream:extension:activate"]', wrapper).append('<div class="u-loader"></div>'); |
| 75 |
}, |
| 76 |
success: function (response, textStatus, jqXHR) { |
| 77 |
$('[data-action="upstream:extension:activate"]', wrapper).replaceWith($('<button></button>', btnAttrs)); |
| 78 |
$('[data-action="upstream:extension:activate"]', wrapper).text(l['LB_ACTIVATE']).attr('disabled', null); |
| 79 |
$('input', wrapper).attr('disabled', null); |
| 80 |
|
| 81 |
if (!response.success) { |
| 82 |
alert(response.err); |
| 83 |
} else { |
| 84 |
if (response.message) { |
| 85 |
$('.u-license-status', wrapper).text(response.message); |
| 86 |
wrapper.attr('class', 'license-' + response.license_status); |
| 87 |
} |
| 88 |
|
| 89 |
if (response.license_status === 'invalid') { |
| 90 |
licenseKeyField.focus(); |
| 91 |
} else { |
| 92 |
$('.u-license', wrapper).html('<strong>License Key:</strong> <code>' + response.license_key + '</code> - <a href="#" data-action="upstream:extension:change">' + l['LB_CHANGE'] + '</a>'); |
| 93 |
$('.u-license-status', wrapper).html($('.u-license-status', wrapper).html() + ' - <a href="#" data-action="upstream:extension:deactivate">' + l['LB_DEACTIVATE'] + '</a>'); |
| 94 |
} |
| 95 |
} |
| 96 |
}, |
| 97 |
error: function (jqXHR, textStatus, errorThrown) { |
| 98 |
console.error(errorThrown); |
| 99 |
|
| 100 |
$('[data-action="upstream:extension:activate"]', wrapper).replaceWith($('<button></button>', btnAttrs)); |
| 101 |
$('[data-action="upstream:extension:activate"]', wrapper).text('Activate').attr('disabled', null); |
| 102 |
$('input', wrapper).attr('disabled', null); |
| 103 |
} |
| 104 |
}); |
| 105 |
}); |
| 106 |
|
| 107 |
$('#installed-extensions-list').on('click', '[data-action="upstream:extension:change"]', function (e) { |
| 108 |
e.preventDefault(); |
| 109 |
|
| 110 |
var self = $(this); |
| 111 |
var wrapper = $(self.parents('article[data-id]')); |
| 112 |
|
| 113 |
$('.u-license', wrapper).html('<input type="text" size="40" placeholder="' + l['MSG_ENTER_LICENSE'] + '" maxlength="32" autocomplete="false"><button type="button" class="button button-primary" data-action="upstream:extension:activate">' + l['LB_ACTIVATE'] + '</button>'); |
| 114 |
$('.u-license-status', wrapper).text(l['MSG_INACTIVE_LICENSE']); |
| 115 |
wrapper.attr('class', 'license-inactive'); |
| 116 |
}); |
| 117 |
|
| 118 |
$('#installed-extensions-list').on('click', '[data-action="upstream:extension:deactivate"]', function (e) { |
| 119 |
e.preventDefault(); |
| 120 |
|
| 121 |
var self = $(this); |
| 122 |
var wrapper = $(self.parents('article[data-id]')); |
| 123 |
|
| 124 |
if (self.attr('disabled') === 'disabled') { |
| 125 |
return; |
| 126 |
} |
| 127 |
|
| 128 |
$.ajax({ |
| 129 |
type: 'POST', |
| 130 |
url: ajaxurl, |
| 131 |
data: { |
| 132 |
action: 'upstream:extensions:license.deactivate', |
| 133 |
extension: wrapper.attr('data-id'), |
| 134 |
nonce: $('#upstream-extensions-nonce').val() |
| 135 |
}, |
| 136 |
beforeSend: function (jqXHR, settings) { |
| 137 |
self.text(l['LB_DEACTIVATING']).attr('disabled', 'disabled'); |
| 138 |
}, |
| 139 |
success: function (response, textStatus, jqXHR) { |
| 140 |
if (!response.success) { |
| 141 |
self.text(l['LB_DEACTIVATE']).attr('disabled', null); |
| 142 |
alert(response.err); |
| 143 |
} else { |
| 144 |
$('.u-license', wrapper).html('<input type="text" size="40" placeholder="' + l['MSG_ENTER_LICENSE'] + '" maxlength="32" autocomplete="false"><button type="button" class="button button-primary" data-action="upstream:extension:activate">' + l['LB_ACTIVATE'] + '</button>'); |
| 145 |
$('.u-license-status', wrapper).text(l['MSG_INACTIVE_LICENSE']); |
| 146 |
wrapper.attr('class', 'license-inactive'); |
| 147 |
} |
| 148 |
}, |
| 149 |
error: function (jqXHR, textStatus, errorThrown) { |
| 150 |
self.text(l['LB_DEACTIVATE']).attr('disabled', null); |
| 151 |
|
| 152 |
console.error(errorThrown); |
| 153 |
} |
| 154 |
}); |
| 155 |
}); |
| 156 |
}); |
| 157 |
})(window, window.document, jQuery, upstreamExtensionsLang); |
| 158 |
upstreamExtensionsLang = null; |
| 159 |
|