Content deleted Content added
MusikAnimal (talk | contribs) No edit summary |
MusikAnimal (talk | contribs) No edit summary |
||
Line 55:
"<legend>Custom watchlists</legend>" + appendHtml;
$("#mw-watchlist-form").after(html);
};
var setupCactionInterface = function(data) {
var customWatchlists = JSON.parse(data.split("\n")[0]),
customWatchlistNames = Object.keys(customWatchlists),
inWatchlists = [];
for(var wl in customWatchlists) {
if(customWatchlists[wl].indexOf(wgPageName) !== -1) {
inWatchlists.push(wl);
}
}
var html = "<div id='cw-overlay'>" +
"<div class='header'>" +
"Add/remove page to custom watchlists" +
"<span class='closer-x' onclick=\"$('#cw-overlay').remove();\"></span>" +
"</div>" +
"<div style='padding:14px' id='cw-overlay-body'>";
for(var i=0; i < customWatchlistNames.length; i++) {
var listName = customWatchlistNames[i];
html += "<span><input class='cw-option' type='checkbox' value='"+i+"' "+(inWatchlists.indexOf(listName) !== -1 ? "checked data-index='"+customWatchlists[listName].indexOf(wgPageName)+"'" : "")+" id='cw-option-"+i+"' /><label for='cw-option-"+i+"'>"+listName+"</label> (<a class='cw-delete' href='javascript:' data-id='"+i+"'>del</a>)</span>";
}
html += "<div id='cw-overlay-new-watchlist'>" +
"<input class='cw-option' type='checkbox' value='-1' id='cw-new-option' /><label for='cw-new-option'>New watchlist</label>" +
"<input type='text' id='cw-overlay-new-watchlist-input' placeholder='Enter watchlist name' />" +
"</div><button id='cw-overlay-selector-submit'>Save changes</button></div></div>";
$("body").append(html);
$("#cw-new-option").change(function() {
if($(this).is(":checked")) {
$("#cw-overlay-new-watchlist-input").show().focus();
} else {
$("#cw-overlay-new-watchlist-input").hide();
}
});
$(".cw-delete").click(function() {
if($(this).text() === "undel") {
$("#cw-option-"+$(this).data('id')).prop('checked',false).siblings("label").removeClass("disabled");
$("#cw-option-"+$(this).data('id')).data('delete',null);
$(this).text("del");
} else {
var name = customWatchlistNames[$(this).data('id')];
var cwToDelete = customWatchlists[name];
if(confirm("Mark the watchlist \""+name+"\" and all it's "+cwToDelete.length+" entries for deletion?")) {
$(this).text("undel");
$("#cw-option-"+$(this).data('id')).data('delete',true);
$("#cw-option-"+$(this).data('id')).prop('checked',true).one("click", function() {
$(this).siblings("a").trigger("click");
}).siblings("label").addClass("disabled");
}
}
});
$("#cw-overlay-selector-submit").click({
customWatchlistNames: customWatchlistNames,
customWatchlists: customWatchlists,
inWatchlists: inWatchlists
}, function(e) {
$("#cw-overlay-selector-submit").replaceWith("Saving...");
var cw = e.data.customWatchlists,
iw = e.data.inWatchlists,
toWatch = false,
updateStr = "";
$.each($(".cw-option"), function(i) {
var id = parseInt($(this).val());
var key = id < 0 ? $("#cw-overlay-new-watchlist-input").val().replace(/[^\w\s]/gi, '') : customWatchlistNames[id];
var exists = iw.indexOf(key) >= 0;
if($(this).is(":checked")) {
if($(this).data("delete")) {
updateStr = "Deleted the custom watchlist <b>"+key+"</b>.";
delete cw[key];
} else {
toWatch = true;
if(id < 0) {
updateStr = "Created the custom watchlist <b>"+key+"</b> with <b>"+wgPageName+"</b>";
cw[key] = [wgPageName];
} else {
updateStr = "Added <b>"+wgPageName+"</b> to the custom watchlist <b>"+key+"</b>";
cw[key].push(wgPageName);
}
}
} else if(exists) {
updateStr = "Removed <b>"+wgPageName+"</b> from the custom watchlist <b>"+key+"</b>";
cw[key].splice($(this).data('index'),1);
}
});
var stringifiedCw = JSON.stringify(cw)+"\nBacklink: [[User:MusikAnimal/customWatchlists]]";
var api = new mw.Api();
api.watch(wgPageName).done(function(watchResult) {
api.postWithToken( "edit", {
action: "edit",
title: "User:"+wgUserName+"/watchlists",
summary: "updating [[User:MusikAnimal/customWatchlists|custom watchlists]]",
text: stringifiedCw
}).done(function(result, jqXHR) {
$("#cw-overlay-body").html("Success!");
mw.notify($("<div>"+updateStr+"</div"));
setTimeout(function() {
$("#cw-overlay").remove();
},3000);
}).fail(function(code, result) {
if ( code === "http" ) {
mw.log( "HTTP error: " + result.textStatus ); // result.xhr contains the jqXHR object
} else if ( code === "ok-but-empty" ) {
mw.log( "Got an empty response from the server" );
} else {
mw.log( "API error: " + code );
}
});
}).fail(function(code, result) {
if ( code === "http" ) {
mw.log( "HTTP error: " + result.textStatus ); // result.xhr contains the jqXHR object
} else if ( code === "ok-but-empty" ) {
mw.log( "Got an empty response from the server" );
} else {
mw.log( "API error: " + code );
}
});
});
};
if(wgRelevantPageName === "Special:Watchlist") {
Line 97 ⟶ 225:
$("#ca-add-to-cw").click(function() {
if($("#cw-overlay")[0]) return false;
getCustomWatchlists().then(
});
}
|