Stackedit/public/res/providers/gdriveProviderBuilder.js

670 lines
32 KiB
JavaScript
Raw Normal View History

2013-12-23 22:33:33 +00:00
/*global gapi */
define([
"jquery",
"underscore",
"constants",
"utils",
"storage",
"logger",
"classes/Provider",
"settings",
"eventMgr",
"fileMgr",
"helpers/googleHelper",
"text!html/dialogExportGdrive.html",
"text!html/dialogAutoSyncGdrive.html",
], function($, _, constants, utils, storage, logger, Provider, settings, eventMgr, fileMgr, googleHelper, dialogExportGdriveHTML, dialogAutoSyncGdriveHTML) {
2014-03-20 00:24:56 +00:00
2013-12-23 22:33:33 +00:00
return function(providerId, providerName, accountIndex) {
var accountId = 'google.gdrive' + accountIndex;
2014-03-20 00:24:56 +00:00
2013-12-23 22:33:33 +00:00
var gdriveProvider = new Provider(providerId, providerName);
gdriveProvider.defaultPublishFormat = "template";
gdriveProvider.exportPreferencesInputIds = [
providerId + "-parentid",
providerId + "-realtime",
];
2014-03-20 00:24:56 +00:00
2013-12-23 22:33:33 +00:00
function createSyncIndex(id) {
return "sync." + providerId + "." + id;
}
2014-03-20 00:24:56 +00:00
2013-12-23 22:33:33 +00:00
function createSyncAttributes(id, etag, content, title) {
var syncAttributes = {};
syncAttributes.provider = gdriveProvider;
syncAttributes.id = id;
syncAttributes.etag = etag;
syncAttributes.contentCRC = utils.crc32(content);
syncAttributes.titleCRC = utils.crc32(title);
syncAttributes.syncIndex = createSyncIndex(id);
return syncAttributes;
}
2014-03-20 00:24:56 +00:00
2013-12-23 22:33:33 +00:00
function importFilesFromIds(ids) {
googleHelper.downloadMetadata(ids, accountId, function(error, result) {
if(error) {
return;
}
googleHelper.downloadContent(result, accountId, function(error, result) {
if(error) {
return;
}
var fileDescList = [];
var fileDesc;
_.each(result, function(file) {
var syncAttributes = createSyncAttributes(file.id, file.etag, file.content, file.title);
syncAttributes.isRealtime = file.isRealtime;
var syncLocations = {};
syncLocations[syncAttributes.syncIndex] = syncAttributes;
2014-03-30 01:44:51 +00:00
var parsedContent = gdriveProvider.parseSerializedContent(file.content);
fileDesc = fileMgr.createFile(file.title, parsedContent.content, parsedContent.discussionList, syncLocations);
2013-12-23 22:33:33 +00:00
fileDescList.push(fileDesc);
});
if(fileDesc !== undefined) {
eventMgr.onSyncImportSuccess(fileDescList, gdriveProvider);
fileMgr.selectFile(fileDesc);
}
});
});
}
2014-03-20 00:24:56 +00:00
2013-12-23 22:33:33 +00:00
gdriveProvider.importFiles = function() {
googleHelper.picker(function(error, docs) {
if(error || docs.length === 0) {
return;
}
var importIds = [];
_.each(docs, function(doc) {
var syncIndex = createSyncIndex(doc.id);
var fileDesc = fileMgr.getFileFromSyncIndex(syncIndex);
if(fileDesc !== undefined) {
eventMgr.onError('"' + fileDesc.title + '" was already imported.');
return;
}
importIds.push(doc.id);
});
importFilesFromIds(importIds);
}, 'doc', accountId);
};
2014-03-20 00:24:56 +00:00
2013-12-23 22:33:33 +00:00
gdriveProvider.exportFile = function(event, title, content, callback) {
var fileId = utils.getInputTextValue('#input-sync-export-' + providerId + '-fileid');
if(fileId) {
// Check that file is not synchronized with another an existing
// document
var syncIndex = createSyncIndex(fileId);
var fileDesc = fileMgr.getFileFromSyncIndex(syncIndex);
if(fileDesc !== undefined) {
eventMgr.onError('File ID is already synchronized with "' + fileDesc.title + '".');
callback(true);
return;
}
}
var parentId = utils.getInputTextValue('#input-sync-export-' + providerId + '-parentid');
googleHelper.upload(fileId, 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);
});
};
2014-03-20 00:24:56 +00:00
2013-12-23 22:33:33 +00:00
gdriveProvider.exportRealtimeFile = function(event, title, content, callback) {
var parentId = utils.getInputTextValue('#input-sync-export-' + providerId + '-parentid');
googleHelper.createRealtimeFile(parentId, title, accountId, function(error, result) {
if(error) {
callback(error);
return;
}
var syncAttributes = createSyncAttributes(result.id, result.etag, content, title);
callback(undefined, syncAttributes);
});
};
2014-03-20 00:24:56 +00:00
2014-03-28 00:49:49 +00:00
var merge = settings.conflictMode == 'merge';
gdriveProvider.syncUp = function(content, contentCRC, title, titleCRC, discussionList, discussionListCRC, syncAttributes, callback) {
if(
(syncAttributes.contentCRC == contentCRC) && // Content CRC hasn't changed
(syncAttributes.titleCRC == titleCRC) && // Content CRC hasn't changed
(syncAttributes.discussionListCRC == discussionListCRC) // Discussion list CRC hasn't changed
) {
return callback(undefined, false);
2013-12-23 22:33:33 +00:00
}
2014-03-28 00:49:49 +00:00
var uploadedContent = gdriveProvider.serializeContent(content, discussionList);
googleHelper.upload(syncAttributes.id, undefined, title, uploadedContent, undefined, syncAttributes.etag, accountId, function(error, result) {
2013-12-23 22:33:33 +00:00
if(error) {
callback(error, true);
return;
}
syncAttributes.etag = result.etag;
2014-03-28 00:49:49 +00:00
if(merge === true) {
// Need to store the whole content for merge
syncAttributes.content = content;
syncAttributes.title = title;
syncAttributes.discussionList = discussionList;
}
syncAttributes.contentCRC = contentCRC;
syncAttributes.titleCRC = titleCRC;
syncAttributes.discussionListCRC = discussionListCRC;
2013-12-23 22:33:33 +00:00
callback(undefined, true);
});
};
2014-03-20 00:24:56 +00:00
2014-03-28 00:49:49 +00:00
gdriveProvider.syncUpRealtime = function(content, contentCRC, title, titleCRC, discussionList, discussionListCRC, syncAttributes, callback) {
2013-12-23 22:33:33 +00:00
// Skip if title CRC has not changed
2014-03-28 00:49:49 +00:00
if(titleCRC == syncAttributes.titleCRC) {
2013-12-23 22:33:33 +00:00
callback(undefined, false);
return;
}
2014-03-28 00:49:49 +00:00
googleHelper.rename(syncAttributes.id, title, accountId, function(error, result) {
2013-12-23 22:33:33 +00:00
if(error) {
callback(error, true);
return;
}
syncAttributes.etag = result.etag;
2014-03-28 00:49:49 +00:00
syncAttributes.titleCRC = titleCRC;
2013-12-23 22:33:33 +00:00
callback(undefined, true);
});
};
2014-03-20 00:24:56 +00:00
2013-12-23 22:33:33 +00:00
gdriveProvider.syncDown = function(callback) {
var lastChangeId = parseInt(storage[accountId + ".gdrive.lastChangeId"], 10);
googleHelper.checkChanges(lastChangeId, accountId, function(error, changes, newChangeId) {
if(error) {
callback(error);
return;
}
var interestingChanges = [];
_.each(changes, function(change) {
var syncIndex = createSyncIndex(change.fileId);
var syncAttributes = fileMgr.getSyncAttributes(syncIndex);
if(syncAttributes === undefined) {
return;
}
// Store syncAttributes to avoid 2 times searching
change.syncAttributes = syncAttributes;
// Delete
if(change.deleted === true) {
interestingChanges.push(change);
return;
}
// Modify
if(syncAttributes.etag != change.file.etag) {
interestingChanges.push(change);
}
});
googleHelper.downloadContent(interestingChanges, accountId, function(error, changes) {
if(error) {
callback(error);
return;
}
2014-03-30 01:44:51 +00:00
function merge() {
if(changes.length === 0) {
storage[accountId + ".gdrive.lastChangeId"] = newChangeId;
return callback();
}
var change = changes.pop();
2013-12-23 22:33:33 +00:00
var syncAttributes = change.syncAttributes;
var syncIndex = syncAttributes.syncIndex;
var fileDesc = fileMgr.getFileFromSyncIndex(syncIndex);
2014-03-30 01:44:51 +00:00
// No file corresponding (file may have been deleted locally)
2013-12-23 22:33:33 +00:00
if(fileDesc === undefined) {
return;
}
// File deleted
if(change.deleted === true) {
2014-03-30 01:44:51 +00:00
eventMgr.onError('"' + fileDesc.title + '" has been removed from ' + providerName + '.');
2013-12-23 22:33:33 +00:00
fileDesc.removeSyncLocation(syncAttributes);
eventMgr.onSyncRemoved(fileDesc, syncAttributes);
if(syncAttributes.isRealtime === true && fileMgr.currentFile === fileDesc) {
gdriveProvider.stopRealtimeSync();
}
return;
}
var file = change.file;
2014-03-30 01:44:51 +00:00
var parsedContent = gdriveProvider.parseSerializedContent(file.content);
var remoteContent = parsedContent.content;
var remoteTitle = file.title;
var remoteDiscussionList = parsedContent.discussionList;
var remoteCRC = gdriveProvider.syncMerge(fileDesc, syncAttributes, remoteContent, remoteTitle, remoteDiscussionList);
2013-12-23 22:33:33 +00:00
// Update syncAttributes
syncAttributes.etag = file.etag;
2014-03-30 01:44:51 +00:00
if(merge === true) {
// Need to store the whole content for merge
syncAttributes.content = remoteContent;
syncAttributes.title = remoteTitle;
syncAttributes.discussionList = remoteDiscussionList;
2013-12-23 22:33:33 +00:00
}
2014-03-30 01:44:51 +00:00
syncAttributes.contentCRC = remoteCRC.contentCRC;
syncAttributes.titleCRC = remoteCRC.titleCRC;
syncAttributes.discussionListCRC = remoteCRC.discussionListCRC;
2013-12-23 22:33:33 +00:00
utils.storeAttributes(syncAttributes);
2014-03-30 01:44:51 +00:00
setTimeout(merge, 5);
}
setTimeout(merge, 5);
2013-12-23 22:33:33 +00:00
});
});
};
2014-03-20 00:24:56 +00:00
2013-12-23 22:33:33 +00:00
gdriveProvider.publish = function(publishAttributes, frontMatter, title, content, callback) {
var contentType = publishAttributes.format != "markdown" ? 'text/html' : undefined;
googleHelper.upload(publishAttributes.id, undefined, publishAttributes.fileName || title, content, contentType, undefined, accountId, function(error, result) {
if(error) {
callback(error);
return;
}
publishAttributes.id = result.id;
callback();
});
};
2014-03-20 00:24:56 +00:00
2013-12-23 22:33:33 +00:00
gdriveProvider.newPublishAttributes = function(event) {
var publishAttributes = {};
publishAttributes.id = utils.getInputTextValue('#input-publish-' + providerId + '-fileid');
publishAttributes.fileName = utils.getInputTextValue('#input-publish-' + providerId + '-filename');
if(event.isPropagationStopped()) {
return undefined;
}
return publishAttributes;
};
2014-03-20 00:24:56 +00:00
2013-12-23 22:33:33 +00:00
// Keep a link to the Pagedown editor
var pagedownEditor;
var undoExecute;
var redoExecute;
var setUndoRedoButtonStates;
eventMgr.addListener("onPagedownConfigure", function(pagedownEditorParam) {
pagedownEditor = pagedownEditorParam;
});
2014-03-20 00:24:56 +00:00
2014-03-23 02:33:41 +00:00
var realtimeContext;
eventMgr.addListener('onContentChanged', function(aceEditorParam) {
});
2013-12-23 22:33:33 +00:00
// Start realtime synchronization
gdriveProvider.startRealtimeSync = function(fileDesc, syncAttributes) {
2014-03-23 02:33:41 +00:00
var context = {
fileDesc: fileDesc
};
realtimeContext = context;
2013-12-23 22:33:33 +00:00
googleHelper.loadRealtime(syncAttributes.id, fileDesc.content, accountId, function(err, doc) {
if(err || !doc) {
return;
}
2014-03-20 00:24:56 +00:00
2013-12-23 22:33:33 +00:00
// If user just switched to another document or file has just been
// reselected
2014-03-23 02:33:41 +00:00
if(context !== realtimeContext) {
return doc.close();
2013-12-23 22:33:33 +00:00
}
2014-03-20 00:24:56 +00:00
2013-12-23 22:33:33 +00:00
logger.log("Starting Google Drive realtime synchronization");
2014-03-23 02:33:41 +00:00
context.document = doc;
2013-12-23 22:33:33 +00:00
var model = doc.getModel();
2014-03-23 02:33:41 +00:00
context.model = realtimeString;
2013-12-23 22:33:33 +00:00
var realtimeString = model.getRoot().get('content');
2014-03-23 02:33:41 +00:00
context.string = realtimeString;
2014-03-20 00:24:56 +00:00
2013-12-23 22:33:33 +00:00
// Saves model content checksum
function updateContentState() {
syncAttributes.contentCRC = utils.crc32(realtimeString.getText());
utils.storeAttributes(syncAttributes);
}
2014-03-20 00:24:56 +00:00
2013-12-23 22:33:33 +00:00
var debouncedRefreshPreview = _.debounce(pagedownEditor.refreshPreview, 100);
2014-03-20 00:24:56 +00:00
2013-12-23 22:33:33 +00:00
// Listen to insert text events
realtimeString.addEventListener(gapi.drive.realtime.EventType.TEXT_INSERTED, function(e) {
if(aceEditor !== undefined && (isAceUpToDate === false || e.isLocal === false)) {
// Update ACE editor
var position = aceEditor.session.doc.indexToPosition(e.index);
aceEditor.session.insert(position, e.text);
isAceUpToDate = true;
}
// If modifications come down from a collaborator
if(e.isLocal === false) {
logger.log("Google Drive realtime document updated from server");
updateContentState();
aceEditor === undefined && debouncedRefreshPreview();
}
});
// Listen to delete text events
realtimeString.addEventListener(gapi.drive.realtime.EventType.TEXT_DELETED, function(e) {
if(aceEditor !== undefined && (isAceUpToDate === false || e.isLocal === false)) {
// Update ACE editor
var range = (function(posStart, posEnd) {
return new Range(posStart.row, posStart.column, posEnd.row, posEnd.column);
})(aceEditor.session.doc.indexToPosition(e.index), aceEditor.session.doc.indexToPosition(e.index + e.text.length));
aceEditor.session.remove(range);
isAceUpToDate = true;
}
// If modifications come down from a collaborator
if(e.isLocal === false) {
logger.log("Google Drive realtime document updated from server");
updateContentState();
aceEditor === undefined && debouncedRefreshPreview();
}
});
doc.addEventListener(gapi.drive.realtime.EventType.DOCUMENT_SAVE_STATE_CHANGED, function(e) {
// Save success event
if(e.isPending === false && e.isSaving === false) {
logger.log("Google Drive realtime document successfully saved on server");
updateContentState();
}
});
2014-03-20 00:24:56 +00:00
2013-12-23 22:33:33 +00:00
// Try to merge offline modifications
var localContent = fileDesc.content;
var localContentChanged = syncAttributes.contentCRC != utils.crc32(localContent);
var remoteContent = realtimeString.getText();
var remoteContentCRC = utils.crc32(remoteContent);
var remoteContentChanged = syncAttributes.contentCRC != remoteContentCRC;
var fileContentChanged = localContent != remoteContent;
2014-03-23 02:33:41 +00:00
model.beginCompoundOperation('Open and merge');
2013-12-23 22:33:33 +00:00
if(fileContentChanged === true && localContentChanged === true) {
if(remoteContentChanged === true) {
// Conflict detected
fileMgr.createFile(fileDesc.title + " (backup)", localContent);
eventMgr.onMessage('Conflict detected on "' + fileDesc.title + '". A backup has been created locally.');
}
else {
// Add local modifications if no collaborators change
realtimeString.setText(localContent);
}
}
2014-03-20 00:24:56 +00:00
2013-12-23 22:33:33 +00:00
// Update content state according to collaborators changes
if(remoteContentChanged === true) {
logger.log("Google Drive realtime document updated from server");
aceEditor !== undefined && aceEditor.setValue(remoteContent, -1);
updateContentState();
aceEditor === undefined && debouncedRefreshPreview();
}
2014-03-20 00:24:56 +00:00
2014-03-23 02:33:41 +00:00
var realtimeDiscussionList = model.getRoot().get('discussionList');
context.discussionList = realtimeDiscussionList;
if(!realtimeDiscussionList) {
realtimeDiscussionList = model.createMap();
model.getRoot().set('discussionList', realtimeDiscussionList);
2013-12-23 22:33:33 +00:00
}
2014-03-23 02:33:41 +00:00
mergeDiscussionList(context, remoteContentChanged === true);
model.endCompoundOperation();
2014-03-20 00:24:56 +00:00
2013-12-23 22:33:33 +00:00
// Save undo/redo buttons default actions
undoExecute = pagedownEditor.uiManager.buttons.undo.execute;
redoExecute = pagedownEditor.uiManager.buttons.redo.execute;
setUndoRedoButtonStates = pagedownEditor.uiManager.setUndoRedoButtonStates;
2014-03-20 00:24:56 +00:00
2013-12-23 22:33:33 +00:00
// Set temporary actions for undo/redo buttons
pagedownEditor.uiManager.buttons.undo.execute = function() {
if(model.canUndo) {
// This flag is used to avoid replaying editor's own
// modifications (assuming it's synchronous)
isAceUpToDate = false;
model.undo();
}
};
pagedownEditor.uiManager.buttons.redo.execute = function() {
if(model.canRedo) {
// This flag is used to avoid replaying editor's own
// modifications (assuming it's synchronous)
isAceUpToDate = false;
model.redo();
}
};
2014-03-20 00:24:56 +00:00
2013-12-23 22:33:33 +00:00
// Add event handler for model's UndoRedoStateChanged events
pagedownEditor.uiManager.setUndoRedoButtonStates = function() {
setTimeout(function() {
pagedownEditor.uiManager.setButtonState(pagedownEditor.uiManager.buttons.undo, model.canUndo);
pagedownEditor.uiManager.setButtonState(pagedownEditor.uiManager.buttons.redo, model.canRedo);
}, 50);
};
pagedownEditor.uiManager.setUndoRedoButtonStates();
model.addEventListener(gapi.drive.realtime.EventType.UNDO_REDO_STATE_CHANGED, function() {
pagedownEditor.uiManager.setUndoRedoButtonStates();
});
2014-03-20 00:24:56 +00:00
2013-12-23 22:33:33 +00:00
}, function(err) {
2014-03-23 02:33:41 +00:00
logger.error(err);
2013-12-23 22:33:33 +00:00
if(err.type == "token_refresh_required") {
googleHelper.refreshGdriveToken(accountId);
}
else if(err.type == "not_found") {
2013-12-27 00:01:11 +00:00
eventMgr.onError('"' + fileDesc.title + '" has been removed from ' + providerName + '.');
2013-12-23 22:33:33 +00:00
fileDesc.removeSyncLocation(syncAttributes);
eventMgr.onSyncRemoved(fileDesc, syncAttributes);
gdriveProvider.stopRealtimeSync();
}
else if(err.isFatal) {
eventMgr.onError('An error has forced real time synchronization to stop.');
gdriveProvider.stopRealtimeSync();
}
});
};
2014-03-20 00:24:56 +00:00
2014-03-23 02:33:41 +00:00
function mergeDiscussion(localDiscussion, realtimeDiscussion, takeServer) {
if(takeServer) {
localDiscussion.selectionStart = realtimeDiscussion.get('selectionStart');
localDiscussion.selectionEnd = realtimeDiscussion.get('selectionEnd');
}
else {
realtimeDiscussion.set('selectionStart', localDiscussion.selectionStart);
realtimeDiscussion.set('selectionEnd', localDiscussion.selectionEnd);
}
function isCommentInDiscussion(comment, commentList) {
return commentList.some(function(commentInDiscussion) {
return comment.author == commentInDiscussion.author && comment.content == commentInDiscussion.content;
});
}
var realtimeCommentList = realtimeDiscussion.get('commentList').asArray();
localDiscussion.commentList.forEach(function(comment) {
if(!isCommentInDiscussion(comment, realtimeCommentList)) {
realtimeDiscussion.get('commentList').push(comment);
}
});
realtimeCommentList.forEach(function(comment) {
if(!isCommentInDiscussion(comment, localDiscussion.commentList)) {
localDiscussion.commentList.push(comment);
}
});
}
function createRealtimeDiscussion(context, discussion) {
var commentList = context.model.createList(discussion.commentList);
var realtimeDiscussion = context.model.createMap({
selectionStart: discussion.selectionStart,
selectionEnd: discussion.selectionEnd,
commentList: commentList
});
context.discussionList.set(discussion.discussionIndex, realtimeDiscussion);
}
function mergeDiscussionList(context, takeServer) {
var localDiscussionList = context.fileDesc.discussionList;
_.each(localDiscussionList, function(localDiscussion) {
var realtimeDiscussion = context.discussionList.get(localDiscussion.discussionIndex);
if(realtimeDiscussion) {
mergeDiscussion(localDiscussion, realtimeDiscussion, takeServer);
}
else {
createRealtimeDiscussion(context, localDiscussion);
}
});
context.discussionList.keys().forEach(function(discussionIndex) {
var localDiscussion = localDiscussionList[discussionIndex];
var realtimeDiscussion = context.discussionList.get(discussionIndex);
if(localDiscussion) {
mergeDiscussion(localDiscussion, realtimeDiscussion, takeServer);
}
else {
var discussion = {
discussionIndex: discussionIndex,
selectionStart: realtimeDiscussion.get('selectionStart'),
selectionEnd: realtimeDiscussion.get('selectionEnd'),
commentList: realtimeDiscussion.get('commentList').asArray()
};
localDiscussionList[discussionIndex] = discussion;
2014-03-26 00:29:34 +00:00
eventMgr.onCommentsChanged(context.fileDesc);
2014-03-23 02:33:41 +00:00
}
});
context.fileDesc.discussionList = localDiscussionList; // Write in localStorage
}
2014-03-26 00:29:34 +00:00
eventMgr.addListener('onCommentsChanged', function(fileDesc, discussion) {
2014-03-23 02:33:41 +00:00
if(realtimeContext === undefined || realtimeContext.fileDesc !== fileDesc) {
return;
}
if(!realtimeContext.discussionList.has(discussion.discussionIndex)) {
createRealtimeDiscussion(realtimeContext, discussion);
}
});
2013-12-23 22:33:33 +00:00
// Stop realtime synchronization
gdriveProvider.stopRealtimeSync = function() {
logger.log("Stopping Google Drive realtime synchronization");
if(realtimeContext !== undefined) {
realtimeContext.document && realtimeContext.document.close();
realtimeContext = undefined;
}
2014-03-20 00:24:56 +00:00
2013-12-23 22:33:33 +00:00
if(setUndoRedoButtonStates !== undefined) {
// Set back original undo/redo actions
pagedownEditor.uiManager.buttons.undo.execute = undoExecute;
pagedownEditor.uiManager.buttons.redo.execute = redoExecute;
pagedownEditor.uiManager.setUndoRedoButtonStates = setUndoRedoButtonStates;
pagedownEditor.uiManager.setUndoRedoButtonStates();
}
};
2014-03-20 00:24:56 +00:00
// 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);
};
2014-03-20 00:24:56 +00:00
// 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;
};
2014-03-20 00:24:56 +00:00
// 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);
});
};
2014-03-20 00:24:56 +00:00
2013-12-23 22:33:33 +00:00
// Disable publish on optional multi-account
gdriveProvider.isPublishEnabled = settings.gdriveMultiAccount > accountIndex;
2014-03-20 00:24:56 +00:00
2013-12-23 22:33:33 +00:00
eventMgr.addListener("onReady", function() {
// Hide optional multi-account sub-menus
$('.submenu-sync-' + providerId).toggle(settings.gdriveMultiAccount > accountIndex);
// Create export dialog
2013-12-27 00:01:11 +00:00
var modalUploadElt = document.querySelector('.modal-upload-' + providerId);
modalUploadElt && (modalUploadElt.innerHTML = _.template(dialogExportGdriveHTML, {
2013-12-23 22:33:33 +00:00
providerId: providerId,
providerName: providerName
2013-12-27 00:01:11 +00:00
}));
2014-03-20 00:24:56 +00:00
// Create autosync dialog
var modalAutosyncElt = document.querySelector('.modal-autosync-' + providerId);
modalAutosyncElt && (modalAutosyncElt.innerHTML = _.template(dialogAutoSyncGdriveHTML, {
providerId: providerId,
providerName: providerName
}));
2014-03-20 00:24:56 +00:00
2013-12-23 22:33:33 +00:00
// Choose folder button in export modal
$('.action-export-' + providerId + '-choose-folder').click(function() {
2013-12-23 22:33:33 +00:00
googleHelper.picker(function(error, docs) {
if(error || docs.length === 0) {
return;
}
// Open export dialog
$(".modal-upload-" + providerId).modal();
// Set parent ID
utils.setInputValue('#input-sync-export-' + providerId + '-parentid', docs[0].id);
}, 'folder', accountId);
});
2014-03-20 00:24:56 +00:00
// 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);
});
2014-03-20 00:24:56 +00:00
2013-12-23 22:33:33 +00:00
// On export, disable file ID input if realtime is checked
var $realtimeCheckboxElt = $('#input-sync-export-' + providerId + '-realtime');
var $fileIdInputElt = $('#input-sync-export-' + providerId + '-fileid');
$('#input-sync-export-' + providerId + '-realtime').change(function() {
$fileIdInputElt.prop('disabled', $realtimeCheckboxElt.prop('checked'));
});
2014-03-20 00:24:56 +00:00
// Skip gdrive action if provider is not enabled in the settings
if(accountIndex >= settings.gdriveMultiAccount) {
return;
}
2013-12-23 22:33:33 +00:00
var state = utils.retrieveIgnoreError(providerId + ".state");
var userId = storage[accountId + '.userId'];
if(state === undefined || (userId && state.userId != userId)) {
2013-12-23 22:33:33 +00:00
return;
}
storage.removeItem(providerId + ".state");
if(state.action == "create") {
googleHelper.upload(undefined, state.folderId, constants.GDRIVE_DEFAULT_FILE_TITLE, settings.defaultContent, undefined, undefined, accountId, function(error, file) {
if(error) {
return;
}
var syncAttributes = createSyncAttributes(file.id, file.etag, file.content, file.title);
var syncLocations = {};
syncLocations[syncAttributes.syncIndex] = syncAttributes;
2014-03-28 00:49:49 +00:00
var fileDesc = fileMgr.createFile(file.title, file.content, undefined, syncLocations);
2013-12-23 22:33:33 +00:00
fileMgr.selectFile(fileDesc);
2013-12-27 00:01:11 +00:00
eventMgr.onMessage('"' + file.title + '" created successfully on ' + providerName + '.');
2013-12-23 22:33:33 +00:00
});
}
else if(state.action == "open") {
var importIds = [];
_.each(state.ids, function(id) {
var syncIndex = createSyncIndex(id);
var fileDesc = fileMgr.getFileFromSyncIndex(syncIndex);
if(fileDesc !== undefined) {
fileDesc !== fileMgr.currentFile && fileMgr.selectFile(fileDesc);
}
else {
importIds.push(id);
}
});
importFilesFromIds(importIds);
}
});
2014-03-20 00:24:56 +00:00
2013-12-23 22:33:33 +00:00
return gdriveProvider;
};
2014-03-20 00:24:56 +00:00
});