Jump to content

User:Awesome Aasim/editform.js

From Wikipedia, the free encyclopedia
The printable version is no longer supported and may have rendering errors. Please update your browser bookmarks and please use the default browser print function instead.
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
function EditForm(id, p, _then, presum, postsum) {
	this.id = id;
	this._then = _then;
	this.presum = presum;
	this.postsum = postsum;
    this.revert = $("#" + this.id).html();
    this.wikitext = "";
    $("#" + this.id).html("<p><b>Loading editor...</b></p>");
    $.get(mw.config.get('wgScriptPath') + '/api.php', {
        "action": "query",
        "format": "json",
        "meta": "tokens",
        "type": "csrf"
    }).done(function (r) {
    	console.log("Part 1 done");
    	$.get(mw.config.get('wgScriptPath') + '/api.php', {
	        "action": "parse",
	        "format": "json",
	        "prop": "wikitext",
	        "page": p,
	        "type": "csrf"
    	}).done(function(wt) {
    		console.log("Part 2 done");
    		if (wt.error) {
    			console.log("Server returned an error");
    			if (wt.error.code == "missingtitle") {
    			this.wikitext = "";
    			$("#" + this.id).html('<form id="' + this.id + '"-editform"></form>');
		        $('<p><a title="Full editor" target="_blank" href="/w/index.php?title=' + p + '&action=edit">Full editor</a></p>').appendTo("#" + this.id + "-editform");
		        $('<textarea id="edit-editarea"/>').val(wt.parse.wikitext["*"]).appendTo("#" + this.id + "-editform").css({ "height": "500px", "overflow": "auto" });
		        $('<br>').appendTo("#" + this.id + "-editform");
		        $('<p></p>').append($('<input id="edit-editsummary" placeholder="Edit summary">').val("")).appendTo("#" + this.id + "-editform");
		        $('<span id="edit-summary"></span>').appendTo("#" + this.id + "-editform");
    			$('<input id="edit-minor" type="checkbox"/><label for="edit-minor">This is a minor edit</label>').appendTo("#" + this.id + "-editform");
		        $('<br>').appendTo("#" + this.id + "-editform");
		        $('<button id="edit-cancel">Cancel</button>').click(function (_e) {
		             this.close();
		        }).css({ "margin-right": "5px", "margin-left": "1px" }).addClass("mw-ui-button").addClass("mw-ui-destructive").appendTo("#" + this.id + "-editform");
		        $('<button id="edit-previewbutton">Preview</button>').click(function (_e) {
		            $.get(mw.config.get('wgScriptPath') + '/api.php', {
		                "action": 'parse',
		                "prop": 'text',
		                "pst": 'true',  // PST = pre-save transform; this makes substitution work properly
		                "text": $("#edit-editarea").val(),
		                "summary": $("#edit-editsummary").val() ? $("#edit-editsummary").val() : "",
		                "format": "json",
		                "title": p
		            }).done(function (r) {
		                console.log(r);
		                if (r.error) {
		                    console.error(r.error);
		                } else {
		                    $("#edit-preview").html(r.parse.text["*"]);
		                    $("#edit-summary").html(r.parse.parsesummary["*"]);
		                }
		            }).fail(function (_r) {
		                $("#edit-preview").html("<span class=\"error\">Could not parse preview.  Please check your edit carefully.  If in doubt, use the full editor.</span>");
		            });
		        }).css({ "margin-right": "5px", "margin-left": "1px" }).addClass("mw-ui-button").appendTo("#" + this.id + "-editform");
		        $('<button id="edit-submit">Save</button>').click(function (_e) {
		            $.post(mw.config.get("wgScriptPath") + '/api.php', {
		                "action": "edit",
		                "format": "json",
		                "title": p,
		                "text": $("#edit-editarea").val(),
		                "summary": (presum ? (presum + ($("#edit-editsummary").val() ? ": " + $("#edit-editsummary").val() : "")) : $("#edit-editsummary").val() ? $("#edit-editsummary").val() : "") + postsum,
		                "minor": $("#edit-minor").is(":checked"),
		                "notminor": !$("#edit-minor").is(":checked"),
		                "token": r.query.tokens.csrftoken
		            }).done(function (h) {
		                if (h.error) {
		                    mw.notify("<p><b><span style=\"color:red;\">" + h.error.info + "</span></b></p>");
		                } else if (h.edit.nochange == "") {
		                    this.close();
		                } else {
		                    $("#" + this.id).html("<p><b><span style=\"color:green;\">Edit saved!</span></p>");
		                    this._then();
		                }
		            }).fail(function (_h) {
						throw _h;
		            });
		        }).css({ "margin-right": "5px", "margin-left": "1px" }).addClass("mw-ui-button").addClass("mw-ui-progressive").appendTo("#" + this.id + "-editform");
		        $('<div id="edit-preview"></div>').appendTo("#" + this.id + "-editform");
    			} else {
    				mw.notify("Error while loading editor:  " + wt.error);
    			}
    		} else {
    			this.wikitext = wt.parse.wikitext["*"];
    			$("#" + this.id).text("");
    			$("#" + this.id).html('<form id="' + this.id + '"-editform"></form>');
		        $('<p><a title="Full editor" target="_blank" href="/w/index.php?title=' + p + '&action=edit">Full editor</a></p>').appendTo("#" + this.id + "-editform");
		        $('<textarea id="edit-editarea"/>').val(wt.parse.wikitext["*"]).appendTo("#" + this.id + "-editform").css({ "height": "500px", "overflow": "auto" });
		        $('<br>').appendTo("#" + this.id + "-editform");
		        $('<p></p>').append($('<input id="edit-editsummary" placeholder="Edit summary">').val("")).appendTo("#" + this.id + "-editform");
		        $('<span id="edit-summary"></span>').appendTo("#" + this.id + "-editform");
    			$('<input id="edit-minor" type="checkbox"/><label for="edit-minor">This is a minor edit</label>').appendTo("#" + this.id + "-editform");
		        $('<br>').appendTo("#" + this.id + "-editform");
		        $('<button id="edit-cancel">Cancel</button>').click(function (_e) {
		            this.close();
		        }).css({ "margin-right": "5px", "margin-left": "1px" }).addClass("mw-ui-button").addClass("mw-ui-destructive").appendTo("#" + this.id + "-editform");
		        $('<button id="edit-previewbutton">Preview</button>').click(function (_e) {
		            $.get(mw.config.get('wgScriptPath') + '/api.php', {
		                "action": 'parse',
		                "prop": 'text',
		                "pst": 'true',  // PST = pre-save transform; this makes substitution work properly
		                "text": $("#edit-editarea").val(),
		                "summary": $("#edit-editsummary").val() ? $("#edit-editsummary").val() : "",
		                "format": "json",
		                "title": p
		            }).done(function (r) {
		                console.log(r);
		                if (r.error) {
		                    console.error(r.error);
		                } else {
		                    $("#edit-preview").html(r.parse.text["*"]);
		                    $("#edit-summary").html(r.parse.parsesummary["*"]);
		                }
		            }).fail(function (_r) {
		                $("#edit-preview").html("<span class=\"error\">Could not parse preview.  Please check your edit carefully.  If in doubt, use the full editor.</span>");
		            });
		        }).css({ "margin-right": "5px", "margin-left": "1px" }).addClass("mw-ui-button").appendTo("#" + this.id + "-editform");
		        $('<button id="edit-submit">Save</button>').click(function (_e) {
		            $.post(mw.config.get("wgScriptPath") + '/api.php', {
		                "action": "edit",
		                "format": "json",
		                "title": p,
		                "text": $("#edit-editarea").val(),
		                "summary": (presum ? (presum + ($("#edit-editsummary").val() ? ": " + $("#edit-editsummary").val() : "")) : $("#edit-editsummary").val() ? $("#edit-editsummary").val() : "") + postsum,
		                "minor": $("#edit-minor").is(":checked"),
		                "notminor": !$("#edit-minor").is(":checked"),
		                "token": r.query.tokens.csrftoken
		            }).done(function (h) {
		                if (h.error) {
		                    mw.notify("<p><b><span style=\"color:red;\">" + h.error.info + "</span></b></p>");
		                } else if (h.edit.nochange == "") {
		                    this.close();
		                } else {
		                    $("#" + this.id).html("<p><b><span style=\"color:green;\">Edit saved!</span></p>");
		                    this._then();
		                }
		            }).fail(function (_h) {
						throw _h;
		            });
		        }).css({ "margin-right": "5px", "margin-left": "1px" }).addClass("mw-ui-button").addClass("mw-ui-progressive").appendTo("#" + this.id + "-editform");
		        $('<div id="edit-preview"></div>').appendTo("#" + this.id + "-editform");
    		}
    	}).fail(function(wt) {
    		throw wt;
    	})

    }).fail(function (r) {
        throw r;
    });
}

EditForm.prototype.disable = function() {
	$("#edit-editarea").prop("disabled", true);
	$("#edit-editsummary").prop("disabled", true);
	$("#edit-minor").prop("disabled", true);
	$("#edit-cancel").prop("disabled", true);
	$("#edit-previewbutton").prop("disabled", true);
	$("#edit-submit").prop("disabled", true);
}
EditForm.prototype.enable = function() {
	$("#edit-editarea").prop("disabled", false);
	$("#edit-editsummary").prop("disabled", false);
	$("#edit-minor").prop("disabled", false);
	$("#edit-cancel").prop("disabled", false);
	$("#edit-previewbutton").prop("disabled", false);
	$("#edit-submit").prop("disabled", false);
}
EditForm.prototype.close = function() {
	$("#" + this.id).html(this.revert);
}