CouchDB sync part 1
This commit is contained in:
parent
2be4919853
commit
8f5da7998f
@ -7,7 +7,7 @@
|
||||
"jquery": "2.0.3",
|
||||
"underscore": "1.5.1",
|
||||
"requirejs": "~2.1.11",
|
||||
"require-css": "0.1.2",
|
||||
"require-css": "0.1.5",
|
||||
"require-less": "0.1.2",
|
||||
"mousetrap": "~1.4.4",
|
||||
"jgrowl": "~1.2.10",
|
||||
|
144
couchdb/setup-db.js
Normal file
144
couchdb/setup-db.js
Normal file
@ -0,0 +1,144 @@
|
||||
var request = require('request');
|
||||
var async = require('async');
|
||||
|
||||
var validate = function(newDoc) {
|
||||
Object.keys(newDoc).forEach(function(key) {
|
||||
if(key[0] !== '_' && [
|
||||
'updated',
|
||||
'tags',
|
||||
'title'
|
||||
].indexOf(key) === -1) {
|
||||
throw({forbidden: 'Unknown document attribute: ' + key});
|
||||
}
|
||||
});
|
||||
var toString = Object.prototype.toString;
|
||||
if(toString.call(newDoc._id) !== '[object String]') {
|
||||
throw({forbidden: 'ID must be a string.'});
|
||||
}
|
||||
if(!newDoc._id.match(/[a-zA-Z0-9]{24}/)) {
|
||||
throw({forbidden: 'Invalid ID format.'});
|
||||
}
|
||||
if(toString.call(newDoc.updated) !== '[object Number]') {
|
||||
throw({forbidden: 'Update time must be an integer.'});
|
||||
}
|
||||
if(newDoc.updated > Date.now() + 60000) {
|
||||
throw({forbidden: 'Update time is in the future, please check your clock!'});
|
||||
}
|
||||
if(toString.call(newDoc.title) !== '[object String]') {
|
||||
throw({forbidden: 'Title must be a string.'});
|
||||
}
|
||||
if(!newDoc.title) {
|
||||
throw({forbidden: 'Title is empty.'});
|
||||
}
|
||||
if(newDoc.title.length >= 256) {
|
||||
throw({forbidden: 'Title too long.'});
|
||||
}
|
||||
if(newDoc.tags !== undefined) {
|
||||
if(toString.call(newDoc.tags) !== '[object Array]') {
|
||||
throw({forbidden: 'Tags must be an array.'});
|
||||
}
|
||||
if(newDoc.tags.length >= 16) {
|
||||
throw({forbidden: 'Too many tags.'});
|
||||
}
|
||||
newDoc.tags.forEach(function(tag) {
|
||||
if(toString.call(tag) !== '[object String]') {
|
||||
throw({forbidden: 'Tags must contain strings only.'});
|
||||
}
|
||||
if(!tag) {
|
||||
throw({forbidden: 'Tag is empty.'});
|
||||
}
|
||||
if(tag.length > 32) {
|
||||
throw({forbidden: 'Tag is too long.'});
|
||||
}
|
||||
});
|
||||
}
|
||||
var attachment = (newDoc._attachments || {}).content;
|
||||
if(!attachment) {
|
||||
throw({forbidden: 'Missing attached content.'});
|
||||
}
|
||||
if(attachment.content_type != 'text/plain') {
|
||||
throw({forbidden: 'Invalid content type.'});
|
||||
}
|
||||
if(Object.keys(newDoc._attachments).length > 1) {
|
||||
throw({forbidden: 'Too many attachments.'});
|
||||
}
|
||||
};
|
||||
|
||||
var byUpdate = function(doc) {
|
||||
emit(doc.updated, null);
|
||||
};
|
||||
|
||||
var byTagAndUpdate = function(doc) {
|
||||
doc.tags && doc.tags.forEach(function(tag) {
|
||||
emit([
|
||||
tag,
|
||||
doc.updated
|
||||
], null);
|
||||
});
|
||||
};
|
||||
|
||||
|
||||
if(process.argv.length < 3) {
|
||||
console.error('Missing URL parameter');
|
||||
process.exit(-1);
|
||||
}
|
||||
|
||||
var url = process.argv[2];
|
||||
|
||||
var ddocs = [
|
||||
{
|
||||
path: '/_design/validate',
|
||||
body: {
|
||||
validate_doc_update: validate.toString()
|
||||
}
|
||||
},
|
||||
{
|
||||
path: '/_design/by_update',
|
||||
body: {
|
||||
views: {
|
||||
default: {
|
||||
map: byUpdate.toString()
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
path: '/_design/by_tag_and_update',
|
||||
body: {
|
||||
views: {
|
||||
default: {
|
||||
map: byTagAndUpdate.toString()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
];
|
||||
|
||||
async.each(ddocs, function(ddoc, cb) {
|
||||
|
||||
request.get(url + ddoc.path, function(err, res) {
|
||||
if(res && res.body) {
|
||||
ddoc.body._rev = JSON.parse(res.body)._rev;
|
||||
}
|
||||
request.put({
|
||||
url: url + ddoc.path,
|
||||
json: true,
|
||||
body: ddoc.body
|
||||
}, function(err, res) {
|
||||
if(err) {
|
||||
return cb(res);
|
||||
}
|
||||
if(res.statusCode >= 300) {
|
||||
return cb(res.body);
|
||||
}
|
||||
cb();
|
||||
});
|
||||
});
|
||||
|
||||
}, function(err) {
|
||||
if(err) {
|
||||
console.error(err);
|
||||
} else {
|
||||
console.log('All design documents updated successfully');
|
||||
}
|
||||
});
|
@ -314,7 +314,7 @@ define([
|
||||
// Create discussion index
|
||||
var discussionIndex;
|
||||
do {
|
||||
discussionIndex = utils.randomString() + utils.randomString(); // Increased size to prevent collision
|
||||
discussionIndex = utils.id();
|
||||
} while(_.has(newDiscussionList, discussionIndex));
|
||||
conflict.discussionIndex = discussionIndex;
|
||||
newDiscussionList[discussionIndex] = conflict;
|
||||
|
@ -27,6 +27,7 @@ define([], function() {
|
||||
constants.PICASA_IMPORT_IMG_URL = "/picasaImportImg";
|
||||
constants.SSH_PUBLISH_URL = '/sshPublish';
|
||||
constants.PDF_EXPORT_URL = "/pdfExport";
|
||||
constants.COUCHDB_URL = 'http://localhost:5984/documents';
|
||||
|
||||
// Site dependent
|
||||
constants.BASE_URL = "http://localhost/";
|
||||
|
@ -55,7 +55,7 @@ define([
|
||||
return;
|
||||
}
|
||||
if(windowId === undefined) {
|
||||
windowId = utils.randomString();
|
||||
windowId = utils.id();
|
||||
storage.frontWindowId = windowId;
|
||||
}
|
||||
var frontWindowId = storage.frontWindowId;
|
||||
|
@ -381,7 +381,7 @@ define([
|
||||
// Create discussion index
|
||||
var discussionIndex;
|
||||
do {
|
||||
discussionIndex = utils.randomString();
|
||||
discussionIndex = utils.id();
|
||||
} while(_.has(discussionList, discussionIndex));
|
||||
discussion.discussionIndex = discussionIndex;
|
||||
discussionList[discussionIndex] = discussion;
|
||||
|
@ -7,7 +7,7 @@ define([
|
||||
"classes/Extension",
|
||||
"classes/FolderDescriptor",
|
||||
"folderList",
|
||||
"fileSystem",
|
||||
"fileSystem"
|
||||
], function($, _, constants, utils, storage, Extension, FolderDescriptor, folderList, fileSystem) {
|
||||
|
||||
var documentManager = new Extension("documentManager", 'Document Manager', false, true);
|
||||
@ -39,17 +39,17 @@ define([
|
||||
'<button class="btn btn-default button-delete" title="Delete"><i class="icon-trash"></i></button>',
|
||||
'<button class="btn btn-default button-rename" title="Rename"><i class="icon-pencil"></i></button>',
|
||||
'<div class="name"><%= fileDesc.composeTitle() %></div>',
|
||||
'<input type="text" class="input-rename form-control hide"></li>',
|
||||
'<input type="text" class="input-rename form-control hide"></li>'
|
||||
].join('');
|
||||
var selectFolderEltTmpl = [
|
||||
'<a href="#" class="list-group-item folder clearfix" data-folder-index="<%= folderDesc.folderIndex %>">',
|
||||
'<div class="pull-right file-count"><%= _.size(folderDesc.fileList) %></div>',
|
||||
'<div class="name"><i class="icon-forward"></i> ',
|
||||
'<%= folderDesc.name %></div></a>',
|
||||
'<%= folderDesc.name %></div></a>'
|
||||
].join('');
|
||||
var selectedDocumentEltTmpl = [
|
||||
'<li class="list-group-item file clearfix">',
|
||||
'<div class="name"><%= fileDesc.composeTitle() %></div></li>',
|
||||
'<div class="name"><%= fileDesc.composeTitle() %></div></li>'
|
||||
].join('');
|
||||
|
||||
var isVisible;
|
||||
@ -86,7 +86,7 @@ define([
|
||||
return fileDesc.title.toLowerCase();
|
||||
}).reduce(function(result, fileDesc) {
|
||||
return result + _.template(selectedDocumentEltTmpl, {
|
||||
fileDesc: fileDesc,
|
||||
fileDesc: fileDesc
|
||||
});
|
||||
}, '').value();
|
||||
selectedDocumentListElt.innerHTML = '<ul class="file-list nav">' + selectedDocumentListHtml + '</ul>';
|
||||
@ -147,7 +147,7 @@ define([
|
||||
_.size(orphanDocumentList),
|
||||
'</div>',
|
||||
'<div class="name"><i class="icon-folder"></i> ',
|
||||
'ROOT folder</div></a>',
|
||||
'ROOT folder</div></a>'
|
||||
].join('');
|
||||
|
||||
// Add orphan documents
|
||||
@ -155,7 +155,7 @@ define([
|
||||
return fileDesc.title.toLowerCase();
|
||||
}).reduce(function(result, fileDesc) {
|
||||
return result + _.template(documentEltTmpl, {
|
||||
fileDesc: fileDesc,
|
||||
fileDesc: fileDesc
|
||||
});
|
||||
}, '').value();
|
||||
orphanListHtml = orphanListHtml && '<ul class="nav">' + orphanListHtml + '</ul>';
|
||||
@ -169,7 +169,7 @@ define([
|
||||
return fileDesc.title.toLowerCase();
|
||||
}).reduce(function(result, fileDesc) {
|
||||
return result + _.template(documentEltTmpl, {
|
||||
fileDesc: fileDesc,
|
||||
fileDesc: fileDesc
|
||||
});
|
||||
}, '').value();
|
||||
fileListHtml = fileListHtml && '<ul class="nav">' + fileListHtml + '</ul>';
|
||||
@ -224,7 +224,7 @@ define([
|
||||
$(modalElt.querySelectorAll('.action-create-folder')).click(function() {
|
||||
var folderIndex;
|
||||
do {
|
||||
folderIndex = "folder." + utils.randomString();
|
||||
folderIndex = "folder." + utils.id();
|
||||
} while (_.has(folderList, folderIndex));
|
||||
|
||||
storage[folderIndex + ".name"] = constants.DEFAULT_FOLDER_NAME;
|
||||
@ -286,13 +286,13 @@ define([
|
||||
_.size(orphanDocumentList),
|
||||
'</div>',
|
||||
'<div class="name"><i class="icon-forward"></i> ',
|
||||
'ROOT folder</div></a>',
|
||||
'ROOT folder</div></a>'
|
||||
].join('');
|
||||
selectFolderListHtml += _.chain(folderList).sortBy(function(folderDesc) {
|
||||
return folderDesc.name.toLowerCase();
|
||||
}).reduce(function(result, folderDesc) {
|
||||
return result + _.template(selectFolderEltTmpl, {
|
||||
folderDesc: folderDesc,
|
||||
folderDesc: folderDesc
|
||||
});
|
||||
}, '').value();
|
||||
selectFolderListElt.innerHTML = selectFolderListHtml;
|
||||
|
@ -68,7 +68,7 @@ define([
|
||||
var fileIndex = constants.TEMPORARY_FILE_INDEX;
|
||||
if(!isTemporary) {
|
||||
do {
|
||||
fileIndex = "file." + utils.randomString();
|
||||
fileIndex = "file." + utils.id();
|
||||
} while(_.has(fileSystem, fileIndex));
|
||||
}
|
||||
|
||||
|
248
public/res/helpers/couchdbHelper.js
Normal file
248
public/res/helpers/couchdbHelper.js
Normal file
@ -0,0 +1,248 @@
|
||||
define([
|
||||
"jquery",
|
||||
"underscore",
|
||||
"constants",
|
||||
"core",
|
||||
"utils",
|
||||
"storage",
|
||||
"logger",
|
||||
"settings",
|
||||
"eventMgr",
|
||||
"classes/AsyncTask"
|
||||
], function($, _, constants, core, utils, storage, logger, settings, eventMgr, AsyncTask) {
|
||||
|
||||
var couchdbHelper = {};
|
||||
|
||||
// Listen to offline status changes
|
||||
var isOffline = false;
|
||||
eventMgr.addListener("onOfflineChanged", function(isOfflineParam) {
|
||||
isOffline = isOfflineParam;
|
||||
});
|
||||
|
||||
couchdbHelper.uploadDocument = function(documentId, title, content, tags, rev, callback) {
|
||||
var result;
|
||||
var task = new AsyncTask();
|
||||
task.onRun(function() {
|
||||
if(tags) {
|
||||
// Has to be an array
|
||||
if(!_.isArray(tags)) {
|
||||
tags = _.chain(('' + tags).split(/\s+/))
|
||||
.compact()
|
||||
.unique()
|
||||
.value();
|
||||
}
|
||||
// Remove invalid tags
|
||||
tags = tags.filter(function(tag) {
|
||||
return _.isString(tag) && tag.length < 32;
|
||||
});
|
||||
// Limit the number of tags
|
||||
tags = tags.slice(0, 16);
|
||||
}
|
||||
else {
|
||||
tags = undefined;
|
||||
}
|
||||
$.ajax({
|
||||
type: 'POST',
|
||||
url: constants.COUCHDB_URL,
|
||||
contentType: 'application/json',
|
||||
dataType: 'json',
|
||||
data: JSON.stringify({
|
||||
_id: documentId || utils.id(),
|
||||
title: title,
|
||||
tags: tags,
|
||||
updated: Date.now(),
|
||||
_rev: rev,
|
||||
_attachments: {
|
||||
content: {
|
||||
content_type: 'text\/plain',
|
||||
data: utils.encodeBase64(content)
|
||||
}
|
||||
}
|
||||
})
|
||||
}).done(function(data) {
|
||||
result = data;
|
||||
task.chain();
|
||||
}).fail(function(jqXHR) {
|
||||
handleError(jqXHR, task);
|
||||
});
|
||||
});
|
||||
task.onSuccess(function() {
|
||||
callback(undefined, result);
|
||||
});
|
||||
task.onError(function(error) {
|
||||
callback(error);
|
||||
});
|
||||
task.enqueue();
|
||||
};
|
||||
|
||||
couchdbHelper.checkChanges = function(lastChangeId, syncLocations, callback) {
|
||||
var changes;
|
||||
var newChangeId = lastChangeId || 0;
|
||||
var task = new AsyncTask();
|
||||
task.onRun(function() {
|
||||
$.ajax({
|
||||
type: 'POST',
|
||||
url: constants.COUCHDB_URL + '/_changes?' + $.param({
|
||||
filter: '_doc_ids',
|
||||
since: newChangeId,
|
||||
include_docs: true,
|
||||
attachments: true
|
||||
}),
|
||||
contentType: 'application/json',
|
||||
dataType: 'json',
|
||||
data: JSON.stringify({
|
||||
doc_ids: Object.keys(syncLocations)
|
||||
})
|
||||
}).done(function(data) {
|
||||
newChangeId = data.last_seq;
|
||||
changes = _.map(data.results, function(result) {
|
||||
return result.deleted ? {
|
||||
_id: result.id,
|
||||
deleted: true
|
||||
} : result.doc;
|
||||
});
|
||||
task.chain();
|
||||
}).fail(function(jqXHR) {
|
||||
handleError(jqXHR, task);
|
||||
});
|
||||
});
|
||||
task.onSuccess(function() {
|
||||
callback(undefined, changes, newChangeId);
|
||||
});
|
||||
task.onError(function(error) {
|
||||
callback(error);
|
||||
});
|
||||
task.enqueue();
|
||||
};
|
||||
|
||||
couchdbHelper.downloadContent = function(documents, callback) {
|
||||
var result = [];
|
||||
var task = new AsyncTask();
|
||||
task.onRun(function() {
|
||||
function recursiveDownloadContent() {
|
||||
if(documents.length === 0) {
|
||||
return task.chain();
|
||||
}
|
||||
var document = documents[0];
|
||||
result.push(document);
|
||||
if(document.deleted || ((document._attachments || {}).content || {}).data !== undefined) {
|
||||
documents.shift();
|
||||
return task.chain(recursiveDownloadContent);
|
||||
}
|
||||
$.ajax({
|
||||
url: constants.COUCHDB_URL + '/' + encodeURIComponent(document._id),
|
||||
headers: {
|
||||
Accept: 'application/json'
|
||||
},
|
||||
contentType: 'application/json',
|
||||
dataType: 'json',
|
||||
data: {
|
||||
attachments: true
|
||||
}
|
||||
}).done(function(doc) {
|
||||
documents.shift();
|
||||
_.extend(document, doc);
|
||||
task.chain(recursiveDownloadContent);
|
||||
}).fail(function(jqXHR) {
|
||||
handleError(jqXHR, task);
|
||||
});
|
||||
}
|
||||
|
||||
task.chain(recursiveDownloadContent);
|
||||
});
|
||||
task.onSuccess(function() {
|
||||
callback(undefined, result);
|
||||
});
|
||||
task.onError(function(error) {
|
||||
callback(error);
|
||||
});
|
||||
task.enqueue();
|
||||
};
|
||||
|
||||
couchdbHelper.listDocuments = function(tag, updated, callback) {
|
||||
var result;
|
||||
var task = new AsyncTask();
|
||||
task.onRun(function() {
|
||||
var ddoc = '/_design/by_' + (tag ? 'tag_and_' : '') + 'update/_view/default';
|
||||
var startKey = tag ? JSON.stringify([
|
||||
tag,
|
||||
updated || []
|
||||
]) : updated;
|
||||
var endKey = tag && JSON.stringify([
|
||||
tag
|
||||
]);
|
||||
$.ajax({
|
||||
url: constants.COUCHDB_URL + ddoc,
|
||||
data: {
|
||||
start_key: startKey,
|
||||
end_key: endKey,
|
||||
descending: true,
|
||||
include_docs: true,
|
||||
limit: 3,
|
||||
reduce: false
|
||||
},
|
||||
dataType: 'json'
|
||||
}).done(function(data) {
|
||||
result = _.pluck(data.rows, 'doc');
|
||||
task.chain();
|
||||
}).fail(function(jqXHR) {
|
||||
handleError(jqXHR, task);
|
||||
});
|
||||
});
|
||||
task.onSuccess(function() {
|
||||
callback(undefined, result);
|
||||
});
|
||||
task.onError(function(error) {
|
||||
callback(error);
|
||||
});
|
||||
task.enqueue();
|
||||
};
|
||||
|
||||
couchdbHelper.deleteDocuments = function(docs) {
|
||||
var task = new AsyncTask();
|
||||
task.onRun(function() {
|
||||
$.ajax({
|
||||
type: 'POST',
|
||||
url: constants.COUCHDB_URL + '/_bulk_docs',
|
||||
data: JSON.stringify({
|
||||
docs: docs.map(function(doc) {
|
||||
return {
|
||||
_id: doc._id,
|
||||
_rev: doc._rev,
|
||||
_deleted: true
|
||||
};
|
||||
})
|
||||
}),
|
||||
contentType: 'application/json',
|
||||
dataType: 'json'
|
||||
}).done(function() {
|
||||
task.chain();
|
||||
}).fail(function(jqXHR) {
|
||||
handleError(jqXHR, task);
|
||||
});
|
||||
});
|
||||
task.enqueue();
|
||||
};
|
||||
|
||||
function handleError(jqXHR, task) {
|
||||
var error = {
|
||||
code: jqXHR.status,
|
||||
message: jqXHR.statusText,
|
||||
reason: (jqXHR.responseJSON || {}).reason
|
||||
};
|
||||
var errorMsg;
|
||||
if(error) {
|
||||
logger.error(error);
|
||||
// Try to analyze the error
|
||||
if(typeof error === "string") {
|
||||
errorMsg = error;
|
||||
}
|
||||
else {
|
||||
errorMsg = "Error " + error.code + ": " + (error.reason || error.message);
|
||||
}
|
||||
}
|
||||
task.error(new Error(errorMsg));
|
||||
}
|
||||
|
||||
return couchdbHelper;
|
||||
});
|
@ -1,6 +1,6 @@
|
||||
<div class="layout-wrapper-l1">
|
||||
<div class="layout-wrapper-l2">
|
||||
<div class="navbar navbar-default ui-layout-north">
|
||||
<div class="navbar navbar-default">
|
||||
<div class="navbar-inner">
|
||||
<div class="nav left-space"></div>
|
||||
<div class="nav right-space pull-right"></div>
|
||||
@ -50,7 +50,7 @@
|
||||
</div>
|
||||
</div>
|
||||
<div class="layout-wrapper-l3">
|
||||
<pre id="wmd-input" class="ui-layout-center form-control"><div class="editor-content" contenteditable=true></div><div class="editor-margin"></div></pre>
|
||||
<pre id="wmd-input" class="form-control"><div class="editor-content" contenteditable=true></div><div class="editor-margin"></div></pre>
|
||||
<div class="preview-panel">
|
||||
<div class="layout-resizer layout-resizer-preview"></div>
|
||||
<div class="layout-toggler layout-toggler-navbar btn btn-info" title="Toggle navigation bar"><i class="icon-th"></i></div>
|
||||
@ -89,7 +89,7 @@
|
||||
<a href="#" data-toggle="collapse" data-target=".collapse-synchronize"
|
||||
class="list-group-item">
|
||||
<div><i class="icon-refresh"></i> Synchronize</div>
|
||||
<small>Backup, collaborate...</small>
|
||||
<small>Open/save in the Cloud</small>
|
||||
</a>
|
||||
<div class="sub-menu collapse collapse-synchronize clearfix">
|
||||
<ul class="nav alert alert-danger show-already-synchronized">
|
||||
@ -101,6 +101,10 @@
|
||||
class="icon-refresh"></i> Manage synchronization</a></li>
|
||||
</ul>
|
||||
<ul class="nav">
|
||||
<li><a href="#" class="action-sync-import-dialog-couchdb"><i
|
||||
class="icon-provider-couchdb"></i> Open from CouchDB <sup class="text-danger">beta</sup></a></li>
|
||||
<li><a href="#" class="action-sync-export-dialog-couchdb"><i
|
||||
class="icon-provider-couchdb"></i> Save on CouchDB <sup class="text-danger">beta</sup></a></li>
|
||||
<li><a href="#" class="action-sync-import-dropbox"><i
|
||||
class="icon-provider-dropbox"></i> Open from Dropbox</a></li>
|
||||
<li><a href="#" class="action-sync-export-dialog-dropbox"><i
|
||||
@ -544,6 +548,110 @@
|
||||
</div>
|
||||
|
||||
|
||||
<div class="modal fade modal-download-couchdb">
|
||||
<div class="modal-dialog">
|
||||
<div class="modal-content">
|
||||
|
||||
<div class="modal-header">
|
||||
<button type="button" class="close" data-dismiss="modal"
|
||||
aria-hidden="true">×</button>
|
||||
<h2 class="modal-title">Open from CouchDB</h2>
|
||||
<br>
|
||||
<div class="form-horizontal">
|
||||
<div class="form-group">
|
||||
<label for="select-sync-import-couchdb-tag" class="col-sm-3 control-label">Filter by tag</label>
|
||||
<div class="col-sm-5">
|
||||
<select id="select-sync-import-couchdb-tag" class="form-control">
|
||||
<option value="">None</option>
|
||||
</select>
|
||||
</div>
|
||||
<div class="col-sm-4">
|
||||
<button class="btn btn-link"><i class="icon-tags"></i> Manage tags</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="modal-body">
|
||||
<div class="form-horizontal hide">
|
||||
<div class="form-group">
|
||||
<label for="input-sync-import-couchdb-documentid" class="col-sm-3 control-label">Document
|
||||
ID</label>
|
||||
|
||||
<div class="col-sm-9">
|
||||
<input id="input-sync-import-couchdb-documentid" class="form-control"
|
||||
placeholder="DocumentID">
|
||||
<span class="help-block">Multiple IDs can be provided (space separated)</span>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<ul class="document-list nav nav-pills">
|
||||
<li class="pull-right dropdown"><a href="#"
|
||||
data-toggle="dropdown"><i class="icon-check"></i> Selection
|
||||
<b class="caret"></b></a>
|
||||
<ul class="dropdown-menu">
|
||||
<li><a href="#" class="action-unselect-all"><i
|
||||
class="icon-check-empty"></i> Unselect all</a></li>
|
||||
<li class="divider"></li>
|
||||
<li><a href="#" class="action-delete-items"><i
|
||||
class="icon-trash"></i> Delete</a></li>
|
||||
</ul>
|
||||
</li>
|
||||
</ul>
|
||||
<p class="document-list">
|
||||
</p>
|
||||
<div class="list-group document-list"></div>
|
||||
<div class="document-list">
|
||||
<div class="please-wait">Please wait...</div>
|
||||
<button class="more-documents btn btn-link">More documents!</button>
|
||||
</div>
|
||||
<p class="confirm-delete hide">The following documents will be
|
||||
removed from the database:</p>
|
||||
|
||||
<div class="confirm-delete list-group selected-document-list hide"></div>
|
||||
</div>
|
||||
<div class="modal-footer">
|
||||
<a href="#"
|
||||
class="btn btn-default confirm-delete action-cancel hide">Cancel</a>
|
||||
<a href="#"
|
||||
class="btn btn-primary confirm-delete action-delete-items-confirm hide">Delete</a>
|
||||
<a href="#" class="btn btn-default document-list" data-dismiss="modal">Cancel</a>
|
||||
<a href="#" data-dismiss="modal"
|
||||
class="btn btn-primary action-sync-import-couchdb document-list">Open</a>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
|
||||
<div class="modal fade modal-upload-couchdb">
|
||||
<div class="modal-dialog">
|
||||
<div class="modal-content">
|
||||
|
||||
<div class="modal-header">
|
||||
<button type="button" class="close" data-dismiss="modal"
|
||||
aria-hidden="true">×</button>
|
||||
<h2 class="modal-title">Save on CouchDB</h2>
|
||||
</div>
|
||||
<div class="modal-body">
|
||||
<p>
|
||||
This will save "<span class="file-title"></span>" to CouchDB and keep it synchronized.
|
||||
</p>
|
||||
<blockquote>
|
||||
<b>Tip:</b> You can use a
|
||||
<a href="http://jekyllrb.com/docs/frontmatter/"
|
||||
target="_blank">YAML front matter</a> to specify tags for your document.
|
||||
</blockquote>
|
||||
</div>
|
||||
<div class="modal-footer">
|
||||
<a href="#" class="btn btn-default" data-dismiss="modal">Cancel</a>
|
||||
<a href="#" data-dismiss="modal"
|
||||
class="btn btn-primary action-sync-export-couchdb">OK</a>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
|
||||
<div class="modal fade modal-manage-sync">
|
||||
<div class="modal-dialog">
|
||||
<div class="modal-content">
|
||||
|
@ -1,6 +1,6 @@
|
||||
<div class="layout-wrapper-l1">
|
||||
<div class="layout-wrapper-l2">
|
||||
<div class="navbar navbar-default ui-layout-north">
|
||||
<div class="navbar navbar-default">
|
||||
<div class="navbar-inner">
|
||||
<div class="nav left-space"></div>
|
||||
<div class="nav right-space pull-right"></div>
|
||||
@ -43,7 +43,7 @@
|
||||
</div>
|
||||
</div>
|
||||
<div class="layout-wrapper-l3">
|
||||
<pre id="wmd-input" class="ui-layout-center form-control"><div class="editor-content"></div><div class="editor-margin"></div></pre>
|
||||
<pre id="wmd-input" class="form-control"><div class="editor-content"></div><div class="editor-margin"></div></pre>
|
||||
<div class="preview-panel">
|
||||
<div class="preview-container">
|
||||
<div id="preview-contents">
|
||||
|
Binary file not shown.
Before Width: | Height: | Size: 5.0 KiB After Width: | Height: | Size: 4.9 KiB |
Binary file not shown.
Before Width: | Height: | Size: 12 KiB After Width: | Height: | Size: 12 KiB |
297
public/res/providers/couchdbProvider.js
Normal file
297
public/res/providers/couchdbProvider.js
Normal file
@ -0,0 +1,297 @@
|
||||
define([
|
||||
"jquery",
|
||||
"underscore",
|
||||
"constants",
|
||||
"utils",
|
||||
"storage",
|
||||
"logger",
|
||||
"classes/Provider",
|
||||
"settings",
|
||||
"eventMgr",
|
||||
"fileMgr",
|
||||
"fileSystem",
|
||||
"editor",
|
||||
"helpers/couchdbHelper"
|
||||
], function($, _, constants, utils, storage, logger, Provider, settings, eventMgr, fileMgr, fileSystem, editor, couchdbHelper) {
|
||||
|
||||
var PROVIDER_COUCHDB = "couchdb";
|
||||
|
||||
var couchdbProvider = new Provider(PROVIDER_COUCHDB, "CouchDB");
|
||||
|
||||
function createSyncIndex(id) {
|
||||
return "sync." + PROVIDER_COUCHDB + "." + id;
|
||||
}
|
||||
|
||||
var merge = settings.conflictMode == 'merge';
|
||||
|
||||
function createSyncAttributes(id, rev, content, title, discussionListJSON) {
|
||||
discussionListJSON = discussionListJSON || '{}';
|
||||
var syncAttributes = {};
|
||||
syncAttributes.provider = couchdbProvider;
|
||||
syncAttributes.id = id;
|
||||
syncAttributes.rev = rev;
|
||||
syncAttributes.contentCRC = utils.crc32(content);
|
||||
syncAttributes.titleCRC = utils.crc32(title);
|
||||
syncAttributes.discussionListCRC = utils.crc32(discussionListJSON);
|
||||
syncAttributes.syncIndex = createSyncIndex(id);
|
||||
if(merge === true) {
|
||||
// Need to store the whole content for merge
|
||||
syncAttributes.content = content;
|
||||
syncAttributes.title = title;
|
||||
syncAttributes.discussionList = discussionListJSON;
|
||||
}
|
||||
return syncAttributes;
|
||||
}
|
||||
|
||||
function importFilesFromIds(ids) {
|
||||
couchdbHelper.downloadContent(ids.map(function(id) {
|
||||
return {
|
||||
_id: id
|
||||
};
|
||||
}), function(error, result) {
|
||||
if(error) {
|
||||
return;
|
||||
}
|
||||
var fileDescList = [];
|
||||
var fileDesc;
|
||||
_.each(result, function(file) {
|
||||
var content = utils.decodeBase64(file._attachments.content.data);
|
||||
var parsedContent = couchdbProvider.parseContent(content);
|
||||
var syncLocations;
|
||||
var syncAttributes = createSyncAttributes(file._id, file._rev, parsedContent.content, file.title, parsedContent.discussionListJSON);
|
||||
syncLocations = {};
|
||||
syncLocations[syncAttributes.syncIndex] = syncAttributes;
|
||||
fileDesc = fileMgr.createFile(file.title, parsedContent.content, parsedContent.discussionListJSON, syncLocations);
|
||||
fileDescList.push(fileDesc);
|
||||
});
|
||||
if(fileDesc !== undefined) {
|
||||
eventMgr.onSyncImportSuccess(fileDescList, couchdbProvider);
|
||||
fileMgr.selectFile(fileDesc);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
couchdbProvider.importFiles = function() {
|
||||
var tag = $('#select-sync-import-couchdb-tag').val();
|
||||
if(!tag) {
|
||||
var ids = _.chain(($('#input-sync-import-couchdb-documentid').val() || '').split(/\s+/))
|
||||
.compact()
|
||||
.unique()
|
||||
.value();
|
||||
var importIds = [];
|
||||
_.each(ids, function(id) {
|
||||
var syncIndex = createSyncIndex(id);
|
||||
var fileDesc = fileMgr.getFileFromSyncIndex(syncIndex);
|
||||
if(fileDesc !== undefined) {
|
||||
return eventMgr.onError('"' + fileDesc.title + '" is already in your local documents.');
|
||||
}
|
||||
importIds.push(id);
|
||||
});
|
||||
importFilesFromIds(importIds);
|
||||
}
|
||||
};
|
||||
|
||||
couchdbProvider.exportFile = function(event, title, content, discussionListJSON, frontMatter, callback) {
|
||||
var data = couchdbProvider.serializeContent(content, discussionListJSON);
|
||||
var tags = frontMatter && frontMatter.tags;
|
||||
couchdbHelper.uploadDocument(undefined, title, data, tags, undefined, function(error, result) {
|
||||
if(error) {
|
||||
return callback(error);
|
||||
}
|
||||
var syncAttributes = createSyncAttributes(result.id, result.rev, content, title, discussionListJSON);
|
||||
callback(undefined, syncAttributes);
|
||||
});
|
||||
};
|
||||
|
||||
couchdbProvider.syncUp = function(content, contentCRC, title, titleCRC, discussionList, discussionListCRC, frontMatter, syncAttributes, callback) {
|
||||
if(
|
||||
(syncAttributes.contentCRC == contentCRC) && // Content CRC hasn't changed
|
||||
(syncAttributes.titleCRC == titleCRC) && // Title CRC hasn't changed
|
||||
(syncAttributes.discussionListCRC == discussionListCRC) // Discussion list CRC hasn't changed
|
||||
) {
|
||||
return callback(undefined, false);
|
||||
}
|
||||
|
||||
var data = couchdbProvider.serializeContent(content, discussionList);
|
||||
var tags = frontMatter && frontMatter.tags;
|
||||
couchdbHelper.uploadDocument(syncAttributes.id, title, data, tags, syncAttributes.rev, function(error, result) {
|
||||
if(error) {
|
||||
return callback(error, true);
|
||||
}
|
||||
syncAttributes.rev = result.rev;
|
||||
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;
|
||||
callback(undefined, true);
|
||||
});
|
||||
};
|
||||
|
||||
couchdbProvider.syncDown = function(callback) {
|
||||
var lastChangeId = parseInt(storage[PROVIDER_COUCHDB + ".lastChangeId"], 10);
|
||||
var syncLocations = {};
|
||||
_.each(fileSystem, function(fileDesc) {
|
||||
_.each(fileDesc.syncLocations, function(syncAttributes) {
|
||||
syncAttributes.provider === couchdbProvider && (syncLocations[syncAttributes.id] = syncAttributes);
|
||||
});
|
||||
});
|
||||
couchdbHelper.checkChanges(lastChangeId, syncLocations, function(error, changes, newChangeId) {
|
||||
if(error) {
|
||||
return callback(error);
|
||||
}
|
||||
var interestingChanges = [];
|
||||
_.each(changes, function(change) {
|
||||
var syncIndex = createSyncIndex(change._id);
|
||||
var fileDesc = fileMgr.getFileFromSyncIndex(syncIndex);
|
||||
var syncAttributes = fileDesc && fileDesc.syncLocations[syncIndex];
|
||||
if(!syncAttributes) {
|
||||
return;
|
||||
}
|
||||
// Store fileDesc and syncAttributes references to avoid 2 times search
|
||||
change.fileDesc = fileDesc;
|
||||
change.syncAttributes = syncAttributes;
|
||||
interestingChanges.push(change);
|
||||
});
|
||||
couchdbHelper.downloadContent(interestingChanges, function(error, changes) {
|
||||
if(error) {
|
||||
return callback(error);
|
||||
}
|
||||
function mergeChange() {
|
||||
if(changes.length === 0) {
|
||||
storage[PROVIDER_COUCHDB + ".lastChangeId"] = newChangeId;
|
||||
return callback();
|
||||
}
|
||||
var change = changes.pop();
|
||||
var fileDesc = change.fileDesc;
|
||||
var syncAttributes = change.syncAttributes;
|
||||
// File deleted
|
||||
if(change.deleted === true) {
|
||||
eventMgr.onError('"' + fileDesc.title + '" has been removed from CouchDB.');
|
||||
fileDesc.removeSyncLocation(syncAttributes);
|
||||
return eventMgr.onSyncRemoved(fileDesc, syncAttributes);
|
||||
}
|
||||
var file = change;
|
||||
var content = utils.decodeBase64(file._attachments.content.data);
|
||||
var parsedContent = couchdbProvider.parseContent(content);
|
||||
var remoteContent = parsedContent.content;
|
||||
var remoteTitle = file.title;
|
||||
var remoteDiscussionListJSON = parsedContent.discussionListJSON;
|
||||
var remoteDiscussionList = parsedContent.discussionList;
|
||||
var remoteCRC = couchdbProvider.syncMerge(fileDesc, syncAttributes, remoteContent, remoteTitle, remoteDiscussionList, remoteDiscussionListJSON);
|
||||
|
||||
// Update syncAttributes
|
||||
syncAttributes.rev = file._rev;
|
||||
if(merge === true) {
|
||||
// Need to store the whole content for merge
|
||||
syncAttributes.content = remoteContent;
|
||||
syncAttributes.title = remoteTitle;
|
||||
syncAttributes.discussionList = remoteDiscussionListJSON;
|
||||
}
|
||||
syncAttributes.contentCRC = remoteCRC.contentCRC;
|
||||
syncAttributes.titleCRC = remoteCRC.titleCRC;
|
||||
syncAttributes.discussionListCRC = remoteCRC.discussionListCRC;
|
||||
utils.storeAttributes(syncAttributes);
|
||||
setTimeout(mergeChange, 5);
|
||||
}
|
||||
|
||||
setTimeout(mergeChange, 5);
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
var documentEltTmpl = [
|
||||
'<a href="#" class="list-group-item document clearfix" data-document-id="<%= document._id %>">',
|
||||
'<div class="date pull-right"><%= date %></div></div>',
|
||||
'<div class="name"><i class="icon-provider-couchdb"></i> ',
|
||||
'<%= document.title %></div>',
|
||||
'</a>'
|
||||
].join('');
|
||||
|
||||
|
||||
eventMgr.addListener("onReady", function() {
|
||||
var modalElt = document.querySelector('.modal-download-couchdb');
|
||||
var $documentListElt = $(modalElt.querySelector('.list-group.document-list'));
|
||||
var $selectedDocumentListElt = $(modalElt.querySelector('.selected-document-list'));
|
||||
var $pleaseWaitElt = $(modalElt.querySelector('.please-wait'));
|
||||
var $moreDocumentsElt = $(modalElt.querySelector('.more-documents'));
|
||||
var documentMap, lastDocument;
|
||||
var selectedDocuments, $selectedElts;
|
||||
function doSelect() {
|
||||
$selectedElts = $documentListElt.children('.active').clone();
|
||||
selectedDocuments = [];
|
||||
$selectedElts.each(function() {
|
||||
selectedDocuments.push(documentMap[$(this).data('documentId')]);
|
||||
});
|
||||
$selectedDocumentListElt.empty().append($selectedElts);
|
||||
$(modalElt.querySelectorAll('.action-delete-items')).parent().toggleClass('disabled', selectedDocuments.length === 0);
|
||||
}
|
||||
function clear() {
|
||||
documentMap = {};
|
||||
lastDocument = undefined;
|
||||
$documentListElt.empty();
|
||||
doSelect();
|
||||
}
|
||||
clear();
|
||||
function deleteMode(enabled) {
|
||||
$(modalElt.querySelectorAll('.confirm-delete')).toggleClass('hide', !enabled);
|
||||
$(modalElt.querySelectorAll('.document-list')).toggleClass('hide', enabled);
|
||||
}
|
||||
function updateDocumentList() {
|
||||
$pleaseWaitElt.removeClass('hide');
|
||||
$moreDocumentsElt.addClass('hide');
|
||||
couchdbHelper.listDocuments(undefined, lastDocument && lastDocument.updated, function(err, result) {
|
||||
if(err) {
|
||||
return;
|
||||
}
|
||||
$pleaseWaitElt.addClass('hide');
|
||||
if(result.length === 3) {
|
||||
$moreDocumentsElt.removeClass('hide');
|
||||
lastDocument = result.pop();
|
||||
}
|
||||
var documentListHtml = _.reduce(result, function(result, document) {
|
||||
documentMap[document._id] = document;
|
||||
return result + _.template(documentEltTmpl, {
|
||||
document: document,
|
||||
date: utils.formatDate(document.updated)
|
||||
});
|
||||
}, '');
|
||||
|
||||
$documentListElt.append(documentListHtml);
|
||||
});
|
||||
deleteMode(false);
|
||||
}
|
||||
$(modalElt)
|
||||
.on('show.bs.modal', updateDocumentList)
|
||||
.on('hidden.bs.modal', clear)
|
||||
.on('click', '.document-list .document', function() {
|
||||
$(this).toggleClass('active');
|
||||
doSelect();
|
||||
})
|
||||
.on('click', '.more-documents', updateDocumentList)
|
||||
.on('click', '.action-unselect-all', function() {
|
||||
$documentListElt.children().removeClass('active');
|
||||
doSelect();
|
||||
})
|
||||
.on('click', '.action-delete-items', function() {
|
||||
doSelect();
|
||||
if($selectedElts.length) {
|
||||
deleteMode(true);
|
||||
}
|
||||
})
|
||||
.on('click', '.action-delete-items-confirm', function() {
|
||||
couchdbHelper.deleteDocuments(selectedDocuments);
|
||||
clear();
|
||||
updateDocumentList();
|
||||
})
|
||||
.on('click', '.action-cancel', function() {
|
||||
deleteMode(false);
|
||||
});
|
||||
});
|
||||
|
||||
return couchdbProvider;
|
||||
});
|
@ -94,7 +94,7 @@ define([
|
||||
});
|
||||
};
|
||||
|
||||
dropboxProvider.exportFile = function(event, title, content, discussionListJSON, callback) {
|
||||
dropboxProvider.exportFile = function(event, title, content, discussionListJSON, frontMatter, callback) {
|
||||
var path = utils.getInputTextValue("#input-sync-export-dropbox-path", event);
|
||||
path = checkPath(path);
|
||||
if(path === undefined) {
|
||||
@ -118,7 +118,7 @@ define([
|
||||
});
|
||||
};
|
||||
|
||||
dropboxProvider.syncUp = function(content, contentCRC, title, titleCRC, discussionList, discussionListCRC, syncAttributes, callback) {
|
||||
dropboxProvider.syncUp = function(content, contentCRC, title, titleCRC, discussionList, discussionListCRC, frontMatter, syncAttributes, callback) {
|
||||
if(
|
||||
(syncAttributes.contentCRC == contentCRC) && // Content CRC hasn't changed
|
||||
(syncAttributes.discussionListCRC == discussionListCRC) // Discussion list CRC hasn't changed
|
||||
|
@ -1,390 +1,394 @@
|
||||
define([
|
||||
"jquery",
|
||||
"underscore",
|
||||
"constants",
|
||||
"utils",
|
||||
"storage",
|
||||
"logger",
|
||||
"classes/Provider",
|
||||
"settings",
|
||||
"eventMgr",
|
||||
"fileMgr",
|
||||
"editor",
|
||||
"helpers/googleHelper",
|
||||
"text!html/dialogExportGdrive.html",
|
||||
"text!html/dialogAutoSyncGdrive.html",
|
||||
"jquery",
|
||||
"underscore",
|
||||
"constants",
|
||||
"utils",
|
||||
"storage",
|
||||
"logger",
|
||||
"classes/Provider",
|
||||
"settings",
|
||||
"eventMgr",
|
||||
"fileMgr",
|
||||
"editor",
|
||||
"helpers/googleHelper",
|
||||
"text!html/dialogExportGdrive.html",
|
||||
"text!html/dialogAutoSyncGdrive.html"
|
||||
], function($, _, constants, utils, storage, logger, Provider, settings, eventMgr, fileMgr, editor, googleHelper, dialogExportGdriveHTML, dialogAutoSyncGdriveHTML) {
|
||||
|
||||
return function(providerId, providerName, accountIndex) {
|
||||
var accountId = 'google.gdrive' + accountIndex;
|
||||
return function(providerId, providerName, accountIndex) {
|
||||
var accountId = 'google.gdrive' + accountIndex;
|
||||
|
||||
var gdriveProvider = new Provider(providerId, providerName);
|
||||
gdriveProvider.defaultPublishFormat = "template";
|
||||
gdriveProvider.exportPreferencesInputIds = [
|
||||
providerId + "-parentid",
|
||||
];
|
||||
var gdriveProvider = new Provider(providerId, providerName);
|
||||
gdriveProvider.defaultPublishFormat = "template";
|
||||
gdriveProvider.exportPreferencesInputIds = [
|
||||
providerId + "-parentid",
|
||||
];
|
||||
|
||||
function createSyncIndex(id) {
|
||||
return "sync." + providerId + "." + id;
|
||||
}
|
||||
function createSyncIndex(id) {
|
||||
return "sync." + providerId + "." + id;
|
||||
}
|
||||
|
||||
var merge = settings.conflictMode == 'merge';
|
||||
function createSyncAttributes(id, etag, content, title, discussionListJSON) {
|
||||
discussionListJSON = discussionListJSON || '{}';
|
||||
var syncAttributes = {};
|
||||
syncAttributes.provider = gdriveProvider;
|
||||
syncAttributes.id = id;
|
||||
syncAttributes.etag = etag;
|
||||
syncAttributes.contentCRC = utils.crc32(content);
|
||||
syncAttributes.titleCRC = utils.crc32(title);
|
||||
syncAttributes.discussionListCRC = utils.crc32(discussionListJSON);
|
||||
syncAttributes.syncIndex = createSyncIndex(id);
|
||||
if(merge === true) {
|
||||
// Need to store the whole content for merge
|
||||
syncAttributes.content = content;
|
||||
syncAttributes.title = title;
|
||||
syncAttributes.discussionList = discussionListJSON;
|
||||
}
|
||||
return syncAttributes;
|
||||
}
|
||||
var merge = settings.conflictMode == 'merge';
|
||||
|
||||
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);
|
||||
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;
|
||||
}
|
||||
fileDesc = fileMgr.createFile(file.title, parsedContent.content, parsedContent.discussionListJSON, syncLocations);
|
||||
fileDescList.push(fileDesc);
|
||||
});
|
||||
if(fileDesc !== undefined) {
|
||||
eventMgr.onSyncImportSuccess(fileDescList, gdriveProvider);
|
||||
fileMgr.selectFile(fileDesc);
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
function createSyncAttributes(id, etag, content, title, discussionListJSON) {
|
||||
discussionListJSON = discussionListJSON || '{}';
|
||||
var syncAttributes = {};
|
||||
syncAttributes.provider = gdriveProvider;
|
||||
syncAttributes.id = id;
|
||||
syncAttributes.etag = etag;
|
||||
syncAttributes.contentCRC = utils.crc32(content);
|
||||
syncAttributes.titleCRC = utils.crc32(title);
|
||||
syncAttributes.discussionListCRC = utils.crc32(discussionListJSON);
|
||||
syncAttributes.syncIndex = createSyncIndex(id);
|
||||
if(merge === true) {
|
||||
// Need to store the whole content for merge
|
||||
syncAttributes.content = content;
|
||||
syncAttributes.title = title;
|
||||
syncAttributes.discussionList = discussionListJSON;
|
||||
}
|
||||
return syncAttributes;
|
||||
}
|
||||
|
||||
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) {
|
||||
return eventMgr.onError('"' + fileDesc.title + '" is already in your local documents.');
|
||||
}
|
||||
importIds.push(doc.id);
|
||||
});
|
||||
importFilesFromIds(importIds);
|
||||
}, 'doc', accountId);
|
||||
};
|
||||
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);
|
||||
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;
|
||||
}
|
||||
fileDesc = fileMgr.createFile(file.title, parsedContent.content, parsedContent.discussionListJSON, syncLocations);
|
||||
fileDescList.push(fileDesc);
|
||||
});
|
||||
if(fileDesc !== undefined) {
|
||||
eventMgr.onSyncImportSuccess(fileDescList, gdriveProvider);
|
||||
fileMgr.selectFile(fileDesc);
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
gdriveProvider.exportFile = function(event, title, content, discussionListJSON, 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 + '".');
|
||||
return callback(true);
|
||||
}
|
||||
}
|
||||
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) {
|
||||
if(error) {
|
||||
return callback(error);
|
||||
}
|
||||
var syncAttributes = createSyncAttributes(result.id, result.etag, content, title, discussionListJSON);
|
||||
callback(undefined, syncAttributes);
|
||||
});
|
||||
};
|
||||
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) {
|
||||
return eventMgr.onError('"' + fileDesc.title + '" is already in your local documents.');
|
||||
}
|
||||
importIds.push(doc.id);
|
||||
});
|
||||
importFilesFromIds(importIds);
|
||||
}, 'doc', accountId);
|
||||
};
|
||||
|
||||
gdriveProvider.syncUp = function(content, contentCRC, title, titleCRC, discussionList, discussionListCRC, syncAttributes, callback) {
|
||||
if(
|
||||
(syncAttributes.contentCRC == contentCRC) && // Content CRC hasn't changed
|
||||
(syncAttributes.titleCRC == titleCRC) && // Title CRC hasn't changed
|
||||
(syncAttributes.discussionListCRC == discussionListCRC) // Discussion list CRC hasn't changed
|
||||
) {
|
||||
return callback(undefined, false);
|
||||
}
|
||||
gdriveProvider.exportFile = function(event, title, content, discussionListJSON, frontMatter, 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 + '".');
|
||||
return callback(true);
|
||||
}
|
||||
}
|
||||
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) {
|
||||
if(error) {
|
||||
return callback(error);
|
||||
}
|
||||
var syncAttributes = createSyncAttributes(result.id, result.etag, content, title, discussionListJSON);
|
||||
callback(undefined, syncAttributes);
|
||||
});
|
||||
};
|
||||
|
||||
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.');
|
||||
}
|
||||
gdriveProvider.syncUp = function(content, contentCRC, title, titleCRC, discussionList, discussionListCRC, frontMatter, syncAttributes, callback) {
|
||||
if(
|
||||
(syncAttributes.contentCRC == contentCRC) && // Content CRC hasn't changed
|
||||
(syncAttributes.titleCRC == titleCRC) && // Title CRC hasn't changed
|
||||
(syncAttributes.discussionListCRC == discussionListCRC) // Discussion list CRC hasn't changed
|
||||
) {
|
||||
return callback(undefined, false);
|
||||
}
|
||||
|
||||
var data = gdriveProvider.serializeContent(content, discussionList);
|
||||
googleHelper.upload(syncAttributes.id, undefined, title, data, undefined, syncAttributes.etag, accountId, function(error, result) {
|
||||
if(error) {
|
||||
return callback(error, true);
|
||||
}
|
||||
syncAttributes.etag = result.etag;
|
||||
// Remove this deprecated flag if any
|
||||
delete syncAttributes.isRealtime;
|
||||
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;
|
||||
callback(undefined, true);
|
||||
});
|
||||
};
|
||||
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.');
|
||||
}
|
||||
|
||||
gdriveProvider.syncDown = function(callback) {
|
||||
var lastChangeId = parseInt(storage[accountId + ".gdrive.lastChangeId"], 10);
|
||||
googleHelper.checkChanges(lastChangeId, accountId, function(error, changes, newChangeId) {
|
||||
if(error) {
|
||||
return callback(error);
|
||||
}
|
||||
var interestingChanges = [];
|
||||
_.each(changes, function(change) {
|
||||
var syncIndex = createSyncIndex(change.fileId);
|
||||
var fileDesc = fileMgr.getFileFromSyncIndex(syncIndex);
|
||||
var syncAttributes = fileDesc && fileDesc.syncLocations[syncIndex];
|
||||
if(!syncAttributes) {
|
||||
return;
|
||||
}
|
||||
// Store fileDesc and syncAttributes references to avoid 2 times search
|
||||
change.fileDesc = fileDesc;
|
||||
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;
|
||||
}
|
||||
function mergeChange() {
|
||||
if(changes.length === 0) {
|
||||
storage[accountId + ".gdrive.lastChangeId"] = newChangeId;
|
||||
return callback();
|
||||
}
|
||||
var change = changes.pop();
|
||||
var fileDesc = change.fileDesc;
|
||||
var syncAttributes = change.syncAttributes;
|
||||
// File deleted
|
||||
if(change.deleted === true) {
|
||||
eventMgr.onError('"' + fileDesc.title + '" has been removed from ' + providerName + '.');
|
||||
fileDesc.removeSyncLocation(syncAttributes);
|
||||
return eventMgr.onSyncRemoved(fileDesc, syncAttributes);
|
||||
}
|
||||
var file = change.file;
|
||||
var parsedContent = gdriveProvider.parseContent(file.content);
|
||||
var remoteContent = parsedContent.content;
|
||||
var remoteTitle = file.title;
|
||||
var remoteDiscussionListJSON = parsedContent.discussionListJSON;
|
||||
var remoteDiscussionList = parsedContent.discussionList;
|
||||
var remoteCRC = gdriveProvider.syncMerge(fileDesc, syncAttributes, remoteContent, remoteTitle, remoteDiscussionList, remoteDiscussionListJSON);
|
||||
var data = gdriveProvider.serializeContent(content, discussionList);
|
||||
googleHelper.upload(syncAttributes.id, undefined, title, data, undefined, syncAttributes.etag, accountId, function(error, result) {
|
||||
if(error) {
|
||||
return callback(error, true);
|
||||
}
|
||||
syncAttributes.etag = result.etag;
|
||||
// Remove this deprecated flag if any
|
||||
delete syncAttributes.isRealtime;
|
||||
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;
|
||||
callback(undefined, true);
|
||||
});
|
||||
};
|
||||
|
||||
// Update syncAttributes
|
||||
syncAttributes.etag = file.etag;
|
||||
if(merge === true) {
|
||||
// Need to store the whole content for merge
|
||||
syncAttributes.content = remoteContent;
|
||||
syncAttributes.title = remoteTitle;
|
||||
syncAttributes.discussionList = remoteDiscussionListJSON;
|
||||
}
|
||||
syncAttributes.contentCRC = remoteCRC.contentCRC;
|
||||
syncAttributes.titleCRC = remoteCRC.titleCRC;
|
||||
syncAttributes.discussionListCRC = remoteCRC.discussionListCRC;
|
||||
utils.storeAttributes(syncAttributes);
|
||||
setTimeout(mergeChange, 5);
|
||||
}
|
||||
setTimeout(mergeChange, 5);
|
||||
});
|
||||
});
|
||||
};
|
||||
gdriveProvider.syncDown = function(callback) {
|
||||
var lastChangeId = parseInt(storage[accountId + ".gdrive.lastChangeId"], 10);
|
||||
googleHelper.checkChanges(lastChangeId, accountId, function(error, changes, newChangeId) {
|
||||
if(error) {
|
||||
return callback(error);
|
||||
}
|
||||
var interestingChanges = [];
|
||||
_.each(changes, function(change) {
|
||||
var syncIndex = createSyncIndex(change.fileId);
|
||||
var fileDesc = fileMgr.getFileFromSyncIndex(syncIndex);
|
||||
var syncAttributes = fileDesc && fileDesc.syncLocations[syncIndex];
|
||||
if(!syncAttributes) {
|
||||
return;
|
||||
}
|
||||
// Store fileDesc and syncAttributes references to avoid 2 times search
|
||||
change.fileDesc = fileDesc;
|
||||
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;
|
||||
}
|
||||
function mergeChange() {
|
||||
if(changes.length === 0) {
|
||||
storage[accountId + ".gdrive.lastChangeId"] = newChangeId;
|
||||
return callback();
|
||||
}
|
||||
var change = changes.pop();
|
||||
var fileDesc = change.fileDesc;
|
||||
var syncAttributes = change.syncAttributes;
|
||||
// File deleted
|
||||
if(change.deleted === true) {
|
||||
eventMgr.onError('"' + fileDesc.title + '" has been removed from ' + providerName + '.');
|
||||
fileDesc.removeSyncLocation(syncAttributes);
|
||||
return eventMgr.onSyncRemoved(fileDesc, syncAttributes);
|
||||
}
|
||||
var file = change.file;
|
||||
var parsedContent = gdriveProvider.parseContent(file.content);
|
||||
var remoteContent = parsedContent.content;
|
||||
var remoteTitle = file.title;
|
||||
var remoteDiscussionListJSON = parsedContent.discussionListJSON;
|
||||
var remoteDiscussionList = parsedContent.discussionList;
|
||||
var remoteCRC = gdriveProvider.syncMerge(fileDesc, syncAttributes, remoteContent, remoteTitle, remoteDiscussionList, remoteDiscussionListJSON);
|
||||
|
||||
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();
|
||||
});
|
||||
};
|
||||
// Update syncAttributes
|
||||
syncAttributes.etag = file.etag;
|
||||
if(merge === true) {
|
||||
// Need to store the whole content for merge
|
||||
syncAttributes.content = remoteContent;
|
||||
syncAttributes.title = remoteTitle;
|
||||
syncAttributes.discussionList = remoteDiscussionListJSON;
|
||||
}
|
||||
syncAttributes.contentCRC = remoteCRC.contentCRC;
|
||||
syncAttributes.titleCRC = remoteCRC.titleCRC;
|
||||
syncAttributes.discussionListCRC = remoteCRC.discussionListCRC;
|
||||
utils.storeAttributes(syncAttributes);
|
||||
setTimeout(mergeChange, 5);
|
||||
}
|
||||
|
||||
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;
|
||||
};
|
||||
setTimeout(mergeChange, 5);
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
// Initialize the AutoSync dialog fields
|
||||
gdriveProvider.setAutosyncDialogConfig = function() {
|
||||
var config = gdriveProvider.autosyncConfig;
|
||||
utils.setInputRadio('radio-autosync-' + providerId + '-mode', config.mode || 'off');
|
||||
utils.setInputValue('#input-autosync-' + providerId + '-parentid', config.parentId);
|
||||
};
|
||||
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();
|
||||
});
|
||||
};
|
||||
|
||||
// Retrieve the AutoSync dialog fields
|
||||
gdriveProvider.getAutosyncDialogConfig = function() {
|
||||
var config = {};
|
||||
config.mode = utils.getInputRadio('radio-autosync-' + providerId + '-mode');
|
||||
config.parentId = utils.getInputTextValue('#input-autosync-' + providerId + '-parentid');
|
||||
return config;
|
||||
};
|
||||
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;
|
||||
};
|
||||
|
||||
// Perform AutoSync
|
||||
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;
|
||||
}
|
||||
var syncAttributes = createSyncAttributes(result.id, result.etag, content, title, discussionListJSON);
|
||||
callback(undefined, syncAttributes);
|
||||
});
|
||||
};
|
||||
// Initialize the AutoSync dialog fields
|
||||
gdriveProvider.setAutosyncDialogConfig = function() {
|
||||
var config = gdriveProvider.autosyncConfig;
|
||||
utils.setInputRadio('radio-autosync-' + providerId + '-mode', config.mode || 'off');
|
||||
utils.setInputValue('#input-autosync-' + providerId + '-parentid', config.parentId);
|
||||
};
|
||||
|
||||
// Disable publish on optional multi-account
|
||||
gdriveProvider.isPublishEnabled = settings.gdriveMultiAccount > accountIndex;
|
||||
// Retrieve the AutoSync dialog fields
|
||||
gdriveProvider.getAutosyncDialogConfig = function() {
|
||||
var config = {};
|
||||
config.mode = utils.getInputRadio('radio-autosync-' + providerId + '-mode');
|
||||
config.parentId = utils.getInputTextValue('#input-autosync-' + providerId + '-parentid');
|
||||
return config;
|
||||
};
|
||||
|
||||
eventMgr.addListener("onReady", function() {
|
||||
// Hide optional multi-account sub-menus
|
||||
$('.submenu-sync-' + providerId).toggle(settings.gdriveMultiAccount > accountIndex);
|
||||
// Perform AutoSync
|
||||
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;
|
||||
}
|
||||
var syncAttributes = createSyncAttributes(result.id, result.etag, content, title, discussionListJSON);
|
||||
callback(undefined, syncAttributes);
|
||||
});
|
||||
};
|
||||
|
||||
// Create export dialog
|
||||
var modalUploadElt = document.querySelector('.modal-upload-' + providerId);
|
||||
modalUploadElt && (modalUploadElt.innerHTML = _.template(dialogExportGdriveHTML, {
|
||||
providerId: providerId,
|
||||
providerName: providerName
|
||||
}));
|
||||
// Disable publish on optional multi-account
|
||||
gdriveProvider.isPublishEnabled = settings.gdriveMultiAccount > accountIndex;
|
||||
|
||||
// Create autosync dialog
|
||||
var modalAutosyncElt = document.querySelector('.modal-autosync-' + providerId);
|
||||
modalAutosyncElt && (modalAutosyncElt.innerHTML = _.template(dialogAutoSyncGdriveHTML, {
|
||||
providerId: providerId,
|
||||
providerName: providerName
|
||||
}));
|
||||
eventMgr.addListener("onReady", function() {
|
||||
// Hide optional multi-account sub-menus
|
||||
$('.submenu-sync-' + providerId).toggle(settings.gdriveMultiAccount > accountIndex);
|
||||
|
||||
// Choose folder button in export modal
|
||||
$('.action-export-' + providerId + '-choose-folder').click(function() {
|
||||
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);
|
||||
});
|
||||
// Create export dialog
|
||||
var modalUploadElt = document.querySelector('.modal-upload-' + providerId);
|
||||
modalUploadElt && (modalUploadElt.innerHTML = _.template(dialogExportGdriveHTML, {
|
||||
providerId: providerId,
|
||||
providerName: providerName
|
||||
}));
|
||||
|
||||
// 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);
|
||||
});
|
||||
// Create autosync dialog
|
||||
var modalAutosyncElt = document.querySelector('.modal-autosync-' + providerId);
|
||||
modalAutosyncElt && (modalAutosyncElt.innerHTML = _.template(dialogAutoSyncGdriveHTML, {
|
||||
providerId: providerId,
|
||||
providerName: providerName
|
||||
}));
|
||||
|
||||
$('.action-remove-google-drive-state').click(function() {
|
||||
storage.removeItem('gdrive.state');
|
||||
});
|
||||
// Choose folder button in export modal
|
||||
$('.action-export-' + providerId + '-choose-folder').click(function() {
|
||||
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);
|
||||
});
|
||||
|
||||
// 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();
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
// 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);
|
||||
});
|
||||
|
||||
storage.removeItem('gdrive.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;
|
||||
var fileDesc = fileMgr.createFile(file.title, file.content, undefined, syncLocations);
|
||||
fileMgr.selectFile(fileDesc);
|
||||
eventMgr.onMessage('"' + file.title + '" created successfully on ' + providerName + '.');
|
||||
});
|
||||
}
|
||||
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);
|
||||
}
|
||||
});
|
||||
$('.action-remove-google-drive-state').click(function() {
|
||||
storage.removeItem('gdrive.state');
|
||||
});
|
||||
|
||||
return gdriveProvider;
|
||||
};
|
||||
// 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();
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
storage.removeItem('gdrive.state');
|
||||
if(state.action == "create") {
|
||||
eventMgr.onMessage('Please wait while creating your document on ' + providerName);
|
||||
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;
|
||||
var fileDesc = fileMgr.createFile(file.title, file.content, undefined, syncLocations);
|
||||
fileMgr.selectFile(fileDesc);
|
||||
eventMgr.onMessage('"' + file.title + '" created successfully on ' + providerName + '.');
|
||||
});
|
||||
}
|
||||
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);
|
||||
}
|
||||
});
|
||||
eventMgr.onMessage('Please wait while loading your document from ' + providerName);
|
||||
importFilesFromIds(importIds);
|
||||
}
|
||||
});
|
||||
|
||||
return gdriveProvider;
|
||||
};
|
||||
});
|
||||
|
@ -188,7 +188,7 @@ define([
|
||||
function createPublishIndex(fileDesc, publishAttributes) {
|
||||
var publishIndex;
|
||||
do {
|
||||
publishIndex = "publish." + utils.randomString();
|
||||
publishIndex = "publish." + utils.id();
|
||||
} while(_.has(storage, publishIndex));
|
||||
publishAttributes.publishIndex = publishIndex;
|
||||
fileDesc.addPublishLocation(publishAttributes);
|
||||
|
@ -455,6 +455,10 @@ kbd {
|
||||
background-position: -144px 0;
|
||||
}
|
||||
|
||||
.icon-provider-couchdb {
|
||||
background-position: -162px 0;
|
||||
}
|
||||
|
||||
/*******************
|
||||
* RTL
|
||||
*******************/
|
||||
|
@ -758,6 +758,22 @@ a {
|
||||
}
|
||||
}
|
||||
}
|
||||
.file-list .list-group-item {
|
||||
background-color: @transparent;
|
||||
padding: 0 3px;
|
||||
}
|
||||
margin-bottom: 0;
|
||||
|
||||
.input-rename {
|
||||
width: 220px;
|
||||
height: @input-height-slim;
|
||||
}
|
||||
}
|
||||
|
||||
.modal {
|
||||
.list-group .list-group-item {
|
||||
border-radius: @border-radius-base;
|
||||
}
|
||||
.list-group-item {
|
||||
padding: 3px;
|
||||
margin: 0;
|
||||
@ -778,28 +794,18 @@ a {
|
||||
}
|
||||
}
|
||||
}
|
||||
.file-list .list-group-item {
|
||||
background-color: @transparent;
|
||||
padding: 0 3px;
|
||||
}
|
||||
margin-bottom: 0;
|
||||
.list-group .list-group-item {
|
||||
border-radius: @border-radius-base;
|
||||
}
|
||||
|
||||
.folder {
|
||||
.folder, .document {
|
||||
font-weight: bold;
|
||||
color: @folder-color;
|
||||
font-size: 15px;
|
||||
background-color: @transparent;
|
||||
}
|
||||
.input-rename {
|
||||
width: 220px;
|
||||
height: @input-height-slim;
|
||||
}
|
||||
.name, .file-count {
|
||||
.name, .date, .file-count {
|
||||
padding: 9px 20px 9px 15px;
|
||||
}
|
||||
.date {
|
||||
font-weight: normal;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -1323,6 +1329,9 @@ a {
|
||||
.layout-animate & {
|
||||
.transition(350ms ease-in-out all);
|
||||
}
|
||||
.layout-vertical & {
|
||||
.box-shadow(inset 0 1px fade(@secondary, 6%));
|
||||
}
|
||||
}
|
||||
|
||||
#preview-contents {
|
||||
|
@ -1,371 +1,386 @@
|
||||
define([
|
||||
"jquery",
|
||||
"underscore",
|
||||
"utils",
|
||||
"storage",
|
||||
"eventMgr",
|
||||
"fileSystem",
|
||||
"fileMgr",
|
||||
"classes/Provider",
|
||||
"providers/dropboxProvider",
|
||||
"providers/gdriveProvider",
|
||||
"providers/gdrivesecProvider",
|
||||
"providers/gdriveterProvider"
|
||||
"jquery",
|
||||
"underscore",
|
||||
"utils",
|
||||
"storage",
|
||||
"eventMgr",
|
||||
"fileSystem",
|
||||
"fileMgr",
|
||||
"classes/Provider",
|
||||
"providers/dropboxProvider",
|
||||
"providers/couchdbProvider",
|
||||
"providers/gdriveProvider",
|
||||
"providers/gdrivesecProvider",
|
||||
"providers/gdriveterProvider"
|
||||
], function($, _, utils, storage, eventMgr, fileSystem, fileMgr, Provider) {
|
||||
|
||||
var synchronizer = {};
|
||||
var synchronizer = {};
|
||||
|
||||
// Create a map with providerId: providerModule
|
||||
var providerMap = _.chain(arguments).map(function(argument) {
|
||||
return argument instanceof Provider && [
|
||||
argument.providerId,
|
||||
argument
|
||||
];
|
||||
}).compact().object().value();
|
||||
// Create a map with providerId: providerModule
|
||||
var providerMap = _.chain(arguments).map(function(argument) {
|
||||
return argument instanceof Provider && [
|
||||
argument.providerId,
|
||||
argument
|
||||
];
|
||||
}).compact().object().value();
|
||||
|
||||
// Retrieve sync locations from storage
|
||||
(function() {
|
||||
var syncIndexMap = {};
|
||||
_.each(fileSystem, function(fileDesc) {
|
||||
utils.retrieveIndexArray(fileDesc.fileIndex + ".sync").forEach(function(syncIndex) {
|
||||
try {
|
||||
var syncAttributes = JSON.parse(storage[syncIndex]);
|
||||
// Store syncIndex
|
||||
syncAttributes.syncIndex = syncIndex;
|
||||
// Replace provider ID by provider module in attributes
|
||||
var provider = providerMap[syncAttributes.provider];
|
||||
if(!provider) {
|
||||
throw new Error("Invalid provider ID: " + syncAttributes.provider);
|
||||
}
|
||||
syncAttributes.provider = provider;
|
||||
fileDesc.syncLocations[syncIndex] = syncAttributes;
|
||||
syncIndexMap[syncIndex] = syncAttributes;
|
||||
}
|
||||
catch(e) {
|
||||
// storage can be corrupted
|
||||
eventMgr.onError(e);
|
||||
// Remove sync location
|
||||
utils.removeIndexFromArray(fileDesc.fileIndex + ".sync", syncIndex);
|
||||
}
|
||||
});
|
||||
});
|
||||
// Retrieve sync locations from storage
|
||||
(function() {
|
||||
var syncIndexMap = {};
|
||||
_.each(fileSystem, function(fileDesc) {
|
||||
utils.retrieveIndexArray(fileDesc.fileIndex + ".sync").forEach(function(syncIndex) {
|
||||
try {
|
||||
var syncAttributes = JSON.parse(storage[syncIndex]);
|
||||
// Store syncIndex
|
||||
syncAttributes.syncIndex = syncIndex;
|
||||
// Replace provider ID by provider module in attributes
|
||||
var provider = providerMap[syncAttributes.provider];
|
||||
if(!provider) {
|
||||
throw new Error("Invalid provider ID: " + syncAttributes.provider);
|
||||
}
|
||||
syncAttributes.provider = provider;
|
||||
fileDesc.syncLocations[syncIndex] = syncAttributes;
|
||||
syncIndexMap[syncIndex] = syncAttributes;
|
||||
}
|
||||
catch(e) {
|
||||
// storage can be corrupted
|
||||
eventMgr.onError(e);
|
||||
// Remove sync location
|
||||
utils.removeIndexFromArray(fileDesc.fileIndex + ".sync", syncIndex);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
// Clean fields from deleted files in local storage
|
||||
Object.keys(storage).forEach(function(key) {
|
||||
var match = key.match(/sync\.\S+/);
|
||||
if(match && !syncIndexMap.hasOwnProperty(match[0])) {
|
||||
storage.removeItem(key);
|
||||
}
|
||||
});
|
||||
})();
|
||||
// Clean fields from deleted files in local storage
|
||||
Object.keys(storage).forEach(function(key) {
|
||||
var match = key.match(/sync\.\S+/);
|
||||
if(match && !syncIndexMap.hasOwnProperty(match[0])) {
|
||||
storage.removeItem(key);
|
||||
}
|
||||
});
|
||||
})();
|
||||
|
||||
// AutoSync configuration
|
||||
_.each(providerMap, function(provider) {
|
||||
provider.autosyncConfig = utils.retrieveIgnoreError(provider.providerId + ".autosyncConfig") || {};
|
||||
});
|
||||
// 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) {
|
||||
return _.some(fileDesc.syncLocations, function(syncAttributes) {
|
||||
return provider === undefined || syncAttributes.provider === provider;
|
||||
});
|
||||
});
|
||||
};
|
||||
// Returns true if at least one file has synchronized location
|
||||
synchronizer.hasSync = function(provider) {
|
||||
return _.some(fileSystem, function(fileDesc) {
|
||||
return _.some(fileDesc.syncLocations, function(syncAttributes) {
|
||||
return provider === undefined || syncAttributes.provider === provider;
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
/***************************************************************************
|
||||
* Synchronization
|
||||
**************************************************************************/
|
||||
/***************************************************************************
|
||||
* Synchronization
|
||||
**************************************************************************/
|
||||
|
||||
// Entry point for up synchronization (upload changes)
|
||||
var uploadCycle = false;
|
||||
function syncUp(callback) {
|
||||
var uploadFileList = [];
|
||||
// Entry point for up synchronization (upload changes)
|
||||
var uploadCycle = false;
|
||||
|
||||
// Recursive function to upload multiple files
|
||||
function fileUp() {
|
||||
// No more fileDesc to synchronize
|
||||
if(uploadFileList.length === 0) {
|
||||
return syncUp(callback);
|
||||
}
|
||||
function syncUp(callback) {
|
||||
var uploadFileList = [];
|
||||
|
||||
// Dequeue a fileDesc to synchronize
|
||||
var fileDesc = uploadFileList.pop();
|
||||
var uploadSyncAttributesList = _.values(fileDesc.syncLocations);
|
||||
if(uploadSyncAttributesList.length === 0) {
|
||||
return fileUp();
|
||||
}
|
||||
// Recursive function to upload multiple files
|
||||
function fileUp() {
|
||||
// No more fileDesc to synchronize
|
||||
if(uploadFileList.length === 0) {
|
||||
return syncUp(callback);
|
||||
}
|
||||
|
||||
var uploadContent = fileDesc.content;
|
||||
var uploadContentCRC = utils.crc32(uploadContent);
|
||||
var uploadTitle = fileDesc.title;
|
||||
var uploadTitleCRC = utils.crc32(uploadTitle);
|
||||
var uploadDiscussionList = fileDesc.discussionListJSON;
|
||||
var uploadDiscussionListCRC = utils.crc32(uploadDiscussionList);
|
||||
// Dequeue a fileDesc to synchronize
|
||||
var fileDesc = uploadFileList.pop();
|
||||
var uploadSyncAttributesList = _.values(fileDesc.syncLocations);
|
||||
if(uploadSyncAttributesList.length === 0) {
|
||||
return fileUp();
|
||||
}
|
||||
|
||||
// Recursive function to upload a single file on multiple locations
|
||||
function locationUp() {
|
||||
// Here we are freezing the data to make sure it's uploaded consistently
|
||||
var uploadContent = fileDesc.content;
|
||||
var uploadContentCRC = utils.crc32(uploadContent);
|
||||
var uploadTitle = fileDesc.title;
|
||||
var uploadTitleCRC = utils.crc32(uploadTitle);
|
||||
var uploadDiscussionList = fileDesc.discussionListJSON;
|
||||
var uploadDiscussionListCRC = utils.crc32(uploadDiscussionList);
|
||||
var uploadFrontMatter = fileDesc.frontMatter;
|
||||
|
||||
// No more synchronized location for this document
|
||||
if(uploadSyncAttributesList.length === 0) {
|
||||
return fileUp();
|
||||
}
|
||||
// Recursive function to upload a single file on multiple locations
|
||||
function locationUp() {
|
||||
|
||||
// Dequeue a synchronized location
|
||||
var syncAttributes = uploadSyncAttributesList.pop();
|
||||
// No more synchronized location for this document
|
||||
if(uploadSyncAttributesList.length === 0) {
|
||||
return fileUp();
|
||||
}
|
||||
|
||||
syncAttributes.provider.syncUp(
|
||||
uploadContent,
|
||||
uploadContentCRC,
|
||||
uploadTitle,
|
||||
uploadTitleCRC,
|
||||
uploadDiscussionList,
|
||||
uploadDiscussionListCRC,
|
||||
syncAttributes,
|
||||
function(error, uploadFlag) {
|
||||
if(uploadFlag === true) {
|
||||
// If uploadFlag is true, request another upload cycle
|
||||
uploadCycle = true;
|
||||
}
|
||||
if(error) {
|
||||
return callback(error);
|
||||
}
|
||||
if(uploadFlag) {
|
||||
// Update syncAttributes in storage
|
||||
utils.storeAttributes(syncAttributes);
|
||||
}
|
||||
locationUp();
|
||||
}
|
||||
);
|
||||
}
|
||||
locationUp();
|
||||
}
|
||||
// Dequeue a synchronized location
|
||||
var syncAttributes = uploadSyncAttributesList.pop();
|
||||
|
||||
if(uploadCycle === true) {
|
||||
// New upload cycle
|
||||
uploadCycle = false;
|
||||
uploadFileList = _.values(fileSystem);
|
||||
fileUp();
|
||||
}
|
||||
else {
|
||||
callback();
|
||||
}
|
||||
}
|
||||
syncAttributes.provider.syncUp(
|
||||
uploadContent,
|
||||
uploadContentCRC,
|
||||
uploadTitle,
|
||||
uploadTitleCRC,
|
||||
uploadDiscussionList,
|
||||
uploadDiscussionListCRC,
|
||||
uploadFrontMatter,
|
||||
syncAttributes,
|
||||
function(error, uploadFlag) {
|
||||
if(uploadFlag === true) {
|
||||
// If uploadFlag is true, request another upload cycle
|
||||
uploadCycle = true;
|
||||
}
|
||||
if(error) {
|
||||
return callback(error);
|
||||
}
|
||||
if(uploadFlag) {
|
||||
// Update syncAttributes in storage
|
||||
utils.storeAttributes(syncAttributes);
|
||||
}
|
||||
locationUp();
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
// Entry point for down synchronization (download changes)
|
||||
function syncDown(callback) {
|
||||
var providerList = _.values(providerMap);
|
||||
locationUp();
|
||||
}
|
||||
|
||||
// Recursive function to download changes from multiple providers
|
||||
function providerDown() {
|
||||
if(providerList.length === 0) {
|
||||
return callback();
|
||||
}
|
||||
var provider = providerList.pop();
|
||||
if(uploadCycle === true) {
|
||||
// New upload cycle
|
||||
uploadCycle = false;
|
||||
uploadFileList = _.values(fileSystem);
|
||||
fileUp();
|
||||
}
|
||||
else {
|
||||
callback();
|
||||
}
|
||||
}
|
||||
|
||||
// Check that provider has files to sync
|
||||
if(!synchronizer.hasSync(provider)) {
|
||||
return providerDown();
|
||||
}
|
||||
// Entry point for down synchronization (download changes)
|
||||
function syncDown(callback) {
|
||||
var providerList = _.values(providerMap);
|
||||
|
||||
// Perform provider's syncDown
|
||||
provider.syncDown(function(error) {
|
||||
if(error) {
|
||||
return callback(error);
|
||||
}
|
||||
providerDown();
|
||||
});
|
||||
}
|
||||
providerDown();
|
||||
}
|
||||
// Recursive function to download changes from multiple providers
|
||||
function providerDown() {
|
||||
if(providerList.length === 0) {
|
||||
return callback();
|
||||
}
|
||||
var provider = providerList.pop();
|
||||
|
||||
// Entry point for the autosync feature
|
||||
function autosyncAll(callback) {
|
||||
var autosyncFileList = _.filter(fileSystem, function(fileDesc) {
|
||||
return _.size(fileDesc.syncLocations) === 0;
|
||||
});
|
||||
// Check that provider has files to sync
|
||||
if(!synchronizer.hasSync(provider)) {
|
||||
return providerDown();
|
||||
}
|
||||
|
||||
// Recursive function to autosync multiple files
|
||||
function fileAutosync() {
|
||||
// No more fileDesc to synchronize
|
||||
if(autosyncFileList.length === 0) {
|
||||
return callback();
|
||||
}
|
||||
var fileDesc = autosyncFileList.pop();
|
||||
// Perform provider's syncDown
|
||||
provider.syncDown(function(error) {
|
||||
if(error) {
|
||||
return callback(error);
|
||||
}
|
||||
providerDown();
|
||||
});
|
||||
}
|
||||
|
||||
var providerList = _.filter(providerMap, function(provider) {
|
||||
return provider.autosyncConfig.mode == 'all';
|
||||
});
|
||||
function providerAutosync() {
|
||||
// No more provider
|
||||
if(providerList.length === 0) {
|
||||
return fileAutosync();
|
||||
}
|
||||
var provider = providerList.pop();
|
||||
providerDown();
|
||||
}
|
||||
|
||||
provider.autosyncFile(fileDesc.title, fileDesc.content, fileDesc.discussionListJSON, provider.autosyncConfig, function(error, syncAttributes) {
|
||||
if(error) {
|
||||
return callback(error);
|
||||
}
|
||||
fileDesc.addSyncLocation(syncAttributes);
|
||||
eventMgr.onSyncExportSuccess(fileDesc, syncAttributes);
|
||||
providerAutosync();
|
||||
});
|
||||
}
|
||||
// Entry point for the autosync feature
|
||||
function autosyncAll(callback) {
|
||||
var autosyncFileList = _.filter(fileSystem, function(fileDesc) {
|
||||
return _.size(fileDesc.syncLocations) === 0;
|
||||
});
|
||||
|
||||
providerAutosync();
|
||||
}
|
||||
// Recursive function to autosync multiple files
|
||||
function fileAutosync() {
|
||||
// No more fileDesc to synchronize
|
||||
if(autosyncFileList.length === 0) {
|
||||
return callback();
|
||||
}
|
||||
var fileDesc = autosyncFileList.pop();
|
||||
|
||||
fileAutosync();
|
||||
}
|
||||
var providerList = _.filter(providerMap, function(provider) {
|
||||
return provider.autosyncConfig.mode == 'all';
|
||||
});
|
||||
|
||||
// Listen to offline status changes
|
||||
var isOffline = false;
|
||||
eventMgr.addListener("onOfflineChanged", function(isOfflineParam) {
|
||||
isOffline = isOfflineParam;
|
||||
});
|
||||
function providerAutosync() {
|
||||
// No more provider
|
||||
if(providerList.length === 0) {
|
||||
return fileAutosync();
|
||||
}
|
||||
var provider = providerList.pop();
|
||||
|
||||
// Main entry point for synchronization
|
||||
var syncRunning = false;
|
||||
synchronizer.sync = function() {
|
||||
// If sync is already running or offline
|
||||
if(syncRunning === true || isOffline === true) {
|
||||
return false;
|
||||
}
|
||||
syncRunning = true;
|
||||
eventMgr.onSyncRunning(true);
|
||||
uploadCycle = true;
|
||||
provider.autosyncFile(fileDesc.title, fileDesc.content, fileDesc.discussionListJSON, provider.autosyncConfig, function(error, syncAttributes) {
|
||||
if(error) {
|
||||
return callback(error);
|
||||
}
|
||||
fileDesc.addSyncLocation(syncAttributes);
|
||||
eventMgr.onSyncExportSuccess(fileDesc, syncAttributes);
|
||||
providerAutosync();
|
||||
});
|
||||
}
|
||||
|
||||
function isError(error) {
|
||||
if(error !== undefined) {
|
||||
syncRunning = false;
|
||||
eventMgr.onSyncRunning(false);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
providerAutosync();
|
||||
}
|
||||
|
||||
autosyncAll(function(error) {
|
||||
if(isError(error)) {
|
||||
return;
|
||||
}
|
||||
syncDown(function(error) {
|
||||
if(isError(error)) {
|
||||
return;
|
||||
}
|
||||
syncUp(function(error) {
|
||||
if(isError(error)) {
|
||||
return;
|
||||
}
|
||||
syncRunning = false;
|
||||
eventMgr.onSyncRunning(false);
|
||||
eventMgr.onSyncSuccess();
|
||||
});
|
||||
});
|
||||
});
|
||||
return true;
|
||||
};
|
||||
fileAutosync();
|
||||
}
|
||||
|
||||
/***************************************************************************
|
||||
* Initialize module
|
||||
**************************************************************************/
|
||||
// Listen to offline status changes
|
||||
var isOffline = false;
|
||||
eventMgr.addListener("onOfflineChanged", function(isOfflineParam) {
|
||||
isOffline = isOfflineParam;
|
||||
});
|
||||
|
||||
// Initialize the export dialog
|
||||
function initExportDialog(provider) {
|
||||
// Main entry point for synchronization
|
||||
var syncRunning = false;
|
||||
synchronizer.sync = function() {
|
||||
// If sync is already running or offline
|
||||
if(syncRunning === true || isOffline === true) {
|
||||
return false;
|
||||
}
|
||||
syncRunning = true;
|
||||
eventMgr.onSyncRunning(true);
|
||||
uploadCycle = true;
|
||||
|
||||
// Reset fields
|
||||
utils.resetModalInputs();
|
||||
function isError(error) {
|
||||
if(error !== undefined) {
|
||||
syncRunning = false;
|
||||
eventMgr.onSyncRunning(false);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
// Load preferences
|
||||
var exportPreferences = utils.retrieveIgnoreError(provider.providerId + ".exportPreferences");
|
||||
if(exportPreferences) {
|
||||
_.each(provider.exportPreferencesInputIds, function(inputId) {
|
||||
var exportPreferenceValue = exportPreferences[inputId];
|
||||
if(_.isBoolean(exportPreferenceValue)) {
|
||||
utils.setInputChecked("#input-sync-export-" + inputId, exportPreferenceValue);
|
||||
}
|
||||
else {
|
||||
utils.setInputValue("#input-sync-export-" + inputId, exportPreferenceValue);
|
||||
}
|
||||
});
|
||||
}
|
||||
autosyncAll(function(error) {
|
||||
if(isError(error)) {
|
||||
return;
|
||||
}
|
||||
syncDown(function(error) {
|
||||
if(isError(error)) {
|
||||
return;
|
||||
}
|
||||
syncUp(function(error) {
|
||||
if(isError(error)) {
|
||||
return;
|
||||
}
|
||||
syncRunning = false;
|
||||
eventMgr.onSyncRunning(false);
|
||||
eventMgr.onSyncSuccess();
|
||||
});
|
||||
});
|
||||
});
|
||||
return true;
|
||||
};
|
||||
|
||||
// Open dialog
|
||||
$(".modal-upload-" + provider.providerId).modal();
|
||||
}
|
||||
/***************************************************************************
|
||||
* Initialize module
|
||||
**************************************************************************/
|
||||
|
||||
eventMgr.addListener("onFileCreated", function(fileDesc) {
|
||||
if(_.size(fileDesc.syncLocations) === 0) {
|
||||
_.each(providerMap, function(provider) {
|
||||
if(provider.autosyncConfig.mode != 'new') {
|
||||
return;
|
||||
}
|
||||
provider.autosyncFile(fileDesc.title, fileDesc.content, fileDesc.discussionListJSON, provider.autosyncConfig, function(error, syncAttributes) {
|
||||
if(error) {
|
||||
return;
|
||||
}
|
||||
fileDesc.addSyncLocation(syncAttributes);
|
||||
eventMgr.onSyncExportSuccess(fileDesc, syncAttributes);
|
||||
});
|
||||
});
|
||||
}
|
||||
});
|
||||
function loadPreferences(provider, action) {
|
||||
utils.resetModalInputs();
|
||||
var preferences = utils.retrieveIgnoreError(provider.providerId + '.' + action + 'Preferences');
|
||||
if(preferences) {
|
||||
_.each(provider.exportPreferencesInputIds, function(inputId) {
|
||||
var exportPreferenceValue = preferences[inputId];
|
||||
var setValue = utils.setInputValue;
|
||||
if(_.isBoolean(exportPreferenceValue)) {
|
||||
setValue = utils.setInputChecked;
|
||||
}
|
||||
setValue('#input-sync-' + action + '-' + inputId, exportPreferenceValue);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
eventMgr.addListener("onReady", function() {
|
||||
// Init each provider
|
||||
_.each(providerMap, function(provider) {
|
||||
// Provider's import button
|
||||
$(".action-sync-import-" + provider.providerId).click(function(event) {
|
||||
provider.importFiles(event);
|
||||
});
|
||||
// Provider's export action
|
||||
$(".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 fileDesc = fileMgr.currentFile;
|
||||
// Initialize the import dialog
|
||||
function initImportDialog(provider) {
|
||||
loadPreferences(provider, 'import');
|
||||
$(".modal-download-" + provider.providerId).modal();
|
||||
}
|
||||
|
||||
provider.exportFile(event, fileDesc.title, fileDesc.content, fileDesc.discussionListJSON, function(error, syncAttributes) {
|
||||
if(error) {
|
||||
return;
|
||||
}
|
||||
fileDesc.addSyncLocation(syncAttributes);
|
||||
eventMgr.onSyncExportSuccess(fileDesc, syncAttributes);
|
||||
});
|
||||
// Initialize the export dialog
|
||||
function initExportDialog(provider) {
|
||||
loadPreferences(provider, 'export');
|
||||
$(".modal-upload-" + provider.providerId).modal();
|
||||
}
|
||||
|
||||
// Store input values as preferences for next time we open the
|
||||
// export dialog
|
||||
var exportPreferences = {};
|
||||
_.each(provider.exportPreferencesInputIds, function(inputId) {
|
||||
var inputElt = document.getElementById("input-sync-export-" + inputId);
|
||||
if(inputElt.type == 'checkbox') {
|
||||
exportPreferences[inputId] = inputElt.checked;
|
||||
}
|
||||
else {
|
||||
exportPreferences[inputId] = inputElt.value;
|
||||
}
|
||||
});
|
||||
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;
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
eventMgr.addListener("onFileCreated", function(fileDesc) {
|
||||
if(_.size(fileDesc.syncLocations) === 0) {
|
||||
_.each(providerMap, function(provider) {
|
||||
if(provider.autosyncConfig.mode != 'new') {
|
||||
return;
|
||||
}
|
||||
provider.autosyncFile(fileDesc.title, fileDesc.content, fileDesc.discussionListJSON, provider.autosyncConfig, function(error, syncAttributes) {
|
||||
if(error) {
|
||||
return;
|
||||
}
|
||||
fileDesc.addSyncLocation(syncAttributes);
|
||||
eventMgr.onSyncExportSuccess(fileDesc, syncAttributes);
|
||||
});
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
eventMgr.onSynchronizerCreated(synchronizer);
|
||||
return synchronizer;
|
||||
eventMgr.addListener("onReady", function() {
|
||||
// Init each provider
|
||||
_.each(providerMap, function(provider) {
|
||||
// Provider's import button
|
||||
$(".action-sync-import-" + provider.providerId).click(function(event) {
|
||||
provider.importFiles(event);
|
||||
});
|
||||
// Provider's import dialog action
|
||||
$(".action-sync-import-dialog-" + provider.providerId).click(function() {
|
||||
initImportDialog(provider);
|
||||
});
|
||||
// Provider's export dialog action
|
||||
$(".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 fileDesc = fileMgr.currentFile;
|
||||
|
||||
provider.exportFile(event, fileDesc.title, fileDesc.content, fileDesc.discussionListJSON, fileDesc.frontMatter, function(error, syncAttributes) {
|
||||
if(error) {
|
||||
return;
|
||||
}
|
||||
fileDesc.addSyncLocation(syncAttributes);
|
||||
eventMgr.onSyncExportSuccess(fileDesc, syncAttributes);
|
||||
});
|
||||
|
||||
// Store input values as preferences for next time we open the
|
||||
// export dialog
|
||||
var exportPreferences = {};
|
||||
_.each(provider.exportPreferencesInputIds, function(inputId) {
|
||||
var inputElt = document.getElementById("input-sync-export-" + inputId);
|
||||
if(inputElt.type == 'checkbox') {
|
||||
exportPreferences[inputId] = inputElt.checked;
|
||||
}
|
||||
else {
|
||||
exportPreferences[inputId] = inputElt.value;
|
||||
}
|
||||
});
|
||||
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;
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
eventMgr.onSynchronizerCreated(synchronizer);
|
||||
return synchronizer;
|
||||
});
|
||||
|
@ -87,24 +87,15 @@ define([
|
||||
};
|
||||
};
|
||||
|
||||
// Generates a 24 chars length random string (should be enough to prevent collisions)
|
||||
utils.randomString = (function() {
|
||||
var max = Math.pow(36, 6);
|
||||
|
||||
function s6() {
|
||||
// Linear [0-9a-z]{6} random string
|
||||
return ('000000' + (Math.random() * max | 0).toString(36)).slice(-6);
|
||||
// Generates a 24 char length random id
|
||||
var idAlphabet = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
|
||||
utils.id = function() {
|
||||
var result = [];
|
||||
for(var i = 0; i < 24; i++) {
|
||||
result.push(idAlphabet[Math.random() * idAlphabet.length | 0]);
|
||||
}
|
||||
|
||||
return function() {
|
||||
return [
|
||||
s6(),
|
||||
s6(),
|
||||
s6(),
|
||||
s6()
|
||||
].join('');
|
||||
};
|
||||
})();
|
||||
return result.join('');
|
||||
};
|
||||
|
||||
// Return a parameter from the URL
|
||||
utils.getURLParameter = function(name) {
|
||||
@ -549,6 +540,122 @@ define([
|
||||
return result.join("");
|
||||
};
|
||||
|
||||
function padNumber(num, digits, trim) {
|
||||
var neg = '';
|
||||
if (num < 0) {
|
||||
neg = '-';
|
||||
num = -num;
|
||||
}
|
||||
num = '' + num;
|
||||
while(num.length < digits) {
|
||||
num = '0' + num;
|
||||
}
|
||||
if (trim) {
|
||||
num = num.substr(num.length - digits);
|
||||
}
|
||||
return neg + num;
|
||||
}
|
||||
|
||||
function dateGetter(name, size, offset, trim) {
|
||||
offset = offset || 0;
|
||||
return function(date) {
|
||||
var value = date['get' + name]();
|
||||
if (offset > 0 || value > -offset) {
|
||||
value += offset;
|
||||
}
|
||||
if (value === 0 && offset == -12 ) {
|
||||
value = 12;
|
||||
}
|
||||
return padNumber(value, size, trim);
|
||||
};
|
||||
}
|
||||
|
||||
function dateStrGetter(name, shortForm) {
|
||||
return function(date, formats) {
|
||||
var value = date['get' + name]();
|
||||
var get = (shortForm ? ('SHORT' + name) : name).toUpperCase();
|
||||
|
||||
return formats[get][value];
|
||||
};
|
||||
}
|
||||
|
||||
var DATE_FORMATS_SPLIT = /((?:[^yMdHhmsaZE']+)|(?:'(?:[^']|'')*')|(?:E+|y+|M+|d+|H+|h+|m+|s+|a|Z))(.*)/;
|
||||
var DATE_FORMATS = {
|
||||
yyyy: dateGetter('FullYear', 4),
|
||||
yy: dateGetter('FullYear', 2, 0, true),
|
||||
y: dateGetter('FullYear', 1),
|
||||
MMMM: dateStrGetter('Month'),
|
||||
MMM: dateStrGetter('Month', true),
|
||||
MM: dateGetter('Month', 2, 1),
|
||||
M: dateGetter('Month', 1, 1),
|
||||
dd: dateGetter('Date', 2),
|
||||
d: dateGetter('Date', 1),
|
||||
HH: dateGetter('Hours', 2),
|
||||
H: dateGetter('Hours', 1),
|
||||
hh: dateGetter('Hours', 2, -12),
|
||||
h: dateGetter('Hours', 1, -12),
|
||||
mm: dateGetter('Minutes', 2),
|
||||
m: dateGetter('Minutes', 1),
|
||||
ss: dateGetter('Seconds', 2),
|
||||
s: dateGetter('Seconds', 1),
|
||||
// while ISO 8601 requires fractions to be prefixed with `.` or `,`
|
||||
// we can be just safely rely on using `sss` since we currently don't support single or two digit fractions
|
||||
sss: dateGetter('Milliseconds', 3),
|
||||
EEEE: dateStrGetter('Day'),
|
||||
EEE: dateStrGetter('Day', true)
|
||||
};
|
||||
|
||||
var DATETIME_FORMATS = {
|
||||
MONTH: 'January,February,March,April,May,June,July,August,September,October,November,December'.split(','),
|
||||
SHORTMONTH: 'Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec'.split(','),
|
||||
DAY: 'Sunday,Monday,Tuesday,Wednesday,Thursday,Friday,Saturday'.split(','),
|
||||
SHORTDAY: 'Sun,Mon,Tue,Wed,Thu,Fri,Sat'.split(','),
|
||||
AMPMS: ['AM','PM'],
|
||||
medium: 'MMM d, y h:mm:ss a',
|
||||
short: 'M/d/yy h:mm a',
|
||||
fullDate: 'EEEE, MMMM d, y',
|
||||
longDate: 'MMMM d, y',
|
||||
mediumDate: 'MMM d, y',
|
||||
shortDate: 'M/d/yy',
|
||||
mediumTime: 'h:mm:ss a',
|
||||
shortTime: 'h:mm a'
|
||||
};
|
||||
|
||||
utils.formatDate = function(date) {
|
||||
var text = '',
|
||||
parts = [],
|
||||
fn, match;
|
||||
|
||||
var interval = Date.now() - date;
|
||||
var format = 'HH:mm';
|
||||
if(interval > 31556940000) {
|
||||
format = 'y';
|
||||
}
|
||||
else if(interval > 86400000) {
|
||||
format = 'MMM d';
|
||||
}
|
||||
date = new Date(date);
|
||||
|
||||
while(format) {
|
||||
match = DATE_FORMATS_SPLIT.exec(format);
|
||||
if (match) {
|
||||
parts = parts.concat(match.slice(1));
|
||||
format = parts.pop();
|
||||
} else {
|
||||
parts.push(format);
|
||||
format = null;
|
||||
}
|
||||
}
|
||||
|
||||
parts.forEach(function(value){
|
||||
fn = DATE_FORMATS[value];
|
||||
text += fn ? fn(date, DATETIME_FORMATS)
|
||||
: value.replace(/(^'|'$)/g, '').replace(/''/g, "'");
|
||||
});
|
||||
|
||||
return text;
|
||||
};
|
||||
|
||||
// Base64 conversion
|
||||
utils.encodeBase64 = function(str) {
|
||||
if(str.length === 0) {
|
||||
@ -604,6 +711,10 @@ define([
|
||||
return x.join('');
|
||||
};
|
||||
|
||||
utils.decodeBase64 = function(str) {
|
||||
return window.unescape(decodeURIComponent(window.atob(str)));
|
||||
};
|
||||
|
||||
// CRC32 algorithm
|
||||
var mHash = [
|
||||
0,
|
||||
|
@ -307,7 +307,7 @@
|
||||
<div class="col-md-4 col-md-offset-2">
|
||||
<h2 id="fully-customizable">Fully customizable</h2>
|
||||
|
||||
<p>StackEdit has an infinite combinations of settings. Theme, layout, shortcuts can be
|
||||
<p>StackEdit offers infinite combinations of settings. Theme, layout, shortcuts can be
|
||||
personalized. For the rest, StackEdit gives you the freedom to make your own extension…</p>
|
||||
</div>
|
||||
</div>
|
||||
|
Loading…
Reference in New Issue
Block a user