Content added Content deleted
mNo edit summary |
(added more button input options) |
||
(13 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
( function ( $, mw, psw ) { |
|||
'use strict'; |
|||
/* |
/* |
||
* QueryOptions |
* QueryOptions |
||
*/ |
*/ |
||
function QueryOptions() { |
function QueryOptions () { |
||
this.queryParams = {}; |
this.queryParams = {}; |
||
this.getters = {}; |
this.getters = {}; |
||
Line 15: | Line 15: | ||
} |
} |
||
QueryOptions.prototype. |
QueryOptions.prototype.addButtonMultiInput = 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 || item.label, |
||
label: item.label |
label: item.label |
||
} ); |
} ); |
||
Line 30: | Line 30: | ||
multiselect: widgetOptions.multiselect || false |
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( [ |
this.fieldset.addItems( [ |
||
Line 38: | Line 46: | ||
] ); |
] ); |
||
this.getters[ param ] = function() { |
this.getters[ param ] = function () { |
||
var selected = widget.findSelectedItems(); |
var selected = widget.findSelectedItems(); |
||
if ( Array.isArray( selected ) ) { |
if ( Array.isArray( selected ) ) { |
||
return selected |
return selected |
||
.map( function ( item ) { |
|||
⚫ | |||
return item.getData(); |
|||
} ) |
|||
.join(widgetOptions.delimiter || ','); |
|||
} else if ( selected !== null ) { |
} else if ( selected !== null ) { |
||
return |
return selected.getData(); |
||
} |
} |
||
return |
return ''; |
||
}; |
}; |
||
}; |
}; |
||
QueryOptions.prototype. |
QueryOptions.prototype.addCheckboxInput = function ( param, fieldOptions, widgetOptions ) { |
||
var widget = new OO.ui.CheckboxInputWidget( { |
var widget = new OO.ui.CheckboxInputWidget( { |
||
selected: widgetOptions.selected || false |
selected: widgetOptions.selected || false |
||
Line 65: | Line 75: | ||
] ); |
] ); |
||
this.getters[ param ] = function() { |
this.getters[ param ] = function () { |
||
var selected = widget.isSelected(); |
var selected = widget.isSelected(); |
||
Line 76: | Line 86: | ||
}; |
}; |
||
QueryOptions.prototype. |
QueryOptions.prototype.addCheckboxMultiInput = function ( param, fieldOptions, widgetOptions ) { |
||
}; |
}; |
||
QueryOptions.prototype. |
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 () { |
|||
⚫ | |||
}; |
|||
}; |
|||
⚫ | |||
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 = {}; |
var opts = {}; |
||
Line 93: | Line 141: | ||
* DisplayOptions |
* DisplayOptions |
||
*/ |
*/ |
||
function DisplayOptions() { |
function DisplayOptions () { |
||
this.displayOptions = {}; |
this.displayOptions = {}; |
||
this.getters = {}; |
|||
this.fieldset = new OO.ui.FieldsetLayout( { |
this.fieldset = new OO.ui.FieldsetLayout( { |
||
Line 102: | Line 151: | ||
} |
} |
||
DisplayOptions.prototype.addColumnSelector = function( columns ) { |
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() { |
DisplayOptions.prototype.getOptions = function () { |
||
var opts = {}; |
|||
for ( var key in this.getters ) { |
|||
opts[ key ] = this.getters[ key ](); |
|||
} |
|||
return opts; |
|||
}; |
}; |
||
function SearchWidget( optionConfig, searchConfig ) { |
function SearchWidget ( optionConfig, searchConfig ) { |
||
this.api = new mw.Api(); |
this.api = new mw.Api(); |
||
Line 132: | Line 208: | ||
if ( optionConfig.displayOptions ) { |
if ( optionConfig.displayOptions ) { |
||
this.displayOptions = optionConfig.displayOptions; |
|||
this.fieldset.addItems( [ |
this.fieldset.addItems( [ |
||
optionConfig.displayOptions.fieldset |
optionConfig.displayOptions.fieldset |
||
Line 145: | Line 222: | ||
} |
} |
||
SearchWidget.prototype.search = function() { |
SearchWidget.prototype.search = function () { |
||
const params = []; |
const params = []; |
||
Line 169: | Line 246: | ||
var selector = this.tableSelector; |
var selector = this.tableSelector; |
||
$( selector ).html( 'Searching...' ); |
|||
this.api.get( { |
this.api.get( { |
||
Line 179: | Line 258: | ||
} ).done( function( results ) { |
} ).done( function( results ) { |
||
$( selector ).empty() |
$( selector ).empty() |
||
.append( $( results.parse.text['*'] ) ); |
.append( $( results.parse.text[ '*' ] ) ); |
||
$( selector + ' table' ).tablesorter(); |
$( selector + ' table' ).tablesorter(); |
||
} ); |
} ); |
||
Line 188: | Line 267: | ||
Object.preventExtensions( SearchWidget ); |
Object.preventExtensions( SearchWidget ); |
||
psw.db = { |
|||
QueryOptions: QueryOptions, |
QueryOptions: QueryOptions, |
||
DisplayOptions: DisplayOptions, |
DisplayOptions: DisplayOptions, |
||
Line 194: | Line 273: | ||
}; |
}; |
||
} )( jQuery, mediaWiki, pswiki ); |
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 );