Toggle menu
Toggle personal menu
Not logged in
Your IP address will be publicly visible if you make any edits.

MediaWiki:Gadget-database-util.js: Difference between revisions

MediaWiki interface page
Content added Content deleted
mNo edit summary
(added more button input options)
 
(26 intermediate revisions by the same user not shown)
Line 1: Line 1:
( function ( $, mw ) {
( function ( $, mw, psw ) {
'use strict';
'use strict';

/*
* QueryOptions
*/
function QueryOptions () {
this.queryParams = {};
this.getters = {};
this.fieldset = new OO.ui.FieldsetLayout( {
label: 'Query options',
items: []
} );
}
QueryOptions.prototype.addButtonMultiInput = function ( param, fieldOptions, widgetOptions ) {
mw.loader.using( ['oojs-ui-core', 'oojs-ui-widgets'] ).done( function() {
var toggleButtons = widgetOptions.items.map( function ( item ) {
var btn = new OO.ui.ButtonOptionWidget( {
/*
data: item.data || item.label,
* QueryOptions
label: item.label
*/
} );
function QueryOptions() {
this.queryParams = {};
this.paramWidgets = {};
return btn;
this.fieldset = new OO.ui.FieldsetLayout( {
} );
label: 'Query options',
items: []
var widget = new OO.ui.ButtonSelectWidget( {
items: toggleButtons,
align: widgetOptions.align || 'left',
multiselect: widgetOptions.multiselect || false
} );
if ( Array.isArray( widgetOptions.selected ) ) {
widgetOptions.selected.forEach( function ( label ) {
widget.selectItemByLabel( label );
} );
} );
} else if ( typeof widgetOptions.selected === 'string' ) {
widget.selectItemByLabel( widgetOptions.selected );
}
}
this.fieldset.addItems( [
QueryOptions.prototype.addButtonGroup = function( param, fieldOptions, widgetOptions ) {
new OO.ui.FieldLayout( widget, {
var toggleButtons = widgetOptions.items.map( function ( item ) {
label: fieldOptions.label,
var btn = new OO.ui.ButtonOptionWidget({
align: fieldOptions.align || 'top'
data: item.data,
} )
label: item.label
});
] );
this.getters[ param ] = function () {
return btn;
var selected = widget.findSelectedItems();
} );
var widget = new OO.ui.ButtonSelectWidget( {
items: toggleButtons,
align: widgetOptions.align || 'left'
} );
if ( Array.isArray( selected ) ) {
this.fieldset.addItems( [
return selected
new OO.ui.FieldLayout( widget, {
.map( function ( item ) {
label: fieldOptions.label,
return item.getData();
align: fieldOptions.align || 'top'
} )
} )
.join(widgetOptions.delimiter || ',');
] );
} else if ( selected !== null ) {
return selected.getData();
}
return '';
};
};
};
QueryOptions.prototype.addCheckboxInput = function ( param, fieldOptions, widgetOptions ) {
var widget = new OO.ui.CheckboxInputWidget( {
selected: widgetOptions.selected || false
} );
this.fieldset.addItems( [
new OO.ui.FieldLayout( widget, {
label: fieldOptions.label,
align: fieldOptions.align || 'inline'
} )
] );
this.getters[ param ] = function () {
QueryOptions.prototype.addButtonMultiselect = function( param, fieldOptions, widgetOptions ) {
var selected = widget.isSelected();
this.queryParams[param] = new pswiki.polyfill.Set();
if ( selected ) {
var paramVal = this.queryParams[param];
return widgetOptions.dataTrue;
}
var toggleButtons = widgetOptions.items.map( function( item ) {
var btn = new OO.ui.ToggleButtonWidget( {
label: item.label,
value: item.value
} );
btn.on( 'change', function( selected ) {
if ( selected ) {
paramVal.add( item.label );
} else {
paramVal.delete( item.label );
}
console.log(paramVal.values());
} );
if ( item.value ) {
paramVal.add( item.label );
}
return btn;
} );
var widget = new OO.ui.ButtonGroupWidget( {
items: toggleButtons,
align: widgetOptions.align || 'left'
} );
this.paramWidgets[ param ] = widget;
this.fieldset.addItems( [
new OO.ui.FieldLayout( widget, {
label: fieldOptions.label,
align: fieldOptions.align || 'top'
} ),
] );
return widgetOptions.dataFalse;
};
};
};
QueryOptions.prototype.addCheckboxMultiInput = function ( param, fieldOptions, widgetOptions ) {
};
QueryOptions.prototype.addCheckboxMultiselect = function( param, fieldOptions, widgetOptions ) {
QueryOptions.prototype.addDateInput = function ( param, fieldOptions, widgetOptions ) {
var widget = new mw.widgets.DateInputWidget( {
type: 'date'
} );
this.fieldset.addItems( [
new OO.ui.FieldLayout( widget, {
label: fieldOptions.label,
align: fieldOptions.align || 'top'
} )
] );
this.getters[ param ] = function () {
return widget.getValue();
};
};
};
QueryOptions.prototype.addNumberInput = function ( param, fieldOptions, widgetOptions ) {
var widget = new OO.ui.NumberInputWidget( {
min: widgetOptions.min,
max: widgetOptions.max,
buttonStep: widgetOptions.buttonStep,
pageStep: widgetOptions.pageStep,
showButtons: widgetOptions.showButtons
} );
this.fieldset.addItems( [
QueryOptions.prototype.getOptions = function() {
new OO.ui.FieldLayout( widget, {
var opts = {};
label: fieldOptions.label,
align: fieldOptions.align || 'top'
return opts;
} )
] );
this.getters[ param ] = function () {
return widget.getValue();
};
};
};
QueryOptions.prototype.getOptions = function () {
var opts = {};
for ( var key in this.getters ) {
/*
opts[ key ] = this.getters[ key ]();
* DisplayOptions
*/
function DisplayOptions() {
this.displayOptions = {};
this.fieldset = new OO.ui.FieldsetLayout( {
label: 'Display options',
items: []
} );
}
}
return opts;
DisplayOptions.prototype.addColumnSelector = function( columns ) {
};
/*
* DisplayOptions
*/
function DisplayOptions () {
this.displayOptions = {};
this.getters = {};
this.fieldset = new OO.ui.FieldsetLayout( {
};
label: 'Display options',
items: []
} );
}
DisplayOptions.prototype.addColumnSelector = function ( columns, fieldOptions ) {
var items = columns.map( function (column) {
return new OO.ui.CheckboxMultioptionWidget( {
data: column.name,
selected: column.selected,
label: column.label
} );
} );
var widget = new OO.ui.CheckboxMultiselectWidget( {
DisplayOptions.prototype.getOptions = function() {
items: items
} );
this.fieldset.addItems( [
new OO.ui.FieldLayout( widget, {
label: 'Visible columns',
align: fieldOptions.align || 'top'
} )
] );
this.getters[ 'columns' ] = function () {
return widget.findSelectedItemsData();
};
};
};
DisplayOptions.prototype.getOptions = function () {
var opts = {};
for ( var key in this.getters ) {
opts[ key ] = this.getters[ key ]();
}
return opts;
function SearchWidget( config ) {
};
this.fieldset = new OO.ui.FieldsetLayout();
this.submitButton = new OO.ui.ButtonInputWidget( {
label: 'Search',
function SearchWidget ( optionConfig, searchConfig ) {
flags: [ 'primary', 'progressive' ],
this.api = new mw.Api();
align: 'left',
icon: 'search'
this.fieldset = new OO.ui.FieldsetLayout();
} );
this.submitButton = new OO.ui.ButtonInputWidget( {
if ( config.queryOptions ) {
label: 'Search',
this.fieldset.addItems( [
flags: [ 'primary', 'progressive' ],
config.queryOptions.fieldset
] );
align: 'left',
icon: 'search'
}
} );
this.submitButton.on( 'click', this.search.bind( this ) );
if ( config.displayOptions ) {
this.fieldset.addItems( [
if ( optionConfig.queryOptions ) {
config.displayOptions.fieldset
this.queryOptions = optionConfig.queryOptions;
] );
}
this.fieldset.addItems( [
this.fieldset.addItems( [
optionConfig.queryOptions.fieldset
new OO.ui.FieldLayout( this.submitButton )
] );
] );
}
}
if ( optionConfig.displayOptions ) {
Object.preventExtensions( QueryOptions );
this.displayOptions = optionConfig.displayOptions;
Object.preventExtensions( DisplayOptions );
this.fieldset.addItems( [
Object.preventExtensions( SearchWidget );
optionConfig.displayOptions.fieldset
] );
}
this.fieldset.addItems( [
window.pswiki.dbUtil = {
new OO.ui.FieldLayout( this.submitButton )
QueryOptions: QueryOptions,
] );
DisplayOptions: DisplayOptions,
SearchWidget: SearchWidget
this.tableSelector = searchConfig.selector;
};
this.template = searchConfig.template;
}
SearchWidget.prototype.search = function () {
const params = [];
if ( this.queryOptions ) {
const options = this.queryOptions.getOptions();
for ( var key in options ) {
params.push( '|' + key + ' = ' + options[ key ] );
}
}
if ( this.displayOptions ) {
const options = this.displayOptions.getOptions();
for ( var key in options ) {
params.push( '|' + key + ' = ' + options[ key ] );
}
}

var text = '{' + '{' +
this.template + '\n' +
params.join('\n') +
'\n}}';
console.log( text );
var selector = this.tableSelector;
$( selector ).html( 'Searching...' );
this.api.get( {
action: 'parse',
title: mw.config.get( 'wgPageName' ),
prop: 'text',
text: text,
maxage: 3600,
smaxage: 3600
} ).done( function( results ) {
$( selector ).empty()
.append( $( results.parse.text[ '*' ] ) );
$( selector + ' table' ).tablesorter();
} );
};
Object.preventExtensions( QueryOptions );
Object.preventExtensions( DisplayOptions );
Object.preventExtensions( SearchWidget );
psw.db = {
QueryOptions: QueryOptions,
DisplayOptions: DisplayOptions,
SearchWidget: SearchWidget
};
} )( jQuery, mediaWiki, pswiki );
} );
} )( jQuery, mediaWiki );

