vue-antd-ui
Version:
An enterprise-class UI design language and Vue-based implementation
255 lines (241 loc) • 8.12 kB
JavaScript
import _defineProperty from 'babel-runtime/helpers/defineProperty';
import _extends from 'babel-runtime/helpers/extends';
import PropTypes from '../../_util/vue-types';
import BaseMixin from '../../_util/BaseMixin';
import classNames from 'classnames';
import getUid from './uid';
import warning from '../../_util/warning';
var IFRAME_STYLE = {
position: 'absolute',
top: 0,
opacity: 0,
filter: 'alpha(opacity=0)',
left: 0,
zIndex: 9999
// diferent from AjaxUpload, can only upload on at one time, serial seriously
};var IframeUploader = {
mixins: [BaseMixin],
props: {
componentTag: PropTypes.string,
// style: PropTypes.object,
disabled: PropTypes.bool,
prefixCls: PropTypes.string,
// className: PropTypes.string,
accept: PropTypes.string,
// onStart: PropTypes.func,
multiple: PropTypes.bool,
// children: PropTypes.any,
data: PropTypes.oneOfType([PropTypes.object, PropTypes.func]),
action: PropTypes.string,
name: PropTypes.string
},
data: function data() {
this.file = {};
return {
uploading: false
};
},
methods: {
onLoad: function onLoad() {
if (!this.uploading) {
return;
}
var file = this.file;
var response = void 0;
try {
var doc = this.getIframeDocument();
var script = doc.getElementsByTagName('script')[0];
if (script && script.parentNode === doc.body) {
doc.body.removeChild(script);
}
response = doc.body.innerHTML;
this.$emit('success', response, file);
} catch (err) {
warning(false, 'cross domain error for Upload. Maybe server should return document.domain script. see Note from https://github.com/react-component/upload');
response = 'cross-domain';
this.$emit('error', err, null, file);
}
this.endUpload();
},
onChange: function onChange() {
var _this = this;
var target = this.getFormInputNode();
// ie8/9 don't support FileList Object
// http://stackoverflow.com/questions/12830058/ie8-input-type-file-get-files
var file = this.file = {
uid: getUid(),
name: target.value
};
this.startUpload();
var props = this.$props;
if (!props.beforeUpload) {
return this.post(file);
}
var before = props.beforeUpload(file);
if (before && before.then) {
before.then(function () {
_this.post(file);
}, function () {
_this.endUpload();
});
} else if (before !== false) {
this.post(file);
} else {
this.endUpload();
}
},
getIframeNode: function getIframeNode() {
return this.$refs.iframeRef;
},
getIframeDocument: function getIframeDocument() {
return this.getIframeNode().contentDocument;
},
getFormNode: function getFormNode() {
return this.getIframeDocument().getElementById('form');
},
getFormInputNode: function getFormInputNode() {
return this.getIframeDocument().getElementById('input');
},
getFormDataNode: function getFormDataNode() {
return this.getIframeDocument().getElementById('data');
},
getFileForMultiple: function getFileForMultiple(file) {
return this.multiple ? [file] : file;
},
getIframeHTML: function getIframeHTML(domain) {
var domainScript = '';
var domainInput = '';
if (domain) {
var script = 'script';
domainScript = '<' + script + '>document.domain="' + domain + '";</' + script + '>';
domainInput = '<input name="_documentDomain" value="' + domain + '" />';
}
return '\n <!DOCTYPE html>\n <html>\n <head>\n <meta http-equiv="X-UA-Compatible" content="IE=edge" />\n <style>\n body,html {padding:0;margin:0;border:0;overflow:hidden;}\n </style>\n ' + domainScript + '\n </head>\n <body>\n <form method="post"\n encType="multipart/form-data"\n action="' + this.action + '" id="form"\n style="display:block;height:9999px;position:relative;overflow:hidden;">\n <input id="input" type="file"\n name="' + this.name + '"\n style="position:absolute;top:0;right:0;height:9999px;font-size:9999px;cursor:pointer;"/>\n ' + domainInput + '\n <span id="data"></span>\n </form>\n </body>\n </html>\n ';
},
initIframeSrc: function initIframeSrc() {
if (this.domain) {
this.getIframeNode().src = 'javascript:void((function(){\n var d = document;\n d.open();\n d.domain=\'' + this.domain + '\';\n d.write(\'\');\n d.close();\n })())';
}
},
initIframe: function initIframe() {
var iframeNode = this.getIframeNode();
var win = iframeNode.contentWindow;
var doc = void 0;
this.domain = this.domain || '';
this.initIframeSrc();
try {
doc = win.document;
} catch (e) {
this.domain = document.domain;
this.initIframeSrc();
win = iframeNode.contentWindow;
doc = win.document;
}
doc.open('text/html', 'replace');
doc.write(this.getIframeHTML(this.domain));
doc.close();
this.getFormInputNode().onchange = this.onChange;
},
endUpload: function endUpload() {
if (this.uploading) {
this.file = {};
// hack avoid batch
this.uploading = false;
this.setState({
uploading: false
});
this.initIframe();
}
},
startUpload: function startUpload() {
if (!this.uploading) {
this.uploading = true;
this.setState({
uploading: true
});
}
},
updateIframeWH: function updateIframeWH() {
var rootNode = this.$el;
var iframeNode = this.getIframeNode();
iframeNode.style.height = rootNode.offsetHeight + 'px';
iframeNode.style.width = rootNode.offsetWidth + 'px';
},
abort: function abort(file) {
if (file) {
var uid = file;
if (file && file.uid) {
uid = file.uid;
}
if (uid === this.file.uid) {
this.endUpload();
}
} else {
this.endUpload();
}
},
post: function post(file) {
var formNode = this.getFormNode();
var dataSpan = this.getFormDataNode();
var data = this.$props.data;
if (typeof data === 'function') {
data = data(file);
}
var inputs = document.createDocumentFragment();
for (var key in data) {
if (data.hasOwnProperty(key)) {
var input = document.createElement('input');
input.setAttribute('name', key);
input.value = data[key];
inputs.appendChild(input);
}
}
dataSpan.appendChild(inputs);
formNode.submit();
dataSpan.innerHTML = '';
this.$emit('start', file);
}
},
mounted: function mounted() {
var _this2 = this;
this.$nextTick(function () {
_this2.updateIframeWH();
_this2.initIframe();
});
},
updated: function updated() {
var _this3 = this;
this.$nextTick(function () {
_this3.updateIframeWH();
});
},
render: function render() {
var _classNames;
var h = arguments[0];
var _$props = this.$props,
Tag = _$props.componentTag,
disabled = _$props.disabled,
prefixCls = _$props.prefixCls;
var iframeStyle = _extends({}, IFRAME_STYLE, {
display: this.uploading || disabled ? 'none' : ''
});
var cls = classNames((_classNames = {}, _defineProperty(_classNames, prefixCls, true), _defineProperty(_classNames, prefixCls + '-disabled', disabled), _classNames));
return h(
Tag,
{
attrs: {
className: cls
},
style: { position: 'relative', zIndex: 0 }
},
[h('iframe', {
ref: 'iframeRef',
on: {
'load': this.onLoad
},
style: iframeStyle
}), this.$slots['default']]
);
}
};
export default IframeUploader;