לדלג לתוכן

מדיה ויקי:Gadget-TemplatesExternalLinks.js: הבדלים בין גרסאות בדף

מתוך צפונות ויקי
formatting
חילוץ פרמטרים
שורה 9: שורה 9:
}
}


function popupPredefinedLinkTemplate(templateName, paramList) {
function popupPredefinedLinkTemplate(templateName, paramList, regexDict) {
var height = 100 + 30 * paramList.length;
var height = 100 + 30 * paramList.length + (regexDict ? 60 : 0);
for (i in paramList)
for (i in paramList)
height += 20 * (paramList[i].length / 60)
height += 20 * (paramList[i].length / 60)
שורה 21: שורה 21:
var form = doc.createElement("form");
var form = doc.createElement("form");
var body = doc.body;
var body = doc.body;
if (regexDict) {
body.appendChild(doc.createTextNode('הדביקו את הקישור כאן:'));
var b = doc.createElement("input");
b.urlInput = doc.createElement("input");
b.urlInput.type = "text";
b.urlInput.maxLength = 600;
b.urlInput.style.width = "20em";
body.appendChild(b.urlInput);
b.type = "button";
b.value = "חילוץ פרמטרים מן הקישור";
b.regexDict = regexDict;
b.doc = doc;
b.onclick = function() {
var matches = this.urlInput.value.match(this.regexDict.regex);
if (matches) {
for (var i = 1; i < matches.length; i++) {
var fieldIndex = this.regexDict.params[i-1]-1; //parameters are counted from one, we count from 0.
if (fieldIndex && this.doc.allFields[fieldIndex])
this.doc.allFields[fieldIndex].value = matches[i];
}
}
}
body.appendChild(b);
}
var table = doc.createElement("table");
var table = doc.createElement("table");
for (i in paramList) {
for (i in paramList) {
שורה 47: שורה 71:
var label = doc.createElement('label');
var label = doc.createElement('label');
label.htmlFor = refCheckBox;
label.htmlFor = refCheckBox;
label.innerHTML =  "מקם את התבנית בתוך הערת שוליים";
label.innerHTML =  "מיקום התבנית בתוך הערת שוליים";
body.appendChild(label);
body.appendChild(label);
body.appendChild(refCheckBox);
body.appendChild(refCheckBox);
שורה 95: שורה 119:
select.options.add(new Option(allnames[i], allnames[i]));
select.options.add(new Option(allnames[i], allnames[i]));
var toolbar = document.getElementById("toolbar");
var toolbar = document.getElementById("toolbar");
if (toolbar)
if (toolbar)  
toolbar.appendChild(select);
toolbar.appendChild(select);
}
}
function templateRegex(templateName) {
var regexes = {
nrg: {regex: /http:\/\/(?:www\.){0,1}nrg\.co\.il\/online\/([^\/]+)\/ART([^\/]+)\/([^\.]+).html/i, params:[6,7,3]},
}
return regexes[templateName];
}
function fireLinkTemplatePopup(templateName) {
var linkTemplates = knownLinkTemplates();
var templateParams = linkTemplates[templateName];
if (templateParams && templateParams.constructor == Array && templateParams.length > 0)
popupPredefinedLinkTemplate(templateName, templateParams, templateRegex(templateName));
}
if (wgAction == 'edit')
addOnloadHook(createLinkTemplatesSelections);


function knownLinkTemplates() {
function knownLinkTemplates() {
שורה 124: שורה 165:
];
];
var teplatesDic = {
var templatesDic = {
"ynet": [1,2,3,4,5,0,],
"ynet": [1,2,3,4,5,0,],
"הארץ": [1, 2, 3, 4],
"הארץ": [1, 2, 3, 4],
שורה 132: שורה 173:
"Mooma": [18],
"Mooma": [18],
};
};
for (var key in teplatesDic)
for (var key in templatesDic)
for (var i in teplatesDic[key])
for (var i in templatesDic[key])
if (typeof(teplatesDic[key][i]) == "number")
if (typeof(templatesDic[key][i]) == "number")
teplatesDic[key][i] = constants[teplatesDic[key][i]];
templatesDic[key][i] = constants[templatesDic[key][i]];
return teplatesDic;
return templatesDic;
}
}
function fireLinkTemplatePopup(templateName) {
var linkTemplates = knownLinkTemplates();
var templateParams = linkTemplates[templateName];
if (templateParams && templateParams.constructor == Array && templateParams.length > 0)
popupPredefinedLinkTemplate(templateName, templateParams);
}
if (wgAction == 'edit')
addOnloadHook(createLinkTemplatesSelections);

גרסה מ־15:49, 5 בפברואר 2011

function createAndInsertLinkTemplate(templateName, fields, isRef) {
	var par = ["{{" + templateName];
	for (var i = 0; i < fields.length; i++)
		par.push(fields[i].value);
	var code = par.join("|") + "}}";
	if (isRef)
		code = "{{הערה|1=" + code + "}}";
	insertTags( "", "", code);
}

