Small fixes
This commit is contained in:
parent
1231b26710
commit
4b875e798a
@ -16,7 +16,7 @@ define([
|
|||||||
};
|
};
|
||||||
|
|
||||||
var monetize = new MonetizeJS({
|
var monetize = new MonetizeJS({
|
||||||
applicationID: 'iklMbzDI7dvMEScb'
|
applicationID: 'ESTHdCYOi18iLhhO'
|
||||||
});
|
});
|
||||||
|
|
||||||
dialogAbout.onReady = function() {
|
dialogAbout.onReady = function() {
|
||||||
|
@ -48,7 +48,6 @@ define([
|
|||||||
utils.setInputChecked("#input-markdownextra-intraword", markdownExtra.config.intraword);
|
utils.setInputChecked("#input-markdownextra-intraword", markdownExtra.config.intraword);
|
||||||
utils.setInputChecked("#input-markdownextra-comments", markdownExtra.config.comments);
|
utils.setInputChecked("#input-markdownextra-comments", markdownExtra.config.comments);
|
||||||
utils.setInputValue("#input-markdownextra-highlighter", markdownExtra.config.highlighter);
|
utils.setInputValue("#input-markdownextra-highlighter", markdownExtra.config.highlighter);
|
||||||
utils.setInputChecked("#input-markdownextra-diagrams", markdownExtra.config.diagrams);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
markdownExtra.onSaveSettings = function(newConfig) {
|
markdownExtra.onSaveSettings = function(newConfig) {
|
||||||
@ -64,7 +63,6 @@ define([
|
|||||||
newConfig.intraword = utils.getInputChecked("#input-markdownextra-intraword");
|
newConfig.intraword = utils.getInputChecked("#input-markdownextra-intraword");
|
||||||
newConfig.comments = utils.getInputChecked("#input-markdownextra-comments");
|
newConfig.comments = utils.getInputChecked("#input-markdownextra-comments");
|
||||||
newConfig.highlighter = utils.getInputValue("#input-markdownextra-highlighter");
|
newConfig.highlighter = utils.getInputValue("#input-markdownextra-highlighter");
|
||||||
newConfig.diagrams = utils.getInputChecked("#input-markdownextra-diagrams");
|
|
||||||
};
|
};
|
||||||
|
|
||||||
var eventMgr;
|
var eventMgr;
|
||||||
|
@ -63,8 +63,7 @@ define([
|
|||||||
element: '.navbar-inner',
|
element: '.navbar-inner',
|
||||||
title: 'StackEdit 4 is out!',
|
title: 'StackEdit 4 is out!',
|
||||||
content: [
|
content: [
|
||||||
'<p>I\'m very pleased to welcome you here!</p>',
|
'<p>I\'m very pleased to welcome you here! StackEdit keeps getting better and I hope you appreciate it.</p>',
|
||||||
'<p>StackEdit keeps getting better and I hope you appreciate it.</p>',
|
|
||||||
'Please click <b>Next</b> to take a quick tour.'
|
'Please click <b>Next</b> to take a quick tour.'
|
||||||
].join(""),
|
].join(""),
|
||||||
placement: 'bottom'
|
placement: 'bottom'
|
||||||
@ -97,20 +96,20 @@ define([
|
|||||||
'<b>Tip:</b> Reopen the Welcome Document from Settings>Utils to discover other new features.'
|
'<b>Tip:</b> Reopen the Welcome Document from Settings>Utils to discover other new features.'
|
||||||
].join(""),
|
].join(""),
|
||||||
placement: 'right',
|
placement: 'right',
|
||||||
reflex: true,
|
reflex: true
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
element: '.navbar-inner',
|
element: '.navbar-inner',
|
||||||
title: 'Happy StackWriting!',
|
title: 'Happy StackWriting!',
|
||||||
content: [
|
content: [
|
||||||
'<p>Enjoy, and don\'t forget to rate 5 stars on the <a target="_blank" href="https://chrome.google.com/webstore/detail/stackedit/iiooodelglhkcpgbajoejffhijaclcdg/reviews">Chrome Web Store</a>...</p>',
|
'<p>Enjoy, and don\'t forget to rate 5 stars on the <a target="_blank" href="https://chrome.google.com/webstore/detail/stackedit/iiooodelglhkcpgbajoejffhijaclcdg/reviews">Chrome Web Store</a>...</p>',
|
||||||
'<a href="https://twitter.com/share" class="twitter-share-button" data-url="https://stackedit.io" data-text="Great #markdown editor!" data-via="stackedit" data-size="large"></a>',
|
'<a href="https://twitter.com/share" class="twitter-share-button" data-url="https://stackedit.io" data-text="Great #markdown editor!" data-via="stackedit" data-size="large"></a>'
|
||||||
].join(""),
|
].join(""),
|
||||||
placement: 'bottom',
|
placement: 'bottom',
|
||||||
onShown: function() {
|
onShown: function() {
|
||||||
eventMgr.onTweet();
|
eventMgr.onTweet();
|
||||||
}
|
}
|
||||||
},
|
}
|
||||||
]);
|
]);
|
||||||
if(!_.has(storage, 'welcomeTour')) {
|
if(!_.has(storage, 'welcomeTour')) {
|
||||||
tour.start();
|
tour.start();
|
||||||
|
@ -1,361 +1,354 @@
|
|||||||
/*global Dropbox */
|
/*global Dropbox */
|
||||||
define([
|
define([
|
||||||
"jquery",
|
"jquery",
|
||||||
"underscore",
|
"underscore",
|
||||||
"constants",
|
"constants",
|
||||||
"core",
|
"core",
|
||||||
"utils",
|
"utils",
|
||||||
"storage",
|
"storage",
|
||||||
"logger",
|
"logger",
|
||||||
"settings",
|
"settings",
|
||||||
"eventMgr",
|
"eventMgr",
|
||||||
"classes/AsyncTask",
|
"classes/AsyncTask"
|
||||||
], function($, _, constants, core, utils, storage, logger, settings, eventMgr, AsyncTask) {
|
], function($, _, constants, core, utils, storage, logger, settings, eventMgr, AsyncTask) {
|
||||||
|
|
||||||
var client;
|
var client;
|
||||||
var authenticated = false;
|
var authenticated = false;
|
||||||
|
|
||||||
var dropboxHelper = {};
|
var dropboxHelper = {};
|
||||||
|
|
||||||
// Listen to offline status changes
|
// Listen to offline status changes
|
||||||
var isOffline = false;
|
var isOffline = false;
|
||||||
eventMgr.addListener("onOfflineChanged", function(isOfflineParam) {
|
eventMgr.addListener("onOfflineChanged", function(isOfflineParam) {
|
||||||
isOffline = isOfflineParam;
|
isOffline = isOfflineParam;
|
||||||
});
|
});
|
||||||
|
|
||||||
// Try to connect dropbox by downloading client.js
|
// Try to connect dropbox by downloading client.js
|
||||||
function connect(task) {
|
function connect(task) {
|
||||||
task.onRun(function() {
|
task.onRun(function() {
|
||||||
if(isOffline === true) {
|
if(isOffline === true) {
|
||||||
client = undefined;
|
client = undefined;
|
||||||
task.error(new Error("Operation not available in offline mode.|stopPublish"));
|
return task.error(new Error("Operation not available in offline mode.|stopPublish"));
|
||||||
return;
|
}
|
||||||
}
|
if(client !== undefined) {
|
||||||
if(client !== undefined) {
|
return task.chain();
|
||||||
task.chain();
|
}
|
||||||
return;
|
$.ajax({
|
||||||
}
|
url: "libs/dropbox.min.js",
|
||||||
$.ajax({
|
dataType: "script",
|
||||||
url: "libs/dropbox.min.js",
|
timeout: constants.AJAX_TIMEOUT
|
||||||
dataType: "script",
|
}).done(function() {
|
||||||
timeout: constants.AJAX_TIMEOUT
|
client = new Dropbox.Client({
|
||||||
}).done(function() {
|
key: settings.dropboxFullAccess === true ? constants.DROPBOX_APP_KEY : constants.DROPBOX_RESTRICTED_APP_KEY,
|
||||||
client = new Dropbox.Client({
|
secret: settings.dropboxFullAccess === true ? constants.DROPBOX_APP_SECRET : constants.DROPBOX_RESTRICTED_APP_SECRET
|
||||||
key: settings.dropboxFullAccess === true ? constants.DROPBOX_APP_KEY : constants.DROPBOX_RESTRICTED_APP_KEY,
|
});
|
||||||
secret: settings.dropboxFullAccess === true ? constants.DROPBOX_APP_SECRET : constants.DROPBOX_RESTRICTED_APP_SECRET
|
client.authDriver(new Dropbox.AuthDriver.Popup({
|
||||||
});
|
receiverUrl: constants.BASE_URL + "html/dropbox-oauth-receiver.html",
|
||||||
client.authDriver(new Dropbox.AuthDriver.Popup({
|
rememberUser: true
|
||||||
receiverUrl: constants.BASE_URL + "html/dropbox-oauth-receiver.html",
|
}));
|
||||||
rememberUser: true
|
task.chain();
|
||||||
}));
|
}).fail(function(jqXHR) {
|
||||||
task.chain();
|
var error = {
|
||||||
}).fail(function(jqXHR) {
|
status: jqXHR.status,
|
||||||
var error = {
|
responseText: jqXHR.statusText
|
||||||
status: jqXHR.status,
|
};
|
||||||
responseText: jqXHR.statusText
|
handleError(error, task);
|
||||||
};
|
});
|
||||||
handleError(error, task);
|
});
|
||||||
});
|
}
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
// Try to authenticate with Oauth
|
// Try to authenticate with Oauth
|
||||||
function authenticate(task) {
|
function authenticate(task) {
|
||||||
task.onRun(function() {
|
task.onRun(function() {
|
||||||
if(authenticated === true) {
|
if(authenticated === true) {
|
||||||
task.chain();
|
return task.chain();
|
||||||
return;
|
}
|
||||||
}
|
var immediate = true;
|
||||||
var immediate = true;
|
|
||||||
function oauthRedirect() {
|
|
||||||
utils.redirectConfirm('You are being redirected to <strong>Dropbox</strong> authorization page.', function() {
|
|
||||||
task.chain(localAuthenticate);
|
|
||||||
}, function() {
|
|
||||||
task.error(new Error('Operation canceled.'));
|
|
||||||
});
|
|
||||||
}
|
|
||||||
function localAuthenticate() {
|
|
||||||
if(immediate === false) {
|
|
||||||
// If not immediate we add time for user to enter his
|
|
||||||
// credentials
|
|
||||||
task.timeout = constants.ASYNC_TASK_LONG_TIMEOUT;
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
client.reset();
|
|
||||||
}
|
|
||||||
client.authenticate({
|
|
||||||
interactive: !immediate
|
|
||||||
}, function(error, client) {
|
|
||||||
// Success
|
|
||||||
if(client.isAuthenticated() === true) {
|
|
||||||
authenticated = true;
|
|
||||||
task.chain();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
// If immediate did not work retry without immediate flag
|
|
||||||
if(immediate === true) {
|
|
||||||
immediate = false;
|
|
||||||
task.chain(oauthRedirect);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
// Error
|
|
||||||
task.error(new Error("Access to Dropbox account is not authorized."));
|
|
||||||
});
|
|
||||||
}
|
|
||||||
task.chain(localAuthenticate);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
dropboxHelper.upload = function(path, content, callback) {
|
function oauthRedirect() {
|
||||||
var result;
|
utils.redirectConfirm('You are being redirected to <strong>Dropbox</strong> authorization page.', function() {
|
||||||
var task = new AsyncTask();
|
task.chain(localAuthenticate);
|
||||||
connect(task);
|
}, function() {
|
||||||
authenticate(task);
|
task.error(new Error('Operation canceled.'));
|
||||||
task.onRun(function() {
|
});
|
||||||
client.writeFile(path, content, function(error, stat) {
|
}
|
||||||
if(!error) {
|
|
||||||
result = stat;
|
|
||||||
task.chain();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
// Handle error
|
|
||||||
if(error.status === 400) {
|
|
||||||
error = 'Could not upload document into path "' + path + '".';
|
|
||||||
}
|
|
||||||
handleError(error, task);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
task.onSuccess(function() {
|
|
||||||
callback(undefined, result);
|
|
||||||
});
|
|
||||||
task.onError(function(error) {
|
|
||||||
callback(error);
|
|
||||||
});
|
|
||||||
task.enqueue();
|
|
||||||
};
|
|
||||||
|
|
||||||
dropboxHelper.checkChanges = function(lastChangeId, callback) {
|
function localAuthenticate() {
|
||||||
var changes = [];
|
if(immediate === false) {
|
||||||
var newChangeId = lastChangeId || 0;
|
// If not immediate we add time for user to enter his
|
||||||
var task = new AsyncTask();
|
// credentials
|
||||||
connect(task);
|
task.timeout = constants.ASYNC_TASK_LONG_TIMEOUT;
|
||||||
authenticate(task);
|
}
|
||||||
task.onRun(function() {
|
else {
|
||||||
function retrievePageOfChanges() {
|
client.reset();
|
||||||
client.pullChanges(newChangeId, function(error, pullChanges) {
|
}
|
||||||
if(error) {
|
client.authenticate({
|
||||||
handleError(error, task);
|
interactive: !immediate
|
||||||
return;
|
}, function(error, client) {
|
||||||
}
|
// Success
|
||||||
// Retrieve success
|
if(client.isAuthenticated() === true) {
|
||||||
newChangeId = pullChanges.cursor();
|
authenticated = true;
|
||||||
if(pullChanges.changes !== undefined) {
|
return task.chain();
|
||||||
changes = changes.concat(pullChanges.changes);
|
}
|
||||||
}
|
// If immediate did not work retry without immediate flag
|
||||||
if(pullChanges.shouldPullAgain) {
|
if(immediate === true) {
|
||||||
task.chain(retrievePageOfChanges);
|
immediate = false;
|
||||||
}
|
return task.chain(oauthRedirect);
|
||||||
else {
|
}
|
||||||
task.chain();
|
// Error
|
||||||
}
|
task.error(new Error("Access to Dropbox account is not authorized."));
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
task.chain(retrievePageOfChanges);
|
|
||||||
});
|
|
||||||
task.onSuccess(function() {
|
|
||||||
callback(undefined, changes, newChangeId);
|
|
||||||
});
|
|
||||||
task.onError(function(error) {
|
|
||||||
callback(error);
|
|
||||||
});
|
|
||||||
task.enqueue();
|
|
||||||
};
|
|
||||||
|
|
||||||
dropboxHelper.downloadMetadata = function(paths, callback) {
|
task.chain(localAuthenticate);
|
||||||
var result = [];
|
});
|
||||||
var task = new AsyncTask();
|
}
|
||||||
connect(task);
|
|
||||||
authenticate(task);
|
|
||||||
task.onRun(function() {
|
|
||||||
function recursiveDownloadMetadata() {
|
|
||||||
if(paths.length === 0) {
|
|
||||||
task.chain();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
var path = paths[0];
|
|
||||||
client.stat(path, function(error, stat) {
|
|
||||||
if(stat) {
|
|
||||||
result.push(stat);
|
|
||||||
paths.shift();
|
|
||||||
task.chain(recursiveDownloadMetadata);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
handleError(error, task);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
task.chain(recursiveDownloadMetadata);
|
|
||||||
});
|
|
||||||
task.onSuccess(function() {
|
|
||||||
callback(undefined, result);
|
|
||||||
});
|
|
||||||
task.onError(function(error) {
|
|
||||||
callback(error);
|
|
||||||
});
|
|
||||||
task.enqueue();
|
|
||||||
};
|
|
||||||
|
|
||||||
dropboxHelper.downloadContent = function(objects, callback) {
|
dropboxHelper.upload = function(path, content, callback) {
|
||||||
var result = [];
|
var result;
|
||||||
var task = new AsyncTask();
|
var task = new AsyncTask();
|
||||||
connect(task);
|
connect(task);
|
||||||
authenticate(task);
|
authenticate(task);
|
||||||
task.onRun(function() {
|
task.onRun(function() {
|
||||||
function recursiveDownloadContent() {
|
client.writeFile(path, content, function(error, stat) {
|
||||||
if(objects.length === 0) {
|
if(!error) {
|
||||||
task.chain();
|
result = stat;
|
||||||
return;
|
return task.chain();
|
||||||
}
|
}
|
||||||
var object = objects[0];
|
// Handle error
|
||||||
result.push(object);
|
if(error.status === 400) {
|
||||||
var file;
|
error = 'Could not upload document into path "' + path + '".';
|
||||||
// object may be a file
|
}
|
||||||
if(object.isFile === true) {
|
handleError(error, task);
|
||||||
file = object;
|
});
|
||||||
}
|
});
|
||||||
// object may be a change
|
task.onSuccess(function() {
|
||||||
else if(object.wasRemoved !== undefined) {
|
callback(undefined, result);
|
||||||
file = object.stat;
|
});
|
||||||
}
|
task.onError(function(error) {
|
||||||
if(!file) {
|
callback(error);
|
||||||
objects.shift();
|
});
|
||||||
task.chain(recursiveDownloadContent);
|
task.enqueue();
|
||||||
return;
|
};
|
||||||
}
|
|
||||||
client.readFile(file.path, function(error, data) {
|
|
||||||
if(_.isString(data)) {
|
|
||||||
file.content = data;
|
|
||||||
objects.shift();
|
|
||||||
task.chain(recursiveDownloadContent);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
handleError(error, task);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
task.chain(recursiveDownloadContent);
|
|
||||||
});
|
|
||||||
task.onSuccess(function() {
|
|
||||||
callback(undefined, result);
|
|
||||||
});
|
|
||||||
task.onError(function(error) {
|
|
||||||
callback(error);
|
|
||||||
});
|
|
||||||
task.enqueue();
|
|
||||||
};
|
|
||||||
|
|
||||||
function handleError(error, task) {
|
dropboxHelper.checkChanges = function(lastChangeId, callback) {
|
||||||
var errorMsg = true;
|
var changes = [];
|
||||||
if(error) {
|
var newChangeId = lastChangeId || 0;
|
||||||
logger.error(error);
|
var task = new AsyncTask();
|
||||||
// Try to analyze the error
|
connect(task);
|
||||||
if(typeof error === "string") {
|
authenticate(task);
|
||||||
errorMsg = error;
|
task.onRun(function() {
|
||||||
}
|
function retrievePageOfChanges() {
|
||||||
else {
|
client.pullChanges(newChangeId, function(error, pullChanges) {
|
||||||
errorMsg = "Dropbox error (" + error.status + ": " + error.responseText + ").";
|
if(error) {
|
||||||
|
return handleError(error, task);
|
||||||
|
}
|
||||||
|
// Retrieve success
|
||||||
|
newChangeId = pullChanges.cursor();
|
||||||
|
if(pullChanges.changes !== undefined) {
|
||||||
|
changes = changes.concat(pullChanges.changes);
|
||||||
|
}
|
||||||
|
if(pullChanges.shouldPullAgain) {
|
||||||
|
task.chain(retrievePageOfChanges);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
task.chain();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
if(error.status === 401 || error.status === 403) {
|
task.chain(retrievePageOfChanges);
|
||||||
authenticated = false;
|
});
|
||||||
errorMsg = "Access to Dropbox account is not authorized.";
|
task.onSuccess(function() {
|
||||||
task.retry(new Error(errorMsg), 1);
|
callback(undefined, changes, newChangeId);
|
||||||
return;
|
});
|
||||||
}
|
task.onError(function(error) {
|
||||||
else if(error.status === 400 && error.responseText.indexOf("oauth_nonce") !== -1) {
|
callback(error);
|
||||||
// A bug I guess...
|
});
|
||||||
_.each(_.keys(storage), function(key) {
|
task.enqueue();
|
||||||
// We have to remove the Oauth cache from the
|
};
|
||||||
// storage
|
|
||||||
if(key.indexOf("dropbox-auth") === 0) {
|
|
||||||
storage.removeItem(key);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
authenticated = false;
|
|
||||||
task.retry(new Error(errorMsg), 1);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
else if(error.status <= 0) {
|
|
||||||
client = undefined;
|
|
||||||
authenticated = false;
|
|
||||||
core.setOffline();
|
|
||||||
errorMsg = "|stopPublish";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
task.error(new Error(errorMsg));
|
|
||||||
}
|
|
||||||
|
|
||||||
var pickerLoaded = false;
|
dropboxHelper.downloadMetadata = function(paths, callback) {
|
||||||
function loadPicker(task) {
|
var result = [];
|
||||||
task.onRun(function() {
|
var task = new AsyncTask();
|
||||||
if(pickerLoaded === true) {
|
connect(task);
|
||||||
task.chain();
|
authenticate(task);
|
||||||
return;
|
task.onRun(function() {
|
||||||
}
|
function recursiveDownloadMetadata() {
|
||||||
function chooserRedirect() {
|
if(paths.length === 0) {
|
||||||
utils.redirectConfirm('You are being redirected to <strong>Dropbox Chooser</strong> page.', function() {
|
return task.chain();
|
||||||
task.chain();
|
}
|
||||||
}, function() {
|
var path = paths[0];
|
||||||
task.error(new Error('Operation canceled.'));
|
client.stat(path, function(error, stat) {
|
||||||
});
|
if(stat) {
|
||||||
}
|
result.push(stat);
|
||||||
$.ajax({
|
paths.shift();
|
||||||
url: "https://www.dropbox.com/static/api/1/dropbox.js",
|
return task.chain(recursiveDownloadMetadata);
|
||||||
dataType: "script",
|
}
|
||||||
timeout: constants.AJAX_TIMEOUT
|
handleError(error, task);
|
||||||
}).done(function() {
|
});
|
||||||
pickerLoaded = true;
|
}
|
||||||
task.chain(chooserRedirect);
|
|
||||||
}).fail(function(jqXHR) {
|
|
||||||
var error = {
|
|
||||||
status: jqXHR.status,
|
|
||||||
responseText: jqXHR.statusText
|
|
||||||
};
|
|
||||||
handleError(error, task);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
dropboxHelper.picker = function(callback) {
|
task.chain(recursiveDownloadMetadata);
|
||||||
var paths = [];
|
});
|
||||||
var task = new AsyncTask();
|
task.onSuccess(function() {
|
||||||
// Add some time for user to choose his files
|
callback(undefined, result);
|
||||||
task.timeout = constants.ASYNC_TASK_LONG_TIMEOUT;
|
});
|
||||||
connect(task);
|
task.onError(function(error) {
|
||||||
loadPicker(task);
|
callback(error);
|
||||||
task.onRun(function() {
|
});
|
||||||
var options = {};
|
task.enqueue();
|
||||||
options.multiselect = true;
|
};
|
||||||
options.linkType = "direct";
|
|
||||||
options.success = function(files) {
|
|
||||||
for ( var i = 0; i < files.length; i++) {
|
|
||||||
var path = files[i].link;
|
|
||||||
path = path.replace(/.*\/view\/[^\/]*/, "");
|
|
||||||
paths.push(decodeURI(path));
|
|
||||||
}
|
|
||||||
task.chain();
|
|
||||||
};
|
|
||||||
options.cancel = function() {
|
|
||||||
task.chain();
|
|
||||||
};
|
|
||||||
Dropbox.choose(options);
|
|
||||||
});
|
|
||||||
task.onSuccess(function() {
|
|
||||||
callback(undefined, paths);
|
|
||||||
});
|
|
||||||
task.onError(function(error) {
|
|
||||||
callback(error);
|
|
||||||
});
|
|
||||||
task.enqueue();
|
|
||||||
};
|
|
||||||
|
|
||||||
return dropboxHelper;
|
dropboxHelper.downloadContent = function(objects, callback) {
|
||||||
|
var result = [];
|
||||||
|
var task = new AsyncTask();
|
||||||
|
connect(task);
|
||||||
|
authenticate(task);
|
||||||
|
task.onRun(function() {
|
||||||
|
function recursiveDownloadContent() {
|
||||||
|
if(objects.length === 0) {
|
||||||
|
return task.chain();
|
||||||
|
}
|
||||||
|
var object = objects[0];
|
||||||
|
result.push(object);
|
||||||
|
var file;
|
||||||
|
// object may be a file
|
||||||
|
if(object.isFile === true) {
|
||||||
|
file = object;
|
||||||
|
}
|
||||||
|
// object may be a change
|
||||||
|
else if(object.wasRemoved !== undefined) {
|
||||||
|
file = object.stat;
|
||||||
|
}
|
||||||
|
if(!file) {
|
||||||
|
objects.shift();
|
||||||
|
return task.chain(recursiveDownloadContent);
|
||||||
|
}
|
||||||
|
client.readFile(file.path, function(error, data) {
|
||||||
|
if(_.isString(data)) {
|
||||||
|
file.content = data;
|
||||||
|
objects.shift();
|
||||||
|
return task.chain(recursiveDownloadContent);
|
||||||
|
}
|
||||||
|
handleError(error, task);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
task.chain(recursiveDownloadContent);
|
||||||
|
});
|
||||||
|
task.onSuccess(function() {
|
||||||
|
callback(undefined, result);
|
||||||
|
});
|
||||||
|
task.onError(function(error) {
|
||||||
|
callback(error);
|
||||||
|
});
|
||||||
|
task.enqueue();
|
||||||
|
};
|
||||||
|
|
||||||
|
function handleError(error, task) {
|
||||||
|
var errorMsg = true;
|
||||||
|
if(error) {
|
||||||
|
logger.error(error);
|
||||||
|
// Try to analyze the error
|
||||||
|
if(typeof error === "string") {
|
||||||
|
errorMsg = error;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
errorMsg = "Dropbox error (" + error.status + ": " + error.responseText + ").";
|
||||||
|
|
||||||
|
if(error.status === 401 || error.status === 403) {
|
||||||
|
authenticated = false;
|
||||||
|
errorMsg = "Access to Dropbox account is not authorized.";
|
||||||
|
return task.retry(new Error(errorMsg), 1);
|
||||||
|
}
|
||||||
|
else if(error.status === 400 && error.responseText.indexOf("oauth_nonce") !== -1) {
|
||||||
|
// A bug I guess...
|
||||||
|
_.each(_.keys(storage), function(key) {
|
||||||
|
// We have to remove the Oauth cache from the
|
||||||
|
// storage
|
||||||
|
if(key.indexOf("dropbox-auth") === 0) {
|
||||||
|
storage.removeItem(key);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
authenticated = false;
|
||||||
|
return task.retry(new Error(errorMsg), 1);
|
||||||
|
}
|
||||||
|
else if(error.status <= 0) {
|
||||||
|
client = undefined;
|
||||||
|
authenticated = false;
|
||||||
|
core.setOffline();
|
||||||
|
errorMsg = "|stopPublish";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
task.error(new Error(errorMsg));
|
||||||
|
}
|
||||||
|
|
||||||
|
var pickerLoaded = false;
|
||||||
|
|
||||||
|
function loadPicker(task) {
|
||||||
|
task.onRun(function() {
|
||||||
|
if(pickerLoaded === true) {
|
||||||
|
return task.chain();
|
||||||
|
}
|
||||||
|
function chooserRedirect() {
|
||||||
|
utils.redirectConfirm('You are being redirected to <strong>Dropbox Chooser</strong> page.', function() {
|
||||||
|
task.chain();
|
||||||
|
}, function() {
|
||||||
|
task.error(new Error('Operation canceled.'));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
$.ajax({
|
||||||
|
url: "https://www.dropbox.com/static/api/1/dropbox.js",
|
||||||
|
dataType: "script",
|
||||||
|
timeout: constants.AJAX_TIMEOUT
|
||||||
|
}).done(function() {
|
||||||
|
pickerLoaded = true;
|
||||||
|
task.chain(chooserRedirect);
|
||||||
|
}).fail(function(jqXHR) {
|
||||||
|
var error = {
|
||||||
|
status: jqXHR.status,
|
||||||
|
responseText: jqXHR.statusText
|
||||||
|
};
|
||||||
|
handleError(error, task);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
dropboxHelper.picker = function(callback) {
|
||||||
|
var paths = [];
|
||||||
|
var task = new AsyncTask();
|
||||||
|
// Add some time for user to choose his files
|
||||||
|
task.timeout = constants.ASYNC_TASK_LONG_TIMEOUT;
|
||||||
|
connect(task);
|
||||||
|
loadPicker(task);
|
||||||
|
task.onRun(function() {
|
||||||
|
var options = {};
|
||||||
|
options.multiselect = true;
|
||||||
|
options.linkType = "direct";
|
||||||
|
options.success = function(files) {
|
||||||
|
for(var i = 0; i < files.length; i++) {
|
||||||
|
var path = files[i].link;
|
||||||
|
path = path.replace(/.*\/view\/[^\/]*/, "");
|
||||||
|
paths.push(decodeURI(path));
|
||||||
|
}
|
||||||
|
task.chain();
|
||||||
|
};
|
||||||
|
options.cancel = function() {
|
||||||
|
task.chain();
|
||||||
|
};
|
||||||
|
Dropbox.choose(options);
|
||||||
|
});
|
||||||
|
task.onSuccess(function() {
|
||||||
|
callback(undefined, paths);
|
||||||
|
});
|
||||||
|
task.onError(function(error) {
|
||||||
|
callback(error);
|
||||||
|
});
|
||||||
|
task.enqueue();
|
||||||
|
};
|
||||||
|
|
||||||
|
return dropboxHelper;
|
||||||
});
|
});
|
||||||
|
Loading…
Reference in New Issue
Block a user