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.

1400 lines
45 KiB

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