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.

1258 lines
40 KiB

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