Skip to content

Commit

Permalink
fixed #1720, fixed #1529
Browse files Browse the repository at this point in the history
  • Loading branch information
danialfarid committed Sep 28, 2016
1 parent 4b5d89f commit 052ea19
Show file tree
Hide file tree
Showing 19 changed files with 245 additions and 161 deletions.
2 changes: 1 addition & 1 deletion demo/src/main/webapp/js/FileAPI.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

70 changes: 43 additions & 27 deletions demo/src/main/webapp/js/ng-file-upload-all.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* progress, resize, thumbnail, preview, validation and CORS
* FileAPI Flash shim for old browsers not supporting FormData
* @author Danial <[email protected]>
* @version 12.2.11
* @version 12.2.12
*/

(function () {
Expand Down Expand Up @@ -424,7 +424,7 @@ if (!window.FileReader) {
* AngularJS file upload directives and services. Supoorts: file upload/drop/paste, resume, cancel/abort,
* progress, resize, thumbnail, preview, validation and CORS
* @author Danial <[email protected]>
* @version 12.2.11
* @version 12.2.12
*/

if (window.XMLHttpRequest && !(window.FileAPI && FileAPI.shouldLoad)) {
Expand All @@ -445,7 +445,7 @@ if (window.XMLHttpRequest && !(window.FileAPI && FileAPI.shouldLoad)) {

var ngFileUpload = angular.module('ngFileUpload', []);

ngFileUpload.version = '12.2.11';
ngFileUpload.version = '12.2.12';

ngFileUpload.service('UploadBase', ['$http', '$q', '$timeout', function ($http, $q, $timeout) {
var upload = this;
Expand Down Expand Up @@ -902,13 +902,13 @@ ngFileUpload.service('Upload', ['$parse', '$timeout', '$compile', '$q', 'UploadE
return $q.all(promises);
}

function resize(files, attr, scope) {
function resizeFile(files, attr, scope, ngModel) {
var resizeVal = upload.attrGetter('ngfResize', attr, scope);
if (!resizeVal || !upload.isResizeSupported() || !files.length) return upload.emptyPromise();
if (resizeVal instanceof Function) {
var defer = $q.defer();
return resizeVal(files).then(function (p) {
resizeWithParams(p, files, attr, scope).then(function (r) {
resizeWithParams(p, files, attr, scope, ngModel).then(function (r) {
defer.resolve(r);
}, function (e) {
defer.reject(e);
Expand All @@ -917,11 +917,11 @@ ngFileUpload.service('Upload', ['$parse', '$timeout', '$compile', '$q', 'UploadE
defer.reject(e);
});
} else {
return resizeWithParams(resizeVal, files, attr, scope);
return resizeWithParams(resizeVal, files, attr, scope, ngModel);
}
}

function resizeWithParams(params, files, attr, scope) {
function resizeWithParams(params, files, attr, scope, ngModel) {
var promises = [upload.emptyPromise()];

function handleFile(f, i) {
Expand All @@ -937,7 +937,10 @@ ngFileUpload.service('Upload', ['$parse', '$timeout', '$compile', '$q', 'UploadE
files.splice(i, 1, resizedFile);
}, function (e) {
f.$error = 'resize';
(f.$errorMessages = (f.$errorMessages || {})).resize = true;
f.$errorParam = (e ? (e.message ? e.message : e) + ': ' : '') + (f && f.name);
ngModel.$ngfValidations.push({name: 'resize', valid: false});
upload.applyModelValidation(ngModel, files);
});
}
}
Expand Down Expand Up @@ -1033,7 +1036,8 @@ ngFileUpload.service('Upload', ['$parse', '$timeout', '$compile', '$q', 'UploadE
}, options && options.debounce ? options.debounce.change || options.debounce : 0);
}

resize(validateAfterResize ? allNewFiles : valids, attr, scope).then(function () {
var resizingFiles = validateAfterResize ? allNewFiles : valids;
resizeFile(resizingFiles, attr, scope, ngModel).then(function () {
if (validateAfterResize) {
upload.validate(allNewFiles, keep ? prevValidFiles.length : 0, ngModel, attr, scope)
.then(function (validationResult) {
Expand All @@ -1044,8 +1048,18 @@ ngFileUpload.service('Upload', ['$parse', '$timeout', '$compile', '$q', 'UploadE
} else {
updateModel();
}
}, function (e) {
throw 'Could not resize files ' + e;
}, function () {
for (var i = 0; i < resizingFiles.length; i++) {
var f = resizingFiles[i];
if (f.$error === 'resize') {
var index = valids.indexOf(f);
if (index > -1) {
valids.splice(index, 1);
invalids.push(f);
}
updateModel();
}
}
});
}

Expand Down Expand Up @@ -1168,21 +1182,25 @@ ngFileUpload.directive('ngfSelect', ['$parse', '$timeout', '$compile', 'Upload',
fileElem.attr('accept', attrGetter('ngfAccept', scope));
}));
}
attr.$observe('accept', function () {
unwatches.push(attr.$observe('accept', function () {
fileElem.attr('accept', attrGetter('accept'));
});
unwatches.push(function () {
if (attr.$$observers) delete attr.$$observers.accept;
});
function bindAttrToFileInput(fileElem) {
}));
function bindAttrToFileInput(fileElem, label) {
function updateId(val) {
fileElem.attr('id', 'ngf-' + val);
label.attr('id', 'ngf-label-' + val);
}

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';
if (attribute.name === 'id') {
updateId(attribute.value);
unwatches.push(attr.$observe('id', updateId));
} else {
fileElem.attr(attribute.name, (!attribute.value && (attribute.name === 'required' ||
attribute.name === 'multiple')) ? attribute.name : attribute.value);
}
fileElem.attr(attribute.name, attribute.name === 'id' ? 'ngf-' + attribute.value : attribute.value);
}
}
}
Expand All @@ -1194,15 +1212,12 @@ ngFileUpload.directive('ngfSelect', ['$parse', '$timeout', '$compile', 'Upload',

var fileElem = angular.element('<input type="file">');

bindAttrToFileInput(fileElem);

var label = angular.element('<label>upload</label>');
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');
if (elem.attr('id')) {
label.attr('id', 'ngf-label-' + elem.attr('id'));
}
bindAttrToFileInput(fileElem, label);

generatedElems.push({el: elem, ref: label});

document.body.appendChild(label.append(fileElem)[0]);
Expand All @@ -1227,7 +1242,8 @@ ngFileUpload.directive('ngfSelect', ['$parse', '$timeout', '$compile', 'Upload',
document.body.appendChild(fileElem.parent()[0]);
fileElem.bind('change', changeFn);
}
} catch(e){/*ignore*/}
} catch (e) {/*ignore*/
}

if (isDelayedClickSupported(navigator.userAgent)) {
setTimeout(function () {
Expand Down Expand Up @@ -1257,7 +1273,7 @@ ngFileUpload.directive('ngfSelect', ['$parse', '$timeout', '$compile', 'Upload',
var currentX = touches[0].clientX;
var currentY = touches[0].clientY;
if ((Math.abs(currentX - initialTouchStartX) > 20) ||
(Math.abs(currentY - initialTouchStartY) > 20)) {
(Math.abs(currentY - initialTouchStartY) > 20)) {
evt.stopPropagation();
evt.preventDefault();
return false;
Expand Down
6 changes: 3 additions & 3 deletions demo/src/main/webapp/js/ng-file-upload-all.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion demo/src/main/webapp/js/ng-file-upload-shim.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* progress, resize, thumbnail, preview, validation and CORS
* FileAPI Flash shim for old browsers not supporting FormData
* @author Danial <[email protected]>
* @version 12.2.11
* @version 12.2.12
*/

(function () {
Expand Down
2 changes: 1 addition & 1 deletion demo/src/main/webapp/js/ng-file-upload-shim.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 052ea19

Please sign in to comment.