-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWEnotesPost.js
More file actions
150 lines (140 loc) · 5.45 KB
/
Copy pathWEnotesPost.js
File metadata and controls
150 lines (140 loc) · 5.45 KB
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
/* WEnotes post form widget
* Copyright 2012-2016 Open Education Resource Foundation
* Copyright 2026 Jim Tittsler and WikiEducator contributors
* Available under CC-BY license.
*/
(function (mw, $) {
var postLength = 300,
rawPostLength = postLength + 20;
function initPostForm($div) {
var tag = $div.data("tag") || "wikieducator";
var button = $div.data("button") || "Post a WEnote";
var leftmargin = $div.data("leftmargin") || 53;
var anonymous = $div.data("anonymous") || null;
var api = new mw.Api();
$div.css("margin", "0px 0px 10px " + leftmargin + "px").append(
"<form>" +
'<textarea rows="4" cols="40" style="width:auto; height: 1.5em; ' +
'float: left; margin-right: 10px; margin-bottom: 5px;"></textarea>' +
'<div style="float: left;">' +
'<input type="submit" disabled="disabled" value="' +
$("<span>").text(button).html() +
'" />' +
'<p class="WEnotesPostCounter" style="color:#999; margin-left: 7px; ' +
'display: none;">' +
postLength +
"</p>" +
"</div>" +
"</form>" +
'<br clear="all" />',
);
var $counter = $div.find("p.WEnotesPostCounter");
var $button = $div.find('input[type="submit"]');
var $text = $div.find("textarea");
function update(t) {
var mt = t.replace(/http:\/\/([^ ]+)/g, function (target) {
return target.length > 19 ? "http://xxx.xx/xxxxx" : target;
});
var l = mt.length;
var r = Math.min(postLength - l, rawPostLength - t.length);
$counter.text(r);
if (r >= 0) {
$counter.css("color", "#999");
$button.prop("disabled", l === 0);
} else {
$counter.css("color", "red");
$button.prop("disabled", true);
}
}
function notifyFeedWidgets() {
// try to find the window.wendivs that need to be updated
// locally, cancel poll timer and trigger immedate poll
var lctag = tag.toLowerCase();
$.each(window.wendivs || [], function (i, wd) {
var wdtag = (wd.tag || "wikieducator").toLowerCase();
// '_' is the all-tags wildcard — always notify it.
if (wdtag === lctag || wdtag === "_") {
if (wd.pollTimer) {
clearTimeout(wd.pollTimer);
wd.pollTimer = null;
}
wd.$d.triggerHandler("WEnotes", [tag]);
}
});
}
function livenForm() {
$button.click(function () {
$button.prop("disabled", true);
api.postWithEditToken({
action: "wenotes",
tag: tag,
text: $text.val(),
})
.done(function (d) {
if ("error" in d) {
alert(
"Unable to save submission:\n " + d.error.info,
);
} else {
notifyFeedWidgets();
}
$button.prop("disabled", false);
$text.val("");
$counter.text(postLength);
})
.fail(function (code) {
alert("Unable to save submission:\n " + code);
$button.prop("disabled", false);
});
return false;
});
$text.on("keyup change", function () {
update($(this).val());
});
$text.focus(function () {
$counter.fadeIn();
$text.css("height", "auto");
});
}
function disableForm() {
var pageName = mw.config.get("wgPageName");
var anonmsg =
anonymous ||
'<a class="plainlinks" href="/Special:UserLogin?returnto=' +
pageName +
'">Login to post</a>';
$button.prop("disabled", true);
$text.prop("disabled", true).attr("rows", "2");
$counter.html(anonmsg);
}
// Check login state — wgUserName is set for in-wiki page renders;
// null means we are in a snapshot/embed and must ask the API.
if (mw.config.get("wgUserName") === null) {
api.get({
action: "query",
meta: "userinfo",
})
.done(function (d) {
if (
d &&
d.query &&
d.query.userinfo &&
!d.query.userinfo.hasOwnProperty("anon")
) {
livenForm();
} else {
disableForm();
}
})
.fail(function () {
disableForm();
});
} else {
livenForm();
}
}
// Self-initialise: find every .WEnotesPost div on the page and wire it up.
$("div.WEnotesPost").each(function () {
initPostForm($(this));
});
})(mediaWiki, jQuery);