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.

1256 lines
40 KiB

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