diff options
Diffstat (limited to 'assets/scripts')
-rw-r--r-- | assets/scripts/feedback-form.js | 84 | ||||
-rw-r--r-- | assets/scripts/jquery.cookie.js | 117 |
2 files changed, 201 insertions, 0 deletions
diff --git a/assets/scripts/feedback-form.js b/assets/scripts/feedback-form.js new file mode 100644 index 000000000..4b5d1559f --- /dev/null +++ b/assets/scripts/feedback-form.js @@ -0,0 +1,84 @@ +$( document ).ready(function( $ ) { + + window.setTimeout(function(){ + if ( $('#feedback_form').length ) { + lastAnswered = $.cookie('survey'); + current = $('input[name="question_no"]').attr('value'); + if ( lastAnswered == null || lastAnswered < current ) { + $('#feedback_form').show('slow'); + } + } + }, 2000); + + // Set the current url to a hidden form value + $("#feedback_form #url").val(window.location.pathname); + + // Hide the form - don't show for 30 days + $("#hide_survey").click(function(event){ + $('#feedback_form').hide('slow'); + set_survey_cookie(); + event.preventDefault(); + }); + + function set_survey_cookie(){ + // Set the cookie to show that the survey has been answered + $.cookie('survey', current, { expires: 30, path: '/' }); + } + + // variable to hold request + var request; + // bind to the submit event of our form + $("#feedback_form").submit(function(event){ + // abort any pending request + if (request) { + request.abort(); + } + // setup some local variables + var $form = $(this); + // let's select and cache all the fields + var $inputs = $form.find("input, select, button, textarea"); + // serialize the data in the form + var serializedData = $form.serialize(); + + // let's disable the inputs for the duration of the ajax request + // Note: we disable elements AFTER the form data has been serialized. + // Disabled form elements will not be serialized. + $inputs.prop("disabled", true); + $('#result').text('Sending data...'); + + // fire off the request to /form.php + request = $.ajax({ + url: "https://script.google.com/macros/s/AKfycbyC2oAdyaAY3StdEKX5zKOln9vt-HX1Eq05nqPmAgUPcpkzqY2K/exec", + type: "post", + data: serializedData + }); + + // callback handler that will be called on success + request.done(function (response, textStatus, jqXHR){ + // log a message to the console + $('#result').html('Thanks for helping us improve!'); + $('#feedback_form #form_elements').hide('slow'); + $('#feedback_form input[type=submit]').hide('slow'); + }); + + // callback handler that will be called on failure + request.fail(function (jqXHR, textStatus, errorThrown){ + // log the error to the console + console.error( + "The following error occured: "+ + textStatus, errorThrown + ); + }); + + // callback handler that will be called regardless + // if the request failed or succeeded + request.always(function () { + // reenable the inputs + $inputs.prop("disabled", false); + set_survey_cookie(); + }); + + // prevent default posting of form + event.preventDefault(); + }); +}); diff --git a/assets/scripts/jquery.cookie.js b/assets/scripts/jquery.cookie.js new file mode 100644 index 000000000..c7f3a59b5 --- /dev/null +++ b/assets/scripts/jquery.cookie.js @@ -0,0 +1,117 @@ +/*! + * jQuery Cookie Plugin v1.4.1 + * https://github.com/carhartl/jquery-cookie + * + * Copyright 2013 Klaus Hartl + * Released under the MIT license + */ +(function (factory) { + if (typeof define === 'function' && define.amd) { + // AMD + define(['jquery'], factory); + } else if (typeof exports === 'object') { + // CommonJS + factory(require('jquery')); + } else { + // Browser globals + factory(jQuery); + } +}(function ($) { + + var pluses = /\+/g; + + function encode(s) { + return config.raw ? s : encodeURIComponent(s); + } + + function decode(s) { + return config.raw ? s : decodeURIComponent(s); + } + + function stringifyCookieValue(value) { + return encode(config.json ? JSON.stringify(value) : String(value)); + } + + function parseCookieValue(s) { + if (s.indexOf('"') === 0) { + // This is a quoted cookie as according to RFC2068, unescape... + s = s.slice(1, -1).replace(/\\"/g, '"').replace(/\\\\/g, '\\'); + } + + try { + // Replace server-side written pluses with spaces. + // If we can't decode the cookie, ignore it, it's unusable. + // If we can't parse the cookie, ignore it, it's unusable. + s = decodeURIComponent(s.replace(pluses, ' ')); + return config.json ? JSON.parse(s) : s; + } catch(e) {} + } + + function read(s, converter) { + var value = config.raw ? s : parseCookieValue(s); + return $.isFunction(converter) ? converter(value) : value; + } + + var config = $.cookie = function (key, value, options) { + + // Write + + if (value !== undefined && !$.isFunction(value)) { + options = $.extend({}, config.defaults, options); + + if (typeof options.expires === 'number') { + var days = options.expires, t = options.expires = new Date(); + t.setTime(+t + days * 864e+5); + } + + return (document.cookie = [ + encode(key), '=', stringifyCookieValue(value), + options.expires ? '; expires=' + options.expires.toUTCString() : '', // use expires attribute, max-age is not supported by IE + options.path ? '; path=' + options.path : '', + options.domain ? '; domain=' + options.domain : '', + options.secure ? '; secure' : '' + ].join('')); + } + + // Read + + var result = key ? undefined : {}; + + // To prevent the for loop in the first place assign an empty array + // in case there are no cookies at all. Also prevents odd result when + // calling $.cookie(). + var cookies = document.cookie ? document.cookie.split('; ') : []; + + for (var i = 0, l = cookies.length; i < l; i++) { + var parts = cookies[i].split('='); + var name = decode(parts.shift()); + var cookie = parts.join('='); + + if (key && key === name) { + // If second argument (value) is a function it's a converter... + result = read(cookie, value); + break; + } + + // Prevent storing a cookie that we couldn't decode. + if (!key && (cookie = read(cookie)) !== undefined) { + result[name] = cookie; + } + } + + return result; + }; + + config.defaults = {}; + + $.removeCookie = function (key, options) { + if ($.cookie(key) === undefined) { + return false; + } + + // Must not alter options, thus extending a fresh object... + $.cookie(key, '', $.extend({}, options, { expires: -1 })); + return !$.cookie(key); + }; + +})); |