T
T
TastyScript2015-09-22 16:42:36
JavaScript
TastyScript, 2015-09-22 16:42:36

How to run javascript every time with preloader?

There is a module for opencart "pop-up cart"
All code works, but does not always display what you need the first time.
To make it clearer:
add the product to the cart -> the cart opens, but it is empty -> go to the cart again and the product is already displayed as added.
It is necessary that the cart is updated the first time, and that the styles do not run while the code is loading, the preloader is shown to the buyer (they say the request is being processed)
I don’t know how to work with javascript code, I searched the Internet for blanks.
Here is the js code of this module

(function($){

  var modPopupCart = {};

  // URI для загрузки содержимого корзины
  var popupCartUri = 'index.php?route=module/popupcart/load';
  // URI страницы оформления заказ
  var cartCheckoutUri = 'index.php?route=checkout/cart';

  // div
  var popupCartContainer = '#popupcart';
  var popupCartTitle = 'div.title';
  var popupCartContent = '#popupcart .popupcart_content';

  /*
  * Инициализация контейнера
  */
  modPopupCart.init = function(config){
    var defConf = {};
    var $this = this;

    defConf = {
      draggable : "1",
    };

    config = $.extend({},defConf,config);

    // Настройка эффекта draggable
    if( parseInt(config.draggable) == 1 ){
      $(popupCartContainer).draggable({ handle: popupCartTitle });
    }

    $(popupCartContainer).disableSelection();
    $(popupCartContainer).resizable({
      maxHeight: 800,
      maxWidth: 800,
      minHeight: 530,
      minWidth: 600,
      alsoResize: popupCartContent
    });
    $(popupCartContent).resizable();

    // Останавливаем дальншейшее всплытие события "click" при клике на область окна
    $(popupCartContainer).click(function(event){
      event.stopPropagation();
    });

    // по клику на область вне окна корзины закрываем окно
    $('body').click(function(){
      $this.close();
    });

    $('body').keydown(function(event){
      if(event.which == 27) { // ESC 
        $this.close();
      }
    });		
  };

  /*
  * Открытие окна корзины
  */
  modPopupCart.open = function(){
    $(popupCartContent).load(popupCartUri,{},function(){
      $(popupCartContainer).css('top', ($(window).height()/2-$(popupCartContainer).height()/2)+ $(window).scrollTop());
      $(popupCartContainer).css('left', $(window).width()/2-$(popupCartContainer).width()/2);
      $(popupCartContainer).css("position","absolute");
      $(popupCartContainer).css("z-index","9999");
      $("#pop_back").last().addClass("pop_back");
      $(popupCartContainer).show();	
    });
    
    return false;
  };

  /*
  * Закрытие окна корзины
  */
  modPopupCart.close = function(){
    $(popupCartContainer).hide();
    $(popupCartContent).empty();
    $("#pop_back").removeClass("pop_back");
    return false;
  };

  /*
  * Удаление товарной позиции
  */
  modPopupCart.removeProduct = function(productId){
    if(getURLVar('route') == 'checkout/cart' || getURLVar('route') == 'checkout/checkout') {
      location = cartCheckoutUri + '&remove=' + productId;
    } else {
      $(popupCartContent).load(popupCartUri + '&remove=' + productId);
      $('#cart').load('index.php?route=module/cart&remove='  + productId + ' #cart > *');
    }
  }

  /*
  * Удаление ваучера
  */
  modPopupCart.removeVoucher = function(voucherId){
    if(getURLVar('route') == 'checkout/cart' || getURLVar('route') == 'checkout/checkout') {
      location = cartCheckoutUri + '&remove=' + voucherId;
    } else {
      $(popupCartContent).load(popupCartUri+'&remove=' + voucherId);
      $('#cart').load('index.php?route=module/cart&remove='  + voucherId + ' #cart > *');
    }
  }

  window.modPopupCart = modPopupCart;

})($);

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question