Stackedit/public/res/providers/gdriveProvider.js

531 lines
23 KiB
JavaScript
Raw Normal View History

2013-11-05 23:03:38 +00:00
/*global gapi */
2013-05-27 19:45:33 +00:00
define([
2013-11-05 23:03:38 +00:00
"jquery",
2013-05-27 19:45:33 +00:00
"underscore",
2013-11-05 23:03:38 +00:00
"constants",
2013-05-27 19:45:33 +00:00
"utils",
2013-11-05 23:03:38 +00:00
"storage",
"logger",
2013-06-22 23:48:57 +00:00
"classes/Provider",
2013-05-30 22:16:12 +00:00
"settings",
2013-07-30 08:46:36 +00:00
"eventMgr",
2013-06-10 21:22:32 +00:00
"fileMgr",
"helpers/googleHelper"
2013-11-05 23:03:38 +00:00
], function($, _, constants, utils, storage, logger, Provider, settings, eventMgr, fileMgr, googleHelper) {
2013-04-20 00:14:20 +00:00
2013-05-29 19:55:23 +00:00
var PROVIDER_GDRIVE = "gdrive";
2013-04-20 00:14:20 +00:00
2013-06-22 23:48:57 +00:00
var gdriveProvider = new Provider(PROVIDER_GDRIVE, "Google Drive");
gdriveProvider.defaultPublishFormat = "template";
gdriveProvider.exportPreferencesInputIds = [
2013-08-23 23:50:14 +00:00
"gdrive-parentid",
"gdrive-realtime",
2013-06-22 23:48:57 +00:00
];
2013-05-29 19:55:23 +00:00
function createSyncIndex(id) {
return "sync." + PROVIDER_GDRIVE + "." + id;
}
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;
}
function importFilesFromIds(ids) {
googleHelper.downloadMetadata(ids, function(error, result) {
if(error) {
return;
}
googleHelper.downloadContent(result, function(error, result) {
if(error) {
return;
}
var fileDescList = [];
2013-11-05 23:03:38 +00:00
var fileDesc;
2013-05-29 19:55:23 +00:00
_.each(result, function(file) {
var syncAttributes = createSyncAttributes(file.id, file.etag, file.content, file.title);
2013-07-20 01:08:17 +00:00
syncAttributes.isRealtime = file.isRealtime;
2013-05-29 19:55:23 +00:00
var syncLocations = {};
syncLocations[syncAttributes.syncIndex] = syncAttributes;
2013-07-20 01:08:17 +00:00
fileDesc = fileMgr.createFile(file.title, file.content, syncLocations);
2013-05-29 19:55:23 +00:00
fileDescList.push(fileDesc);
});
2013-07-20 01:08:17 +00:00
if(fileDesc !== undefined) {
2013-07-30 08:46:36 +00:00
eventMgr.onSyncImportSuccess(fileDescList, gdriveProvider);
2013-07-20 01:08:17 +00:00
fileMgr.selectFile(fileDesc);
2013-05-29 23:04:52 +00:00
}
2013-05-29 19:55:23 +00:00
});
});
}
gdriveProvider.importFiles = function() {
2013-06-02 00:38:23 +00:00
googleHelper.picker(function(error, docs) {
2013-06-02 00:53:51 +00:00
if(error || docs.length === 0) {
2013-05-29 19:55:23 +00:00
return;
}
var importIds = [];
2013-06-02 00:38:23 +00:00
_.each(docs, function(doc) {
var syncIndex = createSyncIndex(doc.id);
2013-05-29 19:55:23 +00:00
var fileDesc = fileMgr.getFileFromSyncIndex(syncIndex);
if(fileDesc !== undefined) {
2013-07-30 08:46:36 +00:00
eventMgr.onError('"' + fileDesc.title + '" was already imported.');
2013-05-29 19:55:23 +00:00
return;
}
2013-06-02 00:38:23 +00:00
importIds.push(doc.id);
2013-05-29 19:55:23 +00:00
});
importFilesFromIds(importIds);
2013-08-23 23:50:14 +00:00
}, 'doc');
2013-05-29 19:55:23 +00:00
};
gdriveProvider.exportFile = function(event, title, content, callback) {
2013-08-14 23:44:51 +00:00
var fileId = utils.getInputTextValue("#input-sync-export-gdrive-fileid");
if(fileId) {
2013-09-14 16:59:40 +00:00
// Check that file is not synchronized with another an existing
// document
2013-08-14 23:44:51 +00:00
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;
}
}
2013-05-29 19:55:23 +00:00
var parentId = utils.getInputTextValue("#input-sync-export-gdrive-parentid");
googleHelper.upload(fileId, parentId, title, content, undefined, undefined, function(error, result) {
2013-05-29 19:55:23 +00:00
if(error) {
callback(error);
return;
}
var syncAttributes = createSyncAttributes(result.id, result.etag, content, title);
callback(undefined, syncAttributes);
});
};
2013-07-21 22:18:35 +00:00
2013-07-18 23:30:28 +00:00
gdriveProvider.exportRealtimeFile = function(event, title, content, callback) {
var parentId = utils.getInputTextValue("#input-sync-export-gdrive-parentid");
googleHelper.createRealtimeFile(parentId, title, function(error, result) {
if(error) {
callback(error);
return;
}
var syncAttributes = createSyncAttributes(result.id, result.etag, content, title);
callback(undefined, syncAttributes);
});
};
2013-05-29 19:55:23 +00:00
gdriveProvider.syncUp = function(uploadContent, uploadContentCRC, uploadTitle, uploadTitleCRC, syncAttributes, callback) {
// Skip if CRC has not changed
if(uploadContentCRC == syncAttributes.contentCRC && uploadTitleCRC == syncAttributes.titleCRC) {
2013-05-29 19:55:23 +00:00
callback(undefined, false);
return;
}
googleHelper.upload(syncAttributes.id, undefined, uploadTitle, uploadContent, undefined, syncAttributes.etag, function(error, result) {
2013-05-29 19:55:23 +00:00
if(error) {
callback(error, true);
return;
}
syncAttributes.etag = result.etag;
syncAttributes.contentCRC = uploadContentCRC;
syncAttributes.titleCRC = uploadTitleCRC;
callback(undefined, true);
});
};
2013-09-14 16:59:40 +00:00
gdriveProvider.syncUpRealtime = function(uploadContent, uploadContentCRC, uploadTitle, uploadTitleCRC, syncAttributes, callback) {
// Skip if title CRC has not changed
if(uploadTitleCRC == syncAttributes.titleCRC) {
2013-09-10 23:51:37 +00:00
callback(undefined, false);
return;
}
googleHelper.rename(syncAttributes.id, uploadTitle, function(error, result) {
if(error) {
callback(error, true);
return;
}
syncAttributes.etag = result.etag;
syncAttributes.titleCRC = uploadTitleCRC;
callback(undefined, true);
});
};
2013-05-29 19:55:23 +00:00
gdriveProvider.syncDown = function(callback) {
2013-11-05 23:03:38 +00:00
var lastChangeId = parseInt(storage[PROVIDER_GDRIVE + ".lastChangeId"], 10);
2013-05-29 19:55:23 +00:00
googleHelper.checkChanges(lastChangeId, 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);
2013-07-21 14:38:53 +00:00
if(syncAttributes === undefined) {
2013-05-29 19:55:23 +00:00
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, function(error, changes) {
if(error) {
callback(error);
return;
}
_.each(changes, function(change) {
var syncAttributes = change.syncAttributes;
var syncIndex = syncAttributes.syncIndex;
var fileDesc = fileMgr.getFileFromSyncIndex(syncIndex);
// No file corresponding (file may have been deleted
// locally)
if(fileDesc === undefined) {
return;
}
var localTitle = fileDesc.title;
// File deleted
if(change.deleted === true) {
2013-07-30 08:46:36 +00:00
eventMgr.onError('"' + localTitle + '" has been removed from Google Drive.');
2013-06-16 10:47:35 +00:00
fileDesc.removeSyncLocation(syncAttributes);
2013-07-30 08:46:36 +00:00
eventMgr.onSyncRemoved(fileDesc, syncAttributes);
2013-07-21 14:38:53 +00:00
if(syncAttributes.isRealtime === true && fileMgr.currentFile === fileDesc) {
gdriveProvider.stopRealtimeSync();
}
2013-05-29 19:55:23 +00:00
return;
}
var localTitleChanged = syncAttributes.titleCRC != utils.crc32(localTitle);
var localContent = fileDesc.content;
var localContentChanged = syncAttributes.contentCRC != utils.crc32(localContent);
var file = change.file;
2013-05-25 18:13:59 +00:00
var remoteTitleCRC = utils.crc32(file.title);
var remoteTitleChanged = syncAttributes.titleCRC != remoteTitleCRC;
var fileTitleChanged = localTitle != file.title;
2013-05-25 18:13:59 +00:00
var remoteContentCRC = utils.crc32(file.content);
var remoteContentChanged = syncAttributes.contentCRC != remoteContentCRC;
2013-05-29 19:55:23 +00:00
var fileContentChanged = localContent != file.content;
// Conflict detection
2013-07-21 14:38:53 +00:00
if((fileTitleChanged === true && localTitleChanged === true && remoteTitleChanged === true) || (!syncAttributes.isRealtime && fileContentChanged === true && localContentChanged === true && remoteContentChanged === true)) {
2013-05-29 19:55:23 +00:00
fileMgr.createFile(localTitle + " (backup)", localContent);
2013-07-30 08:46:36 +00:00
eventMgr.onMessage('Conflict detected on "' + localTitle + '". A backup has been created locally.');
2013-05-29 19:55:23 +00:00
}
// If file title changed
if(fileTitleChanged && remoteTitleChanged === true) {
fileDesc.title = file.title;
2013-07-30 08:46:36 +00:00
eventMgr.onTitleChanged(fileDesc);
eventMgr.onMessage('"' + localTitle + '" has been renamed to "' + file.title + '" on Google Drive.');
2013-05-29 19:55:23 +00:00
}
// If file content changed
2013-07-21 14:38:53 +00:00
if(!syncAttributes.isRealtime && fileContentChanged && remoteContentChanged === true) {
2013-05-29 19:55:23 +00:00
fileDesc.content = file.content;
2013-07-30 08:46:36 +00:00
eventMgr.onContentChanged(fileDesc);
eventMgr.onMessage('"' + file.title + '" has been updated from Google Drive.');
2013-06-19 20:33:46 +00:00
if(fileMgr.currentFile === fileDesc) {
2013-05-29 19:55:23 +00:00
fileMgr.selectFile(); // Refresh editor
}
}
// Update syncAttributes
syncAttributes.etag = file.etag;
2013-07-21 14:38:53 +00:00
if(!syncAttributes.isRealtime) {
syncAttributes.contentCRC = remoteContentCRC;
}
2013-05-29 19:55:23 +00:00
syncAttributes.titleCRC = remoteTitleCRC;
utils.storeAttributes(syncAttributes);
});
2013-11-05 23:03:38 +00:00
storage[PROVIDER_GDRIVE + ".lastChangeId"] = newChangeId;
2013-05-29 19:55:23 +00:00
callback();
});
});
};
2013-10-06 14:34:40 +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, function(error, result) {
2013-05-29 19:55:23 +00:00
if(error) {
callback(error);
return;
}
publishAttributes.id = result.id;
callback();
});
};
gdriveProvider.newPublishAttributes = function(event) {
var publishAttributes = {};
publishAttributes.id = utils.getInputTextValue("#input-publish-gdrive-fileid");
publishAttributes.fileName = utils.getInputTextValue("#input-publish-gdrive-filename");
if(event.isPropagationStopped()) {
return undefined;
}
return publishAttributes;
};
2013-04-20 00:14:20 +00:00
2013-09-10 23:51:37 +00:00
// Keep a link to the Pagedown editor
2013-11-05 23:03:38 +00:00
var pagedownEditor;
var undoExecute;
var redoExecute;
var setUndoRedoButtonStates;
2013-09-10 23:51:37 +00:00
eventMgr.addListener("onPagedownConfigure", function(pagedownEditorParam) {
pagedownEditor = pagedownEditorParam;
});
2013-07-21 22:18:35 +00:00
2013-09-10 23:51:37 +00:00
// Keep a link to the ACE editor
2013-11-05 23:03:38 +00:00
var realtimeContext;
var aceEditor;
2013-09-11 23:26:47 +00:00
var isAceUpToDate = true;
2013-09-10 23:51:37 +00:00
eventMgr.addListener('onAceCreated', function(aceEditorParam) {
aceEditor = aceEditorParam;
// Listen to editor's changes
2013-11-05 23:03:38 +00:00
aceEditor.session.on('change', function() {
// Update the real time model if any
realtimeContext && realtimeContext.string && realtimeContext.string.setText(aceEditor.getValue());
2013-09-10 23:51:37 +00:00
});
});
// Start realtime synchronization
2013-09-10 23:51:37 +00:00
var Range = require('ace/range').Range;
2013-07-21 22:18:35 +00:00
gdriveProvider.startRealtimeSync = function(fileDesc, syncAttributes) {
var localContext = {};
realtimeContext = localContext;
2013-07-21 22:18:35 +00:00
googleHelper.loadRealtime(syncAttributes.id, fileDesc.content, function(err, doc) {
if(err || !doc) {
return;
}
2013-09-14 16:59:40 +00:00
// If user just switched to another document or file has just been
// reselected
if(localContext.isStopped === true) {
2013-07-21 22:18:35 +00:00
doc.close();
return;
}
2013-09-14 16:59:40 +00:00
2013-07-21 22:18:35 +00:00
logger.log("Starting Google Drive realtime synchronization");
localContext.document = doc;
var model = doc.getModel();
var realtimeString = model.getRoot().get('content');
2013-07-21 22:18:35 +00:00
2013-07-21 14:38:53 +00:00
// Saves model content checksum
function updateContentState() {
syncAttributes.contentCRC = utils.crc32(realtimeString.getText());
2013-07-21 14:38:53 +00:00
utils.storeAttributes(syncAttributes);
}
2013-07-21 22:18:35 +00:00
2013-09-14 16:59:40 +00:00
var debouncedRefreshPreview = _.debounce(pagedownEditor.refreshPreview, 100);
2013-09-10 23:51:37 +00:00
// Listen to insert text events
realtimeString.addEventListener(gapi.drive.realtime.EventType.TEXT_INSERTED, function(e) {
2013-09-14 16:59:40 +00:00
if(aceEditor !== undefined && (isAceUpToDate === false || e.isLocal === false)) {
2013-09-11 23:26:47 +00:00
// Update ACE editor
var position = aceEditor.session.doc.indexToPosition(e.index);
aceEditor.session.insert(position, e.text);
isAceUpToDate = true;
2013-09-10 23:51:37 +00:00
}
// If modifications come down from a collaborator
2013-07-21 14:38:53 +00:00
if(e.isLocal === false) {
logger.log("Google Drive realtime document updated from server");
updateContentState();
2013-09-14 16:59:40 +00:00
aceEditor === undefined && debouncedRefreshPreview();
2013-07-21 14:38:53 +00:00
}
2013-09-10 23:51:37 +00:00
});
// Listen to delete text events
realtimeString.addEventListener(gapi.drive.realtime.EventType.TEXT_DELETED, function(e) {
2013-09-14 16:59:40 +00:00
if(aceEditor !== undefined && (isAceUpToDate === false || e.isLocal === false)) {
2013-09-11 23:26:47 +00:00
// 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;
2013-09-10 23:51:37 +00:00
}
// If modifications come down from a collaborator
if(e.isLocal === false) {
logger.log("Google Drive realtime document updated from server");
updateContentState();
2013-09-14 16:59:40 +00:00
aceEditor === undefined && debouncedRefreshPreview();
2013-09-10 23:51:37 +00:00
}
});
doc.addEventListener(gapi.drive.realtime.EventType.DOCUMENT_SAVE_STATE_CHANGED, function(e) {
2013-07-21 14:38:53 +00:00
// Save success event
if(e.isPending === false && e.isSaving === false) {
logger.log("Google Drive realtime document successfully saved on server");
updateContentState();
}
});
2013-07-21 22:18:35 +00:00
2013-07-21 14:38:53 +00:00
// Try to merge offline modifications
2013-07-21 22:18:35 +00:00
var localContent = fileDesc.content;
2013-07-21 14:38:53 +00:00
var localContentChanged = syncAttributes.contentCRC != utils.crc32(localContent);
var remoteContent = realtimeString.getText();
2013-07-21 14:38:53 +00:00
var remoteContentCRC = utils.crc32(remoteContent);
var remoteContentChanged = syncAttributes.contentCRC != remoteContentCRC;
var fileContentChanged = localContent != remoteContent;
if(fileContentChanged === true && localContentChanged === true) {
if(remoteContentChanged === true) {
// Conflict detected
2013-07-21 22:18:35 +00:00
fileMgr.createFile(fileDesc.title + " (backup)", localContent);
2013-07-30 08:46:36 +00:00
eventMgr.onMessage('Conflict detected on "' + fileDesc.title + '". A backup has been created locally.');
2013-07-21 14:38:53 +00:00
}
else {
// Add local modifications if no collaborators change
realtimeString.setText(localContent);
2013-07-21 14:38:53 +00:00
}
}
2013-09-14 16:59:40 +00:00
if(aceEditor === undefined) {
// Binds model with textarea
localContext.binding = gapi.drive.realtime.databinding.bindString(realtimeString, document.getElementById("wmd-input"));
2013-09-14 16:59:40 +00:00
}
2013-07-21 14:38:53 +00:00
// Update content state according to collaborators changes
if(remoteContentChanged === true) {
logger.log("Google Drive realtime document updated from server");
2013-09-14 16:59:40 +00:00
aceEditor !== undefined && aceEditor.setValue(remoteContent, -1);
2013-07-21 14:38:53 +00:00
updateContentState();
2013-09-14 16:59:40 +00:00
aceEditor === undefined && debouncedRefreshPreview();
2013-07-21 14:38:53 +00:00
}
2013-09-14 16:59:40 +00:00
if(aceEditor !== undefined) {
// Tell ACE to update realtime string on each change
localContext.string = realtimeString;
2013-12-05 00:25:17 +00:00
}
2013-12-05 00:25:17 +00:00
// Save undo/redo buttons default actions
undoExecute = pagedownEditor.uiManager.buttons.undo.execute;
redoExecute = pagedownEditor.uiManager.buttons.redo.execute;
setUndoRedoButtonStates = pagedownEditor.uiManager.setUndoRedoButtonStates;
// 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();
}
};
2013-09-14 16:59:40 +00:00
2013-12-05 00:25:17 +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() {
2013-09-10 23:51:37 +00:00
pagedownEditor.uiManager.setUndoRedoButtonStates();
2013-12-05 00:25:17 +00:00
});
2013-09-14 16:59:40 +00:00
2013-07-21 22:18:35 +00:00
}, function(err) {
console.error(err);
if(err.type == "token_refresh_required") {
2013-10-06 14:34:40 +00:00
googleHelper.forceGdriveAuthenticate();
2013-07-21 22:18:35 +00:00
}
else if(err.type == "not_found") {
2013-07-30 08:46:36 +00:00
eventMgr.onError('"' + fileDesc.title + '" has been removed from Google Drive.');
2013-07-21 22:18:35 +00:00
fileDesc.removeSyncLocation(syncAttributes);
2013-07-30 08:46:36 +00:00
eventMgr.onSyncRemoved(fileDesc, syncAttributes);
2013-07-21 22:18:35 +00:00
gdriveProvider.stopRealtimeSync();
}
else if(err.isFatal) {
2013-07-30 08:46:36 +00:00
eventMgr.onError('An error has forced real time synchronization to stop.');
2013-07-21 22:18:35 +00:00
gdriveProvider.stopRealtimeSync();
}
});
};
2013-07-18 23:30:28 +00:00
// Stop realtime synchronization
2013-07-20 01:08:17 +00:00
gdriveProvider.stopRealtimeSync = function() {
2013-07-18 23:30:28 +00:00
logger.log("Stopping Google Drive realtime synchronization");
if(realtimeContext !== undefined) {
realtimeContext.isStopped = true;
realtimeContext.binding && realtimeContext.binding.unbind();
realtimeContext.document && realtimeContext.document.close();
realtimeContext = undefined;
}
2013-07-21 22:18:35 +00:00
if(setUndoRedoButtonStates !== undefined) {
2013-09-14 16:59:40 +00:00
// 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();
}
};
2013-08-04 00:53:46 +00:00
eventMgr.addListener("onReady", function() {
2013-08-23 23:50:14 +00:00
// Choose folder button in export modal
$('.export-gdrive-choose-folder').click(function() {
googleHelper.picker(function(error, docs) {
if(error || docs.length === 0) {
return;
}
// Open export dialog
$(".modal-upload-gdrive").modal();
// Set parent ID
utils.setInputValue('#input-sync-export-gdrive-parentid', docs[0].id);
}, 'folder');
});
2013-09-14 16:59:40 +00:00
2013-08-14 23:44:51 +00:00
// On export, disable file ID input if realtime is checked
var $realtimeCheckboxElt = $('#input-sync-export-gdrive-realtime');
var $fileIdInputElt = $('#input-sync-export-gdrive-fileid');
$('#input-sync-export-gdrive-realtime').change(function() {
$fileIdInputElt.prop('disabled', $realtimeCheckboxElt.prop('checked'));
});
2013-09-14 16:59:40 +00:00
2013-06-03 22:19:52 +00:00
var state = utils.retrieveIgnoreError(PROVIDER_GDRIVE + ".state");
2013-05-29 19:55:23 +00:00
if(state === undefined) {
return;
}
2013-11-05 23:03:38 +00:00
storage.removeItem(PROVIDER_GDRIVE + ".state");
2013-05-29 19:55:23 +00:00
if(state.action == "create") {
2013-11-05 23:03:38 +00:00
googleHelper.upload(undefined, state.folderId, constants.GDRIVE_DEFAULT_FILE_TITLE, settings.defaultContent, undefined, undefined, function(error, file) {
2013-05-29 19:55:23 +00:00
if(error) {
return;
}
var syncAttributes = createSyncAttributes(file.id, file.etag, file.content, file.title);
var syncLocations = {};
syncLocations[syncAttributes.syncIndex] = syncAttributes;
2013-07-17 00:37:25 +00:00
var fileDesc = fileMgr.createFile(file.title, file.content, syncLocations);
2013-05-29 19:55:23 +00:00
fileMgr.selectFile(fileDesc);
2013-07-30 08:46:36 +00:00
eventMgr.onMessage('"' + file.title + '" created successfully on Google Drive.');
2013-05-29 19:55:23 +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);
2013-05-29 19:55:23 +00:00
}
else {
importIds.push(id);
}
});
importFilesFromIds(importIds);
}
});
2013-04-20 00:14:20 +00:00
2013-05-29 19:55:23 +00:00
return gdriveProvider;
2013-04-20 00:14:20 +00:00
});