Skip to content

Instantly share code, notes, and snippets.

@wpmudev-sls
Created June 8, 2018 07:11
Show Gist options
  • Save wpmudev-sls/edaa55bb05def2a4b18b4cadba3d608e to your computer and use it in GitHub Desktop.
Save wpmudev-sls/edaa55bb05def2a4b18b4cadba3d608e to your computer and use it in GitHub Desktop.
Hustle Exit Intent conflict Fix with QuForm
// window.Quform.prototype.setupInsideLabels = function(){}
(function ($) {
$.InFieldLabels = function (label, field, options) {
// To avoid scope issues, use 'base' instead of 'this'
// to reference this class from internal events and functions.
var base = this;
// Access to jQuery and DOM versions of each element
base.$label = $(label);
base.label = label;
base.$field = $(field);
base.field = field;
base.$label.data("InFieldLabels", base);
base.showing = true;
base.init = function () {
var initialSet;
// Merge supplied options with default options
base.options = $.extend({}, $.InFieldLabels.defaultOptions, options);
// Add provided class name to the label, if set
if ( base.options.className ) {
base.$label.addClass(base.options.className);
}
// Check if the field is already filled in
// add a short delay to handle autocomplete
setTimeout(function() {
if (base.$field.val() !== "") {
base.$label.css('display', 'none');
base.showing = false;
} else {
base.$label.css('display', 'block');
base.showing = true;
}
}, 200);
base.$field.focus(function () {
base.fadeOnFocus();
}).blur(function () {
base.checkForEmpty(true);
}).bind('keydown.infieldlabel', function (e) {
// Use of a namespace (.infieldlabel) allows us to
// unbind just this method later
base.hideOnChange(e);
}).bind('paste', function () {
// Since you can not paste an empty string we can assume
// that the field is not empty and the label can be cleared.
base.setOpacity(0.0);
}).change(function () {
base.checkForEmpty();
}).bind('onPropertyChange', function () {
base.checkForEmpty();
}).bind('keyup.infieldlabel', function () {
base.checkForEmpty();
});
if ( base.options.pollDuration > 0 ) {
initialSet = setInterval( function () {
if (base.$field.val() !== "") {
base.$label.hide();
base.showing = false;
clearInterval( initialSet );
}
}, base.options.pollDuration );
}
};
// If the label is currently showing
// then fade it down to the amount
// specified in the settings
base.fadeOnFocus = function () {
if (base.showing) {
base.setOpacity(base.options.fadeOpacity);
}
};
base.setOpacity = function (opacity) {
base.$label.stop().animate({ opacity: opacity }, base.options.fadeDuration, function () {
if (opacity === 0.0) {
base.$label.hide();
}
});
base.showing = (opacity > 0.0);
};
// Checks for empty as a fail safe
// set blur to true when passing from
// the blur event
base.checkForEmpty = function (blur) {
if (base.$field.val() === "") {
base.prepForShow();
base.setOpacity(blur ? 1.0 : base.options.fadeOpacity);
} else {
base.setOpacity(0.0);
}
};
base.prepForShow = function () {
if (!base.showing) {
// Prepare for a animate in...
base.$label.css({opacity: 0.0}).show();
// Reattach the keydown event
base.$field.bind('keydown.infieldlabel', function (e) {
base.hideOnChange(e);
});
}
};
base.hideOnChange = function (e) {
if (
(e.keyCode === 16) || // Skip Shift
(e.keyCode === 9) // Skip Tab
) {
return;
}
if (base.showing) {
base.$label.hide();
base.showing = false;
}
// Remove keydown event to save on CPU processing
base.$field.unbind('keydown.infieldlabel');
};
// Run the initialization method
base.init();
};
$.InFieldLabels.defaultOptions = {
fadeOpacity: 0.5, // Once a field has focus, how transparent should the label be
fadeDuration: 300, // How long should it take to animate from 1.0 opacity to the fadeOpacity
pollDuration: 0, // If set to a number greater than zero, this will poll until content is detected in a field
enabledInputTypes: [ "text", "search", "tel", "url", "email", "password", "number", "textarea" ],
className: false // Class assigned to enhanced labels
};
$.fn.inFieldLabels = function (options) {
var allowed_types = options && options.enabledInputTypes || $.InFieldLabels.defaultOptions.enabledInputTypes;
return this.each(function () {
// Find input or textarea based on for= attribute
// The for attribute on the label must contain the ID
// of the input or textarea element
var for_attr = $(this).attr('for'), field, restrict_type;
if (!for_attr) {
return; // Nothing to attach, since the for field wasn't used
}
// Find the referenced input or textarea element
field = document.getElementById( for_attr );
if ( !field ) {
return; // No element found
}
// Restrict input type
restrict_type = $.inArray( field.type, allowed_types );
if ( restrict_type === -1 && field.nodeName !== "TEXTAREA" ) {
return; // Again, nothing to attach
}
// Only create object for matched input types and textarea
(new $.InFieldLabels(this, field, options));
});
};
}(jQuery));
<?php
/**
* Plugin Name: Hustle Exit Intent conflict Fix with QuForm
* Plugin URI: https://premium.wpmudev.org/
* Description: Hustle Exit Intent conflict Fix with QuForm
* Author: Ariful Islam @ WPMUDEV
* Author URI: https://premium.wpmudev.org/profile/itsarifulislam
* License: GPLv2 or later
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
function hustle_conflict_fix_quform() {
if ( ! class_exists('Hustle_Module_Front') || ! shortcode_exists('quform') ) return;
wp_enqueue_script( 'hustle-exit-intent-fix-with-quform', plugin_dir_url( __FILE__ ) . 'hustle-fix-quform.js', array('quform'), null, true );
}
add_action( 'plugins_loaded', 'hustle_conflict_fix_quform' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment