<commit_before>/**
 * Icon Object
 * @type {Object}
 */
Icon = {
  /**
   * Get list icon via ajax and get back return array json from server
   * @param  {[type]} page   [description]
   * @param  {[type]} by     [description]
   * @param  {[type]} search [description]
   * @return {[type]}        [description]
   */
  getAll: function(page, by, search) {
    var self = this;
    var search = search ? search : "";

    //Ajax method
    $.ajax({
     type: "post",
     url: BASE_URL + "icon/get_all",
     cache: false,    
     data: {page: page, by: by, search: search},
      // 'page=' + page + "&by=" + by + "&search=" + search,
     success: function(response){        
        response = JSON.parse(response);
        //console.log(response);
        var items=[]; 
        var data = response.data;

        //populate data
        if(data.length > 0) {
          for(var i=0; i < data.length; i++) {
            //push data to content html
            var item = self.setSingleContent(data[i]);
            items.push(item);
          }
        } else {
          items.push('<h2 class="text-center" style="color: #3d3938;">Maaf, Icon tidak ditemukan</h2>');
        }

        //and append all icons to display in page html
        $('#icon-list').append.apply($('#icon-list'), items);
        //set button loadmore
        self.setLoadMore(response.more, response.page);        
     },
     error: function(){      
      alert('Error while request..');
     }
    });
  },
  /**
   * get all icon on cart
   * @return {[type]} [description]
   */
  getCart: function() {
    var self = this;
    //clear list cart on page
    $('.dropdown-keranjang').html("");

    //set button download element
    var liDownload = '<li class="divider"></li>';
    liDownload += '<li style="padding-top: 20px; padding-bottom: 30px;" id="btn-download-cart">';
    liDownload += '<a href="'+ BASE_URL +'cart">';
    liDownload += '<span class="green-color glyphicon glyphicon-save" aria-hidden="true"></span> Download all Icons </a> </li>';
                                  
    //set list element if cart empty  
    var liEmpty = '<li style="padding-top: 20px; padding-bottom: 30px;" id="li-cart-empty">Cart Empty</li>';

    //Ajax method
    $.ajax({
     type: "post",
     url: BASE_URL + "icon/get_cart",
     cache: false,    
     data: {},
     success: function(response){        
        response = JSON.parse(response);
        //console.log(response);
        var items=[]; 
        var data = response.data;
        var limit = data.length < 3 ? data.length : 3; //limit only 3 icon show on cart

        //populate data
        if(data.length > 0) {
          for(var i=0; i < limit; i++) {
            //push data to content html
            var item = self.setSingleItemOnCart(data[i]);
            items.push(item);
          }

          items.push(liDownload);
          $('.total-icons-keranjang').show();
        } else {
          $('.total-icons-keranjang').hide();
          items.push(liEmpty);
        }

        //and append all icons to display in cart list
        $('.dropdown-keranjang').append.apply($('.dropdown-keranjang'), items);
     },
     error: function(){      
      alert('Error while request..');
     }
    });
  },
  /**
   * add icon to cart
   * @param {[type]} id   [description]
   * @param {[type]} name [description]
   */
  addToCart: function(id, name) {
    var self = this;
    //get token
    var token = sessionStorage.getItem('userNotRegistered');
    //Ajax method
    $.ajax({
     type: "post",
     url: BASE_URL + "icon/add_to_cart",
     cache: false,    
     data: {id: id, token: token},
     success: function(response){        
       response  = JSON.parse(response);
       if(response.status === true) {
          //refresh cart
          self.getCart();
          //toggle button action to icon item
          $('.btn-add-cart[data-id="'+ id +'"]').remove();
          var btnRemove = self.setBtnRemoveIcon(id, name);
          $('.download-icon[data-id="'+ id +'"]').append(btnRemove);
          swal(name, "ditambahkan ke keranjang", "success")
       }
     },
     error: function(){      
      sweetAlert("Oops...", "Terjadi kesalahan pada sistem", "error");
     }
    });
  },
  /**
   * remove icon selected from cart
   * @param  {[type]} id   [description]
   * @param  {[type]} name [description]
   * @return {[type]}      [description]
   */
  removeFromCart: function(id, name) {
    var self = this;
    //get token
    var token = sessionStorage.getItem('userNotRegistered');

    //show alert warning before remove icon from cart
    swal({
      title: "Hapus Icon",
      text: "Ingin menghapus icon " + name + " dari cart?",
      type: "warning",
      showCancelButton: true,
      closeOnConfirm: false,
      showLoaderOnConfirm: true
    }, function () {
        //if clicked confirmation, call method to remove icon from cart
        $.ajax({
         type: "post",
         url: BASE_URL + "icon/remove_from_cart",
         cache: false,    
         data: {id: id, token: token},
         success: function(response){        
           response  = JSON.parse(response);
           // console.log(response);
           swal("Deleted!", 
            "Icons telah dihapus dari keranjang.", 
            "success");
           //refresh cart
           Icon.getCart();
           Cart.getAll();
           //toggle button action on icon item.
           $('.btn-remove-cart[data-id="'+ id +'"]').remove();
           var btnAdd = self.setBtnAddIcon(id, name);
           $('.download-icon[data-id="'+ id +'"]').append(btnAdd);
         },
         error: function(){      
          sweetAlert("Oops...", "Terjadi kesalahan pada sistem", "error");
         }
        }); 
    });   
  },
  /**
   * set element html for single item on cart list
   * @param {[type]} icon [description]
   */
  setSingleItemOnCart: function(icon) {
    var liCart = document.createElement("li");
    var imgCart = document.createElement("img");
    var spanCart = document.createElement("span");
    var btnRemove = document.createElement("a");
    var iconRemove = document.createElement("i");
    var textRemove = document.createTextNode(" Hapus");

    btnRemove.setAttribute("class", "black-color btn-remove-cart");
    btnRemove.setAttribute("data-id", icon.id);
    btnRemove.setAttribute("data-name", icon.name);
    btnRemove.setAttribute("href", "javascript:void(0)");
    iconRemove.setAttribute("class", "green-color fa fa-trash");
    btnRemove.appendChild(iconRemove);
    btnRemove.appendChild(textRemove);        
    spanCart.appendChild(btnRemove);

    imgCart.setAttribute("alt", icon.name);<commit_msg>modify icon.js<commit_after>/**
 * Icon Object
 * @type {Object}
 */
Icon = {
  /**
   * Get list icon via ajax and get back return array json from server
   * @param  {[type]} page   [description]
   * @param  {[type]} by     [description]
   * @param  {[type]} search [description]
   * @return {[type]}        [description]
   */
  getAll: function(page, by, search) {
    var self = this;
    var search = search ? search : "";

    //Ajax method
    $.ajax({
     type: "post",
     url: BASE_URL + "icon/get_all",
     cache: false,    
     data: {page: page, by: by, search: search},
      // 'page=' + page + "&by=" + by + "&search=" + search,
     success: function(response){        
        response = JSON.parse(response);
        //console.log(response);
        var items=[]; 
        var data = response.data;

        //populate data
        if(data.length > 0) {
          for(var i=0; i < data.length; i++) {
            //push data to content html
            var item = self.setSingleContent(data[i]);
            items.push(item);
          }
        } else {
          items.push('<h2 class="text-center" style="color: #3d3938;">Maaf, Icon tidak ditemukan</h2>');
        }

        //and append all icons to display in page html
        $('#icon-list').append.apply($('#icon-list'), items);
        //set button loadmore
        self.setLoadMore(response.more, response.page);        
     },
     error: function(){      
      alert('Error while request..');
     }
    });
  },
  /**
   * get all icon on cart
   * @return {[type]} [description]
   */
  getCart: function() {
    var self = this;
    //clear list cart on page
    $('.dropdown-keranjang').html("");

    //set button download element
    var liDownload = '<li class="divider"></li>';
    liDownload += '<li style="padding-top: 20px; padding-bottom: 30px;" id="btn-download-cart">';
    liDownload += '<a href="'+ BASE_URL +'cart">';
    liDownload += '<span class="green-color glyphicon glyphicon-save" aria-hidden="true"></span> Download all Icons </a> </li>';
                                  
    //set list element if cart empty  
    var liEmpty = '<li style="padding-top: 20px; padding-bottom: 30px;" id="li-cart-empty">Cart Empty</li>';

    //Ajax method
    $.ajax({
     type: "post",
     url: BASE_URL + "icon/get_cart",
     cache: false,    
     data: {},
     success: function(response){        
        response = JSON.parse(response);
        //console.log(response);
        var items=[]; 
        var data = response.data;
        var limit = data.length < 3 ? data.length : 3; //limit only 3 icon show on cart

        //populate data
        if(data.length > 0) {
          for(var i=0; i < limit; i++) {
            //push data to content html
            var item = self.setSingleItemOnCart(data[i]);
            items.push(item);
          }

          items.push(liDownload);
          $('.total-icons-keranjang').show();
        } else {
          $('.total-icons-keranjang').hide();
          items.push(liEmpty);
        }

        //and append all icons to display in cart list
        $('.dropdown-keranjang').append.apply($('.dropdown-keranjang'), items);
     },
     error: function(){      
      alert('Error while request..');
     }
    });
  },
  /**
   * add icon to cart
   * @param {[type]} id   [description]
   * @param {[type]} name [description]
   */
  addToCart: function(id, name) {
    var self = this;
    //get token
    var token = sessionStorage.getItem('userNotRegistered');
    $('.download-icon[data-id="'+ id +'"]').html("");
    //Ajax method
    $.ajax({
     type: "post",
     url: BASE_URL + "icon/add_to_cart",
     cache: false,    
     data: {id: id, token: token},
     success: function(response){        
       response  = JSON.parse(response);
       if(response.status === true) {
          //refresh cart
          self.getCart();
          //toggle button action to icon item         
          var btnRemove = self.setBtnRemoveIcon(id, name);
          $('.download-icon[data-id="'+ id +'"]').append(btnRemove);
          swal(name, "ditambahkan ke keranjang", "success")
       }
     },
     error: function(){    
      var btnAdd = self.setBtnAddIcon(id, name);
      $('.download-icon[data-id="'+ id +'"]').append(btnAdd);  
      sweetAlert("Oops...", "Terjadi kesalahan pada sistem", "error");
     }
    });
  },
  /**
   * remove icon selected from cart
   * @param  {[type]} id   [description]
   * @param  {[type]} name [description]
   * @return {[type]}      [description]
   */
  removeFromCart: function(id, name) {
    var self = this;
    //get token
    var token = sessionStorage.getItem('userNotRegistered');

    //show alert warning before remove icon from cart
    swal({
      title: "Hapus Icon",
      text: "Ingin menghapus icon " + name + " dari cart?",
      type: "warning",
      showCancelButton: true,
      closeOnConfirm: false,
      showLoaderOnConfirm: true
    }, function () {
         $('.download-icon[data-id="'+ id +'"]').html("");
        //if clicked confirmation, call method to remove icon from cart
        $.ajax({
         type: "post",
         url: BASE_URL + "icon/remove_from_cart",
         cache: false,    
         data: {id: id, token: token},
         success: function(response){        
           response  = JSON.parse(response);
           // console.log(response);
           
           if(response.status === true) {
             swal("Deleted!", 
              "Icons telah dihapus dari keranjang.", 
              "success");             
             //toggle button action on icon item.           
             var btnAdd = self.setBtnAddIcon(id, name);
             $('.download-icon[data-id="'+ id +'"]').append(btnAdd);
          } else {
             swal("Error!", 
              "Icons gagal dihapus dari keranjang.", 
              "error");             
             //toggle button action on icon item.           
             var btnRemove = self.setBtnRemoveIcon(id, name);
             $('.download-icon[data-id="'+ id +'"]').append(btnRemove);
          }

          //refresh cart
          Icon.getCart();
          Cart.getAll();
         },
         error: function(){      
          var btnRemove = self.setBtnRemoveIcon(id, name);
          $('.download-icon[data-id="'+ id +'"]').append(btnRemove);
          sweetAlert("Oops...", "Terjadi kesalahan pada sistem", "error");
         }
        }); 
    });   
  },
  /**
   * set element html for single item on cart list
   * @param {[type]} icon [description]
   */
  setSingleItemOnCart: function(icon) {
    var liCart = document.createElement("li");
    var imgCart = document.createElement("img");
    var spanCart = document.createElement("span");
    var btnRemove = document.createElement("a");
    var iconRemove = document.createElement("i");
    var textRemove = document.createTextNode(" Hapus");

    btnRemove.setAttribute("class", "black-color btn-remove-cart");
    btnRemove.setAttribute("data-id", icon.id);
    btnRemove.setAttribute("data-name", icon.name);
    btnRemove.setAttribute("href", "javascript:void(0)");
    iconRemove.setAttribute("class", "green-color fa fa-trash");
    btnRemove.appendChild(iconRemove);
    btnRemove.appendChild(textRemove);        
    spanCart.appendChild(btnRemove);

    imgCart.setAttribute("alt", icon.name);
{'fraction_non_alphanumeric': 0.15822335745707394, 'fraction_numerical': 0.003263800198666099, 'mean_word_length': 2.0896536606751424, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 31, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 25, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1291046', 'n_tokens_mistral': 4248, 'n_tokens_neox': 4192, 'n_words': 1055}