168 lines
4.3 KiB
JavaScript
168 lines
4.3 KiB
JavaScript
"use strict";
|
|
|
|
const b = document;
|
|
const c = "querySelectorAll";
|
|
const d = "getElementsByClassName";
|
|
|
|
const e = function(a) {
|
|
return b[c](a);
|
|
};
|
|
|
|
const f = {
|
|
type: 0,
|
|
shade: true,
|
|
shadeClose: true,
|
|
fixed: true,
|
|
anim: "scale"
|
|
};
|
|
|
|
const g = {
|
|
extend: function(a) {
|
|
const b = JSON.parse(JSON.stringify(f));
|
|
for (const c in a) {
|
|
b[c] = a[c];
|
|
}
|
|
return b;
|
|
},
|
|
timer: {},
|
|
end: {}
|
|
};
|
|
|
|
g.touch = function(a, b) {
|
|
a.addEventListener("click", function(a) {
|
|
b.call(this, a);
|
|
}, false);
|
|
};
|
|
|
|
let h = 0;
|
|
const i = ["layui-m-layer"];
|
|
|
|
const j = function(a) {
|
|
const b = this;
|
|
b.config = g.extend(a);
|
|
b.view();
|
|
};
|
|
|
|
j.prototype.view = function() {
|
|
const a = this;
|
|
const c = a.config;
|
|
const f = b.createElement("div");
|
|
a.id = f.id = i[0] + h;
|
|
f.setAttribute("class", i[0] + " " + i[0] + (c.type || 0));
|
|
f.setAttribute("index", h);
|
|
|
|
const g = function() {
|
|
const a = typeof c.title === "object";
|
|
return c.title ? `<h3 style="${a ? c.title[1] : ''}">${a ? c.title[0] : c.title}</h3>` : "";
|
|
}();
|
|
|
|
const j = function() {
|
|
if (typeof c.btn === "string") c.btn = [c.btn];
|
|
let a;
|
|
const b = (c.btn || []).length;
|
|
if (b !== 0 && c.btn) {
|
|
a = `<span yes type="1">${c.btn[0]}</span>`;
|
|
if (b === 2) a = `<span no type="0">${c.btn[1]}</span>${a}`;
|
|
return `<div class="layui-m-layerbtn">${a}</div>`;
|
|
}
|
|
return "";
|
|
}();
|
|
|
|
if (!c.fixed) {
|
|
c.top = c.hasOwnProperty("top") ? c.top : 100;
|
|
c.style = c.style || "";
|
|
c.style += ` top:${b.body.scrollTop + c.top}px`;
|
|
}
|
|
|
|
if (c.type === 2) {
|
|
c.content = `<i></i><i class="layui-m-layerload"></i><i></i><p>${c.content || ''}</p>`;
|
|
}
|
|
|
|
if (c.skin) c.anim = "up";
|
|
if (c.skin === "msg") c.shade = false;
|
|
|
|
f.innerHTML = `${c.shade ? `<div ${typeof c.shade === 'string' ? `style="${c.shade}"` : ''} class="layui-m-layershade"></div>` : ''}` +
|
|
`<div class="layui-m-layermain" ${c.fixed ? '' : 'style="position:static;"'}>` +
|
|
`<div class="layui-m-layersection">` +
|
|
`<div class="layui-m-layerchild ${c.skin ? `layui-m-layer-${c.skin} ` : ''}${c.className ? c.className : ''} ${c.anim ? `layui-m-anim-${c.anim}` : ''}" ${c.style ? `style="${c.style}"` : ''}>` +
|
|
`${g}<div class="layui-m-layercont">${c.content}</div>${j}</div></div></div>`;
|
|
|
|
if (!c.type || c.type === 2) {
|
|
const k = b[d](i[0] + c.type);
|
|
const l = k.length;
|
|
if (l >= 1) layer.close(k[0].getAttribute("index"));
|
|
}
|
|
|
|
document.body.appendChild(f);
|
|
const m = a.elem = e(`#${a.id}`)[0];
|
|
if (c.success) c.success(m);
|
|
a.index = h++;
|
|
a.action(c, m);
|
|
};
|
|
|
|
j.prototype.action = function(a, b) {
|
|
const c = this;
|
|
if (a.time) {
|
|
g.timer[c.index] = setTimeout(function() {
|
|
layer.close(c.index);
|
|
}, 1000 * a.time);
|
|
}
|
|
|
|
const e = function() {
|
|
const b = this.getAttribute("type");
|
|
if (b == 0) {
|
|
if (a.no) a.no();
|
|
layer.close(c.index);
|
|
} else {
|
|
if (a.yes) a.yes(c.index);
|
|
else layer.close(c.index);
|
|
}
|
|
};
|
|
|
|
if (a.btn) {
|
|
const f = b[d]("layui-m-layerbtn")[0].children;
|
|
const h = f.length;
|
|
for (let i = 0; i < h; i++) {
|
|
g.touch(f[i], e);
|
|
}
|
|
}
|
|
|
|
if (a.shade && a.shadeClose) {
|
|
const j = b[d]("layui-m-layershade")[0];
|
|
g.touch(j, function() {
|
|
layer.close(c.index, a.end);
|
|
});
|
|
}
|
|
|
|
if (a.end) g.end[c.index] = a.end;
|
|
};
|
|
|
|
const layer = {
|
|
v: "2.0",
|
|
index: h,
|
|
open: function(a) {
|
|
const b = new j(a || {});
|
|
return b.index;
|
|
},
|
|
close: function(a) {
|
|
const c = e(`#${i[0]}${a}`)[0];
|
|
if (c) {
|
|
c.innerHTML = "";
|
|
b.body.removeChild(c);
|
|
clearTimeout(g.timer[a]);
|
|
delete g.timer[a];
|
|
if (typeof g.end[a] === 'function') g.end[a]();
|
|
delete g.end[a];
|
|
}
|
|
},
|
|
closeAll: function() {
|
|
const a = b[d](i[0]);
|
|
const c = a.length;
|
|
for (let e = 0; e < c; e++) {
|
|
layer.close(0 | a[0].getAttribute("index"));
|
|
}
|
|
}
|
|
};
|
|
|
|
export default layer;
|