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.

1151 lines
36 KiB

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