diff --git a/public/res/html/dialogAutoSyncGdrive.html b/public/res/html/dialogAutoSyncGdrive.html
new file mode 100644
index 00000000..f840bc60
--- /dev/null
+++ b/public/res/html/dialogAutoSyncGdrive.html
@@ -0,0 +1,57 @@
+
+
+
+
+
+
+ AutoSync feature automatically exports every new document to your
+ Google Drive
+ account and keep it synchronized.
+
+
+
+ NOTE: Removing a local document will not delete the linked file on Google
+ Drive.
+
+
+
+
+
diff --git a/public/res/html/dialogExportGdrive.html b/public/res/html/dialogExportGdrive.html
index 4d97a24c..841b7c00 100644
--- a/public/res/html/dialogExportGdrive.html
+++ b/public/res/html/dialogExportGdrive.html
@@ -10,7 +10,7 @@
This will save "" to your
- <%= providerName %>
+ Google Drive
account and keep it synchronized.
@@ -23,7 +23,7 @@
diff --git a/public/res/providers/gdriveProviderBuilder.js b/public/res/providers/gdriveProviderBuilder.js
index d2b51cfa..f550fe1a 100644
--- a/public/res/providers/gdriveProviderBuilder.js
+++ b/public/res/providers/gdriveProviderBuilder.js
@@ -11,8 +11,9 @@ define([
"eventMgr",
"fileMgr",
"helpers/googleHelper",
- "text!html/dialogExportGdrive.html"
-], function($, _, constants, utils, storage, logger, Provider, settings, eventMgr, fileMgr, googleHelper, dialogExportGdriveHTML) {
+ "text!html/dialogExportGdrive.html",
+ "text!html/dialogAutoSyncGdrive.html",
+], function($, _, constants, utils, storage, logger, Provider, settings, eventMgr, fileMgr, googleHelper, dialogExportGdriveHTML, dialogAutoSyncGdriveHTML) {
return function(providerId, providerName, accountIndex) {
var accountId = 'google.gdrive' + accountIndex;
@@ -474,6 +475,34 @@ define([
}
};
+ // Initialize the AutoSync dialog fields
+ gdriveProvider.setAutosyncDialogConfig = function() {
+ var config = gdriveProvider.autosyncConfig;
+ utils.setInputChecked('#input-autosync-' + providerId + '-enabled', config.enabled);
+ utils.setInputValue('#input-autosync-' + providerId + '-parentid', config.parentId);
+ };
+
+ // Retrieve the AutoSync dialog fields
+ gdriveProvider.getAutosyncDialogConfig = function() {
+ var config = {};
+ config.enabled = utils.getInputChecked('#input-autosync-' + providerId + '-enabled');
+ config.parentId = utils.getInputTextValue('#input-autosync-' + providerId + '-parentid');
+ return config;
+ };
+
+ // Perform AutoSync
+ gdriveProvider.autosyncFile = function(title, content, config, callback) {
+ var parentId = config.parentId;
+ googleHelper.upload(undefined, parentId, title, content, undefined, undefined, accountId, function(error, result) {
+ if(error) {
+ callback(error);
+ return;
+ }
+ var syncAttributes = createSyncAttributes(result.id, result.etag, content, title);
+ callback(undefined, syncAttributes);
+ });
+ };
+
// Disable publish on optional multi-account
gdriveProvider.isPublishEnabled = settings.gdriveMultiAccount > accountIndex;
@@ -488,8 +517,15 @@ define([
providerName: providerName
}));
+ // Create autosync dialog
+ var modalAutosyncElt = document.querySelector('.modal-autosync-' + providerId);
+ modalAutosyncElt && (modalAutosyncElt.innerHTML = _.template(dialogAutoSyncGdriveHTML, {
+ providerId: providerId,
+ providerName: providerName
+ }));
+
// Choose folder button in export modal
- $('.export-' + providerId + '-choose-folder').click(function() {
+ $('.action-export-' + providerId + '-choose-folder').click(function() {
googleHelper.picker(function(error, docs) {
if(error || docs.length === 0) {
return;
@@ -501,6 +537,19 @@ define([
}, 'folder', accountId);
});
+ // Choose folder button in autosync modal
+ $('.action-autosync-' + providerId + '-choose-folder').click(function() {
+ googleHelper.picker(function(error, docs) {
+ if(error || docs.length === 0) {
+ return;
+ }
+ // Open export dialog
+ $(".modal-autosync-" + providerId).modal();
+ // Set parent ID
+ utils.setInputValue('#input-autosync-' + providerId + '-parentid', docs[0].id);
+ }, 'folder', accountId);
+ });
+
// On export, disable file ID input if realtime is checked
var $realtimeCheckboxElt = $('#input-sync-export-' + providerId + '-realtime');
var $fileIdInputElt = $('#input-sync-export-' + providerId + '-fileid');
diff --git a/public/res/synchronizer.js b/public/res/synchronizer.js
index 44897bcb..e1c79c8c 100644
--- a/public/res/synchronizer.js
+++ b/public/res/synchronizer.js
@@ -48,6 +48,11 @@ define([
});
});
+ // AutoSync configuration
+ _.each(providerMap, function(provider) {
+ provider.autosyncConfig = utils.retrieveIgnoreError(provider.providerId + ".autosyncConfig") || {};
+ });
+
// Returns true if at least one file has synchronized location
synchronizer.hasSync = function(provider) {
return _.some(fileSystem, function(fileDesc) {
@@ -292,10 +297,24 @@ define([
});
}
- // Open dialog box
+ // Open dialog
$(".modal-upload-" + provider.providerId).modal();
}
+ eventMgr.addListener("onFileCreated", function(fileDesc) {
+ if(_.size(fileDesc.syncLocations) === 0) {
+ _.each(providerMap, function(provider) {
+ provider.autosyncConfig.enabled && provider.autosyncFile(fileDesc.title, fileDesc.content, provider.autosyncConfig, function(error, syncAttributes) {
+ if(error) {
+ return;
+ }
+ fileDesc.addSyncLocation(syncAttributes);
+ eventMgr.onSyncExportSuccess(fileDesc, syncAttributes);
+ });
+ });
+ }
+ });
+
eventMgr.addListener("onReady", function() {
// Init each provider
_.each(providerMap, function(provider) {
@@ -307,6 +326,15 @@ define([
$(".action-sync-export-dialog-" + provider.providerId).click(function() {
initExportDialog(provider);
});
+ // Provider's autosync action
+ $(".action-autosync-dialog-" + provider.providerId).click(function() {
+ // Reset fields
+ utils.resetModalInputs();
+ // Load config
+ provider.setAutosyncDialogConfig(provider);
+ // Open dialog
+ $(".modal-autosync-" + provider.providerId).modal();
+ });
$(".action-sync-export-" + provider.providerId).click(function(event) {
var isRealtime = utils.getInputChecked("#input-sync-export-" + provider.providerId + "-realtime");
var fileDesc = fileMgr.currentFile;
@@ -360,6 +388,13 @@ define([
});
storage[provider.providerId + ".exportPreferences"] = JSON.stringify(exportPreferences);
});
+ $(".action-autosync-" + provider.providerId).click(function(event) {
+ var config = provider.getAutosyncDialogConfig(event);
+ if(config !== undefined) {
+ storage[provider.providerId + ".autosyncConfig"] = JSON.stringify(config);
+ provider.autosyncConfig = config;
+ }
+ });
});
});