github.com/bitcubate/cryptojournal@v1.2.5-0.20171102134152-f578b3d788ab/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          var link = this;
    21  
    22          // Confirm action before delete
    23          if (link.getAttribute('method') == 'delete') {
    24              if (!confirm('Are you sure you want to delete this item, this action cannot be undone?')) {
    25                  e.preventDefault();
    26                  return false;
    27              }
    28          }
    29  
    30          // Ignore disabled links
    31          if (DOM.HasClass(link, 'disabled')) {
    32              e.preventDefault();
    33              return false;
    34          }
    35  
    36          // Get authenticity token from head of page
    37          var token = authenticityToken();
    38  
    39          // Perform a post to the specified url (href of link)
    40          var url = link.getAttribute('href');
    41          var data = "authenticity_token=" + token;
    42  
    43          DOM.Post(url, data, function(request) {
    44              if (DOM.HasClass(link, 'vote')) {
    45                  // If a vote, up the points on the page 
    46                  var pointsContainer = link.parentNode.querySelectorAll('.points')[0]
    47                  if (pointsContainer !== undefined) {
    48                      console.log(pointsContainer)
    49                      var points = parseInt(pointsContainer.innerText);
    50                      var newPoints = points + 1;
    51                      if (link.getAttribute('href').indexOf('upvote') == -1) {
    52                          newPoints = points - 1;
    53                      }
    54                      pointsContainer.innerText = newPoints;
    55                  }
    56              } else {
    57                  // Use the response url to redirect 
    58                  window.location = request.responseURL;
    59              }
    60  
    61          }, function(request) {
    62              // Respond to error 
    63              console.log("error", request);
    64          });
    65  
    66          e.preventDefault();
    67          return false;
    68      });
    69  
    70      DOM.On('a[method="back"]', 'click', function(e) {
    71          history.back(); // go back one step in history
    72          e.preventDefault();
    73          return false;
    74      });
    75  
    76  }
    77  
    78  
    79  // Insert an input into every form with js to include the csrf token.
    80  // this saves us having to insert tokens into every form.
    81  function ActivateForms() {
    82      // Get authenticity token from head of page
    83      var token = authenticityToken();
    84  
    85      DOM.Each('form', function(f) {
    86  
    87          // Create an input element 
    88          var csrf = document.createElement("input");
    89          csrf.setAttribute("name", "authenticity_token");
    90          csrf.setAttribute("value", token);
    91          csrf.setAttribute("type", "hidden");
    92  
    93          //Append the input
    94          f.appendChild(csrf);
    95      });
    96  }
    97  
    98  // Submit forms of class .filter-form when filter fields change
    99  function ActivateFilterFields() {
   100      DOM.On('.filter-form .field select, .filter-form .field input', 'change', function(e) {
   101          this.form.submit();
   102      });
   103  }
   104  
   105  // Show/Hide elements with selector in attribute href - do this with a hidden class name
   106  function ActivateShowlinks() {
   107      DOM.On('.show', 'click', function(e) {
   108          e.preventDefault();
   109          var selector = this.getAttribute('data-show');
   110          if (selector == "") {
   111              selector = this.getAttribute('href')
   112          }
   113  
   114          DOM.Each(selector, function(el, i) {
   115              if (DOM.HasClass(el, 'hidden')) {
   116                  DOM.RemoveClass(el, 'hidden')
   117              } else {
   118                  DOM.AddClass(el, 'hidden')
   119              }
   120          });
   121  
   122          return false;
   123      });
   124  }
   125  
   126  function authenticityToken() {
   127      // Collect the authenticity token from meta tags in header
   128      var meta = DOM.First("meta[name='authenticity_token']")
   129      if (meta === undefined) {
   130          e.preventDefault();
   131          return ""
   132      }
   133      return meta.getAttribute('content');
   134  }