Venciity

Countries and towns

Nov 26th, 2014
296
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. $(function () {
  2.     var headers = {
  3.         'X-Parse-Application-Id': '7AD6SQzDTUSWE1PprfO2Wxo6znacDB83EAV650J8',
  4.         'X-Parse-REST-API-Key': 'zLijKqgocsAxC9dx9BhioslXEJWGB1T2Y5aXWkdL'
  5.     },
  6.     countries = [],
  7.     towns = [],
  8.  
  9.     errorMessage = function (message) {
  10.         noty({
  11.             text: message,
  12.             type: 'error',
  13.             layout: 'center',
  14.             timeout: 5000
  15.         });
  16.     },
  17.    
  18.     successMessage = function (message) {
  19.         noty({
  20.             text: message,
  21.             type: 'success',
  22.             layout: 'center',
  23.             timeout: 2000
  24.         });
  25.     }
  26.  
  27.     loadCountries = function () {
  28.         $.ajax({
  29.             url: 'https://api.parse.com/1/classes/Country',
  30.             type: 'GET',
  31.             headers: headers,
  32.             success: function (data) {
  33.                 var countriesList = $('#countries-list');
  34.                 countries = data.results;
  35.                 countriesList.html('');
  36.  
  37.                 $.each(countries, function (index, country) {
  38.                     $('<option/>').addClass('country-item').html(country.name).appendTo(countriesList);
  39.                 })
  40.  
  41.                 loadTowns(getSelectedCountry()[0].objectId);
  42.             },
  43.             error: function () {
  44.                 errorMessage('Error occurred when loading countries!');
  45.             }
  46.         });
  47.     },
  48.  
  49.     loadTowns = function (countryId) {
  50.         $.ajax({
  51.             url: 'https://api.parse.com/1/classes/Town',
  52.             type: 'GET',
  53.             headers: headers,
  54.             success: function (data) {
  55.                 towns = data.results;
  56.                 var selectedTowns = towns.filter(function (country) {
  57.                     return country['country'].objectId == countryId;
  58.                 }),
  59.                 townsList = $('#towns-list');
  60.  
  61.                 townsList.html('');
  62.                 $.each(selectedTowns, function (index, town) {
  63.                     $('<option/>').addClass('town-item').html(town.name).appendTo(townsList);
  64.                 });
  65.             },
  66.             error: function () {
  67.                 errorMessage('Error occurred when loading towns!');
  68.             }
  69.         });
  70.     },
  71.  
  72.     getSelectedCountry = function () {
  73.         var countryName = $('#countries-list').children(':selected').val();
  74.         return countries.filter(function (country) {
  75.             return countryName == country.name;
  76.         });
  77.     },
  78.  
  79.     getSelectedTowns = function () {
  80.         var townsOptions = $('#towns-list').children(':selected'),
  81.             selectedTowns = [];
  82.  
  83.         $(towns).each(function (index, town) {
  84.             $.each(townsOptions, function (index, townOption) {
  85.                 if (town.name == townOption.value) {
  86.                     selectedTowns.push(town);
  87.                 }
  88.             });
  89.         });
  90.         return selectedTowns;
  91.     };
  92.  
  93.     $('#countries-list').change(function () {
  94.         loadTowns(getSelectedCountry()[0].objectId);
  95.     });
  96.  
  97.     $('#country-add-btn').click(function () {
  98.         var input = $('.country-input')[0];
  99.         if (input) {
  100.             if (input.value.length < 3) {
  101.                 return false;
  102.             }
  103.  
  104.             $.ajax({
  105.                 url: 'https://api.parse.com/1/classes/Country',
  106.                 type: 'POST',
  107.                 contentType: 'applications/json',
  108.                 data: JSON.stringify({ 'name': encodeURI(input.value) }),
  109.                 headers: headers,
  110.                 success: function () {
  111.                     successMessage('New country successfully added.');
  112.                 },
  113.                 error: function () {
  114.                     errorMessage('Error occurred while adding new country!');
  115.                 }
  116.             });
  117.             input.remove();
  118.             loadCountries();
  119.         } else {
  120.             $('<input type="text" />').addClass('country-input').prependTo($(this).parent());
  121.         }
  122.     });
  123.  
  124.     $('#town-add-btn').click(function () {
  125.         var input = $('.town-input')[0],
  126.             selectedCountryId = '';
  127.  
  128.         if (input) {
  129.             if (input.value.length < 3) {
  130.                 return false;
  131.             }
  132.  
  133.             selectedCountryId = getSelectedCountry()[0].objectId;
  134.             $.ajax({
  135.                 url: 'https://api.parse.com/1/classes/Town',
  136.                 type: 'POST',
  137.                 contentType: 'applications/json',
  138.                 data: JSON.stringify({
  139.                     'name': encodeURI(input.value),
  140.                     'country': {
  141.                         "__type": "Pointer",
  142.                         "className": "Country",
  143.                         "objectId": selectedCountryId
  144.                     }
  145.                 }),
  146.                 headers: headers,
  147.                 success: function () {
  148.                     successMessage('New town successfully added.');
  149.                 },
  150.                 error: function () {
  151.                     errorMessage('Error occurred while adding new town!');
  152.                 }
  153.             });
  154.             input.remove();
  155.             loadTowns(selectedCountryId);
  156.         } else {
  157.             $('<input type="text" />').addClass('town-input').prependTo($(this).parent());
  158.         }
  159.     });
  160.  
  161.     $('#country-del-btn').click(function () {
  162.         var countryId = getSelectedCountry()[0].objectId;
  163.         $.ajax({
  164.             url: 'https://api.parse.com/1/classes/Country/' + countryId,
  165.             type: 'DELETE',
  166.             headers: headers,
  167.             success: function () {
  168.                 successMessage('Country successfully deleted.');
  169.                 loadCountries();
  170.             },
  171.             error: function () {
  172.                 errorMessage('Error occurred while trying to delete country!');
  173.             }
  174.         });
  175.     });
  176.  
  177.     $('#town-del-btn').click(function () {
  178.         var selectedTowns = getSelectedTowns();
  179.         if (selectedTowns.length > 0) {
  180.             $(selectedTowns).each(function (index, town) {
  181.                 $.ajax({
  182.                     url: 'https://api.parse.com/1/classes/Town/' + town.objectId,
  183.                     headers: headers,
  184.                     type: 'DELETE',
  185.                     success: function () {
  186.                         successMessage('Selected towns successfully deleted.');
  187.                         loadTowns(getSelectedCountry()[0].objectId);
  188.                     },
  189.                     error: function () {
  190.                         errorMessage('Error occurred while trying to delete selected towns!');
  191.                     }
  192.                 });
  193.             });
  194.         }
  195.     });
  196.  
  197.     $('#country-edit-btn').click(function () {
  198.         var selectedCountry = getSelectedCountry(),
  199.             input = $('.country-edit-input')[0];
  200.  
  201.         if (input) {
  202.             if (input.value.length < 3) {
  203.                 return false;
  204.             }
  205.  
  206.             $.ajax({
  207.                 url: 'https://api.parse.com/1/classes/Country/' + selectedCountry[0].objectId,
  208.                 type: 'PUT',
  209.                 contentType: 'applications/json',
  210.                 data: JSON.stringify({
  211.                     'name': encodeURI(input.value)
  212.                 }),
  213.                 headers: headers,
  214.                 success: function () {
  215.                     successMessage('Country edited successfully.');
  216.                     loadCountries();
  217.                 },
  218.                 error: function () {
  219.                     errorMessage('Error occured while editing country!');
  220.                 }
  221.             });
  222.  
  223.             input.remove();
  224.         } else {
  225.             $('<input type="text" />').addClass('country-edit-input').val(selectedCountry[0].name).appendTo($(this).parent());
  226.         }
  227.     });
  228.  
  229.     $('#town-edit-btn').click(function () {
  230.         var selectedTowns = getSelectedTowns(),
  231.             input = $('.town-edit-input')[0],
  232.             selectedCountryId = '';
  233.  
  234.         if (selectedTowns.length !== 1) {
  235.             errorMessage('Please select just one town to edit!');
  236.             return false;
  237.         }
  238.  
  239.         if (input) {
  240.             if (input.value.length < 3) {
  241.                 return false;
  242.             }
  243.  
  244.             selectedCountryId = getSelectedCountry()[0].objectId;
  245.             $.ajax({
  246.                 url: 'https://api.parse.com/1/classes/Town/' + selectedTowns[0].objectId,
  247.                 type: 'PUT',
  248.                 contentType: 'applications/json',
  249.                 data: JSON.stringify({
  250.                     'name': encodeURI(input.value)
  251.                 }),
  252.                 headers: headers,
  253.                 success: function () {
  254.                     successMessage('Town edited successfully.');
  255.                     loadTowns(selectedCountryId);
  256.                 },
  257.                 error: function () {
  258.                     errorMessage('Error occured while editing town!');
  259.                 }
  260.             });
  261.  
  262.             input.remove();
  263.         } else {
  264.             $('<input type="text" />').addClass('town-edit-input').val(selectedTowns[0].name).appendTo($(this).parent());
  265.         }
  266.     });
  267.  
  268.     loadCountries();
  269. });
Advertisement
Add Comment
Please, Sign In to add comment