wp-pointer.js 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  1. /**
  2. * @output wp-includes/js/wp-pointer.js
  3. */
  4. /**
  5. * Initializes the wp-pointer widget using jQuery UI Widget Factory.
  6. */
  7. (function($){
  8. var identifier = 0,
  9. zindex = 9999;
  10. $.widget('wp.pointer',/** @lends $.widget.wp.pointer.prototype */{
  11. options: {
  12. pointerClass: 'wp-pointer',
  13. pointerWidth: 320,
  14. content: function() {
  15. return $(this).text();
  16. },
  17. buttons: function( event, t ) {
  18. var button = $('<a class="close" href="#"></a>').text( wp.i18n.__( 'Dismiss' ) );
  19. return button.on( '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. * A class that represents a WordPress pointer.
  37. *
  38. * @since 3.3.0
  39. * @private
  40. *
  41. * @constructs $.widget.wp.pointer
  42. */
  43. _create: function() {
  44. var positioning,
  45. family;
  46. this.content = $('<div class="wp-pointer-content"></div>');
  47. this.arrow = $('<div class="wp-pointer-arrow"><div class="wp-pointer-arrow-inner"></div></div>');
  48. family = this.element.parents().add( this.element );
  49. positioning = 'absolute';
  50. if ( family.filter(function(){ return 'fixed' === $(this).css('position'); }).length )
  51. positioning = 'fixed';
  52. this.pointer = $('<div />')
  53. .append( this.content )
  54. .append( this.arrow )
  55. .attr('id', 'wp-pointer-' + identifier++)
  56. .addClass( this.options.pointerClass )
  57. .css({'position': positioning, 'width': this.options.pointerWidth+'px', 'display': 'none'})
  58. .appendTo( this.options.document.body );
  59. },
  60. /**
  61. * Sets an option on the pointer instance.
  62. *
  63. * There are 4 special values that do something extra:
  64. *
  65. * - `document` will transfer the pointer to the body of the new document
  66. * specified by the value.
  67. * - `pointerClass` will change the class of the pointer element.
  68. * - `position` will reposition the pointer.
  69. * - `content` will update the content of the pointer.
  70. *
  71. * @since 3.3.0
  72. * @private
  73. *
  74. * @param {string} key The key of the option to set.
  75. * @param {*} value The value to set the option to.
  76. */
  77. _setOption: function( key, value ) {
  78. var o = this.options,
  79. tip = this.pointer;
  80. // Handle document transfer.
  81. if ( key === 'document' && value !== o.document ) {
  82. tip.detach().appendTo( value.body );
  83. // Handle class change.
  84. } else if ( key === 'pointerClass' ) {
  85. tip.removeClass( o.pointerClass ).addClass( value );
  86. }
  87. // Call super method.
  88. $.Widget.prototype._setOption.apply( this, arguments );
  89. // Reposition automatically.
  90. if ( key === 'position' ) {
  91. this.reposition();
  92. // Update content automatically if pointer is open.
  93. } else if ( key === 'content' && this.active ) {
  94. this.update();
  95. }
  96. },
  97. /**
  98. * Removes the pointer element from of the DOM.
  99. *
  100. * Makes sure that the widget and all associated bindings are destroyed.
  101. *
  102. * @since 3.3.0
  103. */
  104. destroy: function() {
  105. this.pointer.remove();
  106. $.Widget.prototype.destroy.call( this );
  107. },
  108. /**
  109. * Returns the pointer element.
  110. *
  111. * @since 3.3.0
  112. *
  113. * @return {Object} Pointer The pointer object.
  114. */
  115. widget: function() {
  116. return this.pointer;
  117. },
  118. /**
  119. * Updates the content of the pointer.
  120. *
  121. * This function doesn't update the content of the pointer itself. That is done
  122. * by the `_update` method. This method will make sure that the `_update` method
  123. * is called with the right content.
  124. *
  125. * The content in the options can either be a string or a callback. If it is a
  126. * callback the result of this callback is used as the content.
  127. *
  128. * @since 3.3.0
  129. *
  130. * @param {Object} event The event that caused the update.
  131. *
  132. * @return {Promise} Resolves when the update has been executed.
  133. */
  134. update: function( event ) {
  135. var self = this,
  136. o = this.options,
  137. dfd = $.Deferred(),
  138. content;
  139. if ( o.disabled )
  140. return;
  141. dfd.done( function( content ) {
  142. self._update( event, content );
  143. });
  144. // Either o.content is a string...
  145. if ( typeof o.content === 'string' ) {
  146. content = o.content;
  147. // ...or o.content is a callback.
  148. } else {
  149. content = o.content.call( this.element[0], dfd.resolve, event, this._handoff() );
  150. }
  151. // If content is set, then complete the update.
  152. if ( content )
  153. dfd.resolve( content );
  154. return dfd.promise();
  155. },
  156. /**
  157. * Updates the content of the pointer.
  158. *
  159. * Will make sure that the pointer is correctly positioned.
  160. *
  161. * @since 3.3.0
  162. * @private
  163. *
  164. * @param {Object} event The event that caused the update.
  165. * @param {*} content The content object. Either a string or a jQuery tree.
  166. */
  167. _update: function( event, content ) {
  168. var buttons,
  169. o = this.options;
  170. if ( ! content )
  171. return;
  172. // Kill any animations on the pointer.
  173. this.pointer.stop();
  174. this.content.html( content );
  175. buttons = o.buttons.call( this.element[0], event, this._handoff() );
  176. if ( buttons ) {
  177. buttons.wrap('<div class="wp-pointer-buttons" />').parent().appendTo( this.content );
  178. }
  179. this.reposition();
  180. },
  181. /**
  182. * Repositions the pointer.
  183. *
  184. * Makes sure the pointer is the correct size for its content and makes sure it
  185. * is positioned to point to the right element.
  186. *
  187. * @since 3.3.0
  188. */
  189. reposition: function() {
  190. var position;
  191. if ( this.options.disabled )
  192. return;
  193. position = this._processPosition( this.options.position );
  194. // Reposition pointer.
  195. this.pointer.css({
  196. top: 0,
  197. left: 0,
  198. zIndex: zindex++ // Increment the z-index so that it shows above other opened pointers.
  199. }).show().position($.extend({
  200. of: this.element,
  201. collision: 'fit none'
  202. }, position )); // The object comes before this.options.position so the user can override position.of.
  203. this.repoint();
  204. },
  205. /**
  206. * Sets the arrow of the pointer to the correct side of the pointer element.
  207. *
  208. * @since 3.3.0
  209. */
  210. repoint: function() {
  211. var o = this.options,
  212. edge;
  213. if ( o.disabled )
  214. return;
  215. edge = ( typeof o.position == 'string' ) ? o.position : o.position.edge;
  216. // Remove arrow classes.
  217. this.pointer[0].className = this.pointer[0].className.replace( /wp-pointer-[^\s'"]*/, '' );
  218. // Add arrow class.
  219. this.pointer.addClass( 'wp-pointer-' + edge );
  220. },
  221. /**
  222. * Calculates the correct position based on a position in the settings.
  223. *
  224. * @since 3.3.0
  225. * @private
  226. *
  227. * @param {string|Object} position Either a side of a pointer or an object
  228. * containing a pointer.
  229. *
  230. * @return {Object} result An object containing position related data.
  231. */
  232. _processPosition: function( position ) {
  233. var opposite = {
  234. top: 'bottom',
  235. bottom: 'top',
  236. left: 'right',
  237. right: 'left'
  238. },
  239. result;
  240. // If the position object is a string, it is shorthand for position.edge.
  241. if ( typeof position == 'string' ) {
  242. result = {
  243. edge: position + ''
  244. };
  245. } else {
  246. result = $.extend( {}, position );
  247. }
  248. if ( ! result.edge )
  249. return result;
  250. if ( result.edge == 'top' || result.edge == 'bottom' ) {
  251. result.align = result.align || 'left';
  252. result.at = result.at || result.align + ' ' + opposite[ result.edge ];
  253. result.my = result.my || result.align + ' ' + result.edge;
  254. } else {
  255. result.align = result.align || 'top';
  256. result.at = result.at || opposite[ result.edge ] + ' ' + result.align;
  257. result.my = result.my || result.edge + ' ' + result.align;
  258. }
  259. return result;
  260. },
  261. /**
  262. * Opens the pointer.
  263. *
  264. * Only opens the pointer widget in case it is closed and not disabled, and
  265. * calls 'update' before doing so. Calling update makes sure that the pointer
  266. * is correctly sized and positioned.
  267. *
  268. * @since 3.3.0
  269. *
  270. * @param {Object} event The event that triggered the opening of this pointer.
  271. */
  272. open: function( event ) {
  273. var self = this,
  274. o = this.options;
  275. if ( this.active || o.disabled || this.element.is(':hidden') )
  276. return;
  277. this.update().done( function() {
  278. self._open( event );
  279. });
  280. },
  281. /**
  282. * Opens and shows the pointer element.
  283. *
  284. * @since 3.3.0
  285. * @private
  286. *
  287. * @param {Object} event An event object.
  288. */
  289. _open: function( event ) {
  290. var self = this,
  291. o = this.options;
  292. if ( this.active || o.disabled || this.element.is(':hidden') )
  293. return;
  294. this.active = true;
  295. this._trigger( 'open', event, this._handoff() );
  296. this._trigger( 'show', event, this._handoff({
  297. opened: function() {
  298. self._trigger( 'opened', event, self._handoff() );
  299. }
  300. }));
  301. },
  302. /**
  303. * Closes and hides the pointer element.
  304. *
  305. * @since 3.3.0
  306. *
  307. * @param {Object} event An event object.
  308. */
  309. close: function( event ) {
  310. if ( !this.active || this.options.disabled )
  311. return;
  312. var self = this;
  313. this.active = false;
  314. this._trigger( 'close', event, this._handoff() );
  315. this._trigger( 'hide', event, this._handoff({
  316. closed: function() {
  317. self._trigger( 'closed', event, self._handoff() );
  318. }
  319. }));
  320. },
  321. /**
  322. * Puts the pointer on top by increasing the z-index.
  323. *
  324. * @since 3.3.0
  325. */
  326. sendToTop: function() {
  327. if ( this.active )
  328. this.pointer.css( 'z-index', zindex++ );
  329. },
  330. /**
  331. * Toggles the element between shown and hidden.
  332. *
  333. * @since 3.3.0
  334. *
  335. * @param {Object} event An event object.
  336. */
  337. toggle: function( event ) {
  338. if ( this.pointer.is(':hidden') )
  339. this.open( event );
  340. else
  341. this.close( event );
  342. },
  343. /**
  344. * Extends the pointer and the widget element with the supplied parameter, which
  345. * is either an element or a function.
  346. *
  347. * @since 3.3.0
  348. * @private
  349. *
  350. * @param {Object} extend The object to be merged into the original object.
  351. *
  352. * @return {Object} The extended object.
  353. */
  354. _handoff: function( extend ) {
  355. return $.extend({
  356. pointer: this.pointer,
  357. element: this.element
  358. }, extend);
  359. }
  360. });
  361. })(jQuery);