Χρήστης:Geraki/assessmentHelper.js

Από τη Βικιπαίδεια, την ελεύθερη εγκυκλοπαίδεια

Μετά την αποθήκευση πρέπει να καθαρίσετε την κρυφή μνήμη (cache) του browser σας για να δείτε τις αλλαγές: Σε Chrome, Firefox, Safari, Internet Explorer και Edge: Κρατήστε πατημένο το Shift και κάντε κλικ στο κουμπί Ανανέωση στην μπάρα εργαλείων.

//<nowiki>

// Make sure we are on a talk page and in view or edit mode. If we are in edit
// mode, make sure we are not editing a section.
if ( mw.config.get( 'wgNamespaceNumber' ) === 0
	&& ( mw.config.get( 'wgAction' ) === 'view' || ( mw.config.get( 'wgAction' ) === 'edit' && !$( 'input[name=wpSection]' ).val() ) )
) {

	// This list should only include active WikiProjects with templates in
	// Category:WikiProject banners with quality assessment.
	// FIXME: Once mw.loader.using() can load URLs, move this list to a separate
	// page that is loaded when the dialog is opened.
	wikiProjectList = [
		"", // empty initial option
		"Ιατρική",
		"Βιογραφία",
		'Στρατιωτική Ιστορία',
		"Β' Παγκόσμιος Πόλεμος",
		'Φυσικές Επιστήμες',
		'Αστρονομία',
		'Αρχαία Ελλάδα',
		"Πολιτική",
		"Ποδόσφαιρο",
		'Κινηματογράφος',
		'Τηλεόραση'
	];
	
	// Script depends on jQuery UI dialog and jQuery cookie modules
	mw.loader.using( ['jquery.ui', 'mediawiki.cookie'], function() {
		// Construct object (to prevent namespace conflicts)
		wikiAssess = {

			pageName: 'Συζήτηση:' + mw.config.get( 'wgPageName' ),

			displayProgress: function( message ) {
				$( '#assessment-form div' ).hide(); // remove everything else from the dialog box
				$( '#assessment-form' ).append ( $('<div class="assess-progress" style="text-align:center;margin:1.8em 0;"></div>').html( message+'<br/><img src="//upload.wikimedia.org/wikipedia/commons/4/42/Loading.gif" />' ) );
			},

			displayError: function( error ) {
				$( '#assessment-form div' ).hide(); // remove everything else from the dialog box
				$( '#assessment-form' ).append ( $('<div class="assess-error" style="color:#990000;margin-top:0.4em;"></div>').html( 'Error: '+error ) );
			},

			addAssessment: function() {
				var oldContent,
					newContent,
					selectedImportance,
					project = 'Επιχείρηση ' + $( '#projectSelect' ).val(),
					classRank = $( '#classSelect' ).val(),
					importance = '',
					template;

				wikiAssess.displayProgress( 'Προσθήκη αποτίμησης στο λήμμα...' );
				selectedImportance = $( "input[type='radio'][name='importance']:checked" );
				if ( selectedImportance.length > 0 ) {
					importance = selectedImportance.val();
				}
				if ( project !== 'Επιχείρηση ' ) {
					mw.cookie.set( 'assessmentHelper', $('#projectSelect').val(), { expires: 30 } );
					template = '{{' + project + '|τάξη=' + classRank + '|σπουδαιότητα=' + importance + '}}';
				} else {
					wikiAssess.displayError( 'Αδόκιμη επιλογή Βικιεπιχείρησης.' );
					return;
				}
				
				if ( mw.config.get( 'wgAction' ) === 'view' ) {
					$.ajax({
						url: mw.util.wikiScript( 'api' ),
						data: {
							action: 'query',
							prop: 'revisions',
							rvprop: 'content',
							titles: wikiAssess.pageName,
							formatversion: 2,
							format: 'json'
						},
						dataType: 'json',
						type: 'GET',
						success: function( data ) {
							if ( data.query !== undefined
								&& data.query.pages[0] !== undefined
								&& data.query.pages[0].revisions[0] !== undefined
								&& typeof data.query.pages[0].revisions[0].content === 'string'
							) {
								oldContent = data.query.pages[0].revisions[0].content;
								// Make sure there isn't already an assessment for this WikiProject.
								// Note that this isn't foolproof due to template aliases.
								if ( oldContent.search( '{{' + project ) !== -1 ) {
									wikiAssess.displayError( 'Ήδη αποτιμημένο λήμμα για αυτή την επιχείρηση.' );
									return;
								}
								// Insert the WikiProject template into the content
								newContent = wikiAssess.insertTemplate( template, oldContent );
								// Check for accidental content deletion
								if ( newContent.length <= oldContent.length ) {
									wikiAssess.displayError( 'Άγνωστο σφάλμα κατά την εισαγωγή προτύπου.' );
									return;
								}
								// Make the actual edit to the talk page
								wikiAssess.editTalkPage( 'Προσθήκη προτύπου για ' + project + '', newContent );
							} else {
								wikiAssess.displayError( 'Αποτυχία για ανάκτηση περιεχομένου σελίδας.' );
							}
						},
						error: function( xhr ) {
							wikiAssess.displayError( 'Αποτυχία για ανάκτηση περιεχομένου σελίδας.' );
						}
					});
				} else if ( mw.config.get( 'wgAction' ) === 'edit' ) {
					oldContent = $( '#wpTextbox1' ).val();
					// Make sure there isn't already an assessment for this WikiProject
					if ( oldContent.search( '{{' + project ) !== -1 ) {
						wikiAssess.displayError( 'Ήδη αποτιμημένο λήμμα για αυτή την επιχείρηση.' );
						return;
					}
					// Insert the WikiProject template into the content
					newContent = wikiAssess.insertTemplate( template, oldContent );
					// Check for accidental content deletion
					if ( newContent.length <= oldContent.length ) {
						wikiAssess.displayError( 'Άγνωστο σφάλμα κατά την εισαγωγή προτύπου.' );
						return;
					}
					// Replace the edit box contents with the new content
					$( '#wpTextbox1' ).val( newContent );
					// Close the dialog
					$assessInterface.dialog( 'close' );
				}
			},
			
			insertTemplate: function( template, oldContent ) {
				var newContent,
					earliestPosition,
					testPosition1 = oldContent.search( /{{Επιχείρηση(?!Αποτίμηση)/ ),
					testPosition2 = oldContent.search( /{{WP(?!BS*\|)/ );
				if ( testPosition1 === -1 && testPosition2 === -1 ) {
					// No existing assessments
					if ( oldContent.search( /^{{/ ) === 0 ) {
						newContent = template + "\n" + oldContent;
					} else {
						newContent = template + "\n\n" + oldContent;
					}
				} else {
					// Talk page already includes assessments
					if ( testPosition1 > -1 ) {
						if ( testPosition2 > -1 ) {
							earliestPosition = Math.min( testPosition1, testPosition2 );
						} else {
							earliestPosition = testPosition1;
						}
					} else {
						earliestPosition = testPosition2;
					}
					newContent = oldContent.substr( 0, earliestPosition ) +
						template + "\n" +
						oldContent.substr( earliestPosition );
				}
				return newContent;
			},

			editTalkPage: function( summary, newContent ) {
				$.ajax({
					url: mw.util.wikiScript( 'api' ),
					data: {
						action: 'edit',
						title: wikiAssess.pageName,
						summary: summary,
						text: newContent,
						format: 'json',
						token: mw.user.tokens.get( 'csrfToken' ),
					},
					dataType: 'json',
					type: 'POST',
					success: function( data ) {
						if ( data.edit !== undefined && data.edit.result == "Επιτυχία" ) {
							window.location.reload();
						} else {
							wikiAssess.displayError( 'Unknown result from API during edit attempt.' );
						}
					},
					error: function( xhr ) {
						wikiAssess.displayError( 'Αποτυχημένη επεξεργασία.' );
						//console.debug( xhr.responseText );
					}
				});
			},

			launchDialog: function() {
				// Restore dialog to original state
				$assessInterface.find( '.assess-progress, .assess-error' ).remove();
				$assessInterface.find( 'div' ).show();
				// Open the dialog box
				$assessInterface.dialog( 'open' );
			},

			initialize: function() {
				var classList = [
						'',
						'Προς επέκταση',
						'Έναρξη',
						'Γ',
						'Β',
						'ΚΛ',
						'Α',
						'ΑΑ',
						'Αποσαφ',
						'NA'
					];

				// Define assessment interface
				$assessInterface = $('<div id="assessment-form" style="position:relative;"></div>')
					.append( $('<div style="margin-top:0.4em;"></div>').html( 'ΒικιΕπιχείρηση: ' )
						.append( $('<select id="projectSelect" style="padding:1px;vertical-align:baseline;"></select>') )
					)
					.append( $('<div style="margin-top:0.4em;"></div>').html( 'Τάξη: ' )
						.append( $('<select id="classSelect" style="padding:1px;vertical-align:baseline;"></select>') )
					)
					.append( $('<div style="margin-top:0.4em;"></div>').html( 'Σπουδαιότητα: ' )
						.append( $('<input type="radio" name="importance" value="Χαμηλή" />') ).append( '&nbsp;Χαμηλή  ' )
						.append( $('<input type="radio" name="importance" value="Μεσαία" />') ).append( '&nbsp;Μεσαία  ' )
						.append( $('<input type="radio" name="importance" value="Υψηλή" />') ).append( '&nbsp;Υψηλή  ' )
						.append( $('<input type="radio" name="importance" value="Ύψιστη" />') ).append( '&nbsp;Ύψιστη  ' )
					)
					.dialog({
						width: 500,
						autoOpen: false,
						title: 'Αποτίμηση του λήμματος',
						modal: true,
						buttons: { "Προσθήκη αποτίμησης": wikiAssess.addAssessment }
					});

				// Populate WikiProject select list
				$.each( wikiProjectList, function(index, value) {
					if ( mw.cookie.get( 'assessmentHelper' ) === value ) {
						$('#projectSelect').append( $('<option></option>').val(value).html(value).attr('selected', 'selected') );
					} else {
						$('#projectSelect').append( $('<option></option>').val(value).html(value) );
					}
				});

				// Populate class select list
				$.each( classList, function(index, value) { $('#classSelect').append( $('<option></option>').val(value).html(value) ); });

				// Insert 'Assess' link into page interface
				$( '#ca-protect' ).after( $( '<li>' ).html( '<a title="Assess this article" onclick="wikiAssess.launchDialog(); return false;" href="#">Assess</a>') );

			} // close initialize function

		} // close wikiAssess object
		wikiAssess.initialize();
	}) // close mw.loader
} // close if
//</nowiki>