parser

Написать ответ на текущее сообщение

 

 
   команды управления поиском

Ответ

Mizter Egoist 07.05.2017 13:42 / 07.05.2017 13:48

Спасибо за прекрасный пример, без него Я бы не понял что делать.
Теперь все заработало.
Вот мой код:
На всех страницах
<header>
<div id="basket"></div>
</header>

На странице /basket
<div id="fullBasket"></div>


basket.html
^if(def $form:basket && def $form:basketAction){

  $_jsonString[^taint[as-is;$form:basket]]
  $_hash[^json:parse[$_jsonString]]

  ^if(def $_hash.uuid){

    $_basketQuery[^get.query[
      $.table[basket]
      $.select[
        $.bID[= '$_hash.uuid']
      ]
    ]]

    $_basketData[^get.selectData[$_basketQuery]]

    $_table[^table::create{item	quantity	price}]

    ^if(def $_hash.items){
      ^_hash.items.foreach[;item]{
        ^_table.append[$item]
      }
    }

    $_hashItems[^json:string[$_table]]

    ^switch[$form:basketAction]{

      ^case[add_basket]{
        ^self._transaction{
          ^if(!def $_basketData.bID && !def $_basketData.items && def $_hashItems && def $_hash.create){
            ^void:sql{
              INSERT INTO basket (bID, items, create_at)
              VALUES ('$_hash.uuid', '^taint[as-is;$_hashItems]', '$_hash.create')
            }
          }(def $_basketData.bID && def $_hashItems && $_basketData.items ne $_hashItems){
            ^void:sql{
              UPDATE basket
              SET items = '^taint[as-is;$_hashItems]'
              WHERE bID = '$_hash.uuid'
            }
          }
        }
      }

      ^case[del_basket]{
        ^self._transaction{
          ^void:sql{
            UPDATE basket
            SET items = '^taint[as-is;$_hashItems]'
            WHERE bID = '$_hash.uuid'
          }
        }
      }

      ^case[cng_basket]{
        ^self._transaction{
          ^void:sql{
            UPDATE basket
            SET items = '^taint[as-is;$_hashItems]'
            WHERE bID = '$_hash.uuid'
          }
        }
      }

    }

  }
}
$_basketQuery[^get.query[
  $.table[basket]
  $.select[
    $.bID[= '$cookie:bID']
  ]
]]
$_basketData[^get.selectData[$_basketQuery]]

^if(def $_basketData.items && $_basketData.items ne '[]'){
  $_basketString[^taint[as-is;$_basketData.items]]
  $_basketHash[^json:parse[$_basketString]]

  $_basketTable[^table::create{item	quantity	price}]
  ^_basketHash.foreach[;item]{
    ^_basketTable.append[$item]
  }
  $_basketHash[^_basketTable.hash[item]]
  ^_basketTable.menu{
    $sum(^eval(^eval($_basketTable.quantity * $_basketTable.price) + $sum))
  }
  $count(^_basketTable.count[])

  $_basketItems[^_basketTable.menu{
    $_basketTable.item
  }[,]]

  $_basketGoods[^get.query[
    $.table[pages]
    $.select[
      $.eID[IN ($_basketItems)]
      $.tID[= '4']
    ]
  ]]
  $basketGoods[^get.selectData[$_basketGoods]]

  ^if(def $basketGoods){
    <div id="basket">
      В вашей ^if($request:uri eq '/basket'){корзине}{<a href="/basket">корзине</a>} $count ^get.variations[$count;товар;товара;товаров] на сумму $sum &#8381; <br>
      ^basketGoods.menu{
        <div class="item">
          <div class="itemName">$basketGoods.name</div>
          <div class="button" data-item="$basketGoods.eID" data-quantity="-1" data-action="cng_basket">-</div>
          <div class="itemQuantity">$_basketHash.[$basketGoods.eID].quantity</div>
          <div class="button" data-item="$basketGoods.eID" data-quantity="+1" data-action="cng_basket">+</div>
          <div class="itemPrice">$_basketHash.[$basketGoods.eID].price</div>
        </div>
      }
    </div>
    <div id="fullBasket">
      В вашей корзине $count ^get.variations[$count;товар;товара;товаров] на сумму $sum &#8381; <br>
      ^basketGoods.menu{
        <div class="item">
          <div class="itemName">$basketGoods.name</div>
          <div class="button" data-item="$basketGoods.eID" data-quantity="-1" data-action="cng_basket">-</div>
          <div class="itemQuantity">$_basketHash.[$basketGoods.eID].quantity</div>
          <div class="button" data-item="$basketGoods.eID" data-quantity="+1" data-action="cng_basket">+</div>
          <div class="itemPrice">$_basketHash.[$basketGoods.eID].price</div>
          <div class="button" data-item="$basketGoods.eID" data-action="del_basket">Удалить</div>
        </div>
      }
    </div>
  }

}{
  <div id="basket">Ваша ^if($request:uri eq '/basket'){корзина}{<a href="/basket">корзина</a>} пуста</div>
  <div id="fullBasket">Ваша корзина пуста</div>
}


