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';
50 this.pointer = $('<div />')
51 .append( this.content )
53 .attr('id', 'wp-pointer-' + identifier++)
54 .addClass( this.options.pointerClass )
55 .css({'position': positioning, 'width': this.options.pointerWidth+'px', 'display': 'none'})
56 .appendTo( this.options.document.body );
59 _setOption: function( key, value ) {
63 // Handle document transfer
64 if ( key === "document" && value !== o.document ) {
65 tip.detach().appendTo( value.body );
67 // Handle class change
68 } else if ( key === "pointerClass" ) {
69 tip.removeClass( o.pointerClass ).addClass( value );
73 $.Widget.prototype._setOption.apply( this, arguments );
75 // Reposition automatically
76 if ( key === "position" ) {
79 // Update content automatically if pointer is open
80 } else if ( key === "content" && this.active ) {
86 this.pointer.remove();
87 $.Widget.prototype.destroy.call( this );
94 update: function( event ) {
103 dfd.done( function( content ) {
104 self._update( event, content );
107 // Either o.content is a string...
108 if ( typeof o.content === 'string' ) {
111 // ...or o.content is a callback.
113 content = o.content.call( this.element[0], dfd.resolve, event, this._handoff() );
116 // If content is set, then complete the update.
118 dfd.resolve( content );
120 return dfd.promise();
124 * Update is separated into two functions to allow events to defer
125 * updating the pointer (e.g. fetch content with ajax, etc).
127 _update: function( event, content ) {
134 this.pointer.stop(); // Kill any animations on the pointer.
135 this.content.html( content );
137 buttons = o.buttons.call( this.element[0], event, this._handoff() );
139 buttons.wrap('<div class="wp-pointer-buttons" />').parent().appendTo( this.content );
145 reposition: function() {
148 if ( this.options.disabled )
151 position = this._processPosition( this.options.position );
153 // Reposition pointer.
157 zIndex: zindex++ // Increment the z-index so that it shows above other opened pointers.
158 }).show().position($.extend({
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