Content deleted Content added
Mike Dillon (talk | contribs) No edit summary |
m Maintenance: Replacing addOnloadHook with native jQuery (mw:ResourceLoader/Migration_guide_(users)#addOnloadHook - phab:T130879) |
||
(21 intermediate revisions by one other user not shown) | |||
Line 1:
// Requires: [[User:Mike Dillon/Scripts/i18n.js]], [[User:Mike Dillon/Scripts/easydom.js]], [[User:Mike Dillon/Scripts/cookies.js]]
/* <pre><nowiki> */
Line 5:
var pageHistoryCookieName;
var pageHistoryCookieItemCount;
var pageHistoryBeforePorlet;
var pageHistoryExpiresInDays;
/* Messages */
addOnloadHook(function () {▼
// pageHistoryTitle: title of page history portlet
wfAddMsg("en", "pageHistoryTitle", "Page history");
wfAddMsg("es", "pageHistoryTitle", "Historial de páginas");
// clearHistoryLabel: label of the "clear history" link
wfAddMsg("en", "clearHistoryLabel", "clear history");
wfAddMsg("es", "clearHistoryLabel", "borrar historial");
// clearHistoryTitle: tooltip of the "clear history" link
wfAddMsg("en", "clearHistoryTitle", "Clear page history");
wfAddMsg("es", "clearHistoryTitle", "Borrar historial de páginas");
// noPageHistoryText: text to display when there is no page history
wfAddMsg("en", "noPageHistoryText", "No page history");
wfAddMsg("es", "noPageHistoryText", "Ningún historial de páginas");
// Set defaults for variables
if (!pageHistoryCookieName) pageHistoryCookieName = "pageHistory";
if (!pageHistoryCookieItemCount) pageHistoryCookieItemCount = 10;
if (!pageHistoryBeforePorlet) pageHistoryBeforePorlet = null;
if (!pageHistoryExpiresInDays) pageHistoryExpiresInDays = 0;
// Create portlet
with (easyDom) {
var historyPortlet = div({ "class": "portlet", "id": "p-history" },
h5(wfMsg("
var historyList = ul();
historyPortlet.appendChild(historyPBody);
}
// Insert portlet
var
document.getElementById("column-one").insertBefore(historyPortlet, beforePortlet);
// Extract previous history from cookie
Line 35 ⟶ 56:
// Prepend the current page to the list, remove duplicates, and control item count
if (
historyItems.unshift(wgPageName);
for (var n = 1; n < historyItems.length; n++) {
Line 42 ⟶ 63:
}
}
while (historyItems.
historyItems.pop();
}
}
}
Line 54 ⟶ 77:
}
with (easyDom) {
historyList.appendChild(li(em(wfMsg("
}
deleteCookie(pageHistoryCookieName, { path: "/" });
return false;
};
Line 62 ⟶ 86:
with (easyDom) {
if (historyItems.length == 0) {
historyList.appendChild(li(em(wfMsg("
} else {
for (var n in historyItems) {
Line 71 ⟶ 95:
var itemLabel = historyItems[n].replace(/_/g, " ");
// Add zero-width spaces after other punctuation
itemLabel = itemLabel.replace(/([:;,.)-])/g, "$1\u200B");
historyList.appendChild(li(
a({ "href": itemUrl, "title": historyItems[n] }, itemLabel)));
}
var clearHistoryLink = a({ "href": "#"
em(wfMsg("clearHistoryLabel")));
clearHistoryLink.onclick = clearHistory;
{ "style": "padding-top: 1em; text-align: right" }, clearHistoryLink));
}
}
Line 87 ⟶ 115:
historyCookie = "";
for (var n in historyItems) {
var encodedItem = encodeURIComponent(historyItems[n]);
// Limit cookie value size to 4000 bytes
if (historyCookie.length + encodedItem.length + 1 > 4000) break;
if (n > 0) historyCookie += ",";
historyCookie +=
}
writeCookie(pageHistoryCookieName, historyCookie,
{ "path": "/", "expiresInDays": pageHistoryExpiresInDays });
});
|