MediaWiki:Gadget-smwlistsfull-core.js
Jump to navigation
Jump to search
After saving, you may need to bypass your browser's cache to see the changes. For further information, see Wikipedia:Bypass your cache.
- In most Windows and Linux browsers: Hold down Ctrl and press F5.
- In Safari: Hold down ⇧ Shift and click the Reload button.
- In Chrome and Firefox for Mac: Hold down both ⌘ Cmd+⇧ Shift and press R.
"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>