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.

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