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
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
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
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
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(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.error(error.noTransition);
  357. }
  358. settings.onShow.call($popup, element);
  359. },
  360. hide: function(callback) {
  361. callback = $.isFunction(callback) ? callback : function(){};
  362. module.debug('Hiding pop-up');
  363. if(settings.transition && $.fn.transition !== undefined && $module.transition('is supported')) {
  364. $popup
  365. .transition({
  366. animation : settings.transition + ' out',
  367. queue : false,
  368. duration : settings.duration,
  369. debug : settings.debug,
  370. verbose : settings.verbose,
  371. onComplete : function() {
  372. module.reset();
  373. callback.call($popup, element);
  374. settings.onHidden.call($popup, element);
  375. }
  376. })
  377. ;
  378. }
  379. else {
  380. module.error(error.noTransition);
  381. }
  382. settings.onHide.call($popup, element);
  383. }
  384. },
  385. get: {
  386. id: function() {
  387. return id;
  388. },
  389. startEvent: function() {
  390. if(settings.on == 'hover') {
  391. return (hasTouch)
  392. ? 'touchstart mouseenter'
  393. : 'mouseenter'
  394. ;
  395. }
  396. else if(settings.on == 'focus') {
  397. return 'focus';
  398. }
  399. return false;
  400. },
  401. scrollEvent: function() {
  402. return (hasTouch)
  403. ? 'touchmove scroll'
  404. : 'scroll'
  405. ;
  406. },
  407. endEvent: function() {
  408. if(settings.on == 'hover') {
  409. return 'mouseleave';
  410. }
  411. else if(settings.on == 'focus') {
  412. return 'blur';
  413. }
  414. return false;
  415. },
  416. offsetParent: function($target) {
  417. var
  418. element = ($target !== undefined)
  419. ? $target[0]
  420. : $module[0],
  421. parentNode = element.parentNode,
  422. $node = $(parentNode)
  423. ;
  424. if(parentNode) {
  425. var
  426. is2D = ($node.css('transform') === 'none'),
  427. isStatic = ($node.css('position') === 'static'),
  428. isHTML = $node.is('html')
  429. ;
  430. while(parentNode && !isHTML && isStatic && is2D) {
  431. parentNode = parentNode.parentNode;
  432. $node = $(parentNode);
  433. is2D = ($node.css('transform') === 'none');
  434. isStatic = ($node.css('position') === 'static');
  435. isHTML = $node.is('html');
  436. }
  437. }
  438. return ($node && $node.length > 0)
  439. ? $node
  440. : $()
  441. ;
  442. },
  443. offstagePosition: function(position) {
  444. var
  445. boundary = {
  446. top : $(window).scrollTop(),
  447. bottom : $(window).scrollTop() + $(window).height(),
  448. left : 0,
  449. right : $(window).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', 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 : 30,
  1079. hide : 0
  1080. },
  1081. // whether fluid variation should assign width explicitly
  1082. setFluidWidth : true,
  1083. // transition settings
  1084. duration : 200,
  1085. easing : 'easeOutQuint',
  1086. transition : 'scale',
  1087. // distance away from activating element in px
  1088. distanceAway : 0,
  1089. // offset on aligning axis from calculated position
  1090. offset : 0,
  1091. // maximum times to look for a position before failing
  1092. maxSearchDepth : 20,
  1093. error: {
  1094. invalidPosition : 'The position you specified is not a valid position',
  1095. cannotPlace : 'No visible position could be found for the popup',
  1096. method : 'The method you called is not defined.',
  1097. noTransition : 'This module requires ui transitions <https://github.com/Semantic-Org/UI-Transition>',
  1098. notFound : 'The target or popup you specified does not exist on the page'
  1099. },
  1100. metadata: {
  1101. activator : 'activator',
  1102. content : 'content',
  1103. html : 'html',
  1104. offset : 'offset',
  1105. position : 'position',
  1106. title : 'title',
  1107. variation : 'variation'
  1108. },
  1109. className : {
  1110. active : 'active',
  1111. animating : 'animating',
  1112. dropdown : 'dropdown',
  1113. fluid : 'fluid',
  1114. loading : 'loading',
  1115. popup : 'ui popup',
  1116. position : 'top left center bottom right',
  1117. visible : 'visible'
  1118. },
  1119. selector : {
  1120. popup : '.ui.popup'
  1121. },
  1122. templates: {
  1123. escape: function(string) {
  1124. var
  1125. badChars = /[&<>"'`]/g,
  1126. shouldEscape = /[&<>"'`]/,
  1127. escape = {
  1128. "&": "&amp;",
  1129. "<": "&lt;",
  1130. ">": "&gt;",
  1131. '"': "&quot;",
  1132. "'": "&#x27;",
  1133. "`": "&#x60;"
  1134. },
  1135. escapedChar = function(chr) {
  1136. return escape[chr];
  1137. }
  1138. ;
  1139. if(shouldEscape.test(string)) {
  1140. return string.replace(badChars, escapedChar);
  1141. }
  1142. return string;
  1143. },
  1144. popup: function(text) {
  1145. var
  1146. html = '',
  1147. escape = $.fn.popup.settings.templates.escape
  1148. ;
  1149. if(typeof text !== undefined) {
  1150. if(typeof text.title !== undefined && text.title) {
  1151. text.title = escape(text.title);
  1152. html += '<div class="header">' + text.title + '</div>';
  1153. }
  1154. if(typeof text.content !== undefined && text.content) {
  1155. text.content = escape(text.content);
  1156. html += '<div class="content">' + text.content + '</div>';
  1157. }
  1158. }
  1159. return html;
  1160. }
  1161. }
  1162. };
  1163. // Adds easing
  1164. $.extend( $.easing, {
  1165. easeOutQuad: function (x, t, b, c, d) {
  1166. return -c *(t/=d)*(t-2) + b;
  1167. }
  1168. });
  1169. })( jQuery, window , document );