Content deleted Content added
. |
enough changed for minor version bump |
||
(38 intermediate revisions by the same user not shown) | |||
Line 1:
/* jshint esversion: 6, laxbreak: true, undef: true, eqnull: true, maxerr: 999 */
/* globals console, document, File, FileReader, fetch, window, $, mw, OO */
// <nowiki>
var setupCovery = function setupCovery() {
var SCRIPT = {
name: 'Covery',
version: '1.
ad: ' (using [[User:Evad37/Covery|Covery]])'
};
Line 123 ⟶ 125:
'Infobox computer game',
'Videogame infobox',
'Video game infobox',
'Infobox video game series',
'Infobox VG series',
'Infobox video game franchise'
];
var infoboxName = toSentanceCase(infobox.name);
Line 161 ⟶ 166:
canvas.width = width;
canvas.height = height;
ctx.drawImage(origImg, 0, 0, width, height);
Line 182 ⟶ 187:
};
/**
*
* @param {String} articleTitle
* @param {String} developer
* @param {String} publisher
* @param {String[]} platforms
*/
var makeDescriptionText = function makeDescriptionText(
articleTitle,
developer,
publisher,
platforms
) {
var platformsParams = platforms.reduce(function(params, platform) {
return params + '|' + platform;
}, '');
return (
'==Summary==\n{{Non-free use rationale video game cover\n' +
Line 195 ⟶ 215:
developer +
'\n}}\n' +
'==Licensing==\n{{Non-free video game cover
platformsParams +
'}}'
);
};
Line 333 ⟶ 355:
.then(function(revision) {
return $.when(
updateWikitext(revision.wikitext, infobox,
revision.timestamp
);
Line 416 ⟶ 438:
this.content = new OO.ui.FieldsetLayout();
this.fileSelect = new OO.ui.SelectFileWidget(
droppable: true,
showDropTarget: true,
// thumbnailSizeLimit: 0,
$element: $("<div style='background: #eee;'>")
});
// this.fileSelect.$element
// .find(".oo-ui-selectFileWidget-dropTarget").css({"height":"auto"});
// this.fileSelect.$element
// .find(".oo-ui-selectFileWidget-thumbnail").css({"display":"none"});
// this.fileSelect.$element
// .find(".oo-ui-selectFileInputWidget-info").css({"margin":"0"});
this.urlInput = new OO.ui.TextInputWidget({
type: 'url',
Line 427 ⟶ 461:
this.developerInput = new OO.ui.TextInputWidget({ required: true });
this.publisherInput = new OO.ui.TextInputWidget({ required: true });
this.platformInput = new OO.ui.MenuTagMultiselectWidget({
inputPosition: 'inline',
allowDisplayInvalidTags: true,
allowArbitrary: true
});
this.fileSelectField = new OO.ui.FieldLayout(this.fileSelect, {
label: 'Upload a file...',
align: '
});
this.fileSelect.field = this.fileSelectField;
Line 444 ⟶ 483:
this.titleInputField = new OO.ui.FieldLayout(this.titleInput, {
label: 'File name',
align: '
});
this.titleInputField.$element
.find(".oo-ui-fieldLayout-messages").css({"margin-top":"2em"}); // prevent errors overlapping input
this.captionInputField = new OO.ui.FieldLayout(this.captionInput, {
label: 'Caption',
Line 460 ⟶ 501:
this.publisherInputField = new OO.ui.FieldLayout(this.publisherInput, {
label: 'Publisher',
align: 'left'
});
this.platformInputField = new OO.ui.FieldLayout(this.platformInput, {
label: 'Platform(s)',
align: 'left'
});
Line 465 ⟶ 510:
this.content.addItems([
this.fileSelectField,
// this.urlInputField,
this.titleInputField,
//this.imagePreviewField,
this.captionInputField,
this.altTextInputField,
this.developerInputField,
this.publisherInputField,
this.platformInputField
]);
Line 511 ⟶ 557:
{ change: 'onRequiredInputChange' }
);
this.platformInput.connect(
this,
{ add: 'onPlatformInputAdd' }
);
(function(self) {
self.platformInput.$element.find('input').on('blur', function() {
self.onPlatformInputBlur.call(self);
});
})(this);
};
Line 531 ⟶ 586:
self.updateSize();
widgetUsed.popPending();
if (widgetUsed.setIndicator
widgetUsed.setIndicator('required');
}
otherWidget.setDisabled(false);
if (otherWidget.setIndicator
otherWidget.setIndicator(null);
}
self.titleInput.setValue(fileName);
self.onRequiredInputChange();
Line 543 ⟶ 602:
self.resizedFile = null;
widgetUsed.popPending();
if (widgetUsed.setIndicator
widgetUsed.setIndicator('clear');
}
widgetUsed.field.setErrors([errorMessage]);
otherWidget.setDisabled(false);
if (otherWidget.setIndicator
otherWidget.setIndicator(null);
}
self.onRequiredInputChange();
}
Line 552 ⟶ 615:
};
CoveryDialog.prototype.onFileSelectChange = function(
var file = files && files[0];
if (!file || !file.name) {
return;
}
this.onFileChosen(file, file.name, this.fileSelect, this.urlInput);
};
Line 566 ⟶ 633:
return;
}
var filePromise = fetch(value, {mode: 'no-cors'}).then(function(result) {
return result.blob();
});
Line 575 ⟶ 642:
CoveryDialog.prototype.onTitleInputFlag = function(flag) {
if (flag.invalid === true) {
if (this.titleInput.getValue().length) {
this.titleInputField.setErrors(['Invalid file name']);
}
this.actions.setAbilities({
upload: false
Line 581 ⟶ 651:
this.onRequiredInputChange();
}
};
CoveryDialog.prototype.checkMimes = function() {
var mimeLookup = {
'.bmp': 'image/bmp',
'.gif': 'image/gif',
'.jpeg': 'image/jpeg',
'.jpg': 'image/jpeg',
'.png': 'image/png',
'.svg': 'image/svg+xml',
'.tif': 'image/tiff',
'.tiff': 'image/tiff'
};
var fileMime = (this.resizedFile && this.resizedFile.type) || '';
var titleParts = this.titleInput
.getValue()
.toLowerCase()
.match(/.*(\..*)$/, '$1');
var titleExtension = titleParts && titleParts[1];
var impliedTitleMime = mimeLookup[titleExtension] || '';
return fileMime === impliedTitleMime;
};
Line 588 ⟶ 679:
$.when((change && change.titleIsValid) || this.titleInput.getValidity()).then(
function() {
// remove any old title input errors
self.titleInputField.setErrors([]);
// check file mime matches title mime
var titleHasCorrectExtension = self.checkMimes();
if (!titleHasCorrectExtension && self.resizedFile) {
self.titleInputField.setErrors([
'Invalid file extension (file is a ' +
self.resizedFile.type
.replace('image/', '')
.replace(/\+.*$/, '')
.toUpperCase() +
' image)'
]);
}
var requirementsMet =
!self.fileSelect.isPending() &&
Line 593 ⟶ 699:
!!self.resizedFile &&
!!self.titleInput.getValue().length &&
titleHasCorrectExtension &&
!!self.developerInput.getValue().length &&
!!self.publisherInput.getValue().length;
Line 600 ⟶ 707:
},
function() {
if (self.titleInput.getValue().length) {
self.titleInputField.setErrors(['Invalid file name']);
}
self.actions.setAbilities({
upload: false
Line 605 ⟶ 715:
}
);
};
CoveryDialog.prototype.onPlatformInputAdd = function(item) {
this.api
.get({
action: 'query',
format: 'json',
titles: 'Category:' + item.data + ' game covers'
})
.then(function(response) {
return $.map(response.query.pages, function(page) {
return page.missing !== '';
})[0];
})
.then(function(isValid) {
item.toggleValid(isValid);
});
};
CoveryDialog.prototype.onPlatformInputBlur = function() {
this.platformInput.doInputEnter();
};
Line 646 ⟶ 777:
});
});
var self = this;
data.api
.get({
action: 'query',
format: 'json',
list: 'categorymembers',
cmtitle: 'Category:Video game covers',
cmprop: 'title',
cmtype: 'subcat',
cmlimit: 'max'
})
.then(function(response) {
return response.query.categorymembers
.map(function(category) {
return category.title;
})
.map(function(categoryTitle) {
return {
data: categoryTitle.replace(/Category\:(.+) game covers/, '$1')
};
});
})
.then(function(platforms) {
self.platformInput.addOptions(platforms);
});
}, this);
};
Line 677 ⟶ 833:
var fileTitle = mw.Title.newFromFileName(this.titleInput.getValue());
return new OO.ui.Process(function() {
var platformValues = this.platformInput.getItems().map(function(item) {
return item.getData();
});
return (
this.uploaded ||
Line 685 ⟶ 844:
new mw.Title(this.pageName),
this.developerInput.getValue(),
this.publisherInput.getValue(),
platformValues
),
fileTitle
Line 839 ⟶ 999:
function(data) {
if (data && data.sucess) {
window.___location.reload();
}
},
Line 925 ⟶ 1,085:
) {
$.when(setupForTesting(), $.ready()).then(function() {
mw.loader.load('https://
$.getScript('https://
/* globals QUnit */
QUnit.module('Get parameters from a template');
QUnit.test('Single unnamed param', function(assert) {
Line 1,462 ⟶ 1,623:
'{{Videogame infobox}}',
'{{Video game infobox}}',
'{{Infobox video game series}}',
'{{Infobox VG series}}',
'{{Infobox video game franchise}}',
'{{infobox video game}}',
'{{infobox Arcade Game}}',
Line 1,480 ⟶ 1,644:
'{{infobox computer game}}',
'{{videogame infobox}}',
'{{video game infobox}}',
'{{infobox video game series}}',
'{{infobox VG series}}',
'{{infobox video game franchise}}'
];
infoboxesExpectedToPass.forEach(infobox => {
QUnit.test(infobox, function(assert) {
let infoboxObject = getInfoboxTemplate(infobox);
Line 1,488 ⟶ 1,655:
assert.deepEqual(checkedInfobox, infoboxObject, infobox + ' passes check');
});
});
QUnit.module('Infobox check (expected fail)');
var infoboxesExpectedToFail = [
Line 1,498 ⟶ 1,665:
'{{infobox foo}}'
];
infoboxesExpectedToFail.forEach(infobox => {
QUnit.test(infobox, function(assert) {
let infoboxObject = getInfoboxTemplate(infobox);
Line 1,505 ⟶ 1,672:
}, infobox + ' throws an error');
});
});
QUnit.module('Make infobox wikitext');
Line 1,524 ⟶ 1,691:
var wikitext1 = makeInfoboxWikitext(infobox1, newParams);
var expected1 = '{{Infobox Video Game\n| image = Foo.png\n}}';
var infobox2 = getInfoboxTemplate(
'{{Infobox Video Game\n| image = \n| developer = DEV\n}}' );
var wikitext2 = makeInfoboxWikitext(infobox2, newParams);
var expected2 =
'{{Infobox Video Game\n| image = Foo.png\n| developer = DEV\n}}'; assert.equal(wikitext1, expected1, 'Otherwise empty infobox');
assert.equal(wikitext2, expected2, 'With one other param');
});
QUnit.test('Override 1 empty param, 1 non-empty param', function(assert) {
var newParams = [
{ name: 'image', value: 'Foo.png' }, { name: 'developer', value: '[[DEV]]' } ];
var infobox1 = getInfoboxTemplate(
'{{Infobox Video Game\n| image = \n| developer = NotTheDev\n}}'
);
var wikitext1 = makeInfoboxWikitext(infobox1, newParams);
var expected1 =
'{{Infobox Video Game\n| image = Foo.png\n| developer = [[DEV]]\n}}'; var infobox2 = getInfoboxTemplate(
'{{Infobox Video Game\n| image = \n| publisher = PUB\n| developer = NotTheDev\n}}' );
var wikitext2 = makeInfoboxWikitext(infobox2, newParams);
var expected2 =
'{{Infobox Video Game\n| image = Foo.png\n| publisher = PUB\n| developer = [[DEV]]\n}}'; assert.equal(wikitext1, expected1, 'No other params');
assert.equal(wikitext2, expected2, 'With one other param');
});
QUnit.module('Make description text');
QUnit.test('No platforms', function(assert) {
var description = makeDescriptionText(new mw.Title('title'), 'dev', 'pub', []);
var expected =
'==Summary==\n{{Non-free use rationale video game cover\n' +
'| Article = Title\n' +
'| Use = Infobox\n' +
'| Publisher = pub\n' +
'| Developer = dev\n' +
'}}\n' +
'==Licensing==\n{{Non-free video game cover}}';
assert.equal(description, expected, 'No platforms');
});
QUnit.test('One platform', function(assert) {
var description = makeDescriptionText(new mw.Title('title'), 'dev', 'pub', [
'Platform'
]);
var expected =
'==Summary==\n{{Non-free use rationale video game cover\n' +
'| Article = Title\n' +
'| Use = Infobox\n' +
'| Publisher = pub\n' +
'| Developer = dev\n' +
'}}\n' +
'==Licensing==\n{{Non-free video game cover|Platform}}';
assert.equal(description, expected, 'One platform');
});
QUnit.test('Two platforms', function(assert) {
var description = makeDescriptionText(new mw.Title('title'), 'dev', 'pub', [
'Platform1',
'Platform2'
]);
var expected =
'==Summary==\n{{Non-free use rationale video game cover\n' +
'| Article = Title\n' +
'| Use = Infobox\n' +
'| Publisher = pub\n' +
'| Developer = dev\n' +
'}}\n' +
'==Licensing==\n{{Non-free video game cover|Platform1|Platform2}}';
assert.equal(description, expected, 'Two platforms');
});
}); // end of "get script QUnit"
Line 1,551 ⟶ 1,773:
'mediawiki.util',
'mediawiki.api',
'oojs-ui-core',
'oojs-ui-widgets',
|