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

Content deleted Content added
Undid revision 356075499 by PleaseStand (talk) - breaks the script
various changes, including fix for loss of changes when back button used
Line 83:
 
// Begin encapsulation (prevent interference with other scripts)
(function SegregateRefsJs(){
 
// Semi-global variables (private to this script)
var editForm, refsDiv, refsH2, mainTextbox, refsTextbox, randPrefix, messages,
refsButton, convertButton, complete, unloadHandlerRegistered = false;
 
// Define a (very important) function that is better than hasOwnProperty
function has(obj, key) {
return Object.hasOwnProperty.call(obj, key);
}
 
// Extend the string object with new methods
Line 135 ⟶ 140:
// Returns the next ref found in the text
getRef: function getRef() {
var results = [0,0];
}do {
while(results[1].toString().toLowerCase() != "ref") {
results = this.refScanRegex.exec(this.wikiText);
if(!results) {
Line 144 ⟶ 149:
results = [0,0];
}
} while(results[1].toString().toLowerCase() != "ref") {;
}
return results[0];
}
Line 168 ⟶ 173:
return null;
}
var attributes = {}, results;
while(true(results = attParseRegex.exec(this.parsedRef[1]))) {
var results = attParseRegex.exec(this.parsedRef[1]);
if(!results) {
break;
}
attributes[results[1].toLowerCase()] = results[2].PsHTMLUnquote();
}
Line 218 ⟶ 219:
// First make sure that the ref is in the right group.
refGroup = attributes.hasOwnPropertyhas(attributes, "group") ? attributes.group : "";
if(group != refGroup) {
continue;
Line 227 ⟶ 228:
// an acceptable ref name to the MediaWiki parser, as verified by
// informal testing.)
if(!attributes.hasOwnPropertyhas(attributes, "name")) {
// Bad: it doesn't have one - create a name for it
refStored = false;
Line 240 ⟶ 241:
refLong = ref;
// Check if the ref name has already been seen.
refStored = refNames.hasOwnPropertyhas(refNames, refName);
// Check if the ref has no contents.
refEmpty = (parser.parsedRef[2].slice(-2) == "/>") ||
Line 328 ⟶ 329:
// First, we build an associative array of all the ref codes
// that we might need to put back into the text.
}
// NOTE: JavaScript does not actually offer real associative array
// functionality, so we emulate it using objects and the has() function
// we defined earlier that is used to verify a property's existence.
scanner = new RefScanner(argRefText);
while((ref = scanner.getRef())) {
parser = new RefParser(ref);
attributes = parser.getAttributes();
if(attributes.hasOwnPropertyhas(attributes, "name")) {
// Only use the first ref with each name
if(!refCodes.hasOwnProperty(attributes.name)) {
Line 347 ⟶ 352:
parser = new RefParser(ref);
attributes = parser.getAttributes();
if(attributes.hasOwnPropertyhas(attributes, "name")) {
if(!usageFreq.hasOwnPropertyhas(usageFreq, attributes.name)) {
// We found a new name
usageFreq[attributes.name] = 1;
Line 370 ⟶ 375:
parser = new RefParser(ref);
attributes = parser.getAttributes();
if(attributes.hasOwnPropertyhas(attributes, "name")) {
// Is this name on the replacement list?
if(refCodes.hasOwnPropertyhas(refCodes, attributes.name)) {
// If we are using caseCues, and another ___location is
// preferred, skip to the next ref.
if(caseCues && preferredRef.hasOwnPropertyhas(preferredRef, attributes.name) &&
ref.slice(0, 4) != "<REF") {
continue;
Line 424 ⟶ 429:
}
 
function submitHandlerunloadHandler(evt) {
// Local variables
var result, refName, unusedRefNamesQuoted = [];
}
// wikEd compatibility (frame -> textarea)
if(typeof editForm.onsubmitwikEdUseWikEd != "undefined" && wikEdUseWikEd) {
WikEdUpdateTextarea();
}
// Do the actual integration work
result = integrateRefs(mainTextbox.value, refsTextbox.value,
randPrefix, complete);
// Find all unused ref names
for(refName in result.unusedRefs) {
if(has(result.unusedRefs.hasOwnProperty(, refName)) {
unusedRefNamesQuoted.push(refName.PsHTMLQuote());
}
Line 443 ⟶ 454:
unusedRefNamesQuoted.join(", "))) {
// Don't submit form
evt.preventDefault();
return false;
}
Line 449 ⟶ 461:
// Otherwise, update the textbox.
mainTextbox.value = result.wikiText;
// wikEd compatibility (textarea -> frame)
if(typeof wikEdUseWikEd != "undefined" && wikEdUseWikEd) {
WikEdUpdateFrame();
}
// Deactivate this event handler
window.removeEventListener("submit", unloadHandler, false);
window.removeEventListener("unload", unloadHandler, false);
unloadHandlerRegistered = false;
// We can delete the header and refs textbox now
Line 454 ⟶ 476:
refsDiv.removeChild(refsTextbox);
// Deactivate this submit handler and run any previously registered handler
editForm.onsubmit = null;
if(typeof editForm.PsOnsubmit == "function") {
editForm.onsubmit = editForm.PsOnsubmit;
editForm.onsubmit.apply(this, arguments);
}
return true;
}
Line 524 ⟶ 540:
// Set up the submit handler (to integrate refs when done editing)
window.addEventListener("submit", unloadHandler, false);
if(typeof editForm.onsubmit != "undefined") {
window.addEventListener("unload", unloadHandler, false);
editForm.PsOnsubmit = editForm.onsubmit;
unloadHandlerRegistered = true;
}
editForm.onsubmit = submitHandler;
// Don't submit form
Line 606 ⟶ 621:
 
function getEditboxContents() { // ajaxPreview compatibility
if(editForm.onsubmit == submitHandlerunloadHandlerRegistered) {
// wikEd compatibility (frame -> textarea)
if(typeof wikEdUseWikEd != "undefined" && wikEdUseWikEd) {
WikEdUpdateTextarea();
}
return integrateRefs(mainTextbox.value, refsTextbox.value,
randPrefix, complete).wikiText;
Line 675 ⟶ 694:
addOnloadHook(loadHandler);
 
})();
} SegregateRefsJs();
 
//</nowiki></pre>