hhweb.js 13.1 KB
/**
 * 通用工具库
 */
layui.define(['form', 'I18nPage', 'element', 'vue', 'layer', 'laydate', 'jquery', 'table'], function (exports) {
    var form = layui.form,
        layer = layui.layer,
        element = layui.element,
        laydate = layui.laydate,
        $ = layui.jquery,
        I18nPage = layui.I18nPage,
        table = layui.table;

    var Lang = I18nPage.GetLang();
    var _lang = "cn";
    if (Lang !== "cn") {
        _lang = "en";
    }

    var DeleteSome = "至少选择一条记录", DeleteConfirm = "确定要删除所选信息", ErrorTitle = "错误信息",
        NumTitle = "请输入整数", NumDecTitle = "请输入数值" ;
    if (Lang !== "cn") {
        DeleteSome = "Please Select Some Data";
        DeleteConfirm = "Confirm To Delete";
        ErrorTitle = "Error Info";
        NumTitle = "Please Input Int";
        NumDecTitle = "Please Input Numeric";
    }

    //字符串常量
    var MOD_NAME = 'hhweb',
        THIS = 'layui-this',
        SHOW = 'layui-show',
        HIDE = 'layui-hide',
        DISABLED = 'layui-disabled';

    //外部接口
    var hhweb = {
        config: {} //全局配置项
        //设置全局项
        , set: function (options) {
            var that = this;
            that.config = $.extend({}, that.config, options);
            return that;
        }
        //事件监听
        , on: function (events, callback) {
            return layui.onevent.call(this, MOD_NAME, events, callback);
        }
        //删除
        , del: function (url, dataids, callback) {
            if (dataids == undefined || dataids == "" || dataids.length == 0) {
                layer.alert(DeleteSome, { icon: 5, shadeClose: true, title: ErrorTitle });
                return;
            }
            var title = "信息";
            if (Lang !== "cn") {
                title = "Message";
            }
            layer.confirm(DeleteConfirm, { icon: 3, title: title }, function (index) {
                $.post(url, { ids: dataids },
                    function (data) {
                        if (data.Code == 200) {
                            if (callback != undefined) callback();
                        } else {
                            layer.msg(data.Message);
                        }
                    }, "json");
                layer.close(index);
            });
        }
        //正则表达式验证输入合法性
        , ValidTextVal(obj, type, msg) {
            if (type == "NumText") {
                var oldlength = $(obj).val().length;
                $(obj).val($(obj).val().replace(/\D/g, ''));
                if ($(obj).val().length != oldlength) {
                    layer.msg(msg)
                }
            }
            if (type == "NumDecText") {
                var oldlength = $(obj).val().length;
                $(obj).val($(obj).val().replace(/[^0-9.]/g, ''));
                if ($(obj).val().length != oldlength) {
                    layer.msg(msg)
                }
            }
        }
        //元素可用
        , DomEnable(dom) {
            $(dom).removeAttr("disabled");
            $(dom).css("background-color", "white");
        }
        //元素禁用
        , DomDisable(dom) {
            $(dom).attr({ disabled: "disabled" });
        }
        //元素禁用样式2
        , DomDisableImportant(dom) {
            $(dom).attr({ disabled: "disabled" });
            $(dom).css("background-color", "#eee");
        }
        //验证必填项
        , CheckRequired(filterId, func) {
            var filter = ".required";
            if (func != undefined) {
                filter = filter + func;
            }

            if (filterId != undefined) {
                filter = filterId + " " + filter;
            }
            var rtn = null;
            var required = $(filter);
            required.each(function () {
                if ($(this).parent().parent().css("display") == "block") {
                    if ($(this).val() == null || $(this).val().replace(/^\s+|\s+$/g, "") == "") {
                        rtn = $(this);
                        return false;
                    }
                }
            });
            return rtn;
        }
        //元素闪烁
        , blink(selector) {
            $(selector).fadeOut('normal', function () {
                $(this).fadeIn('normal', function () {
                    blink(this);
                });
            });
        }
        //获取缓存中当前用户表格字段的设置
        , ColumnSetting(tablelid, cols_arr) {
            var account = window.localStorage.getItem("Account");
            var paths = window.location.pathname.split("/");
            var controller = paths[paths.length - 2];
            var cookieName = account + "_" + controller + "_" + tablelid;
            var cookieval = window.localStorage.getItem(cookieName);
            var colsetting = {};
            if (cookieval !== undefined) {
                colsetting = JSON.parse(cookieval);
            }

            cols_arr[0].forEach(function (item, len) {
                if (colsetting != null) {
                    if (colsetting[item.field] !== undefined) {
                        item.hide = !(colsetting[item.field]);
                    }
                }
            });

            return cols_arr;
        }
        //渲染样式控件
        , InitUIClass() {
            //时间输入框
            $('.layui-date').each(function () {
                laydate.render({
                    elem: this
                    , btns: ['confirm']
                    , type: "datetime"
                    , lang: _lang
                    , done: function (value, date, endDate) {
                        try {
                            var Id = this.elem[0].attributes["name"].nodeValue;
                            var Expression = this.elem[0]['__v_model'].expression;

                            for (var item in hhweb.Config) {
                                var vueobj = item;
                                var vueclass = hhweb.Config[item];

                                if (vueobj === Id) {
                                    vueclass[Expression] = value;
                                }
                            }
                        } catch (e) {

                        }
                    }
                });
            });
            //数值输入框
            $(".NumText").keyup(function () {
                hhweb.ValidTextVal(this, "NumText", NumTitle);
            }).bind("paste", function () {  //CTR+V事件处理    
                hhweb.ValidTextVal(this, "NumText", NumTitle);
            }).css("ime-mode", "disabled"); //CSS设置输入法不可用 
            //小数输入框
            $(".NumDecText").keyup(function () {
                hhweb.ValidTextVal(this, "NumDecText", NumDecTitle);
            }).bind("paste", function () {  //CTR+V事件处理    
                hhweb.ValidTextVal(this, "NumDecText", NumDecTitle);
            }).css("ime-mode", "disabled"); //CSS设置输入法不可用    
            //所有可用输入框绑定事件回车跳转
            var input = $('input:text:not(:disabled)');
            input.each(function () {
                $(this).bind("keydown", function (e) {
                    var n = input.length;
                    if (e.which == 13) {
                        e.preventDefault(); //Skip default behavior of the enter key
                        if ($(this).val() != "") {
                            var nextIndex = input.index(this) + 1;
                            if (nextIndex < n) {
                                input[nextIndex].focus();
                            }
                            else {
                                input[nextIndex - 1].blur();
                            }
                        }
                        else {
                            $(this).focus();
                        }
                    }
                });
                //绑定获取焦点事件
                $(this).bind('focus', function (event) {
                    $(this).css("background-color", "yellow");
                    //$(this).val("");
                });
                //绑定失去焦点事件
                $(this).bind('blur', function (event) {
                    $(this).css("background-color", "white");
                });
            });
        }

        , RenderSwitch(VueConfig) {
            var vueEdit = VueConfig.vueEdit;
            var vueQuery = VueConfig.vueQuery;

            if (vueEdit !== undefined) {
                var vm = vueEdit.vm;
                var layfilter = vueEdit.layfilter;
                if (layfilter == undefined) {
                    layfilter = 'eform';
                }

                $("input:checkbox[lay-filter='" + layfilter + "']").each(function () {
                    if (vm.$data[this.name] !== undefined) {
                        if (vm.$data[this.name] !== null) {
                            $(this).prop("checked", vm.$data[this.name]);
                        }
                        else {
                            $(this).prop("checked", false);
                        }
                    }
                });
                form.render();
            }

            if (vueQuery !== undefined) {
                var vmq = vueQuery.vue;
                var layfilter = vueQuery.layfilter;
                if (layfilter == undefined) {
                    layfilter = 'qform';
                }

                $("input:checkbox[lay-filter='" + layfilter + "']").each(function () {
                    if (vmq.$data[this.name] !== undefined) {
                        if (vmq.$data[this.name] !== null) {
                            $(this).prop("checked", vmq.$data[this.name]);
                        }
                        else {
                            $(this).prop("checked", false);
                        }
                    }
                });
                form.render();
            }
        }
    }

    $(document).ready(function () {
        $(document).on('keydown', function (e) {
            //禁用页面F5刷新
            if (e.which == 116) {
                e.preventDefault(); //Skip default behavior of the enter key
            }

            //CTRL+ENTER 提交
            if (e.ctrlKey && e.keyCode == 13) {
                if ($(".layui-layer-btn0").length > 0) {
                    $(".layui-layer-btn0").click();
                }
            }
            //ESC 取消
            if (e.keyCode == 27) {
                if ($(".layui-layer-close1").length > 0) {
                    $(".layui-layer-close1").click();
                }
            }
        })

        //单击行勾选checkbox事件
        $(document).on("click", ".layui-table-body table.layui-table tbody tr", function () {
            var index = $(this).attr('data-index');
            var tableBox = $(this).parents('.layui-table-box');
            //存在固定列
            if (tableBox.find(".layui-table-fixed.layui-table-fixed-l").length > 0) {
                tableDiv = tableBox.find(".layui-table-fixed.layui-table-fixed-l");
            } else {
                tableDiv = tableBox.find(".layui-table-body.layui-table-main");
            }
            var checkCell = tableDiv.find("tr[data-index=" + index + "]").find("td div.laytable-cell-checkbox div.layui-form-checkbox I");
            if (checkCell.length > 0) {
                checkCell.click();
            }
        });

        $(document).on("click", "td div.laytable-cell-checkbox div.layui-form-checkbox", function (e) {
            e.stopPropagation();
        });

        form.on('checkbox()', function (data) {
            if (data.elem.name == "layTableCheckbox") {
                return null;
            }
            try {
                var attributes = data.elem.attributes;
                if (attributes['lay-filter'] !== undefined) {
                    if (attributes['lay-filter'].nodeValue == 'LAY_TABLE_TOOL_COLS') {
                        var tb = data.elem.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode;
                        var account = window.localStorage.getItem("Account");
                        var paths = window.location.pathname.split("/");
                        var controller = paths[paths.length - 2];
                        var tablelid = tb.previousElementSibling.id;
                        var checkflag = data.elem.checked;
                        var colname = attributes['name'].nodeValue;
                        var cookieName = account + "_" + controller + "_" + tablelid;
                        var cookieval = window.localStorage.getItem(cookieName);
                        var colsetting = {};
                        if (cookieval == undefined) {
                            colsetting[colname] = checkflag;
                        }
                        else {
                            colsetting = JSON.parse(cookieval);
                            colsetting[colname] = checkflag;
                        }
                        window.localStorage.setItem(cookieName, JSON.stringify(colsetting));
                    }
                }
            } catch (e) {
            }
        });
    });

    exports(MOD_NAME, hhweb);
});