MediaWiki:Gadget-edit-form-section-parser.js
Uwaga: aby zobaczyć zmiany po opublikowaniu, może zajść potrzeba wyczyszczenia pamięci podręcznej przeglądarki.
- Firefox / Safari: Przytrzymaj Shift podczas klikania Odśwież bieżącą stronę, lub naciśnij klawisze Ctrl+F5, lub Ctrl+R (⌘-R na komputerze Mac)
- Google Chrome: Naciśnij Ctrl-Shift-R (⌘-Shift-R na komputerze Mac)
- Internet Explorer / Edge: Przytrzymaj Ctrl, jednocześnie klikając Odśwież, lub naciśnij klawisze Ctrl+F5
- Opera: Naciśnij klawisze Ctrl+F5.
var ESectionParser = {},
EConstants = require( './edit-form-constants.js' );
ESectionParser.parse = function ( section ) {
var i, targetSubsections,
subsections = [],
mode = null,
code = section.code;
if ( !section.title ) {
mode = 'INTRO';
} else {
switch ( code ) {
case 'pl':
case 'pl-foreign':
mode = 'POLISH';
break;
case 'zh-char':
mode = 'CHINESE_SIGN';
break;
case 'zh':
mode = 'CHINESE';
break;
case 'egy':
mode = 'EGYPTIAN';
break;
case 'ko':
mode = 'KOREAN';
break;
case 'ja':
mode = 'JAPANESE';
break;
case 'eo':
mode = 'ESPERANTO';
break;
case 'eom':
mode = 'ESPERANTO_M';
break;
case 'inter':
mode = 'INTERNATIONAL';
break;
default:
mode = [];
if ( $.inArray( code, EConstants.DOUBLE_LANGS ) !== -1 ) {
mode.push( 'DOUBLE' );
}
if ( $.inArray( code, EConstants.NON_LATIN_LANGS ) !== -1 ) {
mode.push( 'NON_LATIN' );
} else {
mode.push( 'LATIN' );
}
if ( mode.length === 1 ) {
mode = mode[ 0 ];
}
break;
}
}
subsections.push( {
title: '',
content: '',
shortened: false,
active: true
} );
for ( i in EConstants.SUBSECTIONS.ALL ) {
if ( EConstants.SUBSECTIONS.ALL.hasOwnProperty( i ) ) {
subsections.push( {
title: EConstants.SUBSECTIONS.ALL[ i ],
content: '',
shortened: true,
initcontent: '',
initmultiline: false,
active: true
} );
}
}
targetSubsections = Array.isArray( mode )
? ESectionParser.combineModes( mode )
: ( EConstants.SUBSECTIONS[ mode ] || [] );
section.subsections = subsections;
section.mode = mode;
ESectionParser.parsePreparedSubsections( section, targetSubsections );
};
ESectionParser.combineModes = function ( modes ) {
var hset = [], combined = [];
$.each( modes, function ( i, mode ) {
$.each( EConstants.SUBSECTIONS[ mode ], function ( j, value ) {
hset[ EConstants.SUBSECTIONS.ALL.indexOf( value ) ] = true;
} );
} );
$.each( hset, function ( i, v ) {
if ( v ) {
combined.push( EConstants.SUBSECTIONS.ALL[ i ] );
}
} );
return combined;
};
ESectionParser.alternateTitle = function ( title ) {
switch ( title ) {
case 'transliteracja':
return '|trans';
case 'transkrypcja':
return '|transkr';
case 'kreski':
return '|hanja-kreski';
case 'przykłady':
return '|użycie';
default:
return '';
}
};
ESectionParser.parsePreparedSubsections = function ( section, targetSubsections ) {
var i, j, title, alt, regex, sub, pos, repl, changed, firstbreak,
str = section.content,
subsections = section.subsections,
positions = [];
for ( i in subsections ) {
if ( subsections.hasOwnProperty( i ) ) {
title = subsections[ i ].title;
alt = ESectionParser.alternateTitle( title );
regex = new RegExp( '\\{\\{(' + title + alt + ')\\s*[\\|\\}]', 'g' );
positions.push( {
index: title === '' ? 0 : str.search( regex ),
title: title
} );
}
}
positions.sort( function ( a, b ) {
if ( !a.index && !b.index ) {
return a.title ? 1 : -1;
}
return a.index - b.index;
} );
for ( i in subsections ) {
if ( subsections.hasOwnProperty( i ) ) {
sub = subsections[ i ];
for ( j in positions ) {
if ( positions.hasOwnProperty( j ) ) {
j = parseInt( j, 10 );
pos = positions[ j ];
if ( pos.title === sub.title ) {
if ( pos.index !== -1 ) {
sub.content = ( j < positions.length - 1 )
? $.trim( str.substring( pos.index, positions[ j + 1 ].index ) )
: $.trim( str.substring( pos.index ) );
alt = ESectionParser.alternateTitle( sub.title );
repl = new RegExp( '\\{\\{(' + sub.title + alt + ')\\}\\}' );
changed = sub.content.replace( repl, '' );
if ( changed !== sub.content ) {
firstbreak = changed.search( /\n/ );
if (
firstbreak !== -1 &&
firstbreak < changed.search( /\S/ )
) {
sub.initmultiline = true;
}
sub.content = $.trim( changed );
} else if (
sub.content !== '' ||
sub.title === ''
) {
sub.shortened = false;
}
if (
targetSubsections.indexOf( pos.title ) === -1 &&
sub.content === '' &&
sub.title !== ''
) {
sub.active = false;
} else {
sub.initcontent = sub.content;
}
break;
} else if ( targetSubsections.indexOf( pos.title ) === -1 ) {
sub.active = false;
}
}
}
}
}
}
};
ESectionParser.obligatorySubsection = function ( subsection, section ) {
return (
subsection.title === 'znaczenia' &&
section.mode !== 'CHINESE'
);
};
ESectionParser.botSubsection = function ( subsection, section ) {
return (
subsection.title === 'wymowa' &&
section.mode === 'POLISH' &&
!subsection.content
);
};
module.exports = ESectionParser;