var topWin = (function (p, c) { while (p != c) { c = p p = p.parent } return c })(window.parent, window); /** * 初始化 Layui.Table */ $(document).ready(function () { topWin.hideLoadLayer(); //关闭加载层 layui.table.init("devTable", { skin: 'line', even: 'even', size: 'lg', text: { none: '' } }); initContentHeight(); initNoDataDefaultShow(); }); window.onbeforeunload = function () { topWin.showLoadLayer(); }; /** * 初始化主题内容高度 */ function initContentHeight() { if ($(".bottom-div").length == 0) { return; } $(".layui-anim-fadein").height(($("body").innerHeight() - 40) + "px"); $(".layui-anim-fadein").css("overflow", "auto"); } /** * 弹出层:ifarme * @param src 页面路径 * @param title 弹层标题 * @param width 宽度 * @param height 高度 */ function layAlertPage(src, title, w, h, reload, end) { topWin.showLoadLayer(); var param = { width: w.toString(), height: h.toString(), frameContentChangeFlag: false // 窗口内容改变标记,如改变为真,否则为假 }; // 处理百分比写法,比如:60% if (param.width && param.height) { if (param.width.indexOf("%") != -1) { var _width = param.width.replace("%", ""); param.width = $(top).width() * _width / 100; } if (param.height.indexOf("%") != -1) { var _height = param.height.replace("%", ""); param.height = $(top).height() * _height / 100; } } // 弹层宽度,缺省值960px,如为0,则适应屏幕宽度, 如open为假,则该参数无效。 var width = param.width == null ? 960 : param.width; if (width == 0) { width = $(top).width() - 10; } //弹层宽度若超过浏览器的宽度,改为浏览器的宽度 if (width > $(top).width()) { width = $(top).width(); } // 弹层高度,缺省值680px,如为0,则适应屏幕高度,如open为假,则该参数无效。 var height = param.height == null ? 680 : param.height; if (height == 0) { height = $(top).height() - 10; } //弹层高度若超过浏览器的高度,改为浏览器的高度 if (height > $(top).height()) { height = $(top).height(); } var index = topWin.layer.getFrameIndex(window.name); src += src.indexOf('?') == -1 ? ("?layerIndex=" + index) : ("&layerIndex=" + index); topWin.layer.open({ type: 2, title: title, fixed: false, anim: '5', isOutAnim: false, maxmin: false, resize: false, shade: [0.1, '#fff'], area: [width + 'px', height + 'px'], content: src, success: function (layero, index) { var win = layero[0].getElementsByTagName('iframe')[0].contentWindow; // 监听表单是否提交 win.$("form").on("submit", function () { param.frameContentChangeFlag = true; }) win.isLayer = true; win.layerIndex = index; win.parentWin = window; if (win.layerReady) { win.layerReady(); } }, end: function () { topWin.hideLoadLayer(); if (reload && param.frameContentChangeFlag) { location.reload(true); } if (end) { end(); } } }); } /** * 全屏弹层 * @param content 正文 * @param title 标题 */ function layAlertMaxPage(content) { var index = topWin.layer.open({ type: 1, isOutAnim: false, title: '全屏查看', offset: 't', content: content, area: ['100%', '100%'] }); parent.layer.iframeAuto(index); parent.layer.full(index); } function reqDevService(url) { location.href = url; } /* * 头部通知 * tip.green(msg,time); * msg为通知文字 * time为显示时间(毫秒),,默认3秒关闭 */ var tip = { success: function (msg, time) { suiTip(msg, "success", time); }, error: function (msg, time) { suiTip(msg, "error", time); }, warning: function (msg, time) { suiTip(msg, "warning", time); }, stop: function (msg, time) { suiTip(msg, "stop", time); }, notice: function (msg, time) { suiTip(msg, "notice", time); }, tips: function (msg, time) { suiTip(msg, "tips", time); }, info: function (msg, time) { suiTip(msg, "info", time) }, question: function (msg, time) { suiTip(msg, "question", time) } } function suiTip(msg, type, time) { topWin.tips(msg, type, time); } /** * 当前窗口内提示消息 */ var msg = function (msg) { layer.msg(msg, { anim: 1, offset: 't' }); } /** * 关闭当前窗体 */ function closeLayDialog() { var index = topWin.layer.getFrameIndex(window.name); //获取窗口索引 topWin.layer.close(index); } /** * 判断对象是否为空,为空返回true,不为空返回false * @param {Object} obj */ function isEmpty(obj) { if (typeof obj == "undefined" || obj == null || obj == "") { return true; } else { return false; } } /** * 通过正则表达式获取浏览器的参数 * @param name * @returns */ function getQueryString(name) { var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); var r = window.location.search.substr(1).match(reg); if (r != null) return unescape(r[2]); return null; } /** * 加密字符串 * @param {Object} str */ function compileStr(str) { var c = String.fromCharCode(str.charCodeAt(0) + str.length); for (var i = 1; i < str.length; i++) { c += String.fromCharCode(str.charCodeAt(i) + str.charCodeAt(i - 1)); } return escape(c); } /** * 解密字符串 */ function uncompileStr(str) { str = unescape(str); var c = String.fromCharCode(str.charCodeAt(0) - str.length); for (var i = 1; i < str.length; i++) { c += String.fromCharCode(str.charCodeAt(i) - c.charCodeAt(i - 1)); } return c; } /** * 初始化无数据时的默认显示 */ function initNoDataDefaultShow() { var defaultText = "(null)"; // LayTable var cellAry = $(".layui-table-body").find(".layui-table-cell"); for (var i = 0; i < cellAry.length; i++) { if (space.utils.isNull($(cellAry[i]).html())) { $(cellAry[i]).html(defaultText); } } // 查看 var tdAry = $(".sui-table").find("td"); for (var i = 0; i < tdAry.length; i++) { if (isNull($(tdAry[i]).html())) { $(tdAry[i]).html(defaultText); } } } function isNull(obj) { return (typeof obj == "undefined" || obj == null || obj.trim() == ""); } /** * 列表排序 */ space.plugins.sortable = { init: function () { new Sortable(sortable, { sort: true, group: 'shared', swapThreshold: 0.5, animation: 150, chosenClass: "sortable-chosen", // 选中时的DOM节点的class名称 dragClass: "sortable-drag", // 拖放时的DOM节点的class名称 onStart: function (evt) { // 拖拽开始 }, onEnd: function (evt) { // 拖拽结束 $("#sortNum li").removeClass("highlight"); }, onChoose: function ( /**Event*/ evt) { //点击选择 $("#sortNum li").removeClass("highlight"); $("#sortNum li").eq(evt.oldIndex).addClass("highlight"); }, onMove: function ( /**Event*/ evt, /**Event*/ originalEvent) { //拖拽移动 var numIndex = $(evt.related).index(); $("#sortNum li").removeClass("highlight"); $("#sortNum li").eq(numIndex).addClass("highlight"); } }); }, getData: function () { var dataIds = ""; var itemAry = $("#sortable").find("li"); for (var i = 0; i < itemAry.length; i++) { dataIds += $(itemAry[i]).attr("data-id") + ","; } return dataIds; } } function getStringAsEmpty(str) { return isEmpty(str) ? "" : str; } /** * 异步请求工具类 */ space.utils.ajax = function (setting) { if (setting.success) { console.error("success 函数由 callback 代替,请将 success 函数修改为 callback 后重试"); return; } var index = layer.load(2); setting.complete = function () { layer.close(index); } setting.error = function (xhr, status, error) { console.error(xhr); console.error(status); console.error(error); layer.msg("系统繁忙,请稍后再试"); if (setting.err) { setting.err(); layer.close(index); } }; setting.success = function (data) { try { if (data != undefined && data.code == "error") { layer.msg(data.msg); layer.close(index); } } catch (e) { } if (setting.callback) { setting.callback(data); } } try { $.ajax(setting); } catch (e) { console.error(e); layer.close(index); } } /** * 深复制一个对象 */ space.utils.cloneObj = function (obj) { var newObj = {}; if (obj instanceof Array) { newObj = []; } for (var key in obj) { var val = obj[key]; if (typeof val === 'undefined') { newObj[key] = "undefined"; continue; } // arguments.callee 在哪一个函数中运行,它就代表哪个函数, 一般用在匿名函数中。 newObj[key] = typeof val === 'object' ? space.utils.cloneObj(val) : val; } return newObj; } /** * 下载文件 * @param {*} fileName 文件名 * @param {*} content 文件内容 */ space.utils.download = function (fileName, content) { var eleLink = document.createElement('a'); eleLink.download = fileName; eleLink.style.display = 'none'; var blob = new Blob([content]); eleLink.href = URL.createObjectURL(blob); document.body.appendChild(eleLink); eleLink.click(); document.body.removeChild(eleLink); } /** * 对象转字符串,保留函数体 * @param {*} o */ space.utils.objToStr = function (o) { var r = []; if (typeof o == "string") return "\"" + o.replace(/([\'\"\\])/g, "\\$1").replace(/(\n)/g, "\\n").replace(/(\r)/g, "\\r").replace(/(\t)/g, "\\t") + "\""; if (typeof o == "object") { if (!o.sort) { for (var i in o) r.push(i + ":" + space.utils.objToStr(o[i])); if (!!document.all && !/^\n?function\s*toString\(\)\s*\{\n?\s*\[native code\]\n?\s*\}\n?\s*$/.test(o.toString)) { r.push("toString:" + o.toString.toString()); } r = "{" + r.join() + "}" } else { for (var i = 0; i < o.length; i++) r.push(space.utils.objToStr(o[i])) r = "[" + r.join() + "]" } return r; } return o.toString(); }