diff --git a/dist/smartbanner.js b/dist/smartbanner.js index 54d865e0..1db53ade 100644 --- a/dist/smartbanner.js +++ b/dist/smartbanner.js @@ -1,7 +1,3 @@ -/*! - * smartbanner.js v1.22.0 - * Copyright © 2023 Ain Tohvri, contributors. Licensed under GPL-3.0. - */ (function(){function r(e,n,t){function o(i,f){if(!n[i]){if(!e[i]){var c="function"==typeof require&&require;if(!f&&c)return c(i,!0);if(u)return u(i,!0);var a=new Error("Cannot find module '"+i+"'");throw a.code="MODULE_NOT_FOUND",a}var p=n[i]={exports:{}};e[i][0].call(p.exports,function(r){var n=e[i][1][r];return o(n||r)},p,p.exports,r,e,n,t)}return n[i].exports}for(var u="function"==typeof require&&require,i=0;i 0; @@ -181,18 +177,20 @@ function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == 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, _toPropertyKey(descriptor.key), descriptor); } } function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; } -function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return _typeof(key) === "symbol" ? key : String(key); } -function _toPrimitive(input, hint) { if (_typeof(input) !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (_typeof(res) !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); } +function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(i); } +function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); } var DEFAULT_PLATFORMS = 'android,ios'; var DEFAULT_CLOSE_LABEL = 'Close'; var DEFAULT_BUTTON_LABEL = 'View'; var datas = { originalTop: 'data-smartbanner-original-top', - originalMarginTop: 'data-smartbanner-original-margin-top' + originalMarginTop: 'data-smartbanner-original-margin-top', + smartBannerHeight: 84 }; function handleExitClick(event, self) { self.exit(); event.preventDefault(); + removeFixedMargin(); } function handleClickout(event, self) { self.clickout(); @@ -202,6 +200,42 @@ function handleJQueryMobilePageLoad(event) { setContentPosition(event.data.height); } } + +/** + * https://github.com/ain/smartbanner.js/issues/140 + * Add fixed margin to top of the page + * */ +function addFixedMargin() { + var styleEl = document.createElement("STYLE"); + styleEl.id = "smartbanner_fixed_margin"; + styleEl.innerHTML = "html{margin-top: ".concat(datas.smartBannerHeight, "px !important;}body{margin-top: ").concat(datas.smartBannerHeight, "px !important;}"); + document.body.appendChild(styleEl); +} + +/** + * https://github.com/ain/smartbanner.js/issues/140 + * */ +function removeFixedMargin() { + var styleEl = document.getElementById("smartbanner_fixed_margin"); + if (styleEl) { + styleEl.remove(); + } + var fixedNavBar = findFixedNavBar(); + console.log({ + fixedNavBar: fixedNavBar + }); + console.log("".concat(parseInt(localStorage.getItem("fixedNavBarHeight")), "px")); + if (fixedNavBar) { + fixedNavBar.style.top = '0px'; + } +} +function findFixedNavBar() { + var _window$getComputedSt; + var nav = document.querySelector("nav"); + if (nav && ((_window$getComputedSt = window.getComputedStyle(nav)) === null || _window$getComputedSt === void 0 ? void 0 : _window$getComputedSt.getPropertyValue('position')) === 'fixed') { + return nav; + } +} function addEventListeners(self) { var closeIcon = document.querySelector('.js_smartbanner__exit'); closeIcon.addEventListener('click', function (event) { @@ -408,6 +442,11 @@ var SmartBanner = exports["default"] = /*#__PURE__*/function () { if (!(this.platformEnabled || this.userAgentIncluded)) { return false; } + var fixedNavBar = findFixedNavBar(); + if (fixedNavBar) { + addFixedMargin(datas.smartBannerHeight); + fixedNavBar.style.top = datas.smartBannerHeight + "px"; + } var bannerDiv = document.createElement('div'); document.querySelector('body').prepend(bannerDiv); bannerDiv.outerHTML = this.html; diff --git a/dist/smartbanner.min.js b/dist/smartbanner.min.js index 778aa5c6..c7a5039a 100644 --- a/dist/smartbanner.min.js +++ b/dist/smartbanner.min.js @@ -1,5 +1 @@ -/*! - * smartbanner.js v1.22.0 - * Copyright © 2023 Ain Tohvri, contributors. Licensed under GPL-3.0. - */ -(function(){function r(e,n,t){function o(i,f){if(!n[i]){if(!e[i]){var c="function"==typeof require&&require;if(!f&&c)return c(i,!0);if(u)return u(i,!0);var a=new Error("Cannot find module '"+i+"'");throw a.code="MODULE_NOT_FOUND",a}var p=n[i]={exports:{}};e[i][0].call(p.exports,function(r){var n=e[i][1][r];return o(n||r)},p,p.exports,r,e,n,t)}return n[i].exports}for(var u="function"==typeof require&&require,i=0;i0||/iPhone|iPad|iPod/i.test(userAgent)){return"ios"}}},{key:"userAgentMatchesRegex",value:function userAgentMatchesRegex(regexString){return new RegExp(regexString).test(window.navigator.userAgent)}},{key:"jQueryMobilePage",value:function jQueryMobilePage(){return typeof global.$!=="undefined"&&global.$.mobile!=="undefined"&&document.querySelector(".ui-page")!==null}},{key:"wrapperElement",value:function wrapperElement(){var selector=Detector.jQueryMobilePage()?".ui-page":"html";return document.querySelectorAll(selector)}}]);return Detector}()}).call(this)}).call(this,typeof global!=="undefined"?global:typeof self!=="undefined"?self:typeof window!=="undefined"?window:{})},{}],3:[function(require,module,exports){"use strict";var _smartbanner=_interopRequireDefault(require("./smartbanner.js"));function _interopRequireDefault(obj){return obj&&obj.__esModule?obj:{default:obj}}var smartbanner;window.addEventListener("load",function(){smartbanner=new _smartbanner["default"];if(smartbanner.apiEnabled){window.smartbanner=smartbanner}else{smartbanner.publish()}})},{"./smartbanner.js":5}],4:[function(require,module,exports){"use strict";Object.defineProperty(exports,"__esModule",{value:true});exports["default"]=void 0;function _typeof(o){"@babel/helpers - typeof";return _typeof="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(o){return typeof o}:function(o){return o&&"function"==typeof Symbol&&o.constructor===Symbol&&o!==Symbol.prototype?"symbol":typeof o},_typeof(o)}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;i0}function convertToCamelCase(name){var parts=name.split("-");parts.map(function(part,index){if(index>0){parts[index]=part.charAt(0).toUpperCase()+part.substring(1)}});return parts.join("")}var OptionParser=exports["default"]=function(){function OptionParser(){_classCallCheck(this,OptionParser)}_createClass(OptionParser,[{key:"parse",value:function parse(){var metas=document.getElementsByTagName("meta");var options={};Array.apply(null,metas).forEach(function(meta){var optionName=null;var name=meta.getAttribute("name");var content=meta.getAttribute("content");if(name&&content&&valid(name)&&content.length>0){optionName=name.split(":")[1];if(optionName.indexOf("-")!==-1){optionName=convertToCamelCase(optionName)}options[optionName]=content}});return options}}]);return OptionParser}()},{}],5:[function(require,module,exports){"use strict";Object.defineProperty(exports,"__esModule",{value:true});exports["default"]=void 0;var _optionparser=_interopRequireDefault(require("./optionparser.js"));var _detector=_interopRequireDefault(require("./detector.js"));var _bakery=_interopRequireDefault(require("./bakery.js"));function _interopRequireDefault(obj){return obj&&obj.__esModule?obj:{default:obj}}function _typeof(o){"@babel/helpers - typeof";return _typeof="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(o){return typeof o}:function(o){return o&&"function"==typeof Symbol&&o.constructor===Symbol&&o!==Symbol.prototype?"symbol":typeof o},_typeof(o)}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\n \n
\n
\n
\n
').concat(this.options.title,'
\n
').concat(this.options.author,'
\n
').concat(this.price).concat(this.priceSuffix,'
\n
\n
\n ').concat(this.buttonLabel,"\n ")}},{key:"height",get:function get(){try{return document.querySelector(".js_smartbanner").offsetHeight}catch(error){return 0}}},{key:"platformEnabled",get:function get(){var enabledPlatforms=this.options.enabledPlatforms||DEFAULT_PLATFORMS;return enabledPlatforms&&enabledPlatforms.replace(/\s+/g,"").split(",").indexOf(this.platform)!==-1}},{key:"positioningDisabled",get:function get(){return this.options.disablePositioning==="true"}},{key:"apiEnabled",get:function get(){return this.options.api==="true"}},{key:"userAgentExcluded",get:function get(){if(!this.options.excludeUserAgentRegex){return false}return _detector["default"].userAgentMatchesRegex(this.options.excludeUserAgentRegex)}},{key:"userAgentIncluded",get:function get(){if(!this.options.includeUserAgentRegex){return false}return _detector["default"].userAgentMatchesRegex(this.options.includeUserAgentRegex)}},{key:"hideTtl",get:function get(){return this.options.hideTtl?parseInt(this.options.hideTtl):false}},{key:"hidePath",get:function get(){return this.options.hidePath?this.options.hidePath:"/"}},{key:"publish",value:function publish(){if(Object.keys(this.options).length===0){throw new Error("No options detected. Please consult documentation.")}if(_bakery["default"].baked){return false}if(this.userAgentExcluded){return false}if(!(this.platformEnabled||this.userAgentIncluded)){return false}var bannerDiv=document.createElement("div");document.querySelector("body").prepend(bannerDiv);bannerDiv.outerHTML=this.html;var event=new Event("smartbanner.view");document.dispatchEvent(event);if(!this.positioningDisabled){setContentPosition(this.height)}addEventListeners(this)}},{key:"exit",value:function exit(){removeEventListeners();if(!this.positioningDisabled){restoreContentPosition()}var banner=document.querySelector(".js_smartbanner");document.querySelector("body").removeChild(banner);var event=new Event("smartbanner.exit");document.dispatchEvent(event);_bakery["default"].bake(this.hideTtl,this.hidePath)}},{key:"clickout",value:function clickout(){var event=new Event("smartbanner.clickout");document.dispatchEvent(event)}}]);return SmartBanner}()},{"./bakery.js":1,"./detector.js":2,"./optionparser.js":4}]},{},[3]); \ No newline at end of file +(function(){function r(e,n,t){function o(i,f){if(!n[i]){if(!e[i]){var c="function"==typeof require&&require;if(!f&&c)return c(i,!0);if(u)return u(i,!0);var a=new Error("Cannot find module '"+i+"'");throw a.code="MODULE_NOT_FOUND",a}var p=n[i]={exports:{}};e[i][0].call(p.exports,function(r){var n=e[i][1][r];return o(n||r)},p,p.exports,r,e,n,t)}return n[i].exports}for(var u="function"==typeof require&&require,i=0;i0||/iPhone|iPad|iPod/i.test(userAgent)){return"ios"}}},{key:"userAgentMatchesRegex",value:function userAgentMatchesRegex(regexString){return new RegExp(regexString).test(window.navigator.userAgent)}},{key:"jQueryMobilePage",value:function jQueryMobilePage(){return typeof global.$!=="undefined"&&global.$.mobile!=="undefined"&&document.querySelector(".ui-page")!==null}},{key:"wrapperElement",value:function wrapperElement(){var selector=Detector.jQueryMobilePage()?".ui-page":"html";return document.querySelectorAll(selector)}}]);return Detector}()}).call(this)}).call(this,typeof global!=="undefined"?global:typeof self!=="undefined"?self:typeof window!=="undefined"?window:{})},{}],3:[function(require,module,exports){"use strict";var _smartbanner=_interopRequireDefault(require("./smartbanner.js"));function _interopRequireDefault(obj){return obj&&obj.__esModule?obj:{default:obj}}var smartbanner;window.addEventListener("load",function(){smartbanner=new _smartbanner["default"];if(smartbanner.apiEnabled){window.smartbanner=smartbanner}else{smartbanner.publish()}})},{"./smartbanner.js":5}],4:[function(require,module,exports){"use strict";Object.defineProperty(exports,"__esModule",{value:true});exports["default"]=void 0;function _typeof(o){"@babel/helpers - typeof";return _typeof="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(o){return typeof o}:function(o){return o&&"function"==typeof Symbol&&o.constructor===Symbol&&o!==Symbol.prototype?"symbol":typeof o},_typeof(o)}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;i0}function convertToCamelCase(name){var parts=name.split("-");parts.map(function(part,index){if(index>0){parts[index]=part.charAt(0).toUpperCase()+part.substring(1)}});return parts.join("")}var OptionParser=exports["default"]=function(){function OptionParser(){_classCallCheck(this,OptionParser)}_createClass(OptionParser,[{key:"parse",value:function parse(){var metas=document.getElementsByTagName("meta");var options={};Array.apply(null,metas).forEach(function(meta){var optionName=null;var name=meta.getAttribute("name");var content=meta.getAttribute("content");if(name&&content&&valid(name)&&content.length>0){optionName=name.split(":")[1];if(optionName.indexOf("-")!==-1){optionName=convertToCamelCase(optionName)}options[optionName]=content}});return options}}]);return OptionParser}()},{}],5:[function(require,module,exports){"use strict";Object.defineProperty(exports,"__esModule",{value:true});exports["default"]=void 0;var _optionparser=_interopRequireDefault(require("./optionparser.js"));var _detector=_interopRequireDefault(require("./detector.js"));var _bakery=_interopRequireDefault(require("./bakery.js"));function _interopRequireDefault(obj){return obj&&obj.__esModule?obj:{default:obj}}function _typeof(o){"@babel/helpers - typeof";return _typeof="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(o){return typeof o}:function(o){return o&&"function"==typeof Symbol&&o.constructor===Symbol&&o!==Symbol.prototype?"symbol":typeof o},_typeof(o)}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\n \n
\n
\n
\n
').concat(this.options.title,'
\n
').concat(this.options.author,'
\n
').concat(this.price).concat(this.priceSuffix,'
\n
\n
\n ').concat(this.buttonLabel,"\n ")}},{key:"height",get:function get(){try{return document.querySelector(".js_smartbanner").offsetHeight}catch(error){return 0}}},{key:"platformEnabled",get:function get(){var enabledPlatforms=this.options.enabledPlatforms||DEFAULT_PLATFORMS;return enabledPlatforms&&enabledPlatforms.replace(/\s+/g,"").split(",").indexOf(this.platform)!==-1}},{key:"positioningDisabled",get:function get(){return this.options.disablePositioning==="true"}},{key:"apiEnabled",get:function get(){return this.options.api==="true"}},{key:"userAgentExcluded",get:function get(){if(!this.options.excludeUserAgentRegex){return false}return _detector["default"].userAgentMatchesRegex(this.options.excludeUserAgentRegex)}},{key:"userAgentIncluded",get:function get(){if(!this.options.includeUserAgentRegex){return false}return _detector["default"].userAgentMatchesRegex(this.options.includeUserAgentRegex)}},{key:"hideTtl",get:function get(){return this.options.hideTtl?parseInt(this.options.hideTtl):false}},{key:"hidePath",get:function get(){return this.options.hidePath?this.options.hidePath:"/"}},{key:"publish",value:function publish(){if(Object.keys(this.options).length===0){throw new Error("No options detected. Please consult documentation.")}if(_bakery["default"].baked){return false}if(this.userAgentExcluded){return false}if(!(this.platformEnabled||this.userAgentIncluded)){return false}var fixedNavBar=findFixedNavBar();if(fixedNavBar){addFixedMargin(datas.smartBannerHeight);fixedNavBar.style.top=datas.smartBannerHeight+"px"}var bannerDiv=document.createElement("div");document.querySelector("body").prepend(bannerDiv);bannerDiv.outerHTML=this.html;var event=new Event("smartbanner.view");document.dispatchEvent(event);if(!this.positioningDisabled){setContentPosition(this.height)}addEventListeners(this)}},{key:"exit",value:function exit(){removeEventListeners();if(!this.positioningDisabled){restoreContentPosition()}var banner=document.querySelector(".js_smartbanner");document.querySelector("body").removeChild(banner);var event=new Event("smartbanner.exit");document.dispatchEvent(event);_bakery["default"].bake(this.hideTtl,this.hidePath)}},{key:"clickout",value:function clickout(){var event=new Event("smartbanner.clickout");document.dispatchEvent(event)}}]);return SmartBanner}()},{"./bakery.js":1,"./detector.js":2,"./optionparser.js":4}]},{},[3]); \ No newline at end of file diff --git a/src/smartbanner.js b/src/smartbanner.js index 1baa632c..dd2a5897 100644 --- a/src/smartbanner.js +++ b/src/smartbanner.js @@ -8,12 +8,14 @@ const DEFAULT_BUTTON_LABEL = 'View'; let datas = { originalTop: 'data-smartbanner-original-top', - originalMarginTop: 'data-smartbanner-original-margin-top' + originalMarginTop: 'data-smartbanner-original-margin-top', + smartBannerHeight: 84 }; function handleExitClick(event, self) { self.exit(); event.preventDefault(); + removeFixedMargin(); } function handleClickout(event, self) { @@ -26,6 +28,46 @@ function handleJQueryMobilePageLoad(event) { } } +/** + * https://github.com/ain/smartbanner.js/issues/140 + * Add fixed margin to top of the page for pages with fixed navBar + * */ +function addFixedMargin() { + const styleEl = document.createElement("STYLE"); + styleEl.id = "smartbanner_fixed_margin"; + styleEl.innerHTML = `html{margin-top: ${datas.smartBannerHeight}px !important;}body{margin-top: ${datas.smartBannerHeight}px !important;}`; + document.body.appendChild(styleEl); +} + +/** + * https://github.com/ain/smartbanner.js/issues/140 + * Remove fixed margin to top of the page for pages with fixed navBar + * */ +function removeFixedMargin() { + const styleEl = document.getElementById("smartbanner_fixed_margin"); + if (styleEl) { + styleEl.remove(); + } + const fixedNavBar = findFixedNavBar(); + if (fixedNavBar) { + fixedNavBar.style.top = '0px'; + } +} + +/** + * https://github.com/ain/smartbanner.js/issues/140 + * Find fixed margin to top of the page for pages with fixed navBar + * */ + +function findFixedNavBar() { + const nav = document.querySelector("nav"); + if (nav && window.getComputedStyle(nav)?.getPropertyValue('position') === 'fixed') { + return nav; + } + // Can be more variants of fixed nav bars in
etc. + return null +} + function addEventListeners(self) { let closeIcon = document.querySelector('.js_smartbanner__exit'); closeIcon.addEventListener('click', (event) => handleExitClick(event, self)); @@ -229,6 +271,12 @@ export default class SmartBanner { return false; } + const fixedNavBar = findFixedNavBar(); + if (fixedNavBar) { + addFixedMargin(datas.smartBannerHeight); + fixedNavBar.style.top = datas.smartBannerHeight + "px"; + } + let bannerDiv = document.createElement('div'); document.querySelector('body').prepend(bannerDiv); bannerDiv.outerHTML = this.html;