Advertisement
InasAwad

Untitled

Mar 10th, 2020
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var budgetController = (function (){
  2.    
  3. var Expense = function(id, description, value){
  4.     this.id = id;
  5.     this.description = description;
  6.     this.value = value;
  7. };
  8.    
  9. var Income = function(id, description, value){
  10.     this.id = id;
  11.     this.description = description;
  12.     this.value = value;
  13. };
  14.    
  15.     var data = {
  16.         allItems : {
  17.             exp : [],
  18.             inc : []
  19.             },
  20.         totals : {
  21.             exp : 0,
  22.             inc : 0
  23.         }
  24.     };
  25.    
  26.     return {
  27.         addAllItems : function(type, des, val){
  28.             var newItem, ID;
  29.            
  30.             // create a new ID
  31.             if (data.allItems[type].length > 0){
  32.             ID = data.allItems[type][data.allItems[type].length -1]. id + 1;
  33.             } else {
  34.                 ID = 0;
  35.                
  36.             }
  37.            
  38.             // create a new item based on 'exp' or 'inc' type
  39.             if (type === 'exp'){
  40.                 newItem = new Expense (ID, des, val);
  41.             } else if ( type === 'inc'){
  42.                 newItem = new Income (ID, des, val)
  43.             }
  44.          
  45.             // push it to our data structure
  46.             data.allItems[type].push(newItem);
  47.            
  48.             // return a new element
  49.             return newItem;
  50.            
  51.         },
  52.         testing : function(){
  53.             console.log(data);
  54.         }
  55.    
  56.    
  57.     };
  58.    
  59.    
  60.    
  61. })();
  62.  
  63.  
  64.  
  65. // UI controller
  66. var UIController = (function (){
  67.     var DOMStrings = {
  68.         inputType: '.add__type',
  69.         inputDescription: '.add__description',
  70.         inputValue : '.add__value',
  71.         inputBtn : '.add__btn',
  72.         incomeContainer : '.income__list',
  73.         expensesContainer : '.expenses__list'    
  74.     }
  75.    
  76.  
  77. return {
  78.     getInput : function(){
  79.         return {
  80.             type: document.querySelector(DOMStrings.inputType).value,  // will be either inc or exp.
  81.             description: document.querySelector(DOMStrings.inputDescription).value,
  82.             value: document.querySelector(DOMStrings.inputValue).value
  83.            
  84.         };
  85.     },
  86.    
  87.     addListItem : function(obj, type){
  88.       var html, newhtml, element;
  89.         // Create an HTML string with placeholder text
  90.        
  91.         if (type === 'inc'){
  92.             element = DOMStrings.incomeContainer;
  93.         html = '<div class="item clearfix" id="income-%id%"><div class="item__description">%description%</div><div class="right clearfix"><div class="item__value">%value%</div><div class="item__delete"><button class="item__delete--btn"><i class="ion-ios-close-outline"></i></button></div></div></div>';
  94.            
  95.         } else if (type === 'exp'){
  96.             element = DOMStrings.expensesContainer;
  97.           html ='<div class="item clearfix" id="expense-%id%"><div class="item__description">%description%</div><div class="right clearfix"><div class="item__value">%value%</div><div class="item__percentage">21%</div><div class="item__delete"><button class="item__delete--btn"><i class="ion-ios-close-outline"></i></button></div></div></div>';
  98.            
  99.         }
  100.        
  101.         // Replace the placeholder text with actual data
  102.         newhtml = html.replace ('%id%', obj.id);
  103.         newhtml = newhtml.replace ('%description%', obj.description);
  104.         newhtml = newhtml.replace ('%value%', obj.value);
  105.        
  106.         // Insert the HTML into the DOM
  107.         document.querySelector(element).insertAdjacentHTML('beforeend', newhtml);
  108.        
  109.     },
  110.    
  111.      getDOMStrings : function(){
  112.          return DOMStrings;
  113.      }
  114. };
  115.        
  116.    
  117. })();
  118.  
  119.  
  120. // controller
  121. var controller = (function (budgetCtrl, UICtrl){
  122.    
  123.  
  124.     var setupEventListener = function(){
  125.    
  126.     var DOM = UICtrl.getDOMStrings();
  127.  
  128.     document.querySelector(DOM.inputBtn).addEventListener ('click', ctrlAddItem);
  129.    
  130.     document.addEventListener('keypress', function(event){
  131.         if(event.keypress === 13 || event.which === 13){
  132.            
  133.             ctrlAddItem();
  134.         }
  135.        
  136.     });
  137.        
  138.     };
  139.    
  140.    
  141.     var ctrlAddItem = function(){
  142.         var input, newItem;
  143.        
  144.     // 1. Get user input.
  145.         input = UICtrl.getInput();
  146.    
  147.     // 2. Add the item to the budget controller.
  148.         newItem = budgetCtrl.addAllItems(input.type, input.description, input.value);
  149.        
  150.    
  151.     // 3. Add the item to UI.
  152.         UICtrl.addListItem(newItem, input.type);
  153.    
  154.     // 4. Calculate the budget.
  155.    
  156.     // 5. Display the budget on the UI.
  157.        
  158.     };
  159.    
  160.    return {
  161.        init : function(){
  162.            console.log('Application has started!');
  163.            setupEventListener();
  164.        }
  165.    };
  166.    
  167.    
  168.    
  169. })(budgetController, UIController);
  170.  
  171.  
  172. controller.init();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement