forked from NathanWalker/nativescript-fonticon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnativescript-fonticon.ts
executable file
·87 lines (77 loc) · 2.48 KB
/
nativescript-fonticon.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
import * as app from 'application';
import {knownFolders} from 'file-system';
export class TNSFontIcon {
public static css: any = {}; // font icon collections containing maps of classnames to unicode
public static paths: any = {}; // file paths to font icon collections
public static debug: boolean = false;
public static loadCss(): void {
let cnt = 0;
let currentName: string;
let fontIconCollections = Object.keys(TNSFontIcon.paths);
if (TNSFontIcon.debug) {
console.log(`Collections to load: ${fontIconCollections}`);
}
let initCollection = () => {
currentName = fontIconCollections[cnt];
TNSFontIcon.css[currentName] = {};
};
let mapCss = (data: any): void => {
let sets = data.split('}');
let cleanValue = (val) => {
let v = val.split('content:')[1].replace(/\\f/, '\\uf').trim().replace(/\"/g, '').slice(0, -1);
return v;
};
for (let set of sets) {
let pair = set.split(':before {');
let keyGroups = pair[0];
let keys = keyGroups.split(',');
if (pair[1]) {
let value = cleanValue(pair[1]);
for (let key of keys) {
key = key.trim().slice(1).split(':before')[0];
TNSFontIcon.css[currentName][key] = String.fromCharCode(parseInt(value.substring(2), 16));
if (TNSFontIcon.debug) {
console.log(`${key}: ${value}`);
}
}
}
}
};
let loadFile = (path: string): Promise<any> => {
if (TNSFontIcon.debug) {
console.log(`----------`);
console.log(`Loading collection '${currentName}' from file: ${path}`);
}
let cssFile = knownFolders.currentApp().getFile(path);
return new Promise((resolve, reject) => {
cssFile.readText().then((data) => {
mapCss(data);
resolve();
}, (err) => {
reject(err);
});
});
};
let loadFiles = () => {
initCollection();
if (cnt < fontIconCollections.length) {
loadFile(TNSFontIcon.paths[currentName]).then(() => {
cnt++;
loadFiles();
});
}
};
loadFiles();
}
}
export function fonticon(value: string): string {
if (value) {
if (value.indexOf('-') > -1) {
let prefix = value.split('-')[0];
return TNSFontIcon.css[prefix][value];
} else {
console.log(`Fonticon classname did not contain a prefix. i.e., 'fa-bluetooth'`);
}
}
return value;
}