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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
|
/*
* Garoon Portal sample program
* Copyright (c) 2016 Cybozu
*
* Licensed under the MIT License
* https://opensource.org/license/mit/
*/
(() => {
'use strict';
// ---- settings ----//{{{
const MAIN_TOPIC_ID = 1; // main topic id
const SUB_TOPIC_ID = 2; // sub topic(article) id
const FOLLOW_TOPIC_ID = 3; // sub topic(follow) id
const APP_ID = 4; // kintone app id
// function to escape html
const escapeHtml = (str) => {
if (!str) {
return '';
}
return str
.replace(/&/g, '&')
.replace(/</g, '<')
.replace(/>/g, '>')
.replace(/"/g, '"')
.replace(/'/g, ''');
};
// ---- xml settings ----//{{{
// function to make XML Header for Garoon API
// arg1:services:service type (base,schedule)
// arg2:action:name of API
// return:XML header string
const makeXMLHeader = (services, action) => {
let xmlns;
switch (services) {
case 'base':
xmlns = 'base_services="http://wsdl.cybozu.co.jp/base/2008"';
break;
case 'bulletin':
xmlns = 'workflow_services="http://wsdl.cybozu.co.jp/bulletin/2008"';
break;
default:
alert('Can not select services');
return;
}
const xmlHeader =
'<?xml version="1.0" encoding="UTF-8"?>' +
'<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://www.w3.org/2003/05/soap-envelope" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:' + xmlns + '>' +
'<SOAP-ENV:Header>' +
'<Action SOAP-ENV:mustUnderstand="1" xmlns="http://schemas.xmlsoap.org/ws/2003/03/addressing">' + escapeHtml(action) + '</Action>' +
'<Timestamp SOAP-ENV:mustUnderstand="1" Id="id" xmlns="http://schemas.xmlsoap.org/ws/2002/07/utility">' +
'<Created>2037-08-12T14:45:00Z</Created>' +
'<Expires>2037-08-12T14:45:00Z</Expires>' +
'</Timestamp>' +
'<Locale>jp</Locale>' +
'</SOAP-ENV:Header>';
return xmlHeader;
};
// function to set XMLHTTP
// return xmlhttp object
const setXMLHTTP = () => {
let xmlhttp = false;
if (typeof ActiveXObject !== 'undefined') {
try {
xmlhttp = new ActiveXObject('Microsoft.XMLHTTP');
} catch (e) {
xmlhttp = false;
}
}
if (!xmlhttp && typeof XMLHttpRequest !== 'undefined') {
xmlhttp = new XMLHttpRequest();
}
return xmlhttp;
};
// ---- xml settings end ----//}}}
// ---- getGrnArticleData ----//{{{
const getArticle = (topic_id) => {
return new Promise((resolve, reject) => {
const xhrForArticle = setXMLHTTP();
const url = '/g/cbpapi/bulletin/api.csp';
const apiName = 'BulletinGetTopicByIds';
const articleRequest =
makeXMLHeader('bulletin', apiName) +
'<SOAP-ENV:Body>' +
'<' + apiName + '>' +
'<parameters>' +
'<topics xmlns="" topic_id="' + topic_id + '" is_draft="false"></topics>' +
'</parameters>' +
'</' + apiName + '>' +
'</SOAP-ENV:Body>' +
'</SOAP-ENV:Envelope>';
console.log(articleRequest);
xhrForArticle.open('POST', url, true);
xhrForArticle.onload = function() {
if (xhrForArticle.readyState === 4 && xhrForArticle.status === 200) {
resolve(xhrForArticle.responseXML.getElementsByTagName('topic')[0]);
}
};
xhrForArticle.send(articleRequest);
});
};
// ---- getGrnArticleData finish ----//}}}
// ---- getGrnFollowData ----//{{{
const getNewestFollow = () => {
return new Promise((resolve, reject) => {
const xhrForFollow = setXMLHTTP();
const url = '/g/cbpapi/bulletin/api.csp';
const apiName = 'BulletinGetFollows';
const followRequest =
makeXMLHeader('bulletin', apiName) +
'<SOAP-ENV:Body>' +
'<' + apiName + '>' +
'<parameters topic_id="' + FOLLOW_TOPIC_ID + '" offset="0" limit="1"></parameters>' +
'</' + apiName + '>' +
'</SOAP-ENV:Body>' +
'</SOAP-ENV:Envelope>';
console.log(followRequest);
xhrForFollow.open('POST', url, true);
xhrForFollow.onload = function() {
if (xhrForFollow.readyState === 4 && xhrForFollow.status === 200) {
resolve(xhrForFollow.responseXML.getElementsByTagName('follow')[0]);
}
};
xhrForFollow.send(followRequest);
});
};
// ---- getGrnFollowData finish ----//}}}
// ---- getKintoneData ----//{{{
const getKintoneData = () => {
fetch('/k/v1/records.json?app=' + APP_ID + '&query=' + encodeURIComponent('order by $id desc limit 1 offset 0'), {
method: 'GET',
headers: {
'X-Requested-With': 'XMLHttpRequest',
},
cache: 'no-cache'
})
.then(response => response.json())
.then(resp => {
const records = resp.records;
const url = '/k/' + APP_ID + '/show#record=' + records[0].$id.value;
const title = '<a href="' + url + '" target="_blank"><b>' + records[0].title.value + '</b></a>';
const contents = records[0].content.value;
// データを表示
document.querySelector('#subPost3 .postTitle').innerHTML += title;
document.querySelector('#subPost3 .postText span').innerHTML += contents;
});
};
// ---- getKintoneData finish ----//}}}
// ---- showPortlet ----//{{{
// ---- showMainArticle ----//{{{
const showMainArticleData = (article) => {
console.log(article);
const bodyNode = article.children;
const fileNode = bodyNode[0].children;
const subject = '<a href="/g/bulletin/view.csp?&aid=' + MAIN_TOPIC_ID + '" target="_blank"><b>' + article.getAttribute('subject') + '</b></a>';
let body;
if (bodyNode[0].getAttribute('html_body') != null) {
body = bodyNode[0].getAttribute('html_body');
document.querySelector('.mainPost .postText span').setAttribute('class', 'html');
} else {
body = bodyNode[0].getAttribute('body');
document.querySelector('.mainPost .postText span').setAttribute('class', 'text');
}
const fileName = fileNode[fileNode.length - 1].getAttribute('name');
const fileId = fileNode[fileNode.length - 1].getAttribute('id');
const fileUrl = '/g/bulletin/file_download.csp/-/' + fileName + '?fid=' + fileId + '&ticket=&.jpg';
// show data
document.querySelector('.mainPost .postTitle').innerHTML += subject;
document.querySelector('.mainPost .postText span').innerHTML += body;
const imgElement = document.createElement('img');
imgElement.src = fileUrl;
document.querySelector('.mainPost .photo').prepend(imgElement);
};
// ---- showMainArticle finish ----//}}}
// ---- showSubArticle ----//{{{
const showSubArticleData = (article) => {
console.log(article);
const bodyNode = article.children;
const subjectLink = document.createElement('a');
subjectLink.href = '/g/bulletin/view.csp?&aid=' + SUB_TOPIC_ID;
subjectLink.target = '_blank';
const subjectText = document.createTextNode(article.getAttribute('subject'));
subjectLink.appendChild(subjectText);
const subjectElement = document.createElement('b');
subjectElement.appendChild(subjectLink);
let bodyContent;
if (bodyNode[0].getAttribute('html_body') != null) {
bodyContent = bodyNode[0].getAttribute('html_body');
const postTextSpan = document.querySelector('#subPost1 .postText span');
postTextSpan.setAttribute('class', 'html');
postTextSpan.innerHTML = bodyContent;
} else {
bodyContent = bodyNode[0].getAttribute('body');
const postTextSpan = document.querySelector('#subPost1 .postText span');
postTextSpan.setAttribute('class', 'text');
postTextSpan.textContent = bodyContent;
}
// show data
document.querySelector('#subPost1 .postTitle').appendChild(subjectElement);
};
// ---- showSubArticle finish ----//}}}
// ---- showFollow ----//{{{
const showFollowData = (subject, newestFollow) => {
console.log(newestFollow);
const subjectLink = document.createElement('a');
subjectLink.href = '/g/bulletin/view.csp?&aid=' + FOLLOW_TOPIC_ID;
subjectLink.target = '_blank';
const subjectText = document.createTextNode(subject);
subjectLink.appendChild(subjectText);
const subjectElement = document.createElement('b');
subjectElement.appendChild(subjectLink);
let textContent;
if (newestFollow.getAttribute('html_text') != null) {
textContent = newestFollow.getAttribute('html_text');
const postTextSpan = document.querySelector('#subPost2 .postText span');
postTextSpan.setAttribute('class', 'html');
postTextSpan.innerHTML = textContent;
} else {
textContent = newestFollow.getAttribute('text');
const postTextSpan = document.querySelector('#subPost2 .postText span');
postTextSpan.setAttribute('class', 'text');
postTextSpan.textContent = textContent;
}
// show data
document.querySelector('#subPost2 .postTitle').appendChild(subjectElement);
};
// ---- showFollow finish ----//}}}
// ---- get Main Article-----//{{{
getArticle(MAIN_TOPIC_ID).then(showMainArticleData, (e) => {
console.log(e);
});
// ---- get Main Article finish -----//}}}
// ---- get Sub Article-----//{{{
getArticle(SUB_TOPIC_ID).then(showSubArticleData, (e) => {
console.log(e);
});
// ----get Sub Article finish -----//}}}
// ----get Sub Article(follow)-----{{{
getArticle(FOLLOW_TOPIC_ID).then((article) => {
const subject = article.getAttribute('subject');
getNewestFollow().then((follow) => {
showFollowData(subject, follow);
});
});
// ----get Sub Article(follow) finish -----}}}
// ----get Main Article-----//
getKintoneData();
// ----showPortlet finish----//}}}
})();
|