'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); var _defineProperty2 = require('babel-runtime/helpers/defineProperty'); var _defineProperty3 = _interopRequireDefault(_defineProperty2); var _extends2 = require('babel-runtime/helpers/extends'); var _extends3 = _interopRequireDefault(_extends2); var _vueTypes = require('../../_util/vue-types'); var _vueTypes2 = _interopRequireDefault(_vueTypes); var _BaseMixin = require('../../_util/BaseMixin'); var _BaseMixin2 = _interopRequireDefault(_BaseMixin); var _classnames = require('classnames'); var _classnames2 = _interopRequireDefault(_classnames); var _uid = require('./uid'); var _uid2 = _interopRequireDefault(_uid); var _warning = require('../../_util/warning'); var _warning2 = _interopRequireDefault(_warning); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; } 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: [_BaseMixin2['default']], props: { componentTag: _vueTypes2['default'].string, // style: PropTypes.object, disabled: _vueTypes2['default'].bool, prefixCls: _vueTypes2['default'].string, // className: PropTypes.string, accept: _vueTypes2['default'].string, // onStart: PropTypes.func, multiple: _vueTypes2['default'].bool, // children: PropTypes.any, data: _vueTypes2['default'].oneOfType([_vueTypes2['default'].object, _vueTypes2['default'].func]), action: _vueTypes2['default'].oneOfType([_vueTypes2['default'].string, _vueTypes2['default'].func]), name: _vueTypes2['default'].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) { (0, _warning2['default'])(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: (0, _uid2['default'])(), 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="" 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 _this2 = this; 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); new Promise(function (resolve) { var action = _this2.action; if (typeof action === 'function') { return resolve(action(file)); } resolve(action); }).then(function (action) { formNode.setAttribute('action', action); formNode.submit(); dataSpan.innerHTML = ''; _this2.$emit('start', file); }); } }, mounted: function mounted() { var _this3 = this; this.$nextTick(function () { _this3.updateIframeWH(); _this3.initIframe(); }); }, updated: function updated() { var _this4 = this; this.$nextTick(function () { _this4.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 = (0, _extends3['default'])({}, IFRAME_STYLE, { display: this.uploading || disabled ? 'none' : '' }); var cls = (0, _classnames2['default'])((_classNames = {}, (0, _defineProperty3['default'])(_classNames, prefixCls, true), (0, _defineProperty3['default'])(_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']] ); } }; exports['default'] = IframeUploader;