From 90174366f7d215d46bef84398fdbf4996a2b1908 Mon Sep 17 00:00:00 2001 From: "Alex A. Naanou" Date: Sat, 8 Jun 2013 18:28:10 +0400 Subject: [PATCH] some cleanup and refactoring + started work on history... Signed-off-by: Alex A. Naanou --- ui/Makefile | 4 ++ ui/base.js | 7 +--- ui/data.js | 97 ++++++++++++++++++++-------------------------- ui/files.js | 60 ++++++++++++++++++++++++++-- ui/index.html | 1 + ui/localstorage.js | 24 +++++++++++- ui/setup.js | 5 +++ 7 files changed, 132 insertions(+), 66 deletions(-) diff --git a/ui/Makefile b/ui/Makefile index 9e5e5a3e..33f9bba6 100755 --- a/ui/Makefile +++ b/ui/Makefile @@ -60,6 +60,10 @@ dev: css unzip -uj $(wildcard targets/node-webkit/node-webkit-*-win-ia32.zip) -d . chmod +x *.{exe,dll} +dev-targets: + mkdir -p targets/node-webkit + wget + # build targets... diff --git a/ui/base.js b/ui/base.js index ab367320..b83752f1 100755 --- a/ui/base.js +++ b/ui/base.js @@ -1025,6 +1025,8 @@ function zoomOut(){ /************************************************** Editor Actions ***/ +// NOTE: for shiftImageRight/shiftImageLeft see data.js, as they depend +// on data ordering... function shiftImageTo(image, direction, moving, force_create_ribbon, mode){ if(image == null){ @@ -1061,11 +1063,6 @@ function shiftImageDownNewRibbon(image, moving){ } -// TODO manual image ordering (shiftLeft/shiftRight functions) -// XXX - - - /********************************************************************** * vim:set sw=4 ts=4 : */ diff --git a/ui/data.js b/ui/data.js index 20e67bdb..f51cec87 100755 --- a/ui/data.js +++ b/ui/data.js @@ -113,6 +113,8 @@ var SETTINGS = { } var BASE_URL = '.' +var BASE_URL_HISTORY = [] +var BASE_URL_LIMIT = 10 var IMAGE_CACHE = [] @@ -394,6 +396,19 @@ Array.prototype.binSearch = function(target, cmp, get){ } +// Base URL interface... +// +// NOTE: changing a base URL will trigger a baseURLChanged event... +function setBaseURL(url){ + var old_url = BASE_URL + BASE_URL = url + $('.viewer').trigger('baseURLChanged', [old_url, url]) +} +function getBaseURL(){ + return BASE_URL +} + + // Normalize the path... // // This will: @@ -411,7 +426,7 @@ Array.prototype.binSearch = function(target, cmp, get){ // NOTE: mode can be either 'absolute' (default) or 'relative'... function normalizePath(url, base, mode){ mode = mode == null ? 'absolute' : mode - base = base == null ? BASE_URL : base + base = base == null ? getBaseURL() : base // windows path... // - replace all '\\' with '/'... @@ -624,60 +639,6 @@ function dataFromImages(images){ } -// Construct a ribbons hierarchy from the fav dirs structure -// -// NOTE: this depends on listDir(...) -// NOTE: this assumes that images contain ALL the images... -function ribbonsFromFavDirs(path, images, cmp){ - path = path == null ? BASE_URL : path - images = images == null ? IMAGES : images - - // build a reverse name-gid index for fast access... - var index = {} - var name - for(var gid in images){ - name = images[gid].path.split('/').pop() - // XXX we assume that names are unique... - index[name] = gid - } - - var ribbons = [] - // add the base row... - var base = Object.keys(images) - ribbons.push(base) - - var files = listDir(path) - var cur_path = path - while(files.indexOf('fav') >= 0){ - cur_path += '/fav' - files = listDir(cur_path) - ribbon = [] - // collect the images... - $.each(files, function(i, e){ - var _gid = index[e] - // filter out non-image files... - if(/.*\.(jpg|jpeg)$/i.test(e)){ - ribbon.push(_gid) - } - // remove the found item from each of the below ribbons... - $.each(ribbons, function(i ,e){ - if(e.indexOf(_gid) != -1){ - e.splice(e.indexOf(_gid), 1) - } - }) - }) - ribbons.push(ribbon) - } - - // remove empty ribbons and sort the rest... - ribbons = $.map(ribbons, function(e){ - return e.length > 0 ? [cmp == null ? e : e.sort(cmp)] : null - }) - - return ribbons.reverse() -} - - /********************************************************************** * Format conversion @@ -1092,6 +1053,30 @@ function preCacheAllRibbons(){ +/********************************************************************** +* URL history... +*/ + +function setupBaseURLHistory(){ + $('.viewer') + .on('baseURLChanged', function(evt, old_url, new_url){ + BASE_URL_HISTORY.splice(0, 0, old_url) + + // truncate the history if needed... + if(BASE_URL_HISTORY.length > BASE_URL_LIMIT){ + BASE_URL_HISTORY.splice(BASE_URL_LIMIT, BASE_URL_HISTORY.length) + } + }) +} + +// XXX... +function getNextLocation(){ +} +function getPrevLocation(){ +} + + + /********************************************************************** * Actions... */ @@ -1107,7 +1092,7 @@ function openImage(){ return } // XXX if path is not present try and open the biggest preview... - return runSystem(normalizePath(IMAGES[getImageGID()].path, BASE_URL)) + return runSystem(normalizePath(IMAGES[getImageGID()].path, getBaseURL())) } diff --git a/ui/files.js b/ui/files.js index 07fb10c1..f3d7a481 100755 --- a/ui/files.js +++ b/ui/files.js @@ -167,6 +167,60 @@ function loadLatestFile(path, dfl, pattern, diff_pattern){ } +// Construct a ribbons hierarchy from the fav dirs structure +// +// NOTE: this depends on listDir(...) +// NOTE: this assumes that images contain ALL the images... +function ribbonsFromFavDirs(path, images, cmp){ + path = path == null ? getBaseURL() : path + images = images == null ? IMAGES : images + + // build a reverse name-gid index for fast access... + var index = {} + var name + for(var gid in images){ + name = images[gid].path.split('/').pop() + // XXX we assume that names are unique... + index[name] = gid + } + + var ribbons = [] + // add the base row... + var base = Object.keys(images) + ribbons.push(base) + + var files = listDir(path) + var cur_path = path + while(files.indexOf('fav') >= 0){ + cur_path += '/fav' + files = listDir(cur_path) + ribbon = [] + // collect the images... + $.each(files, function(i, e){ + var _gid = index[e] + // filter out non-image files... + if(/.*\.(jpg|jpeg)$/i.test(e)){ + ribbon.push(_gid) + } + // remove the found item from each of the below ribbons... + $.each(ribbons, function(i ,e){ + if(e.indexOf(_gid) != -1){ + e.splice(e.indexOf(_gid), 1) + } + }) + }) + ribbons.push(ribbon) + } + + // remove empty ribbons and sort the rest... + ribbons = $.map(ribbons, function(e){ + return e.length > 0 ? [cmp == null ? e : e.sort(cmp)] : null + }) + + return ribbons.reverse() +} + + /*********************************************************************/ @@ -307,7 +361,7 @@ function loadFileState(path, prefix){ DATA_FILE_DEFAULT, DATA_FILE_PATTERN), res, true) .done(function(json){ - BASE_URL = base + setBaseURL(base) // legacy format... if(json.version == null){ @@ -325,7 +379,7 @@ function loadFileState(path, prefix){ // XXX load config... // load images... bubbleProgress(prefix, - loadFileImages(DATA.image_file == null ? + loadFileImages(DATA.image_file != null ? normalizePath(DATA.image_file, base) : null), res, true), // load marks if available... @@ -405,7 +459,7 @@ function loadRawDir(path, prefix){ return res.reject() } - BASE_URL = path + setBaseURL(path) IMAGES = imagesFromUrls(image_paths) res.notify(prefix, 'Loaded', 'Images.') diff --git a/ui/index.html b/ui/index.html index c1add00b..172706d1 100755 --- a/ui/index.html +++ b/ui/index.html @@ -169,6 +169,7 @@ $(function(){ centerView() }) + setupBaseURLHistory() setupDataBindings() diff --git a/ui/localstorage.js b/ui/localstorage.js index 39316609..acbcb170 100755 --- a/ui/localstorage.js +++ b/ui/localstorage.js @@ -13,6 +13,26 @@ * XXX should we use jStorage here? */ +function loadLocalStorageBaseURL(attr){ + attr = attr == null ? DATA_ATTR : attr + setBaseURL(localStorage[attr + '_BASE_URL']) +} +function saveLocalStorageBaseURL(attr){ + attr = attr == null ? DATA_ATTR : attr + localStorage[attr + '_BASE_URL'] = getBaseURL() +} + + +function loadLocalStorageBaseURLHistory(attr){ + attr = attr == null ? DATA_ATTR : attr + BASE_URL_HISTORY = JSON.parse(localStorage[attr + '_BASE_URL_HISTORY']) +} +function saveLocalStorageBaseURLHistory(attr){ + attr = attr == null ? DATA_ATTR : attr + localStorage[attr + '_BASE_URL_HISTORY'] = JSON.stringify(BASE_URL_HISTORY) +} + + function loadLocalStorageData(attr){ attr = attr == null ? DATA_ATTR : attr var data = localStorage[attr] @@ -29,7 +49,7 @@ function loadLocalStorageData(attr){ function saveLocalStorageData(attr){ attr = attr == null ? DATA_ATTR : attr localStorage[attr] = JSON.stringify(DATA) - localStorage[attr + '_BASE_URL'] = BASE_URL + saveLocalStorageBaseURL(attr) } @@ -84,7 +104,7 @@ function saveLocalStorageSettings(attr){ function loadLocalStorage(attr){ attr = attr == null ? DATA_ATTR : attr var d = loadLocalStorageData(attr) - BASE_URL = d.base_url + setBaseURL(d.base_url) DATA = d.data IMAGES = loadLocalStorageImages(attr) return reloadViewer() diff --git a/ui/setup.js b/ui/setup.js index b8d79cae..89001324 100755 --- a/ui/setup.js +++ b/ui/setup.js @@ -304,6 +304,11 @@ function setupDataBindings(viewer){ indicator.removeClass('shown') } }) + + .on('baseURLChanged', function(evt, url){ + saveLocalStorageBaseURL() + saveLocalStorageBaseURLHistory() + }) }