popup.js 47 KB

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