Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option smartbanner:parent-element #164

Merged
merged 2 commits into from
Jul 26, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,17 @@ If you want to position smart app banner yourself (e.g. in CSS), you can disable
<meta name="smartbanner:disable-positioning" content="true">
```

#### Define parent element

Additionally to disabling positioning, you might want to place the smart app banner inside a specific container (e.g. parent element). The parent element is defined by a css selector:
Huppys marked this conversation as resolved.
Show resolved Hide resolved
```html
<meta name="smartbanner:disable-positioning" content="true">
<meta name="smartbanner:parent-element" content="#mobile-navigation.banner-container">
Huppys marked this conversation as resolved.
Show resolved Hide resolved
```

If the desired element can't be found by the defined selector the smart app banner will fallback to the document body.
Huppys marked this conversation as resolved.
Show resolved Hide resolved


Huppys marked this conversation as resolved.
Show resolved Hide resolved
### Hide the smartbanner completely

If you want to prevent smartbanner rendering in some html pages, you can add optional `meta` tag:
Expand Down
6 changes: 5 additions & 1 deletion dist/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
<!--<meta name="smartbanner:api" content="true">-->
<!--<meta name="smartbanner:hide-ttl" content="2629746000">-->
<!--<meta name="smartbanner:hide-path" content="/">-->
<!--<meta name="smartbanner:disable-positioning" content="true">-->
<meta name="smartbanner:disable-positioning" content="true">
<meta name="smartbanner:parent-element" content="#container">
Huppys marked this conversation as resolved.
Show resolved Hide resolved
<!-- Enable for all platforms -->
<!--<meta name="smartbanner:include-user-agent-regex" content=".*">-->
<!-- iOS -->
Expand All @@ -31,6 +32,9 @@
</head>
<body>
<h1>&nbsp;</h1>
<header>
<div id="container"></div>
Huppys marked this conversation as resolved.
Show resolved Hide resolved
</header>
<script>
let onLoadHandlerForConflictTesting = function() {};
window.onload = onLoadHandlerForConflictTesting;
Expand Down
8 changes: 7 additions & 1 deletion dist/smartbanner.js
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ var SmartBanner = /*#__PURE__*/function () {
}

var bannerDiv = document.createElement('div');
document.querySelector('body').appendChild(bannerDiv);
this.parentElement.appendChild(bannerDiv);
bannerDiv.outerHTML = this.html;
var event = new Event('smartbanner.view');
document.dispatchEvent(event);
Expand Down Expand Up @@ -468,6 +468,12 @@ var SmartBanner = /*#__PURE__*/function () {
get: function get() {
return this.options.hidePath ? this.options.hidePath : '/';
}
}, {
key: "parentElement",
get: function get() {
var parentElement = this.options.parentElement ? document.querySelector(this.options.parentElement) : null;
return parentElement || document.querySelector('body');
}
}]);

return SmartBanner;
Expand Down
2 changes: 1 addition & 1 deletion dist/smartbanner.min.js

Large diffs are not rendered by default.

8 changes: 7 additions & 1 deletion src/smartbanner.js
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,12 @@ export default class SmartBanner {
return this.options.hidePath ? this.options.hidePath : '/';
}

get parentElement() {
const parentElement = this.options.parentElement ? document.querySelector(this.options.parentElement) : null;

return parentElement || document.querySelector('body');
Huppys marked this conversation as resolved.
Show resolved Hide resolved
}

publish() {
if (Object.keys(this.options).length === 0) {
throw new Error('No options detected. Please consult documentation.');
Expand All @@ -210,7 +216,7 @@ export default class SmartBanner {
}

let bannerDiv = document.createElement('div');
document.querySelector('body').appendChild(bannerDiv);
this.parentElement.appendChild(bannerDiv);
bannerDiv.outerHTML = this.html;
let event = new Event('smartbanner.view');
document.dispatchEvent(event);
Expand Down
77 changes: 72 additions & 5 deletions test/spec/smartbanner_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,36 @@ describe('SmartBanner', function() {
</body>
</html>`;

const SCRIPTS = `<script>window.conclude();</script>`;
const HTML_PARENT_ELEMENT = `<!doctype html>
<html style="margin-top:10px;">
<head>
${HEAD}
<meta name="smartbanner:disable-positioning" content="true">
<meta name="smartbanner:parent-element" content="#smartbanner-parent-container">
</head>
<body>
<header>
<div id="smartbanner-parent-container"></div>
</header>
<div class="ui-page ui-page-active" style="position:absolute; top:12px;"></div>
<div class="ui-page" style="position:absolute; top:13px;"></div>
</body>
</html>`;

const HTML_PARENT_ELEMENT_MISSING = `<!doctype html>
<html style="margin-top:10px;">
<head>
${HEAD}
<meta name="smartbanner:disable-positioning" content="true">
<meta name="smartbanner:parent-element" content="#smartbanner-parent-container">
</head>
<body>
<div class="ui-page ui-page-active" style="position:absolute; top:12px;"></div>
<div class="ui-page" style="position:absolute; top:13px;"></div>
</body>
</html>`;

const SCRIPTS = '<script>window.conclude();</script>';
const SCRIPTS_JQUERY_MOBILE = `<script src="https://code.jquery.com/jquery-1.11.1.min.js"></script>
<script src="https://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.js"></script>
<script>window.conclude();</script>`;
Expand Down Expand Up @@ -874,8 +903,8 @@ describe('SmartBanner', function() {
document.addEventListener('smartbanner.exit', function () {
done();
});
let clickEvent = document.createEvent("HTMLEvents");
clickEvent.initEvent("click", false, true);
let clickEvent = document.createEvent('HTMLEvents');
clickEvent.initEvent('click', false, true);
document.querySelector('.js_smartbanner__exit').dispatchEvent(clickEvent);
});

Expand All @@ -895,8 +924,8 @@ describe('SmartBanner', function() {
document.addEventListener('smartbanner.clickout', function () {
done();
});
let clickEvent = document.createEvent("HTMLEvents");
clickEvent.initEvent("click", false, true);
let clickEvent = document.createEvent('HTMLEvents');
clickEvent.initEvent('click', false, true);
document.querySelector('.js_smartbanner__button').dispatchEvent(clickEvent);
});

Expand Down Expand Up @@ -990,4 +1019,42 @@ describe('SmartBanner', function() {

});

describe('parentElement', function () {

context('desired parent element exists', function () {

before(function() {
const resourceLoader = new jsdom.ResourceLoader({ userAgent: USER_AGENT_ANDROID });
global.window = new JSDOM(HTML_PARENT_ELEMENT, { resources: resourceLoader }).window;
global.document = window.document;
global.getComputedStyle = window.getComputedStyle;
global.Event = window.Event;
smartbanner = new SmartBanner();
smartbanner.publish();
});

it('expect smartbanner to be placed inside defined element', function () {
expect(document.querySelector('#smartbanner-parent-container').childNodes.length).to.eql(1);
expect(document.querySelector('#smartbanner-parent-container .smartbanner')).to.exist;
});
});

context('desired parent element doesnt exist', function () {

before(function() {
const resourceLoader = new jsdom.ResourceLoader({ userAgent: USER_AGENT_ANDROID });
global.window = new JSDOM(HTML_PARENT_ELEMENT_MISSING, { resources: resourceLoader }).window;
global.document = window.document;
global.getComputedStyle = window.getComputedStyle;
global.Event = window.Event;
smartbanner = new SmartBanner();
smartbanner.publish();
});

it('expect smartbanner to be placed inside body element', function () {
expect(document.querySelector('body > .smartbanner')).to.exist;
});
});
});

});