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.

1253 lines
40 KiB

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