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

Content deleted Content added
remove refsCommentIncomplete message (follows-up #606561948)
add refsDiv directly under #editform, not within .wikiEditor-ui (fixes overlap issue)
 
(6 intermediate revisions by the same user not shown)
Line 24:
distribute this software. If you modify the software and do not wish to
license your changes under one or more of the licenses, please remove
the license(s) from the list above.s
*/
 
/*global window, addOnloadHook, SegregateRefsJsL10n, SegregateRefsJsAllowConversion,
wikEdUseWikEd, WikEdUpdateTextarea, WikEdUpdateFrame*/
 
// <nowiki>
 
// Translate the right-hand side of these if necessary.
Line 54 ⟶ 56:
var editForm, refsDiv, refsH2, mainTextbox, refsTextbox, randPrefix, messages,
refsButton, convertButton, unloadHandlerRegistered = false;
 
function has( obj, key ) {
return Object.prototype.hasOwnProperty.call( obj, key );
}
 
/**
Line 130 ⟶ 128:
return null;
}
var attributes = {}Object.create( null ), results;
while ( ( results = attParseRegex.exec( this.parsedRef[1] ) ) ) {
attributes[results[1].toLowerCase()] = htmlUnquote( results[2] );
Line 148 ⟶ 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 176 ⟶ 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 257 ⟶ 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 268 ⟶ 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 282 ⟶ 280:
attributes = parser.getAttributes();
if ( attributes.name !== undefined ) {
if ( !has( usageFreq, attributes.name in usageFreq ) ) {
usageFreq[attributes.name] = 1;
} else {
Line 300 ⟶ 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 376 ⟶ 374:
// 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 460 ⟶ 456:
refsTextbox.id = "PsRefsTextbox";
refsTextbox.value = segFormat.refCodes.join("\n\n");
refsTextbox.rows = Math.floor(mainTextbox.rows / 2)12;
refsTextbox.cols = mainTextbox.cols;
refsTextbox.style.border = "none";
Line 542 ⟶ 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 569 ⟶ 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, true).wikiText;
} else {
return mainTextbox.value;
}
}
 
// Leave a global for ajaxPreview to use.
window.getEditboxContents = getEditboxContents;
 
 
Line 590 ⟶ 569:
try {
 
if( !window.addEventListener ) {
return;
}
// Handle message translations
messages = (typeof SegregateRefsJsL10n == "object" &&
Line 634 ⟶ 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 641 ⟶ 631:
 
})( jQuery );
 
// </nowiki>