This repository has been archived by the owner on Oct 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 226
/
element-action.ts
264 lines (231 loc) · 6.21 KB
/
element-action.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
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 * as Mobx from 'mobx';
import * as Message from '../message';
import * as _ from 'lodash';
import * as Types from '../types';
import { Project } from './project';
import { UserStore } from './user-store';
import { UserStoreAction } from './user-store-action';
import { UserStoreProperty } from './user-store-property';
import * as uuid from 'uuid';
export interface Sender {
send(msg: Message.Message): void;
}
export interface ElementActionInit {
elementPropertyId: string;
id: string;
open: boolean;
payload: string;
payloadType: Types.ElementActionPayloadType;
storeActionId: string;
storePropertyId: string;
}
export class ElementAction {
public readonly model = Types.ModelName.ElementAction;
private elementPropertyId: string;
private id: string;
private userStore: UserStore;
@Mobx.observable private open: boolean;
@Mobx.observable private payload: string;
@Mobx.observable private payloadType: Types.ElementActionPayloadType;
@Mobx.observable private storeActionId: string;
@Mobx.observable private storePropertyId: string;
public constructor(init: ElementActionInit, ctx: { userStore: UserStore }) {
this.elementPropertyId = init.elementPropertyId;
this.id = init.id;
this.open = init.open;
this.payload = init.payload;
this.payloadType = init.payloadType;
this.storeActionId = init.storeActionId;
this.storePropertyId = init.storePropertyId;
this.userStore = ctx.userStore;
}
public static from(
serialized: Types.SerializedElementAction,
ctx: { userStore: UserStore }
): ElementAction {
return new ElementAction(
{
elementPropertyId: serialized.elementPropertyId,
id: serialized.id,
open: serialized.open,
payload: serialized.payload || '',
payloadType: serialized.payloadType,
storeActionId: serialized.storeActionId,
storePropertyId: serialized.storePropertyId
},
ctx
);
}
public clone(): ElementAction {
return new ElementAction(
{
elementPropertyId: this.elementPropertyId,
id: uuid.v4(),
open: false,
payload: this.payload,
payloadType: this.payloadType,
storeActionId: this.storeActionId,
storePropertyId: this.storePropertyId
},
{ userStore: this.userStore }
);
}
public equals(b: this): boolean {
return _.isEqual(this.toJSON(), b.toJSON());
}
public execute({
sender,
project,
event
}: {
sender?: Sender;
project: Project;
event: Event;
}): void {
const userStore = project.getUserStore();
const storeAction = userStore.getActionById(this.storeActionId);
if (!storeAction) {
return;
}
switch (storeAction.getType()) {
case Types.UserStoreActionType.Noop:
return;
case Types.UserStoreActionType.Set: {
const storeProperty = userStore.getPropertyById(this.storePropertyId);
if (storeProperty) {
storeProperty.setValue(this.getResolvedPayload({ project, event }) || '');
}
break;
}
case Types.UserStoreActionType.SetPage: {
const pageProperty = userStore.getPageProperty();
pageProperty.setValue(this.getPayload());
break;
}
case Types.UserStoreActionType.OpenExternal: {
if (sender) {
sender.send({
type: Message.MessageType.OpenExternalURL,
id: uuid.v4(),
payload: this.payload
});
} else {
window.open(this.payload, '_blank', 'noopener');
}
}
}
}
public getId(): string {
return this.id;
}
public getElementPropertyId(): string {
return this.elementPropertyId;
}
public getOpen(): boolean {
return this.open;
}
public getPayload(): string {
return this.payload;
}
// tslint:disable-next-line:no-any
public getResolvedPayload({
project,
event
}: {
project: Project;
// tslint:disable-next-line:no-any
event: any;
}): string | undefined {
console.log(event);
switch (this.payloadType) {
case Types.ElementActionPayloadType.EventPayload: {
switch (this.getPayload()) {
case 'x':
return event.clientX;
case 'y':
return event.clientY;
case 'name':
return event.target.name;
case 'value':
return event.target.value;
case 'checked':
return event.target.checked;
}
return undefined;
}
case Types.ElementActionPayloadType.PropertyPayload: {
const elementProperty = project.getElementPropertyById(this.payload);
return elementProperty ? String(elementProperty.getValue()) : undefined;
}
default:
case Types.ElementActionPayloadType.String:
return this.payload;
}
}
public getPayloadType(): Types.ElementActionPayloadType {
return this.payloadType || Types.ElementActionPayloadType.String;
}
public getStoreActionId(): string {
return this.storeActionId;
}
public getStoreAction(): UserStoreAction | undefined {
if (!this.storeActionId) {
return;
}
return this.userStore.getActionById(this.storeActionId);
}
public getStorePropertyId(): string | undefined {
return this.storePropertyId;
}
public getStoreProperty(): UserStoreProperty | undefined {
const storeAction = this.getStoreAction();
const actionPropId = storeAction && storeAction.getUserStorePropertyId();
if (actionPropId) {
return this.userStore.getPropertyById(actionPropId);
}
if (!this.storePropertyId) {
return;
}
return this.userStore.getPropertyById(this.storePropertyId);
}
@Mobx.action
public setStorePropertyId(storePropertyId: string): void {
this.storePropertyId = storePropertyId;
}
@Mobx.action
public setPayload(payload: string): void {
this.payload = payload;
}
@Mobx.action
public setPayloadType(payloadType: Types.ElementActionPayloadType): void {
this.payloadType = payloadType;
}
@Mobx.action
public setOpen(open: boolean): void {
this.open = open;
}
public toJSON(): Types.SerializedElementAction {
return {
model: this.model,
elementPropertyId: this.elementPropertyId,
id: this.id,
open: this.open,
payload: this.payload,
payloadType: this.payloadType,
storeActionId: this.storeActionId,
storePropertyId: this.storePropertyId
};
}
@Mobx.action
public unsetStorePropertyId(): void {
this.storePropertyId = '';
}
@Mobx.action
public update(b: this): void {
this.id = b.id;
this.payload = b.payload;
this.payloadType = b.payloadType;
this.storeActionId = b.storeActionId;
this.storePropertyId = b.storePropertyId;
}
}