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.

1486 lines
48 KiB

9 years ago
10 years ago
10 years ago
10 years ago
9 years ago
10 years ago
9 years ago
10 years ago
8 years ago
10 years ago
9 years ago
10 years ago
9 years ago
8 years ago
9 years ago
10 years ago
9 years ago
10 years ago
8 years ago
10 years ago
9 years ago
10 years ago
8 years ago
10 years ago
10 years ago
10 years ago
9 years ago
9 years ago
10 years ago
10 years ago
7 years ago
9 years ago
7 years ago
10 years ago
9 years ago
7 years ago
9 years ago
10 years ago
9 years ago
9 years ago
10 years ago
9 years ago
10 years ago
10 years ago
8 years ago
9 years ago
10 years ago
10 years ago
9 years ago
9 years ago
8 years ago
10 years ago
9 years ago
10 years ago
9 years ago
10 years ago
9 years ago
9 years ago
9 years ago
10 years ago
9 years ago
8 years ago
9 years ago
8 years ago
9 years ago
9 years ago
8 years ago
10 years ago
10 years ago
10 years ago
9 years ago
10 years ago
9 years ago
9 years ago
10 years ago
10 years ago
9 years ago
10 years ago
9 years ago
9 years ago
10 years ago
9 years ago
8 years ago
9 years ago
10 years ago
9 years ago
10 years ago
9 years ago
9 years ago
9 years ago
10 years ago
9 years ago
9 years ago
9 years ago
10 years ago
7 years ago
9 years ago
8 years ago
9 years ago
10 years ago
10 years ago
10 years ago
10 years ago
9 years ago
9 years ago
10 years ago
9 years ago
9 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
9 years ago
10 years ago
9 years ago
10 years ago
10 years ago
9 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
9 years ago
10 years ago
9 years ago
10 years ago
9 years ago
9 years ago
9 years ago
8 years ago
9 years ago
8 years ago
8 years ago
9 years ago
8 years ago
9 years ago
8 years ago
9 years ago
8 years ago
9 years ago
9 years ago
9 years ago
8 years ago
9 years ago
8 years ago
9 years ago
9 years ago
10 years ago
9 years ago
10 years ago
9 years ago
9 years ago
9 years ago
10 years ago
9 years ago
9 years ago
9 years ago
10 years ago
10 years ago
10 years ago
9 years ago
9 years ago
9 years ago
10 years ago
9 years ago
10 years ago
9 years ago
10 years ago
9 years ago
9 years ago
9 years ago
10 years ago
9 years ago
10 years ago
9 years ago
10 years ago
9 years ago
10 years ago
9 years ago
10 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
10 years ago
9 years ago
10 years ago
9 years ago
10 years ago
9 years ago
10 years ago
9 years ago
10 years ago
9 years ago
10 years ago
9 years ago
10 years ago
9 years ago
10 years ago
9 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
9 years ago
10 years ago
10 years ago
9 years ago
10 years ago
9 years ago
9 years ago
9 years ago
10 years ago
9 years ago
9 years ago
9 years ago
9 years ago
10 years ago
10 years ago
9 years ago
10 years ago
9 years ago
9 years ago
9 years ago
9 years ago
10 years ago
10 years ago
10 years ago
9 years ago
8 years ago
7 years ago
9 years ago
7 years ago
10 years ago
7 years ago
10 years ago
7 years ago
10 years ago
8 years ago
10 years ago
8 years ago
10 years ago
9 years ago
10 years ago
9 years ago
10 years ago
9 years ago
10 years ago
9 years ago
9 years ago
9 years ago
10 years ago
7 years ago
10 years ago
10 years ago
9 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
8 years ago
8 years ago
8 years ago
8 years ago
9 years ago
8 years ago
9 years ago
8 years ago
9 years ago
8 years ago
9 years ago
8 years ago
9 years ago
9 years ago
8 years ago
9 years ago
8 years ago
10 years ago
8 years ago
9 years ago
9 years ago
9 years ago
8 years ago
9 years ago
8 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
8 years ago
9 years ago
8 years ago
9 years ago
8 years ago
8 years ago
9 years ago
8 years ago
9 years ago
8 years ago
9 years ago
8 years ago
9 years ago
8 years ago
8 years ago
10 years ago
9 years ago
10 years ago
10 years ago
10 years ago
9 years ago
10 years ago
10 years ago
9 years ago
10 years ago
9 years ago
9 years ago
10 years ago
9 years ago
9 years ago
9 years ago
9 years ago
10 years ago
9 years ago
10 years ago
7 years ago
10 years ago
9 years ago
  1. /*!
  2. * # Semantic UI 2.2.14 - Popup
  3. * http://github.com/semantic-org/semantic-ui/
  4. *
  5. *
  6. * Released under the MIT license
  7. * http://opensource.org/licenses/MIT
  8. *
  9. */
  10. ;(function ($, window, document, undefined) {
  11. "use strict";
  12. window = (typeof window != 'undefined' && window.Math == Math)
  13. ? window
  14. : (typeof self != 'undefined' && self.Math == Math)
  15. ? self
  16. : Function('return this')()
  17. ;
  18. $.fn.popup = function(parameters) {
  19. var
  20. $allModules = $(this),
  21. $document = $(document),
  22. $window = $(window),
  23. $body = $('body'),
  24. moduleSelector = $allModules.selector || '',
  25. hasTouch = (true),
  26. time = new Date().getTime(),
  27. performance = [],
  28. query = arguments[0],
  29. methodInvoked = (typeof query == 'string'),
  30. queryArguments = [].slice.call(arguments, 1),
  31. returnedValue
  32. ;
  33. $allModules
  34. .each(function() {
  35. var
  36. settings = ( $.isPlainObject(parameters) )
  37. ? $.extend(true, {}, $.fn.popup.settings, parameters)
  38. : $.extend({}, $.fn.popup.settings),
  39. selector = settings.selector,
  40. className = settings.className,
  41. error = settings.error,
  42. metadata = settings.metadata,
  43. namespace = settings.namespace,
  44. eventNamespace = '.' + settings.namespace,
  45. moduleNamespace = 'module-' + namespace,
  46. $module = $(this),
  47. $context = $(settings.context),
  48. $scrollContext = $(settings.scrollContext),
  49. $boundary = $(settings.boundary),
  50. $target = (settings.target)
  51. ? $(settings.target)
  52. : $module,
  53. $popup,
  54. $offsetParent,
  55. searchDepth = 0,
  56. triedPositions = false,
  57. openedWithTouch = false,
  58. element = this,
  59. instance = $module.data(moduleNamespace),
  60. documentObserver,
  61. elementNamespace,
  62. id,
  63. module
  64. ;
  65. module = {
  66. // binds events
  67. initialize: function() {
  68. module.debug('Initializing', $module);
  69. module.createID();
  70. module.bind.events();
  71. if(!module.exists() && settings.preserve) {
  72. module.create();
  73. }
  74. if(settings.observeChanges) {
  75. module.observeChanges();
  76. }
  77. module.instantiate();
  78. },
  79. instantiate: function() {
  80. module.verbose('Storing instance', module);
  81. instance = module;
  82. $module
  83. .data(moduleNamespace, instance)
  84. ;
  85. },
  86. observeChanges: function() {
  87. if('MutationObserver' in window) {
  88. documentObserver = new MutationObserver(module.event.documentChanged);
  89. documentObserver.observe(document, {
  90. childList : true,
  91. subtree : true
  92. });
  93. module.debug('Setting up mutation observer', documentObserver);
  94. }
  95. },
  96. refresh: function() {
  97. if(settings.popup) {
  98. $popup = $(settings.popup).eq(0);
  99. }
  100. else {
  101. if(settings.inline) {
  102. $popup = $target.nextAll(selector.popup).eq(0);
  103. settings.popup = $popup;
  104. }
  105. }
  106. if(settings.popup) {
  107. $popup.addClass(className.loading);
  108. $offsetParent = module.get.offsetParent();
  109. $popup.removeClass(className.loading);
  110. if(settings.movePopup && module.has.popup() && module.get.offsetParent($popup)[0] !== $offsetParent[0]) {
  111. module.debug('Moving popup to the same offset parent as target');
  112. $popup
  113. .detach()
  114. .appendTo($offsetParent)
  115. ;
  116. }
  117. }
  118. else {
  119. $offsetParent = (settings.inline)
  120. ? module.get.offsetParent($target)
  121. : module.has.popup()
  122. ? module.get.offsetParent($popup)
  123. : $body
  124. ;
  125. }
  126. if( $offsetParent.is('html') && $offsetParent[0] !== $body[0] ) {
  127. module.debug('Setting page as offset parent');
  128. $offsetParent = $body;
  129. }
  130. if( module.get.variation() ) {
  131. module.set.variation();
  132. }
  133. },
  134. reposition: function() {
  135. module.refresh();
  136. module.set.position();
  137. },
  138. destroy: function() {
  139. module.debug('Destroying previous module');
  140. if(documentObserver) {
  141. documentObserver.disconnect();
  142. }
  143. // remove element only if was created dynamically
  144. if($popup && !settings.preserve) {
  145. module.removePopup();
  146. }
  147. // clear all timeouts
  148. clearTimeout(module.hideTimer);
  149. clearTimeout(module.showTimer);
  150. // remove events
  151. module.unbind.close();
  152. module.unbind.events();
  153. $module
  154. .removeData(moduleNamespace)
  155. ;
  156. },
  157. event: {
  158. start: function(event) {
  159. var
  160. delay = ($.isPlainObject(settings.delay))
  161. ? settings.delay.show
  162. : settings.delay
  163. ;
  164. clearTimeout(module.hideTimer);
  165. if(!openedWithTouch) {
  166. module.showTimer = setTimeout(module.show, delay);
  167. }
  168. },
  169. end: function() {
  170. var
  171. delay = ($.isPlainObject(settings.delay))
  172. ? settings.delay.hide
  173. : settings.delay
  174. ;
  175. clearTimeout(module.showTimer);
  176. module.hideTimer = setTimeout(module.hide, delay);
  177. },
  178. touchstart: function(event) {
  179. openedWithTouch = true;
  180. module.show();
  181. },
  182. resize: function() {
  183. if( module.is.visible() ) {
  184. module.set.position();
  185. }
  186. },
  187. documentChanged: function(mutations) {
  188. [].forEach.call(mutations, function(mutation) {
  189. if(mutation.removedNodes) {
  190. [].forEach.call(mutation.removedNodes, function(node) {
  191. if(node == element || $(node).find(element).length > 0) {
  192. module.debug('Element removed from DOM, tearing down events');
  193. module.destroy();
  194. }
  195. });
  196. }
  197. });
  198. },
  199. hideGracefully: function(event) {
  200. var
  201. $target = $(event.target),
  202. isInDOM = $.contains(document.documentElement, event.target),
  203. inPopup = ($target.closest(selector.popup).length > 0)
  204. ;
  205. // don't close on clicks inside popup
  206. if(event && !inPopup && isInDOM) {
  207. module.debug('Click occurred outside popup hiding popup');
  208. module.hide();
  209. }
  210. else {
  211. module.debug('Click was inside popup, keeping popup open');
  212. }
  213. }
  214. },
  215. // generates popup html from metadata
  216. create: function() {
  217. var
  218. html = module.get.html(),
  219. title = module.get.title(),
  220. content = module.get.content()
  221. ;
  222. if(html || content || title) {
  223. module.debug('Creating pop-up html');
  224. if(!html) {
  225. html = settings.templates.popup({
  226. title : title,
  227. content : content
  228. });
  229. }
  230. $popup = $('<div/>')
  231. .addClass(className.popup)
  232. .data(metadata.activator, $module)
  233. .html(html)
  234. ;
  235. if(settings.inline) {
  236. module.verbose('Inserting popup element inline', $popup);
  237. $popup
  238. .insertAfter($module)
  239. ;
  240. }
  241. else {
  242. module.verbose('Appending popup element to body', $popup);
  243. $popup
  244. .appendTo( $context )
  245. ;
  246. }
  247. module.refresh();
  248. module.set.variation();
  249. if(settings.hoverable) {
  250. module.bind.popup();
  251. }
  252. settings.onCreate.call($popup, element);
  253. }
  254. else if($target.next(selector.popup).length !== 0) {
  255. module.verbose('Pre-existing popup found');
  256. settings.inline = true;
  257. settings.popup = $target.next(selector.popup).data(metadata.activator, $module);
  258. module.refresh();
  259. if(settings.hoverable) {
  260. module.bind.popup();
  261. }
  262. }
  263. else if(settings.popup) {
  264. $(settings.popup).data(metadata.activator, $module);
  265. module.verbose('Used popup specified in settings');
  266. module.refresh();
  267. if(settings.hoverable) {
  268. module.bind.popup();
  269. }
  270. }
  271. else {
  272. module.debug('No content specified skipping display', element);
  273. }
  274. },
  275. createID: function() {
  276. id = (Math.random().toString(16) + '000000000').substr(2, 8);
  277. elementNamespace = '.' + id;
  278. module.verbose('Creating unique id for element', id);
  279. },
  280. // determines popup state
  281. toggle: function() {
  282. module.debug('Toggling pop-up');
  283. if( module.is.hidden() ) {
  284. module.debug('Popup is hidden, showing pop-up');
  285. module.unbind.close();
  286. module.show();
  287. }
  288. else {
  289. module.debug('Popup is visible, hiding pop-up');
  290. module.hide();
  291. }
  292. },
  293. show: function(callback) {
  294. callback = callback || function(){};
  295. module.debug('Showing pop-up', settings.transition);
  296. if(module.is.hidden() && !( module.is.active() && module.is.dropdown()) ) {
  297. if( !module.exists() ) {
  298. module.create();
  299. }
  300. if(settings.onShow.call($popup, element) === false) {
  301. module.debug('onShow callback returned false, cancelling popup animation');
  302. return;
  303. }
  304. else if(!settings.preserve && !settings.popup) {
  305. module.refresh();
  306. }
  307. if( $popup && module.set.position() ) {
  308. module.save.conditions();
  309. if(settings.exclusive) {
  310. module.hideAll();
  311. }
  312. module.animate.show(callback);
  313. }
  314. }
  315. },
  316. hide: function(callback) {
  317. callback = callback || function(){};
  318. if( module.is.visible() || module.is.animating() ) {
  319. if(settings.onHide.call($popup, element) === false) {
  320. module.debug('onHide callback returned false, cancelling popup animation');
  321. return;
  322. }
  323. module.remove.visible();
  324. module.unbind.close();
  325. module.restore.conditions();
  326. module.animate.hide(callback);
  327. }
  328. },
  329. hideAll: function() {
  330. $(selector.popup)
  331. .filter('.' + className.popupVisible)
  332. .each(function() {
  333. $(this)
  334. .data(metadata.activator)
  335. .popup('hide')
  336. ;
  337. })
  338. ;
  339. },
  340. exists: function() {
  341. if(!$popup) {
  342. return false;
  343. }
  344. if(settings.inline || settings.popup) {
  345. return ( module.has.popup() );
  346. }
  347. else {
  348. return ( $popup.closest($context).length >= 1 )
  349. ? true
  350. : false
  351. ;
  352. }
  353. },
  354. removePopup: function() {
  355. if( module.has.popup() && !settings.popup) {
  356. module.debug('Removing popup', $popup);
  357. $popup.remove();
  358. $popup = undefined;
  359. settings.onRemove.call($popup, element);
  360. }
  361. },
  362. save: {
  363. conditions: function() {
  364. module.cache = {
  365. title: $module.attr('title')
  366. };
  367. if (module.cache.title) {
  368. $module.removeAttr('title');
  369. }
  370. module.verbose('Saving original attributes', module.cache.title);
  371. }
  372. },
  373. restore: {
  374. conditions: function() {
  375. if(module.cache && module.cache.title) {
  376. $module.attr('title', module.cache.title);
  377. module.verbose('Restoring original attributes', module.cache.title);
  378. }
  379. return true;
  380. }
  381. },
  382. supports: {
  383. svg: function() {
  384. return (typeof SVGGraphicsElement === 'undefined');
  385. }
  386. },
  387. animate: {
  388. show: function(callback) {
  389. callback = $.isFunction(callback) ? callback : function(){};
  390. if(settings.transition && $.fn.transition !== undefined && $module.transition('is supported')) {
  391. module.set.visible();
  392. $popup
  393. .transition({
  394. animation : settings.transition + ' in',
  395. queue : false,
  396. debug : settings.debug,
  397. verbose : settings.verbose,
  398. duration : settings.duration,
  399. onComplete : function() {
  400. module.bind.close();
  401. callback.call($popup, element);
  402. settings.onVisible.call($popup, element);
  403. }
  404. })
  405. ;
  406. }
  407. else {
  408. module.error(error.noTransition);
  409. }
  410. },
  411. hide: function(callback) {
  412. callback = $.isFunction(callback) ? callback : function(){};
  413. module.debug('Hiding pop-up');
  414. if(settings.onHide.call($popup, element) === false) {
  415. module.debug('onHide callback returned false, cancelling popup animation');
  416. return;
  417. }
  418. if(settings.transition && $.fn.transition !== undefined && $module.transition('is supported')) {
  419. $popup
  420. .transition({
  421. animation : settings.transition + ' out',
  422. queue : false,
  423. duration : settings.duration,
  424. debug : settings.debug,
  425. verbose : settings.verbose,
  426. onComplete : function() {
  427. module.reset();
  428. callback.call($popup, element);
  429. settings.onHidden.call($popup, element);
  430. }
  431. })
  432. ;
  433. }
  434. else {
  435. module.error(error.noTransition);
  436. }
  437. }
  438. },
  439. change: {
  440. content: function(html) {
  441. $popup.html(html);
  442. }
  443. },
  444. get: {
  445. html: function() {
  446. $module.removeData(metadata.html);
  447. return $module.data(metadata.html) || settings.html;
  448. },
  449. title: function() {
  450. $module.removeData(metadata.title);
  451. return $module.data(metadata.title) || settings.title;
  452. },
  453. content: function() {
  454. $module.removeData(metadata.content);
  455. return $module.data(metadata.content) || $module.attr('title') || settings.content;
  456. },
  457. variation: function() {
  458. $module.removeData(metadata.variation);
  459. return $module.data(metadata.variation) || settings.variation;
  460. },
  461. popup: function() {
  462. return $popup;
  463. },
  464. popupOffset: function() {
  465. return $popup.offset();
  466. },
  467. calculations: function() {
  468. var
  469. targetElement = $target[0],
  470. isWindow = ($boundary[0] == window),
  471. targetPosition = (settings.inline || (settings.popup && settings.movePopup))
  472. ? $target.position()
  473. : $target.offset(),
  474. screenPosition = (isWindow)
  475. ? { top: 0, left: 0 }
  476. : $boundary.offset(),
  477. calculations = {},
  478. scroll = (isWindow)
  479. ? { top: $window.scrollTop(), left: $window.scrollLeft() }
  480. : { top: 0, left: 0},
  481. screen
  482. ;
  483. calculations = {
  484. // element which is launching popup
  485. target : {
  486. element : $target[0],
  487. width : $target.outerWidth(),
  488. height : $target.outerHeight(),
  489. top : targetPosition.top,
  490. left : targetPosition.left,
  491. margin : {}
  492. },
  493. // popup itself
  494. popup : {
  495. width : $popup.outerWidth(),
  496. height : $popup.outerHeight()
  497. },
  498. // offset container (or 3d context)
  499. parent : {
  500. width : $offsetParent.outerWidth(),
  501. height : $offsetParent.outerHeight()
  502. },
  503. // screen boundaries
  504. screen : {
  505. top : screenPosition.top,
  506. left : screenPosition.left,
  507. scroll: {
  508. top : scroll.top,
  509. left : scroll.left
  510. },
  511. width : $boundary.width(),
  512. height : $boundary.height()
  513. }
  514. };
  515. // add in container calcs if fluid
  516. if( settings.setFluidWidth && module.is.fluid() ) {
  517. calculations.container = {
  518. width: $popup.parent().outerWidth()
  519. };
  520. calculations.popup.width = calculations.container.width;
  521. }
  522. // add in margins if inline
  523. calculations.target.margin.top = (settings.inline)
  524. ? parseInt( window.getComputedStyle(targetElement).getPropertyValue('margin-top'), 10)
  525. : 0
  526. ;
  527. calculations.target.margin.left = (settings.inline)
  528. ? module.is.rtl()
  529. ? parseInt( window.getComputedStyle(targetElement).getPropertyValue('margin-right'), 10)
  530. : parseInt( window.getComputedStyle(targetElement).getPropertyValue('margin-left'), 10)
  531. : 0
  532. ;
  533. // calculate screen boundaries
  534. screen = calculations.screen;
  535. calculations.boundary = {
  536. top : screen.top + screen.scroll.top,
  537. bottom : screen.top + screen.scroll.top + screen.height,
  538. left : screen.left + screen.scroll.left,
  539. right : screen.left + screen.scroll.left + screen.width
  540. };
  541. return calculations;
  542. },
  543. id: function() {
  544. return id;
  545. },
  546. startEvent: function() {
  547. if(settings.on == 'hover') {
  548. return 'mouseenter';
  549. }
  550. else if(settings.on == 'focus') {
  551. return 'focus';
  552. }
  553. return false;
  554. },
  555. scrollEvent: function() {
  556. return 'scroll';
  557. },
  558. endEvent: function() {
  559. if(settings.on == 'hover') {
  560. return 'mouseleave';
  561. }
  562. else if(settings.on == 'focus') {
  563. return 'blur';
  564. }
  565. return false;
  566. },
  567. distanceFromBoundary: function(offset, calculations) {
  568. var
  569. distanceFromBoundary = {},
  570. popup,
  571. boundary
  572. ;
  573. calculations = calculations || module.get.calculations();
  574. // shorthand
  575. popup = calculations.popup;
  576. boundary = calculations.boundary;
  577. if(offset) {
  578. distanceFromBoundary = {
  579. top : (offset.top - boundary.top),
  580. left : (offset.left - boundary.left),
  581. right : (boundary.right - (offset.left + popup.width) ),
  582. bottom : (boundary.bottom - (offset.top + popup.height) )
  583. };
  584. module.verbose('Distance from boundaries determined', offset, distanceFromBoundary);
  585. }
  586. return distanceFromBoundary;
  587. },
  588. offsetParent: function($element) {
  589. var
  590. element = ($element !== undefined)
  591. ? $element[0]
  592. : $target[0],
  593. parentNode = element.parentNode,
  594. $node = $(parentNode)
  595. ;
  596. if(parentNode) {
  597. var
  598. is2D = ($node.css('transform') === 'none'),
  599. isStatic = ($node.css('position') === 'static'),
  600. isHTML = $node.is('html')
  601. ;
  602. while(parentNode && !isHTML && isStatic && is2D) {
  603. parentNode = parentNode.parentNode;
  604. $node = $(parentNode);
  605. is2D = ($node.css('transform') === 'none');
  606. isStatic = ($node.css('position') === 'static');
  607. isHTML = $node.is('html');
  608. }
  609. }
  610. return ($node && $node.length > 0)
  611. ? $node
  612. : $()
  613. ;
  614. },
  615. positions: function() {
  616. return {
  617. 'top left' : false,
  618. 'top center' : false,
  619. 'top right' : false,
  620. 'bottom left' : false,
  621. 'bottom center' : false,
  622. 'bottom right' : false,
  623. 'left center' : false,
  624. 'right center' : false
  625. };
  626. },
  627. nextPosition: function(position) {
  628. var
  629. positions = position.split(' '),
  630. verticalPosition = positions[0],
  631. horizontalPosition = positions[1],
  632. opposite = {
  633. top : 'bottom',
  634. bottom : 'top',
  635. left : 'right',
  636. right : 'left'
  637. },
  638. adjacent = {
  639. left : 'center',
  640. center : 'right',
  641. right : 'left'
  642. },
  643. backup = {
  644. 'top left' : 'top center',
  645. 'top center' : 'top right',
  646. 'top right' : 'right center',
  647. 'right center' : 'bottom right',
  648. 'bottom right' : 'bottom center',
  649. 'bottom center' : 'bottom left',
  650. 'bottom left' : 'left center',
  651. 'left center' : 'top left'
  652. },
  653. adjacentsAvailable = (verticalPosition == 'top' || verticalPosition == 'bottom'),
  654. oppositeTried = false,
  655. adjacentTried = false,
  656. nextPosition = false
  657. ;
  658. if(!triedPositions) {
  659. module.verbose('All available positions available');
  660. triedPositions = module.get.positions();
  661. }
  662. module.debug('Recording last position tried', position);
  663. triedPositions[position] = true;
  664. if(settings.prefer === 'opposite') {
  665. nextPosition = [opposite[verticalPosition], horizontalPosition];
  666. nextPosition = nextPosition.join(' ');
  667. oppositeTried = (triedPositions[nextPosition] === true);
  668. module.debug('Trying opposite strategy', nextPosition);
  669. }
  670. if((settings.prefer === 'adjacent') && adjacentsAvailable ) {
  671. nextPosition = [verticalPosition, adjacent[horizontalPosition]];
  672. nextPosition = nextPosition.join(' ');
  673. adjacentTried = (triedPositions[nextPosition] === true);
  674. module.debug('Trying adjacent strategy', nextPosition);
  675. }
  676. if(adjacentTried || oppositeTried) {
  677. module.debug('Using backup position', nextPosition);
  678. nextPosition = backup[position];
  679. }
  680. return nextPosition;
  681. }
  682. },
  683. set: {
  684. position: function(position, calculations) {
  685. // exit conditions
  686. if($target.length === 0 || $popup.length === 0) {
  687. module.error(error.notFound);
  688. return;
  689. }
  690. var
  691. offset,
  692. distanceAway,
  693. target,
  694. popup,
  695. parent,
  696. positioning,
  697. popupOffset,
  698. distanceFromBoundary
  699. ;
  700. calculations = calculations || module.get.calculations();
  701. position = position || $module.data(metadata.position) || settings.position;
  702. offset = $module.data(metadata.offset) || settings.offset;
  703. distanceAway = settings.distanceAway;
  704. // shorthand
  705. target = calculations.target;
  706. popup = calculations.popup;
  707. parent = calculations.parent;
  708. if(target.width === 0 && target.height === 0 && !module.is.svg(target.element)) {
  709. module.debug('Popup target is hidden, no action taken');
  710. return false;
  711. }
  712. if(settings.inline) {
  713. module.debug('Adding margin to calculation', target.margin);
  714. if(position == 'left center' || position == 'right center') {
  715. offset += target.margin.top;
  716. distanceAway += -target.margin.left;
  717. }
  718. else if (position == 'top left' || position == 'top center' || position == 'top right') {
  719. offset += target.margin.left;
  720. distanceAway -= target.margin.top;
  721. }
  722. else {
  723. offset += target.margin.left;
  724. distanceAway += target.margin.top;
  725. }
  726. }
  727. module.debug('Determining popup position from calculations', position, calculations);
  728. if (module.is.rtl()) {
  729. position = position.replace(/left|right/g, function (match) {
  730. return (match == 'left')
  731. ? 'right'
  732. : 'left'
  733. ;
  734. });
  735. module.debug('RTL: Popup position updated', position);
  736. }
  737. // if last attempt use specified last resort position
  738. if(searchDepth == settings.maxSearchDepth && typeof settings.lastResort === 'string') {
  739. position = settings.lastResort;
  740. }
  741. switch (position) {
  742. case 'top left':
  743. positioning = {
  744. top : 'auto',
  745. bottom : parent.height - target.top + distanceAway,
  746. left : target.left + offset,
  747. right : 'auto'
  748. };
  749. break;
  750. case 'top center':
  751. positioning = {
  752. bottom : parent.height - target.top + distanceAway,
  753. left : target.left + (target.width / 2) - (popup.width / 2) + offset,
  754. top : 'auto',
  755. right : 'auto'
  756. };
  757. break;
  758. case 'top right':
  759. positioning = {
  760. bottom : parent.height - target.top + distanceAway,
  761. right : parent.width - target.left - target.width - offset,
  762. top : 'auto',
  763. left : 'auto'
  764. };
  765. break;
  766. case 'left center':
  767. positioning = {
  768. top : target.top + (target.height / 2) - (popup.height / 2) + offset,
  769. right : parent.width - target.left + distanceAway,
  770. left : 'auto',
  771. bottom : 'auto'
  772. };
  773. break;
  774. case 'right center':
  775. positioning = {
  776. top : target.top + (target.height / 2) - (popup.height / 2) + offset,
  777. left : target.left + target.width + distanceAway,
  778. bottom : 'auto',
  779. right : 'auto'
  780. };
  781. break;
  782. case 'bottom left':
  783. positioning = {
  784. top : target.top + target.height + distanceAway,
  785. left : target.left + offset,
  786. bottom : 'auto',
  787. right : 'auto'
  788. };
  789. break;
  790. case 'bottom center':
  791. positioning = {
  792. top : target.top + target.height + distanceAway,
  793. left : target.left + (target.width / 2) - (popup.width / 2) + offset,
  794. bottom : 'auto',
  795. right : 'auto'
  796. };
  797. break;
  798. case 'bottom right':
  799. positioning = {
  800. top : target.top + target.height + distanceAway,
  801. right : parent.width - target.left - target.width - offset,
  802. left : 'auto',
  803. bottom : 'auto'
  804. };
  805. break;
  806. }
  807. if(positioning === undefined) {
  808. module.error(error.invalidPosition, position);
  809. }
  810. module.debug('Calculated popup positioning values', positioning);
  811. // tentatively place on stage
  812. $popup
  813. .css(positioning)
  814. .removeClass(className.position)
  815. .addClass(position)
  816. .addClass(className.loading)
  817. ;
  818. popupOffset = module.get.popupOffset();
  819. // see if any boundaries are surpassed with this tentative position
  820. distanceFromBoundary = module.get.distanceFromBoundary(popupOffset, calculations);
  821. if( module.is.offstage(distanceFromBoundary, position) ) {
  822. module.debug('Position is outside viewport', position);
  823. if(searchDepth < settings.maxSearchDepth) {
  824. searchDepth++;
  825. position = module.get.nextPosition(position);
  826. module.debug('Trying new position', position);
  827. return ($popup)
  828. ? module.set.position(position, calculations)
  829. : false
  830. ;
  831. }
  832. else {
  833. if(settings.lastResort) {
  834. module.debug('No position found, showing with last position');
  835. }
  836. else {
  837. module.debug('Popup could not find a position to display', $popup);
  838. module.error(error.cannotPlace, element);
  839. module.remove.attempts();
  840. module.remove.loading();
  841. module.reset();
  842. settings.onUnplaceable.call($popup, element);
  843. return false;
  844. }
  845. }
  846. }
  847. module.debug('Position is on stage', position);
  848. module.remove.attempts();
  849. module.remove.loading();
  850. if( settings.setFluidWidth && module.is.fluid() ) {
  851. module.set.fluidWidth(calculations);
  852. }
  853. return true;
  854. },
  855. fluidWidth: function(calculations) {
  856. calculations = calculations || module.get.calculations();
  857. module.debug('Automatically setting element width to parent width', calculations.parent.width);
  858. $popup.css('width', calculations.container.width);
  859. },
  860. variation: function(variation) {
  861. variation = variation || module.get.variation();
  862. if(variation && module.has.popup() ) {
  863. module.verbose('Adding variation to popup', variation);
  864. $popup.addClass(variation);
  865. }
  866. },
  867. visible: function() {
  868. $module.addClass(className.visible);
  869. }
  870. },
  871. remove: {
  872. loading: function() {
  873. $popup.removeClass(className.loading);
  874. },
  875. variation: function(variation) {
  876. variation = variation || module.get.variation();
  877. if(variation) {
  878. module.verbose('Removing variation', variation);
  879. $popup.removeClass(variation);
  880. }
  881. },
  882. visible: function() {
  883. $module.removeClass(className.visible);
  884. },
  885. attempts: function() {
  886. module.verbose('Resetting all searched positions');
  887. searchDepth = 0;
  888. triedPositions = false;
  889. }
  890. },
  891. bind: {
  892. events: function() {
  893. module.debug('Binding popup events to module');
  894. if(settings.on == 'click') {
  895. $module
  896. .on('click' + eventNamespace, module.toggle)
  897. ;
  898. }
  899. if(settings.on == 'hover' && hasTouch) {
  900. $module
  901. .on('touchstart' + eventNamespace, module.event.touchstart)
  902. ;
  903. }
  904. if( module.get.startEvent() ) {
  905. $module
  906. .on(module.get.startEvent() + eventNamespace, module.event.start)
  907. .on(module.get.endEvent() + eventNamespace, module.event.end)
  908. ;
  909. }
  910. if(settings.target) {
  911. module.debug('Target set to element', $target);
  912. }
  913. $window.on('resize' + elementNamespace, module.event.resize);
  914. },
  915. popup: function() {
  916. module.verbose('Allowing hover events on popup to prevent closing');
  917. if( $popup && module.has.popup() ) {
  918. $popup
  919. .on('mouseenter' + eventNamespace, module.event.start)
  920. .on('mouseleave' + eventNamespace, module.event.end)
  921. ;
  922. }
  923. },
  924. close: function() {
  925. if(settings.hideOnScroll === true || (settings.hideOnScroll == 'auto' && settings.on != 'click')) {
  926. module.bind.closeOnScroll();
  927. }
  928. if(settings.on == 'hover' && openedWithTouch) {
  929. module.bind.touchClose();
  930. }
  931. if(settings.on == 'click' && settings.closable) {
  932. module.bind.clickaway();
  933. }
  934. },
  935. closeOnScroll: function() {
  936. module.verbose('Binding scroll close event to document');
  937. $scrollContext
  938. .one(module.get.scrollEvent() + elementNamespace, module.event.hideGracefully)
  939. ;
  940. },
  941. touchClose: function() {
  942. module.verbose('Binding popup touchclose event to document');
  943. $document
  944. .on('touchstart' + elementNamespace, function(event) {
  945. module.verbose('Touched away from popup');
  946. module.event.hideGracefully.call(element, event);
  947. })
  948. ;
  949. },
  950. clickaway: function() {
  951. module.verbose('Binding popup close event to document');
  952. $document
  953. .on('click' + elementNamespace, function(event) {
  954. module.verbose('Clicked away from popup');
  955. module.event.hideGracefully.call(element, event);
  956. })
  957. ;
  958. }
  959. },
  960. unbind: {
  961. events: function() {
  962. $window
  963. .off(elementNamespace)
  964. ;
  965. $module
  966. .off(eventNamespace)
  967. ;
  968. },
  969. close: function() {
  970. $document
  971. .off(elementNamespace)
  972. ;
  973. $scrollContext
  974. .off(elementNamespace)
  975. ;
  976. },
  977. },
  978. has: {
  979. popup: function() {
  980. return ($popup && $popup.length > 0);
  981. }
  982. },
  983. is: {
  984. offstage: function(distanceFromBoundary, position) {
  985. var
  986. offstage = []
  987. ;
  988. // return boundaries that have been surpassed
  989. $.each(distanceFromBoundary, function(direction, distance) {
  990. if(distance < -settings.jitter) {
  991. module.debug('Position exceeds allowable distance from edge', direction, distance, position);
  992. offstage.push(direction);
  993. }
  994. });
  995. if(offstage.length > 0) {
  996. return true;
  997. }
  998. else {
  999. return false;
  1000. }
  1001. },
  1002. svg: function(element) {
  1003. return module.supports.svg() && (element instanceof SVGGraphicsElement);
  1004. },
  1005. active: function() {
  1006. return $module.hasClass(className.active);
  1007. },
  1008. animating: function() {
  1009. return ($popup !== undefined && $popup.hasClass(className.animating) );
  1010. },
  1011. fluid: function() {
  1012. return ($popup !== undefined && $popup.hasClass(className.fluid));
  1013. },
  1014. visible: function() {
  1015. return ($popup !== undefined && $popup.hasClass(className.popupVisible));
  1016. },
  1017. dropdown: function() {
  1018. return $module.hasClass(className.dropdown);
  1019. },
  1020. hidden: function() {
  1021. return !module.is.visible();
  1022. },
  1023. rtl: function () {
  1024. return $module.css('direction') == 'rtl';
  1025. }
  1026. },
  1027. reset: function() {
  1028. module.remove.visible();
  1029. if(settings.preserve) {
  1030. if($.fn.transition !== undefined) {
  1031. $popup
  1032. .transition('remove transition')
  1033. ;
  1034. }
  1035. }
  1036. else {
  1037. module.removePopup();
  1038. }
  1039. },
  1040. setting: function(name, value) {
  1041. if( $.isPlainObject(name) ) {
  1042. $.extend(true, settings, name);
  1043. }
  1044. else if(value !== undefined) {
  1045. settings[name] = value;
  1046. }
  1047. else {
  1048. return settings[name];
  1049. }
  1050. },
  1051. internal: function(name, value) {
  1052. if( $.isPlainObject(name) ) {
  1053. $.extend(true, module, name);
  1054. }
  1055. else if(value !== undefined) {
  1056. module[name] = value;
  1057. }
  1058. else {
  1059. return module[name];
  1060. }
  1061. },
  1062. debug: function() {
  1063. if(!settings.silent && settings.debug) {
  1064. if(settings.performance) {
  1065. module.performance.log(arguments);
  1066. }
  1067. else {
  1068. module.debug = Function.prototype.bind.call(console.info, console, settings.name + ':');
  1069. module.debug.apply(console, arguments);
  1070. }
  1071. }
  1072. },
  1073. verbose: function() {
  1074. if(!settings.silent && settings.verbose && settings.debug) {
  1075. if(settings.performance) {
  1076. module.performance.log(arguments);
  1077. }
  1078. else {
  1079. module.verbose = Function.prototype.bind.call(console.info, console, settings.name + ':');
  1080. module.verbose.apply(console, arguments);
  1081. }
  1082. }
  1083. },
  1084. error: function() {
  1085. if(!settings.silent) {
  1086. module.error = Function.prototype.bind.call(console.error, console, settings.name + ':');
  1087. module.error.apply(console, arguments);
  1088. }
  1089. },
  1090. performance: {
  1091. log: function(message) {
  1092. var
  1093. currentTime,
  1094. executionTime,
  1095. previousTime
  1096. ;
  1097. if(settings.performance) {
  1098. currentTime = new Date().getTime();
  1099. previousTime = time || currentTime;
  1100. executionTime = currentTime - previousTime;
  1101. time = currentTime;
  1102. performance.push({
  1103. 'Name' : message[0],
  1104. 'Arguments' : [].slice.call(message, 1) || '',
  1105. 'Element' : element,
  1106. 'Execution Time' : executionTime
  1107. });
  1108. }
  1109. clearTimeout(module.performance.timer);
  1110. module.performance.timer = setTimeout(module.performance.display, 500);
  1111. },
  1112. display: function() {
  1113. var
  1114. title = settings.name + ':',
  1115. totalTime = 0
  1116. ;
  1117. time = false;
  1118. clearTimeout(module.performance.timer);
  1119. $.each(performance, function(index, data) {
  1120. totalTime += data['Execution Time'];
  1121. });
  1122. title += ' ' + totalTime + 'ms';
  1123. if(moduleSelector) {
  1124. title += ' \'' + moduleSelector + '\'';
  1125. }
  1126. if( (console.group !== undefined || console.table !== undefined) && performance.length > 0) {
  1127. console.groupCollapsed(title);
  1128. if(console.table) {
  1129. console.table(performance);
  1130. }
  1131. else {
  1132. $.each(performance, function(index, data) {
  1133. console.log(data['Name'] + ': ' + data['Execution Time']+'ms');
  1134. });
  1135. }
  1136. console.groupEnd();
  1137. }
  1138. performance = [];
  1139. }
  1140. },
  1141. invoke: function(query, passedArguments, context) {
  1142. var
  1143. object = instance,
  1144. maxDepth,
  1145. found,
  1146. response
  1147. ;
  1148. passedArguments = passedArguments || queryArguments;
  1149. context = element || context;
  1150. if(typeof query == 'string' && object !== undefined) {
  1151. query = query.split(/[\. ]/);
  1152. maxDepth = query.length - 1;
  1153. $.each(query, function(depth, value) {
  1154. var camelCaseValue = (depth != maxDepth)
  1155. ? value + query[depth + 1].charAt(0).toUpperCase() + query[depth + 1].slice(1)
  1156. : query
  1157. ;
  1158. if( $.isPlainObject( object[camelCaseValue] ) && (depth != maxDepth) ) {
  1159. object = object[camelCaseValue];
  1160. }
  1161. else if( object[camelCaseValue] !== undefined ) {
  1162. found = object[camelCaseValue];
  1163. return false;
  1164. }
  1165. else if( $.isPlainObject( object[value] ) && (depth != maxDepth) ) {
  1166. object = object[value];
  1167. }
  1168. else if( object[value] !== undefined ) {
  1169. found = object[value];
  1170. return false;
  1171. }
  1172. else {
  1173. return false;
  1174. }
  1175. });
  1176. }
  1177. if ( $.isFunction( found ) ) {
  1178. response = found.apply(context, passedArguments);
  1179. }
  1180. else if(found !== undefined) {
  1181. response = found;
  1182. }
  1183. if($.isArray(returnedValue)) {
  1184. returnedValue.push(response);
  1185. }
  1186. else if(returnedValue !== undefined) {
  1187. returnedValue = [returnedValue, response];
  1188. }
  1189. else if(response !== undefined) {
  1190. returnedValue = response;
  1191. }
  1192. return found;
  1193. }
  1194. };
  1195. if(methodInvoked) {
  1196. if(instance === undefined) {
  1197. module.initialize();
  1198. }
  1199. module.invoke(query);
  1200. }
  1201. else {
  1202. if(instance !== undefined) {
  1203. instance.invoke('destroy');
  1204. }
  1205. module.initialize();
  1206. }
  1207. })
  1208. ;
  1209. return (returnedValue !== undefined)
  1210. ? returnedValue
  1211. : this
  1212. ;
  1213. };
  1214. $.fn.popup.settings = {
  1215. name : 'Popup',
  1216. // module settings
  1217. silent : false,
  1218. debug : false,
  1219. verbose : false,
  1220. performance : true,
  1221. namespace : 'popup',
  1222. // whether it should use dom mutation observers
  1223. observeChanges : true,
  1224. // callback only when element added to dom
  1225. onCreate : function(){},
  1226. // callback before element removed from dom
  1227. onRemove : function(){},
  1228. // callback before show animation
  1229. onShow : function(){},
  1230. // callback after show animation
  1231. onVisible : function(){},
  1232. // callback before hide animation
  1233. onHide : function(){},
  1234. // callback when popup cannot be positioned in visible screen
  1235. onUnplaceable : function(){},
  1236. // callback after hide animation
  1237. onHidden : function(){},
  1238. // when to show popup
  1239. on : 'hover',
  1240. // element to use to determine if popup is out of boundary
  1241. boundary : window,
  1242. // whether to add touchstart events when using hover
  1243. addTouchEvents : true,
  1244. // default position relative to element
  1245. position : 'top left',
  1246. // name of variation to use
  1247. variation : '',
  1248. // whether popup should be moved to context
  1249. movePopup : true,
  1250. // element which popup should be relative to
  1251. target : false,
  1252. // jq selector or element that should be used as popup
  1253. popup : false,
  1254. // popup should remain inline next to activator
  1255. inline : false,
  1256. // popup should be removed from page on hide
  1257. preserve : false,
  1258. // popup should not close when being hovered on
  1259. hoverable : false,
  1260. // explicitly set content
  1261. content : false,
  1262. // explicitly set html
  1263. html : false,
  1264. // explicitly set title
  1265. title : false,
  1266. // whether automatically close on clickaway when on click
  1267. closable : true,
  1268. // automatically hide on scroll
  1269. hideOnScroll : 'auto',
  1270. // hide other popups on show
  1271. exclusive : false,
  1272. // context to attach popups
  1273. context : 'body',
  1274. // context for binding scroll events
  1275. scrollContext : window,
  1276. // position to prefer when calculating new position
  1277. prefer : 'opposite',
  1278. // specify position to appear even if it doesn't fit
  1279. lastResort : false,
  1280. // delay used to prevent accidental refiring of animations due to user error
  1281. delay : {
  1282. show : 50,
  1283. hide : 70
  1284. },
  1285. // whether fluid variation should assign width explicitly
  1286. setFluidWidth : true,
  1287. // transition settings
  1288. duration : 200,
  1289. transition : 'scale',
  1290. // distance away from activating element in px
  1291. distanceAway : 0,
  1292. // number of pixels an element is allowed to be "offstage" for a position to be chosen (allows for rounding)
  1293. jitter : 2,
  1294. // offset on aligning axis from calculated position
  1295. offset : 0,
  1296. // maximum times to look for a position before failing (9 positions total)
  1297. maxSearchDepth : 15,
  1298. error: {
  1299. invalidPosition : 'The position you specified is not a valid position',
  1300. cannotPlace : 'Popup does not fit within the boundaries of the viewport',
  1301. method : 'The method you called is not defined.',
  1302. noTransition : 'This module requires ui transitions <https://github.com/Semantic-Org/UI-Transition>',
  1303. notFound : 'The target or popup you specified does not exist on the page'
  1304. },
  1305. metadata: {
  1306. activator : 'activator',
  1307. content : 'content',
  1308. html : 'html',
  1309. offset : 'offset',
  1310. position : 'position',
  1311. title : 'title',
  1312. variation : 'variation'
  1313. },
  1314. className : {
  1315. active : 'active',
  1316. animating : 'animating',
  1317. dropdown : 'dropdown',
  1318. fluid : 'fluid',
  1319. loading : 'loading',
  1320. popup : 'ui popup',
  1321. position : 'top left center bottom right',
  1322. visible : 'visible',
  1323. popupVisible : 'visible'
  1324. },
  1325. selector : {
  1326. popup : '.ui.popup'
  1327. },
  1328. templates: {
  1329. escape: function(string) {
  1330. var
  1331. badChars = /[&<>"'`]/g,
  1332. shouldEscape = /[&<>"'`]/,
  1333. escape = {
  1334. "&": "&amp;",
  1335. "<": "&lt;",
  1336. ">": "&gt;",
  1337. '"': "&quot;",
  1338. "'": "&#x27;",
  1339. "`": "&#x60;"
  1340. },
  1341. escapedChar = function(chr) {
  1342. return escape[chr];
  1343. }
  1344. ;
  1345. if(shouldEscape.test(string)) {
  1346. return string.replace(badChars, escapedChar);
  1347. }
  1348. return string;
  1349. },
  1350. popup: function(text) {
  1351. var
  1352. html = '',
  1353. escape = $.fn.popup.settings.templates.escape
  1354. ;
  1355. if(typeof text !== undefined) {
  1356. if(typeof text.title !== undefined && text.title) {
  1357. text.title = escape(text.title);
  1358. html += '<div class="header">' + text.title + '</div>';
  1359. }
  1360. if(typeof text.content !== undefined && text.content) {
  1361. text.content = escape(text.content);
  1362. html += '<div class="content">' + text.content + '</div>';
  1363. }
  1364. }
  1365. return html;
  1366. }
  1367. }
  1368. };
  1369. })( jQuery, window, document );