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.

1413 lines
46 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
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
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
9 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
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
10 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
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
9 years ago
  1. /*!
  2. * # Semantic UI 2.1.7 - 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. change: {
  394. content: function(html) {
  395. $popup.html(html);
  396. }
  397. },
  398. get: {
  399. html: function() {
  400. $module.removeData(metadata.html);
  401. return $module.data(metadata.html) || settings.html;
  402. },
  403. title: function() {
  404. $module.removeData(metadata.title);
  405. return $module.data(metadata.title) || settings.title;
  406. },
  407. content: function() {
  408. $module.removeData(metadata.content);
  409. return $module.data(metadata.content) || $module.attr('title') || settings.content;
  410. },
  411. variation: function() {
  412. $module.removeData(metadata.variation);
  413. return $module.data(metadata.variation) || settings.variation;
  414. },
  415. popup: function() {
  416. return $popup;
  417. },
  418. popupOffset: function() {
  419. return $popup.offset();
  420. },
  421. calculations: function() {
  422. var
  423. targetElement = $target[0],
  424. targetPosition = (settings.inline || (settings.popup && settings.movePopup))
  425. ? $target.position()
  426. : $target.offset(),
  427. calculations = {},
  428. screen
  429. ;
  430. calculations = {
  431. // element which is launching popup
  432. target : {
  433. element : $target[0],
  434. width : $target.outerWidth(),
  435. height : $target.outerHeight(),
  436. top : targetPosition.top,
  437. left : targetPosition.left,
  438. margin : {}
  439. },
  440. // popup itself
  441. popup : {
  442. width : $popup.outerWidth(),
  443. height : $popup.outerHeight()
  444. },
  445. // offset container (or 3d context)
  446. parent : {
  447. width : $offsetParent.outerWidth(),
  448. height : $offsetParent.outerHeight()
  449. },
  450. // screen boundaries
  451. screen : {
  452. scroll: {
  453. top : $window.scrollTop(),
  454. left : $window.scrollLeft()
  455. },
  456. width : $window.width(),
  457. height : $window.height()
  458. }
  459. };
  460. // add in container calcs if fluid
  461. if( settings.setFluidWidth && module.is.fluid() ) {
  462. calculations.container = {
  463. width: $popup.parent().outerWidth()
  464. };
  465. calculations.popup.width = calculations.container.width;
  466. }
  467. // add in margins if inline
  468. calculations.target.margin.top = (settings.inline)
  469. ? parseInt( window.getComputedStyle(targetElement).getPropertyValue('margin-top'), 10)
  470. : 0
  471. ;
  472. calculations.target.margin.left = (settings.inline)
  473. ? module.is.rtl()
  474. ? parseInt( window.getComputedStyle(targetElement).getPropertyValue('margin-right'), 10)
  475. : parseInt( window.getComputedStyle(targetElement).getPropertyValue('margin-left') , 10)
  476. : 0
  477. ;
  478. // calculate screen boundaries
  479. screen = calculations.screen;
  480. calculations.boundary = {
  481. top : screen.scroll.top,
  482. bottom : screen.scroll.top + screen.height,
  483. left : screen.scroll.left,
  484. right : screen.scroll.left + screen.width
  485. };
  486. return calculations;
  487. },
  488. id: function() {
  489. return id;
  490. },
  491. startEvent: function() {
  492. if(settings.on == 'hover') {
  493. return 'mouseenter';
  494. }
  495. else if(settings.on == 'focus') {
  496. return 'focus';
  497. }
  498. return false;
  499. },
  500. scrollEvent: function() {
  501. return 'scroll';
  502. },
  503. endEvent: function() {
  504. if(settings.on == 'hover') {
  505. return 'mouseleave';
  506. }
  507. else if(settings.on == 'focus') {
  508. return 'blur';
  509. }
  510. return false;
  511. },
  512. distanceFromBoundary: function(offset, calculations) {
  513. var
  514. distanceFromBoundary = {},
  515. popup,
  516. boundary
  517. ;
  518. offset = offset || module.get.offset();
  519. calculations = calculations || module.get.calculations();
  520. // shorthand
  521. popup = calculations.popup;
  522. boundary = calculations.boundary;
  523. if(offset) {
  524. distanceFromBoundary = {
  525. top : (offset.top - boundary.top),
  526. left : (offset.left - boundary.left),
  527. right : (boundary.right - (offset.left + popup.width) ),
  528. bottom : (boundary.bottom - (offset.top + popup.height) )
  529. };
  530. module.verbose('Distance from boundaries determined', offset, distanceFromBoundary);
  531. }
  532. return distanceFromBoundary;
  533. },
  534. offsetParent: function($target) {
  535. var
  536. element = ($target !== undefined)
  537. ? $target[0]
  538. : $module[0],
  539. parentNode = element.parentNode,
  540. $node = $(parentNode)
  541. ;
  542. if(parentNode) {
  543. var
  544. is2D = ($node.css('transform') === 'none'),
  545. isStatic = ($node.css('position') === 'static'),
  546. isHTML = $node.is('html')
  547. ;
  548. while(parentNode && !isHTML && isStatic && is2D) {
  549. parentNode = parentNode.parentNode;
  550. $node = $(parentNode);
  551. is2D = ($node.css('transform') === 'none');
  552. isStatic = ($node.css('position') === 'static');
  553. isHTML = $node.is('html');
  554. }
  555. }
  556. return ($node && $node.length > 0)
  557. ? $node
  558. : $()
  559. ;
  560. },
  561. positions: function() {
  562. return {
  563. 'top left' : false,
  564. 'top center' : false,
  565. 'top right' : false,
  566. 'bottom left' : false,
  567. 'bottom center' : false,
  568. 'bottom right' : false,
  569. 'left center' : false,
  570. 'right center' : false
  571. };
  572. },
  573. nextPosition: function(position) {
  574. var
  575. positions = position.split(' '),
  576. verticalPosition = positions[0],
  577. horizontalPosition = positions[1],
  578. opposite = {
  579. top : 'bottom',
  580. bottom : 'top',
  581. left : 'right',
  582. right : 'left'
  583. },
  584. adjacent = {
  585. left : 'center',
  586. center : 'right',
  587. right : 'left'
  588. },
  589. backup = {
  590. 'top left' : 'top center',
  591. 'top center' : 'top right',
  592. 'top right' : 'right center',
  593. 'right center' : 'bottom right',
  594. 'bottom right' : 'bottom center',
  595. 'bottom center' : 'bottom left',
  596. 'bottom left' : 'left center',
  597. 'left center' : 'top left'
  598. },
  599. adjacentsAvailable = (verticalPosition == 'top' || verticalPosition == 'bottom'),
  600. oppositeTried = false,
  601. adjacentTried = false,
  602. nextPosition = false
  603. ;
  604. if(!triedPositions) {
  605. module.verbose('All available positions available');
  606. triedPositions = module.get.positions();
  607. }
  608. module.debug('Recording last position tried', position);
  609. triedPositions[position] = true;
  610. if(settings.prefer === 'opposite') {
  611. nextPosition = [opposite[verticalPosition], horizontalPosition];
  612. nextPosition = nextPosition.join(' ');
  613. oppositeTried = (triedPositions[nextPosition] === true);
  614. module.debug('Trying opposite strategy', nextPosition);
  615. }
  616. if((settings.prefer === 'adjacent') && adjacentsAvailable ) {
  617. nextPosition = [verticalPosition, adjacent[horizontalPosition]];
  618. nextPosition = nextPosition.join(' ');
  619. adjacentTried = (triedPositions[nextPosition] === true);
  620. module.debug('Trying adjacent strategy', nextPosition);
  621. }
  622. if(adjacentTried || oppositeTried) {
  623. module.debug('Using backup position', nextPosition);
  624. nextPosition = backup[position];
  625. }
  626. return nextPosition;
  627. }
  628. },
  629. set: {
  630. position: function(position, calculations) {
  631. // exit conditions
  632. if($target.length === 0 || $popup.length === 0) {
  633. module.error(error.notFound);
  634. return;
  635. }
  636. var
  637. offset,
  638. distanceAway,
  639. target,
  640. popup,
  641. parent,
  642. positioning,
  643. popupOffset,
  644. distanceFromBoundary
  645. ;
  646. calculations = calculations || module.get.calculations();
  647. position = position || $module.data(metadata.position) || settings.position;
  648. offset = $module.data(metadata.offset) || settings.offset;
  649. distanceAway = settings.distanceAway;
  650. // shorthand
  651. target = calculations.target;
  652. popup = calculations.popup;
  653. parent = calculations.parent;
  654. if(target.width === 0 && target.height === 0 && !(target.element instanceof SVGGraphicsElement)) {
  655. module.debug('Popup target is hidden, no action taken');
  656. return false;
  657. }
  658. if(settings.inline) {
  659. module.debug('Adding margin to calculation', target.margin);
  660. if(position == 'left center' || position == 'right center') {
  661. offset += target.margin.top;
  662. distanceAway += -target.margin.left;
  663. }
  664. else if (position == 'top left' || position == 'top center' || position == 'top right') {
  665. offset += target.margin.left;
  666. distanceAway -= target.margin.top;
  667. }
  668. else {
  669. offset += target.margin.left;
  670. distanceAway += target.margin.top;
  671. }
  672. }
  673. module.debug('Determining popup position from calculations', position, calculations);
  674. if (module.is.rtl()) {
  675. position = position.replace(/left|right/g, function (match) {
  676. return (match == 'left')
  677. ? 'right'
  678. : 'left'
  679. ;
  680. });
  681. module.debug('RTL: Popup position updated', position);
  682. }
  683. // if last attempt use specified last resort position
  684. if(searchDepth == settings.maxSearchDepth && typeof settings.lastResort === 'string') {
  685. position = settings.lastResort;
  686. }
  687. switch (position) {
  688. case 'top left':
  689. positioning = {
  690. top : 'auto',
  691. bottom : parent.height - target.top + distanceAway,
  692. left : target.left + offset,
  693. right : 'auto'
  694. };
  695. break;
  696. case 'top center':
  697. positioning = {
  698. bottom : parent.height - target.top + distanceAway,
  699. left : target.left + (target.width / 2) - (popup.width / 2) + offset,
  700. top : 'auto',
  701. right : 'auto'
  702. };
  703. break;
  704. case 'top right':
  705. positioning = {
  706. bottom : parent.height - target.top + distanceAway,
  707. right : parent.width - target.left - target.width - offset,
  708. top : 'auto',
  709. left : 'auto'
  710. };
  711. break;
  712. case 'left center':
  713. positioning = {
  714. top : target.top + (target.height / 2) - (popup.height / 2) + offset,
  715. right : parent.width - target.left + distanceAway,
  716. left : 'auto',
  717. bottom : 'auto'
  718. };
  719. break;
  720. case 'right center':
  721. positioning = {
  722. top : target.top + (target.height / 2) - (popup.height / 2) + offset,
  723. left : target.left + target.width + distanceAway,
  724. bottom : 'auto',
  725. right : 'auto'
  726. };
  727. break;
  728. case 'bottom left':
  729. positioning = {
  730. top : target.top + target.height + distanceAway,
  731. left : target.left + offset,
  732. bottom : 'auto',
  733. right : 'auto'
  734. };
  735. break;
  736. case 'bottom center':
  737. positioning = {
  738. top : target.top + target.height + distanceAway,
  739. left : target.left + (target.width / 2) - (popup.width / 2) + offset,
  740. bottom : 'auto',
  741. right : 'auto'
  742. };
  743. break;
  744. case 'bottom right':
  745. positioning = {
  746. top : target.top + target.height + distanceAway,
  747. right : parent.width - target.left - target.width - offset,
  748. left : 'auto',
  749. bottom : 'auto'
  750. };
  751. break;
  752. }
  753. if(positioning === undefined) {
  754. module.error(error.invalidPosition, position);
  755. }
  756. module.debug('Calculated popup positioning values', positioning);
  757. // tentatively place on stage
  758. $popup
  759. .css(positioning)
  760. .removeClass(className.position)
  761. .addClass(position)
  762. .addClass(className.loading)
  763. ;
  764. popupOffset = module.get.popupOffset();
  765. // see if any boundaries are surpassed with this tentative position
  766. distanceFromBoundary = module.get.distanceFromBoundary(popupOffset, calculations);
  767. if( module.is.offstage(distanceFromBoundary, position) ) {
  768. module.debug('Position is outside viewport', position);
  769. if(searchDepth < settings.maxSearchDepth) {
  770. searchDepth++;
  771. position = module.get.nextPosition(position);
  772. module.debug('Trying new position', position);
  773. return ($popup)
  774. ? module.set.position(position, calculations)
  775. : false
  776. ;
  777. }
  778. else {
  779. if(settings.lastResort) {
  780. module.debug('No position found, showing with last position');
  781. }
  782. else {
  783. module.debug('Popup could not find a position to display', $popup);
  784. module.error(error.cannotPlace, element);
  785. module.remove.attempts();
  786. module.remove.loading();
  787. module.reset();
  788. settings.onUnplaceable.call($popup, element);
  789. return false;
  790. }
  791. }
  792. }
  793. module.debug('Position is on stage', position);
  794. module.remove.attempts();
  795. module.remove.loading();
  796. if( settings.setFluidWidth && module.is.fluid() ) {
  797. module.set.fluidWidth(calculations);
  798. }
  799. return true;
  800. },
  801. fluidWidth: function(calculations) {
  802. calculations = calculations || module.get.calculations();
  803. module.debug('Automatically setting element width to parent width', calculations.parent.width);
  804. $popup.css('width', calculations.container.width);
  805. },
  806. variation: function(variation) {
  807. variation = variation || module.get.variation();
  808. if(variation && module.has.popup() ) {
  809. module.verbose('Adding variation to popup', variation);
  810. $popup.addClass(variation);
  811. }
  812. },
  813. visible: function() {
  814. $module.addClass(className.visible);
  815. }
  816. },
  817. remove: {
  818. loading: function() {
  819. $popup.removeClass(className.loading);
  820. },
  821. variation: function(variation) {
  822. variation = variation || module.get.variation();
  823. if(variation) {
  824. module.verbose('Removing variation', variation);
  825. $popup.removeClass(variation);
  826. }
  827. },
  828. visible: function() {
  829. $module.removeClass(className.visible);
  830. },
  831. attempts: function() {
  832. module.verbose('Resetting all searched positions');
  833. searchDepth = 0;
  834. triedPositions = false;
  835. }
  836. },
  837. bind: {
  838. events: function() {
  839. module.debug('Binding popup events to module');
  840. if(settings.on == 'click') {
  841. $module
  842. .on('click' + eventNamespace, module.toggle)
  843. ;
  844. }
  845. if(settings.on == 'hover' && hasTouch) {
  846. $module
  847. .on('touchstart' + eventNamespace, module.event.touchstart)
  848. ;
  849. }
  850. if( module.get.startEvent() ) {
  851. $module
  852. .on(module.get.startEvent() + eventNamespace, module.event.start)
  853. .on(module.get.endEvent() + eventNamespace, module.event.end)
  854. ;
  855. }
  856. if(settings.target) {
  857. module.debug('Target set to element', $target);
  858. }
  859. $window.on('resize' + elementNamespace, module.event.resize);
  860. },
  861. popup: function() {
  862. module.verbose('Allowing hover events on popup to prevent closing');
  863. if( $popup && module.has.popup() ) {
  864. $popup
  865. .on('mouseenter' + eventNamespace, module.event.start)
  866. .on('mouseleave' + eventNamespace, module.event.end)
  867. ;
  868. }
  869. },
  870. close: function() {
  871. if(settings.hideOnScroll === true || (settings.hideOnScroll == 'auto' && settings.on != 'click')) {
  872. $document
  873. .one(module.get.scrollEvent() + elementNamespace, module.event.hideGracefully)
  874. ;
  875. $context
  876. .one(module.get.scrollEvent() + elementNamespace, module.event.hideGracefully)
  877. ;
  878. }
  879. if(settings.on == 'hover' && openedWithTouch) {
  880. module.verbose('Binding popup close event to document');
  881. $document
  882. .on('touchstart' + elementNamespace, function(event) {
  883. module.verbose('Touched away from popup');
  884. module.event.hideGracefully.call(element, event);
  885. })
  886. ;
  887. }
  888. if(settings.on == 'click' && settings.closable) {
  889. module.verbose('Binding popup close event to document');
  890. $document
  891. .on('click' + elementNamespace, function(event) {
  892. module.verbose('Clicked away from popup');
  893. module.event.hideGracefully.call(element, event);
  894. })
  895. ;
  896. }
  897. }
  898. },
  899. unbind: {
  900. close: function() {
  901. if(settings.hideOnScroll === true || (settings.hideOnScroll == 'auto' && settings.on != 'click')) {
  902. $document
  903. .off('scroll' + elementNamespace, module.hide)
  904. ;
  905. $context
  906. .off('scroll' + elementNamespace, module.hide)
  907. ;
  908. }
  909. if(settings.on == 'hover' && openedWithTouch) {
  910. $document
  911. .off('touchstart' + elementNamespace)
  912. ;
  913. openedWithTouch = false;
  914. }
  915. if(settings.on == 'click' && settings.closable) {
  916. module.verbose('Removing close event from document');
  917. $document
  918. .off('click' + elementNamespace)
  919. ;
  920. }
  921. }
  922. },
  923. has: {
  924. popup: function() {
  925. return ($popup && $popup.length > 0);
  926. }
  927. },
  928. is: {
  929. offstage: function(distanceFromBoundary, position) {
  930. var
  931. offstage = []
  932. ;
  933. // return boundaries that have been surpassed
  934. $.each(distanceFromBoundary, function(direction, distance) {
  935. if(distance < -settings.jitter) {
  936. module.debug('Position exceeds allowable distance from edge', direction, distance, position);
  937. offstage.push(direction);
  938. }
  939. });
  940. if(offstage.length > 0) {
  941. return true;
  942. }
  943. else {
  944. return false;
  945. }
  946. },
  947. active: function() {
  948. return $module.hasClass(className.active);
  949. },
  950. animating: function() {
  951. return ($popup !== undefined && $popup.hasClass(className.animating) );
  952. },
  953. fluid: function() {
  954. return ($popup !== undefined && $popup.hasClass(className.fluid));
  955. },
  956. visible: function() {
  957. return ($popup !== undefined && $popup.hasClass(className.visible));
  958. },
  959. dropdown: function() {
  960. return $module.hasClass(className.dropdown);
  961. },
  962. hidden: function() {
  963. return !module.is.visible();
  964. },
  965. rtl: function () {
  966. return $module.css('direction') == 'rtl';
  967. }
  968. },
  969. reset: function() {
  970. module.remove.visible();
  971. if(settings.preserve) {
  972. if($.fn.transition !== undefined) {
  973. $popup
  974. .transition('remove transition')
  975. ;
  976. }
  977. }
  978. else {
  979. module.removePopup();
  980. }
  981. },
  982. setting: function(name, value) {
  983. if( $.isPlainObject(name) ) {
  984. $.extend(true, settings, name);
  985. }
  986. else if(value !== undefined) {
  987. settings[name] = value;
  988. }
  989. else {
  990. return settings[name];
  991. }
  992. },
  993. internal: function(name, value) {
  994. if( $.isPlainObject(name) ) {
  995. $.extend(true, module, name);
  996. }
  997. else if(value !== undefined) {
  998. module[name] = value;
  999. }
  1000. else {
  1001. return module[name];
  1002. }
  1003. },
  1004. debug: function() {
  1005. if(settings.debug) {
  1006. if(settings.performance) {
  1007. module.performance.log(arguments);
  1008. }
  1009. else {
  1010. module.debug = Function.prototype.bind.call(console.info, console, settings.name + ':');
  1011. module.debug.apply(console, arguments);
  1012. }
  1013. }
  1014. },
  1015. verbose: function() {
  1016. if(settings.verbose && settings.debug) {
  1017. if(settings.performance) {
  1018. module.performance.log(arguments);
  1019. }
  1020. else {
  1021. module.verbose = Function.prototype.bind.call(console.info, console, settings.name + ':');
  1022. module.verbose.apply(console, arguments);
  1023. }
  1024. }
  1025. },
  1026. error: function() {
  1027. module.error = Function.prototype.bind.call(console.error, console, settings.name + ':');
  1028. module.error.apply(console, arguments);
  1029. },
  1030. performance: {
  1031. log: function(message) {
  1032. var
  1033. currentTime,
  1034. executionTime,
  1035. previousTime
  1036. ;
  1037. if(settings.performance) {
  1038. currentTime = new Date().getTime();
  1039. previousTime = time || currentTime;
  1040. executionTime = currentTime - previousTime;
  1041. time = currentTime;
  1042. performance.push({
  1043. 'Name' : message[0],
  1044. 'Arguments' : [].slice.call(message, 1) || '',
  1045. 'Element' : element,
  1046. 'Execution Time' : executionTime
  1047. });
  1048. }
  1049. clearTimeout(module.performance.timer);
  1050. module.performance.timer = setTimeout(module.performance.display, 500);
  1051. },
  1052. display: function() {
  1053. var
  1054. title = settings.name + ':',
  1055. totalTime = 0
  1056. ;
  1057. time = false;
  1058. clearTimeout(module.performance.timer);
  1059. $.each(performance, function(index, data) {
  1060. totalTime += data['Execution Time'];
  1061. });
  1062. title += ' ' + totalTime + 'ms';
  1063. if(moduleSelector) {
  1064. title += ' \'' + moduleSelector + '\'';
  1065. }
  1066. if( (console.group !== undefined || console.table !== undefined) && performance.length > 0) {
  1067. console.groupCollapsed(title);
  1068. if(console.table) {
  1069. console.table(performance);
  1070. }
  1071. else {
  1072. $.each(performance, function(index, data) {
  1073. console.log(data['Name'] + ': ' + data['Execution Time']+'ms');
  1074. });
  1075. }
  1076. console.groupEnd();
  1077. }
  1078. performance = [];
  1079. }
  1080. },
  1081. invoke: function(query, passedArguments, context) {
  1082. var
  1083. object = instance,
  1084. maxDepth,
  1085. found,
  1086. response
  1087. ;
  1088. passedArguments = passedArguments || queryArguments;
  1089. context = element || context;
  1090. if(typeof query == 'string' && object !== undefined) {
  1091. query = query.split(/[\. ]/);
  1092. maxDepth = query.length - 1;
  1093. $.each(query, function(depth, value) {
  1094. var camelCaseValue = (depth != maxDepth)
  1095. ? value + query[depth + 1].charAt(0).toUpperCase() + query[depth + 1].slice(1)
  1096. : query
  1097. ;
  1098. if( $.isPlainObject( object[camelCaseValue] ) && (depth != maxDepth) ) {
  1099. object = object[camelCaseValue];
  1100. }
  1101. else if( object[camelCaseValue] !== undefined ) {
  1102. found = object[camelCaseValue];
  1103. return false;
  1104. }
  1105. else if( $.isPlainObject( object[value] ) && (depth != maxDepth) ) {
  1106. object = object[value];
  1107. }
  1108. else if( object[value] !== undefined ) {
  1109. found = object[value];
  1110. return false;
  1111. }
  1112. else {
  1113. return false;
  1114. }
  1115. });
  1116. }
  1117. if ( $.isFunction( found ) ) {
  1118. response = found.apply(context, passedArguments);
  1119. }
  1120. else if(found !== undefined) {
  1121. response = found;
  1122. }
  1123. if($.isArray(returnedValue)) {
  1124. returnedValue.push(response);
  1125. }
  1126. else if(returnedValue !== undefined) {
  1127. returnedValue = [returnedValue, response];
  1128. }
  1129. else if(response !== undefined) {
  1130. returnedValue = response;
  1131. }
  1132. return found;
  1133. }
  1134. };
  1135. if(methodInvoked) {
  1136. if(instance === undefined) {
  1137. module.initialize();
  1138. }
  1139. module.invoke(query);
  1140. }
  1141. else {
  1142. if(instance !== undefined) {
  1143. instance.invoke('destroy');
  1144. }
  1145. module.initialize();
  1146. }
  1147. })
  1148. ;
  1149. return (returnedValue !== undefined)
  1150. ? returnedValue
  1151. : this
  1152. ;
  1153. };
  1154. $.fn.popup.settings = {
  1155. name : 'Popup',
  1156. // module settings
  1157. debug : false,
  1158. verbose : false,
  1159. performance : true,
  1160. namespace : 'popup',
  1161. // callback only when element added to dom
  1162. onCreate : function(){},
  1163. // callback before element removed from dom
  1164. onRemove : function(){},
  1165. // callback before show animation
  1166. onShow : function(){},
  1167. // callback after show animation
  1168. onVisible : function(){},
  1169. // callback before hide animation
  1170. onHide : function(){},
  1171. // callback when popup cannot be positioned in visible screen
  1172. onUnplaceable: function(){},
  1173. // callback after hide animation
  1174. onHidden : function(){},
  1175. // when to show popup
  1176. on : 'hover',
  1177. // whether to add touchstart events when using hover
  1178. addTouchEvents : true,
  1179. // default position relative to element
  1180. position : 'top left',
  1181. // name of variation to use
  1182. variation : '',
  1183. // whether popup should be moved to context
  1184. movePopup : true,
  1185. // element which popup should be relative to
  1186. target : false,
  1187. // jq selector or element that should be used as popup
  1188. popup : false,
  1189. // popup should remain inline next to activator
  1190. inline : false,
  1191. // popup should be removed from page on hide
  1192. preserve : false,
  1193. // popup should not close when being hovered on
  1194. hoverable : false,
  1195. // explicitly set content
  1196. content : false,
  1197. // explicitly set html
  1198. html : false,
  1199. // explicitly set title
  1200. title : false,
  1201. // whether automatically close on clickaway when on click
  1202. closable : true,
  1203. // automatically hide on scroll
  1204. hideOnScroll : 'auto',
  1205. // hide other popups on show
  1206. exclusive : false,
  1207. // context to attach popups
  1208. context : 'body',
  1209. // position to prefer when calculating new position
  1210. prefer : 'opposite',
  1211. // specify position to appear even if it doesn't fit
  1212. lastResort : false,
  1213. // delay used to prevent accidental refiring of animations due to user error
  1214. delay : {
  1215. show : 50,
  1216. hide : 70
  1217. },
  1218. // whether fluid variation should assign width explicitly
  1219. setFluidWidth : true,
  1220. // transition settings
  1221. duration : 200,
  1222. transition : 'scale',
  1223. // distance away from activating element in px
  1224. distanceAway : 0,
  1225. // number of pixels an element is allowed to be "offstage" for a position to be chosen (allows for rounding)
  1226. jitter : 2,
  1227. // offset on aligning axis from calculated position
  1228. offset : 0,
  1229. // maximum times to look for a position before failing (9 positions total)
  1230. maxSearchDepth : 15,
  1231. error: {
  1232. invalidPosition : 'The position you specified is not a valid position',
  1233. cannotPlace : 'Popup does not fit within the boundaries of the viewport',
  1234. method : 'The method you called is not defined.',
  1235. noTransition : 'This module requires ui transitions <https://github.com/Semantic-Org/UI-Transition>',
  1236. notFound : 'The target or popup you specified does not exist on the page'
  1237. },
  1238. metadata: {
  1239. activator : 'activator',
  1240. content : 'content',
  1241. html : 'html',
  1242. offset : 'offset',
  1243. position : 'position',
  1244. title : 'title',
  1245. variation : 'variation'
  1246. },
  1247. className : {
  1248. active : 'active',
  1249. animating : 'animating',
  1250. dropdown : 'dropdown',
  1251. fluid : 'fluid',
  1252. loading : 'loading',
  1253. popup : 'ui popup',
  1254. position : 'top left center bottom right',
  1255. visible : 'visible'
  1256. },
  1257. selector : {
  1258. popup : '.ui.popup'
  1259. },
  1260. templates: {
  1261. escape: function(string) {
  1262. var
  1263. badChars = /[&<>"'`]/g,
  1264. shouldEscape = /[&<>"'`]/,
  1265. escape = {
  1266. "&": "&amp;",
  1267. "<": "&lt;",
  1268. ">": "&gt;",
  1269. '"': "&quot;",
  1270. "'": "&#x27;",
  1271. "`": "&#x60;"
  1272. },
  1273. escapedChar = function(chr) {
  1274. return escape[chr];
  1275. }
  1276. ;
  1277. if(shouldEscape.test(string)) {
  1278. return string.replace(badChars, escapedChar);
  1279. }
  1280. return string;
  1281. },
  1282. popup: function(text) {
  1283. var
  1284. html = '',
  1285. escape = $.fn.popup.settings.templates.escape
  1286. ;
  1287. if(typeof text !== undefined) {
  1288. if(typeof text.title !== undefined && text.title) {
  1289. text.title = escape(text.title);
  1290. html += '<div class="header">' + text.title + '</div>';
  1291. }
  1292. if(typeof text.content !== undefined && text.content) {
  1293. text.content = escape(text.content);
  1294. html += '<div class="content">' + text.content + '</div>';
  1295. }
  1296. }
  1297. return html;
  1298. }
  1299. }
  1300. };
  1301. })( jQuery, window, document );