MediaWiki:Gadget-NeoWikiMetadataDroplets.js: Difference between revisions

Meryam Sahin (talk | contribs)
Form fields for metadata droplets
 
Meryam Sahin (talk | contribs)
Leave out empty parameters so template defaults apply
Line 10: Line 10:
if ( !template ) { return mwData; }
if ( !template ) { return mwData; }
template.params = template.params || {};
template.params = template.params || {};
fields.forEach( function ( key ) { var value = newData[ key ]; if ( value === undefined || value === null ) { value = ''; } template.params[ key ] = { wt: String( value ) }; } );
fields.forEach( function ( key ) { var value = newData[ key ]; if ( value === undefined || value === null || String( value ).trim() === '' ) { delete template.params[ key ]; return; } template.params[ key ] = { wt: String( value ) }; } );
return mwData;
return mwData;
};
};

Revision as of 13:30, 4 November 2026

( function () {
	if ( !window.ext || !ext.contentdroplets || !ext.contentdroplets.object ) { return; }
	function createDroplet( fields, formItems ) {
		var Droplet = function ( cfg ) { Droplet.parent.call( this, cfg ); };
		OO.inheritClass( Droplet, ext.contentdroplets.object.TransclusionDroplet );
		Droplet.prototype.getFormItems = function () { return formItems; };
		Droplet.prototype.updateMWData = function ( newData, mwData ) {
			var template = mwData.parts && mwData.parts[ 0 ] && mwData.parts[ 0 ].template;
			newData = newData || {};
			if ( !template ) { return mwData; }
			template.params = template.params || {};
			fields.forEach( function ( key ) { var value = newData[ key ]; if ( value === undefined || value === null || String( value ).trim() === '' ) { delete template.params[ key ]; return; } template.params[ key ] = { wt: String( value ) }; } );
			return mwData;
		};
		Droplet.prototype.convertDataForForm = function ( data ) {
			var out = {}, key;
			for ( key in data ) { if ( Object.prototype.hasOwnProperty.call( data, key ) ) { out[ key ] = ( data[ key ] && typeof data[ key ] === 'object' ) ? data[ key ].wt : data[ key ]; } }
			return out;
		};
		return Droplet;
	}
	ext.contentdroplets.registry.register( 'metadata-value', createDroplet( [ 'page', 'property', 'dateformat' ], [
		{ name: 'page', type: 'text', label: 'Page (leave empty for the current page)' },
		{ name: 'property', type: 'text', required: true, label: 'Property (Schema.Property, e.g. Document Control.Valid until)' },
		{ name: 'dateformat', type: 'text', label: 'Date format (only for dates, e.g. d.m.Y)' } ] ) );
	ext.contentdroplets.registry.register( 'metadata-query', createDroplet(
		[ 'schema', 'property', 'operator', 'value', 'format', 'separator', 'display', 'limit' ], [
		{ name: 'schema', type: 'text', required: true, label: 'Schema (e.g. ISO 27001 Control)' },
		{ name: 'property', type: 'text', label: 'Filter property (optional, Schema.Property)' },
		{ name: 'operator', type: 'dropdown', value: '=', label: 'Operator', options: [ {data:'=',label:'= (equals)'},{data:'!=',label:'!= (not equal)'},{data:'>',label:'> (greater than)'},{data:'>=',label:'>= (greater or equal)'},{data:'<',label:'< (less than)'},{data:'<=',label:'<= (less or equal)'},{data:'~',label:'~ (contains)'},{data:'^',label:'^ (starts with)'} ] },
		{ name: 'value', type: 'text', label: 'Value (for select properties use the label)' },
		{ name: 'format', type: 'dropdown', value: 'count', label: 'Format', options: [ {data:'count',label:'Count'},{data:'list',label:'List'} ] },
		{ name: 'separator', type: 'text', label: 'Separator (list only, default: comma)' },
		{ name: 'display', type: 'dropdown', value: 'subpage', label: 'Page name display (list only)', options: [ {data:'subpage',label:'Subpage name only'},{data:'text',label:'Page name'},{data:'prefixed',label:'Page name with namespace'} ] },
		{ name: 'limit', type: 'number', value: 50, label: 'Limit' } ] ) );
}() );