MediaWiki:Gadget-smwlistsfull-core.js: Difference between revisions
Jump to navigation
Jump to search
Content added Content deleted
(Created page with "// <nowiki> var SMW_TEMPLATES = [ { name: 'Drop sources (without RDT)', template: 'Drop sources', short: 'drops' }, { name: 'Drop sources', template: 'Drop sources', short: 'dropsrdt', extra_params: '|incrdt=y' }, { name: 'Store locations', template: 'Store locations list', short: 'stores' }, { name: 'Products', template: 'Uses material list', short: 'products' }, { name: 'Facilities', template: 'Uses facility list', short: 'facilities' }, { name: 'Tools', template...") |
No edit summary |
||
Line 1: | Line 1: | ||
"use strict"; |
|||
// <nowiki> |
// <nowiki> |
||
var SMW_TEMPLATES = [ |
var SMW_TEMPLATES = [{ |
||
name: 'Drop sources (without RDT)', |
|||
template: 'Drop sources', |
|||
"short": 'drops' |
|||
{ name: 'Store locations', template: 'Store locations list', short: 'stores' }, |
|||
}, { |
|||
{ name: 'Products', template: 'Uses material list', short: 'products' }, |
|||
name: 'Drop sources', |
|||
{ name: 'Facilities', template: 'Uses facility list', short: 'facilities' }, |
|||
template: 'Drop sources', |
|||
"short": 'dropsrdt', |
|||
]; |
|||
extra_params: '|incrdt=y' |
|||
}, { |
|||
name: 'Store locations', |
|||
template: 'Store locations list', |
|||
"short": 'stores' |
|||
}, { |
|||
name: 'Products', |
|||
template: 'Uses material list', |
|||
"short": 'products' |
|||
}, { |
|||
name: 'Facilities', |
|||
template: 'Uses facility list', |
|||
"short": 'facilities' |
|||
}, { |
|||
name: 'Tools', |
|||
template: 'Uses tool list', |
|||
"short": 'tools' |
|||
}]; |
|||
function find_short(x) { |
function find_short(x) { |
||
return SMW_TEMPLATES.find(function (el) { |
|||
return el["short"] === x; |
|||
}); |
|||
} |
} |
||
var $dmlfForm, $dmlfOut, typeSelect, txtField, submitButton; |
var $dmlfForm, $dmlfOut, typeSelect, txtField, submitButton; |
||
function submit() { |
function submit() { |
||
var val = txtField.getValue(), |
|||
smw_type = typeSelect.findSelectedItem(); |
|||
val = String(val).trim(); |
|||
val = String(val).trim(); |
|||
if (val.length === 0) { |
|||
$dmlfOut.text('Please enter an item name').addClass('smw-list-error'); |
|||
$dmlfOut.text('Please enter an item name').addClass('smw-list-error'); |
|||
return; |
|||
return; |
|||
} |
|||
} |
|||
if (smw_type === null) { |
|||
if (smw_type === null) { |
|||
$dmlfOut.text('Please select a list type').addClass('smw-list-error'); |
|||
$dmlfOut.text('Please select a list type').addClass('smw-list-error'); |
|||
return; |
|||
return; |
|||
} |
|||
} |
|||
smw_type = smw_type.getData(); |
|||
smw_type = smw_type.getData(); |
|||
console.log(smw_type); |
|||
console.log(smw_type); |
|||
var smw_data = find_short(smw_type); |
|||
if (smw_data === undefined) { |
|||
$dmlfOut.text('Something went wrong, please refresh and try again (ERR: no smw_type)').addClass('smw-list-error'); |
|||
$dmlfOut.text('Something went wrong, please refresh and try again (ERR: no smw_type)').addClass('smw-list-error'); |
|||
return; |
|||
return; |
|||
} |
|||
} |
|||
$dmlfOut.text('Loading...').removeClass('smw-list-error'); |
|||
var extra_params = smw_data.extra_params === undefined ? '' : smw_data.extra_params; |
|||
$.getJSON('/api.php', { |
|||
format: 'json', |
|||
action: 'parse', |
|||
text: '{{' + smw_data.template + '|' + val + '|limit=10000' + extra_params + '}}', |
|||
title: val, |
|||
disablelimitreport: true, |
|||
contentmodel: 'wikitext' |
|||
}, function (data) { |
|||
var $html = $(data.parse.text['*']); |
|||
var $tbl = $html.find('table'); |
|||
if ($tbl.length < 1) { |
|||
$dmlfOut.addClass('smw-list-error').text('No results found for ' + val); |
|||
return; |
|||
} |
|||
} |
|||
var intro = '<div class="smw-list-intro">Showing all ' + ($tbl.find('tr').length - 1) + ' results.</div>'; |
|||
$dmlfOut.removeClass('smw-list-error').empty().append(intro, $tbl); |
|||
var uri = new mw.Uri(), |
|||
uri2 = new mw.Uri(); |
|||
uri.query = { page: val, type: smw_data.short }; |
|||
uri.query = { |
|||
uri2.fragment = uri.getQueryString(); |
|||
page: val, |
|||
window.history.replaceState(window.history.state, '', uri2.toString()); |
|||
type: smw_data["short"] |
|||
if ($tbl.hasClass('sortable')) { |
|||
}; |
|||
$tbl.tablesorter(); |
|||
uri2.fragment = uri.getQueryString(); |
|||
} |
|||
window.history.replaceState(window.history.state, '', uri2.toString()); |
|||
}).fail(function () { |
|||
if ($tbl.hasClass('sortable')) { |
|||
$dmlfOut.addClass('smw-list-error').text('Something went wrong when loading data') |
|||
$tbl.tablesorter(); |
|||
}); |
|||
} |
|||
}).fail(function () { |
|||
$dmlfOut.addClass('smw-list-error').text('Something went wrong when loading data'); |
|||
}); |
|||
} |
} |
||
function init() { |
function init() { |
||
if (!$('#smw-list-full').length) return; |
|||
$dmlfForm = $('<div>').addClass('smw-list-form'); |
|||
$dmlfOut = $('<div>').addClass('smw-list-output'); |
|||
var buttonoptions = []; |
|||
for (var i = 0; i < SMW_TEMPLATES.length; i++) { |
|||
var buttonoptions = []; |
|||
var d = SMW_TEMPLATES[i]; |
|||
for (var i=0; i<SMW_TEMPLATES.length; i++) { |
|||
buttonoptions.push(new OO.ui.ButtonOptionWidget({ |
|||
var d = SMW_TEMPLATES[i]; |
|||
label: d.name, |
|||
buttonoptions.push(new OO.ui.ButtonOptionWidget({ |
|||
data: d["short"] |
|||
label: d.name, |
|||
})); |
|||
data: d.short |
|||
} |
|||
})); |
|||
typeSelect = new OO.ui.ButtonSelectWidget({ |
|||
items: buttonoptions |
|||
} |
|||
}); |
|||
typeSelect = new OO.ui.ButtonSelectWidget({ |
|||
txtField = new OO.ui.TextInputWidget({ |
|||
items: buttonoptions |
|||
placeholder: 'Enter item name' |
|||
}); |
|||
}); |
|||
submitButton = new OO.ui.ButtonInputWidget({ |
|||
txtField = new OO.ui.TextInputWidget({ placeholder: 'Enter item name' }); |
|||
label: 'Submit' |
|||
submitButton = new OO.ui.ButtonInputWidget({ |
|||
}); |
|||
label: 'Submit' |
|||
submitButton.on('click', submit); |
|||
}); |
|||
txtField.on('enter', submit); |
|||
var fieldset = new OO.ui.FieldsetLayout(); |
|||
txtField.on('enter', submit); |
|||
fieldset.addItems([new OO.ui.FieldLayout(typeSelect, { |
|||
label: 'List to display', |
|||
var fieldset = new OO.ui.FieldsetLayout(); |
|||
align: 'left' |
|||
fieldset.addItems([ |
|||
}), new OO.ui.FieldLayout(txtField, { |
|||
label: 'Item name', |
|||
align: 'left' |
|||
submitButton |
|||
}), submitButton]); |
|||
]); |
|||
$dmlfForm.append(fieldset.$element); |
|||
$('#smw-list-full').empty().append($dmlfForm, $dmlfOut); |
|||
var uri = new mw.Uri(); |
|||
$('#smw-list-full').empty().append($dmlfForm, $dmlfOut); |
|||
if (typeof uri.fragment === 'string') { |
|||
var uri2 = new mw.Uri(uri.path + '?' + uri.fragment); |
|||
var qs = $.extend({}, uri.query, uri2.query); |
|||
if (typeof uri.fragment === 'string') { |
|||
if (qs.page && qs.type) { |
|||
var uri2 = new mw.Uri(uri.path + '?' + uri.fragment); |
|||
txtField.setValue(qs.page); |
|||
var qs = $.extend({}, uri.query, uri2.query); |
|||
typeSelect.selectItemByData(qs.type); |
|||
if (qs.page && qs.type) { |
|||
submit(); |
|||
txtField.setValue(qs.page); |
|||
} |
|||
typeSelect.selectItemByData(qs.type) |
|||
} |
|||
submit(); |
|||
} |
|||
} |
|||
} |
} |
||
$(init); |
$(init); |
||
// </nowiki> |
// </nowiki> |
Latest revision as of 12:06, 20 October 2024
"use strict";
// <nowiki>
var SMW_TEMPLATES = [{
name: 'Drop sources (without RDT)',
template: 'Drop sources',
"short": 'drops'
}, {
name: 'Drop sources',
template: 'Drop sources',
"short": 'dropsrdt',
extra_params: '|incrdt=y'
}, {
name: 'Store locations',
template: 'Store locations list',
"short": 'stores'
}, {
name: 'Products',
template: 'Uses material list',
"short": 'products'
}, {
name: 'Facilities',
template: 'Uses facility list',
"short": 'facilities'
}, {
name: 'Tools',
template: 'Uses tool list',
"short": 'tools'
}];
function find_short(x) {
return SMW_TEMPLATES.find(function (el) {
return el["short"] === x;
});
}
var $dmlfForm, $dmlfOut, typeSelect, txtField, submitButton;
function submit() {
var val = txtField.getValue(),
smw_type = typeSelect.findSelectedItem();
val = String(val).trim();
if (val.length === 0) {
$dmlfOut.text('Please enter an item name').addClass('smw-list-error');
return;
}
if (smw_type === null) {
$dmlfOut.text('Please select a list type').addClass('smw-list-error');
return;
}
smw_type = smw_type.getData();
console.log(smw_type);
var smw_data = find_short(smw_type);
if (smw_data === undefined) {
$dmlfOut.text('Something went wrong, please refresh and try again (ERR: no smw_type)').addClass('smw-list-error');
return;
}
$dmlfOut.text('Loading...').removeClass('smw-list-error');
var extra_params = smw_data.extra_params === undefined ? '' : smw_data.extra_params;
$.getJSON('/api.php', {
format: 'json',
action: 'parse',
text: '{{' + smw_data.template + '|' + val + '|limit=10000' + extra_params + '}}',
title: val,
disablelimitreport: true,
contentmodel: 'wikitext'
}, function (data) {
var $html = $(data.parse.text['*']);
var $tbl = $html.find('table');
if ($tbl.length < 1) {
$dmlfOut.addClass('smw-list-error').text('No results found for ' + val);
return;
}
var intro = '<div class="smw-list-intro">Showing all ' + ($tbl.find('tr').length - 1) + ' results.</div>';
$dmlfOut.removeClass('smw-list-error').empty().append(intro, $tbl);
var uri = new mw.Uri(),
uri2 = new mw.Uri();
uri.query = {
page: val,
type: smw_data["short"]
};
uri2.fragment = uri.getQueryString();
window.history.replaceState(window.history.state, '', uri2.toString());
if ($tbl.hasClass('sortable')) {
$tbl.tablesorter();
}
}).fail(function () {
$dmlfOut.addClass('smw-list-error').text('Something went wrong when loading data');
});
}
function init() {
if (!$('#smw-list-full').length) return;
$dmlfForm = $('<div>').addClass('smw-list-form');
$dmlfOut = $('<div>').addClass('smw-list-output');
var buttonoptions = [];
for (var i = 0; i < SMW_TEMPLATES.length; i++) {
var d = SMW_TEMPLATES[i];
buttonoptions.push(new OO.ui.ButtonOptionWidget({
label: d.name,
data: d["short"]
}));
}
typeSelect = new OO.ui.ButtonSelectWidget({
items: buttonoptions
});
txtField = new OO.ui.TextInputWidget({
placeholder: 'Enter item name'
});
submitButton = new OO.ui.ButtonInputWidget({
label: 'Submit'
});
submitButton.on('click', submit);
txtField.on('enter', submit);
var fieldset = new OO.ui.FieldsetLayout();
fieldset.addItems([new OO.ui.FieldLayout(typeSelect, {
label: 'List to display',
align: 'left'
}), new OO.ui.FieldLayout(txtField, {
label: 'Item name',
align: 'left'
}), submitButton]);
$dmlfForm.append(fieldset.$element);
$('#smw-list-full').empty().append($dmlfForm, $dmlfOut);
var uri = new mw.Uri();
if (typeof uri.fragment === 'string') {
var uri2 = new mw.Uri(uri.path + '?' + uri.fragment);
var qs = $.extend({}, uri.query, uri2.query);
if (qs.page && qs.type) {
txtField.setValue(qs.page);
typeSelect.selectItemByData(qs.type);
submit();
}
}
}
$(init);
// </nowiki>