google.js 3.6 KB

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