diff --git a/ui/data.js b/ui/data.js index c88225d7..5aa2d988 100755 --- a/ui/data.js +++ b/ui/data.js @@ -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,31 +1031,73 @@ 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){ - return /.*-data.json$/.test(e) ? e : null - }).sort().reverse()[0] - data = (data == null && files.indexOf('data.json') >= 0) ? 'data.json' : data - // look in the cache dir... - if(data == null){ - path += '/' + CACHE_DIR + if(files == null){ + console.error('Path error:', path) + return + } - files = listDir(path) - data = $.map(listDir(path), function(e){ + 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 + + // look in the cache dir... + if(data == null){ + 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 + } + } } - console.log('Loading:', data) + // load the found data file... + if(data != null){ + console.log('Loading:', data) - data = path + '/' + data + data = path + '/' + data - return loadFileState(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() + } } diff --git a/ui/index.html b/ui/index.html index 9d7e1b94..2f1a738b 100755 --- a/ui/index.html +++ b/ui/index.html @@ -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')