/*!! * VtexUtils.js v1.4.7 * https://github.com/zeindelf/vtex-utils * * Copyright (c) 2017-2018 Zeindelf * Released under the MIT license * * Date: 2018-03-31T23:21:37.450Z */ !function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):(e.VTEX=e.VTEX||{},e.VTEX.VtexUtils=t())}(this,function(){"use strict";var e="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e},t=function(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")},r=function(){function e(e,t){for(var r=0;r0},isEmail:function(e){return/^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i.test(e)},isEmpty:function(e){if(this.isObject(e)){var t=Object.getOwnPropertyNames(e).length;return!!(0===t||1===t&&this.isArray(e)||2===t&&this.isArguments(e))}return""===e},isError:function(e){return"[object Error]"===i.call(e)},isFunction:function(e){return"[object Function]"===i.call(e)||"function"==typeof e},isJson:function(e){try{var t=JSON.parse(e);return this.isObject(t)}catch(e){}return!1},isNull:function(e){return null===e},isNumber:function(e){var t=Number.isNaN||window.isNaN;return"number"==typeof e&&!t(e)},isObject:function(t){return"object"===(void 0===t?"undefined":e(t))&&null!==t},isObjectEmpty:function(e){if(!this.isObject(e))return!1;for(var t in e)if({}.hasOwnProperty.call(e,t))return!1;return!0},isPlainObject:function(e){if(!this.isObject(e))return!1;try{var t=e.constructor,r=t.prototype;return t&&r&&hasOwnProperty.call(r,"isPrototypeOf")}catch(e){return!1}},isRegexp:function(e){return"[object RegExp]"===i.call(e)},isSameType:function(e,t){return i.call(e)===i.call(t)},isString:function(e){return"[object String]"===i.call(e)},isUndefined:function(e){return void 0===e}},a={arrayUnique:function(e){return e.filter(function(e,t,r){return r.indexOf(e)===t})},capitalize:function(e){return e.replace(/(?:^|\s)\S/g,function(e){return e.toUpperCase()})},chunk:function(e,t){t=Math.max(t,0);var r=null===e?0:e.length;if(!r||t<1)return[];for(var n=0,i=0,o=new Array(Math.ceil(r/t));n=0},debounce:function(e,t,r){var n=void 0,i=void 0,a=void 0,s=void 0,u=void 0,c=void 0,f=0,l=!1,g=!1,h=!0,v=!t&&0!==t&&"function"==typeof window.requestAnimationFrame;if("function"!=typeof e)throw new TypeError("Expected a function");function p(t){var r=n,o=i;return n=i=void 0,f=t,s=e.apply(o,r)}function d(e,t){return v?window.requestAnimationFrame(e):setTimeout(e,t)}function m(e){var r=e-c;return void 0===c||r>=t||r<0||g&&e-f>=a}function y(){var e,r,n,i=Date.now();if(m(i))return b(i);u=d(y,(r=(e=i)-f,n=t-(e-c),g?Math.min(n,a-r):n))}function b(e){return u=void 0,h&&n?p(e):(n=i=void 0,s)}function A(){for(var e,r=Date.now(),o=m(r),a=arguments.length,h=Array(a),v=0;v/g,">").replace(/\//g,"/").replace(/\\/g,"\").replace(/`/g,"`")},extend:function(e){for(var t=arguments.length,r=Array(t>1?t-1:0),n=1;n0){if(Object.assign)return Object.assign.apply(Object,[e].concat(function(e){if(Array.isArray(e)){for(var t=0,r=Array(e.length);t0;i--)t=Math.floor(Math.random()*(i+1)),r=e[i],n[i]=e[t],n[t]=r;return n},slice:function(e,t,r){var n=null==e?0:e.length;if(!n)return[];(t=null==t?0:t)<0&&(t=-t>n?0:n+t),(r=(r=void 0===r?n:r)>n?n:r)<0&&(r+=n),n=t>r?0:r-t>>>0,t>>>=0;for(var i=-1,o=new Array(n);++i9007199254740991)return[];for(var n=-1,i=Math.min(e,r),o=new Array(i);++n").replace(///g,"/").replace(/\/g,"\\").replace(/`/g,"`")},unserialize:function(e){if((e=o.isString(e)?e:window.location.href).indexOf("?")<0)return{};for(var t={},r=(e=0===e.indexOf("?")?e.substr(1):e.slice(e.indexOf("?")+1)).split("&"),n=0,i=r.length;n0?"\\D":"$")+")";return i+(e=(1*(e/=100)).toFixed(Math.max(0,~~n))).replace(".",r||",").replace(new RegExp(a,"g"),"$&"+(t||"."))},getOriginalImage:function(e){return o.isString(e)?e.replace(/(ids\/[0-9]+)-([0-9-]+)\//,"$1/"):e},getResizedImage:function(e,t,r){return o.isUndefined(t)||o.isUndefined(r)||!o.isString(e)?e:(t=Math.round(t),r=Math.round(r),(e=e.replace(/(?:ids\/[0-9]+)-([0-9]+)-([0-9]+)\//,function(e,n,i){return e.replace("-"+n+"-"+i,"-"+t+"-"+r)})).replace(/(ids\/[0-9]+)\//,"$1-"+t+"-"+r+"/"))},getResizeImageByRatio:function(e,t,r,n){var i=a.resizeImageByRatio(t,r,n);return this.getResizedImage(e,i.width,i.height)},getServerTime:function(e){return $.ajax({url:"/no-cache/HoraAtualServidor.aspx",type:"get"}).then(function(t){var r=t.match(/([0-9]+):([0-5][0-9]):([0-5][0-9])/)[0],n=parseInt(t.match(/[a-z]{3} ([0-9]{1,2})/)[1]),i=["jan","fev","mar","abr","mai","jun","jul","ago","set","out","nov","dez"].indexOf(t.match(/[a-z]{3}/)[0])+1,a=parseInt(t.match(/[0-9]{4}/)[0]);n<10&&(n="0"+n),i<10&&(i="0"+i),o.isFunction(e)&&e.call(null,new Date(a+"/"+i+"/"+n+" "+r))})},getCategories:function(e,t){return $.Deferred(function(r){return $.ajax({type:"get",url:"/api/catalog_system/pub/category/tree/"+(t||50),dataType:"json",headers:{accept:"application/json",contentType:"application/json; charset=utf-8"}}).done(function(t){o.isUndefined(e)?r.resolve(t):r.resolve(a.objectSearch(t,{id:e}))}).fail(function(e){return r.reject(e)})}).promise()},replaceBreakLines:function(e){return e=e.replace?e.replace(/(?:\r\n|\r|\n)/g,"
"):""},checkLogin:function(){return $.Deferred(function(e){return $.ajax({type:"get",url:"/no-cache/profileSystem/getProfile"}).done(function(t){o.isUndefined(t.IsUserDefined)||!t.IsUserDefined?e.reject(t):e.resolve(t)}).fail(function(t){return e.reject(t)})}).promise()},openPopupLogin:function(e,t){e=!!o.isBoolean(e)&&e,t=o.isString(t)?t:"/",t=e?window.location.href:t,vtexid.start({returnUrl:t})},addToCart:function(e,t,r){if(!o.isArray(e))throw new TypeError("Items must be an Array of Object(s) with item(s) to add, e.g. var items = [{id: 123, quantity: 1, seller: '1'}, {id: 321, quantity: 2, seller: '1'}]");if(a.length(e)<1)throw new Error("Items can't be an empty Array.");return t=o.isUndefined(t)?null:t,r=o.isUndefined?1:r,$.Deferred(function(n){return vtexjs.checkout.getOrderForm().done(function(){return vtexjs.checkout.addToCart(e,t,r).done(function(e){return n.resolve(e)}).fail(function(e){return n.reject()})}).fail(function(e){return n.reject(e)})}).promise()},clearCart:function(){return $.Deferred(function(e){vtexjs.checkout.getOrderForm().done(function(t){return t.items.length?vtexjs.checkout.removeAllItems(t.items).done(function(t){return e.resolve(t)}):e.resolve(t)}).fail(function(t){return e.reject(t)})}).promise()}},u=arguments;"object"!==("undefined"==typeof window?"undefined":e(window))&&(global.window=global,global.window.navigator={}),"rivets"in window&&(rivets.formatters["!"]=function(e){return!e},rivets.formatters.eq=function(e,t){return e===t},rivets.formatters.neq=function(e,t){return e!==t},rivets.formatters.gt=function(e,t){return e>t},rivets.formatters.gte=function(e,t){return e>=t},rivets.formatters.lt=function(e,t){return e_.length(this._area)&&(n--,r--)}return t||this},keys:function(e){return this.each(function(e,t){t.push(e)},e||[])},get:function(e,t){var r=_.get(this._area,this._in(e));return null!==r?_.parse(r):t||r},getAll:function(e){return this.each(function(e,t){t[e]=this.get(e)},e||{})},transact:function(e,t,r){var n=this.get(e,r),i=t(n);return this.set(e,void 0===i?n:i),this},set:function(e,t,r){var n=this.get(e);return null!=n&&!1===r?t:_.set(this._area,this._in(e),_.stringify(t),r)||n},setAll:function(e,t){var r,n;for(var i in e)n=e[i],this.set(i,n,t)!==n&&(r=!0);return r},add:function(t,r){var n=this.get(t);if(n instanceof Array)r=n.concat(r);else if(null!==n){var i=void 0===n?"undefined":e(n);if(i===(void 0===r?"undefined":e(r))&&"object"===i){for(var o in r)n[o]=r[o];r=n}else r=n+r}return _.set(this._area,this._in(t),_.stringify(r)),r},remove:function(e){var t=this.get(e);return _.remove(this._area,this._in(e)),t},clear:function(){return this._ns?this.each(function(e){_.remove(this._area,this._in(e))},1):_.clear(this._area),this},clearAll:function(){var e=this._area;for(var t in _.areas)_.areas.hasOwnProperty(t)&&(this._area=_.areas[t],this.clear());return this._area=e,this},_in:function(e){return"string"!=typeof e&&(e=_.stringify(e)),this._ns?this._ns+e:e},_out:function(e){return this._ns?e&&0===e.indexOf(this._ns)?e.substring(this._ns.length):void 0:e}},storageAPI:{length:0,has:function(e){return this.items.hasOwnProperty(e)},key:function(e){var t=0;for(var r in this.items)if(this.has(r)&&e===t++)return r},setItem:function(e,t){this.has(e)||this.length++,this.items[e]=t},removeItem:function(e){this.has(e)&&(delete this.items[e],this.length--)},getItem:function(e){return this.has(e)?this.items[e]:null},clear:function(){for(var e in this.items)this.removeItem(e)},toString:function(){return this.length+" items in "+this.name+"Storage"}}},k=_.Store("local",function(){try{return localStorage}catch(e){}}());k.local=k,k._=_,k.area("session",function(){try{return sessionStorage}catch(e){}}()),c=k,f=k._,l=f.set,g=f.get,h=f.remove,v=f.key,p=f.length,d=f.clear,f.overflow=function(e,t){var r=e===f.areas.local?"+local+":e===f.areas.session&&"+session+";if(r){var n=f.areas[r];return t&&!n?n=c.area(r)._area:!1===t&&(delete f.areas[r],delete c[r]),n}},f.set=function(e,t,r){try{l.apply(this,arguments)}catch(n){if("QUOTA_EXCEEDED_ERR"===n.name||"NS_ERROR_DOM_QUOTA_REACHED"===n.name||-1!==n.toString().indexOf("QUOTA_EXCEEDED_ERR")||-1!==n.toString().indexOf("QuotaExceededError"))return f.set(f.overflow(e,!0),t,r);throw n}},f.get=function(e,t){var r=f.overflow(e);return r&&g.call(this,r,t)||g.apply(this,arguments)},f.remove=function(e,t){var r=f.overflow(e);r&&h.call(this,r,t),h.apply(this,arguments)},f.key=function(e,t){var r=f.overflow(e);if(r){var n=p.call(this,e);if(t>=n){t-=n;for(var i=0,o=p.call(this,r);i=n?e.removeItem(t):r},m.get=function(e,t){var r=m.cache(e,t);return void 0===r?null:r},m.set=function(e,t,r,n){try{n&&(r=y+(m.when()+n)+";"+r),w(e,t,r)}catch(r){if("QUOTA_EXCEEDED_ERR"===r.name||"NS_ERROR_DOM_QUOTA_REACHED"===r.name){for(var i=!1,o=0,a=e.length;o1?t-1:0),n=1;n