-
Notifications
You must be signed in to change notification settings - Fork 0
/
strolch-wc-component-behavior.html
244 lines (217 loc) · 8.83 KB
/
strolch-wc-component-behavior.html
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
<link rel="import" href="../polymer/polymer.html">
<link rel="import" href="../strolch-wc-localize-behavior/strolch-wc-localize-behavior.html">
<link rel="import" href="./strolch-wc-util-behavior.html">
<script>
// custom behaviour accessing the configuration above
StrolchComponentBehaviorImpl = {
//
// app functions
//
clearSearchTerm: function () {
this.fire("strolch-clear-search-term");
},
// fires an event that will show a dialog
showDialog: function (message, isError) {
this.fire("strolch-show-dialog", {
message: message,
isError: isError
});
},
// fires an event that will show a dialog
showDialogTitle: function (title, message) {
this.fire("strolch-show-dialog", {
title: title,
message: message
});
},
// fires an event that will show an information dialog, with optional callback
showInfo: function (data) {
if (data.callback != null)
data.callback.bind(this);
this.fire("strolch-show-info", data);
},
// fires an event that will show a notification below the toolbar
showNotification: function (id, message, faIcon, action1, callback1, action2, callback2, action3, callback3) {
this.fire("strolch-show-notification", {
id: id,
message: message,
faIcon: faIcon,
action1: action1,
callback1: callback1,
action2: action2,
callback2: callback2,
action3: action3,
callback3: callback3,
bind: this
});
},
clearNotification: function (id) {
this.fire("strolch-clear-notification", {
id: id
});
},
showError: function (title, text) {
this.showDialogTitle(title, text, true);
},
showToast: function (text) {
this.fire('strolch-show-toast', {text: text});
},
/**
* Set an environment property to a localized value
* @param version the version response from the server
*/
updateEnvironment: function (version) {
if (version == null || version.agentVersion == null || version.agentVersion.environment == null) {
this.environment = "-";
return;
}
var environment = version.agentVersion.environment;
if (this.localize == null) {
this.environment = environment;
this.async(function () {
if (this.localize == null) {
console.error("The localize function is still not available after 100ms of waiting! Cancelling.");
} else {
this.updateEnvironment(version);
}
}.bind(this), 100);
return;
}
if (environment.indexOf('prod') >= 0) {
this.environment = this.localize('production');
} else if (environment.indexOf('test') >= 0) {
this.environment = this.localize('testing');
} else if (environment.indexOf('stag') >= 0) {
this.environment = this.localize('staging');
} else if (environment.indexOf('dev') >= 0) {
this.environment = this.localize('development');
} else if (environment.indexOf('demo') >= 0) {
this.environment = this.localize('demo');
} else {
this.environment = environment;
}
},
//
// Navigation functions
//
// fires an event that will change the page of the app
changePage: function (page, keepQueryParams) {
this.fire("strolch-change-page", {
page: page,
keepQueryParams: typeof keepQueryParams === 'boolean' && keepQueryParams
});
},
//
// AJAX response helpers
//
localizeJsonMsg: function (jsonMsg) {
if (jsonMsg == null)
return jsonMsg;
this.localize.useKeyIfMissing = true;
try {
if (jsonMsg.values == null || Object.keys(jsonMsg.values).length === 0)
return this.localize(jsonMsg.key);
var values = [jsonMsg.key];
var keys = Object.keys(jsonMsg.values);
for (var i = 0; i < keys.length; i++) {
var key = keys[i];
values.push(key);
values.push(jsonMsg.values[key]);
}
return this.localize.apply(this.localize, values);
} catch (e) {
console.log(e);
return jsonMsg.key;
}
},
// convenience function to get the error message from responses
requestEventToUrl: function (event) {
return event.detail.detail.request.url;
},
onRequestError: function (event) {
var readyState = event.detail.request.xhr.readyState;
var status = event.detail.request.xhr.status;
if (readyState === 4 && status === 0) {
this.fire("strolch-server-not-available", event);
} else if (status === 401) {
this.fire("strolch-session-invalid", event);
} else if (status === 403) {
this.fire("strolch-privilege-denied", event);
} else {
this.requestErrorToMsg(event);
}
},
localizeServerI18nMessage: function (i18n) {
var args = [i18n.key];
if (i18n.values != null) {
Object.keys(i18n.values).forEach(function (key) {
args.push(key);
args.push(i18n.values[key]);
});
}
return this.localize.apply(this, args);
},
getResponseFromEvent: function (event) {
if (event.detail.request)
return event.detail.request.xhr.response;
return event.detail.detail.error.message;
},
localizeRequestErrorToMsg: function (event) {
var response = this.getResponseFromEvent(event);
var msg;
if (response && response.i18n) {
msg = this.localizeServerI18nMessage(response.i18n);
} else {
if (response && response.msg) {
msg = response.msg;
} else if (typeof (response) == 'string') {
if (response.trim().charAt(0) === '{') {
var json = JSON.parse(response);
if (json && json.i18n) {
msg = this.localizeServerI18nMessage(json.i18n);
} else if (json.msg) {
msg = json.msg;
} else {
msg = response;
}
} else {
msg = response;
}
} else if (event.detail && event.detail.error && event.detail.error.message && event.detail.request && event.detail.request.url) {
msg = event.detail.error.message + ": " + event.detail.request.url;
} else if (event.detail && event.detail.error && event.detail.error.message) {
msg = event.detail.error.message;
} else {
console.error("Missing error message on request error", event);
msg = "Request Failed and no message available!";
}
}
return msg;
},
requestErrorToMsg: function (event) {
var response = this.getResponseFromEvent(event);
var isError = response && response.state && response.state === "EXCEPTION";
var msg = this.localizeRequestErrorToMsg(event);
this.fire('strolch-show-dialog', {
isError: isError,
text: msg
});
},
//
// UI helper functions
//
onFocusSelectAll: function (e) {
if (e.target.inputElement) {
var input = e.target.inputElement;
if (typeof input.select === "function") {
input.select();
} else if (input.$ && input.$.textarea && typeof input.$.textarea.select === "function") {
input.$.textarea.select();
}
} else if (e.target.select) {
e.target.select();
}
},
};
StrolchComponentBehavior = [StrolchLocalizeBehavior, StrolchUtilBehavior, StrolchComponentBehaviorImpl];
</script>