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.

1272 lines
41 KiB

9 years ago
9 years ago
9 years ago
10 years ago
9 years ago
10 years ago
10 years ago
10 years ago
10 years ago
9 years ago
10 years ago
9 years ago
9 years ago
10 years ago
9 years ago
10 years ago
10 years ago
10 years ago
9 years ago
10 years ago
10 years ago
9 years ago
10 years ago
9 years ago
10 years ago
9 years ago
10 years ago
10 years ago
10 years ago
9 years ago
10 years ago
10 years ago
9 years ago
9 years ago
10 years ago
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
9 years ago
9 years ago
10 years ago
9 years ago
10 years ago
10 years ago
9 years ago
10 years ago
9 years ago
10 years ago
10 years ago
9 years ago
10 years ago
10 years ago
10 years ago
9 years ago
10 years ago
9 years ago
10 years ago
9 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
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
9 years ago
10 years ago
10 years ago
9 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
9 years ago
9 years ago
10 years ago
10 years ago
10 years ago
10 years ago
9 years ago
10 years ago
10 years ago
9 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
9 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
9 years ago
10 years ago
10 years ago
10 years ago
10 years ago
9 years ago
9 years ago
10 years ago
9 years ago
10 years ago
10 years ago
10 years ago
9 years ago
9 years ago
10 years ago
9 years ago
10 years ago
9 years ago
10 years ago
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
10 years ago
10 years ago
10 years ago
10 years ago
9 years ago
10 years ago
9 years ago
10 years ago
10 years ago
10 years ago
  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. // exit conditions
  566. if($target.length === 0 || $popup.length === 0) {
  567. module.error(error.notFound);
  568. return;
  569. }
  570. var
  571. windowWidth = $(window).width(),
  572. windowHeight = $(window).height(),
  573. targetWidth = $target.outerWidth(),
  574. targetHeight = $target.outerHeight(),
  575. popupWidth = $popup.outerWidth(),
  576. popupHeight = $popup.outerHeight(),
  577. parentWidth = $offsetParent.outerWidth(),
  578. parentHeight = $offsetParent.outerHeight(),
  579. distanceAway = settings.distanceAway,
  580. targetElement = $target[0],
  581. marginTop = (settings.inline)
  582. ? parseInt( window.getComputedStyle(targetElement).getPropertyValue('margin-top'), 10)
  583. : 0,
  584. marginLeft = (settings.inline)
  585. ? module.is.rtl()
  586. ? parseInt( window.getComputedStyle(targetElement).getPropertyValue('margin-right'), 10)
  587. : parseInt( window.getComputedStyle(targetElement).getPropertyValue('margin-left') , 10)
  588. : 0,
  589. target = (settings.inline || settings.popup)
  590. ? $target.position()
  591. : $target.offset(),
  592. computedPosition,
  593. positioning,
  594. offstagePosition
  595. ;
  596. position = position || $module.data(metadata.position) || settings.position;
  597. arrowOffset = arrowOffset || $module.data(metadata.offset) || settings.offset;
  598. if(target.top === 0 && target.left === 0) {
  599. module.debug('Popup target is hidden, no action taken');
  600. return false;
  601. }
  602. if(searchDepth == settings.maxSearchDepth && settings.lastResort) {
  603. module.debug('Using last resort position to display', settings.lastResort);
  604. position = settings.lastResort;
  605. }
  606. if(settings.inline) {
  607. module.debug('Adding targets margin to calculation');
  608. if(position == 'left center' || position == 'right center') {
  609. arrowOffset += marginTop;
  610. distanceAway += -marginLeft;
  611. }
  612. else if (position == 'top left' || position == 'top center' || position == 'top right') {
  613. arrowOffset += marginLeft;
  614. distanceAway -= marginTop;
  615. }
  616. else {
  617. arrowOffset += marginLeft;
  618. distanceAway += marginTop;
  619. }
  620. }
  621. module.debug('Calculating popup positioning', position);
  622. computedPosition = position;
  623. if (module.is.rtl()) {
  624. computedPosition = computedPosition.replace(/left|right/g, function (match) {
  625. return (match == 'left')
  626. ? 'right'
  627. : 'left'
  628. ;
  629. });
  630. module.debug('RTL: Popup positioning updated', computedPosition);
  631. }
  632. switch (computedPosition) {
  633. case 'top left':
  634. positioning = {
  635. top : 'auto',
  636. bottom : parentHeight - target.top + distanceAway,
  637. left : target.left + arrowOffset,
  638. right : 'auto'
  639. };
  640. break;
  641. case 'top center':
  642. positioning = {
  643. bottom : parentHeight - target.top + distanceAway,
  644. left : target.left + (targetWidth / 2) - (popupWidth / 2) + arrowOffset,
  645. top : 'auto',
  646. right : 'auto'
  647. };
  648. break;
  649. case 'top right':
  650. positioning = {
  651. bottom : parentHeight - target.top + distanceAway,
  652. right : parentWidth - target.left - targetWidth - arrowOffset,
  653. top : 'auto',
  654. left : 'auto'
  655. };
  656. break;
  657. case 'left center':
  658. positioning = {
  659. top : target.top + (targetHeight / 2) - (popupHeight / 2) + arrowOffset,
  660. right : parentWidth - target.left + distanceAway,
  661. left : 'auto',
  662. bottom : 'auto'
  663. };
  664. break;
  665. case 'right center':
  666. positioning = {
  667. top : target.top + (targetHeight / 2) - (popupHeight / 2) + arrowOffset,
  668. left : target.left + targetWidth + distanceAway,
  669. bottom : 'auto',
  670. right : 'auto'
  671. };
  672. break;
  673. case 'bottom left':
  674. positioning = {
  675. top : target.top + targetHeight + distanceAway,
  676. left : target.left + arrowOffset,
  677. bottom : 'auto',
  678. right : 'auto'
  679. };
  680. break;
  681. case 'bottom center':
  682. positioning = {
  683. top : target.top + targetHeight + distanceAway,
  684. left : target.left + (targetWidth / 2) - (popupWidth / 2) + arrowOffset,
  685. bottom : 'auto',
  686. right : 'auto'
  687. };
  688. break;
  689. case 'bottom right':
  690. positioning = {
  691. top : target.top + targetHeight + distanceAway,
  692. right : parentWidth - target.left - targetWidth - arrowOffset,
  693. left : 'auto',
  694. bottom : 'auto'
  695. };
  696. break;
  697. }
  698. if(positioning === undefined) {
  699. module.error(error.invalidPosition, position);
  700. }
  701. module.debug('Calculated popup positioning values', positioning);
  702. // tentatively place on stage
  703. $popup
  704. .css(positioning)
  705. .removeClass(className.position)
  706. .addClass(position)
  707. .addClass(className.loading)
  708. ;
  709. // check if is offstage
  710. offstagePosition = module.get.offstagePosition(position);
  711. // recursively find new positioning
  712. if(offstagePosition) {
  713. module.debug('Popup cant fit into viewport', offstagePosition);
  714. if(searchDepth < settings.maxSearchDepth) {
  715. searchDepth++;
  716. position = module.get.nextPosition(position);
  717. module.debug('Trying new position', position);
  718. return ($popup)
  719. ? module.set.position(position)
  720. : false
  721. ;
  722. }
  723. else if(!settings.lastResort) {
  724. module.debug('Popup could not find a position in view', $popup);
  725. // module.error(error.cannotPlace, element);
  726. module.remove.attempts();
  727. module.remove.loading();
  728. module.reset();
  729. return false;
  730. }
  731. }
  732. module.debug('Position is on stage', position);
  733. module.remove.attempts();
  734. module.set.fluidWidth();
  735. module.remove.loading();
  736. return true;
  737. },
  738. fluidWidth: function() {
  739. if( settings.setFluidWidth && $popup.hasClass(className.fluid) ) {
  740. $popup.css('width', $offsetParent.width());
  741. }
  742. },
  743. visible: function() {
  744. $module.addClass(className.visible);
  745. }
  746. },
  747. remove: {
  748. loading: function() {
  749. $popup.removeClass(className.loading);
  750. },
  751. visible: function() {
  752. $module.removeClass(className.visible);
  753. },
  754. attempts: function() {
  755. module.verbose('Resetting all searched positions');
  756. searchDepth = 0;
  757. triedPositions = false;
  758. }
  759. },
  760. bind: {
  761. events: function() {
  762. module.debug('Binding popup events to module');
  763. if(settings.on == 'click') {
  764. $module
  765. .on('click' + eventNamespace, module.toggle)
  766. ;
  767. }
  768. else if( module.get.startEvent() ) {
  769. $module
  770. .on(module.get.startEvent() + eventNamespace, module.event.start)
  771. .on(module.get.endEvent() + eventNamespace, module.event.end)
  772. ;
  773. }
  774. if(settings.target) {
  775. module.debug('Target set to element', $target);
  776. }
  777. $window.on('resize' + elementNamespace, module.event.resize);
  778. },
  779. popup: function() {
  780. module.verbose('Allowing hover events on popup to prevent closing');
  781. if( $popup && module.has.popup() ) {
  782. $popup
  783. .on('mouseenter' + eventNamespace, module.event.start)
  784. .on('mouseleave' + eventNamespace, module.event.end)
  785. ;
  786. }
  787. },
  788. close:function() {
  789. if(settings.hideOnScroll === true || settings.hideOnScroll == 'auto' && settings.on != 'click') {
  790. $document
  791. .one(module.get.scrollEvent() + elementNamespace, module.hideGracefully)
  792. ;
  793. $context
  794. .one(module.get.scrollEvent() + elementNamespace, module.hideGracefully)
  795. ;
  796. }
  797. if(settings.on == 'click' && settings.closable) {
  798. module.verbose('Binding popup close event to document');
  799. $document
  800. .on('click' + elementNamespace, function(event) {
  801. module.verbose('Pop-up clickaway intent detected');
  802. module.hideGracefully.call(element, event);
  803. })
  804. ;
  805. }
  806. }
  807. },
  808. unbind: {
  809. close: function() {
  810. if(settings.hideOnScroll === true || settings.hideOnScroll == 'auto' && settings.on != 'click') {
  811. $document
  812. .off('scroll' + elementNamespace, module.hide)
  813. ;
  814. $context
  815. .off('scroll' + elementNamespace, module.hide)
  816. ;
  817. }
  818. if(settings.on == 'click' && settings.closable) {
  819. module.verbose('Removing close event from document');
  820. $document
  821. .off('click' + elementNamespace)
  822. ;
  823. }
  824. }
  825. },
  826. has: {
  827. popup: function() {
  828. return ($popup && $popup.length > 0);
  829. }
  830. },
  831. is: {
  832. active: function() {
  833. return $module.hasClass(className.active);
  834. },
  835. animating: function() {
  836. return ( $popup && $popup.is(':animated') || $popup.hasClass(className.animating) );
  837. },
  838. visible: function() {
  839. return $popup && $popup.is(':visible');
  840. },
  841. dropdown: function() {
  842. return $module.hasClass(className.dropdown);
  843. },
  844. hidden: function() {
  845. return !module.is.visible();
  846. },
  847. rtl: function () {
  848. return $module.css('direction') == 'rtl';
  849. }
  850. },
  851. reset: function() {
  852. module.remove.visible();
  853. if(settings.preserve) {
  854. if($.fn.transition !== undefined) {
  855. $popup
  856. .transition('remove transition')
  857. ;
  858. }
  859. }
  860. else {
  861. module.removePopup();
  862. }
  863. },
  864. setting: function(name, value) {
  865. if( $.isPlainObject(name) ) {
  866. $.extend(true, settings, name);
  867. }
  868. else if(value !== undefined) {
  869. settings[name] = value;
  870. }
  871. else {
  872. return settings[name];
  873. }
  874. },
  875. internal: function(name, value) {
  876. if( $.isPlainObject(name) ) {
  877. $.extend(true, module, name);
  878. }
  879. else if(value !== undefined) {
  880. module[name] = value;
  881. }
  882. else {
  883. return module[name];
  884. }
  885. },
  886. debug: function() {
  887. if(settings.debug) {
  888. if(settings.performance) {
  889. module.performance.log(arguments);
  890. }
  891. else {
  892. module.debug = Function.prototype.bind.call(console.info, console, settings.name + ':');
  893. module.debug.apply(console, arguments);
  894. }
  895. }
  896. },
  897. verbose: function() {
  898. if(settings.verbose && settings.debug) {
  899. if(settings.performance) {
  900. module.performance.log(arguments);
  901. }
  902. else {
  903. module.verbose = Function.prototype.bind.call(console.info, console, settings.name + ':');
  904. module.verbose.apply(console, arguments);
  905. }
  906. }
  907. },
  908. error: function() {
  909. module.error = Function.prototype.bind.call(console.error, console, settings.name + ':');
  910. module.error.apply(console, arguments);
  911. },
  912. performance: {
  913. log: function(message) {
  914. var
  915. currentTime,
  916. executionTime,
  917. previousTime
  918. ;
  919. if(settings.performance) {
  920. currentTime = new Date().getTime();
  921. previousTime = time || currentTime;
  922. executionTime = currentTime - previousTime;
  923. time = currentTime;
  924. performance.push({
  925. 'Name' : message[0],
  926. 'Arguments' : [].slice.call(message, 1) || '',
  927. 'Element' : element,
  928. 'Execution Time' : executionTime
  929. });
  930. }
  931. clearTimeout(module.performance.timer);
  932. module.performance.timer = setTimeout(module.performance.display, 500);
  933. },
  934. display: function() {
  935. var
  936. title = settings.name + ':',
  937. totalTime = 0
  938. ;
  939. time = false;
  940. clearTimeout(module.performance.timer);
  941. $.each(performance, function(index, data) {
  942. totalTime += data['Execution Time'];
  943. });
  944. title += ' ' + totalTime + 'ms';
  945. if(moduleSelector) {
  946. title += ' \'' + moduleSelector + '\'';
  947. }
  948. if( (console.group !== undefined || console.table !== undefined) && performance.length > 0) {
  949. console.groupCollapsed(title);
  950. if(console.table) {
  951. console.table(performance);
  952. }
  953. else {
  954. $.each(performance, function(index, data) {
  955. console.log(data['Name'] + ': ' + data['Execution Time']+'ms');
  956. });
  957. }
  958. console.groupEnd();
  959. }
  960. performance = [];
  961. }
  962. },
  963. invoke: function(query, passedArguments, context) {
  964. var
  965. object = instance,
  966. maxDepth,
  967. found,
  968. response
  969. ;
  970. passedArguments = passedArguments || queryArguments;
  971. context = element || context;
  972. if(typeof query == 'string' && object !== undefined) {
  973. query = query.split(/[\. ]/);
  974. maxDepth = query.length - 1;
  975. $.each(query, function(depth, value) {
  976. var camelCaseValue = (depth != maxDepth)
  977. ? value + query[depth + 1].charAt(0).toUpperCase() + query[depth + 1].slice(1)
  978. : query
  979. ;
  980. if( $.isPlainObject( object[camelCaseValue] ) && (depth != maxDepth) ) {
  981. object = object[camelCaseValue];
  982. }
  983. else if( object[camelCaseValue] !== undefined ) {
  984. found = object[camelCaseValue];
  985. return false;
  986. }
  987. else if( $.isPlainObject( object[value] ) && (depth != maxDepth) ) {
  988. object = object[value];
  989. }
  990. else if( object[value] !== undefined ) {
  991. found = object[value];
  992. return false;
  993. }
  994. else {
  995. return false;
  996. }
  997. });
  998. }
  999. if ( $.isFunction( found ) ) {
  1000. response = found.apply(context, passedArguments);
  1001. }
  1002. else if(found !== undefined) {
  1003. response = found;
  1004. }
  1005. if($.isArray(returnedValue)) {
  1006. returnedValue.push(response);
  1007. }
  1008. else if(returnedValue !== undefined) {
  1009. returnedValue = [returnedValue, response];
  1010. }
  1011. else if(response !== undefined) {
  1012. returnedValue = response;
  1013. }
  1014. return found;
  1015. }
  1016. };
  1017. if(methodInvoked) {
  1018. if(instance === undefined) {
  1019. module.initialize();
  1020. }
  1021. module.invoke(query);
  1022. }
  1023. else {
  1024. if(instance !== undefined) {
  1025. instance.invoke('destroy');
  1026. }
  1027. module.initialize();
  1028. }
  1029. })
  1030. ;
  1031. return (returnedValue !== undefined)
  1032. ? returnedValue
  1033. : this
  1034. ;
  1035. };
  1036. $.fn.popup.settings = {
  1037. name : 'Popup',
  1038. // module settings
  1039. debug : false,
  1040. verbose : false,
  1041. performance : true,
  1042. namespace : 'popup',
  1043. // callback only when element added to dom
  1044. onCreate : function(){},
  1045. // callback before element removed from dom
  1046. onRemove : function(){},
  1047. // callback before show animation
  1048. onShow : function(){},
  1049. // callback after show animation
  1050. onVisible : function(){},
  1051. // callback before hide animation
  1052. onHide : function(){},
  1053. // callback after hide animation
  1054. onHidden : function(){},
  1055. // when to show popup
  1056. on : 'hover',
  1057. // default position relative to element
  1058. position : 'top left',
  1059. // name of variation to use
  1060. variation : '',
  1061. // whether popup should be moved to context
  1062. movePopup : true,
  1063. // element which popup should be relative to
  1064. target : false,
  1065. // jq selector or element that should be used as popup
  1066. popup : false,
  1067. // popup should remain inline next to activator
  1068. inline : false,
  1069. // popup should be removed from page on hide
  1070. preserve : false,
  1071. // popup should not close when being hovered on
  1072. hoverable : false,
  1073. // explicitly set content
  1074. content : false,
  1075. // explicitly set html
  1076. html : false,
  1077. // explicitly set title
  1078. title : false,
  1079. // whether automatically close on clickaway when on click
  1080. closable : true,
  1081. // automatically hide on scroll
  1082. hideOnScroll : 'auto',
  1083. // hide other popups on show
  1084. exclusive : true,
  1085. // context to attach popups
  1086. context : 'body',
  1087. // position to prefer when calculating new position
  1088. prefer : 'opposite',
  1089. // specify position to appear even if it doesn't fit
  1090. lastResort : false,
  1091. // delay used to prevent accidental refiring of animations due to user error
  1092. delay : {
  1093. show : 30,
  1094. hide : 0
  1095. },
  1096. // whether fluid variation should assign width explicitly
  1097. setFluidWidth : true,
  1098. // transition settings
  1099. duration : 200,
  1100. easing : 'easeOutQuint',
  1101. transition : 'scale',
  1102. // distance away from activating element in px
  1103. distanceAway : 0,
  1104. // offset on aligning axis from calculated position
  1105. offset : 0,
  1106. // maximum times to look for a position before failing
  1107. maxSearchDepth : 20,
  1108. error: {
  1109. invalidPosition : 'The position you specified is not a valid position',
  1110. cannotPlace : 'No visible position could be found for the popup',
  1111. method : 'The method you called is not defined.',
  1112. notFound : 'The target or popup you specified does not exist on the page'
  1113. },
  1114. metadata: {
  1115. activator : 'activator',
  1116. content : 'content',
  1117. html : 'html',
  1118. offset : 'offset',
  1119. position : 'position',
  1120. title : 'title',
  1121. variation : 'variation'
  1122. },
  1123. className : {
  1124. active : 'active',
  1125. animating : 'animating',
  1126. dropdown : 'dropdown',
  1127. fluid : 'fluid',
  1128. loading : 'loading',
  1129. popup : 'ui popup',
  1130. position : 'top left center bottom right',
  1131. visible : 'visible'
  1132. },
  1133. selector : {
  1134. popup : '.ui.popup'
  1135. },
  1136. templates: {
  1137. escape: function(string) {
  1138. var
  1139. badChars = /[&<>"'`]/g,
  1140. shouldEscape = /[&<>"'`]/,
  1141. escape = {
  1142. "&": "&amp;",
  1143. "<": "&lt;",
  1144. ">": "&gt;",
  1145. '"': "&quot;",
  1146. "'": "&#x27;",
  1147. "`": "&#x60;"
  1148. },
  1149. escapedChar = function(chr) {
  1150. return escape[chr];
  1151. }
  1152. ;
  1153. if(shouldEscape.test(string)) {
  1154. return string.replace(badChars, escapedChar);
  1155. }
  1156. return string;
  1157. },
  1158. popup: function(text) {
  1159. var
  1160. html = '',
  1161. escape = $.fn.popup.settings.templates.escape
  1162. ;
  1163. if(typeof text !== undefined) {
  1164. if(typeof text.title !== undefined && text.title) {
  1165. text.title = escape(text.title);
  1166. html += '<div class="header">' + text.title + '</div>';
  1167. }
  1168. if(typeof text.content !== undefined && text.content) {
  1169. text.content = escape(text.content);
  1170. html += '<div class="content">' + text.content + '</div>';
  1171. }
  1172. }
  1173. return html;
  1174. }
  1175. }
  1176. };
  1177. // Adds easing
  1178. $.extend( $.easing, {
  1179. easeOutQuad: function (x, t, b, c, d) {
  1180. return -c *(t/=d)*(t-2) + b;
  1181. }
  1182. });
  1183. })( jQuery, window , document );