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.

1409 lines
45 KiB

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