Benutzer:P.Copp/scripts/templatepreview.js
< Benutzer:P.Copp | scripts
Hinweis: Leere nach dem Veröffentlichen den Browser-Cache, um die Änderungen sehen zu können.
- Firefox/Safari: Umschalttaste drücken und gleichzeitig Aktualisieren anklicken oder entweder Strg+F5 oder Strg+R (⌘+R auf dem Mac) drücken
- Google Chrome: Umschalttaste+Strg+R (⌘+Umschalttaste+R auf dem Mac) drücken
- Edge: Strg+F5 drücken oder Strg drücken und gleichzeitig Aktualisieren anklicken
// Erweitert beim Bearbeiten von Vorlagen die Vorschau um eine Möglichkeit,
// Vorlageneinbindungen zu testen.
if ((wgNamespaceNumber == 10 || wgNamespaceNumber == 2) && (wgAction=='edit' || wgAction=='submit')) {
importScript('Benutzer:P.Copp/scripts/preprocessor.js'); //[[Benutzer:P.Copp/scripts/preprocessor.js]]<nowiki>
var tplPreview = {
//replace copywarn notice with our new input elements
initialize : function() {
var copywarn = document.getElementById('editpage-copywarn');
if (!copywarn) return;
tplPreview.inputbox = document.createElement('fieldset');
var legend = tplPreview.el('legend','Vorlagenvorschau');
legend.style.padding = 0;
tplPreview.inputbox.appendChild(legend);
tplPreview.togglelink = tplPreview.createToggle('Ausklappen',tplPreview.toggleInput);
tplPreview.inputbox.appendChild(tplPreview.togglelink.parentNode);
tplPreview.inputbox.className = 'portlet pBody tplpreviewinput';
tplPreview.inputbox.style.width = '98%';
copywarn.parentNode.insertBefore(tplPreview.inputbox,copywarn);
copywarn.style.display = 'none';
},
createToggle : function(s,onclick) {
var span = this.el('span','[');
var link = this.el('a',s);
link.href = '#';
link.onclick = onclick;
span.appendChild(link);
span.appendChild(document.createTextNode(']'));
span.style.fontSize = 'x-small';
span.style.cssFloat = 'right';
span.style.styleFloat = 'right';
return link;
},
toggleInput : function () {
if (!tplPreview.inputdiv) tplPreview.createInput();
var t = tplPreview.inputdiv.style.display == 'block';
tplPreview.inputdiv.style.display = t ? 'none' : 'block';
tplPreview.togglelink.firstChild.nodeValue = t ? 'Ausklappen' : 'Einklappen';
return false;
},
createInput : function() {
this.inputdiv = this.el('div','Titel der Seite, von der eingebunden wird: ');
this.titleinput = document.createElement('input');
this.titleinput.type = 'text';
this.titleinput.size = 60;
this.titleinput.value = 'Vorlagentest';
this.inputdiv.appendChild(this.titleinput);
this.inputdiv.appendChild(document.createElement('br'));
this.inputdiv.appendChild(document.createTextNode('Testeinbindung:'));
this.paraminput = this.el('textarea','');
this.paraminput.rows = 10;
this.inputdiv.appendChild(this.paraminput);
var link = this.el('a','Vorschau für diesen Vorlagenaufruf anzeigen');
link.style.fontSize = '150%';
link.href = '#bodyContent';
link.onclick = this.getTextPreview;
this.inputdiv.appendChild(link);
this.inputdiv.appendChild(document.createTextNode(' oder '));
var link = this.el('a','für die angegebene Seite');
link.style.fontSize = '150%';
link.href = '#bodyContent';
link.onclick = this.getPagePreview;
this.inputdiv.appendChild(link);
this.inputbox.appendChild(this.inputdiv);
},
getPagePreview : function () {
if (!tplPreview.previewbox) tplPreview.createPreviewBox();
tplPreview.showState('Erstelle Vorschau...');
tplPreview.title = tplPreview.titleinput.value;
tplPreview.getUrl(wgScriptPath + '/index.php?title='+encodeURIComponent(tplPreview.title)+'&action=raw','',tplPreview.getPreview);
},
getTextPreview : function () {
if (!tplPreview.previewbox) tplPreview.createPreviewBox();
tplPreview.showState('Erstelle Vorschau...');
tplPreview.paraminput.value = tplPreview.extendInput(tplPreview.paraminput.value);
tplPreview.getPreview(tplPreview.paraminput.value);
},
getPreview : function (text) {
var title = tplPreview.titleinput.value;
var template = document.getElementById('wpTextbox1').value.replace(/\n$/,''); //Kill the extra newline at the end
var text = '__NOEDITSECTION__\n'+tplPreview.transform(text,template,wgPageName);//Disable section edit links
tplPreview.getUrl(wgScriptPath + '/api.php?action=parse&format=json&prop=text|categories&pst&title='
+ encodeURIComponent(title),'text='+encodeURIComponent(text),tplPreview.showPreview,true);
},
createPreviewBox : function () {
var box = document.createElement('fieldset');
box.className = 'tplpreviewbox';
box.style.width = '98%';
box.appendChild(tplPreview.el('legend','Vorlagenvorschau'));
box.appendChild(tplPreview.previewbox = tplPreview.el('div',''));
tplPreview.content = document.getElementById('bodyContent')
|| document.getElementById('mw_contentholder')
|| document.getElementById('article');
tplPreview.content.insertBefore(box,tplPreview.content.firstChild);
tplPreview.content.insertBefore(tplPreview.createToggle('Schließen',tplPreview.closePreview).parentNode,
tplPreview.content.firstChild);
},
transform : function (text,template,title) {
this.count = 0;
var title = Wiki.getPage(title).ptitle;
var dom = Wiki.preprocessToObject(text,false);
var tpldom = Wiki.preprocessToObject(template,true);
var PPCustomFrame = function() { this.self = PPCustomFrame; };
PPCustomFrame.prototype = new PPTemplateFrame();
PPCustomFrame.prototype.expandTemplate = function(obj) {
var page = Wiki.getPage(this.expandArg( obj, 0 ).trim(),10);
if (page && page.ptitle == title) {
tplPreview.count++;
return this.expandTemplatePage( page, tpldom, obj );
} else return this.expandBrokenTemplate( obj );
};
var pp = new PPCustomFrame();
return pp.expand(dom);
},
showPreview : function(res) {
tplPreview.showState('Vorschau - ' + tplPreview.count + ' Vorlage(n) ersetzt');
tplPreview.previewbox.innerHTML = res.parse.text['*'];
if (res.parse.categories.length) tplPreview.showCategories(res.parse.categories);
},
showCategories : function(cats) {
var div = document.createElement('div');
div.id = 'catlinks';
div.className = 'catlinks';
var a = this.el('a','Kategorien');
a.href = '/wiki/Spezial:Kategorien';
a.title = 'Spezial:Kategorien';
div.appendChild(a);
div.appendChild(document.createTextNode(': '));
div.appendChild(this.createCatLink(cats[0]));
for (var i=1;i<cats.length;i++) {
div.appendChild(document.createTextNode(' | '));
div.appendChild(this.createCatLink(cats[i]));
}
this.previewbox.appendChild(div);
},
createCatLink : function(cat) {
var name = cat['*'].replace(/_/g,' ');
var a = this.el('a',name);
a.title = 'Kategorie:' + name;
a.href = '/wiki/Kategorie:' + name;
return a;
},
closePreview: function () {
tplPreview.content.removeChild(tplPreview.content.firstChild);
tplPreview.content.removeChild(tplPreview.content.firstChild);
delete tplPreview.previewbox;
return false;
},
getUrl : function( url, data, cb, json) {
var req = sajax_init_object();
req.open( data ? 'POST' : 'GET', url, true );
if (data) req.setRequestHeader( 'Content-Type', 'application/x-www-form-urlencoded' );
req.onreadystatechange = function () {
if ( req.readyState == 4 && req.status == 200 ) {
if (json) eval( 'cb(' + req.responseText + ');' );
else cb(req.responseText);
} else if ( req.readyState == 4 ) tplPreview.showState('Seite "'+tplPreview.title+'" nicht gefunden');
};
req.send( data || '' );
delete req;
},
extendInput: function (text) {
if (!text.match(/^\s*\{\{|\}\}\s*$/))
return '{{:' + wgPageName + (text && '|') + '\n' + text.replace(/^\s*\|/,'') + '\n}}';
else return text;
},
showState: function (s) {
tplPreview.previewbox.parentNode.firstChild.firstChild.nodeValue = s;
},
el : function(name,text) {
var e = document.createElement(name);
e.appendChild(document.createTextNode(text));
return e;
}
};
$(tplPreview.initialize);
}; //end if </nowiki>