-
-
Notifications
You must be signed in to change notification settings - Fork 103
/
pathConfiguration.ts
99 lines (83 loc) · 2.78 KB
/
pathConfiguration.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import { PathOptionSpec } from "./optionSpec.interface.js";
import { OSKResourcePathConfiguration } from 'keyman/engine/osk';
const addDelimiter = (p: string) => {
// Add delimiter if missing
if(p.substring(p.length-1) != '/') {
return p + '/';
} else {
return p;
}
}
export default class PathConfiguration implements OSKResourcePathConfiguration {
readonly root: string;
readonly resources: string;
readonly keyboards: string;
readonly sourcePath: string;
// May get its initial value from the Keyman Cloud API after a query if not
// otherwise specified.
private _fonts: string;
readonly protocol: string;
/*
* Pre-modularization code corresponding to `sourcePath`:
```
// Determine path and protocol of executing script, setting them as
// construction defaults.
//
// This can only be done during load when the active script will be the
// last script loaded. Otherwise the script must be identified by name.
var scripts = document.getElementsByTagName('script');
var ss = scripts[scripts.length-1].src;
var sPath = ss.substr(0,ss.lastIndexOf('/')+1);
```
*/
constructor(pathSpec: Required<PathOptionSpec>, sourcePath: string) {
sourcePath = addDelimiter(sourcePath);
this.sourcePath = sourcePath;
const _rootPath = sourcePath.replace(/(https?:\/\/)([^\/]*)(.*)/,'$1$2/');
this.protocol = sourcePath.replace(/(.{3,5}:)(.*)/,'$1');
// Get default paths and device options
this.root = _rootPath;
if(pathSpec.root != '') {
this.root = this.fixPath(pathSpec.root);
} else {
this.root = this.fixPath(_rootPath);
}
// Resources are located with respect to the engine by default
let resources = pathSpec.resources; // avoid mutating the parameter!
if(resources == '') {
resources = sourcePath;
}
// Convert resource, keyboard and font paths to absolute URLs
this.resources = this.fixPath(resources);
this.keyboards = this.fixPath(pathSpec.keyboards);
this._fonts = this.fixPath(pathSpec.fonts);
}
// Local function to convert relative to absolute URLs
// with respect to the source path, server root and protocol
fixPath(p: string) {
if(p.length == 0) {
return p;
}
p = addDelimiter(p);
// Absolute
if((p.replace(/^(http)s?:.*/,'$1') == 'http') || (p.replace(/^(file):.*/,'$1') == 'file')) {
return p;
}
// Absolute (except for protocol)
if(p.substring(0,2) == '//') {
return this.protocol + p;
}
// Relative to server root
if(p.substring(0,1) == '/') {
return this.root + p.substring(1);
}
// Otherwise, assume relative to source path
return this.sourcePath + p;
}
get fonts(): string {
return this._fonts;
}
set fonts(str: string) {
this._fonts = this.fixPath(str);
}
}