Skip to content

Commit

Permalink
Fix loading of jsu as script | refs #39301
Browse files Browse the repository at this point in the history
  • Loading branch information
sdiemer committed Feb 9, 2024
1 parent f78a0c4 commit f05d30e
Show file tree
Hide file tree
Showing 10 changed files with 88 additions and 25 deletions.
31 changes: 31 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,37 @@ Please use `eslint` to clean the code using the guidelines specified in the `.es

Use the `make lint` command to check your code before pushing it.

## Usage

### As script

Add to your page:

```html
<script type="text/javascript" src="dist/jsu.min.js"></script>
```

You can then use the library:

```js
jsu.translate('test');
```

### As module

Import the module:

```js
import JavaScriptUtilities from 'dist/jsu.mod.min.js';
```

You can then use the library:

```js
const jsu = new JavaScriptUtilities();
jsu.translate('test');
```

## History

This project is a refactoring of utils.js:
Expand Down
2 changes: 1 addition & 1 deletion dist/jsu.min.js

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions dist/jsu.mod.min.js

Large diffs are not rendered by default.

22 changes: 16 additions & 6 deletions gulpfile.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,26 @@
/* sudo npm install -g gulp gulp-concat gulp-minify */
/* sudo npm install -g gulp gulp-concat gulp-minify gulp-replace */
import gulp from 'gulp';
import * as concat from 'gulp-concat';
import * as minify from 'gulp-minify';
import concat from 'gulp-concat';
import minify from 'gulp-minify';
import replace from 'gulp-replace';

gulp.task('autobuild', function () {
gulp.watch('src/**', {'ignoreInitial': false}, gulp.series('build'));
});

gulp.task('build', function () {
return gulp.src(['src/jsu.js', 'src/lib/*.js'])
.pipe(concat.default('dist/jsu.js'))
.pipe(minify.default({
gulp.src(['src/jsu.js', 'src/lib/*.js'])
.pipe(concat('dist/jsu.js'))
.pipe(minify({
ext: {'src': '.mod.tmp.js', 'min': '.mod.min.js'},
compress: {'hoist_vars': true}
}))
.pipe(gulp.dest('.'));

return gulp.src(['src/jsu.js', 'src/lib/*.js', 'src/load.js'])
.pipe(replace(/export (default ){0,1}/g, ''))
.pipe(concat('dist/jsu.js'))
.pipe(minify({
ext: {'src': '.tmp.js', 'min': '.min.js'},
compress: {'hoist_vars': true}
}))
Expand Down
5 changes: 3 additions & 2 deletions karma.conf.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ module.exports = (config) => {

// list of files / patterns to load in the browser
files: [
'src/**/*.js',
'src/jsu.js',
'src/lib/*.js',
'tests/*.spec.js',
// fixtures
{pattern: 'tests/mocking/*.json', watched: true, served: true, included: false}
Expand All @@ -30,7 +31,7 @@ module.exports = (config) => {
// available preprocessors: https://www.npmjs.com/search?q=keywords:karma-preprocessor
preprocessors: {
'src/**/*.js': ['browserify', 'coverage'],
'tests/*.spec.js': ['browserify'],
'tests/*.spec.js': ['browserify']
},


Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"gulp": "latest",
"gulp-concat": "latest",
"gulp-minify": "latest",
"gulp-replace": "latest",
"karma": "latest",
"karma-browserify": "latest",
"karma-chai": "latest",
Expand Down
42 changes: 28 additions & 14 deletions src/jsu.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
/*******************************************
* jsu: JavaScript Utilities *
*******************************************/
const VERSION = 9;
/*
jsu (JavaScript Utilities)
*/

export default class JavaScriptUtilities {

constructor () {
this.version = VERSION;
this.version = 10; // Change this when updating this script
this.ignoreUntilFocusChanges = false;
this.userAgent = window.navigator && window.navigator.userAgent ? window.navigator.userAgent.toLowerCase() : 'unknown';
this.userAgentData = null;
Expand All @@ -24,11 +22,12 @@ export default class JavaScriptUtilities {
this._overrideHttpRequest();
if (!window.jsu) {
window.jsu = this;
} else {
if (VERSION != window.jsu.version) {
console.warn('Another version of jsu.js was already imported, jsu will be replaced by most recent version. Version: "' + VERSION + '", other version: "' + window.jsu.version + '".');
}
if (window.jsu.version < VERSION) {
} else if (this.version != window.jsu.version) {
console.warn(
'Another version of jsu.js was already imported, jsu will be replaced by most recent version. ' +
'Version: "' + this.version + '", other version: "' + window.jsu.version + '".'
);
if (window.jsu.version < this.version) {
window.jsu = this;
}
}
Expand Down Expand Up @@ -390,7 +389,9 @@ export default class JavaScriptUtilities {
const platform = window.navigator.platform.toLowerCase();
if (platform.indexOf('ipad') != -1 || platform.indexOf('iphone') != -1 || platform.indexOf('ipod') != -1) {
name = 'ios';
version = parseFloat(('' + (/CPU.*OS ([0-9_]{1,5})|(CPU like).*AppleWebKit.*Mobile/i.exec(navigator.userAgent) || [0,''])[1]).replace('undefined', '3_2').replace('_', '.').replace('_', '')) || false;
version = parseFloat(('' + (
/CPU.*OS ([0-9_]{1,5})|(CPU like).*AppleWebKit.*Mobile/i.exec(navigator.userAgent) || [0,'']
)[1]).replace('undefined', '3_2').replace('_', '.').replace('_', '')) || false;
}
}
if (!name && window.navigator && window.navigator.appVersion) {
Expand Down Expand Up @@ -490,7 +491,21 @@ export default class JavaScriptUtilities {
this.isMobile = Boolean(window.navigator.userAgentData.mobile);
} else {
const ua = this.userAgent;
this.isMobile = ua.indexOf('iphone') != -1 || ua.indexOf('ipod') != -1 || ua.indexOf('android') != -1 || ua.indexOf('iemobile') != -1 || ua.indexOf('opera mobi') != -1 || ua.indexOf('opera mini') != -1 || ua.indexOf('windows ce') != -1 || ua.indexOf('fennec') != -1 || ua.indexOf('series60') != -1 || ua.indexOf('symbian') != -1 || ua.indexOf('blackberry') != -1 || window.orientation !== undefined || (window.navigator && window.navigator.platform == 'iPad');
this.isMobile = (
ua.indexOf('iphone') != -1
|| ua.indexOf('ipod') != -1
|| ua.indexOf('android') != -1
|| ua.indexOf('iemobile') != -1
|| ua.indexOf('opera mobi') != -1
|| ua.indexOf('opera mini') != -1
|| ua.indexOf('windows ce') != -1
|| ua.indexOf('fennec') != -1
|| ua.indexOf('series60') != -1
|| ua.indexOf('symbian') != -1
|| ua.indexOf('blackberry') != -1
|| window.orientation !== undefined
|| (window.navigator && window.navigator.platform == 'iPad')
);
}
this.isTactile = document.documentElement && 'ontouchstart' in document.documentElement;

Expand Down Expand Up @@ -735,4 +750,3 @@ export default class JavaScriptUtilities {
};
}
}
new JavaScriptUtilities();
1 change: 0 additions & 1 deletion src/lib/chunked-upload.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ Module to upload a file by chunks.
*/
/* global jsu */

// eslint-disable-next-line no-unused-vars
export class ChunkedUpload {
constructor (options) {
// Get and check options
Expand Down
6 changes: 6 additions & 0 deletions src/load.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/*
Script to load jsu when used as script and not as module
*/
/* global JavaScriptUtilities */

new JavaScriptUtilities();
2 changes: 1 addition & 1 deletion tests/test_jsu.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const jsu = new JavaScriptUtilities();

describe('JSU', () => {
it('should return correct version', () => {
assert(jsu.version === 9);
assert(jsu.version === 10);
});
it('should set/get cookies', () => {
jsu.setCookie('a', '1');
Expand Down

0 comments on commit f05d30e

Please sign in to comment.