2013-04-20 00:14:20 +00:00
|
|
|
define(["jquery", "async-runner"], function($, asyncRunner) {
|
2013-03-27 20:19:12 +00:00
|
|
|
|
2013-04-02 18:42:47 +00:00
|
|
|
// Dependencies
|
2013-04-10 18:14:59 +00:00
|
|
|
var core = undefined;
|
2013-03-27 20:19:12 +00:00
|
|
|
|
2013-03-30 11:56:17 +00:00
|
|
|
var connected = false;
|
|
|
|
var authenticated = false;
|
2013-03-27 20:19:12 +00:00
|
|
|
|
2013-04-09 07:58:06 +00:00
|
|
|
var googleHelper = {};
|
2013-04-01 01:06:52 +00:00
|
|
|
|
2013-03-30 11:56:17 +00:00
|
|
|
// Try to connect Gdrive by downloading client.js
|
2013-04-20 00:14:20 +00:00
|
|
|
function connect(task) {
|
|
|
|
task.onRun(function() {
|
2013-04-05 23:59:59 +00:00
|
|
|
if(core.isOffline === true) {
|
|
|
|
connected = false;
|
|
|
|
core.showMessage("Operation not available in offline mode.");
|
2013-04-20 00:14:20 +00:00
|
|
|
task.error();
|
2013-04-05 23:59:59 +00:00
|
|
|
return;
|
|
|
|
}
|
2013-04-01 01:06:52 +00:00
|
|
|
if (connected === true) {
|
2013-04-20 00:14:20 +00:00
|
|
|
task.chain();
|
2013-03-30 11:56:17 +00:00
|
|
|
return;
|
|
|
|
}
|
2013-04-02 18:42:47 +00:00
|
|
|
delayedFunction = function() {
|
2013-04-20 00:14:20 +00:00
|
|
|
connected = true;
|
|
|
|
task.chain();
|
2013-03-30 11:56:17 +00:00
|
|
|
};
|
2013-04-01 16:46:48 +00:00
|
|
|
$.ajax({
|
2013-04-02 18:42:47 +00:00
|
|
|
url : "https://apis.google.com/js/client.js?onload=runDelayedFunction",
|
2013-04-01 16:46:48 +00:00
|
|
|
dataType : "script", timeout : AJAX_TIMEOUT
|
|
|
|
}).fail(function() {
|
2013-04-20 00:14:20 +00:00
|
|
|
core.setOffline();
|
|
|
|
task.error(new Error("Network timeout"));
|
2013-04-01 16:46:48 +00:00
|
|
|
});
|
2013-04-20 00:14:20 +00:00
|
|
|
});
|
2013-03-30 11:56:17 +00:00
|
|
|
}
|
2013-04-01 01:06:52 +00:00
|
|
|
|
2013-03-30 11:56:17 +00:00
|
|
|
// Try to authenticate with Oauth
|
2013-04-20 00:14:20 +00:00
|
|
|
function authenticate(task) {
|
|
|
|
task.onRun(function() {
|
|
|
|
if (authenticated === true) {
|
|
|
|
task.chain();
|
2013-03-30 11:56:17 +00:00
|
|
|
return;
|
|
|
|
}
|
2013-04-20 00:14:20 +00:00
|
|
|
var immediate = true;
|
|
|
|
function localAuthenticate() {
|
2013-04-01 01:06:52 +00:00
|
|
|
if (immediate === false) {
|
2013-04-02 18:42:47 +00:00
|
|
|
core.showMessage("Please make sure the Google authorization popup is not blocked by your browser.");
|
2013-04-20 00:14:20 +00:00
|
|
|
// If not immediate we add time for user to enter his credentials
|
|
|
|
task.timeout = ASYNC_TASK_LONG_TIMEOUT;
|
2013-04-01 01:06:52 +00:00
|
|
|
}
|
|
|
|
gapi.auth.authorize({ 'client_id' : GOOGLE_CLIENT_ID,
|
2013-04-02 18:42:47 +00:00
|
|
|
'scope' : GOOGLE_SCOPES, 'immediate' : immediate }, function(
|
2013-04-01 01:06:52 +00:00
|
|
|
authResult) {
|
2013-03-30 11:56:17 +00:00
|
|
|
gapi.client.load('drive', 'v2', function() {
|
2013-04-05 23:59:59 +00:00
|
|
|
if (!authResult || authResult.error) {
|
2013-04-20 00:14:20 +00:00
|
|
|
// If immediate did not work retry without immediate flag
|
|
|
|
if (connected === true && immediate === true) {
|
|
|
|
immediate = false;
|
|
|
|
task.chain(localAuthenticate);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Error
|
|
|
|
var errorMsg = "Access to Google account is not authorized.";
|
|
|
|
core.showError(errorMsg);
|
|
|
|
task.error(new Error(errorMsg));
|
2013-04-05 23:59:59 +00:00
|
|
|
return;
|
|
|
|
}
|
2013-04-20 00:14:20 +00:00
|
|
|
// Success
|
2013-03-30 11:56:17 +00:00
|
|
|
authenticated = true;
|
2013-04-20 00:14:20 +00:00
|
|
|
task.chain();
|
2013-03-30 11:56:17 +00:00
|
|
|
});
|
|
|
|
});
|
2013-04-20 00:14:20 +00:00
|
|
|
}
|
|
|
|
task.chain(localAuthenticate);
|
2013-03-30 11:56:17 +00:00
|
|
|
});
|
2013-03-27 20:19:12 +00:00
|
|
|
}
|
|
|
|
|
2013-04-20 17:40:05 +00:00
|
|
|
googleHelper.upload = function(fileId, parentId, title, content, etag, callback) {
|
2013-04-02 18:42:47 +00:00
|
|
|
callback = callback || core.doNothing;
|
2013-04-20 00:14:20 +00:00
|
|
|
var result = undefined;
|
|
|
|
var task = asyncRunner.createTask();
|
|
|
|
connect(task);
|
|
|
|
authenticate(task);
|
|
|
|
task.onRun(function() {
|
|
|
|
var boundary = '-------314159265358979323846';
|
|
|
|
var delimiter = "\r\n--" + boundary + "\r\n";
|
|
|
|
var close_delim = "\r\n--" + boundary + "--";
|
|
|
|
var contentType = 'text/x-markdown';
|
|
|
|
var metadata = { title : title, mimeType : contentType };
|
|
|
|
if (parentId !== undefined) {
|
|
|
|
// Specify the directory
|
|
|
|
metadata.parents = [ { kind : 'drive#fileLink',
|
|
|
|
id : parentId } ];
|
|
|
|
}
|
|
|
|
var path = '/upload/drive/v2/files';
|
|
|
|
var method = 'POST';
|
|
|
|
if (fileId !== undefined) {
|
|
|
|
// If it's an update
|
|
|
|
path += "/" + fileId;
|
|
|
|
method = 'PUT';
|
|
|
|
}
|
|
|
|
var headers = { 'Content-Type' : 'multipart/mixed; boundary="'
|
|
|
|
+ boundary + '"', };
|
|
|
|
if(etag !== undefined) {
|
|
|
|
headers["If-Match"] = etag;
|
2013-03-30 11:56:17 +00:00
|
|
|
}
|
2013-04-01 01:06:52 +00:00
|
|
|
|
2013-04-20 00:14:20 +00:00
|
|
|
var base64Data = core.encodeBase64(content);
|
|
|
|
var multipartRequestBody = delimiter
|
|
|
|
+ 'Content-Type: application/json\r\n\r\n'
|
|
|
|
+ JSON.stringify(metadata) + delimiter + 'Content-Type: '
|
|
|
|
+ contentType + '\r\n'
|
|
|
|
+ 'Content-Transfer-Encoding: base64\r\n' + '\r\n'
|
|
|
|
+ base64Data + close_delim;
|
2013-03-27 20:19:12 +00:00
|
|
|
|
2013-04-20 00:14:20 +00:00
|
|
|
var request = gapi.client
|
|
|
|
.request({
|
|
|
|
'path' : path,
|
|
|
|
'method' : method,
|
|
|
|
'params' : { 'uploadType' : 'multipart', },
|
|
|
|
'headers' : headers,
|
|
|
|
'body' : multipartRequestBody, });
|
|
|
|
request.execute(function(response) {
|
|
|
|
if (response && response.id) {
|
|
|
|
// Upload success
|
|
|
|
result = response;
|
|
|
|
task.chain();
|
|
|
|
return;
|
2013-03-30 11:56:17 +00:00
|
|
|
}
|
2013-04-20 00:14:20 +00:00
|
|
|
var error = response.error;
|
|
|
|
// Handle error
|
|
|
|
if(error !== undefined && fileId !== undefined) {
|
|
|
|
if(error.code === 404) {
|
|
|
|
error = 'File ID "' + fileId + '" does not exist on Google Drive.';
|
2013-03-30 11:56:17 +00:00
|
|
|
}
|
2013-04-20 00:14:20 +00:00
|
|
|
else if(error.code === 412) {
|
|
|
|
// We may have missed a file update
|
|
|
|
localStorage.removeItem("sync.gdrive.lastChangeId");
|
|
|
|
error = 'Conflict on file ID "' + fileId + '". Please restart the synchronization.';
|
2013-03-30 11:56:17 +00:00
|
|
|
}
|
2013-04-20 00:14:20 +00:00
|
|
|
}
|
|
|
|
handleError(error, task, callback);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
task.onSuccess(function() {
|
|
|
|
callback(undefined, result);
|
|
|
|
});
|
|
|
|
task.onError(function(error) {
|
|
|
|
callback(error);
|
2013-04-01 01:06:52 +00:00
|
|
|
});
|
2013-04-20 00:14:20 +00:00
|
|
|
asyncRunner.addTask(task);
|
2013-04-07 15:22:13 +00:00
|
|
|
};
|
2013-04-01 01:06:52 +00:00
|
|
|
|
2013-04-20 17:40:05 +00:00
|
|
|
googleHelper.checkChanges = function(lastChangeId, callback) {
|
2013-04-02 18:42:47 +00:00
|
|
|
callback = callback || core.doNothing;
|
2013-04-20 00:14:20 +00:00
|
|
|
var changes = [];
|
|
|
|
var newChangeId = lastChangeId || 0;
|
|
|
|
var task = asyncRunner.createTask();
|
|
|
|
connect(task);
|
|
|
|
authenticate(task);
|
|
|
|
task.onRun(function() {
|
|
|
|
var nextPageToken = undefined;
|
|
|
|
function retrievePageOfChanges() {
|
|
|
|
var request = undefined;
|
|
|
|
if(nextPageToken === undefined) {
|
|
|
|
request = gapi.client.drive.changes
|
|
|
|
.list({ 'startChangeId' : newChangeId + 1 });
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
request = gapi.client.drive.changes
|
|
|
|
.list({ 'pageToken' : nextPageToken });
|
|
|
|
}
|
2013-04-01 01:06:52 +00:00
|
|
|
|
2013-04-20 00:14:20 +00:00
|
|
|
request.execute(function(response) {
|
|
|
|
if (!response || !response.largestChangeId) {
|
|
|
|
// Handle error
|
|
|
|
handleError(response.error, task, callback);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Retrieve success
|
|
|
|
newChangeId = response.largestChangeId;
|
|
|
|
nextPageToken = response.nextPageToken;
|
|
|
|
if (response.items !== undefined) {
|
2013-04-20 17:40:05 +00:00
|
|
|
changes = changes.concat(response.items);
|
2013-04-20 00:14:20 +00:00
|
|
|
}
|
2013-04-01 01:06:52 +00:00
|
|
|
if (nextPageToken !== undefined) {
|
2013-04-20 00:14:20 +00:00
|
|
|
task.chain(retrievePageOfChanges);
|
2013-04-01 01:06:52 +00:00
|
|
|
}
|
2013-04-20 00:14:20 +00:00
|
|
|
else {
|
|
|
|
task.chain();
|
|
|
|
}
|
|
|
|
});
|
2013-04-01 01:06:52 +00:00
|
|
|
}
|
2013-04-20 00:14:20 +00:00
|
|
|
task.chain(retrievePageOfChanges);
|
2013-04-01 01:06:52 +00:00
|
|
|
});
|
2013-04-20 00:14:20 +00:00
|
|
|
task.onSuccess(function() {
|
|
|
|
callback(undefined, changes, newChangeId);
|
|
|
|
});
|
|
|
|
task.onError(function(error) {
|
|
|
|
callback(error);
|
|
|
|
});
|
|
|
|
asyncRunner.addTask(task);
|
2013-04-01 01:06:52 +00:00
|
|
|
};
|
|
|
|
|
2013-04-20 00:14:20 +00:00
|
|
|
googleHelper.downloadMetadata = function(ids, callback) {
|
2013-04-02 18:42:47 +00:00
|
|
|
callback = callback || core.doNothing;
|
2013-04-20 00:14:20 +00:00
|
|
|
var result = [];
|
|
|
|
var task = asyncRunner.createTask();
|
|
|
|
connect(task);
|
|
|
|
authenticate(task);
|
|
|
|
task.onRun(function() {
|
|
|
|
function recursiveDownloadMetadata() {
|
|
|
|
if(ids.length === 0) {
|
|
|
|
task.chain();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
var id = ids.pop();
|
2013-04-07 15:22:13 +00:00
|
|
|
var token = gapi.auth.getToken();
|
|
|
|
var headers = {
|
|
|
|
Authorization : token ? "Bearer " + token.access_token: null
|
|
|
|
};
|
2013-04-01 23:12:28 +00:00
|
|
|
$.ajax({
|
|
|
|
url : "https://www.googleapis.com/drive/v2/files/" + id,
|
2013-04-07 15:22:13 +00:00
|
|
|
headers : headers,
|
2013-04-01 23:12:28 +00:00
|
|
|
dataType : "json",
|
|
|
|
timeout : AJAX_TIMEOUT
|
|
|
|
}).done(function(data, textStatus, jqXHR) {
|
|
|
|
result.push(data);
|
2013-04-20 00:14:20 +00:00
|
|
|
task.chain(recursiveDownloadMetadata);
|
2013-04-01 23:12:28 +00:00
|
|
|
}).fail(function(jqXHR) {
|
|
|
|
var error = {
|
|
|
|
code: jqXHR.status,
|
|
|
|
message: jqXHR.statusText
|
|
|
|
};
|
|
|
|
// Handle error
|
2013-04-03 22:52:29 +00:00
|
|
|
if(error.code === 404) {
|
2013-04-07 15:22:13 +00:00
|
|
|
error = 'File ID "' + id + '" does not exist on Google Drive.';
|
2013-04-02 18:42:47 +00:00
|
|
|
}
|
2013-04-20 00:14:20 +00:00
|
|
|
handleError(error, task, callback);
|
2013-04-01 23:12:28 +00:00
|
|
|
});
|
2013-04-20 00:14:20 +00:00
|
|
|
}
|
|
|
|
task.chain(recursiveDownloadMetadata);
|
|
|
|
});
|
|
|
|
task.onSuccess(function() {
|
|
|
|
callback(undefined, result);
|
|
|
|
});
|
|
|
|
task.onError(function(error) {
|
|
|
|
callback(error);
|
2013-04-01 23:12:28 +00:00
|
|
|
});
|
2013-04-20 00:14:20 +00:00
|
|
|
asyncRunner.addTask(task);
|
2013-04-01 23:12:28 +00:00
|
|
|
};
|
|
|
|
|
2013-04-20 00:14:20 +00:00
|
|
|
googleHelper.downloadContent = function(objects, callback) {
|
2013-04-02 18:42:47 +00:00
|
|
|
callback = callback || core.doNothing;
|
2013-04-20 00:14:20 +00:00
|
|
|
var result = [];
|
|
|
|
var task = asyncRunner.createTask();
|
|
|
|
// Add some time for user to choose his files
|
|
|
|
task.timeout = ASYNC_TASK_LONG_TIMEOUT;
|
|
|
|
connect(task);
|
|
|
|
authenticate(task);
|
|
|
|
task.onRun(function() {
|
|
|
|
function recursiveDownloadContent() {
|
|
|
|
if(objects.length === 0) {
|
|
|
|
task.chain();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
var object = objects.pop();
|
|
|
|
result.push(object);
|
|
|
|
var file = undefined;
|
|
|
|
// object may be a file
|
|
|
|
if(object.kind == "drive#file") {
|
|
|
|
file = object;
|
|
|
|
}
|
|
|
|
// object may be a change
|
|
|
|
else if(object.kind == "drive#change") {
|
|
|
|
file = object.file;
|
|
|
|
}
|
|
|
|
if(!file) {
|
|
|
|
task.chain(recursiveDownloadContent);
|
|
|
|
return;
|
|
|
|
}
|
2013-04-07 15:22:13 +00:00
|
|
|
var token = gapi.auth.getToken();
|
|
|
|
var headers = {
|
|
|
|
Authorization : token ? "Bearer " + token.access_token: null
|
|
|
|
};
|
2013-04-01 16:46:48 +00:00
|
|
|
$.ajax({
|
|
|
|
url : file.downloadUrl,
|
2013-04-07 15:22:13 +00:00
|
|
|
headers : headers,
|
2013-04-01 23:12:28 +00:00
|
|
|
dataType : "text",
|
2013-04-01 16:46:48 +00:00
|
|
|
timeout : AJAX_TIMEOUT
|
|
|
|
}).done(function(data, textStatus, jqXHR) {
|
|
|
|
file.content = data;
|
2013-04-20 00:14:20 +00:00
|
|
|
task.chain(recursiveDownloadContent);
|
2013-04-01 16:46:48 +00:00
|
|
|
}).fail(function(jqXHR) {
|
|
|
|
var error = {
|
|
|
|
code: jqXHR.status,
|
|
|
|
message: jqXHR.statusText
|
|
|
|
};
|
|
|
|
// Handle error
|
2013-04-20 00:14:20 +00:00
|
|
|
handleError(error, task, callback);
|
2013-04-01 16:46:48 +00:00
|
|
|
});
|
2013-04-20 00:14:20 +00:00
|
|
|
}
|
|
|
|
task.chain(recursiveDownloadContent);
|
2013-04-01 01:06:52 +00:00
|
|
|
});
|
2013-04-20 00:14:20 +00:00
|
|
|
task.onSuccess(function() {
|
|
|
|
callback(undefined, result);
|
|
|
|
});
|
|
|
|
task.onError(function(error) {
|
|
|
|
callback(error);
|
|
|
|
});
|
|
|
|
asyncRunner.addTask(task);
|
2013-04-01 01:06:52 +00:00
|
|
|
};
|
2013-04-01 23:12:28 +00:00
|
|
|
|
2013-04-20 00:14:20 +00:00
|
|
|
function handleError(error, task, callback) {
|
2013-04-02 18:42:47 +00:00
|
|
|
var errorMsg = undefined;
|
|
|
|
if (error) {
|
2013-04-13 18:11:54 +00:00
|
|
|
console.error(error);
|
2013-04-02 18:42:47 +00:00
|
|
|
// Try to analyze the error
|
|
|
|
if (typeof error === "string") {
|
|
|
|
errorMsg = error;
|
|
|
|
}
|
2013-04-03 22:52:29 +00:00
|
|
|
else if (error.code >= 500 && error.code < 600) {
|
2013-04-02 18:42:47 +00:00
|
|
|
// Retry as described in Google's best practices
|
2013-04-20 00:14:20 +00:00
|
|
|
task.retry();
|
2013-04-02 18:42:47 +00:00
|
|
|
return;
|
|
|
|
} else if (error.code === 401 || error.code === 403) {
|
|
|
|
authenticated = false;
|
2013-04-20 00:14:20 +00:00
|
|
|
task.retry();
|
|
|
|
return;
|
2013-04-02 18:42:47 +00:00
|
|
|
} else if (error.code <= 0) {
|
|
|
|
connected = false;
|
|
|
|
authenticated = false;
|
|
|
|
core.setOffline();
|
|
|
|
} else {
|
2013-04-20 00:14:20 +00:00
|
|
|
errorMsg = "Google error (" + error.code + ": "
|
2013-04-02 18:42:47 +00:00
|
|
|
+ error.message + ").";
|
|
|
|
}
|
2013-04-20 00:14:20 +00:00
|
|
|
core.showError(errorMsg);
|
2013-04-02 18:42:47 +00:00
|
|
|
}
|
2013-04-20 00:14:20 +00:00
|
|
|
task.error(new Error(errorMsg));
|
2013-04-02 18:42:47 +00:00
|
|
|
}
|
|
|
|
|
2013-04-05 23:59:59 +00:00
|
|
|
var pickerLoaded = false;
|
2013-04-20 00:14:20 +00:00
|
|
|
function loadPicker(task) {
|
|
|
|
task.onRun(function() {
|
|
|
|
if (pickerLoaded === true) {
|
|
|
|
task.chain();
|
2013-04-05 23:59:59 +00:00
|
|
|
return;
|
2013-04-20 00:14:20 +00:00
|
|
|
}
|
|
|
|
$.ajax({
|
|
|
|
url : "//www.google.com/jsapi",
|
|
|
|
data : {key: GOOGLE_KEY},
|
|
|
|
dataType : "script", timeout : AJAX_TIMEOUT
|
|
|
|
}).done(function() {
|
|
|
|
google.load('picker', '1', {callback: task.chain});
|
2013-04-06 00:57:52 +00:00
|
|
|
pickerLoaded = true;
|
2013-04-20 00:14:20 +00:00
|
|
|
}).fail(function() {
|
2013-04-06 00:57:52 +00:00
|
|
|
core.setOffline();
|
2013-04-20 00:14:20 +00:00
|
|
|
task.error(new Error("Network timeout"));
|
|
|
|
});
|
2013-04-06 00:57:52 +00:00
|
|
|
});
|
2013-04-05 23:59:59 +00:00
|
|
|
}
|
|
|
|
|
2013-04-09 07:58:06 +00:00
|
|
|
googleHelper.picker = function(callback) {
|
2013-04-05 23:59:59 +00:00
|
|
|
callback = callback || core.doNothing;
|
2013-04-20 00:14:20 +00:00
|
|
|
var ids = [];
|
|
|
|
var picker = undefined;
|
|
|
|
var task = asyncRunner.createTask();
|
|
|
|
connect(task);
|
|
|
|
loadPicker(task);
|
|
|
|
task.onRun(function() {
|
2013-04-06 01:07:45 +00:00
|
|
|
var view = new google.picker.View(google.picker.ViewId.DOCS);
|
|
|
|
view.setMimeTypes("text/x-markdown,text/plain");
|
|
|
|
var pickerBuilder = new google.picker.PickerBuilder();
|
|
|
|
pickerBuilder.enableFeature(google.picker.Feature.NAV_HIDDEN);
|
|
|
|
pickerBuilder.enableFeature(google.picker.Feature.MULTISELECT_ENABLED);
|
|
|
|
pickerBuilder.setAppId(GOOGLE_DRIVE_APP_ID);
|
2013-04-06 00:57:52 +00:00
|
|
|
var token = gapi.auth.getToken();
|
2013-04-06 01:07:45 +00:00
|
|
|
if(token) {
|
|
|
|
pickerBuilder.setOAuthToken(token.access_token);
|
|
|
|
}
|
|
|
|
pickerBuilder.addView(view);
|
|
|
|
pickerBuilder.addView(new google.picker.DocsUploadView());
|
|
|
|
pickerBuilder.setCallback(function(data) {
|
|
|
|
if (data.action == google.picker.Action.PICKED ||
|
|
|
|
data.action == google.picker.Action.CANCEL) {
|
|
|
|
if(data.action == google.picker.Action.PICKED) {
|
|
|
|
for(var i=0; i<data.docs.length; i++) {
|
|
|
|
ids.push(data.docs[i].id);
|
2013-04-05 23:59:59 +00:00
|
|
|
}
|
2013-04-06 01:07:45 +00:00
|
|
|
}
|
|
|
|
$(".modal-backdrop, .picker").remove();
|
2013-04-20 00:14:20 +00:00
|
|
|
task.chain();
|
2013-04-06 01:07:45 +00:00
|
|
|
}
|
|
|
|
});
|
2013-04-20 00:14:20 +00:00
|
|
|
picker = pickerBuilder.build();
|
2013-04-06 01:07:45 +00:00
|
|
|
$("body").append($("<div>").addClass("modal-backdrop").click(function() {
|
|
|
|
picker.setVisible(false);
|
2013-04-06 00:57:52 +00:00
|
|
|
$(".modal-backdrop, .picker").remove();
|
2013-04-20 00:14:20 +00:00
|
|
|
task.chain();
|
2013-04-06 01:07:45 +00:00
|
|
|
}));
|
|
|
|
picker.setVisible(true);
|
2013-04-05 23:59:59 +00:00
|
|
|
});
|
2013-04-20 00:14:20 +00:00
|
|
|
task.onSuccess(function() {
|
|
|
|
callback(undefined, ids);
|
|
|
|
});
|
|
|
|
task.onError(function(error) {
|
|
|
|
if(picker !== undefined) {
|
|
|
|
picker.setVisible(false);
|
|
|
|
$(".modal-backdrop, .picker").remove();
|
|
|
|
task.chain();
|
2013-04-03 18:35:25 +00:00
|
|
|
}
|
2013-04-20 00:14:20 +00:00
|
|
|
callback(error);
|
2013-04-03 18:35:25 +00:00
|
|
|
});
|
2013-04-20 00:14:20 +00:00
|
|
|
asyncRunner.addTask(task);
|
2013-04-03 18:35:25 +00:00
|
|
|
};
|
2013-03-27 20:19:12 +00:00
|
|
|
|
2013-04-15 21:15:15 +00:00
|
|
|
googleHelper.uploadBlogger = function(blogUrl, blogId, postId, title, content, callback) {
|
2013-04-20 00:14:20 +00:00
|
|
|
var task = asyncRunner.createTask();
|
|
|
|
connect(task);
|
|
|
|
authenticate(task);
|
|
|
|
task.onRun(function() {
|
|
|
|
var token = gapi.auth.getToken();
|
|
|
|
var headers = {
|
|
|
|
Authorization : token ? "Bearer " + token.access_token: null
|
|
|
|
};
|
|
|
|
function publish() {
|
|
|
|
var url = "https://www.googleapis.com/blogger/v3/blogs/" + blogId + "/posts/";
|
|
|
|
var data = {
|
|
|
|
kind: "blogger#post",
|
|
|
|
blog: { id: blogId },
|
|
|
|
title: title,
|
|
|
|
content: content
|
2013-04-10 23:13:31 +00:00
|
|
|
};
|
2013-04-20 00:14:20 +00:00
|
|
|
var type = "POST";
|
|
|
|
// If it's an update
|
|
|
|
if(postId !== undefined) {
|
|
|
|
url += postId;
|
|
|
|
data.id = postId;
|
|
|
|
type = "PUT";
|
2013-04-15 21:15:15 +00:00
|
|
|
}
|
2013-04-20 00:14:20 +00:00
|
|
|
$.ajax({
|
|
|
|
url : url,
|
|
|
|
data: JSON.stringify(data),
|
|
|
|
headers : headers,
|
|
|
|
type: type,
|
|
|
|
contentType: "application/json",
|
|
|
|
dataType : "json",
|
|
|
|
timeout : AJAX_TIMEOUT
|
|
|
|
}).done(function(post, textStatus, jqXHR) {
|
|
|
|
postId = post.id;
|
|
|
|
task.chain();
|
|
|
|
}).fail(function(jqXHR) {
|
|
|
|
var error = {
|
|
|
|
code: jqXHR.status,
|
|
|
|
message: jqXHR.statusText
|
2013-04-10 23:13:31 +00:00
|
|
|
};
|
2013-04-20 00:14:20 +00:00
|
|
|
// Handle error
|
|
|
|
if(error.code === 404 && postId !== undefined) {
|
|
|
|
error = 'Post ' + postId + ' not found on Blogger.';
|
2013-04-15 21:15:15 +00:00
|
|
|
}
|
2013-04-20 00:14:20 +00:00
|
|
|
handleError(error, task, callback);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
function getBlogId() {
|
|
|
|
if(blogId !== undefined) {
|
|
|
|
task.chain(publish);
|
2013-04-15 21:15:15 +00:00
|
|
|
}
|
2013-04-20 00:14:20 +00:00
|
|
|
$.ajax({
|
|
|
|
url : "https://www.googleapis.com/blogger/v3/blogs/byurl",
|
|
|
|
data: { url: blogUrl },
|
|
|
|
headers : headers,
|
|
|
|
dataType : "json",
|
|
|
|
timeout : AJAX_TIMEOUT
|
|
|
|
}).done(function(blog, textStatus, jqXHR) {
|
|
|
|
blogId = blog.id;
|
|
|
|
task.chain(publish);
|
|
|
|
}).fail(function(jqXHR) {
|
|
|
|
var error = {
|
|
|
|
code: jqXHR.status,
|
|
|
|
message: jqXHR.statusText
|
|
|
|
};
|
|
|
|
// Handle error
|
|
|
|
if(error.code === 404) {
|
|
|
|
error = 'Blog "' + blogUrl + '" not found on Blogger.';
|
|
|
|
}
|
|
|
|
handleError(error, task, callback);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
task.chain(getBlogId);
|
2013-04-10 23:13:31 +00:00
|
|
|
});
|
2013-04-20 00:14:20 +00:00
|
|
|
task.onSuccess = function() {
|
|
|
|
callback(undefined, blogId, postId);
|
|
|
|
};
|
|
|
|
task.onError = function(error) {
|
|
|
|
callback(error);
|
|
|
|
};
|
|
|
|
asyncRunner.addTask(task);
|
2013-04-10 23:13:31 +00:00
|
|
|
};
|
2013-04-11 22:38:41 +00:00
|
|
|
|
2013-04-20 00:14:20 +00:00
|
|
|
googleHelper.init = function(coreModule) {
|
2013-04-10 18:14:59 +00:00
|
|
|
core = coreModule;
|
2013-03-27 20:19:12 +00:00
|
|
|
};
|
2013-04-05 23:59:59 +00:00
|
|
|
|
2013-04-09 07:58:06 +00:00
|
|
|
return googleHelper;
|
2013-04-02 18:42:47 +00:00
|
|
|
});
|