Content added Content deleted
mNo edit summary |
No edit summary |
||
Line 17: | Line 17: | ||
QueryOptions.prototype.addButtonGroup = function( param, fieldOptions, widgetOptions ) { |
QueryOptions.prototype.addButtonGroup = function( param, fieldOptions, widgetOptions ) { |
||
var toggleButtons = widgetOptions.items.map( function ( item ) { |
var toggleButtons = widgetOptions.items.map( function ( item ) { |
||
var btn = new OO.ui.ButtonOptionWidget({ |
var btn = new OO.ui.ButtonOptionWidget( { |
||
data: item.data, |
data: item.data, |
||
label: item.label |
label: item.label |
||
}); |
} ); |
||
return btn; |
return btn; |
||
Line 41: | Line 41: | ||
var selected = widget.findSelectedItems(); |
var selected = widget.findSelectedItems(); |
||
if ( Array.isArray(selected) ) { |
if ( Array.isArray( selected ) ) { |
||
return selected.map( function ( item ) { |
return selected.map( function ( item ) { |
||
return item.getData(); |
return item.getData(); |
||
Line 111: | Line 111: | ||
function SearchWidget( |
function SearchWidget( optionConfig, searchConfig ) { |
||
this.api = new mw.Api(); |
|||
this.fieldset = new OO.ui.FieldsetLayout(); |
this.fieldset = new OO.ui.FieldsetLayout(); |
||
this.submitButton = new OO.ui.ButtonInputWidget( { |
this.submitButton = new OO.ui.ButtonInputWidget( { |
||
label: 'Search', |
label: 'Search', |
||
Line 119: | Line 122: | ||
icon: 'search' |
icon: 'search' |
||
} ); |
} ); |
||
⚫ | |||
if ( |
if ( optionConfig.queryOptions ) { |
||
this.queryOptions = optionConfig.queryOptions; |
|||
this.fieldset.addItems( [ |
this.fieldset.addItems( [ |
||
optionConfig.queryOptions.fieldset |
|||
] ); |
] ); |
||
} |
} |
||
if ( |
if ( optionConfig.displayOptions ) { |
||
this.fieldset.addItems( [ |
this.fieldset.addItems( [ |
||
optionConfig.displayOptions.fieldset |
|||
] ); |
] ); |
||
} |
} |
||
Line 136: | Line 141: | ||
] ); |
] ); |
||
this.tableSelector = searchConfig.selector; |
|||
⚫ | |||
this.template = searchConfig.template; |
|||
⚫ | |||
⚫ | |||
} |
} |
||
SearchWidget.prototype.search = function() { |
|||
const params = []; |
|||
if ( this.queryOptions ) { |
|||
⚫ | |||
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 z = '{' + '{' + |
|||
this.template + '\n' + |
|||
params.join('\n') + |
|||
'\n}}'; |
|||
this.api.get({ |
|||
action: 'parse', |
|||
title: mw.config.get('wgPageName'), |
|||
prop: 'text', |
|||
text: z, |
|||
maxage: 3600, |
|||
smaxage: 3600 |
|||
}).done(function(results) { |
|||
$( this.tableSelector ) |
|||
.empty() |
|||
.append( $( results.parse.text['*'] ) ); |
|||
$( this.tableSelector + ' table' ).tablesorter(); |
|||
⚫ | |||
}; |
|||
Object.preventExtensions( QueryOptions ); |
Object.preventExtensions( QueryOptions ); |
Revision as of 23:41, 21 December 2021
( function ( $, mw, psw ) {
'use strict';
/*
* QueryOptions
*/
function QueryOptions() {
this.queryParams = {};
this.getters = {};
this.fieldset = new OO.ui.FieldsetLayout( {
label: 'Query options',
items: []
} );
}
QueryOptions.prototype.addButtonGroup = function( param, fieldOptions, widgetOptions ) {
var toggleButtons = widgetOptions.items.map( function ( item ) {
var btn = new OO.ui.ButtonOptionWidget( {
data: item.data,
label: item.label
} );
return btn;
} );
var widget = new OO.ui.ButtonSelectWidget( {
items: toggleButtons,
align: widgetOptions.align || 'left',
multiselect: widgetOptions.multiselect || false
} );
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();
} );
} else if ( selected !== null ) {
return [ selected.getData() ];
}
return [];
};
};
QueryOptions.prototype.addCheckbox = 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.addCheckboxMulti = function( param, fieldOptions, widgetOptions ) {
};
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.fieldset = new OO.ui.FieldsetLayout( {
label: 'Display options',
items: []
} );
}
DisplayOptions.prototype.addColumnSelector = function( columns ) {
};
DisplayOptions.prototype.getOptions = function() {
};
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 );
if ( optionConfig.queryOptions ) {
this.queryOptions = optionConfig.queryOptions;
this.fieldset.addItems( [
optionConfig.queryOptions.fieldset
] );
}
if ( 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 z = '{' + '{' +
this.template + '\n' +
params.join('\n') +
'\n}}';
this.api.get({
action: 'parse',
title: mw.config.get('wgPageName'),
prop: 'text',
text: z,
maxage: 3600,
smaxage: 3600
}).done(function(results) {
$( this.tableSelector )
.empty()
.append( $( results.parse.text['*'] ) );
$( this.tableSelector + ' table' ).tablesorter();
});
};
Object.preventExtensions( QueryOptions );
Object.preventExtensions( DisplayOptions );
Object.preventExtensions( SearchWidget );
psw.db = {
QueryOptions: QueryOptions,
DisplayOptions: DisplayOptions,
SearchWidget: SearchWidget
};
} )( jQuery, mediaWiki, pswiki );