basket.js
$(document).ready(function() {
    var click = new Date();
    var clickDate = click.format("yyyy-mm-dd HH:MM:ss");

    var backend = '/bulk/ajax/basket.html';

    var basket;
    var basketData = JSON.parse(localStorage.getItem('basket'));
    var basketCookie = getCookie('bID');

    if (basketCookie !== false) {
        var path = '/bulk/ajax/basket.html?showJSON=true&uuid=' + basketCookie;
    }

    //Работа с кнопками
    $('body').on('click', '.button', function() {
        if (this.hasAttribute('data-item')) {
            var item_id = this.getAttribute('data-item');
        }
        if (this.hasAttribute('data-price')) {
            var price = this.getAttribute('data-price');
        }
        if (this.hasAttribute('data-quantity')) {
            var count = this.getAttribute('data-quantity');
            var quantity = document.getElementById(count);
        }
        if (this.hasAttribute('data-action')) {
            var action = this.getAttribute('data-action');
        }
        switch (action) {
            case 'add_basket':
                basket = addBasket(item_id, quantity.value, price, action);
                break;
            case 'del_basket':
                basket = delBasket(item_id, action);
                break;
            case 'cng_basket':
                basket = cngBasket(item_id, count, action);
                break;
            default:
                break;
        }
        var data = {
            'basket': basket,
            'basketAction': action
        }
        $.post(backend, data, function(response) {
          var userURI = location.pathname;
          if (userURI == '/basket') {
              var result = $('<div />').append(response).find('#fullBasket').html();
              $('#fullBasket').html(result);
          }
          var result = $('<div />').append(response).find('#basket').html();
          $('#basket').html(result);
        });
    });

    if (typeof(basketData) != "undefined" && basketData !== null) {

        if (basketCookie == false) {
            var cookieExpires = new Date(Date.parse(basketData.create) + 1000 * 60 * 60 * 24 * 3);
            var dataCookie = {
                'expires': cookieExpires,
                'path': '/'
            };
            setCookie('bID', basketData.uuid, dataCookie);
        }

    } else if (basketData == null && basketCookie !== false) {

        $.getJSON(path, function(data) {
            var storage = {};

            for (var key in data) {
                if (key == 'items') {
                    if (data[key] == '') {
                        storage[key] = [];
                    } else {
                        storage[key] = data[key];
                    }
                } else {
                    storage[key] = data[key];
                }
            }

            localStorage.setItem('basket', $.toJSON(storage));
        });

    } else if (basketData == null && basketCookie == false) {
        var newUUID = generateUUID();
        var data = {
            'uuid': newUUID,
            'items': [],
            'create': clickDate
        };

        localStorage.setItem('basket', $.toJSON(data));

        basketData = JSON.parse(localStorage.getItem('basket'));

        if (basketCookie == false) {
            var cookieExpires = new Date(Date.parse(basketData.create) + 1000 * 60 * 60 * 24 * 3);
            var dataCookie = {
                'expires': cookieExpires,
                'path': '/'
            };
            setCookie('bID', basketData.uuid, dataCookie);
        }

    }
    $.post(backend, function(response) {
        var userURI = location.pathname;
        if (userURI == '/basket') {
            var result = $('<div />').append(response).find('#fullBasket').html();
            $('#fullBasket').html(result);
        }
        var result = $('<div />').append(response).find('#basket').html();
        $('#basket').html(result);
    });
});