now loadDir(...) can load raw directories, still needs cleaning...

Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
Alex A. Naanou 2013-05-28 19:25:13 +04:00
parent 6dec27b3d5
commit a8bfe85f5b
2 changed files with 137 additions and 13 deletions

View File

@ -370,6 +370,47 @@ function normalizePath(url, base, mode){
/**********************************************************************
* Constructors
*/
function urlList2Images(lst){
var res = {}
$.each(lst, function(i, e){
var gid = 'image-' + i
res[gid] = {
id: gid,
type: 'image',
state: 'single',
path: e,
ctime: Date.now(),
preview: {},
classes: '',
orientation: 0,
}
})
return res
}
function dataFromImages(images){
var gids = Object.keys(images).sort()
return {
version: '2.0',
current: gids[0],
ribbons: [
gids
],
order: gids.slice(),
image_file: null
}
}
/**********************************************************************
* Format conversion
*/
@ -990,11 +1031,28 @@ function saveFileState(name, no_normalize_path){
}
function loadDir(path){
// Load a path
//
// This will try and to this in the following order:
// 1) find a data file in the given path
// 2) find a cache directory and a data file there
// 3) list the images and load them as-is
//
// XXX make sure that save works...
function loadDir(path, raw_load){
path = normalizePath(path)
var orig_path = path
var data
var files = listDir(path)
var data = $.map(files, function(e){
if(files == null){
console.error('Path error:', path)
return
}
if(!raw_load){
data = $.map(files, function(e){
return /.*-data.json$/.test(e) ? e : null
}).sort().reverse()[0]
data = (data == null && files.indexOf('data.json') >= 0) ? 'data.json' : data
@ -1004,17 +1062,42 @@ function loadDir(path){
path += '/' + CACHE_DIR
files = listDir(path)
if(files != null){
data = $.map(listDir(path), function(e){
return /.*-data.json$/.test(e) ? e : null
}).sort().reverse()[0]
data = (data == null && files.indexOf('data.json') >= 0) ? 'data.json' : data
}
}
}
// load the found data file...
if(data != null){
console.log('Loading:', data)
data = path + '/' + data
return loadFileState(data)
// load the dir as-is...
} else {
files = listDir(orig_path)
var image_paths = $.map(files, function(e){
return /.*\.(jpg|jpeg|png|gif)$/i.test(e) ? e : null
})
if(image_paths.length == 0){
console.error('No images in:', orig_path)
return
}
IMAGES = urlList2Images(image_paths)
DATA = dataFromImages(IMAGES)
MARKED = []
BASE_URL = orig_path
loadData()
}
}

View File

@ -455,6 +455,47 @@ $(function(){
setupDataBindings()
/* XXX drag/drop
$(document)
.bind('dragover', function(e){
e.stopPropagation()
e.preventDefault()
// XXX is there a jQuery way out of this??
e.originalEvent.dataTransfer.dropEffect = 'copy' // Explicitly show this is a copy.
})
.bind('drop', function(e){
e.stopPropagation()
e.preventDefault()
// XXX is there a jQuery way out of this??
var files = e.originalEvent.dataTransfer.files
// XXX should we be using the loadJSON here???
// XXX desperatly need image caching and preview generation...
for (var i = 0, f; f = files[i]; i++) {
if (!f.type.match('image.*')) {
continue
}
console.log('FILE:', f)
var reader = new FileReader()
reader.onload = function(i){
return function(e){
// XXX need to avoid data URLs here and use plain old paths...
//ribbon.append(makeImage(e.target.result, i))
console.log('DROPPED')//, e.target.result)
}
}(i)
reader.readAsDataURL(f)
//reader.readAsText(f)
}
})
*/
//setElementOrigin($('.ribbon-set'), 'top', 'left')