Zum Inhalt springen

„Benutzer:P.Copp/scripts/quickrespond.js“ – Versionsunterschied

aus Wikipedia, der freien Enzyklopädie
Inhalt gelöscht Inhalt hinzugefügt
P.Copp (Diskussion | Beiträge)
K fix für Pfeil-Hoch-Helferlein, siehe http://de.wikipedia.org/w/index.php?title=diff=76648762&oldid=76596404
P.Copp (Diskussion | Beiträge)
K Replace missing ts_getInnerText
Zeile 40: Zeile 40:
quickRespond.cursection.num = match[2];
quickRespond.cursection.num = match[2];
} else if (node.className == 'mw-headline') {
} else if (node.className == 'mw-headline') {
quickRespond.cursection.heading = ts_getInnerText(node).replace( /^↑ */, ''); //Remove arrow inserted by some other gadget
quickRespond.cursection.heading = $( node ).text().replace( /^↑ */, ''); //Remove arrow inserted by some other gadget
}
}
}
}

Version vom 11. Januar 2012, 15:33 Uhr

var quickRespond = {
	cursection : false,
	d    : [],
	QR   : function (sig) {
		this.section = quickRespond.cursection;
		this.sig = sig;		
		this.section.sigs[sig] = (this.section.sigs[sig] || 0) + 1;
		this.signum = this.section.sigs[sig];
		this.num = quickRespond.d.length;
		quickRespond.d.push(this);
	},
	Section : function () {
		this.sigs = {};
	},
	Link : function (text,title,func,num) {
		this.link = document.createElement('a');
		this.link.href = 'javascript:quickRespond.d[' + num + '].' + func + '();';
		this.link.title = title;
		this.link.appendChild(document.createTextNode(text));
		this.span = document.createElement('span');
		this.span.appendChild(document.createTextNode(' ['));
		this.span.appendChild(this.link);
		this.span.appendChild(document.createTextNode('] '));
		this.span.className = 'quickrespondlink';
	},
	recAddQRlinks : function (node) {
		if (node.nodeType == 3 && quickRespond.cursection.page) {
			var matches = /.{20}\((?:CES?T|MES?Z)\)/g.exec(node.nodeValue);
			if (!matches) return;
			for (var i=0;i<matches.length;i++)
				var qr = new quickRespond.QR(matches[i]);
			qr.makeEditlink(node);
		} else if (node.nodeType == 1) {
			if (node.nodeName.match(/H[123456]/i)) {
				quickRespond.cursection = new quickRespond.Section();
			} else if (node.nodeName == 'SPAN') {
				if (node.className == 'editsection') {
					var match = node.childNodes[1].href.match(/\?title=([^&]*).*&section=T?-?(\d*)/);
					quickRespond.cursection.page = match[1];
					quickRespond.cursection.num = match[2];
				} else if (node.className == 'mw-headline') {
					quickRespond.cursection.heading = $( node ).text().replace( /^↑ */, ''); //Remove arrow inserted by some other gadget
				}
			}
			if (node.hasChildNodes())
				for (var i=0;i<node.childNodes.length;i++)
					quickRespond.recAddQRlinks(node.childNodes[i]);
		}
	},
	callApi : function (query,data,cb) {
		var method = data ? 'POST' : 'GET';
		var url = wgScriptPath + '/api.php?format=json&' + query;
		var	req = sajax_init_object();
		if (!req) {alert('AJAX wird von diesem Browser nicht unterstützt.');return;}
		req.open(method,url,true);
		if (method=='POST')
			req.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
		req.onreadystatechange = function() {
			if (req.readyState == 4) {
				if (req.status == 200)
					eval('cb(' + req.responseText + ');');
				else if (confirm('Verbindungsfehler. Erneut versuchen?'))
					quickRespond.callApi(query,data,cb);
			}
		};
		req.send(data || '');
		delete req;
	}
}

