generated from League-of-Foundry-Developers/FoundryVTT-Module-Template
-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathforms.js
264 lines (240 loc) · 8.68 KB
/
forms.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
import { ChallengeTrackerSettings, ChallengeTracker } from './main.js'
import { ChallengeTrackerFlag } from './flags.js'
Hooks.on('closeChallengeTrackerForm', () => {
const buttonLocation = game.settings.get('challenge-tracker', 'buttonLocation')
const controls = ui.controls
const controlsControls = controls.controls
const control = controlsControls.find(c => c.name === buttonLocation)
if (!control) return
const tool = control.tools.find(ct => ct.name === 'challenge-tracker')
if (!tool) return
tool.active = false
controls.render()
})
/* Display challenge trackers in a list with options */
export class ChallengeTrackerForm extends FormApplication {
constructor (options) {
super(options)
this.scrollTop = 0
}
static get defaultOptions () {
const defaults = super.defaultOptions
const overrides = {
height: 'auto',
width: 'auto',
id: 'challenge-tracker-form',
template: ChallengeTrackerSettings.templates.challengeTrackerForm,
title: game.i18n.localize('challengeTracker.labels.challengeTrackerFormTitle'),
userId: game.userId,
closeOnSubmit: false,
submitOnChange: true
}
const mergedOptions = foundry.utils.mergeObject(defaults, overrides)
return mergedOptions
}
getData (options) {
return {
ownerId: options.userId,
challengeTrackerList: ChallengeTrackerFlag.getList(options.userId)
}
}
/* Initialise the ChallengeTrackerForm */
static init () {
game.challengeTrackerForm = new ChallengeTrackerForm()
game.challengeTrackerForm._init()
}
_init () {
Hooks.on('renderChallengeTrackerForm', async () => {
const ul = $('ul.challenge-tracker-form')[0]
if (!ul) return
ul.scrollTop = this.scrollTop
})
}
/**
* Open the Challenge Tracker form by event
* @param {object} event Event trigger
**/
static openByEvent (event) {
const userId = $(event.currentTarget).parents('[data-user-id]')?.data()?.userId
game.challengeTrackerForm.render(true, { userId })
}
/**
* Open the Challenge Tracker form by user name or current user
* @param {string} [userName=null] User name
**/
static open (userName = null) {
let userId
if (game.user.isGM && userName) {
userId = game.users.find(u => u.name === userName).id
if (!userId) {
ui.notifications.info(`User '${userName}' does not exist.`)
return
}
} else {
userId = game.userId
}
game.challengeTrackerForm.render(true, { userId })
}
activateListeners (html) {
super.activateListeners(html)
html.on('click', '[data-action]', this._handleButtonClick.bind(this))
}
/**
* Handle form button events
* @param {object} event Event trigger
**/
async _handleButtonClick (event) {
event.preventDefault()
const clickedElement = $(event.currentTarget)
const action = clickedElement.data().action
const ownerId = clickedElement.parents('[data-owner-id]')?.data()?.ownerId
const challengeTrackerId = clickedElement.parents('li')?.data()?.challengeTrackerId
const ul = $('ul.challenge-tracker-form')[0]
this.scrollTop = ul.scrollTop
switch (action) {
case 'open' : {
ChallengeTracker.open(null, null, { id: challengeTrackerId, ownerId })
break
}
case 'edit': {
await ChallengeTrackerEditForm.open(ownerId, challengeTrackerId)
break
}
case 'copy': {
await ChallengeTrackerFlag.copy(ownerId, challengeTrackerId)
break
}
case 'delete': {
await ChallengeTrackerFlag.unset(ownerId, challengeTrackerId)
break
}
case 'move-up': {
await this.move('up', ownerId, challengeTrackerId)
this.render(false, { width: 'auto', height: 'auto' })
break
}
case 'move-down': {
await this.move('down', ownerId, challengeTrackerId)
this.render(false, { width: 'auto', height: 'auto' })
break
}
case 'new': {
await ChallengeTrackerEditForm.open(ownerId)
break
}
}
this.render(false, { width: 'auto', height: 'auto' })
}
async move (direction, ownerId, challengeTrackerId) {
const flagLength = Object.keys(game.users.get(ownerId).data.flags['challenge-tracker']).length
const challengeTracker1 = ChallengeTrackerFlag.get(ownerId, challengeTrackerId)
if (!challengeTracker1) return
const originalPosition = challengeTracker1.listPosition
if ((direction === 'up' && originalPosition === 1) ||
(direction === 'down' && originalPosition >= flagLength)) return
let newPosition = null
switch (direction) {
case 'up':
newPosition = originalPosition - 1
break
case 'down':
newPosition = originalPosition + 1
break
}
const challengeTracker2 = Object.values(game.users.get(ownerId).data.flags['challenge-tracker']).find(ct => ct.listPosition === newPosition)
await ChallengeTrackerFlag.set(ownerId, { id: challengeTrackerId, listPosition: newPosition })
if (challengeTracker2) await ChallengeTrackerFlag.set(ownerId, { id: challengeTracker2.id, listPosition: originalPosition })
}
}
/* Display challenge tracker options */
export class ChallengeTrackerEditForm extends FormApplication {
constructor (ownerId, challengeTrackerId) {
super()
this.ownerId = ownerId
this.challengeTrackerId = challengeTrackerId
}
static get defaultOptions () {
const defaults = super.defaultOptions
const overrides = {
height: 'auto',
width: '300px',
id: 'challenge-tracker-edit-form',
template: ChallengeTrackerSettings.templates.challengeTrackerEditForm,
title: game.i18n.localize('challengeTracker.labels.challengeTrackerEditFormTitle'),
userId: game.userId,
closeOnSubmit: true
}
const mergedOptions = foundry.utils.mergeObject(defaults, overrides)
return mergedOptions
}
getData (options) {
if (this.challengeTrackerId) {
return { challengeTracker: ChallengeTrackerFlag.get(this.ownerId, this.challengeTrackerId) }
} else {
return {
challengeTracker: {
backgroundImage: null,
frameColor: null,
frameWidth: 'medium',
id: `${ChallengeTrackerSettings.id}-${Math.random().toString(16).slice(2)}`,
image: null,
innerBackgroundColor: null,
innerColor: null,
innerCurrent: 0,
innerTotal: 3,
outerBackgroundColor: null,
outerColor: null,
outerCurrent: 0,
outerTotal: 4,
ownerId: this.ownerId,
persist: true,
show: false,
size: null,
title: game.i18n.localize('challengeTracker.labels.challengeTrackerTitle'),
windowed: true
}
}
}
}
/* Initialise the ChallengeTrackerEditForm */
static init () {
this.challengeTrackerEditForm = new ChallengeTrackerEditForm()
}
/**
* Open the Edit Challenge Tracker form
* @param {string} ownerId User that owns the flag
* @param {string} [challengeTrackerId=null] Unique identifier for the Challenge Tracker
**/
static async open (ownerId, challengeTrackerId = null) {
ChallengeTrackerEditForm.challengeTrackerEditForm.ownerId = ownerId
ChallengeTrackerEditForm.challengeTrackerEditForm.challengeTrackerId = challengeTrackerId
ChallengeTrackerEditForm.challengeTrackerEditForm.render(true)
}
activateListeners (html) {
super.activateListeners(html)
ColorPicker.install()
}
/**
* Merge options with flag and, if open, redraw the Challenge Tracker
* @param {string} ownerId User that owns the flag
* @param {string} [challengeTrackerId=null] Unique identifier for the Challenge Tracker
**/
async _updateObject (event, formData) {
const ownerId = event.currentTarget.dataset.ownerId
const challengeTrackerId = event.currentTarget.dataset.challengeTrackerId
const flag = ChallengeTrackerFlag.get(ownerId, challengeTrackerId)
let challengeTrackerOptions
if (flag) {
challengeTrackerOptions = foundry.utils.mergeObject(flag, formData)
} else {
const title = formData.title ?? game.i18n.localize('challengeTracker.labels.challengeTrackerTitle')
const persist = true
const id = challengeTrackerId
const listPosition = Object.keys(game.users.get(ownerId).data.flags['challenge-tracker'] || {}).length + 1
challengeTrackerOptions = foundry.utils.mergeObject(formData, { ownerId, id, listPosition, persist, title })
}
await ChallengeTrackerFlag.set(ownerId, challengeTrackerOptions)
const challengeTracker = Object.values(game.challengeTracker).find(ct => ct.challengeTrackerOptions.id === challengeTrackerId)
if (challengeTracker) challengeTracker.draw(challengeTrackerOptions)
}
}