mirror of
				https://github.com/flynx/ImageGrid.git
				synced 2025-11-03 21:00:14 +00:00 
			
		
		
		
	now browse.update(..) is async and returns a deferred...
Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
		
							parent
							
								
									9f2d4971a8
								
							
						
					
					
						commit
						6b5a3828a2
					
				@ -6,7 +6,8 @@
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
var fs = require('fs')
 | 
					var fs = require('fs')
 | 
				
			||||||
var path = require('path')
 | 
					var path = require('path')
 | 
				
			||||||
var walk = require('glob')
 | 
					var promise = require('promise')
 | 
				
			||||||
 | 
					var glob = require('glob')
 | 
				
			||||||
var guaranteeEvents = require('guarantee-events')
 | 
					var guaranteeEvents = require('guarantee-events')
 | 
				
			||||||
 | 
					
 | 
				
			||||||
define(function(require){ var module = {}
 | 
					define(function(require){ var module = {}
 | 
				
			||||||
@ -64,27 +65,43 @@ function(path, make){
 | 
				
			|||||||
	path = /^[a-zA-Z]:$/.test(path.trim()) ? path+'/' : path
 | 
						path = /^[a-zA-Z]:$/.test(path.trim()) ? path+'/' : path
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	var fullpath = false
 | 
						var fullpath = false
 | 
				
			||||||
 | 
						var stat = promise.denodeify(fs.stat)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						return new promise(function(resolve, reject){
 | 
				
			||||||
		fs.readdir(path, function(err, files){
 | 
							fs.readdir(path, function(err, files){
 | 
				
			||||||
			// XXX
 | 
								// XXX
 | 
				
			||||||
			if(err){
 | 
								if(err){
 | 
				
			||||||
 | 
									reject(err)
 | 
				
			||||||
				return
 | 
									return
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
 | 
								var res = []
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		files.forEach(function(file){
 | 
								files.map(function(file){
 | 
				
			||||||
			fs.stat(path +'/'+ file, function(err, stat){
 | 
									return stat(path +'/'+ file)
 | 
				
			||||||
				if(err){
 | 
										.catch(function(err){
 | 
				
			||||||
						make(fullpath 
 | 
											make(fullpath 
 | 
				
			||||||
							? path +'/'+ file 
 | 
												? path +'/'+ file 
 | 
				
			||||||
							: file, null, true)
 | 
												: file, null, true)
 | 
				
			||||||
				} else {
 | 
										})
 | 
				
			||||||
 | 
										.then(function(res){
 | 
				
			||||||
 | 
											if(!res){
 | 
				
			||||||
 | 
												return
 | 
				
			||||||
 | 
											}
 | 
				
			||||||
						make(fullpath 
 | 
											make(fullpath 
 | 
				
			||||||
							? path +'/'+ file 
 | 
												? path +'/'+ file 
 | 
				
			||||||
						: file + (stat.isDirectory() ? '/' : ''))
 | 
												: file + (res.isDirectory() ? '/' : ''))
 | 
				
			||||||
 | 
										})
 | 
				
			||||||
 | 
										// NOTE: we are not using promise.all(..) here because it
 | 
				
			||||||
 | 
										// 		triggers BEFORE the first make(..) is called...
 | 
				
			||||||
 | 
										.then(function(){
 | 
				
			||||||
 | 
											res.push(file)
 | 
				
			||||||
 | 
											if(res.length == files.length){
 | 
				
			||||||
 | 
												resolve()
 | 
				
			||||||
						}
 | 
											}
 | 
				
			||||||
					})
 | 
										})
 | 
				
			||||||
			})
 | 
								})
 | 
				
			||||||
		})
 | 
							})
 | 
				
			||||||
 | 
						})
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// NOTE: this should work from a chrome app and does not require anything
 | 
					// NOTE: this should work from a chrome app and does not require anything
 | 
				
			||||||
 | 
				
			|||||||
@ -19,6 +19,8 @@ define(function(require){ var module = {}
 | 
				
			|||||||
console.log('>>> browse')
 | 
					console.log('>>> browse')
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					//var promise = require('promise')
 | 
				
			||||||
 | 
					
 | 
				
			||||||
var keyboard = require('../keyboard')
 | 
					var keyboard = require('../keyboard')
 | 
				
			||||||
var object = require('../../object')
 | 
					var object = require('../../object')
 | 
				
			||||||
var widget = require('./widget')
 | 
					var widget = require('./widget')
 | 
				
			||||||
@ -374,7 +376,7 @@ var BrowserPrototype = {
 | 
				
			|||||||
			.toArray()
 | 
								.toArray()
 | 
				
			||||||
	},
 | 
						},
 | 
				
			||||||
	set path(value){
 | 
						set path(value){
 | 
				
			||||||
		return this.update(value)
 | 
							this.update(value)
 | 
				
			||||||
	},
 | 
						},
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// String path...
 | 
						// String path...
 | 
				
			||||||
@ -462,6 +464,7 @@ var BrowserPrototype = {
 | 
				
			|||||||
	// 	- build the path
 | 
						// 	- build the path
 | 
				
			||||||
	// 	- build the element list
 | 
						// 	- build the element list
 | 
				
			||||||
	// 	- bind to control events
 | 
						// 	- bind to control events
 | 
				
			||||||
 | 
						// 	- return a deferred
 | 
				
			||||||
	//
 | 
						//
 | 
				
			||||||
	// This will trigger the 'update' event.
 | 
						// This will trigger the 'update' event.
 | 
				
			||||||
	//
 | 
						//
 | 
				
			||||||
@ -500,6 +503,8 @@ var BrowserPrototype = {
 | 
				
			|||||||
		var focus = browser.find(':focus').length > 0
 | 
							var focus = browser.find(':focus').length > 0
 | 
				
			||||||
		list = list || this.list
 | 
							list = list || this.list
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							var deferred = $.Deferred()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		// string path and terminated with '/' -- no selection...
 | 
							// string path and terminated with '/' -- no selection...
 | 
				
			||||||
		if(typeof(path) == typeof('str') && !/[\\\/]/.test(path.trim().slice(-1))){
 | 
							if(typeof(path) == typeof('str') && !/[\\\/]/.test(path.trim().slice(-1))){
 | 
				
			||||||
			path = this.path2list(path)
 | 
								path = this.path2list(path)
 | 
				
			||||||
@ -522,10 +527,13 @@ var BrowserPrototype = {
 | 
				
			|||||||
			p.append($('<div>')
 | 
								p.append($('<div>')
 | 
				
			||||||
				.addClass('dir')
 | 
									.addClass('dir')
 | 
				
			||||||
				.click(function(){
 | 
									.click(function(){
 | 
				
			||||||
 | 
										// XXX we should use pop here...
 | 
				
			||||||
					if(that.traversable){
 | 
										if(that.traversable){
 | 
				
			||||||
						that
 | 
											that
 | 
				
			||||||
							.update(cur.slice(0, -1)) 
 | 
												.update(cur.slice(0, -1)) 
 | 
				
			||||||
							.select('"'+cur.pop()+'"')
 | 
													.done(function(){
 | 
				
			||||||
 | 
														that.select('"'+cur.pop()+'"')
 | 
				
			||||||
 | 
													})
 | 
				
			||||||
					}
 | 
										}
 | 
				
			||||||
				})
 | 
									})
 | 
				
			||||||
				.text(e))
 | 
									.text(e))
 | 
				
			||||||
@ -609,16 +617,28 @@ var BrowserPrototype = {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
		// second API: make is not called and .list(..) returns an Array
 | 
							// second API: make is not called and .list(..) returns an Array
 | 
				
			||||||
		// that will get loaded as list items...
 | 
							// that will get loaded as list items...
 | 
				
			||||||
		if(!interactive && res){
 | 
							if(!interactive && res && res.constructor == Array){
 | 
				
			||||||
			res.forEach(make)
 | 
								res.forEach(make)
 | 
				
			||||||
		} 
 | 
							} 
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		this.dom.attr('path', this.strPath)
 | 
							// wait for the render...
 | 
				
			||||||
		this.trigger('update')
 | 
							if(res && res.then){
 | 
				
			||||||
 | 
								res.then(function(){ deferred.resolve() })
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							// sync...
 | 
				
			||||||
 | 
							} else {
 | 
				
			||||||
 | 
								deferred.resolve()
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							//return this
 | 
				
			||||||
 | 
							return deferred
 | 
				
			||||||
 | 
								.done(function(){
 | 
				
			||||||
 | 
									that.dom.attr('path', this.strPath)
 | 
				
			||||||
 | 
									that.trigger('update')
 | 
				
			||||||
 | 
					
 | 
				
			||||||
				// select the item...
 | 
									// select the item...
 | 
				
			||||||
				if(selection){
 | 
									if(selection){
 | 
				
			||||||
			this.select(selection)
 | 
										that.select(selection)
 | 
				
			||||||
				}
 | 
									}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
				// maintain focus within the widget...
 | 
									// maintain focus within the widget...
 | 
				
			||||||
@ -626,7 +646,7 @@ var BrowserPrototype = {
 | 
				
			|||||||
					this.focus()
 | 
										this.focus()
 | 
				
			||||||
				}
 | 
									}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		return this
 | 
								})
 | 
				
			||||||
	},
 | 
						},
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// Filter the item list...
 | 
						// Filter the item list...
 | 
				
			||||||
@ -1257,6 +1277,7 @@ var BrowserPrototype = {
 | 
				
			|||||||
	//
 | 
						//
 | 
				
			||||||
	// XXX revise event...
 | 
						// XXX revise event...
 | 
				
			||||||
	pop: function(){
 | 
						pop: function(){
 | 
				
			||||||
 | 
							var that = this
 | 
				
			||||||
		var browser = this.dom
 | 
							var browser = this.dom
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		if(!this.traversable){
 | 
							if(!this.traversable){
 | 
				
			||||||
@ -1268,11 +1289,12 @@ var BrowserPrototype = {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
		// XXX should this be before or after the actual path update???
 | 
							// XXX should this be before or after the actual path update???
 | 
				
			||||||
		// XXX can we cancel the update from a handler???
 | 
							// XXX can we cancel the update from a handler???
 | 
				
			||||||
		this.trigger('pop', path)
 | 
							this
 | 
				
			||||||
 | 
								.trigger('pop', path)
 | 
				
			||||||
		this.update(path)
 | 
								.update(path)
 | 
				
			||||||
 | 
									.done(function(){
 | 
				
			||||||
		this.select('"'+dir+'"')
 | 
										that.select('"'+dir+'"')
 | 
				
			||||||
 | 
									})
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		return this
 | 
							return this
 | 
				
			||||||
	},
 | 
						},
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user