Stackedit/public/res/providers/gdriveProviderBuilder.js

391 lines
18 KiB
JavaScript
Raw Normal View History

2013-12-23 22:33:33 +00:00
define([
"jquery",
"underscore",
"constants",
"utils",
"storage",
"logger",
"classes/Provider",
"settings",
"eventMgr",
"fileMgr",
2014-04-06 23:39:24 +00:00
"editor",
2013-12-23 22:33:33 +00:00
"helpers/googleHelper",
"text!html/dialogExportGdrive.html",
"text!html/dialogAutoSyncGdrive.html",
2014-04-06 23:39:24 +00:00
], function($, _, constants, utils, storage, logger, Provider, settings, eventMgr, fileMgr, editor, 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",
];
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
2014-04-07 23:19:47 +00:00
var merge = settings.conflictMode == 'merge';
function createSyncAttributes(id, etag, content, title, discussionListJSON) {
discussionListJSON = discussionListJSON || '{}';
2013-12-23 22:33:33 +00:00
var syncAttributes = {};
syncAttributes.provider = gdriveProvider;
syncAttributes.id = id;
syncAttributes.etag = etag;
syncAttributes.contentCRC = utils.crc32(content);
syncAttributes.titleCRC = utils.crc32(title);
2014-04-07 23:19:47 +00:00
syncAttributes.discussionListCRC = utils.crc32(discussionListJSON);
2013-12-23 22:33:33 +00:00
syncAttributes.syncIndex = createSyncIndex(id);
2014-04-07 23:19:47 +00:00
if(merge === true) {
// Need to store the whole content for merge
syncAttributes.content = content;
syncAttributes.title = title;
syncAttributes.discussionList = discussionListJSON;
}
2013-12-23 22:33:33 +00:00
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 parsedContent = gdriveProvider.parseContent(file.content);
2014-04-13 10:45:31 +00:00
var syncLocations;
if(file.isRealtime) {
eventMgr.onError('Real time synchronization is not supported anymore. Please use standard synchronization.');
}
else {
var syncAttributes = createSyncAttributes(file.id, file.etag, parsedContent.content, file.title, parsedContent.discussionListJSON);
syncLocations = {};
syncLocations[syncAttributes.syncIndex] = syncAttributes;
}
2014-04-07 23:19:47 +00:00
fileDesc = fileMgr.createFile(file.title, parsedContent.content, parsedContent.discussionListJSON, 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) {
2014-05-04 17:31:57 +00:00
return eventMgr.onError('"' + fileDesc.title + '" was already imported.');
2013-12-23 22:33:33 +00:00
}
importIds.push(doc.id);
});
importFilesFromIds(importIds);
}, 'doc', accountId);
};
2014-03-20 00:24:56 +00:00
2014-04-07 23:19:47 +00:00
gdriveProvider.exportFile = function(event, title, content, discussionListJSON, callback) {
2013-12-23 22:33:33 +00:00
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 + '".');
2014-05-04 17:31:57 +00:00
return callback(true);
2013-12-23 22:33:33 +00:00
}
}
var parentId = utils.getInputTextValue('#input-sync-export-' + providerId + '-parentid');
var data = gdriveProvider.serializeContent(content, discussionListJSON);
googleHelper.upload(fileId, parentId, title, data, undefined, undefined, accountId, function(error, result) {
2013-12-23 22:33:33 +00:00
if(error) {
2014-05-04 17:31:57 +00:00
return callback(error);
2013-12-23 22:33:33 +00:00
}
2014-04-07 23:19:47 +00:00
var syncAttributes = createSyncAttributes(result.id, result.etag, content, title, discussionListJSON);
2013-12-23 22:33:33 +00:00
callback(undefined, syncAttributes);
});
};
2014-03-20 00:24:56 +00:00
2014-03-28 00:49:49 +00:00
gdriveProvider.syncUp = function(content, contentCRC, title, titleCRC, discussionList, discussionListCRC, syncAttributes, callback) {
if(
(syncAttributes.contentCRC == contentCRC) && // Content CRC hasn't changed
2014-05-04 17:31:57 +00:00
(syncAttributes.titleCRC == titleCRC) && // Title CRC hasn't changed
2014-03-28 00:49:49 +00:00
(syncAttributes.discussionListCRC == discussionListCRC) // Discussion list CRC hasn't changed
) {
return callback(undefined, false);
2013-12-23 22:33:33 +00:00
}
2014-04-13 10:45:31 +00:00
if(syncAttributes.isRealtime) {
var fileDesc = fileMgr.getFileFromSyncIndex(syncAttributes.syncIndex);
fileDesc.removeSyncLocation(syncAttributes);
eventMgr.onSyncRemoved(fileDesc, syncAttributes);
return eventMgr.onError('Real time synchronization is not supported anymore. Please use standard synchronization.');
}
var data = gdriveProvider.serializeContent(content, discussionList);
googleHelper.upload(syncAttributes.id, undefined, title, data, undefined, syncAttributes.etag, accountId, function(error, result) {
2013-12-23 22:33:33 +00:00
if(error) {
2014-05-04 17:31:57 +00:00
return callback(error, true);
2013-12-23 22:33:33 +00:00
}
syncAttributes.etag = result.etag;
2014-04-21 15:36:22 +00:00
// Remove this deprecated flag if any
2014-04-13 10:45:31 +00:00
delete syncAttributes.isRealtime;
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
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) {
2014-05-04 17:31:57 +00:00
return callback(error);
2013-12-23 22:33:33 +00:00
}
var interestingChanges = [];
_.each(changes, function(change) {
var syncIndex = createSyncIndex(change.fileId);
var fileDesc = fileMgr.getFileFromSyncIndex(syncIndex);
var syncAttributes = fileDesc && fileDesc.syncLocations[syncIndex];
if(!syncAttributes) {
2013-12-23 22:33:33 +00:00
return;
}
// Store fileDesc and syncAttributes references to avoid 2 times search
change.fileDesc = fileDesc;
2013-12-23 22:33:33 +00:00
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-04-21 15:36:22 +00:00
function mergeChange() {
2014-03-30 01:44:51 +00:00
if(changes.length === 0) {
storage[accountId + ".gdrive.lastChangeId"] = newChangeId;
return callback();
}
var change = changes.pop();
var fileDesc = change.fileDesc;
2013-12-23 22:33:33 +00:00
var syncAttributes = change.syncAttributes;
// 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);
2014-04-13 10:45:31 +00:00
return eventMgr.onSyncRemoved(fileDesc, syncAttributes);
2013-12-23 22:33:33 +00:00
}
var file = change.file;
var parsedContent = gdriveProvider.parseContent(file.content);
2014-03-30 01:44:51 +00:00
var remoteContent = parsedContent.content;
var remoteTitle = file.title;
2014-04-07 23:19:47 +00:00
var remoteDiscussionListJSON = parsedContent.discussionListJSON;
var remoteDiscussionList = parsedContent.discussionList;
2014-04-07 23:19:47 +00:00
var remoteCRC = gdriveProvider.syncMerge(fileDesc, syncAttributes, remoteContent, remoteTitle, remoteDiscussionList, remoteDiscussionListJSON);
2014-03-30 01:44:51 +00:00
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;
2014-04-07 23:19:47 +00:00
syncAttributes.discussionList = remoteDiscussionListJSON;
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-04-21 15:36:22 +00:00
setTimeout(mergeChange, 5);
2014-03-30 01:44:51 +00:00
}
2014-04-21 15:36:22 +00:00
setTimeout(mergeChange, 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
// Initialize the AutoSync dialog fields
gdriveProvider.setAutosyncDialogConfig = function() {
var config = gdriveProvider.autosyncConfig;
2014-04-20 01:13:43 +00:00
utils.setInputRadio('radio-autosync-' + providerId + '-mode', config.mode || 'off');
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 = {};
2014-04-20 01:13:43 +00:00
config.mode = utils.getInputRadio('radio-autosync-' + providerId + '-mode');
config.parentId = utils.getInputTextValue('#input-autosync-' + providerId + '-parentid');
return config;
};
2014-03-20 00:24:56 +00:00
// Perform AutoSync
2014-04-07 23:19:47 +00:00
gdriveProvider.autosyncFile = function(title, content, discussionListJSON, config, callback) {
var parentId = config.parentId;
googleHelper.upload(undefined, parentId, title, content, undefined, undefined, accountId, function(error, result) {
if(error) {
callback(error);
return;
}
2014-04-07 23:19:47 +00:00
var syncAttributes = createSyncAttributes(result.id, result.etag, content, title, discussionListJSON);
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
$('.action-remove-google-drive-state').click(function() {
storage.removeItem('gdrive.state');
});
// Skip gdrive action if provider is not enabled in the settings
if(accountIndex >= settings.gdriveMultiAccount) {
return;
}
var state = utils.retrieveIgnoreError('gdrive.state');
var userId = storage[accountId + '.userId'];
if(state === undefined) {
return;
}
if(userId && state.userId != userId) {
if(accountIndex === settings.gdriveMultiAccount - 1) {
if(settings.gdriveMultiAccount === 3) {
eventMgr.onError('None of your 3 Google Drive accounts is able to perform this request.');
storage.removeItem('gdrive.state');
}
else {
$(".modal-add-google-drive-account").modal();
}
}
2013-12-23 22:33:33 +00:00
return;
}
storage.removeItem('gdrive.state');
2013-12-23 22:33:33 +00:00
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
});