Anonymous user
MediaWiki:Gadget-morebits.js: Difference between revisions
m
Reverted edits by Amorymeltzer (talk) to last version by MusikAnimal
imported>Amorymeltzer (Repo at a999149: add documentation; more documentation) |
m (Reverted edits by Amorymeltzer (talk) to last version by MusikAnimal) |
||
Line 40:
* **************** Morebits.userIsInGroup() ****************
* Simple helper function to see what groups a user might belong
*/
Morebits.userIsInGroup = function ( group ) {
return mw.config.get( 'wgUserGroups' ).indexOf( group ) !== -1;
Line 55 ⟶ 54:
* Converts an IPv6 address to the canonical form stored and used by MediaWiki.
*/
Morebits.sanitizeIPv6 = function ( address ) {
address = address.trim();
Line 148:
*/
Morebits.quickForm = function QuickForm( event, eventType ) {
this.root = new Morebits.quickForm.element( { type: 'form', event: event, eventType:eventType } );
};
Morebits.quickForm.prototype.render = function QuickFormRender() {
var ret = this.root.render();
Line 167 ⟶ 158:
};
Morebits.quickForm.prototype.append = function QuickFormAppend( data ) {
return this.root.append( data );
};
Morebits.quickForm.element = function QuickFormElement( data ) {
this.data = data;
Line 187 ⟶ 170:
Morebits.quickForm.element.id = 0;
Morebits.quickForm.element.prototype.append = function QuickFormElementAppend( data ) {
var child;
Line 204 ⟶ 181:
};
// This should be called without parameters: form.render()
Morebits.quickForm.element.prototype.render = function QuickFormElementRender( internal_subgroup_id ) {
var currentNode = this.compute( this.data, internal_subgroup_id );
Line 676 ⟶ 649:
};
Morebits.quickForm.element.generateTooltip = function QuickFormElementGenerateTooltip( node, data ) {
$('<span/>', {
Line 692 ⟶ 661:
});
};
/**
* Some utility methods for manipulating quickForms after their creation
* (None of them work for "dyninput" type fields at present)
*
* Morebits.quickForm.getElements(form, fieldName)
* Returns all form elements with a given field name or ID
*
* Morebits.quickForm.getCheckboxOrRadio(elementArray, value)
* Searches the array of elements for a checkbox or radio button with a certain |value| attribute
*
* Morebits.quickForm.getElementContainer(element)
* Returns the <div> containing the form element, or the form element itself
* May not work as expected on checkboxes or radios
*
* Morebits.quickForm.getElementLabelObject(element)
* Gets the HTML element that contains the label of the given form element (mainly for internal use)
*
* Morebits.quickForm.getElementLabel(element)
* Gets the label text of the element
*
* Morebits.quickForm.setElementLabel(element, labelText)
* Sets the label of the element to the given text
*
* Morebits.quickForm.overrideElementLabel(element, temporaryLabelText)
* Stores the element's current label, and temporarily sets the label to the given text
*
* Morebits.quickForm.resetElementLabel(element)
* Restores the label stored by overrideElementLabel
*
* Morebits.quickForm.setElementVisibility(element, visibility)
* Shows or hides a form element plus its label and tooltip
*
* Morebits.quickForm.setElementTooltipVisibility(element, visibility)
* Shows or hides the "question mark" icon next to a form element
*/
Morebits.quickForm.getElements = function QuickFormGetElements(form, fieldName) {
var $form = $(form);
Line 715 ⟶ 711:
};
Morebits.quickForm.getCheckboxOrRadio = function QuickFormGetCheckboxOrRadio(elementArray, value) {
var found = $.grep(elementArray, function(el) {
Line 732 ⟶ 721:
};
Morebits.quickForm.getElementContainer = function QuickFormGetElementContainer(element) {
// for divs, headings and fieldsets, the container is the element itself
Line 749 ⟶ 732:
};
Morebits.quickForm.getElementLabelObject = function QuickFormGetElementLabelObject(element) {
// for buttons, divs and headers, the label is on the element itself
Line 775 ⟶ 752:
};
Morebits.quickForm.getElementLabel = function QuickFormGetElementLabel(element) {
var labelElement = Morebits.quickForm.getElementLabelObject(element);
Line 789 ⟶ 761:
};
Morebits.quickForm.setElementLabel = function QuickFormSetElementLabel(element, labelText) {
var labelElement = Morebits.quickForm.getElementLabelObject(element);
Line 805 ⟶ 771:
};
Morebits.quickForm.overrideElementLabel = function QuickFormOverrideElementLabel(element, temporaryLabelText) {
if (!element.hasAttribute("data-oldlabel")) {
Line 818 ⟶ 778:
};
Morebits.quickForm.resetElementLabel = function QuickFormResetElementLabel(element) {
if (element.hasAttribute("data-oldlabel")) {
Line 830 ⟶ 785:
};
Morebits.quickForm.setElementVisibility = function QuickFormSetElementVisibility(element, visibility) {
$(element).toggle(visibility);
};
Morebits.quickForm.setElementTooltipVisibility = function QuickFormSetElementTooltipVisibility(element, visibility) {
$(Morebits.quickForm.getElementContainer(element)).find(".morebits-tooltip").toggle(visibility);
Line 852 ⟶ 797:
/**
* **************** HTMLFormElement ****************
*
* getChecked:
* XXX Doesn't seem to work reliably across all browsers at the moment. -- see getChecked2 in twinkleunlink.js, which is better
*
* Returns an array containing the values of elements with the given name, that has it's
* checked property set to true. (i.e. a checkbox or a radiobutton is checked), or select options
* that have selected set to true. (don't try to mix selects with radio/checkboxes, please)
* Type is optional and can specify if either radio or checkbox (for the event
* that both checkboxes and radiobuttons have the same name.
*/
HTMLFormElement.prototype.getChecked = function( name, type ) {
var elements = this.elements[name];
Line 913 ⟶ 856:
* **************** RegExp ****************
*
*
*/
Line 1,049 ⟶ 990:
return str.substr( 0, 1 ).toLowerCase() + str.substr( 1 );
},
splitWeightedByKeys: function( str, start, end, skip ) {
if( start.length !== end.length ) {
throw new Error( 'start marker and end marker must be of the same length' );
Line 1,066 ⟶ 997:
var initial = null;
var result = [];
if( ! Array.isArray(
if(
} else if( typeof
} else {
throw new Error( "non-applicable
}
}
for( var i = 0; i < str.length; ++i ) {
for( var j = 0; j <
if( str.substr( i,
i +=
continue;
}
Line 1,100 ⟶ 1,031:
return result;
},
// for deletion/other templates taking a freeform "reason" from a textarea (e.g. PROD, XFD, RPP)
formatReasonText: function( str ) {
var result = str.toString().trimRight();
Line 1,114 ⟶ 1,039:
return unbinder.rebind();
},
// a replacement for String.prototype.replace() when the second parameter (the
// replacement string) is arbitrary, such as a username or freeform user input,
// and may contain dollar signs
safeReplace: function morebitsStringSafeReplace(string, pattern, replacement) {
return string.replace(pattern, replacement.replace(/\$/g, "$$$$"));
}
};
/**
* **************** Morebits.array ****************
*
* uniq(arr): returns a copy of the array with duplicates removed
*
* dups(arr): returns a copy of the array with the first instance of each value
* removed; subsequent instances of those values (duplicates) remain
*
* chunk(arr, size): breaks up |arr| into smaller arrays of length |size|, and
* returns an array of these "chunked" arrays
*/
Morebits.array = {
uniq: function(arr) {
if ( ! Array.isArray( arr ) ) {
Line 1,147 ⟶ 1,075:
return result;
},
dups: function(arr) {
if ( ! Array.isArray( arr ) ) {
Line 1,168 ⟶ 1,091:
return result;
},
chunk: function( arr, size ) {
if ( ! Array.isArray( arr ) ) {
Line 1,196 ⟶ 1,110:
}
};
Line 1,204 ⟶ 1,119:
*/
Morebits.pageNameNorm = mw.config.get('wgPageName').replace(/_/g, ' ');
/**
* **************** Morebits.unbinder ****************
* Used by Morebits.wikitext.page.commentOutImage
*/
Morebits.unbinder = function Unbinder( string ) {
if( typeof string !== 'string' ) {
Line 1,237 ⟶ 1,139:
Morebits.unbinder.prototype = {
unbind: function UnbinderUnbind( prefix, postfix ) {
var re = new RegExp( prefix + '(.*?)' + postfix, '
this.content = this.content.replace( re, Morebits.unbinder.getCallback( this ) );
},
rebind: function UnbinderRebind() {
var content = this.content;
Line 1,285 ⟶ 1,179:
*/
Date.monthNames = [
'January',
'February',
'March',
'April',
'May',
'June',
'July',
'August',
'September',
'October',
'November',
'December'
];
Date.monthNamesAbbrev = [
'Jan',
'Feb',
'Mar',
'Apr',
'May',
'Jun',
'Jul',
'Aug',
'Sep',
'Oct',
'Nov',
'Dec'
];
Date.prototype.getMonthName = function() {
Line 1,385 ⟶ 1,304:
* Various objects for wiki editing and API access
*/
Morebits.wiki = {};
// Determines whether the current page is a redirect or soft redirect
// (fails to detect soft redirects on edit, history, etc. pages)
Morebits.wiki.isPageRedirect = function wikipediaIsPageRedirect() {
return !!(mw.config.get("wgIsRedirect") || document.getElementById("softredirect"));
Line 1,400 ⟶ 1,318:
* **************** Morebits.wiki.actionCompleted ****************
*
*
* Every call to Morebits.wiki.api.post() results in the dispatch of
* an asynchronous callback. Each callback can in turn
Line 1,468 ⟶ 1,386:
* **************** Morebits.wiki.api ****************
* An easy way to talk to the MediaWiki API.
*
* Constructor parameters:
* currentAction: the current action (required)
* query: the query (required)
* onSuccess: the function to call when request gotten
* statusElement: a Morebits.status object to use for status messages (optional)
*
*/
Morebits.wiki.api = function( currentAction, query, onSuccess, statusElement, onError ) {
Line 1,506 ⟶ 1,422:
errorText: null, // full error description, if any
// post(): carries out the request
// do not specify a parameter unless you really really want to give jQuery some extra parameters
post: function( callerAjaxParameters ) {
Line 1,601 ⟶ 1,514:
var morebitsWikiApiUserAgent = 'morebits.js/2.0 ([[w:WT:TW]])';
// Sets the custom user agent header
Morebits.wiki.api.setApiUserAgent = function( ua ) {
morebitsWikiApiUserAgent = ( ua ? ua + ' ' : '' ) + 'morebits.js/2.0 ([[w:WT:TW]])';
Line 1,626 ⟶ 1,536:
*
*
* NOTE: This list of member functions is incomplete.
*
* Constructor: Morebits.wiki.page(pageName, currentAction)
Line 1,633 ⟶ 1,543:
* currentAction - a string describing the action about to be undertaken (optional)
*
* load(onSuccess
* onSuccess - callback function which is called when the load has succeeded
* onFailure - callback function which is called when the load fails (optional)
*
*
* onSuccess - callback function which is called when the save has succeeded (optional)
* onFailure - callback function which is called when the save fails (optional)
* Warning: Calling save() can result in additional calls to the previous load() callbacks to
* recover from edit conflicts!
Line 1,647 ⟶ 1,555:
* This behavior can be disabled with setMaxConflictRetries(0).
*
* append(
*
* onSuccess - callback function which is called when the method has succeeded (optional)
* onFailure - callback function which is called when the method fails (optional)
*
* prepend(
*
* onSuccess - callback function which is called when the method has succeeded (optional)
* onFailure - callback function which is called when the method fails (optional)
*
* getPageName(): returns a string containing the name of the loaded page, including the namespace
*
* getPageText(): returns a string containing the text of the page after a successful load()
*
* setPageText(pageText)
* pageText - string containing the updated page text that will be saved when save() is called
*
* setAppendText(appendText)
* appendText - string containing the text that will be appended to the page when append() is called
*
* setPrependText(prependText)
* prependText - string containing the text that will be prepended to the page when prepend() is called
*
* setEditSummary(summary)
* summary - string containing the text of the edit summary that will be used when save() is called
*
* setMinorEdit(minorEdit)
* minorEdit is a boolean value:
* true - When save is called, the resulting edit will be marked as "minor".
* false - When save is called, the resulting edit will not be marked as "minor". (default)
*
* setBotEdit(botEdit)
* botEdit is a boolean value:
* true - When save is called, the resulting edit will be marked as "bot".
* false - When save is called, the resulting edit will not be marked as "bot". (default)
*
* setPageSection(pageSection)
* pageSection - integer specifying the section number to load or save. The default is |null|, which means
* that the entire page will be retrieved.
*
* setMaxConflictRetries(maxRetries)
* maxRetries - number of retries for save errors involving an edit conflict or loss of edit token
* default: 2
*
* setMaxRetries(maxRetries)
* maxRetries - number of retries for save errors not involving an edit conflict or loss of edit token
* default: 2
*
* setCallbackParameters(callbackParameters)
Line 1,680 ⟶ 1,616:
* getStatusElement(): returns the Status element created by the constructor
*
* setFollowRedirect(followRedirect)
* followRedirect is a boolean value:
* true - a maximum of one redirect will be followed.
* In the event of a redirect, a message is displayed to the user and
* the redirect target can be retrieved with getPageName().
* false - the requested pageName will be used without regard to any redirect. (default)
*
* setWatchlist(watchlistOption)
* watchlistOption is a boolean value:
* true - page will be added to the user's watchlist when save() is called
* false - watchlist status of the page will not be changed (default)
*
* setWatchlistFromPreferences(watchlistOption)
* watchlistOption is a boolean value:
* true - page watchlist status will be set based on the user's
* preference settings when save() is called
* false - watchlist status of the page will not be changed (default)
*
* Watchlist notes:
* 1. The MediaWiki API value of 'unwatch', which explicitly removes the page from the
* user's watchlist, is not used.
* 2. If both setWatchlist() and setWatchlistFromPreferences() are called,
* the last call takes priority.
* 3. Twinkle modules should use the appropriate preference to set the watchlist options.
* 4. Most Twinkle modules use setWatchlist().
* setWatchlistFromPreferences() is only needed for the few Twinkle watchlist preferences
* that accept a string value of 'default'.
*
* setCreateOption(createOption)
* createOption is a string value:
* 'recreate' - create the page if it does not exist, or edit it if it exists
* 'createonly' - create the page if it does not exist, but return an error if it
* already exists
* 'nocreate' - don't create the page, only edit it if it already exists
* null - create the page if it does not exist, unless it was deleted in the moment
* between retrieving the edit token and saving the edit (default)
*
* exists(): returns true if the page existed on the wiki when it was last loaded
*
* lookupCreator(onSuccess): Retrieves the username of the user who created the page
Line 1,689 ⟶ 1,660:
*
* getCreator(): returns the user who created the page following lookupCreator()
*
* getCurrentID(): returns a string containing the current revision ID of the page
*
* patrol(): marks the page as patrolled, if possible
*
* move(onSuccess, onFailure): Moves a page to another title
*
* deletePage(onSuccess, onFailure): Deletes a page (for admins only)
*
*/
Line 1,720 ⟶ 1,699:
*/
Morebits.wiki.page = function(pageName, currentAction) {
Line 1,824 ⟶ 1,797:
/**
* Public interface accessors
*/
this.getPageName = function() {
return ctx.pageName;
};
this.getPageText = function() {
return ctx.pageText;
};
this.setPageText = function(pageText) {
ctx.editMode = 'all';
ctx.pageText = pageText;
};
this.setAppendText = function(appendText) {
ctx.editMode = 'append';
ctx.appendText = appendText;
};
this.setPrependText = function(prependText) {
ctx.editMode = 'prepend';
ctx.prependText = prependText;
};
this.setEditSummary = function(summary) {
ctx.editSummary = summary;
};
this.setCreateOption = function(createOption) {
ctx.createOption = createOption;
};
this.setMinorEdit = function(minorEdit) {
ctx.minorEdit = minorEdit;
};
this.setBotEdit = function(botEdit) {
ctx.botEdit = botEdit;
};
this.setPageSection = function(pageSection) {
ctx.pageSection = pageSection;
};
this.setMaxConflictRetries = function(maxRetries) {
ctx.maxConflictRetries = maxRetries;
};
this.setMaxRetries = function(maxRetries) {
ctx.maxRetries = maxRetries;
};
this.setCallbackParameters = function(callbackParameters) {
ctx.callbackParameters = callbackParameters;
};
this.getCallbackParameters = function() {
return ctx.callbackParameters;
};
this.getCreator = function() {
return ctx.creator;
};
this.setOldID = function(oldID) {
ctx.revertOldID = oldID;
};
this.getCurrentID = function() {
return ctx.revertCurID;
};
this.getRevisionUser = function() {
return ctx.revertUser;
};
this.setMoveDestination = function(destination) {
ctx.moveDestination = destination;
};
this.setMoveTalkPage = function(flag) {
ctx.moveTalkPage = !!flag;
};
this.setMoveSubpages = function(flag) {
ctx.moveSubpages = !!flag;
};
this.setMoveSuppressRedirect = function(flag) {
ctx.moveSuppressRedirect = !!flag;
};
this.setEditProtection = function(level, expiry) {
ctx.protectEdit = { level: level, expiry: expiry };
};
this.setMoveProtection = function(level, expiry) {
ctx.protectMove = { level: level, expiry: expiry };
};
this.setCreateProtection = function(level, expiry) {
ctx.protectCreate = { level: level, expiry: expiry };
};
this.setCascadingProtection = function(flag) {
ctx.protectCascade = !!flag;
};
this.setFlaggedRevs = function(level, expiry) {
ctx.flaggedRevs = { level: level, expiry: expiry };
};
this.getStatusElement = function() {
return ctx.statusElement;
};
this.setFollowRedirect = function(followRedirect) {
if (ctx.pageLoaded) {
ctx.statusElement.error("Internal error: cannot change redirect setting after the page has been loaded!");
return;
}
ctx.followRedirect = followRedirect;
};
this.setWatchlist = function(flag) {
if (flag) {
ctx.watchlistOption = 'watch';
} else {
ctx.watchlistOption = 'nochange';
}
};
this.setWatchlistFromPreferences = function(flag) {
if (flag) {
ctx.watchlistOption = 'preferences';
} else {
ctx.watchlistOption = 'nochange';
}
};
this.suppressProtectWarning = function() {
ctx.suppressProtectWarning = true;
};
this.exists = function() {
return ctx.pageExists;
};
this.load = function(onSuccess, onFailure) {
ctx.onLoadSuccess = onSuccess;
Line 1,870 ⟶ 1,988:
};
// Save updated .pageText to Wikipedia
// Only valid after successful .load()
this.save = function(onSuccess, onFailure) {
ctx.onSaveSuccess = onSuccess;
Line 1,974 ⟶ 2,081:
};
this.append = function(onSuccess, onFailure) {
ctx.editMode = 'append';
Line 1,992 ⟶ 2,093:
};
this.prepend = function(onSuccess, onFailure) {
ctx.editMode = 'prepend';
Line 2,010 ⟶ 2,105:
};
this.lookupCreator = function(onSuccess) {
if (!onSuccess) {
Line 2,294 ⟶ 2,130:
};
this.patrol = function() {
// There's no patrol link on page, so we can't patrol
Line 2,322 ⟶ 2,155:
};
this.revert = function(onSuccess, onFailure) {
ctx.onSaveSuccess = onSuccess;
Line 2,341 ⟶ 2,169:
};
this.move = function(onSuccess, onFailure) {
ctx.onMoveSuccess = onSuccess;
Line 2,380 ⟶ 2,203:
// |delete| is a reserved word in some flavours of JS
this.deletePage = function(onSuccess, onFailure) {
ctx.onDeleteSuccess = onSuccess;
Line 2,421 ⟶ 2,239:
};
this.protect = function(onSuccess, onFailure) {
ctx.onProtectSuccess = onSuccess;
Line 2,505 ⟶ 2,318:
};
/* Private member functions
*
* These are not exposed outside
*/
Line 2,517 ⟶ 2,330:
*
* @param {string} action The action being undertaken, e.g. "edit", "delete".
*/
var fnCanUseMwUserToken = function(action) {
Line 3,036 ⟶ 2,848:
}; // end Morebits.wiki.page
/** Morebits.wiki.page TODO: (XXX)
* - Should we retry loads also?
* - Need to reset current action before the save?
* - Deal with action.completed stuff
* - Need to reset all parameters once done (e.g. edit summary, move destination, etc.)
*/
Line 3,048 ⟶ 2,860:
* **************** Morebits.wiki.preview ****************
* Uses the API to parse a fragment of wikitext and render it as HTML.
*
* Constructor: Morebits.wiki.preview(previewbox, currentAction)
* previewbox - the <div> element that will contain the rendered HTML
*
* beginRender(wikitext): Displays the preview box, and begins an asynchronous attempt
* to render the specified wikitext.
* wikitext - wikitext to render; most things should work, including subst: and ~~~~
* pageTitle - optional parameter for the page this should be rendered as being on
*
* closePreview(): Hides the preview box and clears it.
*
* The suggested implementation pattern (in Morebits.simpleWindow + Morebits.quickForm situations) is to
Line 3,055 ⟶ 2,877:
*/
Morebits.wiki.preview = function(previewbox) {
this.previewbox = previewbox;
$(previewbox).addClass("morebits-previewbox").hide();
this.beginRender = function(wikitext, pageTitle) {
$(previewbox).show();
Line 3,095 ⟶ 2,907:
}
previewbox.innerHTML = html;
$(previewbox).find("a").attr("target", "_blank");
};
this.closePreview = function() {
$(previewbox).empty().hide();
Line 3,149 ⟶ 2,958:
}
if( test2 === ']]' ) {
current +=
++i;
--level;
Line 3,207 ⟶ 3,016:
};
Morebits.wikitext.page = function mediawikiPage( text ) {
this.text = text;
Line 3,217 ⟶ 3,022:
Morebits.wikitext.page.prototype = {
text: '',
removeLink: function( link_target ) {
var first_char = link_target.substr( 0, 1 );
Line 3,235 ⟶ 3,035:
this.text = this.text.replace( link_simple_re, "$1" ).replace( link_named_re, "$1" );
},
commentOutImage: function( image, reason ) {
var unbinder = new Morebits.unbinder( this.text );
Line 3,250 ⟶ 3,043:
var image_re_string = "[" + first_char.toUpperCase() + first_char.toLowerCase() + ']' + RegExp.escape( image.substr( 1 ), true );
/*
* Check for normal image links, i.e. [[Image:Foobar.png|...]]
* Will eat the whole link
*/
var links_re = new RegExp( "\\[\\[(?:[Ii]mage|[Ff]ile):\\s*" + image_re_string );
var allLinks = Morebits.array.uniq(Morebits.string.splitWeightedByKeys( unbinder.content, '[[', ']]' ));
Line 3,263 ⟶ 3,058:
unbinder.unbind( '<!--', '-->' );
/*
*/
var gallery_image_re = new RegExp( "(^\\s*(?:[Ii]mage|[Ff]ile):\\s*" + image_re_string + ".*?$)", 'mg' );
unbinder.content = unbinder.content.replace( gallery_image_re, "<!-- " + reason + "$1 -->" );
Line 3,271 ⟶ 3,067:
// unbind the newly created comments
unbinder.unbind( '<!--', '-->' );
/*
*/
var free_image_re = new RegExp( "(\\|\\s*(?:[\\w\\s]+\\=)?\\s*(?:(?:[Ii]mage|[Ff]ile):\\s*)?" + image_re_string + ")", 'mg' );
unbinder.content = unbinder.content.replace( free_image_re, "<!-- " + reason + "$1 -->" );
// Rebind the content now, we are done!
this.text = unbinder.rebind();
},
addToImageComment: function( image, data ) {
var first_char = image.substr( 0, 1 );
Line 3,306 ⟶ 3,098:
this.text = this.text.replace( gallery_re, newtext );
},
removeTemplate: function( template ) {
var first_char = template.substr( 0, 1 );
Line 3,323 ⟶ 3,109:
}
},
getText: function() {
return this.text;
Line 3,333 ⟶ 3,118:
/**
* **************** Morebits.queryString ****************
* Maps the querystring to an
*
* Functions:
*
* Morebits.queryString.exists(key)
* returns true if the particular key is set
* Morebits.queryString.get(key)
* returns the value associated to the key
* Morebits.queryString.equals(key, value)
* returns true if the value associated with given key equals given value
* Morebits.queryString.toString()
* returns the query string as a string
* Morebits.queryString.create( hash )
* creates an querystring and encodes strings via encodeURIComponent and joins arrays with |
*
* In static context, the value of location.search.substring(1), else the value given to the constructor is going to be used. The mapped hash is saved in the object.
*
* Example:
Line 3,343 ⟶ 3,140:
* var obj = new Morebits.queryString('foo=bar&baz=quux');
* value = obj.get('foo');
*/
Morebits.queryString = function QueryString(qString) {
Line 3,383 ⟶ 3,175:
Morebits.queryString.staticInit();
return Morebits.queryString.staticstr.get(key);
};
Morebits.queryString.prototype.get = function(key) {
return this.params[key] ? this.params[key] : null;
};
Line 3,388 ⟶ 3,184:
Morebits.queryString.staticInit();
return Morebits.queryString.staticstr.exists(key);
};
Morebits.queryString.prototype.exists = function(key) {
return this.params[key] ? true : false;
};
Line 3,393 ⟶ 3,193:
Morebits.queryString.staticInit();
return Morebits.queryString.staticstr.equals(key, value);
};
Morebits.queryString.prototype.equals = function(key, value) {
return this.params[key] === value ? true : false;
};
Line 3,398 ⟶ 3,202:
Morebits.queryString.staticInit();
return Morebits.queryString.staticstr.toString();
};
Morebits.queryString.prototype.toString = function() {
return this.string ? this.string : null;
};
Line 3,428 ⟶ 3,236:
return resarr.join('&');
};
Morebits.queryString.prototype.create = Morebits.queryString.create;
Line 3,601 ⟶ 3,371:
};
// display the user's rationale, comments, etc. back to them after a failure,
// so they don't use it
Morebits.status.printUserText = function( comments, message ) {
var p = document.createElement( 'p' );
Line 3,684 ⟶ 3,452:
}
};
Line 3,722 ⟶ 3,490:
*/
Morebits.batchOperation = function(currentAction) {
var ctx = {
Line 3,751 ⟶ 3,515:
};
this.setPageList = function(pageList) {
ctx.pageList = pageList;
};
this.setOption = function(optionName, optionValue) {
ctx.options[optionName] = optionValue;
};
this.run = function(worker) {
if (ctx.running) {
Line 3,899 ⟶ 3,646:
*/
// The height passed in here is the maximum allowable height for the content area.
Morebits.simpleWindow = function SimpleWindow( width, height ) {
var content = document.createElement( 'div' );
Line 3,913 ⟶ 3,656:
$(this.content).dialog({
autoOpen: false,
buttons: { "Placeholder button": function() {} },
dialogClass: 'morebits-dialog',
width: Math.min(parseInt(window.innerWidth, 10), parseInt(width ? width : 800, 10)),
// give jQuery the given height value (which represents the anticipated height of the dialog) here, so
// it can position the dialog appropriately
// the 20 pixels represents adjustment for the extra height of the jQuery dialog "chrome", compared
// to that of the old SimpleWindow
height: height + 20,
close: function(event) {
// dialogs and their content can be destroyed once closed
$(event.target).dialog("destroy").remove();
},
resizeStart: function() {
this.scrollbox = $(this).find(".morebits-scrollbox")[0];
if (this.scrollbox) {
this.scrollbox.style.maxHeight = "none";
}
},
resizeEnd: function() {
this.scrollbox = null;
},
resize: function() {
this.style.maxHeight = "";
if (this.scrollbox) {
this.scrollbox.style.width = "";
}
}
}
var $widget = $(this.content).dialog("widget");
Line 3,972 ⟶ 3,715:
scriptName: null,
// Focuses the dialog. This might work, or on the contrary, it might not.
focus: function() {
$(this.content).dialog("moveToTop");
return this;
},
// Closes the dialog. If this is set as an event handler, it will stop the event from doing anything more.
close: function(event) {
if (event) {
Line 3,991 ⟶ 3,727:
}
$(this.content).dialog("close");
return this;
},
// Shows the dialog. Calling display() on a dialog that has previously been closed might work, but it is not guaranteed.
display: function() {
if (this.scriptName) {
Line 4,015 ⟶ 3,747:
}
this.setHeight( this.height ); // init height algorithm
return this;
},
// Sets the dialog title.
setTitle: function( title ) {
$(this.content).dialog("option", "title", title);
return this;
},
// Sets the script name, appearing as a prefix to the title to help users determine which
// user script is producing which dialog. For instance, Twinkle modules set this to "Twinkle".
setScriptName: function( name ) {
this.scriptName = name;
return this;
},
// Sets the dialog width.
setWidth: function( width ) {
$(this.content).dialog("option", "width", width);
return this;
},
// Sets the dialog's maximum height. The dialog will auto-size to fit its contents,
// but the content area will grow no larger than the height given here.
setHeight: function( height ) {
this.height = height;
// from display time onwards, let the browser determine the optimum height, and instead limit the height at the given value
// note that the given height will exclude the approx. 20px that the jQuery UI chrome has in height in addition to the height
// of an equivalent "classic" Morebits.simpleWindow
if (parseInt(getComputedStyle($(this.content).dialog("widget")[0], null).height, 10) > window.innerHeight) {
$(this.content).dialog("option", "height", window.innerHeight - 2).dialog("option", "position", "top");
Line 4,069 ⟶ 3,783:
}
$(this.content).dialog("widget").find(".morebits-dialog-content")[0].style.maxHeight = parseInt(this.height - 30, 10) + "px";
return this;
},
// Sets the content of the dialog to the given element node, usually from rendering a Morebits.quickForm.
// Re-enumerates the footer buttons, but leaves the footer links as they are.
// Be sure to call this at least once before the dialog is displayed...
setContent: function( content ) {
this.purgeContent();
this.addContent( content );
return this;
},
addContent: function( content ) {
this.content.appendChild( content );
Line 4,097 ⟶ 3,801:
var thisproxy = this;
$(this.content).find('input[type="submit"], button[type="submit"]').each(function(key, value) {
value.style.display = "none";
var button = document.createElement("button");
button.textContent = (value.hasAttribute("value") ? value.getAttribute("value") : (value.textContent ? value.textContent : "Submit Query"));
// here is an instance of cheap coding, probably a memory-usage hit in using a closure here
button.addEventListener("click", function() { value.click(); }, false);
thisproxy.buttons.push(button);
});
// remove all buttons from the button pane and re-add them
if (this.buttons.length > 0) {
Line 4,110 ⟶ 3,814:
$(this.content).dialog("widget").find(".morebits-dialog-buttons")[0].setAttribute("data-empty", "data-empty"); // used by CSS
}
return this;
},
purgeContent: function() {
this.buttons = [];
Line 4,125 ⟶ 3,825:
this.content.removeChild( this.content.firstChild );
}
return this;
},
// Adds a link in the bottom-right corner of the dialog.
// This can be used to provide help or policy links.
// For example, Twinkle's CSD module adds a link to the CSD policy page,
// as well as a link to Twinkle's documentation.
addFooterLink: function( text, wikiPage ) {
var $footerlinks = $(this.content).dialog("widget").find(".morebits-dialog-footerlinks");
Line 4,151 ⟶ 3,846:
$footerlinks.append(link);
this.hasFooterLinks = true;
return this;
},
setModality: function( modal ) {
$(this.content).dialog("option", "modal", modal);
return this;
}
};
// Enables or disables all footer buttons on all Morebits.simpleWindows in the current page.
// This should be called with |false| when the button(s) become irrelevant (e.g. just before Morebits.status.init is called).
// This is not an instance method so that consumers don't have to keep a reference to the original
// Morebits.simpleWindow object sitting around somewhere. Anyway, most of the time there will only be one
// Morebits.simpleWindow open, so this shouldn't matter.
Morebits.simpleWindow.setButtonsEnabled = function( enabled ) {
$(".morebits-dialog-buttons button").prop("disabled", !enabled);
|