UNPKG

@apicart/vue-components

Version:

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

322 lines (297 loc) 19.7 kB
/** * @apicart/vue-components v1.0.0-alpha7 * (c) 2018-2020 Apicart Company * Released under the MIT License. */ (function (global, factory) { typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory(require('@apicart/core-sdk'), require('../..'), require('./..')) : typeof define === 'function' && define.amd ? define(['@apicart/core-sdk', '../..', './..'], factory) : (global = global || self, global.ApicartShippingMethodsList = factory(global.Apicart, global.src, global.components)); }(this, (function (Apicart, src, components) { 'use strict'; Apicart = Apicart && Object.prototype.hasOwnProperty.call(Apicart, 'default') ? Apicart['default'] : Apicart; /*! ***************************************************************************** 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 store; var translator = Apicart.VueComponentsTranslator || src.Translator; var i18n = translator.getI18n(); var windowIsDefined = typeof window !== 'undefined'; var script = { name: 'apicart-shipping-methods-list', i18n: i18n, props: { loadingEnabled: { type: Boolean, default: true } }, components: { 'apicart-loader': windowIsDefined && window.ApicartLoader ? window.ApicartLoader : components.ApicartLoader }, data: function () { return { shippingMethods: [], shippingMethodsLoaded: false, selectedShippingMethodIndex: null, selectedShippingMethod: null, allowedMethods: [], disabledMethods: [] }; }, watch: { loadingEnabled: function () { this.loadMethodsList(); } }, methods: { toggleShippingMethod: function (index) { return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_a) { switch (_a.label) { case 0: this.selectedShippingMethodIndex = index; this.selectedShippingMethod = this.shippingMethods[index]; return [4, store.getCart()]; case 1: (_a.sent()).addParameter('shippingMethod', { id: this.selectedShippingMethod.getId() }); this.$emit('shipping-method-updated', this.selectedShippingMethod); return [2]; } }); }); }, loadMethodsList: function () { var _this = this; if (!this.loadingEnabled || this.shippingMethodsList) { return; } store.getShippingMethods().then(function (result) { return __awaiter(_this, void 0, void 0, function () { var cart, selectedShippingMethodParameter, selectedShippingMethodId; var _this = this; return __generator(this, function (_a) { switch (_a.label) { case 0: this.shippingMethods = result.filter(function (shippingMethod) { var methodUid = shippingMethod.getUid(); var methodAllowed = true; if (_this.allowedMethods.length && _this.allowedMethods.indexOf(methodUid) === -1) { methodAllowed = false; } if (_this.disabledMethods.length && _this.disabledMethods.indexOf(methodUid) > -1) { methodAllowed = false; } return methodAllowed; }); this.shippingMethodsLoaded = true; return [4, store.getCart()]; case 1: cart = _a.sent(); return [4, cart.getParameterValue('shippingMethod')]; case 2: selectedShippingMethodParameter = _a.sent(); selectedShippingMethodId = selectedShippingMethodParameter ? selectedShippingMethodParameter.id : null; if (selectedShippingMethodId && this.shippingMethods.length) { Apicart.Utils.Loops.forEach(this.shippingMethods, function (shippingMethod, index) { if (shippingMethod.getId() === selectedShippingMethodId) { _this.selectedShippingMethodIndex = index; _this.selectedShippingMethod = shippingMethod; _this.$emit('shipping-method-updated', _this.selectedShippingMethod); return false; } }); } return [2]; } }); }); }); } }, mounted: function () { store = Apicart.getConfigParameter('store'); var config = Apicart.getConfigParameter('vueComponents.shippingMethodsList'); if (config) { if (Array.isArray(config.allowedMethods)) { this.allowedMethods = config.allowedMethods; } if (Array.isArray(config.disabledMethods)) { this.disabledMethods = config.disabledMethods; } } this.loadMethodsList(); } }; 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-shipping-methods-list{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-shipping-methods-list *{box-sizing:border-box;outline:0}.apicart-shipping-methods-list{margin-bottom:20px;display:flex;flex-direction:column;width:100%;border:1px solid #e6e6e7;border-radius:2px}.apicart-shipping-methods-list__item{border-bottom:1px solid #e6e6e7;cursor:pointer;user-select:none;position:relative;padding:12px 12px 12px 0}.apicart-shipping-methods-list__item--no-methods-found{text-align:center;cursor:initial}.apicart-shipping-methods-list__item:last-of-type{border-bottom:0}.apicart-shipping-methods-list__item--selected .apicart-shipping-methods-list__item-description{display:block;padding-left:50px}.apicart-shipping-methods-list__item-header{display:flex;flex-direction:row;align-items:center}.apicart-shipping-methods-list__item-checkbox-wrapper{display:flex;position:relative;justify-content:center;padding:14px 26px;align-items:center}.apicart-shipping-methods-list__item-checkbox-input{display:none}.apicart-shipping-methods-list__item-checkbox-input-placeholder{position:absolute;display:inline-block;border:1px solid #e6e6e7;display:block;width:20px;height:20px;border-radius:100%;pointer-events:none;user-select:none;background-color:#e6e6e7;background-repeat:no-repeat;background-position:center center;background-size:90%;box-shadow:0;transition:background-color .3s}.apicart-shipping-methods-list__item-details-wrapper{display:flex;justify-content:space-between;width:100%;width:100%;align-items:center}.apicart-shipping-methods-list__item-details-name-image{display:flex;flex-direction:column;align-items:flex-start}@media (min-width:576px){.apicart-shipping-methods-list__item-details-name-image{flex-direction:row;align-items:center}}.apicart-shipping-methods-list__item-image{max-width:90px;margin-bottom:12px}@media (min-width:576px){.apicart-shipping-methods-list__item-image{margin-bottom:0;margin-right:20px}}.apicart-shipping-methods-list__item-name{font-size:12px}.apicart-shipping-methods-list__item-description{font-size:14px;line-height:1.5;padding-left:50px;color:#b3b3b3;overflow:hidden;height:auto;max-height:0;transition:max-height .3s,margin-top .3s;margin-top:0}.apicart-shipping-methods-list__item-description>:last-child{margin-bottom:0}.apicart-shipping-methods-list__item-details-price{font-size:18px;font-weight:500}.apicart-shipping-methods-list__item--selected .apicart-shipping-methods-list__item-description{max-height:100px;margin-top:8px;margin-top:8px}.apicart-shipping-methods-list__item--selected .apicart-shipping-methods-list__item-checkbox-input-placeholder{background-color:#121212}.apicart-shipping-methods-list--loading{padding:20px;text-align:center}.apicart-shipping-methods-list--loading p{margin-top: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 (!_vm.shippingMethodsLoaded)?_c('div',{staticClass:"apicart-shipping-methods-list apicart-shipping-methods-list--loading",attrs:{"id":_vm.$t('shippingMethodsList.id')}},[_c('apicart-loader',{attrs:{"visible":true}})],1):_c('div',{staticClass:"apicart-shipping-methods-list",attrs:{"id":_vm.$t('shippingMethodsList.id')}},[(_vm.shippingMethods.length)?_c('div',_vm._l((_vm.shippingMethods),function(shippingMethod,index){return _c('div',{key:index,staticClass:"apicart-shipping-methods-list__item",class:{'apicart-shipping-methods-list__item--selected': index === _vm.selectedShippingMethodIndex},attrs:{"data-shipping-method-uid":shippingMethod.getUid()},on:{"click":function($event){return _vm.toggleShippingMethod(index)}}},[_c('div',{staticClass:"apicart-shipping-methods-list__item-header"},[_c('div',{staticClass:"apicart-shipping-methods-list__item-checkbox-wrapper"},[_c('input',{staticClass:"apicart-shipping-methods-list__item-checkbox-input",attrs:{"type":"radio","name":"shipping-method"},domProps:{"checked":index === _vm.selectedShippingMethodIndex,"value":index}}),_c('div',{staticClass:"apicart-shipping-methods-list__item-checkbox-input-placeholder"})]),_c('div',{staticClass:"apicart-shipping-methods-list__item-details-wrapper"},[_c('div',{staticClass:"apicart-shipping-methods-list__item-details-name-image"},[(shippingMethod.getImage())?_c('img',{staticClass:"apicart-shipping-methods-list__item-image",attrs:{"src":shippingMethod.getImage(),"loading":"lazy","alt":""}}):_vm._e(),_c('div',{staticClass:"apicart-shipping-methods-list__item-name"},[_vm._v(_vm._s(shippingMethod.getName()))])]),_c('div',{staticClass:"apicart-shipping-methods-list__item-details-price"},[_vm._v(" "+_vm._s(_vm.$n(shippingMethod.getPrice(), 'currency'))+" ")])])]),_c('div',{staticClass:"apicart-shipping-methods-list__item-description apicart-shipping-methods-list__item-description--collapsed",domProps:{"innerHTML":_vm._s(shippingMethod.getDescription())}})])}),0):_c('div',{staticClass:"apicart-shipping-methods-list__item apicart-shipping-methods-list__item--no-methods-found"},[_vm._v(" "+_vm._s(_vm.$t('shippingMethodsList.methodsNotFound'))+" ")])])}; 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 ); return __vue_component__; })));