User:Awesome Aasim/editform.js

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(" Loading editor... "); $.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(' ');		       $(' Full editor ').appendTo("#" + this.id + "-editform");		        $('').val(wt.parse.wikitext["*"]).appendTo("#" + this.id + "-editform").css({ "height": "500px", "overflow": "auto" });		        $(' ').appendTo("#" + this.id + "-editform");		        $('  ').append($('').val("")).appendTo("#" + this.id + "-editform");		        $(' ').appendTo("#" + this.id + "-editform");    			$('This is a minor edit ').appendTo("#" + this.id + "-editform");		        $(' ').appendTo("#" + this.id + "-editform");		        $('Cancel ').click(function (_e) {		             this.close; }).css({ "margin-right": "5px", "margin-left": "1px" }).addClass("mw-ui-button").addClass("mw-ui-destructive").appendTo("#" + this.id + "-editform");		       $('Preview ').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("Could not parse preview. Please check your edit carefully. If in doubt, use the full editor. ");		           });		        }).css({ "margin-right": "5px", "margin-left": "1px" }).addClass("mw-ui-button").appendTo("#" + this.id + "-editform");		        $('Save ').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(" " + h.error.info + "  "); } else if (h.edit.nochange == "") { this.close; } else { $("#" + this.id).html(" Edit saved! "); 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"); $(' ').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(' ');		       $(' Full editor</a> ').appendTo("#" + this.id + "-editform");		        $('<textarea id="edit-editarea"/>').val(wt.parse.wikitext["*"]).appendTo("#" + this.id + "-editform").css({ "height": "500px", "overflow": "auto" });		        $(' ').appendTo("#" + this.id + "-editform");		        $('  ').append($('<input id="edit-editsummary" placeholder="Edit summary">').val("")).appendTo("#" + this.id + "-editform");		        $(' ').appendTo("#" + this.id + "-editform");    			$('<input id="edit-minor" type="checkbox"/><label for="edit-minor">This is a minor edit ').appendTo("#" + this.id + "-editform");		        $(' ').appendTo("#" + this.id + "-editform");		        $('<button id="edit-cancel">Cancel ').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 ').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. ");		           });		        }).css({ "margin-right": "5px", "margin-left": "1px" }).addClass("mw-ui-button").appendTo("#" + this.id + "-editform");		        $('<button id="edit-submit">Save ').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(" <span style=\"color:red;\">" + h.error.info + "  "); } else if (h.edit.nochange == "") { this.close; } else { $("#" + this.id).html(" <b><span style=\"color:green;\">Edit saved! "); 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"); $(' ').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); }