diff e2gallerypro/e2upload/Source/Uploader/Swiff.Uploader.js @ 3:3f6b44aa6b35 judyates

[svn r4] added ability to buy stuff, from a Prints page, but it doesn't work well with the css, and it also has not been fitted into the perl make system.
author rlm
date Mon, 22 Feb 2010 08:02:39 -0500
parents
children
line wrap: on
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/e2gallerypro/e2upload/Source/Uploader/Swiff.Uploader.js	Mon Feb 22 08:02:39 2010 -0500
     1.3 @@ -0,0 +1,476 @@
     1.4 +/**
     1.5 + * Swiff.Uploader - Flash FileReference Control
     1.6 + *
     1.7 + * @version		3.0 rc1
     1.8 + *
     1.9 + * @license		MIT License
    1.10 + *
    1.11 + * @author		Harald Kirschner <mail [at] digitarald [dot] de>
    1.12 + * @copyright	Authors
    1.13 + */
    1.14 +
    1.15 +Swiff.Uploader = new Class({
    1.16 +
    1.17 +	Extends: Swiff,
    1.18 +
    1.19 +	Implements: Events,
    1.20 +
    1.21 +	options: {
    1.22 +		path: 'Swiff.Uploader.swf',
    1.23 +		
    1.24 +		target: null,
    1.25 +		zIndex: 9999,
    1.26 +		
    1.27 +		height: 30,
    1.28 +		width: 100,
    1.29 +		callBacks: null,
    1.30 +		params: {
    1.31 +			wMode: 'opaque',
    1.32 +			menu: 'false',
    1.33 +			allowScriptAccess: 'always'
    1.34 +		},
    1.35 +
    1.36 +		typeFilter: null,
    1.37 +		multiple: true,
    1.38 +		queued: true,
    1.39 +		verbose: false,
    1.40 +
    1.41 +		url: null,
    1.42 +		method: null,
    1.43 +		data: null,
    1.44 +		mergeData: true,
    1.45 +		fieldName: null,
    1.46 +
    1.47 +		fileSizeMin: 1,
    1.48 +		fileSizeMax: null, // Official limit is 100 MB for FileReference!
    1.49 +		allowDuplicates: false,
    1.50 +
    1.51 +		buttonImage: null,
    1.52 +		
    1.53 +		fileListMax: 0,
    1.54 +		fileListSizeMax: 0,
    1.55 +
    1.56 +		instantStart: false,
    1.57 +		appendCookieData: false,
    1.58 +		
    1.59 +		fileClass: null
    1.60 +		/*
    1.61 +		onLoad: $empty,
    1.62 +		onFail: $empty,
    1.63 +		onStart: $empty,
    1.64 +		onQueue: $empty,
    1.65 +		onComplete: $empty,
    1.66 +		onBrowse: $empty,
    1.67 +		onDisabledBrowse: $empty,
    1.68 +		onCancel: $empty,
    1.69 +		onSelect: $empty,
    1.70 +		onSelectSuccess: $empty,
    1.71 +		onSelectFail: $empty,
    1.72 +		
    1.73 +		onButtonEnter: $empty,
    1.74 +		onButtonLeave: $empty,
    1.75 +		onButtonDown: $empty,
    1.76 +		onButtonDisable: $empty,
    1.77 +		
    1.78 +		onFileStart: $empty,
    1.79 +		onFileStop: $empty,
    1.80 +		onFileRequeue: $empty,
    1.81 +		onFileOpen: $empty,
    1.82 +		onFileProgress: $empty,
    1.83 +		onFileComplete: $empty,
    1.84 +		onFileRemove: $empty
    1.85 +		*/
    1.86 +	},
    1.87 +
    1.88 +	initialize: function(options) {
    1.89 +		// protected events to control the class, added
    1.90 +		// before setting options (which adds own events)
    1.91 +		this.addEvent('load', this.initializeSwiff, true)
    1.92 +			.addEvent('select', this.processFiles, true)
    1.93 +			.addEvent('complete', this.update, true)
    1.94 +			.addEvent('fileRemove', function(file) {
    1.95 +				this.fileList.erase(file);
    1.96 +			}.bind(this), true);
    1.97 +
    1.98 +		this.setOptions(options);
    1.99 +
   1.100 +		// callbacks are no longer in the options, every callback
   1.101 +		// is fired as event, this is just compat
   1.102 +		if (this.options.callBacks) {
   1.103 +			Hash.each(this.options.callBacks, function(fn, name) {
   1.104 +				this.addEvent(name, fn);
   1.105 +			}, this);
   1.106 +		}
   1.107 +
   1.108 +		this.options.callBacks = {
   1.109 +			fireCallback: this.fireCallback.bind(this)
   1.110 +		};
   1.111 +
   1.112 +		var path = this.options.path;
   1.113 +		if (!path.contains('?')) path += '?noCache=' + $time(); // cache in IE
   1.114 +
   1.115 +		// container options for Swiff class
   1.116 +		this.options.container = this.box = new Element('span', {'class': 'swiff-uploader-box'}).inject($(this.options.container) || document.body);
   1.117 +
   1.118 +		// target 
   1.119 +		this.target = $(this.options.target);
   1.120 +		if (this.target) {
   1.121 +			var scroll = window.getScroll();
   1.122 +			this.box.setStyles({
   1.123 +				position: 'absolute',
   1.124 +				visibility: 'visible',
   1.125 +				zIndex: this.options.zIndex,
   1.126 +				overflow: 'hidden',
   1.127 +				height: 1, width: 1,
   1.128 +				top: scroll.y, left: scroll.x
   1.129 +			});
   1.130 +			
   1.131 +			// we force wMode to transparent for the overlay effect
   1.132 +			this.parent(path, {
   1.133 +				params: {
   1.134 +					wMode: 'transparent'
   1.135 +				},
   1.136 +				height: '100%',
   1.137 +				width: '100%'
   1.138 +			});
   1.139 +			
   1.140 +			this.target.addEvent('mouseenter', this.reposition.bind(this, []));
   1.141 +			
   1.142 +			// button interactions, relayed to to the target
   1.143 +			this.addEvents({
   1.144 +				buttonEnter: this.targetRelay.bind(this, ['mouseenter']),
   1.145 +				buttonLeave: this.targetRelay.bind(this, ['mouseleave']),
   1.146 +				buttonDown: this.targetRelay.bind(this, ['mousedown']),
   1.147 +				buttonDisable: this.targetRelay.bind(this, ['disable'])
   1.148 +			});
   1.149 +			
   1.150 +			this.reposition();
   1.151 +			window.addEvent('resize', this.reposition.bind(this, []));
   1.152 +		} else {
   1.153 +			this.parent(path);
   1.154 +		}
   1.155 +
   1.156 +		this.inject(this.box);
   1.157 +
   1.158 +		this.fileList = [];
   1.159 +		
   1.160 +		this.size = this.uploading = this.bytesLoaded = this.percentLoaded = 0;
   1.161 +		
   1.162 +		if (Browser.Plugins.Flash.version < 9) {
   1.163 +			this.fireEvent('fail', ['flash']);
   1.164 +		} else {
   1.165 +			this.verifyLoad.delay(500, this);
   1.166 +		}
   1.167 +	},
   1.168 +	
   1.169 +	verifyLoad: function() {
   1.170 +		if (this.loaded) return;
   1.171 +		if (!this.object.parentNode) {
   1.172 +			this.fireEvent('fail', ['disabled']);
   1.173 +		} else if (this.object.style.display == 'none') {
   1.174 +			this.fireEvent('fail', ['hidden']);
   1.175 +		} else if (!this.object.offsetWidth) {
   1.176 +			this.fireEvent('fail', ['empty']);
   1.177 +		}
   1.178 +	},
   1.179 +
   1.180 +	fireCallback: function(name, args) {
   1.181 +		// file* callbacks are relayed to the specific file
   1.182 +		if (name.substr(0, 4) == 'file') {
   1.183 +			// updated queue data is the second argument
   1.184 +			if (args.length > 1) this.update(args[1]);
   1.185 +			var data = args[0];
   1.186 +			
   1.187 +			var file = this.findFile(data.id);
   1.188 +			this.fireEvent(name, file || data, 5);
   1.189 +			if (file) {
   1.190 +				var fire = name.replace(/^file([A-Z])/, function($0, $1) {
   1.191 +					return $1.toLowerCase();
   1.192 +				});
   1.193 +				file.update(data).fireEvent(fire, [data], 10);
   1.194 +			}
   1.195 +		} else {
   1.196 +			this.fireEvent(name, args, 5);
   1.197 +		}
   1.198 +	},
   1.199 +
   1.200 +	update: function(data) {
   1.201 +		// the data is saved right to the instance 
   1.202 +		$extend(this, data);
   1.203 +		this.fireEvent('queue', [this], 10);
   1.204 +		return this;
   1.205 +	},
   1.206 +
   1.207 +	findFile: function(id) {
   1.208 +		for (var i = 0; i < this.fileList.length; i++) {
   1.209 +			if (this.fileList[i].id == id) return this.fileList[i];
   1.210 +		}
   1.211 +		return null;
   1.212 +	},
   1.213 +
   1.214 +	initializeSwiff: function() {
   1.215 +		// extracted options for the swf 
   1.216 +		this.remote('initialize', {
   1.217 +			width: this.options.width,
   1.218 +			height: this.options.height,
   1.219 +			typeFilter: this.options.typeFilter,
   1.220 +			multiple: this.options.multiple,
   1.221 +			queued: this.options.queued,
   1.222 +			url: this.options.url,
   1.223 +			method: this.options.method,
   1.224 +			data: this.options.data,
   1.225 +			mergeData: this.options.mergeData,
   1.226 +			fieldName: this.options.fieldName,
   1.227 +			verbose: this.options.verbose,
   1.228 +			fileSizeMin: this.options.fileSizeMin,
   1.229 +			fileSizeMax: this.options.fileSizeMax,
   1.230 +			allowDuplicates: this.options.allowDuplicates,
   1.231 +			buttonImage: this.options.buttonImage
   1.232 +		});
   1.233 +
   1.234 +		this.loaded = true;
   1.235 +
   1.236 +		this.appendCookieData();
   1.237 +	},
   1.238 +	
   1.239 +	targetRelay: function(name) {
   1.240 +		if (this.target) this.target.fireEvent(name);
   1.241 +	},
   1.242 +
   1.243 +	reposition: function(coords) {
   1.244 +		// update coordinates, manual or automatically
   1.245 +		coords = coords || (this.target && this.target.offsetHeight)
   1.246 +			? this.target.getCoordinates(this.box.getOffsetParent())
   1.247 +			: {top: window.getScrollTop(), left: 0, width: 40, height: 40}
   1.248 +		this.box.setStyles(coords);
   1.249 +		this.fireEvent('reposition', [coords, this.box, this.target]);
   1.250 +	},
   1.251 +
   1.252 +	setOptions: function(options) {
   1.253 +		if (options) {
   1.254 +			if (options.url) options.url = Swiff.Uploader.qualifyPath(options.url);
   1.255 +			if (options.buttonImage) options.buttonImage = Swiff.Uploader.qualifyPath(options.buttonImage);
   1.256 +			this.parent(options);
   1.257 +			if (this.loaded) this.remote('setOptions', options);
   1.258 +		}
   1.259 +		return this;
   1.260 +	},
   1.261 +
   1.262 +	setEnabled: function(status) {
   1.263 +		this.remote('setEnabled', status);
   1.264 +	},
   1.265 +
   1.266 +	start: function() {
   1.267 +		this.remote('start');
   1.268 +	},
   1.269 +
   1.270 +	stop: function() {
   1.271 +		this.remote('stop');
   1.272 +	},
   1.273 +
   1.274 +	remove: function() {
   1.275 +		this.remote('remove');
   1.276 +	},
   1.277 +
   1.278 +	fileStart: function(file) {
   1.279 +		this.remote('fileStart', file.id);
   1.280 +	},
   1.281 +
   1.282 +	fileStop: function(file) {
   1.283 +		this.remote('fileStop', file.id);
   1.284 +	},
   1.285 +
   1.286 +	fileRemove: function(file) {
   1.287 +		this.remote('fileRemove', file.id);
   1.288 +	},
   1.289 +
   1.290 +	fileRequeue: function(file) {
   1.291 +		this.remote('fileRequeue', file.id);
   1.292 +	},
   1.293 +
   1.294 +	appendCookieData: function() {
   1.295 +		var append = this.options.appendCookieData;
   1.296 +		if (!append) return;
   1.297 +		
   1.298 +		var hash = {};
   1.299 +		document.cookie.split(/;\s*/).each(function(cookie) {
   1.300 +			cookie = cookie.split('=');
   1.301 +			if (cookie.length == 2) {
   1.302 +				hash[decodeURIComponent(cookie[0])] = decodeURIComponent(cookie[1]);
   1.303 +			}
   1.304 +		});
   1.305 +
   1.306 +		var data = this.options.data || {};
   1.307 +		if ($type(append) == 'string') data[append] = hash;
   1.308 +		else $extend(data, hash);
   1.309 +
   1.310 +		this.setOptions({data: data});
   1.311 +	},
   1.312 +
   1.313 +	processFiles: function(successraw, failraw, queue) {
   1.314 +		var cls = this.options.fileClass || Swiff.Uploader.File;
   1.315 +
   1.316 +		var fail = [], success = [];
   1.317 +
   1.318 +		if (successraw) {
   1.319 +			successraw.each(function(data) {
   1.320 +				var ret = new cls(this, data);
   1.321 +				if (!ret.validate()) {
   1.322 +					ret.remove.delay(10, ret);
   1.323 +					fail.push(ret);
   1.324 +				} else {
   1.325 +					this.size += data.size;
   1.326 +					this.fileList.push(ret);
   1.327 +					success.push(ret);
   1.328 +					ret.render();
   1.329 +				}
   1.330 +			}, this);
   1.331 +
   1.332 +			this.fireEvent('selectSuccess', [success], 10);
   1.333 +		}
   1.334 +
   1.335 +		if (failraw || fail.length) {
   1.336 +			fail.extend((failraw) ? failraw.map(function(data) {
   1.337 +				return new cls(this, data);
   1.338 +			}, this) : []).each(function(file) {
   1.339 +				file.invalidate().render();
   1.340 +			});
   1.341 +
   1.342 +			this.fireEvent('selectFail', [fail], 10);
   1.343 +		}
   1.344 +
   1.345 +		this.update(queue);
   1.346 +
   1.347 +		if (this.options.instantStart && success.length) this.start();
   1.348 +	}
   1.349 +
   1.350 +});
   1.351 +
   1.352 +$extend(Swiff.Uploader, {
   1.353 +
   1.354 +	STATUS_QUEUED: 0,
   1.355 +	STATUS_RUNNING: 1,
   1.356 +	STATUS_ERROR: 2,
   1.357 +	STATUS_COMPLETE: 3,
   1.358 +	STATUS_STOPPED: 4,
   1.359 +
   1.360 +	log: function() {
   1.361 +		if (window.console && console.info) console.info.apply(console, arguments);
   1.362 +	},
   1.363 +
   1.364 +	unitLabels: {
   1.365 +		b: [{min: 1, unit: 'B'}, {min: 1024, unit: 'kB'}, {min: 1048576, unit: 'MB'}, {min: 1073741824, unit: 'GB'}],
   1.366 +		s: [{min: 1, unit: 's'}, {min: 60, unit: 'm'}, {min: 3600, unit: 'h'}, {min: 86400, unit: 'd'}]
   1.367 +	},
   1.368 +
   1.369 +	formatUnit: function(base, type, join) {
   1.370 +		var labels = Swiff.Uploader.unitLabels[(type == 'bps') ? 'b' : type];
   1.371 +		var append = (type == 'bps') ? '/s' : '';
   1.372 +		var i, l = labels.length, value;
   1.373 +
   1.374 +		if (base < 1) return '0 ' + labels[0].unit + append;
   1.375 +
   1.376 +		if (type == 's') {
   1.377 +			var units = [];
   1.378 +
   1.379 +			for (i = l - 1; i >= 0; i--) {
   1.380 +				value = Math.floor(base / labels[i].min);
   1.381 +				if (value) {
   1.382 +					units.push(value + ' ' + labels[i].unit);
   1.383 +					base -= value * labels[i].min;
   1.384 +					if (!base) break;
   1.385 +				}
   1.386 +			}
   1.387 +
   1.388 +			return (join === false) ? units : units.join(join || ', ');
   1.389 +		}
   1.390 +
   1.391 +		for (i = l - 1; i >= 0; i--) {
   1.392 +			value = labels[i].min;
   1.393 +			if (base >= value) break;
   1.394 +		}
   1.395 +
   1.396 +		return (base / value).toFixed(1) + ' ' + labels[i].unit + append;
   1.397 +	}
   1.398 +
   1.399 +});
   1.400 +
   1.401 +Swiff.Uploader.qualifyPath = (function() {
   1.402 +	
   1.403 +	var anchor;
   1.404 +	
   1.405 +	return function(path) {
   1.406 +		(anchor || (anchor = new Element('a'))).href = path;
   1.407 +		return anchor.href;
   1.408 +	};
   1.409 +
   1.410 +})();
   1.411 +
   1.412 +Swiff.Uploader.File = new Class({
   1.413 +
   1.414 +	Implements: Events,
   1.415 +
   1.416 +	initialize: function(base, data) {
   1.417 +		this.base = base;
   1.418 +		this.update(data);
   1.419 +	},
   1.420 +
   1.421 +	update: function(data) {
   1.422 +		return $extend(this, data);
   1.423 +	},
   1.424 +
   1.425 +	validate: function() {
   1.426 +		var options = this.base.options;
   1.427 +		
   1.428 +		if (options.fileListMax && this.base.fileList.length >= options.fileListMax) {
   1.429 +			this.validationError = 'fileListMax';
   1.430 +			return false;
   1.431 +		}
   1.432 +		
   1.433 +		if (options.fileListSizeMax && (this.base.size + this.size) > options.fileListSizeMax) {
   1.434 +			this.validationError = 'fileListSizeMax';
   1.435 +			return false;
   1.436 +		}
   1.437 +		
   1.438 +		return true;
   1.439 +	},
   1.440 +
   1.441 +	invalidate: function() {
   1.442 +		this.invalid = true;
   1.443 +		this.base.fireEvent('fileInvalid', this, 10);
   1.444 +		return this.fireEvent('invalid', this, 10);
   1.445 +	},
   1.446 +
   1.447 +	render: function() {
   1.448 +		return this;
   1.449 +	},
   1.450 +
   1.451 +	setOptions: function(options) {
   1.452 +		if (options) {
   1.453 +			if (options.url) options.url = Swiff.Uploader.qualifyPath(options.url);
   1.454 +			this.base.remote('fileSetOptions', this.id, options);
   1.455 +			this.options = $merge(this.options, options);
   1.456 +		}
   1.457 +		return this;
   1.458 +	},
   1.459 +
   1.460 +	start: function() {
   1.461 +		this.base.fileStart(this);
   1.462 +		return this;
   1.463 +	},
   1.464 +
   1.465 +	stop: function() {
   1.466 +		this.base.fileStop(this);
   1.467 +		return this;
   1.468 +	},
   1.469 +
   1.470 +	remove: function() {
   1.471 +		this.base.fileRemove(this);
   1.472 +		return this;
   1.473 +	},
   1.474 +
   1.475 +	requeue: function() {
   1.476 +		this.base.fileRequeue(this);
   1.477 +	} 
   1.478 +
   1.479 +});