Secondary Google account (part2)

This commit is contained in:
benweet 2013-12-16 00:31:40 +00:00
parent 4b0d5306e9
commit 31fea5be52
7 changed files with 36 additions and 98 deletions

View File

@ -43,8 +43,7 @@ define([], function() {
constants.GATEKEEPER_URL = "https://stackedit-io-gatekeeper.herokuapp.com/";
constants.TUMBLR_PROXY_URL = "https://stackedit-io-tumblr-proxy.herokuapp.com/";
}
if(location.hostname.indexOf("benweet.github.io") === 0) {
else if(location.hostname.indexOf("benweet.github.io") === 0) {
constants.BASE_URL = 'http://benweet.github.io/stackedit/';
constants.GOOGLE_CLIENT_ID = '241271498917-jpto9lls9fqnem1e4h6ppds9uob8rpvu.apps.googleusercontent.com';
constants.GITHUB_CLIENT_ID = 'fa0d09514da8377ee32e';
@ -53,8 +52,7 @@ define([], function() {
constants.WORDPRESS_CLIENT_ID = '3185';
constants.WORDPRESS_PROXY_URL = "https://stackedit-wordpress-proxy.herokuapp.com/";
}
if(location.hostname.indexOf("benweet.insomnia247.nl") === 0) {
else if(location.hostname.indexOf("benweet.insomnia247.nl") === 0) {
constants.BASE_URL = "http://benweet.insomnia247.nl/stackedit/";
constants.GOOGLE_CLIENT_ID = '241271498917-52hae7a08hv7ltenv7km8h7lghno9sk3.apps.googleusercontent.com';
constants.GITHUB_CLIENT_ID = 'd2943d6074b2d9c4a830';

View File

@ -13,7 +13,7 @@ define([
var fileDesc;
var $fileTitleNavbar;
var updateTitle = function(fileDescParameter) {
var updateTitle = _.debounce(function(fileDescParameter) {
if(fileDescParameter !== fileDesc) {
return;
}
@ -24,11 +24,8 @@ define([
$(".file-title").text(title);
$(".input-file-title").val(title);
if(layout !== undefined) {
// Use defer to make sure UI has been updated
_.defer(layout.resizeAll);
}
};
layout && layout.resizeAll();
}, 50);
documentTitle.onFileSelected = function(fileDescParameter) {
fileDesc = fileDescParameter;
@ -40,6 +37,7 @@ define([
documentTitle.onSyncRemoved = updateTitle;
documentTitle.onNewPublishSuccess = updateTitle;
documentTitle.onPublishRemoved = updateTitle;
documentTitle.onReady = updateTitle;
documentTitle.onReady = function() {
$fileTitleNavbar = $(".file-title-navbar");

View File

@ -16,20 +16,20 @@ define([
var authorizationMgrMap = {};
function AuthorizationMgr(accountId) {
var permissionList = {};
var isAuthorized = false;
var refreshFlag = true;
_.each((storage[accountId + '.permissions'] || '').split(';'), function(permission) {
permission && (permissionList[permission] = true);
});
this.reset = function() {
isAuthorized = false;
this.setRefreshFlag = function() {
refreshFlag = true;
};
this.isAuthorized = function(permission) {
return isAuthorized && _.has(permissionList, permission);
return refreshFlag === false && _.has(permissionList, permission);
};
this.add = function(permission) {
permissionList[permission] = true;
storage[accountId + '.permissions'] = _.keys(permissionList).join(';');
isAuthorized = true;
refreshFlag = false;
};
this.getListWithNew = function(permission) {
var result = _.keys(permissionList);
@ -103,7 +103,7 @@ define([
'https://picasaweb.google.com/data/'
]
};
function authenticate(task, permission, accountId, refresh) {
function authenticate(task, permission, accountId) {
accountId = accountId || 'google.0';
var authorizationMgr = authorizationMgrMap[accountId];
if(!authorizationMgr) {
@ -116,7 +116,7 @@ define([
if(token.access_token) {
immediate = true;
gapi.auth.setToken(token);
if(!refresh && authorizationMgr.isAuthorized(permission)) {
if(authorizationMgr.isAuthorized(permission)) {
task.chain();
return;
}
@ -138,9 +138,10 @@ define([
}
var scopeList = _.chain(scopeMap).pick(authorizationMgr.getListWithNew(permission)).flatten().value();
gapi.auth.authorize({
'client_id': constants.GOOGLE_CLIENT_ID,
'scope': scopeList,
'immediate': immediate
client_id: constants.GOOGLE_CLIENT_ID,
scope: scopeList,
immediate: immediate,
authuser: immediate ? undefined : ''
}, function(authResult) {
gapi.client.load('drive', 'v2', function() {
if(!authResult || authResult.error) {
@ -157,7 +158,7 @@ define([
}
// Success
authorizationMgr.add(permission);
authorizationMgr.saveToken();
immediate === false && authorizationMgr.saveToken();
task.chain();
});
});
@ -168,7 +169,9 @@ define([
googleHelper.refreshGdriveToken = function(accountId) {
var task = new AsyncTask();
connect(task);
authenticate(task, 'gdrive', accountId, true);
var authorizationMgr = authorizationMgrMap[accountId];
authorizationMgr && authorizationMgr.setRefreshFlag();
authenticate(task, 'gdrive', accountId);
task.enqueue();
};
@ -626,7 +629,7 @@ define([
}
else if(error.code === 401 || error.code === 403 || error.code == "token_refresh_required") {
_.each(authorizationMgrMap, function(authorizationMgr) {
authorizationMgr.reset();
authorizationMgr.setRefreshFlag();
});
errorMsg = "Access to Google account is not authorized.";
task.retry(new Error(errorMsg), 1);
@ -635,7 +638,7 @@ define([
else if(error.code === 0 || error.code === -1) {
connected = false;
_.each(authorizationMgrMap, function(authorizationMgr) {
authorizationMgr.reset();
authorizationMgr.setRefreshFlag();
});
core.setOffline();
errorMsg = "|stopPublish";
@ -709,7 +712,7 @@ define([
pickerBuilder.enableFeature(google.picker.Feature.NAV_HIDDEN);
pickerBuilder.enableFeature(google.picker.Feature.MULTISELECT_ENABLED);
pickerBuilder.addView(view);
pickerBuilder.setOAuthToken(gapi.auth.getToken());
pickerBuilder.setOAuthToken(gapi.auth.getToken().access_token);
}
else if(pickerType == 'folder') {
view = new google.picker.DocsView(google.picker.ViewId.FOLDERS);
@ -718,7 +721,7 @@ define([
view.setMimeTypes('application/vnd.google-apps.folder');
pickerBuilder.enableFeature(google.picker.Feature.NAV_HIDDEN);
pickerBuilder.addView(view);
pickerBuilder.setOAuthToken(gapi.auth.getToken());
pickerBuilder.setOAuthToken(gapi.auth.getToken().access_token);
}
else if(pickerType == 'img') {
view = new google.picker.PhotosView();

View File

@ -117,6 +117,11 @@
Google Drive</a></li>
<li><a href="#" class="action-sync-export-dialog-gdrive">Export
to Google Drive</a></li>
<li><a href="#" class="action-sync-import-gdrivesec"
data-toggle="collapse" data-target=".menu-panel">Import from
Google Drive (secondary)</a></li>
<li><a href="#" class="action-sync-export-dialog-gdrivesec">Export
to Google Drive (secondary)</a></li>
</ul>
</div>
<a href="#" data-toggle="collapse"
@ -459,76 +464,8 @@
<div class="modal modal-upload-gdrive">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal"
aria-hidden="true">&times;</button>
<h3 class="modal-title">Export to Google Drive</h3>
</div>
<div class="modal-body">
<p>
This will save "<span class="file-title"></span>" to your <i
class="icon-provider-gdrive"></i>
<code>Google Drive</code>
account and keep it synchronized.
</p>
<div class="collapse form-horizontal sync-export-gdrive-options-collapse">
<div class="form-group">
<label class="col-lg-3 control-label"
for="input-sync-export-gdrive-parentid">Folder ID
(optional)</label>
<div class="col-lg-8">
<div class="input-group">
<input type="text" id="input-sync-export-gdrive-parentid"
placeholder="FolderID" class="form-control">
<div class="input-group-btn">
<a class="btn btn-link export-gdrive-choose-folder"
title="Choose folder" data-dismiss="modal"><i
class="icon-folder-open"></i></a>
</div>
</div>
<span class="help-block"> If no folder ID is supplied, the
file will be created in your root folder. </span>
</div>
</div>
<div class="form-group">
<div class="col-lg-3 control-label"></div>
<div class="col-lg-8">
<label> <input id="input-sync-export-gdrive-realtime"
type="checkbox"> Create a real time collaborative
document
</label>
<span class="help-block"> Collaborative documents cannot be open outside
StackEdit nor have multiple synchronized locations. </span>
</div>
</div>
<div class="form-group">
<label class="col-lg-3 control-label"
for="input-sync-export-gdrive-fileid">Existing file ID
(optional)</label>
<div class="col-lg-8">
<input type="text" id="input-sync-export-gdrive-fileid"
placeholder="FileID" class="form-control"> <span
class="help-block"> This will overwrite the existing file
on the server. </span>
</div>
</div>
</div>
<p class="align-right"><a href="#" data-toggle="collapse" data-target=".sync-export-gdrive-options-collapse">Options...</a></p>
<blockquote>
<b>Tip:</b> You can move or rename the file afterwards within Google
Drive.
</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-gdrive">OK</a>
</div>
</div>
</div>
</div>
<div class="modal modal-upload-gdrivesec">
</div>

View File

@ -16,6 +16,7 @@ define([
"providers/gistProvider",
"providers/githubProvider",
"providers/gdriveProvider",
"providers/gdrivesecProvider",
"providers/sshProvider",
"providers/tumblrProvider",
"providers/wordpressProvider"

View File

@ -325,7 +325,7 @@ kbd {
background-position: 0 0;
}
.icon-provider-gdrive {
.icon-provider-gdrive, .icon-provider-gdrivesec {
background-position: -18px 0;
&.realtime {
background-position: -162px 0;

View File

@ -8,7 +8,8 @@ define([
"fileMgr",
"classes/Provider",
"providers/dropboxProvider",
"providers/gdriveProvider"
"providers/gdriveProvider",
"providers/gdrivesecProvider"
], function($, _, utils, storage, eventMgr, fileSystem, fileMgr, Provider) {
var synchronizer = {};