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.

746 lines
22 KiB

10 years ago
10 years ago
10 years ago
  1. /*
  2. * # Semantic - Modal
  3. * http://github.com/semantic-org/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. $.fn.modal = function(parameters) {
  13. var
  14. $allModules = $(this),
  15. $window = $(window),
  16. $document = $(document),
  17. $body = $('body'),
  18. moduleSelector = $allModules.selector || '',
  19. time = new Date().getTime(),
  20. performance = [],
  21. query = arguments[0],
  22. methodInvoked = (typeof query == 'string'),
  23. queryArguments = [].slice.call(arguments, 1),
  24. requestAnimationFrame = window.requestAnimationFrame
  25. || window.mozRequestAnimationFrame
  26. || window.webkitRequestAnimationFrame
  27. || window.msRequestAnimationFrame
  28. || function(callback) { setTimeout(callback, 0); },
  29. returnedValue
  30. ;
  31. $allModules
  32. .each(function() {
  33. var
  34. settings = ( $.isPlainObject(parameters) )
  35. ? $.extend(true, {}, $.fn.modal.settings, parameters)
  36. : $.extend({}, $.fn.modal.settings),
  37. selector = settings.selector,
  38. className = settings.className,
  39. namespace = settings.namespace,
  40. error = settings.error,
  41. eventNamespace = '.' + namespace,
  42. moduleNamespace = 'module-' + namespace,
  43. $module = $(this),
  44. $context = $(settings.context),
  45. $close = $module.find(selector.close),
  46. $allModals,
  47. $otherModals,
  48. $focusedElement,
  49. $dimmable,
  50. $dimmer,
  51. element = this,
  52. instance = $module.data(moduleNamespace),
  53. module
  54. ;
  55. module = {
  56. initialize: function() {
  57. module.verbose('Initializing dimmer', $context);
  58. if($.fn.dimmer === undefined) {
  59. module.error(error.dimmer);
  60. return;
  61. }
  62. $dimmable = $context
  63. .dimmer({
  64. closable : false,
  65. useCSS : true,
  66. duration : {
  67. show : settings.duration * 0.9,
  68. hide : settings.duration * 1.1
  69. }
  70. })
  71. ;
  72. if(settings.detachable) {
  73. $dimmable.dimmer('add content', $module);
  74. }
  75. $dimmer = $dimmable
  76. .dimmer('get dimmer')
  77. ;
  78. $otherModals = $module.siblings(selector.modal);
  79. $allModals = $otherModals.add($module);
  80. module.verbose('Attaching close events', $close);
  81. $close
  82. .on('click' + eventNamespace, module.event.close)
  83. ;
  84. $window
  85. .on('resize' + eventNamespace, module.event.resize)
  86. ;
  87. module.instantiate();
  88. },
  89. instantiate: function() {
  90. module.verbose('Storing instance of modal');
  91. instance = module;
  92. $module
  93. .data(moduleNamespace, instance)
  94. ;
  95. },
  96. destroy: function() {
  97. module.verbose('Destroying previous modal');
  98. $module
  99. .removeData(moduleNamespace)
  100. .off(eventNamespace)
  101. ;
  102. $close
  103. .off(eventNamespace)
  104. ;
  105. $context
  106. .dimmer('destroy')
  107. ;
  108. },
  109. refresh: function() {
  110. module.remove.scrolling();
  111. module.cacheSizes();
  112. module.set.screenHeight();
  113. module.set.type();
  114. module.set.position();
  115. },
  116. attachEvents: function(selector, event) {
  117. var
  118. $toggle = $(selector)
  119. ;
  120. event = $.isFunction(module[event])
  121. ? module[event]
  122. : module.toggle
  123. ;
  124. if($toggle.size() > 0) {
  125. module.debug('Attaching modal events to element', selector, event);
  126. $toggle
  127. .off(eventNamespace)
  128. .on('click' + eventNamespace, event)
  129. ;
  130. }
  131. else {
  132. module.error(error.notFound);
  133. }
  134. },
  135. event: {
  136. close: function() {
  137. module.verbose('Closing element pressed');
  138. if( $(this).is(selector.approve) ) {
  139. if($.proxy(settings.onApprove, element)() !== false) {
  140. module.hide();
  141. }
  142. else {
  143. module.verbose('Approve callback returned false cancelling hide');
  144. }
  145. }
  146. else if( $(this).is(selector.deny) ) {
  147. if($.proxy(settings.onDeny, element)() !== false) {
  148. module.hide();
  149. }
  150. else {
  151. module.verbose('Deny callback returned false cancelling hide');
  152. }
  153. }
  154. else {
  155. module.hide();
  156. }
  157. },
  158. click: function(event) {
  159. if( $(event.target).closest(selector.modal).size() === 0 ) {
  160. module.debug('Dimmer clicked, hiding all modals');
  161. if(settings.allowMultiple) {
  162. module.hide();
  163. }
  164. else {
  165. module.hideAll();
  166. }
  167. event.stopImmediatePropagation();
  168. }
  169. },
  170. debounce: function(method, delay) {
  171. clearTimeout(module.timer);
  172. module.timer = setTimeout(method, delay);
  173. },
  174. keyboard: function(event) {
  175. var
  176. keyCode = event.which,
  177. escapeKey = 27
  178. ;
  179. if(keyCode == escapeKey) {
  180. if(settings.closable) {
  181. module.debug('Escape key pressed hiding modal');
  182. module.hide();
  183. }
  184. else {
  185. module.debug('Escape key pressed, but closable is set to false');
  186. }
  187. event.preventDefault();
  188. }
  189. },
  190. resize: function() {
  191. if( $dimmable.dimmer('is active') ) {
  192. requestAnimationFrame(module.refresh);
  193. }
  194. }
  195. },
  196. toggle: function() {
  197. if( module.is.active() ) {
  198. module.hide();
  199. }
  200. else {
  201. module.show();
  202. }
  203. },
  204. show: function(callback) {
  205. callback = $.isFunction(callback)
  206. ? callback
  207. : function(){}
  208. ;
  209. module.showDimmer();
  210. module.showModal(callback);
  211. },
  212. showModal: function(callback) {
  213. callback = $.isFunction(callback)
  214. ? callback
  215. : function(){}
  216. ;
  217. if( !module.is.active() ) {
  218. module.cacheSizes();
  219. module.set.position();
  220. module.set.screenHeight();
  221. module.set.type();
  222. if( $otherModals.filter(':visible').size() > 0 && !settings.allowMultiple) {
  223. module.debug('Other modals visible, queueing show animation');
  224. module.hideOthers(module.showModal);
  225. }
  226. else {
  227. $.proxy(settings.onShow, element)();
  228. if(settings.transition && $.fn.transition !== undefined && $module.transition('is supported')) {
  229. module.debug('Showing modal with css animations');
  230. $module
  231. .transition(settings.transition + ' in', settings.duration, function() {
  232. $.proxy(settings.onVisible, element)();
  233. module.set.active();
  234. callback();
  235. })
  236. ;
  237. }
  238. else {
  239. module.debug('Showing modal with javascript');
  240. $module
  241. .fadeIn(settings.duration, settings.easing, function() {
  242. $.proxy(settings.onVisible, element)();
  243. module.set.active();
  244. callback();
  245. })
  246. ;
  247. }
  248. }
  249. }
  250. else {
  251. module.debug('Modal is already visible');
  252. }
  253. },
  254. showDimmer: function() {
  255. if( !$dimmable.dimmer('is active') ) {
  256. module.debug('Showing dimmer');
  257. $dimmable.dimmer('show');
  258. }
  259. else {
  260. module.debug('Dimmer already visible');
  261. }
  262. },
  263. hide: function(callback) {
  264. callback = $.isFunction(callback)
  265. ? callback
  266. : function(){}
  267. ;
  268. if($allModals.filter(':visible').size() <= 1) {
  269. module.hideDimmer();
  270. }
  271. module.hideModal(callback);
  272. },
  273. hideDimmer: function() {
  274. if( !module.is.active() ) {
  275. module.debug('Dimmer is not visible cannot hide');
  276. return;
  277. }
  278. module.debug('Hiding dimmer');
  279. if(settings.closable) {
  280. $dimmer
  281. .off('click' + eventNamespace)
  282. ;
  283. }
  284. $dimmable.dimmer('hide', function() {
  285. if(settings.transition && $.fn.transition !== undefined && $module.transition('is supported')) {
  286. $module
  287. .transition('reset')
  288. ;
  289. module.remove.screenHeight();
  290. }
  291. module.remove.active();
  292. });
  293. },
  294. hideModal: function(callback) {
  295. callback = $.isFunction(callback)
  296. ? callback
  297. : function(){}
  298. ;
  299. if( !module.is.active() ) {
  300. module.debug('Cannot hide modal it is not active');
  301. return;
  302. }
  303. module.debug('Hiding modal');
  304. module.remove.keyboardShortcuts();
  305. $.proxy(settings.onHide, element)();
  306. if(settings.transition && $.fn.transition !== undefined && $module.transition('is supported')) {
  307. $module
  308. .transition(settings.transition + ' out', settings.duration, function() {
  309. $.proxy(settings.onHidden, element)();
  310. module.remove.active();
  311. module.restore.focus();
  312. callback();
  313. })
  314. ;
  315. }
  316. else {
  317. $module
  318. .fadeOut(settings.duration, settings.easing, function() {
  319. $.proxy(settings.onHidden, element)();
  320. module.remove.active();
  321. module.restore.focus();
  322. callback();
  323. })
  324. ;
  325. }
  326. },
  327. hideAll: function(callback) {
  328. callback = $.isFunction(callback)
  329. ? callback
  330. : function(){}
  331. ;
  332. if( $allModals.is(':visible') ) {
  333. module.debug('Hiding all visible modals');
  334. module.hideDimmer();
  335. $allModals
  336. .filter(':visible')
  337. .modal('hide modal', callback)
  338. ;
  339. }
  340. },
  341. hideOthers: function(callback) {
  342. callback = $.isFunction(callback)
  343. ? callback
  344. : function(){}
  345. ;
  346. if( $otherModals.is(':visible') ) {
  347. module.debug('Hiding other modals');
  348. $otherModals
  349. .filter(':visible')
  350. .modal('hide modal', callback)
  351. ;
  352. }
  353. },
  354. add: {
  355. keyboardShortcuts: function() {
  356. module.verbose('Adding keyboard shortcuts');
  357. $document
  358. .on('keyup' + eventNamespace, module.event.keyboard)
  359. ;
  360. }
  361. },
  362. save: {
  363. focus: function() {
  364. $focusedElement = $(document.activeElement).blur();
  365. }
  366. },
  367. restore: {
  368. focus: function() {
  369. if($focusedElement && $focusedElement.size() > 0) {
  370. $focusedElement.focus();
  371. }
  372. }
  373. },
  374. remove: {
  375. active: function() {
  376. $module.removeClass(className.active);
  377. },
  378. screenHeight: function() {
  379. if(module.cache.height > module.cache.pageHeight) {
  380. module.debug('Removing page height');
  381. $body
  382. .css('height', '')
  383. ;
  384. }
  385. },
  386. keyboardShortcuts: function() {
  387. module.verbose('Removing keyboard shortcuts');
  388. $document
  389. .off('keyup' + eventNamespace)
  390. ;
  391. },
  392. scrolling: function() {
  393. $dimmable.removeClass(className.scrolling);
  394. $module.removeClass(className.scrolling);
  395. }
  396. },
  397. cacheSizes: function() {
  398. module.cache = {
  399. pageHeight : $(document).outerHeight(),
  400. height : $module.outerHeight() + settings.offset,
  401. contextHeight : (settings.context == 'body')
  402. ? $(window).height()
  403. : $dimmable.height()
  404. };
  405. module.debug('Caching modal and container sizes', module.cache);
  406. },
  407. can: {
  408. fit: function() {
  409. return (module.cache.height < module.cache.contextHeight);
  410. }
  411. },
  412. is: {
  413. active: function() {
  414. return $module.hasClass(className.active);
  415. },
  416. modernBrowser: function() {
  417. // appName for IE11 reports 'Netscape' can no longer use
  418. return !(window.ActiveXObject || "ActiveXObject" in window);
  419. }
  420. },
  421. set: {
  422. screenHeight: function() {
  423. if(module.cache.height > module.cache.pageHeight) {
  424. module.debug('Modal is taller than page content, resizing page height');
  425. $body
  426. .css('height', module.cache.height + settings.padding)
  427. ;
  428. }
  429. },
  430. active: function() {
  431. module.add.keyboardShortcuts();
  432. module.save.focus();
  433. $module
  434. .addClass(className.active)
  435. ;
  436. if(settings.closable) {
  437. $dimmer
  438. .off('click' + eventNamespace)
  439. .on('click' + eventNamespace, module.event.click)
  440. ;
  441. }
  442. },
  443. scrolling: function() {
  444. $dimmable.addClass(className.scrolling);
  445. $module.addClass(className.scrolling);
  446. },
  447. type: function() {
  448. if(module.can.fit()) {
  449. module.verbose('Modal fits on screen');
  450. module.remove.scrolling();
  451. }
  452. else {
  453. module.verbose('Modal cannot fit on screen setting to scrolling');
  454. module.set.scrolling();
  455. }
  456. },
  457. position: function() {
  458. module.verbose('Centering modal on page', module.cache);
  459. if(module.can.fit()) {
  460. $module
  461. .css({
  462. top: '',
  463. marginTop: -(module.cache.height / 2)
  464. })
  465. ;
  466. }
  467. else {
  468. $module
  469. .css({
  470. marginTop : '',
  471. top : $document.scrollTop()
  472. })
  473. ;
  474. }
  475. }
  476. },
  477. setting: function(name, value) {
  478. module.debug('Changing setting', name, value);
  479. if( $.isPlainObject(name) ) {
  480. $.extend(true, settings, name);
  481. }
  482. else if(value !== undefined) {
  483. settings[name] = value;
  484. }
  485. else {
  486. return settings[name];
  487. }
  488. },
  489. internal: function(name, value) {
  490. if( $.isPlainObject(name) ) {
  491. $.extend(true, module, name);
  492. }
  493. else if(value !== undefined) {
  494. module[name] = value;
  495. }
  496. else {
  497. return module[name];
  498. }
  499. },
  500. debug: function() {
  501. if(settings.debug) {
  502. if(settings.performance) {
  503. module.performance.log(arguments);
  504. }
  505. else {
  506. module.debug = Function.prototype.bind.call(console.info, console, settings.name + ':');
  507. module.debug.apply(console, arguments);
  508. }
  509. }
  510. },
  511. verbose: function() {
  512. if(settings.verbose && settings.debug) {
  513. if(settings.performance) {
  514. module.performance.log(arguments);
  515. }
  516. else {
  517. module.verbose = Function.prototype.bind.call(console.info, console, settings.name + ':');
  518. module.verbose.apply(console, arguments);
  519. }
  520. }
  521. },
  522. error: function() {
  523. module.error = Function.prototype.bind.call(console.error, console, settings.name + ':');
  524. module.error.apply(console, arguments);
  525. },
  526. performance: {
  527. log: function(message) {
  528. var
  529. currentTime,
  530. executionTime,
  531. previousTime
  532. ;
  533. if(settings.performance) {
  534. currentTime = new Date().getTime();
  535. previousTime = time || currentTime;
  536. executionTime = currentTime - previousTime;
  537. time = currentTime;
  538. performance.push({
  539. 'Element' : element,
  540. 'Name' : message[0],
  541. 'Arguments' : [].slice.call(message, 1) || '',
  542. 'Execution Time' : executionTime
  543. });
  544. }
  545. clearTimeout(module.performance.timer);
  546. module.performance.timer = setTimeout(module.performance.display, 100);
  547. },
  548. display: function() {
  549. var
  550. title = settings.name + ':',
  551. totalTime = 0
  552. ;
  553. time = false;
  554. clearTimeout(module.performance.timer);
  555. $.each(performance, function(index, data) {
  556. totalTime += data['Execution Time'];
  557. });
  558. title += ' ' + totalTime + 'ms';
  559. if(moduleSelector) {
  560. title += ' \'' + moduleSelector + '\'';
  561. }
  562. if( (console.group !== undefined || console.table !== undefined) && performance.length > 0) {
  563. console.groupCollapsed(title);
  564. if(console.table) {
  565. console.table(performance);
  566. }
  567. else {
  568. $.each(performance, function(index, data) {
  569. console.log(data['Name'] + ': ' + data['Execution Time']+'ms');
  570. });
  571. }
  572. console.groupEnd();
  573. }
  574. performance = [];
  575. }
  576. },
  577. invoke: function(query, passedArguments, context) {
  578. var
  579. object = instance,
  580. maxDepth,
  581. found,
  582. response
  583. ;
  584. passedArguments = passedArguments || queryArguments;
  585. context = element || context;
  586. if(typeof query == 'string' && object !== undefined) {
  587. query = query.split(/[\. ]/);
  588. maxDepth = query.length - 1;
  589. $.each(query, function(depth, value) {
  590. var camelCaseValue = (depth != maxDepth)
  591. ? value + query[depth + 1].charAt(0).toUpperCase() + query[depth + 1].slice(1)
  592. : query
  593. ;
  594. if( $.isPlainObject( object[camelCaseValue] ) && (depth != maxDepth) ) {
  595. object = object[camelCaseValue];
  596. }
  597. else if( object[camelCaseValue] !== undefined ) {
  598. found = object[camelCaseValue];
  599. return false;
  600. }
  601. else if( $.isPlainObject( object[value] ) && (depth != maxDepth) ) {
  602. object = object[value];
  603. }
  604. else if( object[value] !== undefined ) {
  605. found = object[value];
  606. return false;
  607. }
  608. else {
  609. return false;
  610. }
  611. });
  612. }
  613. if ( $.isFunction( found ) ) {
  614. response = found.apply(context, passedArguments);
  615. }
  616. else if(found !== undefined) {
  617. response = found;
  618. }
  619. if($.isArray(returnedValue)) {
  620. returnedValue.push(response);
  621. }
  622. else if(returnedValue !== undefined) {
  623. returnedValue = [returnedValue, response];
  624. }
  625. else if(response !== undefined) {
  626. returnedValue = response;
  627. }
  628. return found;
  629. }
  630. };
  631. if(methodInvoked) {
  632. if(instance === undefined) {
  633. module.initialize();
  634. }
  635. module.invoke(query);
  636. }
  637. else {
  638. if(instance !== undefined) {
  639. module.destroy();
  640. }
  641. module.initialize();
  642. }
  643. })
  644. ;
  645. return (returnedValue !== undefined)
  646. ? returnedValue
  647. : this
  648. ;
  649. };
  650. $.fn.modal.settings = {
  651. name : 'Modal',
  652. namespace : 'modal',
  653. debug : false,
  654. verbose : true,
  655. performance : true,
  656. allowMultiple : true,
  657. detachable : true,
  658. closable : true,
  659. context : 'body',
  660. duration : 500,
  661. easing : 'easeOutExpo',
  662. offset : 0,
  663. transition : 'scale',
  664. padding : 30,
  665. onShow : function(){},
  666. onHide : function(){},
  667. onVisible : function(){},
  668. onHidden : function(){},
  669. onApprove : function(){ return true; },
  670. onDeny : function(){ return true; },
  671. selector : {
  672. close : '.close, .actions .button',
  673. approve : '.actions .positive, .actions .approve, .actions .ok',
  674. deny : '.actions .negative, .actions .deny, .actions .cancel',
  675. modal : '.ui.modal'
  676. },
  677. error : {
  678. dimmer : 'UI Dimmer, a required component is not included in this page',
  679. method : 'The method you called is not defined.',
  680. notFound : 'The element you specified could not be found'
  681. },
  682. className : {
  683. active : 'active',
  684. scrolling : 'scrolling'
  685. }
  686. };
  687. })( jQuery, window , document );