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
|
/*
* Garoon Portal sample program
* Copyright (c) 2016 Cybozu
*
* Licensed under the MIT License
* https://opensource.org/license/mit/
*/
(function($) {
'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
function 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
function 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
function 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 ----//{{{
function 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 ----//{{{
function 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 ----//{{{
function getKintoneData() {
$.ajax({
type: 'GET',
url: '/k/v1/records.json',
data: {app: APP_ID, query: 'order by $id desc limit 1 offset 0'},
cache: false,
dataType: 'json'
}).done((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;
// show data
$('#subPost3 .postTitle').append(title);
$('#subPost3 .postText span').append(contents);
});// end of done
}
// ---- getKintoneData finish ----//}}}
// ---- showPortlet ----//{{{
// ---- showMainArticle ----//{{{
function 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');
$('.mainPost .postText span').attr('class', 'html');
} else {
body = bodyNode[0].getAttribute('body');
$('.mainPost .postText span').attr('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
$('.mainPost .postTitle').append(subject);
$('.mainPost .postText span').append(body);
$('.mainPost .photo').prepend('<img src="' + fileUrl + '">');
}
// ---- showMainArticle finish ----//}}}
// ---- showSubArticle ----//{{{
function showSubArticleData(article) {
console.log(article);
const bodyNode = article.children;
const subject = '<a href="/g/bulletin/view.csp?&aid=' + SUB_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');
$('#subPost1 .postText span').attr('class', 'html');
} else {
body = bodyNode[0].getAttribute('body');
$('#subPost1 .postText span').attr('class', 'text');
}
// show data
$('#subPost1 .postTitle').append(subject);
$('#subPost1 .postText span').append(body);
}
// ---- showSubArticle finish ----//}}}
// ---- showFollow ----//{{{
function showFollowData(subject, newestFollow) {
console.log(newestFollow);
const title = '<a href="/g/bulletin/view.csp?&aid=' + FOLLOW_TOPIC_ID + '" target="_blank"><b>' + subject + '</b></a>';
// const text = (newestFollow.getAttribute('html_text') != null) ? newestFollow.getAttribute('html_text') : newestFollow.getAttribute('text');
let text;
if (newestFollow.getAttribute('html_text') != null) {
text = newestFollow.getAttribute('html_text');
$('#subPost2 .postText span').attr('class', 'html');
} else {
text = newestFollow.getAttribute('text');
$('#subPost2 .postText span').attr('class', 'text');
}
// show data
$('#subPost2 .postTitle').append(title);
$('#subPost2 .postText span').append(text);
}
// ---- 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----//}}}
})(jQuery.noConflict(true));
|