Advertisement
simonradev

da

Feb 20th, 2018
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function initializeTable() {
  2.     $('#createLink').on('click', onCreateLinkClicked);
  3.    
  4.     function onCreateLinkClicked(event) {  
  5.         const inputCountry = $('#newCountryText');
  6.         const inputCapital = $('#newCapitalText');
  7.  
  8.         const country = inputCountry.val();
  9.         const capital = inputCapital.val();
  10.  
  11.         inputCountry.val('');
  12.         inputCapital.val('');
  13.        
  14.         const elementCountry = $('<td></td>').text(country);
  15.         const elementCapital = $('<td></td>').text(capital);
  16.  
  17.         const elementLinks = $('<td></td>');
  18.         $createATag('[Up]', onUpClicked, elementLinks);
  19.         $createATag('[Down]', onDownClicked, elementLinks);
  20.         $createATag('[Delete]', onDeleteClicked, elementLinks);
  21.  
  22.         const elementRow = $('<tr></tr>')
  23.                              .append(elementCountry)
  24.                              .append(elementCapital)
  25.                              .append(elementLinks);
  26.  
  27.         $('#countriesTable').append(elementRow);
  28.  
  29.         $fixLinks();
  30.     }
  31.  
  32.     function $createATag(text, eventHandler, appendTo) {
  33.         $('<a href="#"></a>')
  34.                         .text(text)
  35.                         .on('click', eventHandler)
  36.                         .appendTo(appendTo);
  37.     }
  38.  
  39.     function onUpClicked(event) {
  40.         const targetRow = $getParentRow(event.target);
  41.         targetRow.fadeOut(function () {
  42.             targetRow.prev().insertAfter(targetRow);
  43.             $fixLinks();
  44.             targetRow.fadeIn();
  45.         });
  46.     }
  47.  
  48.     function onDownClicked(event) {
  49.         const targetRow = $getParentRow(event.target);
  50.         targetRow.fadeOut(function () {
  51.             targetRow.next().insertBefore(targetRow);
  52.             $fixLinks();
  53.             targetRow.fadeIn();
  54.         });
  55.     }
  56.  
  57.     function onDeleteClicked(event) {
  58.         const targetRow = $getParentRow(event.target);
  59.         targetRow.fadeOut(function () {
  60.             targetRow.remove();
  61.             $fixLinks();
  62.         });
  63.     }
  64.  
  65.     function $getParentRow(element) {
  66.         return $(element)
  67.                     .parent()
  68.                     .parent();
  69.     }
  70.  
  71.     function $fixLinks() {
  72.         $('#countriesTable a').css('display', 'inline');
  73.  
  74.         $('#countriesTable tr:nth-child(3) a:contains("Up")').css('display', 'none');
  75.         $('#countriesTable tr:last-child a:contains("Down")').css('display', 'none');
  76.     }
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement