-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcardTask.js
100 lines (87 loc) · 3.89 KB
/
cardTask.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
// Card Literals
var urlHeaderTask = 'http://contentus.blob.core.windows.net/images/header_task.png';
// Create Task Card
function createCardTask(serialNumber) {
console.log("Create Task Card");
var viewflowURL = buildK2ViewflowURL(findProcessInstId(serialNumber));
var formURL = buildK2FormUrl(serialNumber);
var buttonViewFlow = CardService.newTextButton()
.setText("<font color=\"#1FA9F2\">View Workflow</font>")
.setOpenLink(CardService.newOpenLink().setUrl(viewflowURL));
var buttonOpenForm = CardService.newTextButton()
.setText("<font color=\"#1FA9F2\">Open Form</font>")
.setOpenLink(CardService.newOpenLink().setUrl(formURL));
var buttonSetViewFlow = CardService.newButtonSet().addButton(buttonViewFlow);
var buttonSetOpenForm = CardService.newButtonSet().addButton(buttonOpenForm);
var sectionLinks = CardService.newCardSection().addWidget(CardService.newTextParagraph().setText('Links'))
.addWidget(buttonSetViewFlow)
.addWidget(buttonSetOpenForm);
var card = CardService.newCardBuilder().setName(serialNumber)
.setHeader(CardService.newCardHeader()
.setTitle('Task')
.setImageStyle(CardService.ImageStyle.SQUARE)
.setImageUrl(urlHeaderTask))
.addSection(populateButtonTasks(serialNumber))
.addSection(sectionLinks);
return card.build();
}
function populateButtonTasks(serialNumber) {
console.log("populateButtonTasks");
var url = buildK2TaskActionsURL(serialNumber);
console.log ("Actions URL: " + url);
try
{
var response = accessProtectedResource(url,"GET");
var json = JSON.parse(response.getContentText());
var actionCount = json.batchableActions.length;
console.log ("JSON Response Actions: " + json);
}
catch(e)
{
// NOTE: The error usually in retrieving tasks, which is correct so simply set tasks to 0 to show the standard 'no tasks' card
// "Message": "Task could not be retrieved. Either access denied or not found."
actionCount = 0;
console.log(e);
}
console.log(actionCount);
if (actionCount && actionCount > 0) {
console.log('Actions:');
var sectionActions = CardService.newCardSection().addWidget(CardService.newTextParagraph().setText('Actions'))
json.batchableActions.forEach(function(action) {
console.log('%s (%s)', action, serialNumber);
var buttonAction = CardService.newTextButton()
.setText(action)
.setTextButtonStyle(CardService.TextButtonStyle.FILLED)
.setBackgroundColor('#1FA9F2')
.setOnClickAction(CardService.newAction()
.setFunctionName('onTaskAction')
.setParameters({customAction: action, serialNumber: serialNumber}));
var buttonSet = CardService.newButtonSet()
.addButton(buttonAction);
sectionActions.addWidget(buttonSet);
})
} else {
var sectionActions = CardService.newCardSection().addWidget(CardService.newTextParagraph().setText('No Actions available.'))
console.log('No Actions available.');
}
return sectionActions;
}
function actionK2Task(customAction, serialNumber) {
console.log('Function: ActionK2Task - Serial Number: ' + serialNumber + ' Custom Action: ' + customAction);
var request = {
"xmlFields": [],
"itemReferences": {},
"dataFields": {}
};
var url = buildK2TaskActionURL(serialNumber, customAction);
var response = accessProtectedResource(url,"POST", request);
return createCardTask(serialNumber);
}
function onTaskAction(e)
{
console.log('onTaskAction');
console.log('Custom Action: ' + e.parameters.customAction);
console.log('Serial Number: ' + e.parameters.serialNumber);
console.log('Process Inst Id: ' + findProcessInstId(e.parameters.serialNumber));
return actionK2Task(e.parameters.customAction,e.parameters.serialNumber);
}