Content deleted Content added
-1.26 |
-4.3 |
||
(24 intermediate revisions by the same user not shown) | |||
Line 1:
/// User:PerfektesChaos/js/citoidWikitext/core.js
// Execute core functionality for citoid on wikitext source editing
///
// ResourceLoader: compatible;
// dependencies: --
Line 17:
( function ( mw, $ ) {
"use strict";
var
DESC
EDIT = {
GUIP = {
support: "user:PerfektesChaos/js/" + Signature },
I18N = { },
PREGO = { maxage: 604812,
signature: "preferencesGadgetOptions",
site: "w:en",
store: "User:PerfektesChaos/js/",
sub: "/r.js" },
REPOS = { maxage: 86400,
site: "w:en",
store: "User:PerfektesChaos/js/" },
PROJ = false,
TASK = { },
UTIL = { },
XPORT = { core: { sites: Sites } },
CITWT;
function facilitated() {
// > Signature
// > XPORT
// < .signature
// mw.loader.getState()
// mw.hook()
// 2018-08-24 PerfektesChaos@de.wikipedia
sub = "/" + Sub,
signature = sign + sub,
s = mw.loader.getState( signature ),
e, o, rls;
if ( s !== "loaded" && s !== "ready" ) {
rls = { };
rls[ signature ] = "loaded";
mw.loader.state( rls );
if ( typeof mw.libs[ Signature ] !== "object" ||
! mw.libs[ Signature ] ) {
mw.libs[ Signature ] = { };
}
CITWT = mw.libs[ Signature ];
CITWT.signature = sign;
if ( typeof CITWT.vsn === "string" ) {
CITWT.vsn = CITWT.vsn + " ";
} else {
CITWT.vsn = "";
}
CITWT.vsn = CITWT.vsn + Sub.substr( 0, 1 ) + ":" + Version;
CITWT.type = Signature;
CITWT.suite = ( Version > 1 ? "r" : "d" );
if ( typeof CITWT[ Sub ] !== "object" ||
! CITWT[ Sub ] ) {
CITWT[ Sub ] = { };
}
e = XPORT[ Sub ];
o = CITWT[ Sub ];
for ( s in e ) {
o[ s ] = e[ s ];
} // for s in e
for ( s in XPORT ) {
if ( typeof XPORT[ s ] === "function" ) {
CITWT[ s ] = XPORT[ s ];
}
} // for s in XPORT
rls[ signature ] = "ready";
mw.loader.state( rls );
mw.hook( CITWT.type + sub + ".ready" ).fire();
}
} // facilitated()
Line 60 ⟶ 114:
APIC.site = "https://citoid.wikimedia.org/api";
APIC.stateless = "/api/rest_v1/data/citation/mediawiki/";
Line 73 ⟶ 128:
// GUIP.flat()
// console.log()
// console.dir()
// Remark: Used as event handler -- 'this' is not APIC
// 2015-05-01 PerfektesChaos@de.wikipedia
Line 96 ⟶ 152:
// >< APIC.queries
// TASK.follow()
// 2017-06-10
var n = APIC.queries.length,
i, j, q, r, ufo;
for ( i = 0; i <
r = APIC.results[ i ];
if ( r ) {
if ( q === r.url ) {
r.lucky = true;
APIC.queries[ j ] = r;
r = false;
break; // for j
}
} // for j
if ( r ) {
if ( ! ufo ) {
ufo = [ ];
}
r.idResult = i;
ufo.push( r );
}
}
} // for i
Line 145 ⟶ 202:
// > .server
// > APIC.site
// > APIC.stateless
// > APIC.queries
// >< APIC.data
Line 154 ⟶ 212:
// (APIC.fault)
// Remark: Used as event handler -- 'this' is not APIC
//
var f, i,
if ( typeof APIC.jq !== "object" ) {
f = function ( jqXHR ) {
Line 166 ⟶ 224:
service = APIC.site;
}
APIC.data = { format: "
APIC.jq = { beforeSend: f,
dataType: "json", // No "Intelligent Guess"
Line 173 ⟶ 231:
if ( APIC.legacy ) {
APIC.jq.type = "POST";
} else if ( APIC.stateless ) {
APIC.site = window.___location.protocol + "//" +
window.___location.hostname + APIC.stateless;
} else {
APIC.jq.data = APIC.data;
Line 179 ⟶ 240:
APIC.results = [ ];
APIC.missing = APIC.queries.length;
for ( i = 0; i < APIC.
/* citoid.wmflabs appears to handle an Array of URL
and will return an Array of result objects,
Line 185 ⟶ 246:
nor a stable implementation in spring 2015.
*/
APIC.jq.data
} else if ( APIC.stateless ) {
} else
APIC.jq.data.search = query;
}
$.ajax( APIC.jq ).done( APIC.found )
.fail( APIC.fault );
break;
case "object":
if ( typeof query.beforeSend === "string"
&& query.beforeSend === "json" ) {
query.jq.beforeSend = f;
}
query.jq.beforeSend = f;
$.ajax( query.jq ).done( query.found )
.fail( query.fault );
break;
} // switch typeof query
} // for i
}; // APIC.fire()
Line 202 ⟶ 278:
// Answer arrived
// Precondition:
// arrived -- object, full result
// string, favorite result
// boolean, no favorite result
// Uses:
// >< APIC.missing
// >< APIC.results
// GUIP.flat()
// TASK.follow()
// APIC.finish()
// Remark: Used as event handler -- 'this' is not APIC
//
var got;
APIC.missing--;
case
APIC.missing = -1;
GUIP.flat();
TASK.follow();
break;
case "object":
if ( arrived && typeof arrived.length === "number" ) {
got = arrived[ 0 ];
if ( got && typeof got === "object" ) {
if ( typeof got.url === "string" && got.url ) {
APIC.results.push( got );
APIC.missing--;
} else if ( typeof got.Error === "string" ) {
// Never reached
// HTTP-OPTIONS call APIC.fault() only
// Plan:
// * Record on console
// shared with APIC.fault()
// * Enrich flat("unanswered") by details
GUIP.flat( "unanswered" );
// GUIP.flat( "unanswered", $span );
}
}
}
if ( ! APIC.missing )
}
}; // APIC.found()
Line 238 ⟶ 331:
BOOK.
// 2016-03-21 PerfektesChaos@de.wikipedia
}; // BOOK.fire()
BOOK.
// Uses:
// Precondition:
//
// Uses:
//
//
//
// 2016-03-25 PerfektesChaos@de.wikipedia
CITWT.isbnLib =
switch ( typeof CITWT.isbn ) {
case "string":
case "object":
} // switch typeof CITWT.isbn
}; //
Line 461 ⟶ 377:
// Returns object, or false
// Uses:
// > GUIP.caret.story
// DESC.fold()
Line 552 ⟶ 467:
// > PROJ
// mw.config.get()
//
var r = false,
env, got, i, ns, sign, space, suffix;
Line 590 ⟶ 505:
case "DNB":
case "DOI":
case "ISBN":
case "LCCN":
case "OCLC":
case "PMC":
case "PMID":
case "RFC":
got = /^\|\s*([^|}]+)\s*[|}]/.exec( suffix );
if ( got ) {
Line 789 ⟶ 707:
// Returns object, or not
// Uses:
//
//
//
var got, i, isbnLib, r;
switch ( achieved ) {
case 0:
Line 805 ⟶ 723:
got = /^([-0-9]+x?)(?:[^-0-9x].*)?$/i.exec( r.isbn );
if ( got ) {
isbnLib = CITWT.isbnLib;
r.isbn = got[ 1 ].replace( /-/g, "" );
switch ( r.isbn.length ) {
Line 810 ⟶ 729:
r.isbn = r.isbn.toUpperCase();
if ( /^[0-9]+X?$/.test( r.isbn ) ) {
r.lang =
r.url = true;
}
break;
Line 818 ⟶ 737:
if ( got ) {
if ( got[ 1 ] === "8" ) {
r.lang =
} else {
r.lang =
}
r.url = true;
Line 827 ⟶ 746:
} // switch n
if ( r.url ) {
r.url =
r.say = r.isbn;
r.url.push( "/wiki/Special:Booksources/#" );
Line 1,128 ⟶ 1,047:
// (.fire);
// Remark: Used as event handler -- 'this' is not GUIP
//
var require;
GUIP.$textarea = $( "#wpTextbox1" );
Line 1,138 ⟶ 1,057:
require = [ "jquery.textSelection",
"mediawiki.api" ];
if ( CITWT.bibRecord && CITWT.bibRecord.launch ) {
mw.loader.using( require, CITWT.fire );
} else {
Line 1,163 ⟶ 1,082:
// > .vsn
// > GUIP.$panel
// < GUIP.live
// GUIP.input.free()
Line 1,297 ⟶ 1,215:
// GUIP.btn.portlet()
// (GUIP.btn.wikiEditor)
//
if ( ! this.wikEd() ) {
if ( GUIP.opts.usebetatoolbar ) {
GUIP.submit = "wikiEditor ... (loading)";
mw.loader.using( [ "ext.wikiEditor
this.wikiEditor );
} else if ( GUIP.opts.showtoolbar ) {
Line 1,369 ⟶ 1,287:
// > GUIP.btn.show
// < GUIP.submit
// <
// mw.user.options.get()
// GUIP.file()
//
var id = 746,
cnf;
if ( mw.user.options.get( "gadget-wikEd" )
|| ( typeof window.wikEd === "object"
&& window.wikEd ) ) {
if ( typeof window.wikEd !== "object" ) {
window.wikEd = { config: { } };
}
if ( typeof
EDIT.wikEd.config = { button: { },
buttonBar: { } };
}
cnf =
if ( typeof cnf.button !== "object" ) {
cnf.button = { };
Line 1,410 ⟶ 1,330:
}
cnf.buttonBar.custom1[ 6 ].push( id );
if ( typeof
typeof EDIT.wikEd.turnedOn === "boolean" &&
typeof EDIT.wikEd.useWikEd === "boolean"
&& EDIT.wikEd.useWikEd ) {
GUIP.submit = "wikEd";
}
}
return GUIP.submit;
Line 1,513 ⟶ 1,435:
// > GUIP.caret.max
// > GUIP.$textarea
// > EDIT.codeMirror
// > EDIT.wikEd
// Remark: Used as event handler -- 'this' is not GUIP.caret
//
var shift, start, story;
if ( GUIP.caret.max >= 0 ) {
Line 1,533 ⟶ 1,457:
story = start + shift + story;
GUIP.$textarea.val( story );
if (
} else if ( alien === "wikEd" ) {
EDIT.wikEd.UpdateFrame();
}
}
Line 1,542 ⟶ 1,468:
GUIP.caret.find = function () {
// Get selection in regular textarea, but perhaps CodeMirror
// Precondition:
// jquery.textSelection has been loaded
Line 1,548 ⟶ 1,474:
// selection assigned
// Uses:
// > CodeMirror
// > GUIP.$textarea
// > GUIP.caret.further
// >< EDIT.codeMirror
// < GUIP.caret.story
// < GUIP.caret.min
Line 1,556 ⟶ 1,484:
// < GUIP.caret.starter
// Remark: Used as event handler -- 'this' is not GUIP.caret
//
var rg, uo;
if ( ! EDIT.codeMirror &&
typeof window.CodeMirror === "function" &&
typeof window.CodeMirror.doc === "object"
&& window.CodeMirror.doc ) {
EDIT.codeMirror = window.CodeMirror;
}
if ( EDIT.codeMirror ) {
uo = mw.user.options.get( "usecodemirror" );
EDIT.codeMirror.liveCITWT = ( typeof uo === "number"
&& uo > 0 );
if ( EDIT.codeMirror.liveCITWT ) {
GUIP.$textarea.val( EDIT.codeMirror.doc.getValue() );
}
}
rg = GUIP.$textarea.textSelection( "getCaretPosition",
{ "startAndEnd": true } );
GUIP.caret.story = GUIP.$textarea.val();
if ( rg[0] <= rg[1] ) {
Line 1,579 ⟶ 1,522:
// Uses:
// this
// >
// > GUIP.$textarea
// < GUIP.caret.start
Line 1,590 ⟶ 1,532:
// < GUIP.caret.starter
// (GUIP.caret.wikEdEnclose)
//
var r;
if (
! EDIT.wikEd.disabled &&
EDIT.wikEd.useWikEd &&
EDIT.wikEd.turnedOn &&
EDIT.wikEd.highlightSyntax ) {
this.start = "##\f#citoid_marker_begin##\f#";
this.stop = "##\f#citoid_marker_end##\f#";
r = GUIP.$textarea.val();
this.min = r.search( this.start );
Line 1,605 ⟶ 1,551:
this.max = r.search( this.stop ) - 1;
r = r.replace( this.stop, "" );
GUIP.
this.story = r;
this.selection = this.story.substr( this.min );
this.starter = this.story.substr( 0, this.min );
r = true;
}
Line 1,624 ⟶ 1,570:
// assign got further properties
// Uses:
// > EDIT.wikEd
// > GUIP.caret.start
// > GUIP.caret.stop
// Remark: Used as event handler -- 'this' is not GUIP.caret
//
if ( assign.selection.plain === "" ) {
assign.sel =
if ( assign.sel.anchorNode.previousSibling ) {
assign.changed = assign.cursor;
} else {
assign.changed = assign.whole;
}
Line 1,718 ⟶ 1,665:
// event -- jQuery event object
// Uses:
// > HELP.support
// UTIL.fullstop()
// I18N.fair()
// mw.util.getUrl()
// Remark: Used as event handler -- 'this' is not GUIP.gadget
//
var support = "https://" + I18N.fair( "helpSite", true )
+ ".wikipedia.org"
+ mw.util.getUrl(
UTIL.fullstop( event );
window.open( support, "citWThelp" );
Line 2,173 ⟶ 2,120:
}
}; // I18N.fire()
//-----------------------------------------------------------------------
PREGO.feed = function () {
// Provide PREGO
// Uses:
// > PREGO.signature
// > PREGO.site
// > PREGO.store
// > PREGO.sub
// > PREGO.maxage
// mw.loader.getState()
// mw.loader.state()
// REPOS.fire()
// 2018-09-03 PerfektesChaos@de.wikipedia
var sign = "ext.gadget." + PREGO.signature,
rls;
if ( ! mw.loader.getState( sign ) ) {
rls = { };
rls[ sign ] = "loading";
mw.loader.state( rls );
REPOS.fire( PREGO.site,
PREGO.store + PREGO.signature + PREGO.sub,
false,
{ action: "raw",
ctype: "text/javascript",
bcache: 1,
maxage: PREGO.maxage } );
}
}; // PREGO.feed()
//-----------------------------------------------------------------------
REPOS.fire = function ( at, access, append, alter ) {
// Load from external URL
// Precondition:
// at -- Wikimedia Foundation site code, or not
// access -- string with basic page name
// append -- string with subpage, or not
// alter -- parameter object, or MIME string, or not
// Uses:
// >< REPOS.requests
// REPOS.foundation()
// mw.loader.load()
// 2018-03-21 PerfektesChaos@de.wikipedia
var source, syntax;
if ( typeof REPOS.requests !== "object" ) {
REPOS.requests = { };
}
if ( typeof REPOS.requests[ access ] !== "boolean" ) {
REPOS.requests[ access ] = true;
if ( append ) {
source = access + append;
} else {
source = access;
}
if ( at ) {
source = REPOS.foundation( at, source, alter );
if ( typeof alter === "object"
&& alter &&
typeof alter.ctype === "string" ) {
syntax = alter.ctype;
}
} else {
syntax = alter;
}
mw.loader.load( source, syntax );
}
}; // REPOS.fire()
REPOS.foundation = function ( at, access, alter ) {
// Create URL within Wikimedia Foundation
// Precondition:
// at -- site code, or not
// access -- string with page name
// alter -- parameter object, or not
// Postcondition:
// Returns full URL
// 2018-03-21 PerfektesChaos@de.wikipedia
var s = access,
r = encodeURI( s );
if ( typeof alter === "object"
&& alter ) {
r = "/w/index.php?title=" + r;
if ( access.substr( -3 ) === ".js" ) {
alter.ctype = "text/javascript";
} else if ( access.substr( -4 ) === ".css" ) {
alter.ctype = "text/css";
}
alter.action = "raw";
for ( s in alter ) {
r = r + "&" + s + "=" + encodeURI( alter[ s ] );
} // for s in alter
} else {
r = "/wiki/" + r;
}
if ( typeof at === "string"
&& at ) {
switch ( at ) {
case "meta":
r = "meta.wikimedia.org" + r;
break;
case "mw":
r = "www.mediawiki.org" + r;
break;
case "w:en":
r = "en.wikipedia.org" + r;
break;
default:
r = window.___location.host + r;
} // switch at
r = "https://" + r;
}
return r;
}; // REPOS.foundation()
Line 2,231 ⟶ 2,303:
// GUIP.push.flip()
// Remark: Used as event handler -- 'this' is not TASK
//
var mode, result;
if ( ! APIC.missing
result = CITWT.opus.fire( TASK.query, APIC.queries );
}
TASK.template = PROJ.fire( result );
switch ( typeof TASK.template ) {
case "object":
mode = ( TASK.template[ 0 ][ 2 ] ? 4 : 3 );
break;
case "string":
if ( CITWT.bibRecord && CITWT.bibRecord.launch ) {
GUIP.caret.fill( TASK.template );
TASK.template = false;
Line 2,272 ⟶ 2,344:
// mw.user.options.get()
// mw.loader.using()
//
// (
//
if ( typeof CITWT[ CITWT.core.site ] === "object" ) {
PROJ = CITWT[ CITWT.core.site ];
Line 2,284 ⟶ 2,356:
// race condition
mw.loader.using( [ "ext.wikiEditor" ],
} else {
}
}
Line 2,302 ⟶ 2,374:
// mw.loader.using()
// (TASK.filter)
//
GUIP.flat( "translated" );
mw.loader.using( [ CITWT.signature + "/opus",
"user:PerfektesChaos
TASK.filter );
GUIP.flat( "translated", TASK.query.$links );
Line 2,319 ⟶ 2,391:
// > GUIP.caret.selection
// > .bibRecord
// > APIC.stateless
// > APIC.found
// < TASK.query
// < APIC.queries
// DESC.fire()
// GUIP.flat()
// APIC.worldCat.fire()
// .(project).favorite()
// GUIP.$flip()
// GUIP.$flipper()
Line 2,328 ⟶ 2,405:
// GUIP.input.filler()
// (APIC.fire)
//
var search = ( ask ? ask : GUIP.caret.selection ),
i, k, m, project, q, suggest, url;
if ( ! search && CITWT.bibRecord ) {
search = CITWT.bibRecord.sign + " " + CITWT.bibRecord.subject;
Line 2,374 ⟶ 2,452:
}
GUIP.flat( "throbber", q.$links );
if ( typeof APIC.stateless === "string" &&
typeof q.scheme === "string" &&
stateless.indexOf( "|" + q.scheme + "|" ) >= 0 ) {
stateless = q[ q.scheme ];
if ( typeof stateless === "string" ) {
stateless = q.scheme.toUpperCase()
+ " " + stateless;
APIC.queries.push( stateless );
}
}
if ( typeof PROJ.favorite === "function" ) {
project = PROJ.favorite( q, APIC.found );
if ( project ) {
project.local = true;
APIC.queries.push( project );
}
}
mw.loader.using( [ "mediawiki.api" ],
APIC.fire );
Line 2,439 ⟶ 2,534:
UTIL.feed = function ( assigned, access
// Load and run script
// Precondition:
// assigned -- signature
// access -- script
// address -- URL to be used, else created from access
// Uses:
// > .suite
// > REPOS.maxage
// mw.loader.getState()
// mw.loader.
//
// 2018-09-03 PerfektesChaos@de.wikipedia
var p, rls;
if ( ! mw.loader.getState( assigned ) ) {
rls[ assigned
mw.loader.state( rls );
if ( address )
mw.loader.load( address
} else
p = { bcache:
p.maxage = ( CITWT.suite === "r" ?
REPOS.fire(
REPOS.store + access + ".js",
false,
p );
}
}
}; // UTIL.feed()
Line 2,468 ⟶ 2,568:
UTIL.fetch = function ( access
// Run subscripts
// Precondition:
// access -- subscript
// Uses:
// > .signature
// > .type
// >
// UTIL.feed()
//
var sub = "/" + access;
UTIL.feed( CITWT.signature + sub,
CITWT.type + sub + "/" + CITWT.suite
}; // UTIL.fetch()
Line 2,493 ⟶ 2,588:
// Load current project plug-in, if appropriate
// Postcondition:
// Returns something, if plug-in
// Uses:
// > .signature
Line 2,502 ⟶ 2,597:
// UTIL.fetch()
// console.log()
//
var signature = CITWT.signature + "/" + CITWT.core.site,
r, s;
Line 2,510 ⟶ 2,605:
r = CITWT[ CITWT.core.site ];
if ( r ) {
UTIL.feed( signature
}
break;
Line 2,559 ⟶ 2,654:
// Derive simple template parameters; not nested
// Precondition:
Line 2,605 ⟶ 2,700:
} // for i
return r;
}; //
Line 2,613 ⟶ 2,708:
// Tool button has been clicked
// Precondition:
Line 2,623 ⟶ 2,718:
// GUIP.factory()
// TASK.fiat()
// Remark: Used as event handler -- 'this' is not
// 2015-05-23 PerfektesChaos@de.wikipedia
UTIL.fullstop( event );
Line 2,632 ⟶ 2,727:
}
TASK.fiat();
}; //
Line 2,643 ⟶ 2,738:
// Start regular workflow in edit mode, if appropriate
// Uses:
// >
// >
// > HELP.support
// > Version
// > .signature
// >< .bibRecord
// < .support
// < .core.site
//
// mw.config.get()
// PREGO.feed()
// UTIL.fetch()
// UTIL.feed()
// UTIL.friend()
// mw.loader.using()
// (TASK.fire)
//
var
env = mw.config.get( [ "wgAction",
"wgDBname",
"wgIsProbablyEditable",
"wgPageContentModel" ] );
CITWT.support = "[[" + HELP.site + ":" + HELP.support + "]]";
CITWT.core.site = env.wgDBname;
suitable = "|edit|submit|";
if ( suitable.indexOf( env.wgAction ) > 0 &&
env.wgIsProbablyEditable &&
env.wgPageContentModel === "wikitext" &&
UTIL.friend() ) {
UTIL.fetch( "opus" );
sub = ( Version > 1 ? "r" : "d" );
UTIL.feed( "ext.gadget.isbnLib",
"isbnLib/" + sub );
if ( typeof CITWT.bibRecord !== "object" ) {
CITWT.bibRecord = false;
Line 2,697 ⟶ 2,795:
fire();
}( window.mediaWiki, window.jQuery ) );
/// EOF </nowiki> citoidWikitext/core/d.js
|