Skip to content

Commit

Permalink
Fixed #298
Browse files Browse the repository at this point in the history
  • Loading branch information
Danial Farid authored and Danial Farid committed Sep 26, 2014
1 parent 05a3a55 commit 4c30954
Show file tree
Hide file tree
Showing 8 changed files with 93 additions and 53 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ Thumbs.db
classes/
._*
/node_modules
*.jar
*.jar
release.sh
3 changes: 3 additions & 0 deletions demo/war/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,9 @@ <h3>
myModel: <input type="text" ng-model="myModel"> model object to be sent with the file.<br/>
<div class="upload-buttons">
choose a single file: <input type="file" ng-file-select="onFileSelect($files)" onclick="this.value=null">
<button type="button" ng-file-select="onFileSelect($files)">aaaaaaa</button>
<input type="button" ng-file-select="onFileSelect($files)" name="zzzzzzzzzz" value="vvvvvvvvv">
<input type="file" ng-file-select="onFileSelect($files)">
<br/>
or multiple files: &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <input type="file" ng-file-select="onFileSelect($files)" multiple onclick="this.value=null">
<br/>
Expand Down
30 changes: 19 additions & 11 deletions demo/war/js/angular-file-upload-shim.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ var hasFlash = function() {
var fo = new ActiveXObject('ShockwaveFlash.ShockwaveFlash');
if (fo) return true;
} catch(e) {
if (navigator.mimeTypes["application/x-shockwave-flash"] != undefined) return true;
if (navigator.mimeTypes['application/x-shockwave-flash'] != undefined) return true;
}
return false;
}
Expand All @@ -22,7 +22,7 @@ var patchXHR = function(fnName, newFn) {
if (window.XMLHttpRequest) {
if (window.FormData && (!window.FileAPI || !FileAPI.forceLoad)) {
// allow access to Angular XHR private field: https://github.com/angular/angular.js/issues/1934
patchXHR("setRequestHeader", function(orig) {
patchXHR('setRequestHeader', function(orig) {
return function(header, value) {
if (header === '__setXHR_') {
var val = value(this);
Expand All @@ -48,7 +48,7 @@ if (window.XMLHttpRequest) {
}
}

patchXHR("open", function(orig) {
patchXHR('open', function(orig) {
return function(m, url, b) {
initializeUploadListener(this);
this.__url = url;
Expand All @@ -62,25 +62,25 @@ if (window.XMLHttpRequest) {
}
});

patchXHR("getResponseHeader", function(orig) {
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) {
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) {
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) {
patchXHR('setRequestHeader', function(orig) {
return function(header, value) {
if (header === '__setXHR_') {
initializeUploadListener(this);
Expand All @@ -97,7 +97,7 @@ if (window.XMLHttpRequest) {
}
});

patchXHR("send", function(orig) {
patchXHR('send', function(orig) {
return function() {
var xhr = this;
if (arguments[0] && arguments[0].__isShim) {
Expand Down Expand Up @@ -138,7 +138,7 @@ if (window.XMLHttpRequest) {
xhr.getAllResponseHeaders = function(){};
_this.complete(null, {status: 204, statusText: 'No Content'});
}
}, 5000);
}, 10000);
}
},
headers: xhr.__requestHeaders
Expand Down Expand Up @@ -187,6 +187,14 @@ if (!window.FormData || (window.FileAPI && FileAPI.forceLoad)) {
wrap.appendChild(elem);
} else {
el.addClass('js-fileapi-wrapper');
if (el.parent()[0].__file_click_fn_delegate_) {
if (el.parent().css('position') === '' || el.parent().css('position') === 'static') {
el.parent().css('position', 'relative');
}
el.css('top', 0).css('bottom', 0).css('left', 0).css('right', 0).css('width', '100%').css('height', '100%').
css('padding', 0).css('margin', 0);
el.parent().unbind('click', el.parent()[0].__file_click_fn_delegate_);
}
}
}
}
Expand Down Expand Up @@ -236,7 +244,7 @@ if (!window.FormData || (window.FileAPI && FileAPI.forceLoad)) {
addFlash(this);
if (window.jQuery) {
// fix for #281 jQuery on IE8
angular.element(this).bind("change", changeFnWrapper(null));
angular.element(this).bind('change', changeFnWrapper(null));
} else {
origAttachEvent.apply(this, [e, changeFnWrapper(fn)]);
}
Expand Down Expand Up @@ -291,7 +299,7 @@ if (!window.FormData || (window.FileAPI && FileAPI.forceLoad)) {
}

if (FileAPI.staticPath == null) FileAPI.staticPath = basePath;
script.setAttribute('src', jsUrl || basePath + "FileAPI.min.js");
script.setAttribute('src', jsUrl || basePath + 'FileAPI.min.js');
document.getElementsByTagName('head')[0].appendChild(script);
FileAPI.hasFlash = hasFlash();
}
Expand Down
38 changes: 24 additions & 14 deletions demo/war/js/angular-file-upload.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,24 +145,34 @@ angularFileUpload.directive('ngFileSelect', [ '$parse', '$timeout', function($pa
var fn = $parse(attr['ngFileSelect']);
if (elem[0].tagName.toLowerCase() !== 'input' || (elem.attr('type') && elem.attr('type').toLowerCase()) !== 'file') {
var fileElem = angular.element('<input type="file">')
for (var i = 0; i < elem[0].attributes.length; i++) {
fileElem.attr(elem[0].attributes[i].name, elem[0].attributes[i].value);
var attrs = elem[0].attributes;
for (var i = 0; i < attrs.length; i++) {
if (attrs[i].name.toLowerCase() !== 'type') {
fileElem.attr(attrs[i].name, attrs[i].value);
}
}
if (attr["multiple"]) fileElem.attr("multiple", "true");
fileElem.css("top", 0).css("bottom", 0).css("left", 0).css("right", 0).css("width", "100%").
css("opacity", 0).css("position", "absolute").css('filter', 'alpha(opacity=0)').css("padding", 0);
fileElem.css("width", "1px").css("height", "1px").css("opacity", 0).css("position", "absolute").css('filter', 'alpha(opacity=0)');
fileElem.attr('__wrapper_for_parent_', true);

// fileElem.css("top", 0).css("bottom", 0).css("left", 0).css("right", 0).css("width", "100%").
// css("opacity", 0).css("position", "absolute").css('filter', 'alpha(opacity=0)').css("padding", 0).css("margin", 0);
elem.append(fileElem);
elem[0].__file_click_fn_delegate_ = function() {
fileElem[0].click();
};
elem.bind('click', elem[0].__file_click_fn_delegate_);
elem.css("overflow", "hidden");
if (fileElem.parent()[0] != elem[0]) {
//fix #298 button element
elem.wrap('<span>');
elem.css("z-index", "-1000")
elem.parent().append(fileElem);
elem = elem.parent();
}
if (elem.css("position") === '' || elem.css("position") === 'static') {
elem.css("position", "relative");
}
// if (fileElem.parent()[0] != elem[0]) {
// //fix #298 button element
// elem.wrap('<span>');
// elem.css("z-index", "-1000")
// elem.parent().append(fileElem);
// elem = elem.parent();
// }
// if (elem.css("position") === '' || elem.css("position") === 'static') {
// elem.css("position", "relative");
// }
elem = fileElem;
}
elem.bind('change', function(evt) {
Expand Down
30 changes: 19 additions & 11 deletions dist/angular-file-upload-shim.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ var hasFlash = function() {
var fo = new ActiveXObject('ShockwaveFlash.ShockwaveFlash');
if (fo) return true;
} catch(e) {
if (navigator.mimeTypes["application/x-shockwave-flash"] != undefined) return true;
if (navigator.mimeTypes['application/x-shockwave-flash'] != undefined) return true;
}
return false;
}
Expand All @@ -22,7 +22,7 @@ var patchXHR = function(fnName, newFn) {
if (window.XMLHttpRequest) {
if (window.FormData && (!window.FileAPI || !FileAPI.forceLoad)) {
// allow access to Angular XHR private field: https://github.com/angular/angular.js/issues/1934
patchXHR("setRequestHeader", function(orig) {
patchXHR('setRequestHeader', function(orig) {
return function(header, value) {
if (header === '__setXHR_') {
var val = value(this);
Expand All @@ -48,7 +48,7 @@ if (window.XMLHttpRequest) {
}
}

patchXHR("open", function(orig) {
patchXHR('open', function(orig) {
return function(m, url, b) {
initializeUploadListener(this);
this.__url = url;
Expand All @@ -62,25 +62,25 @@ if (window.XMLHttpRequest) {
}
});

patchXHR("getResponseHeader", function(orig) {
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) {
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) {
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) {
patchXHR('setRequestHeader', function(orig) {
return function(header, value) {
if (header === '__setXHR_') {
initializeUploadListener(this);
Expand All @@ -97,7 +97,7 @@ if (window.XMLHttpRequest) {
}
});

patchXHR("send", function(orig) {
patchXHR('send', function(orig) {
return function() {
var xhr = this;
if (arguments[0] && arguments[0].__isShim) {
Expand Down Expand Up @@ -138,7 +138,7 @@ if (window.XMLHttpRequest) {
xhr.getAllResponseHeaders = function(){};
_this.complete(null, {status: 204, statusText: 'No Content'});
}
}, 5000);
}, 10000);
}
},
headers: xhr.__requestHeaders
Expand Down Expand Up @@ -187,6 +187,14 @@ if (!window.FormData || (window.FileAPI && FileAPI.forceLoad)) {
wrap.appendChild(elem);
} else {
el.addClass('js-fileapi-wrapper');
if (el.parent()[0].__file_click_fn_delegate_) {
if (el.parent().css('position') === '' || el.parent().css('position') === 'static') {
el.parent().css('position', 'relative');
}
el.css('top', 0).css('bottom', 0).css('left', 0).css('right', 0).css('width', '100%').css('height', '100%').
css('padding', 0).css('margin', 0);
el.parent().unbind('click', el.parent()[0].__file_click_fn_delegate_);
}
}
}
}
Expand Down Expand Up @@ -236,7 +244,7 @@ if (!window.FormData || (window.FileAPI && FileAPI.forceLoad)) {
addFlash(this);
if (window.jQuery) {
// fix for #281 jQuery on IE8
angular.element(this).bind("change", changeFnWrapper(null));
angular.element(this).bind('change', changeFnWrapper(null));
} else {
origAttachEvent.apply(this, [e, changeFnWrapper(fn)]);
}
Expand Down Expand Up @@ -291,7 +299,7 @@ if (!window.FormData || (window.FileAPI && FileAPI.forceLoad)) {
}

if (FileAPI.staticPath == null) FileAPI.staticPath = basePath;
script.setAttribute('src', jsUrl || basePath + "FileAPI.min.js");
script.setAttribute('src', jsUrl || basePath + 'FileAPI.min.js');
document.getElementsByTagName('head')[0].appendChild(script);
FileAPI.hasFlash = hasFlash();
}
Expand Down
Loading

0 comments on commit 4c30954

Please # to comment.