JS 通用 GMFP.JS

(function ($) {
    $.fn.outerHTML = function (s) {
        return (s)
            ? this.before(s).remove()
            : $("<div>").append(this.eq(0).clone()).html();
    };

    $.Gmfp = {};

    /**
    * 发送get请求
    * strUrl, oParams, funCallback
    */
    $.Gmfp.AjaxGet = function (strUrl, oParams, funCallback) {
        $.get(strUrl, oParams, funCallback);
    };

    /**
    * 发送post请求
    * strUrl, oParams, funSuccessCallback, funErrorCallback
    */
    $.Gmfp.AjaxPost = function (strUrl, oParams, funSuccessCallback, funErrorCallback) {
        var strJsonParams = $.toJSON(oParams);
        $.ajax({
            type: "POST",url: strUrl,
            data: strJsonParams,
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: funSuccessCallback,
            error: funErrorCallback
        });
    };

    /**
    * 生成随机的guid
    */
    $.Gmfp.NewGuid = function () {
        var guid = "";
        for (var i = 1; i <= 32; i++) {
            var n = Math.floor(Math.random() * 16.0).toString(16);
            guid += n;
            if ((i == 8) || (i == 12) || (i == 16) || (i == 20))
                guid += "-";
        }
        return guid;
    };


    /**
    * HtmlEncode
    */
    $.Gmfp.HtmlEncode = function (text) {
        return text.replace(/&/g, '&amp;').replace(/\ "/g, '&quot;').replace(/</g, '&lt;').replace(/>/g, '&gt;');
    };

    /**
    * HtmlDecode
    */
    $.Gmfp.HtmlDecode = function (text) {
        return text.replace(/&amp;/g, '&').replace(/&quot;/g, '\" ').replace(/&lt;/g, '<').replace(/&gt;/g, '>');
    };

    /**
    * 注意,以后JS代码中如果遇到回调,可以在对象中通过oTag传递数据对象!!!!
    * 给JQuery对象设置oTag属性
    * oItem, oTag
    * oItem是Element对象
    */
    $.Gmfp.SetData = function (oItem, oTag) {
        $(oItem).data("oTag", oTag);
    };
    /**
    * 从JQuery对象中获取oTag属性
    * oItem, oTag
    * oItem是Element对象
    */
    $.Gmfp.GetData = function (oItem) {
        return $(oItem).data("oTag");
    };

    /**
    * 播放FLASH
    * strDivid, url
    */
    $.Gmfp.Flash = function (strDivid, url) {
        $('#' + strDivid).flash({ src: url, width: 1, height: 1 });
    };
    /**
    * 停止FLASH
    * strDivid, url
    */
    $.Gmfp.FlashStop = function (strDivid) {
        $('#' + strDivid).html('');
    };

    /**
    * 显示消息框
    * strMsg,
    * strType: "success","notice","warning","error"
    */
    $.Gmfp.ShowMsg = function (strMsg, strType, iStayTime) {
        $().toastmessage('showToast', {
            text: strMsg,
            stayTime: iStayTime || 3000,
            //fixed: false,
            //modal: false,
            //sticky: true,
            position: 'top-center',
            type: strType
        });
    };

    /**
    * 显示不关闭的消息框,返回提示框句柄
    * strMsg,
    * strType: "success","notice","warning","error"
    */
    $.Gmfp.ShowStickyMsg = function (strMsg, strType) {
        return $().toastmessage('showToast', {
            text: strMsg,
            //stayTime: iStayTime || 3000,
            //fixed: false,
            //modal: false,
            sticky: true,
            position: 'top-center',
            type: strType
        });
    };

    /**
    * 关闭消息弹出框
    * strMsg
    */
    $.Gmfp.CloseStickyMsg = function (toastItemInner, localSettings) {
        $().toastmessage('removeToast', toastItemInner, localSettings);
    };
    /**
    * 改变消息弹出框文字信息
    * strMsg
    */
    $.Gmfp.ChangeStickyMsg = function (toastItemInner, text) {
        $().toastmessage('changeToast', toastItemInner, text);
    };
    /**
    * 显示消息弹出框
    * strMsg
    */
    $.Gmfp.ShowMsgDialog = function (strMsg) {
        var options = {
            title: "提示",
            show: true,
            unloadOnHide: true,
            modal: true,
            closeable: true
        };
        var oTextArea = $("<textarea>").css({
            width: "400px",
            height: "300px"
        }).html(strMsg);
        var oBody = $("<div>").css("display", "inline").append(oTextArea);
        new Boxy(oBody, options, true);
    };

    /**
    * 时间控件    
    */
    $.Gmfp.Date = function (strId, strDateFormat, bClear, strMinDate, strMaxDate, Changing) {
        if (strDateFormat == undefined) {
            strDateFormat = "yyyy-MM-dd";
        }
        if (bClear == undefined) {
            bClear = true;
        }
        if (strMinDate == undefined) {
            strMinDate = "1900-01-01";
        }
        if (strMaxDate == undefined) {
            strMaxDate = "2099-12-31";
        }
        if (Changing == undefined) {
            Changing = function () { };
        }

        if (typeof (strId) == 'string') {
            strId = $("#" + strId);
        }

        strId.attr("class", "Wdate").focus(function () {

            WdatePicker({ dateFmt: strDateFormat, isShowClear: bClear, minDate: strMinDate,
                maxDate: strMaxDate, readOnly: true, onpicked: Changing
            });

        });
    };

    /**
    * 注册回车事件
    * oJqEle, JQ对象
    * oCallback: 回调方法
    */
    $.Gmfp.Regist13Key = function (oJqEle, oCallback) {
        oJqEle.keydown(function (e) {
            if (e.keyCode == 13) {
                if (oCallback != undefined) {
                    oCallback();
                }
            }
        });
    };

    /**
    * 加载SELECT
    */
    $.Gmfp.InitSelect = function (oSelect, arrOption, strKeyField, strValueField) {
        //复制一个select元素(因为IE创建SELECT的BUG所以要这么做,我也不想的...)
        var oSel = $(oSelect.outerHTML());

        for (var i = 0; i < arrOption.length; i++) {
            var option = arrOption[i];
            if (typeof (option) != "object") {
                option = $.Gmfp.HtmlEncode(option);
                $("<option>").attr('value', option).html(option).appendTo(oSel);
            }
            else {
                option[strValueField] = $.Gmfp.HtmlEncode(option[strValueField]);
                $("<option>").attr('value', option[strKeyField]).html(option[strValueField]).appendTo(oSel);
            }
        }
        oSelect.after(oSel);
        oSelect.remove();
    };

    /**
    * 上传功能, AjaxUpload
    strSrcId: 触发上传的控件的(Jquery对象)
    strAction: 接收文件的后台接口
    onComplete: 完成上传后的回调方法 function (file, response) {}
    onSubmit: 开始上传的回调方法,返回false可以取消上传 function (file, ext) {}  
    */
    $.Gmfp.AjaxUpload = function (oJqSrc, strAction, onComplete, onSubmit) {
        new AjaxUpload(oJqSrc, {
            action: strAction,
            name: 'uploadfile',
            onSubmit: onSubmit == undefined ? function () { } : onSubmit,
            onComplete: onComplete == undefined ? function () { } : onComplete
        });
    };

    /**
    * 确认框
    strMsg: 提示信息
    oCallback: 确认后的回调方法
    */
    $.Gmfp.Confirm = function (strMsg, oCallback) {
        Boxy.confirm(strMsg, oCallback, {
            title: "提示信息"
        });
    };
    /**
    * 确认框
    strMsg: 提示信息
    oCallback: 确认后的回调方法
    */
    $.Gmfp.FishConfirm = function (strMsg, oCallback) {
        var box = Boxy.fishmadeinchina(strMsg, oCallback, {
            title: "提示信息"
        });
        return box;
    };
    /**
    * 打开新页面
    url 新页面地址
    */
    $.Gmfp.OpenNewPage = function (url, param, strNewOpen) {
        if (param != undefined && typeof (param) == "object") {
            param = $.param(param);
            url = url + "?" + param;
        }
        var _target = "_blant";
        if (strNewOpen != undefined) {
            _target = "_parent";
        }
        if ($.browser.webkit) {
            window.location = url;
        }
        else {
            var oA = document.createElement('a');
            oA.href = url;
            oA.target = _target;
            oA.style.display = 'none';
            document.body.appendChild(oA);
            oA.click();
            document.body.removeChild(oA);
        }
    };

    /**
    * 获取Url里面的get提交参数
    */
    $.Gmfp.GetUrlParams = function () {
        var strRawUrl = decodeURI(window.location);
        var regex = /^[^&]+?[?]([\s\S]*)$/g;
        var matches = regex.exec(strRawUrl);
        if (matches.length >= 2) {
            var strParams = matches[1];
            regex = /([^&=]+)=([^&=]*)/g;
            var resObj = {};
            while (matches = regex.exec(strParams)) {
                resObj[matches[1]] = matches[2];
            }
            return resObj;
        }
        else {
            return {};
        }
    }

    /**
    * 为oText设置默认Text和颜色
    */
    $.Gmfp.ToSearchText = function (oText, strDefaultText, strDefaultColor) {
        //οnfοcus="if(this._d == undefined || this._d == true){  this.style.color='#000';value=''; this._d = false;}"
        //οnblur="if(value==''){this.style.color='@color';value='@defaultText';this._d = true;}"
        oText.focus(function () {
            if (this._d == undefined || this._d == true) {
                this.style.color = '#000';
                this.value = '';
                this._d = false;
            }
        });
        var onblur = function () {
            if (this.value == '') {
                this.style.color = strDefaultColor;
                this.value = strDefaultText;
                this._d = true;
            }
        };
        oText.blur(onblur);
        //onblur.call(oText.get(0));
        var _text = oText.get(0);
        if (_text.value == '' || _text.value == strDefaultText) {
            _text.style.color = strDefaultColor;
            _text.value = strDefaultText;
            _text._d = true;
        }
    }

    /**
    *    导航栏
    */
    $.Gmfp.NavMenu = function (oNavMenu, Current) {
        //        oNavMenu.each(function (i, item) {
        //            $(item).mouseover(function () {
        //                $(this).find("ul").css("display", "block");
        //            });
        //            $(item).mouseout(function () {
        //                $(this).find("ul").css("display", "none");
        //            })
        //        });
        $.each(oNavMenu, function (i, item) {
            $(item).mouseover(function () {
                $(this).find("ul").css("display", "block");
            });
            $(item).mouseout(function () {
                $(this).find("ul").css("display", "none");
            });
            //设置当前页
            var current = $(item).find("a[current]").first();
            var reg = new RegExp(Current);
            if (reg.test(current.attr("current"))) {
                current.attr("id", "current");
            }
        });
    }

    //回到顶部
    $.Gmfp.Top = function (oTopBtn) {
        var _this = this;

        window.onscroll = function () { _this.GetScrollTop() > 0 ? oTopBtn.fadeIn("normal") : oTopBtn.fadeOut("normal"); }
        oTopBtn.click(function () {
            var scrollMove = function () {
                _this.SetScrollTop(_this.GetScrollTop() / 1.1);
                if (_this.GetScrollTop() < 1) window.clearInterval(goTop);
            }
            var goTop = setInterval(scrollMove, 10);
        });

    }
    //获取滚动条的值
    $.Gmfp.GetScrollTop = function () {
        return document.body.scrollTop || document.documentElement.scrollTop;
    }
    //设置滚动条的值
    $.Gmfp.SetScrollTop = function (value) {
        if (document.body.scrollTop) {
            document.body.scrollTop = value;
        }
        else {
            document.documentElement.scrollTop = value;
        }
    }

    //格式化字符串
    $.Gmfp.Format = function (source, params) {
        if (arguments.length == 1)
            return function () {
                var args = $.makeArray(arguments);
                args.unshift(source);
                return $.format.apply(this, args);
            };
        if (arguments.length > 2 && params.constructor != Array) {
            params = $.makeArray(arguments).slice(1);
        }
        if (params.constructor != Array) {
            params = [params];
        }
        $.each(params, function (i, n) {
            source = source.replace(new RegExp("\\{" + i + "\\}", "g"), n);
        });
        return source;
    }

    //son对象转字符串形式
    $.Gmfp.json2str = function (o) {
        var arr = [];
        var fmt = function (s) {
            if (typeof s == 'object' && s != null) return json2str(s);
            return /^(string|number)$/.test(typeof s) ? "'" + s + "'" : s;
        }
        for (var i in o) {
            arr.push("'" + i + "':" + fmt(o[i]));
        }

        return '{' + arr.join(',') + '}';
    }


})(jQuery);


转载于:https://www.cnblogs.com/zhanlongjie/archive/2013/05/03/3056973.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值