Widget:File upload: verschil tussen versies
Uit Dorpsbelang Den Hout
Regel 29: | Regel 29: | ||
var fileInput = document.getElementById('widget-file-upload-files'); | var fileInput = document.getElementById('widget-file-upload-files'); | ||
− | console.log( | + | console.log(createWikitext('hoi')) |
function createFilename(filename){ | function createFilename(filename){ | ||
var output = settingsFilename; | var output = settingsFilename; | ||
− | + | var regex = /<(\w+)>/gm; | |
− | + | var m; | |
while ((m = regex.exec(settingsFilename)) !== null) { | while ((m = regex.exec(settingsFilename)) !== null) { | ||
Regel 54: | Regel 54: | ||
output = output.replace('<'+match+'>', val); | output = output.replace('<'+match+'>', val); | ||
} | } | ||
− | + | ||
} | } | ||
// console.log(`Found match, group ${groupIndex}: ${match}`); | // console.log(`Found match, group ${groupIndex}: ${match}`); | ||
Regel 62: | Regel 62: | ||
} | } | ||
− | + | function createWikitext(filename){ | |
+ | var output = settingsFilepageparameters; | ||
+ | var regex = /<(\w+)>/gm; | ||
+ | var m; | ||
+ | |||
+ | while ((m = regex.exec(settingsFilepageparameters)) !== null) { | ||
+ | // This is necessary to avoid infinite loops with zero-width matches | ||
+ | if (m.index === regex.lastIndex) { | ||
+ | regex.lastIndex++; | ||
+ | } | ||
+ | |||
+ | // The result can be accessed through the `m`-variable. | ||
+ | m.forEach((match, groupIndex) => { | ||
+ | |||
+ | if(match == "FILENAME"){ | ||
+ | output = output.replace('<'+match+'>', filename); | ||
+ | }else{ | ||
+ | var input = fileInput.closest('form').querySelector('[name="'+match+'"]'); | ||
+ | if(input){ | ||
+ | val = input.value; | ||
+ | output = output.replace('<'+match+'>', val); | ||
+ | } | ||
+ | |||
+ | } | ||
+ | // console.log(`Found match, group ${groupIndex}: ${match}`); | ||
+ | }); | ||
+ | } | ||
+ | output = output.replace(':','='); | ||
+ | output = output.replace(';','|'); | ||
+ | output = '{{'+settingsFilepagetemplate+'|'+output+'}}'; | ||
+ | return output; | ||
+ | } | ||
function handleFileSelect() { | function handleFileSelect() { | ||
Regel 90: | Regel 121: | ||
formdata = new FormData(); | formdata = new FormData(); | ||
formdata.append("action", "upload"); | formdata.append("action", "upload"); | ||
− | formdata.append("filename", | + | formdata.append("filename", createFilename(fileName)); |
//formdata.append("filename", fileName); | //formdata.append("filename", fileName); | ||
formdata.append("token", mw.user.tokens.get( 'editToken' ) ); | formdata.append("token", mw.user.tokens.get( 'editToken' ) ); | ||
Regel 131: | Regel 162: | ||
var fileToLoad = filesSelected[i]; | var fileToLoad = filesSelected[i]; | ||
var fname = fileToLoad.name; | var fname = fileToLoad.name; | ||
− | + | ||
− | |||
if(fname.substr( fname.length -3 ) === "pdf"){ | if(fname.substr( fname.length -3 ) === "pdf"){ | ||
− | + | //add somthing here | |
+ | |||
}else{ | }else{ | ||
− | + | var fileReader = new FileReader(); | |
− | + | ||
− | + | fileReader.onload = function(fileLoadedEvent) { | |
var srcData = fileLoadedEvent.target.result; // <--- data: base64 | var srcData = fileLoadedEvent.target.result; // <--- data: base64 | ||
var newImage = document.createElement('img'); | var newImage = document.createElement('img'); | ||
newImage.src = srcData; | newImage.src = srcData; | ||
− | var dt = filesSelected[counter] | + | var dt = filesSelected[counter] |
− | counter = counter + 1; | + | counter = counter + 1; |
document.getElementById("widget-file-upload-preview").innerHTML = document.getElementById("widget-file-upload-preview").innerHTML+'<div class="upload-group">'+newImage.outerHTML+'<input data="'+dt.name+'" type="text" placeholder="caption" class="form-control" /></div>'; | document.getElementById("widget-file-upload-preview").innerHTML = document.getElementById("widget-file-upload-preview").innerHTML+'<div class="upload-group">'+newImage.outerHTML+'<input data="'+dt.name+'" type="text" placeholder="caption" class="form-control" /></div>'; | ||
− | + | ||
− | |||
} | } | ||
Regel 161: | Regel 191: | ||
function editFilePage(file){ | function editFilePage(file){ | ||
− | |||
var caption = document.querySelector('[data="'+file+'"]').value; | var caption = document.querySelector('[data="'+file+'"]').value; | ||
Regel 175: | Regel 204: | ||
var params = { | var params = { | ||
action: 'edit', | action: 'edit', | ||
− | title: 'File:'+ | + | title: 'File:'+createFilename(edit), |
text: newWikitextEdit, | text: newWikitextEdit, | ||
format: 'json' | format: 'json' |
Versie van 7 sep 2020 09:40
This is the Widget:Multi upload page. It should be called in the following format:
{{#widget:Multi upload }}