MediaWiki:Gadget-PreloadEditor.js: Difference between revisions
MediaWiki interface page
More actions
ReisuDesign (talk | contribs) No edit summary |
ReisuDesign (talk | contribs) No edit summary |
||
| (One intermediate revision by the same user not shown) | |||
| Line 1,457: | Line 1,457: | ||
); | ); | ||
} | } | ||
if ( | if ( | ||
field.type === 'file' | |||
) { | ) { | ||
if ( | if ( | ||
field. | field.accept !== undefined && | ||
!Array.isArray(field.accept) | |||
) { | ) { | ||
errors.push( | errors.push( | ||
'File field "' + | 'File field "' + | ||
name + | name + | ||
'" | '" must have "accept" as an array.' | ||
); | ); | ||
} | } | ||
if ( | if ( | ||
field. | Array.isArray(field.accept) && | ||
field.accept.some( | |||
value => | |||
!String( | |||
value || '' | |||
).trim() | |||
) | |||
) { | ) { | ||
errors.push( | errors.push( | ||
'File field "' + | 'File field "' + | ||
name + | name + | ||
'" | '" contains an empty accepted MIME type.' | ||
); | ); | ||
} | } | ||
if ( | if ( | ||
field. | field.filename !== undefined && | ||
typeof field.filename !== 'string' | |||
) { | ) { | ||
errors.push( | errors.push( | ||
'File field "' + | 'File field "' + | ||
name + | name + | ||
'" must have " | '" must have "filename" as a string.' | ||
); | ); | ||
} | } | ||
if ( | |||
field.filetext !== undefined && | |||
typeof field.filetext !== 'string' | |||
) { | |||
errors.push( | |||
'File field "' + | |||
name + | |||
'" must have "filetext" as a string.' | |||
); | |||
} | |||
if ( | |||
!field.accept || | |||
!field.accept.length | |||
) { | |||
warnings.push( | |||
'File field "' + | |||
name + | |||
'" has no accepted MIME types.' | |||
); | |||
} | |||
if ( | |||
!field.filename | |||
) { | |||
warnings.push( | |||
'File field "' + | |||
name + | |||
'" has no filename template.' | |||
); | |||
} | |||
} | |||
if ( | |||
( | |||
field.type === 'select' || | |||
field.type === 'multiselect' | |||
) && | |||
!field.source && | |||
!Array.isArray(field.options) | |||
) { | |||
warnings.push( | |||
'Field "' + | |||
name + | |||
'" is a ' + | |||
field.type + | |||
' without a source or manual options.' | |||
); | |||
} | |||
if (field.type === 'file') { | |||
if ( | |||
field.filename !== undefined && | |||
typeof field.filename !== 'string' | |||
) { | |||
errors.push( | |||
'File field "' + | |||
name + | |||
'" has an invalid "filename" value.' | |||
); | |||
} | |||
if ( | |||
field.filetext !== undefined && | |||
typeof field.filetext !== 'string' | |||
) { | |||
errors.push( | |||
'File field "' + | |||
name + | |||
}; | '" has an invalid "filetext" value.' | ||
); | |||
} | |||
if ( | |||
field.accept !== undefined && | |||
( | |||
!Array.isArray(field.accept) || | |||
field.accept.some( | |||
value => | |||
typeof value !== 'string' || | |||
!value.trim() | |||
) | |||
) | |||
) { | |||
errors.push( | |||
'File field "' + | |||
name + | |||
'" must have "accept" as an array of MIME types.' | |||
); | |||
} | |||
} | |||
}); | |||
return { | |||
valid: !errors.length, | |||
} | errors, | ||
warnings | |||
}; | |||
}, | }, | ||
// ========================================================= | // ========================================================= | ||
// | // Transformation helpers | ||
// ========================================================= | // ========================================================= | ||
transformationObject( | |||
transform, | |||
prefix, | |||
suffix, | |||
from, | |||
to | |||
) { | |||
if (!transform) { | |||
return null; | |||
} | |||
switch (transform) { | |||
case 'remove-prefix': | |||
return prefix | |||
? { | |||
transform, | |||
prefix | |||
} | |||
: null; | |||
case 'remove-suffix': | |||
return suffix | |||
? { | |||
transform, | |||
suffix | |||
} | |||
: null; | |||
case 'replace': | |||
return from === '' | |||
? null | |||
: { | |||
transform, | |||
from, | |||
to: to || '' | |||
}; | |||
default: | |||
return null; | |||
} | |||
}, | }, | ||
createButton(label, className, callback) { | // ========================================================= | ||
const button = $('<button>') | // UI helpers | ||
// ========================================================= | |||
createSection(title, className) { | |||
const section = $('<section>') | |||
.addClass('preload-editor-section'); | |||
if (className) { | |||
section.addClass(className); | |||
} | |||
$('<h2>') | |||
.addClass('preload-editor-section-title') | |||
.text(title) | |||
.appendTo(section); | |||
return section; | |||
}, | |||
createButton(label, className, callback) { | |||
const button = $('<button>') | |||
.attr('type', 'button') | .attr('type', 'button') | ||
.addClass('preload-editor-button') | .addClass('preload-editor-button') | ||
| Line 4,849: | Line 4,923: | ||
) { | ) { | ||
this.renderSelectConfiguration( | this.renderSelectConfiguration( | ||
card, | |||
field | |||
); | |||
} | |||
if ( | |||
field.type === 'file' | |||
) { | |||
this.renderFileConfiguration( | |||
card, | card, | ||
field | field | ||
| Line 5,115: | Line 5,198: | ||
options.on( | options.on( | ||
'input.preloadEditor', | 'input.preloadEditor', | ||
() => { | () => { | ||
const lines = | const lines = | ||
options | options | ||
.val() | .val() | ||
.split('\n') | .split('\n') | ||
.map( | .map( | ||
line => | line => | ||
line.trim() | line.trim() | ||
) | ) | ||
.filter(Boolean); | .filter(Boolean); | ||
if (!lines.length) { | if (!lines.length) { | ||
delete field.options; | delete field.options; | ||
} else { | |||
field.options = | |||
lines.map( | |||
line => { | |||
const parts = | |||
line.split( | |||
/\s*\|\s*/ | |||
); | |||
if ( | |||
parts.length === | |||
1 | |||
) { | |||
return parts[0]; | |||
} | |||
return { | |||
value: | |||
parts[0], | |||
label: | |||
parts | |||
.slice(1) | |||
.join( | |||
' | ' | |||
) | |||
}; | |||
} | |||
); | |||
} | |||
this.markDirty(); | |||
this.renderConfigReviewAvailability(); | |||
} | |||
); | |||
card.append( | |||
this.fieldRow( | |||
'Manual options', | |||
options, | |||
'One per line. Optional label format: value | label. Leave empty when using a source.' | |||
) | |||
); | |||
}, | |||
renderFileConfiguration( | |||
card, | |||
field | |||
) { | |||
const acceptValue = | |||
Array.isArray(field.accept) | |||
? field.accept.join('\n') | |||
: ''; | |||
const accept = | |||
this.textarea( | |||
acceptValue | |||
).addClass( | |||
'preload-editor-small-textarea' | |||
); | |||
accept.on( | |||
'input.preloadEditor', | |||
() => { | |||
const values = | |||
accept | |||
.val() | |||
.split('\n') | |||
.map( | |||
value => | |||
value.trim() | |||
) | |||
.filter(Boolean); | |||
if (values.length) { | |||
field.accept = values; | |||
} else { | |||
delete field.accept; | |||
} | |||
this.markDirty(); | |||
this.renderConfigReviewAvailability(); | |||
} | |||
); | |||
card.append( | |||
this.fieldRow( | |||
'Accepted MIME types', | |||
accept, | |||
'One MIME type per line. Example: image/png' | |||
) | |||
); | |||
const filename = | |||
this.textInput( | |||
field.filename || | |||
'%%PAGE%%.%%EXT%%' | |||
); | |||
filename.on( | |||
'input.preloadEditor', | |||
() => { | |||
const value = | |||
filename.val(); | |||
if (value) { | |||
field.filename = value; | |||
} else { | |||
delete field.filename; | |||
} | |||
this.markDirty(); | |||
this.renderConfigReviewAvailability(); | |||
} | |||
); | |||
card.append( | |||
this.fieldRow( | |||
'Filename', | |||
filename, | |||
'Filename template. %%PAGE%% is replaced with the page name and %%EXT%% with the uploaded file extension.' | |||
) | |||
); | |||
const filetext = | |||
this.textarea( | |||
field.filetext || '' | |||
); | |||
filetext.on( | |||
'input.preloadEditor', | |||
() => { | |||
const value = | |||
filetext.val(); | |||
if (value) { | |||
field.filetext = value; | |||
} else { | } else { | ||
field. | delete field.filetext; | ||
} | } | ||
| Line 5,166: | Line 5,359: | ||
card.append( | card.append( | ||
this.fieldRow( | this.fieldRow( | ||
' | 'File page text', | ||
filetext, | |||
' | 'Wikitext used for the uploaded File: page.' | ||
) | ) | ||
); | ); | ||
| Line 6,021: | Line 6,214: | ||
if ( | if ( | ||
mw.config.get('wgPageName') !== | mw.config.get('wgPageName') !== | ||
' | 'Petit_Planet_Wiki:Preload_Editor' | ||
) { | ) { | ||
return; | return; | ||