google.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. $(document).ready(function() {
  2. $('#externalStorage tbody tr.\\\\OC\\\\Files\\\\Storage\\\\Google').each(function(index, tr) {
  3. setupGoogleRow(tr);
  4. });
  5. $('#externalStorage').on('change', '#selectBackend', function() {
  6. if ($(this).val() == '\\OC\\Files\\Storage\\Google') {
  7. setupGoogleRow($('#externalStorage tbody>tr:last').prev('tr'));
  8. }
  9. });
  10. function setupGoogleRow(tr) {
  11. var configured = $(tr).find('[data-parameter="configured"]');
  12. if ($(configured).val() == 'true') {
  13. $(tr).find('.configuration').append('<span id="access" style="padding-left:0.5em;">'+t('files_external', 'Access granted')+'</span>');
  14. } else {
  15. var token = $(tr).find('[data-parameter="token"]');
  16. var token_secret = $(tr).find('[data-parameter="token_secret"]');
  17. var params = {};
  18. window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi, function(m, key, value) {
  19. params[key] = value;
  20. });
  21. if (params['oauth_token'] !== undefined && params['oauth_verifier'] !== undefined && decodeURIComponent(params['oauth_token']) == $(token).val()) {
  22. var statusSpan = $(tr).find('.status span');
  23. statusSpan.removeClass();
  24. statusSpan.addClass('waiting');
  25. $.post(OC.filePath('files_external', 'ajax', 'google.php'), { step: 2, oauth_verifier: params['oauth_verifier'], request_token: $(token).val(), request_token_secret: $(token_secret).val() }, function(result) {
  26. if (result && result.status == 'success') {
  27. $(token).val(result.access_token);
  28. $(token_secret).val(result.access_token_secret);
  29. $(configured).val('true');
  30. OC.MountConfig.saveStorage(tr);
  31. $(tr).find('.configuration').append('<span id="access" style="padding-left:0.5em;">'+t('files_external', 'Access granted')+'</span>');
  32. } else {
  33. OC.dialogs.alert(result.data.message, t('files_external', 'Error configuring Google Drive storage'));
  34. onGoogleInputsChange(tr);
  35. }
  36. });
  37. } else {
  38. onGoogleInputsChange(tr);
  39. }
  40. }
  41. }
  42. $('#externalStorage').on('paste', 'tbody tr.\\\\OC\\\\Files\\\\Storage\\\\Google td', function() {
  43. var tr = $(this).parent();
  44. setTimeout(function() {
  45. onGoogleInputsChange(tr);
  46. }, 20);
  47. });
  48. $('#externalStorage').on('keyup', 'tbody tr.\\\\OC\\\\Files\\\\Storage\\\\Google td', function() {
  49. onGoogleInputsChange($(this).parent());
  50. });
  51. $('#externalStorage').on('change', 'tbody tr.\\\\OC\\\\Files\\\\Storage\\\\Google .chzn-select', function() {
  52. onGoogleInputsChange($(this).parent().parent());
  53. });
  54. function onGoogleInputsChange(tr) {
  55. if ($(tr).find('[data-parameter="configured"]').val() != 'true') {
  56. var config = $(tr).find('.configuration');
  57. if ($(tr).find('.mountPoint input').val() != '' && ($(tr).find('.chzn-select').length == 0 || $(tr).find('.chzn-select').val() != null)) {
  58. if ($(tr).find('.google').length == 0) {
  59. $(config).append('<a class="button google">'+t('files_external', 'Grant access')+'</a>');
  60. } else {
  61. $(tr).find('.google').show();
  62. }
  63. } else if ($(tr).find('.google').length > 0) {
  64. $(tr).find('.google').hide();
  65. }
  66. }
  67. }
  68. $('#externalStorage').on('click', '.google', function(event) {
  69. event.preventDefault();
  70. var tr = $(this).parent().parent();
  71. var configured = $(this).parent().find('[data-parameter="configured"]');
  72. var token = $(this).parent().find('[data-parameter="token"]');
  73. var token_secret = $(this).parent().find('[data-parameter="token_secret"]');
  74. var statusSpan = $(tr).find('.status span');
  75. $.post(OC.filePath('files_external', 'ajax', 'google.php'), { step: 1, callback: location.protocol + '//' + location.host + location.pathname }, function(result) {
  76. if (result && result.status == 'success') {
  77. $(configured).val('false');
  78. $(token).val(result.data.request_token);
  79. $(token_secret).val(result.data.request_token_secret);
  80. OC.MountConfig.saveStorage(tr);
  81. statusSpan.removeClass();
  82. statusSpan.addClass('waiting');
  83. window.location = result.data.url;
  84. } else {
  85. OC.dialogs.alert(result.data.message, t('files_external', 'Error configuring Google Drive storage'));
  86. }
  87. });
  88. });
  89. });