@shopgate/pwa-common
Version:
Common library for the Shopgate Connect PWA.
22 lines • 6.4 kB
JavaScript
function _typeof(obj){if(typeof Symbol==="function"&&typeof Symbol.iterator==="symbol"){_typeof=function _typeof(obj){return typeof obj;};}else{_typeof=function _typeof(obj){return obj&&typeof Symbol==="function"&&obj.constructor===Symbol&&obj!==Symbol.prototype?"symbol":typeof obj;};}return _typeof(obj);}function _classCallCheck(instance,Constructor){if(!(instance instanceof Constructor)){throw new TypeError("Cannot call a class as a function");}}function _defineProperties(target,props){for(var i=0;i<props.length;i++){var descriptor=props[i];descriptor.enumerable=descriptor.enumerable||false;descriptor.configurable=true;if("value"in descriptor)descriptor.writable=true;Object.defineProperty(target,descriptor.key,descriptor);}}function _createClass(Constructor,protoProps,staticProps){if(protoProps)_defineProperties(Constructor.prototype,protoProps);if(staticProps)_defineProperties(Constructor,staticProps);return Constructor;}function _callSuper(_this,derived,args){function isNativeReflectConstruct(){if(typeof Reflect==="undefined"||!Reflect.construct)return false;if(Reflect.construct.sham)return false;if(typeof Proxy==="function")return true;try{return!Boolean.prototype.valueOf.call(Reflect.construct(Boolean,[],function(){}));}catch(e){return false;}}derived=_getPrototypeOf(derived);return _possibleConstructorReturn(_this,isNativeReflectConstruct()?Reflect.construct(derived,args||[],_getPrototypeOf(_this).constructor):derived.apply(_this,args));}function _possibleConstructorReturn(self,call){if(call&&(_typeof(call)==="object"||typeof call==="function")){return call;}return _assertThisInitialized(self);}function _assertThisInitialized(self){if(self===void 0){throw new ReferenceError("this hasn't been initialised - super() hasn't been called");}return self;}function _getPrototypeOf(o){_getPrototypeOf=Object.setPrototypeOf?Object.getPrototypeOf:function _getPrototypeOf(o){return o.__proto__||Object.getPrototypeOf(o);};return _getPrototypeOf(o);}function _inherits(subClass,superClass){if(typeof superClass!=="function"&&superClass!==null){throw new TypeError("Super expression must either be null or a function");}subClass.prototype=Object.create(superClass&&superClass.prototype,{constructor:{value:subClass,writable:true,configurable:true}});if(superClass)_setPrototypeOf(subClass,superClass);}function _setPrototypeOf(o,p){_setPrototypeOf=Object.setPrototypeOf||function _setPrototypeOf(o,p){o.__proto__=p;return o;};return _setPrototypeOf(o,p);}function _defineProperty(obj,key,value){if(key in obj){Object.defineProperty(obj,key,{value:value,enumerable:true,configurable:true,writable:true});}else{obj[key]=value;}return obj;}import React,{Component}from'react';import PropTypes from'prop-types';import classNames from'classnames';import{embeddedMedia}from'@shopgate/pwa-common/collections';import EmbeddedMedia from"../EmbeddedMedia";import parseHTML from"../../helpers/html/parseHTML";import connect from"./connector";/**
* HtmlSanitizer component.
*/var HtmlSanitizer=/*#__PURE__*/function(_Component){/**
* @param {Object} props The component props.
*/function HtmlSanitizer(props){var _this2;_classCallCheck(this,HtmlSanitizer);_this2=_callSuper(this,HtmlSanitizer,[props]);/**
* If the user tapped a link element, prevent the default behavior.
* @param {Object} event The touchstart event.
*/_defineProperty(_this2,"handleTap",function(event){var linkTag=event.target.closest('a');if(linkTag){var _linkTag$attributes3=linkTag.attributes,_linkTag$attributes4=_linkTag$attributes3===void 0?{}:_linkTag$attributes3,_linkTag$attributes2$7=_linkTag$attributes4.href,_linkTag$attributes2$8=_linkTag$attributes2$7===void 0?{}:_linkTag$attributes2$7,_linkTag$attributes2$9=_linkTag$attributes2$8.value,href=_linkTag$attributes2$9===void 0?'':_linkTag$attributes2$9,_linkTag$attributes2$10=_linkTag$attributes4.target,_linkTag$attributes2$11=_linkTag$attributes2$10===void 0?{}:_linkTag$attributes2$10,_linkTag$attributes2$12=_linkTag$attributes2$11.value,target=_linkTag$attributes2$12===void 0?'':_linkTag$attributes2$12;if(href){event.preventDefault();if(_this2.props.settings.handleClick){_this2.props.settings.handleClick(href,target);}else{_this2.props.navigate(href,target);}}}});_this2.htmlContainer=React.createRef();return _this2;}/**
* Registers the event handler for when the user taps inside the html content.
*/_inherits(HtmlSanitizer,_Component);return _createClass(HtmlSanitizer,[{key:"componentDidMount",value:function componentDidMount(){this.htmlContainer.current.addEventListener('click',this.handleTap,true);embeddedMedia.add(this.htmlContainer.current);}/**
* Only update if the HTML changed.
* @param {Object} nextProps The next props for the component.
* @return {boolean}
*/},{key:"shouldComponentUpdate",value:function shouldComponentUpdate(nextProps){return nextProps.children!==this.props.children||nextProps.comfortCookiesAccepted!==this.props.comfortCookiesAccepted||nextProps.statisticsCookiesAccepted!==this.props.statisticsCookiesAccepted;}/**
* Updates embedded media within the html container.
*/},{key:"componentDidUpdate",value:function componentDidUpdate(){embeddedMedia.add(this.htmlContainer.current);}/**
* Removes the event handler.
*/},{key:"componentWillUnmount",value:function componentWillUnmount(){this.htmlContainer.current.removeEventListener('click',this.handleTap,true);embeddedMedia.remove(this.htmlContainer.current);}},{key:"render",value:/**
* Renders the component.
* @returns {JSX}
*/function render(){var cookieConsentSettings={comfortCookiesAccepted:this.props.comfortCookiesAccepted,statisticsCookiesAccepted:this.props.statisticsCookiesAccepted};var innerHTML={__html:parseHTML(this.props.children,this.props.decode,this.props.settings,this.props.processStyles,cookieConsentSettings)};var Wrapper=this.props.wrapper;return React.createElement(Wrapper,{cookieConsentSettings:cookieConsentSettings},React.createElement("div",{// eslint-disable-next-line react/no-danger
dangerouslySetInnerHTML:innerHTML,ref:this.htmlContainer,className:classNames(this.props.className,'common__html-sanitizer')}));}}]);}(Component);_defineProperty(HtmlSanitizer,"defaultProps",{children:'',className:'',decode:false,processStyles:false,settings:{},wrapper:EmbeddedMedia,comfortCookiesAccepted:false,statisticsCookiesAccepted:false});export default connect(HtmlSanitizer);