Jump to content
Toggle menu
Toggle preferences menu
Toggle personal menu
Not logged in
If you make an edit, a temporary account will be created for you.

MediaWiki:Gadget-PreloadEditor.js: Difference between revisions

MediaWiki interface page
ReisuDesign (talk | contribs)
No edit summary
ReisuDesign (talk | contribs)
No edit summary
Line 1,457: Line 1,457:
                     );
                     );
                 }
                 }
 
               
                 if (
                 if (
                     (
                     field.type === 'file'
                        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 (
                     if (
                         field.filename !== undefined &&
                         field.accept !== undefined &&
                         typeof field.filename !== 'string'
                         !Array.isArray(field.accept)
                     ) {
                     ) {
                         errors.push(
                         errors.push(
                             'File field "' +
                             'File field "' +
                             name +
                             name +
                             '" has an invalid "filename" value.'
                             '" must have "accept" as an array.'
                         );
                         );
                     }
                     }


                     if (
                     if (
                         field.filetext !== undefined &&
                         Array.isArray(field.accept) &&
                         typeof field.filetext !== 'string'
                         field.accept.some(
                            value =>
                                !String(
                                    value || ''
                                ).trim()
                        )
                     ) {
                     ) {
                         errors.push(
                         errors.push(
                             'File field "' +
                             'File field "' +
                             name +
                             name +
                             '" has an invalid "filetext" value.'
                             '" contains an empty accepted MIME type.'
                         );
                         );
                     }
                     }


                     if (
                     if (
                         field.accept !== undefined &&
                         field.filename !== undefined &&
                         (
                         typeof field.filename !== 'string'
                            !Array.isArray(field.accept) ||
                            field.accept.some(
                                value =>
                                    typeof value !== 'string' ||
                                    !value.trim()
                            )
                        )
                     ) {
                     ) {
                         errors.push(
                         errors.push(
                             'File field "' +
                             'File field "' +
                             name +
                             name +
                             '" must have "accept" as an array of MIME types.'
                             '" must have "filename" as a string.'
                         );
                         );
                     }
                     }
                }
            });


            return {
                    if (
                valid: !errors.length,
                        field.filetext !== undefined &&
                errors,
                        typeof field.filetext !== 'string'
                warnings
                    ) {
            };
                        errors.push(
        },
                            'File field "' +
                            name +
                            '" must have "filetext" as a string.'
                        );
                    }


        // =========================================================
                    if (
        // Transformation helpers
                        !field.accept ||
        // =========================================================
                        !field.accept.length
                    ) {
                        warnings.push(
                            'File field "' +
                            name +
                            '" has no accepted MIME types.'
                        );
                    }


        transformationObject(
                    if (
            transform,
                        !field.filename
            prefix,
                    ) {
            suffix,
                        warnings.push(
            from,
                            'File field "' +
            to
                            name +
        ) {
                            '" has no filename template.'
            if (!transform) {
                        );
                 return null;
                    }
            }
                 }


            switch (transform) {
                if (
                 case 'remove-prefix':
                    (
                     return prefix
                        field.type === 'select' ||
                         ? {
                        field.type === 'multiselect'
                            transform,
                    ) &&
                            prefix
                    !field.source &&
                         }
                    !Array.isArray(field.options)
                         : null;
                 ) {
                     warnings.push(
                         'Field "' +
                        name +
                        '" is a ' +
                         field.type +
                         ' without a source or manual options.'
                    );
                }


                 case 'remove-suffix':
                 if (field.type === 'file') {
                     return suffix
                     if (
                         ? {
                         field.filename !== undefined &&
                            transform,
                         typeof field.filename !== 'string'
                            suffix
                    ) {
                        }
                         errors.push(
                         : null;
                             'File field "' +
 
                             name +
                case 'replace':
                             '" has an invalid "filename" value.'
                    return from === ''
                         );
                        ? null
                    }
                         : {
                             transform,
                             from,
                             to: to || ''
                         };


                default:
                    if (
                     return null;
                        field.filetext !== undefined &&
            }
                        typeof field.filetext !== 'string'
        },
                     ) {
                        errors.push(
                            'File field "' +
                            name +
                            '" has an invalid "filetext" value.'
                        );
                    }


        // =========================================================
                    if (
        // UI helpers
                        field.accept !== undefined &&
        // =========================================================
                        (
 
                            !Array.isArray(field.accept) ||
        createSection(title, className) {
                            field.accept.some(
            const section = $('<section>')
                                value =>
                 .addClass('preload-editor-section');
                                    typeof value !== 'string' ||
                                    !value.trim()
                            )
                        )
                    ) {
                        errors.push(
                            'File field "' +
                            name +
                            '" must have "accept" as an array of MIME types.'
                        );
                    }
                 }
            });


             if (className) {
             return {
                 section.addClass(className);
                 valid: !errors.length,
            }
                 errors,
 
                 warnings
            $('<h2>')
             };
                 .addClass('preload-editor-section-title')
                 .text(title)
                .appendTo(section);
 
             return section;
         },
         },


         createButton(label, className, callback) {
         // =========================================================
            const button = $('<button>')
        // Transformation helpers
                .attr('type', 'button')
        // =========================================================
                .addClass('preload-editor-button')
                .text(label);


             if (className) {
        transformationObject(
                 button.addClass(className);
            transform,
            prefix,
            suffix,
            from,
            to
        ) {
             if (!transform) {
                 return null;
             }
             }


             if (callback) {
             switch (transform) {
                 button.on(
                 case 'remove-prefix':
                    'click.preloadEditor',
                     return prefix
                     callback
                        ? {
                );
                            transform,
            }
                            prefix
                        }
                        : null;


            return button;
                case 'remove-suffix':
        },
                    return suffix
                        ? {
                            transform,
                            suffix
                        }
                        : null;


        fieldRow(label, input, description) {
                case 'replace':
            const row = $('<div>')
                    return from === ''
                .addClass('preload-editor-field');
                        ? null
                        : {
                            transform,
                            from,
                            to: to || ''
                        };


             $('<label>')
                default:
                 .addClass('preload-editor-label')
                    return null;
                .text(label)
            }
                .appendTo(row);
        },
 
        // =========================================================
        // UI helpers
        // =========================================================
 
        createSection(title, className) {
             const section = $('<section>')
                 .addClass('preload-editor-section');


             if (description) {
             if (className) {
                 $('<div>')
                 section.addClass(className);
                    .addClass('preload-editor-help')
                    .text(description)
                    .appendTo(row);
             }
             }


             row.append(input);
             $('<h2>')
                .addClass('preload-editor-section-title')
                .text(title)
                .appendTo(section);


             return row;
             return section;
         },
         },


         textInput(value, placeholder) {
        createButton(label, className, callback) {
             return $('<input>')
            const button = $('<button>')
                 .attr({
                .attr('type', 'button')
                     type: 'text',
                .addClass('preload-editor-button')
                     value: value == null ? '' : value,
                .text(label);
 
            if (className) {
                button.addClass(className);
            }
 
            if (callback) {
                button.on(
                    'click.preloadEditor',
                    callback
                );
            }
 
            return button;
        },
 
        fieldRow(label, input, description) {
            const row = $('<div>')
                .addClass('preload-editor-field');
 
            $('<label>')
                .addClass('preload-editor-label')
                .text(label)
                .appendTo(row);
 
            if (description) {
                $('<div>')
                    .addClass('preload-editor-help')
                    .text(description)
                    .appendTo(row);
            }
 
            row.append(input);
 
            return row;
        },
 
         textInput(value, placeholder) {
             return $('<input>')
                 .attr({
                     type: 'text',
                     value: value == null ? '' : value,
                     placeholder: placeholder || ''
                     placeholder: placeholder || ''
                 })
                 })
Line 4,849: Line 4,923:
             ) {
             ) {
                 this.renderSelectConfiguration(
                 this.renderSelectConfiguration(
                    card,
                    field
                );
            }
            if (
                field.type === 'file'
            ) {
                this.renderFileConfiguration(
                     card,
                     card,
                     field
                     field
Line 5,119: Line 5,202:
                         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.options =
                         delete field.filetext;
                            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(
                                                    ' | '
                                                )
                                    };
                                }
                            );
                     }
                     }


Line 5,166: Line 5,359:
             card.append(
             card.append(
                 this.fieldRow(
                 this.fieldRow(
                     'Manual options',
                     'File page text',
                     options,
                     filetext,
                     'One per line. Optional label format: value | label. Leave empty when using a source.'
                     'Wikitext used for the uploaded File: page.'
                 )
                 )
             );
             );