import layerVue from './layer.vue' const Layer = function (Vue) { const LayerVueExtend = Vue.extend(layerVue) const self = {} const defOptions = { type: 0, // 0(信息框,默认)1(页面层)2(iframe层)3(加载层)4(tips层),5msg,6prompt title: '信息', content: '', btn: '确定' } self.instances = {} self.instancesVue = [] self.iframeMinList = [] let seed = 0 /** * [function description] * @method function * @param {[type]} options [description] * @return {[type]} [description] */ self.open = function (opt) { /* let option = { type: 1, content: opt.content, area: opt.area } */ var options = mergeJson(opt, defOptions) const id = `notification_${new Date().getTime()}_${seed++}` options.id = id options.layer = self options.content.content = Vue.extend(options.content.content) const instance = new LayerVueExtend({ data: options }) instance.id = id instance.vm = instance.$mount() self.instances[id] = { inst: instance, type: options.type } document.body.appendChild(instance.vm.$el) instance.init() self.instancesVue[id] = { mask: '', main: instance.vm, iframe: '' } return id } /** * 关闭一个弹窗 * @param {[type]} id [description] * @return {[type]} [description] */ self.close = function (id) { console.log(id) const oElm = document.getElementById(id).parentElement if (oElm) { document.body.removeChild(oElm) delete self.instances[id] self.instancesVue[id].main.$destroy() if (self.instancesVue[id].iframe !== '') { const minindex = parseInt(self.instancesVue[id].main.$el.getAttribute('minindex') || -2) if (minindex > -1) { self.iframeMinList[minindex] = -1 } self.instancesVue[id].iframe.$destroy() } // 取消隐藏滚动条 if (!self.instancesVue[id].main.scrollbar) { let scrollbarCount = 0 for (const key in self.instancesVue) { if (!self.instancesVue[key].main.scrollbar) { scrollbarCount++ } } if (scrollbarCount === 1) { const htmlDom = document.getElementsByTagName('html')[0] htmlDom.style.marginRight = 'auto' htmlDom.classList.remove('vl-html-scrollbar-hidden') } } // 控制遮罩,删除掉当前的遮罩 if (self.instancesVue[id].main.shade) { const layerMask = document.getElementById(id + '_mask') const maskId = id + '_mask' document.body.removeChild(layerMask) if (self.instancesVue[maskId]) { self.instancesVue[maskId].mask.$destroy() } } delete self.instancesVue[id] } else { setTimeout(function () { const oElm = document.getElementById(id) if (oElm) { document.body.removeChild(oElm) delete self.instances[id] self.instancesVue[id].main.$destroy() if (self.instancesVue[id].iframe !== '') { self.instancesVue[id].iframe.$destroy() } } }, 200) } } /** * 关闭一个弹窗 * @param {[type]} id [description] * @return {[type]} [description] */ self.closeAll = function (type = -1) { const types = { page: 0, iframe: 1 } if (type === -1) { for (const k in self.instances) { self.close(k) } } else { const targetType = types[type] for (const k in self.instances) { if (self.instances[k].type === targetType) { self.close(k) } } } } /** * 手动最大化 */ self.full = function (id = '') { document.querySelector('#' + id + ' .lv-icon-max').click() } /** * 手动最小化 */ self.min = function (id = '') { document.querySelector('#' + id + ' .lv-icon-mini').click() } /** * 手动最小化 */ self.restore = function (id = '') { document.querySelector('#' + id + ' .lv-icon-huanyuan').click() } /** * 合并json * @method mergeJson * @param {[type]} optons [description] * @param {[type]} def [description] * @return {[type]} [description] */ function mergeJson (options, def) { for (const key in def) { if (options[key] === undefined) { options[key] = def[key] } } return options } return self } // module.exports = Notification; export default Layer