diff --git a/xstatic/pkg/angular_fileupload/__init__.py b/xstatic/pkg/angular_fileupload/__init__.py index f70e68e..43c9db9 100644 --- a/xstatic/pkg/angular_fileupload/__init__.py +++ b/xstatic/pkg/angular_fileupload/__init__.py @@ -11,9 +11,9 @@ NAME = __name__.split('.')[-1] # package name (e.g. 'foo' or 'foo_bar') # please use a all-lowercase valid python # package name -VERSION = '1.4.0' # version of the packaged files, please use the upstream +VERSION = '12.0.4' # version of the packaged files, please use the upstream # version number -BUILD = '2' # our package build number, so we can release new builds +BUILD = '0' # our package build number, so we can release new builds # with fixes for xstatic stuff. PACKAGE_VERSION = VERSION + '.' + BUILD # version used for PyPi @@ -24,8 +24,8 @@ CLASSIFIERS = [] KEYWORDS = '%s xstatic' % NAME # XStatic-* package maintainer: -MAINTAINER = 'Jordan OMara' -MAINTAINER_EMAIL = 'jsomara@gmail.com' +MAINTAINER = 'Rob Cresswell' +MAINTAINER_EMAIL = 'robert.cresswell@outlook.com' # this refers to the project homepage of the stuff we packaged: HOMEPAGE = 'https://github.com/danialfarid/angular-file-upload' diff --git a/xstatic/pkg/angular_fileupload/data/FileAPI.flash.swf b/xstatic/pkg/angular_fileupload/data/FileAPI.flash.swf new file mode 100644 index 0000000..65de396 Binary files /dev/null and b/xstatic/pkg/angular_fileupload/data/FileAPI.flash.swf differ diff --git a/xstatic/pkg/angular_fileupload/data/FileAPI.js b/xstatic/pkg/angular_fileupload/data/FileAPI.js new file mode 100644 index 0000000..5caa110 --- /dev/null +++ b/xstatic/pkg/angular_fileupload/data/FileAPI.js @@ -0,0 +1,4313 @@ +/*! FileAPI 2.0.7 - BSD | git://github.com/mailru/FileAPI.git + * FileAPI — a set of javascript tools for working with files. Multiupload, drag'n'drop and chunked file upload. Images: crop, resize and auto orientation by EXIF. + */ + +/* + * JavaScript Canvas to Blob 2.0.5 + * https://github.com/blueimp/JavaScript-Canvas-to-Blob + * + * Copyright 2012, Sebastian Tschan + * https://blueimp.net + * + * Licensed under the MIT license: + * http://www.opensource.org/licenses/MIT + * + * Based on stackoverflow user Stoive's code snippet: + * http://stackoverflow.com/q/4998908 + */ + +/*jslint nomen: true, regexp: true */ +/*global window, atob, Blob, ArrayBuffer, Uint8Array */ + +(function (window) { + 'use strict'; + var CanvasPrototype = window.HTMLCanvasElement && + window.HTMLCanvasElement.prototype, + hasBlobConstructor = window.Blob && (function () { + try { + return Boolean(new Blob()); + } catch (e) { + return false; + } + }()), + hasArrayBufferViewSupport = hasBlobConstructor && window.Uint8Array && + (function () { + try { + return new Blob([new Uint8Array(100)]).size === 100; + } catch (e) { + return false; + } + }()), + BlobBuilder = window.BlobBuilder || window.WebKitBlobBuilder || + window.MozBlobBuilder || window.MSBlobBuilder, + dataURLtoBlob = (hasBlobConstructor || BlobBuilder) && window.atob && + window.ArrayBuffer && window.Uint8Array && function (dataURI) { + var byteString, + arrayBuffer, + intArray, + i, + mimeString, + bb; + if (dataURI.split(',')[0].indexOf('base64') >= 0) { + // Convert base64 to raw binary data held in a string: + byteString = atob(dataURI.split(',')[1]); + } else { + // Convert base64/URLEncoded data component to raw binary data: + byteString = decodeURIComponent(dataURI.split(',')[1]); + } + // Write the bytes of the string to an ArrayBuffer: + arrayBuffer = new ArrayBuffer(byteString.length); + intArray = new Uint8Array(arrayBuffer); + for (i = 0; i < byteString.length; i += 1) { + intArray[i] = byteString.charCodeAt(i); + } + // Separate out the mime component: + mimeString = dataURI.split(',')[0].split(':')[1].split(';')[0]; + // Write the ArrayBuffer (or ArrayBufferView) to a blob: + if (hasBlobConstructor) { + return new Blob( + [hasArrayBufferViewSupport ? intArray : arrayBuffer], + {type: mimeString} + ); + } + bb = new BlobBuilder(); + bb.append(arrayBuffer); + return bb.getBlob(mimeString); + }; + if (window.HTMLCanvasElement && !CanvasPrototype.toBlob) { + if (CanvasPrototype.mozGetAsFile) { + CanvasPrototype.toBlob = function (callback, type, quality) { + if (quality && CanvasPrototype.toDataURL && dataURLtoBlob) { + callback(dataURLtoBlob(this.toDataURL(type, quality))); + } else { + callback(this.mozGetAsFile('blob', type)); + } + }; + } else if (CanvasPrototype.toDataURL && dataURLtoBlob) { + CanvasPrototype.toBlob = function (callback, type, quality) { + callback(dataURLtoBlob(this.toDataURL(type, quality))); + }; + } + } + window.dataURLtoBlob = dataURLtoBlob; +})(window); + +/*jslint evil: true */ +/*global window, URL, webkitURL, ActiveXObject */ + +(function (window, undef){ + 'use strict'; + + var + gid = 1, + noop = function (){}, + + document = window.document, + doctype = document.doctype || {}, + userAgent = window.navigator.userAgent, + + // https://github.com/blueimp/JavaScript-Load-Image/blob/master/load-image.js#L48 + apiURL = (window.createObjectURL && window) || (window.URL && URL.revokeObjectURL && URL) || (window.webkitURL && webkitURL), + + Blob = window.Blob, + File = window.File, + FileReader = window.FileReader, + FormData = window.FormData, + + + XMLHttpRequest = window.XMLHttpRequest, + jQuery = window.jQuery, + + html5 = !!(File && (FileReader && (window.Uint8Array || FormData || XMLHttpRequest.prototype.sendAsBinary))) + && !(/safari\//i.test(userAgent) && !/chrome\//i.test(userAgent) && /windows/i.test(userAgent)), // BugFix: https://github.com/mailru/FileAPI/issues/25 + + cors = html5 && ('withCredentials' in (new XMLHttpRequest)), + + chunked = html5 && !!Blob && !!(Blob.prototype.webkitSlice || Blob.prototype.mozSlice || Blob.prototype.slice), + + // https://github.com/blueimp/JavaScript-Canvas-to-Blob + dataURLtoBlob = window.dataURLtoBlob, + + + _rimg = /img/i, + _rcanvas = /canvas/i, + _rimgcanvas = /img|canvas/i, + _rinput = /input/i, + _rdata = /^data:[^,]+,/, + + _toString = {}.toString, + + + Math = window.Math, + + _SIZE_CONST = function (pow){ + pow = new window.Number(Math.pow(1024, pow)); + pow.from = function (sz){ return Math.round(sz * this); }; + return pow; + }, + + _elEvents = {}, // element event listeners + _infoReader = [], // list of file info processors + + _readerEvents = 'abort progress error load loadend', + _xhrPropsExport = 'status statusText readyState response responseXML responseText responseBody'.split(' '), + + currentTarget = 'currentTarget', // for minimize + preventDefault = 'preventDefault', // and this too + + _isArray = function (ar) { + return ar && ('length' in ar); + }, + + /** + * Iterate over a object or array + */ + _each = function (obj, fn, ctx){ + if( obj ){ + if( _isArray(obj) ){ + for( var i = 0, n = obj.length; i < n; i++ ){ + if( i in obj ){ + fn.call(ctx, obj[i], i, obj); + } + } + } + else { + for( var key in obj ){ + if( obj.hasOwnProperty(key) ){ + fn.call(ctx, obj[key], key, obj); + } + } + } + } + }, + + /** + * Merge the contents of two or more objects together into the first object + */ + _extend = function (dst){ + var args = arguments, i = 1, _ext = function (val, key){ dst[key] = val; }; + for( ; i < args.length; i++ ){ + _each(args[i], _ext); + } + return dst; + }, + + /** + * Add event listener + */ + _on = function (el, type, fn){ + if( el ){ + var uid = api.uid(el); + + if( !_elEvents[uid] ){ + _elEvents[uid] = {}; + } + + var isFileReader = (FileReader && el) && (el instanceof FileReader); + _each(type.split(/\s+/), function (type){ + if( jQuery && !isFileReader){ + jQuery.event.add(el, type, fn); + } else { + if( !_elEvents[uid][type] ){ + _elEvents[uid][type] = []; + } + + _elEvents[uid][type].push(fn); + + if( el.addEventListener ){ el.addEventListener(type, fn, false); } + else if( el.attachEvent ){ el.attachEvent('on'+type, fn); } + else { el['on'+type] = fn; } + } + }); + } + }, + + + /** + * Remove event listener + */ + _off = function (el, type, fn){ + if( el ){ + var uid = api.uid(el), events = _elEvents[uid] || {}; + + var isFileReader = (FileReader && el) && (el instanceof FileReader); + _each(type.split(/\s+/), function (type){ + if( jQuery && !isFileReader){ + jQuery.event.remove(el, type, fn); + } + else { + var fns = events[type] || [], i = fns.length; + + while( i-- ){ + if( fns[i] === fn ){ + fns.splice(i, 1); + break; + } + } + + if( el.addEventListener ){ el.removeEventListener(type, fn, false); } + else if( el.detachEvent ){ el.detachEvent('on'+type, fn); } + else { el['on'+type] = null; } + } + }); + } + }, + + + _one = function(el, type, fn){ + _on(el, type, function _(evt){ + _off(el, type, _); + fn(evt); + }); + }, + + + _fixEvent = function (evt){ + if( !evt.target ){ evt.target = window.event && window.event.srcElement || document; } + if( evt.target.nodeType === 3 ){ evt.target = evt.target.parentNode; } + return evt; + }, + + + _supportInputAttr = function (attr){ + var input = document.createElement('input'); + input.setAttribute('type', "file"); + return attr in input; + }, + + /** + * FileAPI (core object) + */ + api = { + version: '2.0.7', + + cors: false, + html5: true, + media: false, + formData: true, + multiPassResize: true, + + debug: false, + pingUrl: false, + multiFlash: false, + flashAbortTimeout: 0, + withCredentials: true, + + staticPath: './dist/', + + flashUrl: 0, // @default: './FileAPI.flash.swf' + flashImageUrl: 0, // @default: './FileAPI.flash.image.swf' + + postNameConcat: function (name, idx){ + return name + (idx != null ? '['+ idx +']' : ''); + }, + + ext2mime: { + jpg: 'image/jpeg' + , tif: 'image/tiff' + , txt: 'text/plain' + }, + + // Fallback for flash + accept: { + 'image/*': 'art bm bmp dwg dxf cbr cbz fif fpx gif ico iefs jfif jpe jpeg jpg jps jut mcf nap nif pbm pcx pgm pict pm png pnm qif qtif ras rast rf rp svf tga tif tiff xbm xbm xpm xwd' + , 'audio/*': 'm4a flac aac rm mpa wav wma ogg mp3 mp2 m3u mod amf dmf dsm far gdm imf it m15 med okt s3m stm sfx ult uni xm sid ac3 dts cue aif aiff wpl ape mac mpc mpp shn wv nsf spc gym adplug adx dsp adp ymf ast afc hps xs' + , 'video/*': 'm4v 3gp nsv ts ty strm rm rmvb m3u ifo mov qt divx xvid bivx vob nrg img iso pva wmv asf asx ogm m2v avi bin dat dvr-ms mpg mpeg mp4 mkv avc vp3 svq3 nuv viv dv fli flv wpl' + }, + + uploadRetry : 0, + networkDownRetryTimeout : 5000, // milliseconds, don't flood when network is down + + chunkSize : 0, + chunkUploadRetry : 0, + chunkNetworkDownRetryTimeout : 2000, // milliseconds, don't flood when network is down + + KB: _SIZE_CONST(1), + MB: _SIZE_CONST(2), + GB: _SIZE_CONST(3), + TB: _SIZE_CONST(4), + + EMPTY_PNG: 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAC0lEQVQIW2NkAAIAAAoAAggA9GkAAAAASUVORK5CYII=', + + expando: 'fileapi' + (new Date).getTime(), + + uid: function (obj){ + return obj + ? (obj[api.expando] = obj[api.expando] || api.uid()) + : (++gid, api.expando + gid) + ; + }, + + log: function (){ + // ngf fix for IE8 #1071 + if( api.debug && api._supportConsoleLog ){ + if( api._supportConsoleLogApply ){ + console.log.apply(console, arguments); + } + else { + console.log([].join.call(arguments, ' ')); + } + } + }, + + /** + * Create new image + * + * @param {String} [src] + * @param {Function} [fn] 1. error -- boolean, 2. img -- Image element + * @returns {HTMLElement} + */ + newImage: function (src, fn){ + var img = document.createElement('img'); + if( fn ){ + api.event.one(img, 'error load', function (evt){ + fn(evt.type == 'error', img); + img = null; + }); + } + img.src = src; + return img; + }, + + /** + * Get XHR + * @returns {XMLHttpRequest} + */ + getXHR: function (){ + var xhr; + + if( XMLHttpRequest ){ + xhr = new XMLHttpRequest; + } + else if( window.ActiveXObject ){ + try { + xhr = new ActiveXObject('MSXML2.XMLHttp.3.0'); + } catch (e) { + xhr = new ActiveXObject('Microsoft.XMLHTTP'); + } + } + + return xhr; + }, + + isArray: _isArray, + + support: { + dnd: cors && ('ondrop' in document.createElement('div')), + cors: cors, + html5: html5, + chunked: chunked, + dataURI: true, + accept: _supportInputAttr('accept'), + multiple: _supportInputAttr('multiple') + }, + + event: { + on: _on + , off: _off + , one: _one + , fix: _fixEvent + }, + + + throttle: function(fn, delay) { + var id, args; + + return function _throttle(){ + args = arguments; + + if( !id ){ + fn.apply(window, args); + id = setTimeout(function (){ + id = 0; + fn.apply(window, args); + }, delay); + } + }; + }, + + + F: function (){}, + + + parseJSON: function (str){ + var json; + if( window.JSON && JSON.parse ){ + json = JSON.parse(str); + } + else { + json = (new Function('return ('+str.replace(/([\r\n])/g, '\\$1')+');'))(); + } + return json; + }, + + + trim: function (str){ + str = String(str); + return str.trim ? str.trim() : str.replace(/^\s+|\s+$/g, ''); + }, + + /** + * Simple Defer + * @return {Object} + */ + defer: function (){ + var + list = [] + , result + , error + , defer = { + resolve: function (err, res){ + defer.resolve = noop; + error = err || false; + result = res; + + while( res = list.shift() ){ + res(error, result); + } + }, + + then: function (fn){ + if( error !== undef ){ + fn(error, result); + } else { + list.push(fn); + } + } + }; + + return defer; + }, + + queue: function (fn){ + var + _idx = 0 + , _length = 0 + , _fail = false + , _end = false + , queue = { + inc: function (){ + _length++; + }, + + next: function (){ + _idx++; + setTimeout(queue.check, 0); + }, + + check: function (){ + (_idx >= _length) && !_fail && queue.end(); + }, + + isFail: function (){ + return _fail; + }, + + fail: function (){ + !_fail && fn(_fail = true); + }, + + end: function (){ + if( !_end ){ + _end = true; + fn(); + } + } + } + ; + return queue; + }, + + + /** + * For each object + * + * @param {Object|Array} obj + * @param {Function} fn + * @param {*} [ctx] + */ + each: _each, + + + /** + * Async for + * @param {Array} array + * @param {Function} callback + */ + afor: function (array, callback){ + var i = 0, n = array.length; + + if( _isArray(array) && n-- ){ + (function _next(){ + callback(n != i && _next, array[i], i++); + })(); + } + else { + callback(false); + } + }, + + + /** + * Merge the contents of two or more objects together into the first object + * + * @param {Object} dst + * @return {Object} + */ + extend: _extend, + + + /** + * Is file? + * @param {File} file + * @return {Boolean} + */ + isFile: function (file){ + return _toString.call(file) === '[object File]'; + }, + + + /** + * Is blob? + * @param {Blob} blob + * @returns {Boolean} + */ + isBlob: function (blob) { + return this.isFile(blob) || (_toString.call(blob) === '[object Blob]'); + }, + + + /** + * Is canvas element + * + * @param {HTMLElement} el + * @return {Boolean} + */ + isCanvas: function (el){ + return el && _rcanvas.test(el.nodeName); + }, + + + getFilesFilter: function (filter){ + filter = typeof filter == 'string' ? filter : (filter.getAttribute && filter.getAttribute('accept') || ''); + return filter ? new RegExp('('+ filter.replace(/\./g, '\\.').replace(/,/g, '|') +')$', 'i') : /./; + }, + + + + /** + * Read as DataURL + * + * @param {File|Element} file + * @param {Function} fn + */ + readAsDataURL: function (file, fn){ + if( api.isCanvas(file) ){ + _emit(file, fn, 'load', api.toDataURL(file)); + } + else { + _readAs(file, fn, 'DataURL'); + } + }, + + + /** + * Read as Binary string + * + * @param {File} file + * @param {Function} fn + */ + readAsBinaryString: function (file, fn){ + if( _hasSupportReadAs('BinaryString') ){ + _readAs(file, fn, 'BinaryString'); + } else { + // Hello IE10! + _readAs(file, function (evt){ + if( evt.type == 'load' ){ + try { + // dataURL -> binaryString + evt.result = api.toBinaryString(evt.result); + } catch (e){ + evt.type = 'error'; + evt.message = e.toString(); + } + } + fn(evt); + }, 'DataURL'); + } + }, + + + /** + * Read as ArrayBuffer + * + * @param {File} file + * @param {Function} fn + */ + readAsArrayBuffer: function(file, fn){ + _readAs(file, fn, 'ArrayBuffer'); + }, + + + /** + * Read as text + * + * @param {File} file + * @param {String} encoding + * @param {Function} [fn] + */ + readAsText: function(file, encoding, fn){ + if( !fn ){ + fn = encoding; + encoding = 'utf-8'; + } + + _readAs(file, fn, 'Text', encoding); + }, + + + /** + * Convert image or canvas to DataURL + * + * @param {Element} el Image or Canvas element + * @param {String} [type] mime-type + * @return {String} + */ + toDataURL: function (el, type){ + if( typeof el == 'string' ){ + return el; + } + else if( el.toDataURL ){ + return el.toDataURL(type || 'image/png'); + } + }, + + + /** + * Canvert string, image or canvas to binary string + * + * @param {String|Element} val + * @return {String} + */ + toBinaryString: function (val){ + return window.atob(api.toDataURL(val).replace(_rdata, '')); + }, + + + /** + * Read file or DataURL as ImageElement + * + * @param {File|String} file + * @param {Function} fn + * @param {Boolean} [progress] + */ + readAsImage: function (file, fn, progress){ + if( api.isFile(file) ){ + if( apiURL ){ + /** @namespace apiURL.createObjectURL */ + var data = apiURL.createObjectURL(file); + if( data === undef ){ + _emit(file, fn, 'error'); + } + else { + api.readAsImage(data, fn, progress); + } + } + else { + api.readAsDataURL(file, function (evt){ + if( evt.type == 'load' ){ + api.readAsImage(evt.result, fn, progress); + } + else if( progress || evt.type == 'error' ){ + _emit(file, fn, evt, null, { loaded: evt.loaded, total: evt.total }); + } + }); + } + } + else if( api.isCanvas(file) ){ + _emit(file, fn, 'load', file); + } + else if( _rimg.test(file.nodeName) ){ + if( file.complete ){ + _emit(file, fn, 'load', file); + } + else { + var events = 'error abort load'; + _one(file, events, function _fn(evt){ + if( evt.type == 'load' && apiURL ){ + /** @namespace apiURL.revokeObjectURL */ + apiURL.revokeObjectURL(file.src); + } + + _off(file, events, _fn); + _emit(file, fn, evt, file); + }); + } + } + else if( file.iframe ){ + _emit(file, fn, { type: 'error' }); + } + else { + // Created image + var img = api.newImage(file.dataURL || file); + api.readAsImage(img, fn, progress); + } + }, + + + /** + * Make file by name + * + * @param {String} name + * @return {Array} + */ + checkFileObj: function (name){ + var file = {}, accept = api.accept; + + if( typeof name == 'object' ){ + file = name; + } + else { + file.name = (name + '').split(/\\|\//g).pop(); + } + + if( file.type == null ){ + file.type = file.name.split('.').pop(); + } + + _each(accept, function (ext, type){ + ext = new RegExp(ext.replace(/\s/g, '|'), 'i'); + if( ext.test(file.type) || api.ext2mime[file.type] ){ + file.type = api.ext2mime[file.type] || (type.split('/')[0] +'/'+ file.type); + } + }); + + return file; + }, + + + /** + * Get drop files + * + * @param {Event} evt + * @param {Function} callback + */ + getDropFiles: function (evt, callback){ + var + files = [] + , dataTransfer = _getDataTransfer(evt) + , entrySupport = _isArray(dataTransfer.items) && dataTransfer.items[0] && _getAsEntry(dataTransfer.items[0]) + , queue = api.queue(function (){ callback(files); }) + ; + + _each((entrySupport ? dataTransfer.items : dataTransfer.files) || [], function (item){ + queue.inc(); + + try { + if( entrySupport ){ + _readEntryAsFiles(item, function (err, entryFiles){ + if( err ){ + api.log('[err] getDropFiles:', err); + } else { + files.push.apply(files, entryFiles); + } + queue.next(); + }); + } + else { + _isRegularFile(item, function (yes){ + yes && files.push(item); + queue.next(); + }); + } + } + catch( err ){ + queue.next(); + api.log('[err] getDropFiles: ', err); + } + }); + + queue.check(); + }, + + + /** + * Get file list + * + * @param {HTMLInputElement|Event} input + * @param {String|Function} [filter] + * @param {Function} [callback] + * @return {Array|Null} + */ + getFiles: function (input, filter, callback){ + var files = []; + + if( callback ){ + api.filterFiles(api.getFiles(input), filter, callback); + return null; + } + + if( input.jquery ){ + // jQuery object + input.each(function (){ + files = files.concat(api.getFiles(this)); + }); + input = files; + files = []; + } + + if( typeof filter == 'string' ){ + filter = api.getFilesFilter(filter); + } + + if( input.originalEvent ){ + // jQuery event + input = _fixEvent(input.originalEvent); + } + else if( input.srcElement ){ + // IE Event + input = _fixEvent(input); + } + + + if( input.dataTransfer ){ + // Drag'n'Drop + input = input.dataTransfer; + } + else if( input.target ){ + // Event + input = input.target; + } + + if( input.files ){ + // Input[type="file"] + files = input.files; + + if( !html5 ){ + // Partial support for file api + files[0].blob = input; + files[0].iframe = true; + } + } + else if( !html5 && isInputFile(input) ){ + if( api.trim(input.value) ){ + files = [api.checkFileObj(input.value)]; + files[0].blob = input; + files[0].iframe = true; + } + } + else if( _isArray(input) ){ + files = input; + } + + return api.filter(files, function (file){ return !filter || filter.test(file.name); }); + }, + + + /** + * Get total file size + * @param {Array} files + * @return {Number} + */ + getTotalSize: function (files){ + var size = 0, i = files && files.length; + while( i-- ){ + size += files[i].size; + } + return size; + }, + + + /** + * Get image information + * + * @param {File} file + * @param {Function} fn + */ + getInfo: function (file, fn){ + var info = {}, readers = _infoReader.concat(); + + if( api.isFile(file) ){ + (function _next(){ + var reader = readers.shift(); + if( reader ){ + if( reader.test(file.type) ){ + reader(file, function (err, res){ + if( err ){ + fn(err); + } + else { + _extend(info, res); + _next(); + } + }); + } + else { + _next(); + } + } + else { + fn(false, info); + } + })(); + } + else { + fn('not_support_info', info); + } + }, + + + /** + * Add information reader + * + * @param {RegExp} mime + * @param {Function} fn + */ + addInfoReader: function (mime, fn){ + fn.test = function (type){ return mime.test(type); }; + _infoReader.push(fn); + }, + + + /** + * Filter of array + * + * @param {Array} input + * @param {Function} fn + * @return {Array} + */ + filter: function (input, fn){ + var result = [], i = 0, n = input.length, val; + + for( ; i < n; i++ ){ + if( i in input ){ + val = input[i]; + if( fn.call(val, val, i, input) ){ + result.push(val); + } + } + } + + return result; + }, + + + /** + * Filter files + * + * @param {Array} files + * @param {Function} eachFn + * @param {Function} resultFn + */ + filterFiles: function (files, eachFn, resultFn){ + if( files.length ){ + // HTML5 or Flash + var queue = files.concat(), file, result = [], deleted = []; + + (function _next(){ + if( queue.length ){ + file = queue.shift(); + api.getInfo(file, function (err, info){ + (eachFn(file, err ? false : info) ? result : deleted).push(file); + _next(); + }); + } + else { + resultFn(result, deleted); + } + })(); + } + else { + resultFn([], files); + } + }, + + + upload: function (options){ + options = _extend({ + jsonp: 'callback' + , prepare: api.F + , beforeupload: api.F + , upload: api.F + , fileupload: api.F + , fileprogress: api.F + , filecomplete: api.F + , progress: api.F + , complete: api.F + , pause: api.F + , imageOriginal: true + , chunkSize: api.chunkSize + , chunkUploadRetry: api.chunkUploadRetry + , uploadRetry: api.uploadRetry + }, options); + + + if( options.imageAutoOrientation && !options.imageTransform ){ + options.imageTransform = { rotate: 'auto' }; + } + + + var + proxyXHR = new api.XHR(options) + , dataArray = this._getFilesDataArray(options.files) + , _this = this + , _total = 0 + , _loaded = 0 + , _nextFile + , _complete = false + ; + + + // calc total size + _each(dataArray, function (data){ + _total += data.size; + }); + + // Array of files + proxyXHR.files = []; + _each(dataArray, function (data){ + proxyXHR.files.push(data.file); + }); + + // Set upload status props + proxyXHR.total = _total; + proxyXHR.loaded = 0; + proxyXHR.filesLeft = dataArray.length; + + // emit "beforeupload" event + options.beforeupload(proxyXHR, options); + + // Upload by file + _nextFile = function (){ + var + data = dataArray.shift() + , _file = data && data.file + , _fileLoaded = false + , _fileOptions = _simpleClone(options) + ; + + proxyXHR.filesLeft = dataArray.length; + + if( _file && _file.name === api.expando ){ + _file = null; + api.log('[warn] FileAPI.upload() — called without files'); + } + + if( ( proxyXHR.statusText != 'abort' || proxyXHR.current ) && data ){ + // Mark active job + _complete = false; + + // Set current upload file + proxyXHR.currentFile = _file; + + // Prepare file options + if (_file && options.prepare(_file, _fileOptions) === false) { + _nextFile.call(_this); + return; + } + _fileOptions.file = _file; + + _this._getFormData(_fileOptions, data, function (form){ + if( !_loaded ){ + // emit "upload" event + options.upload(proxyXHR, options); + } + + var xhr = new api.XHR(_extend({}, _fileOptions, { + + upload: _file ? function (){ + // emit "fileupload" event + options.fileupload(_file, xhr, _fileOptions); + } : noop, + + progress: _file ? function (evt){ + if( !_fileLoaded ){ + // For ignore the double calls. + _fileLoaded = (evt.loaded === evt.total); + + // emit "fileprogress" event + options.fileprogress({ + type: 'progress' + , total: data.total = evt.total + , loaded: data.loaded = evt.loaded + }, _file, xhr, _fileOptions); + + // emit "progress" event + options.progress({ + type: 'progress' + , total: _total + , loaded: proxyXHR.loaded = (_loaded + data.size * (evt.loaded/evt.total))|0 + }, _file, xhr, _fileOptions); + } + } : noop, + + complete: function (err){ + _each(_xhrPropsExport, function (name){ + proxyXHR[name] = xhr[name]; + }); + + if( _file ){ + data.total = (data.total || data.size); + data.loaded = data.total; + + if( !err ) { + // emulate 100% "progress" + this.progress(data); + + // fixed throttle event + _fileLoaded = true; + + // bytes loaded + _loaded += data.size; // data.size != data.total, it's desirable fix this + proxyXHR.loaded = _loaded; + } + + // emit "filecomplete" event + options.filecomplete(err, xhr, _file, _fileOptions); + } + + // upload next file + setTimeout(function () {_nextFile.call(_this);}, 0); + } + })); // xhr + + + // ... + proxyXHR.abort = function (current){ + if (!current) { dataArray.length = 0; } + this.current = current; + xhr.abort(); + }; + + // Start upload + xhr.send(form); + }); + } + else { + var successful = proxyXHR.status == 200 || proxyXHR.status == 201 || proxyXHR.status == 204; + options.complete(successful ? false : (proxyXHR.statusText || 'error'), proxyXHR, options); + // Mark done state + _complete = true; + } + }; + + + // Next tick + setTimeout(_nextFile, 0); + + + // Append more files to the existing request + // first - add them to the queue head/tail + proxyXHR.append = function (files, first) { + files = api._getFilesDataArray([].concat(files)); + + _each(files, function (data) { + _total += data.size; + proxyXHR.files.push(data.file); + if (first) { + dataArray.unshift(data); + } else { + dataArray.push(data); + } + }); + + proxyXHR.statusText = ""; + + if( _complete ){ + _nextFile.call(_this); + } + }; + + + // Removes file from queue by file reference and returns it + proxyXHR.remove = function (file) { + var i = dataArray.length, _file; + while( i-- ){ + if( dataArray[i].file == file ){ + _file = dataArray.splice(i, 1); + _total -= _file.size; + } + } + return _file; + }; + + return proxyXHR; + }, + + + _getFilesDataArray: function (data){ + var files = [], oFiles = {}; + + if( isInputFile(data) ){ + var tmp = api.getFiles(data); + oFiles[data.name || 'file'] = data.getAttribute('multiple') !== null ? tmp : tmp[0]; + } + else if( _isArray(data) && isInputFile(data[0]) ){ + _each(data, function (input){ + oFiles[input.name || 'file'] = api.getFiles(input); + }); + } + else { + oFiles = data; + } + + _each(oFiles, function add(file, name){ + if( _isArray(file) ){ + _each(file, function (file){ + add(file, name); + }); + } + else if( file && (file.name || file.image) ){ + files.push({ + name: name + , file: file + , size: file.size + , total: file.size + , loaded: 0 + }); + } + }); + + if( !files.length ){ + // Create fake `file` object + files.push({ file: { name: api.expando } }); + } + + return files; + }, + + + _getFormData: function (options, data, fn){ + var + file = data.file + , name = data.name + , filename = file.name + , filetype = file.type + , trans = api.support.transform && options.imageTransform + , Form = new api.Form + , queue = api.queue(function (){ fn(Form); }) + , isOrignTrans = trans && _isOriginTransform(trans) + , postNameConcat = api.postNameConcat + ; + + // Append data + _each(options.data, function add(val, name){ + if( typeof val == 'object' ){ + _each(val, function (v, i){ + add(v, postNameConcat(name, i)); + }); + } + else { + Form.append(name, val); + } + }); + + (function _addFile(file/**Object*/){ + if( file.image ){ // This is a FileAPI.Image + queue.inc(); + + file.toData(function (err, image){ + // @todo: error + filename = filename || (new Date).getTime()+'.png'; + + _addFile(image); + queue.next(); + }); + } + else if( api.Image && trans && (/^image/.test(file.type) || _rimgcanvas.test(file.nodeName)) ){ + queue.inc(); + + if( isOrignTrans ){ + // Convert to array for transform function + trans = [trans]; + } + + api.Image.transform(file, trans, options.imageAutoOrientation, function (err, images){ + if( isOrignTrans && !err ){ + if( !dataURLtoBlob && !api.flashEngine ){ + // Canvas.toBlob or Flash not supported, use multipart + Form.multipart = true; + } + + Form.append(name, images[0], filename, trans[0].type || filetype); + } + else { + var addOrigin = 0; + + if( !err ){ + _each(images, function (image, idx){ + if( !dataURLtoBlob && !api.flashEngine ){ + Form.multipart = true; + } + + if( !trans[idx].postName ){ + addOrigin = 1; + } + + Form.append(trans[idx].postName || postNameConcat(name, idx), image, filename, trans[idx].type || filetype); + }); + } + + if( err || options.imageOriginal ){ + Form.append(postNameConcat(name, (addOrigin ? 'original' : null)), file, filename, filetype); + } + } + + queue.next(); + }); + } + else if( filename !== api.expando ){ + Form.append(name, file, filename); + } + })(file); + + queue.check(); + }, + + + reset: function (inp, notRemove){ + var parent, clone; + + if( jQuery ){ + clone = jQuery(inp).clone(true).insertBefore(inp).val('')[0]; + if( !notRemove ){ + jQuery(inp).remove(); + } + } else { + parent = inp.parentNode; + clone = parent.insertBefore(inp.cloneNode(true), inp); + clone.value = ''; + + if( !notRemove ){ + parent.removeChild(inp); + } + + _each(_elEvents[api.uid(inp)], function (fns, type){ + _each(fns, function (fn){ + _off(inp, type, fn); + _on(clone, type, fn); + }); + }); + } + + return clone; + }, + + + /** + * Load remote file + * + * @param {String} url + * @param {Function} fn + * @return {XMLHttpRequest} + */ + load: function (url, fn){ + var xhr = api.getXHR(); + if( xhr ){ + xhr.open('GET', url, true); + + if( xhr.overrideMimeType ){ + xhr.overrideMimeType('text/plain; charset=x-user-defined'); + } + + _on(xhr, 'progress', function (/**Event*/evt){ + /** @namespace evt.lengthComputable */ + if( evt.lengthComputable ){ + fn({ type: evt.type, loaded: evt.loaded, total: evt.total }, xhr); + } + }); + + xhr.onreadystatechange = function(){ + if( xhr.readyState == 4 ){ + xhr.onreadystatechange = null; + if( xhr.status == 200 ){ + url = url.split('/'); + /** @namespace xhr.responseBody */ + var file = { + name: url[url.length-1] + , size: xhr.getResponseHeader('Content-Length') + , type: xhr.getResponseHeader('Content-Type') + }; + file.dataURL = 'data:'+file.type+';base64,' + api.encode64(xhr.responseBody || xhr.responseText); + fn({ type: 'load', result: file }, xhr); + } + else { + fn({ type: 'error' }, xhr); + } + } + }; + xhr.send(null); + } else { + fn({ type: 'error' }); + } + + return xhr; + }, + + encode64: function (str){ + var b64 = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=', outStr = '', i = 0; + + if( typeof str !== 'string' ){ + str = String(str); + } + + while( i < str.length ){ + //all three "& 0xff" added below are there to fix a known bug + //with bytes returned by xhr.responseText + var + byte1 = str.charCodeAt(i++) & 0xff + , byte2 = str.charCodeAt(i++) & 0xff + , byte3 = str.charCodeAt(i++) & 0xff + , enc1 = byte1 >> 2 + , enc2 = ((byte1 & 3) << 4) | (byte2 >> 4) + , enc3, enc4 + ; + + if( isNaN(byte2) ){ + enc3 = enc4 = 64; + } else { + enc3 = ((byte2 & 15) << 2) | (byte3 >> 6); + enc4 = isNaN(byte3) ? 64 : byte3 & 63; + } + + outStr += b64.charAt(enc1) + b64.charAt(enc2) + b64.charAt(enc3) + b64.charAt(enc4); + } + + return outStr; + } + + } // api + ; + + + function _emit(target, fn, name, res, ext){ + var evt = { + type: name.type || name + , target: target + , result: res + }; + _extend(evt, ext); + fn(evt); + } + + + function _hasSupportReadAs(as){ + return FileReader && !!FileReader.prototype['readAs'+as]; + } + + + function _readAs(file, fn, as, encoding){ + if( api.isBlob(file) && _hasSupportReadAs(as) ){ + var Reader = new FileReader; + + // Add event listener + _on(Reader, _readerEvents, function _fn(evt){ + var type = evt.type; + if( type == 'progress' ){ + _emit(file, fn, evt, evt.target.result, { loaded: evt.loaded, total: evt.total }); + } + else if( type == 'loadend' ){ + _off(Reader, _readerEvents, _fn); + Reader = null; + } + else { + _emit(file, fn, evt, evt.target.result); + } + }); + + + try { + // ReadAs ... + if( encoding ){ + Reader['readAs'+as](file, encoding); + } + else { + Reader['readAs'+as](file); + } + } + catch (err){ + _emit(file, fn, 'error', undef, { error: err.toString() }); + } + } + else { + _emit(file, fn, 'error', undef, { error: 'FileReader_not_support_'+as }); + } + } + + + function _isRegularFile(file, callback){ + // http://stackoverflow.com/questions/8856628/detecting-folders-directories-in-javascript-filelist-objects + if( !file.type && (file.size % 4096) === 0 && (file.size <= 102400) ){ + if( FileReader ){ + try { + var Reader = new FileReader(); + + _one(Reader, _readerEvents, function (evt){ + var isFile = evt.type != 'error'; + callback(isFile); + if( isFile ){ + Reader.abort(); + } + }); + + Reader.readAsDataURL(file); + } catch( err ){ + callback(false); + } + } + else { + callback(null); + } + } + else { + callback(true); + } + } + + + function _getAsEntry(item){ + var entry; + if( item.getAsEntry ){ entry = item.getAsEntry(); } + else if( item.webkitGetAsEntry ){ entry = item.webkitGetAsEntry(); } + return entry; + } + + + function _readEntryAsFiles(entry, callback){ + if( !entry ){ + // error + callback('invalid entry'); + } + else if( entry.isFile ){ + // Read as file + entry.file(function(file){ + // success + file.fullPath = entry.fullPath; + callback(false, [file]); + }, function (err){ + // error + callback('FileError.code: '+err.code); + }); + } + else if( entry.isDirectory ){ + var reader = entry.createReader(), result = []; + + reader.readEntries(function(entries){ + // success + api.afor(entries, function (next, entry){ + _readEntryAsFiles(entry, function (err, files){ + if( err ){ + api.log(err); + } + else { + result = result.concat(files); + } + + if( next ){ + next(); + } + else { + callback(false, result); + } + }); + }); + }, function (err){ + // error + callback('directory_reader: ' + err); + }); + } + else { + _readEntryAsFiles(_getAsEntry(entry), callback); + } + } + + + function _simpleClone(obj){ + var copy = {}; + _each(obj, function (val, key){ + if( val && (typeof val === 'object') && (val.nodeType === void 0) ){ + val = _extend({}, val); + } + copy[key] = val; + }); + return copy; + } + + + function isInputFile(el){ + return _rinput.test(el && el.tagName); + } + + + function _getDataTransfer(evt){ + return (evt.originalEvent || evt || '').dataTransfer || {}; + } + + + function _isOriginTransform(trans){ + var key; + for( key in trans ){ + if( trans.hasOwnProperty(key) ){ + if( !(trans[key] instanceof Object || key === 'overlay' || key === 'filter') ){ + return true; + } + } + } + return false; + } + + + // Add default image info reader + api.addInfoReader(/^image/, function (file/**File*/, callback/**Function*/){ + if( !file.__dimensions ){ + var defer = file.__dimensions = api.defer(); + + api.readAsImage(file, function (evt){ + var img = evt.target; + defer.resolve(evt.type == 'load' ? false : 'error', { + width: img.width + , height: img.height + }); + img.src = api.EMPTY_PNG; + img = null; + }); + } + + file.__dimensions.then(callback); + }); + + + /** + * Drag'n'Drop special event + * + * @param {HTMLElement} el + * @param {Function} onHover + * @param {Function} onDrop + */ + api.event.dnd = function (el, onHover, onDrop){ + var _id, _type; + + if( !onDrop ){ + onDrop = onHover; + onHover = api.F; + } + + if( FileReader ){ + // Hover + _on(el, 'dragenter dragleave dragover', onHover.ff = onHover.ff || function (evt){ + var + types = _getDataTransfer(evt).types + , i = types && types.length + , debounceTrigger = false + ; + + while( i-- ){ + if( ~types[i].indexOf('File') ){ + evt[preventDefault](); + + if( _type !== evt.type ){ + _type = evt.type; // Store current type of event + + if( _type != 'dragleave' ){ + onHover.call(evt[currentTarget], true, evt); + } + + debounceTrigger = true; + } + + break; // exit from "while" + } + } + + if( debounceTrigger ){ + clearTimeout(_id); + _id = setTimeout(function (){ + onHover.call(evt[currentTarget], _type != 'dragleave', evt); + }, 50); + } + }); + + + // Drop + _on(el, 'drop', onDrop.ff = onDrop.ff || function (evt){ + evt[preventDefault](); + + _type = 0; + onHover.call(evt[currentTarget], false, evt); + + api.getDropFiles(evt, function (files){ + onDrop.call(evt[currentTarget], files, evt); + }); + }); + } + else { + api.log("Drag'n'Drop -- not supported"); + } + }; + + + /** + * Remove drag'n'drop + * @param {HTMLElement} el + * @param {Function} onHover + * @param {Function} onDrop + */ + api.event.dnd.off = function (el, onHover, onDrop){ + _off(el, 'dragenter dragleave dragover', onHover.ff); + _off(el, 'drop', onDrop.ff); + }; + + + // Support jQuery + if( jQuery && !jQuery.fn.dnd ){ + jQuery.fn.dnd = function (onHover, onDrop){ + return this.each(function (){ + api.event.dnd(this, onHover, onDrop); + }); + }; + + jQuery.fn.offdnd = function (onHover, onDrop){ + return this.each(function (){ + api.event.dnd.off(this, onHover, onDrop); + }); + }; + } + + // @export + window.FileAPI = _extend(api, window.FileAPI); + + + // Debug info + api.log('FileAPI: ' + api.version); + api.log('protocol: ' + window.location.protocol); + api.log('doctype: [' + doctype.name + '] ' + doctype.publicId + ' ' + doctype.systemId); + + + // @detect 'x-ua-compatible' + _each(document.getElementsByTagName('meta'), function (meta){ + if( /x-ua-compatible/i.test(meta.getAttribute('http-equiv')) ){ + api.log('meta.http-equiv: ' + meta.getAttribute('content')); + } + }); + + + // configuration + try { + api._supportConsoleLog = !!console.log; + api._supportConsoleLogApply = !!console.log.apply; + } catch (err) {} + + if( !api.flashUrl ){ api.flashUrl = api.staticPath + 'FileAPI.flash.swf'; } + if( !api.flashImageUrl ){ api.flashImageUrl = api.staticPath + 'FileAPI.flash.image.swf'; } + if( !api.flashWebcamUrl ){ api.flashWebcamUrl = api.staticPath + 'FileAPI.flash.camera.swf'; } +})(window, void 0); + +/*global window, FileAPI, document */ + +(function (api, document, undef) { + 'use strict'; + + var + min = Math.min, + round = Math.round, + getCanvas = function () { return document.createElement('canvas'); }, + support = false, + exifOrientation = { + 8: 270 + , 3: 180 + , 6: 90 + , 7: 270 + , 4: 180 + , 5: 90 + } + ; + + try { + support = getCanvas().toDataURL('image/png').indexOf('data:image/png') > -1; + } + catch (e){} + + + function Image(file){ + if( file instanceof Image ){ + var img = new Image(file.file); + api.extend(img.matrix, file.matrix); + return img; + } + else if( !(this instanceof Image) ){ + return new Image(file); + } + + this.file = file; + this.size = file.size || 100; + + this.matrix = { + sx: 0, + sy: 0, + sw: 0, + sh: 0, + dx: 0, + dy: 0, + dw: 0, + dh: 0, + resize: 0, // min, max OR preview + deg: 0, + quality: 1, // jpeg quality + filter: 0 + }; + } + + + Image.prototype = { + image: true, + constructor: Image, + + set: function (attrs){ + api.extend(this.matrix, attrs); + return this; + }, + + crop: function (x, y, w, h){ + if( w === undef ){ + w = x; + h = y; + x = y = 0; + } + return this.set({ sx: x, sy: y, sw: w, sh: h || w }); + }, + + resize: function (w, h, strategy){ + if( /min|max/.test(h) ){ + strategy = h; + h = w; + } + + return this.set({ dw: w, dh: h || w, resize: strategy }); + }, + + preview: function (w, h){ + return this.resize(w, h || w, 'preview'); + }, + + rotate: function (deg){ + return this.set({ deg: deg }); + }, + + filter: function (filter){ + return this.set({ filter: filter }); + }, + + overlay: function (images){ + return this.set({ overlay: images }); + }, + + clone: function (){ + return new Image(this); + }, + + _load: function (image, fn){ + var self = this; + + if( /img|video/i.test(image.nodeName) ){ + fn.call(self, null, image); + } + else { + api.readAsImage(image, function (evt){ + fn.call(self, evt.type != 'load', evt.result); + }); + } + }, + + _apply: function (image, fn){ + var + canvas = getCanvas() + , m = this.getMatrix(image) + , ctx = canvas.getContext('2d') + , width = image.videoWidth || image.width + , height = image.videoHeight || image.height + , deg = m.deg + , dw = m.dw + , dh = m.dh + , w = width + , h = height + , filter = m.filter + , copy // canvas copy + , buffer = image + , overlay = m.overlay + , queue = api.queue(function (){ image.src = api.EMPTY_PNG; fn(false, canvas); }) + , renderImageToCanvas = api.renderImageToCanvas + ; + + // Normalize angle + deg = deg - Math.floor(deg/360)*360; + + // For `renderImageToCanvas` + image._type = this.file.type; + + while(m.multipass && min(w/dw, h/dh) > 2 ){ + w = (w/2 + 0.5)|0; + h = (h/2 + 0.5)|0; + + copy = getCanvas(); + copy.width = w; + copy.height = h; + + if( buffer !== image ){ + renderImageToCanvas(copy, buffer, 0, 0, buffer.width, buffer.height, 0, 0, w, h); + buffer = copy; + } + else { + buffer = copy; + renderImageToCanvas(buffer, image, m.sx, m.sy, m.sw, m.sh, 0, 0, w, h); + m.sx = m.sy = m.sw = m.sh = 0; + } + } + + + canvas.width = (deg % 180) ? dh : dw; + canvas.height = (deg % 180) ? dw : dh; + + canvas.type = m.type; + canvas.quality = m.quality; + + ctx.rotate(deg * Math.PI / 180); + renderImageToCanvas(ctx.canvas, buffer + , m.sx, m.sy + , m.sw || buffer.width + , m.sh || buffer.height + , (deg == 180 || deg == 270 ? -dw : 0) + , (deg == 90 || deg == 180 ? -dh : 0) + , dw, dh + ); + dw = canvas.width; + dh = canvas.height; + + // Apply overlay + overlay && api.each([].concat(overlay), function (over){ + queue.inc(); + // preload + var img = new window.Image, fn = function (){ + var + x = over.x|0 + , y = over.y|0 + , w = over.w || img.width + , h = over.h || img.height + , rel = over.rel + ; + + // center | right | left + x = (rel == 1 || rel == 4 || rel == 7) ? (dw - w + x)/2 : (rel == 2 || rel == 5 || rel == 8 ? dw - (w + x) : x); + + // center | bottom | top + y = (rel == 3 || rel == 4 || rel == 5) ? (dh - h + y)/2 : (rel >= 6 ? dh - (h + y) : y); + + api.event.off(img, 'error load abort', fn); + + try { + ctx.globalAlpha = over.opacity || 1; + ctx.drawImage(img, x, y, w, h); + } + catch (er){} + + queue.next(); + }; + + api.event.on(img, 'error load abort', fn); + img.src = over.src; + + if( img.complete ){ + fn(); + } + }); + + if( filter ){ + queue.inc(); + Image.applyFilter(canvas, filter, queue.next); + } + + queue.check(); + }, + + getMatrix: function (image){ + var + m = api.extend({}, this.matrix) + , sw = m.sw = m.sw || image.videoWidth || image.naturalWidth || image.width + , sh = m.sh = m.sh || image.videoHeight || image.naturalHeight || image.height + , dw = m.dw = m.dw || sw + , dh = m.dh = m.dh || sh + , sf = sw/sh, df = dw/dh + , strategy = m.resize + ; + + if( strategy == 'preview' ){ + if( dw != sw || dh != sh ){ + // Make preview + var w, h; + + if( df >= sf ){ + w = sw; + h = w / df; + } else { + h = sh; + w = h * df; + } + + if( w != sw || h != sh ){ + m.sx = ~~((sw - w)/2); + m.sy = ~~((sh - h)/2); + sw = w; + sh = h; + } + } + } + else if( strategy ){ + if( !(sw > dw || sh > dh) ){ + dw = sw; + dh = sh; + } + else if( strategy == 'min' ){ + dw = round(sf < df ? min(sw, dw) : dh*sf); + dh = round(sf < df ? dw/sf : min(sh, dh)); + } + else { + dw = round(sf >= df ? min(sw, dw) : dh*sf); + dh = round(sf >= df ? dw/sf : min(sh, dh)); + } + } + + m.sw = sw; + m.sh = sh; + m.dw = dw; + m.dh = dh; + m.multipass = api.multiPassResize; + return m; + }, + + _trans: function (fn){ + this._load(this.file, function (err, image){ + if( err ){ + fn(err); + } + else { + try { + this._apply(image, fn); + } catch (err){ + api.log('[err] FileAPI.Image.fn._apply:', err); + fn(err); + } + } + }); + }, + + + get: function (fn){ + if( api.support.transform ){ + var _this = this, matrix = _this.matrix; + + if( matrix.deg == 'auto' ){ + api.getInfo(_this.file, function (err, info){ + // rotate by exif orientation + matrix.deg = exifOrientation[info && info.exif && info.exif.Orientation] || 0; + _this._trans(fn); + }); + } + else { + _this._trans(fn); + } + } + else { + fn('not_support_transform'); + } + + return this; + }, + + + toData: function (fn){ + return this.get(fn); + } + + }; + + + Image.exifOrientation = exifOrientation; + + + Image.transform = function (file, transform, autoOrientation, fn){ + function _transform(err, img){ + // img -- info object + var + images = {} + , queue = api.queue(function (err){ + fn(err, images); + }) + ; + + if( !err ){ + api.each(transform, function (params, name){ + if( !queue.isFail() ){ + var ImgTrans = new Image(img.nodeType ? img : file), isFn = typeof params == 'function'; + + if( isFn ){ + params(img, ImgTrans); + } + else if( params.width ){ + ImgTrans[params.preview ? 'preview' : 'resize'](params.width, params.height, params.strategy); + } + else { + if( params.maxWidth && (img.width > params.maxWidth || img.height > params.maxHeight) ){ + ImgTrans.resize(params.maxWidth, params.maxHeight, 'max'); + } + } + + if( params.crop ){ + var crop = params.crop; + ImgTrans.crop(crop.x|0, crop.y|0, crop.w || crop.width, crop.h || crop.height); + } + + if( params.rotate === undef && autoOrientation ){ + params.rotate = 'auto'; + } + + ImgTrans.set({ type: ImgTrans.matrix.type || params.type || file.type || 'image/png' }); + + if( !isFn ){ + ImgTrans.set({ + deg: params.rotate + , overlay: params.overlay + , filter: params.filter + , quality: params.quality || 1 + }); + } + + queue.inc(); + ImgTrans.toData(function (err, image){ + if( err ){ + queue.fail(); + } + else { + images[name] = image; + queue.next(); + } + }); + } + }); + } + else { + queue.fail(); + } + } + + + // @todo: Оло-ло, нужно рефакторить это место + if( file.width ){ + _transform(false, file); + } else { + api.getInfo(file, _transform); + } + }; + + + // @const + api.each(['TOP', 'CENTER', 'BOTTOM'], function (x, i){ + api.each(['LEFT', 'CENTER', 'RIGHT'], function (y, j){ + Image[x+'_'+y] = i*3 + j; + Image[y+'_'+x] = i*3 + j; + }); + }); + + + /** + * Trabsform element to canvas + * + * @param {Image|HTMLVideoElement} el + * @returns {Canvas} + */ + Image.toCanvas = function(el){ + var canvas = document.createElement('canvas'); + canvas.width = el.videoWidth || el.width; + canvas.height = el.videoHeight || el.height; + canvas.getContext('2d').drawImage(el, 0, 0); + return canvas; + }; + + + /** + * Create image from DataURL + * @param {String} dataURL + * @param {Object} size + * @param {Function} callback + */ + Image.fromDataURL = function (dataURL, size, callback){ + var img = api.newImage(dataURL); + api.extend(img, size); + callback(img); + }; + + + /** + * Apply filter (caman.js) + * + * @param {Canvas|Image} canvas + * @param {String|Function} filter + * @param {Function} doneFn + */ + Image.applyFilter = function (canvas, filter, doneFn){ + if( typeof filter == 'function' ){ + filter(canvas, doneFn); + } + else if( window.Caman ){ + // http://camanjs.com/guides/ + window.Caman(canvas.tagName == 'IMG' ? Image.toCanvas(canvas) : canvas, function (){ + if( typeof filter == 'string' ){ + this[filter](); + } + else { + api.each(filter, function (val, method){ + this[method](val); + }, this); + } + this.render(doneFn); + }); + } + }; + + + /** + * For load-image-ios.js + */ + api.renderImageToCanvas = function (canvas, img, sx, sy, sw, sh, dx, dy, dw, dh){ + try { + return canvas.getContext('2d').drawImage(img, sx, sy, sw, sh, dx, dy, dw, dh); + } catch (ex) { + api.log('renderImageToCanvas failed'); + throw ex; + } + }; + + + // @export + api.support.canvas = api.support.transform = support; + api.Image = Image; +})(FileAPI, document); + +/* + * JavaScript Load Image iOS scaling fixes 1.0.3 + * https://github.com/blueimp/JavaScript-Load-Image + * + * Copyright 2013, Sebastian Tschan + * https://blueimp.net + * + * iOS image scaling fixes based on + * https://github.com/stomita/ios-imagefile-megapixel + * + * Licensed under the MIT license: + * http://www.opensource.org/licenses/MIT + */ + +/*jslint nomen: true, bitwise: true */ +/*global FileAPI, window, document */ + +(function (factory) { + 'use strict'; + factory(FileAPI); +}(function (loadImage) { + 'use strict'; + + // Only apply fixes on the iOS platform: + if (!window.navigator || !window.navigator.platform || + !(/iP(hone|od|ad)/).test(window.navigator.platform)) { + return; + } + + var originalRenderMethod = loadImage.renderImageToCanvas; + + // Detects subsampling in JPEG images: + loadImage.detectSubsampling = function (img) { + var canvas, + context; + if (img.width * img.height > 1024 * 1024) { // only consider mexapixel images + canvas = document.createElement('canvas'); + canvas.width = canvas.height = 1; + context = canvas.getContext('2d'); + context.drawImage(img, -img.width + 1, 0); + // subsampled image becomes half smaller in rendering size. + // check alpha channel value to confirm image is covering edge pixel or not. + // if alpha value is 0 image is not covering, hence subsampled. + return context.getImageData(0, 0, 1, 1).data[3] === 0; + } + return false; + }; + + // Detects vertical squash in JPEG images: + loadImage.detectVerticalSquash = function (img, subsampled) { + var naturalHeight = img.naturalHeight || img.height, + canvas = document.createElement('canvas'), + context = canvas.getContext('2d'), + data, + sy, + ey, + py, + alpha; + if (subsampled) { + naturalHeight /= 2; + } + canvas.width = 1; + canvas.height = naturalHeight; + context.drawImage(img, 0, 0); + data = context.getImageData(0, 0, 1, naturalHeight).data; + // search image edge pixel position in case it is squashed vertically: + sy = 0; + ey = naturalHeight; + py = naturalHeight; + while (py > sy) { + alpha = data[(py - 1) * 4 + 3]; + if (alpha === 0) { + ey = py; + } else { + sy = py; + } + py = (ey + sy) >> 1; + } + return (py / naturalHeight) || 1; + }; + + // Renders image to canvas while working around iOS image scaling bugs: + // https://github.com/blueimp/JavaScript-Load-Image/issues/13 + loadImage.renderImageToCanvas = function ( + canvas, + img, + sourceX, + sourceY, + sourceWidth, + sourceHeight, + destX, + destY, + destWidth, + destHeight + ) { + if (img._type === 'image/jpeg') { + var context = canvas.getContext('2d'), + tmpCanvas = document.createElement('canvas'), + tileSize = 1024, + tmpContext = tmpCanvas.getContext('2d'), + subsampled, + vertSquashRatio, + tileX, + tileY; + tmpCanvas.width = tileSize; + tmpCanvas.height = tileSize; + context.save(); + subsampled = loadImage.detectSubsampling(img); + if (subsampled) { + sourceX /= 2; + sourceY /= 2; + sourceWidth /= 2; + sourceHeight /= 2; + } + vertSquashRatio = loadImage.detectVerticalSquash(img, subsampled); + if (subsampled || vertSquashRatio !== 1) { + sourceY *= vertSquashRatio; + destWidth = Math.ceil(tileSize * destWidth / sourceWidth); + destHeight = Math.ceil( + tileSize * destHeight / sourceHeight / vertSquashRatio + ); + destY = 0; + tileY = 0; + while (tileY < sourceHeight) { + destX = 0; + tileX = 0; + while (tileX < sourceWidth) { + tmpContext.clearRect(0, 0, tileSize, tileSize); + tmpContext.drawImage( + img, + sourceX, + sourceY, + sourceWidth, + sourceHeight, + -tileX, + -tileY, + sourceWidth, + sourceHeight + ); + context.drawImage( + tmpCanvas, + 0, + 0, + tileSize, + tileSize, + destX, + destY, + destWidth, + destHeight + ); + tileX += tileSize; + destX += destWidth; + } + tileY += tileSize; + destY += destHeight; + } + context.restore(); + return canvas; + } + } + return originalRenderMethod( + canvas, + img, + sourceX, + sourceY, + sourceWidth, + sourceHeight, + destX, + destY, + destWidth, + destHeight + ); + }; + +})); + +/*global window, FileAPI */ + +(function (api, window){ + "use strict"; + + var + document = window.document + , FormData = window.FormData + , Form = function (){ this.items = []; } + , encodeURIComponent = window.encodeURIComponent + ; + + + Form.prototype = { + + append: function (name, blob, file, type){ + this.items.push({ + name: name + , blob: blob && blob.blob || (blob == void 0 ? '' : blob) + , file: blob && (file || blob.name) + , type: blob && (type || blob.type) + }); + }, + + each: function (fn){ + var i = 0, n = this.items.length; + for( ; i < n; i++ ){ + fn.call(this, this.items[i]); + } + }, + + toData: function (fn, options){ + // allow chunked transfer if we have only one file to send + // flag is used below and in XHR._send + options._chunked = api.support.chunked && options.chunkSize > 0 && api.filter(this.items, function (item){ return item.file; }).length == 1; + + if( !api.support.html5 ){ + api.log('FileAPI.Form.toHtmlData'); + this.toHtmlData(fn); + } + else if( !api.formData || this.multipart || !FormData ){ + api.log('FileAPI.Form.toMultipartData'); + this.toMultipartData(fn); + } + else if( options._chunked ){ + api.log('FileAPI.Form.toPlainData'); + this.toPlainData(fn); + } + else { + api.log('FileAPI.Form.toFormData'); + this.toFormData(fn); + } + }, + + _to: function (data, complete, next, arg){ + var queue = api.queue(function (){ + complete(data); + }); + + this.each(function (file){ + next(file, data, queue, arg); + }); + + queue.check(); + }, + + + toHtmlData: function (fn){ + this._to(document.createDocumentFragment(), fn, function (file, data/**DocumentFragment*/){ + var blob = file.blob, hidden; + + if( file.file ){ + api.reset(blob, true); + // set new name + blob.name = file.name; + blob.disabled = false; + data.appendChild(blob); + } + else { + hidden = document.createElement('input'); + hidden.name = file.name; + hidden.type = 'hidden'; + hidden.value = blob; + data.appendChild(hidden); + } + }); + }, + + toPlainData: function (fn){ + this._to({}, fn, function (file, data, queue){ + if( file.file ){ + data.type = file.file; + } + + if( file.blob.toBlob ){ + // canvas + queue.inc(); + _convertFile(file, function (file, blob){ + data.name = file.name; + data.file = blob; + data.size = blob.length; + data.type = file.type; + queue.next(); + }); + } + else if( file.file ){ + // file + data.name = file.blob.name; + data.file = file.blob; + data.size = file.blob.size; + data.type = file.type; + } + else { + // additional data + if( !data.params ){ + data.params = []; + } + data.params.push(encodeURIComponent(file.name) +"="+ encodeURIComponent(file.blob)); + } + + data.start = -1; + data.end = data.file && data.file.FileAPIReadPosition || -1; + data.retry = 0; + }); + }, + + toFormData: function (fn){ + this._to(new FormData, fn, function (file, data, queue){ + if( file.blob && file.blob.toBlob ){ + queue.inc(); + _convertFile(file, function (file, blob){ + data.append(file.name, blob, file.file); + queue.next(); + }); + } + else if( file.file ){ + data.append(file.name, file.blob, file.file); + } + else { + data.append(file.name, file.blob); + } + + if( file.file ){ + data.append('_'+file.name, file.file); + } + }); + }, + + + toMultipartData: function (fn){ + this._to([], fn, function (file, data, queue, boundary){ + queue.inc(); + _convertFile(file, function (file, blob){ + data.push( + '--_' + boundary + ('\r\nContent-Disposition: form-data; name="'+ file.name +'"'+ (file.file ? '; filename="'+ encodeURIComponent(file.file) +'"' : '') + + (file.file ? '\r\nContent-Type: '+ (file.type || 'application/octet-stream') : '') + + '\r\n' + + '\r\n'+ (file.file ? blob : encodeURIComponent(blob)) + + '\r\n') + ); + queue.next(); + }, true); + }, api.expando); + } + }; + + + function _convertFile(file, fn, useBinaryString){ + var blob = file.blob, filename = file.file; + + if( filename ){ + if( !blob.toDataURL ){ + // The Blob is not an image. + api.readAsBinaryString(blob, function (evt){ + if( evt.type == 'load' ){ + fn(file, evt.result); + } + }); + return; + } + + var + mime = { 'image/jpeg': '.jpe?g', 'image/png': '.png' } + , type = mime[file.type] ? file.type : 'image/png' + , ext = mime[type] || '.png' + , quality = blob.quality || 1 + ; + + if( !filename.match(new RegExp(ext+'$', 'i')) ){ + // Does not change the current extension, but add a new one. + filename += ext.replace('?', ''); + } + + file.file = filename; + file.type = type; + + if( !useBinaryString && blob.toBlob ){ + blob.toBlob(function (blob){ + fn(file, blob); + }, type, quality); + } + else { + fn(file, api.toBinaryString(blob.toDataURL(type, quality))); + } + } + else { + fn(file, blob); + } + } + + + // @export + api.Form = Form; +})(FileAPI, window); + +/*global window, FileAPI, Uint8Array */ + +(function (window, api){ + "use strict"; + + var + noop = function (){} + , document = window.document + + , XHR = function (options){ + this.uid = api.uid(); + this.xhr = { + abort: noop + , getResponseHeader: noop + , getAllResponseHeaders: noop + }; + this.options = options; + }, + + _xhrResponsePostfix = { '': 1, XML: 1, Text: 1, Body: 1 } + ; + + + XHR.prototype = { + status: 0, + statusText: '', + constructor: XHR, + + getResponseHeader: function (name){ + return this.xhr.getResponseHeader(name); + }, + + getAllResponseHeaders: function (){ + return this.xhr.getAllResponseHeaders() || {}; + }, + + end: function (status, statusText){ + var _this = this, options = _this.options; + + _this.end = + _this.abort = noop; + _this.status = status; + + if( statusText ){ + _this.statusText = statusText; + } + + api.log('xhr.end:', status, statusText); + options.complete(status == 200 || status == 201 ? false : _this.statusText || 'unknown', _this); + + if( _this.xhr && _this.xhr.node ){ + setTimeout(function (){ + var node = _this.xhr.node; + try { node.parentNode.removeChild(node); } catch (e){} + try { delete window[_this.uid]; } catch (e){} + window[_this.uid] = _this.xhr.node = null; + }, 9); + } + }, + + abort: function (){ + this.end(0, 'abort'); + + if( this.xhr ){ + this.xhr.aborted = true; + this.xhr.abort(); + } + }, + + send: function (FormData){ + var _this = this, options = this.options; + + FormData.toData(function (data){ + // Start uploading + options.upload(options, _this); + _this._send.call(_this, options, data); + }, options); + }, + + _send: function (options, data){ + var _this = this, xhr, uid = _this.uid, onloadFuncName = _this.uid + "Load", url = options.url; + + api.log('XHR._send:', data); + + if( !options.cache ){ + // No cache + url += (~url.indexOf('?') ? '&' : '?') + api.uid(); + } + + if( data.nodeName ){ + var jsonp = options.jsonp; + + // prepare callback in GET + url = url.replace(/([a-z]+)=(\?)/i, '$1='+uid); + + // legacy + options.upload(options, _this); + + var + onPostMessage = function (evt){ + if( ~url.indexOf(evt.origin) ){ + try { + var result = api.parseJSON(evt.data); + if( result.id == uid ){ + complete(result.status, result.statusText, result.response); + } + } catch( err ){ + complete(0, err.message); + } + } + }, + + // jsonp-callack + complete = window[uid] = function (status, statusText, response){ + _this.readyState = 4; + _this.responseText = response; + _this.end(status, statusText); + + api.event.off(window, 'message', onPostMessage); + window[uid] = xhr = transport = window[onloadFuncName] = null; + } + ; + + _this.xhr.abort = function (){ + try { + if( transport.stop ){ transport.stop(); } + else if( transport.contentWindow.stop ){ transport.contentWindow.stop(); } + else { transport.contentWindow.document.execCommand('Stop'); } + } + catch (er) {} + complete(0, "abort"); + }; + + api.event.on(window, 'message', onPostMessage); + + window[onloadFuncName] = function (){ + try { + var + win = transport.contentWindow + , doc = win.document + , result = win.result || api.parseJSON(doc.body.innerHTML) + ; + complete(result.status, result.statusText, result.response); + } catch (e){ + api.log('[transport.onload]', e); + } + }; + + xhr = document.createElement('div'); + xhr.innerHTML = '
' + + '' + + (jsonp && (options.url.indexOf('=?') < 0) ? '' : '') + + '
' + ; + + // get form-data & transport + var + form = xhr.getElementsByTagName('form')[0] + , transport = xhr.getElementsByTagName('iframe')[0] + ; + + form.appendChild(data); + + api.log(form.parentNode.innerHTML); + + // append to DOM + document.body.appendChild(xhr); + + // keep a reference to node-transport + _this.xhr.node = xhr; + + // send + _this.readyState = 2; // loaded + form.submit(); + form = null; + } + else { + // Clean url + url = url.replace(/([a-z]+)=(\?)&?/i, ''); + + // html5 + if (this.xhr && this.xhr.aborted) { + api.log("Error: already aborted"); + return; + } + xhr = _this.xhr = api.getXHR(); + + if (data.params) { + url += (url.indexOf('?') < 0 ? "?" : "&") + data.params.join("&"); + } + + xhr.open('POST', url, true); + + if( api.withCredentials ){ + xhr.withCredentials = "true"; + } + + if( !options.headers || !options.headers['X-Requested-With'] ){ + xhr.setRequestHeader('X-Requested-With', 'XMLHttpRequest'); + } + + api.each(options.headers, function (val, key){ + xhr.setRequestHeader(key, val); + }); + + + if ( options._chunked ) { + // chunked upload + if( xhr.upload ){ + xhr.upload.addEventListener('progress', api.throttle(function (/**Event*/evt){ + if (!data.retry) { + // show progress only for correct chunk uploads + options.progress({ + type: evt.type + , total: data.size + , loaded: data.start + evt.loaded + , totalSize: data.size + }, _this, options); + } + }, 100), false); + } + + xhr.onreadystatechange = function (){ + var lkb = parseInt(xhr.getResponseHeader('X-Last-Known-Byte'), 10); + + _this.status = xhr.status; + _this.statusText = xhr.statusText; + _this.readyState = xhr.readyState; + + if( xhr.readyState == 4 ){ + try { + for( var k in _xhrResponsePostfix ){ + _this['response'+k] = xhr['response'+k]; + } + }catch(_){} + xhr.onreadystatechange = null; + + if (!xhr.status || xhr.status - 201 > 0) { + api.log("Error: " + xhr.status); + // some kind of error + // 0 - connection fail or timeout, if xhr.aborted is true, then it's not recoverable user action + // up - server error + if (((!xhr.status && !xhr.aborted) || 500 == xhr.status || 416 == xhr.status) && ++data.retry <= options.chunkUploadRetry) { + // let's try again the same chunk + // only applicable for recoverable error codes 500 && 416 + var delay = xhr.status ? 0 : api.chunkNetworkDownRetryTimeout; + + // inform about recoverable problems + options.pause(data.file, options); + + // smart restart if server reports about the last known byte + api.log("X-Last-Known-Byte: " + lkb); + if (lkb) { + data.end = lkb; + } else { + data.end = data.start - 1; + if (416 == xhr.status) { + data.end = data.end - options.chunkSize; + } + } + + setTimeout(function () { + _this._send(options, data); + }, delay); + } else { + // no mo retries + _this.end(xhr.status); + } + } else { + // success + data.retry = 0; + + if (data.end == data.size - 1) { + // finished + _this.end(xhr.status); + } else { + // next chunk + + // shift position if server reports about the last known byte + api.log("X-Last-Known-Byte: " + lkb); + if (lkb) { + data.end = lkb; + } + data.file.FileAPIReadPosition = data.end; + + setTimeout(function () { + _this._send(options, data); + }, 0); + } + } + + xhr = null; + } + }; + + data.start = data.end + 1; + data.end = Math.max(Math.min(data.start + options.chunkSize, data.size) - 1, data.start); + + // Retrieve a slice of file + var + file = data.file + , slice = (file.slice || file.mozSlice || file.webkitSlice).call(file, data.start, data.end + 1) + ; + + if( data.size && !slice.size ){ + setTimeout(function (){ + _this.end(-1); + }); + } else { + xhr.setRequestHeader("Content-Range", "bytes " + data.start + "-" + data.end + "/" + data.size); + xhr.setRequestHeader("Content-Disposition", 'attachment; filename=' + encodeURIComponent(data.name)); + xhr.setRequestHeader("Content-Type", data.type || "application/octet-stream"); + + xhr.send(slice); + } + + file = slice = null; + } else { + // single piece upload + if( xhr.upload ){ + // https://github.com/blueimp/jQuery-File-Upload/wiki/Fixing-Safari-hanging-on-very-high-speed-connections-%281Gbps%29 + xhr.upload.addEventListener('progress', api.throttle(function (/**Event*/evt){ + options.progress(evt, _this, options); + }, 100), false); + } + + xhr.onreadystatechange = function (){ + _this.status = xhr.status; + _this.statusText = xhr.statusText; + _this.readyState = xhr.readyState; + + if( xhr.readyState == 4 ){ + for( var k in _xhrResponsePostfix ){ + _this['response'+k] = xhr['response'+k]; + } + xhr.onreadystatechange = null; + + if (!xhr.status || xhr.status > 201) { + api.log("Error: " + xhr.status); + if (((!xhr.status && !xhr.aborted) || 500 == xhr.status) && (options.retry || 0) < options.uploadRetry) { + options.retry = (options.retry || 0) + 1; + var delay = api.networkDownRetryTimeout; + + // inform about recoverable problems + options.pause(options.file, options); + + setTimeout(function () { + _this._send(options, data); + }, delay); + } else { + //success + _this.end(xhr.status); + } + } else { + //success + _this.end(xhr.status); + } + + xhr = null; + } + }; + + if( api.isArray(data) ){ + // multipart + xhr.setRequestHeader('Content-Type', 'multipart/form-data; boundary=_'+api.expando); + var rawData = data.join('') +'--_'+ api.expando +'--'; + + /** @namespace xhr.sendAsBinary https://developer.mozilla.org/ru/XMLHttpRequest#Sending_binary_content */ + if( xhr.sendAsBinary ){ + xhr.sendAsBinary(rawData); + } + else { + var bytes = Array.prototype.map.call(rawData, function(c){ return c.charCodeAt(0) & 0xff; }); + xhr.send(new Uint8Array(bytes).buffer); + + } + } else { + // FormData + xhr.send(data); + } + } + } + } + }; + + + // @export + api.XHR = XHR; +})(window, FileAPI); + +/** + * @class FileAPI.Camera + * @author RubaXa + * @support Chrome 21+, FF 18+, Opera 12+ + */ + +/*global window, FileAPI, jQuery */ +/** @namespace LocalMediaStream -- https://developer.mozilla.org/en-US/docs/WebRTC/MediaStream_API#LocalMediaStream */ +(function (window, api){ + "use strict"; + + var + URL = window.URL || window.webkitURL, + + document = window.document, + navigator = window.navigator, + + getMedia = navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia || navigator.msGetUserMedia, + + html5 = !!getMedia + ; + + + // Support "media" + api.support.media = html5; + + + var Camera = function (video){ + this.video = video; + }; + + + Camera.prototype = { + isActive: function (){ + return !!this._active; + }, + + + /** + * Start camera streaming + * @param {Function} callback + */ + start: function (callback){ + var + _this = this + , video = _this.video + , _successId + , _failId + , _complete = function (err){ + _this._active = !err; + clearTimeout(_failId); + clearTimeout(_successId); +// api.event.off(video, 'loadedmetadata', _complete); + callback && callback(err, _this); + } + ; + + getMedia.call(navigator, { video: true }, function (stream/**LocalMediaStream*/){ + // Success + _this.stream = stream; + +// api.event.on(video, 'loadedmetadata', function (){ +// _complete(null); +// }); + + // Set camera stream + video.src = URL.createObjectURL(stream); + + // Note: onloadedmetadata doesn't fire in Chrome when using it with getUserMedia. + // See crbug.com/110938. + _successId = setInterval(function (){ + if( _detectVideoSignal(video) ){ + _complete(null); + } + }, 1000); + + _failId = setTimeout(function (){ + _complete('timeout'); + }, 5000); + + // Go-go-go! + video.play(); + }, _complete/*error*/); + }, + + + /** + * Stop camera streaming + */ + stop: function (){ + try { + this._active = false; + this.video.pause(); + this.stream.stop(); + } catch( err ){ } + }, + + + /** + * Create screenshot + * @return {FileAPI.Camera.Shot} + */ + shot: function (){ + return new Shot(this.video); + } + }; + + + /** + * Get camera element from container + * + * @static + * @param {HTMLElement} el + * @return {Camera} + */ + Camera.get = function (el){ + return new Camera(el.firstChild); + }; + + + /** + * Publish camera element into container + * + * @static + * @param {HTMLElement} el + * @param {Object} options + * @param {Function} [callback] + */ + Camera.publish = function (el, options, callback){ + if( typeof options == 'function' ){ + callback = options; + options = {}; + } + + // Dimensions of "camera" + options = api.extend({}, { + width: '100%' + , height: '100%' + , start: true + }, options); + + + if( el.jquery ){ + // Extract first element, from jQuery collection + el = el[0]; + } + + + var doneFn = function (err){ + if( err ){ + callback(err); + } + else { + // Get camera + var cam = Camera.get(el); + if( options.start ){ + cam.start(callback); + } + else { + callback(null, cam); + } + } + }; + + + el.style.width = _px(options.width); + el.style.height = _px(options.height); + + + if( api.html5 && html5 ){ + // Create video element + var video = document.createElement('video'); + + // Set dimensions + video.style.width = _px(options.width); + video.style.height = _px(options.height); + + // Clean container + if( window.jQuery ){ + jQuery(el).empty(); + } else { + el.innerHTML = ''; + } + + // Add "camera" to container + el.appendChild(video); + + // end + doneFn(); + } + else { + Camera.fallback(el, options, doneFn); + } + }; + + + Camera.fallback = function (el, options, callback){ + callback('not_support_camera'); + }; + + + /** + * @class FileAPI.Camera.Shot + */ + var Shot = function (video){ + var canvas = video.nodeName ? api.Image.toCanvas(video) : video; + var shot = api.Image(canvas); + shot.type = 'image/png'; + shot.width = canvas.width; + shot.height = canvas.height; + shot.size = canvas.width * canvas.height * 4; + return shot; + }; + + + /** + * Add "px" postfix, if value is a number + * + * @private + * @param {*} val + * @return {String} + */ + function _px(val){ + return val >= 0 ? val + 'px' : val; + } + + + /** + * @private + * @param {HTMLVideoElement} video + * @return {Boolean} + */ + function _detectVideoSignal(video){ + var canvas = document.createElement('canvas'), ctx, res = false; + try { + ctx = canvas.getContext('2d'); + ctx.drawImage(video, 0, 0, 1, 1); + res = ctx.getImageData(0, 0, 1, 1).data[4] != 255; + } + catch( e ){} + return res; + } + + + // @export + Camera.Shot = Shot; + api.Camera = Camera; +})(window, FileAPI); + +/** + * FileAPI fallback to Flash + * + * @flash-developer "Vladimir Demidov" + */ + +/*global window, ActiveXObject, FileAPI */ +(function (window, jQuery, api) { + "use strict"; + + var + document = window.document + , location = window.location + , navigator = window.navigator + , _each = api.each + ; + + + api.support.flash = (function (){ + var mime = navigator.mimeTypes, has = false; + + if( navigator.plugins && typeof navigator.plugins['Shockwave Flash'] == 'object' ){ + has = navigator.plugins['Shockwave Flash'].description && !(mime && mime['application/x-shockwave-flash'] && !mime['application/x-shockwave-flash'].enabledPlugin); + } + else { + try { + has = !!(window.ActiveXObject && new ActiveXObject('ShockwaveFlash.ShockwaveFlash')); + } + catch(er){ + api.log('Flash -- does not supported.'); + } + } + + if( has && /^file:/i.test(location) ){ + api.log('[warn] Flash does not work on `file:` protocol.'); + } + + return has; + })(); + + + api.support.flash + && (0 + || !api.html5 || !api.support.html5 + || (api.cors && !api.support.cors) + || (api.media && !api.support.media) + ) + && (function (){ + var + _attr = api.uid() + , _retry = 0 + , _files = {} + , _rhttp = /^https?:/i + + , flash = { + _fn: {}, + + + /** + * Publish flash-object + * + * @param {HTMLElement} el + * @param {String} id + * @param {Object} [opts] + */ + publish: function (el, id, opts){ + opts = opts || {}; + el.innerHTML = _makeFlashHTML({ + id: id + , src: _getUrl(api.flashUrl, 'r=' + api.version) +// , src: _getUrl('http://v.demidov.boom.corp.mail.ru/uploaderfileapi/FlashFileAPI.swf?1') + , wmode: opts.camera ? '' : 'transparent' + , flashvars: 'callback=' + (opts.onEvent || 'FileAPI.Flash.onEvent') + + '&flashId='+ id + + '&storeKey='+ navigator.userAgent.match(/\d/ig).join('') +'_'+ api.version + + (flash.isReady || (api.pingUrl ? '&ping='+api.pingUrl : '')) + + '&timeout='+api.flashAbortTimeout + + (opts.camera ? '&useCamera=' + _getUrl(api.flashWebcamUrl) : '') + + '&debug='+(api.debug?"1":"") + }, opts); + }, + + + /** + * Initialization & preload flash object + */ + init: function (){ + var child = document.body && document.body.firstChild; + + if( child ){ + do { + if( child.nodeType == 1 ){ + api.log('FlashAPI.state: awaiting'); + + var dummy = document.createElement('div'); + + dummy.id = '_' + _attr; + + _css(dummy, { + top: 1 + , right: 1 + , width: 5 + , height: 5 + , position: 'absolute' + , zIndex: 1e6+'' // set max zIndex + }); + + child.parentNode.insertBefore(dummy, child); + flash.publish(dummy, _attr); + + return; + } + } + while( child = child.nextSibling ); + } + + if( _retry < 10 ){ + setTimeout(flash.init, ++_retry*50); + } + }, + + + ready: function (){ + api.log('FlashAPI.state: ready'); + + flash.ready = api.F; + flash.isReady = true; + flash.patch(); + flash.patchCamera && flash.patchCamera(); + api.event.on(document, 'mouseover', flash.mouseover); + api.event.on(document, 'click', function (evt){ + if( flash.mouseover(evt) ){ + evt.preventDefault + ? evt.preventDefault() + : (evt.returnValue = true) + ; + } + }); + }, + + + getEl: function (){ + return document.getElementById('_'+_attr); + }, + + + getWrapper: function (node){ + do { + if( /js-fileapi-wrapper/.test(node.className) ){ + return node; + } + } + while( (node = node.parentNode) && (node !== document.body) ); + }, + + disableMouseover: false, + + mouseover: function (evt){ + if (!flash.disableMouseover) { + var target = api.event.fix(evt).target; + + if( /input/i.test(target.nodeName) && target.type == 'file' && !target.disabled ){ + var + state = target.getAttribute(_attr) + , wrapper = flash.getWrapper(target) + ; + + if( api.multiFlash ){ + // check state: + // i — published + // i — initialization + // r — ready + if( state == 'i' || state == 'r' ){ + // publish fail + return false; + } + else if( state != 'p' ){ + // set "init" state + target.setAttribute(_attr, 'i'); + + var dummy = document.createElement('div'); + + if( !wrapper ){ + api.log('[err] FlashAPI.mouseover: js-fileapi-wrapper not found'); + return; + } + + _css(dummy, { + top: 0 + , left: 0 + , width: target.offsetWidth + , height: target.offsetHeight + , zIndex: 1e6+'' // set max zIndex + , position: 'absolute' + }); + + wrapper.appendChild(dummy); + flash.publish(dummy, api.uid()); + + // set "publish" state + target.setAttribute(_attr, 'p'); + } + + return true; + } + else if( wrapper ){ + // Use one flash element + var box = _getDimensions(wrapper); + _css(flash.getEl(), box); + + // Set current input + flash.curInp = target; + } + } + else if( !/object|embed/i.test(target.nodeName) ){ + _css(flash.getEl(), { top: 1, left: 1, width: 5, height: 5 }); + } + } + }, + + onEvent: function (evt){ + var type = evt.type; + + if( type == 'ready' ){ + try { + // set "ready" state + flash.getInput(evt.flashId).setAttribute(_attr, 'r'); + } catch (e){ + } + + flash.ready(); + setTimeout(function (){ flash.mouseenter(evt); }, 50); + return true; + } + else if( type === 'ping' ){ + api.log('(flash -> js).ping:', [evt.status, evt.savedStatus], evt.error); + } + else if( type === 'log' ){ + api.log('(flash -> js).log:', evt.target); + } + else if( type in flash ){ + setTimeout(function (){ + api.log('FlashAPI.event.'+evt.type+':', evt); + flash[type](evt); + }, 1); + } + }, + mouseDown: function(evt) { + flash.disableMouseover = true; + }, + cancel: function(evt) { + flash.disableMouseover = false; + }, + mouseenter: function (evt){ + var node = flash.getInput(evt.flashId); + + if( node ){ + // Set multiple mode + flash.cmd(evt, 'multiple', node.getAttribute('multiple') != null); + + + // Set files filter + var accept = [], exts = {}; + + _each((node.getAttribute('accept') || '').split(/,\s*/), function (mime){ + api.accept[mime] && _each(api.accept[mime].split(' '), function (ext){ + exts[ext] = 1; + }); + }); + + _each(exts, function (i, ext){ + accept.push( ext ); + }); + + flash.cmd(evt, 'accept', accept.length ? accept.join(',')+','+accept.join(',').toUpperCase() : '*'); + } + }, + + + get: function (id){ + return document[id] || window[id] || document.embeds[id]; + }, + + + getInput: function (id){ + if( api.multiFlash ){ + try { + var node = flash.getWrapper(flash.get(id)); + if( node ){ + return node.getElementsByTagName('input')[0]; + } + } catch (e){ + api.log('[err] Can not find "input" by flashId:', id, e); + } + } else { + return flash.curInp; + } + }, + + + select: function (evt){ + try { + var + inp = flash.getInput(evt.flashId) + , uid = api.uid(inp) + , files = evt.target.files + , event + ; + _each(files, function (file){ + api.checkFileObj(file); + }); + + _files[uid] = files; + + if( document.createEvent ){ + event = document.createEvent('Event'); + event.files = files; + event.initEvent('change', true, true); + inp.dispatchEvent(event); + } + else if( jQuery ){ + jQuery(inp).trigger({ type: 'change', files: files }); + } + else { + event = document.createEventObject(); + event.files = files; + inp.fireEvent('onchange', event); + } + } finally { + flash.disableMouseover = false; + } + }, + + interval: null, + cmd: function (id, name, data, last) { + if (flash.uploadInProgress && flash.readInProgress) { + setTimeout(function() { + flash.cmd(id, name, data, last); + }, 100); + } else { + this.cmdFn(id, name, data, last); + } + }, + + cmdFn: function(id, name, data, last) { + try { + api.log('(js -> flash).'+name+':', data); + return flash.get(id.flashId || id).cmd(name, data); + } catch (e){ + api.log('(js -> flash).onError:', e); + if( !last ){ + // try again + setTimeout(function (){ flash.cmd(id, name, data, true); }, 50); + } + } + }, + + patch: function (){ + api.flashEngine = true; + + // FileAPI + _inherit(api, { + readAsDataURL: function (file, callback){ + if( _isHtmlFile(file) ){ + this.parent.apply(this, arguments); + } + else { + api.log('FlashAPI.readAsBase64'); + flash.readInProgress = true; + flash.cmd(file, 'readAsBase64', { + id: file.id, + callback: _wrap(function _(err, base64){ + flash.readInProgress = false; + _unwrap(_); + + api.log('FlashAPI.readAsBase64:', err); + + callback({ + type: err ? 'error' : 'load' + , error: err + , result: 'data:'+ file.type +';base64,'+ base64 + }); + }) + }); + } + }, + + readAsText: function (file, encoding, callback){ + if( callback ){ + api.log('[warn] FlashAPI.readAsText not supported `encoding` param'); + } else { + callback = encoding; + } + + api.readAsDataURL(file, function (evt){ + if( evt.type == 'load' ){ + try { + evt.result = window.atob(evt.result.split(';base64,')[1]); + } catch( err ){ + evt.type = 'error'; + evt.error = err.toString(); + } + } + callback(evt); + }); + }, + + getFiles: function (input, filter, callback){ + if( callback ){ + api.filterFiles(api.getFiles(input), filter, callback); + return null; + } + + var files = api.isArray(input) ? input : _files[api.uid(input.target || input.srcElement || input)]; + + + if( !files ){ + // Файлов нету, вызываем родительский метод + return this.parent.apply(this, arguments); + } + + + if( filter ){ + filter = api.getFilesFilter(filter); + files = api.filter(files, function (file){ return filter.test(file.name); }); + } + + return files; + }, + + + getInfo: function (file, fn){ + if( _isHtmlFile(file) ){ + this.parent.apply(this, arguments); + } + else if( file.isShot ){ + fn(null, file.info = { + width: file.width, + height: file.height + }); + } + else { + if( !file.__info ){ + var defer = file.__info = api.defer(); + +// flash.cmd(file, 'getFileInfo', { +// id: file.id +// , callback: _wrap(function _(err, info){ +// _unwrap(_); +// defer.resolve(err, file.info = info); +// }) +// }); + defer.resolve(null, file.info = null); + + } + + file.__info.then(fn); + } + } + }); + + + // FileAPI.Image + api.support.transform = true; + api.Image && _inherit(api.Image.prototype, { + get: function (fn, scaleMode){ + this.set({ scaleMode: scaleMode || 'noScale' }); // noScale, exactFit + return this.parent(fn); + }, + + _load: function (file, fn){ + api.log('FlashAPI.Image._load:', file); + + if( _isHtmlFile(file) ){ + this.parent.apply(this, arguments); + } + else { + var _this = this; + api.getInfo(file, function (err){ + fn.call(_this, err, file); + }); + } + }, + + _apply: function (file, fn){ + api.log('FlashAPI.Image._apply:', file); + + if( _isHtmlFile(file) ){ + this.parent.apply(this, arguments); + } + else { + var m = this.getMatrix(file.info), doneFn = fn; + + flash.cmd(file, 'imageTransform', { + id: file.id + , matrix: m + , callback: _wrap(function _(err, base64){ + api.log('FlashAPI.Image._apply.callback:', err); + _unwrap(_); + + if( err ){ + doneFn(err); + } + else if( !api.support.html5 && (!api.support.dataURI || base64.length > 3e4) ){ + _makeFlashImage({ + width: (m.deg % 180) ? m.dh : m.dw + , height: (m.deg % 180) ? m.dw : m.dh + , scale: m.scaleMode + }, base64, doneFn); + } + else { + if( m.filter ){ + doneFn = function (err, img){ + if( err ){ + fn(err); + } + else { + api.Image.applyFilter(img, m.filter, function (){ + fn(err, this.canvas); + }); + } + }; + } + + api.newImage('data:'+ file.type +';base64,'+ base64, doneFn); + } + }) + }); + } + }, + + toData: function (fn){ + var + file = this.file + , info = file.info + , matrix = this.getMatrix(info) + ; + api.log('FlashAPI.Image.toData'); + + if( _isHtmlFile(file) ){ + this.parent.apply(this, arguments); + } + else { + if( matrix.deg == 'auto' ){ + matrix.deg = api.Image.exifOrientation[info && info.exif && info.exif.Orientation] || 0; + } + + fn.call(this, !file.info, { + id: file.id + , flashId: file.flashId + , name: file.name + , type: file.type + , matrix: matrix + }); + } + } + }); + + + api.Image && _inherit(api.Image, { + fromDataURL: function (dataURL, size, callback){ + if( !api.support.dataURI || dataURL.length > 3e4 ){ + _makeFlashImage( + api.extend({ scale: 'exactFit' }, size) + , dataURL.replace(/^data:[^,]+,/, '') + , function (err, el){ callback(el); } + ); + } + else { + this.parent(dataURL, size, callback); + } + } + }); + + // FileAPI.Form + _inherit(api.Form.prototype, { + toData: function (fn){ + var items = this.items, i = items.length; + + for( ; i--; ){ + if( items[i].file && _isHtmlFile(items[i].blob) ){ + return this.parent.apply(this, arguments); + } + } + + api.log('FlashAPI.Form.toData'); + fn(items); + } + }); + + + // FileAPI.XHR + _inherit(api.XHR.prototype, { + _send: function (options, formData){ + if( + formData.nodeName + || formData.append && api.support.html5 + || api.isArray(formData) && (typeof formData[0] === 'string') + ){ + // HTML5, Multipart or IFrame + return this.parent.apply(this, arguments); + } + + + var + data = {} + , files = {} + , _this = this + , flashId + , fileId + ; + + _each(formData, function (item){ + if( item.file ){ + files[item.name] = item = _getFileDescr(item.blob); + fileId = item.id; + flashId = item.flashId; + } + else { + data[item.name] = item.blob; + } + }); + + if( !fileId ){ + flashId = _attr; + } + + if( !flashId ){ + api.log('[err] FlashAPI._send: flashId -- undefined'); + return this.parent.apply(this, arguments); + } + else { + api.log('FlashAPI.XHR._send: '+ flashId +' -> '+ fileId); + } + + _this.xhr = { + headers: {}, + abort: function (){ flash.uploadInProgress = false; flash.cmd(flashId, 'abort', { id: fileId }); }, + getResponseHeader: function (name){ return this.headers[name]; }, + getAllResponseHeaders: function (){ return this.headers; } + }; + + var queue = api.queue(function (){ + flash.uploadInProgress = true; + flash.cmd(flashId, 'upload', { + url: _getUrl(options.url.replace(/([a-z]+)=(\?)&?/i, '')) + , data: data + , files: fileId ? files : null + , headers: options.headers || {} + , callback: _wrap(function upload(evt){ + var type = evt.type, result = evt.result; + + api.log('FlashAPI.upload.'+type); + + if( type == 'progress' ){ + evt.loaded = Math.min(evt.loaded, evt.total); // @todo fixme + evt.lengthComputable = true; + options.progress(evt); + } + else if( type == 'complete' ){ + flash.uploadInProgress = false; + _unwrap(upload); + + if( typeof result == 'string' ){ + _this.responseText = result.replace(/%22/g, "\"").replace(/%5c/g, "\\").replace(/%26/g, "&").replace(/%25/g, "%"); + } + + _this.end(evt.status || 200); + } + else if( type == 'abort' || type == 'error' ){ + flash.uploadInProgress = false; + _this.end(evt.status || 0, evt.message); + _unwrap(upload); + } + }) + }); + }); + + + // #2174: FileReference.load() call while FileReference.upload() or vice versa + _each(files, function (file){ + queue.inc(); + api.getInfo(file, queue.next); + }); + + queue.check(); + } + }); + } + } + ; + + + function _makeFlashHTML(opts){ + return ('' + + '' + + '' + + '' + + '' + + '' + + '' + + '' + + '' + + '').replace(/#(\w+)#/ig, function (a, name){ return opts[name]; }) + ; + } + + + function _css(el, css){ + if( el && el.style ){ + var key, val; + for( key in css ){ + val = css[key]; + if( typeof val == 'number' ){ + val += 'px'; + } + try { el.style[key] = val; } catch (e) {} + } + + } + } + + + function _inherit(obj, methods){ + _each(methods, function (fn, name){ + var prev = obj[name]; + obj[name] = function (){ + this.parent = prev; + return fn.apply(this, arguments); + }; + }); + } + + function _isHtmlFile(file){ + return file && !file.flashId; + } + + function _wrap(fn){ + var id = fn.wid = api.uid(); + flash._fn[id] = fn; + return 'FileAPI.Flash._fn.'+id; + } + + + function _unwrap(fn){ + try { + flash._fn[fn.wid] = null; + delete flash._fn[fn.wid]; + } + catch(e){} + } + + + function _getUrl(url, params){ + if( !_rhttp.test(url) ){ + if( /^\.\//.test(url) || '/' != url.charAt(0) ){ + var path = location.pathname; + path = path.substr(0, path.lastIndexOf('/')); + url = (path +'/'+ url).replace('/./', '/'); + } + + if( '//' != url.substr(0, 2) ){ + url = '//' + location.host + url; + } + + if( !_rhttp.test(url) ){ + url = location.protocol + url; + } + } + + if( params ){ + url += (/\?/.test(url) ? '&' : '?') + params; + } + + return url; + } + + + function _makeFlashImage(opts, base64, fn){ + var + key + , flashId = api.uid() + , el = document.createElement('div') + , attempts = 10 + ; + + for( key in opts ){ + el.setAttribute(key, opts[key]); + el[key] = opts[key]; + } + + _css(el, opts); + + opts.width = '100%'; + opts.height = '100%'; + + el.innerHTML = _makeFlashHTML(api.extend({ + id: flashId + , src: _getUrl(api.flashImageUrl, 'r='+ api.uid()) + , wmode: 'opaque' + , flashvars: 'scale='+ opts.scale +'&callback='+_wrap(function _(){ + _unwrap(_); + if( --attempts > 0 ){ + _setImage(); + } + return true; + }) + }, opts)); + + function _setImage(){ + try { + // Get flash-object by id + var img = flash.get(flashId); + img.setImage(base64); + } catch (e){ + api.log('[err] FlashAPI.Preview.setImage -- can not set "base64":', e); + } + } + + fn(false, el); + el = null; + } + + + function _getFileDescr(file){ + return { + id: file.id + , name: file.name + , matrix: file.matrix + , flashId: file.flashId + }; + } + + + function _getDimensions(el){ + var + box = el.getBoundingClientRect() + , body = document.body + , docEl = (el && el.ownerDocument).documentElement + ; + + function getOffset(obj) { + var left, top; + left = top = 0; + if (obj.offsetParent) { + do { + left += obj.offsetLeft; + top += obj.offsetTop; + } while (obj = obj.offsetParent); + } + return { + left : left, + top : top + }; + }; + + return { + top: getOffset(el).top + , left: getOffset(el).left + , width: el.offsetWidth + , height: el.offsetHeight + }; + } + + // @export + api.Flash = flash; + + + // Check dataURI support + api.newImage('data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///ywAAAAAAQABAAACAUwAOw==', function (err, img){ + api.support.dataURI = !(img.width != 1 || img.height != 1); + flash.init(); + }); + })(); +})(window, window.jQuery, FileAPI); + +/** + * FileAPI fallback to Flash + * + * @flash-developer "Vladimir Demidov" + */ + +/*global window, FileAPI */ +(function (window, jQuery, api) { + "use strict"; + + var _each = api.each, + _cameraQueue = []; + + + if (api.support.flash && (api.media && !api.support.media)) { + (function () { + + function _wrap(fn) { + var id = fn.wid = api.uid(); + api.Flash._fn[id] = fn; + return 'FileAPI.Flash._fn.' + id; + } + + + function _unwrap(fn) { + try { + api.Flash._fn[fn.wid] = null; + delete api.Flash._fn[fn.wid]; + } catch (e) { + } + } + + var flash = api.Flash; + api.extend(api.Flash, { + + patchCamera: function () { + api.Camera.fallback = function (el, options, callback) { + var camId = api.uid(); + api.log('FlashAPI.Camera.publish: ' + camId); + flash.publish(el, camId, api.extend(options, { + camera: true, + onEvent: _wrap(function _(evt) { + if (evt.type === 'camera') { + _unwrap(_); + + if (evt.error) { + api.log('FlashAPI.Camera.publish.error: ' + evt.error); + callback(evt.error); + } else { + api.log('FlashAPI.Camera.publish.success: ' + camId); + callback(null); + } + } + }) + })); + }; + // Run + _each(_cameraQueue, function (args) { + api.Camera.fallback.apply(api.Camera, args); + }); + _cameraQueue = []; + + + // FileAPI.Camera:proto + api.extend(api.Camera.prototype, { + _id: function () { + return this.video.id; + }, + + start: function (callback) { + var _this = this; + flash.cmd(this._id(), 'camera.on', { + callback: _wrap(function _(evt) { + _unwrap(_); + + if (evt.error) { + api.log('FlashAPI.camera.on.error: ' + evt.error); + callback(evt.error, _this); + } else { + api.log('FlashAPI.camera.on.success: ' + _this._id()); + _this._active = true; + callback(null, _this); + } + }) + }); + }, + + stop: function () { + this._active = false; + flash.cmd(this._id(), 'camera.off'); + }, + + shot: function () { + api.log('FlashAPI.Camera.shot:', this._id()); + + var shot = api.Flash.cmd(this._id(), 'shot', {}); + shot.type = 'image/png'; + shot.flashId = this._id(); + shot.isShot = true; + + return new api.Camera.Shot(shot); + } + }); + } + }); + + api.Camera.fallback = function () { + _cameraQueue.push(arguments); + }; + + }()); + } +}(window, window.jQuery, FileAPI)); +if( typeof define === "function" && define.amd ){ define("FileAPI", [], function (){ return FileAPI; }); } diff --git a/xstatic/pkg/angular_fileupload/data/FileAPI.min.js b/xstatic/pkg/angular_fileupload/data/FileAPI.min.js new file mode 100644 index 0000000..c7b0266 --- /dev/null +++ b/xstatic/pkg/angular_fileupload/data/FileAPI.min.js @@ -0,0 +1,6 @@ +/*! 12.0.4 */ +/*! FileAPI 2.0.7 - BSD | git://github.com/mailru/FileAPI.git + * FileAPI — a set of javascript tools for working with files. Multiupload, drag'n'drop and chunked file upload. Images: crop, resize and auto orientation by EXIF. + */ +!function(a){"use strict";var b=a.HTMLCanvasElement&&a.HTMLCanvasElement.prototype,c=a.Blob&&function(){try{return Boolean(new Blob)}catch(a){return!1}}(),d=c&&a.Uint8Array&&function(){try{return 100===new Blob([new Uint8Array(100)]).size}catch(a){return!1}}(),e=a.BlobBuilder||a.WebKitBlobBuilder||a.MozBlobBuilder||a.MSBlobBuilder,f=(c||e)&&a.atob&&a.ArrayBuffer&&a.Uint8Array&&function(a){var b,f,g,h,i,j;for(b=a.split(",")[0].indexOf("base64")>=0?atob(a.split(",")[1]):decodeURIComponent(a.split(",")[1]),f=new ArrayBuffer(b.length),g=new Uint8Array(f),h=0;hd;d++)d in a&&b.call(c,a[d],d,a);else for(var f in a)a.hasOwnProperty(f)&&b.call(c,a[f],f,a)},S=function(a){for(var b=arguments,c=1,d=function(b,c){a[c]=b};c=c&&!d&&f.end()},isFail:function(){return d},fail:function(){!d&&a(d=!0)},end:function(){e||(e=!0,a())}};return f},each:R,afor:function(a,b){var c=0,d=a.length;Q(a)&&d--?!function e(){b(d!=c&&e,a[c],c++)}():b(!1)},extend:S,isFile:function(a){return"[object File]"===H.call(a)},isBlob:function(a){return this.isFile(a)||"[object Blob]"===H.call(a)},isCanvas:function(a){return a&&D.test(a.nodeName)},getFilesFilter:function(a){return a="string"==typeof a?a:a.getAttribute&&a.getAttribute("accept")||"",a?new RegExp("("+a.replace(/\./g,"\\.").replace(/,/g,"|")+")$","i"):/./},readAsDataURL:function(a,b){Y.isCanvas(a)?c(a,b,"load",Y.toDataURL(a)):e(a,b,"DataURL")},readAsBinaryString:function(a,b){d("BinaryString")?e(a,b,"BinaryString"):e(a,function(a){if("load"==a.type)try{a.result=Y.toBinaryString(a.result)}catch(c){a.type="error",a.message=c.toString()}b(a)},"DataURL")},readAsArrayBuffer:function(a,b){e(a,b,"ArrayBuffer")},readAsText:function(a,b,c){c||(c=b,b="utf-8"),e(a,c,"Text",b)},toDataURL:function(a,b){return"string"==typeof a?a:a.toDataURL?a.toDataURL(b||"image/png"):void 0},toBinaryString:function(b){return a.atob(Y.toDataURL(b).replace(G,""))},readAsImage:function(a,d,e){if(Y.isFile(a))if(r){var f=r.createObjectURL(a);f===b?c(a,d,"error"):Y.readAsImage(f,d,e)}else Y.readAsDataURL(a,function(b){"load"==b.type?Y.readAsImage(b.result,d,e):(e||"error"==b.type)&&c(a,d,b,null,{loaded:b.loaded,total:b.total})});else if(Y.isCanvas(a))c(a,d,"load",a);else if(C.test(a.nodeName))if(a.complete)c(a,d,"load",a);else{var g="error abort load";V(a,g,function i(b){"load"==b.type&&r&&r.revokeObjectURL(a.src),U(a,g,i),c(a,d,b,a)})}else if(a.iframe)c(a,d,{type:"error"});else{var h=Y.newImage(a.dataURL||a);Y.readAsImage(h,d,e)}},checkFileObj:function(a){var b={},c=Y.accept;return"object"==typeof a?b=a:b.name=(a+"").split(/\\|\//g).pop(),null==b.type&&(b.type=b.name.split(".").pop()),R(c,function(a,c){a=new RegExp(a.replace(/\s/g,"|"),"i"),(a.test(b.type)||Y.ext2mime[b.type])&&(b.type=Y.ext2mime[b.type]||c.split("/")[0]+"/"+b.type)}),b},getDropFiles:function(a,b){var c=[],d=k(a),e=Q(d.items)&&d.items[0]&&g(d.items[0]),i=Y.queue(function(){b(c)});R((e?d.items:d.files)||[],function(a){i.inc();try{e?h(a,function(a,b){a?Y.log("[err] getDropFiles:",a):c.push.apply(c,b),i.next()}):f(a,function(b){b&&c.push(a),i.next()})}catch(b){i.next(),Y.log("[err] getDropFiles: ",b)}}),i.check()},getFiles:function(a,b,c){var d=[];return c?(Y.filterFiles(Y.getFiles(a),b,c),null):(a.jquery&&(a.each(function(){d=d.concat(Y.getFiles(this))}),a=d,d=[]),"string"==typeof b&&(b=Y.getFilesFilter(b)),a.originalEvent?a=W(a.originalEvent):a.srcElement&&(a=W(a)),a.dataTransfer?a=a.dataTransfer:a.target&&(a=a.target),a.files?(d=a.files,y||(d[0].blob=a,d[0].iframe=!0)):!y&&j(a)?Y.trim(a.value)&&(d=[Y.checkFileObj(a.value)],d[0].blob=a,d[0].iframe=!0):Q(a)&&(d=a),Y.filter(d,function(a){return!b||b.test(a.name)}))},getTotalSize:function(a){for(var b=0,c=a&&a.length;c--;)b+=a[c].size;return b},getInfo:function(a,b){var c={},d=L.concat();Y.isFile(a)?!function e(){var f=d.shift();f?f.test(a.type)?f(a,function(a,d){a?b(a):(S(c,d),e())}):e():b(!1,c)}():b("not_support_info",c)},addInfoReader:function(a,b){b.test=function(b){return a.test(b)},L.push(b)},filter:function(a,b){for(var c,d=[],e=0,f=a.length;f>e;e++)e in a&&(c=a[e],b.call(c,c,e,a)&&d.push(c));return d},filterFiles:function(a,b,c){if(a.length){var d,e=a.concat(),f=[],g=[];!function h(){e.length?(d=e.shift(),Y.getInfo(d,function(a,c){(b(d,a?!1:c)?f:g).push(d),h()})):c(f,g)}()}else c([],a)},upload:function(a){a=S({jsonp:"callback",prepare:Y.F,beforeupload:Y.F,upload:Y.F,fileupload:Y.F,fileprogress:Y.F,filecomplete:Y.F,progress:Y.F,complete:Y.F,pause:Y.F,imageOriginal:!0,chunkSize:Y.chunkSize,chunkUploadRetry:Y.chunkUploadRetry,uploadRetry:Y.uploadRetry},a),a.imageAutoOrientation&&!a.imageTransform&&(a.imageTransform={rotate:"auto"});var b,c=new Y.XHR(a),d=this._getFilesDataArray(a.files),e=this,f=0,g=0,h=!1;return R(d,function(a){f+=a.size}),c.files=[],R(d,function(a){c.files.push(a.file)}),c.total=f,c.loaded=0,c.filesLeft=d.length,a.beforeupload(c,a),b=function(){var j=d.shift(),k=j&&j.file,l=!1,m=i(a);if(c.filesLeft=d.length,k&&k.name===Y.expando&&(k=null,Y.log("[warn] FileAPI.upload() — called without files")),("abort"!=c.statusText||c.current)&&j){if(h=!1,c.currentFile=k,k&&a.prepare(k,m)===!1)return void b.call(e);m.file=k,e._getFormData(m,j,function(h){g||a.upload(c,a);var i=new Y.XHR(S({},m,{upload:k?function(){a.fileupload(k,i,m)}:n,progress:k?function(b){l||(l=b.loaded===b.total,a.fileprogress({type:"progress",total:j.total=b.total,loaded:j.loaded=b.loaded},k,i,m),a.progress({type:"progress",total:f,loaded:c.loaded=g+j.size*(b.loaded/b.total)|0},k,i,m))}:n,complete:function(d){R(N,function(a){c[a]=i[a]}),k&&(j.total=j.total||j.size,j.loaded=j.total,d||(this.progress(j),l=!0,g+=j.size,c.loaded=g),a.filecomplete(d,i,k,m)),setTimeout(function(){b.call(e)},0)}}));c.abort=function(a){a||(d.length=0),this.current=a,i.abort()},i.send(h)})}else{var o=200==c.status||201==c.status||204==c.status;a.complete(o?!1:c.statusText||"error",c,a),h=!0}},setTimeout(b,0),c.append=function(a,g){a=Y._getFilesDataArray([].concat(a)),R(a,function(a){f+=a.size,c.files.push(a.file),g?d.unshift(a):d.push(a)}),c.statusText="",h&&b.call(e)},c.remove=function(a){for(var b,c=d.length;c--;)d[c].file==a&&(b=d.splice(c,1),f-=b.size);return b},c},_getFilesDataArray:function(a){var b=[],c={};if(j(a)){var d=Y.getFiles(a);c[a.name||"file"]=null!==a.getAttribute("multiple")?d:d[0]}else Q(a)&&j(a[0])?R(a,function(a){c[a.name||"file"]=Y.getFiles(a)}):c=a;return R(c,function e(a,c){Q(a)?R(a,function(a){e(a,c)}):a&&(a.name||a.image)&&b.push({name:c,file:a,size:a.size,total:a.size,loaded:0})}),b.length||b.push({file:{name:Y.expando}}),b},_getFormData:function(a,b,c){var d=b.file,e=b.name,f=d.name,g=d.type,h=Y.support.transform&&a.imageTransform,i=new Y.Form,j=Y.queue(function(){c(i)}),k=h&&l(h),m=Y.postNameConcat;R(a.data,function n(a,b){"object"==typeof a?R(a,function(a,c){n(a,m(b,c))}):i.append(b,a)}),function o(b){b.image?(j.inc(),b.toData(function(a,b){f=f||(new Date).getTime()+".png",o(b),j.next()})):Y.Image&&h&&(/^image/.test(b.type)||E.test(b.nodeName))?(j.inc(),k&&(h=[h]),Y.Image.transform(b,h,a.imageAutoOrientation,function(c,d){if(k&&!c)B||Y.flashEngine||(i.multipart=!0),i.append(e,d[0],f,h[0].type||g);else{var l=0;c||R(d,function(a,b){B||Y.flashEngine||(i.multipart=!0),h[b].postName||(l=1),i.append(h[b].postName||m(e,b),a,f,h[b].type||g)}),(c||a.imageOriginal)&&i.append(m(e,l?"original":null),b,f,g)}j.next()})):f!==Y.expando&&i.append(e,b,f)}(d),j.check()},reset:function(a,b){var c,d;return x?(d=x(a).clone(!0).insertBefore(a).val("")[0],b||x(a).remove()):(c=a.parentNode,d=c.insertBefore(a.cloneNode(!0),a),d.value="",b||c.removeChild(a),R(K[Y.uid(a)],function(b,c){R(b,function(b){U(a,c,b),T(d,c,b)})})),d},load:function(a,b){var c=Y.getXHR();return c?(c.open("GET",a,!0),c.overrideMimeType&&c.overrideMimeType("text/plain; charset=x-user-defined"),T(c,"progress",function(a){a.lengthComputable&&b({type:a.type,loaded:a.loaded,total:a.total},c)}),c.onreadystatechange=function(){if(4==c.readyState)if(c.onreadystatechange=null,200==c.status){a=a.split("/");var d={name:a[a.length-1],size:c.getResponseHeader("Content-Length"),type:c.getResponseHeader("Content-Type")};d.dataURL="data:"+d.type+";base64,"+Y.encode64(c.responseBody||c.responseText),b({type:"load",result:d},c)}else b({type:"error"},c)},c.send(null)):b({type:"error"}),c},encode64:function(a){var b="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",c="",d=0;for("string"!=typeof a&&(a=String(a));d>2,k=(3&g)<<4|h>>4;isNaN(h)?e=f=64:(e=(15&h)<<2|i>>6,f=isNaN(i)?64:63&i),c+=b.charAt(j)+b.charAt(k)+b.charAt(e)+b.charAt(f)}return c}};Y.addInfoReader(/^image/,function(a,b){if(!a.__dimensions){var c=a.__dimensions=Y.defer();Y.readAsImage(a,function(a){var b=a.target;c.resolve("load"==a.type?!1:"error",{width:b.width,height:b.height}),b.src=Y.EMPTY_PNG,b=null})}a.__dimensions.then(b)}),Y.event.dnd=function(a,b,c){var d,e;c||(c=b,b=Y.F),u?(T(a,"dragenter dragleave dragover",b.ff=b.ff||function(a){for(var c=k(a).types,f=c&&c.length,g=!1;f--;)if(~c[f].indexOf("File")){a[P](),e!==a.type&&(e=a.type,"dragleave"!=e&&b.call(a[O],!0,a),g=!0);break}g&&(clearTimeout(d),d=setTimeout(function(){b.call(a[O],"dragleave"!=e,a)},50))}),T(a,"drop",c.ff=c.ff||function(a){a[P](),e=0,b.call(a[O],!1,a),Y.getDropFiles(a,function(b){c.call(a[O],b,a)})})):Y.log("Drag'n'Drop -- not supported")},Y.event.dnd.off=function(a,b,c){U(a,"dragenter dragleave dragover",b.ff),U(a,"drop",c.ff)},x&&!x.fn.dnd&&(x.fn.dnd=function(a,b){return this.each(function(){Y.event.dnd(this,a,b)})},x.fn.offdnd=function(a,b){return this.each(function(){Y.event.dnd.off(this,a,b)})}),a.FileAPI=S(Y,a.FileAPI),Y.log("FileAPI: "+Y.version),Y.log("protocol: "+a.location.protocol),Y.log("doctype: ["+p.name+"] "+p.publicId+" "+p.systemId),R(o.getElementsByTagName("meta"),function(a){/x-ua-compatible/i.test(a.getAttribute("http-equiv"))&&Y.log("meta.http-equiv: "+a.getAttribute("content"))});try{Y._supportConsoleLog=!!console.log,Y._supportConsoleLogApply=!!console.log.apply}catch(Z){}Y.flashUrl||(Y.flashUrl=Y.staticPath+"FileAPI.flash.swf"),Y.flashImageUrl||(Y.flashImageUrl=Y.staticPath+"FileAPI.flash.image.swf"),Y.flashWebcamUrl||(Y.flashWebcamUrl=Y.staticPath+"FileAPI.flash.camera.swf")}(window,void 0),function(a,b,c){"use strict";function d(b){if(b instanceof d){var c=new d(b.file);return a.extend(c.matrix,b.matrix),c}return this instanceof d?(this.file=b,this.size=b.size||100,void(this.matrix={sx:0,sy:0,sw:0,sh:0,dx:0,dy:0,dw:0,dh:0,resize:0,deg:0,quality:1,filter:0})):new d(b)}var e=Math.min,f=Math.round,g=function(){return b.createElement("canvas")},h=!1,i={8:270,3:180,6:90,7:270,4:180,5:90};try{h=g().toDataURL("image/png").indexOf("data:image/png")>-1}catch(j){}d.prototype={image:!0,constructor:d,set:function(b){return a.extend(this.matrix,b),this},crop:function(a,b,d,e){return d===c&&(d=a,e=b,a=b=0),this.set({sx:a,sy:b,sw:d,sh:e||d})},resize:function(a,b,c){return/min|max/.test(b)&&(c=b,b=a),this.set({dw:a,dh:b||a,resize:c})},preview:function(a,b){return this.resize(a,b||a,"preview")},rotate:function(a){return this.set({deg:a})},filter:function(a){return this.set({filter:a})},overlay:function(a){return this.set({overlay:a})},clone:function(){return new d(this)},_load:function(b,c){var d=this;/img|video/i.test(b.nodeName)?c.call(d,null,b):a.readAsImage(b,function(a){c.call(d,"load"!=a.type,a.result)})},_apply:function(b,c){var f,h=g(),i=this.getMatrix(b),j=h.getContext("2d"),k=b.videoWidth||b.width,l=b.videoHeight||b.height,m=i.deg,n=i.dw,o=i.dh,p=k,q=l,r=i.filter,s=b,t=i.overlay,u=a.queue(function(){b.src=a.EMPTY_PNG,c(!1,h)}),v=a.renderImageToCanvas;for(m-=360*Math.floor(m/360),b._type=this.file.type;i.multipass&&e(p/n,q/o)>2;)p=p/2+.5|0,q=q/2+.5|0,f=g(),f.width=p,f.height=q,s!==b?(v(f,s,0,0,s.width,s.height,0,0,p,q),s=f):(s=f,v(s,b,i.sx,i.sy,i.sw,i.sh,0,0,p,q),i.sx=i.sy=i.sw=i.sh=0);h.width=m%180?o:n,h.height=m%180?n:o,h.type=i.type,h.quality=i.quality,j.rotate(m*Math.PI/180),v(j.canvas,s,i.sx,i.sy,i.sw||s.width,i.sh||s.height,180==m||270==m?-n:0,90==m||180==m?-o:0,n,o),n=h.width,o=h.height,t&&a.each([].concat(t),function(b){u.inc();var c=new window.Image,d=function(){var e=0|b.x,f=0|b.y,g=b.w||c.width,h=b.h||c.height,i=b.rel;e=1==i||4==i||7==i?(n-g+e)/2:2==i||5==i||8==i?n-(g+e):e,f=3==i||4==i||5==i?(o-h+f)/2:i>=6?o-(h+f):f,a.event.off(c,"error load abort",d);try{j.globalAlpha=b.opacity||1,j.drawImage(c,e,f,g,h)}catch(k){}u.next()};a.event.on(c,"error load abort",d),c.src=b.src,c.complete&&d()}),r&&(u.inc(),d.applyFilter(h,r,u.next)),u.check()},getMatrix:function(b){var c=a.extend({},this.matrix),d=c.sw=c.sw||b.videoWidth||b.naturalWidth||b.width,g=c.sh=c.sh||b.videoHeight||b.naturalHeight||b.height,h=c.dw=c.dw||d,i=c.dh=c.dh||g,j=d/g,k=h/i,l=c.resize;if("preview"==l){if(h!=d||i!=g){var m,n;k>=j?(m=d,n=m/k):(n=g,m=n*k),(m!=d||n!=g)&&(c.sx=~~((d-m)/2),c.sy=~~((g-n)/2),d=m,g=n)}}else l&&(d>h||g>i?"min"==l?(h=f(k>j?e(d,h):i*j),i=f(k>j?h/j:e(g,i))):(h=f(j>=k?e(d,h):i*j),i=f(j>=k?h/j:e(g,i))):(h=d,i=g));return c.sw=d,c.sh=g,c.dw=h,c.dh=i,c.multipass=a.multiPassResize,c},_trans:function(b){this._load(this.file,function(c,d){if(c)b(c);else try{this._apply(d,b)}catch(c){a.log("[err] FileAPI.Image.fn._apply:",c),b(c)}})},get:function(b){if(a.support.transform){var c=this,d=c.matrix;"auto"==d.deg?a.getInfo(c.file,function(a,e){d.deg=i[e&&e.exif&&e.exif.Orientation]||0,c._trans(b)}):c._trans(b)}else b("not_support_transform");return this},toData:function(a){return this.get(a)}},d.exifOrientation=i,d.transform=function(b,e,f,g){function h(h,i){var j={},k=a.queue(function(a){g(a,j)});h?k.fail():a.each(e,function(a,e){if(!k.isFail()){var g=new d(i.nodeType?i:b),h="function"==typeof a;if(h?a(i,g):a.width?g[a.preview?"preview":"resize"](a.width,a.height,a.strategy):a.maxWidth&&(i.width>a.maxWidth||i.height>a.maxHeight)&&g.resize(a.maxWidth,a.maxHeight,"max"),a.crop){var l=a.crop;g.crop(0|l.x,0|l.y,l.w||l.width,l.h||l.height)}a.rotate===c&&f&&(a.rotate="auto"),g.set({type:g.matrix.type||a.type||b.type||"image/png"}),h||g.set({deg:a.rotate,overlay:a.overlay,filter:a.filter,quality:a.quality||1}),k.inc(),g.toData(function(a,b){a?k.fail():(j[e]=b,k.next())})}})}b.width?h(!1,b):a.getInfo(b,h)},a.each(["TOP","CENTER","BOTTOM"],function(b,c){a.each(["LEFT","CENTER","RIGHT"],function(a,e){d[b+"_"+a]=3*c+e,d[a+"_"+b]=3*c+e})}),d.toCanvas=function(a){var c=b.createElement("canvas");return c.width=a.videoWidth||a.width,c.height=a.videoHeight||a.height,c.getContext("2d").drawImage(a,0,0),c},d.fromDataURL=function(b,c,d){var e=a.newImage(b);a.extend(e,c),d(e)},d.applyFilter=function(b,c,e){"function"==typeof c?c(b,e):window.Caman&&window.Caman("IMG"==b.tagName?d.toCanvas(b):b,function(){"string"==typeof c?this[c]():a.each(c,function(a,b){this[b](a)},this),this.render(e)})},a.renderImageToCanvas=function(b,c,d,e,f,g,h,i,j,k){try{return b.getContext("2d").drawImage(c,d,e,f,g,h,i,j,k)}catch(l){throw a.log("renderImageToCanvas failed"),l}},a.support.canvas=a.support.transform=h,a.Image=d}(FileAPI,document),function(a){"use strict";a(FileAPI)}(function(a){"use strict";if(window.navigator&&window.navigator.platform&&/iP(hone|od|ad)/.test(window.navigator.platform)){var b=a.renderImageToCanvas;a.detectSubsampling=function(a){var b,c;return a.width*a.height>1048576?(b=document.createElement("canvas"),b.width=b.height=1,c=b.getContext("2d"),c.drawImage(a,-a.width+1,0),0===c.getImageData(0,0,1,1).data[3]):!1},a.detectVerticalSquash=function(a,b){var c,d,e,f,g,h=a.naturalHeight||a.height,i=document.createElement("canvas"),j=i.getContext("2d");for(b&&(h/=2),i.width=1,i.height=h,j.drawImage(a,0,0),c=j.getImageData(0,0,1,h).data,d=0,e=h,f=h;f>d;)g=c[4*(f-1)+3],0===g?e=f:d=f,f=e+d>>1;return f/h||1},a.renderImageToCanvas=function(c,d,e,f,g,h,i,j,k,l){if("image/jpeg"===d._type){var m,n,o,p,q=c.getContext("2d"),r=document.createElement("canvas"),s=1024,t=r.getContext("2d");if(r.width=s,r.height=s,q.save(),m=a.detectSubsampling(d),m&&(e/=2,f/=2,g/=2,h/=2),n=a.detectVerticalSquash(d,m),m||1!==n){for(f*=n,k=Math.ceil(s*k/g),l=Math.ceil(s*l/h/n),j=0,p=0;h>p;){for(i=0,o=0;g>o;)t.clearRect(0,0,s,s),t.drawImage(d,e,f,g,h,-o,-p,g,h),q.drawImage(r,0,0,s,s,i,j,k,l),o+=s,i+=k;p+=s,j+=l}return q.restore(),c}}return b(c,d,e,f,g,h,i,j,k,l)}}}),function(a,b){"use strict";function c(b,c,d){var e=b.blob,f=b.file;if(f){if(!e.toDataURL)return void a.readAsBinaryString(e,function(a){"load"==a.type&&c(b,a.result)});var g={"image/jpeg":".jpe?g","image/png":".png"},h=g[b.type]?b.type:"image/png",i=g[h]||".png",j=e.quality||1;f.match(new RegExp(i+"$","i"))||(f+=i.replace("?","")),b.file=f,b.type=h,!d&&e.toBlob?e.toBlob(function(a){c(b,a)},h,j):c(b,a.toBinaryString(e.toDataURL(h,j)))}else c(b,e)}var d=b.document,e=b.FormData,f=function(){this.items=[]},g=b.encodeURIComponent;f.prototype={append:function(a,b,c,d){this.items.push({name:a,blob:b&&b.blob||(void 0==b?"":b),file:b&&(c||b.name),type:b&&(d||b.type)})},each:function(a){for(var b=0,c=this.items.length;c>b;b++)a.call(this,this.items[b])},toData:function(b,c){c._chunked=a.support.chunked&&c.chunkSize>0&&1==a.filter(this.items,function(a){return a.file}).length,a.support.html5?a.formData&&!this.multipart&&e?c._chunked?(a.log("FileAPI.Form.toPlainData"),this.toPlainData(b)):(a.log("FileAPI.Form.toFormData"),this.toFormData(b)):(a.log("FileAPI.Form.toMultipartData"),this.toMultipartData(b)):(a.log("FileAPI.Form.toHtmlData"),this.toHtmlData(b))},_to:function(b,c,d,e){var f=a.queue(function(){c(b)});this.each(function(a){d(a,b,f,e)}),f.check()},toHtmlData:function(b){this._to(d.createDocumentFragment(),b,function(b,c){var e,f=b.blob;b.file?(a.reset(f,!0),f.name=b.name,f.disabled=!1,c.appendChild(f)):(e=d.createElement("input"),e.name=b.name,e.type="hidden",e.value=f,c.appendChild(e))})},toPlainData:function(a){this._to({},a,function(a,b,d){a.file&&(b.type=a.file),a.blob.toBlob?(d.inc(),c(a,function(a,c){b.name=a.name,b.file=c,b.size=c.length,b.type=a.type,d.next()})):a.file?(b.name=a.blob.name,b.file=a.blob,b.size=a.blob.size,b.type=a.type):(b.params||(b.params=[]),b.params.push(g(a.name)+"="+g(a.blob))),b.start=-1,b.end=b.file&&b.file.FileAPIReadPosition||-1,b.retry=0})},toFormData:function(a){this._to(new e,a,function(a,b,d){a.blob&&a.blob.toBlob?(d.inc(),c(a,function(a,c){b.append(a.name,c,a.file),d.next()})):a.file?b.append(a.name,a.blob,a.file):b.append(a.name,a.blob),a.file&&b.append("_"+a.name,a.file)})},toMultipartData:function(b){this._to([],b,function(a,b,d,e){d.inc(),c(a,function(a,c){b.push("--_"+e+('\r\nContent-Disposition: form-data; name="'+a.name+'"'+(a.file?'; filename="'+g(a.file)+'"':"")+(a.file?"\r\nContent-Type: "+(a.type||"application/octet-stream"):"")+"\r\n\r\n"+(a.file?c:g(c))+"\r\n")),d.next()},!0)},a.expando)}},a.Form=f}(FileAPI,window),function(a,b){"use strict";var c=function(){},d=a.document,e=function(a){this.uid=b.uid(),this.xhr={abort:c,getResponseHeader:c,getAllResponseHeaders:c},this.options=a},f={"":1,XML:1,Text:1,Body:1};e.prototype={status:0,statusText:"",constructor:e,getResponseHeader:function(a){return this.xhr.getResponseHeader(a)},getAllResponseHeaders:function(){return this.xhr.getAllResponseHeaders()||{}},end:function(d,e){var f=this,g=f.options;f.end=f.abort=c,f.status=d,e&&(f.statusText=e),b.log("xhr.end:",d,e),g.complete(200==d||201==d?!1:f.statusText||"unknown",f),f.xhr&&f.xhr.node&&setTimeout(function(){var b=f.xhr.node;try{b.parentNode.removeChild(b)}catch(c){}try{delete a[f.uid]}catch(c){}a[f.uid]=f.xhr.node=null},9)},abort:function(){this.end(0,"abort"),this.xhr&&(this.xhr.aborted=!0,this.xhr.abort())},send:function(a){var b=this,c=this.options;a.toData(function(a){c.upload(c,b),b._send.call(b,c,a)},c)},_send:function(c,e){var g,h=this,i=h.uid,j=h.uid+"Load",k=c.url;if(b.log("XHR._send:",e),c.cache||(k+=(~k.indexOf("?")?"&":"?")+b.uid()),e.nodeName){var l=c.jsonp;k=k.replace(/([a-z]+)=(\?)/i,"$1="+i),c.upload(c,h);var m=function(a){if(~k.indexOf(a.origin))try{var c=b.parseJSON(a.data);c.id==i&&n(c.status,c.statusText,c.response)}catch(d){n(0,d.message)}},n=a[i]=function(c,d,e){h.readyState=4,h.responseText=e,h.end(c,d),b.event.off(a,"message",m),a[i]=g=p=a[j]=null};h.xhr.abort=function(){try{p.stop?p.stop():p.contentWindow.stop?p.contentWindow.stop():p.contentWindow.document.execCommand("Stop")}catch(a){}n(0,"abort")},b.event.on(a,"message",m),a[j]=function(){try{var a=p.contentWindow,c=a.document,d=a.result||b.parseJSON(c.body.innerHTML);n(d.status,d.statusText,d.response)}catch(e){b.log("[transport.onload]",e)}},g=d.createElement("div"),g.innerHTML='
'+(l&&c.url.indexOf("=?")<0?'':"")+"
";var o=g.getElementsByTagName("form")[0],p=g.getElementsByTagName("iframe")[0];o.appendChild(e),b.log(o.parentNode.innerHTML),d.body.appendChild(g),h.xhr.node=g,h.readyState=2,o.submit(),o=null}else{if(k=k.replace(/([a-z]+)=(\?)&?/i,""),this.xhr&&this.xhr.aborted)return void b.log("Error: already aborted");if(g=h.xhr=b.getXHR(),e.params&&(k+=(k.indexOf("?")<0?"?":"&")+e.params.join("&")),g.open("POST",k,!0),b.withCredentials&&(g.withCredentials="true"),c.headers&&c.headers["X-Requested-With"]||g.setRequestHeader("X-Requested-With","XMLHttpRequest"),b.each(c.headers,function(a,b){g.setRequestHeader(b,a)}),c._chunked){g.upload&&g.upload.addEventListener("progress",b.throttle(function(a){e.retry||c.progress({type:a.type,total:e.size,loaded:e.start+a.loaded,totalSize:e.size},h,c)},100),!1),g.onreadystatechange=function(){var a=parseInt(g.getResponseHeader("X-Last-Known-Byte"),10);if(h.status=g.status,h.statusText=g.statusText,h.readyState=g.readyState,4==g.readyState){try{for(var d in f)h["response"+d]=g["response"+d]}catch(i){}if(g.onreadystatechange=null,!g.status||g.status-201>0)if(b.log("Error: "+g.status),(!g.status&&!g.aborted||500==g.status||416==g.status)&&++e.retry<=c.chunkUploadRetry){var j=g.status?0:b.chunkNetworkDownRetryTimeout;c.pause(e.file,c),b.log("X-Last-Known-Byte: "+a),a?e.end=a:(e.end=e.start-1,416==g.status&&(e.end=e.end-c.chunkSize)),setTimeout(function(){h._send(c,e)},j)}else h.end(g.status);else e.retry=0,e.end==e.size-1?h.end(g.status):(b.log("X-Last-Known-Byte: "+a),a&&(e.end=a),e.file.FileAPIReadPosition=e.end,setTimeout(function(){h._send(c,e)},0));g=null}},e.start=e.end+1,e.end=Math.max(Math.min(e.start+c.chunkSize,e.size)-1,e.start);var q=e.file,r=(q.slice||q.mozSlice||q.webkitSlice).call(q,e.start,e.end+1);e.size&&!r.size?setTimeout(function(){h.end(-1)}):(g.setRequestHeader("Content-Range","bytes "+e.start+"-"+e.end+"/"+e.size),g.setRequestHeader("Content-Disposition","attachment; filename="+encodeURIComponent(e.name)),g.setRequestHeader("Content-Type",e.type||"application/octet-stream"),g.send(r)),q=r=null}else if(g.upload&&g.upload.addEventListener("progress",b.throttle(function(a){c.progress(a,h,c)},100),!1),g.onreadystatechange=function(){if(h.status=g.status,h.statusText=g.statusText,h.readyState=g.readyState,4==g.readyState){for(var a in f)h["response"+a]=g["response"+a];if(g.onreadystatechange=null,!g.status||g.status>201)if(b.log("Error: "+g.status),(!g.status&&!g.aborted||500==g.status)&&(c.retry||0)=0?a+"px":a}function d(a){var b,c=f.createElement("canvas"),d=!1;try{b=c.getContext("2d"),b.drawImage(a,0,0,1,1),d=255!=b.getImageData(0,0,1,1).data[4]}catch(e){}return d}var e=a.URL||a.webkitURL,f=a.document,g=a.navigator,h=g.getUserMedia||g.webkitGetUserMedia||g.mozGetUserMedia||g.msGetUserMedia,i=!!h;b.support.media=i;var j=function(a){this.video=a};j.prototype={isActive:function(){return!!this._active},start:function(a){var b,c,f=this,i=f.video,j=function(d){f._active=!d,clearTimeout(c),clearTimeout(b),a&&a(d,f)};h.call(g,{video:!0},function(a){f.stream=a,i.src=e.createObjectURL(a),b=setInterval(function(){d(i)&&j(null)},1e3),c=setTimeout(function(){j("timeout")},5e3),i.play()},j)},stop:function(){try{this._active=!1,this.video.pause(),this.stream.stop()}catch(a){}},shot:function(){return new k(this.video)}},j.get=function(a){return new j(a.firstChild)},j.publish=function(d,e,g){"function"==typeof e&&(g=e,e={}),e=b.extend({},{width:"100%",height:"100%",start:!0},e),d.jquery&&(d=d[0]);var h=function(a){if(a)g(a);else{var b=j.get(d);e.start?b.start(g):g(null,b)}};if(d.style.width=c(e.width),d.style.height=c(e.height),b.html5&&i){var k=f.createElement("video");k.style.width=c(e.width),k.style.height=c(e.height),a.jQuery?jQuery(d).empty():d.innerHTML="",d.appendChild(k),h()}else j.fallback(d,e,h)},j.fallback=function(a,b,c){c("not_support_camera")};var k=function(a){var c=a.nodeName?b.Image.toCanvas(a):a,d=b.Image(c);return d.type="image/png",d.width=c.width,d.height=c.height,d.size=c.width*c.height*4,d};j.Shot=k,b.Camera=j}(window,FileAPI),function(a,b,c){"use strict";var d=a.document,e=a.location,f=a.navigator,g=c.each;c.support.flash=function(){var b=f.mimeTypes,d=!1;if(f.plugins&&"object"==typeof f.plugins["Shockwave Flash"])d=f.plugins["Shockwave Flash"].description&&!(b&&b["application/x-shockwave-flash"]&&!b["application/x-shockwave-flash"].enabledPlugin); +else try{d=!(!a.ActiveXObject||!new ActiveXObject("ShockwaveFlash.ShockwaveFlash"))}catch(g){c.log("Flash -- does not supported.")}return d&&/^file:/i.test(e)&&c.log("[warn] Flash does not work on `file:` protocol."),d}(),c.support.flash&&(0||!c.html5||!c.support.html5||c.cors&&!c.support.cors||c.media&&!c.support.media)&&function(){function h(a){return('').replace(/#(\w+)#/gi,function(b,c){return a[c]})}function i(a,b){if(a&&a.style){var c,d;for(c in b){d=b[c],"number"==typeof d&&(d+="px");try{a.style[c]=d}catch(e){}}}}function j(a,b){g(b,function(b,c){var d=a[c];a[c]=function(){return this.parent=d,b.apply(this,arguments)}})}function k(a){return a&&!a.flashId}function l(a){var b=a.wid=c.uid();return v._fn[b]=a,"FileAPI.Flash._fn."+b}function m(a){try{v._fn[a.wid]=null,delete v._fn[a.wid]}catch(b){}}function n(a,b){if(!u.test(a)){if(/^\.\//.test(a)||"/"!=a.charAt(0)){var c=e.pathname;c=c.substr(0,c.lastIndexOf("/")),a=(c+"/"+a).replace("/./","/")}"//"!=a.substr(0,2)&&(a="//"+e.host+a),u.test(a)||(a=e.protocol+a)}return b&&(a+=(/\?/.test(a)?"&":"?")+b),a}function o(a,b,e){function f(){try{var a=v.get(j);a.setImage(b)}catch(d){c.log('[err] FlashAPI.Preview.setImage -- can not set "base64":',d)}}var g,j=c.uid(),k=d.createElement("div"),o=10;for(g in a)k.setAttribute(g,a[g]),k[g]=a[g];i(k,a),a.width="100%",a.height="100%",k.innerHTML=h(c.extend({id:j,src:n(c.flashImageUrl,"r="+c.uid()),wmode:"opaque",flashvars:"scale="+a.scale+"&callback="+l(function p(){return m(p),--o>0&&f(),!0})},a)),e(!1,k),k=null}function p(a){return{id:a.id,name:a.name,matrix:a.matrix,flashId:a.flashId}}function q(a){function b(a){var b,c;if(b=c=0,a.offsetParent)do b+=a.offsetLeft,c+=a.offsetTop;while(a=a.offsetParent);return{left:b,top:c}}a.getBoundingClientRect(),d.body,(a&&a.ownerDocument).documentElement;return{top:b(a).top,left:b(a).left,width:a.offsetWidth,height:a.offsetHeight}}var r=c.uid(),s=0,t={},u=/^https?:/i,v={_fn:{},publish:function(a,b,d){d=d||{},a.innerHTML=h({id:b,src:n(c.flashUrl,"r="+c.version),wmode:d.camera?"":"transparent",flashvars:"callback="+(d.onEvent||"FileAPI.Flash.onEvent")+"&flashId="+b+"&storeKey="+f.userAgent.match(/\d/gi).join("")+"_"+c.version+(v.isReady||(c.pingUrl?"&ping="+c.pingUrl:""))+"&timeout="+c.flashAbortTimeout+(d.camera?"&useCamera="+n(c.flashWebcamUrl):"")+"&debug="+(c.debug?"1":"")},d)},init:function(){var a=d.body&&d.body.firstChild;if(a)do if(1==a.nodeType){c.log("FlashAPI.state: awaiting");var b=d.createElement("div");return b.id="_"+r,i(b,{top:1,right:1,width:5,height:5,position:"absolute",zIndex:1e6+""}),a.parentNode.insertBefore(b,a),void v.publish(b,r)}while(a=a.nextSibling);10>s&&setTimeout(v.init,50*++s)},ready:function(){c.log("FlashAPI.state: ready"),v.ready=c.F,v.isReady=!0,v.patch(),v.patchCamera&&v.patchCamera(),c.event.on(d,"mouseover",v.mouseover),c.event.on(d,"click",function(a){v.mouseover(a)&&(a.preventDefault?a.preventDefault():a.returnValue=!0)})},getEl:function(){return d.getElementById("_"+r)},getWrapper:function(a){do if(/js-fileapi-wrapper/.test(a.className))return a;while((a=a.parentNode)&&a!==d.body)},disableMouseover:!1,mouseover:function(a){if(!v.disableMouseover){var b=c.event.fix(a).target;if(/input/i.test(b.nodeName)&&"file"==b.type&&!b.disabled){var e=b.getAttribute(r),f=v.getWrapper(b);if(c.multiFlash){if("i"==e||"r"==e)return!1;if("p"!=e){b.setAttribute(r,"i");var g=d.createElement("div");if(!f)return void c.log("[err] FlashAPI.mouseover: js-fileapi-wrapper not found");i(g,{top:0,left:0,width:b.offsetWidth,height:b.offsetHeight,zIndex:1e6+"",position:"absolute"}),f.appendChild(g),v.publish(g,c.uid()),b.setAttribute(r,"p")}return!0}if(f){var h=q(f);i(v.getEl(),h),v.curInp=b}}else/object|embed/i.test(b.nodeName)||i(v.getEl(),{top:1,left:1,width:5,height:5})}},onEvent:function(a){var b=a.type;if("ready"==b){try{v.getInput(a.flashId).setAttribute(r,"r")}catch(d){}return v.ready(),setTimeout(function(){v.mouseenter(a)},50),!0}"ping"===b?c.log("(flash -> js).ping:",[a.status,a.savedStatus],a.error):"log"===b?c.log("(flash -> js).log:",a.target):b in v&&setTimeout(function(){c.log("FlashAPI.event."+a.type+":",a),v[b](a)},1)},mouseDown:function(){v.disableMouseover=!0},cancel:function(){v.disableMouseover=!1},mouseenter:function(a){var b=v.getInput(a.flashId);if(b){v.cmd(a,"multiple",null!=b.getAttribute("multiple"));var d=[],e={};g((b.getAttribute("accept")||"").split(/,\s*/),function(a){c.accept[a]&&g(c.accept[a].split(" "),function(a){e[a]=1})}),g(e,function(a,b){d.push(b)}),v.cmd(a,"accept",d.length?d.join(",")+","+d.join(",").toUpperCase():"*")}},get:function(b){return d[b]||a[b]||d.embeds[b]},getInput:function(a){if(!c.multiFlash)return v.curInp;try{var b=v.getWrapper(v.get(a));if(b)return b.getElementsByTagName("input")[0]}catch(d){c.log('[err] Can not find "input" by flashId:',a,d)}},select:function(a){try{var e,f=v.getInput(a.flashId),h=c.uid(f),i=a.target.files;g(i,function(a){c.checkFileObj(a)}),t[h]=i,d.createEvent?(e=d.createEvent("Event"),e.files=i,e.initEvent("change",!0,!0),f.dispatchEvent(e)):b?b(f).trigger({type:"change",files:i}):(e=d.createEventObject(),e.files=i,f.fireEvent("onchange",e))}finally{v.disableMouseover=!1}},interval:null,cmd:function(a,b,c,d){v.uploadInProgress&&v.readInProgress?setTimeout(function(){v.cmd(a,b,c,d)},100):this.cmdFn(a,b,c,d)},cmdFn:function(a,b,d,e){try{return c.log("(js -> flash)."+b+":",d),v.get(a.flashId||a).cmd(b,d)}catch(f){c.log("(js -> flash).onError:",f),e||setTimeout(function(){v.cmd(a,b,d,!0)},50)}},patch:function(){c.flashEngine=!0,j(c,{readAsDataURL:function(a,b){k(a)?this.parent.apply(this,arguments):(c.log("FlashAPI.readAsBase64"),v.readInProgress=!0,v.cmd(a,"readAsBase64",{id:a.id,callback:l(function d(e,f){v.readInProgress=!1,m(d),c.log("FlashAPI.readAsBase64:",e),b({type:e?"error":"load",error:e,result:"data:"+a.type+";base64,"+f})})}))},readAsText:function(b,d,e){e?c.log("[warn] FlashAPI.readAsText not supported `encoding` param"):e=d,c.readAsDataURL(b,function(b){if("load"==b.type)try{b.result=a.atob(b.result.split(";base64,")[1])}catch(c){b.type="error",b.error=c.toString()}e(b)})},getFiles:function(a,b,d){if(d)return c.filterFiles(c.getFiles(a),b,d),null;var e=c.isArray(a)?a:t[c.uid(a.target||a.srcElement||a)];return e?(b&&(b=c.getFilesFilter(b),e=c.filter(e,function(a){return b.test(a.name)})),e):this.parent.apply(this,arguments)},getInfo:function(a,b){if(k(a))this.parent.apply(this,arguments);else if(a.isShot)b(null,a.info={width:a.width,height:a.height});else{if(!a.__info){var d=a.__info=c.defer();d.resolve(null,a.info=null)}a.__info.then(b)}}}),c.support.transform=!0,c.Image&&j(c.Image.prototype,{get:function(a,b){return this.set({scaleMode:b||"noScale"}),this.parent(a)},_load:function(a,b){if(c.log("FlashAPI.Image._load:",a),k(a))this.parent.apply(this,arguments);else{var d=this;c.getInfo(a,function(c){b.call(d,c,a)})}},_apply:function(a,b){if(c.log("FlashAPI.Image._apply:",a),k(a))this.parent.apply(this,arguments);else{var d=this.getMatrix(a.info),e=b;v.cmd(a,"imageTransform",{id:a.id,matrix:d,callback:l(function f(g,h){c.log("FlashAPI.Image._apply.callback:",g),m(f),g?e(g):c.support.html5||c.support.dataURI&&!(h.length>3e4)?(d.filter&&(e=function(a,e){a?b(a):c.Image.applyFilter(e,d.filter,function(){b(a,this.canvas)})}),c.newImage("data:"+a.type+";base64,"+h,e)):o({width:d.deg%180?d.dh:d.dw,height:d.deg%180?d.dw:d.dh,scale:d.scaleMode},h,e)})})}},toData:function(a){var b=this.file,d=b.info,e=this.getMatrix(d);c.log("FlashAPI.Image.toData"),k(b)?this.parent.apply(this,arguments):("auto"==e.deg&&(e.deg=c.Image.exifOrientation[d&&d.exif&&d.exif.Orientation]||0),a.call(this,!b.info,{id:b.id,flashId:b.flashId,name:b.name,type:b.type,matrix:e}))}}),c.Image&&j(c.Image,{fromDataURL:function(a,b,d){!c.support.dataURI||a.length>3e4?o(c.extend({scale:"exactFit"},b),a.replace(/^data:[^,]+,/,""),function(a,b){d(b)}):this.parent(a,b,d)}}),j(c.Form.prototype,{toData:function(a){for(var b=this.items,d=b.length;d--;)if(b[d].file&&k(b[d].blob))return this.parent.apply(this,arguments);c.log("FlashAPI.Form.toData"),a(b)}}),j(c.XHR.prototype,{_send:function(a,b){if(b.nodeName||b.append&&c.support.html5||c.isArray(b)&&"string"==typeof b[0])return this.parent.apply(this,arguments);var d,e,f={},h={},i=this;if(g(b,function(a){a.file?(h[a.name]=a=p(a.blob),e=a.id,d=a.flashId):f[a.name]=a.blob}),e||(d=r),!d)return c.log("[err] FlashAPI._send: flashId -- undefined"),this.parent.apply(this,arguments);c.log("FlashAPI.XHR._send: "+d+" -> "+e),i.xhr={headers:{},abort:function(){v.uploadInProgress=!1,v.cmd(d,"abort",{id:e})},getResponseHeader:function(a){return this.headers[a]},getAllResponseHeaders:function(){return this.headers}};var j=c.queue(function(){v.uploadInProgress=!0,v.cmd(d,"upload",{url:n(a.url.replace(/([a-z]+)=(\?)&?/i,"")),data:f,files:e?h:null,headers:a.headers||{},callback:l(function b(d){var e=d.type,f=d.result;c.log("FlashAPI.upload."+e),"progress"==e?(d.loaded=Math.min(d.loaded,d.total),d.lengthComputable=!0,a.progress(d)):"complete"==e?(v.uploadInProgress=!1,m(b),"string"==typeof f&&(i.responseText=f.replace(/%22/g,'"').replace(/%5c/g,"\\").replace(/%26/g,"&").replace(/%25/g,"%")),i.end(d.status||200)):("abort"==e||"error"==e)&&(v.uploadInProgress=!1,i.end(d.status||0,d.message),m(b))})})});g(h,function(a){j.inc(),c.getInfo(a,j.next)}),j.check()}})}};c.Flash=v,c.newImage("data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///ywAAAAAAQABAAACAUwAOw==",function(a,b){c.support.dataURI=!(1!=b.width||1!=b.height),v.init()})}()}(window,window.jQuery,FileAPI),function(a,b,c){"use strict";var d=c.each,e=[];c.support.flash&&c.media&&!c.support.media&&!function(){function a(a){var b=a.wid=c.uid();return c.Flash._fn[b]=a,"FileAPI.Flash._fn."+b}function b(a){try{c.Flash._fn[a.wid]=null,delete c.Flash._fn[a.wid]}catch(b){}}var f=c.Flash;c.extend(c.Flash,{patchCamera:function(){c.Camera.fallback=function(d,e,g){var h=c.uid();c.log("FlashAPI.Camera.publish: "+h),f.publish(d,h,c.extend(e,{camera:!0,onEvent:a(function i(a){"camera"===a.type&&(b(i),a.error?(c.log("FlashAPI.Camera.publish.error: "+a.error),g(a.error)):(c.log("FlashAPI.Camera.publish.success: "+h),g(null)))})}))},d(e,function(a){c.Camera.fallback.apply(c.Camera,a)}),e=[],c.extend(c.Camera.prototype,{_id:function(){return this.video.id},start:function(d){var e=this;f.cmd(this._id(),"camera.on",{callback:a(function g(a){b(g),a.error?(c.log("FlashAPI.camera.on.error: "+a.error),d(a.error,e)):(c.log("FlashAPI.camera.on.success: "+e._id()),e._active=!0,d(null,e))})})},stop:function(){this._active=!1,f.cmd(this._id(),"camera.off")},shot:function(){c.log("FlashAPI.Camera.shot:",this._id());var a=c.Flash.cmd(this._id(),"shot",{});return a.type="image/png",a.flashId=this._id(),a.isShot=!0,new c.Camera.Shot(a)}})}}),c.Camera.fallback=function(){e.push(arguments)}}()}(window,window.jQuery,FileAPI),"function"==typeof define&&define.amd&&define("FileAPI",[],function(){return FileAPI}); \ No newline at end of file diff --git a/xstatic/pkg/angular_fileupload/data/angular-file-upload.js b/xstatic/pkg/angular_fileupload/data/angular-file-upload.js deleted file mode 100644 index 2aa65fc..0000000 --- a/xstatic/pkg/angular_fileupload/data/angular-file-upload.js +++ /dev/null @@ -1,250 +0,0 @@ -/**! - * AngularJS file upload/drop directive with http post and progress - * @author Danial - * @version 1.4.0 - */ -(function() { - -var angularFileUpload = angular.module('angularFileUpload', []); - -angularFileUpload.service('$upload', ['$http', '$timeout', function($http, $timeout) { - function sendHttp(config) { - config.method = config.method || 'POST'; - config.headers = config.headers || {}; - config.transformRequest = config.transformRequest || function(data, headersGetter) { - if (window.ArrayBuffer && data instanceof window.ArrayBuffer) { - return data; - } - return $http.defaults.transformRequest[0](data, headersGetter); - }; - - if (window.XMLHttpRequest.__isShim) { - config.headers['__setXHR_'] = function() { - return function(xhr) { - if (!xhr) return; - config.__XHR = xhr; - config.xhrFn && config.xhrFn(xhr); - xhr.upload.addEventListener('progress', function(e) { - if (config.progress) { - $timeout(function() { - if(config.progress) config.progress(e); - }); - } - }, false); - //fix for firefox not firing upload progress end, also IE8-9 - xhr.upload.addEventListener('load', function(e) { - if (e.lengthComputable) { - if(config.progress) config.progress(e); - } - }, false); - }; - }; - } - - var promise = $http(config); - - promise.progress = function(fn) { - config.progress = fn; - return promise; - }; - promise.abort = function() { - if (config.__XHR) { - $timeout(function() { - config.__XHR.abort(); - }); - } - return promise; - }; - promise.xhr = function(fn) { - config.xhrFn = fn; - return promise; - }; - promise.then = (function(promise, origThen) { - return function(s, e, p) { - config.progress = p || config.progress; - var result = origThen.apply(promise, [s, e, p]); - result.abort = promise.abort; - result.progress = promise.progress; - result.xhr = promise.xhr; - result.then = promise.then; - return result; - }; - })(promise, promise.then); - - return promise; - } - - this.upload = function(config) { - config.headers = config.headers || {}; - config.headers['Content-Type'] = undefined; - config.transformRequest = config.transformRequest || $http.defaults.transformRequest; - var formData = new FormData(); - var origTransformRequest = config.transformRequest; - var origData = config.data; - config.transformRequest = function(formData, headerGetter) { - if (origData) { - if (config.formDataAppender) { - for (var key in origData) { - var val = origData[key]; - config.formDataAppender(formData, key, val); - } - } else { - for (var key in origData) { - var val = origData[key]; - if (typeof origTransformRequest == 'function') { - val = origTransformRequest(val, headerGetter); - } else { - for (var i = 0; i < origTransformRequest.length; i++) { - var transformFn = origTransformRequest[i]; - if (typeof transformFn == 'function') { - val = transformFn(val, headerGetter); - } - } - } - formData.append(key, val); - } - } - } - - if (config.file != null) { - var fileFormName = config.fileFormDataName || 'file'; - - if (Object.prototype.toString.call(config.file) === '[object Array]') { - var isFileFormNameString = Object.prototype.toString.call(fileFormName) === '[object String]'; - for (var i = 0; i < config.file.length; i++) { - formData.append(isFileFormNameString ? fileFormName + i : fileFormName[i], config.file[i], config.file[i].name); - } - } else { - formData.append(fileFormName, config.file, config.file.name); - } - } - return formData; - }; - - config.data = formData; - - return sendHttp(config); - }; - - this.http = function(config) { - return sendHttp(config); - } -}]); - -angularFileUpload.directive('ngFileSelect', [ '$parse', '$timeout', function($parse, $timeout) { - return function(scope, elem, attr) { - var fn = $parse(attr['ngFileSelect']); - elem.bind('change', function(evt) { - var files = [], fileList, i; - fileList = evt.target.files; - if (fileList != null) { - for (i = 0; i < fileList.length; i++) { - files.push(fileList.item(i)); - } - } - $timeout(function() { - fn(scope, { - $files : files, - $event : evt - }); - }); - }); - // removed this since it was confusing if the user click on browse and then cancel #181 -// elem.bind('click', function(){ -// this.value = null; -// }); - - // touch screens - if (('ontouchstart' in window) || - (navigator.maxTouchPoints > 0) || (navigator.msMaxTouchPoints > 0)) { - elem.bind('touchend', function(e) { - e.preventDefault(); - e.target.click(); - }); - } - }; -} ]); - -angularFileUpload.directive('ngFileDropAvailable', [ '$parse', '$timeout', function($parse, $timeout) { - return function(scope, elem, attr) { - if ('draggable' in document.createElement('span')) { - var fn = $parse(attr['ngFileDropAvailable']); - $timeout(function() { - fn(scope); - }); - } - }; -} ]); - -angularFileUpload.directive('ngFileDrop', [ '$parse', '$timeout', function($parse, $timeout) { - return function(scope, elem, attr) { - if ('draggable' in document.createElement('span')) { - var cancel = null; - var fn = $parse(attr['ngFileDrop']); - elem[0].addEventListener("dragover", function(evt) { - $timeout.cancel(cancel); - evt.stopPropagation(); - evt.preventDefault(); - elem.addClass(attr['ngFileDragOverClass'] || "dragover"); - }, false); - elem[0].addEventListener("dragleave", function(evt) { - cancel = $timeout(function() { - elem.removeClass(attr['ngFileDragOverClass'] || "dragover"); - }); - }, false); - - var processing = 0; - function traverseFileTree(files, item) { - if (item.isDirectory) { - var dirReader = item.createReader(); - processing++; - dirReader.readEntries(function(entries) { - for (var i = 0; i < entries.length; i++) { - traverseFileTree(files, entries[i]); - } - processing--; - }); - } else { - processing++; - item.file(function(file) { - processing--; - files.push(file); - }); - } - } - - elem[0].addEventListener("drop", function(evt) { - evt.stopPropagation(); - evt.preventDefault(); - elem.removeClass(attr['ngFileDragOverClass'] || "dragover"); - var files = [], items = evt.dataTransfer.items; - if (items && items.length > 0 && items[0].webkitGetAsEntry) { - for (var i = 0; i < items.length; i++) { - traverseFileTree(files, items[i].webkitGetAsEntry()); - } - } else { - var fileList = evt.dataTransfer.files; - if (fileList != null) { - for (var i = 0; i < fileList.length; i++) { - files.push(fileList.item(i)); - } - } - } - (function callback(delay) { - $timeout(function() { - if (!processing) { - fn(scope, { - $files : files, - $event : evt - }); - } else { - callback(10); - } - }, delay || 0) - })(); - }, false); - } - }; -} ]); - -})(); diff --git a/xstatic/pkg/angular_fileupload/data/ng-file-upload-all.js b/xstatic/pkg/angular_fileupload/data/ng-file-upload-all.js new file mode 100644 index 0000000..fca2e64 --- /dev/null +++ b/xstatic/pkg/angular_fileupload/data/ng-file-upload-all.js @@ -0,0 +1,2801 @@ +/**! + * AngularJS file upload directives and services. Supoorts: file upload/drop/paste, resume, cancel/abort, + * progress, resize, thumbnail, preview, validation and CORS + * FileAPI Flash shim for old browsers not supporting FormData + * @author Danial + * @version 12.0.4 + */ + +(function () { + /** @namespace FileAPI.noContentTimeout */ + + function patchXHR(fnName, newFn) { + window.XMLHttpRequest.prototype[fnName] = newFn(window.XMLHttpRequest.prototype[fnName]); + } + + function redefineProp(xhr, prop, fn) { + try { + Object.defineProperty(xhr, prop, {get: fn}); + } catch (e) {/*ignore*/ + } + } + + if (!window.FileAPI) { + window.FileAPI = {}; + } + + if (!window.XMLHttpRequest) { + throw 'AJAX is not supported. XMLHttpRequest is not defined.'; + } + + FileAPI.shouldLoad = !window.FormData || FileAPI.forceLoad; + if (FileAPI.shouldLoad) { + var initializeUploadListener = function (xhr) { + if (!xhr.__listeners) { + if (!xhr.upload) xhr.upload = {}; + xhr.__listeners = []; + var origAddEventListener = xhr.upload.addEventListener; + xhr.upload.addEventListener = function (t, fn) { + xhr.__listeners[t] = fn; + if (origAddEventListener) origAddEventListener.apply(this, arguments); + }; + } + }; + + patchXHR('open', function (orig) { + return function (m, url, b) { + initializeUploadListener(this); + this.__url = url; + try { + orig.apply(this, [m, url, b]); + } catch (e) { + if (e.message.indexOf('Access is denied') > -1) { + this.__origError = e; + orig.apply(this, [m, '_fix_for_ie_crossdomain__', b]); + } + } + }; + }); + + patchXHR('getResponseHeader', function (orig) { + return function (h) { + return this.__fileApiXHR && this.__fileApiXHR.getResponseHeader ? this.__fileApiXHR.getResponseHeader(h) : (orig == null ? null : orig.apply(this, [h])); + }; + }); + + patchXHR('getAllResponseHeaders', function (orig) { + return function () { + return this.__fileApiXHR && this.__fileApiXHR.getAllResponseHeaders ? this.__fileApiXHR.getAllResponseHeaders() : (orig == null ? null : orig.apply(this)); + }; + }); + + patchXHR('abort', function (orig) { + return function () { + return this.__fileApiXHR && this.__fileApiXHR.abort ? this.__fileApiXHR.abort() : (orig == null ? null : orig.apply(this)); + }; + }); + + patchXHR('setRequestHeader', function (orig) { + return function (header, value) { + if (header === '__setXHR_') { + initializeUploadListener(this); + var val = value(this); + // fix for angular < 1.2.0 + if (val instanceof Function) { + val(this); + } + } else { + this.__requestHeaders = this.__requestHeaders || {}; + this.__requestHeaders[header] = value; + orig.apply(this, arguments); + } + }; + }); + + patchXHR('send', function (orig) { + return function () { + var xhr = this; + if (arguments[0] && arguments[0].__isFileAPIShim) { + var formData = arguments[0]; + var config = { + url: xhr.__url, + jsonp: false, //removes the callback form param + cache: true, //removes the ?fileapiXXX in the url + complete: function (err, fileApiXHR) { + if (err && angular.isString(err) && err.indexOf('#2174') !== -1) { + // this error seems to be fine the file is being uploaded properly. + err = null; + } + xhr.__completed = true; + if (!err && xhr.__listeners.load) + xhr.__listeners.load({ + type: 'load', + loaded: xhr.__loaded, + total: xhr.__total, + target: xhr, + lengthComputable: true + }); + if (!err && xhr.__listeners.loadend) + xhr.__listeners.loadend({ + type: 'loadend', + loaded: xhr.__loaded, + total: xhr.__total, + target: xhr, + lengthComputable: true + }); + if (err === 'abort' && xhr.__listeners.abort) + xhr.__listeners.abort({ + type: 'abort', + loaded: xhr.__loaded, + total: xhr.__total, + target: xhr, + lengthComputable: true + }); + if (fileApiXHR.status !== undefined) redefineProp(xhr, 'status', function () { + return (fileApiXHR.status === 0 && err && err !== 'abort') ? 500 : fileApiXHR.status; + }); + if (fileApiXHR.statusText !== undefined) redefineProp(xhr, 'statusText', function () { + return fileApiXHR.statusText; + }); + redefineProp(xhr, 'readyState', function () { + return 4; + }); + if (fileApiXHR.response !== undefined) redefineProp(xhr, 'response', function () { + return fileApiXHR.response; + }); + var resp = fileApiXHR.responseText || (err && fileApiXHR.status === 0 && err !== 'abort' ? err : undefined); + redefineProp(xhr, 'responseText', function () { + return resp; + }); + redefineProp(xhr, 'response', function () { + return resp; + }); + if (err) redefineProp(xhr, 'err', function () { + return err; + }); + xhr.__fileApiXHR = fileApiXHR; + if (xhr.onreadystatechange) xhr.onreadystatechange(); + if (xhr.onload) xhr.onload(); + }, + progress: function (e) { + e.target = xhr; + if (xhr.__listeners.progress) xhr.__listeners.progress(e); + xhr.__total = e.total; + xhr.__loaded = e.loaded; + if (e.total === e.loaded) { + // fix flash issue that doesn't call complete if there is no response text from the server + var _this = this; + setTimeout(function () { + if (!xhr.__completed) { + xhr.getAllResponseHeaders = function () { + }; + _this.complete(null, {status: 204, statusText: 'No Content'}); + } + }, FileAPI.noContentTimeout || 10000); + } + }, + headers: xhr.__requestHeaders + }; + config.data = {}; + config.files = {}; + for (var i = 0; i < formData.data.length; i++) { + var item = formData.data[i]; + if (item.val != null && item.val.name != null && item.val.size != null && item.val.type != null) { + config.files[item.key] = item.val; + } else { + config.data[item.key] = item.val; + } + } + + setTimeout(function () { + if (!FileAPI.hasFlash) { + throw 'Adode Flash Player need to be installed. To check ahead use "FileAPI.hasFlash"'; + } + xhr.__fileApiXHR = FileAPI.upload(config); + }, 1); + } else { + if (this.__origError) { + throw this.__origError; + } + orig.apply(xhr, arguments); + } + }; + }); + window.XMLHttpRequest.__isFileAPIShim = true; + window.FormData = FormData = function () { + return { + append: function (key, val, name) { + if (val.__isFileAPIBlobShim) { + val = val.data[0]; + } + this.data.push({ + key: key, + val: val, + name: name + }); + }, + data: [], + __isFileAPIShim: true + }; + }; + + window.Blob = Blob = function (b) { + return { + data: b, + __isFileAPIBlobShim: true + }; + }; + } + +})(); + +(function () { + /** @namespace FileAPI.forceLoad */ + /** @namespace window.FileAPI.jsUrl */ + /** @namespace window.FileAPI.jsPath */ + + function isInputTypeFile(elem) { + return elem[0].tagName.toLowerCase() === 'input' && elem.attr('type') && elem.attr('type').toLowerCase() === 'file'; + } + + function hasFlash() { + try { + var fo = new ActiveXObject('ShockwaveFlash.ShockwaveFlash'); + if (fo) return true; + } catch (e) { + if (navigator.mimeTypes['application/x-shockwave-flash'] !== undefined) return true; + } + return false; + } + + function getOffset(obj) { + var left = 0, top = 0; + + if (window.jQuery) { + return jQuery(obj).offset(); + } + + if (obj.offsetParent) { + do { + left += (obj.offsetLeft - obj.scrollLeft); + top += (obj.offsetTop - obj.scrollTop); + obj = obj.offsetParent; + } while (obj); + } + return { + left: left, + top: top + }; + } + + if (FileAPI.shouldLoad) { + FileAPI.hasFlash = hasFlash(); + + //load FileAPI + if (FileAPI.forceLoad) { + FileAPI.html5 = false; + } + + if (!FileAPI.upload) { + var jsUrl, basePath, script = document.createElement('script'), allScripts = document.getElementsByTagName('script'), i, index, src; + if (window.FileAPI.jsUrl) { + jsUrl = window.FileAPI.jsUrl; + } else if (window.FileAPI.jsPath) { + basePath = window.FileAPI.jsPath; + } else { + for (i = 0; i < allScripts.length; i++) { + src = allScripts[i].src; + index = src.search(/\/ng\-file\-upload[\-a-zA-z0-9\.]*\.js/); + if (index > -1) { + basePath = src.substring(0, index + 1); + break; + } + } + } + + if (FileAPI.staticPath == null) FileAPI.staticPath = basePath; + script.setAttribute('src', jsUrl || basePath + 'FileAPI.min.js'); + document.getElementsByTagName('head')[0].appendChild(script); + } + + FileAPI.ngfFixIE = function (elem, fileElem, changeFn) { + if (!hasFlash()) { + throw 'Adode Flash Player need to be installed. To check ahead use "FileAPI.hasFlash"'; + } + var fixInputStyle = function () { + var label = fileElem.parent(); + if (elem.attr('disabled')) { + if (label) label.removeClass('js-fileapi-wrapper'); + } else { + if (!fileElem.attr('__ngf_flash_')) { + fileElem.unbind('change'); + fileElem.unbind('click'); + fileElem.bind('change', function (evt) { + fileApiChangeFn.apply(this, [evt]); + changeFn.apply(this, [evt]); + }); + fileElem.attr('__ngf_flash_', 'true'); + } + label.addClass('js-fileapi-wrapper'); + if (!isInputTypeFile(elem)) { + label.css('position', 'absolute') + .css('top', getOffset(elem[0]).top + 'px').css('left', getOffset(elem[0]).left + 'px') + .css('width', elem[0].offsetWidth + 'px').css('height', elem[0].offsetHeight + 'px') + .css('filter', 'alpha(opacity=0)').css('display', elem.css('display')) + .css('overflow', 'hidden').css('z-index', '900000') + .css('visibility', 'visible'); + fileElem.css('width', elem[0].offsetWidth + 'px').css('height', elem[0].offsetHeight + 'px') + .css('position', 'absolute').css('top', '0px').css('left', '0px'); + } + } + }; + + elem.bind('mouseenter', fixInputStyle); + + var fileApiChangeFn = function (evt) { + var files = FileAPI.getFiles(evt); + //just a double check for #233 + for (var i = 0; i < files.length; i++) { + if (files[i].size === undefined) files[i].size = 0; + if (files[i].name === undefined) files[i].name = 'file'; + if (files[i].type === undefined) files[i].type = 'undefined'; + } + if (!evt.target) { + evt.target = {}; + } + evt.target.files = files; + // if evt.target.files is not writable use helper field + if (evt.target.files !== files) { + evt.__files_ = files; + } + (evt.__files_ || evt.target.files).item = function (i) { + return (evt.__files_ || evt.target.files)[i] || null; + }; + }; + }; + + FileAPI.disableFileInput = function (elem, disable) { + if (disable) { + elem.removeClass('js-fileapi-wrapper'); + } else { + elem.addClass('js-fileapi-wrapper'); + } + }; + } +})(); + +if (!window.FileReader) { + window.FileReader = function () { + var _this = this, loadStarted = false; + this.listeners = {}; + this.addEventListener = function (type, fn) { + _this.listeners[type] = _this.listeners[type] || []; + _this.listeners[type].push(fn); + }; + this.removeEventListener = function (type, fn) { + if (_this.listeners[type]) _this.listeners[type].splice(_this.listeners[type].indexOf(fn), 1); + }; + this.dispatchEvent = function (evt) { + var list = _this.listeners[evt.type]; + if (list) { + for (var i = 0; i < list.length; i++) { + list[i].call(_this, evt); + } + } + }; + this.onabort = this.onerror = this.onload = this.onloadstart = this.onloadend = this.onprogress = null; + + var constructEvent = function (type, evt) { + var e = {type: type, target: _this, loaded: evt.loaded, total: evt.total, error: evt.error}; + if (evt.result != null) e.target.result = evt.result; + return e; + }; + var listener = function (evt) { + if (!loadStarted) { + loadStarted = true; + if (_this.onloadstart) _this.onloadstart(constructEvent('loadstart', evt)); + } + var e; + if (evt.type === 'load') { + if (_this.onloadend) _this.onloadend(constructEvent('loadend', evt)); + e = constructEvent('load', evt); + if (_this.onload) _this.onload(e); + _this.dispatchEvent(e); + } else if (evt.type === 'progress') { + e = constructEvent('progress', evt); + if (_this.onprogress) _this.onprogress(e); + _this.dispatchEvent(e); + } else { + e = constructEvent('error', evt); + if (_this.onerror) _this.onerror(e); + _this.dispatchEvent(e); + } + }; + this.readAsDataURL = function (file) { + FileAPI.readAsDataURL(file, listener); + }; + this.readAsText = function (file) { + FileAPI.readAsText(file, listener); + }; + }; +} + +/**! + * AngularJS file upload directives and services. Supoorts: file upload/drop/paste, resume, cancel/abort, + * progress, resize, thumbnail, preview, validation and CORS + * @author Danial + * @version 12.0.4 + */ + +if (window.XMLHttpRequest && !(window.FileAPI && FileAPI.shouldLoad)) { + window.XMLHttpRequest.prototype.setRequestHeader = (function (orig) { + return function (header, value) { + if (header === '__setXHR_') { + var val = value(this); + // fix for angular < 1.2.0 + if (val instanceof Function) { + val(this); + } + } else { + orig.apply(this, arguments); + } + }; + })(window.XMLHttpRequest.prototype.setRequestHeader); +} + +var ngFileUpload = angular.module('ngFileUpload', []); + +ngFileUpload.version = '12.0.4'; + +ngFileUpload.service('UploadBase', ['$http', '$q', '$timeout', function ($http, $q, $timeout) { + var upload = this; + upload.promisesCount = 0; + + this.isResumeSupported = function () { + return window.Blob && window.Blob.prototype.slice; + }; + + var resumeSupported = this.isResumeSupported(); + + function sendHttp(config) { + config.method = config.method || 'POST'; + config.headers = config.headers || {}; + + var deferred = config._deferred = config._deferred || $q.defer(); + var promise = deferred.promise; + + function notifyProgress(e) { + if (deferred.notify) { + deferred.notify(e); + } + if (promise.progressFunc) { + $timeout(function () { + promise.progressFunc(e); + }); + } + } + + function getNotifyEvent(n) { + if (config._start != null && resumeSupported) { + return { + loaded: n.loaded + config._start, + total: (config._file && config._file.size) || n.total, + type: n.type, config: config, + lengthComputable: true, target: n.target + }; + } else { + return n; + } + } + + if (!config.disableProgress) { + config.headers.__setXHR_ = function () { + return function (xhr) { + if (!xhr || !xhr.upload || !xhr.upload.addEventListener) return; + config.__XHR = xhr; + if (config.xhrFn) config.xhrFn(xhr); + xhr.upload.addEventListener('progress', function (e) { + e.config = config; + notifyProgress(getNotifyEvent(e)); + }, false); + //fix for firefox not firing upload progress end, also IE8-9 + xhr.upload.addEventListener('load', function (e) { + if (e.lengthComputable) { + e.config = config; + notifyProgress(getNotifyEvent(e)); + } + }, false); + }; + }; + } + + function uploadWithAngular() { + $http(config).then(function (r) { + if (resumeSupported && config._chunkSize && !config._finished && config._file) { + notifyProgress({ + loaded: config._end, + total: config._file && config._file.size, + config: config, type: 'progress' + } + ); + upload.upload(config, true); + } else { + if (config._finished) delete config._finished; + deferred.resolve(r); + } + }, function (e) { + deferred.reject(e); + }, function (n) { + deferred.notify(n); + } + ); + } + + if (!resumeSupported) { + uploadWithAngular(); + } else if (config._chunkSize && config._end && !config._finished) { + config._start = config._end; + config._end += config._chunkSize; + uploadWithAngular(); + } else if (config.resumeSizeUrl) { + $http.get(config.resumeSizeUrl).then(function (resp) { + if (config.resumeSizeResponseReader) { + config._start = config.resumeSizeResponseReader(resp.data); + } else { + config._start = parseInt((resp.data.size == null ? resp.data : resp.data.size).toString()); + } + if (config._chunkSize) { + config._end = config._start + config._chunkSize; + } + uploadWithAngular(); + }, function (e) { + throw e; + }); + } else if (config.resumeSize) { + config.resumeSize().then(function (size) { + config._start = size; + uploadWithAngular(); + }, function (e) { + throw e; + }); + } else { + if (config._chunkSize) { + config._start = 0; + config._end = config._start + config._chunkSize; + } + uploadWithAngular(); + } + + + promise.success = function (fn) { + promise.then(function (response) { + fn(response.data, response.status, response.headers, config); + }); + return promise; + }; + + promise.error = function (fn) { + promise.then(null, function (response) { + fn(response.data, response.status, response.headers, config); + }); + return promise; + }; + + promise.progress = function (fn) { + promise.progressFunc = fn; + promise.then(null, null, function (n) { + fn(n); + }); + return promise; + }; + promise.abort = promise.pause = function () { + if (config.__XHR) { + $timeout(function () { + config.__XHR.abort(); + }); + } + return promise; + }; + promise.xhr = function (fn) { + config.xhrFn = (function (origXhrFn) { + return function () { + if (origXhrFn) origXhrFn.apply(promise, arguments); + fn.apply(promise, arguments); + }; + })(config.xhrFn); + return promise; + }; + + upload.promisesCount++; + promise['finally'](function () { + upload.promisesCount--; + }); + return promise; + } + + this.isUploadInProgress = function () { + return upload.promisesCount > 0; + }; + + this.rename = function (file, name) { + file.ngfName = name; + return file; + }; + + this.jsonBlob = function (val) { + if (val != null && !angular.isString(val)) { + val = JSON.stringify(val); + } + var blob = new window.Blob([val], {type: 'application/json'}); + blob._ngfBlob = true; + return blob; + }; + + this.json = function (val) { + return angular.toJson(val); + }; + + function copy(obj) { + var clone = {}; + for (var key in obj) { + if (obj.hasOwnProperty(key)) { + clone[key] = obj[key]; + } + } + return clone; + } + + this.isFile = function (file) { + return file != null && (file instanceof window.Blob || (file.flashId && file.name && file.size)); + }; + + this.upload = function (config, internal) { + function toResumeFile(file, formData) { + if (file._ngfBlob) return file; + config._file = config._file || file; + if (config._start != null && resumeSupported) { + if (config._end && config._end >= file.size) { + config._finished = true; + config._end = file.size; + } + var slice = file.slice(config._start, config._end || file.size); + slice.name = file.name; + slice.ngfName = file.ngfName; + if (config._chunkSize) { + formData.append('_chunkSize', config._chunkSize); + formData.append('_currentChunkSize', config._end - config._start); + formData.append('_chunkNumber', Math.floor(config._start / config._chunkSize)); + formData.append('_totalSize', config._file.size); + } + return slice; + } + return file; + } + + function addFieldToFormData(formData, val, key) { + if (val !== undefined) { + if (angular.isDate(val)) { + val = val.toISOString(); + } + if (angular.isString(val)) { + formData.append(key, val); + } else if (upload.isFile(val)) { + var file = toResumeFile(val, formData); + var split = key.split(','); + if (split[1]) { + file.ngfName = split[1].replace(/^\s+|\s+$/g, ''); + key = split[0]; + } + config._fileKey = config._fileKey || key; + formData.append(key, file, file.ngfName || file.name); + } else { + if (angular.isObject(val)) { + if (val.$$ngfCircularDetection) throw 'ngFileUpload: Circular reference in config.data. Make sure specified data for Upload.upload() has no circular reference: ' + key; + + val.$$ngfCircularDetection = true; + try { + for (var k in val) { + if (val.hasOwnProperty(k) && k !== '$$ngfCircularDetection') { + var objectKey = config.objectKey == null ? '[i]' : config.objectKey; + if (val.length && parseInt(k) > -1) { + objectKey = config.arrayKey == null ? objectKey : config.arrayKey; + } + addFieldToFormData(formData, val[k], key + objectKey.replace(/[ik]/g, k)); + } + } + } finally { + delete val.$$ngfCircularDetection; + } + } else { + formData.append(key, val); + } + } + } + } + + function digestConfig() { + config._chunkSize = upload.translateScalars(config.resumeChunkSize); + config._chunkSize = config._chunkSize ? parseInt(config._chunkSize.toString()) : null; + + config.headers = config.headers || {}; + config.headers['Content-Type'] = undefined; + config.transformRequest = config.transformRequest ? + (angular.isArray(config.transformRequest) ? + config.transformRequest : [config.transformRequest]) : []; + config.transformRequest.push(function (data) { + var formData = new window.FormData(), key; + data = data || config.fields || {}; + if (config.file) { + data.file = config.file; + } + for (key in data) { + if (data.hasOwnProperty(key)) { + var val = data[key]; + if (config.formDataAppender) { + config.formDataAppender(formData, key, val); + } else { + addFieldToFormData(formData, val, key); + } + } + } + + return formData; + }); + } + + if (!internal) config = copy(config); + if (!config._isDigested) { + config._isDigested = true; + digestConfig(); + } + + return sendHttp(config); + }; + + this.http = function (config) { + config = copy(config); + config.transformRequest = config.transformRequest || function (data) { + if ((window.ArrayBuffer && data instanceof window.ArrayBuffer) || data instanceof window.Blob) { + return data; + } + return $http.defaults.transformRequest[0].apply(this, arguments); + }; + config._chunkSize = upload.translateScalars(config.resumeChunkSize); + config._chunkSize = config._chunkSize ? parseInt(config._chunkSize.toString()) : null; + + return sendHttp(config); + }; + + this.translateScalars = function (str) { + if (angular.isString(str)) { + if (str.search(/kb/i) === str.length - 2) { + return parseFloat(str.substring(0, str.length - 2) * 1024); + } else if (str.search(/mb/i) === str.length - 2) { + return parseFloat(str.substring(0, str.length - 2) * 1048576); + } else if (str.search(/gb/i) === str.length - 2) { + return parseFloat(str.substring(0, str.length - 2) * 1073741824); + } else if (str.search(/b/i) === str.length - 1) { + return parseFloat(str.substring(0, str.length - 1)); + } else if (str.search(/s/i) === str.length - 1) { + return parseFloat(str.substring(0, str.length - 1)); + } else if (str.search(/m/i) === str.length - 1) { + return parseFloat(str.substring(0, str.length - 1) * 60); + } else if (str.search(/h/i) === str.length - 1) { + return parseFloat(str.substring(0, str.length - 1) * 3600); + } + } + return str; + }; + + this.urlToBlob = function(url) { + var defer = $q.defer(); + $http({url: url, method: 'get', responseType: 'arraybuffer'}).then(function (resp) { + var arrayBufferView = new Uint8Array(resp.data); + var type = resp.headers('content-type') || 'image/WebP'; + var blob = new window.Blob([arrayBufferView], {type: type}); + defer.resolve(blob); + //var split = type.split('[/;]'); + //blob.name = url.substring(0, 150).replace(/\W+/g, '') + '.' + (split.length > 1 ? split[1] : 'jpg'); + }, function (e) { + defer.reject(e); + }); + return defer.promise; + }; + + this.setDefaults = function (defaults) { + this.defaults = defaults || {}; + }; + + this.defaults = {}; + this.version = ngFileUpload.version; +} + +]); + +ngFileUpload.service('Upload', ['$parse', '$timeout', '$compile', '$q', 'UploadExif', function ($parse, $timeout, $compile, $q, UploadExif) { + var upload = UploadExif; + upload.getAttrWithDefaults = function (attr, name) { + if (attr[name] != null) return attr[name]; + var def = upload.defaults[name]; + return (def == null ? def : (angular.isString(def) ? def : JSON.stringify(def))); + }; + + upload.attrGetter = function (name, attr, scope, params) { + var attrVal = this.getAttrWithDefaults(attr, name); + if (scope) { + try { + if (params) { + return $parse(attrVal)(scope, params); + } else { + return $parse(attrVal)(scope); + } + } catch (e) { + // hangle string value without single qoute + if (name.search(/min|max|pattern/i)) { + return attrVal; + } else { + throw e; + } + } + } else { + return attrVal; + } + }; + + upload.shouldUpdateOn = function (type, attr, scope) { + var modelOptions = upload.attrGetter('ngModelOptions', attr, scope); + if (modelOptions && modelOptions.updateOn) { + return modelOptions.updateOn.split(' ').indexOf(type) > -1; + } + return true; + }; + + upload.emptyPromise = function () { + var d = $q.defer(); + var args = arguments; + $timeout(function () { + d.resolve.apply(d, args); + }); + return d.promise; + }; + + upload.rejectPromise = function () { + var d = $q.defer(); + var args = arguments; + $timeout(function () { + d.reject.apply(d, args); + }); + return d.promise; + }; + + upload.happyPromise = function (promise, data) { + var d = $q.defer(); + promise.then(function (result) { + d.resolve(result); + }, function (error) { + $timeout(function () { + throw error; + }); + d.resolve(data); + }); + return d.promise; + }; + + function applyExifRotations(files, attr, scope) { + var promises = [upload.emptyPromise()]; + angular.forEach(files, function (f, i) { + if (f.type.indexOf('image/jpeg') === 0 && upload.attrGetter('ngfFixOrientation', attr, scope, {$file: f})) { + promises.push(upload.happyPromise(upload.applyExifRotation(f), f).then(function (fixedFile) { + files.splice(i, 1, fixedFile); + })); + } + }); + return $q.all(promises); + } + + function resize(files, attr, scope) { + var resizeVal = upload.attrGetter('ngfResize', attr, scope); + if (!resizeVal || !upload.isResizeSupported() || !files.length) return upload.emptyPromise(); + if (resizeVal instanceof Function) { + var defer = $q.defer(); + resizeVal(files).then(function (p) { + resizeWithParams(p, files, attr, scope).then(function (r) { + defer.resolve(r); + }, function (e) { + defer.reject(e); + }); + }, function (e) { + defer.reject(e); + }); + } else { + return resizeWithParams(resizeVal, files, attr, scope); + } + } + + function resizeWithParams(param, files, attr, scope) { + var promises = [upload.emptyPromise()]; + + function handleFile(f, i) { + if (f.type.indexOf('image') === 0) { + if (param.pattern && !upload.validatePattern(f, param.pattern)) return; + var promise = upload.resize(f, param.width, param.height, param.quality, + param.type, param.ratio, param.centerCrop, function (width, height) { + return upload.attrGetter('ngfResizeIf', attr, scope, + {$width: width, $height: height, $file: f}); + }, param.restoreExif !== false); + promises.push(promise); + promise.then(function (resizedFile) { + files.splice(i, 1, resizedFile); + }, function (e) { + f.$error = 'resize'; + f.$errorParam = (e ? (e.message ? e.message : e) + ': ' : '') + (f && f.name); + }); + } + } + + for (var i = 0; i < files.length; i++) { + handleFile(files[i], i); + } + return $q.all(promises); + } + + upload.updateModel = function (ngModel, attr, scope, fileChange, files, evt, noDelay) { + function update(files, invalidFiles, newFiles, dupFiles, isSingleModel) { + attr.$$ngfPrevValidFiles = files; + attr.$$ngfPrevInvalidFiles = invalidFiles; + var file = files && files.length ? files[0] : null; + var invalidFile = invalidFiles && invalidFiles.length ? invalidFiles[0] : null; + + if (ngModel) { + upload.applyModelValidation(ngModel, files); + ngModel.$setViewValue(isSingleModel ? file : files); + } + + if (fileChange) { + $parse(fileChange)(scope, { + $files: files, + $file: file, + $newFiles: newFiles, + $duplicateFiles: dupFiles, + $invalidFiles: invalidFiles, + $invalidFile: invalidFile, + $event: evt + }); + } + + var invalidModel = upload.attrGetter('ngfModelInvalid', attr); + if (invalidModel) { + $timeout(function () { + $parse(invalidModel).assign(scope, isSingleModel ? invalidFile : invalidFiles); + }); + } + $timeout(function () { + // scope apply changes + }); + } + + var allNewFiles, dupFiles = [], prevValidFiles, prevInvalidFiles, + invalids = [], valids = []; + + function removeDuplicates() { + function equals(f1, f2) { + return f1.name === f2.name && (f1.$ngfOrigSize || f1.size) === (f2.$ngfOrigSize || f2.size) && + f1.type === f2.type; + } + + function isInPrevFiles(f) { + var j; + for (j = 0; j < prevValidFiles.length; j++) { + if (equals(f, prevValidFiles[j])) { + return true; + } + } + for (j = 0; j < prevInvalidFiles.length; j++) { + if (equals(f, prevInvalidFiles[j])) { + return true; + } + } + return false; + } + + if (files) { + allNewFiles = []; + dupFiles = []; + for (var i = 0; i < files.length; i++) { + if (isInPrevFiles(files[i])) { + dupFiles.push(files[i]); + } else { + allNewFiles.push(files[i]); + } + } + } + } + + function toArray(v) { + return angular.isArray(v) ? v : [v]; + } + + function separateInvalids() { + valids = []; + invalids = []; + angular.forEach(allNewFiles, function (file) { + if (file.$error) { + invalids.push(file); + } else { + valids.push(file); + } + }); + } + + function resizeAndUpdate() { + function updateModel() { + $timeout(function () { + update(keep ? prevValidFiles.concat(valids) : valids, + keep ? prevInvalidFiles.concat(invalids) : invalids, + files, dupFiles, isSingleModel); + }, options && options.debounce ? options.debounce.change || options.debounce : 0); + } + + resize(validateAfterResize ? allNewFiles : valids, attr, scope).then(function () { + if (validateAfterResize) { + upload.validate(allNewFiles, prevValidFiles.length, ngModel, attr, scope).then(function () { + separateInvalids(); + updateModel(); + }); + } else { + updateModel(); + } + }, function (e) { + throw 'Could not resize files ' + e; + }); + } + + prevValidFiles = attr.$$ngfPrevValidFiles || []; + prevInvalidFiles = attr.$$ngfPrevInvalidFiles || []; + if (ngModel && ngModel.$modelValue) { + prevValidFiles = toArray(ngModel.$modelValue); + } + + var keep = upload.attrGetter('ngfKeep', attr, scope); + allNewFiles = (files || []).slice(0); + if (keep === 'distinct' || upload.attrGetter('ngfKeepDistinct', attr, scope) === true) { + removeDuplicates(attr, scope); + } + + var isSingleModel = !keep && !upload.attrGetter('ngfMultiple', attr, scope) && !upload.attrGetter('multiple', attr); + + if (keep && !allNewFiles.length) return; + + upload.attrGetter('ngfBeforeModelChange', attr, scope, { + $files: files, + $file: files && files.length ? files[0] : null, + $newFiles: allNewFiles, + $duplicateFiles: dupFiles, + $event: evt + }); + + var validateAfterResize = upload.attrGetter('ngfValidateAfterResize', attr, scope); + + var options = upload.attrGetter('ngModelOptions', attr, scope); + upload.validate(allNewFiles, prevValidFiles.length, ngModel, attr, scope).then(function () { + if (noDelay) { + update(allNewFiles, [], files, dupFiles, isSingleModel); + } else { + if ((!options || !options.allowInvalid) && !validateAfterResize) { + separateInvalids(); + } else { + valids = allNewFiles; + } + if (upload.attrGetter('ngfFixOrientation', attr, scope) && upload.isExifSupported()) { + applyExifRotations(valids, attr, scope).then(function () { + resizeAndUpdate(); + }); + } else { + resizeAndUpdate(); + } + } + }); + }; + + return upload; +}]); + +ngFileUpload.directive('ngfSelect', ['$parse', '$timeout', '$compile', 'Upload', function ($parse, $timeout, $compile, Upload) { + var generatedElems = []; + + function isDelayedClickSupported(ua) { + // fix for android native browser < 4.4 and safari windows + var m = ua.match(/Android[^\d]*(\d+)\.(\d+)/); + if (m && m.length > 2) { + var v = Upload.defaults.androidFixMinorVersion || 4; + return parseInt(m[1]) < 4 || (parseInt(m[1]) === v && parseInt(m[2]) < v); + } + + // safari on windows + return ua.indexOf('Chrome') === -1 && /.*Windows.*Safari.*/.test(ua); + } + + function linkFileSelect(scope, elem, attr, ngModel, $parse, $timeout, $compile, upload) { + /** @namespace attr.ngfSelect */ + /** @namespace attr.ngfChange */ + /** @namespace attr.ngModel */ + /** @namespace attr.ngModelOptions */ + /** @namespace attr.ngfMultiple */ + /** @namespace attr.ngfCapture */ + /** @namespace attr.ngfValidate */ + /** @namespace attr.ngfKeep */ + var attrGetter = function (name, scope) { + return upload.attrGetter(name, attr, scope); + }; + + function isInputTypeFile() { + return elem[0].tagName.toLowerCase() === 'input' && attr.type && attr.type.toLowerCase() === 'file'; + } + + function fileChangeAttr() { + return attrGetter('ngfChange') || attrGetter('ngfSelect'); + } + + function changeFn(evt) { + if (upload.shouldUpdateOn('change', attr, scope)) { + var fileList = evt.__files_ || (evt.target && evt.target.files), files = []; + for (var i = 0; i < fileList.length; i++) { + files.push(fileList[i]); + } + upload.updateModel(ngModel, attr, scope, fileChangeAttr(), + files.length ? files : null, evt); + } + } + + upload.registerModelChangeValidator(ngModel, attr, scope); + + var unwatches = []; + unwatches.push(scope.$watch(attrGetter('ngfMultiple'), function () { + fileElem.attr('multiple', attrGetter('ngfMultiple', scope)); + })); + unwatches.push(scope.$watch(attrGetter('ngfCapture'), function () { + fileElem.attr('capture', attrGetter('ngfCapture', scope)); + })); + unwatches.push(scope.$watch(attrGetter('ngfAccept'), function () { + fileElem.attr('accept', attrGetter('ngfAccept', scope)); + })); + attr.$observe('accept', function () { + fileElem.attr('accept', attrGetter('accept')); + }); + unwatches.push(function () { + if (attr.$$observers) delete attr.$$observers.accept; + }); + function bindAttrToFileInput(fileElem) { + if (elem !== fileElem) { + for (var i = 0; i < elem[0].attributes.length; i++) { + var attribute = elem[0].attributes[i]; + if (attribute.name !== 'type' && attribute.name !== 'class' && attribute.name !== 'style') { + if (attribute.value == null || attribute.value === '') { + if (attribute.name === 'required') attribute.value = 'required'; + if (attribute.name === 'multiple') attribute.value = 'multiple'; + } + fileElem.attr(attribute.name, attribute.name === 'id' ? 'ngf-' + attribute.value : attribute.value); + } + } + } + } + + function createFileInput() { + if (isInputTypeFile()) { + return elem; + } + + var fileElem = angular.element(''); + + bindAttrToFileInput(fileElem); + + var label = angular.element(''); + label.css('visibility', 'hidden').css('position', 'absolute').css('overflow', 'hidden') + .css('width', '0px').css('height', '0px').css('border', 'none') + .css('margin', '0px').css('padding', '0px').attr('tabindex', '-1'); + generatedElems.push({el: elem, ref: label}); + + document.body.appendChild(label.append(fileElem)[0]); + + return fileElem; + } + + var initialTouchStartY = 0; + + function clickHandler(evt) { + if (elem.attr('disabled')) return false; + if (attrGetter('ngfSelectDisabled', scope)) return; + + var r = handleTouch(evt); + if (r != null) return r; + + resetModel(evt); + + // fix for md when the element is removed from the DOM and added back #460 + try { + if (!isInputTypeFile() && !document.body.contains(fileElem[0])) { + generatedElems.push({el: elem, ref: fileElem.parent()}); + document.body.appendChild(fileElem.parent()[0]); + fileElem.bind('change', changeFn); + } + } catch(e){/*ignore*/} + + if (isDelayedClickSupported(navigator.userAgent)) { + setTimeout(function () { + fileElem[0].click(); + }, 0); + } else { + fileElem[0].click(); + } + + return false; + } + + function handleTouch(evt) { + var touches = evt.changedTouches || (evt.originalEvent && evt.originalEvent.changedTouches); + if (evt.type === 'touchstart') { + initialTouchStartY = touches ? touches[0].clientY : 0; + return true; // don't block event default + } else { + evt.stopPropagation(); + evt.preventDefault(); + + // prevent scroll from triggering event + if (evt.type === 'touchend') { + var currentLocation = touches ? touches[0].clientY : 0; + if (Math.abs(currentLocation - initialTouchStartY) > 20) return false; + } + } + } + + var fileElem = elem; + + function resetModel(evt) { + if (upload.shouldUpdateOn('click', attr, scope) && fileElem.val()) { + fileElem.val(null); + upload.updateModel(ngModel, attr, scope, fileChangeAttr(), null, evt, true); + } + } + + if (!isInputTypeFile()) { + fileElem = createFileInput(); + } + fileElem.bind('change', changeFn); + + if (!isInputTypeFile()) { + elem.bind('click touchstart touchend', clickHandler); + } else { + elem.bind('click', resetModel); + } + + function ie10SameFileSelectFix(evt) { + if (fileElem && !fileElem.attr('__ngf_ie10_Fix_')) { + if (!fileElem[0].parentNode) { + fileElem = null; + return; + } + evt.preventDefault(); + evt.stopPropagation(); + fileElem.unbind('click'); + var clone = fileElem.clone(); + fileElem.replaceWith(clone); + fileElem = clone; + fileElem.attr('__ngf_ie10_Fix_', 'true'); + fileElem.bind('change', changeFn); + fileElem.bind('click', ie10SameFileSelectFix); + fileElem[0].click(); + return false; + } else { + fileElem.removeAttr('__ngf_ie10_Fix_'); + } + } + + if (navigator.appVersion.indexOf('MSIE 10') !== -1) { + fileElem.bind('click', ie10SameFileSelectFix); + } + + if (ngModel) ngModel.$formatters.push(function (val) { + if (val == null || val.length === 0) { + if (fileElem.val()) { + fileElem.val(null); + } + } + return val; + }); + + scope.$on('$destroy', function () { + if (!isInputTypeFile()) fileElem.parent().remove(); + angular.forEach(unwatches, function (unwatch) { + unwatch(); + }); + }); + + $timeout(function () { + for (var i = 0; i < generatedElems.length; i++) { + var g = generatedElems[i]; + if (!document.body.contains(g.el[0])) { + generatedElems.splice(i, 1); + g.ref.remove(); + } + } + }); + + if (window.FileAPI && window.FileAPI.ngfFixIE) { + window.FileAPI.ngfFixIE(elem, fileElem, changeFn); + } + } + + return { + restrict: 'AEC', + require: '?ngModel', + link: function (scope, elem, attr, ngModel) { + linkFileSelect(scope, elem, attr, ngModel, $parse, $timeout, $compile, Upload); + } + }; +}]); + +(function () { + + ngFileUpload.service('UploadDataUrl', ['UploadBase', '$timeout', '$q', function (UploadBase, $timeout, $q) { + var upload = UploadBase; + upload.base64DataUrl = function (file) { + if (angular.isArray(file)) { + var d = $q.defer(), count = 0; + angular.forEach(file, function (f) { + upload.dataUrl(f, true)['finally'](function () { + count++; + if (count === file.length) { + var urls = []; + angular.forEach(file, function (ff) { + urls.push(ff.$ngfDataUrl); + }); + d.resolve(urls, file); + } + }); + }); + return d.promise; + } else { + return upload.dataUrl(file, true); + } + }; + upload.dataUrl = function (file, disallowObjectUrl) { + if (!file) return upload.emptyPromise(file, file); + if ((disallowObjectUrl && file.$ngfDataUrl != null) || (!disallowObjectUrl && file.$ngfBlobUrl != null)) { + return upload.emptyPromise(disallowObjectUrl ? file.$ngfDataUrl : file.$ngfBlobUrl, file); + } + var p = disallowObjectUrl ? file.$$ngfDataUrlPromise : file.$$ngfBlobUrlPromise; + if (p) return p; + + var deferred = $q.defer(); + $timeout(function () { + if (window.FileReader && file && + (!window.FileAPI || navigator.userAgent.indexOf('MSIE 8') === -1 || file.size < 20000) && + (!window.FileAPI || navigator.userAgent.indexOf('MSIE 9') === -1 || file.size < 4000000)) { + //prefer URL.createObjectURL for handling refrences to files of all sizes + //since it doesn´t build a large string in memory + var URL = window.URL || window.webkitURL; + if (URL && URL.createObjectURL && !disallowObjectUrl) { + var url; + try { + url = URL.createObjectURL(file); + } catch (e) { + $timeout(function () { + file.$ngfBlobUrl = ''; + deferred.reject(); + }); + return; + } + $timeout(function () { + file.$ngfBlobUrl = url; + if (url) { + deferred.resolve(url, file); + upload.blobUrls = upload.blobUrls || []; + upload.blobUrlsTotalSize = upload.blobUrlsTotalSize || 0; + upload.blobUrls.push({url: url, size: file.size}); + upload.blobUrlsTotalSize += file.size || 0; + var maxMemory = upload.defaults.blobUrlsMaxMemory || 268435456; + var maxLength = upload.defaults.blobUrlsMaxQueueSize || 200; + while ((upload.blobUrlsTotalSize > maxMemory || upload.blobUrls.length > maxLength) && upload.blobUrls.length > 1) { + var obj = upload.blobUrls.splice(0, 1)[0]; + URL.revokeObjectURL(obj.url); + upload.blobUrlsTotalSize -= obj.size; + } + } + }); + } else { + var fileReader = new FileReader(); + fileReader.onload = function (e) { + $timeout(function () { + file.$ngfDataUrl = e.target.result; + deferred.resolve(e.target.result, file); + $timeout(function () { + delete file.$ngfDataUrl; + }, 1000); + }); + }; + fileReader.onerror = function () { + $timeout(function () { + file.$ngfDataUrl = ''; + deferred.reject(); + }); + }; + fileReader.readAsDataURL(file); + } + } else { + $timeout(function () { + file[disallowObjectUrl ? '$ngfDataUrl' : '$ngfBlobUrl'] = ''; + deferred.reject(); + }); + } + }); + + if (disallowObjectUrl) { + p = file.$$ngfDataUrlPromise = deferred.promise; + } else { + p = file.$$ngfBlobUrlPromise = deferred.promise; + } + p['finally'](function () { + delete file[disallowObjectUrl ? '$$ngfDataUrlPromise' : '$$ngfBlobUrlPromise']; + }); + return p; + }; + return upload; + }]); + + function getTagType(el) { + if (el.tagName.toLowerCase() === 'img') return 'image'; + if (el.tagName.toLowerCase() === 'audio') return 'audio'; + if (el.tagName.toLowerCase() === 'video') return 'video'; + return /./; + } + + function linkFileDirective(Upload, $timeout, scope, elem, attr, directiveName, resizeParams, isBackground) { + function constructDataUrl(file) { + var disallowObjectUrl = Upload.attrGetter('ngfNoObjectUrl', attr, scope); + Upload.dataUrl(file, disallowObjectUrl)['finally'](function () { + $timeout(function () { + var src = (disallowObjectUrl ? file.$ngfDataUrl : file.$ngfBlobUrl) || file.$ngfDataUrl; + if (isBackground) { + elem.css('background-image', 'url(\'' + (src || '') + '\')'); + } else { + elem.attr('src', src); + } + if (src) { + elem.removeClass('ng-hide'); + } else { + elem.addClass('ng-hide'); + } + }); + }); + } + + $timeout(function () { + var unwatch = scope.$watch(attr[directiveName], function (file) { + var size = resizeParams; + if (directiveName === 'ngfThumbnail') { + if (!size) { + size = {width: elem[0].clientWidth, height: elem[0].clientHeight}; + } + if (size.width === 0 && window.getComputedStyle) { + var style = getComputedStyle(elem[0]); + size = { + width: parseInt(style.width.slice(0, -2)), + height: parseInt(style.height.slice(0, -2)) + }; + } + } + + if (angular.isString(file)) { + elem.removeClass('ng-hide'); + if (isBackground) { + return elem.css('background-image', 'url(\'' + file + '\')'); + } else { + return elem.attr('src', file); + } + } + if (file && file.type && file.type.search(getTagType(elem[0])) === 0 && + (!isBackground || file.type.indexOf('image') === 0)) { + if (size && Upload.isResizeSupported()) { + Upload.resize(file, size.width, size.height, size.quality).then( + function (f) { + constructDataUrl(f); + }, function (e) { + throw e; + } + ); + } else { + constructDataUrl(file); + } + } else { + elem.addClass('ng-hide'); + } + }); + + scope.$on('$destroy', function () { + unwatch(); + }); + }); + } + + + /** @namespace attr.ngfSrc */ + /** @namespace attr.ngfNoObjectUrl */ + ngFileUpload.directive('ngfSrc', ['Upload', '$timeout', function (Upload, $timeout) { + return { + restrict: 'AE', + link: function (scope, elem, attr) { + linkFileDirective(Upload, $timeout, scope, elem, attr, 'ngfSrc', + Upload.attrGetter('ngfResize', attr, scope), false); + } + }; + }]); + + /** @namespace attr.ngfBackground */ + /** @namespace attr.ngfNoObjectUrl */ + ngFileUpload.directive('ngfBackground', ['Upload', '$timeout', function (Upload, $timeout) { + return { + restrict: 'AE', + link: function (scope, elem, attr) { + linkFileDirective(Upload, $timeout, scope, elem, attr, 'ngfBackground', + Upload.attrGetter('ngfResize', attr, scope), true); + } + }; + }]); + + /** @namespace attr.ngfThumbnail */ + /** @namespace attr.ngfAsBackground */ + /** @namespace attr.ngfSize */ + /** @namespace attr.ngfNoObjectUrl */ + ngFileUpload.directive('ngfThumbnail', ['Upload', '$timeout', function (Upload, $timeout) { + return { + restrict: 'AE', + link: function (scope, elem, attr) { + var size = Upload.attrGetter('ngfSize', attr, scope); + linkFileDirective(Upload, $timeout, scope, elem, attr, 'ngfThumbnail', size, + Upload.attrGetter('ngfAsBackground', attr, scope)); + } + }; + }]); + + ngFileUpload.config(['$compileProvider', function ($compileProvider) { + if ($compileProvider.imgSrcSanitizationWhitelist) $compileProvider.imgSrcSanitizationWhitelist(/^\s*(https?|ftp|mailto|tel|local|file|data|blob):/); + if ($compileProvider.aHrefSanitizationWhitelist) $compileProvider.aHrefSanitizationWhitelist(/^\s*(https?|ftp|mailto|tel|local|file|data|blob):/); + }]); + + ngFileUpload.filter('ngfDataUrl', ['UploadDataUrl', '$sce', function (UploadDataUrl, $sce) { + return function (file, disallowObjectUrl, trustedUrl) { + if (angular.isString(file)) { + return $sce.trustAsResourceUrl(file); + } + var src = file && ((disallowObjectUrl ? file.$ngfDataUrl : file.$ngfBlobUrl) || file.$ngfDataUrl); + if (file && !src) { + if (!file.$ngfDataUrlFilterInProgress && angular.isObject(file)) { + file.$ngfDataUrlFilterInProgress = true; + UploadDataUrl.dataUrl(file, disallowObjectUrl); + } + return ''; + } + if (file) delete file.$ngfDataUrlFilterInProgress; + return (file && src ? (trustedUrl ? $sce.trustAsResourceUrl(src) : src) : file) || ''; + }; + }]); + +})(); + +ngFileUpload.service('UploadValidate', ['UploadDataUrl', '$q', '$timeout', function (UploadDataUrl, $q, $timeout) { + var upload = UploadDataUrl; + + function globStringToRegex(str) { + var regexp = '', excludes = []; + if (str.length > 2 && str[0] === '/' && str[str.length - 1] === '/') { + regexp = str.substring(1, str.length - 1); + } else { + var split = str.split(','); + if (split.length > 1) { + for (var i = 0; i < split.length; i++) { + var r = globStringToRegex(split[i]); + if (r.regexp) { + regexp += '(' + r.regexp + ')'; + if (i < split.length - 1) { + regexp += '|'; + } + } else { + excludes = excludes.concat(r.excludes); + } + } + } else { + if (str.indexOf('!') === 0) { + excludes.push('^((?!' + globStringToRegex(str.substring(1)).regexp + ').)*$'); + } else { + if (str.indexOf('.') === 0) { + str = '*' + str; + } + regexp = '^' + str.replace(new RegExp('[.\\\\+*?\\[\\^\\]$(){}=!<>|:\\-]', 'g'), '\\$&') + '$'; + regexp = regexp.replace(/\\\*/g, '.*').replace(/\\\?/g, '.'); + } + } + } + return {regexp: regexp, excludes: excludes}; + } + + upload.validatePattern = function (file, val) { + if (!val) { + return true; + } + var pattern = globStringToRegex(val), valid = true; + if (pattern.regexp && pattern.regexp.length) { + var regexp = new RegExp(pattern.regexp, 'i'); + valid = (file.type != null && regexp.test(file.type)) || + (file.name != null && regexp.test(file.name)); + } + var len = pattern.excludes.length; + while (len--) { + var exclude = new RegExp(pattern.excludes[len], 'i'); + valid = valid && (file.type == null || exclude.test(file.type)) && + (file.name == null || exclude.test(file.name)); + } + return valid; + }; + + upload.ratioToFloat = function (val) { + var r = val.toString(), xIndex = r.search(/[x:]/i); + if (xIndex > -1) { + r = parseFloat(r.substring(0, xIndex)) / parseFloat(r.substring(xIndex + 1)); + } else { + r = parseFloat(r); + } + return r; + }; + + upload.registerModelChangeValidator = function (ngModel, attr, scope) { + if (ngModel) { + ngModel.$formatters.push(function (files) { + if (ngModel.$dirty) { + if (files && !angular.isArray(files)) { + files = [files]; + } + upload.validate(files, 0, ngModel, attr, scope).then(function () { + upload.applyModelValidation(ngModel, files); + }); + } + }); + } + }; + + function markModelAsDirty(ngModel, files) { + if (files != null && !ngModel.$dirty) { + if (ngModel.$setDirty) { + ngModel.$setDirty(); + } else { + ngModel.$dirty = true; + } + } + } + + upload.applyModelValidation = function (ngModel, files) { + markModelAsDirty(ngModel, files); + angular.forEach(ngModel.$ngfValidations, function (validation) { + ngModel.$setValidity(validation.name, validation.valid); + }); + }; + + upload.getValidationAttr = function (attr, scope, name, validationName, file) { + var dName = 'ngf' + name[0].toUpperCase() + name.substr(1); + var val = upload.attrGetter(dName, attr, scope, {$file: file}); + if (val == null) { + val = upload.attrGetter('ngfValidate', attr, scope, {$file: file}); + if (val) { + var split = (validationName || name).split('.'); + val = val[split[0]]; + if (split.length > 1) { + val = val && val[split[1]]; + } + } + } + return val; + }; + + upload.validate = function (files, prevLength, ngModel, attr, scope) { + ngModel = ngModel || {}; + ngModel.$ngfValidations = ngModel.$ngfValidations || []; + + angular.forEach(ngModel.$ngfValidations, function (v) { + v.valid = true; + }); + + var attrGetter = function (name, params) { + return upload.attrGetter(name, attr, scope, params); + }; + + if (files == null || files.length === 0) { + return upload.emptyPromise(ngModel); + } + + files = files.length === undefined ? [files] : files.slice(0); + + function validateSync(name, validationName, fn) { + if (files) { + var i = files.length, valid = null; + while (i--) { + var file = files[i]; + if (file) { + var val = upload.getValidationAttr(attr, scope, name, validationName, file); + if (val != null) { + if (!fn(file, val, i)) { + file.$error = name; + (file.$errorMessages = (file.$errorMessages || {}))[name] = true; + file.$errorParam = val; + files.splice(i, 1); + valid = false; + } + } + } + } + if (valid !== null) { + ngModel.$ngfValidations.push({name: name, valid: valid}); + } + } + } + + validateSync('maxFiles', null, function (file, val, i) { + return prevLength + i < val; + }); + validateSync('pattern', null, upload.validatePattern); + validateSync('minSize', 'size.min', function (file, val) { + return file.size + 0.1 >= upload.translateScalars(val); + }); + validateSync('maxSize', 'size.max', function (file, val) { + return file.size - 0.1 <= upload.translateScalars(val); + }); + var totalSize = 0; + validateSync('maxTotalSize', null, function (file, val) { + totalSize += file.size; + if (totalSize > upload.translateScalars(val)) { + files.splice(0, files.length); + return false; + } + return true; + }); + + validateSync('validateFn', null, function (file, r) { + return r === true || r === null || r === ''; + }); + + if (!files.length) { + return upload.emptyPromise(ngModel, ngModel.$ngfValidations); + } + + function validateAsync(name, validationName, type, asyncFn, fn) { + function resolveResult(defer, file, val) { + if (val != null) { + asyncFn(file, val).then(function (d) { + if (!fn(d, val)) { + file.$error = name; + (file.$errorMessages = (file.$errorMessages || {}))[name] = true; + file.$errorParam = val; + defer.reject(); + } else { + defer.resolve(); + } + }, function () { + if (attrGetter('ngfValidateForce', {$file: file})) { + file.$error = name; + (file.$errorMessages = (file.$errorMessages || {}))[name] = true; + file.$errorParam = val; + defer.reject(); + } else { + defer.resolve(); + } + }); + } else { + defer.resolve(); + } + } + + var promises = [upload.emptyPromise()]; + if (files) { + files = files.length === undefined ? [files] : files; + angular.forEach(files, function (file) { + var defer = $q.defer(); + promises.push(defer.promise); + if (type && (file.type == null || file.type.search(type) !== 0)) { + defer.resolve(); + return; + } + if (name === 'dimensions' && upload.attrGetter('ngfDimensions', attr) != null) { + upload.imageDimensions(file).then(function (d) { + resolveResult(defer, file, + attrGetter('ngfDimensions', {$file: file, $width: d.width, $height: d.height})); + }, function () { + defer.reject(); + }); + } else if (name === 'duration' && upload.attrGetter('ngfDuration', attr) != null) { + upload.mediaDuration(file).then(function (d) { + resolveResult(defer, file, + attrGetter('ngfDuration', {$file: file, $duration: d})); + }, function () { + defer.reject(); + }); + } else { + resolveResult(defer, file, + upload.getValidationAttr(attr, scope, name, validationName, file)); + } + }); + return $q.all(promises).then(function () { + ngModel.$ngfValidations.push({name: name, valid: true}); + }, function () { + ngModel.$ngfValidations.push({name: name, valid: false}); + }); + } + } + + var deffer = $q.defer(); + var promises = []; + + promises.push(upload.happyPromise(validateAsync('maxHeight', 'height.max', /image/, + this.imageDimensions, function (d, val) { + return d.height <= val; + }))); + promises.push(upload.happyPromise(validateAsync('minHeight', 'height.min', /image/, + this.imageDimensions, function (d, val) { + return d.height >= val; + }))); + promises.push(upload.happyPromise(validateAsync('maxWidth', 'width.max', /image/, + this.imageDimensions, function (d, val) { + return d.width <= val; + }))); + promises.push(upload.happyPromise(validateAsync('minWidth', 'width.min', /image/, + this.imageDimensions, function (d, val) { + return d.width >= val; + }))); + promises.push(upload.happyPromise(validateAsync('dimensions', null, /image/, + function (file, val) { + return upload.emptyPromise(val); + }, function (r) { + return r; + }))); + promises.push(upload.happyPromise(validateAsync('ratio', null, /image/, + this.imageDimensions, function (d, val) { + var split = val.toString().split(','), valid = false; + for (var i = 0; i < split.length; i++) { + if (Math.abs((d.width / d.height) - upload.ratioToFloat(split[i])) < 0.0001) { + valid = true; + } + } + return valid; + }))); + promises.push(upload.happyPromise(validateAsync('maxRatio', 'ratio.max', /image/, + this.imageDimensions, function (d, val) { + return (d.width / d.height) - upload.ratioToFloat(val) < 0.0001; + }))); + promises.push(upload.happyPromise(validateAsync('minRatio', 'ratio.min', /image/, + this.imageDimensions, function (d, val) { + return (d.width / d.height) - upload.ratioToFloat(val) > -0.0001; + }))); + promises.push(upload.happyPromise(validateAsync('maxDuration', 'duration.max', /audio|video/, + this.mediaDuration, function (d, val) { + return d <= upload.translateScalars(val); + }))); + promises.push(upload.happyPromise(validateAsync('minDuration', 'duration.min', /audio|video/, + this.mediaDuration, function (d, val) { + return d >= upload.translateScalars(val); + }))); + promises.push(upload.happyPromise(validateAsync('duration', null, /audio|video/, + function (file, val) { + return upload.emptyPromise(val); + }, function (r) { + return r; + }))); + + promises.push(upload.happyPromise(validateAsync('validateAsyncFn', null, null, + function (file, val) { + return val; + }, function (r) { + return r === true || r === null || r === ''; + }))); + + return $q.all(promises).then(function () { + deffer.resolve(ngModel, ngModel.$ngfValidations); + }); + }; + + upload.imageDimensions = function (file) { + if (file.$ngfWidth && file.$ngfHeight) { + var d = $q.defer(); + $timeout(function () { + d.resolve({width: file.$ngfWidth, height: file.$ngfHeight}); + }); + return d.promise; + } + if (file.$ngfDimensionPromise) return file.$ngfDimensionPromise; + + var deferred = $q.defer(); + $timeout(function () { + if (file.type.indexOf('image') !== 0) { + deferred.reject('not image'); + return; + } + upload.dataUrl(file).then(function (dataUrl) { + var img = angular.element('').attr('src', dataUrl) + .css('visibility', 'hidden').css('position', 'fixed') + .css('max-width', 'none !important').css('max-height', 'none !important'); + + function success() { + var width = img[0].clientWidth; + var height = img[0].clientHeight; + img.remove(); + file.$ngfWidth = width; + file.$ngfHeight = height; + deferred.resolve({width: width, height: height}); + } + + function error() { + img.remove(); + deferred.reject('load error'); + } + + img.on('load', success); + img.on('error', error); + var count = 0; + + function checkLoadError() { + $timeout(function () { + if (img[0].parentNode) { + if (img[0].clientWidth) { + success(); + } else if (count > 10) { + error(); + } else { + checkLoadError(); + } + } + }, 1000); + } + + checkLoadError(); + + angular.element(document.getElementsByTagName('body')[0]).append(img); + }, function () { + deferred.reject('load error'); + }); + }); + + file.$ngfDimensionPromise = deferred.promise; + file.$ngfDimensionPromise['finally'](function () { + delete file.$ngfDimensionPromise; + }); + return file.$ngfDimensionPromise; + }; + + upload.mediaDuration = function (file) { + if (file.$ngfDuration) { + var d = $q.defer(); + $timeout(function () { + d.resolve(file.$ngfDuration); + }); + return d.promise; + } + if (file.$ngfDurationPromise) return file.$ngfDurationPromise; + + var deferred = $q.defer(); + $timeout(function () { + if (file.type.indexOf('audio') !== 0 && file.type.indexOf('video') !== 0) { + deferred.reject('not media'); + return; + } + upload.dataUrl(file).then(function (dataUrl) { + var el = angular.element(file.type.indexOf('audio') === 0 ? '