From 71919aa2062b4f4d3447caa50b93957b441ff534 Mon Sep 17 00:00:00 2001 From: coderaiser Date: Wed, 5 Dec 2012 06:32:59 -0500 Subject: [PATCH] added ability to upload file to gists on github thru menu --- ChangeLog | 2 + lib/client.js | 6 +- lib/client/editor/_codemirror.js | 17 ++--- lib/client/menu.js | 34 ++++++++- lib/client/storage/_github.js | 121 +++++++++++++++---------------- lib/util.js | 19 ++++- 6 files changed, 116 insertions(+), 83 deletions(-) diff --git a/ChangeLog b/ChangeLog index 6c2ead23..57989950 100644 --- a/ChangeLog +++ b/ChangeLog @@ -160,6 +160,8 @@ Added dropbox_id option. * Added confirmation before (not real) deleting file. +* Added ability to upload file to gists on github thru menu. + 2012.10.01, Version 0.1.7 diff --git a/lib/client.js b/lib/client.js index fd5b770c..a500906c 100644 --- a/lib/client.js +++ b/lib/client.js @@ -90,7 +90,7 @@ var loadModule = function(pParams){ var lSlash = lName.indexOf('/'); if(lSlash > 0){ var lAfterSlash = lName.substr(lSlash); - lName = lName.replace(lAfterSlash, ''); + lName = Util.removeStr(lName, lAfterSlash); } } @@ -103,11 +103,9 @@ var loadModule = function(pParams){ return DOM.jsload(cloudcmd.LIBDIRCLIENT + lPath, lFunc || function(){ - cloudcmd[lName].init(pArg); + Util.exec(cloudcmd[lName].init, pArg); }); }; - else - cloudcmd[lName + lPath]; }; CloudClient.GoogleAnalytics = function(){ diff --git a/lib/client/editor/_codemirror.js b/lib/client/editor/_codemirror.js index 8efe50a3..b209215c 100644 --- a/lib/client/editor/_codemirror.js +++ b/lib/client/editor/_codemirror.js @@ -127,7 +127,7 @@ var CloudCommander, Util, DOM, CloudFunc, CodeMirror; lA = lA.href; /* убираем адрес хоста*/ - lA = lA.replace(cloudcmd.HOST, ''); + lA = Util.removeStr(lA, cloudcmd.HOST); /* checking is this link is to directory */ var lSize = DOM.getByClass('size', lCurrentFile); @@ -138,15 +138,12 @@ var CloudCommander, Util, DOM, CloudFunc, CodeMirror; * not html data */ if (lSize === ''){ - var lIndexOfNOJS = lA.indexOf(CloudFunc.NOJS); - if (lIndexOfNOJS === CloudFunc.FS.length){ - lA = lA.replace(CloudFunc.NOJS, ''); - /* when folder view - * is no need to edit - * data - */ - ReadOnly = true; - } + lA = Util.removeStr(lA, CloudFunc.NOJS); + /* when folder view + * is no need to edit + * data + */ + ReadOnly = true; } } diff --git a/lib/client/menu.js b/lib/client/menu.js index ea38f014..0d143c26 100644 --- a/lib/client/menu.js +++ b/lib/client/menu.js @@ -67,7 +67,33 @@ var CloudCommander, Util, DOM, CloudFunc, $; items: { 'upload_to_gist': {name: 'to Gist', callback: function(key, opt){ - console.log('Upload To Gist'); + var lCurrent = DOM.getCurrentFile(), + lA = DOM.getCurrentLink(lCurrent).href, + lName = DOM.getCurrentName(lCurrent); + /* убираем адрес хоста*/ + lA = Util.removeStr(lA, cloudcmd.HOST); + lA = Util.removeStr(lA, CloudFunc.NOJS); + + DOM.ajax({ + url : lA, + error : DOM.Images.showError, + success : function(data, textStatus, jqXHR){ + if( Util.isObject(data) ) + data = JSON.stringify(data, null, 4); + + var lGitHub = cloudcmd.GitHub; + + if('init' in lGitHub) + lGitHub.createGist(data, lName); + else + Util.exec(cloudcmd.GitHub, + function(){ + lGitHub.createGist(data, lName); + }); + } + }); + + Util.log('Uploading to gist...'); } }, }, @@ -159,9 +185,9 @@ var CloudCommander, Util, DOM, CloudFunc, $; .replace('z-index: 1', 'z-index:-1'); /* get element by point */ - var lElement = document.elementFromPoint(pEvent.x, pEvent.y); - var lTag = lElement.tagName; - var lParent; + var lElement = document.elementFromPoint(pEvent.x, pEvent.y), + lTag = lElement.tagName, + lParent; if(lTag === 'A' || lTag === 'SPAN'){ if (lElement.tagName === 'A') diff --git a/lib/client/storage/_github.js b/lib/client/storage/_github.js index a043ec41..5c3bf5b8 100644 --- a/lib/client/storage/_github.js +++ b/lib/client/storage/_github.js @@ -27,7 +27,7 @@ var CloudCommander, Util, DOM, $, Github, cb; function load(pCallBack){ console.time('github load'); - var lDir = './lib/client/storage/github/', + var lDir = '/lib/client/storage/github/', lFiles = [ lDir + 'github.js', lDir + 'lib/base64.js', @@ -53,35 +53,8 @@ var CloudCommander, Util, DOM, $, Github, cb; }); } - function saveToken(pToken){ - return Cache.set('token', pToken); - } - - function getToken(){ - return Cache.get('token'); - } - - - /* PUBLICK 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 init(pCallBack){ - var lToken = getToken(); + function init(pCallBack){ + var lToken = Cache.get('token'); if(lToken){ GithubStore.Login(lToken); Util.exec(pCallBack); @@ -100,7 +73,7 @@ var CloudCommander, Util, DOM, $, Github, cb; lToken = pData.token; GithubStore.Login(lToken); - saveToken(lToken); + Cache.set('token', lToken); Util.exec(pCallBack); } else @@ -116,58 +89,80 @@ var CloudCommander, Util, DOM, $, Github, cb; } } - function getUserData(){ - var lName, lRepoNames, - lGetTree = function(pError ,pData){ - Util.log('Tree of ripository ' + lRepoNames[0].name + ': '); - var lTree = pData || []; - if(!pError) - for(var i = 0, n = lTree.length; i < n ; i++) - if( !Util.isContainStr(lTree[i].path, '/') ) - console.log(lTree[i].path); - else - Util.log(pError); - }, - - lShowRepos = function(pError, pRepos){ - lRepoNames = pRepos || []; - Util.log('Repositories: '); - if(!pError){ - for(var i = 0, n = pRepos.length; i < n ; i++) - console.log(pRepos[i].name); - - var lRepo = GithubLocal.getRepo(lName, pRepos[0].name); - lRepo.getTree('master?recursive=true', lGetTree); - } - else - DOM.Cache.remove('token'); - }, + function getUserData(pCallBack){ + var lName, lShowUserInfo = function(pError, pData){ if(!pError){ lName = pData.name; console.log('Hello ' + lName + ' :)!'); - User.repos(lShowRepos); } else DOM.Cache.remove('token'); }; - - + User.show(null, lShowUserInfo); + + Util.exec(pCallBack); } - cloudcmd.GitHub.init = function(){ + /* PUBLICK 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 + */ + cloudcmd.GitHub.createGist = function(pContent, pFileName){ + if(pContent){ + if(!pFileName) + pFileName = Util.getDate(); + + var lGist = GithubLocal.getGist(), + lFiles = {}, + lOptions = { + description: "uploaded from cloudcmd", + public: true + }; + + lFiles[pFileName] ={ + content: pContent + }; + + lOptions.files = lFiles; + + lGist.create(lOptions, cb); + } + + return pContent; + }; + + cloudcmd.GitHub.init = function(pCallBack){ Util.loadOnLoad([ + Util.retExec(pCallBack), getUserData, init, setConfig, load ]); - this.init = null; + cloudcmd.GitHub.init = null; }; - cloudcmd.Github = GithubStore; + cloudcmd.Github = GithubStore; })(); diff --git a/lib/util.js b/lib/util.js index 4c2d3372..9143bc54 100644 --- a/lib/util.js +++ b/lib/util.js @@ -363,7 +363,8 @@ var Util, exports; * Gets current time in format hh:mm:ss */ Util.getTime = function(){ - var date = new Date(), + var lRet, + date = new Date(), hours = date.getHours(), minutes = date.getMinutes(), seconds = date.getSeconds(); @@ -371,7 +372,21 @@ var Util, exports; minutes = minutes < 10 ? '0' + minutes : minutes; seconds = seconds < 10 ? '0' + seconds : seconds; - return hours + ":" + minutes + ":" + seconds; + lRet = hours + ":" + minutes + ":" + seconds; + + return lRet; + }; + /** + * Gets current date in format yy.mm.dd hh:mm:ss + */ + Util.getDate = function(){ + var date = new Date(), + day = date.getDate(), + month = date.getMonth() + 1, + year = date.getFullYear(), + lRet = year + "-" + month + "-" + day + " " + Util.getTime(); + + return lRet; }; })(); \ No newline at end of file