-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpowtho_jslib_v1.js
More file actions
265 lines (229 loc) · 8.72 KB
/
Copy pathpowtho_jslib_v1.js
File metadata and controls
265 lines (229 loc) · 8.72 KB
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
265
// Note:
// Replace the PowerThomas namespace with your own.
var PowerThomas = PowerThomas || {};
(function () {
"use strict";
// If true, various messages are logged in the console.
const debug = true;
const logIndentation = " ";
// The line used in logs.
const logLine = "--------------------------------------------------";
/**
* This function checks if the form is valid and if there are changes.
* If it is NOT valid or there are changes, the form will be saved.
* As it is not valid, the error notifications will be created.
* True is returned if the form was saved successfully, false otherwise.
*
* @param {object} executionContext The Power Platform execution context (PrimaryControl on forms).
*/
async function trySave(executionContext) {
const isValid = executionContext?.data?.isValid?.();
const isDirty = executionContext?.data?.getIsDirty?.();
// Save the current form
if (!isValid || isDirty) {
Xrm.Utility.showProgressIndicator("Loading...");
if (!isValid)
console.debug(`Calling "save" as the record is invalid. This will create all notifications.`);
else if (isDirty)
console.debug(`Calling "save" as the record is valid, but changed.`);
try {
await executionContext.data.save();
}
catch (ex) {
return false;
}
finally {
Xrm.Utility.closeProgressIndicator();
}
}
else
console.debug(`No need to save. Everything is up to date.`);
return true;
}
this.TrySave = trySave;
/**
* Removes the curly brackets from the usually passed row ID.
*
* @param {string} id The entity ID.
* @returns {string} The id as an actual guid (uppercase, no braces).
*/
function cleanID(id) {
return id ? String(id).replace(/[{}]/g, "").toUpperCase() : null;
}
this.CleanID = cleanID;
/**
* Resolve the first selected record id based on context and sourceType.
* Returns a clean GUID (without braces) or null.
*
* @param {object} ctx PrimaryControl (form) or SelectedControl (view/subgrid)
* @param {number} sourceType 1 = form, 2 = view/subgrid (must be numeric)
*/
function resolveRecordIdFromContext(ctx, sourceType) {
// Form context (PrimaryControl)
if (sourceType === 1) {
const rawId = ctx?.data?.entity?.getId?.();
return cleanID(rawId);
}
// View/Subgrid context (SelectedControl is usually a GridControl)
if (sourceType === 2) {
const grid = (typeof ctx?.getGrid === "function") ? ctx.getGrid() : ctx;
const rows = grid?.getSelectedRows?.();
if (!rows) return null;
const row = rows.get ? rows.get(0) : rows.getAll?.()[0];
if (!row) return null;
// Prefer modern shape; fall back to legacy methods for back-compat
const data = row?.data ?? row?.getData?.();
const entity = data?.entity ?? data?.getEntity?.();
const rawId = entity?.getId?.() ?? entity?.Id ?? entity?.id ?? null;
return cleanID(rawId);
}
return null;
}
this.ResolveRecordIdFromContext = resolveRecordIdFromContext;
/**
* Refresh current context after the dialog closes.
* - Form (1): refresh data + ribbon
* - Grid/Subgrid (2): prefer GridControl.refresh(); fall back to Grid.refresh()
*/
async function refreshContext(executionContext, sourceType) {
if (sourceType === 1 && executionContext?.data) {
await executionContext.data.refresh();
await executionContext.ui?.refreshRibbon?.(true);
return;
}
if (sourceType === 2 && executionContext) {
// SelectedControl is typically a GridControl; .refresh() is usually here
const gridControl = (typeof executionContext.getGrid === "function") ? executionContext : null;
const grid = gridControl?.getGrid?.() ?? executionContext;
// Prefer control.refresh(); fallback to grid.refresh()
if (typeof gridControl?.refresh === "function") {
await gridControl.refresh();
return;
}
if (typeof grid?.refresh === "function") {
await grid.refresh();
return;
}
// Optional: als niets beschikbaar is, log het zodat we weten wat er binnenkwam
console.warn("[refreshContext] No refresh() available on SelectedControl or Grid.");
}
}
/**
* Opens a Page.
*
* @param {object} executionContext The event context from which this function is executed.
* For a command bar button: on forms this is **PrimaryControl**; on views/subgrids this is **SelectedControl**.
* @param {number} sourceType The type of source from where the Page is opened. 1 = form, 2 = view/subgrid
* @param {string} pageTitle The title of the Page to open.
* @param {string} pageType The type of Page, e.g. "custom".
* @param {string} pageLogicalName The logical name of the Page to open.
* @param {string} entityLogicalName The logical name of the entity from which the page is opened.
* @param {number} target How to open the Page. 1 = full page, 2 = dialog
* @param {number} position If the target is set to 2, this defines the type of dialog. 1 = centered, 2 = sidepane.
* @param {number} widthValue The width of the page.
* @param {number} heightValue The height of the page.
* @param {string} widthUnit The unit used to determine the width, e.g. "px" or "%".
* @param {string} heightUnit The unit used to determine the height, e.g. "px" or "%".
* @param {boolean} refreshOnClose Whether to refresh the current context once the Page is closed (successfully).
* On forms: refresh form + ribbon. On views/subgrids: refresh grid (if supported).
* @param {boolean} navigateToOverviewOnClose If true, navigates to the overview of the current entity once the Page is closed (successfully).
*/
async function openPage(
executionContext, sourceType, pageTitle, pageType, pageLogicalName,
entityLogicalName, target, position, widthValue, heightValue,
widthUnit, heightUnit, refreshOnClose, navigateToOverviewOnClose
) {
log("openPage", arguments);
try {
// Only save when coming from a FORM (PrimaryControl).
if (sourceType === 1 && executionContext) {
if (!await trySave(executionContext)) return;
}
// Determine recordId based on source type
const recordID = resolveRecordIdFromContext(executionContext, sourceType);
// Gather input for navigation
let pageInput = {
pageType: pageType,
name: pageLogicalName,
entityName: entityLogicalName,
recordId: recordID
};
let navigationOptions = {
target: target,
position: position,
width: { value: widthValue, unit: widthUnit },
height: { value: heightValue, unit: heightUnit },
title: pageTitle
};
// Navigate to that page - this awaits until the page is closed.
await Xrm.Navigation.navigateTo(pageInput, navigationOptions);
// Show this indicator again, as the page was just closed.
Xrm.Utility.showProgressIndicator("Loading...");
// After closing: refresh current context or navigate to overview
if (refreshOnClose) {
await refreshContext(executionContext, sourceType);
} else if (navigateToOverviewOnClose) {
console.log("Navigating to overview...");
const pageInput = { pageType: "entitylist", entityName: entityLogicalName };
await Xrm.Navigation.navigateTo(pageInput);
}
}
catch (ex) {
console.error(`An error occurred while opening a page. Details:`);
console.error(ex);
}
finally {
Xrm.Utility.closeProgressIndicator();
}
}
this.OpenPage = openPage;
/**
* Logs a title, plus a line for each key-value pair found in the values.
*
* @param {string} title The title for the log.
* @param {object} values A key-value pair array. This includes be JSON. You can also pass an array.
*/
function log(title, value) {
if (!debug)
return;
console.log(logLine);
console.log(title);
if (Array.isArray(value)) {
if (value.length == 0)
console.log("[]");
else
logArray(value);
}
else if (typeof value === 'object') {
if (value === null)
console.log("null");
else
logObject(value);
}
else
console.log(value);
console.log(logLine);
}
this.Log = log;
/**
* Logs an array.
*
* @param {array} array The array to log.
*/
function logArray(array) {
if (!debug)
return;
for (let i = 0; i < array.length; i++)
console.log(i, array[i]);
}
/**
* Logs an object.
*
* @param {object} obj The object to log.
*/
function logObject(obj) {
if (!debug)
return;
Object.keys(obj).forEach(key => console.log(key + ": " + obj[key]));
}
}).call(PowerThomas);