Advertisement
Guest User

Untitled

a guest
Mar 26th, 2019
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. (function($) {
  2.  
  3.   // Set objects
  4.   var $generator = $('.generator');
  5.       $complicate = $('.complicate', $generator),
  6.       $generate = $('.generate', $generator),
  7.       $passwordFinnal = $('#passwordFinnal', $generator),
  8.       $passwordPlain = $('#passwordPlain', $generator),
  9.       $copy = $('.input--wrap-icon', $generator),
  10.       $indicator = $('.generator-indicator span', $generator);
  11.  
  12.   // Show copy
  13.   var showCopy = function(val) {
  14.  
  15.     // Show
  16.     if(val) {
  17.       $copy.show();
  18.     }
  19.  
  20.     // Hide
  21.     else {
  22.       $copy.hide();
  23.     }
  24.  
  25.   }
  26.  
  27.   // Change indicator
  28.   var changeIndicator = function(password) {
  29.  
  30.     // Set strength
  31.     var strength = 0;
  32.  
  33.     // Length 5 characters or more
  34.     if(password.length >= 7) {
  35.       strength++;
  36.     }
  37.  
  38.     // Contains lowercase characters
  39.     if(password.match(/[a-z]+/)) {
  40.       strength++;
  41.     }
  42.  
  43.     // Contains digits
  44.     if(password.match(/[0-9]+/)) {
  45.       strength++;
  46.     }
  47.  
  48.     // Contains uppercase characters
  49.     if(password.match(/[A-Z]+/)) {
  50.       strength++;
  51.     }
  52.  
  53.     // Contains special characters
  54.     if(password.match(/[!@#$%^&*()_+\-=\[\]{};':"\\|,.<>\/?]+/)) {
  55.       strength++;
  56.     }
  57.  
  58.     // Set indicator width
  59.     $indicator.css('width', (strength * 20) + '%');
  60.  
  61.   }
  62.  
  63.   // Show generator
  64.   $generator.show();
  65.  
  66.   // Hide complicate button and copy
  67.   $('.complicate', $generator).add($copy).hide();
  68.  
  69.   // Generate and complicate
  70.   $generate.add($complicate).on('click', function() {
  71.  
  72.     // Save self
  73.     var $self = $(this);
  74.  
  75.     // Create ajax request
  76.     $.get('http://leximo.criticalworks.cz/projects/inste.cz/generator-hesel/?password=' + $passwordPlain.val() + '&complicate=' + $self.hasClass('complicate'), function(data, status, response){
  77.  
  78.       // Error happened
  79.       if(status == 'error') {
  80.  
  81.         // Throw errior into console
  82.         console.log(response);
  83.  
  84.       // All is fine
  85.       } else {
  86.  
  87.         // Check data
  88.         if(data) {
  89.  
  90.           // Append password
  91.           $passwordFinnal.val(data);
  92.  
  93.           // Change indicator
  94.           changeIndicator(data);
  95.  
  96.           // Show copy
  97.           showCopy(data.length > 0);
  98.  
  99.           // Keep plain passqword
  100.           if(!$self.hasClass('complicate')) {
  101.             $passwordPlain.val(data);
  102.           }
  103.  
  104.           // Show complicate button
  105.           $complicate.show();
  106.  
  107.         }
  108.  
  109.         // Data are empty
  110.         else {
  111.           console.log('retrieved password is empty')
  112.         }
  113.  
  114.       }
  115.  
  116.     });
  117.  
  118.   });
  119.  
  120.   // Show complicate button
  121.   $passwordFinnal.on('keyup change', function() {
  122.  
  123.     // Set val
  124.     var val = $(this).val();
  125.  
  126.     // Change indicator
  127.     changeIndicator(val);
  128.  
  129.     // Show copy
  130.     showCopy(val.length > 0);
  131.  
  132.     // Show
  133.     if(val.length >= 10) {
  134.       $complicate.show();
  135.     }
  136.  
  137.     // Hide
  138.     else {
  139.       $complicate.hide();
  140.     }
  141.  
  142.   });
  143.  
  144.   // Copy to clipboard
  145.   $copy.on('click', function() {
  146.  
  147.     // Create temp
  148.     var $temp = $('<input>');
  149.  
  150.     // Append tempt to body
  151.     $('body').append($temp);
  152.  
  153.     // Select text
  154.     $temp.val($passwordFinnal.val()).select();
  155.  
  156.     // Copy
  157.     document.execCommand('copy');
  158.  
  159.     // Remove temp
  160.     $temp.remove();
  161.  
  162.     // Alert
  163.     alert("Heslo bylo zkopírováno");
  164.  
  165.   });
  166.  
  167. })($);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement