| 
									
										
										
										
											2013-06-07 00:56:43 +04:00
										 |  |  | /********************************************************************** | 
					
						
							|  |  |  | *  | 
					
						
							|  |  |  | * | 
					
						
							|  |  |  | * | 
					
						
							|  |  |  | **********************************************************************/ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | //var DEBUG = DEBUG != null ? DEBUG : true
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /********************************************************************** | 
					
						
							|  |  |  | * File storage (Extension API -- CEF/PhoneGap/...) | 
					
						
							|  |  |  | * | 
					
						
							|  |  |  | * XXX need to cleanup this section... | 
					
						
							|  |  |  | */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /********************************************************* Helpers ***/ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Report deferred progress
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // This uses showStatus(...) and showErrorStatus(...) to report actions.
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // Will use showErrorStatus(...) iff "Error" is the last argument of the
 | 
					
						
							|  |  |  | // progress/notify action, removing it (string 'Error') from the arguments.
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // Will return the original deferred.
 | 
					
						
							| 
									
										
										
										
											2013-06-08 01:48:22 +04:00
										 |  |  | function statusNotify(prefix, loader, not_queued){ | 
					
						
							|  |  |  | 	var report = not_queued == true ? showStatus : showStatusQ | 
					
						
							| 
									
										
										
										
											2013-06-07 00:56:43 +04:00
										 |  |  | 	if(loader == null){ | 
					
						
							|  |  |  | 		loader = prefix | 
					
						
							|  |  |  | 		prefix = null | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return loader | 
					
						
							|  |  |  | 		.progress(function(){ | 
					
						
							|  |  |  | 			var args = Array.apply(null, arguments) | 
					
						
							|  |  |  | 			if(prefix != null && prefix != ''){ | 
					
						
							|  |  |  | 				args.splice(0, 0, prefix) | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			if(args.indexOf('Error') >= 0){ | 
					
						
							|  |  |  | 				args.pop() | 
					
						
							|  |  |  | 				return showErrorStatus(args.join(': ')) | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2013-06-08 01:48:22 +04:00
										 |  |  | 			return report(args.join(': ')) | 
					
						
							| 
									
										
										
										
											2013-06-07 00:56:43 +04:00
										 |  |  | 		}) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Bubble up actions in the deferred chain
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // Will chain progress/notify and if only_progress is not set, also
 | 
					
						
							|  |  |  | // done/resolve and fail/reject from "from" to "to" deferred objects.
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // Will add prefix to the list of arguments of progress/notify and
 | 
					
						
							|  |  |  | // fail/reject (if not disabled), unless it is set to null.
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // Will return "from" object.
 | 
					
						
							|  |  |  | function bubbleProgress(prefix, from, to, only_progress){ | 
					
						
							|  |  |  | 	from | 
					
						
							|  |  |  | 		.progress(function(){  | 
					
						
							|  |  |  | 			var args = Array.apply(null, arguments) | 
					
						
							|  |  |  | 			prefix != null && args.splice(0, 0, prefix) | 
					
						
							|  |  |  | 			to.notify.apply(to, args)  | 
					
						
							|  |  |  | 		}) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if(only_progress == null){ | 
					
						
							|  |  |  | 		from | 
					
						
							|  |  |  | 			.done(function(){ | 
					
						
							|  |  |  | 				var args = Array.apply(null, arguments) | 
					
						
							|  |  |  | 				to.resolve.apply(to, args)  | 
					
						
							|  |  |  | 			}) | 
					
						
							|  |  |  | 			.fail(function(){ | 
					
						
							|  |  |  | 				var args = Array.apply(null, arguments) | 
					
						
							|  |  |  | 				prefix != null && args.splice(0, 0, prefix) | 
					
						
							|  |  |  | 				to.reject.apply(to, args)  | 
					
						
							|  |  |  | 			}) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return from | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Semi-generic deferred file loader
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // if pattern is given, then search for the latest (ordered last) file 
 | 
					
						
							|  |  |  | // and load that.
 | 
					
						
							|  |  |  | // else load the dfl file.
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // if diff_pattern is given, then merge all matching files in order 
 | 
					
						
							|  |  |  | // (first to last) with the loaded "main" file
 | 
					
						
							|  |  |  | //
 | 
					
						
							| 
									
										
										
										
											2013-06-14 13:16:27 +04:00
										 |  |  | // NOTE: this expects a file to be JSON.
 | 
					
						
							|  |  |  | // NOTE: if diffs are available this expects the file to contain an object,
 | 
					
						
							|  |  |  | // 		and will extend that object.
 | 
					
						
							| 
									
										
										
										
											2013-06-07 00:56:43 +04:00
										 |  |  | // NOTE: if neither of dfl, pattern or diff_pattern are given, then this
 | 
					
						
							|  |  |  | // 		is essentially the same as $.getJSON(...)
 | 
					
						
							|  |  |  | // NOTE: this needs listDir(...) to search for latest versions of files.
 | 
					
						
							|  |  |  | function loadLatestFile(path, dfl, pattern, diff_pattern){ | 
					
						
							| 
									
										
										
										
											2013-06-08 23:53:35 +04:00
										 |  |  | 	var pparts = path.split(/[\/\\]/) | 
					
						
							|  |  |  | 	dfl = dfl == null ? pparts.pop() : dfl | 
					
						
							|  |  |  | 	//path = path == dfl ? '.' : path
 | 
					
						
							|  |  |  | 	path = pparts.join('/') | 
					
						
							| 
									
										
										
										
											2013-06-07 00:56:43 +04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	var res = $.Deferred() | 
					
						
							|  |  |  | 	 | 
					
						
							| 
									
										
										
										
											2013-06-08 23:53:35 +04:00
										 |  |  | 	if(dfl == ''){ | 
					
						
							|  |  |  | 		return res.reject() | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-06-07 00:56:43 +04:00
										 |  |  | 	// can't find diffs if can't list dirs...
 | 
					
						
							|  |  |  | 	if(window.listDir == null && (pattern != null || diff_pattern != null)){ | 
					
						
							|  |  |  | 		res.notify('Unsupported', 'directory listing.') | 
					
						
							|  |  |  | 		return res.reject('listDir unsupported.') | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// find the latest...
 | 
					
						
							|  |  |  | 	if(pattern != null){ | 
					
						
							|  |  |  | 		pattern = RegExp(pattern) | 
					
						
							|  |  |  | 		var file = $.map(listDir(path), function(e){  | 
					
						
							|  |  |  | 			return pattern.test(e) ? e : null | 
					
						
							|  |  |  | 		}).sort().reverse()[0] | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	var file = file == null ? dfl : file | 
					
						
							|  |  |  | 	 | 
					
						
							|  |  |  | 	var diff_data = {} | 
					
						
							|  |  |  | 	var diff = true | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// collect and merge diffs...
 | 
					
						
							|  |  |  | 	if(diff_pattern != null){ | 
					
						
							|  |  |  | 		diff_pattern = RegExp(diff_pattern) | 
					
						
							|  |  |  | 		var diff_data = [diff_data] | 
					
						
							|  |  |  | 		var diffs_names = $.map(listDir(path), function(e){  | 
					
						
							|  |  |  | 			return diff_pattern.test(e) ? e : null | 
					
						
							|  |  |  | 		}).sort() | 
					
						
							|  |  |  | 		diff = $.when.apply(null, $.map(diffs_names, function(e, i){ | 
					
						
							|  |  |  | 					return $.getJSON(path +'/'+ e) | 
					
						
							|  |  |  | 						.done(function(data){ | 
					
						
							|  |  |  | 							diff_data[i+1] = data | 
					
						
							|  |  |  | 							res.notify('Loaded', e) | 
					
						
							|  |  |  | 						}) | 
					
						
							|  |  |  | 						.fail(function(){ | 
					
						
							|  |  |  | 							// XXX should we kill the load here???
 | 
					
						
							|  |  |  | 							res.notify('Loading', e, 'Error') | 
					
						
							|  |  |  | 						}) | 
					
						
							|  |  |  | 				})) | 
					
						
							|  |  |  | 			// NOTE: .then(...) handlers get different signature args 
 | 
					
						
							|  |  |  | 			// 		depending on the number of arguments to .when(...)...
 | 
					
						
							|  |  |  | 			.then(function(){ | 
					
						
							|  |  |  | 				$.extend.apply(null, diff_data) | 
					
						
							|  |  |  | 				diff_data = diff_data[0] | 
					
						
							|  |  |  | 			}) | 
					
						
							|  |  |  | 	}  | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// load the main file and merge the diff with it...
 | 
					
						
							|  |  |  | 	$.when(diff, $.getJSON(path +'/'+ file)) | 
					
						
							|  |  |  | 		.done(function(_, json){ | 
					
						
							|  |  |  | 			json = json[0] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			res.notify('Loaded', file) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			// merge diffs...
 | 
					
						
							|  |  |  | 			if(Object.keys(diff_data).length != 0){ | 
					
						
							|  |  |  | 				$.extend(json, diff_data) | 
					
						
							|  |  |  | 				res.notify('Merged') | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			res.resolve(json) | 
					
						
							|  |  |  | 		}) | 
					
						
							|  |  |  | 		.fail(function(){ | 
					
						
							|  |  |  | 			res.notify('Loading', file, 'Error') | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			return res.reject(file) | 
					
						
							|  |  |  | 		}) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return res | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-06-08 18:28:10 +04:00
										 |  |  | // Construct a ribbons hierarchy from the fav dirs structure
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // NOTE: this depends on listDir(...)
 | 
					
						
							|  |  |  | // NOTE: this assumes that images contain ALL the images...
 | 
					
						
							| 
									
										
										
										
											2013-06-08 20:04:27 +04:00
										 |  |  | // NOTE: this assumes that all file names are unique...
 | 
					
						
							| 
									
										
										
										
											2013-06-08 18:28:10 +04:00
										 |  |  | 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] | 
					
						
							| 
									
										
										
										
											2013-06-08 20:04:27 +04:00
										 |  |  | 			// skip files not in index...
 | 
					
						
							|  |  |  | 			// NOTE: we do not need to filter the files by name as we 
 | 
					
						
							|  |  |  | 			// 		trust the index...
 | 
					
						
							|  |  |  | 			if(_gid == null){ | 
					
						
							|  |  |  | 				return  | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2013-06-08 18:28:10 +04:00
										 |  |  | 			// 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) | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 			}) | 
					
						
							| 
									
										
										
										
											2013-06-08 20:04:27 +04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 			ribbon.push(_gid) | 
					
						
							| 
									
										
										
										
											2013-06-08 18:28:10 +04:00
										 |  |  | 		}) | 
					
						
							|  |  |  | 		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() | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-06-07 00:56:43 +04:00
										 |  |  | 
 | 
					
						
							|  |  |  | /*********************************************************************/ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Load images from file
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // This will also merge all diff files.
 | 
					
						
							|  |  |  | function loadFileImages(path, no_load_diffs){ | 
					
						
							|  |  |  | 	no_load_diffs = window.listDir == null ? true : no_load_diffs  | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	var res = $.Deferred() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// default locations...
 | 
					
						
							|  |  |  | 	if(path == null){ | 
					
						
							| 
									
										
										
										
											2013-06-14 01:11:11 +04:00
										 |  |  | 		var base = normalizePath(CACHE_DIR_VAR)  | 
					
						
							| 
									
										
										
										
											2013-06-07 00:56:43 +04:00
										 |  |  | 		var loader = loadLatestFile(base,  | 
					
						
							|  |  |  | 				IMAGES_FILE_DEFAULT,  | 
					
						
							|  |  |  | 				IMAGES_FILE_PATTERN,  | 
					
						
							|  |  |  | 				IMAGES_DIFF_FILE_PATTERN) | 
					
						
							|  |  |  | 	 | 
					
						
							| 
									
										
										
										
											2013-06-09 02:44:20 +04:00
										 |  |  | 	// explicit base dir...
 | 
					
						
							|  |  |  | 	} else if(!/\.json$/i.test(path)) { | 
					
						
							| 
									
										
										
										
											2013-06-14 01:11:11 +04:00
										 |  |  | 		var base = normalizePath(path +'/'+ CACHE_DIR_VAR)  | 
					
						
							| 
									
										
										
										
											2013-06-09 02:44:20 +04:00
										 |  |  | 		var loader = loadLatestFile(base,  | 
					
						
							|  |  |  | 				IMAGES_FILE_DEFAULT,  | 
					
						
							|  |  |  | 				IMAGES_FILE_PATTERN,  | 
					
						
							|  |  |  | 				IMAGES_DIFF_FILE_PATTERN) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-06-07 00:56:43 +04:00
										 |  |  | 	// explicit path...
 | 
					
						
							|  |  |  | 	} else { | 
					
						
							| 
									
										
										
										
											2013-06-08 23:53:35 +04:00
										 |  |  | 		var loader = loadLatestFile(normalizePath(path)) | 
					
						
							| 
									
										
										
										
											2013-06-07 00:56:43 +04:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	bubbleProgress('Images', loader, res) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	res.done(function(images){ | 
					
						
							|  |  |  | 		IMAGES = images | 
					
						
							|  |  |  | 	}) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return res | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Save current images list to file
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // If not name is given this will merge all the diffs and save a "clean"
 | 
					
						
							|  |  |  | // (full) images.json file. Also removing the diff files.
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // NOTE: if an explicit name is given then this will not remove anything.
 | 
					
						
							| 
									
										
										
										
											2013-06-14 13:16:27 +04:00
										 |  |  | // NOTE: this will use CACHE_DIR as the location if no name is given.
 | 
					
						
							| 
									
										
										
										
											2013-06-07 00:56:43 +04:00
										 |  |  | function saveFileImages(name){ | 
					
						
							|  |  |  | 	var remove_diffs = (name == null) | 
					
						
							| 
									
										
										
										
											2013-06-14 01:11:11 +04:00
										 |  |  | 	name = name == null ? normalizePath(CACHE_DIR_VAR +'/'+ Date.timeStamp()) : name | 
					
						
							| 
									
										
										
										
											2013-06-07 00:56:43 +04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	if(window.dumpJSON == null){ | 
					
						
							|  |  |  | 		showErrorStatus('Can\'t save to file.') | 
					
						
							|  |  |  | 		return | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// remove the diffs...
 | 
					
						
							|  |  |  | 	if(remove_diffs){ | 
					
						
							| 
									
										
										
										
											2013-06-14 01:11:11 +04:00
										 |  |  | 		$.each($.map(listDir(normalizePath(CACHE_DIR_VAR)), function(e){  | 
					
						
							| 
									
										
										
										
											2013-06-07 00:56:43 +04:00
										 |  |  | 				return IMAGES_DIFF_FILE_PATTERN.test(e) ? e : null | 
					
						
							|  |  |  | 			}), function(i, e){ | 
					
						
							| 
									
										
										
										
											2013-06-08 01:48:22 +04:00
										 |  |  | 				showStatusQ('removeing:', e) | 
					
						
							| 
									
										
										
										
											2013-06-14 01:11:11 +04:00
										 |  |  | 				removeFile(normalizePath(CACHE_DIR_VAR +'/'+ e)) | 
					
						
							| 
									
										
										
										
											2013-06-07 00:56:43 +04:00
										 |  |  | 			}) | 
					
						
							|  |  |  | 		IMAGES_UPDATED = [] | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// XXX use the pattern...
 | 
					
						
							|  |  |  | 	dumpJSON(name + '-images.json', IMAGES) | 
					
						
							|  |  |  | 	//DATA.image_file = normalizePath(name + '-images.json', null, 'relative')
 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Load image marks form file
 | 
					
						
							|  |  |  | function loadFileMarks(path){ | 
					
						
							|  |  |  | 	var res = $.Deferred() | 
					
						
							|  |  |  | 	// default locations...
 | 
					
						
							|  |  |  | 	if(path == null){ | 
					
						
							| 
									
										
										
										
											2013-06-14 01:11:11 +04:00
										 |  |  | 		var base = normalizePath(CACHE_DIR_VAR) | 
					
						
							| 
									
										
										
										
											2013-06-07 00:56:43 +04:00
										 |  |  | 		var loader = loadLatestFile(base,  | 
					
						
							|  |  |  | 				MARKED_FILE_DEFAULT,  | 
					
						
							|  |  |  | 				MARKED_FILE_PATTERN) | 
					
						
							|  |  |  | 	 | 
					
						
							|  |  |  | 	// explicit path...
 | 
					
						
							|  |  |  | 	// XXX need to account for paths without a CACHE_DIR
 | 
					
						
							|  |  |  | 	} else { | 
					
						
							|  |  |  | 		path = normalizePath(path) | 
					
						
							|  |  |  | 		var base = path.split(CACHE_DIR)[0] | 
					
						
							| 
									
										
										
										
											2013-06-14 01:11:11 +04:00
										 |  |  | 		//base = normalizePath(path +'/'+ CACHE_DIR_VAR)
 | 
					
						
							|  |  |  | 		base = path +'/'+ CACHE_DIR | 
					
						
							| 
									
										
										
										
											2013-06-07 00:56:43 +04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		// XXX is this correct???
 | 
					
						
							|  |  |  | 		var loader = loadLatestFile(base,  | 
					
						
							|  |  |  | 				path.split(base)[0],  | 
					
						
							|  |  |  | 				RegExp(path.split(base)[0])) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	bubbleProgress('Marks', loader, res) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	res.done(function(images){ | 
					
						
							|  |  |  | 		MARKED = images | 
					
						
							|  |  |  | 	}) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return res | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Save image marks to file
 | 
					
						
							|  |  |  | function saveFileMarks(name){ | 
					
						
							| 
									
										
										
										
											2013-06-14 01:11:11 +04:00
										 |  |  | 	name = name == null ? normalizePath(CACHE_DIR_VAR +'/'+ Date.timeStamp()) : name | 
					
						
							| 
									
										
										
										
											2013-06-07 00:56:43 +04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	dumpJSON(name + '-marked.json', MARKED) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Load images, ribbons and marks from cache
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // XXX add support for explicit filenames...
 | 
					
						
							|  |  |  | function loadFileState(path, prefix){ | 
					
						
							|  |  |  | 	prefix = prefix == null ? 'Data' : prefix | 
					
						
							|  |  |  | 	prefix = prefix === false ? null : prefix | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// XXX explicit data file path...
 | 
					
						
							|  |  |  | 	if(/\.json$/i.test(path)){ | 
					
						
							|  |  |  | 		// XXX at this 
 | 
					
						
							|  |  |  | 		var base = path.split(CACHE_DIR)[0] | 
					
						
							|  |  |  | 		base = base == path ? '.' : base | 
					
						
							|  |  |  | 	} else { | 
					
						
							|  |  |  | 		var base = path.split(CACHE_DIR)[0] | 
					
						
							|  |  |  | 		base = base == path ? '.' : base | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	var res = $.Deferred() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	bubbleProgress(prefix, | 
					
						
							|  |  |  | 			loadLatestFile(path,  | 
					
						
							|  |  |  | 				DATA_FILE_DEFAULT,  | 
					
						
							|  |  |  | 				DATA_FILE_PATTERN), res, true) | 
					
						
							|  |  |  | 		.done(function(json){ | 
					
						
							| 
									
										
										
										
											2013-06-08 18:28:10 +04:00
										 |  |  | 			setBaseURL(base) | 
					
						
							| 
									
										
										
										
											2013-06-07 00:56:43 +04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 			// legacy format...
 | 
					
						
							|  |  |  | 			if(json.version == null){ | 
					
						
							|  |  |  | 				json = convertDataGen1(json) | 
					
						
							|  |  |  | 				DATA = json.data | 
					
						
							|  |  |  | 				IMAGES = json.images | 
					
						
							|  |  |  | 				MARKED = [] | 
					
						
							|  |  |  | 				reloadViewer() | 
					
						
							|  |  |  | 				res.resolve() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			// version 2.0
 | 
					
						
							|  |  |  | 			} else if(json.version == '2.0') { | 
					
						
							|  |  |  | 				DATA = json | 
					
						
							|  |  |  | 				$.when( | 
					
						
							|  |  |  | 						// XXX load config...
 | 
					
						
							|  |  |  | 						// load images...
 | 
					
						
							|  |  |  | 						bubbleProgress(prefix, | 
					
						
							| 
									
										
										
										
											2013-06-09 02:44:20 +04:00
										 |  |  | 							loadFileImages(base), res, true), | 
					
						
							|  |  |  | 							//loadFileImages(DATA.image_file != null ?
 | 
					
						
							|  |  |  | 							//		normalizePath(DATA.image_file, base) 
 | 
					
						
							|  |  |  | 							//		: null), res, true),
 | 
					
						
							| 
									
										
										
										
											2013-06-07 00:56:43 +04:00
										 |  |  | 						// load marks if available...
 | 
					
						
							|  |  |  | 						bubbleProgress(prefix, | 
					
						
							|  |  |  | 							loadFileMarks(), res, true)) | 
					
						
							|  |  |  | 					.done(function(){ | 
					
						
							|  |  |  | 						reloadViewer() | 
					
						
							|  |  |  | 						res.resolve() | 
					
						
							|  |  |  | 					}) | 
					
						
							|  |  |  | 					// XXX fail???
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			// unknown format...
 | 
					
						
							|  |  |  | 			} else { | 
					
						
							|  |  |  | 				res.reject('unknown format.') | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		}) | 
					
						
							|  |  |  | 		.fail(function(){ | 
					
						
							|  |  |  | 			res.reject('Loading', path, 'Error') | 
					
						
							|  |  |  | 		}) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return res | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Save, ribbons and marks to cache
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // NOTE: this will NOT save images, that operation must be explicitly 
 | 
					
						
							|  |  |  | // 		performed by saveFileImages(...)
 | 
					
						
							|  |  |  | function saveFileState(name, no_normalize_path){ | 
					
						
							|  |  |  | 	name = name == null ? Date.timeStamp() : name | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if(!no_normalize_path){ | 
					
						
							| 
									
										
										
										
											2013-06-14 01:11:11 +04:00
										 |  |  | 		name = normalizePath(CACHE_DIR_VAR +'/'+ name) | 
					
						
							| 
									
										
										
										
											2013-06-07 00:56:43 +04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// write .image_file only if saving data to a non-cache dir...
 | 
					
						
							|  |  |  | 	// XXX check if this is correct...
 | 
					
						
							|  |  |  | 	} else { | 
					
						
							|  |  |  | 		if(DATA.image_file == null){ | 
					
						
							|  |  |  | 			DATA.image_file = name + '-images.json' | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	dumpJSON(name + '-data.json', DATA) | 
					
						
							|  |  |  | 	// XXX do we need to do this???
 | 
					
						
							|  |  |  | 	saveFileMarks(name) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// save the updated images...
 | 
					
						
							|  |  |  | 	if(IMAGES_UPDATED.length > 0){ | 
					
						
							|  |  |  | 		var updated = {} | 
					
						
							|  |  |  | 		$.each(IMAGES_UPDATED, function(i, e){ | 
					
						
							|  |  |  | 			updated[e] = IMAGES[e] | 
					
						
							|  |  |  | 		}) | 
					
						
							|  |  |  | 		dumpJSON(name + '-images-diff.json', updated) | 
					
						
							|  |  |  | 		IMAGES_UPDATED = [] | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Load a directory as-is
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // XXX check if we need to pass down sorting settings to the generators...
 | 
					
						
							|  |  |  | function loadRawDir(path, prefix){ | 
					
						
							|  |  |  | 	prefix = prefix == null ? 'Data' : prefix | 
					
						
							|  |  |  | 	prefix = prefix === false ? null : prefix | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	var files = listDir(path) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	var res = $.Deferred() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	var image_paths = $.map(files, function(e){ | 
					
						
							|  |  |  | 		return IMAGE_PATTERN.test(e) ? e : null | 
					
						
							|  |  |  | 	}) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if(image_paths.length == 0){ | 
					
						
							|  |  |  | 		// no images in path...
 | 
					
						
							|  |  |  | 		res.notify(prefix, 'Load', path, 'Error') | 
					
						
							|  |  |  | 		return res.reject() | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-06-08 18:28:10 +04:00
										 |  |  | 	setBaseURL(path) | 
					
						
							| 
									
										
										
										
											2013-06-07 00:56:43 +04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	IMAGES = imagesFromUrls(image_paths) | 
					
						
							|  |  |  | 	res.notify(prefix, 'Loaded', 'Images.') | 
					
						
							| 
									
										
										
										
											2013-06-14 13:16:27 +04:00
										 |  |  | 	IMAGES_CREATED = true | 
					
						
							| 
									
										
										
										
											2013-06-07 00:56:43 +04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	DATA = dataFromImages(IMAGES) | 
					
						
							|  |  |  | 	res.notify(prefix, 'Loaded', 'Data.') | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	updateRibbonsFromFavDirs() | 
					
						
							|  |  |  | 	res.notify(prefix, 'Loaded', 'Fav dirs.') | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	MARKED = [] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	reloadViewer() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return res.resolve() | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Load a path
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // This will try and do one of the following in order:
 | 
					
						
							|  |  |  | // 	1) look for a cache and load it,
 | 
					
						
							|  |  |  | // 	2) load data from within the directory
 | 
					
						
							|  |  |  | // 	3) load a directory as-is
 | 
					
						
							|  |  |  | // 		load fav dirs
 | 
					
						
							|  |  |  | //
 | 
					
						
							| 
									
										
										
										
											2013-06-13 19:42:42 +04:00
										 |  |  | // NOTE: this will create an images.json file in cache on opening an 
 | 
					
						
							|  |  |  | // 		un-cached dir (XXX is this correct???)
 | 
					
						
							| 
									
										
										
										
											2013-06-07 00:56:43 +04:00
										 |  |  | function loadDir(path, prefix){ | 
					
						
							|  |  |  | 	prefix = prefix == null ? 'Data' : prefix | 
					
						
							|  |  |  | 	prefix = prefix === false ? null : prefix | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-06-13 19:42:42 +04:00
										 |  |  | 	IMAGES_CREATED = false | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-06-07 00:56:43 +04:00
										 |  |  | 	path = normalizePath(path) | 
					
						
							|  |  |  | 	var orig_path = path | 
					
						
							|  |  |  | 	var data | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	var res = $.Deferred() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	res.notify(prefix, 'Loading', path) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	var files = listDir(path) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if(files == null){ | 
					
						
							|  |  |  | 		//showErrorStatus('No files in path: ' + path)
 | 
					
						
							|  |  |  | 		res.notify('load_error', path) | 
					
						
							|  |  |  | 		return res.reject() | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// see if there is a cache...
 | 
					
						
							|  |  |  | 	if(files.indexOf(CACHE_DIR) >= 0){ | 
					
						
							|  |  |  | 		path = path +'/'+ CACHE_DIR | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	bubbleProgress(prefix,  | 
					
						
							|  |  |  | 			loadFileState(path, false), res, true) | 
					
						
							|  |  |  | 		.done(function(){ | 
					
						
							|  |  |  | 			res.resolve() | 
					
						
							|  |  |  | 		}) | 
					
						
							|  |  |  | 		.fail(function(){ | 
					
						
							|  |  |  | 			bubbleProgress('Raw directory', loadRawDir(orig_path), res) | 
					
						
							|  |  |  | 		}) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return res | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Load ribbon structure from fav directory tree
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // XXX loads duplicate images....
 | 
					
						
							|  |  |  | function updateRibbonsFromFavDirs(){ | 
					
						
							|  |  |  | 	DATA.ribbons = ribbonsFromFavDirs(null, null, imageOrderCmp) | 
					
						
							|  |  |  | 	sortImagesByDate() | 
					
						
							|  |  |  | 	reloadViewer() | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-06-21 16:58:33 +04:00
										 |  |  | // Export current state to directory...
 | 
					
						
							|  |  |  | //
 | 
					
						
							| 
									
										
										
										
											2013-07-01 18:25:33 +04:00
										 |  |  | // XXX this copies the files in parallel, make it sync and sequential...
 | 
					
						
							|  |  |  | // 		...reason is simple, if we stop the copy we need to end up with 
 | 
					
						
							|  |  |  | // 		part of the files copied full rather than all partially...
 | 
					
						
							| 
									
										
										
										
											2013-06-21 16:58:33 +04:00
										 |  |  | function exportTo(path, im_name, dir_name, size){ | 
					
						
							|  |  |  | 	path = path == null ? BASE_URL : path | 
					
						
							|  |  |  | 	im_name = im_name == null ? '%f' : im_name | 
					
						
							|  |  |  | 	dir_name = dir_name == null ? 'fav' : dir_name | 
					
						
							| 
									
										
										
										
											2013-06-21 17:23:09 +04:00
										 |  |  | 	size = size == null ? 1000 : size | 
					
						
							| 
									
										
										
										
											2013-06-21 16:58:33 +04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-07-01 18:05:50 +04:00
										 |  |  | 	// starting point...
 | 
					
						
							|  |  |  | 	//var deferred = $.Deferred().resolve()
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-06-21 16:58:33 +04:00
										 |  |  | 	var base_path = path | 
					
						
							|  |  |  | 	path = normalizePath(path) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	var order = DATA.order | 
					
						
							| 
									
										
										
										
											2013-07-01 18:25:33 +04:00
										 |  |  | 	var Z = (('10e' + (order.length + '').length) * 1 + '').slice(2) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// mainly used for file naming, gives us ability to number images 
 | 
					
						
							|  |  |  | 	// in the current selection...
 | 
					
						
							|  |  |  | 	var selection = [] | 
					
						
							|  |  |  | 	$.each(DATA.ribbons, function(_, e){ | 
					
						
							|  |  |  | 		selection = selection.concat(e) | 
					
						
							|  |  |  | 	}) | 
					
						
							|  |  |  | 	selection.sort(imageOrderCmp) | 
					
						
							|  |  |  | 	var z = (('10e' + (selection.length + '').length) * 1 + '').slice(2) | 
					
						
							| 
									
										
										
										
											2013-06-21 16:58:33 +04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// go through ribbons...
 | 
					
						
							|  |  |  | 	for(var i=DATA.ribbons.length-1; i >= 0; i--){ | 
					
						
							|  |  |  | 		var ribbon = DATA.ribbons[i] | 
					
						
							|  |  |  | 		// go through images...
 | 
					
						
							|  |  |  | 		for(var j=0; j < ribbon.length; j++){ | 
					
						
							|  |  |  | 			var gid = ribbon[j] | 
					
						
							|  |  |  | 			// get correct preview...
 | 
					
						
							|  |  |  | 			var src = getBestPreview(gid, size).url | 
					
						
							|  |  |  | 			var orig = IMAGES[gid].path.split('/').pop() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-07-07 00:22:08 +04:00
										 |  |  | 			// XXX might be a good idea to combine this with docs as a 
 | 
					
						
							|  |  |  | 			// 		single mechanism...
 | 
					
						
							| 
									
										
										
										
											2013-06-21 16:58:33 +04:00
										 |  |  | 			// form image name...
 | 
					
						
							|  |  |  | 			var dest = im_name | 
					
						
							|  |  |  | 			// full filename...
 | 
					
						
							|  |  |  | 			dest = dest.replace('%f', orig) | 
					
						
							|  |  |  | 			// file name w.o. ext...
 | 
					
						
							|  |  |  | 			dest = dest.replace('%n', orig.split('.')[0]) | 
					
						
							|  |  |  | 			// ext...
 | 
					
						
							|  |  |  | 			dest = dest.replace('%e', src.split('.').pop()) | 
					
						
							| 
									
										
										
										
											2013-07-07 00:22:08 +04:00
										 |  |  | 			// marked status...
 | 
					
						
							|  |  |  | 			dest = dest.replace('%m', MARKED.indexOf(gid) >= 0 ? 'm' : '') | 
					
						
							| 
									
										
										
										
											2013-06-21 16:58:33 +04:00
										 |  |  | 			// gid...
 | 
					
						
							|  |  |  | 			dest = dest.replace('%gid', gid) | 
					
						
							|  |  |  | 			dest = dest.replace('%g', gid.slice(34)) | 
					
						
							| 
									
										
										
										
											2013-07-01 18:25:33 +04:00
										 |  |  | 			// global order...
 | 
					
						
							| 
									
										
										
										
											2013-06-21 16:58:33 +04:00
										 |  |  | 			var o = order.indexOf(gid) + '' | 
					
						
							| 
									
										
										
										
											2013-07-01 18:25:33 +04:00
										 |  |  | 			dest = dest.replace('%I', (Z + o).slice(o.length)) | 
					
						
							| 
									
										
										
										
											2013-07-03 01:44:03 +04:00
										 |  |  | 			// current order...
 | 
					
						
							| 
									
										
										
										
											2013-07-01 18:25:33 +04:00
										 |  |  | 			var o = selection.indexOf(gid) + '' | 
					
						
							| 
									
										
										
										
											2013-06-21 16:58:33 +04:00
										 |  |  | 			dest = dest.replace('%i', (z + o).slice(o.length)) | 
					
						
							|  |  |  | 			// XXX Metadata...
 | 
					
						
							|  |  |  | 			// XXX
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			dest = path +'/'+ dest | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-06-21 17:23:09 +04:00
										 |  |  | 			// copy... 
 | 
					
						
							| 
									
										
										
										
											2013-06-21 16:58:33 +04:00
										 |  |  | 			copyFile(src, dest) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		path = normalizePath(path +'/'+ dir_name) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-06-07 00:56:43 +04:00
										 |  |  | 
 | 
					
						
							|  |  |  | /********************************************************************** | 
					
						
							|  |  |  | * vim:set ts=4 sw=4 :                                                */ |