/*! * CallPowerForm.js * Connects embedded action form to CallPower campaign * Requires jQuery >= 1.7.0 * proxy (1.4), deferred (1.5), on (1.7) * * Displays call script in overlay or by replacing form * Override functions onSuccess or onError in CallPowerOptions * Instantiate with form selector to connect callbacks * (c) Spacedog.xyz 2015, license AGPLv3 */ var CallPowerForm = function (formSelector, $) { // instance variables this.form = $(formSelector); this.locationField = $('#location_id'); this.phoneField = $('#phone_id'); this.scriptDisplay = 'overlay'; // allow options override for (var option in window.CallPowerOptions || []) { this[option] = CallPowerOptions[option]; } this.form.on("submit", $.proxy(this.makeCall, this)); // include custom css if(this.customCSS !== undefined) { $('head').append(''); } }; CallPowerForm.prototype = function($) { // prototype variables var createCallURL = 'https://call.eff.org/call/create'; var campaignId = "9"; var getCountry = function() { return "US"; }; var cleanUSZipcode = function() { if (this.locationField.length === 0) { return undefined; } var isValid = /(\d{5}([\-]\d{4})?)/.test(this.locationField.val()); return isValid ? this.locationField.val() : false; }; var cleanUSPhone = function() { if (this.phoneField.length === 0) { return undefined; } var num = this.phoneField.val(); // remove whitespace, parens num = num.replace(/\s/g, '').replace(/\(/g, '').replace(/\)/g, ''); // plus, dashes num = num.replace("+", "").replace(/\-/g, ''); // leading 1 if (num.charAt(0) == "1") num = num.substr(1); var isValid = (num.length == 10); // ensure just 10 digits remain return isValid ? num : false; }; // default to US validators var cleanPhone = cleanUSPhone; var cleanLocation = cleanUSZipcode; var onSuccess = function(response) { if (response.campaign === 'archived') { return onError(this.form, 'This campaign is no longer active.'); } if (response.campaign !== 'live') { return onError(this.form, 'This campaign is not active.'); } if (response.call !== 'queued') { return onError(this.form, 'Could not start call.'); } if (response.script === undefined) { return false; } if (this.scriptDisplay === 'overlay') { // display simple overlay with script content var scriptOverlay = $('
'); $('body').append(scriptOverlay); scriptOverlay.overlay(); scriptOverlay.trigger('show'); } if (this.scriptDisplay === 'replace') { // replace form with script content this.form.html(response.script); } // run custom js function if(this.customJS !== undefined) { eval(this.customJS); } return true; }; var onError = function(element, message) { if (element !== undefined) { element.addClass('has-error'); } else { console.error(message); } return false; }; var makeCall = function(event, options) { if (event !== undefined) { event.preventDefault(); } // stop default submit event options = options || {}; if (options.call_started) { return true; } if (this.locationField.length && !this.location()) { return this.onError(this.locationField, 'Invalid location'); } if (this.phoneField.length && !this.phone()) { return this.onError(this.phoneField, 'Invalid phone number'); } $.ajax(createCallURL, { method: 'GET', data: { campaignId: campaignId, userLocation: this.location(), userPhone: this.phone(), userCountry: this.country() }, success: $.proxy(this.onSuccess, this), error: $.proxy(this.onError, this, this.form, 'Please fill out the form completely') }).then(function() { // run previous default event without this callback $(event.currentTarget).trigger(event.type, { 'call_started': true }); }).fail(this.onError); }; // public method interface return { country: getCountry, location: cleanLocation, phone: cleanPhone, onError: onError, onSuccess: onSuccess, makeCall: makeCall }; } (jQuery); var main = function() { callPowerForm = new CallPowerForm('#call_form', jQuery); } // from substack/semver-compare // license MIT function versionCmp (a, b) { var pa = a.split('.'); var pb = b.split('.'); for (var i = 0; i < 3; i++) { var na = Number(pa[i]); var nb = Number(pb[i]); if (na > nb) return 1; if (nb > na) return -1; if (!isNaN(na) && isNaN(nb)) return 1; if (isNaN(na) && !isNaN(nb)) return -1; } return 0; }; if ((typeof jQuery == 'undefined') || (versionCmp(jQuery.fn.jquery, '1.7.0') < 0)) { // load jQuery from cloudflare var scriptElement = document.createElement("script"); scriptElement.src = '//cdnjs.cloudflare.com/ajax/libs/jquery/1.11.3/jquery.min.js'; scriptElement.type = "text/javascript"; var head = document.getElementsByTagName("head")[0] || document.documentElement; head.appendChild(scriptElement); scriptElement.onload = main; } else { // use in-page jQuery jQuery(document).ready(main); }