/
BlogPost.js
375 lines (335 loc) · 15.8 KB
/
BlogPost.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
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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
/************************************************************************
* BlogPost.js *
* *
* Javascript code to implement dynamic functionality of the *
* page BlogPost.php. *
* *
* History: *
* 2018/09/12 created *
* 2018/10/30 use Node.textContent rather than getText *
* 2019/02/10 no longer need to call pageInit *
* 2019/04/13 support new tinyMCE *
* 2019/08/27 add Close button *
* 2020/06/03 hide right column *
* 2021/01/16 use XMLSerializer for diagnostic output *
* 2021/04/04 use ES2015 import *
* *
* Copyright © 2021 James A. Cobban *
************************************************************************/
import {HTTP} from "../jscripts6/js20/http.js";
import {actMouseOverHelp, hideRightColumn, openFrame, openSignon,
popupAlert, args, iframe}
from "../jscripts6/util.js";
import "../jscripts6/CommonForm.js";
/* global tinyMCE */
/************************************************************************
* Initialization code that is executed when this script is loaded. *
* *
* Define the function to be called once the web page is loaded. *
************************************************************************/
window.addEventListener("load", onLoad);
var lang = 'en';
var blogid = 0;
var edit = 'N';
var debug = false;
for(const key in args)
{ // loop through parameters
switch (key.toLowerCase())
{ // act on specific parameters
case 'lang':
lang = args[key];
break;
case 'blogid':
blogid = args[key];
break;
case 'edit':
edit = args[key].toUpperCase();
break;
case 'debug':
if (args[key].toLowerCase() == 'y')
debug = true;
break;
} // act on specific parameters
} // loop through parameters
/************************************************************************
* function onLoad *
* *
* Perform initialization functions once the page is loaded. *
************************************************************************/
function onLoad()
{
document.body.onresize = onWindowResize;
// scan through all forms and set dynamic functionality
// for specific elements
for(let i = 0; i < document.forms.length; i++)
{
let form = document.forms[i];
for(let j = 0; j < form.elements.length; j++)
{
let element = form.elements[j];
let name = element.name;
if (name.length == 0)
{ // button elements usually have id not name
name = element.id;
} // button elements usually have id not name
let result = /^([a-zA-Z]*)([0-9]*)$/.exec(name);
if (result !== null)
{
name = result[1];
//id = result[2];
}
// take action specific to the element based on its name
switch(name.toLowerCase())
{ // switch on name
case 'subject':
{
if (blogid == 0)
element.focus();
break;
}
case 'message':
{ // blog text area
let msgLabel = document.getElementById('msgLabel');
let mframe = tinyMCE.DOM.get('message_ifr');
let textwidth = window.innerWidth -
msgLabel.offsetWidth - 40;
tinyMCE.DOM.setStyle(mframe, 'width', textwidth + 'px');
if (blogid > 0)
{ // focus on the current input element
tinyMCE.get('message').focus();
} // focus on the current input element
break;
} // blog text area
case 'postblog':
{ // post blog button
element.addEventListener('click', postBlog);
break;
} // post blog button
case 'edit':
{
element.addEventListener('click', editBlog);
break;
}
case 'del':
{
element.addEventListener('click', delBlog);
break;
}
case 'close':
{
element.addEventListener('click', close);
break;
}
default:
{
break;
}
} // switch on name
} // loop through elements in form
} // iterate through all forms
// pop up help balloon if the mouse hovers over the message input field
// for more than 2 seconds
let element = document.getElementById('message_ifr');
if (element)
{
actMouseOverHelp.call(element, 'message');
}
hideRightColumn();
} // function onLoad
/************************************************************************
* function onWindowResize *
* *
* This method is called when the browser window size is changed. *
* For example if the window is split between the main display and *
* a second display, resize. *
* *
* Input: *
* this <body> element *
************************************************************************/
function onWindowResize()
{
if (iframe)
openFrame(iframe.name, null, "right");
let msgLabel = document.getElementById('msgLabel');
let mframe = tinyMCE.DOM.get('message_ifr');
let textwidth = window.innerWidth - msgLabel.offsetWidth - 40;
tinyMCE.DOM.setStyle(mframe, 'width', textwidth + 'px');
let subject = document.getElementById('subject');
subject.style.width = textwidth + 'px';
let email = document.getElementById('emailAddress');
email.style.width = textwidth + 'px';
} // function onWindowResize
/************************************************************************
* function postBlog *
* *
* This method is called when the user requests to post *
* a message to the blog of an individual. *
* *
* Input: *
* this <button id='PostBlog'> *
* ev instance of click Event *
************************************************************************/
function postBlog(ev)
{
ev.stopPropagation();
let form = this.form;
let userid = form.userid.value;
let email = '';
let subject = form.subject.value;
if (form.emailAddress)
email = form.emailAddress.value;
if (userid == '' && email == '')
{ // not signed on or identified
openSignon(); // require user to sign on
} // not signed on or identified
else
{ // identified
let idir = form.blogid.value;
let message = tinyMCE.get('message').getContent();
let parms = { "idir" : idir,
"table" : 'Blogs',
"emailAddress" : email,
"subject" : subject,
"message" : message};
if (edit == 'Y')
{
parms['update'] = 'Y';
}
if (debug)
{
let blogParms = JSON.stringify(parms);
alert("BlogPost.js: postBlog: " + blogParms);
parms['debug'] = 'y';
}
// invoke script to update Event and return XML result
HTTP.post('/postBlogXml.php',
parms,
gotBlog,
noBlog);
} // identified
} // function postBlog
/************************************************************************
* function gotBlog *
* *
* This method is called when the XML file representing *
* a posted blog is retrieved from the database. *
* *
* Input: *
* xmlDoc response from web server as XML document *
************************************************************************/
function gotBlog(xmlDoc)
{
let root = xmlDoc.documentElement;
let messageElt = document.getElementById('PostBlog');
let msg = "";
if (debug.toLowerCase() == 'y')
{
alert("BlogPost.js: gotBlog: xmlDoc=" +
new XMLSerializer().serializeToString(xmlDoc));
}
if (root && root.nodeName == 'blog')
{
for(let i = 0; i < root.childNodes.length; i++)
{ // loop through children
let node = root.childNodes[i];
if (node.nodeName == 'msg')
msg += node.textContent;
} // loop through children
}
else
{ // error
if (root)
{
for(let i = 0; i < root.childNodes.length; i++)
{ // loop through children
let node = root.childNodes[i];
if (node.nodeValue != null)
msg += node.nodeValue;
} // loop through children
}
else
msg += root;
} // error
if (msg.length > 0)
popupAlert(msg, messageElt);
let url = location.href;
url = url.replace(/&edit=Y/i, '');
location = url; // refresh the page
} // function gotBlog
/************************************************************************
* function noBlog *
* *
* This method is called if there is no blog script on the web server. *
************************************************************************/
function noBlog()
{
let messageElt = document.getElementById('PostBlog');
popupAlert('BlogPost.js: noBlog: ' +
'script "postBlogXml.php" not found on web server',
messageElt);
location.reload(); // refresh the page
} // function noBlog
/************************************************************************
* function editBlog *
* *
* This method is called if the user requests to edit the blog *
* message. *
* *
* Input: *
* this <button id='edit...'> *
* e instance of click Event *
************************************************************************/
function editBlog(e)
{
e.stopPropagation();
location = "/BlogPost.php?blogid=" + this.id.substring(4) +
"&table=Blogs&lang=" + lang + "&edit=Y";
return false;
} // function editBlog
/************************************************************************
* function delBlog *
* *
* This method is called if the user requests to delete the blog *
* message. *
* *
* Input: *
* this <button id='del...'> *
* ev instance of click Event *
************************************************************************/
function delBlog(ev)
{
ev.stopPropagation();
let blid = this.id.substring(3);
let parms = {"blid" : blid};
// invoke script to update blog and return XML result
HTTP.post('/deleteBlogXml.php',
parms,
gotBlog,
noDelBlog);
} // function delBlog
/************************************************************************
* function noDelBlog *
* *
* This method is called if there is no blog script on the web server. *
************************************************************************/
function noDelBlog()
{
alert('BlogPost.js: noDelBlog: ' +
'script "deleteBlogXml.php" not found on web server');
} // function noDelBlog
/************************************************************************
* function close *
* *
* This method is called if the user requests to edit the blog *
* message. *
* *
* Input: *
* this <button id='close'> *
* e instance of click Event *
************************************************************************/
function close(e)
{
e.stopPropagation();
location = "/Blogs.php";
return false;
} // function close