more refactoring and tweaking...

Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
Alex A. Naanou 2013-12-16 01:49:47 +04:00
parent 5603fed36b
commit 798a433ab1
5 changed files with 96 additions and 46 deletions

View File

@ -22,32 +22,10 @@ var BOOKMARKS_FILE_PATTERN = /^[0-9]*-bookmarked.json$/
* Helpers
*/
// This is the same as getGIDBefore(..) but will return the currently
// loaded and bookmarked image before current.
//
// for exact protocol see: getGIDBefore(..)
//
// XXX argument processing...
function getBookmarkedGIDBefore(gid){
if(BOOKMARKS.length == 0){
return null
}
gid = gid == null ? getImageGID() : gid
var prev
// need to account for cropping here...
do {
prev = getGIDBefore(gid, BOOKMARKS)
gid = getGIDBefore(prev)
} while(prev != gid && prev != null)
// no bookmarks before current image...
if(prev == null){
return prev
}
return prev
}
var getBookmarkedGIDBefore = makeGIDBeforeGetterFromList(
function(){
return BOOKMARKS
})
@ -101,18 +79,9 @@ var toggleBookmark = makeMarkToggler(
// focus next bookmark...
//
// NOTE: this will not jump to bookmarks on other ribbons...
//
// XXX make a generic next/prev marked function...
var nextBookmark = makeNextFromListAction(
getBookmarkedGIDBefore,
function(){ return BOOKMARKS })
// focus previous bookmark...
//
// NOTE: this will not jump to bookmarks on other ribbons...
//
// XXX make a generic next/prev marked function...
var prevBookmark = makePrevFromListAction(
getBookmarkedGIDBefore,
function(){ return BOOKMARKS })

View File

@ -629,6 +629,46 @@ function getGIDBefore(gid, ribbon, search, data){
}
// Construct a function similar to getGIDBefore(..) that will get the
// closest gid from a list...
//
// for exact protocol see: getGIDBefore(..)
//
// NOTE: this will consider only loaded images...
// NOTE: this needs the list sorted in the same order as the ribbons
// i.e. via DATA.order...
// NOTE: passing a ribbon number or setting restrict_to_ribbon to true
// will restrict the search to a specific ribbon only...
function makeGIDBeforeGetterFromList(get_list, restrict_to_ribbon){
return function(gid, ribbon){
ribbon = ribbon == null && restrict_to_ribbon == true
? getGIDRibbonIndex(gid)
: ribbon
var list = get_list()
if(list.length == 0){
return null
}
console.log('>>>>', ribbon)
gid = gid == null ? getImageGID(null, ribbon) : gid
var prev
// need to account for cropping here...
// skip until we find a match from the list...
do {
prev = getGIDBefore(gid, list)
gid = getGIDBefore(prev, ribbon)
} while(prev != gid && prev != null)
// nothing found before current image...
if(prev == null){
return prev
}
return prev
}
}
// Get "count" of GIDs starting with a given gid ("from")
//
// count can be either negative or positive, this will indicate load
@ -966,6 +1006,8 @@ function imageUpdated(gid){
// Key differences:
// - nextImage(..) uses DOM to get the next image which is simpler
// - nextImage(..) accepts an offset argument
// NOTE: passing a ribbon number or setting restrict_to_ribbon to true
// will restrict the search to a specific ribbon only...
//
// XXX not sure if we need the offset argument here...
// a-la nextImage(n) / prevImage(n)
@ -973,19 +1015,22 @@ function imageUpdated(gid){
// loaded gids?
// (i.e. filter it first and then get the needed gid rather
// iterating...)
function makeNextFromListAction(get_closest, get_list){
function makeNextFromListAction(get_closest, get_list, restrict_to_ribbon){
get_closest = get_closest == null ? getGIDBefore : get_closest
get_list = get_list == null ? getRibbonGIDs : get_list
return function(){
return function(ribbon){
var list = get_list()
if(list.length == 0){
flashIndicator('end')
return getImage()
}
var cur = getImageGID()
ribbon = ribbon == null && restrict_to_ribbon == true
? getGIDRibbonIndex(cur)
: ribbon
var o = getGIDOrder(cur)
var next = get_closest(cur)
var next = get_closest(cur, ribbon)
var i = list.indexOf(next)+1
// we are before the first loaded bookmark, find the first...
@ -994,7 +1039,7 @@ function makeNextFromListAction(get_closest, get_list){
|| getGIDOrder(next) < o)
&& i < list.length){
next = list[i]
next = get_closest(next)
next = get_closest(next, ribbon)
i++
}
@ -1013,18 +1058,21 @@ function makeNextFromListAction(get_closest, get_list){
// see makeNextFromListAction(..) above for documentation...
function makePrevFromListAction(get_closest, get_list){
function makePrevFromListAction(get_closest, get_list, restrict_to_ribbon){
get_closest = get_closest == null ? getGIDBefore : get_closest
get_list = get_list == null ? getRibbonGIDs : get_list
return function(){
return function(ribbon){
var list = get_list()
if(list.length == 0){
flashIndicator('start')
return getImage(cur)
}
var cur = getImageGID()
var prev = get_closest(cur)
ribbon = ribbon == null && restrict_to_ribbon == true
? getGIDRibbonIndex(cur)
: ribbon
var prev = get_closest(cur, ribbon)
// nothing bookmarked before us...
if(prev == null){
@ -1035,7 +1083,7 @@ function makePrevFromListAction(get_closest, get_list){
// current image is bookmarked, get the bookmark before it...
if(prev == cur){
prev = list[list.indexOf(prev)-1]
prev = prev != null ? get_closest(prev) : prev
prev = prev != null ? get_closest(prev, ribbon) : prev
// no loaded (crop mode?) bookmark before us...
if(prev == null){
flashIndicator('start')

View File

@ -584,8 +584,10 @@ var KEYBOARD_CONFIG = {
ctrl: doc('Toggle bookmark',
function(){ toggleBookmark() }),
},
'[': doc('Previous bookmarked image', prevBookmark),
']': doc('Next bookmarked image', nextBookmark),
'[': doc('Previous bookmarked image',
function(){ prevBookmark() }),
']': doc('Next bookmarked image',
function(){ nextBookmark() }),
S: {
default: doc('Start slideshow',

View File

@ -54,6 +54,12 @@ function _removeMark(cls, gid, image){
}
var getMarkedGIDBefore = makeGIDBeforeGetterFromList(
function(){
return MARKED
})
// Make a mark toggler
//
// The toggler will:
@ -378,6 +384,20 @@ function shiftMarkedImagesRight(){
}
// focus next/prev mark...
//
// NOTE: these will not jump to marks on other ribbons... to prevent this
// add true as the final argument (see restrict_to_ribbon argument
// of makeNextFromListAction(..) for more info)
var nextMark = makeNextFromListAction(
getMarkedGIDBefore,
function(){ return MARKED })
var prevMark = makePrevFromListAction(
getMarkedGIDBefore,
function(){ return MARKED })
/**********************************************************************
* Dialogs...
@ -501,7 +521,10 @@ function setupMarks(viewer){
.on('togglingMark', function(evt, gid, action){
// add marked image to list...
if(action == 'on'){
MARKED.indexOf(gid) == -1 && MARKED.push(gid)
if(MARKED.indexOf(gid) == -1){
MARKED.push(gid)
MARKED.sort(imageOrderCmp)
}
// remove marked image from list...
} else {
@ -526,6 +549,7 @@ function setupMarks(viewer){
// do the toggle...
if(state){
MARKED.push(e)
MARKED.sort(imageOrderCmp)
} else {
MARKED.splice(MARKED.indexOf(e), 1)
}
@ -562,6 +586,7 @@ function setupMarks(viewer){
var i = MARKED.indexOf(e)
if(i == -1){
MARKED.push(e)
MARKED.sort(imageOrderCmp)
}
})
})
@ -574,6 +599,7 @@ function setupMarks(viewer){
var i = MARKED.indexOf(e)
if(i == -1){
MARKED.push(e)
MARKED.sort(imageOrderCmp)
} else {
MARKED.splice(i, 1)
}

View File

@ -4,6 +4,10 @@
*
**********************************************************************/
// Tag index
//
// This can be constructed from tags in IMAGES with buildTagsFromImages(..)
//
// format:
// {
// tag: [ gid, ... ],
@ -314,5 +318,6 @@ function setupTags(viewer){
SETUP_BINDINGS.push(setupTags)
/**********************************************************************
* vim:set ts=4 sw=4 : */