web/wp-includes/js/wp-pointer.js
changeset 204 09a1c134465b
parent 194 32102edaa81b
equal deleted inserted replaced
203:f507feede89a 204:09a1c134465b
     1 (function(c){var a=0,b=9999;c.widget("wp.pointer",{options:{pointerClass:"wp-pointer",pointerWidth:320,content:function(f,e,d){return c(this).text()},buttons:function(f,e){var g=(wpPointerL10n)?wpPointerL10n.dismiss:"Dismiss",d=c('<a class="close" href="#">'+g+"</a>");return d.bind("click.pointer",function(h){h.preventDefault();e.element.pointer("close")})},position:"top",show:function(e,d){d.pointer.show();d.opened()},hide:function(e,d){d.pointer.hide();d.closed()},document:document},_create:function(){var e,d;this.content=c('<div class="wp-pointer-content"></div>');this.arrow=c('<div class="wp-pointer-arrow"><div class="wp-pointer-arrow-inner"></div></div>');d=this.element.parents().add(this.element);e="absolute";if(d.filter(function(){return"fixed"===c(this).css("position")}).length){e="fixed"}this.pointer=c("<div />").append(this.content).append(this.arrow).attr("id","wp-pointer-"+a++).addClass(this.options.pointerClass).css({position:e,width:this.options.pointerWidth+"px",display:"none"}).appendTo(this.options.document.body)},_setOption:function(d,f){var g=this.options,e=this.pointer;if(d==="document"&&f!==g.document){e.detach().appendTo(f.body)}else{if(d==="pointerClass"){e.removeClass(g.pointerClass).addClass(f)}}c.Widget.prototype._setOption.apply(this,arguments);if(d==="position"){this.reposition()}else{if(d==="content"&&this.active){this.update()}}},destroy:function(){this.pointer.remove();c.Widget.prototype.destroy.call(this)},widget:function(){return this.pointer},update:function(g){var e=this,h=this.options,d=c.Deferred(),f;if(h.disabled){return}d.done(function(i){e._update(g,i)});if(typeof h.content==="string"){f=h.content}else{f=h.content.call(this.element[0],d.resolve,g,this._handoff())}if(f){d.resolve(f)}return d.promise()},_update:function(f,e){var d,g=this.options;if(!e){return}this.pointer.stop();this.content.html(e);d=g.buttons.call(this.element[0],f,this._handoff());if(d){d.wrap('<div class="wp-pointer-buttons" />').parent().appendTo(this.content)}this.reposition()},reposition:function(){var d;if(this.options.disabled){return}d=this._processPosition(this.options.position);this.pointer.css({top:0,left:0,zIndex:b++}).show().position(c.extend({of:this.element,collision:"fit none"},d));this.repoint()},repoint:function(){var e=this.options,d;if(e.disabled){return}d=(typeof e.position=="string")?e.position:e.position.edge;this.pointer[0].className=this.pointer[0].className.replace(/wp-pointer-[^\s'"]*/,"");this.pointer.addClass("wp-pointer-"+d)},_processPosition:function(e){var f={top:"bottom",bottom:"top",left:"right",right:"left"},d;if(typeof e=="string"){d={edge:e+""}}else{d=c.extend({},e)}if(!d.edge){return d}if(d.edge=="top"||d.edge=="bottom"){d.align=d.align||"left";d.at=d.at||d.align+" "+f[d.edge];d.my=d.my||d.align+" "+d.edge}else{d.align=d.align||"top";d.at=d.at||f[d.edge]+" "+d.align;d.my=d.my||d.edge+" "+d.align}return d},open:function(e){var d=this,f=this.options;if(this.active||f.disabled||this.element.is(":hidden")){return}this.update().done(function(){d._open(e)})},_open:function(e){var d=this,f=this.options;if(this.active||f.disabled||this.element.is(":hidden")){return}this.active=true;this._trigger("open",e,this._handoff());this._trigger("show",e,this._handoff({opened:function(){d._trigger("opened",e,d._handoff())}}))},close:function(e){if(!this.active||this.options.disabled){return}var d=this;this.active=false;this._trigger("close",e,this._handoff());this._trigger("hide",e,this._handoff({closed:function(){d._trigger("closed",e,d._handoff())}}))},sendToTop:function(d){if(this.active){this.pointer.css("z-index",b++)}},toggle:function(d){if(this.pointer.is(":hidden")){this.open(d)}else{this.close(d)}},_handoff:function(d){return c.extend({pointer:this.pointer,element:this.element},d)}})})(jQuery);
     1 /**
       
     2  * Pointer jQuery widget.
       
     3  */
       
     4 (function($){
       
     5 	var identifier = 0,
       
     6 		zindex = 9999;
       
     7 
       
     8 	$.widget("wp.pointer", {
       
     9 		options: {
       
    10 			pointerClass: 'wp-pointer',
       
    11 			pointerWidth: 320,
       
    12 			content: function( respond, event, t ) {
       
    13 				return $(this).text();
       
    14 			},
       
    15 			buttons: function( event, t ) {
       
    16 				var close  = ( wpPointerL10n ) ? wpPointerL10n.dismiss : 'Dismiss',
       
    17 					button = $('<a class="close" href="#">' + close + '</a>');
       
    18 
       
    19 				return button.bind( 'click.pointer', function(e) {
       
    20 					e.preventDefault();
       
    21 					t.element.pointer('close');
       
    22 				});
       
    23 			},
       
    24 			position: 'top',
       
    25 			show: function( event, t ) {
       
    26 				t.pointer.show();
       
    27 				t.opened();
       
    28 			},
       
    29 			hide: function( event, t ) {
       
    30 				t.pointer.hide();
       
    31 				t.closed();
       
    32 			},
       
    33 			document: document
       
    34 		},
       
    35 
       
    36 		_create: function() {
       
    37 			var positioning,
       
    38 				family;
       
    39 
       
    40 			this.content = $('<div class="wp-pointer-content"></div>');
       
    41 			this.arrow   = $('<div class="wp-pointer-arrow"><div class="wp-pointer-arrow-inner"></div></div>');
       
    42 
       
    43 			family = this.element.parents().add( this.element );
       
    44 			positioning = 'absolute';
       
    45 
       
    46 			if ( family.filter(function(){ return 'fixed' === $(this).css('position'); }).length )
       
    47 				positioning = 'fixed';
       
    48 
       
    49 			this.pointer = $('<div />')
       
    50 				.append( this.content )
       
    51 				.append( this.arrow )
       
    52 				.attr('id', 'wp-pointer-' + identifier++)
       
    53 				.addClass( this.options.pointerClass )
       
    54 				.css({'position': positioning, 'width': this.options.pointerWidth+'px', 'display': 'none'})
       
    55 				.appendTo( this.options.document.body );
       
    56 		},
       
    57 
       
    58 		_setOption: function( key, value ) {
       
    59 			var o   = this.options,
       
    60 				tip = this.pointer;
       
    61 
       
    62 			// Handle document transfer
       
    63 			if ( key === "document" && value !== o.document ) {
       
    64 				tip.detach().appendTo( value.body );
       
    65 
       
    66 			// Handle class change
       
    67 			} else if ( key === "pointerClass" ) {
       
    68 				tip.removeClass( o.pointerClass ).addClass( value );
       
    69 			}
       
    70 
       
    71 			// Call super method.
       
    72 			$.Widget.prototype._setOption.apply( this, arguments );
       
    73 
       
    74 			// Reposition automatically
       
    75 			if ( key === "position" ) {
       
    76 				this.reposition();
       
    77 
       
    78 			// Update content automatically if pointer is open
       
    79 			} else if ( key === "content" && this.active ) {
       
    80 				this.update();
       
    81 			}
       
    82 		},
       
    83 
       
    84 		destroy: function() {
       
    85 			this.pointer.remove();
       
    86 			$.Widget.prototype.destroy.call( this );
       
    87 		},
       
    88 
       
    89 		widget: function() {
       
    90 			return this.pointer;
       
    91 		},
       
    92 
       
    93 		update: function( event ) {
       
    94 			var self = this,
       
    95 				o    = this.options,
       
    96 				dfd  = $.Deferred(),
       
    97 				content;
       
    98 
       
    99 			if ( o.disabled )
       
   100 				return;
       
   101 
       
   102 			dfd.done( function( content ) {
       
   103 				self._update( event, content );
       
   104 			})
       
   105 
       
   106 			// Either o.content is a string...
       
   107 			if ( typeof o.content === 'string' ) {
       
   108 				content = o.content;
       
   109 
       
   110 			// ...or o.content is a callback.
       
   111 			} else {
       
   112 				content = o.content.call( this.element[0], dfd.resolve, event, this._handoff() );
       
   113 			}
       
   114 
       
   115 			// If content is set, then complete the update.
       
   116 			if ( content )
       
   117 				dfd.resolve( content );
       
   118 
       
   119 			return dfd.promise();
       
   120 		},
       
   121 
       
   122 		/**
       
   123 		 * Update is separated into two functions to allow events to defer
       
   124 		 * updating the pointer (e.g. fetch content with ajax, etc).
       
   125 		 */
       
   126 		_update: function( event, content ) {
       
   127 			var buttons,
       
   128 				o = this.options;
       
   129 
       
   130 			if ( ! content )
       
   131 				return;
       
   132 
       
   133 			this.pointer.stop(); // Kill any animations on the pointer.
       
   134 			this.content.html( content );
       
   135 
       
   136 			buttons = o.buttons.call( this.element[0], event, this._handoff() );
       
   137 			if ( buttons ) {
       
   138 				buttons.wrap('<div class="wp-pointer-buttons" />').parent().appendTo( this.content );
       
   139 			}
       
   140 
       
   141 			this.reposition();
       
   142 		},
       
   143 
       
   144 		reposition: function() {
       
   145 			var position;
       
   146 
       
   147 			if ( this.options.disabled )
       
   148 				return;
       
   149 
       
   150 			position = this._processPosition( this.options.position );
       
   151 
       
   152 			// Reposition pointer.
       
   153 			this.pointer.css({
       
   154 				top: 0,
       
   155 				left: 0,
       
   156 				zIndex: zindex++ // Increment the z-index so that it shows above other opened pointers.
       
   157 			}).show().position($.extend({
       
   158 				of: this.element,
       
   159 				collision: 'fit none'
       
   160 			}, position )); // the object comes before this.options.position so the user can override position.of.
       
   161 
       
   162 			this.repoint();
       
   163 		},
       
   164 
       
   165 		repoint: function() {
       
   166 			var o = this.options,
       
   167 				edge;
       
   168 
       
   169 			if ( o.disabled )
       
   170 				return;
       
   171 
       
   172 			edge = ( typeof o.position == 'string' ) ? o.position : o.position.edge;
       
   173 
       
   174 			// Remove arrow classes.
       
   175 			this.pointer[0].className = this.pointer[0].className.replace( /wp-pointer-[^\s'"]*/, '' );
       
   176 
       
   177 			// Add arrow class.
       
   178 			this.pointer.addClass( 'wp-pointer-' + edge );
       
   179 		},
       
   180 
       
   181 		_processPosition: function( position ) {
       
   182 			var opposite = {
       
   183 					top: 'bottom',
       
   184 					bottom: 'top',
       
   185 					left: 'right',
       
   186 					right: 'left'
       
   187 				},
       
   188 				result;
       
   189 
       
   190 			// If the position object is a string, it is shorthand for position.edge.
       
   191 			if ( typeof position == 'string' ) {
       
   192 				result = {
       
   193 					edge: position + ''
       
   194 				};
       
   195 			} else {
       
   196 				result = $.extend( {}, position );
       
   197 			}
       
   198 
       
   199 			if ( ! result.edge )
       
   200 				return result;
       
   201 
       
   202 			if ( result.edge == 'top' || result.edge == 'bottom' ) {
       
   203 				result.align = result.align || 'left';
       
   204 
       
   205 				result.at = result.at || result.align + ' ' + opposite[ result.edge ];
       
   206 				result.my = result.my || result.align + ' ' + result.edge;
       
   207 			} else {
       
   208 				result.align = result.align || 'top';
       
   209 
       
   210 				result.at = result.at || opposite[ result.edge ] + ' ' + result.align;
       
   211 				result.my = result.my || result.edge + ' ' + result.align;
       
   212 			}
       
   213 
       
   214 			return result;
       
   215 		},
       
   216 
       
   217 		open: function( event ) {
       
   218 			var self = this,
       
   219 				o    = this.options;
       
   220 
       
   221 			if ( this.active || o.disabled || this.element.is(':hidden') )
       
   222 				return;
       
   223 
       
   224 			this.update().done( function() {
       
   225 				self._open( event );
       
   226 			});
       
   227 		},
       
   228 
       
   229 		_open: function( event ) {
       
   230 			var self = this,
       
   231 				o    = this.options;
       
   232 
       
   233 			if ( this.active || o.disabled || this.element.is(':hidden') )
       
   234 				return;
       
   235 
       
   236 			this.active = true;
       
   237 
       
   238 			this._trigger( "open", event, this._handoff() );
       
   239 
       
   240 			this._trigger( "show", event, this._handoff({
       
   241 				opened: function() {
       
   242 					self._trigger( "opened", event, self._handoff() );
       
   243 				}
       
   244 			}));
       
   245 		},
       
   246 
       
   247 		close: function( event ) {
       
   248 			if ( !this.active || this.options.disabled )
       
   249 				return;
       
   250 
       
   251 			var self = this;
       
   252 			this.active = false;
       
   253 
       
   254 			this._trigger( "close", event, this._handoff() );
       
   255 			this._trigger( "hide", event, this._handoff({
       
   256 				closed: function() {
       
   257 					self._trigger( "closed", event, self._handoff() );
       
   258 				}
       
   259 			}));
       
   260 		},
       
   261 
       
   262 		sendToTop: function( event ) {
       
   263 			if ( this.active )
       
   264 				this.pointer.css( 'z-index', zindex++ );
       
   265 		},
       
   266 
       
   267 		toggle: function( event ) {
       
   268 			if ( this.pointer.is(':hidden') )
       
   269 				this.open( event );
       
   270 			else
       
   271 				this.close( event );
       
   272 		},
       
   273 
       
   274 		_handoff: function( extend ) {
       
   275 			return $.extend({
       
   276 				pointer: this.pointer,
       
   277 				element: this.element
       
   278 			}, extend);
       
   279 		}
       
   280 	});
       
   281 })(jQuery);