pophint.js 3.9 KB
/**
* Pophint
* Code in 2014/10/16
* By van
*/

define( 'pophint' ,function( require, exports, module ){

	require( 'fancybox' );

    var pophint = function( target,options ){
        this.target = target;
		this.href = null;
        this._opts = {
            showid  : "s-cancel-ensure", // 弹层容器ID
            content : "您确定要继续操作吗?",
            on      : "s-f-confirm",
            off     : "s-f-remove",
            operation : function( target ){},
            showButton:true
        }
        this._opts = $.extend( this._opts,options );

        this.init();
    };

    pophint.prototype = {
		//初始化
        init: function(){
            
            var _this = this;
            if( $( _this.target ).is( 'a' ) ){ 
				_this.href =  $( _this.target ).attr( 'href' );
                $( _this.target ).attr( 'href' , 'javascript:void( 0 )' );
            }
			if ( !$( "#s-cancel-ensure" ).length > 0 ){
                this.makeHtml();
            };
            
            this.setContent();
			
			_this.setpop();
        },

		// 创建弹层内容
        makeHtml: function(){
			var _this = this;
			// 创建html
            var html =  '<span href="#s-cancel-ensure" id="fancybox-cancel-ensure"></span>'+
                        '<div class="cancel-ensure" id="s-cancel-ensure" style="display: none;">'+
	                    '<div class="cbd">您确定要继续操作吗?</div>'+
	                    '<div class="s-cft">'+
		                '<button class="submitBtn  s-f-confirm">确定</button>'+
		                '<button class="submitBtn grayBtn s-f-remove">取消</button>'+
	                    '</div></div>';

            if ( !$( "#s-cancel-ensure" ).length > 0 ){
                $( "body" ).append( html );
            };

            // if( !_this._opts.showButton ){
            //     $( '.s-cft .s-f-remove' ).hide();
            // }else{
            //     $( '.s-cft .s-f-remove' ).show();
            // }
			
			
            $( '#fancybox-cancel-ensure' ).click(function(){
				
            }).fancybox({
                padding:0,
                closeBtn:false,
                scrolling:'visible',
                keys:{
                    close: null
                },
                helpers:{
                    overlay:{
                        closeClick : false
                    }
                },
                afterClose:function(){ 
                    _this._opts.operation = function(){};
                }
            });

            //$( '#fancybox-cancel-ensure' ).trigger( "click" );
        },
		//获取弹层事件
		setpop:function(){


			var _this = this;
			 $( '#fancybox-cancel-ensure' ).trigger( "click" );

            if( !_this._opts.showButton ){
                $( '.s-cft .s-f-remove' ).hide();
            }else{
                $( '.s-cft .s-f-remove' ).show();
            }

            // 确定操作事件绑定
            $( '.s-f-confirm' ).bind( 'click' ,function(){ 
                
                _this._opts.operation.call( _this , _this.target );
                //如果是链接 允许其跳转
                if( $( _this.target ).is( 'a' ) ){ 
                    window.location.href = _this.href ;
                }
                _this._opts.operation = function(){};
                $.fancybox.close();
            });
            //取消操作事件绑定
            $( '.s-f-remove' ).bind( 'click' ,function(){
                //如果是链接 还原链接
                if( $( _this.target ).is( 'a' ) ){
                    $( _this.target ).attr( 'href', _this.href );
                }
                _this._opts.operation = function(){};
                $.fancybox.close();
                return false;
            });

		},
		//设置弹层内容
        setContent: function(){
			var _this = this;
            $( "#"+_this._opts.showid ).find( ".cbd" ).html( _this._opts.content );
        }
    }
    
    return pophint;
});