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

Content deleted Content added
Copy button
Compact & Copy button, etc.; not decoding the url
Line 1:
// v. 20172018-1108-1023; en.wikipedia.org/wiki/User:V111P/js/WebRef
window.webRef = window.webRef || {}; // object used to communicate with webRefSetup
window.webRef.getRef = (function () {
Line 38:
work: 'work',
date: 'date',
accessDate: 'accessdateaccess-date',
url: 'url',
publisher: 'publisher',
Line 46:
last: 'last',
first: 'first',
coauthors: 'coauthors', // not used if (coauthorsNumParams == true); if not set, only "author" is used
authorWikiArticle: 'authorlink',
coauthorsNumParams: true, // last2, first2, last3, first3, etc. instead of "coauthors" or only "author"
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: '%YYYY%-%MM%-%DD%', //Leave %D%empty orto %DD%,use %M%default or %MM% or %MMM%,format %YY%(dateFormat or %YYYY%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)
barH: ' |', // ... for Horizontal format
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 ttp = 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
 
Line 64 ⟶ 72:
setupButton: 'Site setup',
hideButton: 'Close',
markButtonselectButton: 'Select',
copyButton: 'Copy',
compactCopyButton: 'Compact & Copy',
compactSelectButton : 'Compact & Select',
copyFailed: ' Copying failed!',
reloadButton: 'Reload',
formatNamesPromptButton: 'Authors',
formatNamesPrompt: 'Enter the author names (separated by commas) to be formatted and inserted into the template:',
singleMultiLineButton: 'SingleHoriz./multi lineVertical',
formatDatePromptButton: 'Date formatter',
formatDatePrompt: 'Enter the date to be formatted and inserted into the template:',
Line 384 ⟶ 394:
 
function formatDateParams(format, useUSFormatIfUnclear) {
var t = templateParams;
var trimCollapse = aux.collapseWhitespace;
var strt = '\\|\\s*';
var end = '\\s*=\\s*([^|}]+*)';
var oldVal = codeTextArea.value;
var val;
 
val = formatDateParam(oldVal, format, ttp.date);
if (val === null) return; // user pressed Cancel
if (ttp.accessDate && ttp.dateFormatRetrieved == '')
val = formatDateParam(val, format, ttp.accessDate, true);
if (val != oldVal) {
codeTextArea.value = val;
Line 406 ⟶ 416:
if (dateInTemplate || noPrompt)
userDate = dateInTemplate;
else {
userDate var promptResponse = trimCollapse((prompt(msgs.formatDatePrompt, dateInTemplate) || ''));
if (promptResponse === null) // user pressed Cancel
return null;
userDate = trimCollapse(promptResponse);
}
 
if (userDate) {
Line 413 ⟶ 427:
formatted = dateFormatter(userDate, format, useUSFormatIfUnclear);
if (formatted) {
var dateParamPospos = templStr.indexOfsearch('| ' + paramName + ' ='dateParamRe) - 1;
if (pos < 0) pos = templStr.indexOf('|');
if (ipos >< 10) pos = 0;
templStr = templStr.replace(dateParamRe, '');
var postemplStr = templStr.slice(dateParamPos0, >pos) 0+ ?'\n' dateParamPos+ :barV + paramName + eqV + formatted + templStr.indexOfslice('|') - 1pos);
templStr = templStr.slice(0, pos) + '\n| ' + paramName + ' = ' + formatted + templStr.slice(pos);
if (monthWarningNeeded)
addWarning(msgs.monthWarning, msgs.monthWarningTitle);
Line 431 ⟶ 446:
function formatNamesPrompt() {
var val = codeTextArea.value;
var t = templateParams;
var names = '';
var authorParamNames = [ttp.author, ttp.coauthors, ttp.first, ttp.last];
for (var n = 1; n <= 9; n++) {
authorParamNames.push(ttp.first + n)
authorParamNames.push(ttp.last + n);
}
 
var strt = '\\|\\s*';
var end = '\\s*=\\s*([^|}]+)';
names = (val.match(strt + ttp.author + end) || ['', ''])[1] + ',';
var trimCollapse = aux.collapseWhitespace;
for (var n = 0; n <= 9; n++)
names += (val.match(strt + ttp.first + (n == 0 ? '' : n) + end) || ['', ''])[1] + ' '
+ trimCollapseaux.collapseWhitespace((val.match(strt + ttp.last + (n == 0 ? '' : n) + end) || ['', ''])[1])
.replace(/ /g, '_') + ',';
if (ttp.coauthors)
names += (val.match(strt + ttp.coauthors + end) || ['', ''])[1];
names = names.replace(/\s+/g, ' ').replace(/\s?,\s?/g, ',')
.replace(/,,+/g, ',').replace(/^,|,$/g, '').replace(/,/g, ', ');
Line 454 ⟶ 468:
var namesNewParams = authorParams(nameFormatter(userAuthors));
if (namesNewParams) {
var authorPospos = val.indexOfsearch(new RegExp('\\| \\s*(' + ttp.last + '1?|' + tp.author + ')\\s*='));
if (authorPospos < 0) pos == -1val.indexOf('|');
authorPosif = val.indexOf('|pos '< +0) t.authorpos += ' =')0;
for (var n = 0; n < authorParamNames.length; n++)
val = val.replace(new RegExp(strt + authorParamNames[n] + end), '');
 
var pos = (val.charAt(authorPos) == '|' ? authorPos : val.indexOf('|')) - 1;
val = val.slice(0, pos) + namesNewParams + val.slice(pos);
codeTextArea.value = val;
Line 489 ⟶ 502:
 
 
function authorParams(arrauthorsArr, additionalNotAnAuthor) {
var t = templateParams;
var str = '';
var nl = '\n| ' + barV;
if (!authorsArr && siteObj.authorName) {
var authors;
if str (!arr= &&nl + tp.author + eqV + siteObj.authorName) {;
str = nl + t.author + ' = ' + siteObj.authorName;
if (siteObj.authorWikiArticle)
str += nl + ttp.authorWikiArticle + ' = 'eqV + siteObj.authorWikiArticle;
}
else if ((authorsauthorsArr = arrauthorsArr || searchFor('author', siteObj.notAnAuthor)) !== null) {
if (additionalNotAnAuthor) { // to remove e.g. "CNN" from "James Smith, CNN"
var co = ''; // coauthors
var i = authorsArr.length;
var last1 = authors[0][0];
else if while (coi--) {
var first1 = authors[0][1];
if (authorsArr[i][0] === additionalNotAnAuthor && !authorsArr[i][1])
if (first1 && (
authors authorsArr.length ==splice(i, 1);
}
|| !(!t.coauthorsNumParams && !t.coauthors)
)) {}
if ( authorsArr.length == 1 && !authorsArr[0][1] ) // only 1 author without a first name
str = nl + t.last + ' = ' + last1;
str += 'nl |+ 'tp.author + t.firsteqV + 'authorsArr[0][0]; = // use the 'author' + first1;param
else { // use the 'last' and 'first' params
for (var i = 10; i < authorsauthorsArr.length; i++) {
var num = (i > 0 || authorsArr.length > 1 ? String(i + 1) : ''); // omit number if only 1 author
if (authorsauthorsArr[i][0]) {
str += nl + ttp.last + 'num =+ 'eqV + last1authorsArr[i][0];
if (authorsauthorsArr[i][1])
str += tp.barVsameLine + tp.first + num + eqV + authorsArr[i][1];
}
}
}
else
str = nl + t.author + ' = ' + last1 + (first1 ? ', ' + first1 : '');
 
 
for (var i = 1; i < authors.length; i++) {
if (t.coauthorsNumParams) {
if (authors[i][0]) {
co += nl + t.last + (i + 1) + ' = ' + authors[i][0];
if (authors[i][1])
co += ' | ' + t.first + (i + 1) + ' = ' + authors[i][1];
}
}
else {
if (i > 1)
co += ', ';
if (authors[i][1])
co += authors[i][1] + ' ';
co += authors[i][0];
}
}
if (t.coauthorsNumParams) {
if (co)
str += co;
}
else if (co) {
if (t.coauthors)
str += nl + t.coauthors + ' = ' + co;
else
str += ', ' + co; // add them to the author param value
}
}
return str;
Line 547 ⟶ 540:
var val = aux.trimStr(codeTextArea.value);
var nowMulti = (val.indexOf('\n') > -1) && !toMulti;
if ( toMulti === false || (!toMulti && nowMulti) ) {
val = val.replace(/\n/g, ' ');
.replace(/\s*\|\s*([^|= ]+)\s*=\s*/g, barH + '$1' + eqH)
else {
// a space after |url= helps with line wrapping :
var t = templateParams;
val = val .replace(/new RegExp('(\\|/g,\\s*' + tp.url + ')\n|\s*=\\s*').replace(/ }}/, '\n}}$1' + tp.urlEqH);
} else {
val = val.replace(/\s*\|\s*([^|= ]+)\s*=\s*/g, '\n' + barV + '$1' + eqV ).replace(/\s*}}/, '\n}}')
.replace( // move the "last" and "first" parameters on one line
new RegExp('(\\|\\s*' + ttp.last + '\\d?\\s*=[^\\n|}]*)\n(\\| ' + ttp.first + '\\d?\\s*=)', 'g'),
'$1 $2'
);
Line 589 ⟶ 584:
var buttons = [
{
id: copyButtonSupported ? 'copyButtoncompactCopyButton' : 'markButtoncompactSelectButton',
onclick: function () {
var taval = domcodeTextArea.byId('codeTA', refFrame.doc)value;
val = val.replace(new RegExp('\\s*\\|\\s*' + tp.quote + '\\s*=\\s*(\\||})'), '$1') // rm empty quote
ta.focus();
.replace(new RegExp('\\s*\\|\\s*' + tp.lang + '\\s*=\\s*(\\||})'), '$1') // rm empty lang
ta.select();
.replace(/\s+}}\s*$/, '}}'); // remove spaces before and after final }}
codeTextArea.value = val;
singleMultiLine(false);
tacodeTextArea.focus();
tacodeTextArea.select();
if (copyButtonSupported) {
var copyFailed = !refFrame.doc.queryCommandEnabled('copy');
Line 612:
},
{
id: (templateParamstp.altDateFormat ? 'altDateFormatButton' : ''),
onclick: function () {
formatDateParams(templateParamstp.altDateFormat, altDateFormatIsUSFormat);
}
},
Line 811:
function dateToString(day, month, year, format) { // month is 0..11
day = +day; month = +month; year = +year;
return ((format || templateParamstp.dateFormat)
.replace(/%DD%/, (day > 9 ? '' : '0') + day)
.replace(/%D%/, day)
Line 828:
today.getMonth(),
today.getFullYear(),
(templateParamstp.dateFormatRetrieved || templateParamstp.dateFormat)
);
}
Line 910:
 
function go() {
var t = templateParams;
var title, date, lang, langParam, quote, selection, work, publisher;
 
Line 945 ⟶ 944:
}
if (!lang || lang != langToIgnore) // add the param only if lang is not langToIgnore
langParam = '| 'barV + ttp.lang + ' = 'eqV + lang + ' \n';
 
quote = '| 'barV + ttp.quote + ' = 'eqV;
selection = getSelectedText();
if (selection) {
Line 962 ⟶ 961:
 
codeTextArea.value = '<ref name="' + makeRefName(title) + '">{{'
+ ttp.templateName
+ '\n| ' + tbarV + tp.title + ' = 'eqV + title
+ (!lang || lang == langToIgnore ? '' : '\n| ' + tbarV + tp.transTitle + ' = 'eqV) // translated title
+ authorParams(null, work)
+ '\n| ' + tbarV + tp.work + ' = 'eqV + work
+ '\n| ' + tbarV + tp.date + ' = 'eqV + date
+ '\n| ' + tbarV + tp.accessDate + ' = 'eqV + getTodaysDateStr()
+ '\n| ' + tbarV + tp.url + ' = 'eqV + decodeURI(___location.href) + '\n'
+ (publisher ? '| 'barV + ttp.publisher + ' = 'eqV + publisher + '\n' : '')
+ langParam
+ quote