github.com/fragmenta/fragmenta-cms@v1.5.5/src/app/assets/scripts/app.js (about) 1 DOM.Ready(function() { 2 3 // Perform AJAX post on click on method=post|delete anchors 4 ActivateMethodLinks(); 5 6 // Show/Hide elements with selector in attribute data-show 7 ActivateShowlinks(); 8 9 // Submit forms of class .filter-form when filter fields change 10 ActivateFilterFields(); 11 12 // Insert CSRF tokens into forms 13 ActivateForms(); 14 15 }); 16 17 // Perform AJAX post on click on method=post|delete anchors 18 function ActivateMethodLinks() { 19 DOM.On('a[method="post"], a[method="delete"]', 'click', function(e) { 20 // Confirm action before delete 21 if (this.getAttribute('method') == 'delete') { 22 if (!confirm('Are you sure you want to delete this item, this action cannot be undone?')) { 23 return false; 24 } 25 } 26 27 // Get authenticity token from head of page 28 var token = authenticityToken(); 29 30 // Perform a post to the specified url (href of link) 31 var url = this.getAttribute('href'); 32 var data = "authenticity_token=" + token 33 34 DOM.Post(url, data, function(request) { 35 // Use the response url to redirect 36 window.location = request.responseURL 37 }, function(request) { 38 console.log("error with request:", request) 39 // Use the response url to redirect even if not found 40 window.location = request.responseURL 41 }); 42 43 e.preventDefault(); 44 return false; 45 }); 46 47 48 DOM.On('a[method="back"]', 'click', function(e) { 49 history.back(); // go back one step in history 50 e.preventDefault(); 51 return false; 52 }); 53 54 } 55 56 57 // Insert an input into every form with js to include the csrf token. 58 // this saves us having to insert tokens into every form. 59 function ActivateForms() { 60 // Get authenticity token from head of page 61 var token = authenticityToken(); 62 63 DOM.Each('form', function(f) { 64 65 // Create an input element 66 var csrf = document.createElement("input"); 67 csrf.setAttribute("name", "authenticity_token"); 68 csrf.setAttribute("value", token); 69 csrf.setAttribute("type", "hidden"); 70 71 //Append the input 72 f.appendChild(csrf); 73 }); 74 } 75 76 // Submit forms of class .filter-form when filter fields change 77 function ActivateFilterFields() { 78 DOM.On('.filter-form .field select, .filter-form .field input', 'change', function(e) { 79 this.form.submit(); 80 }); 81 } 82 83 // Show/Hide elements with selector in attribute href - do this with a hidden class name 84 function ActivateShowlinks() { 85 DOM.On('.show', 'click', function(e) { 86 var selector = this.getAttribute('href'); 87 DOM.Each(selector, function(el, i) { 88 if (el.className != 'hidden') { 89 el.className = 'hidden'; 90 } else { 91 el.className = el.className.replace(/hidden/gi, ''); 92 } 93 }); 94 95 return false; 96 }); 97 } 98 99 function authenticityToken() { 100 // Collect the authenticity token from meta tags in header 101 var meta = DOM.First("meta[name='authenticity_token']") 102 if (meta === undefined) { 103 e.preventDefault(); 104 return "" 105 } 106 return meta.getAttribute('content'); 107 }