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.

856 lines
26 KiB

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