iwmlib/lib/bootstrap.js

171 lines
4.9 KiB
JavaScript
Raw Normal View History

2019-03-21 09:57:27 +01:00
class Bootstrap extends Object {
static get isSafari() {
2019-07-30 16:56:29 +02:00
return /Safari/.test(navigator.userAgent) && /Apple Computer, Inc/.test(navigator.vendor)
2019-03-21 09:57:27 +01:00
}
static get isModernSafari() {
2019-07-18 12:26:39 +02:00
if (!this.isSafari) return false
2019-03-21 09:57:27 +01:00
let agent = navigator.appVersion
let offset = agent.indexOf('Version')
if (offset != -1) {
let version = parseFloat(agent.substring(offset + 8))
return version >= 10.1
}
return false
}
static get isChrome() {
const isChromium = window.chrome
const winNav = window.navigator
const vendorName = winNav.vendor
const isOpera = winNav.userAgent.indexOf('OPR') > -1
const isIEedge = winNav.userAgent.indexOf('Edge') > -1
const isIOSChrome = winNav.userAgent.match('CriOS')
if (isIOSChrome) {
return true
2019-07-18 12:26:39 +02:00
} else if (
isChromium !== null &&
isChromium !== undefined &&
vendorName === 'Google Inc.' &&
isOpera == false &&
isIEedge == false
) {
2019-03-21 09:57:27 +01:00
return true
} else {
return false
}
}
static get isModernChrome() {
if (!this.isChrome) {
return false
}
const raw = navigator.userAgent.match(/Chrom(e|ium)\/([0-9]+)\./)
const version = raw ? parseInt(raw[2], 10) : false
//return version > 70
return false
}
static get isFirefox() {
if (window.navigator.userAgent.toLowerCase().indexOf('firefox') > -1) {
return true
}
return false
}
static get isModernFirefox() {
if (!this.isFirefox) {
return false
}
const match = window.navigator.userAgent.match(/Firefox\/([0-9]+)\./)
const version = match ? parseInt(match[1]) : 0
return version > 58
}
static import(src, callback = null) {
if (src.endsWith('babel.js')) {
2019-07-18 12:26:39 +02:00
this.load(
this.baseUrl + '/3rdparty/polyfills/babel-polyfill.js',
2019-03-21 09:57:27 +01:00
() => {
this.load(src, callback, null)
},
2019-07-18 12:26:39 +02:00
null
)
2019-07-30 16:56:29 +02:00
} else if (this.isModernSafari || this.isModernChrome || this.isModernFirefox) {
2019-03-21 09:57:27 +01:00
this.load(src, callback)
} else {
2019-07-18 12:26:39 +02:00
this.load(
this.baseUrl + '/3rdparty/systemjs/system.js',
() => {
SystemJS.config(this.systemjsConfig)
let promise = SystemJS.import(src)
if (promise) {
promise.then(() => {
if (callback) {
callback.call(this)
}
})
}
},
'script'
)
2019-03-21 09:57:27 +01:00
}
}
static load(src, callback, type = 'module') {
let script = document.createElement('script')
if (type === 'module') {
script.setAttribute('type', 'module')
script.setAttribute('crossorigin', 'use-credentials')
}
script.onload = () => {
if (callback) {
callback.call(this, script)
}
}
script.src = src
document.head.appendChild(script)
}
static require(src) {
console.log('Dummy require')
}
static get systemjsConfig() {
const baseUrl = this.baseUrl
return {
baseURL: baseUrl,
map: {
'plugin-babel': baseUrl + '/3rdparty/systemjs/plugin-babel.js',
2019-07-30 16:56:29 +02:00
'systemjs-babel-build': baseUrl + '/3rdparty/systemjs/systemjs-babel-browser.js'
2019-03-21 09:57:27 +01:00
},
transpiler: 'plugin-babel',
meta: {
'*.js': {
authorization: true,
babelOptions: {
es2015: false
}
}
}
}
}
static get baseUrl() {
let baseUrl = './'
let scripts = document.getElementsByTagName('script')
for (let i = 0; i < scripts.length; i++) {
const script = scripts[i]
const src = script.getAttribute('src')
const re = /\/bootstrap(.babel)?\.js$/
if (re.test(src)) {
baseUrl = src.replace(re, '')
}
}
return baseUrl
}
static renderFont(font = 'Open Sans') {
for (let weight of [300, 400, 600, 700, 800]) {
for (let style of ['normal', 'italic']) {
let p = document.createElement('p')
p.innerHTML = '.'
document.body.appendChild(p)
2019-07-18 12:26:39 +02:00
p.setAttribute(
'style',
`font-family: '${font}'; font-weight: ${weight}; font-style: '${style}'; position: absolute; top: -10000px;`
)
2019-03-21 09:57:27 +01:00
}
}
}
}
window.Bootstrap = Bootstrap