2013-05-13 02:24:36 +04:00
|
|
|
/**********************************************************************
|
|
|
|
|
*
|
2013-09-25 02:08:25 +04:00
|
|
|
* Data API and Data DOM connections...
|
2013-05-13 02:24:36 +04:00
|
|
|
*
|
2013-05-17 02:30:24 +04:00
|
|
|
* TODO move DATA to a more logical context avoiding the global vars...
|
2013-09-25 02:08:25 +04:00
|
|
|
* TODO try and split this into:
|
|
|
|
|
* - data.js -- pure DATA API
|
|
|
|
|
* - data-ribbons.js -- DATA and Ribbon API mashup...
|
2013-05-13 02:24:36 +04:00
|
|
|
*
|
|
|
|
|
**********************************************************************/
|
|
|
|
|
|
|
|
|
|
//var DEBUG = DEBUG != null ? DEBUG : true
|
2013-05-13 02:31:09 +04:00
|
|
|
|
2013-06-11 17:12:50 +04:00
|
|
|
var APP_NAME = 'ImageGrid.Viewer'
|
|
|
|
|
|
2013-05-31 20:10:23 +04:00
|
|
|
var DATA_ATTR = 'DATA'
|
|
|
|
|
|
2013-05-20 02:49:14 +04:00
|
|
|
var LOAD_SCREENS = 6
|
2013-05-25 14:24:29 +04:00
|
|
|
|
2013-05-20 02:49:14 +04:00
|
|
|
var DEFAULT_SCREEN_IMAGES = 4
|
2013-05-14 00:01:03 +04:00
|
|
|
var MAX_SCREEN_IMAGES = 12
|
2013-05-13 02:31:09 +04:00
|
|
|
|
2013-06-14 01:11:11 +04:00
|
|
|
var CACHE_DIR = '.ImageGrid'
|
|
|
|
|
var CACHE_DIR_VAR = '${CACHE_DIR}'
|
2013-05-26 02:55:41 +04:00
|
|
|
|
2013-05-17 23:18:55 +04:00
|
|
|
// A stub image, also here for documentation...
|
2013-05-14 21:49:05 +04:00
|
|
|
var STUB_IMAGE_DATA = {
|
2013-06-03 21:10:42 +04:00
|
|
|
// Entity GID...
|
2013-05-14 21:49:05 +04:00
|
|
|
id: 'SIZE',
|
2013-06-03 21:10:42 +04:00
|
|
|
|
|
|
|
|
// Entity type
|
|
|
|
|
// can be:
|
|
|
|
|
// - 'image'
|
|
|
|
|
// - 'group'
|
2013-05-17 23:18:55 +04:00
|
|
|
type: 'image',
|
2013-06-03 21:10:42 +04:00
|
|
|
|
|
|
|
|
// Entity state
|
|
|
|
|
// can be:
|
|
|
|
|
// - 'single'
|
|
|
|
|
// - 'grouped'
|
|
|
|
|
// - 'hidden'
|
|
|
|
|
// - ...
|
2013-05-17 23:18:55 +04:00
|
|
|
state: 'single',
|
2013-06-03 21:10:42 +04:00
|
|
|
|
|
|
|
|
// Creation time...
|
2013-05-14 21:49:05 +04:00
|
|
|
ctime: 0,
|
2013-06-03 21:10:42 +04:00
|
|
|
|
|
|
|
|
// Original path...
|
2013-05-14 21:49:05 +04:00
|
|
|
path: './images/sizes/900px/SIZE.jpg',
|
2013-06-03 21:10:42 +04:00
|
|
|
|
|
|
|
|
// Previews...
|
|
|
|
|
// NOTE: the actual values depend on specific image and can be
|
|
|
|
|
// any size...
|
2013-05-14 21:49:05 +04:00
|
|
|
preview: {
|
|
|
|
|
'150px': './images/sizes/150px/SIZE.jpg',
|
|
|
|
|
'350px': './images/sizes/350px/SIZE.jpg',
|
|
|
|
|
'900px': './images/sizes/900px/SIZE.jpg',
|
|
|
|
|
},
|
2013-06-03 21:10:42 +04:00
|
|
|
|
|
|
|
|
// Classes
|
|
|
|
|
// XXX currently unused...
|
2013-05-14 21:49:05 +04:00
|
|
|
classes: '',
|
2013-06-03 21:10:42 +04:00
|
|
|
|
|
|
|
|
// Image orientation
|
2013-06-04 22:01:41 +04:00
|
|
|
//
|
2013-06-03 21:10:42 +04:00
|
|
|
// can be:
|
|
|
|
|
// - 0 (default) - load as-is
|
|
|
|
|
// - 90 - rotate 90deg CW
|
|
|
|
|
// - 180 - rotate 180deg CW
|
|
|
|
|
// - 270 - rotate 270deg CW (90deg CCW)
|
2013-05-23 17:17:31 +04:00
|
|
|
orientation: 0,
|
2013-06-04 22:01:41 +04:00
|
|
|
|
|
|
|
|
// Image flip state
|
|
|
|
|
//
|
|
|
|
|
// can be:
|
|
|
|
|
// - null/undefined
|
|
|
|
|
// - array
|
|
|
|
|
//
|
|
|
|
|
// can contain:
|
|
|
|
|
// - 'vertical'
|
|
|
|
|
// - 'horizontal'
|
|
|
|
|
flipped: null,
|
2013-05-14 21:49:05 +04:00
|
|
|
}
|
|
|
|
|
|
2013-05-13 02:24:36 +04:00
|
|
|
// Data format...
|
|
|
|
|
var DATA = {
|
2013-06-03 21:10:42 +04:00
|
|
|
// Format version...
|
2013-05-19 22:48:28 +04:00
|
|
|
version: '2.0',
|
2013-06-02 20:14:39 +04:00
|
|
|
|
2013-06-03 21:10:42 +04:00
|
|
|
// Current position, GID...
|
2013-06-02 20:14:39 +04:00
|
|
|
current: null,
|
|
|
|
|
|
2013-06-03 21:10:42 +04:00
|
|
|
// The ribbon cache...
|
2013-05-13 02:24:36 +04:00
|
|
|
// in the simplest form this is a list of lists of GIDs
|
2013-06-02 20:14:39 +04:00
|
|
|
ribbons: [],
|
|
|
|
|
|
2013-06-03 21:10:42 +04:00
|
|
|
// Flat ordered list of images in current context...
|
2013-05-13 02:24:36 +04:00
|
|
|
// in the simplest form this is a list of GIDs.
|
2013-06-02 20:14:39 +04:00
|
|
|
order: [],
|
2013-05-19 22:48:28 +04:00
|
|
|
|
2013-06-03 21:10:42 +04:00
|
|
|
// This can be used to store the filename/path of the file containing
|
2013-05-19 22:48:28 +04:00
|
|
|
// image data...
|
|
|
|
|
image_file: null
|
2013-05-13 02:24:36 +04:00
|
|
|
}
|
|
|
|
|
|
2013-05-19 22:48:28 +04:00
|
|
|
// the images object, this is indexed by image GID and contains all
|
|
|
|
|
// the needed data...
|
|
|
|
|
var IMAGES = {}
|
2013-06-02 20:14:39 +04:00
|
|
|
// list of image GIDs that have been updated...
|
2013-05-28 02:17:24 +04:00
|
|
|
var IMAGES_UPDATED = []
|
2013-05-19 22:48:28 +04:00
|
|
|
|
2013-06-13 19:42:42 +04:00
|
|
|
var IMAGES_CREATED = false
|
|
|
|
|
|
2013-05-17 04:52:43 +04:00
|
|
|
var MARKED = []
|
2013-05-14 18:10:33 +04:00
|
|
|
|
2013-07-03 20:18:00 +04:00
|
|
|
// NOTE: these are named: <mode>-<feature>
|
2013-05-17 17:58:23 +04:00
|
|
|
var SETTINGS = {
|
2013-07-03 20:18:00 +04:00
|
|
|
'global-theme': null,
|
|
|
|
|
'ribbon-mode-screen-images': null,
|
|
|
|
|
'single-image-mode-screen-images': null,
|
2013-05-24 00:09:13 +04:00
|
|
|
'single-image-mode-proportions': null,
|
2013-07-03 20:18:00 +04:00
|
|
|
'ribbon-mode-image-info': 'off',
|
2013-05-17 17:58:23 +04:00
|
|
|
}
|
|
|
|
|
|
2013-05-25 14:24:29 +04:00
|
|
|
var BASE_URL = '.'
|
|
|
|
|
|
2013-05-31 20:10:23 +04:00
|
|
|
var IMAGE_CACHE = []
|
|
|
|
|
|
2013-06-06 07:39:30 +04:00
|
|
|
// XXX make these usable for both saving and loading...
|
|
|
|
|
// XXX get these from config...
|
|
|
|
|
var IMAGES_FILE_DEFAULT = 'images.json'
|
|
|
|
|
var IMAGES_FILE_PATTERN = /^[0-9]*-images.json$/
|
|
|
|
|
var IMAGES_DIFF_FILE_PATTERN = /^[0-9]*-images-diff.json$/
|
|
|
|
|
|
|
|
|
|
var MARKED_FILE_DEFAULT = 'marked.json'
|
|
|
|
|
var MARKED_FILE_PATTERN = /^[0-9]*-marked.json$/
|
|
|
|
|
|
|
|
|
|
var DATA_FILE_DEFAULT = 'data.json'
|
|
|
|
|
var DATA_FILE_PATTERN = /^[0-9]*-data.json$/
|
|
|
|
|
|
|
|
|
|
var IMAGE_PATTERN = /.*\.(jpg|jpeg|png|gif)$/i
|
|
|
|
|
|
2013-06-02 23:07:18 +04:00
|
|
|
var UPDATE_SORT_ENABLED = false
|
|
|
|
|
// XXX for some reason the sync version appears to work faster...
|
|
|
|
|
var UPDATE_SYNC = false
|
|
|
|
|
|
2013-12-02 17:45:54 +04:00
|
|
|
// if this is true image previews will be loaded synchronously by
|
|
|
|
|
// default...
|
|
|
|
|
var SYNC_IMG_LOADER = false
|
|
|
|
|
|
2013-05-13 02:24:36 +04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
/**********************************************************************
|
|
|
|
|
* Helpers
|
|
|
|
|
*/
|
|
|
|
|
|
2013-08-18 21:19:23 +04:00
|
|
|
// Zip concatenate lists from each argument.
|
|
|
|
|
//
|
|
|
|
|
// NOTE: this will skip null values.
|
|
|
|
|
function concatZip(){
|
|
|
|
|
var res = []
|
|
|
|
|
$.each(arguments, function(i, lst){
|
|
|
|
|
$.each(lst, function(j, e){
|
|
|
|
|
if(e != null){
|
|
|
|
|
if(res[j] == null){
|
|
|
|
|
res[j] = e
|
|
|
|
|
} else {
|
|
|
|
|
res[j] = res[j].concat(e)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
return res
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2013-10-18 05:03:11 +04:00
|
|
|
function getImageFileName(gid, images, do_unescape){
|
|
|
|
|
gid = gid == null ? getImageGID() : gid
|
|
|
|
|
images = images == null ? IMAGES : images
|
|
|
|
|
do_unescape = do_unescape == null ? true : do_unescape
|
|
|
|
|
|
|
|
|
|
if(do_unescape){
|
|
|
|
|
return unescape(images[gid].path.split('/').pop())
|
|
|
|
|
} else {
|
|
|
|
|
return images[gid].path.split('/').pop()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2013-09-23 17:47:27 +04:00
|
|
|
// Get the first sequence of numbers in the file name...
|
|
|
|
|
function getImageNameSeq(gid, data){
|
|
|
|
|
data = data == null ? IMAGES : data
|
2013-10-18 05:03:11 +04:00
|
|
|
var n = getImageFileName(gid, data)
|
2013-09-23 17:47:27 +04:00
|
|
|
var r = /([0-9]+)/m.exec(n)
|
|
|
|
|
return r == null ? n : parseInt(r[1])
|
|
|
|
|
}
|
|
|
|
|
|
2013-09-25 17:00:54 +04:00
|
|
|
|
2013-09-23 17:47:27 +04:00
|
|
|
// Get the first sequence of numbers in the file name but only if it is
|
|
|
|
|
// at the filename start...
|
|
|
|
|
function getImageNameLeadingSeq(gid, data){
|
|
|
|
|
data = data == null ? IMAGES : data
|
2013-10-18 05:03:11 +04:00
|
|
|
var n = getImageFileName(gid, data)
|
2013-09-23 17:47:27 +04:00
|
|
|
var r = /^([0-9]+)/g.exec(n)
|
|
|
|
|
return r == null ? n : parseInt(r[1])
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2013-09-25 17:00:54 +04:00
|
|
|
function getGIDDistance(a, b, get, data){
|
|
|
|
|
data = data == null ? DATA : data
|
|
|
|
|
var order = data.order
|
2013-09-23 17:47:27 +04:00
|
|
|
if(get != null){
|
|
|
|
|
a = get(a)
|
|
|
|
|
b = get(b)
|
|
|
|
|
}
|
2013-09-25 17:00:54 +04:00
|
|
|
a = order.indexOf(a)
|
|
|
|
|
b = order.indexOf(b)
|
|
|
|
|
return Math.abs(a - b)
|
|
|
|
|
}
|
2013-09-23 17:47:27 +04:00
|
|
|
|
|
|
|
|
|
2013-09-27 21:45:31 +04:00
|
|
|
// Construct 2D distance from gid getter
|
|
|
|
|
//
|
|
|
|
|
// The distance dimensions are:
|
|
|
|
|
// - ribbons
|
|
|
|
|
// - gids within a ribbon
|
|
|
|
|
//
|
|
|
|
|
// This is a constructor to cache the generated index as it is quite
|
|
|
|
|
// slow to construct, but needs to be current...
|
|
|
|
|
//
|
|
|
|
|
// NOTE: this is very similar in effect to getGIDDistance(...) but will
|
|
|
|
|
// also account for ribbons...
|
|
|
|
|
// NOTE: see getGIDRibbonDistance(...) for usage example...
|
2013-09-25 17:00:54 +04:00
|
|
|
function makeGIDRibbonDistanceGetter(gid, data){
|
|
|
|
|
data = data == null ? DATA : data
|
2013-09-23 17:47:27 +04:00
|
|
|
|
2013-09-25 17:00:54 +04:00
|
|
|
// make a cmp index...
|
|
|
|
|
var ribbons = $.map(DATA.ribbons, function(r, i){
|
|
|
|
|
// sort each ribbon by distance from closest gid...
|
|
|
|
|
//return [r.slice().sort(makeGIDDistanceCmp(getGIDBefore(gid, i)))]
|
|
|
|
|
return [r.slice().sort(makeGIDDistanceCmp(gid))]
|
|
|
|
|
})
|
|
|
|
|
var gids = $.map(ribbons, function(e){ return [e[0]] })
|
|
|
|
|
var ri = gids.indexOf(gid)
|
2013-09-23 17:47:27 +04:00
|
|
|
|
2013-09-25 17:00:54 +04:00
|
|
|
// the basic calculator...
|
|
|
|
|
return function(gid){
|
|
|
|
|
var r = ribbons[getGIDRibbonIndex(gid, {ribbons: ribbons})]
|
|
|
|
|
var x = r.indexOf(gid)
|
|
|
|
|
var y = Math.abs(gids.indexOf(r[0]) - ri)
|
|
|
|
|
|
|
|
|
|
// calculate real distance...
|
|
|
|
|
return Math.sqrt(x*x + y*y)
|
2013-05-17 05:07:43 +04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-09-25 17:00:54 +04:00
|
|
|
|
|
|
|
|
// Get distance between two gids taking into account ribbons...
|
2013-09-23 17:47:27 +04:00
|
|
|
//
|
2013-09-25 17:00:54 +04:00
|
|
|
// This is essentially a 2D distance between two gids in data.
|
2013-09-23 17:47:27 +04:00
|
|
|
//
|
2013-09-25 17:00:54 +04:00
|
|
|
// NOTE: to get lots of distances from a specific image use
|
|
|
|
|
// makeGIDDistanceCmp(...) for faster results...
|
|
|
|
|
function getGIDRibbonDistance(a, b, data){
|
|
|
|
|
return makeDistanceFromGIDGetter(a, data)(b)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function cmp(a, b, get){
|
|
|
|
|
if(get == null){
|
|
|
|
|
return a - b
|
|
|
|
|
}
|
|
|
|
|
return get(a) - get(b)
|
2013-09-23 17:47:27 +04:00
|
|
|
}
|
|
|
|
|
|
2013-05-31 20:10:23 +04:00
|
|
|
|
2013-09-27 21:45:31 +04:00
|
|
|
// Generic image ordering comparison via DATA.order
|
2013-09-25 17:00:54 +04:00
|
|
|
//
|
2013-09-27 21:45:31 +04:00
|
|
|
// NOTE: see updateRibbonORder(...) for a general view on image sorting
|
|
|
|
|
// and re-sorting mechanics.
|
2013-05-31 20:10:23 +04:00
|
|
|
// NOTE: this expects gids...
|
2013-09-25 17:00:54 +04:00
|
|
|
// NOTE: this is not in sort.js because it is a generic base sort method
|
2013-05-31 20:10:23 +04:00
|
|
|
function imageOrderCmp(a, b, get, data){
|
2013-05-29 02:41:35 +04:00
|
|
|
data = data == null ? DATA : data
|
2013-09-23 17:47:27 +04:00
|
|
|
if(get != null){
|
|
|
|
|
a = get(a)
|
|
|
|
|
b = get(b)
|
2013-05-31 20:10:23 +04:00
|
|
|
}
|
2013-09-23 17:47:27 +04:00
|
|
|
return data.order.indexOf(a) - data.order.indexOf(b)
|
2013-05-29 02:41:35 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2013-05-18 15:48:22 +04:00
|
|
|
// Check if a is at position i in lst
|
|
|
|
|
//
|
|
|
|
|
// This will return:
|
|
|
|
|
// - 0 if a is equal to position i
|
2013-05-19 17:26:53 +04:00
|
|
|
// - -1 if a is less than position i
|
|
|
|
|
// - +1 if a is greater than position i
|
2013-05-18 15:48:22 +04:00
|
|
|
//
|
2013-06-04 02:08:20 +04:00
|
|
|
// NOTE: the signature is different from the traditional cmp(a, b) so as
|
2013-05-18 15:48:22 +04:00
|
|
|
// to enable more complex comparisons involving adjacent elements
|
|
|
|
|
// (see isBetween(...) for an example)
|
2013-06-04 02:08:20 +04:00
|
|
|
function lcmp(a, i, lst, get){
|
|
|
|
|
var b = get == null ? lst[i] : get(lst[i])
|
|
|
|
|
|
2013-05-18 15:48:22 +04:00
|
|
|
if(a == b){
|
|
|
|
|
return 0
|
|
|
|
|
} else if(a < b){
|
|
|
|
|
return -1
|
|
|
|
|
} else {
|
|
|
|
|
return 1
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Check if a is at position i in lst or between positions i and i+1
|
|
|
|
|
//
|
|
|
|
|
// This will return:
|
2013-05-13 02:24:36 +04:00
|
|
|
// - 0 if a is equal at position i in lst or is between i and i+1
|
|
|
|
|
// - -1 if a is "below" position i
|
|
|
|
|
// - +1 if a is "above" position i
|
|
|
|
|
//
|
2013-06-04 02:08:20 +04:00
|
|
|
// NOTE: this is here mostly to make debugging easy...
|
|
|
|
|
function isBetween(a, i, lst, get){
|
|
|
|
|
var b = get == null ? lst[i] : get(lst[i])
|
2013-05-19 17:26:53 +04:00
|
|
|
|
|
|
|
|
// special case: tail...
|
|
|
|
|
if(i == lst.length-1 && a >= b){
|
|
|
|
|
return 0
|
|
|
|
|
}
|
|
|
|
|
|
2013-05-13 02:24:36 +04:00
|
|
|
var c = lst[i+1]
|
2013-05-19 17:26:53 +04:00
|
|
|
|
2013-05-13 02:24:36 +04:00
|
|
|
// hit...
|
|
|
|
|
if(a == b || (a > b && a < c)){
|
|
|
|
|
return 0
|
|
|
|
|
// before...
|
|
|
|
|
} else if(a < b){
|
|
|
|
|
return -1
|
|
|
|
|
// later...
|
|
|
|
|
} else {
|
|
|
|
|
return 1
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2013-06-04 02:08:20 +04:00
|
|
|
/*
|
2013-05-13 02:24:36 +04:00
|
|
|
// Basic liner search...
|
2013-05-19 17:43:28 +04:00
|
|
|
//
|
|
|
|
|
// NOTE: this is here for testing reasons only...
|
2013-06-04 02:08:20 +04:00
|
|
|
function linSearch(target, lst, check, return_position, get){
|
2013-05-31 20:10:23 +04:00
|
|
|
check = check == null ? lcmp : check
|
2013-05-13 02:24:36 +04:00
|
|
|
|
|
|
|
|
for(var i=0; i < lst.length; i++){
|
2013-06-04 02:08:20 +04:00
|
|
|
if(check(target, i, lst, get) == 0){
|
2013-05-13 02:24:36 +04:00
|
|
|
return return_position ? i : lst[i]
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// no hit...
|
|
|
|
|
return return_position ? -1 : null
|
|
|
|
|
}
|
2013-06-04 02:08:20 +04:00
|
|
|
Array.prototype.linSearch = function(target, cmp, get){
|
|
|
|
|
return linSearch(target, this, cmp, true, get)
|
2013-05-19 17:26:53 +04:00
|
|
|
}
|
2013-06-04 02:08:20 +04:00
|
|
|
*/
|
2013-05-13 02:24:36 +04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
// Basic binary search implementation...
|
|
|
|
|
//
|
|
|
|
|
// NOTE: this will return the object by default, to return position set
|
|
|
|
|
// return_position to true.
|
2013-05-19 17:26:53 +04:00
|
|
|
// NOTE: by default this will use cmp as a predicate.
|
2013-11-09 15:03:49 +04:00
|
|
|
// NOTE: this expects lst to be sorted in a check-compatible way...
|
2013-06-04 02:08:20 +04:00
|
|
|
function binSearch(target, lst, check, return_position, get){
|
2013-05-31 20:10:23 +04:00
|
|
|
check = check == null ? lcmp : check
|
2013-05-19 17:26:53 +04:00
|
|
|
var h = 0
|
|
|
|
|
var t = lst.length - 1
|
|
|
|
|
var m, res
|
|
|
|
|
|
|
|
|
|
while(h <= t){
|
|
|
|
|
m = Math.floor((h + t)/2)
|
2013-06-04 02:08:20 +04:00
|
|
|
res = check(target, m, lst, get)
|
2013-05-19 17:26:53 +04:00
|
|
|
|
|
|
|
|
// match...
|
|
|
|
|
if(res == 0){
|
|
|
|
|
return return_position ? m : lst[m]
|
|
|
|
|
|
|
|
|
|
// below...
|
2013-05-13 02:24:36 +04:00
|
|
|
} else if(res < 0){
|
2013-05-19 17:26:53 +04:00
|
|
|
t = m - 1
|
|
|
|
|
|
|
|
|
|
// above...
|
2013-05-13 02:24:36 +04:00
|
|
|
} else {
|
2013-05-19 17:26:53 +04:00
|
|
|
h = m + 1
|
2013-05-13 02:24:36 +04:00
|
|
|
}
|
|
|
|
|
}
|
2013-05-19 17:26:53 +04:00
|
|
|
|
|
|
|
|
// no result...
|
2013-05-13 02:24:36 +04:00
|
|
|
return return_position ? -1 : null
|
|
|
|
|
}
|
2013-06-04 02:08:20 +04:00
|
|
|
Array.prototype.binSearch = function(target, cmp, get){
|
|
|
|
|
return binSearch(target, this, cmp, true, get)
|
2013-05-19 17:26:53 +04:00
|
|
|
}
|
2013-05-13 02:24:36 +04:00
|
|
|
|
|
|
|
|
|
2013-09-27 21:45:31 +04:00
|
|
|
// Base ribbon index interface...
|
|
|
|
|
//
|
|
|
|
|
// XXX we need a persistent way to store this index
|
|
|
|
|
//
|
|
|
|
|
// - DATA.base_ribbon
|
|
|
|
|
// - need to be kept in sync all the time (for shift)
|
|
|
|
|
// + simple and obvious for a data format
|
|
|
|
|
//
|
|
|
|
|
// - DATA.ribbons[n].base = true
|
|
|
|
|
// + persistent and no sync required
|
|
|
|
|
// - not storable directly via JSON.stringify(...)
|
|
|
|
|
//
|
|
|
|
|
// - do not persistently store the base ribbon unless explicitly
|
|
|
|
|
// required, and set it to 0 on each load/reload
|
|
|
|
|
// ~ will need to decide what to do on each save/exit:
|
|
|
|
|
// - align ribbons to top (base = 0)
|
|
|
|
|
// - save "in-progress" state as-is (base > 0)
|
|
|
|
|
// - reset base (base = 0)
|
|
|
|
|
// this is a good idea if we have fine grained auto-save and
|
|
|
|
|
// a Ctrl-S triggers a major save, possibly requiring a user
|
|
|
|
|
// comment (a-la VCS)
|
|
|
|
|
//
|
|
|
|
|
// - treat ribbons in the same way as images, with a GID...
|
|
|
|
|
// - format change (v3.0)
|
|
|
|
|
// ~ rewrite everything that accesses DATA.ribbons
|
|
|
|
|
// this is not that critical as the changes are simple in
|
|
|
|
|
// most cases...
|
|
|
|
|
// + ribbons are a first class object and can be treated as
|
|
|
|
|
// such...
|
|
|
|
|
// - more natural ribbon operations: grouping, combining, ...
|
|
|
|
|
// - ribbon tagging
|
|
|
|
|
// - a ribbon can be treated as an entity, thus simplifying
|
|
|
|
|
// work on collections...
|
|
|
|
|
// - added complexity
|
|
|
|
|
//
|
|
|
|
|
// XXX this is a stub...
|
|
|
|
|
function getBaseRibbonIndex(){
|
|
|
|
|
|
|
|
|
|
// XXX
|
|
|
|
|
|
|
|
|
|
return 0
|
|
|
|
|
}
|
|
|
|
|
function setBaseRibbonIndex(n){
|
|
|
|
|
n = n == null ? 0 : n
|
|
|
|
|
|
|
|
|
|
// XXX
|
|
|
|
|
|
|
|
|
|
return n
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2013-09-13 23:13:16 +04:00
|
|
|
// like getRibbonIndex but get the index only via DATA...
|
2013-09-25 17:00:54 +04:00
|
|
|
function getGIDRibbonIndex(gid, data){
|
2013-09-13 23:13:16 +04:00
|
|
|
gid = gid == null ? getImageGID() : gid
|
|
|
|
|
data = data == null ? DATA : data
|
|
|
|
|
|
2013-09-25 17:00:54 +04:00
|
|
|
var ribbons = data.ribbons
|
|
|
|
|
|
|
|
|
|
for(var i=0; i < ribbons.length; i++){
|
|
|
|
|
if(ribbons[i].indexOf(gid) >= 0){
|
2013-09-13 23:13:16 +04:00
|
|
|
return i
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return -1
|
|
|
|
|
}
|
|
|
|
|
|
2013-09-25 17:00:54 +04:00
|
|
|
|
2013-06-01 18:21:48 +04:00
|
|
|
// Same as getImageBefore(...), but uses gids and searches in DATA...
|
2013-05-19 17:26:53 +04:00
|
|
|
//
|
2013-11-09 15:03:49 +04:00
|
|
|
// Return:
|
|
|
|
|
// null - no image is before gid
|
|
|
|
|
// gid - the image before
|
|
|
|
|
//
|
|
|
|
|
// NOTE: if gid is present in the searched ribbon this will return it.
|
2013-05-19 17:26:53 +04:00
|
|
|
// NOTE: this uses it's own predicate...
|
2013-08-23 00:33:23 +04:00
|
|
|
function getGIDBefore(gid, ribbon, search, data){
|
2013-05-28 21:59:38 +04:00
|
|
|
gid = gid == null ? getImageGID() : gid
|
2013-08-23 00:33:23 +04:00
|
|
|
data = data == null ? DATA : data
|
2013-09-13 23:13:16 +04:00
|
|
|
// XXX get a ribbon without getting into DOM...
|
|
|
|
|
// ...dependency leek...
|
2013-09-25 17:00:54 +04:00
|
|
|
ribbon = ribbon == null ? getGIDRibbonIndex(gid, data) : ribbon
|
2013-09-13 23:13:16 +04:00
|
|
|
search = search == null ? binSearch : search
|
2013-05-19 17:43:28 +04:00
|
|
|
//search = search == null ? match2(linSearch, binSearch) : search
|
2013-08-23 00:33:23 +04:00
|
|
|
ribbon = data.ribbons[ribbon]
|
|
|
|
|
var order = data.order
|
2013-05-13 02:24:36 +04:00
|
|
|
|
|
|
|
|
var target = order.indexOf(gid)
|
|
|
|
|
|
2013-05-19 17:26:53 +04:00
|
|
|
return search(target, ribbon, function(a, i, lst){
|
2013-05-13 02:24:36 +04:00
|
|
|
var b = order.indexOf(lst[i])
|
2013-05-19 17:26:53 +04:00
|
|
|
|
|
|
|
|
// special case: tail...
|
|
|
|
|
if(i == lst.length-1 && a >= b){
|
|
|
|
|
return 0
|
|
|
|
|
}
|
|
|
|
|
|
2013-05-13 02:24:36 +04:00
|
|
|
var c = order.indexOf(lst[i+1])
|
2013-05-19 17:26:53 +04:00
|
|
|
|
2013-05-13 02:24:36 +04:00
|
|
|
// hit...
|
|
|
|
|
if(a == b || (a > b && a < c)){
|
|
|
|
|
return 0
|
2013-05-19 17:26:53 +04:00
|
|
|
|
2013-05-13 02:24:36 +04:00
|
|
|
// before...
|
|
|
|
|
} else if(a < b){
|
|
|
|
|
return -1
|
2013-05-19 17:26:53 +04:00
|
|
|
|
2013-05-13 02:24:36 +04:00
|
|
|
// later...
|
|
|
|
|
} else {
|
|
|
|
|
return 1
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2013-09-25 17:00:54 +04:00
|
|
|
// Base URL interface...
|
|
|
|
|
//
|
|
|
|
|
// NOTE: changing a base URL will trigger a baseURLChanged event...
|
|
|
|
|
function getBaseURL(){
|
|
|
|
|
return BASE_URL
|
|
|
|
|
}
|
|
|
|
|
function setBaseURL(url){
|
|
|
|
|
var old_url = BASE_URL
|
|
|
|
|
url = url.replace(/\/*$/, '/')
|
|
|
|
|
BASE_URL = url
|
|
|
|
|
$('.viewer').trigger('baseURLChanged', [old_url, url])
|
|
|
|
|
return url
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Normalize the path...
|
|
|
|
|
//
|
|
|
|
|
// This will:
|
|
|
|
|
// - convert windows absolute paths 'X:\...' -> 'file:///X:/...'
|
|
|
|
|
// - if mode is 'absolute':
|
|
|
|
|
// - return absolute paths as-is
|
|
|
|
|
// - base relative paths on base/BASE_URL, returning an absolute
|
|
|
|
|
// path
|
|
|
|
|
// - if mode is relative:
|
|
|
|
|
// - if absolute path is based on base/BASE_URL make a relative
|
|
|
|
|
// to base path out of it buy cutting the base out.
|
|
|
|
|
// - return absolute paths as-is
|
|
|
|
|
// - return relative paths as-is
|
|
|
|
|
//
|
|
|
|
|
// NOTE: mode can be either 'absolute' (default) or 'relative'...
|
2013-10-16 03:42:42 +04:00
|
|
|
function normalizePath(url, base, mode, do_unescape){
|
2013-09-25 17:00:54 +04:00
|
|
|
base = base == null ? getBaseURL() : base
|
|
|
|
|
//mode = /^\./.test(base) && mode == null ? 'relative' : null
|
|
|
|
|
mode = mode == null ? 'absolute' : mode
|
2013-10-16 03:42:42 +04:00
|
|
|
// XXX is this the correct default?
|
|
|
|
|
do_unescape = do_unescape == null ? true : do_unescape
|
2013-09-25 17:00:54 +04:00
|
|
|
|
|
|
|
|
res = ''
|
|
|
|
|
|
|
|
|
|
// windows path...
|
|
|
|
|
// - replace all '\\' with '/'...
|
|
|
|
|
url = url.replace(/\\/g, '/')
|
|
|
|
|
// - replace 'X:/...' with 'file:///X:/...'
|
|
|
|
|
if(/^[A-Z]:\//.test(url)){
|
|
|
|
|
url = 'file:///' + url
|
|
|
|
|
}
|
2013-11-12 02:43:12 +04:00
|
|
|
// UN*X/OSX path...
|
|
|
|
|
if(url[0] == '/'){
|
|
|
|
|
// XXX test exactly how many slashes to we need, two or three?
|
|
|
|
|
url = 'file://' + url
|
|
|
|
|
}
|
2013-09-25 17:00:54 +04:00
|
|
|
|
|
|
|
|
// we got absolute path...
|
|
|
|
|
if(/^(file|http|https):\/\/.*$/.test(url)){
|
|
|
|
|
// check if we start with base, and remove it if so...
|
|
|
|
|
if(mode == 'relative' && url.substring(0, base.length) == base){
|
|
|
|
|
url = url.substring(base.length - 1)
|
|
|
|
|
res = url[0] == '/' ? url.substring(1) : url
|
|
|
|
|
|
|
|
|
|
// if it's a different path, return as-is
|
|
|
|
|
} else if(mode == 'absolute'){
|
|
|
|
|
res = url
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// make an absolute path...
|
|
|
|
|
} else if(mode == 'absolute') {
|
|
|
|
|
// if base ends and url starts with '.' avoid making it a '..'
|
|
|
|
|
if(base[base.length-1] == '.' && url[0] == '.'){
|
|
|
|
|
res = base + url.substring(1)
|
|
|
|
|
// avoid creating '//'...
|
|
|
|
|
} else if(base[base.length-1] != '/' && url[0] != '/'){
|
|
|
|
|
res = base + '/' + url
|
|
|
|
|
} else {
|
|
|
|
|
res = base + url
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// get the actual path...
|
|
|
|
|
res = res.replace('${CACHE_DIR}', CACHE_DIR)
|
|
|
|
|
|
|
|
|
|
// XXX legacy support...
|
|
|
|
|
res = res.replace('.ImageGridCache', CACHE_DIR)
|
|
|
|
|
|
2013-10-16 03:42:42 +04:00
|
|
|
if(do_unescape){
|
|
|
|
|
return unescape(res)
|
|
|
|
|
} else {
|
|
|
|
|
return res
|
|
|
|
|
}
|
2013-09-25 17:00:54 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2013-05-17 01:04:20 +04:00
|
|
|
// Select best preview by size...
|
|
|
|
|
//
|
|
|
|
|
// NOTE: this will use the original if everything else is smaller...
|
|
|
|
|
function getBestPreview(gid, size){
|
2013-05-28 21:59:38 +04:00
|
|
|
gid = gid == null ? getImageGID(): gid
|
2013-05-17 16:21:03 +04:00
|
|
|
size = size == null ? getVisibleImageSize('max') : size
|
2013-05-17 01:04:20 +04:00
|
|
|
var s
|
2013-05-19 22:48:28 +04:00
|
|
|
var img_data = IMAGES[gid]
|
2013-05-17 01:04:20 +04:00
|
|
|
var url = img_data.path
|
|
|
|
|
var preview_size = 'Original'
|
|
|
|
|
var p = Infinity
|
|
|
|
|
|
|
|
|
|
for(var k in img_data.preview){
|
|
|
|
|
s = parseInt(k)
|
|
|
|
|
if(s < p && s > size){
|
|
|
|
|
preview_size = k
|
|
|
|
|
p = s
|
|
|
|
|
url = img_data.preview[k]
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return {
|
2013-05-25 14:24:29 +04:00
|
|
|
url: normalizePath(url),
|
2013-05-17 01:04:20 +04:00
|
|
|
size: preview_size
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2013-09-25 17:00:54 +04:00
|
|
|
// Orientation translation...
|
|
|
|
|
function orientationExif2ImageGrid(orientation){
|
|
|
|
|
return {
|
|
|
|
|
orientation: {
|
|
|
|
|
0: 0,
|
|
|
|
|
1: 0,
|
|
|
|
|
2: 0,
|
|
|
|
|
3: 180,
|
|
|
|
|
4: 0,
|
|
|
|
|
5: 90,
|
|
|
|
|
6: 90,
|
|
|
|
|
7: 90,
|
|
|
|
|
8: 270,
|
|
|
|
|
}[orientation],
|
|
|
|
|
flipped: {
|
|
|
|
|
0: null,
|
|
|
|
|
1: null,
|
|
|
|
|
2: ['horizontal'],
|
|
|
|
|
3: null,
|
|
|
|
|
4: ['vertical'],
|
|
|
|
|
5: ['vertical'],
|
|
|
|
|
6: null,
|
|
|
|
|
7: ['horizontal'],
|
|
|
|
|
8: null,
|
|
|
|
|
}[orientation]
|
2013-06-02 23:07:18 +04:00
|
|
|
}
|
2013-06-02 02:17:04 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2013-05-13 02:31:09 +04:00
|
|
|
|
2013-05-28 19:25:13 +04:00
|
|
|
/**********************************************************************
|
2013-09-27 21:45:31 +04:00
|
|
|
* Constructors and general data manipulation
|
2013-05-28 19:25:13 +04:00
|
|
|
*/
|
|
|
|
|
|
2013-06-02 23:07:18 +04:00
|
|
|
// Construct an IMAGES object from list of urls.
|
|
|
|
|
//
|
2013-05-29 01:57:24 +04:00
|
|
|
// NOTE: this depends on that the base dir contains ALL the images...
|
2013-07-08 01:59:30 +04:00
|
|
|
// NOTE: if base is not given, this will not read image to get
|
|
|
|
|
// orientation data...
|
2013-11-06 03:44:14 +04:00
|
|
|
function imagesFromUrls(lst, ctime_getter){
|
|
|
|
|
ctime_getter = (ctime_getter == null
|
|
|
|
|
? function(){ return Date.now()/1000 }
|
|
|
|
|
: ctime_getter)
|
2013-05-28 19:25:13 +04:00
|
|
|
var res = {}
|
|
|
|
|
|
|
|
|
|
$.each(lst, function(i, e){
|
2013-05-29 01:57:24 +04:00
|
|
|
|
2013-06-21 20:09:24 +04:00
|
|
|
/*
|
2013-05-30 03:26:49 +04:00
|
|
|
// this is ugly but I'm bored so this is pretty...
|
2013-06-02 20:14:39 +04:00
|
|
|
var ii = i < 10 ? '0000000' + i
|
2013-05-29 01:57:24 +04:00
|
|
|
: i < 100 ? '000000' + i
|
|
|
|
|
: i < 1000 ? '00000' + i
|
|
|
|
|
: i < 10000 ? '0000' + i
|
|
|
|
|
: i < 100000 ? '000' + i
|
|
|
|
|
: i < 1000000 ? '00' + i
|
|
|
|
|
: i < 10000000 ? '0' + i
|
|
|
|
|
: i
|
2013-06-21 20:09:24 +04:00
|
|
|
*/
|
|
|
|
|
i = i+''
|
|
|
|
|
var ii = ('00000000' + i).slice(i.length)
|
2013-05-29 01:57:24 +04:00
|
|
|
var gid = 'image-' + ii
|
2013-05-28 19:25:13 +04:00
|
|
|
res[gid] = {
|
|
|
|
|
id: gid,
|
|
|
|
|
type: 'image',
|
|
|
|
|
state: 'single',
|
|
|
|
|
path: e,
|
2013-11-06 03:44:14 +04:00
|
|
|
ctime: ctime_getter(e),
|
2013-05-28 19:25:13 +04:00
|
|
|
preview: {},
|
|
|
|
|
classes: '',
|
|
|
|
|
orientation: 0,
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
return res
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2013-06-02 23:07:18 +04:00
|
|
|
// Construct a DATA object from a list of images
|
|
|
|
|
//
|
|
|
|
|
// NOTE: this will create a single ribbon...
|
2013-05-28 19:25:13 +04:00
|
|
|
function dataFromImages(images){
|
|
|
|
|
var gids = Object.keys(images).sort()
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
version: '2.0',
|
|
|
|
|
current: gids[0],
|
|
|
|
|
ribbons: [
|
|
|
|
|
gids
|
|
|
|
|
],
|
|
|
|
|
order: gids.slice(),
|
|
|
|
|
image_file: null
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2013-09-06 03:16:48 +04:00
|
|
|
// Clean out empty ribbons...
|
|
|
|
|
//
|
|
|
|
|
function dropEmptyRibbons(data){
|
|
|
|
|
data = data == null ? DATA : data
|
|
|
|
|
|
|
|
|
|
var ribbons = data.ribbons
|
|
|
|
|
|
|
|
|
|
var i = 0
|
|
|
|
|
while(i < ribbons.length){
|
|
|
|
|
if(ribbons[i].length == 0){
|
|
|
|
|
ribbons.splice(i, 1)
|
|
|
|
|
} else {
|
|
|
|
|
i++
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return data
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2013-08-19 19:24:47 +04:00
|
|
|
// Merge two or more data objects
|
2013-08-18 21:19:23 +04:00
|
|
|
//
|
2013-08-19 19:24:47 +04:00
|
|
|
// Each data object can be:
|
|
|
|
|
// - straight data object
|
|
|
|
|
// - array with ribbon shift at position 0 and the data at 1.
|
2013-08-18 21:19:23 +04:00
|
|
|
//
|
2013-08-19 19:24:47 +04:00
|
|
|
// The shift can be either positive or negative value. Positive shift
|
|
|
|
|
// will shift the ribbons down (add padding to the top), while negative
|
|
|
|
|
// will shift the ribbons up.
|
2013-08-18 21:19:23 +04:00
|
|
|
//
|
2013-08-19 19:24:47 +04:00
|
|
|
// NOTE: if no shift is given it will default to 0, i.e. align by top
|
|
|
|
|
// ribbon.
|
|
|
|
|
// NOTE: shifting one set of ribbons up (negative shift) is the same as
|
|
|
|
|
// shifting every other set down by the same amount down (positive).
|
|
|
|
|
// e.g. these shifts:
|
|
|
|
|
// -1 0 2 -5 0 0
|
|
|
|
|
// will be normalized to, or are equivalent to:
|
|
|
|
|
// 4 5 7 0 5 5
|
|
|
|
|
// (we add abs max shift |-5| to each element, to align top to 0)
|
2013-08-19 19:54:49 +04:00
|
|
|
// NOTE: this will not set .current
|
|
|
|
|
// NOTE: there should not be any gid collisions between data sets.
|
2013-08-18 21:19:23 +04:00
|
|
|
//
|
2013-08-19 19:54:49 +04:00
|
|
|
// XXX should we try and resolve gid collisions here??
|
|
|
|
|
// ...don't think so...
|
2013-08-23 02:39:50 +04:00
|
|
|
// XXX should we check the data version???
|
2013-08-19 19:47:29 +04:00
|
|
|
// XXX needs testing...
|
2013-08-19 19:24:47 +04:00
|
|
|
function mergeData(a, b){
|
|
|
|
|
var order = []
|
|
|
|
|
var ribbon_sets = []
|
|
|
|
|
var shifts = []
|
2013-08-19 19:47:29 +04:00
|
|
|
var shift = 0
|
2013-08-19 19:24:47 +04:00
|
|
|
|
2013-08-19 19:47:29 +04:00
|
|
|
// prepare the data...
|
|
|
|
|
// build the ribbon_set, shifts, accumulate order and set shift bounds...
|
2013-08-19 19:24:47 +04:00
|
|
|
$.each(arguments, function(_, d){
|
|
|
|
|
if(typeof(d) == typeof([]) && d.constructor.name == 'Array'){
|
2013-08-19 19:47:29 +04:00
|
|
|
// process the shift...
|
2013-08-19 19:24:47 +04:00
|
|
|
var s = d[0]
|
|
|
|
|
shifts.push(s)
|
|
|
|
|
// NOTE: min shift (max negative shift) is needed so as to
|
|
|
|
|
// calculate the actual padding per each aligned ribbon
|
|
|
|
|
// set in the resulting structure...
|
2013-08-19 19:47:29 +04:00
|
|
|
shift = Math.min(s, shift)
|
|
|
|
|
// get the actual data...
|
|
|
|
|
d = d[1]
|
2013-08-19 19:24:47 +04:00
|
|
|
|
|
|
|
|
} else {
|
2013-08-19 19:47:29 +04:00
|
|
|
// default shift...
|
2013-08-19 19:24:47 +04:00
|
|
|
shifts.push(0)
|
|
|
|
|
}
|
|
|
|
|
ribbon_sets.push(d.ribbons)
|
|
|
|
|
order = order.concat(d.order)
|
|
|
|
|
})
|
2013-08-19 19:47:29 +04:00
|
|
|
shift = Math.abs(shift)
|
2013-08-19 19:24:47 +04:00
|
|
|
|
2013-08-19 19:47:29 +04:00
|
|
|
// normalize ribbon_set...
|
2013-08-19 19:24:47 +04:00
|
|
|
// NOTE: this will shift the ribbons to the required alignment...
|
|
|
|
|
$.each(shifts, function(i, s){
|
2013-08-19 19:47:29 +04:00
|
|
|
if(shift + s != 0){
|
|
|
|
|
ribbon_sets[i] = new Array(shift + s).concat(ribbon_sets[i])
|
|
|
|
|
}
|
2013-08-19 19:24:47 +04:00
|
|
|
})
|
|
|
|
|
|
2013-08-18 21:19:23 +04:00
|
|
|
return {
|
|
|
|
|
version: '2.0',
|
|
|
|
|
current: null,
|
2013-08-19 19:24:47 +04:00
|
|
|
ribbons: concatZip.apply(null, ribbon_sets),
|
|
|
|
|
order: order,
|
2013-08-18 21:19:23 +04:00
|
|
|
image_file: null
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2013-08-23 02:39:50 +04:00
|
|
|
// Split the given data at gid1[, gid2[, ...]]
|
|
|
|
|
//
|
|
|
|
|
// This will return a list of data objects, each containing gids that
|
|
|
|
|
// are later than gidN and earlier or the same as gidN+1, preserving the
|
|
|
|
|
// ribbon structure.
|
|
|
|
|
//
|
|
|
|
|
// NOTE: if a given object does not contain any gid in ribbon N then that
|
|
|
|
|
// ribbon will be represented by an empty list.
|
|
|
|
|
// NOTE: the above makes the data objects not compatible with anything that
|
|
|
|
|
// expects the ribbon to have at least one gid.
|
|
|
|
|
// NOTE: this takes one or more gids.
|
|
|
|
|
// NOTE: this will not set .current fields.
|
|
|
|
|
// NOTE: this is the opposite of mergeData():
|
|
|
|
|
// mergeData(splitData(data, ...)) == data
|
|
|
|
|
// with the exception of .current
|
2013-08-23 03:28:43 +04:00
|
|
|
// NOTE: this will ALWAYS return n+1 sections for n gids, even though
|
|
|
|
|
// some of them may be empty...
|
2013-08-23 02:39:50 +04:00
|
|
|
//
|
2013-08-23 00:33:23 +04:00
|
|
|
// XXX this is a bit brain-dead at the moment...
|
2013-08-23 02:28:28 +04:00
|
|
|
// XXX do we need to check if supplied gids exist in data???
|
2013-08-23 00:33:23 +04:00
|
|
|
function splitData(data, gid1){
|
|
|
|
|
var gids = []
|
|
|
|
|
var res = []
|
|
|
|
|
var cur = 0
|
|
|
|
|
|
|
|
|
|
// build the resulting data objects...
|
|
|
|
|
// XXX revise...
|
|
|
|
|
for(var i=1; i<arguments.length; i++){
|
|
|
|
|
var prev = cur
|
|
|
|
|
cur = data.order.indexOf(arguments[i])
|
|
|
|
|
gids.push(arguments[i])
|
|
|
|
|
|
|
|
|
|
res.push({
|
|
|
|
|
version: '2.0',
|
|
|
|
|
current: null,
|
|
|
|
|
ribbons: [],
|
|
|
|
|
order: data.order.slice(prev, cur),
|
|
|
|
|
image_file: null
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
// tail section...
|
|
|
|
|
res.push({
|
|
|
|
|
version: '2.0',
|
|
|
|
|
current: null,
|
|
|
|
|
ribbons: [],
|
|
|
|
|
order: data.order.slice(cur),
|
|
|
|
|
image_file: null
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
// split the ribbons...
|
|
|
|
|
for(var i=0; i<data.ribbons.length; i++){
|
|
|
|
|
var r = data.ribbons[i]
|
|
|
|
|
var cur = 0
|
|
|
|
|
|
|
|
|
|
// get all split positions...
|
|
|
|
|
// XXX revise...
|
|
|
|
|
for(var j=0; j<gids.length; j++){
|
|
|
|
|
var prev = cur
|
2013-08-23 04:20:47 +04:00
|
|
|
var gid = getGIDBefore(gids[j], i, null, data)
|
|
|
|
|
if(gid == gids[j]){
|
|
|
|
|
var cur = r.indexOf(gid)
|
|
|
|
|
} else {
|
|
|
|
|
var cur = r.indexOf(gid) + 1
|
|
|
|
|
}
|
2013-08-23 00:33:23 +04:00
|
|
|
|
|
|
|
|
// split and save the section to the corresponding data object...
|
|
|
|
|
res[j].ribbons.push(r.slice(prev, cur))
|
|
|
|
|
}
|
|
|
|
|
// tail section...
|
2013-08-23 02:28:28 +04:00
|
|
|
res[j].ribbons.push(r.slice(cur))
|
2013-08-23 00:33:23 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return res
|
2013-08-18 21:19:23 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2013-09-08 18:04:44 +04:00
|
|
|
// Align a section of data to the base ribbon.
|
|
|
|
|
//
|
|
|
|
|
// The data will be "cut" vertically from start gid (inclusive) up until
|
|
|
|
|
// end the gid (non-inclusive), if given.
|
|
|
|
|
//
|
|
|
|
|
// If neither start and/or end gids are given then the ribbons above the
|
|
|
|
|
// base ribbon will be used to set the start and end.
|
|
|
|
|
//
|
|
|
|
|
// This will return a new data object, without modifying the original.
|
2013-08-23 03:09:45 +04:00
|
|
|
//
|
2013-08-23 05:47:55 +04:00
|
|
|
//
|
|
|
|
|
// Illustration of operation:
|
|
|
|
|
// 1) Initial state, locate bounds...
|
2013-08-23 03:09:45 +04:00
|
|
|
//
|
2013-09-08 18:04:44 +04:00
|
|
|
// start ---+ +--- end
|
|
|
|
|
// v v
|
2013-08-23 03:09:45 +04:00
|
|
|
// | oooooooooooo |
|
2013-08-23 05:47:55 +04:00
|
|
|
// ...ooooooooo|ooooooooooooooooooo|ooooooooooooooooo... < base
|
2013-08-23 03:09:45 +04:00
|
|
|
// oooo|oooooooooooooooooooooooo|ooooooo
|
|
|
|
|
//
|
|
|
|
|
// The sections are split by precedence relative to the first and
|
|
|
|
|
// last elements of the ribbon above the current...
|
|
|
|
|
// i.e. the first section contains all the elements less than the
|
|
|
|
|
// first, the third is greater than the last, and the mid-section
|
|
|
|
|
// contains all elements that are in-between (inclusive).
|
|
|
|
|
//
|
|
|
|
|
//
|
|
|
|
|
// 2) Split and realign sections...
|
|
|
|
|
//
|
2013-08-23 05:47:55 +04:00
|
|
|
// ...ooooooooo| oooooooooooo |ooooooooooooooooo... < base
|
2013-08-23 03:09:45 +04:00
|
|
|
// oooo| ooooooooooooooooooo |ooooooo
|
|
|
|
|
// |oooooooooooooooooooooooo|
|
|
|
|
|
//
|
|
|
|
|
// The central section is shifted down (dropped), by 1 in this case.
|
|
|
|
|
//
|
|
|
|
|
//
|
|
|
|
|
// 3) Merge...
|
|
|
|
|
//
|
2013-08-23 05:47:55 +04:00
|
|
|
// ...ooooooooo|oooooooooooo|oooooooooooooooooooooooo... < base
|
2013-08-23 03:09:45 +04:00
|
|
|
// oooo|ooooooooooooooooooo|ooooooo
|
|
|
|
|
// |oooooooooooooooooooooooo|
|
|
|
|
|
//
|
|
|
|
|
//
|
2013-09-08 18:04:44 +04:00
|
|
|
// NOTE: the ends of the set may get "messed up" unless explicitly marked.
|
|
|
|
|
// ...the first/last several images in the base ribbon (if present)
|
|
|
|
|
// will get shifted to the top.
|
|
|
|
|
// NOTE: setting the start/end to the first/last images of the set will
|
|
|
|
|
// effectively just change the base ribbon w.o. affecting any data.
|
|
|
|
|
// XXX test this!!!
|
|
|
|
|
// XXX does this require a faster short path (special case)?
|
|
|
|
|
//
|
2013-08-23 03:28:43 +04:00
|
|
|
//
|
2013-09-06 03:48:15 +04:00
|
|
|
// XXX for this to be "smart" we need to introduce a concept of a
|
|
|
|
|
// "base ribbon" (default ribbon to align to) and supporting API...
|
2013-08-23 05:43:55 +04:00
|
|
|
// XXX figure out a way to accomplish one of (in order of preference):
|
|
|
|
|
// - auto-call this and make it expected and transparent to the user
|
|
|
|
|
// - manually called in *obvious* situations...
|
2013-09-08 18:04:44 +04:00
|
|
|
function alignDataToRibbon(base_ribbon, data, start, end){
|
2013-09-27 21:45:31 +04:00
|
|
|
// XXX get base ribbon...
|
|
|
|
|
base_ribbon = base_ribbon == null ? getBaseRibbonIndex() : base_ribbon
|
2013-08-23 03:28:43 +04:00
|
|
|
data = data == null ? DATA : data
|
|
|
|
|
|
2013-09-06 03:16:48 +04:00
|
|
|
// get the first and last elements of the ribbon-set above the base
|
|
|
|
|
// ribbon...
|
2013-09-08 18:04:44 +04:00
|
|
|
if(start == null || end == null){
|
|
|
|
|
var r = []
|
|
|
|
|
for(var i=0; i < base_ribbon; i++){
|
|
|
|
|
r.push(data.ribbons[i][0])
|
|
|
|
|
r.push(data.ribbons[i][data.ribbons[i].length-1])
|
|
|
|
|
}
|
|
|
|
|
r.sort(function(a, b){return imageOrderCmp(a, b, null, data)})
|
|
|
|
|
}
|
|
|
|
|
start = start == null ? r[0] : start
|
|
|
|
|
if(end == null){
|
|
|
|
|
end = r[r.length-1]
|
|
|
|
|
// get the gid after the end...
|
|
|
|
|
// NOTE: this can be null/undefined if we are looking at the last
|
|
|
|
|
// element...
|
|
|
|
|
end = data.order[data.order.indexOf(end)+1]
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// NOTE: will this always return 3 sections (see docs), even if
|
|
|
|
|
// start and/or end are null...
|
2013-08-23 03:28:43 +04:00
|
|
|
var sections = splitData(data, start, end)
|
|
|
|
|
|
|
|
|
|
// prepare to align...
|
2013-09-08 18:04:44 +04:00
|
|
|
sections[1] = [ base_ribbon, sections[1] ]
|
2013-08-23 03:28:43 +04:00
|
|
|
|
|
|
|
|
var res = mergeData.apply(null, sections)
|
|
|
|
|
res.current = data.current
|
2013-08-23 04:20:47 +04:00
|
|
|
|
2013-09-06 03:16:48 +04:00
|
|
|
dropEmptyRibbons(res)
|
2013-08-23 05:43:55 +04:00
|
|
|
|
|
|
|
|
return res
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2013-08-23 05:47:55 +04:00
|
|
|
// Shift a section of ribbons n positions.
|
2013-08-23 05:43:55 +04:00
|
|
|
//
|
2013-08-23 05:47:55 +04:00
|
|
|
// Illustration of operation:
|
|
|
|
|
// 1) Initial state, X is the current image...
|
2013-08-23 05:43:55 +04:00
|
|
|
//
|
|
|
|
|
// oooooo|oooo
|
|
|
|
|
// oooooooooo|Xoooooooooo
|
|
|
|
|
// oooooooooooooo|oooooooooooooooo
|
|
|
|
|
//
|
|
|
|
|
//
|
2013-08-23 05:47:55 +04:00
|
|
|
// 2) shiftRibbons(X, n) with positive n (shift down)
|
2013-08-23 05:43:55 +04:00
|
|
|
//
|
|
|
|
|
// oooooo|
|
|
|
|
|
// oooooooooo|oooo
|
|
|
|
|
// oooooooooooooo|Xoooooooooo
|
|
|
|
|
// |oooooooooooooooo
|
|
|
|
|
//
|
|
|
|
|
//
|
2013-08-23 05:47:55 +04:00
|
|
|
// 3) shiftRibbons(X, n) with negative n (shift up)
|
2013-08-23 05:43:55 +04:00
|
|
|
//
|
|
|
|
|
// |oooo
|
|
|
|
|
// oooooo|Xoooooooooo
|
|
|
|
|
// oooooooooo|oooooooooooooooo
|
|
|
|
|
// oooooooooooooo|
|
|
|
|
|
//
|
2013-08-23 05:47:55 +04:00
|
|
|
//
|
2013-08-23 05:43:55 +04:00
|
|
|
// XXX needs testing...
|
|
|
|
|
// XXX should this modify the view in place (and reload?)???
|
2013-08-23 05:47:55 +04:00
|
|
|
// XXX this and alignDataToRibbon(...) share a lot of code, split into
|
|
|
|
|
// two generations...
|
2013-08-23 05:43:55 +04:00
|
|
|
function shiftRibbonsBy(n, gid, data){
|
|
|
|
|
gid = gid == null ? getImageGID() : gid
|
|
|
|
|
data = data == null ? DATA : data
|
|
|
|
|
|
|
|
|
|
var sections = splitData(data, gid)
|
|
|
|
|
|
|
|
|
|
// prepare to align...
|
|
|
|
|
sections[1] = [ n, sections[1] ]
|
|
|
|
|
|
|
|
|
|
var res = mergeData.apply(null, sections)
|
|
|
|
|
res.current = data.current
|
|
|
|
|
|
2013-09-06 03:16:48 +04:00
|
|
|
dropEmptyRibbons(res)
|
2013-08-23 04:20:47 +04:00
|
|
|
|
2013-08-23 03:28:43 +04:00
|
|
|
return res
|
2013-08-23 03:09:45 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2013-05-28 19:25:13 +04:00
|
|
|
|
2013-05-28 02:17:24 +04:00
|
|
|
/**********************************************************************
|
|
|
|
|
* Format conversion
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
// Convert legacy Gen1 data format to current Gen3 (v2.0)
|
|
|
|
|
function convertDataGen1(data, cmp){
|
|
|
|
|
var res = {
|
|
|
|
|
data: {
|
|
|
|
|
version: '2.0',
|
|
|
|
|
current: null,
|
|
|
|
|
ribbons: [],
|
|
|
|
|
order: [],
|
|
|
|
|
},
|
|
|
|
|
images: {}
|
|
|
|
|
}
|
|
|
|
|
cmp = cmp == null ?
|
|
|
|
|
function(a, b){
|
2013-06-15 20:08:23 +04:00
|
|
|
return imageDateCmp(a, b, null, res.images)
|
2013-05-28 02:17:24 +04:00
|
|
|
}
|
|
|
|
|
: cmp
|
|
|
|
|
var ribbons = res.data.ribbons
|
|
|
|
|
var order = res.data.order
|
|
|
|
|
var images = res.images
|
|
|
|
|
|
|
|
|
|
// position...
|
|
|
|
|
res.data.current = data.position
|
|
|
|
|
|
|
|
|
|
// ribbons and images...
|
|
|
|
|
$.each(data.ribbons, function(i, input_images){
|
|
|
|
|
var ribbon = []
|
|
|
|
|
ribbons.push(ribbon)
|
|
|
|
|
for(var id in input_images){
|
|
|
|
|
var image = input_images[id]
|
|
|
|
|
ribbon.push(id)
|
|
|
|
|
order.push(id)
|
|
|
|
|
images[id] = image
|
|
|
|
|
}
|
|
|
|
|
ribbon.sort(cmp)
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
order.sort(cmp)
|
|
|
|
|
|
|
|
|
|
// XXX STUB
|
|
|
|
|
res.data.current = order[0]
|
|
|
|
|
|
|
|
|
|
return res
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-05-13 02:31:09 +04:00
|
|
|
/**********************************************************************
|
|
|
|
|
* Loaders
|
|
|
|
|
*/
|
|
|
|
|
|
2013-12-04 23:18:23 +04:00
|
|
|
|
2013-12-02 17:45:54 +04:00
|
|
|
function updateImageIndicators(gid, image){
|
|
|
|
|
gid = gid == null ? getImageGID() : gid
|
|
|
|
|
image = image == null ? getImage() : $(image)
|
|
|
|
|
|
|
|
|
|
// marks...
|
|
|
|
|
if(MARKED.indexOf(gid) != -1){
|
|
|
|
|
image.addClass('marked')
|
2013-12-04 23:18:23 +04:00
|
|
|
// XXX
|
|
|
|
|
_addMark('selected', gid, image)
|
2013-12-02 17:45:54 +04:00
|
|
|
} else {
|
|
|
|
|
image.removeClass('marked')
|
2013-12-04 23:18:23 +04:00
|
|
|
// XXX
|
|
|
|
|
_removeMark('selected', gid, image)
|
2013-12-02 17:45:54 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return image
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2013-12-02 04:22:57 +04:00
|
|
|
// helper...
|
2013-12-02 17:45:54 +04:00
|
|
|
function _loadImagePreviewURL(image, url){
|
2013-12-02 04:22:57 +04:00
|
|
|
// pre-cache and load image...
|
|
|
|
|
// NOTE: this will make images load without a blackout...
|
|
|
|
|
var img = new Image()
|
|
|
|
|
img.onload = function(){
|
|
|
|
|
image.css({
|
|
|
|
|
'background-image': 'url("'+ url +'")',
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
img.src = url
|
|
|
|
|
return img
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2013-06-02 23:07:18 +04:00
|
|
|
// Update an image element
|
2013-06-04 23:32:36 +04:00
|
|
|
//
|
|
|
|
|
// NOTE: care must be taken to reset ALL attributes an image can have,
|
|
|
|
|
// a common bug if this is not done correctly, is that some settings
|
|
|
|
|
// may leak to newly loaded images...
|
2013-06-15 02:45:19 +04:00
|
|
|
// XXX do a pre-caching framework...
|
2013-12-02 04:22:57 +04:00
|
|
|
function updateImage(image, gid, size, sync){
|
2013-12-04 23:18:23 +04:00
|
|
|
image = image == null ? getImage() : $(image)
|
2013-12-02 04:22:57 +04:00
|
|
|
sync = sync == null ? SYNC_IMG_LOADER : sync
|
2013-05-31 20:37:46 +04:00
|
|
|
var oldgid = getImageGID(image)
|
|
|
|
|
|
|
|
|
|
if(oldgid == gid || gid == null){
|
2013-06-14 20:47:37 +04:00
|
|
|
gid = oldgid
|
2013-05-31 20:37:46 +04:00
|
|
|
|
2013-05-13 02:24:36 +04:00
|
|
|
} else {
|
2013-12-04 23:18:23 +04:00
|
|
|
// remove old marks...
|
|
|
|
|
if(typeof(oldgid) == typeof('str')){
|
|
|
|
|
getImageMarks(oldgid).remove()
|
|
|
|
|
}
|
|
|
|
|
// reset gid...
|
2013-05-31 20:37:46 +04:00
|
|
|
image
|
|
|
|
|
.attr('gid', JSON.stringify(gid))
|
|
|
|
|
.css({
|
|
|
|
|
// clear the old preview...
|
2013-06-01 18:21:48 +04:00
|
|
|
'background-image': '',
|
2013-05-31 20:37:46 +04:00
|
|
|
})
|
2013-05-13 02:24:36 +04:00
|
|
|
}
|
2013-05-17 16:21:03 +04:00
|
|
|
size = size == null ? getVisibleImageSize('max') : size
|
2013-05-13 02:24:36 +04:00
|
|
|
|
2013-05-30 03:26:49 +04:00
|
|
|
// get the image data...
|
2013-05-19 22:48:28 +04:00
|
|
|
var img_data = IMAGES[gid]
|
2013-05-14 21:49:05 +04:00
|
|
|
if(img_data == null){
|
|
|
|
|
img_data = STUB_IMAGE_DATA
|
|
|
|
|
}
|
2013-05-13 02:24:36 +04:00
|
|
|
|
2013-11-26 18:34:56 +04:00
|
|
|
/* XXX does not seem to be needing this...
|
2013-11-25 02:26:38 +04:00
|
|
|
// set the current class...
|
|
|
|
|
if(gid == DATA.current){
|
|
|
|
|
image.addClass('current')
|
|
|
|
|
} else {
|
|
|
|
|
image.removeClass('current')
|
|
|
|
|
}
|
|
|
|
|
*/
|
|
|
|
|
|
2013-05-30 03:26:49 +04:00
|
|
|
// preview...
|
2013-06-14 20:47:37 +04:00
|
|
|
var p_url = getBestPreview(gid, size).url
|
2013-12-02 04:22:57 +04:00
|
|
|
// sync load...
|
|
|
|
|
if(sync){
|
2013-12-02 17:45:54 +04:00
|
|
|
_loadImagePreviewURL(image, p_url)
|
2013-06-14 20:47:37 +04:00
|
|
|
|
2013-12-02 04:22:57 +04:00
|
|
|
// async load...
|
|
|
|
|
} else {
|
|
|
|
|
// NOTE: storing the url in .data() makes the image load the
|
2013-12-02 17:45:54 +04:00
|
|
|
// last requested preview and in a case when we manage to
|
|
|
|
|
// call updateImage(...) on the same element multiple times
|
|
|
|
|
// before the previews get loaded...
|
2013-12-02 04:22:57 +04:00
|
|
|
// ...setting the data().loading is sync while loading an
|
2013-12-02 17:45:54 +04:00
|
|
|
// image is not, and if several loads are done in sequence
|
2013-12-02 04:22:57 +04:00
|
|
|
// there is no guarantee that they will happen in the same
|
|
|
|
|
// order as requested...
|
|
|
|
|
image.data().loading = p_url
|
|
|
|
|
setTimeout(function(){
|
2013-12-02 17:45:54 +04:00
|
|
|
_loadImagePreviewURL(image, image.data().loading)
|
2013-12-02 04:22:57 +04:00
|
|
|
}, 0)
|
2013-05-31 20:37:46 +04:00
|
|
|
}
|
|
|
|
|
|
2013-06-01 18:21:48 +04:00
|
|
|
// main attrs...
|
2013-05-23 17:17:31 +04:00
|
|
|
image
|
|
|
|
|
.attr({
|
2013-05-24 17:08:16 +04:00
|
|
|
order: DATA.order.indexOf(gid),
|
2013-05-23 17:17:31 +04:00
|
|
|
orientation: img_data.orientation == null ? 0 : img_data.orientation,
|
|
|
|
|
})
|
2013-05-14 21:49:05 +04:00
|
|
|
|
2013-06-04 22:01:41 +04:00
|
|
|
// flip...
|
2013-06-04 23:29:44 +04:00
|
|
|
setImageFlipState(image, img_data.flipped == null ? [] : img_data.flipped)
|
2013-06-04 22:01:41 +04:00
|
|
|
|
2013-12-02 06:23:19 +04:00
|
|
|
// XXX filter settings...
|
|
|
|
|
// XXX
|
|
|
|
|
|
2013-11-25 05:12:01 +04:00
|
|
|
// NOTE: this only has effect on non-square image blocks...
|
|
|
|
|
correctImageProportionsForRotation(image)
|
|
|
|
|
|
2013-12-02 17:45:54 +04:00
|
|
|
// marks and other indicators...
|
|
|
|
|
updateImageIndicators(gid, image)
|
2013-05-24 17:08:16 +04:00
|
|
|
|
2013-05-14 21:49:05 +04:00
|
|
|
return image
|
2013-05-13 02:24:36 +04:00
|
|
|
}
|
|
|
|
|
|
2013-05-13 02:31:09 +04:00
|
|
|
|
2013-05-28 21:59:38 +04:00
|
|
|
// Same as updateImage(...) but will update all images.
|
2013-05-31 20:10:23 +04:00
|
|
|
//
|
|
|
|
|
// NOTE: this will prioritize images by distance from current image...
|
|
|
|
|
//
|
|
|
|
|
// XXX need to run this in the background...
|
|
|
|
|
function updateImages(size, cmp){
|
|
|
|
|
var deferred = $.Deferred()
|
|
|
|
|
|
|
|
|
|
function _worker(){
|
|
|
|
|
size = size == null ? getVisibleImageSize('max') : size
|
|
|
|
|
|
|
|
|
|
// sorted run...
|
|
|
|
|
if(UPDATE_SORT_ENABLED && cmp != false){
|
|
|
|
|
cmp = cmp == null ?
|
2013-09-25 17:00:54 +04:00
|
|
|
makeGIDDistanceCmp(getImageGID(), getImageGID)
|
2013-05-31 20:10:23 +04:00
|
|
|
// XXX this is more correct but is slow...
|
2013-09-25 17:00:54 +04:00
|
|
|
//makeGIDRibbonDistanceCmp(getImageGID(), getImageGID)
|
2013-05-31 20:10:23 +04:00
|
|
|
: cmp
|
|
|
|
|
deferred.resolve($('.image')
|
|
|
|
|
// sort images by distance from current, so as to update what
|
|
|
|
|
// the user is looking at first...
|
|
|
|
|
.sort(cmp)
|
|
|
|
|
.each(function(){
|
|
|
|
|
updateImage($(this), null, size)
|
|
|
|
|
}))
|
|
|
|
|
|
|
|
|
|
// do a fast run w.o. sorting images...
|
|
|
|
|
} else {
|
|
|
|
|
deferred.resolve($('.image')
|
|
|
|
|
.each(function(){
|
|
|
|
|
updateImage($(this), null, size)
|
|
|
|
|
}))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(UPDATE_SYNC){
|
|
|
|
|
_worker()
|
|
|
|
|
} else {
|
|
|
|
|
setTimeout(_worker, 0)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return deferred
|
2013-05-13 02:24:36 +04:00
|
|
|
}
|
|
|
|
|
|
2013-06-02 23:07:18 +04:00
|
|
|
|
2013-11-25 05:12:44 +04:00
|
|
|
/* XXX for some very odd reason this is slower than the monster above...
|
2013-05-31 20:37:46 +04:00
|
|
|
function updateImages(size){
|
|
|
|
|
size = size == null ? getVisibleImageSize('max') : size
|
|
|
|
|
return $('.image')
|
|
|
|
|
.each(function(){
|
|
|
|
|
updateImage($(this), null, size)
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
*/
|
|
|
|
|
|
2013-11-25 05:20:27 +04:00
|
|
|
|
|
|
|
|
// Get "count" of GIDs starting with a given gid ("from")
|
|
|
|
|
//
|
|
|
|
|
// NOTE: this will not include the 'from' GID in the resulting list,
|
|
|
|
|
// unless inclusive is set to true.
|
|
|
|
|
// NOTE: count can be either negative or positive, this will indicate
|
|
|
|
|
// load direction...
|
|
|
|
|
// NOTE: this can calculate the ribbon number where the image is located.
|
|
|
|
|
// NOTE: if an image can be in more than one ribbon, one MUST suply the
|
|
|
|
|
// correct ribbon number...
|
|
|
|
|
//
|
|
|
|
|
// XXX do we need more checking???
|
|
|
|
|
// XXX Race condition: when this is called while DATA is not yet fully
|
|
|
|
|
// loaded (old data), the from gid will not be present in
|
|
|
|
|
// DATA.ribbons...
|
2013-11-25 07:04:18 +04:00
|
|
|
function getGIDsAfter(count, gid, ribbon, inclusive, data){
|
2013-11-25 05:20:27 +04:00
|
|
|
if(count == 0){
|
|
|
|
|
return []
|
|
|
|
|
}
|
2013-11-25 07:04:18 +04:00
|
|
|
// default values...
|
|
|
|
|
gid = gid == null ? getImageGID() : gid
|
|
|
|
|
data = data == null ? DATA : data
|
|
|
|
|
ribbon = ribbon == null ? getRibbonIndex() : ribbon
|
|
|
|
|
count = count == null ? Math.round(LOAD_SCREENS * getScreenWidthInImages()) : count
|
|
|
|
|
|
2013-11-25 05:20:27 +04:00
|
|
|
// ribbon default value...
|
|
|
|
|
// XXX Race condition: if DATA is not yet loaded this can return
|
|
|
|
|
// ribbon == null...
|
|
|
|
|
if(ribbon == null){
|
2013-11-25 07:04:18 +04:00
|
|
|
$(data.ribbons).each(function(i, e){
|
|
|
|
|
if(e.indexOf(gid) >= 0){
|
2013-11-25 05:20:27 +04:00
|
|
|
ribbon = i
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
2013-11-25 07:04:18 +04:00
|
|
|
ribbon = data.ribbons[ribbon]
|
2013-11-25 05:20:27 +04:00
|
|
|
|
|
|
|
|
// ribbon this is empty or non-existant...
|
|
|
|
|
// XXX need to check when can we get a ribbon == undefined case...
|
|
|
|
|
// ...race?
|
|
|
|
|
//if(ribbon == null){
|
|
|
|
|
// // XXX
|
|
|
|
|
//}
|
|
|
|
|
if(ribbon == null || ribbon.length == 0){
|
|
|
|
|
return []
|
|
|
|
|
}
|
|
|
|
|
if(count > 0){
|
|
|
|
|
var c = inclusive == null ? 1 : 0
|
2013-11-25 07:04:18 +04:00
|
|
|
var start = ribbon.indexOf(gid) + c
|
2013-11-25 05:20:27 +04:00
|
|
|
return ribbon.slice(start, start + count)
|
|
|
|
|
} else {
|
|
|
|
|
var c = inclusive == null ? 0 : 1
|
2013-11-25 07:04:18 +04:00
|
|
|
var end = ribbon.indexOf(gid)
|
2013-11-25 05:20:27 +04:00
|
|
|
return ribbon.slice((Math.abs(count) >= end ? 0 : end + count + c), end + c)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2013-11-16 02:22:21 +04:00
|
|
|
// Get a sub-ribbon of count elements around a given gid
|
|
|
|
|
//
|
|
|
|
|
// +- ribbon count
|
|
|
|
|
// v |<------>|
|
|
|
|
|
// ooooooooooooooooXoooooooooooooooo -> ooooXoooo
|
|
|
|
|
// ^
|
|
|
|
|
// gid
|
|
|
|
|
//
|
|
|
|
|
// If gid does not exist in the requested ribbon then getGIDBefore() is
|
|
|
|
|
// used to get an appropriate alternative gid.
|
|
|
|
|
//
|
2013-12-06 17:09:02 +04:00
|
|
|
// If gid is less than count/2 to ribbon head/tail, then less than count
|
|
|
|
|
// gids will be returned
|
|
|
|
|
//
|
|
|
|
|
// count
|
|
|
|
|
// |<------>|
|
|
|
|
|
// oXoooooooooooooooo -> ___oXoooo
|
|
|
|
|
// ^
|
|
|
|
|
// gid
|
|
|
|
|
//
|
|
|
|
|
//
|
|
|
|
|
// Setting force_count will make this always return count images, even
|
|
|
|
|
// at the start and end of the ribbon.
|
|
|
|
|
//
|
|
|
|
|
// count
|
|
|
|
|
// |<------>|
|
|
|
|
|
// oXoooooooooooooooo -> oXooooooo
|
|
|
|
|
// ^
|
|
|
|
|
// gid
|
|
|
|
|
//
|
|
|
|
|
// Otherwise this will return less.
|
|
|
|
|
//
|
2013-11-16 02:22:21 +04:00
|
|
|
// NOTE: skipping gid and ribbon while passing data may not work correctly...
|
|
|
|
|
// NOTE: count represents section diameter...
|
2013-12-06 17:09:02 +04:00
|
|
|
function getGIDsAround(count, gid, ribbon, data, force_count){
|
2013-11-25 07:04:18 +04:00
|
|
|
if(count == 0){
|
|
|
|
|
return []
|
|
|
|
|
}
|
2013-11-16 02:22:21 +04:00
|
|
|
// default values...
|
|
|
|
|
data = data == null ? DATA : data
|
|
|
|
|
gid = gid == null ? getImageGID() : gid
|
|
|
|
|
ribbon = ribbon == null ? getRibbonIndex() : ribbon
|
2013-12-06 17:09:02 +04:00
|
|
|
// XXX is this out of context here???
|
2013-11-16 02:22:21 +04:00
|
|
|
count = count == null ? Math.round(LOAD_SCREENS * getScreenWidthInImages()) : count
|
|
|
|
|
|
|
|
|
|
var ribbon_data = data.ribbons[ribbon]
|
|
|
|
|
// get a gid that's in the current ribbon...
|
|
|
|
|
gid = ribbon_data.indexOf(gid) < 0 ? getGIDBefore(gid, ribbon, null, data) : gid
|
|
|
|
|
|
|
|
|
|
// calculate the bounds...
|
|
|
|
|
var i = ribbon_data.indexOf(gid)
|
|
|
|
|
|
|
|
|
|
var start = i - Math.floor(count/2)
|
|
|
|
|
start = start < 0 ? 0 : start
|
|
|
|
|
|
|
|
|
|
var end = i + Math.ceil(count/2)
|
|
|
|
|
end = end > ribbon_data.length ? ribbon_data.length : end
|
|
|
|
|
|
2013-12-06 17:09:02 +04:00
|
|
|
// force count by extending the ribbon at the opposite end...
|
|
|
|
|
if(force_count && ribbon_data.length > count){
|
|
|
|
|
var d = count - (end - start)
|
|
|
|
|
|
|
|
|
|
start = end >= ribbon_data.length ? start - d : start
|
|
|
|
|
start = start < 0 ? 0 : start
|
|
|
|
|
|
|
|
|
|
end = start <= 0 ? end + d : end
|
|
|
|
|
end = end > ribbon_data.length ? ribbon_data.length : end
|
|
|
|
|
}
|
|
|
|
|
|
2013-11-16 02:22:21 +04:00
|
|
|
// get the actual data...
|
|
|
|
|
return ribbon_data.slice(start, end)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2013-11-19 06:00:26 +04:00
|
|
|
// NOTE: this expects that both arrays cleanly intersect each other only
|
2013-11-16 02:22:21 +04:00
|
|
|
// once...
|
2013-11-25 02:32:47 +04:00
|
|
|
// XXX this sometimes returns a null and a value which seems to be
|
|
|
|
|
// impossible...
|
|
|
|
|
// ...this does not affect anything, but still need to investigate...
|
2013-11-16 02:22:21 +04:00
|
|
|
function getCommonSubArrayOffsets(L1, L2){
|
|
|
|
|
var res = {}
|
|
|
|
|
|
2013-11-19 06:00:26 +04:00
|
|
|
// defaults for if one of the lists is empty...
|
|
|
|
|
if(L1.length == 0){
|
|
|
|
|
res.left = -(L2.length)
|
|
|
|
|
res.right = 0
|
|
|
|
|
return res
|
|
|
|
|
} else if(L2.length == 0){
|
|
|
|
|
res.left = L1.length
|
|
|
|
|
res.right = 0
|
|
|
|
|
return res
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// head...
|
2013-11-19 18:10:54 +04:00
|
|
|
var a = L2.indexOf(L1[0])
|
|
|
|
|
var b = L1.indexOf(L2[0])
|
2013-11-16 02:22:21 +04:00
|
|
|
res.left = a >= 0 ? -a
|
|
|
|
|
: b >= 0 ? b
|
|
|
|
|
: null
|
|
|
|
|
|
2013-11-19 06:00:26 +04:00
|
|
|
// tail...
|
2013-11-19 18:10:54 +04:00
|
|
|
a = L2.indexOf(L1[L1.length-1])
|
|
|
|
|
b = L1.indexOf(L2[L2.length-1])
|
2013-11-16 02:22:21 +04:00
|
|
|
res.right = a >= 0 ? -(L2.length - a - 1)
|
|
|
|
|
: b >= 0 ? L1.length - b - 1
|
|
|
|
|
: null
|
|
|
|
|
|
|
|
|
|
return res
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// NOTE: this expects that bot arrays cleanly intersect each other only
|
|
|
|
|
// once...
|
|
|
|
|
function getCommonSubArray(L1, L2){
|
|
|
|
|
var res = getCommonSubArrayOffsets(L1, L2)
|
|
|
|
|
var left = res.left
|
|
|
|
|
var right = res.right
|
|
|
|
|
|
|
|
|
|
if(left == null && right == null){
|
|
|
|
|
return []
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//a = L1.slice(Math.max(0, left), L1.length - Math.max(right, 0))
|
|
|
|
|
//b = L2.slice(Math.max(0, -left), L2.length - Math.max(-right, 0))
|
|
|
|
|
return L1.slice(Math.max(0, left), L1.length - Math.max(right, 0))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2013-11-25 03:01:56 +04:00
|
|
|
// Load count images around a given image/gid into the given ribbon.
|
|
|
|
|
//
|
2013-12-06 17:09:02 +04:00
|
|
|
function loadImagesAround(count, gid, ribbon, data, force_count){
|
2013-11-16 02:22:21 +04:00
|
|
|
// default values...
|
|
|
|
|
data = data == null ? DATA : data
|
|
|
|
|
ribbon = ribbon == null ? getRibbonIndex() : ribbon
|
2013-11-19 06:00:26 +04:00
|
|
|
ribbon = typeof(ribbon) != typeof(123) ? getRibbonIndex(ribbon) : ribbon
|
2013-11-16 02:22:21 +04:00
|
|
|
count = count == null ? Math.round(LOAD_SCREENS * getScreenWidthInImages()) : count
|
2013-11-25 02:32:47 +04:00
|
|
|
// get a gid that exists in the current ribbon...
|
2013-11-25 03:01:56 +04:00
|
|
|
gid = getGIDBefore(gid, ribbon, null, data)
|
2013-11-16 02:22:21 +04:00
|
|
|
|
2013-11-19 06:00:26 +04:00
|
|
|
var ribbon_elem = getRibbon(ribbon)
|
2013-11-16 02:22:21 +04:00
|
|
|
|
|
|
|
|
var old_ribbon = ribbon_elem
|
|
|
|
|
.find('.image')
|
|
|
|
|
.map(function(_, e){ return getImageGID(e) })
|
|
|
|
|
.toArray()
|
2013-12-06 17:09:02 +04:00
|
|
|
var new_ribbon = getGIDsAround(count, gid, ribbon, data, force_count)
|
2013-11-16 02:22:21 +04:00
|
|
|
|
|
|
|
|
// get the common sub-ribbon...
|
|
|
|
|
// NOTE: we are only interested in continuous sub-ribbons...
|
|
|
|
|
var res = getCommonSubArrayOffsets(new_ribbon, old_ribbon)
|
|
|
|
|
var left = res.left
|
|
|
|
|
var right = res.right
|
|
|
|
|
|
|
|
|
|
// special case: nothing to do...
|
|
|
|
|
if(left == 0 && right == 0){
|
|
|
|
|
return ribbon_elem.find('.image')
|
|
|
|
|
}
|
|
|
|
|
|
2013-11-21 00:13:07 +04:00
|
|
|
var size = getVisibleImageSize('max')
|
|
|
|
|
|
2013-11-19 18:10:54 +04:00
|
|
|
// no common sections, do a full reload...
|
2013-11-25 02:32:47 +04:00
|
|
|
// XXX NOTE: we use || instead of && here to compensate for an oddity
|
|
|
|
|
// in getCommonSubArrayOffsets(...), see it for further details...
|
2013-11-19 18:10:54 +04:00
|
|
|
if(left == null || right == null){
|
|
|
|
|
var n = new_ribbon.indexOf(gid)
|
|
|
|
|
var o = old_ribbon.indexOf(gid)
|
2013-11-21 00:13:07 +04:00
|
|
|
o = o < 0 ? n : o
|
2013-11-19 18:10:54 +04:00
|
|
|
|
2013-11-21 04:11:33 +04:00
|
|
|
// calculate offsets...
|
2013-11-21 00:13:07 +04:00
|
|
|
var left = n - o
|
|
|
|
|
var right = (new_ribbon.length - old_ribbon.length) - left
|
|
|
|
|
|
2013-11-19 18:10:54 +04:00
|
|
|
extendRibbon(left, right, ribbon_elem)
|
|
|
|
|
|
2013-11-21 04:11:33 +04:00
|
|
|
// update the images...
|
2013-11-21 00:13:07 +04:00
|
|
|
ribbon_elem.find('.image')
|
|
|
|
|
.each(function(i, e){
|
|
|
|
|
updateImage(e, new_ribbon[i], size)
|
|
|
|
|
})
|
|
|
|
|
var updated = new_ribbon.length
|
2013-11-16 02:22:21 +04:00
|
|
|
|
2013-11-19 18:10:54 +04:00
|
|
|
// partial reload...
|
2013-11-16 02:22:21 +04:00
|
|
|
} else {
|
2013-11-19 18:10:54 +04:00
|
|
|
var res = extendRibbon(left, right, ribbon_elem)
|
2013-11-16 02:22:21 +04:00
|
|
|
// XXX this will get all the current images, not the resulting ones...
|
|
|
|
|
var images = ribbon_elem.find('.image')
|
2013-11-21 00:13:07 +04:00
|
|
|
var updated = 0
|
2013-11-16 02:22:21 +04:00
|
|
|
|
2013-11-21 00:13:07 +04:00
|
|
|
// update the images...
|
|
|
|
|
res.left.each(function(i, e){
|
|
|
|
|
updateImage(e, new_ribbon[i], size)
|
|
|
|
|
updated++
|
|
|
|
|
})
|
|
|
|
|
var l = res.right.length
|
|
|
|
|
res.right.each(function(i, e){
|
|
|
|
|
updateImage(e, new_ribbon[new_ribbon.length-l+i], size)
|
|
|
|
|
updated++
|
|
|
|
|
})
|
|
|
|
|
}
|
2013-11-16 02:22:21 +04:00
|
|
|
|
2013-11-19 06:00:26 +04:00
|
|
|
if(updated > 0){
|
|
|
|
|
$('.viewer').trigger('updatedRibbon', [ribbon_elem])
|
|
|
|
|
}
|
|
|
|
|
|
2013-11-16 02:22:21 +04:00
|
|
|
return images
|
|
|
|
|
}
|
|
|
|
|
|
2013-05-13 02:24:36 +04:00
|
|
|
|
2013-05-28 02:17:24 +04:00
|
|
|
// Roll ribbon and load new images in the updated section.
|
|
|
|
|
//
|
2013-05-13 02:24:36 +04:00
|
|
|
// NOTE: this is signature-compatible with rollRibbon...
|
|
|
|
|
// NOTE: this will load data ONLY if it is available, otherwise this
|
|
|
|
|
// will have no effect...
|
|
|
|
|
// NOTE: this can roll past the currently loaded images (n > images.length)
|
2013-05-14 00:01:03 +04:00
|
|
|
function rollImages(n, ribbon, extend, no_compensate_shift){
|
2013-05-13 02:24:36 +04:00
|
|
|
if(n == 0){
|
|
|
|
|
return $([])
|
|
|
|
|
}
|
2013-11-25 04:12:40 +04:00
|
|
|
var r = typeof(ribbon) == typeof(123) ? ribbon : null
|
|
|
|
|
ribbon = ribbon == null ? getRibbon()
|
|
|
|
|
: r != null ? getRibbon(ribbon)
|
|
|
|
|
: $(ribbon)
|
|
|
|
|
var r = r == null ? getRibbonIndex(ribbon) : r
|
2013-05-13 02:24:36 +04:00
|
|
|
var images = ribbon.find('.image')
|
|
|
|
|
|
2013-05-14 21:49:05 +04:00
|
|
|
var from = n > 0 ? getImageGID(ribbon.find('.image').last())
|
|
|
|
|
: getImageGID(ribbon.find('.image').first())
|
2013-11-25 05:22:31 +04:00
|
|
|
var gids = getGIDsAfter(n, from, r)
|
2013-05-13 02:24:36 +04:00
|
|
|
if(gids.length == 0){
|
|
|
|
|
return $([])
|
|
|
|
|
}
|
2013-11-25 02:26:38 +04:00
|
|
|
var l = gids.length
|
2013-05-13 02:24:36 +04:00
|
|
|
// truncate the results to the length of images...
|
2013-11-25 02:26:38 +04:00
|
|
|
if(n > 0 && l > images.length){
|
2013-05-13 02:24:36 +04:00
|
|
|
gids.reverse().splice(images.length)
|
|
|
|
|
gids.reverse()
|
2013-11-25 02:26:38 +04:00
|
|
|
} else if(l > images.length){
|
2013-05-13 02:24:36 +04:00
|
|
|
gids.splice(images.length)
|
|
|
|
|
}
|
2013-11-25 02:26:38 +04:00
|
|
|
l = gids.length
|
2013-05-13 02:24:36 +04:00
|
|
|
|
2013-11-25 02:26:38 +04:00
|
|
|
if(l < images.length){
|
|
|
|
|
images = rollRibbon(l * (n > 0 ? 1 : -1), ribbon, extend, no_compensate_shift)
|
2013-05-13 02:24:36 +04:00
|
|
|
}
|
|
|
|
|
|
2013-05-17 16:21:03 +04:00
|
|
|
var size = getVisibleImageSize('max')
|
2013-05-13 02:24:36 +04:00
|
|
|
images.each(function(i, e){
|
|
|
|
|
updateImage($(e), gids[i], size)
|
|
|
|
|
})
|
|
|
|
|
|
2013-05-17 01:04:20 +04:00
|
|
|
$('.viewer').trigger('updatedRibbon', [ribbon])
|
|
|
|
|
|
2013-05-13 02:24:36 +04:00
|
|
|
return images
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2013-06-02 23:07:18 +04:00
|
|
|
// Reload the viewer using the current DATA and IMAGES objects
|
2013-06-02 20:14:39 +04:00
|
|
|
function reloadViewer(images_per_screen){
|
2013-05-13 02:31:09 +04:00
|
|
|
var ribbons_set = $('.ribbon-set')
|
2013-05-19 22:48:28 +04:00
|
|
|
var current = DATA.current
|
2013-05-13 02:31:09 +04:00
|
|
|
// if no width is given, use the current or default...
|
|
|
|
|
var w = images_per_screen == null ? getScreenWidthInImages() : images_per_screen
|
|
|
|
|
w = w > MAX_SCREEN_IMAGES ? DEFAULT_SCREEN_IMAGES : w
|
|
|
|
|
|
|
|
|
|
// clear data...
|
|
|
|
|
$('.ribbon').remove()
|
|
|
|
|
|
|
|
|
|
// create ribbons...
|
2013-05-19 22:48:28 +04:00
|
|
|
$.each(DATA.ribbons, function(i, e){
|
2013-05-13 02:31:09 +04:00
|
|
|
createRibbon().appendTo(ribbons_set)
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
// create images...
|
|
|
|
|
$('.ribbon').each(function(i, e){
|
2013-11-19 06:00:26 +04:00
|
|
|
loadImagesAround(Math.round(w * LOAD_SCREENS), current, i)
|
2013-05-13 02:31:09 +04:00
|
|
|
})
|
|
|
|
|
|
2013-11-25 03:01:56 +04:00
|
|
|
focusImage(getImage(current))
|
2013-05-13 02:31:09 +04:00
|
|
|
|
|
|
|
|
fitNImages(w)
|
|
|
|
|
centerRibbons('css')
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2013-06-02 23:07:18 +04:00
|
|
|
// Apply the current SETTINGS to current viewer
|
2013-05-24 00:32:42 +04:00
|
|
|
function loadSettings(){
|
2013-07-03 20:18:00 +04:00
|
|
|
toggleTheme(SETTINGS['global-theme'])
|
2013-05-24 00:32:42 +04:00
|
|
|
|
|
|
|
|
if(toggleSingleImageMode('?') == 'on'){
|
2013-07-03 20:18:00 +04:00
|
|
|
var w = SETTINGS['single-image-mode-screen-images']
|
2013-06-08 21:32:22 +04:00
|
|
|
if(window.PROPORTIONS_RATIO_THRESHOLD == null){
|
|
|
|
|
var p = SETTINGS['single-image-mode-proportions']
|
|
|
|
|
toggleImageProportions(p)
|
|
|
|
|
}
|
2013-05-24 00:32:42 +04:00
|
|
|
} else {
|
2013-07-03 20:18:00 +04:00
|
|
|
var w = SETTINGS['ribbon-mode-screen-images']
|
|
|
|
|
toggleImageInfo(SETTINGS['ribbon-mode-image-info'] == 'on' ? 'on' : 'off')
|
2013-05-24 00:32:42 +04:00
|
|
|
}
|
|
|
|
|
fitNImages(w)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2013-05-13 02:24:36 +04:00
|
|
|
|
2013-05-28 02:17:24 +04:00
|
|
|
/**********************************************************************
|
|
|
|
|
* Image caching...
|
|
|
|
|
*/
|
|
|
|
|
|
2013-06-04 23:32:36 +04:00
|
|
|
// TODO add global cache...
|
|
|
|
|
// - manage cache by number and preview size...
|
|
|
|
|
// - keep in biggish...
|
|
|
|
|
|
|
|
|
|
|
2013-05-28 02:17:24 +04:00
|
|
|
// NOTE: this will always overwrite the previous cache set for a ribbon...
|
2013-06-01 18:21:48 +04:00
|
|
|
// NOTE: it appears that sorting images by priority before loading them
|
2013-06-14 13:16:27 +04:00
|
|
|
// to cache has little or no effect on the order they are
|
2013-06-01 18:21:48 +04:00
|
|
|
// loaded/rendered...
|
2013-12-06 17:09:02 +04:00
|
|
|
// NOTE: this is not meant to be a real cache, rather a que for the OS and
|
|
|
|
|
// backend/webkit on what's next...
|
2013-05-28 02:17:24 +04:00
|
|
|
function preCacheRibbonImages(ribbon){
|
2013-12-06 17:09:02 +04:00
|
|
|
var deferred = $.Deferred()
|
|
|
|
|
setTimeout(function(){
|
|
|
|
|
var i = getRibbonIndex(ribbon)
|
|
|
|
|
var size = getVisibleImageSize('max')
|
|
|
|
|
var screen_size = getScreenWidthInImages(getVisibleImageSize())
|
|
|
|
|
// XXX
|
|
|
|
|
var cache_frame_size = (screen_size * LOAD_SCREENS) / 2
|
|
|
|
|
var images = ribbon.find('.image')
|
|
|
|
|
var first = getImageGID(images.first())
|
|
|
|
|
var last = getImageGID(images.last())
|
|
|
|
|
|
|
|
|
|
var gids = getGIDsAfter(-cache_frame_size, first)
|
|
|
|
|
.concat(getGIDsAfter(cache_frame_size, last))
|
|
|
|
|
|
|
|
|
|
var cache = []
|
|
|
|
|
IMAGE_CACHE[i] = cache
|
|
|
|
|
$.each(gids, function(i, e){
|
|
|
|
|
var img = new Image()
|
|
|
|
|
img.src = getBestPreview(e, size).url
|
|
|
|
|
cache.push(img)
|
|
|
|
|
})
|
2013-05-28 02:17:24 +04:00
|
|
|
|
2013-12-06 17:09:02 +04:00
|
|
|
deferred.resolve(cache)
|
|
|
|
|
}, 0)
|
|
|
|
|
return deferred
|
2013-05-28 02:17:24 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function preCacheAllRibbons(){
|
|
|
|
|
$('.ribbon').each(function(){
|
|
|
|
|
preCacheRibbonImages($(this))
|
|
|
|
|
})
|
|
|
|
|
return IMAGE_CACHE
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-06-02 23:07:18 +04:00
|
|
|
/**********************************************************************
|
|
|
|
|
* Actions...
|
|
|
|
|
*/
|
|
|
|
|
|
2013-09-25 17:00:54 +04:00
|
|
|
// Sort the ribbons by DATA.order and re-render...
|
|
|
|
|
//
|
2013-09-27 21:45:31 +04:00
|
|
|
// This is the main way to sort images:
|
|
|
|
|
// - sort DATA.order
|
|
|
|
|
// - call updateRibbonOrder() that will:
|
|
|
|
|
// - sort all the ribbons in DATA
|
|
|
|
|
// - trigger reloadViewer() to render the new state
|
|
|
|
|
//
|
|
|
|
|
// No direct sorting is required.
|
|
|
|
|
//
|
2013-09-25 17:00:54 +04:00
|
|
|
// NOTE: due to how the format is structured, to sort the images one
|
|
|
|
|
// only needs to sort DATA.order and call this.
|
2013-09-27 21:45:31 +04:00
|
|
|
// NOTE: if no_reload_viewer is true, then no re-rendering is triggered.
|
2013-09-25 17:00:54 +04:00
|
|
|
function updateRibbonOrder(no_reload_viewer){
|
|
|
|
|
for(var i=0; i < DATA.ribbons.length; i++){
|
|
|
|
|
DATA.ribbons[i].sort(imageOrderCmp)
|
|
|
|
|
}
|
|
|
|
|
if(!no_reload_viewer){
|
|
|
|
|
reloadViewer()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2013-10-15 18:47:49 +04:00
|
|
|
// Action wrapper of alignDataToRibbon(...)
|
|
|
|
|
//
|
|
|
|
|
// Align ribbons to the current ribbon.
|
|
|
|
|
//
|
|
|
|
|
// XXX need to change the default to base ribbon for production...
|
|
|
|
|
function alignRibbons(ribbon){
|
|
|
|
|
console.warn('alignRibbons(): not yet ready for production use!')
|
|
|
|
|
// XXX remove this line for production....
|
|
|
|
|
ribbon = ribbon == null ? getRibbonIndex() : ribbon
|
|
|
|
|
|
|
|
|
|
DATA = alignDataToRibbon(ribbon)
|
|
|
|
|
|
|
|
|
|
reloadViewer()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-07-14 20:53:30 +04:00
|
|
|
/******************************************************* Extension ***/
|
2013-06-04 16:32:33 +04:00
|
|
|
|
2013-05-28 04:41:16 +04:00
|
|
|
// Open image in an external editor/viewer
|
|
|
|
|
//
|
|
|
|
|
// NOTE: this will open the default editor/viewer.
|
|
|
|
|
function openImage(){
|
|
|
|
|
if(window.runSystem == null){
|
2013-06-01 18:21:48 +04:00
|
|
|
showErrorStatus('Can\'t run external programs.')
|
2013-05-28 04:41:16 +04:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
// XXX if path is not present try and open the biggest preview...
|
2013-06-08 18:28:10 +04:00
|
|
|
return runSystem(normalizePath(IMAGES[getImageGID()].path, getBaseURL()))
|
2013-05-28 04:41:16 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2013-06-07 00:56:43 +04:00
|
|
|
// XXX
|
|
|
|
|
function openImageWith(prog){
|
|
|
|
|
// XXX
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2013-06-04 16:32:33 +04:00
|
|
|
|
2013-10-15 18:47:49 +04:00
|
|
|
/**********************************************************************
|
2013-11-06 03:44:14 +04:00
|
|
|
* Experimental & utility
|
2013-10-15 18:47:49 +04:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
// The idea here is to add markers as first-class image-like elements...
|
|
|
|
|
//
|
|
|
|
|
// + can be ordered
|
|
|
|
|
// - re-sorting via metadata will mess things up
|
|
|
|
|
//
|
|
|
|
|
// XXX this is not persistent...
|
|
|
|
|
function appendMarker(){
|
|
|
|
|
return $('<div class="marker"/>').insertAfter(getImage())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2013-10-31 07:40:02 +04:00
|
|
|
// NOTE: if cmp is explicitly false then no sorting will be done.
|
2013-11-01 15:15:01 +04:00
|
|
|
function loadRibbonsFromPath(path, cmp, reverse, dir_name){
|
2013-10-31 07:40:02 +04:00
|
|
|
path = path == null ? BASE_URL : path
|
|
|
|
|
path = normalizePath(path)
|
|
|
|
|
cmp = cmp == null ? imageDateCmp : cmp
|
|
|
|
|
|
2013-11-01 15:15:01 +04:00
|
|
|
// NOTE: we explicitly sort later, this makes no difference
|
|
|
|
|
// speed-wise, but will make the code simpler...
|
|
|
|
|
DATA.ribbons = ribbonsFromFavDirs(path, null, null, dir_name)
|
2013-10-31 07:40:02 +04:00
|
|
|
|
2013-11-01 15:15:01 +04:00
|
|
|
// do the sort...
|
2013-10-31 07:40:02 +04:00
|
|
|
if(cmp != false){
|
|
|
|
|
sortImages(cmp, reverse)
|
|
|
|
|
} else {
|
|
|
|
|
reloadViewer()
|
|
|
|
|
}
|
2013-11-01 15:15:01 +04:00
|
|
|
|
|
|
|
|
return DATA
|
2013-10-31 07:40:02 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2013-11-06 03:44:14 +04:00
|
|
|
function readImageDate(gid, images){
|
|
|
|
|
images = images == null ? IMAGES : images
|
|
|
|
|
var img = images[gid]
|
|
|
|
|
return getEXIFDate(normalizePath(img.path))
|
|
|
|
|
.done(function(date){
|
|
|
|
|
img.ctime = Date.fromTimeStamp(date).getTime()/1000
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
function readImagesDates(images){
|
|
|
|
|
images = images == null ? IMAGES : images
|
|
|
|
|
|
|
|
|
|
return $.when.apply(null, $.map(images, function(_, gid){
|
|
|
|
|
return readImageDate(gid, images)
|
2013-11-07 02:20:20 +04:00
|
|
|
.done(function(){
|
|
|
|
|
IMAGES_UPDATED.push(gid)
|
|
|
|
|
})
|
2013-11-06 03:44:14 +04:00
|
|
|
}))
|
|
|
|
|
}
|
|
|
|
|
function readImagesDatesQ(images){
|
|
|
|
|
images = images == null ? IMAGES : images
|
|
|
|
|
|
|
|
|
|
var queue = getWorkerQueue('date_reader')
|
|
|
|
|
|
|
|
|
|
$.each(images, function(gid, img){
|
|
|
|
|
queue.enqueue(readImageDate, gid, images)
|
2013-11-07 02:20:20 +04:00
|
|
|
.always(function(){
|
|
|
|
|
IMAGES_UPDATED.push(gid)
|
|
|
|
|
queue.notify(gid, 'done')
|
|
|
|
|
})
|
2013-11-06 03:44:14 +04:00
|
|
|
})
|
|
|
|
|
|
|
|
|
|
return queue
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2013-11-09 15:03:49 +04:00
|
|
|
// XXX deleting images is not sported, we need to explicitly re-save...
|
|
|
|
|
// XXX need to reload the viewer...
|
|
|
|
|
// XXX not tested...
|
2013-11-06 03:44:14 +04:00
|
|
|
function updateImageGID(gid, images, data){
|
|
|
|
|
images = images == null ? IMAGES : images
|
|
|
|
|
var img = images[gid]
|
|
|
|
|
return getEXIFGID(normalizePath(img.path))
|
|
|
|
|
.done(function(gid){
|
|
|
|
|
img.id = gid
|
|
|
|
|
// images...
|
|
|
|
|
images[gid] = images[key]
|
|
|
|
|
delete images[key]
|
2013-11-09 15:03:49 +04:00
|
|
|
IMAGES_UPDATED.push(gid)
|
2013-11-06 03:44:14 +04:00
|
|
|
|
|
|
|
|
// data...
|
|
|
|
|
if(data != null){
|
|
|
|
|
// replace current...
|
|
|
|
|
if(data.current == key){
|
|
|
|
|
data.current = gid
|
|
|
|
|
}
|
|
|
|
|
// replace in order...
|
|
|
|
|
data.order[data.order.indexOf(key)] = gid
|
|
|
|
|
// replace in ribbons...
|
|
|
|
|
for(var i=0; i < data.ribbons; i++){
|
|
|
|
|
var r = data.ribbons[i]
|
|
|
|
|
var k = r.indexOf(key)
|
|
|
|
|
if(k >= 0){
|
|
|
|
|
r[k] = gid
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
function updateImagesGIDs(images, data){
|
|
|
|
|
images = images == null ? IMAGES : images
|
|
|
|
|
|
|
|
|
|
return $.when.apply(null, $.map(images, function(_, key){
|
|
|
|
|
return updateImageGID(key, images, data)
|
|
|
|
|
}))
|
|
|
|
|
}
|
|
|
|
|
function updateImagesGIDsQ(images, data){
|
|
|
|
|
images = images == null ? IMAGES : images
|
|
|
|
|
|
|
|
|
|
var queue = getWorkerQueue('gid_updater')
|
|
|
|
|
|
|
|
|
|
$.each(images, function(_, key){
|
|
|
|
|
queue.enqueue(updateImageGID, key, images, data)
|
|
|
|
|
.always(function(){ queue.notify(key, 'done') })
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
return queue
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2013-10-15 18:47:49 +04:00
|
|
|
|
2013-05-13 02:24:36 +04:00
|
|
|
/**********************************************************************
|
2013-05-31 20:10:23 +04:00
|
|
|
* vim:set ts=4 sw=4 spell : */
|