-
-
Notifications
You must be signed in to change notification settings - Fork 14
/
gm-notes.js
166 lines (142 loc) · 5.67 KB
/
gm-notes.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
class GMNote extends FormApplication {
constructor(object, options) {
super(object, options);
this.entity.apps[this.appId] = this;
}
get entity() {
return this.object;
}
get showExtraButtons() {
return (game.dnd5e && this.entity.constructor.name !== 'RollTable');
}
static get defaultOptions() {
const options = super.defaultOptions;
options.template = "modules/gm-notes/templates.html";
options.width = '600';
options.height = '700';
options.classes = ['gm-notes', 'sheet'];
options.title = game.i18n.localize('GMNote.label');
options.resizable = true;
options.editable = true;
return options;
}
getData() {
const data = super.getData();
data.notes = this.entity.getFlag('gm-notes', 'notes');
data.flags = this.entity.data.flags;
data.owner = game.user.id;
data.isGM = game.user.isGM;
data.showExtraButtons = this.showExtraButtons;
return data;
}
activateListeners(html) {
super.activateListeners(html);
html.find('.moveToNote').click(ev => this._moveToNotes());
html.find('.moveToDescription').click(ev => this._moveToDescription());
}
async _updateObject(event, formData) {
if (game.user.isGM) {
await this.entity.setFlag('gm-notes', 'notes', formData["flags.gm-notes.notes"]);
this.render();
} else {
ui.notifications.error("You have to be GM to edit GM Notes.");
}
}
static _initEntityHook(app, html, data) {
if (game.user.isGM) {
let labelTxt = '';
let labelStyle= "";
let title = game.i18n.localize('GMNote.label');
let notes = app.document.getFlag('gm-notes', 'notes');
if (game.settings.get('gm-notes', 'hideLabel') === false) {
labelTxt = ' ' + title;
}
if (game.settings.get('gm-notes', 'colorLabel') === true && notes) {
labelStyle = "style='color:green;'";
}
let openBtn = $(`<a class="open-gm-note" title="${title}" ${labelStyle} ><i class="fas fa-clipboard${notes ? '-check':''}"></i>${labelTxt}</a>`);
openBtn.click(ev => {
let noteApp = null;
for (let key in app.document.apps) {
let obj = app.document.apps[key];
if (obj instanceof GMNote) {
noteApp = obj;
break;
}
}
if (!noteApp) noteApp = new GMNote(app.document, { submitOnClose: true, closeOnSubmit: false, submitOnUnfocus: true });
noteApp.render(true);
});
html.closest('.app').find('.open-gm-note').remove();
let titleElement = html.closest('.app').find('.window-title');
openBtn.insertAfter(titleElement);
}
}
async _moveToNotes() {
if (game.dnd5e) {
let descPath = '';
switch (this.entity.constructor.name) {
case 'Actor5e': descPath = 'data.details.biography.value'; break;
case 'Item5e': descPath = 'data.description.value'; break;
case 'JournalEntry': descPath = 'content'; break;
}
let description = getProperty(this.entity, 'data.'+descPath);
let notes = getProperty(this.entity, 'data.flags.gm-notes.notes');
if (notes === undefined) notes = '';
if (description === undefined) description = '';
let obj = {};
obj[descPath] = '';
obj['flags.gm-notes.notes'] = notes + description;
await this.entity.update(obj);
this.render();
}
}
async _moveToDescription() {
if (game.dnd5e) {
let descPath = '';
switch (this.entity.constructor.name) {
case 'Actor5e': descPath = 'data.details.biography.value'; break;
case 'Item5e': descPath = 'data.description.value'; break;
case 'JournalEntry': descPath = 'content'; break;
}
let description = getProperty(this.entity, 'data.' + descPath);
let notes = getProperty(this.entity, 'data.flags.gm-notes.notes');
if (notes === undefined) notes = '';
if (description === undefined) description = '';
let obj = {};
obj[descPath] = description + notes;
obj['flags.gm-notes.notes'] = '';
await this.entity.update(obj);
this.render();
}
}
}
Hooks.on('init', () => {
game.settings.register("gm-notes", 'hideLabel', {
name: game.i18n.localize('GMNote.setting'),
hint: game.i18n.localize('GMNote.settingHint'),
scope: "world",
config: true,
default: false,
type: Boolean
});
game.settings.register("gm-notes", 'colorLabel', {
name: game.i18n.localize('GMNote.colorSetting'),
scope: "world",
config: true,
default: false,
type: Boolean
});
});
Hooks.on('renderActorSheet', (app, html, data) => {
GMNote._initEntityHook(app, html, data);
});
Hooks.on('renderItemSheet', (app, html, data) => {
GMNote._initEntityHook(app, html, data);
});
Hooks.on('renderJournalSheet', (app, html, data) => {
GMNote._initEntityHook(app, html, data);
});
Hooks.on('renderRollTableConfig', (app, html, data) => {
GMNote._initEntityHook(app, html, data);
});