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.

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