|
| 1 | +/*global webshim:true*/ |
| 2 | + |
| 3 | +'use strict'; |
| 4 | + |
| 5 | +var enableCsrf = require('./lib/enable-csrf.js'); |
| 6 | + |
| 7 | +require('drmonty-garlicjs'); |
| 8 | +require('webshim'); |
| 9 | + |
| 10 | +enableCsrf($); |
| 11 | + |
| 12 | +// Specifically so IE will support the HTML5 form attribute on <input> elements |
| 13 | +webshim.setOptions('basePath', '/static/vendor/shims/'); |
| 14 | +webshim.polyfill('forms'); |
| 15 | + |
| 16 | +function parsleyForm(element) { |
| 17 | + return $(element).parsley({ |
| 18 | + successClass: 'has-success', |
| 19 | + errorClass: 'has-error', |
| 20 | + trigger: 'change keyup focusout', |
| 21 | + classHandler: function (field) { |
| 22 | + if (field.$element.attr('type') === 'radio') { |
| 23 | + return $('input[type=radio][name=' + field.$element.attr('name') + ']') |
| 24 | + .parents('.radio'); |
| 25 | + } |
| 26 | + |
| 27 | + return field.$element.parents('.form-group'); |
| 28 | + }, |
| 29 | + errorsContainer: function (field) { |
| 30 | + var $field = field.$element; |
| 31 | + |
| 32 | + if (field.$element.attr('type') === 'radio') { |
| 33 | + $field = $('input[type=radio][name=' + field.$element.attr('name') + |
| 34 | + ']:last').parent().parent(); |
| 35 | + |
| 36 | + return $('<span></span>').insertAfter($field); |
| 37 | + } |
| 38 | + |
| 39 | + if (field.$element.parent('.input-group')) { |
| 40 | + $field = field.$element.parent('.input-group'); |
| 41 | + } |
| 42 | + |
| 43 | + return $('<span></span>').insertAfter($field); |
| 44 | + }, |
| 45 | + errorsWrapper: '<span class="help-block"></span>', |
| 46 | + errorTemplate: '<div></div>' |
| 47 | + }); |
| 48 | +} |
| 49 | + |
| 50 | +function showModal(modalId) { |
| 51 | + return function (e) { |
| 52 | + // Allow for middle-clicking, control-clicking, and command-clicking |
| 53 | + if (e.isDefaultPrevented() || e.metaKey || e.ctrlKey) { |
| 54 | + return; |
| 55 | + } |
| 56 | + |
| 57 | + e.preventDefault(); |
| 58 | + |
| 59 | + $(modalId).modal({remote: false}); |
| 60 | + }; |
| 61 | +} |
| 62 | + |
| 63 | +function storeRedirect(){ |
| 64 | + var location = window.location.href; |
| 65 | + |
| 66 | + // Make a POST, but not asynchronously! |
| 67 | + $.ajax({ type: 'POST', |
| 68 | + url: '/account/storeredirect/', |
| 69 | + data: {next_url: location}, |
| 70 | + success: function(){}, |
| 71 | + async:false }); |
| 72 | +} |
| 73 | + |
| 74 | +$(function () { |
| 75 | + $('[rel=persist]').garlic(); |
| 76 | + |
| 77 | + parsleyForm('form'); |
| 78 | + |
| 79 | + $('.logout-link').click(function (e) { |
| 80 | + e.preventDefault(); |
| 81 | + |
| 82 | + $.post($(this).attr('href'), function () { |
| 83 | + location.reload(); |
| 84 | + }); |
| 85 | + }); |
| 86 | + |
| 87 | + // Add these modals with JavaScript rather than data- attributes to prevent |
| 88 | + // AJAX loading of modal content by Bootstrap. |
| 89 | + $('.login-link').click(showModal('#login-modal')); |
| 90 | + $('.signup-link').click(showModal('#signup-modal')); |
| 91 | + |
| 92 | + // POST current URL to be stored as a redirect link upon login/signup |
| 93 | + $('.redirect-storage-link').click(storeRedirect); |
| 94 | +}); |
0 commit comments