mirror of
https://github.com/coderaiser/cloudcmd.git
synced 2026-07-17 16:38:18 +00:00
refactor(github) add GitHubProto
This commit is contained in:
parent
c46928d4df
commit
98f2d7a28b
3 changed files with 158 additions and 149 deletions
|
|
@ -6,9 +6,11 @@
|
|||
(function(){
|
||||
'use strict';
|
||||
|
||||
if(window.opener){
|
||||
var lGitHub = window.opener.CloudCommander.GitHub;
|
||||
lGitHub.autorize(lGitHub.callback, window.location.search);
|
||||
var opener = window.opener;
|
||||
|
||||
if (opener){
|
||||
var GitHub = opener.CloudCmd.GitHub;
|
||||
GitHub.autorize(GitHub.callback, window.location.search);
|
||||
window.close();
|
||||
}
|
||||
})();
|
||||
|
|
|
|||
|
|
@ -3,18 +3,19 @@
|
|||
<head></head>
|
||||
<body>
|
||||
<script>
|
||||
(function(){
|
||||
(function() {
|
||||
"use strict";
|
||||
|
||||
var lOpener = window.opener;
|
||||
if(lOpener){
|
||||
var CloudCmd = lOpener.CloudCmd;
|
||||
var opener = window.opener;
|
||||
|
||||
if (opener) {
|
||||
var CloudCmd = opener.CloudCmd;
|
||||
|
||||
CloudCmd.getModules(function(pModules){
|
||||
var Util = lOpener.Util,
|
||||
lStorage = Util.findObjByNameInArr(pModules, 'storage'),
|
||||
lGitHub = Util.findObjByNameInArr(lStorage, 'GitHub'),
|
||||
GitHub_ID = lGitHub && lGitHub.key;
|
||||
CloudCmd.getModules(function(pModules) {
|
||||
var Util = opener.Util,
|
||||
Storage = Util.findObjByNameInArr(pModules, 'storage'),
|
||||
GitHub = Util.findObjByNameInArr(Storage, 'GitHub'),
|
||||
GitHub_ID = GitHub && GitHub.key;
|
||||
|
||||
window.location =
|
||||
'https://github.com/login/oauth/authorize?client_id=' +
|
||||
|
|
|
|||
|
|
@ -1,159 +1,165 @@
|
|||
var CloudCmd, Util, DOM, $, Github, cb;
|
||||
/* module for work with github */
|
||||
|
||||
(function(CloudCmd, Util, DOM){
|
||||
(function(CloudCmd, Util, DOM) {
|
||||
'use strict';
|
||||
|
||||
var Storage = DOM.Storage,
|
||||
GithubLocal,
|
||||
User,
|
||||
GitHubStore = {};
|
||||
CloudCmd.GitHub = GitHubProto;
|
||||
|
||||
/* temporary callback function for work with github */
|
||||
cb = function (err, data){ Util.log(err || data);};
|
||||
|
||||
/* PRIVATE FUNCTIONS */
|
||||
|
||||
/**
|
||||
* function loads github.js
|
||||
*/
|
||||
function load(pCallBack){
|
||||
console.time('github load');
|
||||
|
||||
var lDir = '/lib/client/storage/github/',
|
||||
lFiles = [
|
||||
lDir + 'github.js',
|
||||
lDir + 'lib/base64.js',
|
||||
lDir + 'lib/underscore.js'
|
||||
];
|
||||
|
||||
DOM.anyLoadInParallel(lFiles, function(){
|
||||
console.timeEnd('github load');
|
||||
DOM.Images.hideLoad();
|
||||
function GitHubProto(callback) {
|
||||
var GitHub = this,
|
||||
Storage = DOM.Storage,
|
||||
|
||||
Util.exec(pCallBack);
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
GitHubStore.autorize = function(pCallBack, pCode){
|
||||
var lToken = Storage.get('token');
|
||||
if(lToken){
|
||||
GitHubStore.Login(lToken);
|
||||
Util.exec(pCallBack);
|
||||
}
|
||||
else{
|
||||
var lCode = pCode || window.location.search;
|
||||
if (lCode || Util.isContainStr(lCode, '?code=') )
|
||||
CloudCmd.getConfig(function(pConfig){
|
||||
DOM.ajax({
|
||||
type : 'put',
|
||||
url : pConfig && pConfig.api_url + '/auth',
|
||||
data : Util.removeStr(lCode, '?code='),
|
||||
success : function(pData){
|
||||
if(pData && pData.token){
|
||||
lToken = pData.token;
|
||||
|
||||
GitHubStore.Login(lToken);
|
||||
Storage.set('token', lToken);
|
||||
Util.exec(pCallBack);
|
||||
}
|
||||
else
|
||||
Util.log('Worning: token not getted...');
|
||||
}
|
||||
});
|
||||
});
|
||||
else{
|
||||
var lUrl = '//' + window.location.host + '/auth/github';
|
||||
DOM.openWindow(lUrl);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
GitHubStore.getUserData = function(pCallBack){
|
||||
User.show(null, function(pError, pData){
|
||||
if(!pError){
|
||||
var lName = pData.name;
|
||||
Util.log('Hello ' + lName + ' :)!');
|
||||
}
|
||||
else
|
||||
DOM.Storage.remove('token');
|
||||
});
|
||||
GithubLocal,
|
||||
User,
|
||||
GitHubStore = {};
|
||||
|
||||
Util.exec(pCallBack);
|
||||
};
|
||||
|
||||
/* PUBLIC FUNCTIONS */
|
||||
GitHubStore.basicLogin = function(pUser, pPasswd){
|
||||
GithubLocal = new Github({
|
||||
username: pUser,
|
||||
password: pPasswd,
|
||||
auth : 'basic'
|
||||
});
|
||||
};
|
||||
|
||||
GitHubStore.Login = function(pToken){
|
||||
Github = GithubLocal = new Github({
|
||||
token : pToken,
|
||||
auth : 'oauth'
|
||||
});
|
||||
cb = function (err, data) { Util.log(err || data);};
|
||||
|
||||
User = GithubLocal.getUser();
|
||||
};
|
||||
|
||||
/**
|
||||
* function creates gist
|
||||
*/
|
||||
GitHubStore.uploadFile = function(pParams, pCallBack){
|
||||
var lContent = pParams.data,
|
||||
lName = pParams.name;
|
||||
|
||||
if(lContent){
|
||||
DOM.Images.showLoad();
|
||||
if(!lName)
|
||||
lName = Util.getDate();
|
||||
function init(pCallBack) {
|
||||
Util.loadOnLoad([
|
||||
load,
|
||||
GitHubStore.autorize,
|
||||
GitHubStore.getUserData,
|
||||
Util.retExec(pCallBack)
|
||||
]);
|
||||
|
||||
var lGist = GithubLocal.getGist(),
|
||||
lFiles = {},
|
||||
lHost = CloudCmd.HOST,
|
||||
lOptions = {
|
||||
description: 'Uplouded by Cloud Commander from ' + lHost,
|
||||
public: true
|
||||
};
|
||||
|
||||
lFiles[lName] ={
|
||||
content: lContent
|
||||
GitHubStore.callback = function() {
|
||||
Util.loadOnLoad([
|
||||
GitHubStore.getUserData,
|
||||
Util.retExec(pCallBack)
|
||||
]);
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* function loads github.js
|
||||
*/
|
||||
function load(pCallBack) {
|
||||
console.time('github load');
|
||||
|
||||
lOptions.files = lFiles;
|
||||
var lDir = '/lib/client/storage/github/',
|
||||
lFiles = [
|
||||
lDir + 'github.js',
|
||||
lDir + 'lib/base64.js',
|
||||
lDir + 'lib/underscore.js'
|
||||
];
|
||||
|
||||
lGist.create(lOptions, function(pError, pData){
|
||||
DOM.anyLoadInParallel(lFiles, function() {
|
||||
console.timeEnd('github load');
|
||||
DOM.Images.hideLoad();
|
||||
Util.log(pError || pData);
|
||||
Util.log(pData && pData.html_url);
|
||||
|
||||
Util.exec(pCallBack);
|
||||
});
|
||||
}
|
||||
|
||||
return lContent;
|
||||
};
|
||||
|
||||
GitHubStore.init = function(pCallBack){
|
||||
Util.loadOnLoad([
|
||||
load,
|
||||
GitHubStore.autorize,
|
||||
GitHubStore.getUserData,
|
||||
Util.retExec(pCallBack)
|
||||
]);
|
||||
|
||||
GitHubStore.callback = function(){
|
||||
Util.loadOnLoad([
|
||||
GitHubStore.getUserData,
|
||||
Util.retExec(pCallBack)
|
||||
]);
|
||||
GitHubStore.autorize = function(pCallBack, pCode) {
|
||||
var lCode, lToken = Storage.get('token');
|
||||
|
||||
if (lToken) {
|
||||
GitHubStore.Login(lToken);
|
||||
Util.exec(pCallBack);
|
||||
}
|
||||
else {
|
||||
lCode = pCode || window.location.search;
|
||||
|
||||
if (lCode || Util.isContainStr(lCode, '?code=') )
|
||||
CloudCmd.getConfig(function(pConfig) {
|
||||
DOM.ajax({
|
||||
type : 'put',
|
||||
url : pConfig && pConfig.api_url + '/auth',
|
||||
data : Util.removeStr(lCode, '?code='),
|
||||
success : function(pData) {
|
||||
if (pData && pData.token) {
|
||||
lToken = pData.token;
|
||||
|
||||
GitHubStore.Login(lToken);
|
||||
Storage.set('token', lToken);
|
||||
Util.exec(pCallBack);
|
||||
}
|
||||
else
|
||||
Util.log('Worning: token not getted...');
|
||||
}
|
||||
});
|
||||
});
|
||||
else{
|
||||
var lUrl = '//' + window.location.host + '/auth/github';
|
||||
DOM.openWindow(lUrl);
|
||||
}
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
CloudCmd.GitHub = GitHubStore;
|
||||
|
||||
GitHubStore.getUserData = function(pCallBack) {
|
||||
User.show(null, function(pError, pData) {
|
||||
if (!pError) {
|
||||
var lName = pData.name;
|
||||
Util.log('Hello ' + lName + ' :)!');
|
||||
}
|
||||
else
|
||||
DOM.Storage.remove('token');
|
||||
});
|
||||
|
||||
Util.exec(pCallBack);
|
||||
};
|
||||
|
||||
/* PUBLIC FUNCTIONS */
|
||||
GitHubStore.basicLogin = function(pUser, pPasswd) {
|
||||
GithubLocal = new Github({
|
||||
username: pUser,
|
||||
password: pPasswd,
|
||||
auth : 'basic'
|
||||
});
|
||||
};
|
||||
|
||||
GitHubStore.Login = function(pToken) {
|
||||
Github = GithubLocal = new Github({
|
||||
token : pToken,
|
||||
auth : 'oauth'
|
||||
});
|
||||
|
||||
User = GithubLocal.getUser();
|
||||
};
|
||||
|
||||
/**
|
||||
* function creates gist
|
||||
*/
|
||||
GitHubStore.uploadFile = function(pParams, pCallBack) {
|
||||
var lContent = pParams.data,
|
||||
lName = pParams.name;
|
||||
|
||||
if (lContent) {
|
||||
DOM.Images.showLoad();
|
||||
if (!lName)
|
||||
lName = Util.getDate();
|
||||
|
||||
var lGist = GithubLocal.getGist(),
|
||||
lFiles = {},
|
||||
lHost = CloudCmd.HOST,
|
||||
lOptions = {
|
||||
description: 'Uplouded by Cloud Commander from ' + lHost,
|
||||
public: true
|
||||
};
|
||||
|
||||
lFiles[lName] ={
|
||||
content: lContent
|
||||
};
|
||||
|
||||
lOptions.files = lFiles;
|
||||
|
||||
lGist.create(lOptions, function(pError, pData) {
|
||||
DOM.Images.hideLoad();
|
||||
Util.log(pError || pData);
|
||||
Util.log(pData && pData.html_url);
|
||||
|
||||
Util.exec(pCallBack);
|
||||
});
|
||||
}
|
||||
|
||||
return lContent;
|
||||
};
|
||||
|
||||
init(callback);
|
||||
}
|
||||
})(CloudCmd, Util, DOM);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue