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.

1353 lines
43 KiB

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