vendor/assets/js/foundation.reveal.js in foundation-rails-6.1.1.3 vs vendor/assets/js/foundation.reveal.js in foundation-rails-6.1.2.0
- old
+ new
@@ -104,15 +104,21 @@
* @option
* @example true
*/
overlay: true,
/**
- * Allows the modal to remove and reinject markup on close. Should be true if using video elements w/o using provider's api.
+ * Allows the modal to remove and reinject markup on close. Should be true if using video elements w/o using provider's api, otherwise, videos will continue to play in the background.
* @option
* @example false
*/
- resetOnClose: false
+ resetOnClose: false,
+ /**
+ * Allows the modal to alter the url on open/close, and allows the use of the `back` button to close modals. ALSO, allows a modal to auto-maniacally open on page load IF the hash === the modal's user-set id.
+ * @option
+ * @example false
+ */
+ deepLink: false
};
/**
* Initializes the modal by adding the overlay and close buttons, (if selected).
* @private
@@ -150,10 +156,13 @@
'data-yeti-box': this.id,
'data-resize': this.id
});
this._events();
+ if(this.options.deepLink && window.location.hash === ( '#' + this.id)){
+ $(window).one('load.zf.reveal', this.open.bind(this));
+ }
};
/**
* Creates an overlay div to display behind the modal.
* @private
@@ -201,12 +210,23 @@
if(this.options.closeOnClick && this.options.overlay){
this.$overlay.off('.zf.reveal').on('click.zf.reveal', this.close.bind(this));
}
+ if(this.options.deepLink){
+ $(window).on('popstate.zf.reveal:' + this.id, this._handleState.bind(this));
+ }
};
/**
+ * Handles modal methods on back/forward button clicks or any other event that triggers popstate.
+ * @private
+ */
+ Reveal.prototype._handleState = function(e){
+ if(window.location.hash === ( '#' + this.id) && !this.isActive){ this.open(); }
+ else{ this.close(); }
+ };
+ /**
* Sets the position of the modal before opening
* @param {Function} cb - a callback function to execute when positioning is complete.
* @private
*/
Reveal.prototype._setPosition = function(cb){
@@ -248,10 +268,20 @@
* @function
* @fires Reveal#closeme
* @fires Reveal#open
*/
Reveal.prototype.open = function(){
+ if(this.options.deepLink){
+ var hash = '#' + this.id;
+
+ if(window.history.pushState){
+ window.history.pushState(null, null, hash);
+ }else{
+ window.location.hash = hash;
+ }
+ }
+
var _this = this;
this.isActive = true;
//make element invisible, but remove display: none so we can get size and positioning
this.$element
.css({'visibility': 'hidden'})
@@ -305,11 +335,10 @@
$('body').addClass('is-reveal-open')
.attr({'aria-hidden': (this.options.overlay || this.options.fullScreen) ? true : false});
setTimeout(function(){
_this._extraHandlers();
- // Foundation.reflow();
}, 0);
};
/**
* Adds extra event handlers for the body and window if necessary.
@@ -319,12 +348,12 @@
var _this = this;
this.focusableElements = Foundation.Keyboard.findFocusable(this.$element);
if(!this.options.overlay && this.options.closeOnClick && !this.options.fullScreen){
$('body').on('click.zf.reveal', function(e){
- // if()
- _this.close();
+ if(e.target === _this.$element[0] || $.contains(_this.$element[0], e.target)){ return; }
+ _this.close();
});
}
if(this.options.closeOnEsc){
$(window).on('keydown.zf.reveal', function(e){
Foundation.Keyboard.handleKey(e, 'Reveal', {
@@ -390,56 +419,62 @@
var _this = this;
if(this.options.animationOut){
Foundation.Motion.animateOut(this.$element, this.options.animationOut, function(){
if(_this.options.overlay){
- Foundation.Motion.animateOut(_this.$overlay, 'fade-out', function(){
- });
- }
+ Foundation.Motion.animateOut(_this.$overlay, 'fade-out', finishUp);
+ }else{ finishUp(); }
});
}else{
this.$element.hide(_this.options.hideDelay, function(){
if(_this.options.overlay){
- _this.$overlay.hide(0, function(){
- });
- }
+ _this.$overlay.hide(0, finishUp);
+ }else{ finishUp(); }
});
}
//conditionals to remove extra event listeners added on open
if(this.options.closeOnEsc){
$(window).off('keydown.zf.reveal');
}
if(!this.options.overlay && this.options.closeOnClick){
$('body').off('click.zf.reveal');
}
this.$element.off('keydown.zf.reveal');
-
- //if the modal changed size, reset it
- if(this.changedSize){
- this.$element.css({
- 'height': '',
- 'width': ''
- });
+ function finishUp(){
+ //if the modal changed size, reset it
+ if(_this.changedSize){
+ _this.$element.css({
+ 'height': '',
+ 'width': ''
+ });
+ }
+ $('body').removeClass('is-reveal-open').attr({'aria-hidden': false, 'tabindex': ''});
+ _this.$element.attr({'aria-hidden': true})
+ /**
+ * Fires when the modal is done closing.
+ * @event Reveal#closed
+ */
+ .trigger('closed.zf.reveal');
}
- $('body').removeClass('is-reveal-open').attr({'aria-hidden': false, 'tabindex': ''});
/**
* Resets the modal content
* This prevents a running video to keep going in the background
*/
if(this.options.resetOnClose) {
this.$element.html(this.$element.html());
}
this.isActive = false;
- this.$element.attr({'aria-hidden': true})
- /**
- * Fires when the modal is done closing.
- * @event Reveal#closed
- */
- .trigger('closed.zf.reveal');
+ if(_this.options.deepLink){
+ if(window.history.replaceState){
+ window.history.replaceState("", document.title, window.location.pathname);
+ }else{
+ window.location.hash = '';
+ }
+ }
};
/**
* Toggles the open/closed state of a modal.
* @function
*/
@@ -457,11 +492,12 @@
*/
Reveal.prototype.destroy = function() {
if(this.options.overlay){
this.$overlay.hide().off().remove();
}
- this.$element.hide();
- this.$anchor.off();
+ this.$element.hide().off();
+ this.$anchor.off('.zf');
+ $(window).off('.zf.reveal:' + this.id);
Foundation.unregisterPlugin(this);
};
Foundation.plugin(Reveal, 'Reveal');