You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

283 lines
6.5 KiB

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