Content deleted Content added
mNo edit summary |
YMD date button; removing <ref name="..." |
||
(5 intermediate revisions by the same user not shown) | |||
Line 1:
// v.
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
// Use %D% or %DD%, %M% or %MM% or %MMM%, %YY% or %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 dateFormatDefault
var templateParams = { // LOCALIZE
templateName: 'Cite web',
title: 'title',
transTitle: 'trans-title', // the title translated to English
work: 'work',
date: 'date',
accessDate: '
url: 'url',
publisher: 'publisher',
Line 45 ⟶ 54:
last: 'last',
first: 'first',
coauthors: 'coauthors', // not
authorWikiArticle: 'authorlink',
barV: '| ', // bar and spaces around it (but no newlines) for Vertical format
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 tp = templateParams;
var barV = tp.barV, barH = tp.barH, eqV = tp.eqV, eqH = tp.eqH;
var msgs = webRef.idToText = webRef.msgs = webRef.msgs || webRef.idToText || {};
var msgs_default = { // LOCALIZE
programName: 'WebRef',
monthWarning: 'Check month!',
monthWarningTitle: 'Was the date in the MM/DD/YY(YY) or DD/MM/YY(YY) format?',
quoteUsedWarning: 'Quotation!',
quoteUsedWarningTitle: 'The selected text on the page was used for the value of the
setupButton: 'Site setup',
hideButton: 'Close',
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',
couldntParse: 'Sorry, I couldn\'t parse that.',
Line 83 ⟶ 102:
aboutSiteH: 'About the site',
aboutSiteSectionIntro: 'You can fill this information, but data that is automatically filled doesn\'t need saving in the configuration. Using en as the language code for sites in English will hide the language parameter in the Cite web template. Use en-US if the date is in the MM/DD/YY(YY) format, with the month number instead of its name used in the date.',
siteName_L: 'Site name (can be a wikilink):',
lang_L: 'Language code for the site:',
Line 127 ⟶ 146:
var ___domain = window.webRef.___domain = ___location.href.replace(/[^\/]+\/\/(www\.)?/, '').replace(/\/.*/, '') || '?';
var metaContent;
var siteObj; // = window.webRefSiteData && webRefSiteData[___domain] || get_from_local_storage
var refFrame = {
Line 137 ⟶ 156:
var codeTextArea;
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 306 ⟶ 327:
function dateFormatter(dateStr, format,
var formattedDate = '';
var monthStr = '';
var month;
format = format || dateFormatDefault;
for (var m in monthNameToNum) {
Line 350 ⟶ 372:
year = '19' + year; // 1900+ otherwise :)
if (siteLang == 'en-US' || dateParts[2] > 12) { // American style date
month = dateParts[1];
day = dateParts[2];
}
else {
if (!siteLang || siteLang == 'en')
if (useUSFormatIfUnclear) {
month = dateParts[1];
day = dateParts[2];
}
else {
day = dateParts[1];
month = dateParts[2];
}
}
day = ('0' + day).slice(-2);
Line 370 ⟶ 399:
function formatDateParams(format, useUSFormatIfUnclear) {
var trimCollapse = aux.collapseWhitespace;
var strt = '\\|\\s*';
var end = '\\s*=\\s*([^|}]
var oldVal = codeTextArea.value;
var val = formatDateParam(oldVal, format, tp.date);
if (tp.accessDate && dateFormatRetrieved == '')
val = formatDateParam(val, format, tp.accessDate);
if (val != oldVal) {
codeTextArea.value = val;
Line 386 ⟶ 413:
}
function formatDateParam(templStr, format, paramName
var dateParamRe = new RegExp(strt + paramName + end);
var dateInTemplate = trimCollapse((templStr.match(dateParamRe) || ['', ''])[1]);
if (dateInTemplate)
monthWarningNeeded = false;
var formattedDate = dateFormatter(dateInTemplate, format, useUSFormatIfUnclear);
if (formattedDate) {
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'
if (monthWarningNeeded)
addWarning(msgs.monthWarning, msgs.monthWarningTitle);
}
else
alert(msgs.programName + ':\n' + msgs.couldntParse + '\n' +
}
return templStr;
Line 415 ⟶ 441:
function formatNamesPrompt() {
var val = codeTextArea.value;
var names = '';
var authorParamNames = [
for (var n = 1; n <= 9; n++) {
authorParamNames.push(
authorParamNames.push(
}
var strt = '\\|\\s*';
var end = '\\s*=\\s*([^|}]+)';
names = (val.match(strt +
for (var n = 0; n <= 9; n++)
names += (val.match(strt +
.replace(/ /g, '_') + ','; if (
names += (val.match(strt +
names = names.replace(/\s+/g, ' ').replace(/\s?,\s?/g, ',')
.replace(/,,+/g, ',').replace(/^,|,$/g, '').replace(/,/g, ', ');
Line 438 ⟶ 463:
var namesNewParams = authorParams(nameFormatter(userAuthors));
if (namesNewParams) {
var
if (
for (var n = 0; n < authorParamNames.length; n++)
val = val.replace(new RegExp(strt + authorParamNames[n] + end), '');
val = val.slice(0, pos) + namesNewParams + val.slice(pos);
codeTextArea.value = val;
Line 473 ⟶ 497:
function authorParams(
var str = '';
var nl = '\n
if (!authorsArr && siteObj.authorName) {
if (siteObj.authorWikiArticle)
str += nl +
}
else if ((
if (additionalNotAnAuthor) { // to remove e.g. "CNN" from "James Smith, CNN"
var i = authorsArr.length;
while (i--) {
if (authorsArr[i][0] === additionalNotAnAuthor && !authorsArr[i][1])
}
}
if ( authorsArr.length == 1 && !authorsArr[0][1] ) // only 1 author without a first name
str = nl +
else { // use the 'last' and 'first' params
for (var i =
var num = (i > 0 || authorsArr.length > 1 ? String(i + 1) : ''); // omit number if only 1 author
if (
if (
}
}
}
return str;
Line 531 ⟶ 535:
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)
// a space after |url= helps with line wrapping :
} 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*' +
'$1 $2'
);
Line 565 ⟶ 571:
function createUI() {
var copyButtonSupported;
try {
copyButtonSupported = document.queryCommandSupported && document.queryCommandSupported('copy');
} catch (e) {
copyButtonSupported = false;
}
var buttons = [
{
id: copyButtonSupported ? 'compactCopyButton' : 'compactSelectButton',
onclick: function () {
var
val = val.replace(new RegExp('\\s*\\|\\s*' + tp.quote + '\\s*=\\s*(\\||})'), '$1') // rm empty quote
.replace(new RegExp('\\s*\\|\\s*' + tp.lang + '\\s*=\\s*(\\||})'), '$1') // rm empty lang
.replace(/\s+}}\s*$/, '}}'); // remove spaces before and after final }}
codeTextArea.value = val;
singleMultiLine(false);
codeTextArea.focus();
codeTextArea.select();
if (copyButtonSupported) {
var copyFailed = !refFrame.doc.queryCommandEnabled('copy');
if (!copyFailed) try {
copyFailed = !refFrame.doc.execCommand('copy');
} catch (e) { copyFailed = true; }
if (copyFailed) this.parentNode.insertBefore(dom.textNode(msgs['copyFailed']), this.nextSibling);
}
}
},
Line 579 ⟶ 603:
},
{
id: '
onclick: function () { formatDateParams(dateFormatDMY); }
},
{
id: 'mdy',
onclick: function () { formatDateParams(dateFormatMDY); }
},
{
id: 'ymd',
onclick: function () { formatDateParams(dateFormatYMD); }
},
{
id: 'refName',
onclick: function () {
var val, oldVal = codeTextArea.value;
val = oldVal.replace(/^<ref name="[^"]*"/, '<ref');
if (val == oldVal)
val = val.replace(/^<ref/, '<ref name=""');
codeTextArea.value = val;
}
},
Line 624 ⟶ 660:
br();
var btn;
for (var i = 0; i < buttons.length; i++) {
if (!btn.id)
continue;
docFrag.appendChild(dom.newEl('button', {
onclick: btn.onclick
}));
dom.text(' ', docFrag);
}
dom.text('| ', docFrag);
docFrag.appendChild(dom.newEl('a', {
Line 646 ⟶ 685:
css: {display: 'none'}
}));
docFrag.appendChild(dom.newEl('span', {id: 'warnings'}));
refFrame.frame = dom.byId('ref00ref');
Line 760 ⟶ 796:
if (text && parts.length > 1) {
var
var matched = text.match(parts[1] + '\\s*(.+' + (!ending ? ')' : '?)\\s*' + ending));
if (matched && matched[1])
text = matched[1];
Line 784 ⟶ 821:
function dateToString(day, month, year, format) { // month is 0..11
day = +day; month = +month; year = +year;
return
.replace(/%DD%/, (day > 9 ? '' : '0') + day)
.replace(/%D%/, day)
Line 791 ⟶ 828:
.replace(/%M%/, (month + 1))
.replace(/%YYYY%/, year)
.replace(/%YY%/, year % 100
}
Line 801 ⟶ 838:
today.getMonth(),
today.getFullYear(),
(
);
}
Line 817 ⟶ 854:
}
return siteName;
}
Line 839 ⟶ 871:
return text;
}
Line 862 ⟶ 890:
return text;
}
function addWarning(label, title) {
var warnSpan = dom.byId('warnings', refFrame.doc);
var warnSepar = dom.byId('warningsSeparator', refFrame.doc);
warnSepar.style.display = 'inline';
warnSpan.appendChild(dom.textNode(' '));
warnSpan.appendChild(dom.newEl('span', {
text: label,
title: title,
css: {backgroundColor: 'green'}
}));
}
function clearWarnings() {
var warnSpan = dom.byId('warnings', refFrame.doc);
var warnSepar = dom.byId('warningsSeparator', refFrame.doc);
warnSpan.innerHTML = '';
warnSepar.style.display = 'none';
}
function go() {
var title, date, lang, langParam, quote, selection, work, publisher;
metaContent = window.webRef.metaContent = getMetaContent();
siteObj = window.webRefSiteData && webRefSiteData[___domain] || (function () {
Line 876 ⟶ 927:
return sO || {};
})();
siteLang = siteObj.lang || '';
clearWarnings();
var warnings = [];
title = cleanParam(searchFor('title'));
monthWarningNeeded = false;
date = searchFor('date');
if (monthWarningNeeded) { // set in dateFormatter() if not clear whether the date is MM/DD/YYYY or DD/MM/YYYY
warnings.push([msgs.monthWarning, msgs.monthWarningTitle]);
}
langParam = '';
if (siteLang.search(/^en-US/i) == 0) siteLang = 'en-US';
lang = (siteLang == 'en-US' ? 'en' : siteLang);
if (!lang) {
lang = autoSearchFor('lang') || '';
Line 888 ⟶ 950:
}
if (!lang || lang != langToIgnore) // add the param only if lang is not langToIgnore
langParam =
quote =
selection = getSelectedText();
if (selection) {
quote += cleanParam(selection);
warnings.push([msgs.quoteUsedWarning, msgs.quoteUsedWarningTitle]);
}
for (var i = 0; i < warnings.length; i++) {
addWarning(warnings[i][0], warnings[i][1]);
}
Line 907 ⟶ 966:
publisher = siteObj.publisher || '';
codeTextArea.value = '<ref
+
+ '\n
+ (!lang || lang == langToIgnore ? '' : '\n' + barV + tp.transTitle + eqV) // translated title
+
+ '\n
+ '\n
+ '\n
+
+ (publisher ? barV + tp.publisher + eqV + publisher + '\n' : '')
+ langParam
+ quote
Line 922 ⟶ 982:
document.body.scrollTop = document.documentElement.scrollTop = 0;
}
createUI();
return go;
|