User:V111P/js/webRef.js: Difference between revisions

Content deleted Content added
Compact & Copy button, etc.; not decoding the url
YMD date button; removing <ref name="..."
 
Line 1:
// v. 20182021-0809-2301; en.wikipedia.org/wiki/User:V111P/js/WebRef
window.webRef = window.webRef || {}; // object used to communicate with webRefSetup
window.webRef.getRef = (function () {
Line 31:
// these words are used when spliting names in the case of multipe article authors
var andWords = ['and']; // LOCALIZE by adding the word(s) for "and" in the local lang
 
altDateFormat: '%MMM% %D%, %YYYY%', // Use %D% or %DD%, %M% or %MM% or %MMM%, %YY% or %YYYY%.
dateFormat: '%D% %MMM% %YYYY%', // %M% -> Feb is 2; %MM% -> Feb is 02; %MMM% -> Feb is monthNumToName[1]
var dateFormatMDY = '%MMM% %D%, %YYYY%';
var dateFormatDMY = '%D% %MMM% %YYYY%';
var dateFormatYMD = '%YYYY%-%MM%-%DD%';
var dateFormatDefault = dateFormatDMY;
var dateFormatRetrieved: = '',; // Leave empty to use default format (dateFormat or altDateFormat)dateFormatDefault
 
var templateParams = { // LOCALIZE
Line 48 ⟶ 56:
coauthors: 'coauthors', // not used
authorWikiArticle: 'authorlink',
dateFormat: '%D% %MMM% %YYYY%', // %M% -> Feb is 2; %MM% -> Feb is 02; %MMM% -> Feb is monthNumToName[1]
altDateFormat: '%MMM% %D%, %YYYY%', // %D% or %DD%, %M% or %MM% or %MMM%, %YY% or %YYYY%.
dateFormatRetrieved: '', //Leave empty to use default format (dateFormat or altDateFormat)
barV: '| ', // bar and spaces around it (but no newlines) for Vertical format
barVsameLine: ' | ', // (e.g. first name is on the same line as last name even in vertical mode)
Line 56 ⟶ 61:
eqV: ' = ', // equal sign and spaces around it (but no newline) for Vertical format
eqH: '=', // ... for Horizontal format
urlEqH: '= ' // a space before the URL helps with line wrapping
};
var tp = templateParams;
var barV = tp.barV, barH = tp.barH, eqV = tp.eqV, eqH = tp.eqH;
 
var altDateFormatIsUSFormat = true; // LOCALIZE. True in the case of the English Wikipedia
 
var msgs = webRef.idToText = webRef.msgs = webRef.msgs || webRef.idToText || {};
Line 75 ⟶ 78:
copyButton: 'Copy',
compactCopyButton: 'Compact & Copy',
compactSelectButton : 'Compact & Select',
copyFailed: ' Copying failed!',
reloadButton: 'Reload',
formatNamesPromptButton: 'Authors',
formatNamesPrompt: 'Enter the author names (separated by commas if more than one author) to be formatted and inserted into the template:',
dmy: 'DMY',
mdy: 'MDY',
ymd: 'YMD',
refName: 'RefName',
singleMultiLineButton: 'Horiz./Vertical',
formatDatePromptButton: 'Date formatter',
formatDatePrompt: 'Enter the date to be formatted and inserted into the template:',
altDateFormatButton: 'U.S. date',
couldntParse: 'Sorry, I couldn\'t parse that.',
 
Line 154 ⟶ 158:
var siteLang; // used for date formatting if equals en-US and month is represented as a number
var monthWarningNeeded; // set in dateFormatter if not clear if date is in U.S. format or not
var siteDateIsUS = false;
 
var refDocData = window.webRef.refDocData = {
Line 326 ⟶ 331:
var monthStr = '';
var month;
format = format || dateFormatDefault;
 
for (var m in monthNameToNum) {
Line 398 ⟶ 404:
var end = '\\s*=\\s*([^|}]*)';
var oldVal = codeTextArea.value;
var val = formatDateParam(oldVal, format, tp.date);
var val;
 
if (tp.accessDate && tp.dateFormatRetrieved == '')
val = formatDateParam(oldVal, format, tp.date);
val = formatDateParam(val, format, tp.accessDate, true);
if (val === null) return; // user pressed Cancel
if (tp.accessDate && tp.dateFormatRetrieved == '')
val = formatDateParam(val, format, tp.accessDate, true);
if (val != oldVal) {
codeTextArea.value = val;
Line 409 ⟶ 413:
}
 
function formatDateParam(templStr, format, paramName, noPrompt) {
var dateParamRe = new RegExp(strt + paramName + end);
var dateInTemplate = trimCollapse((templStr.match(dateParamRe) || ['', ''])[1]);
var userDate, formatted;
 
if (dateInTemplate) || noPrompt){
userDate = dateInTemplate;
else {
var promptResponse = prompt(msgs.formatDatePrompt, dateInTemplate);
if (promptResponse === null) // user pressed Cancel
return null;
userDate = trimCollapse(promptResponse);
}
 
if (userDate) {
monthWarningNeeded = false;
formattedvar formattedDate = dateFormatter(userDatedateInTemplate, format, useUSFormatIfUnclear);
if (formattedformattedDate) {
var pos = templStr.search(dateParamRe);
if (pos < 0) pos = templStr.indexOf('|');
if (pos < 0) pos = 0;
templStr = templStr.replace(dateParamRe, '');
templStr = templStr.slice(0, pos) + '\n'
+ barV + paramName + eqV + formattedformattedDate + templStr.slice(pos);
if (monthWarningNeeded)
addWarning(msgs.monthWarning, msgs.monthWarningTitle);
}
else
alert(msgs.programName + ':\n' + msgs.couldntParse + '\n' + userDatedateInTemplate);
}
return templStr;
Line 521 ⟶ 516:
str = nl + tp.author + eqV + authorsArr[0][0]; // use the 'author' param
else { // use the 'last' and 'first' params
for (var i = 0; i < authorsArr.length; i++) {
var num = (i > 0 || authorsArr.length > 1 ? String(i + 1) : ''); // omit number if only 1 author
if (authorsArr[i][0]) {
str += nl + tp.last + num + eqV + authorsArr[i][0];
if (authorsArr[i][1])
str += tp.barVsameLine + tp.first + num + eqV + authorsArr[i][1];
}
}
}
Line 608 ⟶ 603:
},
{
id: 'formatDatePromptButtondmy',
onclick: function () { formatDateParams(dateFormatDMY); }
},
{
id: 'mdy',
id: (tp.altDateFormat ? 'altDateFormatButton' : ''),
onclick: function () { formatDateParams(dateFormatMDY); }
},
else {
id: 'ymd',
onclick: function () { formatDateParams(dateFormatYMD); }
},
} {
id: 'refName',
onclick: function () {
var val, oldVal = codeTextArea.value;
formatDateParams(tp.altDateFormat, altDateFormatIsUSFormat);
val = oldVal.replace(/^<ref name="[^"]*"/, '<ref');
if (val == oldVal)
val = val.replace(/^<ref/, '<ref name=""');
codeTextArea.value = val;
}
},
Line 653 ⟶ 660:
br();
 
var valbtn;
for (var i = 0; i < buttons.length; i++) {
ifbtn = (buttons[i].id) {;
if (userDate!btn.id) {
docFrag.appendChild(dom.newEl('button', {
continue;
text: msgs[buttons[i].id],
onclick: buttons[i].onclick
}));
dom.text(' ', docFrag);
}
}
 
docFrag.appendChild(dom.newEl('button', {
text: msgs[buttons[i]btn.id],
onclick: buttons[i]btn.onclick
}));
 
dom.text(' ', docFrag);
}
dom.text('| ', docFrag);
docFrag.appendChild(dom.newEl('a', {
Line 811 ⟶ 821:
function dateToString(day, month, year, format) { // month is 0..11
day = +day; month = +month; year = +year;
return ((format || tp.dateFormat)
.replace(/%DD%/, (day > 9 ? '' : '0') + day)
.replace(/%D%/, day)
Line 818 ⟶ 828:
.replace(/%M%/, (month + 1))
.replace(/%YYYY%/, year)
.replace(/%YY%/, year % 100));
}
 
Line 828 ⟶ 838:
today.getMonth(),
today.getFullYear(),
(tp.dateFormatRetrieved || tp.dateFormatdateFormatDefault)
);
}
Line 844 ⟶ 854:
}
return siteName;
}
 
 
function makeRefName(title) {
return ___domain.slice(0, 4) + '_' + title.replace(/\s+/g, '').slice(0, 4);
}
 
Line 928 ⟶ 933:
 
title = cleanParam(searchFor('title'));
 
monthWarningNeeded = false;
date = searchFor('date');
Line 960 ⟶ 966:
publisher = siteObj.publisher || '';
 
codeTextArea.value = '<ref name="' + makeRefName(title) + '">{{'
+ tp.templateName
+ '\n' + barV + tp.title + eqV + title