Revisión | c169a5abb0f02f31949d142b81687f4b6c6fc750 (tree) |
---|---|
Tiempo | 2017-09-09 21:03:07 |
Autor | umorigu <umorigu@gmai...> |
Commiter | umorigu |
BugTrack/184 Save user's "name" for commenting
* Target plugin: comment, pcomment, article and bugtrack
* Save user's name in localStorage of Web browser
@@ -5,8 +5,99 @@ | ||
5 | 5 | // License: GPL v2 or (at your option) any later version |
6 | 6 | // |
7 | 7 | // PukiWiki JavaScript client script |
8 | -if (window.addEventListener) { | |
9 | - window.addEventListener('DOMContentLoaded', function() { | |
8 | +window.addEventListener && window.addEventListener('DOMContentLoaded', function() { | |
9 | + // Name for comment | |
10 | + function setYourName() { | |
11 | + var NAME_KEY_ID = 'pukiwiki_comment_plugin_name'; | |
12 | + var actionPathname = null; | |
13 | + function getPathname(formAction) { | |
14 | + if (actionPathname) return actionPathname; | |
15 | + try { | |
16 | + var u = new URL(formAction, document.location); | |
17 | + var u2 = new URL('./', u); | |
18 | + actionPathname = u2.pathname; | |
19 | + return u2.pathname; | |
20 | + } catch (e) { | |
21 | + // Note: Internet Explorer doesn't support URL class | |
22 | + var m = formAction.match(/^https?:\/\/([^\/]+)(\/([^\?&]+\/)?)/); | |
23 | + if (m) { | |
24 | + actionPathname = m[2]; // pathname | |
25 | + } else { | |
26 | + actionPathname = '/'; | |
27 | + } | |
28 | + return actionPathname; | |
29 | + } | |
30 | + } | |
31 | + function getNameKey(form) { | |
32 | + var pathname = getPathname(form.action); | |
33 | + var key = 'path.' + pathname + '.' + NAME_KEY_ID; | |
34 | + return key; | |
35 | + } | |
36 | + function isEmpty(s) { | |
37 | + if (s.match(/^\s*$/)) { | |
38 | + return true; | |
39 | + } | |
40 | + return false; | |
41 | + } | |
42 | + function getForm(element) { | |
43 | + if (element.form && element.form.tagName === 'FORM' && false) { | |
44 | + return element.form; | |
45 | + } | |
46 | + var e = element.parentElement; | |
47 | + for (var i = 0; i < 5; i++) { | |
48 | + if (e.tagName === 'FORM') { | |
49 | + return e; | |
50 | + } | |
51 | + e = e.parentElement; | |
52 | + } | |
53 | + return null; | |
54 | + } | |
55 | + function handleCommentPlugin(form) { | |
56 | + var namePrevious = ''; | |
57 | + var nameKey = getNameKey(form); | |
58 | + if (typeof localStorage !== 'undefined') { | |
59 | + namePrevious = localStorage[nameKey]; | |
60 | + } | |
61 | + var onFocusForm = function () { | |
62 | + if (form.name && !form.name.value && namePrevious) { | |
63 | + form.name.value = namePrevious; | |
64 | + } | |
65 | + }; | |
66 | + var addOnForcusForm = function(eNullable) { | |
67 | + if (!eNullable) return; | |
68 | + var e = eNullable; | |
69 | + e.addEventListener && e.addEventListener('focus', onFocusForm); | |
70 | + } | |
71 | + if (namePrevious) { | |
72 | + var textList = form.querySelectorAll('input[type=text],textarea'); | |
73 | + textList.forEach(function (v) { | |
74 | + addOnForcusForm(v); | |
75 | + }); | |
76 | + } | |
77 | + form.addEventListener('submit', function(evt) { | |
78 | + if (typeof localStorage !== 'undefined') { | |
79 | + localStorage[nameKey] = form.name.value; | |
80 | + } | |
81 | + }, false); | |
82 | + } | |
83 | + function setNameForComment() { | |
84 | + if (!document.querySelectorAll) return; | |
85 | + var elements = document.querySelectorAll( | |
86 | + 'input[type=hidden][name=plugin][value=comment],' + | |
87 | + 'input[type=hidden][name=plugin][value=pcomment],' + | |
88 | + 'input[type=hidden][name=plugin][value=article],' + | |
89 | + 'input[type=hidden][name=plugin][value=bugtrack]'); | |
90 | + for (var i = 0; i < elements.length; i++) { | |
91 | + var form = getForm(elements[i]); | |
92 | + if (form) { | |
93 | + handleCommentPlugin(form) | |
94 | + } | |
95 | + } | |
96 | + } | |
97 | + setNameForComment(); | |
98 | + } | |
99 | + // AutoTicketLink | |
100 | + function autoTicketLink() { | |
10 | 101 | if (!Array.prototype.indexOf || !document.createDocumentFragment) { |
11 | 102 | return; |
12 | 103 | } |
@@ -149,5 +240,7 @@ if (window.addEventListener) { | ||
149 | 240 | } |
150 | 241 | var target = document.getElementById('body'); |
151 | 242 | walkElement(target); |
152 | - }); | |
153 | -} | |
243 | + } | |
244 | + setYourName(); | |
245 | + autoTicketLink(); | |
246 | +}); |