Nulo
3a0c41b736
Squashed commit of the following: commit 482eea28821868f03ace33562e7bd34ab9a4478f Merge: 5f485281c128f2
Author: f <f@sutty.nl> Date: Thu Nov 25 18:31:35 2021 -0300 Merge branch 'master' into limpiar-eventos commit 5f48528c28b0709bd859a4dc52a830f60bfedc6e Author: f <f@sutty.nl> Date: Thu Nov 25 18:23:23 2021 -0300 pretty commit 70d05bc90a6cb64d1c4bfc39f48388af3fbc3c18 Merge: c4f33c0ff1bc21
Author: Nulo <nulo@sutty.nl> Date: Thu Oct 28 16:46:31 2021 -0300 Merge branch 'master' into limpiar-eventos commit c4f33c084058002a10fc0ec2137ffe045826cfd2 Author: f <f@sutty.nl> Date: Thu Oct 28 14:52:41 2021 -0300 limpiar eventos
82 lines
2 KiB
JavaScript
82 lines
2 KiB
JavaScript
import { CartBaseController } from "./cart_base_controller";
|
|
|
|
/*
|
|
* Renders the order table. All products are stored on localStorage, so
|
|
* we just fetch that information and render the cart contents using
|
|
* Liquid.
|
|
*/
|
|
export default class extends CartBaseController {
|
|
static targets = ["cart", "subtotal", "itemCount"];
|
|
|
|
async connect() {
|
|
const products = this.products;
|
|
const site = window.site;
|
|
|
|
this.render({ products, site });
|
|
this.subtotalUpdate();
|
|
this.itemCountUpdate();
|
|
|
|
this.storage_event = this._storage_event.bind(this);
|
|
this.cart_subtotal_update_event =
|
|
this._cart_subtotal_update_event.bind(this);
|
|
|
|
window.addEventListener("storage", this.storage_event);
|
|
window.addEventListener(
|
|
"cart:subtotal:update",
|
|
this.cart_subtotal_update_event
|
|
);
|
|
}
|
|
|
|
disconnect() {
|
|
window.removeEventListener("storage", this.storage_event);
|
|
window.removeEventListener(
|
|
"cart:subtotal:update",
|
|
this.cart_subtotal_update_event
|
|
);
|
|
}
|
|
|
|
async _storage_event(event) {
|
|
if (!event.key?.startsWith("cart:item:")) return;
|
|
|
|
const products = this.products;
|
|
const site = window.site;
|
|
|
|
this.render({ products, site });
|
|
this.subtotalUpdate();
|
|
this.itemCountUpdate();
|
|
}
|
|
|
|
_cart_subtotal_update_event(event) {
|
|
this.itemCountUpdate();
|
|
this.subtotalUpdate();
|
|
}
|
|
|
|
/*
|
|
* Download the item template and render the order
|
|
*/
|
|
render(data = {}) {
|
|
const template = window.templates[this.data.get("itemTemplate")];
|
|
|
|
this.engine
|
|
.parseAndRender(template, data)
|
|
.then((html) => (this.cartTarget.innerHTML = html));
|
|
}
|
|
|
|
/*
|
|
* Updates the subtotal
|
|
*
|
|
* XXX: This also updates the currency
|
|
*/
|
|
subtotalUpdate() {
|
|
if (!this.cart) return;
|
|
|
|
this.subtotalTarget.innerText = this.cart.data.attributes.display_total;
|
|
}
|
|
|
|
itemCountUpdate() {
|
|
if (!this.hasItemCountTarget) return;
|
|
if (!this.cart) return;
|
|
|
|
this.itemCountTarget.innerText = this.cart.data.attributes.item_count;
|
|
}
|
|
}
|