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.

691 lines
21 KiB

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