dialog.js 3.53 KB
/**
 * @fileOverview 弹出对话框组件
 * @author:Hbomb(zhouqq@yoho.cn)
 * @date:2012-08-28
 */
define('dialog', function(require, exports)
{
    var $ = require("jquery");
 

    //初始化
    var _init = function(opts)
    {
        /**
         * 配置
         * 
         */
        var opt = 
        {
            con:"",
            title: null,        //标题,如果需要显示标题,则传递标题名称即可
            buttons:'',
            noclose:false,
            id:null,
            alpha:30,
            zindex:9999
        };
        
        $.extend(opt, opts); 
        if(!opt.noclose)
        {
            exports.close();
        }
        var idstr = opt.id?('id="'+opt.id+'"'):"";
        var idmaskstr = opt.id?('id="'+opt.id+'mask"'):"";
        $("body").append('<div class="windows" '+idstr+'" style="position:absolute;z-index:'+(opt.zindex+1)+'">'+
                '<div class="rel_title">'+
                '<div class="rel_title_ico"><strong style="padding-left:24px;"></strong></div>'+
                '<a href="javascript:void(0)" class="rel_title_close"></a>'+
                '</div><div class="rel_content"></div></div>');
        $("body").append('<div class="d-mask" '+idmaskstr+' style="top:0;left:0;width:100%;position:absolute;z-index:'+opt.zindex+';background:#000;filter:alpha(opacity='+
                         opt.alpha+');opacity:'+(opt.alpha/100)+';position:absolute;"></div>');
        obj = opt.id?$("#"+opt.id):$(".windows");

        if(opt.width)
        {
            obj.width(opt.width);
        }
        if(opt.con)
        {
            obj.find(".rel_content").append(opt.con);
        }
        if(opt.buttons)
        {
            obj.append(opt.buttons);
        }
        //处理标题
        if(opt.title)
        {
        	var title = opts.title;
        	obj.find('.rel_title strong').text(title);
        } 

        obj.find(".rel_title_close").click(function()
        {
            exports.close(opt.id);
        });
        _setPos(obj);
        $(window).bind('resize',function(){_setPos(obj);});
    };
    
    exports.setPos = function(obj)
    {
        _setPos(obj);
    };
    
    exports.setMask = function(obj,eh)
    {
        var h = obj.offset().top+obj.height();
        h = eh?(h+eh):h;
        var height = h>$(document).height()?h:$(document).height();
        $(".d-mask").css(
        {
            "width":"100%",
            "height":height+"px",
            "top":"0",
            "left":"0"
        });
    };
    
    var _setPos = function(obj)
    {
        var w = $(window).width();
        var t = $(window).scrollTop();
        var h = $(window).height();
        var ww =  obj.width();
        var hh =  obj.height();
        var top = h/2-hh/2;
        top = top<0?0:top;
        obj.css(
        {
            "top":(top+t)+"px",
            "left":(w/2-ww/2)+"px"
        });
        if(!($.browser.msie&&$.browser.version=='6.0'))
        {
           // obj.css({"position":"fixed"});
        }
        else
        {
            obj.css(
            {
                "top":((h/2-hh/2)+t)+"px"
            });
        }
        exports.setMask(obj);
    };
    
    //开启对话框
    exports.open = function(opt)
    {
        _init(opt);
    };
    //关闭对话框
    exports.close = function(id)
    {
        if(id)
        {
            $("#"+id).remove();
            $("#"+id+"mask").remove();
        }
        else
        {
            $(".windows").remove();
            $(".d-mask").remove();
        }
        $(window).unbind('resize',_setPos);
    };
});