UNPKG

@apicart/vue-components

Version:

Apicart Vue.Js components for simple e-commerce platform development

318 lines (295 loc) 18.7 kB
/** * @apicart/vue-components v1.0.0-alpha7 * (c) 2018-2020 Apicart Company * Released under the MIT License. */ import Apicart from '@apicart/core-sdk'; import { Translator } from '../..'; import { ApicartButton, ApicartBuyButtonWithQuantity } from './..'; /*! ***************************************************************************** Copyright (c) Microsoft Corporation. Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. ***************************************************************************** */ function __awaiter(thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); } function __generator(thisArg, body) { var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; function verb(n) { return function (v) { return step([n, v]); }; } function step(op) { if (f) throw new TypeError("Generator is already executing."); while (_) try { if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; if (y = 0, t) op = [op[0] & 2, t.value]; switch (op[0]) { case 0: case 1: t = op; break; case 4: _.label++; return { value: op[1], done: false }; case 5: _.label++; y = op[1]; op = [0]; continue; case 7: op = _.ops.pop(); _.trys.pop(); continue; default: if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } if (t[2]) _.ops.pop(); _.trys.pop(); continue; } op = body.call(thisArg, _); } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; } } var config = { dropdownPosition: 'left' }; var store; var translator = Apicart.VueComponentsTranslator || Translator; var i18n = translator.getI18n(); function getDataForTemplate() { return __awaiter(this, void 0, void 0, function () { var products, itemsCountChanged, itemsCount, totalPrice, cart, cartEntity; return __generator(this, function (_a) { switch (_a.label) { case 0: products = []; itemsCountChanged = false; this.products = products; itemsCount = 0; totalPrice = 0; if (!(store && store.hasCartHash())) return [3, 4]; return [4, store.getCart()]; case 1: cart = _a.sent(); return [4, cart.getEntity()]; case 2: cartEntity = _a.sent(); if (!cartEntity) { return [2]; } return [4, cartEntity.getItems()]; case 3: products = _a.sent(); itemsCountChanged = this.itemsCount !== cartEntity.getTotalItemsQuantity(); itemsCount = cartEntity.getTotalItemsQuantity(); totalPrice = cartEntity.getTotalPrice(); _a.label = 4; case 4: this.products = products; this.itemsCount = itemsCount; this.totalPrice = totalPrice; if (itemsCountChanged) { this.runToggleButtonQuantityBounceEffect(); } return [2]; } }); }); } var closeTimeout = null; var windowIsDefined = typeof window !== 'undefined'; var script = { name: 'apicart-cart-dropdown', components: { 'apicart-button': windowIsDefined && window.ApicartButton ? window.ApicartButton : ApicartButton, 'apicart-buy-button-with-quantity': windowIsDefined && window.ApicartBuyButtonWithQuantity ? window.ApicartBuyButtonWithQuantity : ApicartBuyButtonWithQuantity }, i18n: i18n, data: function () { return { dataInitialized: false, products: [], totalPrice: 0, itemsCount: 0, visible: false, hideDropdownTimeout: null, dropdownPosition: null, showToggleButtonQuantityBounceEffect: false }; }, methods: { open: function () { clearTimeout(closeTimeout); this.visible = true; }, close: function () { var _this = this; closeTimeout = setTimeout(function () { _this.visible = false; }, 500); }, footerButtonClick: function () { this.$emit('footer-button-click'); this.close(); }, toggleButtonClick: function () { this.$emit('toggle-utton-click'); }, runToggleButtonQuantityBounceEffect: function () { var _this = this; this.showToggleButtonQuantityBounceEffect = true; setTimeout(function () { _this.showToggleButtonQuantityBounceEffect = false; }, 500); } }, beforeCreate: function () { var _this = this; Apicart.Utils.EventDispatcher.addListener('apicart-cart-dropdown-configure-' + this._uid, 'apicart:configure', function () { _this.$forceUpdate(); }); }, created: function () { var _this = this; config = Apicart.Utils.Objects.merge(config, Apicart.getConfigParameter('vueComponents.cartDropdown')); store = Apicart.getConfigParameter('store'); this.dropdownPosition = config.dropdownPosition; getDataForTemplate.call(this); Apicart.Utils.EventDispatcher.addListener('apicart-cart-dropdown-' + this._uid, 'apicart:cart:updated', function () { getDataForTemplate.call(_this); }); } }; function styleInject(css, ref) { if ( ref === void 0 ) ref = {}; var insertAt = ref.insertAt; if (!css || typeof document === 'undefined') { return; } var head = document.head || document.getElementsByTagName('head')[0]; var style = document.createElement('style'); style.type = 'text/css'; if (insertAt === 'top') { if (head.firstChild) { head.insertBefore(style, head.firstChild); } else { head.appendChild(style); } } else { head.appendChild(style); } if (style.styleSheet) { style.styleSheet.cssText = css; } else { style.appendChild(document.createTextNode(css)); } } var css_248z = ".apicart-cart-dropdown{font-family:-apple-system,\"Segoe UI\",Roboto,Oxygen,Ubuntu,Cantarell,\"Helvetica Neue\",Arial,sans-serif,\"Apple Color Emoji\",\"Segoe UI Emoji\",\"Segoe UI Symbol\";-webkit-font-smoothing:antialiased;box-sizing:border-box;outline:0}.apicart-cart-dropdown *{box-sizing:border-box;outline:0}.apicart-cart-dropdown{display:inline-block}@media (min-width:576px){.apicart-cart-dropdown{position:relative}}.apicart-cart-dropdown__button{cursor:pointer;align-items:center;display:flex;padding:14px}.apicart-cart-dropdown__button-icon-wrapper{position:relative;margin-right:14px}.apicart-cart-dropdown__button-icon{font-size:28px}.apicart-cart-dropdown__button-icon-quantity-wrapper{position:absolute;top:-10px;right:-10px;z-index:1}.apicart-cart-dropdown__button-icon-quantity{width:20px;line-height:20px;font-weight:500;text-align:center;color:#fff;border-radius:100%;font-size:10px;font-weight:500!important;background-color:#121212}.apicart-cart-dropdown__button-icon-quantity--bounce-once{animation:bounce-in .5s}@keyframes bounce-in{0%{transform:scale(1)}50%{transform:scale(1.8)}100%{transform:scale(1)}}.apicart-cart-dropdown__button-label{font-weight:500;text-transform:uppercase}.apicart-cart-dropdown__dropdown{background:#fff;border-radius:2px;transition:opacity .3s;opacity:1;width:100vw;margin-top:-10px;right:0;z-index:1;position:absolute;box-shadow:2px 4px 15px 0 rgba(0,0,0,.1)}@media (min-width:576px){.apicart-cart-dropdown__dropdown{width:500px}}.apicart-cart-dropdown__empty-cart{padding:20px 10px 10px 10px;text-align:center}.apicart-cart-dropdown--left{left:0}.apicart-cart-dropdown__items{max-height:324px;overflow:auto}.apicart-cart-dropdown__item{display:flex;align-items:center;border-bottom:1px solid #e6e6e7;padding:10px;height:76px}.apicart-cart-dropdown__item-image-wrapper{margin-right:10px;width:60px;overflow:hidden;display:flex}.apicart-cart-dropdown__item-image{width:100%;max-height:60px}.apicart-cart-dropdown__item-info{display:flex;align-items:center;justify-content:space-between;flex:1;flex-wrap:wrap}@media (min-width:576px){.apicart-cart-dropdown__item-info{flex-wrap:nowrap}}.apicart-cart-dropdown__item-name{font-size:14px;color:#121212;width:100%;margin-bottom:8px}@media (min-width:576px){.apicart-cart-dropdown__item-name{margin-bottom:0;width:40%}}.apicart-cart-dropdown__item-price{white-space:nowrap;font-size:16px;width:100px;text-align:center;font-weight:500}.apicart-cart-dropdown__footer{padding:10px}.apicart-cart-dropdown__footer-summary{align-items:center;display:flex;justify-content:space-between}.apicart-cart-dropdown__footer-summary-left-item{color:#75788a;font-size:14px}@media (min-width:576px){.apicart-cart-dropdown__footer-summary-left-item{font-size:16px}}.apicart-cart-dropdown__footer-summary-left-item-label{margin-right:8px;display:inline-block}.apicart-cart-dropdown__wrapper.apicart-cart-dropdown__wrapper--hovered .apicart-cart-dropdown{visibility:visible}.apicart-cart-dropdown-enter,.apicart-cart-dropdown-leave-active{opacity:0}.apicart-cart-dropdown-enter,.apicart-cart-dropdown-leave-to{opacity:0}"; styleInject(css_248z); function normalizeComponent(template, style, script, scopeId, isFunctionalTemplate, moduleIdentifier /* server only */, shadowMode, createInjector, createInjectorSSR, createInjectorShadow) { if (typeof shadowMode !== 'boolean') { createInjectorSSR = createInjector; createInjector = shadowMode; shadowMode = false; } // Vue.extend constructor export interop. const options = typeof script === 'function' ? script.options : script; // render functions if (template && template.render) { options.render = template.render; options.staticRenderFns = template.staticRenderFns; options._compiled = true; // functional template if (isFunctionalTemplate) { options.functional = true; } } // scopedId if (scopeId) { options._scopeId = scopeId; } let hook; if (moduleIdentifier) { // server build hook = function (context) { // 2.3 injection context = context || // cached call (this.$vnode && this.$vnode.ssrContext) || // stateful (this.parent && this.parent.$vnode && this.parent.$vnode.ssrContext); // functional // 2.2 with runInNewContext: true if (!context && typeof __VUE_SSR_CONTEXT__ !== 'undefined') { context = __VUE_SSR_CONTEXT__; } // inject component styles if (style) { style.call(this, createInjectorSSR(context)); } // register component module identifier for async chunk inference if (context && context._registeredComponents) { context._registeredComponents.add(moduleIdentifier); } }; // used by ssr in case component is cached and beforeCreate // never gets called options._ssrRegister = hook; } else if (style) { hook = shadowMode ? function (context) { style.call(this, createInjectorShadow(context, this.$root.$options.shadowRoot)); } : function (context) { style.call(this, createInjector(context)); }; } if (hook) { if (options.functional) { // register for functional component in vue file const originalRender = options.render; options.render = function renderWithStyleInjection(h, context) { hook.call(context); return originalRender(h, context); }; } else { // inject component registration as beforeCreate hook const existing = options.beforeCreate; options.beforeCreate = existing ? [].concat(existing, hook) : [hook]; } } return script; } /* script */ const __vue_script__ = script; /* template */ var __vue_render__ = function () {var _vm=this;var _h=_vm.$createElement;var _c=_vm._self._c||_h;return _c('div',{staticClass:"apicart-block apicart-cart-dropdown",attrs:{"id":_vm.$t('cartDropdown.id')},on:{"mouseover":_vm.open,"mouseleave":_vm.close}},[_c('div',{staticClass:"apicart-cart-dropdown__button",on:{"click":_vm.toggleButtonClick}},[_c('div',{staticClass:"apicart-cart-dropdown__button-icon-wrapper"},[_c('span',{staticClass:"apicart-cart-dropdown__button-icon apicart-icon-shopping-bag"}),_c('div',{staticClass:"apicart-cart-dropdown__button-icon-quantity-wrapper"},[_c('div',{class:[{'apicart-cart-dropdown__button-icon-quantity--bounce-once': _vm.showToggleButtonQuantityBounceEffect}, 'apicart-cart-dropdown__button-icon-quantity']},[_vm._v(" "+_vm._s(_vm.itemsCount)+" ")])])]),_c('div',{staticClass:"apicart-cart-dropdown__button-label"},[_vm._v(" "+_vm._s(_vm.$t('cartDropdown.toggleButton'))+" ")])]),_c('transition',{attrs:{"name":"apicart-cart-dropdown"}},[_c('div',{directives:[{name:"show",rawName:"v-show",value:(_vm.visible),expression:"visible"}],class:'apicart-cart-dropdown__dropdown apicart-cart-dropdown--' + _vm.dropdownPosition},[_c('div',{staticClass:"apicart-cart-dropdown__items"},[_vm._l((_vm.products),function(product){return _c('div',{key:product.getItem().getExternalId(),staticClass:"apicart-cart-dropdown__item"},[(product.getItem().getParameterValue('images').primary.url)?_c('div',{staticClass:"apicart-cart-dropdown__item-image-wrapper"},[_c('img',{staticClass:"apicart-cart-dropdown__item-image",attrs:{"src":product.getItem().getParameterValue('images').primary.url,"loading":"lazy","alt":""}})]):_vm._e(),_c('div',{staticClass:"apicart-cart-dropdown__item-info"},[_c('div',{staticClass:"apicart-cart-dropdown__item-name"},[_vm._v(_vm._s(product.getItem().getName()))]),_c('div',{staticClass:"apicart-cart-dropdown__item-quantity-manipulator"},[_c('apicart-buy-button-with-quantity',{attrs:{"quantity":product.getQuantity(),"productUrl":product.getItem().getDataUrl()}})],1),_c('div',{staticClass:"apicart-cart-dropdown__item-price"},[_vm._v(_vm._s(_vm.$n(product.getTotalPrice(), 'currency')))])])])}),_c('div',{directives:[{name:"show",rawName:"v-show",value:(!_vm.products.length),expression:"!products.length"}],staticClass:"apicart-cart-dropdown__empty-cart"},[_vm._v(" "+_vm._s(_vm.$t('cartDropdown.emptyCart'))+" ")])],2),_c('div',{staticClass:"apicart-cart-dropdown__footer"},[_c('div',{staticClass:"apicart-cart-dropdown__footer-summary"},[_c('div',{staticClass:"apicart-cart-dropdown__footer-summary-left"},[_c('div',{staticClass:"apicart-cart-dropdown__footer-summary-left-item"},[_c('span',{staticClass:"apicart-cart-dropdown__footer-summary-left-item-label"},[_vm._v(_vm._s(_vm.$t('cartDropdown.itemsCount'))+":")]),_c('span',[_vm._v(_vm._s(_vm.itemsCount))])]),_c('div',{staticClass:"apicart-cart-dropdown__footer-summary-left-item"},[_c('span',{staticClass:"apicart-cart-dropdown__footer-summary-left-item-label"},[_vm._v(_vm._s(_vm.$t('cartDropdown.totalPrice'))+":")]),_c('span',[_vm._v(_vm._s(_vm.$n(_vm.totalPrice, 'currency')))])])]),_c('div',{staticClass:"apicart-cart-dropdown__footer-summary-right"},[_c('apicart-button',{directives:[{name:"show",rawName:"v-show",value:(_vm.products.length),expression:"products.length"}],attrs:{"type":"outlined"},nativeOn:{"click":function($event){return _vm.footerButtonClick($event)}}},[_vm._v(" "+_vm._s(_vm.$t('cartDropdown.button'))+" ")])],1)])])])])],1)}; var __vue_staticRenderFns__ = []; /* style */ const __vue_inject_styles__ = undefined; /* scoped */ const __vue_scope_id__ = undefined; /* module identifier */ const __vue_module_identifier__ = undefined; /* functional template */ const __vue_is_functional_template__ = false; /* style inject */ /* style inject SSR */ /* style inject shadow dom */ const __vue_component__ = normalizeComponent( { render: __vue_render__, staticRenderFns: __vue_staticRenderFns__ }, __vue_inject_styles__, __vue_script__, __vue_scope_id__, __vue_is_functional_template__, __vue_module_identifier__, false, undefined, undefined, undefined ); export default __vue_component__;