Jump to content

User:PerfektesChaos/js/jsonDebug/r.js

From Wikipedia, the free encyclopedia
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.
/// User:PerfektesChaos/js/jsonDebug/r.js
/// 2018-08-24 PerfektesChaos@de.wikipedia
/// Fingerprint:#0#1A19FAF0#
/// @license GPL [//www.mediawiki.org/w/COPYING](+GFDL,LGPL,CC-BY-SA)
///<nowiki>
(function(mw,$){
"use strict";
var Version=1.2,Signature="jsonDebug",JSOND={site:"w:en",store:"User:PerfektesChaos/js/"+Signature,sel:"json-code-lint",src:"div,pre",using:null},REPOS={requests:false},UI={bad:{"background-color":"#F0F080","border":"#FF0000 1px solid",
"color":"#FF0000","font-family":"monospace",
"font-size":"80%","font-weight":"normal",
"padding":"0.3em"},code:{"margin-top":"1em",
"max-width":"95%","width":"95%"},css:{"background-color":"#D0D0D0","border":"#808080 1px solid",
"clear":"both","margin-bottom":"1rem",
"max-width":"95%","padding":"1em"},normal:10,$button:false},VALID={jsonlint:false,reLineNum:"line ([0-9]+)"},ZEBRA={codemirror:false};
JSOND.feed=function(ask){
var s;
if(typeof ask==="string"){
s=ask.trim();
if(s){
JSOND.source=s;
if(JSOND.using){
mw.loader.using(JSOND.using,JSOND.feeder);
}else{
JSOND.feeder();}}}
};
JSOND.feeder=function(){
$(JSOND.feeding);
};
JSOND.feeding=function(){
VALID.fire(JSOND.source);
JSOND.flip();
};
JSOND.fire=function(){
JSOND.support=Signature+"-button";
mw.hook("wikipage.content").add(JSOND.fresh);
};
JSOND.fired=function(event){
var $button,$json;
event.stopPropagation();
JSOND.flip();
JSOND.source=false;
if(JSOND.large){
if(UI.live){
JSOND.source=UI.$source.val();
}else{
JSOND.source=UI.$source.text();}
}else{
$button=$(event.target);
$json=$button.next();
$button.attr({disabled:true});
if($json.hasClass(JSOND.sel)&&$json.filter(JSOND.src)){
JSOND.source=$json.text();}}
if(JSOND.source){
JSOND.source=JSOND.source.trim();
VALID.fire(JSOND.source);}
};
JSOND.first=function(){
var env,pub,rls;
JSOND.signature="ext.gadget."+Signature;
if(mw.loader.getState(JSOND.signature)!=="ready"){
env=mw.config.get(["wgAction","wgPageContentModel"]);
rls={};
rls[JSOND.signature]="ready";
mw.loader.state(rls);
JSOND.large=(env.wgPageContentModel.toLowerCase().indexOf("json")>=0);
switch(env.wgAction){
case "view":
case "edit":
case "submit":
case "parsermigration-edit":
if(JSOND.using){
mw.loader.using(JSOND.using,JSOND.fire);
}else{
JSOND.fire();}
break;}
pub={doc:"[["+JSOND.site+":"+JSOND.store+"]]",feed:JSOND.feed,type:Signature,vsn:Version};
mw.hook(Signature+".ready").fire(pub);}
};
JSOND.flip=function(){
UI.$area.find("."+JSOND.support).attr({disabled:false});
};
JSOND.fresh=function($area){
var $e;
UI.$area=$area;
if(JSOND.large){
$e=UI.$area.find("#wpTextbox1");
UI.live=$e.length;
if(UI.live){
UI.$source=$e;
}else{
UI.$source=false;
$e=UI.$area.children();
if($e.length===1){
$e=$e.children();
if($e.length===1&&$e.hasClass("mw-highlight")){
$e=$e.children();
if($e.length===1&&!$e.children().length&&$e.filter("pre").length){
UI.$source=$e;}}}}
if(UI.$source&&!JSOND.$button){
UI.$button=JSOND.furnish(0,$area.get(0));
}else if(JSOND.$button){
JSOND.$button.remove();
JSOND.$button=false;}
}else{
UI.$area.find("."+JSOND.support).remove();
UI.$area.find("."+JSOND.sel).filter(JSOND.src).each(JSOND.furnish);}
};
JSOND.furnish=function(at,area){
var $r=$("<button>").addClass(JSOND.support).click(JSOND.fired).css({"margin-left":"0.5em",
"margin-right":"0.5em"}).text(Signature);
$(area).before($r);
return $r;
};
REPOS.fire=function(at,access,append,alter){
var source,syntax;
if(typeof REPOS.requests!=="object"){
REPOS.requests={};}
if(typeof REPOS.requests[access]!=="boolean"){
REPOS.requests[access]=true;
if(append){
source=access+append;
}else{
source=access;}
if(at){
source=REPOS.foundation(at,source,alter);
if(typeof alter==="object"&&alter&&typeof alter.ctype==="string"){
syntax=alter.ctype;}
}else{
syntax=alter;}
mw.loader.load(source,syntax);}
};
REPOS.foundation=function(at,access,alter){
var s=access,r=encodeURI(s);
if(typeof alter==="object"&&alter){
r="/w/index.php?title="+r;
if(access.substr(-3)===".js"){
alter.ctype="text/javascript";
}else if(access.substr(-4)===".css"){
alter.ctype="text/css";}
alter.action="raw";
for(s in alter){
r=r+"&"+s+"="+encodeURI(alter[s]);}
}else{
r="/wiki/"+r;}
if(typeof at==="string"&&at){
switch(at){
case "meta":
r="meta.wikimedia.org"+r;
break;
case "mw":
r="www.mediawiki.org"+r;
break;
case "w:en":
r="en.wikipedia.org"+r;
break;
default:
r=window.location.host+r;}
r="https://"+r;}
return r;
};
UI.factory=function(){
var ltr=($("html").attr("dir")!=="rtl"),$head=$("<div>"),$e;
UI.$wrapper=$("<div>").addClass(UI.signature);
UI.$wrapper.css({"clear":"both","font-size":"1rem",
"font-style":"normal","font-weight":"normal",
"max-width":"100%","width":"100%"});
if(typeof UI.css==="object"&&UI.css){
UI.$wrapper.css(UI.css);}
if(typeof JSOND.store==="string"&&JSOND.store){
$e=$("<a>").attr({href:REPOS.foundation(JSOND.site,JSOND.store),target:Signature,title:"Tool info / doc"});
}else{
$e=$("<span>");}
$e.css({"font-family":"monospace","font-size":"1.6em",
"font-weight":"bold"}).text(Signature);
$head.append($e);
$e=$("<span>").css({"font-size":"smaller","margin-left":"2em",
"margin-right":"2em"}).text(Version+"");
$head.append($e).css({"float":(ltr?"left":"right")});
UI.$wrapper.append($head);
$e=$("<button>").click(UI.flip).css({"color":"#FF0000",
"float":(ltr?"right":
"left"),"font-weight":"bold",
"padding":"0.2em"}).text("X");
UI.$wrapper.append($e);
UI.$area.prepend(UI.$wrapper);
$e=$("<div>").css({"clear":"both"});
UI.$wrapper.append($e);
UI.$warn=$("<pre>").addClass(Signature+"-warn").css({"clear":"both",
"overflow":"auto"}).hide();
if(typeof UI.bad==="object"&&UI.bad){
UI.$warn.css(UI.bad);}
UI.$wrapper.append(UI.$warn);
UI.$code=$("<textarea>").addClass(Signature+"-code").attr({disabled:true}).css({"overflow":"auto"})
.hide();
if(typeof UI.code==="object"&&UI.code){
UI.$code.css(UI.code);}
UI.$wrapper.append(UI.$code);
};
UI.fire=function(apply,alert){
var got,k,n;
UI.signature=Signature+"-wrapper";
UI.$wrapper=UI.$area.find("."+UI.signature);
if(!UI.$wrapper.length){
UI.factory();}
if(!apply||!alert){
UI.$warn.hide();
if(!apply){
UI.$code.hide();}}
UI.$area.scrollTop(0);
UI.$wrapper.show();
if(apply){
if(alert){
UI.$warn.show().text(alert);
if(typeof VALID.reLineNum==="string"){
VALID.reLineNum=new RegExp(VALID.reLineNum);}
if(typeof VALID.reLineNum==="object"){
got=alert.match(VALID.reLineNum);
if(got){
k=parseInt(got[1],10);
if(k>1){
k--;}}}}
n=apply.split("\n").length;
if(n>UI.normal){
n=UI.normal;}
UI.$code.attr("rows",n).show().val(apply);
ZEBRA.fire(apply,k);}
};
UI.flip=function(){
UI.$wrapper.hide();
JSOND.flip();
};
VALID.fiat=function(){
VALID.scream=false;
try{
VALID.jsonlint.parse(VALID.source);
}catch(e){
VALID.scream=e.message;}
UI.fire(VALID.source,VALID.scream);
};
VALID.fire=function(apply){
UI.fire();
ZEBRA.first();
VALID.source=apply;
if(VALID.jsonlint){
VALID.fiat();
}else if(VALID.jsonlint===false){
VALID.jsonlint=null;
REPOS.fire(JSOND.site,JSOND.store+"/jsonlint.js",false,{action:"raw",ctype:"text/javascript",bcache:1,maxage:604800});
mw.hook(Signature+".jsonlint").add(VALID.furnish);}
};
VALID.furnish=function(application){
if(application&&typeof application==="object"&&typeof application.parse==="function"){
VALID.jsonlint=application;
VALID.fiat();}
};
ZEBRA.config={autofocus:true,gutters:["CodeMirror-linenumbers"],lineNumbers:true,mode:"javascript",readOnly:true,tabSize:2,theme:Signature};
ZEBRA.fiat=function(apply){
if(typeof apply==="object"&&apply&&typeof apply.mode==="function"){
ZEBRA.codemirror.defineMode("javascript",apply.mode);}
if(ZEBRA.editor){
ZEBRA.editor.toTextArea();}
ZEBRA.editor=ZEBRA.codemirror.fromTextArea(UI.$code.get(0),ZEBRA.config);
ZEBRA.editor.setSize(null,UI.$code.height());
ZEBRA.editor.setValue(ZEBRA.source);
if(ZEBRA.invalid){
ZEBRA.focus(ZEBRA.invalid);}
};
ZEBRA.fire=function(apply,at){
ZEBRA.source=apply;
ZEBRA.invalid=at||false;
if(ZEBRA.codemirror){
ZEBRA.fiat();
}else{
mw.loader.using(["ext.CodeMirror"],ZEBRA.furnish);}
};
ZEBRA.first=function(){
if(ZEBRA.codemirror===false){
ZEBRA.codemirror=null;
mw.loader.load(["ext.CodeMirror"]);
REPOS.fire(JSOND.site,JSOND.store+".css",false,{action:"raw",ctype:"text/css",bcache:1,maxage:604800});
REPOS.fire(JSOND.site,JSOND.store+"/codemirrorJavascript.js",false,{action:"raw",ctype:"text/javascript",bcache:1,maxage:604800});}
};
ZEBRA.focus=function(at){
ZEBRA.editor.addLineClass(at,"background",Signature+"-line-error");
ZEBRA.editor.setCursor(at);
};
ZEBRA.furnish=function(){
if(!ZEBRA.codemirror&&typeof window.CodeMirror!=="undefined"&&window.CodeMirror){
ZEBRA.codemirror=window.CodeMirror;
ZEBRA.editor=false;}
if(ZEBRA.codemirror){
mw.hook(Signature+".cm-mode-js").add(ZEBRA.fiat);}
};
JSOND.first();
}(window.mediaWiki,window.jQuery));
/// EOF</nowiki>jsonDebug/r.js