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.

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