Advertisement
Guest User

Untitled

a guest
Apr 9th, 2020
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. $(document).ready(function(){
  2.  
  3.   const $content = $("div#content");
  4.   let noteArray = [];
  5.  
  6.   const Utils = {
  7.     uniqueID: function(){
  8.       return Math.random().toString(36).substr(2,9);
  9.     }
  10.   }
  11.  
  12.   const App = {
  13.  
  14.     init: function() {
  15.       console.log("initialized")
  16.       App.bindEvents();
  17.     },
  18.  
  19.     newNote: function() {
  20.  
  21.       const $win = $(window);
  22.       const $input = $('input');
  23.       let x = Math.random() * ( $win.width() - 100 );
  24.       let y = 40 + Math.random() * ( $win.height() - 100 );
  25.  
  26.       let note = {
  27.         id : Utils.uniqueID(),
  28.         text : $input.val(),
  29.         top : parseInt(y),
  30.         left : parseInt(x)
  31.       }
  32.  
  33.       noteArray.push( note );
  34.       App.printNote( note.id, note.top, note.left, note.text )
  35.       console.log( noteArray );
  36.  
  37.     },
  38.  
  39.     printNote: function( id, top, left, text ) {
  40.  
  41.       let note = $(
  42.         `<div id=${id} class="note" style="top: ${top}px; left: ${left}px;">
  43.         <p>${text}</p>
  44.         <div class="kill">x</div>
  45.         </div>`
  46.       );
  47.  
  48.       note.appendTo( $content );
  49.  
  50.     },
  51.  
  52.     deleteNote: function( that ) {
  53.  
  54.       that.parent().remove();
  55.  
  56.     },
  57.  
  58.     bindEvents: function() {
  59.  
  60.       $('input').change(function(){
  61.         App.newNote();
  62.         $(this).val('');
  63.       });
  64.  
  65.       $(document).on('click', '.kill', function(){
  66.         App.deleteNote( $(this) );
  67.       });
  68.  
  69.     }
  70.  
  71.   }
  72.  
  73.   App.init();
  74.  
  75. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement