-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
127 lines (110 loc) · 2.86 KB
/
index.js
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/*
Loads dependencies based on desired group of function.
*/
/**
* @typedef {(string)|(string[])} Dependency
* @typedef {Record<string, Dependency>} DependencyMap { packageName1: filePath, packageName2: [filePath1, filePathn, ... ] }
*/
const DEFAULTS = ['css', 'jquery', 'underscore']
const { loadDependencyUrls } = require('js-functions').Utility
/**
* @type {DependencyMap}
*/
const dependencies = require('./mapping.json')
let depsUrl = './lib/jsonform/deps/'
let jsonFormUrl = './lib/jsonform.js'
/**
* @param {string[]} keys
*/
function load (keys = []) {
const requiredUrls = createDependencyList(DEFAULTS)
return loadDependencies(requiredUrls)
.then(() => {
// console.log(window.$, window._)
const optionalUrls = createDependencyList(keys, DEFAULTS)
return loadDependencies(optionalUrls)
})
.then(() => {
return loadDependencies([jsonFormUrl])
})
}
function getKeys () {
return Object.keys(dependencies)
}
/**
* @param {{ depsUrl?: string, jsonFormUrl?: string }} options
*/
function setOptions (options = {}) {
if (options.depsUrl) {
depsUrl = options.depsUrl
}
if (options.jsonFormUrl) {
jsonFormUrl = options.jsonFormUrl
}
return {
depsUrl,
jsonFormUrl
}
}
/**
* @param {string[]} rawKeys
* @param {string[]} disallowed
*/
function createDependencyList (rawKeys = [], disallowed = []) {
// Remove disallowed
/**
* @type {string[]}
*/
const keys = []
rawKeys.forEach((key, index) => {
if (disallowed.indexOf(key) < 0) {
keys.push(key)
}
})
console.log('createDependencyList', rawKeys, keys, disallowed)
/**
* @type {string[]}
*/
let usedDependencies = []
keys.forEach((key) => {
let val = dependencies[key]
if (!val) {
console.warn('Following key is not a valid dependency', key)
return
}
const thisDependencies = Array.isArray(val) ? val : [val]
usedDependencies = usedDependencies.concat(usedDependencies, thisDependencies)
})
// Make sure is unique
usedDependencies = uniqueArray(usedDependencies)
// Add dependencies directory prefix
usedDependencies = usedDependencies.map(url => {
return `${depsUrl}${url}`
})
return usedDependencies
}
/**
* Loads dependencies(css and js) in order.
* @param {string[]} urls
*/
function loadDependencies (urls = []) {
return loadDependencyUrls(urls, {ordered: true})
}
/**
* @param {any[]} arr
*/
function uniqueArray (arr) {
return [...new window.Set(arr)]
}
const loader = {
load: load,
getKeys: getKeys,
setOptions: setOptions
}
if (typeof module === 'object') {
module.exports = loader
}
if (typeof window === 'object') {
const w = /** @type {Window & { JsonFormLoader?: typeof loader }} */ (window)
w.JsonFormLoader = loader
}