-
-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathImagePicker.js
111 lines (97 loc) · 2.9 KB
/
ImagePicker.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
import DirectoryPicker from "./DirectoryPicker.js";
/**
* Game Settings: ImagePicker
*/
class ImagePicker extends FilePicker {
constructor(options = {}) {
super(options);
}
_onSubmit(event) {
event.preventDefault();
const path = event.target.file.value;
const activeSource = this.activeSource;
const bucket = event.target.bucket ? event.target.bucket.value : null;
this.field.value = ImagePicker.format({
activeSource,
bucket,
path,
});
this.close();
}
static async uploadToPath(path, file) {
const options = DirectoryPicker.parse(path);
return FilePicker.upload(options.activeSource, options.current, file, { bucket: options.bucket });
}
// returns the type "Img" for rendering the SettingsConfig
static Img(val) {
return val === null ? '' : String(val);
}
// formats the data into a string for saving it as a GameSetting
static format(value) {
return value.bucket !== null
? `[${value.activeSource}:${value.bucket}] ${value.path}`
: `[${value.activeSource}] ${value.path}`;
}
// parses the string back to something the FilePicker can understand as an option
static parse(inStr) {
const str = inStr ?? '';
let matches = str.match(/\[(.+)\]\s*(.+)?/u);
if (matches) {
let [, source, current = ''] = matches;
current = current.trim();
const [s3, bucket] = source.split(":");
if (bucket !== undefined) {
return {
activeSource: s3,
bucket: bucket,
current: current,
};
} else {
return {
activeSource: s3,
bucket: null,
current: current,
};
}
}
// failsave, try it at least
return {
activeSource: "data",
bucket: null,
current: str,
};
}
// Adds a FilePicker-Simulator-Button next to the input fields
static processHtml(html) {
$(html)
.find(`input[data-dtype="Img"]`)
.each((index, element) => {
// $(element).prop("readonly", true);
if (!$(element).next().length) {
let picker = new ImagePicker({
field: $(element)[0],
...ImagePicker.parse(this.value),
});
// data-type="image" data-target="img"
let pickerButton = $(
'<button type="button" class="file-picker" title="Pick image"><i class="fas fa-file-import fa-fw"></i></button>',
);
pickerButton.on("click", () => {
picker.render(true);
});
$(element).parent().append(pickerButton);
}
});
}
/** @override */
activateListeners(html) {
super.activateListeners(html);
// remove unnecessary elements
$(html).find("footer button").text("Select Image");
}
}
// eslint-disable-next-line no-unused-vars
Hooks.on("renderSettingsConfig", (app, html, user) => {
ImagePicker.processHtml(html);
});
export default ImagePicker;