2017-09-21 20:09:36 +00:00
|
|
|
/*global jQuery, Backbone, _, woocommerce_admin_api_keys, wcSetClipboard, wcClearClipboard */
|
2015-06-08 22:41:35 +00:00
|
|
|
(function( $ ) {
|
|
|
|
|
|
|
|
var APIView = Backbone.View.extend({
|
2015-07-10 04:32:30 +00:00
|
|
|
/**
|
|
|
|
* Element
|
|
|
|
*
|
|
|
|
* @param {Object} '#key-fields'
|
|
|
|
*/
|
2015-06-08 22:41:35 +00:00
|
|
|
el: $( '#key-fields' ),
|
2015-07-10 04:32:30 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Events
|
|
|
|
*
|
|
|
|
* @type {Object}
|
|
|
|
*/
|
2015-06-08 22:41:35 +00:00
|
|
|
events: {
|
|
|
|
'click input#update_api_key': 'saveKey'
|
|
|
|
},
|
2015-07-10 04:32:30 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Initialize actions
|
|
|
|
*/
|
2015-06-08 22:41:35 +00:00
|
|
|
initialize: function(){
|
|
|
|
_.bindAll( this, 'saveKey' );
|
|
|
|
},
|
2015-07-10 04:32:30 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Init jQuery.BlockUI
|
|
|
|
*/
|
2015-06-08 22:41:35 +00:00
|
|
|
block: function() {
|
|
|
|
$( this.el ).block({
|
|
|
|
message: null,
|
|
|
|
overlayCSS: {
|
|
|
|
background: '#fff',
|
|
|
|
opacity: 0.6
|
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
2015-07-10 04:32:30 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove jQuery.BlockUI
|
|
|
|
*/
|
2015-06-08 22:41:35 +00:00
|
|
|
unblock: function() {
|
|
|
|
$( this.el ).unblock();
|
|
|
|
},
|
2015-07-10 04:32:30 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Init TipTip
|
|
|
|
*/
|
2016-03-02 18:13:17 +00:00
|
|
|
initTipTip: function( css_class ) {
|
2016-03-10 17:58:50 +00:00
|
|
|
$( document.body )
|
2017-09-21 20:09:36 +00:00
|
|
|
.on( 'click', css_class, function( evt ) {
|
|
|
|
evt.preventDefault();
|
2016-03-10 17:58:50 +00:00
|
|
|
if ( ! document.queryCommandSupported( 'copy' ) ) {
|
2021-02-04 16:08:25 +00:00
|
|
|
$( css_class ).parent().find( 'input' ).trigger( 'focus' ).trigger( 'select' );
|
2016-03-10 17:58:50 +00:00
|
|
|
$( '#copy-error' ).text( woocommerce_admin_api_keys.clipboard_failed );
|
2017-09-21 20:09:36 +00:00
|
|
|
} else {
|
|
|
|
$( '#copy-error' ).text( '' );
|
|
|
|
wcClearClipboard();
|
2021-01-07 22:15:15 +00:00
|
|
|
wcSetClipboard( $( this ).prev( 'input' ).val().trim(), $( css_class ) );
|
2016-03-10 17:58:50 +00:00
|
|
|
}
|
|
|
|
} )
|
2017-09-21 20:09:36 +00:00
|
|
|
.on( 'aftercopy', css_class, function() {
|
2016-03-02 18:13:17 +00:00
|
|
|
$( '#copy-error' ).text( '' );
|
2017-09-21 20:09:36 +00:00
|
|
|
$( css_class ).tipTip( {
|
|
|
|
'attribute': 'data-tip',
|
|
|
|
'activation': 'focus',
|
|
|
|
'fadeIn': 50,
|
|
|
|
'fadeOut': 50,
|
|
|
|
'delay': 0
|
2021-02-04 16:08:25 +00:00
|
|
|
} ).trigger( 'focus' );
|
2017-09-21 20:09:36 +00:00
|
|
|
} )
|
|
|
|
.on( 'aftercopyerror', css_class, function() {
|
2021-02-04 16:08:25 +00:00
|
|
|
$( css_class ).parent().find( 'input' ).trigger( 'focus' ).trigger( 'select' );
|
2017-09-21 20:09:36 +00:00
|
|
|
$( '#copy-error' ).text( woocommerce_admin_api_keys.clipboard_failed );
|
2016-03-10 17:58:50 +00:00
|
|
|
} );
|
2015-06-08 22:41:35 +00:00
|
|
|
},
|
2015-07-10 04:32:30 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Create qrcode
|
|
|
|
*
|
|
|
|
* @param {string} consumer_key
|
|
|
|
* @param {string} consumer_secret
|
|
|
|
*/
|
2015-06-08 22:41:35 +00:00
|
|
|
createQRCode: function( consumer_key, consumer_secret ) {
|
|
|
|
$( '#keys-qrcode' ).qrcode({
|
|
|
|
text: consumer_key + '|' + consumer_secret,
|
|
|
|
width: 120,
|
|
|
|
height: 120
|
|
|
|
});
|
|
|
|
},
|
2015-07-10 04:32:30 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Save API Key using ajax
|
|
|
|
*
|
|
|
|
* @param {Object} e
|
|
|
|
*/
|
2015-06-08 22:41:35 +00:00
|
|
|
saveKey: function( e ) {
|
|
|
|
e.preventDefault();
|
|
|
|
|
|
|
|
var self = this;
|
|
|
|
|
|
|
|
self.block();
|
|
|
|
|
|
|
|
Backbone.ajax({
|
|
|
|
method: 'POST',
|
|
|
|
dataType: 'json',
|
|
|
|
url: woocommerce_admin_api_keys.ajax_url,
|
|
|
|
data: {
|
|
|
|
action: 'woocommerce_update_api_key',
|
|
|
|
security: woocommerce_admin_api_keys.update_api_nonce,
|
|
|
|
key_id: $( '#key_id', self.el ).val(),
|
|
|
|
description: $( '#key_description', self.el ).val(),
|
|
|
|
user: $( '#key_user', self.el ).val(),
|
|
|
|
permissions: $( '#key_permissions', self.el ).val()
|
|
|
|
},
|
|
|
|
success: function( response ) {
|
|
|
|
$( '.wc-api-message', self.el ).remove();
|
|
|
|
|
|
|
|
if ( response.success ) {
|
|
|
|
var data = response.data;
|
|
|
|
|
2018-07-04 19:13:25 +00:00
|
|
|
$( 'h2, h3', self.el ).first().append( '<div class="wc-api-message updated"><p>' + data.message + '</p></div>' );
|
2015-06-08 22:41:35 +00:00
|
|
|
|
|
|
|
if ( 0 < data.consumer_key.length && 0 < data.consumer_secret.length ) {
|
2015-06-08 23:22:49 +00:00
|
|
|
$( '#api-keys-options', self.el ).remove();
|
|
|
|
$( 'p.submit', self.el ).empty().append( data.revoke_url );
|
2015-06-08 22:41:35 +00:00
|
|
|
|
2015-08-05 18:02:55 +00:00
|
|
|
var template = wp.template( 'api-keys-template' );
|
|
|
|
|
|
|
|
$( 'p.submit', self.el ).before( template({
|
2015-06-08 22:41:35 +00:00
|
|
|
consumer_key: data.consumer_key,
|
|
|
|
consumer_secret: data.consumer_secret
|
2015-08-05 18:02:55 +00:00
|
|
|
}) );
|
2015-06-08 22:41:35 +00:00
|
|
|
self.createQRCode( data.consumer_key, data.consumer_secret );
|
2016-03-02 18:13:17 +00:00
|
|
|
self.initTipTip( '.copy-key' );
|
|
|
|
self.initTipTip( '.copy-secret' );
|
2015-06-08 23:22:49 +00:00
|
|
|
} else {
|
|
|
|
$( '#key_description', self.el ).val( data.description );
|
|
|
|
$( '#key_user', self.el ).val( data.user_id );
|
|
|
|
$( '#key_permissions', self.el ).val( data.permissions );
|
2015-06-08 22:41:35 +00:00
|
|
|
}
|
|
|
|
} else {
|
2020-05-05 03:49:29 +00:00
|
|
|
$( 'h2, h3', self.el )
|
|
|
|
.first()
|
|
|
|
.append( '<div class="wc-api-message error"><p>' + response.data.message + '</p></div>' );
|
2015-06-08 22:41:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
self.unblock();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
new APIView();
|
|
|
|
|
|
|
|
})( jQuery );
|