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.

99 lines
2.5 KiB

1 year ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /**
  2. * dialogFx.js v1.0.0
  3. * http://www.codrops.com
  4. *
  5. * Licensed under the MIT license.
  6. * http://www.opensource.org/licenses/mit-license.php
  7. *
  8. * Copyright 2014, Codrops
  9. * http://www.codrops.com
  10. */
  11. ;( function( window ) {
  12. 'use strict';
  13. var support = { animations : Modernizr.cssanimations },
  14. animEndEventNames = { 'WebkitAnimation' : 'webkitAnimationEnd', 'OAnimation' : 'oAnimationEnd', 'msAnimation' : 'MSAnimationEnd', 'animation' : 'animationend' },
  15. animEndEventName = animEndEventNames[ Modernizr.prefixed( 'animation' ) ],
  16. onEndAnimation = function( el, callback ) {
  17. var onEndCallbackFn = function( ev ) {
  18. if( support.animations ) {
  19. if( ev.target != this ) return;
  20. this.removeEventListener( animEndEventName, onEndCallbackFn );
  21. }
  22. if( callback && typeof callback === 'function' ) { callback.call(); }
  23. };
  24. if( support.animations ) {
  25. el.addEventListener( animEndEventName, onEndCallbackFn );
  26. }
  27. else {
  28. onEndCallbackFn();
  29. }
  30. };
  31. function extend( a, b ) {
  32. for( var key in b ) {
  33. if( b.hasOwnProperty( key ) ) {
  34. a[key] = b[key];
  35. }
  36. }
  37. return a;
  38. }
  39. function DialogFx( el, options ) {
  40. this.el = el;
  41. this.options = extend( {}, this.options );
  42. extend( this.options, options );
  43. this.ctrlClose = this.el.querySelector( '[data-dialog-close]' );
  44. this.isOpen = false;
  45. this._initEvents();
  46. }
  47. DialogFx.prototype.options = {
  48. // callbacks
  49. onOpenDialog : function() { return false; },
  50. onCloseDialog : function() { return false; }
  51. }
  52. DialogFx.prototype._initEvents = function() {
  53. var self = this;
  54. // close action
  55. this.ctrlClose.addEventListener( 'click', this.toggle.bind(this) );
  56. // esc key closes dialog
  57. document.addEventListener( 'keydown', function( ev ) {
  58. var keyCode = ev.keyCode || ev.which;
  59. if( keyCode === 27 && self.isOpen ) {
  60. self.toggle();
  61. }
  62. } );
  63. this.el.querySelector( '.dialog__overlay' ).addEventListener( 'click', this.toggle.bind(this) );
  64. }
  65. DialogFx.prototype.toggle = function() {
  66. var self = this;
  67. if( this.isOpen ) {
  68. classie.remove( this.el, 'dialog--open' );
  69. classie.add( self.el, 'dialog--close' );
  70. onEndAnimation( this.el.querySelector( '.dialog__content' ), function() {
  71. classie.remove( self.el, 'dialog--close' );
  72. } );
  73. // callback on close
  74. this.options.onCloseDialog( this );
  75. }
  76. else {
  77. classie.add( this.el, 'dialog--open' );
  78. // callback on open
  79. this.options.onOpenDialog( this );
  80. }
  81. this.isOpen = !this.isOpen;
  82. };
  83. // add to global namespace
  84. window.DialogFx = DialogFx;
  85. })( window );