//API functions
quickRespond.Section.prototype.fetchContent = function (qr,reload) {
	var sc = this;
	if (!reload && sc.content) {qr.checkSection();return;}
	var query = 'action=query&rvprop=content|timestamp&rvsection=' + sc.num + '&titles='
		+ sc.page + '&prop=revisions';
	if (!quickRespond.token) query += '|info&intoken=edit';
	quickRespond.callApi(query,'',function (res) {
	if (!res || !res.query || !res.query.pages) {alert('Abschnitt wurde verändert oder entfernt.');return;}
	for (var i in res.query.pages) {
		var p = res.query.pages[i];
		if (p.edittoken) quickRespond.token = p.edittoken;
		sc.content = p.revisions ? p.revisions[0]['*'] : '';
		sc.timestamp = p.revisions ? p.revisions[0].timestamp : '';
	}
	qr.checkSection();
	});
};

quickRespond.QR.prototype.checkSection = function () {
	this.pos = -1;
	for (i=0;i<this.signum;i++) this.pos = this.section.content.indexOf(this.sig,this.pos+1);
	if (this.pos == -1) {alert('Beitrag wurde verändert oder entfernt.');return;}
	if (this.text) this.trySave();
};

quickRespond.QR.prototype.save = function () {
	if (!this.area.value) {alert('Kein Text eingegeben.');return;}
	this.savelink.progress('Speichere...');
	this.text = this.area.value;
	this.comment = this.commentinput.value;
	if (!this.comment) {//<nowiki>
		var stripped = this.text.match(/^[\\:\*#;]*((.|\n)*)/m)[1].replace(/\n/g,' ').replace(/~~~~/g,'');//</nowiki>
		this.comment = stripped.length < 50 ? stripped : 'aw';
	}
	this.comment = '/* ' + this.section.heading + ' */ ' + this.comment;
	if (this.section.content) this.trySave();
};

quickRespond.QR.prototype.preview = function () {
	var qr = this;
	var l = this.area.value.match(/^\\*/)[0].length;
	var text = this.area.value.substr(l);
	quickRespond.callApi('action=parse&prop=text&title=' + qr.section.page + '&text=' + encodeURIComponent(text),'',function (res) {
	if (!res || !res.parse || !res.parse.text) {alert('Fehler beim Erstellen der Vorschau, bitte erneut versuchen.');return;}
	if (!qr.previewbox) {
		qr.previewbox = document.createElement('div');
		qr.previewbox.className = 'quickrespondpreview';
		qr.editform.insertBefore(qr.previewbox,qr.area);
	}
	qr.previewbox.innerHTML = res.parse.text['*'] || '';
	qr.area.focus();
	});
};

quickRespond.QR.prototype.transformed = function () {
	var backsl = this.text.match(/^\\*/)[0].length;
	if (backsl == 1)
		var prefix = '';
	else {
		var linestart = this.section.content.lastIndexOf('\n',this.pos) + 1;
		var colons = this.section.content.substring(linestart,this.pos).match(/^[:\*#]*/)[0] + ':';
		var prefix = colons.substr(backsl ? backsl - 1 : 0);
	}
	var text = prefix + this.text.substr(backsl).replace(/\n([^\n])/g,'\n' + prefix + '$1');//<nowiki>
	if (text.indexOf('~~~~') == -1) text += window.usersignature || '-- ~~~~';//</nowiki>
	if (text[text.length-1] != '\n') text += '\n';
	return text;
}

quickRespond.QR.prototype.insertresponse = function (text) {
	var c = this.section.content;
	var firstprefix = text.match(/^[:\*#]*/)[0].length;
	var ins = c.indexOf('\n',this.pos) + 1;
	while (ins>0 && (c[ins] == '\n' || c.substr(ins).match(/^[:\*#]*/)[0].length >= firstprefix))
		ins = c.indexOf('\n',ins) + 1;
	if (ins == 0) ins = c.length;
	if (c[ins-1] != '\n') text = '\n' + text;
	return c.substr(0,ins) + text + c.substring(ins,c.length);
}

quickRespond.QR.prototype.trySave = function () {
	var qr = this;
	var text = qr.transformed();
	var newtext = qr.insertresponse(text);
	var query = 'action=edit&nocreate&title=' + qr.section.page + '&section=' + qr.section.num + '&token='
		+ encodeURIComponent(quickRespond.token) + '&summary=' + encodeURIComponent(qr.comment)
		+ '&basetimestamp=' + encodeURIComponent(qr.section.timestamp);
	quickRespond.callApi(query,'text=' + encodeURIComponent(newtext),function (res) {
	if (res && res.edit && res.edit.result == 'Success') {
		qr.makeSuccessMsg(res.edit.oldrevid,res.edit.newrevid,text);
	} else if (res && res.error){
		if (res.error.code == 'editconflict') {
			qr.savelink.progress('BK auflösen...');
			qr.section.fetchContent(qr,true);
		} else {
			alert('Fehler beim Bearbeiten: '+res.error.info);
			qr.savelink.restore();
		}
	} else {
		alert('Fehler beim Bearbeiten, bitte erneut versuchen.');
		qr.savelink.restore();
	}
	});
};

//UI functions
quickRespond.Link.prototype.appendTo = function (node) {
	node.appendChild(this.span);
};
quickRespond.Link.prototype.progress = function (text) {
	this.span.replaceChild(document.createTextNode(text),this.span.childNodes[1]);
};
quickRespond.Link.prototype.restore = function () {
	this.span.replaceChild(this.link,this.span.childNodes[1]);
};

quickRespond.QR.prototype.makeEditlink = function (node) {
	var parent = node.parentNode;
	var nextcont = node.nextSibling;
	while (nextcont && nextcont.nodeName != 'DL' && nextcont.nodeName != 'P') {
		if (nextcont.className == 'quickrespondlink') return;
		nextcont = nextcont.nextSibling;
	}
	this.editlink = new quickRespond.Link('+','Antworten','toggle',this.num);
	parent.insertBefore(this.editlink.span,nextcont);
};	

quickRespond.QR.prototype.toggle = function () {
	var t = this.editlink.link.firstChild.nodeValue == '+';
	this.editlink.link.firstChild.nodeValue = t ? '--' : '+';
	if (this.editform)
		this.editform.style.display = t ? 'block' : 'none';
	else {
		this.makeEditForm();
		this.section.fetchContent(this,false);
	}
};

quickRespond.QR.prototype.makeEditForm = function () {
	var dl = document.createElement('dl');
	this.editform = document.createElement('dd');
	this.editform.className = 'quickrespondform';
	dl.appendChild(this.editform);
	this.area = document.createElement('textarea');
	this.savelink = new quickRespond.Link('Speichern','','save',this.num);
	this.previewlink = new quickRespond.Link('Vorschau','','preview',this.num);
	this.commentinput = document.createElement('input');
	this.commentinput.type = 'text';
	this.commentinput.size = 80;
	this.editform.appendChild(this.area);
	this.savelink.appendTo(this.editform);
	this.previewlink.appendTo(this.editform);
	this.editform.appendChild(this.commentinput);
	var parent = this.editlink.span.parentNode;
	if (parent.nodeName=='DD')
		parent.insertBefore(dl,this.editlink.span.nextSibling);
	else
		parent.parentNode.insertBefore(dl,parent.nextSibling);
	this.area.focus();
};

quickRespond.QR.prototype.makeSuccessMsg = function(oldid,newid,text) {
	var dd = document.createElement('dd');
	dd.className = 'quickrespondsuccess';
	dd.appendChild(document.createTextNode('Antwort wurde erfolgreich gespeichert. ('));
	var link = document.createElement('a');
	link.appendChild(document.createTextNode('Difflink'));
	link.href = wgArticlePath.replace(/\$1/g,'?diff=' + newid + '&oldid=' + oldid);
	dd.appendChild(link);
	dd.appendChild(document.createTextNode(')'));
	dd.appendChild(document.createElement('br'));
	var pre = document.createElement('pre');
	pre.appendChild(document.createTextNode(text));
	dd.appendChild(pre);
	this.toggle();
	this.editform.parentNode.replaceChild(dd,this.editform);
	delete this.editform;
	delete this.text;
	if (this.previewbox) delete this.previewbox;
};


if( wgNamespaceNumber > 0 && ( wgAction == 'view' || wgAction == 'historysubmit' ) ) {
	addOnloadHook(function() {
		var content = document.getElementById('bodyContent')
			|| document.getElementById('mw_contentholder')
			|| document.getElementById('article');
		quickRespond.recAddQRlinks(content);
	});
	appendCSS('.quickrespondlink {font-size: x-small;}\n'
		+'.quickrespondpreview {border: 1px solid #bbbbbb;padding: 0.2em;}\n'
		+'.quickrespondform textarea {width: 99%}\n'
		+'.quickrespondsuccess {background-color: #ddffcc;padding: 0.2em;}');
}