kc-sdk 7.0.152-rc → 7.0.195-rc

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,732 +1,743 @@
1
1
  'use strict';
2
2
 
3
- var kcSdkVersion = '7.0.152';
3
+ var kcSdkVersion = '7.0.195';
4
4
 
5
5
  var kcSdk = function kcSdk() {
6
- // locals
7
- var communicationPacket = {
8
- sender: 'kcSdk',
9
- windowName: '',
10
- action: '',
11
- value: null
12
- };
13
- var workflow = [];
14
- var callbackFns = [];
15
-
16
- var responseTime;
17
- var timeoutThresholdSeconds = 30;
18
-
19
- // window locals
20
- var kcWindowName = 'kc-sdk-launched-instance';
21
- var kcWindowParams = ['height=' + screen.height, 'width=' + screen.width, 'location=0', 'top=10', 'left=10'].join(',');
22
-
23
- var encoderWindowName = 'encoder-sdk-launched-instance';
24
- var encoderWindowParams = 'resizable=1,scrollbars=0,location=0,menubar=0,toolbar=0,top=10,left=10,width=950,height=500';
25
-
26
- var kcWindow = {
27
- reference: null,
28
- opened: false,
29
- name: kcWindowName,
30
- currentWorkflow: {},
31
- responseTime: responseTime,
32
- launch: launchKc,
33
- loadPatientRecord: loadPatientRecord,
34
- assignConfig: assignConfig,
35
- setAuthToken: setAuthToken,
36
- setI9I10State: setI9I10State,
37
- setDemographics: setDemographics,
38
- addCodes: addCodes,
39
- clearPatientRecord: clearPatientRecord,
40
- closeAllKCWindowsExceptEncoder: closeAllKCWindowsExceptEncoder,
41
- deleteCodes: deleteCodes,
42
- getPayerList: getPayerList,
43
- getRowSpecificInfo: getRowSpecificInfo,
44
- clearSelectedRow: clearSelectedRow,
45
- hasCriticalEdits: hasCriticalEdits,
46
- hasEdits: hasEdits,
47
- launchCodeSummaryReport: launchCodeSummaryReport,
48
- launchEdits: launchEdits,
49
- launchFacilityLookup: launchFacilityLookup,
50
- launchIndex: launchIndex,
51
- launchLogic: launchLogic,
52
- launchTabular: launchTabular,
53
- logout: logout,
54
- openEncoder: openEncoderWindow,
55
- resetApplication: resetApplication,
56
- savePatientRecord: savePatientRecord,
57
- updateCodes: updateCodes,
58
- updateDemographics: updateDemographics,
59
- authTokenData: {}
60
- };
61
-
62
- var encoderWindow = {
63
- reference: null,
64
- name: encoderWindowName,
65
- opened: false,
66
- currentWorkflow: {},
67
- responseTime: responseTime,
68
- launch: launchEncoder,
69
- setAuthToken: setAuthToken,
70
- setI9I10State: setI9I10State,
71
- setDemographics: setDemographics,
72
- launchIndex: launchIndex,
73
- launchLogic: launchLogic,
74
- launchTabular: launchTabular,
75
- authTokenData: {}
76
- };
77
-
78
- // service
79
- var service = {
80
- encoderTabs: encoderTabs,
81
- sessionTypes: sessionTypes,
82
- patientType: patientType,
83
- i9I10States: i9I10States,
84
- listenerResponse: listenerResponse,
85
- kcWindow: kcWindow,
86
- encoderWindow: encoderWindow,
87
- closeAllKCWindows: closeAllKCWindows,
88
- kcWindowIsListening: false,
89
- encoderWindowIsListening: false,
90
- launchReferences: kcMreService.launchMre,
91
- setCallback: setCallback,
92
- callbacks: callbacks,
93
- workflow: workflow,
94
- addToWorkflow: addToWorkflow,
95
- removeFromWorkflow: removeFromWorkflow,
96
- processWorkflow: processWorkflow,
97
- clearWorkflow: clearWorkflow,
98
- workflowComplete: true,
99
- listenerException: false,
100
- version: kcSdkVersion
101
- };
102
-
103
- // listener for opened window messages
104
- window.addEventListener(
105
- 'message',
106
- function (e) {
107
- if (event.data.callback === 'kcRefreshed' && service.kcWindow.reference) {
108
- service.kcWindow.opened = true;
109
- service.kcWindowIsListening = true;
110
- } else if (event.data.callback === 'encoderRefreshed' && service.encoderWindow.reference) {
111
- service.encoderWindow.opened = true;
112
- service.encoderWindowIsListening = true;
113
- }
114
-
115
- if (!(service.kcWindow.reference && event.data.sender === 'kcWindow') && !(service.encoderWindow.reference && event.data.sender === 'encoderWindow')) {
116
- return;
117
- }
118
-
119
- var needsAuthentication = false;
120
- var callback = event.data.callback;
121
-
122
- switch (event.data.action) {
123
- case 'sdkServiceResponse':
124
- if (event.data.sender === 'kcWindow') {
125
- if (event.data.callback === 'kcListeningChanged') {
126
- service.kcWindowIsListening = service.listenerResponse.OPENKC === event.data.value;
127
- if (service.kcWindowIsListening) {
128
- needsAuthentication = service.workflow.length === 0 || (service.workflow.length > 0 && service.workflow[0].name !== 'setAuthToken');
129
- } else {
130
- closeKcWindow();
131
- }
132
- } else if (event.data.callback === 'kcRefreshed') {
133
- callback = 'sdkServiceResponse';
134
- clearWorkflow();
135
- } else {
136
- callback = 'sdkServiceResponse';
137
- }
138
- } else if (event.data.sender === 'encoderWindow') {
139
- if (event.data.callback === 'encoderListeningChanged') {
140
- service.encoderWindowIsListening = service.listenerResponse.OPENENCODER === event.data.value;
141
- if (service.encoderWindowIsListening) {
142
- needsAuthentication = service.workflow.length === 0 || (service.workflow.length > 0 && service.workflow[0].name !== 'setAuthToken');
143
- } else {
144
- closeEncoderWindow();
145
- }
146
- } else if (event.data.callback === 'encoderRefreshed') {
147
- callback = 'sdkServiceResponse';
148
- clearWorkflow();
149
- } else {
150
- callback = 'sdkServiceResponse';
151
- }
152
- }
153
- break;
154
- case 'sdkRequestException':
155
- service.listenerException = true;
156
- callback = event.data.action;
157
- break;
158
- default:
159
- break;
160
- }
161
-
162
- invokeCallback(callback, event.data);
163
-
164
- if (service.listenerException) {
165
- stopWorkflow();
166
- service.listenerException = false;
167
- }
168
-
169
- var cwf = event.data.sender === 'kcWindow' ? service.kcWindow.currentWorkflow : service.encoderWindow.currentWorkflow;
170
-
171
- if (!cwf) {
172
- stopWorkflow();
173
- return;
174
- }
175
-
176
- if (needsAuthentication) {
177
- service.workflow.unshift({
178
- name: 'setAuthToken',
179
- fn: service[event.data.sender].setAuthToken,
180
- sendToWindow: [event.data.sender],
181
- args: [service[event.data.sender].authTokenData, service[event.data.sender].name]
182
- });
183
- }
184
-
185
- if (service.workflow.length === 0) {
186
- clearTimeout(service[event.data.sender].responseTime);
187
- }
188
-
189
- if (cwf.workflowCompleteMessage === event.data.value) {
190
- clearTimeout(service[event.data.sender].responseTime);
191
- processWorkflow(service[event.data.sender]);
192
- }
193
- },
194
- false
195
- );
196
-
197
- window.addEventListener('unload', function (event) {
198
- service.closeAllKCWindows();
199
- });
200
-
201
- return service;
202
- /////////////// return service
203
-
204
- function openKc(url, authToken) {
205
- if (!service.kcWindow.reference || !windowExists(service.kcWindow) || !service.kcWindowIsListening) {
206
- kcWindow.authTokenData = authToken;
207
-
208
- service.kcWindow.reference = window.open(url, kcWindowName, kcWindowParams);
209
- service.kcWindow.opened = true;
210
- service.kcWindow.reference.focus();
211
- } else {
212
- service.kcWindow.reference.focus();
213
- processWorkflow(service.kcWindow);
214
- }
215
- }
216
-
217
- function openEncoder(url, authToken) {
218
- if (!service.encoderWindow.reference || !windowExists(service.encoderWindow) || !service.encoderWindowIsListening) {
219
- encoderWindow.authTokenData = authToken;
220
-
221
- service.encoderWindow.reference = window.open(url, encoderWindowName, encoderWindowParams);
222
- service.encoderWindow.opened = true;
223
- service.encoderWindow.reference.focus();
224
- } else {
225
- service.encoderWindow.reference.focus();
226
- processWorkflow(service.encoderWindow);
227
- }
228
- }
229
-
230
- // apis to send to our open windows
231
- function loadPhraseInLogic(input) {
232
- communicationPacket.action = 'logicRequest';
233
- communicationPacket.value = input;
234
- communication(communicationPacket, listeningWindows.ENCODER);
235
- }
236
-
237
- function loadPhraseInIndex(input) {
238
- communicationPacket.action = 'indexRequest';
239
- communicationPacket.value = input;
240
- communication(communicationPacket, listeningWindows.ENCODER);
241
- }
242
-
243
- function loadCodeInTabular(input) {
244
- communicationPacket.action = 'tabularRequest';
245
- communicationPacket.value = input;
246
- communication(communicationPacket, listeningWindows.ENCODER);
247
- }
248
-
249
- function loadPatientRecord(patientRecord) {
250
- communicationPacket.action = 'loadPatientRecord';
251
- communicationPacket.value = patientRecord;
252
- communication(communicationPacket, listeningWindows.KC);
253
- }
254
-
255
- function assignConfig(input) {
256
- communicationPacket.action = 'assignConfig';
257
- communicationPacket.value = input;
258
- communication(communicationPacket, listeningWindows.KC);
259
- }
260
-
261
- function setI9I10State(state) {
262
- communicationPacket.action = 'setI9I10State';
263
- communicationPacket.value = state;
264
- communication(communicationPacket, listeningWindows.ALL);
265
- }
266
- function setAuthToken(data) {
267
- communicationPacket.action = 'setAuthToken';
268
- communicationPacket.value = data;
269
- communication(communicationPacket, listeningWindows.ALL);
270
- }
271
-
272
- function setDemographics(demographicsData) {
273
- communicationPacket.action = 'setDemographics';
274
- communicationPacket.value = demographicsData;
275
- communication(communicationPacket, listeningWindows.ALL);
276
- }
277
-
278
- function encoderStartupComplete() {
279
- communicationPacket.action = 'encoderStartupComplete';
280
- communicationPacket.value = '';
281
- communication(communicationPacket, listeningWindows.ENCODER);
282
- }
283
-
284
- function closeAllKCWindows() {
285
- communicationPacket.action = 'closeAllKCWindows';
286
- communicationPacket.value = null;
287
- communication(communicationPacket, listeningWindows.ALL);
288
- }
289
-
290
- function addCodes(codesData) {
291
- communicationPacket.action = 'addCodes';
292
- communicationPacket.value = codesData;
293
- communication(communicationPacket, listeningWindows.KC);
294
- if(service.kcWindow.opened === true) {
295
- service.kcWindow.reference.focus();
296
- }
297
- }
298
-
299
- function clearPatientRecord() {
300
- communicationPacket.action = 'clearPatientRecord';
301
- communicationPacket.value = null;
302
- communication(communicationPacket, listeningWindows.KC);
303
- if (service.kcWindow.opened === true) {
304
- service.kcWindow.reference.focus();
305
- }
306
- }
307
-
308
- function closeAllKCWindowsExceptEncoder() {
309
- communicationPacket.action = 'closeAllKCWindowsExceptEncoder';
310
- communicationPacket.value = null;
311
- communication(communicationPacket, listeningWindows.ALL);
312
- }
313
-
314
- function deleteCodes(codes) {
315
- communicationPacket.action = 'deleteCodes';
316
- communicationPacket.value = codes;
317
- communication(communicationPacket, listeningWindows.KC);
318
- if (service.kcWindow.opened === true) {
319
- service.kcWindow.reference.focus();
320
- }
321
- }
322
-
323
- function getPayerList(payerList) {
324
- communicationPacket.action = 'getPayerList';
325
- communicationPacket.value = payerList;
326
- communication(communicationPacket, listeningWindows.KC);
327
- }
328
-
329
- function getRowSpecificInfo(uniqueIds) {
330
- communicationPacket.action = 'getRowSpecificInfo';
331
- communicationPacket.value = uniqueIds;
332
- communication(communicationPacket, listeningWindows.KC);
333
- }
334
-
335
- function clearSelectedRow() {
336
- communicationPacket.action = 'clearSelectedRow';
337
- communicationPacket.value = null;
338
- communication(communicationPacket, listeningWindows.KC);
339
- if (service.kcWindow.opened === true) {
340
- service.kcWindow.reference.focus();
341
- }
342
- }
343
- function hasCriticalEdits() {
344
- communicationPacket.action = 'hasCriticalEdits';
345
- communicationPacket.value = null;
346
- communication(communicationPacket, listeningWindows.KC);
347
- }
348
- function hasEdits() {
349
- communicationPacket.action = 'hasEdits';
350
- communicationPacket.value = null;
351
- communication(communicationPacket, listeningWindows.KC);
352
- }
353
- function launchCodeSummaryReport() {
354
- communicationPacket.action = 'launchCodeSummaryReport';
355
- communicationPacket.value = null;
356
- communication(communicationPacket, listeningWindows.KC);
357
- }
358
- function launchEdits() {
359
- communicationPacket.action = 'launchEdits';
360
- communicationPacket.value = null;
361
- communication(communicationPacket, listeningWindows.KC);
362
- }
363
- function launchFacilityLookup(codes) {
364
- communicationPacket.action = 'launchFacilityLookup'
365
- communicationPacket.value = codes;
366
- communication(communicationPacket, listeningWindows.KC)
367
- }
368
- function launchIndex(codes) {
369
- communicationPacket.action = 'launchIndex';
370
- communicationPacket.value = codes;
371
- communication(communicationPacket, service.encoderWindowIsListening ? listeningWindows.ENCODER : listeningWindows.KC);
372
- if (service.encoderWindow.opened) {
373
- service.encoderWindow.reference.focus();
374
- }
375
-
376
- }
377
- function launchLogic(codes) {
378
- communicationPacket.action = 'launchLogic';
379
- communicationPacket.value = codes;
380
- communication(communicationPacket, service.encoderWindowIsListening ? listeningWindows.ENCODER : listeningWindows.KC);
381
- if (service.encoderWindow.opened == true) {
382
- service.encoderWindow.reference.focus();
383
- }
384
-
385
- }
386
- function launchTabular(codes) {
387
- communicationPacket.action = 'launchTabular';
388
- communicationPacket.value = codes;
389
- communication(communicationPacket, service.encoderWindowIsListening ? listeningWindows.ENCODER : listeningWindows.KC);
390
- if (service.encoderWindow.opened == true) {
391
- service.encoderWindow.reference.focus();
392
- }
393
- }
394
- function logout() {
395
- communicationPacket.action = 'logout';
396
- communicationPacket.value = null;
397
- communication(communicationPacket, listeningWindows.KC);
398
- if (service.kcWindow.opened === true) {
399
- service.kcWindow.reference.focus();
400
- }
401
- }
402
- function resetApplication() {
403
- communicationPacket.action = 'resetApplication';
404
- communicationPacket.value = null;
405
- communication(communicationPacket, listeningWindows.KC);
406
- if (service.kcWindow.opened === true) {
407
- service.kcWindow.reference.focus();
408
- }
409
- }
410
- function savePatientRecord() {
411
- communicationPacket.action = 'savePatientRecord';
412
- communicationPacket.value = null;
413
- communication(communicationPacket, listeningWindows.KC);
414
- if (service.kcWindow.opened === true) {
415
- service.kcWindow.reference.focus();
416
- }
417
- }
418
- function updateCodes(codes) {
419
- communicationPacket.action = 'updateCodes';
420
- communicationPacket.value = codes;
421
- communication(communicationPacket, listeningWindows.KC);
422
- if (service.kcWindow.opened === true) {
423
- service.kcWindow.reference.focus();
424
- }
425
- }
426
- function updateDemographics(updatedDemographics) {
427
- communicationPacket.action = 'updateDemographics';
428
- communicationPacket.value = updatedDemographics;
429
- communication(communicationPacket, listeningWindows.KC);
430
- if (service.kcWindow.opened === true) {
431
- service.kcWindow.reference.focus();
432
- }
433
- }
434
-
435
- function openEncoderWindow() {
436
- communicationPacket.action = 'openEncoder';
437
- communicationPacket.value = null;
438
- communication(communicationPacket,service.encoderWindowIsListening ? listeningWindows.ENCODER : listeningWindows.KC);
439
- if (service.encoderWindow.opened == true) {
440
- service.encoderWindow.reference.focus();
441
- }
442
- }
443
-
444
- // workflow to launch kc
445
- function launchKc(url, authToken, state, demographics) {
446
- kcWindow.authTokenData = authToken;
447
-
448
- var wfArray = [
449
- {
450
- fn: openKc,
451
- args: [url, authToken]
452
- },
453
- {
454
- fn: setI9I10State,
455
- args: state
456
- },
457
- {
458
- fn: setDemographics,
459
- args: demographics
460
- }
461
- ];
462
-
463
- var updatedWf = wfArray.concat(service.workflow);
464
- service.clearWorkflow();
465
-
466
- service.addToWorkflow(updatedWf);
467
-
468
- service.processWorkflow(service.kcWindow);
469
- }
470
-
471
- // workflow to launch encoder
472
- function launchEncoder(url, authToken, state, demographics, request) {
473
- encoderWindow.authTokenData = authToken;
474
-
475
- var logicIndexRequest = JSON.stringify(
476
- {
477
- sessionType: request.sessionType,
478
- phrase: request.codePhrase
479
- },
480
- null,
481
- 2
482
- );
483
-
484
- var tabularRequest = JSON.stringify(
485
- {
486
- sessionType: request.sessionType,
487
- code: request.codePhrase
488
- },
489
- null,
490
- 2
491
- );
492
-
493
- var wfArray = [
494
- {
495
- fn: openEncoder,
496
- args: [url, authToken]
497
- },
498
- {
499
- fn: setI9I10State,
500
- args: state
501
- },
502
- {
503
- fn: setDemographics,
504
- args: demographics
505
- },
506
- {
507
- fn: encoderStartupComplete,
508
- args: []
509
- }
510
- ];
511
-
512
- if (request.encoderTab === service.encoderTabs.LOGIC) {
513
- wfArray.push({
514
- fn: loadPhraseInLogic,
515
- args: logicIndexRequest
516
- });
517
- } else if (request.encoderTab === service.encoderTabs.INDEX) {
518
- wfArray.push({
519
- fn: loadPhraseInIndex,
520
- args: logicIndexRequest
521
- });
522
- } else if (request.encoderTab === service.encoderTabs.TABULAR) {
523
- wfArray.push({
524
- fn: loadCodeInTabular,
525
- args: tabularRequest
526
- });
527
- }
528
-
529
- var updatedWf = wfArray.concat(service.workflow);
530
- service.clearWorkflow();
531
-
532
- service.addToWorkflow(updatedWf);
533
-
534
- service.processWorkflow(service.encoderWindow);
535
- }
536
-
537
- // window functions
538
- function communication(packet, listener) {
539
- try {
540
- var ourWindows = getWindowsToCommunicate(listener);
541
-
542
- if (ourWindows.length === 0) {
543
- console.log(new Error('No window listening'));
544
- return;
545
- }
546
-
547
- if (packet.value) {
548
- packet.value = typeof packet.value === 'string' ? JSON.parse(packet.value) : packet.value;
549
- }
550
- ourWindows.forEach(function (w) {
551
- w.responseTime = 0;
552
- packet.windowName = w.name;
553
- messagingProvider.post(w.reference, packet);
554
- responseTimeout(packet, w);
555
- });
556
- } catch (e) {
557
- console.log(e);
558
- stopWorkflow();
559
- invokeCallback(service.callbacks.SDKEXCEPTION, { callback: packet.action, value: e });
560
- }
561
- }
562
-
563
- function getWindowsToCommunicate(listener) {
564
- var windowArray = [];
565
-
566
- switch (listener) {
567
- case listeningWindows.ENCODER:
568
- if (service.encoderWindowIsListening) {
569
- windowArray.push(service.encoderWindow);
570
- }
571
- break;
572
-
573
- case listeningWindows.KC:
574
- if (service.kcWindowIsListening) {
575
- windowArray.push(service.kcWindow);
576
- }
577
- break;
578
-
579
- case listeningWindows.ALL:
580
- if (service.kcWindowIsListening) {
581
- windowArray.push(service.kcWindow);
582
- }
583
- if (service.encoderWindowIsListening) {
584
- windowArray.push(service.encoderWindow);
585
- }
586
-
587
- break;
588
-
589
- default:
590
- return [];
591
- }
592
-
593
- return windowArray.filter(function (w) {
594
- return w !== null && windowExists(w);
595
- });
596
- }
597
-
598
- function windowExists(window) {
599
- if (!window.opened) {
600
- if (!window.name) {
6
+ // locals
7
+ var communicationPacket = {
8
+ sender: 'kcSdk',
9
+ windowName: '',
10
+ action: '',
11
+ value: null
12
+ };
13
+ var workflow = [];
14
+ var callbackFns = [];
15
+
16
+ var responseTime;
17
+ var timeoutThresholdSeconds = 30;
18
+
19
+ // window locals
20
+ var kcWindowName = 'kc-sdk-launched-instance';
21
+ var kcWindowParams = ['height=' + screen.height, 'width=' + screen.width, 'location=0', 'top=10', 'left=10'].join(',');
22
+
23
+ var encoderWindowName = 'encoder-sdk-launched-instance';
24
+ var encoderWindowParams = 'resizable=1,scrollbars=0,location=0,menubar=0,toolbar=0,top=10,left=10,width=950,height=500';
25
+
26
+ var kcWindow = {
27
+ reference: null,
28
+ opened: false,
29
+ name: kcWindowName,
30
+ currentWorkflow: {},
31
+ responseTime: responseTime,
32
+ launch: launchKc,
33
+ openKcSentinel: openKcSentinel,
34
+ loadPatientRecord: loadPatientRecord,
35
+ assignConfig: assignConfig,
36
+ setAuthToken: setAuthToken,
37
+ setI9I10State: setI9I10State,
38
+ setDemographics: setDemographics,
39
+ addCodes: addCodes,
40
+ clearPatientRecord: clearPatientRecord,
41
+ closeAllKCWindowsExceptEncoder: closeAllKCWindowsExceptEncoder,
42
+ deleteCodes: deleteCodes,
43
+ getPayerList: getPayerList,
44
+ getRowSpecificInfo: getRowSpecificInfo,
45
+ clearSelectedRow: clearSelectedRow,
46
+ hasCriticalEdits: hasCriticalEdits,
47
+ hasEdits: hasEdits,
48
+ launchCodeSummaryReport: launchCodeSummaryReport,
49
+ launchEdits: launchEdits,
50
+ launchFacilityLookup: launchFacilityLookup,
51
+ launchIndex: launchIndex,
52
+ launchLogic: launchLogic,
53
+ launchTabular: launchTabular,
54
+ logout: logout,
55
+ openEncoder: openEncoderWindow,
56
+ resetApplication: resetApplication,
57
+ savePatientRecord: savePatientRecord,
58
+ updateCodes: updateCodes,
59
+ updateDemographics: updateDemographics,
60
+ authTokenData: {}
61
+ };
62
+
63
+ var encoderWindow = {
64
+ reference: null,
65
+ name: encoderWindowName,
66
+ opened: false,
67
+ currentWorkflow: {},
68
+ responseTime: responseTime,
69
+ launch: launchEncoder,
70
+ setAuthToken: setAuthToken,
71
+ setI9I10State: setI9I10State,
72
+ setDemographics: setDemographics,
73
+ launchIndex: launchIndex,
74
+ launchLogic: launchLogic,
75
+ launchTabular: launchTabular,
76
+ authTokenData: {}
77
+ };
78
+
79
+ // service
80
+ var service = {
81
+ encoderTabs: encoderTabs,
82
+ sessionTypes: sessionTypes,
83
+ patientType: patientType,
84
+ i9I10States: i9I10States,
85
+ listenerResponse: listenerResponse,
86
+ kcWindow: kcWindow,
87
+ encoderWindow: encoderWindow,
88
+ closeAllKCWindows: closeAllKCWindows,
89
+ kcWindowIsListening: false,
90
+ encoderWindowIsListening: false,
91
+ launchReferences: kcMreService.launchMre,
92
+ setCallback: setCallback,
93
+ callbacks: callbacks,
94
+ workflow: workflow,
95
+ addToWorkflow: addToWorkflow,
96
+ removeFromWorkflow: removeFromWorkflow,
97
+ processWorkflow: processWorkflow,
98
+ clearWorkflow: clearWorkflow,
99
+ workflowComplete: true,
100
+ listenerException: false,
101
+ version: kcSdkVersion
102
+ };
103
+
104
+ // listener for opened window messages
105
+ window.addEventListener(
106
+ 'message',
107
+ function (e) {
108
+ if (event.data.callback === 'kcRefreshed' && service.kcWindow.reference) {
109
+ service.kcWindow.opened = true;
110
+ service.kcWindowIsListening = true;
111
+ } else if (event.data.callback === 'encoderRefreshed' && service.encoderWindow.reference) {
112
+ service.encoderWindow.opened = true;
113
+ service.encoderWindowIsListening = true;
114
+ }
115
+
116
+ if (!(service.kcWindow.reference && event.data.sender === 'kcWindow') && !(service.encoderWindow.reference && event.data.sender === 'encoderWindow')) {
117
+ return;
118
+ }
119
+
120
+ var needsAuthentication = false;
121
+ var callback = event.data.callback;
122
+
123
+ switch (event.data.action) {
124
+ case 'sdkServiceResponse':
125
+ if (event.data.sender === 'kcWindow') {
126
+ if (event.data.callback === 'kcListeningChanged') {
127
+ service.kcWindowIsListening = service.listenerResponse.OPENKC === event.data.value;
128
+ if (service.kcWindowIsListening) {
129
+ needsAuthentication = service.workflow.length === 0 || (service.workflow.length > 0 && service.workflow[0].name !== 'setAuthToken');
130
+ } else {
601
131
  closeKcWindow();
602
- closeEncoderWindow();
132
+ }
133
+ } else if (event.data.callback === 'kcRefreshed') {
134
+ callback = 'sdkServiceResponse';
135
+ clearWorkflow();
136
+ } else {
137
+ callback = 'sdkServiceResponse';
603
138
  }
604
- if (window.name === service.kcWindow.name) {
605
- closeKcWindow();
606
- } else if (window.name === service.encoderWindow.name) {
139
+ } else if (event.data.sender === 'encoderWindow') {
140
+ if (event.data.callback === 'encoderListeningChanged') {
141
+ service.encoderWindowIsListening = service.listenerResponse.OPENENCODER === event.data.value;
142
+ if (service.encoderWindowIsListening) {
143
+ needsAuthentication = service.workflow.length === 0 || (service.workflow.length > 0 && service.workflow[0].name !== 'setAuthToken');
144
+ } else {
607
145
  closeEncoderWindow();
146
+ }
147
+ } else if (event.data.callback === 'encoderRefreshed') {
148
+ callback = 'sdkServiceResponse';
149
+ clearWorkflow();
150
+ } else {
151
+ callback = 'sdkServiceResponse';
608
152
  }
609
- return false;
610
- }
611
-
612
- return true;
613
- }
614
-
615
- function closeKcWindow() {
616
- service.kcWindow.opened = false;
617
- service.kcWindowIsListening = false;
618
- }
153
+ }
154
+ break;
155
+ case 'sdkRequestException':
156
+ service.listenerException = true;
157
+ callback = event.data.action;
158
+ break;
159
+ default:
160
+ break;
161
+ }
162
+
163
+ invokeCallback(callback, event.data);
164
+
165
+ if (service.listenerException) {
166
+ stopWorkflow();
167
+ service.listenerException = false;
168
+ }
619
169
 
620
- function closeEncoderWindow() {
621
- service.encoderWindow.opened = false;
622
- service.encoderWindowIsListening = false;
623
- }
170
+ var cwf = event.data.sender === 'kcWindow' ? service.kcWindow.currentWorkflow : service.encoderWindow.currentWorkflow;
624
171
 
625
- // workflow functions
626
- function addToWorkflow(wf) {
627
- var wfArray = Array.isArray(wf) ? wf : [wf];
628
- var kcWindowFunctions = ['openKc', 'launchKc', 'loadPatientRecord', 'assignConfig', 'addCodes', 'clearPatientRecord', 'deleteCodes', 'getPayerList', 'getRowSpecificInfo', 'clearSelectedRow', 'hasCriticalEdits', 'hasEdits', 'launchCodeSummaryReport',
629
- 'launchEdits', 'launchFacilityLookup', 'logout', 'resetApplication', 'savePatientRecord', 'updateCodes', 'updateDemographics'];
630
- var encoderWindowFunctions = ['loadPhraseInLogic', 'loadPhraseInIndex', 'loadCodeInTabular'];
631
- var allWindowFunctions = ['setI9I10State', 'setAuthToken', 'setDemographics', 'closeAllKCWindows', 'closeAllKCWindowsExceptEncoder', 'launchIndex', 'launchLogic', 'launchTabular','openEncoder'];
632
-
633
- wfArray.forEach(function (w) {
634
- w.sendToWindow = [];
635
- if (kcWindowFunctions.indexOf(w.name) > -1) {
636
- w.sendToWindow.push('kcWindow');
637
- }
638
- if (encoderWindowFunctions.indexOf(w.name) > -1) {
639
- w.sendToWindow.push('encoderWindow');
640
- }
641
- if (allWindowFunctions.indexOf(w.name) > -1) {
642
- w.sendToWindow.push('kcWindow');
643
- w.sendToWindow.push('encoderWindow');
644
- }
645
- w.name = w.fn.name || w.fn.toString().match(/^function\s?([^\s(]*)/)[1];
646
- service.workflow.push(w);
172
+ if (!cwf) {
173
+ stopWorkflow();
174
+ return;
175
+ }
176
+
177
+ if (needsAuthentication) {
178
+ service.workflow.unshift({
179
+ name: 'setAuthToken',
180
+ fn: service[event.data.sender].setAuthToken,
181
+ sendToWindow: [event.data.sender],
182
+ args: [service[event.data.sender].authTokenData, service[event.data.sender].name]
647
183
  });
648
- return service.workflow;
649
- }
184
+ }
185
+
186
+ if (service.workflow.length === 0) {
187
+ clearTimeout(service[event.data.sender].responseTime);
188
+ }
189
+
190
+ if (cwf.workflowCompleteMessage === event.data.value) {
191
+ clearTimeout(service[event.data.sender].responseTime);
192
+ processWorkflow(service[event.data.sender]);
193
+ }
194
+ },
195
+ false
196
+ );
197
+
198
+ window.addEventListener('unload', function (event) {
199
+ service.closeAllKCWindows();
200
+ });
201
+
202
+ return service;
203
+ /////////////// return service
204
+
205
+ function openKc(url, authToken) {
206
+ if (!service.kcWindow.reference || !windowExists(service.kcWindow) || !service.kcWindowIsListening) {
207
+ kcWindow.authTokenData = authToken;
208
+
209
+ service.kcWindow.reference = window.open(url, kcWindowName, kcWindowParams);
210
+ service.kcWindow.opened = true;
211
+ service.kcWindow.reference.focus();
212
+ } else {
213
+ service.kcWindow.reference.focus();
214
+ processWorkflow(service.kcWindow);
215
+ }
216
+ }
217
+
218
+ function openKcSentinel(url) {
219
+ if (!service.kcWindow.reference || !windowExists(service.kcWindow) || !service.kcWindowIsListening) {
220
+ service.kcWindow.reference = window.open(url, kcWindowName, "kc-sdk-launched-instance-sentinel");
221
+ service.kcWindow.opened = true;
222
+ service.kcWindow.reference.focus();
223
+ } else {
224
+ service.kcWindow.reference.focus();
225
+ }
226
+ }
227
+
228
+ function openEncoder(url, authToken) {
229
+ if (!service.encoderWindow.reference || !windowExists(service.encoderWindow) || !service.encoderWindowIsListening) {
230
+ encoderWindow.authTokenData = authToken;
231
+
232
+ service.encoderWindow.reference = window.open(url, encoderWindowName, encoderWindowParams);
233
+ service.encoderWindow.opened = true;
234
+ service.encoderWindow.reference.focus();
235
+ } else {
236
+ service.encoderWindow.reference.focus();
237
+ processWorkflow(service.encoderWindow);
238
+ }
239
+ }
240
+
241
+ // apis to send to our open windows
242
+ function loadPhraseInLogic(input) {
243
+ communicationPacket.action = 'logicRequest';
244
+ communicationPacket.value = input;
245
+ communication(communicationPacket, listeningWindows.ENCODER);
246
+ }
247
+
248
+ function loadPhraseInIndex(input) {
249
+ communicationPacket.action = 'indexRequest';
250
+ communicationPacket.value = input;
251
+ communication(communicationPacket, listeningWindows.ENCODER);
252
+ }
253
+
254
+ function loadCodeInTabular(input) {
255
+ communicationPacket.action = 'tabularRequest';
256
+ communicationPacket.value = input;
257
+ communication(communicationPacket, listeningWindows.ENCODER);
258
+ }
259
+
260
+ function loadPatientRecord(patientRecord) {
261
+ communicationPacket.action = 'loadPatientRecord';
262
+ communicationPacket.value = patientRecord;
263
+ communication(communicationPacket, listeningWindows.KC);
264
+ }
265
+
266
+ function assignConfig(input) {
267
+ communicationPacket.action = 'assignConfig';
268
+ communicationPacket.value = input;
269
+ communication(communicationPacket, listeningWindows.KC);
270
+ }
271
+
272
+ function setI9I10State(state) {
273
+ communicationPacket.action = 'setI9I10State';
274
+ communicationPacket.value = state;
275
+ communication(communicationPacket, listeningWindows.ALL);
276
+ }
277
+ function setAuthToken(data) {
278
+ communicationPacket.action = 'setAuthToken';
279
+ communicationPacket.value = data;
280
+ communication(communicationPacket, listeningWindows.ALL);
281
+ }
282
+
283
+ function setDemographics(demographicsData) {
284
+ communicationPacket.action = 'setDemographics';
285
+ communicationPacket.value = demographicsData;
286
+ communication(communicationPacket, listeningWindows.ALL);
287
+ }
288
+
289
+ function encoderStartupComplete() {
290
+ communicationPacket.action = 'encoderStartupComplete';
291
+ communicationPacket.value = '';
292
+ communication(communicationPacket, listeningWindows.ENCODER);
293
+ }
294
+
295
+ function closeAllKCWindows() {
296
+ communicationPacket.action = 'closeAllKCWindows';
297
+ communicationPacket.value = null;
298
+ communication(communicationPacket, listeningWindows.ALL);
299
+ }
300
+
301
+ function addCodes(codesData) {
302
+ communicationPacket.action = 'addCodes';
303
+ communicationPacket.value = codesData;
304
+ communication(communicationPacket, listeningWindows.KC);
305
+ if (service.kcWindow.opened === true) {
306
+ service.kcWindow.reference.focus();
307
+ }
308
+ }
309
+
310
+ function clearPatientRecord() {
311
+ communicationPacket.action = 'clearPatientRecord';
312
+ communicationPacket.value = null;
313
+ communication(communicationPacket, listeningWindows.KC);
314
+ if (service.kcWindow.opened === true) {
315
+ service.kcWindow.reference.focus();
316
+ }
317
+ }
318
+
319
+ function closeAllKCWindowsExceptEncoder() {
320
+ communicationPacket.action = 'closeAllKCWindowsExceptEncoder';
321
+ communicationPacket.value = null;
322
+ communication(communicationPacket, listeningWindows.ALL);
323
+ }
324
+
325
+ function deleteCodes(codes) {
326
+ communicationPacket.action = 'deleteCodes';
327
+ communicationPacket.value = codes;
328
+ communication(communicationPacket, listeningWindows.KC);
329
+ if (service.kcWindow.opened === true) {
330
+ service.kcWindow.reference.focus();
331
+ }
332
+ }
333
+
334
+ function getPayerList(payerList) {
335
+ communicationPacket.action = 'getPayerList';
336
+ communicationPacket.value = payerList;
337
+ communication(communicationPacket, listeningWindows.KC);
338
+ }
339
+
340
+ function getRowSpecificInfo(uniqueIds) {
341
+ communicationPacket.action = 'getRowSpecificInfo';
342
+ communicationPacket.value = uniqueIds;
343
+ communication(communicationPacket, listeningWindows.KC);
344
+ }
345
+
346
+ function clearSelectedRow() {
347
+ communicationPacket.action = 'clearSelectedRow';
348
+ communicationPacket.value = null;
349
+ communication(communicationPacket, listeningWindows.KC);
350
+ if (service.kcWindow.opened === true) {
351
+ service.kcWindow.reference.focus();
352
+ }
353
+ }
354
+ function hasCriticalEdits() {
355
+ communicationPacket.action = 'hasCriticalEdits';
356
+ communicationPacket.value = null;
357
+ communication(communicationPacket, listeningWindows.KC);
358
+ }
359
+ function hasEdits() {
360
+ communicationPacket.action = 'hasEdits';
361
+ communicationPacket.value = null;
362
+ communication(communicationPacket, listeningWindows.KC);
363
+ }
364
+ function launchCodeSummaryReport() {
365
+ communicationPacket.action = 'launchCodeSummaryReport';
366
+ communicationPacket.value = null;
367
+ communication(communicationPacket, listeningWindows.KC);
368
+ }
369
+ function launchEdits() {
370
+ communicationPacket.action = 'launchEdits';
371
+ communicationPacket.value = null;
372
+ communication(communicationPacket, listeningWindows.KC);
373
+ }
374
+ function launchFacilityLookup(codes) {
375
+ communicationPacket.action = 'launchFacilityLookup'
376
+ communicationPacket.value = codes;
377
+ communication(communicationPacket, listeningWindows.KC)
378
+ }
379
+ function launchIndex(codes) {
380
+ communicationPacket.action = 'launchIndex';
381
+ communicationPacket.value = codes;
382
+ communication(communicationPacket, service.encoderWindowIsListening ? listeningWindows.ENCODER : listeningWindows.KC);
383
+ if (service.encoderWindow.opened) {
384
+ service.encoderWindow.reference.focus();
385
+ }
386
+
387
+ }
388
+ function launchLogic(codes) {
389
+ communicationPacket.action = 'launchLogic';
390
+ communicationPacket.value = codes;
391
+ communication(communicationPacket, service.encoderWindowIsListening ? listeningWindows.ENCODER : listeningWindows.KC);
392
+ if (service.encoderWindow.opened == true) {
393
+ service.encoderWindow.reference.focus();
394
+ }
395
+
396
+ }
397
+ function launchTabular(codes) {
398
+ communicationPacket.action = 'launchTabular';
399
+ communicationPacket.value = codes;
400
+ communication(communicationPacket, service.encoderWindowIsListening ? listeningWindows.ENCODER : listeningWindows.KC);
401
+ if (service.encoderWindow.opened == true) {
402
+ service.encoderWindow.reference.focus();
403
+ }
404
+ }
405
+ function logout() {
406
+ communicationPacket.action = 'logout';
407
+ communicationPacket.value = null;
408
+ communication(communicationPacket, listeningWindows.KC);
409
+ if (service.kcWindow.opened === true) {
410
+ service.kcWindow.reference.focus();
411
+ }
412
+ }
413
+ function resetApplication() {
414
+ communicationPacket.action = 'resetApplication';
415
+ communicationPacket.value = null;
416
+ communication(communicationPacket, listeningWindows.KC);
417
+ if (service.kcWindow.opened === true) {
418
+ service.kcWindow.reference.focus();
419
+ }
420
+ }
421
+ function savePatientRecord() {
422
+ communicationPacket.action = 'savePatientRecord';
423
+ communicationPacket.value = null;
424
+ communication(communicationPacket, listeningWindows.KC);
425
+ if (service.kcWindow.opened === true) {
426
+ service.kcWindow.reference.focus();
427
+ }
428
+ }
429
+ function updateCodes(codes) {
430
+ communicationPacket.action = 'updateCodes';
431
+ communicationPacket.value = codes;
432
+ communication(communicationPacket, listeningWindows.KC);
433
+ if (service.kcWindow.opened === true) {
434
+ service.kcWindow.reference.focus();
435
+ }
436
+ }
437
+ function updateDemographics(updatedDemographics) {
438
+ communicationPacket.action = 'updateDemographics';
439
+ communicationPacket.value = updatedDemographics;
440
+ communication(communicationPacket, listeningWindows.KC);
441
+ if (service.kcWindow.opened === true) {
442
+ service.kcWindow.reference.focus();
443
+ }
444
+ }
445
+
446
+ function openEncoderWindow() {
447
+ communicationPacket.action = 'openEncoder';
448
+ communicationPacket.value = null;
449
+ communication(communicationPacket, service.encoderWindowIsListening ? listeningWindows.ENCODER : listeningWindows.KC);
450
+ if (service.encoderWindow.opened == true) {
451
+ service.encoderWindow.reference.focus();
452
+ }
453
+ }
454
+
455
+ // workflow to launch kc
456
+ function launchKc(url, authToken, state, demographics) {
457
+ kcWindow.authTokenData = authToken;
458
+
459
+ var wfArray = [
460
+ {
461
+ fn: openKc,
462
+ args: [url, authToken]
463
+ },
464
+ {
465
+ fn: setI9I10State,
466
+ args: state
467
+ },
468
+ {
469
+ fn: setDemographics,
470
+ args: demographics
471
+ }
472
+ ];
473
+
474
+ var updatedWf = wfArray.concat(service.workflow);
475
+ service.clearWorkflow();
476
+
477
+ service.addToWorkflow(updatedWf);
478
+
479
+ service.processWorkflow(service.kcWindow);
480
+ }
481
+
482
+ // workflow to launch encoder
483
+ function launchEncoder(url, authToken, state, demographics, request) {
484
+ encoderWindow.authTokenData = authToken;
485
+
486
+ var logicIndexRequest = JSON.stringify(
487
+ {
488
+ sessionType: request.sessionType,
489
+ phrase: request.codePhrase
490
+ },
491
+ null,
492
+ 2
493
+ );
650
494
 
651
- function removeFromWorkflow(index) {
652
- service.workflow.splice(index, 1);
653
- }
495
+ var tabularRequest = JSON.stringify(
496
+ {
497
+ sessionType: request.sessionType,
498
+ code: request.codePhrase
499
+ },
500
+ null,
501
+ 2
502
+ );
654
503
 
655
- function processWorkflow(windowToSend) {
656
- service.workflowComplete = false;
657
- if (service.workflow.length === 0) {
658
- stopWorkflow();
659
- return;
504
+ var wfArray = [
505
+ {
506
+ fn: openEncoder,
507
+ args: [url, authToken]
508
+ },
509
+ {
510
+ fn: setI9I10State,
511
+ args: state
512
+ },
513
+ {
514
+ fn: setDemographics,
515
+ args: demographics
516
+ },
517
+ {
518
+ fn: encoderStartupComplete,
519
+ args: []
520
+ }
521
+ ];
522
+
523
+ if (request.encoderTab === service.encoderTabs.LOGIC) {
524
+ wfArray.push({
525
+ fn: loadPhraseInLogic,
526
+ args: logicIndexRequest
527
+ });
528
+ } else if (request.encoderTab === service.encoderTabs.INDEX) {
529
+ wfArray.push({
530
+ fn: loadPhraseInIndex,
531
+ args: logicIndexRequest
532
+ });
533
+ } else if (request.encoderTab === service.encoderTabs.TABULAR) {
534
+ wfArray.push({
535
+ fn: loadCodeInTabular,
536
+ args: tabularRequest
537
+ });
538
+ }
539
+
540
+ var updatedWf = wfArray.concat(service.workflow);
541
+ service.clearWorkflow();
542
+
543
+ service.addToWorkflow(updatedWf);
544
+
545
+ service.processWorkflow(service.encoderWindow);
546
+ }
547
+
548
+ // window functions
549
+ function communication(packet, listener) {
550
+ try {
551
+ var ourWindows = getWindowsToCommunicate(listener);
552
+
553
+ if (ourWindows.length === 0) {
554
+ console.log(new Error('No window listening'));
555
+ return;
556
+ }
557
+
558
+ if (packet.value) {
559
+ packet.value = typeof packet.value === 'string' ? JSON.parse(packet.value) : packet.value;
560
+ }
561
+ ourWindows.forEach(function (w) {
562
+ w.responseTime = 0;
563
+ packet.windowName = w.name;
564
+ messagingProvider.post(w.reference, packet);
565
+ responseTimeout(packet, w);
566
+ });
567
+ } catch (e) {
568
+ console.log(e);
569
+ stopWorkflow();
570
+ invokeCallback(service.callbacks.SDKEXCEPTION, { callback: packet.action, value: e });
571
+ }
572
+ }
573
+
574
+ function getWindowsToCommunicate(listener) {
575
+ var windowArray = [];
576
+
577
+ switch (listener) {
578
+ case listeningWindows.ENCODER:
579
+ if (service.encoderWindowIsListening) {
580
+ windowArray.push(service.encoderWindow);
660
581
  }
582
+ break;
661
583
 
662
- var cwf = service.workflow.shift() || {};
663
-
664
- if (typeof windowToSend === 'string') {
665
- if (windowToSend === service.kcWindow.name) {
666
- windowToSend = service.kcWindow;
667
- } else if (windowToSend === service.encoderWindow.name) {
668
- windowToSend = service.encoderWindow;
669
- } else {
670
- stopWorkflow();
671
- return;
672
- }
584
+ case listeningWindows.KC:
585
+ if (service.kcWindowIsListening) {
586
+ windowArray.push(service.kcWindow);
673
587
  }
588
+ break;
674
589
 
675
- windowToSend.currentWorkflow = cwf;
676
- cwf.workflowCompleteMessage = listenerResponse[cwf.name.toUpperCase()];
677
-
678
- if (!windowToSend.currentWorkflow.fn) {
679
- stopWorkflow();
680
- return;
590
+ case listeningWindows.ALL:
591
+ if (service.kcWindowIsListening) {
592
+ windowArray.push(service.kcWindow);
681
593
  }
682
- var args = Array.isArray(windowToSend.currentWorkflow.args) ? windowToSend.currentWorkflow.args : [windowToSend.currentWorkflow.args];
683
-
684
- if (args.length > 0) {
685
- windowToSend.currentWorkflow.fn.apply(this, args);
686
- } else {
687
- windowToSend.currentWorkflow.fn.apply();
594
+ if (service.encoderWindowIsListening) {
595
+ windowArray.push(service.encoderWindow);
688
596
  }
689
- }
690
597
 
691
- function clearWorkflow() {
692
- service.workflow.length = 0;
693
- service.currentWorkflow = {};
694
- }
598
+ break;
695
599
 
696
- function stopWorkflow() {
697
- service.workflowComplete = true;
698
- clearTimeout(service.kcWindow.responseTime);
699
- clearTimeout(service.encoderWindow.responseTime);
700
- invokeCallback('workflowComplete');
600
+ default:
601
+ return [];
701
602
  }
702
603
 
703
- // handles timeout of calls
704
- function responseTimeout(packet, window) {
705
- window.responseTime = setTimeout(function () {
706
- responseFailed(packet, window);
707
- }, timeoutThresholdSeconds * 1000);
708
- }
604
+ return windowArray.filter(function (w) {
605
+ return w !== null && windowExists(w);
606
+ });
607
+ }
608
+
609
+ function windowExists(window) {
610
+ if (!window.opened) {
611
+ if (!window.name) {
612
+ closeKcWindow();
613
+ closeEncoderWindow();
614
+ }
615
+ if (window.name === service.kcWindow.name) {
616
+ closeKcWindow();
617
+ } else if (window.name === service.encoderWindow.name) {
618
+ closeEncoderWindow();
619
+ }
620
+ return false;
621
+ }
622
+
623
+ return true;
624
+ }
625
+
626
+ function closeKcWindow() {
627
+ service.kcWindow.opened = false;
628
+ service.kcWindowIsListening = false;
629
+ }
630
+
631
+ function closeEncoderWindow() {
632
+ service.encoderWindow.opened = false;
633
+ service.encoderWindowIsListening = false;
634
+ }
635
+
636
+ // workflow functions
637
+ function addToWorkflow(wf) {
638
+ var wfArray = Array.isArray(wf) ? wf : [wf];
639
+ var kcWindowFunctions = ['openKc', 'launchKc', 'loadPatientRecord', 'assignConfig', 'addCodes', 'clearPatientRecord', 'deleteCodes', 'getPayerList', 'getRowSpecificInfo', 'clearSelectedRow', 'hasCriticalEdits', 'hasEdits', 'launchCodeSummaryReport',
640
+ 'launchEdits', 'launchFacilityLookup', 'logout', 'resetApplication', 'savePatientRecord', 'updateCodes', 'updateDemographics'];
641
+ var encoderWindowFunctions = ['loadPhraseInLogic', 'loadPhraseInIndex', 'loadCodeInTabular'];
642
+ var allWindowFunctions = ['setI9I10State', 'setAuthToken', 'setDemographics', 'closeAllKCWindows', 'closeAllKCWindowsExceptEncoder', 'launchIndex', 'launchLogic', 'launchTabular', 'openEncoder'];
643
+
644
+ wfArray.forEach(function (w) {
645
+ w.sendToWindow = [];
646
+ if (kcWindowFunctions.indexOf(w.name) > -1) {
647
+ w.sendToWindow.push('kcWindow');
648
+ }
649
+ if (encoderWindowFunctions.indexOf(w.name) > -1) {
650
+ w.sendToWindow.push('encoderWindow');
651
+ }
652
+ if (allWindowFunctions.indexOf(w.name) > -1) {
653
+ w.sendToWindow.push('kcWindow');
654
+ w.sendToWindow.push('encoderWindow');
655
+ }
656
+ w.name = w.fn.name || w.fn.toString().match(/^function\s?([^\s(]*)/)[1];
657
+ service.workflow.push(w);
658
+ });
659
+ return service.workflow;
660
+ }
709
661
 
710
- function responseFailed(packet, window) {
711
- invokeCallback(service.callbacks.SDKEXCEPTION, { callback: packet.action, value: 'Timed out.' });
712
- stopWorkflow();
713
- }
662
+ function removeFromWorkflow(index) {
663
+ service.workflow.splice(index, 1);
664
+ }
714
665
 
715
- function setCallback(name, func) {
716
- callbackFns[name] = func;
666
+ function processWorkflow(windowToSend) {
667
+ service.workflowComplete = false;
668
+ if (service.workflow.length === 0) {
669
+ stopWorkflow();
670
+ return;
717
671
  }
718
672
 
719
- function invokeCallback(callbackName, args) {
720
- var callbackFn = callbackFns[callbackName];
721
- var args = Array.isArray(args) ? args : [args];
722
- if (typeof callbackFn === 'function') {
723
- try {
724
- callbackFn.apply(this, args);
725
- } catch (e) {
726
- console.log(e);
727
- }
728
- }
729
- }
673
+ var cwf = service.workflow.shift() || {};
674
+
675
+ if (typeof windowToSend === 'string') {
676
+ if (windowToSend === service.kcWindow.name) {
677
+ windowToSend = service.kcWindow;
678
+ } else if (windowToSend === service.encoderWindow.name) {
679
+ windowToSend = service.encoderWindow;
680
+ } else {
681
+ stopWorkflow();
682
+ return;
683
+ }
684
+ }
685
+
686
+ windowToSend.currentWorkflow = cwf;
687
+ cwf.workflowCompleteMessage = listenerResponse[cwf.name.toUpperCase()];
688
+
689
+ if (!windowToSend.currentWorkflow.fn) {
690
+ stopWorkflow();
691
+ return;
692
+ }
693
+ var args = Array.isArray(windowToSend.currentWorkflow.args) ? windowToSend.currentWorkflow.args : [windowToSend.currentWorkflow.args];
694
+
695
+ if (args.length > 0) {
696
+ windowToSend.currentWorkflow.fn.apply(this, args);
697
+ } else {
698
+ windowToSend.currentWorkflow.fn.apply();
699
+ }
700
+ }
701
+
702
+ function clearWorkflow() {
703
+ service.workflow.length = 0;
704
+ service.currentWorkflow = {};
705
+ }
706
+
707
+ function stopWorkflow() {
708
+ service.workflowComplete = true;
709
+ clearTimeout(service.kcWindow.responseTime);
710
+ clearTimeout(service.encoderWindow.responseTime);
711
+ invokeCallback('workflowComplete');
712
+ }
713
+
714
+ // handles timeout of calls
715
+ function responseTimeout(packet, window) {
716
+ window.responseTime = setTimeout(function () {
717
+ responseFailed(packet, window);
718
+ }, timeoutThresholdSeconds * 1000);
719
+ }
720
+
721
+ function responseFailed(packet, window) {
722
+ invokeCallback(service.callbacks.SDKEXCEPTION, { callback: packet.action, value: 'Timed out.' });
723
+ stopWorkflow();
724
+ }
725
+
726
+ function setCallback(name, func) {
727
+ callbackFns[name] = func;
728
+ }
729
+
730
+ function invokeCallback(callbackName, args) {
731
+ var callbackFn = callbackFns[callbackName];
732
+ var args = Array.isArray(args) ? args : [args];
733
+ if (typeof callbackFn === 'function') {
734
+ try {
735
+ callbackFn.apply(this, args);
736
+ } catch (e) {
737
+ console.log(e);
738
+ }
739
+ }
740
+ }
730
741
  };
731
742
 
732
743
  (function (window) {