From d08d05d6d75079f8f51784b90b979974404c593f Mon Sep 17 00:00:00 2001 From: coderaiser Date: Mon, 3 Dec 2012 05:21:45 -0500 Subject: [PATCH] improved work with browsers history api --- ChangeLog | 2 ++ cloudcmd.js | 2 +- lib/client.js | 48 +++++++++++++++++++++++++++-------------------- lib/client/dom.js | 24 +++++++++++++++++++++++- lib/cloudfunc.js | 16 +++++++++------- 5 files changed, 63 insertions(+), 29 deletions(-) diff --git a/ChangeLog b/ChangeLog index adbfd31b..caa2fc9b 100644 --- a/ChangeLog +++ b/ChangeLog @@ -140,6 +140,8 @@ Javascript dissabled. * client.js and server.js moved to lib directory. +* Improved work with browsers history api. + 2012.10.01, Version 0.1.7 diff --git a/cloudcmd.js b/cloudcmd.js index a5a96dd6..efe5ab2f 100644 --- a/cloudcmd.js +++ b/cloudcmd.js @@ -59,7 +59,7 @@ /* меняем title */ lData = lData.replace('Cloud Commander', - '' + CloudFunc.setTitle() + ''); + '' + CloudFunc.getTitle() + ''); if(!srv.Config.appcache) lData = Util.removeStr(lData, ' manifest="/cloudcmd.appcache"'); diff --git a/lib/client.js b/lib/client.js index 6cf7764e..342397be 100644 --- a/lib/client.js +++ b/lib/client.js @@ -3,7 +3,7 @@ * клиентский и серверный */ -var Util, DOM, CloudCommander = (function(){ +var Util, DOM, CloudFunc, CloudCommander = (function(){ "use strict"; /* Клиентский обьект, содержащий функциональную часть*/ @@ -62,7 +62,7 @@ var CloudClient = { var cloudcmd = CloudClient, /* глобальные переменные */ -CloudFunc, $, KeyBinding, + $, KeyBinding, /* short names used all the time functions */ getByClass, getById; @@ -149,7 +149,7 @@ CloudClient._loadDir = function(pLink,pNeedRefresh){ lHref = lHref.replace(lSubstr+'/',''); /* загружаем содержимое каталога */ - CloudClient._ajaxLoad(pLink, pNeedRefresh); + CloudClient._ajaxLoad(pLink, { refresh: pNeedRefresh }); /* получаем все элементы выделенной папки*/ /* при этом, если мы нажали обновить @@ -413,16 +413,18 @@ function baseInit(pCallBack){ Util.exec(lFunc); }; } - /* меняем title - * если js включен - имена папок отображать необязательно... - * а может и обязательно при переходе, можно будет это сделать - */ - var lTitle = DOM.getByTag('title'); - if(lTitle.length > 0) - lTitle[0].textContent = 'Cloud Commander'; - + /* загружаем общие функции для клиента и сервера */ DOM.jsload(cloudcmd.LIBDIR + 'cloudfunc.js',function(){ + DOM.addListener("popstate", function(pEvent) { + var lPath = pEvent.state; + + if(lPath) + cloudcmd._ajaxLoad(lPath, {nohistory: true}); + + return true; + }); + /* берём из обьекта window общий с сервером функционал */ CloudFunc = window.CloudFunc; @@ -623,16 +625,19 @@ CloudClient._changeLinks = function(pPanelID){ * Функция загружает json-данные о Файловой Системе * через ajax-запрос. * @param path - каталог для чтения - * @param pNeedRefresh - необходимость обновить данные о каталоге + * @param pOptions + * { refresh, nohistory } - необходимость обновить данные о каталоге */ -CloudClient._ajaxLoad = function(pFullPath, pNeedRefresh){ - /* Отображаем красивые пути */ - +CloudClient._ajaxLoad = function(pFullPath, pOptions){ + if(!pOptions) + pOptions = {}; + /* Отображаем красивые пути */ /* added supporting of russian language */ pFullPath = decodeURI(pFullPath); var lPath = pFullPath, - lFSPath, + lFSPath = pFullPath, + lFS_s = CloudFunc.FS, lNoJS_s = CloudFunc.NOJS; /* @@ -652,6 +657,12 @@ CloudClient._ajaxLoad = function(pFullPath, pNeedRefresh){ console.log ('reading dir: "' + lPath + '";'); + + if(!pOptions.nohistory) + DOM.setHistory(pFullPath, null, pFullPath); + + DOM.setTitle( CloudFunc.getTitle(lPath) ); + /* если доступен localStorage и * в нём есть нужная нам директория - * читаем данные с него и @@ -666,7 +677,7 @@ CloudClient._ajaxLoad = function(pFullPath, pNeedRefresh){ var lPanel = DOM.getPanel().id, lError; - if(pNeedRefresh === undefined && lPanel){ + if(!pOptions.refresh && lPanel){ var lJSON = DOM.Cache.get(lPath); if (lJSON){ @@ -682,7 +693,6 @@ CloudClient._ajaxLoad = function(pFullPath, pNeedRefresh){ CloudClient._createFileTable(lPanel, lJSON); CloudClient._changeLinks(lPanel); - DOM.setHistory(lPath, 'Cloud Commander', pFullPath); return; } @@ -716,8 +726,6 @@ CloudClient._ajaxLoad = function(pFullPath, pNeedRefresh){ */ if(lJSON_s.length < 50000 ) DOM.Cache.set(lPath, lJSON_s); - - DOM.setHistory(lPath, 'Cloud Commander', pFullPath); } }); }; diff --git a/lib/client/dom.js b/lib/client/dom.js index 0b2adae2..9f884d85 100644 --- a/lib/client/dom.js +++ b/lib/client/dom.js @@ -33,6 +33,7 @@ var CloudCommander, Util, DOM, CloudFunc; /* private members */ var XMLHTTP, + Title, LoadingImage, ErrorImage, @@ -101,7 +102,7 @@ var CloudCommander, Util, DOM, CloudFunc; DOM.addListener = function(pType, pListener, pUseCapture, pElement){ var lRet = this; - (pElement || document).addEventListener( + (pElement || window).addEventListener( pType, pListener, pUseCapture || false @@ -810,6 +811,27 @@ var CloudCommander, Util, DOM, CloudFunc; return CloudCommander.KeysPanel[pKey].onclick = pFunc; }; + /** + * set title with pName + * create title element + * if it absent + * @param pName + */ + + DOM.setTitle = function(pName){ + if(!Title) + Title = DOM.getByTag('title')[0] || + DOM.anyload({ + name:'title', + parentElement: document.head, + innerHTML: pName + }); + if(Title) + Title.textContent = pName; + + return Title; + }; + /** * current file check * diff --git a/lib/cloudfunc.js b/lib/cloudfunc.js index 6daf20e7..8c9454eb 100644 --- a/lib/cloudfunc.js +++ b/lib/cloudfunc.js @@ -44,13 +44,15 @@ var CloudFunc, exports; else return pPath; }; - /* Функция возвращает заголовок веб страницы */ - CloudFunc.setTitle = function(){ - - return CloudFunc.Path==='' ? CloudFunc.NAME: - CloudFunc.Path + - ' - ' + - CloudFunc.NAME; + /** Функция возвращает заголовок веб страницы + * @pPath + */ + CloudFunc.getTitle = function(pPath){ + if(!CloudFunc.Path) + CloudFunc.Path = '/'; + + return CloudFunc.NAME + ' - ' + (pPath || CloudFunc.Path); + }; /** * Функция переводит права из цыфрового вида в символьный