2 * Pointer jQuery widget.
8 $.widget("wp.pointer", {
10 pointerClass: 'wp-pointer',
12 content: function( respond, event, t ) {
13 return $(this).text();
15 buttons: function( event, t ) {
16 var close = ( wpPointerL10n ) ? wpPointerL10n.dismiss : 'Dismiss',
17 button = $('<a class="close" href="#">' + close + '</a>');
19 return button.bind( 'click.pointer', function(e) {
21 t.element.pointer('close');
25 show: function( event, t ) {
29 hide: function( event, t ) {
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>');
43 family = this.element.parents().add( this.element );
44 positioning = 'absolute';
46 if ( family.filter(function(){ return 'fixed' === $(this).css('position'); }).length )
47 positioning = 'fixed';
49 this.pointer = $('<div />')
50 .append( this.content )
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 );
58 _setOption: function( key, value ) {
62 // Handle document transfer
63 if ( key === "document" && value !== o.document ) {
64 tip.detach().appendTo( value.body );
66 // Handle class change
67 } else if ( key === "pointerClass" ) {
68 tip.removeClass( o.pointerClass ).addClass( value );
72 $.Widget.prototype._setOption.apply( this, arguments );
74 // Reposition automatically
75 if ( key === "position" ) {
78 // Update content automatically if pointer is open
79 } else if ( key === "content" && this.active ) {
85 this.pointer.remove();
86 $.Widget.prototype.destroy.call( this );
93 update: function( event ) {
102 dfd.done( function( content ) {
103 self._update( event, content );
106 // Either o.content is a string...
107 if ( typeof o.content === 'string' ) {
110 // ...or o.content is a callback.
112 content = o.content.call( this.element[0], dfd.resolve, event, this._handoff() );
115 // If content is set, then complete the update.
117 dfd.resolve( content );
119 return dfd.promise();
123 * Update is separated into two functions to allow events to defer
124 * updating the pointer (e.g. fetch content with ajax, etc).
126 _update: function( event, content ) {
133 this.pointer.stop(); // Kill any animations on the pointer.
134 this.content.html( content );
136 buttons = o.buttons.call( this.element[0], event, this._handoff() );
138 buttons.wrap('<div class="wp-pointer-buttons" />').parent().appendTo( this.content );
144 reposition: function() {
147 if ( this.options.disabled )
150 position = this._processPosition( this.options.position );
152 // Reposition pointer.
156 zIndex: zindex++ // Increment the z-index so that it shows above other opened pointers.
157 }).show().position($.extend({
159 collision: 'fit none'
160 }, position )); // the object comes before this.options.position so the user can override position.of.
165 repoint: function() {
166 var o = this.options,
172 edge = ( typeof o.position == 'string' ) ? o.position : o.position.edge;
174 // Remove arrow classes.
175 this.pointer[0].className = this.pointer[0].className.replace( /wp-pointer-[^\s'"]*/, '' );
178 this.pointer.addClass( 'wp-pointer-' + edge );
181 _processPosition: function( position ) {
190 // If the position object is a string, it is shorthand for position.edge.
191 if ( typeof position == 'string' ) {
196 result = $.extend( {}, position );
202 if ( result.edge == 'top' || result.edge == 'bottom' ) {
203 result.align = result.align || 'left';
205 result.at = result.at || result.align + ' ' + opposite[ result.edge ];
206 result.my = result.my || result.align + ' ' + result.edge;
208 result.align = result.align || 'top';
210 result.at = result.at || opposite[ result.edge ] + ' ' + result.align;
211 result.my = result.my || result.edge + ' ' + result.align;
217 open: function( event ) {
221 if ( this.active || o.disabled || this.element.is(':hidden') )
224 this.update().done( function() {
229 _open: function( event ) {
233 if ( this.active || o.disabled || this.element.is(':hidden') )
238 this._trigger( "open", event, this._handoff() );
240 this._trigger( "show", event, this._handoff({
242 self._trigger( "opened", event, self._handoff() );
247 close: function( event ) {
248 if ( !this.active || this.options.disabled )
254 this._trigger( "close", event, this._handoff() );
255 this._trigger( "hide", event, this._handoff({
257 self._trigger( "closed", event, self._handoff() );
262 sendToTop: function( event ) {
264 this.pointer.css( 'z-index', zindex++ );
267 toggle: function( event ) {
268 if ( this.pointer.is(':hidden') )
274 _handoff: function( extend ) {
276 pointer: this.pointer,
277 element: this.element