MediaWiki:Common.js: Difference between revisions
Linkify 5th class as Italian 5; 540-R as MII 540 |
Soldier page upload box in Gallery section |
||
| Line 292: | Line 292: | ||
$.each(items, function (_, clause) { | $.each(items, function (_, clause) { | ||
$ul.append($('<li>').html(linkifyTeams(clause))); | $ul.append($('<li>').html(linkifyTeams(clause))); | ||
}); | |||
}); | |||
/** | |||
* Soldier page upload box (Gallery section) — profile photos and documents. | |||
*/ | |||
$(function () { | |||
if (mw.config.get('wgNamespaceNumber') !== 0 || !$('.soldier-infobox').length) { | |||
return; | |||
} | |||
var PHOTO_EXT = /\.(jpe?g|png|gif|webp)$/i; | |||
var DOC_EXT = /\.(pdf|docx?|txt|rtf|odt|tiff?)$/i; | |||
var ALLOWED = /\.(jpe?g|png|gif|webp|pdf|docx?|txt|rtf|odt|tiff?)$/i; | |||
var $galleryHeading = $('#Gallery').closest('h2'); | |||
if (!$galleryHeading.length) { | |||
$galleryHeading = $('h2').filter(function () { | |||
return $.trim($(this).text()).replace(/\[edit\]/i, '') === 'Gallery'; | |||
}).first(); | |||
} | |||
if (!$galleryHeading.length) { | |||
return; | |||
} | |||
var pageTitle = mw.config.get('wgPageName'); | |||
var loggedIn = !!mw.config.get('wgUserName'); | |||
var uploadUrl = mw.util.getUrl('Special:Upload') + '&wpRitchieSoldierPage=' + encodeURIComponent(pageTitle); | |||
var $box = $('<div>', { 'class': 'soldier-upload-box' }); | |||
$box.append($('<p>').append( | |||
$('<strong>').text('Upload for this soldier'), | |||
document.createTextNode(' — photos (infobox portrait) or documents (PDF, Word, etc.). You can also use '), | |||
$('<a>', { href: uploadUrl, text: 'Special:Upload' }), | |||
document.createTextNode('.') | |||
)); | |||
if (!loggedIn) { | |||
$box.append($('<p>', { 'class': 'soldier-upload-login' }).html( | |||
'<a href="' + mw.util.getUrl('Special:UserLogin', { returnto: pageTitle }) + '">Log in</a> to upload files.' | |||
)); | |||
$galleryHeading.after($box); | |||
return; | |||
} | |||
var $form = $('<form>', { 'class': 'soldier-upload-form' }); | |||
var $file = $('<input>', { type: 'file', name: 'ritchieUploadFile', 'class': 'soldier-upload-file' }); | |||
var $profile = $('<label>').append( | |||
$('<input>', { type: 'checkbox', name: 'ritchieProfile', checked: true }), | |||
' Use as profile photo (images only)' | |||
); | |||
var $gallery = $('<label>').append( | |||
$('<input>', { type: 'checkbox', name: 'ritchieGallery' }), | |||
' Add to Gallery (documents, or extra photos)' | |||
); | |||
var $status = $('<p>', { 'class': 'soldier-upload-status', text: '' }); | |||
var $submit = $('<button>', { type: 'submit', text: 'Upload file' }); | |||
$form.append($file, $profile, $gallery, $submit, $status); | |||
$box.append($form); | |||
$galleryHeading.after($box); | |||
function setStatus(msg, isError) { | |||
$status.text(msg).css('color', isError ? '#ba0000' : '#202122'); | |||
} | |||
function refreshChecks() { | |||
var name = ($file.val() || '').split(/[/\\]/).pop(); | |||
var isPhoto = PHOTO_EXT.test(name); | |||
var isDoc = DOC_EXT.test(name); | |||
$profile.toggle(isPhoto || !name); | |||
if (isDoc) { | |||
$profile.prop('checked', false); | |||
$gallery.prop('checked', true); | |||
} else if (isPhoto) { | |||
$profile.prop('checked', true); | |||
$gallery.prop('checked', false); | |||
} | |||
} | |||
$file.on('change', refreshChecks); | |||
$form.on('submit', function (evt) { | |||
evt.preventDefault(); | |||
var file = $file[0].files[0]; | |||
if (!file) { | |||
setStatus('Choose a file first.', true); | |||
return; | |||
} | |||
if (!ALLOWED.test(file.name)) { | |||
setStatus('File type not allowed.', true); | |||
return; | |||
} | |||
var isPhoto = PHOTO_EXT.test(file.name); | |||
var isDoc = DOC_EXT.test(file.name); | |||
var useProfile = $profile.prop('checked') && isPhoto; | |||
var addGallery = $gallery.prop('checked') || isDoc; | |||
if (!useProfile && !addGallery) { | |||
setStatus('Choose profile photo and/or Gallery.', true); | |||
return; | |||
} | |||
$submit.prop('disabled', true); | |||
setStatus('Uploading…'); | |||
var api = new mw.Api(); | |||
var fileText = (isDoc ? 'Document' : 'Portrait') + ' for [[' + pageTitle + ']].'; | |||
api.postWithFormData('csrf', { | |||
action: 'upload', | |||
filename: file.name, | |||
file: file, | |||
comment: 'Uploaded from soldier page', | |||
text: fileText, | |||
format: 'json' | |||
}).then(function (data) { | |||
var uploaded = data.upload && data.upload.filename; | |||
if (!uploaded) { | |||
throw new Error('Upload failed'); | |||
} | |||
setStatus('Attaching to page…'); | |||
return api.postWithToken('csrf', { | |||
action: 'ritchieattachfile', | |||
filename: uploaded, | |||
soldier: pageTitle, | |||
profile: useProfile ? 1 : 0, | |||
gallery: addGallery ? 1 : 0, | |||
format: 'json' | |||
}); | |||
}).done(function () { | |||
setStatus('Done. Reloading…'); | |||
window.location.reload(); | |||
}).fail(function (code, data) { | |||
var err = (data && data.error && data.error.info) || code || 'Upload failed'; | |||
setStatus(err, true); | |||
$submit.prop('disabled', false); | |||
}); | |||
}); | }); | ||
}); | }); | ||