Skip to content

Commit

Permalink
Resolve #167 jQuery Mobile EOL
Browse files Browse the repository at this point in the history
  • Loading branch information
ain committed Jul 26, 2020
1 parent be08c11 commit 2951e53
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 218 deletions.
3 changes: 1 addition & 2 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
"env": {
"browser": true,
"commonjs": true,
"es6": true,
"jquery": true
"es6": true
},
"extends": "eslint:recommended",
"parserOptions": {
Expand Down
54 changes: 8 additions & 46 deletions dist/smartbanner.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ var Bakery = /*#__PURE__*/function () {
exports["default"] = Bakery;

},{}],2:[function(require,module,exports){
(function (global){
"use strict";

Object.defineProperty(exports, "__esModule", {
Expand Down Expand Up @@ -85,16 +84,10 @@ var Detector = /*#__PURE__*/function () {
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 document.querySelectorAll('html');
}
}]);

Expand All @@ -103,7 +96,6 @@ var Detector = /*#__PURE__*/function () {

exports["default"] = Detector;

}).call(this,typeof global !== "undefined" ? global : typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : {})
},{}],3:[function(require,module,exports){
"use strict";

Expand Down Expand Up @@ -223,12 +215,6 @@ function handleClickout(event, self) {
self.clickout();
}

function handleJQueryMobilePageLoad(event) {
if (!this.positioningDisabled) {
setContentPosition(event.data.height);
}
}

function addEventListeners(self) {
var closeIcon = document.querySelector('.js_smartbanner__exit');
closeIcon.addEventListener('click', function (event) {
Expand All @@ -238,16 +224,6 @@ function addEventListeners(self) {
button.addEventListener('click', function (event) {
return handleClickout(event, self);
});

if (_detector["default"].jQueryMobilePage()) {
$(document).on('pagebeforeshow', self, handleJQueryMobilePageLoad);
}
}

function removeEventListeners() {
if (_detector["default"].jQueryMobilePage()) {
$(document).off('pagebeforeshow', handleJQueryMobilePageLoad);
}
}

function setContentPosition(value) {
Expand All @@ -256,23 +232,13 @@ function setContentPosition(value) {
for (var i = 0, l = wrappers.length, wrapper; i < l; i++) {
wrapper = wrappers[i];

if (_detector["default"].jQueryMobilePage()) {
if (wrapper.getAttribute(datas.originalTop)) {
continue;
}

var top = parseFloat(getComputedStyle(wrapper).top);
wrapper.setAttribute(datas.originalTop, isNaN(top) ? 0 : top);
wrapper.style.top = value + 'px';
} else {
if (wrapper.getAttribute(datas.originalMarginTop)) {
continue;
}

var margin = parseFloat(getComputedStyle(wrapper).marginTop);
wrapper.setAttribute(datas.originalMarginTop, isNaN(margin) ? 0 : margin);
wrapper.style.marginTop = value + 'px';
if (wrapper.getAttribute(datas.originalMarginTop)) {
continue;
}

var margin = parseFloat(getComputedStyle(wrapper).marginTop);
wrapper.setAttribute(datas.originalMarginTop, isNaN(margin) ? 0 : margin);
wrapper.style.marginTop = value + 'px';
}
}

Expand All @@ -282,9 +248,7 @@ function restoreContentPosition() {
for (var i = 0, l = wrappers.length, wrapper; i < l; i++) {
wrapper = wrappers[i];

if (_detector["default"].jQueryMobilePage() && wrapper.getAttribute(datas.originalTop)) {
wrapper.style.top = wrapper.getAttribute(datas.originalTop) + 'px';
} else if (wrapper.getAttribute(datas.originalMarginTop)) {
if (wrapper.getAttribute(datas.originalMarginTop)) {
wrapper.style.marginTop = wrapper.getAttribute(datas.originalMarginTop) + 'px';
}
}
Expand Down Expand Up @@ -339,8 +303,6 @@ var SmartBanner = /*#__PURE__*/function () {
}, {
key: "exit",
value: function exit() {
removeEventListeners();

if (!this.positioningDisabled) {
restoreContentPosition();
}
Expand Down
2 changes: 1 addition & 1 deletion dist/smartbanner.min.js

Large diffs are not rendered by default.

8 changes: 1 addition & 7 deletions src/detector.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,7 @@ export default class Detector {
return new RegExp(regexString).test(window.navigator.userAgent);
}

static jQueryMobilePage() {
return typeof global.$ !== 'undefined' && global.$.mobile !== 'undefined' && document.querySelector('.ui-page') !== null;
}

static wrapperElement() {
let selector = Detector.jQueryMobilePage() ? '.ui-page' : 'html';
return document.querySelectorAll(selector);
return document.querySelectorAll('html');
}

}
40 changes: 6 additions & 34 deletions src/smartbanner.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,59 +19,32 @@ function handleClickout(event, self) {
self.clickout();
}

function handleJQueryMobilePageLoad(event) {
if (!this.positioningDisabled) {
setContentPosition(event.data.height);
}
}

function addEventListeners(self) {
let closeIcon = document.querySelector('.js_smartbanner__exit');
closeIcon.addEventListener('click', (event) => handleExitClick(event, self));

let button = document.querySelector('.js_smartbanner__button');
button.addEventListener('click', (event) => handleClickout(event, self));

if (Detector.jQueryMobilePage()) {
$(document).on('pagebeforeshow', self, handleJQueryMobilePageLoad);
}
}

function removeEventListeners() {
if (Detector.jQueryMobilePage()) {
$(document).off('pagebeforeshow', handleJQueryMobilePageLoad);
}
}

function setContentPosition(value) {
let wrappers = Detector.wrapperElement();
for (let i = 0, l = wrappers.length, wrapper; i < l; i++) {
wrapper = wrappers[i];
if (Detector.jQueryMobilePage()) {
if (wrapper.getAttribute(datas.originalTop)) {
continue;
}
let top = parseFloat(getComputedStyle(wrapper).top);
wrapper.setAttribute(datas.originalTop, isNaN(top) ? 0 : top);
wrapper.style.top = value + 'px';
} else {
if (wrapper.getAttribute(datas.originalMarginTop)) {
continue;
}
let margin = parseFloat(getComputedStyle(wrapper).marginTop);
wrapper.setAttribute(datas.originalMarginTop, isNaN(margin) ? 0 : margin);
wrapper.style.marginTop = value + 'px';
if (wrapper.getAttribute(datas.originalMarginTop)) {
continue;
}
let margin = parseFloat(getComputedStyle(wrapper).marginTop);
wrapper.setAttribute(datas.originalMarginTop, isNaN(margin) ? 0 : margin);
wrapper.style.marginTop = value + 'px';
}
}

function restoreContentPosition() {
let wrappers = Detector.wrapperElement();
for (let i = 0, l = wrappers.length, wrapper; i < l; i++) {
wrapper = wrappers[i];
if (Detector.jQueryMobilePage() && wrapper.getAttribute(datas.originalTop)) {
wrapper.style.top = wrapper.getAttribute(datas.originalTop) + 'px';
} else if (wrapper.getAttribute(datas.originalMarginTop)) {
if (wrapper.getAttribute(datas.originalMarginTop)) {
wrapper.style.marginTop = wrapper.getAttribute(datas.originalMarginTop) + 'px';
}
}
Expand Down Expand Up @@ -227,7 +200,6 @@ export default class SmartBanner {
}

exit() {
removeEventListeners();
if (!this.positioningDisabled) {
restoreContentPosition();
}
Expand Down
70 changes: 5 additions & 65 deletions test/spec/detector_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,7 @@ describe('Detector', function() {
const INCLUDE_USER_AGENT_REGEX = '.*iPhone OS [9\\-10].*';

const { JSDOM } = jsdom;
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>`;
const HTML = `<!doctype html><html><head></head><body></body></html>`;
const HTML_WITH_JQUERY_MOBILE = `<!doctype html><html><head></head><body class="ui-page">${SCRIPTS_JQUERY_MOBILE}</body></html>`;

describe('platform', function() {

Expand Down Expand Up @@ -178,71 +174,15 @@ describe('Detector', function() {
});
});

describe('jQueryMobilePage', function() {

context('without jQuery Mobile', function() {

before(function() {
global.window = new JSDOM(HTML).window;
global.document = window.document;
});

it('expected to return false', function() {
expect(Detector.jQueryMobilePage()).to.be.false;
});

});

context('with jQuery Mobile', function() {

before(function(done) {
global.window = new JSDOM(HTML_WITH_JQUERY_MOBILE, { runScripts: 'dangerously', resources: "usable" }).window;
global.window.conclude = function() {
global.document = window.document;
global.$ = window.jQuery;
done();
};
});

it('expected to return true', function(done) {
expect(Detector.jQueryMobilePage()).to.be.true;
done();
});

});
});

describe('marginedElement', function() {

context('without jQuery Mobile', function() {

before(function() {
global.window = new JSDOM(HTML).window;
global.document = window.document;
});

it('expected to return html element as first item of array', function() {
expect(Detector.wrapperElement()[0]).to.eql(document.querySelector('html'));
});

before(function() {
global.window = new JSDOM(HTML).window;
global.document = window.document;
});

context('with jQuery Mobile', function() {

before(function(done) {
global.window = new JSDOM(HTML_WITH_JQUERY_MOBILE, { runScripts: 'dangerously', resources: "usable" }).window;
global.window.conclude = () => {
global.document = window.document;
global.$ = window.jQuery;
done();
};
});

it('expected to return ui-page element as first item of array', function(done) {
expect(Detector.wrapperElement()[0]).to.eql(document.querySelector('.ui-page'));
done();
});

it('expected to return html element as first item of array', function() {
expect(Detector.wrapperElement()[0]).to.eql(document.querySelector('html'));
});
});

Expand Down
Loading

0 comments on commit 2951e53

Please sign in to comment.