1. 程式人生 > >jquery二次開發之擴充套件物件基元

jquery二次開發之擴充套件物件基元

 

(function (window, $, undefined) {

    var _Core = function () {
        var eventarr = [];
        var _OnPageLoad = undefined;
        ///獲取USER資訊
        var getUser = function () {
            //var obj = {};
            //obj.USERID = $.cookie(COOKIE_USER);
            var cookieData = $.cookie(COOKIE_USER);
            if (cookieData === undefined) {
                $Core.TimeOut();
                return {};
            }
            var cookieuser;
            try {
                cookieuser = $.parseJSON(cookieData);
            } catch (e) {
                $Core.TimeOut();
                //window.location.href = TIMEOUT_PATH;
                return {};
            }

            if (cookieuser == undefined || cookieuser.USERID == undefined) {
                //每一登入資訊跳轉至登入頁面
                //alert("登入超時!");
                $Core.TimeOut();
                //window.location.href = TIMEOUT_PATH;
                return {};
            }
            return cookieuser;

        }
        //登入 設定使用者本地資訊
        this.Login = function (userinfo) {
            $.cookie(COOKIE_USER, JSON.stringify(userinfo), { json: true });

        };
        this.LogOut = function () {
            $.removeCookie(COOKIE_USER);
        };
        //對外訪問
        this.USER = function () {
            return getUser();
        };
        //服務地址
        this.SERVICEPATH = function () {
            return SERVICE_PATH;
        };
        this.SERVICEROOT = function () {
            return SERVICE_ROOT;
        }
        this.APPID = APP_ID;
        //驗證失敗,
        this.TimeOut = function () {
            //alert("身份驗證失敗,請重新登入!");
            $Core.LogOut();
            window.top.location.href = TIMEOUT_PATH;
        };
        //驗證服務是否可用
        this.ValidPage = function (rid) {

            var resid = rid || $Core.util.getResId();
            if (resid) {
                var isexists = window.top.RESLIST.contains({ resId: resid },
                    function (item1, item2) {
                        return item1.resId === item2.resId;
                    });
                if (!isexists) {
                    location.href = "/403.html";
                    return;
                }
            }
            //驗證
            //$.ajax({
            //    url: "WebForm1.aspx",
            //    data: { resid:rid },
            //    success: function (datas) {
            //        if (datas.state == -1) {
            //            //沒有許可權跳轉
            //        }
            //    }
            //});
        };
        /**
         * 獲取
         * @param {any} resid 頁面資源id
         * @param {any} func  回撥
         */
        this.ValidAction = function (resid, func) {
            var btns = $("[data-rescode]");
            btns.hide();
            var resbtns = $Core.util.getResActions();
            if (resbtns.length > 0);
            {
                $.each(resbtns,
                    function (i, n) {
                        $.each(btns,
                            function (j, btn) {
                                var jbtn = $(btn);
                                var btnRes = jbtn.data("rescode");
                                if (btnRes != undefined &&
                                    btnRes !== "" &&
                                    btnRes.toLowerCase() === n.resCode.toLowerCase()) {
                                    jbtn.show();
                                }

                            });
                    });
                btns.filter(":hidden").remove();
            }
        };

        this.ajax = jqueryAjax;
        this.IsAjaxTimeOut = false;

        var _loading = function () {
            this.AjaxLoadingStart = function () {
                $("#pageloadingright").fadeIn();
            };

            this.HideAjaxLoading = function () {
                $("#pageloadingright").fadeOut(500);
            };

            this.HidePageLoading = function () {
                $("#pageloading").fadeOut(500);
            }

            this.HideAll = function () {
                this.HideAjaxLoading();
                this.HidePageLoading();
            }

        };
        this.Loading = new _loading();
        /**
         * 頁面所有ajax加在完畢後執行
         * @param {function()} func 回撥函式
         */
        this.OnPageLoad = function (func) {
            if (func !== undefined)
                _OnPageLoad = func;
        };

        var pageLoad = function () {
            if (_OnPageLoad)
                _OnPageLoad();
        };
        $(function () {
            var _interval = setInterval(function () {
                //if
                if (!window.ajaxruning) {
                    clearInterval(_interval);
                    pageLoad();
                }
            }, 100);
            $Core.ValidPage();
            $Core.ValidAction();

        });
    };
    window.$Core = new _Core();

    var config = function () {
        return {
            //附件上傳單檔案大小限制
            WebUpload_fileSingleSizeLimit: 50 * 1024 * 1024,
            //ajax請求超時時間
            ajaxTimeout: 30000
        };
    };
    $Core.Config = new config();

    /******************************************UI****************************** */
    var _UI = function () {
        var _message = function () {
            var createAlert = function (type, msg, timeout) {
                if (timeout === undefined)
                    timeout = 2000;

                var div = $(
                    '<div class="cntenalert" >' +
                    '<div class="alert-icons"></div>' +
                    '<div class="alert-close">×</div>' +
                    '<div class="alert-msg"></div></div>');
                var winWidth = ($(window).width() - 200) / 2;
                div.css("left", winWidth);
                div.addClass(type);

                var close = div.find(".alert-close");
                var divmsg = div.find(".alert-msg");
                close.click(function () {
                    div.fadeOut(function () {
                        div.remove();
                    });
                });

                divmsg.text(msg);

                div.fadeIn(function () {

                });

                $("body").append(div);
                if (timeout > 0)
                    setTimeout(function () {
                        div.fadeOut(function () {
                            div.remove();
                        });
                    }, timeout);

            }
            /**
             * 成功提示
             * @param {any} msg
             */
            this.success = function (msg) {
                createAlert("alert", msg);
            }
            /**
             * 錯誤的提示
             * @param {any} msg
             */
            this.error = function (msg) {
                createAlert("alert-error", msg, 5000);
            }
            /**
             * 提醒
             * @param {any} msg
             */
            this.warning = function (msg) {
                createAlert("alert-warning", msg, 3000);
            }
            /**
             * 清除所有提示資訊
             * */
            this.clear = function () {
                $(".cntenalert").fadeOut(function () {
                    $(".cntenalert").remove();
                });
            }

        }
        this.message = new _message();
        /**
         * 開啟一個url 的Dialog 對話方塊
         * @param {any} id 指定一個名字
         * @param {any} url 頁面地址
         * @param {any} options Dialog 引數
         */
        this.openDialog = function (id, url, options) {
            var frameid = id + "_frame";
            var win = $("#" + id);
            if (url.indexOf("?") > -1)
                url = url + "&_r=" + Math.random();
            else
                url = url + "?_r=" + Math.random();
            var getframe = function (id, url) {
                var f = $('<iframe id="{0}" src="{1}" width="100%" height="100%" scrolling="no" frameborder="0" ></iframe>'.format(id, url));
                return f;
            };

            if (win.size() == 0) {
                var $frame = getframe(frameid, url);
                //$frame.height(options.height - 43);
                var defaultOptions = {
                    maximizable: true,
                    resizable: true,
                    closed: false,
                    cache: false,
                    modal: true,
                    constrain: true,
                    width: "80%",
                    height: "90%"
                };
                if (options) {
                    $.extend(defaultOptions, options,
                        {
                            onClose: function () {
                                win.remove();
                                if ($.isFunction(options.onClose)) {
                                    options.onClose.apply(this);
                                }
                            }
                        });
                }
                defaultOptions.content = $frame;

                var dd = $("<div id='" + id + "' style='overflow:hidden' />");
                return dd.dialog(defaultOptions);
            } else {
                $("#" + frameid).attr("src", url);
                return win.dialog("open");
            }

        }

        /**
         * 重新設定datagrid 排序狀態
         * @param {any} dg jquery物件
         */
        this.resetDataGridSort = function (dg, psotrName, psortOrder) {
            var opt = dg.datagrid('options');
            opt.sortName = psotrName || "";
            opt.sortOder = psortOrder || "";
            var panel = dg.datagrid("getPanel");
            var sortcell = panel.find(".datagrid-sort");
            $.each(sortcell, function (i, n) {
                var targer = $(n);
                targer.removeClass("datagrid-sort-asc datagrid-sort-desc");
            });
        }
    };
    $Core.UI = new _UI();

})(window, jQuery);

基元使用

 $Core.OnPageLoad(function () {
            $Core.Loading.HidePageLoading();
        });

                var url = "InstallApplyForm.html?s=" + Math.random();
                $Core.UI.openDialog("redwind", url + "&installApplyId=" + value +"&type=red",
                    {
                        title: "檢視【使用者裝置安裝申請】",
                        width: '80%',
                        height: '90%',
                        onClose: function () {
                        }
                    });