pophint.js 4.61 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",
            onName  : "确定",
            offName : "取消",
            operation : function( target ){}, //点击确定按钮执行的回调
            cancelOpera : function( target ){},  //点击取消按钮可传入的回调
            showButton: true,
			head: null
        }
        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="s-chd"></div>'+
	                    '<div class="cbd">您确定要继续操作吗?</div>'+
	                    '<div class="s-cft">'+
		                '<button class="submitBtn  s-f-confirm">'+ _this._opts.onName +
                        '</button>'+
		                '<button class="submitBtn grayBtn s-f-remove">'+ _this._opts.offName +
                        '</button>'+
	                    '</div></div>';

            if ( !$( "#s-cancel-ensure" ).length > 0 ){
                $( "body" ).append( html );
            };
			
            $( '#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(){};
                    _this._opts.cancelOpera = 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(){};
                _this._opts.cancelOpera = function(){};
                $.fancybox.close();

            });
            //取消操作事件绑定
            $( '.s-f-remove' ).bind( 'click' ,function(){

                _this._opts.cancelOpera.call( _this , _this.target );
                //如果是链接 还原链接
                if( $( _this.target ).is( 'a' ) ){
                    $( _this.target ).attr( 'href', _this.href );
                }
                _this._opts.cancelOpera = function(){};
                 _this._opts.operation = function(){};
                $.fancybox.close();
                return false;

            });

		},
		//设置弹层内容
        setContent: function(){

			var _this = this,
				head = this._opts.head;
				
            $( "#"+_this._opts.showid ).find( ".cbd" ).html( _this._opts.content );
            $( "#"+_this._opts.showid ).find( ".s-f-confirm" ).html( _this._opts.onName );
            $( "#"+_this._opts.showid ).find( ".s-f-remove" ).html( _this._opts.offName );
			
			if ( typeof head == "string" && head.length ){
				$( "#"+_this._opts.showid ).find( ".s-chd" ).html( head ).show();
			}

        }
    }
    
    return pophint;
});