brandlabs-bigcommerce-wishlist
Version:
Easy-to-use wishlist functionality for BigCommerce themes.
1 lines • 18 kB
JavaScript
define("wishlistManager",[],(function(){return function(t){var e={};function r(i){if(e[i])return e[i].exports;var n=e[i]={i:i,l:!1,exports:{}};return t[i].call(n.exports,n,n.exports,r),n.l=!0,n.exports}return r.m=t,r.c=e,r.d=function(t,e,i){r.o(t,e)||Object.defineProperty(t,e,{enumerable:!0,get:i})},r.r=function(t){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(t,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(t,"__esModule",{value:!0})},r.t=function(t,e){if(1&e&&(t=r(t)),8&e)return t;if(4&e&&"object"==typeof t&&t&&t.__esModule)return t;var i=Object.create(null);if(r.r(i),Object.defineProperty(i,"default",{enumerable:!0,value:t}),2&e&&"string"!=typeof t)for(var n in t)r.d(i,n,function(e){return t[e]}.bind(null,n));return i},r.n=function(t){var e=t&&t.__esModule?function(){return t.default}:function(){return t};return r.d(e,"a",e),e},r.o=function(t,e){return Object.prototype.hasOwnProperty.call(t,e)},r.p="",r(r.s=9)}([function(t,e,r){t.exports=r(8)},function(t,e){function r(t,e,r,i,n,s,a){try{var o=t[s](a),u=o.value}catch(t){return void r(t)}o.done?e(u):Promise.resolve(u).then(i,n)}t.exports=function(t){return function(){var e=this,i=arguments;return new Promise((function(n,s){var a=t.apply(e,i);function o(t){r(a,n,s,o,u,"next",t)}function u(t){r(a,n,s,o,u,"throw",t)}o(void 0)}))}}},function(t,e,r){var i=r(5),n=r(6),s=r(7);t.exports=function(t,e){return i(t)||n(t,e)||s()}},function(t,e){t.exports=function(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}},function(t,e){function r(t,e){for(var r=0;r<e.length;r++){var i=e[r];i.enumerable=i.enumerable||!1,i.configurable=!0,"value"in i&&(i.writable=!0),Object.defineProperty(t,i.key,i)}}t.exports=function(t,e,i){return e&&r(t.prototype,e),i&&r(t,i),t}},function(t,e){t.exports=function(t){if(Array.isArray(t))return t}},function(t,e){t.exports=function(t,e){if(Symbol.iterator in Object(t)||"[object Arguments]"===Object.prototype.toString.call(t)){var r=[],i=!0,n=!1,s=void 0;try{for(var a,o=t[Symbol.iterator]();!(i=(a=o.next()).done)&&(r.push(a.value),!e||r.length!==e);i=!0);}catch(t){n=!0,s=t}finally{try{i||null==o.return||o.return()}finally{if(n)throw s}}return r}}},function(t,e){t.exports=function(){throw new TypeError("Invalid attempt to destructure non-iterable instance")}},function(t,e,r){var i=function(t){"use strict";var e=Object.prototype,r=e.hasOwnProperty,i="function"==typeof Symbol?Symbol:{},n=i.iterator||"@@iterator",s=i.asyncIterator||"@@asyncIterator",a=i.toStringTag||"@@toStringTag";function o(t,e,r,i){var n=e&&e.prototype instanceof h?e:h,s=Object.create(n.prototype),a=new k(i||[]);return s._invoke=function(t,e,r){var i="suspendedStart";return function(n,s){if("executing"===i)throw new Error("Generator is already running");if("completed"===i){if("throw"===n)throw s;return L()}for(r.method=n,r.arg=s;;){var a=r.delegate;if(a){var o=g(a,r);if(o){if(o===c)continue;return o}}if("next"===r.method)r.sent=r._sent=r.arg;else if("throw"===r.method){if("suspendedStart"===i)throw i="completed",r.arg;r.dispatchException(r.arg)}else"return"===r.method&&r.abrupt("return",r.arg);i="executing";var h=u(t,e,r);if("normal"===h.type){if(i=r.done?"completed":"suspendedYield",h.arg===c)continue;return{value:h.arg,done:r.done}}"throw"===h.type&&(i="completed",r.method="throw",r.arg=h.arg)}}}(t,r,a),s}function u(t,e,r){try{return{type:"normal",arg:t.call(e,r)}}catch(t){return{type:"throw",arg:t}}}t.wrap=o;var c={};function h(){}function l(){}function f(){}var p={};p[n]=function(){return this};var d=Object.getPrototypeOf,v=d&&d(d(W([])));v&&v!==e&&r.call(v,n)&&(p=v);var y=f.prototype=h.prototype=Object.create(p);function m(t){["next","throw","return"].forEach((function(e){t[e]=function(t){return this._invoke(e,t)}}))}function w(t,e){var i;this._invoke=function(n,s){function a(){return new e((function(i,a){!function i(n,s,a,o){var c=u(t[n],t,s);if("throw"!==c.type){var h=c.arg,l=h.value;return l&&"object"==typeof l&&r.call(l,"__await")?e.resolve(l.__await).then((function(t){i("next",t,a,o)}),(function(t){i("throw",t,a,o)})):e.resolve(l).then((function(t){h.value=t,a(h)}),(function(t){return i("throw",t,a,o)}))}o(c.arg)}(n,s,i,a)}))}return i=i?i.then(a,a):a()}}function g(t,e){var r=t.iterator[e.method];if(void 0===r){if(e.delegate=null,"throw"===e.method){if(t.iterator.return&&(e.method="return",e.arg=void 0,g(t,e),"throw"===e.method))return c;e.method="throw",e.arg=new TypeError("The iterator does not provide a 'throw' method")}return c}var i=u(r,t.iterator,e.arg);if("throw"===i.type)return e.method="throw",e.arg=i.arg,e.delegate=null,c;var n=i.arg;return n?n.done?(e[t.resultName]=n.value,e.next=t.nextLoc,"return"!==e.method&&(e.method="next",e.arg=void 0),e.delegate=null,c):n:(e.method="throw",e.arg=new TypeError("iterator result is not an object"),e.delegate=null,c)}function b(t){var e={tryLoc:t[0]};1 in t&&(e.catchLoc=t[1]),2 in t&&(e.finallyLoc=t[2],e.afterLoc=t[3]),this.tryEntries.push(e)}function x(t){var e=t.completion||{};e.type="normal",delete e.arg,t.completion=e}function k(t){this.tryEntries=[{tryLoc:"root"}],t.forEach(b,this),this.reset(!0)}function W(t){if(t){var e=t[n];if(e)return e.call(t);if("function"==typeof t.next)return t;if(!isNaN(t.length)){var i=-1,s=function e(){for(;++i<t.length;)if(r.call(t,i))return e.value=t[i],e.done=!1,e;return e.value=void 0,e.done=!0,e};return s.next=s}}return{next:L}}function L(){return{value:void 0,done:!0}}return l.prototype=y.constructor=f,f.constructor=l,f[a]=l.displayName="GeneratorFunction",t.isGeneratorFunction=function(t){var e="function"==typeof t&&t.constructor;return!!e&&(e===l||"GeneratorFunction"===(e.displayName||e.name))},t.mark=function(t){return Object.setPrototypeOf?Object.setPrototypeOf(t,f):(t.__proto__=f,a in t||(t[a]="GeneratorFunction")),t.prototype=Object.create(y),t},t.awrap=function(t){return{__await:t}},m(w.prototype),w.prototype[s]=function(){return this},t.AsyncIterator=w,t.async=function(e,r,i,n,s){void 0===s&&(s=Promise);var a=new w(o(e,r,i,n),s);return t.isGeneratorFunction(r)?a:a.next().then((function(t){return t.done?t.value:a.next()}))},m(y),y[a]="Generator",y[n]=function(){return this},y.toString=function(){return"[object Generator]"},t.keys=function(t){var e=[];for(var r in t)e.push(r);return e.reverse(),function r(){for(;e.length;){var i=e.pop();if(i in t)return r.value=i,r.done=!1,r}return r.done=!0,r}},t.values=W,k.prototype={constructor:k,reset:function(t){if(this.prev=0,this.next=0,this.sent=this._sent=void 0,this.done=!1,this.delegate=null,this.method="next",this.arg=void 0,this.tryEntries.forEach(x),!t)for(var e in this)"t"===e.charAt(0)&&r.call(this,e)&&!isNaN(+e.slice(1))&&(this[e]=void 0)},stop:function(){this.done=!0;var t=this.tryEntries[0].completion;if("throw"===t.type)throw t.arg;return this.rval},dispatchException:function(t){if(this.done)throw t;var e=this;function i(r,i){return a.type="throw",a.arg=t,e.next=r,i&&(e.method="next",e.arg=void 0),!!i}for(var n=this.tryEntries.length-1;n>=0;--n){var s=this.tryEntries[n],a=s.completion;if("root"===s.tryLoc)return i("end");if(s.tryLoc<=this.prev){var o=r.call(s,"catchLoc"),u=r.call(s,"finallyLoc");if(o&&u){if(this.prev<s.catchLoc)return i(s.catchLoc,!0);if(this.prev<s.finallyLoc)return i(s.finallyLoc)}else if(o){if(this.prev<s.catchLoc)return i(s.catchLoc,!0)}else{if(!u)throw new Error("try statement without catch or finally");if(this.prev<s.finallyLoc)return i(s.finallyLoc)}}}},abrupt:function(t,e){for(var i=this.tryEntries.length-1;i>=0;--i){var n=this.tryEntries[i];if(n.tryLoc<=this.prev&&r.call(n,"finallyLoc")&&this.prev<n.finallyLoc){var s=n;break}}s&&("break"===t||"continue"===t)&&s.tryLoc<=e&&e<=s.finallyLoc&&(s=null);var a=s?s.completion:{};return a.type=t,a.arg=e,s?(this.method="next",this.next=s.finallyLoc,c):this.complete(a)},complete:function(t,e){if("throw"===t.type)throw t.arg;return"break"===t.type||"continue"===t.type?this.next=t.arg:"return"===t.type?(this.rval=this.arg=t.arg,this.method="return",this.next="end"):"normal"===t.type&&e&&(this.next=e),c},finish:function(t){for(var e=this.tryEntries.length-1;e>=0;--e){var r=this.tryEntries[e];if(r.finallyLoc===t)return this.complete(r.completion,r.afterLoc),x(r),c}},catch:function(t){for(var e=this.tryEntries.length-1;e>=0;--e){var r=this.tryEntries[e];if(r.tryLoc===t){var i=r.completion;if("throw"===i.type){var n=i.arg;x(r)}return n}}throw new Error("illegal catch attempt")},delegateYield:function(t,e,r){return this.delegate={iterator:W(t),resultName:e,nextLoc:r},"next"===this.method&&(this.arg=void 0),c}},t}(t.exports);try{regeneratorRuntime=i}catch(t){Function("r","regeneratorRuntime = r")(i)}},function(t,e,r){"use strict";r.r(e);var i=r(2),n=r.n(i),s=r(0),a=r.n(s),o=r(1),u=r.n(o),c=r(3),h=r.n(c),l=r(4),f=r.n(l),p=new(function(){function t(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{},r=e.useCache,i=void 0===r||r,n=e.cacheDuration,s=void 0===n?60:n;h()(this,t),this.useCache=Boolean(i),this.cacheDuration=Number(s),this.urls={base:window.location.origin,all:"/wishlist.php",add:"/wishlist.php?action=addwishlist",edit:"/wishlist.php?action=editwishlist",delete:"/wishlist.php?action=deletewishlist",view:"/wishlist.php?action=viewwishlistitems",add_item:"/wishlist.php?action=add",delete_item:"/wishlist.php?action=remove",share:"/wishlist.php?action=sharewishlist"},this.events={createWishlist:"createWishlist",deleteWishlist:"deleteWishlist",updateWishlist:"updateWishlist",addWishlistItem:"addWishlistItem",deleteWishlistItem:"deleteWishlistItem"},this.eventListeners={},this.makeRequest=stencilUtils.api.search.makeRequest,this.wishlistsLoaded=!1,this.wishlists={}}var e,r,i,s,o,c,l,p,d,v,y;return f()(t,[{key:"init",value:(y=u()(a.a.mark((function t(){var e,r,i,n=arguments;return a.a.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(e=n.length>0&&void 0!==n[0]?n[0]:{},r=e.wishlists,(i=e.urls)&&i.account&&i.account.wishlists&&Object.assign(this.urls,i.account.wishlists),this.useCache&&this.getAllWishlistsFromCache(),!Array.isArray(r)){t.next=7;break}this.setWishlists(r),t.next=9;break;case 7:return t.next=9,this.getAllWishlists(!0);case 9:case"end":return t.stop()}}),t,this)}))),function(){return y.apply(this,arguments)})},{key:"getAllWishlists",value:(v=u()(a.a.mark((function t(){var e,r,i=arguments;return a.a.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(i.length>0&&void 0!==i[0]&&i[0]||!this.useCache||!this.wishlistsLoaded){t.next=5;break}if(!(e=this.getAllWishlistsFromCache())){t.next=5;break}return t.abrupt("return",e);case 5:return t.next=7,this.request("all");case 7:return r=t.sent,t.abrupt("return",r&&r.customer&&r.customer.wishlists);case 9:case"end":return t.stop()}}),t,this)}))),function(){return v.apply(this,arguments)})},{key:"getWishlist",value:(d=u()(a.a.mark((function t(e){var r,i,n=arguments;return a.a.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n.length>1&&void 0!==n[1]&&n[1]||!this.useCache){t.next=5;break}if(!(r=this.getWishlistFromCache(e))||!Array.isArray(r.items)){t.next=5;break}return t.abrupt("return",r);case 5:return t.next=7,this.request(["view",{wishlistid:e}]);case 7:return i=t.sent,t.abrupt("return",i&&i.wishlist);case 9:case"end":return t.stop()}}),t,this)}))),function(t){return d.apply(this,arguments)})},{key:"createWishlist",value:(p=u()(a.a.mark((function t(e){var r,i,n,s,o,u,c,h;return a.a.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=e.name,i=e.is_public,n=e.product_id,s=["add"],n&&s.push({product_id:n}),o={submit:"",wishlistname:r},i&&(o.publicwishlist="on"),t.next=7,this.request(s,o);case 7:return u=t.sent,n?c=u&&u.wishlist:(h=u&&u.customer&&Array.isArray(u.customer.wishlists)&&u.customer.wishlists.filter((function(t){return t.name===r})),c=Array.isArray(h)&&h.reduce((function(t,e){return t.id>e.id?t:e}))),this.emitEvent(this.events.createWishlist,{wishlist:c}),t.abrupt("return",c);case 11:case"end":return t.stop()}}),t,this)}))),function(t){return p.apply(this,arguments)})},{key:"deleteWishlist",value:(l=u()(a.a.mark((function t(e){return a.a.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,this.request(["delete",{wishlistid:e}],{});case 2:this.emitEvent(this.events.deleteWishlist,{wishlistid:e});case 3:case"end":return t.stop()}}),t,this)}))),function(t){return l.apply(this,arguments)})},{key:"updateWishlist",value:(c=u()(a.a.mark((function t(e,r){var i,n,s,o,u;return a.a.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return i=r.name,n=r.is_public,s={submit:"",wishlistname:i},n&&(s.publicwishlist="on"),t.next=5,this.request(["edit",{wishlistid:e}],s);case 5:return o=t.sent,u=o&&o.customer&&Array.isArray(o.customer.wishlists)&&o.customer.wishlists.find((function(t){return t.id===Number(e)})),this.emitEvent(this.events.updateWishlist,{wishlist:u}),t.abrupt("return",u);case 9:case"end":return t.stop()}}),t,this)}))),function(t,e){return c.apply(this,arguments)})},{key:"addWishlistItem",value:(o=u()(a.a.mark((function t(e,r){var i,n,s;return a.a.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,this.request(["add_item",{wishlistid:e,product_id:r}],{});case 2:return i=t.sent,n=i&&i.wishlist,s=n&&Array.isArray(n.items)&&n.items.find((function(t){return t.product_id===Number(r)})),this.emitEvent(this.events.addWishlistItem,{wishlist:n,item:s}),t.abrupt("return",s);case 7:case"end":return t.stop()}}),t,this)}))),function(t,e){return o.apply(this,arguments)})},{key:"deleteWishlistItem",value:(s=u()(a.a.mark((function t(e,r){return a.a.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,this.request(["delete_item",{wishlistid:e,item_id:r}],{});case 2:this.emitEvent(this.events.deleteWishlistItem,{wishlistid:e,item_id:r});case 3:case"end":return t.stop()}}),t,this)}))),function(t,e){return s.apply(this,arguments)})},{key:"getAllWishlistsDetails",value:(i=u()(a.a.mark((function t(){var e,r=this;return a.a.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,this.getAllWishlists();case 2:return e=t.sent,t.next=5,Promise.all(e.map((function(t){return r.getWishlist(t.id)})));case 5:return t.abrupt("return",Object.values(this.wishlists));case 6:case"end":return t.stop()}}),t,this)}))),function(){return i.apply(this,arguments)})},{key:"isProductInAnyWishlist",value:(r=u()(a.a.mark((function t(e){var r,i;return a.a.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,this.getAllWishlistsDetails();case 2:r=t.sent,i=0;case 4:if(!(i<r.length)){t.next=12;break}if(!r[i].items.find((function(t){return t.product_id===Number(e)}))){t.next=9;break}return t.abrupt("return",!0);case 9:i++,t.next=4;break;case 12:return t.abrupt("return",!1);case 13:case"end":return t.stop()}}),t,this)}))),function(t){return r.apply(this,arguments)})},{key:"getUrl",value:function(t,e){if(Array.isArray(t)){var r=t,i=n()(r,2);t=i[0],e=i[1]}var s=new URL(this.urls[t],this.urls.base);return e&&Object.keys(e).forEach((function(t){s.searchParams.append(t,e[t])})),s.toString()}},{key:"request",value:(e=u()(a.a.mark((function t(e,r){var i,n,s,o=this;return a.a.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return i=this.getUrl(e),s={template:"custom/json-this"},"POST"==(n=void 0===r?"GET":"POST")&&(s.params=r),t.abrupt("return",new Promise((function(t,e){o.makeRequest(i,n,s,!1,(function(r,i){if(r)e(r);else try{var n=JSON.parse(i);n&&n.customer&&Array.isArray(n.customer.wishlists)&&o.setWishlists(n.customer.wishlists),n&&n.wishlist&&Array.isArray(n.wishlist.items)&&o.setWishlistDetails(n.wishlist),t(n)}catch(t){e(t)}}))})));case 5:case"end":return t.stop()}}),t,this)}))),function(t,r){return e.apply(this,arguments)})},{key:"setWishlists",value:function(t){var e=this;t.forEach((function(t){return e.setWishlistDetails(t)}));var r=t.reduce((function(t,e){return t.push(e.id),t}),[]);Object.keys(this.wishlists).forEach((function(t){-1===r.indexOf(Number(t))&&delete e.wishlists[t]})),this.purgeWishlistsCache(r),this.wishlistsLoaded=!0}},{key:"setWishlistDetails",value:function(t){void 0===this.wishlists[t.id]?this.wishlists[t.id]=t:Object.assign(this.wishlists[t.id],t),Array.isArray(t.items)&&"number"==typeof this.wishlists[t.id].num_items&&(this.wishlists[t.id].num_items=t.items.length),this.saveWishlistInCache(this.wishlists[t.id])}},{key:"purgeWishlistsCache",value:function(t){var e=Object.keys(localStorage).filter((function(t){return t.startsWith("wishlist-")})),r=/^wishlist-(\d+)$/;e.forEach((function(e){var i=e.match(r);if(i){var n=i[1];-1===t.indexOf(Number(n))&&localStorage.removeItem(e)}}))}},{key:"saveWishlistInCache",value:function(t){var e="wishlist-".concat(t.id),r=Date.now();localStorage.setItem(e,JSON.stringify({created_at:r,wishlist:t}))}},{key:"getAllWishlistsFromCache",value:function(){var t=this,e=Object.keys(localStorage).filter((function(t){return t.startsWith("wishlist-")})),r=/^wishlist-(\d+)$/,i=[];if(e.forEach((function(e){var n=e.match(r);if(n){var s=n[1],a=t.getWishlistFromCache(s);a&&i.push(a)}})),i.length>0)return i}},{key:"getWishlistFromCache",value:function(t){var e,r="wishlist-".concat(t),i=localStorage.getItem(r);if("string"==typeof i){try{e=JSON.parse(i)}catch(t){console.error("Invalid '".concat(r,"' entry at localStorage"))}return e&&e.created_at&&!this.isExpired(e)&&e.wishlist&&e.wishlist.id?(this.setWishlistDetails(e.wishlist),e.wishlist):void 0}}},{key:"isExpired",value:function(t){var e=t.created_at;return Date.now()-e>60*this.cacheDuration*1e3}},{key:"on",value:function(t,e){if(Array.isArray(this.eventListeners[t])||(this.eventListeners[t]=[]),"function"!=typeof e)throw new Error("Callback must be a function.");this.eventListeners[t].push(e)}},{key:"off",value:function(t,e){if(Array.isArray(this.eventListeners[t])){var r=this.eventListeners[t].indexOf(e);r>-1&&this.eventListeners[t].splice(r,1)}}},{key:"emitEvent",value:function(t,e){var r=this;Array.isArray(this.eventListeners[t])&&this.eventListeners[t].forEach((function(t){t.call(r,e)}))}}]),t}());e.default=p}])}));