User:PleaseStand/segregate-refs-dev.js: Difference between revisions

Content deleted Content added
m scanner.slice is not a function
add refsDiv directly under #editform, not within .wikiEditor-ui (fixes overlap issue)
 
(8 intermediate revisions by the same user not shown)
Line 29:
/*global window, addOnloadHook, SegregateRefsJsL10n, SegregateRefsJsAllowConversion,
wikEdUseWikEd, WikEdUpdateTextarea, WikEdUpdateFrame*/
 
// <nowiki>
 
// Translate the right-hand side of these if necessary.
Line 44 ⟶ 46:
refsHeader: "Inline footnotes",
convertHeader: "Generated refs list",
refsCommentIncomplete: "<!-- This list of footnotes may be incomplete. Do not use for conversion purposes. -->\n\n",
refsCommentComplete: "<!-- Converted to LDR format\n using [[User:PleaseStand/References segregator]] -->\n\n",
convertSummary: "Converted footnotes to LDR format (using [[User:PleaseStand/References segregator|segregate-refs]])",
Line 54 ⟶ 55:
 
var editForm, refsDiv, refsH2, mainTextbox, refsTextbox, randPrefix, messages,
refsButton, convertButton, complete, unloadHandlerRegistered = false;
 
function has( obj, key ) {
return Object.prototype.hasOwnProperty.call( obj, key );
}
 
/**
Line 131 ⟶ 128:
return null;
}
var attributes = {}Object.create( null ), results;
while ( ( results = attParseRegex.exec( this.parsedRef[1] ) ) ) {
attributes[results[1].toLowerCase()] = htmlUnquote( results[2] );
Line 149 ⟶ 146:
var prefixChars, randNo, randPrefix, refPreferred, scanner, ref, parser, attributes,
refGroup, refName, refStored, refEmpty, refLong, unnamedRefs = 0,
refNames = {}Object.create( null ), refCodes = [], refShort, outWikiText = '', offset = 0;
 
// Create a random prefix for autogenerated ref names
Line 177 ⟶ 174:
// The ref already has a name (possibly the empty string)
refName = attributes.name;
refStored = has(refName in refNames, refName );
refEmpty = parser.parsedRef[2].slice( -2 ) == '/>' ||
parser.parsedRef[2].slice( 0, 3 ) == '></';
Line 258 ⟶ 255:
}
 
var scanner, ref, parser, attributes, refCodes = {}Object.create( null ), usageFreq = {}Object.create( null ),
preferredRef = {}Object.create( null ), refLong, outWikiText = '', offset = 0;
 
// First, we build an associative array of all the ref codes
Line 269 ⟶ 266:
if ( attributes.name !== undefined ) {
// Only use the first ref having each name
if ( !refCodes.hasOwnProperty( attributes.name in refCodes ) ) {
refCodes[attributes.name] = ref;
}
Line 283 ⟶ 280:
attributes = parser.getAttributes();
if ( attributes.name !== undefined ) {
if ( !has( usageFreq, attributes.name in usageFreq ) ) {
usageFreq[attributes.name] = 1;
} else {
Line 301 ⟶ 298:
parser = new RefParser( ref );
attributes = parser.getAttributes();
if ( has(attributes, 'name' )in attributes ) {
// Is this name on the replacement list?
if ( has( refCodes, attributes.name )in refCodes ) {
 
// If we are using caseCues, and another ___location is
// preferred, skip to the next ref.
if ( caseCues && has( preferredRef, attributes.name )in preferredRef &&
ref.slice( 0, 4 ) != '<REF'
) {
Line 373 ⟶ 370:
// Do the actual integration work
result = integrateRefs(mainTextbox.value, refsTextbox.value, randPrefix, completetrue);
 
// Find all unused ref names
for(refName in result.unusedRefs) {
unusedRefNamesQuoted.push(htmlQuote(refName));
if(has(result.unusedRefs, refName)) {
unusedRefNamesQuoted.push(htmlQuote(refName));
}
}
// If any refs are unused, warn and allow the user to cancel;
Line 428 ⟶ 423:
// Do the actual segregation work and save the random prefix
var segFormat = segregateRefs(mainTextbox.value, "", completetrue);
if(!segFormat) {
return false;
Line 460 ⟶ 455:
refsTextbox = document.createElement("textarea");
refsTextbox.id = "PsRefsTextbox";
refsTextbox.value = segFormat.refCodes.join("\n\n");
if(!complete) {
refsTextbox.valuerows = messages.refsCommentIncomplete +12;
segFormat.refCodes.join("\n\n");
} else {
refsTextbox.value = segFormat.refCodes.join("\n\n");
}
refsTextbox.rows = Math.floor(mainTextbox.rows / 2);
refsTextbox.cols = mainTextbox.cols;
refsTextbox.style.border = "none";
Line 548 ⟶ 537:
refsTextbox.value = messages.refsCommentComplete +
segFormat.refCodes.join("\n");
refsTextbox.rows = Math.floor(mainTextbox.rows / 2)12;
refsTextbox.cols = mainTextbox.cols;
 
refsTextbox.style.border = "none";
Line 575 ⟶ 563:
return false;
}
 
function getEditboxContents() { // ajaxPreview compatibility
if(unloadHandlerRegistered) {
// wikEd compatibility (frame -> textarea)
if(typeof wikEdUseWikEd != "undefined" && wikEdUseWikEd) {
WikEdUpdateTextarea();
}
return integrateRefs(mainTextbox.value, refsTextbox.value,
randPrefix, complete).wikiText;
} else {
return mainTextbox.value;
}
}
 
// Leave a global for ajaxPreview to use.
window.getEditboxContents = getEditboxContents;
 
 
Line 597 ⟶ 569:
try {
 
if( !window.addEventListener ) {
return;
}
// Handle message translations
messages = (typeof SegregateRefsJsL10n == "object" &&
Line 641 ⟶ 609:
refsDiv.appendChild(refsButton);
refsDiv.appendChild(convertButton);
// Find position within the edit form to insert it at
mainTextbox.parentNode.insertBefore(refsDiv, mainTextbox.nextSibling);
var refsDivPos = mainTextbox, refsDivPosParent = refsDivPos.parentNode;
while (refsDivPosParent !== editForm) {
refsDivPos = refsDivPosParent;
refsDivPosParent = refsDivPos.parentNode;
if (!refsDivPosParent) {
refsTextbox.cols refsDivPos = mainTextbox.cols;
refsDivPosParent = refsDivPos.parentNode;
break;
}
}
refsDivPos = refsDivPos.nextSibling;
if (refsDivPos && refsDivPos.classList.contains("wikiEditor-ui-clear")) {
refsDivPos = refsDivPos.nextSibling;
}
refsDivPosParent.insertBefore(refsDiv, refsDivPos);
} catch(e) {
}
Line 648 ⟶ 631:
 
})( jQuery );
 
// </nowiki>