From 2f8f41b50ebeddf9d3ee2b03548c43f993d6bbbb Mon Sep 17 00:00:00 2001 From: coderaiser Date: Wed, 27 Feb 2013 04:25:32 -0500 Subject: [PATCH] added RESTfull object to DOM for easy work with CloudCmd REST API --- ChangeLog | 5 +- lib/client/dom.js | 124 +++++++++++++++++++------------ lib/client/editor/_codemirror.js | 14 +--- 3 files changed, 83 insertions(+), 60 deletions(-) diff --git a/ChangeLog b/ChangeLog index b4ed3607..903a8339 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,4 +1,4 @@ -2012.*.*, Version 0.1.9 +2012.03.01, Version 0.1.9 * Changed the way of getting github application id (now it's just from config, rest api removed). @@ -186,6 +186,9 @@ generation of json of directory listing. * Added ability to rename files. +* Added RESTfull object to DOM for easy work with +CloudCmd REST API. + 2012.12.12, Version 0.1.8 diff --git a/lib/client/dom.js b/lib/client/dom.js index fc748496..ee459de9 100644 --- a/lib/client/dom.js +++ b/lib/client/dom.js @@ -14,11 +14,8 @@ var CloudCommander, Util, XMLHTTP, Title, - /* Обьект, который содержит - * функции для отображения - * картинок - */ - Images = function (){ + /* Обьект содержит функции для отображения картинок */ + Images = function (){ var getImage = function(pName){ var lId = pName + '-image', lE = DOM.getById(lId); @@ -41,10 +38,73 @@ var CloudCommander, Util, this.error = function(){ return getImage('error'); }; + }, + + /* Обьект содержит функции для работы с CloudCmd API */ + RESTfull = function(){ + this.delete = function(pUrl, pCallBack){ + DOM.Images.showLoad(); + sendRequest({ + method : 'DELETE', + url : pUrl, + callback : pCallBack + }); + }; + + this.save = function(pUrl, pData, pCallBack){ + sendRequest({ + method : 'PUT', + url : pUrl, + data : pData, + callback : pCallBack + }); + }; + + this.mv = function(pData, pCallBack){ + sendRequest({ + method : 'PUT', + url : '/mv', + data : pData, + callback : pCallBack + }); + }; + + function sendRequest(pParams){ + var lRet = Util.checkObjTrue(pParams, ['method']); + if(lRet){ + DOM.Images.showLoad(); + CloudCommander.getConfig(function(pConfig){ + var p = pParams, lData; + + if( Util.isString(p.url) ) + p.url = decodeURI(p.url); + + if(p.data) + lData = Util.stringifyJSON(p.data); + + p.url = pConfig && pConfig.api_url + p.url, + DOM.ajax({ + method : p.method, + url : p.url, + data : lData, + error : DOM.Images.showError, + success : function(pData){ + DOM.Images.hideLoad(); + Util.log(pData); + Util.exec(p.callback); + } + }); + }); + } + + return lRet; + } }; Images = new Images(); + DOM.RESTfull = new RESTfull(); + function removeListenerFromList(pElement){ var lRet; @@ -1308,32 +1368,18 @@ var CloudCommander, Util, if( DOM.isCurrentIsDir(lCurrent) ) lUrl += '?dir'; - if(lCurrent && lParent && lName !== '..'){ - DOM.Images.showLoad(); - CloudCommander.getConfig(function(pConfig){ - lUrl = decodeURI(lUrl); - lUrl = pConfig && pConfig.api_url + lUrl; - - DOM.ajax({ - method : 'DELETE', - url : lUrl, - error : DOM.Images.showError, - success : function(pData){ - var lNext = lCurrent.nextSibling, - lPrevious = lCurrent.previousSibling; - - DOM.Images.hideLoad(); - if(lNext) - DOM.setCurrentFile(lNext); - else if(lPrevious) - DOM.setCurrentFile(lPrevious); - - lParent.removeChild(lCurrent); - Util.log(pData); - } - }); + if(lCurrent && lParent && lName !== '..') + DOM.RESTfull.delete(lUrl, function(){ + var lNext = lCurrent.nextSibling, + lPrevious = lCurrent.previousSibling; + + if(lNext) + DOM.setCurrentFile(lNext); + else if(lPrevious) + DOM.setCurrentFile(lPrevious); + + lParent.removeChild(lCurrent); }); - } return lCurrent; }; @@ -1357,22 +1403,8 @@ var CloudCommander, Util, to : lDirPath + lTo }; - DOM.Images.showLoad(); - - CloudCommander.getConfig(function(pConfig){ - var lUrl = pConfig && pConfig.api_url + '/mv'; - - DOM.ajax({ - method : 'put', - data : Util.stringifyJSON(lFiles), - url : lUrl, - error : DOM.Images.showError, - success : function(pData){ - DOM.Images.hideLoad(); - DOM.setCurrentName(lTo, lCurrent); - Util.log(pData); - } - }); + DOM.RESTfull.mv(lFiles, function(){ + DOM.setCurrentName(lTo, lCurrent); }); } }; diff --git a/lib/client/editor/_codemirror.js b/lib/client/editor/_codemirror.js index 3caae0e0..6739c6d6 100644 --- a/lib/client/editor/_codemirror.js +++ b/lib/client/editor/_codemirror.js @@ -78,19 +78,7 @@ var CloudCommander, Util, DOM, CloudFunc, CodeMirror; DOM.remove(lCSS, document.head); }, 'Ctrl-S': function(){ - CloudCmd.getConfig(function(pConfig){ - var lURL = pConfig && pConfig.api_url + DOM.getCurrentPath(); - - DOM.ajax({ - method : 'put', - url : lURL, - data : lEditor.getValue(), - error : DOM.Images.showError, - success : function(pData){ - Util.log(pData); - } - }); - }); + DOM.RESTfull.save(DOM.getCurrentPath(), lEditor.getValue()); } }, readOnly : ReadOnly