MarkJane
4/12/2017 - 7:49 AM

定义工具类GF.js

//自己用的js工具类
var Validator = {  
    // 邮箱  
    isEmail : function(s) {  
        var p = "^[-!#$%&\'*+\\./0-9=?A-Z^_`a-z{|}~]+@[-!#$%&\'*+\\/0-9=?A-Z^_`a-z{|}~]+\.[-!#$%&\'*+\\./0-9=?A-Z^_`a-z{|}~]+$";  
        return this.test(s, p);  
    },  

    // 手机号码  
    isMobile : function(s) {  
        return this.test(s, /^(180|189|133|134|153|181)\d{8}$/);  
    },  

    // 电话号码  
    isPhone : function(s) {  
        return this.test(s, /^[0-9]{3,4}\-[0-9]{7,8}$/);  
    },  

    // 邮编  
    isPostCode : function(s) {  
        return this.test(s, /^[1-9][0-9]{5}$/);  
    },  

    // 数字  
    isNumber : function(s, d) {  
        return !isNaN(s.nodeType == 1 ? s.value : s)  
                && (!d || !this.test(s, '^-?[0-9]*\\.[0-9]*$'));  
    },  

    // 判断是否为空  
    isEmpty : function(s) {  
        return !jQuery.isEmptyObject(s);  
    },  

    // 正则匹配  
    test : function(s, p) {  
        s = s.nodeType == 1 ? s.value : s;  
        return new RegExp(p).test(s);  
    }  
};
var appClass = function (baseurl) {
	var _self = this;
	_self.baseurl = baseurl;
	_self.dialog_title = "";
	_self.dialog_body = "";
	_self.dialog_button = "";
	_self.gourl = function (url,stime){
		setTimeout(function(){window.location.href=url;},stime*1000);
	},
	_self.refresh = function (stime) {
	    if (!stime) stime = 2000;
	    setTimeout(function() {
	        window.location.reload();
	    },stime);
	},
	_self.swal_http = function (tip, http, url, callback, data){
		if (!data) data = '';
	    swal({
	            title: "您确定?",
	            text: tip,
	            type: "warning",
	            showCancelButton: true,
	            confirmButtonColor: '#DD6B55',
	            confirmButtonText: '是',
	            cancelButtonText: "否",
	            closeOnConfirm: false,
	        },
	        function(isConfirm) {
	            if (isConfirm) {
	            	http == 'get' && http_get(url, callback);
	            	http == 'post' && http_post(url, data, callback);
	            }
	        }
	    );
	},
	_self.http_post = function (url,data,callback,ctype) {
		$.post(url, data, function(data) {
			this.http_callback(data,callback,ctype);
	    });
	},
	_self.http_get = function (url,callback,ctype) {
		$.get(url, function(data,callback,ctype) {
	        this.http_callback(data,callback,ctype);
	    });
	},
	_self.http_ajax = function (){

	},
	_self.http_callback = function (data,callback,ctype){
		data = $.$.parseJSON(data);
		if(ctype == 1 && callback){
			callback(data);
		}else{
			var msg = data.msg ? data.msg : (data.status == 1 ? "操作成功!" : "操作失败! :)");
			if(data.status == 1){
 				if(callback){
 					swal({ title:"Success!", text: "操作成功!", type: "success"}, function(){
 						callback();
 					});
 				}else{
 					swal("Success!", "操作成功!", "success");
 				}
			}else{
				 swal("Error!", "操作失败! :)", "error");
			}
		}
	},
	_self.clearForm = function (form,sindex) {
	    form = arguments[0] ? arguments[0] : $('#modal form');
	    // iterate over all of the inputs for the form
	    // element that was passed in
	    $(':input', form).each(function() {
	        var type = this.type;
	        var tag = this.tagName.toLowerCase(); // normalize case
	        // it's ok to reset the value attr of text inputs,
	        // password inputs, and textareas
	        if (type == 'text' || type == 'password' || tag == 'textarea')
	            this.value = "";
	        // checkboxes and radios need to have their checked state cleared
	        // but should *not* have their 'value' changed
	        else if (type == 'checkbox' || type == 'radio')
	            this.checked = false;
	        // select elements need to have their 'selectedIndex' property set to -1
	        // (this works for both single and multiple select elements)
	        else if (tag == 'select')
	            sindex ? this.selectedIndex = 0 : this.selectedIndex = sindex;
	    });
	},
	_self.dialog = function (){

	}
	_self.getDialogHtml = function(){
		var html = "<div class=\"modal fade\" id=\"modal\" tabindex=\"-1\" role=\"dialog\" aria-labelledby=\"modalLabel\">" +
		        "<div class=\"modal-dialog\" role=\"document\">" +
		        "<div class=\"modal-content\">" +
		        "<div class=\"modal-header\">" +
		        "<button type=\"button\" class=\"close\" data-dismiss=\"modal\" aria-label=\"Close\"><span aria-hidden=\"true\">&times;</span></button>" +
		        "<h4 class=\"modal-title\" id=\"mLabel\">" + _self.dialog_title + "</h4>" +
		        "</div>"+
		        "<div class=\"modal-body\">" +
		        _self.dialog_body+
		        "</div>" +
		        "<div class=\"modal-footer\">" +
		        "<button type=\"button\" class=\"btn btn-default\" data-dismiss=\"modal\">关闭</button>" +
		        _self.dialog_button+
		        "</div>" +
		        "</div>" +
		        "</div>" +
		        "</div>";
	    return html;
	},
}

var GF = new appClass();

var autoComplete = function(formObj,data) {
    //formObj = arguments[0] ? arguments[0] : $('#modal form');
    //console.log(data,formObj);
    var _self = this;
    _self.data = (typeof(data) == 'object') ? data : (data.length ? eval('(' + data + ')') : '');
    _self.formObj = formObj;

    /**
     * @brief 构造函数
     */
    this.__construct = function() {
        if (typeof(_self.data) != 'object') {
            return false;
        }

        _self.init();
    }

    /**
     * @brief 填充内容
     */
    _self.init = function(data) {
        (typeof(data) == 'undefined') && (data = _self.data);
        for (var i in data) {
            if (typeof(data[i]) == 'object') {
                _self.init(data[i]);
            } else {
                var obj = $('[name="' + i + '"]');
                var tag_name = '';
                if (obj[0]) {
                    tag_name = obj[0].tagName.toLowerCase();
                }
                var tag_type = obj.attr('type');

                switch (tag_type) {
                    case 'radio':
                    case 'checkbox':
                        obj.each(function() {
                            if ($(this).val() == data[i]) {
                                $(this).prop('checked', true);
                            } else {
                                $(this).prop('checked', false);
                            }
                        });
                        break;
                    case 'img':
                        obj.attr('src', data[i]);
                        break;
                    default:
                        obj.val(data[i].toString().replace(/amp;/g, ''));
                        break;
                }
            }
        }
    }

    _self.__construct();
}