Jump to content

User:Alexis Jazz/Factotum/modules/AltRefModule.js

From Wikipedia, the free encyclopedia
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
//AltRefModule provides an alternative reflist for Factotum. See https://en.wikipedia.org/wiki/User:Alexis_Jazz/Factotum/Modules#Alternative_reference_selector
//AltRefModule is public domain, irrevocably released as WTFPL Version 2[www.wtfpl.net/about/] by its author, Alexis Jazz.
if ( typeof window.FTTModules == 'undefined' ) { window.FTTModules=[]; }
FTTModules.push({'load':['afterPreload'],'afterPreloadFunc':
	function(int,rInt,errInt){
		FTT.MD.secRefL = {};
		if ( ! FTT.MD.secRefL.CSS ) {
			mw.util.addCSS('#HLSecRefDropDownL>span a{box-shadow:inset 0 0 0 0.15em rgba(0,0,255,0.5)}.HLRefDropDown .oo-ui-menuSelectWidget{z-index:20}');
			FTT.MD.secRefL.CSS = 1;
		}
		FTT.MD.secRefL.refs = FTT.UITextInput.getValue().match(/<ref( name="[^"]+")?>(([^<]|<(?!\/ref>))+)<\/ref>/g);
		if ( ! FTT.MD.secRefL.refs ) {
			return;
		}
		if ( ! FTT.referenceArray ) {
			$('#mw-content-text ol.references').append($('#mw-content-text ol.references>li style')[0]); //sometimes style gets included in innerText but not always? seems flaky, moving style element out of the way
			FTT.referenceArray = Array.from($('#mw-content-text ol.references>li'));
		}
		FTT.MD.secRefL.HLRefDropDownOpts = [];
			for (rInt=0;rInt<FTT.MD.secRefL.refs.length;rInt++) {
			FTT.MD.secRefL.refArrLabel = FTT.MD.secRefL.refs[rInt].replace(/<ref( name="[^"]+")?>(([^<]|<(?!\/ref>))+)<\/ref>/,'$2');
			FTT.MD.secRefL.refArrName = FTT.MD.secRefL.refs[rInt].match(/<ref( name="([^"]+)")?>/);
			FTT.MD.secRefL.url = FTT.MD.secRefL.refArrLabel.match(/http[s]?:\/\/[^ \|<>]+/);
			FTT.MD.secRefL.longNum = FTT.MD.secRefL.refArrLabel.match(/[0-9]{7,}/);
			FTT.MD.secRefL.refArrLabel = FTT.escapeHTML(FTT.MD.secRefL.refArrLabel);
			FTT.MD.secRefL.gotEl = 0;
			try{
				FTT.MD.secRefL.nameEl = document.getElementById('cite_note-'+FTT.MD.secRefL.refArrName[2].replace(/ /g,'_')+'-1');
				FTT.MD.secRefL.refArrLabel = FTT.escapeHTML(FTT.MD.secRefL.nameEl.querySelectorAll('.reference-text')[0].innerText.split(/\n/)[0]);
				FTT.MD.secRefL.gotEl=1;
			} catch (e) {}
			if ( ! FTT.MD.secRefL.gotEl ) {
				for (int=0;int<FTT.referenceArray.length;int++) {
					if ( ( FTT.MD.secRefL.url && FTT.referenceArray[int].innerHTML.match(FTT.MD.secRefL.url[0]) ) || ( FTT.MD.secRefL.longNum && FTT.referenceArray[int].innerText.match(FTT.MD.secRefL.longNum[0]) ) ) {
						try{
							FTT.MD.secRefL.refArrLabel = FTT.escapeHTML(FTT.referenceArray[int].querySelectorAll('.reference-text')[0].innerText.split(/\n/)[0]);
							FTT.MD.secRefL.refArrLabelErr = '';
							for ( errInt=0;errInt<FTT.referenceArray[int].querySelectorAll('.cs1-visible-error').length;errInt++) {
								FTT.MD.secRefL.refArrLabelErr = FTT.MD.secRefL.refArrLabelErr + FTT.referenceArray[int].querySelectorAll('.cs1-visible-error')[errInt].innerText;
							}
							FTT.MD.secRefL.refArrLabel = '<span class="FTTRedBG">'+FTT.escapeHTML(FTT.MD.secRefL.refArrLabelErr) + '</span>' + FTT.MD.secRefL.refArrLabel;
						} catch (e) {}
					}
				}
			}
			if ( FTT.MD.secRefL.refArrLabel.length > 300 ) {
				FTT.MD.secRefL.refArrLabel = FTT.MD.secRefL.refArrLabel.slice(0,295)+'…';
			}
			if ( FTT.MD.secRefL.refArrLabel != '' ) {
				FTT.MD.secRefL.HLRefDropDownOpts.push({data:rInt,label:new OO.ui.HtmlSnippet(FTT.MD.secRefL.refArrLabel)});
			}
		}
		FTT.MD.secRefL.HLRefDropDown = new OO.ui.DropdownInputWidget( {
			id: 'HLSecRefDropDown',
			classes: ['HLRefDropDown'],
			options: FTT.MD.secRefL.HLRefDropDownOpts,
		});
		FTT.MD.secRefL.selectRef = function() {
			FTT.MD.secRefL.refIndexStart = FTT.UITextInput.getValue().split(FTT.MD.secRefL.refs[FTT.MD.secRefL.HLRefDropDown.getValue()])[0].length;
			FTT.MD.secRefL.refIndexEnd = FTT.MD.secRefL.refIndexStart + FTT.MD.secRefL.refs[FTT.MD.secRefL.HLRefDropDown.getValue()].length;
			FTT.HLSelectRef(FTT.MD.secRefL.refIndexStart,FTT.MD.secRefL.refIndexEnd);
		};
		FTT.MD.secRefL.HLRefDropDown.on('change',function() {
			FTT.MD.secRefL.selectRef();
		});
		FTT.MD.secRefL.HLRefDropDownGo = new OO.ui.ButtonWidget( {
			label:FTT.basicmsgs.go,
		});
		FTT.MD.secRefL.HLRefDropDownGo.on('click',function(){
			FTT.MD.secRefL.selectRef();
		});
		FTT.MD.secRefL.HLRefDropDownL = new OO.ui.HorizontalLayout({id:'HLSecRefDropDownL',classes:['HLRefDropDownL'],items:[FTT.MD.secRefL.HLRefDropDown,FTT.MD.secRefL.HLRefDropDownGo]});
		if ( ! FTT.MD.secRefL.HLRefDropDownL.isElementAttached() ) {
			$('#FTTTextAndPreview').append(FTT.MD.secRefL.HLRefDropDownL.$element);
			FTT.insertAfter($('#FTTTextAndPreview')[0],$('#HLSecRefDropDownL')[0]);
		}
	}
});