dialog.js 3.57 KB
/**
 * Created with IntelliJ IDEA.
 * User: Administrator
 * Date: 13-6-4
 * Time: 下午6:01
 * To change this template use File | Settings | File Templates.
 */
define(function(require, exports, module){
    var $ = require('jquery');
    //require('css/dialog.css');

    exports.open = function (opts){
        /*
        * 设置默认参数
        * @content 弹出层显示的内容
        * @title 弹出层标题
        * @id 弹出层id
        * @close 是否有关闭按钮
        * @alpha 是否有遮罩层
        * @zindex 弹出层显示的层级
        * */
        var opt = {
            content: '',
            id: '',
            close: false,
            alpha: 50,
            zindex: 9999,
            isSlider: false,
            sliderId: '',
            autoLayer: true
        };
        /*合并参数*/
        $.extend(opt, opts);

        var idStr = opt.id ? 'id="'+ opt.id +'"' : '';
        var maskStr = opt.id ? 'id="'+ opt.id +'mask"' : 'id="mask"';

        $('body').append('<div class="uiDialogLayer" '+ idStr +' style="z-index:'+ (opt.zindex+1) +'">' +
                '<a href="javascript:;" class="closeLayer"></a>' +
                '<div class="dialogContent"></div>' +
            '</div> ');

        if(opt.alpha){
            $('body').append('<div class="maskBackground" '+ maskStr +' style="z-index:'+ opt.zindex +';opacity:'+ opt.alpha/100 +'; filter:alpha(opacity:'+ opt.alpha +');"></div> ');
        }

        $('#'+opt.id+'mask').height($(document).height());

        var obj = opt.id ? $("#"+opt.id) : $('.uiDialogLayer');
        opt.width && obj.width(opt.width);
        opt.height && obj.height(opt.height);
        opt.content && obj.find('.dialogContent').append(opt.content);
        opt.buttons && obj.find('.dialogContent').append(opt.buttons);
        obj.find('.closeLayer').click(function(){
            exports.close(opt.id);
        });
        if(opt.autoLayer)
        {
            if($.browser.msie && $.browser.version == 6.0){
                _setPos(obj);

                $(window).bind('resize scroll', function(){
                    _setPos(obj);
                });
            }else{
                obj.css({
                    position: 'fixed',
                    left: '50%',
                    top: '50%',
                    marginTop: -obj.height()/2,
                    marginLeft: -obj.width()/2
                });
            }
        }else
        {
            _setPos(obj);
        }

        if(opt.isSlider){
            exports.slider(opt.sliderId);
        }
    };

    function _setPos(obj){
        var clientWidth = $(window).width(),
            clientHeight = $(window).height(),
            scrollTop = $(window).scrollTop(),
            dialogWidth = $(obj).outerWidth(),
            dialogHeight = $(obj).outerHeight();
        obj.css({
            left: (clientWidth-dialogWidth)/2,
            top: (clientHeight-dialogHeight)/2+scrollTop
        });
    }

    exports.close = function(id){
        if(id){
            $('#'+id).remove();
            $('#'+id+'mask').remove();
        }else{
            $('.uiDialogLayer').remove();
            $('.maskBackground').remove();
        }
    }

    exports.slider = function (id){
        require.async('lib/ui/jquery.cycle', function(cycle){
            $(id).before('<div class="activity-info-control" style="position:absolute; z-index: 9; left: 50%; bottom: 10px; margin: 0 0 0 -30px;"></div>').cycle({
                fx: 'fade',
                speed: 800,
                timeout: 0,
                pager: '.activity-info-control'
            });
        });
    }
});