You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

google.js 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. $(document).ready(function() {
  2. $('#externalStorage tbody tr.\\\\OC\\\\Files\\\\Storage\\\\Google').each(function() {
  3. var configured = $(this).find('[data-parameter="configured"]');
  4. if ($(configured).val() == 'true') {
  5. $(this).find('.configuration input').attr('disabled', 'disabled');
  6. $(this).find('.configuration').append($('<span/>').attr('id', 'access')
  7. .text(t('files_external', 'Access granted')));
  8. } else {
  9. var client_id = $(this).find('.configuration [data-parameter="client_id"]').val();
  10. var client_secret = $(this).find('.configuration [data-parameter="client_secret"]')
  11. .val();
  12. if (client_id != '' && client_secret != '') {
  13. var params = {};
  14. window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi, function(m, key, value) {
  15. params[key] = value;
  16. });
  17. if (params['code'] !== undefined) {
  18. var tr = $(this);
  19. var token = $(this).find('.configuration [data-parameter="token"]');
  20. var statusSpan = $(tr).find('.status span');
  21. statusSpan.removeClass();
  22. statusSpan.addClass('waiting');
  23. $.post(OC.filePath('files_external', 'ajax', 'google.php'),
  24. {
  25. step: 2,
  26. client_id: client_id,
  27. client_secret: client_secret,
  28. redirect: location.protocol + '//' + location.host + location.pathname,
  29. code: params['code'],
  30. }, function(result) {
  31. if (result && result.status == 'success') {
  32. $(token).val(result.data.token);
  33. $(configured).val('true');
  34. OC.MountConfig.saveStorage(tr);
  35. $(tr).find('.configuration input').attr('disabled', 'disabled');
  36. $(tr).find('.configuration').append($('<span/>')
  37. .attr('id', 'access')
  38. .text(t('files_external', 'Access granted')));
  39. } else {
  40. OC.dialogs.alert(result.data.message,
  41. t('files_external', 'Error configuring Google Drive storage')
  42. );
  43. }
  44. }
  45. );
  46. }
  47. } else {
  48. onGoogleInputsChange($(this));
  49. }
  50. }
  51. });
  52. $('#externalStorage').on('paste', 'tbody tr.\\\\OC\\\\Files\\\\Storage\\\\Google td',
  53. function() {
  54. var tr = $(this).parent();
  55. setTimeout(function() {
  56. onGoogleInputsChange(tr);
  57. }, 20);
  58. }
  59. );
  60. $('#externalStorage').on('keyup', 'tbody tr.\\\\OC\\\\Files\\\\Storage\\\\Google td',
  61. function() {
  62. onGoogleInputsChange($(this).parent());
  63. }
  64. );
  65. $('#externalStorage').on('change', 'tbody tr.\\\\OC\\\\Files\\\\Storage\\\\Google .chzn-select'
  66. , function() {
  67. onGoogleInputsChange($(this).parent().parent());
  68. }
  69. );
  70. function onGoogleInputsChange(tr) {
  71. if ($(tr).find('[data-parameter="configured"]').val() != 'true') {
  72. var config = $(tr).find('.configuration');
  73. if ($(tr).find('.mountPoint input').val() != ''
  74. && $(config).find('[data-parameter="client_id"]').val() != ''
  75. && $(config).find('[data-parameter="client_secret"]').val() != ''
  76. && ($(tr).find('.chzn-select').length == 0
  77. || $(tr).find('.chzn-select').val() != null))
  78. {
  79. if ($(tr).find('.google').length == 0) {
  80. $(config).append($('<a/>').addClass('button google')
  81. .text(t('files_external', 'Grant access')));
  82. } else {
  83. $(tr).find('.google').show();
  84. }
  85. } else if ($(tr).find('.google').length > 0) {
  86. $(tr).find('.google').hide();
  87. }
  88. }
  89. }
  90. $('#externalStorage').on('click', '.google', function(event) {
  91. event.preventDefault();
  92. var tr = $(this).parent().parent();
  93. var configured = $(this).parent().find('[data-parameter="configured"]');
  94. var client_id = $(this).parent().find('[data-parameter="client_id"]').val();
  95. var client_secret = $(this).parent().find('[data-parameter="client_secret"]').val();
  96. var statusSpan = $(tr).find('.status span');
  97. if (client_id != '' && client_secret != '') {
  98. var token = $(this).parent().find('[data-parameter="token"]');
  99. $.post(OC.filePath('files_external', 'ajax', 'google.php'),
  100. {
  101. step: 1,
  102. client_id: client_id,
  103. client_secret: client_secret,
  104. redirect: location.protocol + '//' + location.host + location.pathname,
  105. }, function(result) {
  106. if (result && result.status == 'success') {
  107. $(configured).val('false');
  108. $(token).val('false');
  109. OC.MountConfig.saveStorage(tr);
  110. statusSpan.removeClass();
  111. statusSpan.addClass('waiting');
  112. window.location = result.data.url;
  113. } else {
  114. OC.dialogs.alert(result.data.message,
  115. t('files_external', 'Error configuring Google Drive storage')
  116. );
  117. }
  118. }
  119. );
  120. }
  121. });
  122. });