api-keys.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /*global jQuery, Backbone, _, woocommerce_admin_api_keys, wcSetClipboard, wcClearClipboard */
  2. (function( $ ) {
  3. var APIView = Backbone.View.extend({
  4. /**
  5. * Element
  6. *
  7. * @param {Object} '#key-fields'
  8. */
  9. el: $( '#key-fields' ),
  10. /**
  11. * Events
  12. *
  13. * @type {Object}
  14. */
  15. events: {
  16. 'click input#update_api_key': 'saveKey'
  17. },
  18. /**
  19. * Initialize actions
  20. */
  21. initialize: function(){
  22. _.bindAll( this, 'saveKey' );
  23. },
  24. /**
  25. * Init jQuery.BlockUI
  26. */
  27. block: function() {
  28. $( this.el ).block({
  29. message: null,
  30. overlayCSS: {
  31. background: '#fff',
  32. opacity: 0.6
  33. }
  34. });
  35. },
  36. /**
  37. * Remove jQuery.BlockUI
  38. */
  39. unblock: function() {
  40. $( this.el ).unblock();
  41. },
  42. /**
  43. * Init TipTip
  44. */
  45. initTipTip: function( css_class ) {
  46. $( document.body )
  47. .on( 'click', css_class, function( evt ) {
  48. evt.preventDefault();
  49. if ( ! document.queryCommandSupported( 'copy' ) ) {
  50. $( css_class ).parent().find( 'input' ).focus().select();
  51. $( '#copy-error' ).text( woocommerce_admin_api_keys.clipboard_failed );
  52. } else {
  53. $( '#copy-error' ).text( '' );
  54. wcClearClipboard();
  55. wcSetClipboard( $.trim( $( this ).prev( 'input' ).val() ), $( css_class ) );
  56. }
  57. } )
  58. .on( 'aftercopy', css_class, function() {
  59. $( '#copy-error' ).text( '' );
  60. $( css_class ).tipTip( {
  61. 'attribute': 'data-tip',
  62. 'activation': 'focus',
  63. 'fadeIn': 50,
  64. 'fadeOut': 50,
  65. 'delay': 0
  66. } ).focus();
  67. } )
  68. .on( 'aftercopyerror', css_class, function() {
  69. $( css_class ).parent().find( 'input' ).focus().select();
  70. $( '#copy-error' ).text( woocommerce_admin_api_keys.clipboard_failed );
  71. } );
  72. },
  73. /**
  74. * Create qrcode
  75. *
  76. * @param {string} consumer_key
  77. * @param {string} consumer_secret
  78. */
  79. createQRCode: function( consumer_key, consumer_secret ) {
  80. $( '#keys-qrcode' ).qrcode({
  81. text: consumer_key + '|' + consumer_secret,
  82. width: 120,
  83. height: 120
  84. });
  85. },
  86. /**
  87. * Save API Key using ajax
  88. *
  89. * @param {Object} e
  90. */
  91. saveKey: function( e ) {
  92. e.preventDefault();
  93. var self = this;
  94. self.block();
  95. Backbone.ajax({
  96. method: 'POST',
  97. dataType: 'json',
  98. url: woocommerce_admin_api_keys.ajax_url,
  99. data: {
  100. action: 'woocommerce_update_api_key',
  101. security: woocommerce_admin_api_keys.update_api_nonce,
  102. key_id: $( '#key_id', self.el ).val(),
  103. description: $( '#key_description', self.el ).val(),
  104. user: $( '#key_user', self.el ).val(),
  105. permissions: $( '#key_permissions', self.el ).val()
  106. },
  107. success: function( response ) {
  108. $( '.wc-api-message', self.el ).remove();
  109. if ( response.success ) {
  110. var data = response.data;
  111. $( 'h2, h3', self.el ).first().append( '<div class="wc-api-message updated"><p>' + data.message + '</p></div>' );
  112. if ( 0 < data.consumer_key.length && 0 < data.consumer_secret.length ) {
  113. $( '#api-keys-options', self.el ).remove();
  114. $( 'p.submit', self.el ).empty().append( data.revoke_url );
  115. var template = wp.template( 'api-keys-template' );
  116. $( 'p.submit', self.el ).before( template({
  117. consumer_key: data.consumer_key,
  118. consumer_secret: data.consumer_secret
  119. }) );
  120. self.createQRCode( data.consumer_key, data.consumer_secret );
  121. self.initTipTip( '.copy-key' );
  122. self.initTipTip( '.copy-secret' );
  123. } else {
  124. $( '#key_description', self.el ).val( data.description );
  125. $( '#key_user', self.el ).val( data.user_id );
  126. $( '#key_permissions', self.el ).val( data.permissions );
  127. }
  128. } else {
  129. $( 'h3', self.el ).first().append( '<div class="wc-api-message error"><p>' + response.data.message + '</p></div>' );
  130. }
  131. self.unblock();
  132. }
  133. });
  134. }
  135. });
  136. new APIView();
  137. })( jQuery );