-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuttonnotready.js
executable file
·233 lines (210 loc) · 7.45 KB
/
buttonnotready.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
var finesse = finesse || {};
finesse.gadget = finesse.gadget || {};
finesse.container = finesse.container || {};
/*global logFinesse */
finesse.modules = finesse.modules || {};
finesse.modules.buttonnotready = (function ($) {
var clientLogs,
user,
mediaList,
reasonCodes,
reasonCodeSelect, // NEW: We'll store a reference to the dropdown
// Called once the MediaList is initially loaded
handleMediaListLoaded = function () {
clientLogs.log("Media list has been loaded.");
populateGadget();
},
// Called whenever a new Media object is added
handleMediaAdd = function (media) {
clientLogs.log("Media added: " + media.getMediaType());
populateGadget();
},
// Called whenever a Media object is deleted
handleMediaDelete = function (media) {
clientLogs.log("Media removed: " + media.getMediaType());
populateGadget();
},
// Handler for change agent state failures
handleChangeStateError = function (error) {
clientLogs.log("handleChangeStateError(): Error changing state");
clientLogs.log("ErrorXML: " + error.content);
clientLogs.log("ErrorData: " + error.object.ApiErrors.ApiError.ErrorData);
clientLogs.log(
"ErrorMessage: " + error.object.ApiErrors.ApiError.ErrorMessage
);
clientLogs.log("ErrorType: " + error.object.ApiErrors.ApiError.ErrorType);
},
// NEW: Function to populate the reason code dropdown
populateReasonCodes = function () {
user.getNotReadyReasonCodes({
success: function (rsp) {
reasonCodes = rsp;
reasonCodeSelect.empty();
if (!reasonCodes || reasonCodes.length === 0) {
reasonCodeSelect.append(
$("<option></option>").val("").text("No reason codes found")
);
return;
}
$.each(reasonCodes, function (i, reasonCode) {
reasonCodeSelect.append(
$("<option></option>").val(reasonCode.id).text(reasonCode.label)
);
});
},
});
},
// Populates the fields in the gadget with data
populateGadget = function () {
var currentState = user.getState();
mediaList = user.getMediaList({
onCollectionAdd: handleMediaAdd,
onCollectionDelete: handleMediaDelete,
onLoad: handleMediaListLoaded,
});
var mediaCollection = mediaList.getCollection();
var mrdNames = [];
var mrdStates = [];
for (var mediaId in mediaCollection) {
if (mediaCollection.hasOwnProperty(mediaId)) {
var media = mediaCollection[mediaId];
if (media.isLoggedIn()) {
mrdNames.push(media.getName());
mrdStates.push(media.getState());
clientLogs.log(
"User is in Logged in to" +
media.getName() +
"with ID " +
media.getMediaId() +
"and current state is " +
media.getState()
);
}
}
}
$("#mrdNames").text(mrdNames.join(", "));
$("#mrdStates").text(mrdStates.join(", "));
// Populate other fields
$("#userId").text(user.getId());
$("#firstName").text(user.getFirstName());
$("#lastName").text(user.getLastName());
if (user.hasSupervisorRole()) {
$("#userRole").text("Supervisor");
} else {
$("#userRole").text("Agent");
}
$("#extension").text(user.getExtension());
$("#userState").text(currentState);
$("#teamId").text(user.getTeamId());
$("#teamName").text(user.getTeamName());
// Show/hide the state change buttons (existing logic)
if (currentState === finesse.restservices.User.States.NOT_READY) {
$("#goReady").show();
$("#goNotReady").hide();
} else if (currentState === finesse.restservices.User.States.READY) {
$("#goNotReady").show();
$("#goReady").hide();
} else {
$("#goNotReady").hide();
$("#goReady").hide();
}
// Adjust gadget height
gadgets.window.adjustHeight();
},
// Handler for all User updates
handleUserChange = function (userevent) {
populateGadget();
},
// Handler for the onLoad of the User object
handleUserLoad = function (userevent) {
// Populate reason codes once the user object is loaded
populateReasonCodes();
// Now populate the rest of the gadget
populateGadget();
};
return {
/**
* Sets the user state (e.g. READY or NOT_READY).
* MODIFIED: We now pass the selected reason code if the state is NOT_READY
*/
setUserState: function (state) {
mediaList = user.getMediaList({
onCollectionAdd: handleMediaAdd,
onCollectionDelete: handleMediaDelete,
onLoad: handleMediaListLoaded,
});
var mediaCollection = mediaList.getCollection();
var selectedReasonCode = null;
if (state === finesse.restservices.User.States.NOT_READY) {
var selectedReasonCodeId = reasonCodeSelect.val();
// Find the matching ReasonCode object
$.each(reasonCodes, function (i, reasonCode) {
if (reasonCode.id === selectedReasonCodeId) {
selectedReasonCode = reasonCode;
return false; // break out of the loop
}
});
clientLogs.log("Selected ReasonCode ID: " + selectedReasonCodeId);
}
// For each logged-in media, set its state
for (var mediaId in mediaCollection) {
if (mediaCollection.hasOwnProperty(mediaId)) {
var media = mediaCollection[mediaId];
if (media.isLoggedIn()) {
if (state === finesse.restservices.User.States.NOT_READY) {
// Pass in the entire ReasonCode object
media.setState(state, selectedReasonCode, {
error: handleChangeStateError,
});
} else {
media.setState(state, null, {
error: handleChangeStateError,
});
}
}
}
}
// Then set the overall user state after some delay
setTimeout(function () {
if (state === finesse.restservices.User.States.NOT_READY) {
user.setState(state, selectedReasonCode, {
error: handleChangeStateError,
});
} else {
user.setState(state, null, {
error: handleChangeStateError,
});
}
}, 4000);
},
/**
* Gadget initialization
*/
init: function () {
var cfg = finesse.gadget.Config;
// Initialize logging
clientLogs = finesse.cslogger.ClientLogger;
clientLogs.init(gadgets.Hub, "buttonnotready", cfg);
// Keep a handle to the reason code dropdown
reasonCodeSelect = $("#reasonCodeSelect"); // NEW
// Initialize the BOSH connection
finesse.clientservices.ClientServices.init(cfg);
// ContainerServices for tab handling
containerServices = finesse.containerservices.ContainerServices.init();
containerServices.addHandler(
finesse.containerservices.ContainerServices.Topics.ACTIVE_TAB,
function () {
clientLogs.log("Gadget is now visible");
gadgets.window.adjustHeight();
}
);
containerServices.makeActiveTabReq();
// Create the User object for the logged in user
user = new finesse.restservices.User({
id: cfg.id,
onLoad: handleUserLoad,
onChange: handleUserChange,
});
},
};
})(jQuery);