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.

815 lines
24 KiB

11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
  1. /*
  2. * # Semantic - Popup
  3. * http://github.com/jlukic/semantic-ui/
  4. *
  5. *
  6. * Copyright 2013 Contributors
  7. * Released under the MIT license
  8. * http://opensource.org/licenses/MIT
  9. *
  10. */
  11. ;(function ($, window, document, undefined) {
  12. $.fn.popup = function(parameters) {
  13. var
  14. $allModules = $(this),
  15. $document = $(document),
  16. $body = $('body'),
  17. moduleSelector = $allModules.selector || '',
  18. time = new Date().getTime(),
  19. performance = [],
  20. query = arguments[0],
  21. methodInvoked = (typeof query == 'string'),
  22. queryArguments = [].slice.call(arguments, 1),
  23. returnedValue
  24. ;
  25. $allModules
  26. .each(function() {
  27. var
  28. settings = ( $.isPlainObject(parameters) )
  29. ? $.extend(true, {}, $.fn.popup.settings, parameters)
  30. : $.extend({}, $.fn.popup.settings),
  31. selector = settings.selector,
  32. className = settings.className,
  33. error = settings.error,
  34. metadata = settings.metadata,
  35. namespace = settings.namespace,
  36. eventNamespace = '.' + settings.namespace,
  37. moduleNamespace = 'module-' + namespace,
  38. $module = $(this),
  39. $target = (settings.target)
  40. ? $(settings.target)
  41. : $module,
  42. $window = $(window),
  43. $offsetParent = (settings.inline)
  44. ? $target.offsetParent()
  45. : $window,
  46. $popup = (settings.inline)
  47. ? $target.next(settings.selector.popup)
  48. : $window.children(settings.selector.popup).last(),
  49. searchDepth = 0,
  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. if(settings.on == 'click') {
  59. $module
  60. .on('click', module.toggle)
  61. ;
  62. }
  63. else {
  64. $module
  65. .on(module.get.startEvent() + eventNamespace, module.event.start)
  66. .on(module.get.endEvent() + eventNamespace, module.event.end)
  67. ;
  68. }
  69. if(settings.target) {
  70. module.debug('Target set to element', $target);
  71. }
  72. $window
  73. .on('resize' + eventNamespace, module.event.resize)
  74. ;
  75. module.instantiate();
  76. },
  77. instantiate: function() {
  78. module.verbose('Storing instance of module', module);
  79. instance = module;
  80. $module
  81. .data(moduleNamespace, instance)
  82. ;
  83. },
  84. refresh: function() {
  85. if(settings.inline) {
  86. $popup = $target.next(selector.popup);
  87. $offsetParent = $target.offsetParent();
  88. }
  89. else {
  90. console.log('found by refresh');
  91. $popup = $window.children(selector.popup).last();
  92. }
  93. },
  94. destroy: function() {
  95. module.debug('Destroying previous module');
  96. $window
  97. .off(eventNamespace)
  98. ;
  99. $popup
  100. .remove()
  101. ;
  102. $module
  103. .off(eventNamespace)
  104. .removeData(moduleNamespace)
  105. ;
  106. },
  107. event: {
  108. start: function(event) {
  109. module.timer = setTimeout(function() {
  110. if( module.is.hidden() ) {
  111. module.show();
  112. }
  113. }, settings.delay);
  114. },
  115. end: function() {
  116. clearTimeout(module.timer);
  117. if( module.is.visible() ) {
  118. module.hide();
  119. }
  120. },
  121. resize: function() {
  122. if( module.is.visible() ) {
  123. module.set.position();
  124. }
  125. }
  126. },
  127. // generates popup html from metadata
  128. create: function() {
  129. module.debug('Creating pop-up html');
  130. var
  131. html = $module.data(metadata.html) || settings.html,
  132. variation = $module.data(metadata.variation) || settings.variation,
  133. title = $module.data(metadata.title) || settings.title,
  134. content = $module.data(metadata.content) || $module.attr('title') || settings.content
  135. ;
  136. if(html || content || title) {
  137. if(!html) {
  138. html = settings.template({
  139. title : title,
  140. content : content
  141. });
  142. }
  143. $popup = $('<div/>')
  144. .addClass(className.popup)
  145. .addClass(variation)
  146. .html(html)
  147. ;
  148. if(settings.inline) {
  149. module.verbose('Inserting popup element inline', $popup);
  150. $popup
  151. .insertAfter($module)
  152. ;
  153. }
  154. else {
  155. module.verbose('Appending popup element to body', $popup);
  156. $popup
  157. .appendTo( $body )
  158. ;
  159. }
  160. $.proxy(settings.onCreate, $popup)();
  161. }
  162. else {
  163. module.error(error.content);
  164. }
  165. },
  166. // determines popup state
  167. toggle: function() {
  168. module.debug('Toggling pop-up');
  169. if( module.is.hidden() ) {
  170. module.hideAll();
  171. module.show();
  172. }
  173. else {
  174. module.hide();
  175. }
  176. },
  177. show: function(callback) {
  178. callback = callback || function(){};
  179. module.debug('Showing pop-up', settings.transition);
  180. if(!settings.preserve) {
  181. module.refresh();
  182. }
  183. if( !module.exists() ) {
  184. module.create();
  185. }
  186. module.set.position();
  187. module.animate.show(callback);
  188. },
  189. hide: function(callback) {
  190. callback = callback || function(){};
  191. $module
  192. .removeClass(className.visible)
  193. ;
  194. module.unbind.close();
  195. if( module.is.visible() ) {
  196. module.animate.hide(callback);
  197. }
  198. },
  199. hideAll: function() {
  200. $(selector.popup)
  201. .filter(':visible')
  202. .popup('hide')
  203. ;
  204. },
  205. hideGracefully: function(event) {
  206. // don't close on clicks inside popup
  207. if( $(event.target).closest(selector.popup).size() === 0) {
  208. module.hide();
  209. }
  210. },
  211. exists: function() {
  212. if(settings.inline) {
  213. return ( $popup.size() !== 0 );
  214. }
  215. else {
  216. return ( $popup.parent($body).size() );
  217. }
  218. },
  219. remove: function() {
  220. module.debug('Removing popup');
  221. $popup
  222. .remove()
  223. ;
  224. },
  225. animate: {
  226. show: function(callback) {
  227. callback = callback || function(){};
  228. $module
  229. .addClass(className.visible)
  230. ;
  231. if(settings.transition && $.fn.transition !== undefined) {
  232. $popup
  233. .transition(settings.transition + ' in', settings.duration, function() {
  234. module.bind.close();
  235. $.proxy(callback, element)();
  236. })
  237. ;
  238. }
  239. else {
  240. $popup
  241. .stop()
  242. .fadeIn(settings.duration, settings.easing, function() {
  243. module.bind.close();
  244. $.proxy(callback, element)();
  245. })
  246. ;
  247. }
  248. $.proxy(settings.onShow, element)();
  249. },
  250. hide: function(callback) {
  251. callback = callback || function(){};
  252. module.debug('Hiding pop-up');
  253. if(settings.transition && $.fn.transition !== undefined) {
  254. $popup
  255. .transition(settings.transition + ' out', settings.duration, function() {
  256. module.reset();
  257. callback();
  258. })
  259. ;
  260. }
  261. else {
  262. $popup
  263. .stop()
  264. .fadeOut(settings.duration, settings.easing, function() {
  265. module.reset();
  266. callback();
  267. })
  268. ;
  269. }
  270. $.proxy(settings.onHide, element)();
  271. }
  272. },
  273. get: {
  274. startEvent: function() {
  275. if(settings.on == 'hover') {
  276. return 'mouseenter';
  277. }
  278. else if(settings.on == 'focus') {
  279. return 'focus';
  280. }
  281. },
  282. endEvent: function() {
  283. if(settings.on == 'hover') {
  284. return 'mouseleave';
  285. }
  286. else if(settings.on == 'focus') {
  287. return 'blur';
  288. }
  289. },
  290. offstagePosition: function() {
  291. var
  292. boundary = {
  293. top : $(window).scrollTop(),
  294. bottom : $(window).scrollTop() + $(window).height(),
  295. left : 0,
  296. right : $(window).width()
  297. },
  298. popup = {
  299. width : $popup.width(),
  300. height : $popup.outerHeight(),
  301. position : $popup.offset()
  302. },
  303. offstage = {},
  304. offstagePositions = []
  305. ;
  306. if(popup.position) {
  307. offstage = {
  308. top : (popup.position.top < boundary.top),
  309. bottom : (popup.position.top + popup.height > boundary.bottom),
  310. right : (popup.position.left + popup.width > boundary.right),
  311. left : (popup.position.left < boundary.left)
  312. };
  313. }
  314. module.verbose('Checking if outside viewable area', popup.position);
  315. // return only boundaries that have been surpassed
  316. $.each(offstage, function(direction, isOffstage) {
  317. if(isOffstage) {
  318. offstagePositions.push(direction);
  319. }
  320. });
  321. return (offstagePositions.length > 0)
  322. ? offstagePositions.join(' ')
  323. : false
  324. ;
  325. },
  326. nextPosition: function(position) {
  327. switch(position) {
  328. case 'top left':
  329. position = 'bottom left';
  330. break;
  331. case 'bottom left':
  332. position = 'top right';
  333. break;
  334. case 'top right':
  335. position = 'bottom right';
  336. break;
  337. case 'bottom right':
  338. position = 'top center';
  339. break;
  340. case 'top center':
  341. position = 'bottom center';
  342. break;
  343. case 'bottom center':
  344. position = 'right center';
  345. break;
  346. case 'right center':
  347. position = 'left center';
  348. break;
  349. case 'left center':
  350. position = 'top center';
  351. break;
  352. }
  353. return position;
  354. }
  355. },
  356. set: {
  357. position: function(position, arrowOffset) {
  358. var
  359. windowWidth = $(window).width(),
  360. windowHeight = $(window).height(),
  361. width = $target.outerWidth(),
  362. height = $target.outerHeight(),
  363. popupWidth = $popup.width(),
  364. popupHeight = $popup.outerHeight(),
  365. parentWidth = $offsetParent.outerWidth(),
  366. parentHeight = $offsetParent.outerHeight(),
  367. distanceAway = settings.distanceAway,
  368. offset = (settings.inline)
  369. ? $target.position()
  370. : $target.offset(),
  371. positioning,
  372. offstagePosition
  373. ;
  374. position = position || $module.data(metadata.position) || settings.position;
  375. arrowOffset = arrowOffset || $module.data(metadata.offset) || settings.offset;
  376. // adjust for margin when inline
  377. if(settings.inline) {
  378. if(position == 'left center' || position == 'right center') {
  379. arrowOffset += parseInt( window.getComputedStyle(element).getPropertyValue('margin-top'), 10);
  380. distanceAway += -parseInt( window.getComputedStyle(element).getPropertyValue('margin-left'), 10);
  381. }
  382. else {
  383. arrowOffset += parseInt( window.getComputedStyle(element).getPropertyValue('margin-left'), 10);
  384. distanceAway += parseInt( window.getComputedStyle(element).getPropertyValue('margin-top'), 10);
  385. }
  386. }
  387. module.debug('Calculating offset for position', position);
  388. switch(position) {
  389. case 'top left':
  390. positioning = {
  391. bottom : parentHeight - offset.top + distanceAway,
  392. right : parentWidth - offset.left - arrowOffset,
  393. top : 'auto',
  394. left : 'auto'
  395. };
  396. break;
  397. case 'top center':
  398. positioning = {
  399. bottom : parentHeight - offset.top + distanceAway,
  400. left : offset.left + (width / 2) - (popupWidth / 2) + arrowOffset,
  401. top : 'auto',
  402. right : 'auto'
  403. };
  404. break;
  405. case 'top right':
  406. positioning = {
  407. top : 'auto',
  408. bottom : parentHeight - offset.top + distanceAway,
  409. left : offset.left + width + arrowOffset,
  410. right : 'auto'
  411. };
  412. break;
  413. case 'left center':
  414. positioning = {
  415. top : offset.top + (height / 2) - (popupHeight / 2) + arrowOffset,
  416. right : parentWidth - offset.left + distanceAway,
  417. left : 'auto',
  418. bottom : 'auto'
  419. };
  420. break;
  421. case 'right center':
  422. positioning = {
  423. top : offset.top + (height / 2) - (popupHeight / 2) + arrowOffset,
  424. left : offset.left + width + distanceAway,
  425. bottom : 'auto',
  426. right : 'auto'
  427. };
  428. break;
  429. case 'bottom left':
  430. positioning = {
  431. top : offset.top + height + distanceAway,
  432. right : parentWidth - offset.left - arrowOffset,
  433. left : 'auto',
  434. bottom : 'auto'
  435. };
  436. break;
  437. case 'bottom center':
  438. positioning = {
  439. top : offset.top + height + distanceAway,
  440. left : offset.left + (width / 2) - (popupWidth / 2) + arrowOffset,
  441. bottom : 'auto',
  442. right : 'auto'
  443. };
  444. break;
  445. case 'bottom right':
  446. positioning = {
  447. top : offset.top + height + distanceAway,
  448. left : offset.left + width + arrowOffset,
  449. bottom : 'auto',
  450. right : 'auto'
  451. };
  452. break;
  453. }
  454. // tentatively place on stage
  455. $popup
  456. .css(positioning)
  457. .removeClass(className.position)
  458. .addClass(position)
  459. ;
  460. // check if is offstage
  461. offstagePosition = module.get.offstagePosition();
  462. // recursively find new positioning
  463. if(offstagePosition) {
  464. module.debug('Element is outside boundaries', offstagePosition);
  465. if(searchDepth < settings.maxSearchDepth) {
  466. position = module.get.nextPosition(position);
  467. searchDepth++;
  468. module.debug('Trying new position', position);
  469. return module.set.position(position);
  470. }
  471. else {
  472. module.error(error.recursion);
  473. searchDepth = 0;
  474. return false;
  475. }
  476. }
  477. else {
  478. module.debug('Position is on stage', position);
  479. searchDepth = 0;
  480. return true;
  481. }
  482. }
  483. },
  484. bind: {
  485. close:function() {
  486. if(settings.on == 'click' && settings.closable) {
  487. module.verbose('Binding popup close event to document');
  488. $document
  489. .on('click' + eventNamespace, module.hideGracefully)
  490. ;
  491. }
  492. }
  493. },
  494. unbind: {
  495. close: function() {
  496. if(settings.on == 'click' && settings.closable) {
  497. module.verbose('Removing close event from document');
  498. $document
  499. .off('click' + eventNamespace)
  500. ;
  501. }
  502. }
  503. },
  504. is: {
  505. visible: function() {
  506. return $popup.is(':visible');
  507. },
  508. hidden: function() {
  509. return !module.is.visible();
  510. }
  511. },
  512. reset: function() {
  513. $popup
  514. .attr('style', '')
  515. .removeAttr('style')
  516. ;
  517. if(!settings.preserve) {
  518. module.remove();
  519. }
  520. },
  521. setting: function(name, value) {
  522. if( $.isPlainObject(name) ) {
  523. $.extend(true, settings, name);
  524. }
  525. else if(value !== undefined) {
  526. settings[name] = value;
  527. }
  528. else {
  529. return settings[name];
  530. }
  531. },
  532. internal: function(name, value) {
  533. if( $.isPlainObject(name) ) {
  534. $.extend(true, module, name);
  535. }
  536. else if(value !== undefined) {
  537. module[name] = value;
  538. }
  539. else {
  540. return module[name];
  541. }
  542. },
  543. debug: function() {
  544. if(settings.debug) {
  545. if(settings.performance) {
  546. module.performance.log(arguments);
  547. }
  548. else {
  549. module.debug = Function.prototype.bind.call(console.info, console, settings.name + ':');
  550. module.debug.apply(console, arguments);
  551. }
  552. }
  553. },
  554. verbose: function() {
  555. if(settings.verbose && settings.debug) {
  556. if(settings.performance) {
  557. module.performance.log(arguments);
  558. }
  559. else {
  560. module.verbose = Function.prototype.bind.call(console.info, console, settings.name + ':');
  561. module.verbose.apply(console, arguments);
  562. }
  563. }
  564. },
  565. error: function() {
  566. module.error = Function.prototype.bind.call(console.error, console, settings.name + ':');
  567. module.error.apply(console, arguments);
  568. },
  569. performance: {
  570. log: function(message) {
  571. var
  572. currentTime,
  573. executionTime,
  574. previousTime
  575. ;
  576. if(settings.performance) {
  577. currentTime = new Date().getTime();
  578. previousTime = time || currentTime;
  579. executionTime = currentTime - previousTime;
  580. time = currentTime;
  581. performance.push({
  582. 'Element' : element,
  583. 'Name' : message[0],
  584. 'Arguments' : [].slice.call(message, 1) || '',
  585. 'Execution Time' : executionTime
  586. });
  587. }
  588. clearTimeout(module.performance.timer);
  589. module.performance.timer = setTimeout(module.performance.display, 100);
  590. },
  591. display: function() {
  592. var
  593. title = settings.name + ':',
  594. totalTime = 0
  595. ;
  596. time = false;
  597. clearTimeout(module.performance.timer);
  598. $.each(performance, function(index, data) {
  599. totalTime += data['Execution Time'];
  600. });
  601. title += ' ' + totalTime + 'ms';
  602. if(moduleSelector) {
  603. title += ' \'' + moduleSelector + '\'';
  604. }
  605. if( (console.group !== undefined || console.table !== undefined) && performance.length > 0) {
  606. console.groupCollapsed(title);
  607. if(console.table) {
  608. console.table(performance);
  609. }
  610. else {
  611. $.each(performance, function(index, data) {
  612. console.log(data['Name'] + ': ' + data['Execution Time']+'ms');
  613. });
  614. }
  615. console.groupEnd();
  616. }
  617. performance = [];
  618. }
  619. },
  620. invoke: function(query, passedArguments, context) {
  621. var
  622. maxDepth,
  623. found,
  624. response
  625. ;
  626. passedArguments = passedArguments || queryArguments;
  627. context = element || context;
  628. if(typeof query == 'string' && instance !== undefined) {
  629. query = query.split(/[\. ]/);
  630. maxDepth = query.length - 1;
  631. $.each(query, function(depth, value) {
  632. var camelCaseValue = (depth != maxDepth)
  633. ? value + query[depth + 1].charAt(0).toUpperCase() + query[depth + 1].slice(1)
  634. : query
  635. ;
  636. if( $.isPlainObject( instance[value] ) && (depth != maxDepth) ) {
  637. instance = instance[value];
  638. }
  639. else if( $.isPlainObject( instance[camelCaseValue] ) && (depth != maxDepth) ) {
  640. instance = instance[camelCaseValue];
  641. }
  642. else if( instance[value] !== undefined ) {
  643. found = instance[value];
  644. return false;
  645. }
  646. else if( instance[camelCaseValue] !== undefined ) {
  647. found = instance[camelCaseValue];
  648. return false;
  649. }
  650. else {
  651. module.error(error.method, query);
  652. return false;
  653. }
  654. });
  655. }
  656. if ( $.isFunction( found ) ) {
  657. response = found.apply(context, passedArguments);
  658. }
  659. else if(found !== undefined) {
  660. response = found;
  661. }
  662. if($.isArray(returnedValue)) {
  663. returnedValue.push(response);
  664. }
  665. else if(returnedValue !== undefined) {
  666. returnedValue = [returnedValue, response];
  667. }
  668. else if(response !== undefined) {
  669. returnedValue = response;
  670. }
  671. return found;
  672. }
  673. };
  674. if(methodInvoked) {
  675. if(instance === undefined) {
  676. module.initialize();
  677. }
  678. module.invoke(query);
  679. }
  680. else {
  681. if(instance !== undefined) {
  682. module.destroy();
  683. }
  684. module.initialize();
  685. }
  686. })
  687. ;
  688. return (returnedValue !== undefined)
  689. ? returnedValue
  690. : this
  691. ;
  692. };
  693. $.fn.popup.settings = {
  694. name : 'Popup',
  695. debug : true,
  696. verbose : true,
  697. performance : true,
  698. namespace : 'popup',
  699. onCreate : function(){},
  700. onShow : function(){},
  701. onHide : function(){},
  702. variation : '',
  703. content : false,
  704. html : false,
  705. title : false,
  706. on : 'hover',
  707. target : false,
  708. closable : true,
  709. position : 'top center',
  710. delay : 150,
  711. inline : false,
  712. preserve : false,
  713. duration : 250,
  714. easing : 'easeOutQuint',
  715. transition : 'scale',
  716. distanceAway : 0,
  717. offset : 0,
  718. maxSearchDepth : 10,
  719. error: {
  720. content : 'Your popup has no content specified',
  721. method : 'The method you called is not defined.',
  722. recursion : 'Popup attempted to reposition element to fit, but could not find an adequate position.'
  723. },
  724. metadata: {
  725. content : 'content',
  726. html : 'html',
  727. offset : 'offset',
  728. position : 'position',
  729. title : 'title',
  730. variation : 'variation'
  731. },
  732. className : {
  733. loading : 'loading',
  734. popup : 'ui popup',
  735. position : 'top left center bottom right',
  736. visible : 'visible'
  737. },
  738. selector : {
  739. popup : '.ui.popup'
  740. },
  741. template: function(text) {
  742. var html = '';
  743. if(typeof text !== undefined) {
  744. if(typeof text.title !== undefined && text.title) {
  745. html += '<div class="header">' + text.title + '</div class="header">';
  746. }
  747. if(typeof text.content !== undefined && text.content) {
  748. html += '<div class="content">' + text.content + '</div>';
  749. }
  750. }
  751. return html;
  752. }
  753. };
  754. })( jQuery, window , document );