Latest revision as of 03:54, 14 April 2023

( function ( $, mw, psw ) {
	'use strict';

	/*
	 * QueryOptions
	 */
	function QueryOptions () {
		this.queryParams = {};
		this.getters = {};
		
		this.fieldset = new OO.ui.FieldsetLayout( {
		    label: 'Query options',
		    items: []
		} );
	}
	
	QueryOptions.prototype.addButtonMultiInput = function ( param, fieldOptions, widgetOptions ) {
		var toggleButtons = widgetOptions.items.map( function ( item ) {
			var btn = new OO.ui.ButtonOptionWidget( {
				data: item.data || item.label,
				label: item.label
			} );
			
			return btn;
		} );
			
		var widget = new OO.ui.ButtonSelectWidget( {
			items: toggleButtons,
			align: widgetOptions.align || 'left',
			multiselect: widgetOptions.multiselect || false
		} );
		
		if ( Array.isArray( widgetOptions.selected ) ) {
			widgetOptions.selected.forEach( function ( label ) {
				widget.selectItemByLabel( label );
			} );
		} else if ( typeof widgetOptions.selected === 'string' ) {
			widget.selectItemByLabel( widgetOptions.selected );
		}
		
		this.fieldset.addItems( [
			new OO.ui.FieldLayout( widget, {
				label: fieldOptions.label,
				align: fieldOptions.align || 'top'
			} )
		] );
		
		this.getters[ param ] = function () {
			var selected = widget.findSelectedItems();
			
			if ( Array.isArray( selected ) ) {
				return selected
					.map( function ( item ) { 
						return item.getData();
					} )
					.join(widgetOptions.delimiter || ',');
			} else if ( selected !== null ) {
				return selected.getData();
			}
			
			return '';
		};
	};
	
	QueryOptions.prototype.addCheckboxInput = function ( param, fieldOptions, widgetOptions ) {
		var widget = new OO.ui.CheckboxInputWidget( {
	        selected: widgetOptions.selected || false
		} );
		
		this.fieldset.addItems( [
			new OO.ui.FieldLayout( widget, {
				label: fieldOptions.label,
				align: fieldOptions.align || 'inline'
			} )
		] );
		
		this.getters[ param ] = function () {
			var selected = widget.isSelected();
			
			if ( selected ) {
				return widgetOptions.dataTrue;
			}
			
			return widgetOptions.dataFalse;
		};
	};
	
	QueryOptions.prototype.addCheckboxMultiInput = function ( param, fieldOptions, widgetOptions ) {
		
	};
	
	QueryOptions.prototype.addDateInput = function ( param, fieldOptions, widgetOptions ) {
		var widget = new mw.widgets.DateInputWidget( {
			type: 'date'
		} );
		
		this.fieldset.addItems( [
			new OO.ui.FieldLayout( widget, {
				label: fieldOptions.label,
				align: fieldOptions.align || 'top'
			} )
		] );
		
		this.getters[ param ] = function () {
			return widget.getValue();
		};
	};
	
	QueryOptions.prototype.addNumberInput = function ( param, fieldOptions, widgetOptions ) {
		var widget = new OO.ui.NumberInputWidget( {
			min: widgetOptions.min,
			max: widgetOptions.max,
			buttonStep: widgetOptions.buttonStep,
			pageStep: widgetOptions.pageStep,
			showButtons: widgetOptions.showButtons
		} );
		
		this.fieldset.addItems( [
			new OO.ui.FieldLayout( widget, {
				label: fieldOptions.label,
				align: fieldOptions.align || 'top'
			} )
		] );
		
		this.getters[ param ] = function () {
			return widget.getValue();
		};
	};
	
	QueryOptions.prototype.getOptions = function () {
		var opts = {};
		
		for ( var key in this.getters ) {
			opts[ key ] = this.getters[ key ]();
		}
		
		return opts;
	};
	
	/*
	 * DisplayOptions
	 */
	function DisplayOptions () {
		this.displayOptions = {};
		this.getters = {};
		
		this.fieldset = new OO.ui.FieldsetLayout( {
		    label: 'Display options',
		    items: []
		} );
	}
	
	DisplayOptions.prototype.addColumnSelector = function ( columns, fieldOptions ) {
		var items = columns.map( function (column) {
			return new OO.ui.CheckboxMultioptionWidget( {
				data: column.name,
				selected: column.selected,
				label: column.label
			} );
		} );
		
		var widget = new OO.ui.CheckboxMultiselectWidget( {
			items: items
		} );
		
		this.fieldset.addItems( [
			new OO.ui.FieldLayout( widget, {
				label: 'Visible columns',
				align: fieldOptions.align || 'top'
			} )
		] );
		
		this.getters[ 'columns' ] = function () {
			return widget.findSelectedItemsData();
		};
	};
	
	DisplayOptions.prototype.getOptions = function () {
		var opts = {};
		
		for ( var key in this.getters ) {
			opts[ key ] = this.getters[ key ]();
		}
		
		return opts;
	};
	
	
	function SearchWidget ( optionConfig, searchConfig ) {
		this.api = new mw.Api();
		
		this.fieldset = new OO.ui.FieldsetLayout();
		
		this.submitButton = new OO.ui.ButtonInputWidget( { 
			label: 'Search',
			flags: [ 'primary', 'progressive' ],
			align: 'left',
			icon: 'search'
		} );
		this.submitButton.on( 'click', this.search.bind( this ) );
		
		if ( optionConfig.queryOptions ) {
			this.queryOptions = optionConfig.queryOptions;
			this.fieldset.addItems( [
				optionConfig.queryOptions.fieldset
			] );
		}
		
		if ( optionConfig.displayOptions ) {
			this.displayOptions = optionConfig.displayOptions;
			this.fieldset.addItems( [
				optionConfig.displayOptions.fieldset
			] );
		}
		
		this.fieldset.addItems( [
	    	new OO.ui.FieldLayout( this.submitButton )
		] );
		
		this.tableSelector = searchConfig.selector;
		this.template      = searchConfig.template;
	}
	
	SearchWidget.prototype.search = function () {
		const params = [];
		
		if ( this.queryOptions ) {
			const options = this.queryOptions.getOptions();
			for ( var key in options ) {
				params.push( '|' + key + ' = ' + options[ key ] );
			}
		}
		
		if ( this.displayOptions ) {
			const options = this.displayOptions.getOptions();
			for ( var key in options ) {
				params.push( '|' + key + ' = ' + options[ key ] );
			}
		}

		var text = '{' + '{' +
			this.template + '\n' + 
			params.join('\n') + 
			'\n}}';
		console.log( text );
			
		var selector = this.tableSelector;
		
		$( selector ).html( 'Searching...' );
		
		this.api.get( {
			action: 'parse',
			title: mw.config.get( 'wgPageName' ),
			prop: 'text',
			text: text,
			maxage: 3600,
			smaxage: 3600
		} ).done( function( results ) {
			$( selector ).empty()
				.append( $( results.parse.text[ '*' ] ) );
			$( selector + ' table' ).tablesorter();
		} );
	};
	
	Object.preventExtensions( QueryOptions );
	Object.preventExtensions( DisplayOptions );
	Object.preventExtensions( SearchWidget );
	
	psw.db = {
		QueryOptions: QueryOptions,
		DisplayOptions: DisplayOptions,
		SearchWidget: SearchWidget
	};
		
} )( jQuery, mediaWiki, pswiki );
Cookies help us deliver our services. By using our services, you agree to our use of cookies.