1
0

google.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. OCA.External.Settings.mountConfig.saveStorageConfig(tr, function(status) {
  35. if (status) {
  36. $(tr).find('.configuration input').attr('disabled', 'disabled');
  37. $(tr).find('.configuration').append($('<span/>')
  38. .attr('id', 'access')
  39. .text(t('files_external', 'Access granted')));
  40. }
  41. });
  42. } else {
  43. OC.dialogs.alert(result.data.message,
  44. t('files_external', 'Error configuring Google Drive storage')
  45. );
  46. }
  47. }
  48. );
  49. }
  50. } else {
  51. onGoogleInputsChange($(this));
  52. }
  53. }
  54. });
  55. $('#externalStorage').on('paste', 'tbody tr.\\\\OC\\\\Files\\\\Storage\\\\Google td',
  56. function() {
  57. var tr = $(this).parent();
  58. setTimeout(function() {
  59. onGoogleInputsChange(tr);
  60. }, 20);
  61. }
  62. );
  63. $('#externalStorage').on('keyup', 'tbody tr.\\\\OC\\\\Files\\\\Storage\\\\Google td',
  64. function() {
  65. onGoogleInputsChange($(this).parent());
  66. }
  67. );
  68. $('#externalStorage').on('change', 'tbody tr.\\\\OC\\\\Files\\\\Storage\\\\Google .chzn-select'
  69. , function() {
  70. onGoogleInputsChange($(this).parent().parent());
  71. }
  72. );
  73. function onGoogleInputsChange(tr) {
  74. if ($(tr).find('[data-parameter="configured"]').val() != 'true') {
  75. var config = $(tr).find('.configuration');
  76. if ($(tr).find('.mountPoint input').val() != ''
  77. && $(config).find('[data-parameter="client_id"]').val() != ''
  78. && $(config).find('[data-parameter="client_secret"]').val() != ''
  79. && ($(tr).find('.chzn-select').length == 0
  80. || $(tr).find('.chzn-select').val() != null))
  81. {
  82. if ($(tr).find('.google').length == 0) {
  83. $(config).append($(document.createElement('input')).addClass('button google')
  84. .attr('type', 'button')
  85. .attr('value', t('files_external', 'Grant access')));
  86. } else {
  87. $(tr).find('.google').show();
  88. }
  89. } else if ($(tr).find('.google').length > 0) {
  90. $(tr).find('.google').hide();
  91. }
  92. }
  93. }
  94. $('#externalStorage').on('click', '.google', function(event) {
  95. event.preventDefault();
  96. var tr = $(this).parent().parent();
  97. var configured = $(this).parent().find('[data-parameter="configured"]');
  98. var client_id = $(this).parent().find('[data-parameter="client_id"]').val();
  99. var client_secret = $(this).parent().find('[data-parameter="client_secret"]').val();
  100. if (client_id != '' && client_secret != '') {
  101. var token = $(this).parent().find('[data-parameter="token"]');
  102. $.post(OC.filePath('files_external', 'ajax', 'google.php'),
  103. {
  104. step: 1,
  105. client_id: client_id,
  106. client_secret: client_secret,
  107. redirect: location.protocol + '//' + location.host + location.pathname,
  108. }, function(result) {
  109. if (result && result.status == 'success') {
  110. $(configured).val('false');
  111. $(token).val('false');
  112. OCA.External.Settings.mountConfig.saveStorageConfig(tr, function(status) {
  113. window.location = result.data.url;
  114. });
  115. } else {
  116. OC.dialogs.alert(result.data.message,
  117. t('files_external', 'Error configuring Google Drive storage')
  118. );
  119. }
  120. }
  121. );
  122. }
  123. });
  124. });