Skip to content

Commit

Permalink
Bug IET-OU#7, add ./compat Javascript checker [iet:10364720]
Browse files Browse the repository at this point in the history
* Executed on JS include, not in `require().app.run()`
  • Loading branch information
nfreear committed Oct 1, 2018
1 parent 20e96c3 commit 8b58002
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

module.exports.run = run;

const IS_COMPAT = require('./compat').check();

const LOC = window.location;
const CORE = require('./core');
const LAYOUT = require('./layout');
Expand All @@ -13,6 +15,10 @@ const UTIL = require('./util'); // Was: require('./config');
const VIEWS = require('./views');

function run (config) {
if (!IS_COMPAT) {
return;
}

console.warn('The our-journey API:', require('../index'), 'config:', config);

UTIL.putConfig(config);
Expand Down
56 changes: 56 additions & 0 deletions src/compat.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/* Browser compatibility | ©The Open University.
*/

module.exports.check = checkAndHandle;

const UA = window.navigator.userAgent;
const DOC = window.document;
const LOC = window.location;
const NO_COMPAT_MSG = [
// '<style> .ojs-error { color: #a00; } </style>',
'<div class="X-ojs-error alert alert-danger" role="alert">',
' <p>Sorry! <i>our-journey</i> does not work on Internet Explorer.</p>',
' <p><a href="https://browsehappy.com">Try a different browser — Browse Happy</a></p>',
'</div>'
];
const COMPAT_REGEX = /(MSIE|Trident\/)/; // Live!
// const COMPAT_REGEX = /(MSIE|Trident\/|Chrome)/; // Test!

function checkAndHandle () {
const IS_COMPAT = (!COMPAT_REGEX.test(UA) || /compatCheck=false/.test(LOC.href));

if (IS_COMPAT) {
console.warn('our-journey. Browser is compatible');
} else {
// console.error('our-journey. Browser NOT compatible (MSIE ?)');

notCompatibleMessage();

tryHideContainer();

let err = new Error('our-journey. Browser NOT compatible (MSIE ?)');
err.name = 'CompatError';
throw err;
}

return IS_COMPAT;
}

function notCompatibleMessage () {
// const CTR = UTIL.config('container');
const DIV = DOC.createElement('div');

DIV.innerHTML = NO_COMPAT_MSG.join('\n');
DIV.className = 'our-journey-js ojs-error ojs-no-compat ojs-msie';

DOC.body.insertBefore(DIV, DOC.body.firstChild); // Was: DOC.body.appendChild(CTR);
}

function tryHideContainer () {
// We can't use UTIL.config('container') here, so guess!
const TRY_CTR = DOC.querySelector('#our-journey-tool');

if (TRY_CTR) {
TRY_CTR.style.display = 'none';
}
}
4 changes: 4 additions & 0 deletions style/journeystyle.css
Original file line number Diff line number Diff line change
Expand Up @@ -207,3 +207,7 @@ img[ src $= 'assets/iet-logo.svg' ] {
margin-right: 16px;
padding: 5px;
}

.ojs-error {
colour: #a00;
}

0 comments on commit 8b58002

Please sign in to comment.