function popupPredefinedLinkTemplate(templateName, paramList, regexDict) {
	var height = 100 + 30 * paramList.length + (regexDict ? 60 : 0);
	for (i in paramList)
		height += 20 * (paramList[i].length / 60)
	var popup = window.open("", "popup", "height=" + height + ",width=500" );
	var doc = popup.document;
	doc.title =  " הוספת תבנית: " + templateName;
	doc.direction = doc.dir = "rtl";	
	doc.templateName = templateName;
	doc.allFields = [];
	var form = doc.createElement("form");
	var body = doc.body;
	if (regexDict) {
		body.appendChild(doc.createTextNode('הדביקו את הקישור כאן:'));
		var b = doc.createElement("input");
		b.urlInput = doc.createElement("input");
		b.urlInput.type = "text";
		b.urlInput.maxLength = 600;
		b.urlInput.style.width = "20em";
		body.appendChild(b.urlInput);
		b.type = "button";
		b.value = "חילוץ פרמטרים מן הקישור";
		b.regexDict = regexDict;
		b.doc = doc;
		b.onclick = function() {
			var matches = this.urlInput.value.match(this.regexDict.regex);
			if (matches) {
				for (var i = 1; i < matches.length; i++) {
					var fieldIndex = this.regexDict.params[i-1]-1; //parameters are counted from one, we count from 0.
					if (fieldIndex && this.doc.allFields[fieldIndex])
						this.doc.allFields[fieldIndex].value = matches[i];
				}
			}
		}
		body.appendChild(b);
	}
	var table = doc.createElement("table");
	for (i in paramList) {
		var param = paramList[i];
		if (param.length == 0) { // this allow defining an empty parameter. by use of a "pseudo field".
			doc.allFields.push({value:""});
			continue;
		}
		var row = table.insertRow(-1);
		var cell = row.insertCell(-1);
		cell.innerHTML = param;
		cell.style.maxWidth = "12em";
		var field = doc.createElement("input");
		field.type = "text";
		field.maxLength = 120;
		field.style.width = "20em";
		cell = row.insertCell(-1);
		cell.appendChild(field);
		doc.allFields.push(field);
	}
	form.appendChild(table);
	body.appendChild(form);
	body.appendChild(doc.createElement("p"));
	var refCheckBox = doc.createElement('input');
	refCheckBox.type = "checkbox";
	var label = doc.createElement('label');
	label.htmlFor = refCheckBox;
	label.innerHTML =  "מיקום התבנית בתוך הערת שוליים";
	body.appendChild(label);
	body.appendChild(refCheckBox);
	body.appendChild(doc.createElement("p"));
	var button = doc.createElement("input");
	button.type = "button";
	button.value = "אישור";
	button.father = this;
	button.doc = doc;
	button.popup = popup;
	button.refCheckBox = refCheckBox;
	button.onclick = function() {
		this.father.createAndInsertLinkTemplate(this.doc.templateName, this.doc.allFields, this.refCheckBox.checked); 
		this.popup.close();
	}
	body.appendChild(button);
	var button = doc.createElement("input");
	button.type = "button";
	button.value = "ביטול";
	button.popup = popup;
	button.onclick = function() {
		this.popup.close();
	}
	body.appendChild(button);
}	

function createLinkTemplatesSelections() {
	var select = document.createElement("select");
	select.id = "linkTemplatesList";
	select.onchange = function() {
		fireLinkTemplatePopup(this.value);
		this.selectedIndex = 0;
		return false;
	}
	var fullList = knownLinkTemplates();
	var names = [], hnames = [];
	var re = /[a-zA-Z]/;
	for (x in fullList)
		if (re.test(x.charAt(0)))
			names.push(x);
		else
			hnames.push(x);
	names.sort(); hnames.sort();
	var allnames = hnames.concat(names);
	select.options.add(new Option("בחרו קישור להוספה", ""));
	for (var i = 0; i < allnames.length; i++)
		select.options.add(new Option(allnames[i], allnames[i]));
	var toolbar = document.getElementById("toolbar");
	if (toolbar) 
		toolbar.appendChild(select);
}

function templateRegex(templateName) {
	var regexes = {
		nrg: {regex: /http:\/\/(?:www\.){0,1}nrg\.co\.il\/online\/([^\/]+)\/ART([^\/]+)\/([^\.]+).html/i, params:[6,7,3]},
	}
	return regexes[templateName];
}

function fireLinkTemplatePopup(templateName) {
	var linkTemplates = knownLinkTemplates();
	var templateParams = linkTemplates[templateName];
	if (templateParams && templateParams.constructor == Array && templateParams.length > 0)
		popupPredefinedLinkTemplate(templateName, templateParams, templateRegex(templateName));
}
	
if (wgAction == 'edit')
	addOnloadHook(createLinkTemplatesSelections);

function knownLinkTemplates() {
	var constants = [
		"",
		"שם המחבר",
		"כותרת  המאמר",
		"מספר (כפי שמופע בקישור)",
		"מידע נוסף (למשל תאריך)",
		"מספר הסדרה", 
		 "קידומת",
		"מידע נוסף (לא תאריך)",
		"ספר",
		"פרק",
		"פסוק",
		"מסכת",
		"דף",
		"עמוד",
		"משנה",
		"סימן",
		"סעיף",
		"הלכות",
		"שם",
		"מספר הסדרה",
		'מספר סדרת "ART"',
	];
	
	var templatesDic = {
		"ynet": [1,2,3,4,5,0,],
		"הארץ": [1, 2, 3, 4],
		"דבר": [1,2,"תאריך כתבה, בצורה 1949/07/25 (כי שמופיע ב-URL של הכתבה)",3,6,"סוג הפרסום. אם ה-URL כולל 'EntityId=Ad' או 'EntityId=Pc' למלא 'Ad' או 'Pc'"],
		'תנ"ך': [8,9,10],
		"nrg": [1,2,3,4,0,19,20],
		"Mooma": [18],
	};
	for (var key in templatesDic)
		for (var i in templatesDic[key])
			if (typeof(templatesDic[key][i]) == "number")
				templatesDic[key][i] = constants[templatesDic[key][i]];
	return templatesDic;
}