User:Ashwinpp/common.js

mw.loader.using( 'ext.visualEditor.desktopArticleTarget.init', function { 	mw.libs.ve.addPlugin( function  { /*!	 * VisualEditor UserInterface MWLinkSuggestionDialog class. *	 * @copyright 2011-2015 VisualEditor Team and others; see http://ve.mit-license.org */

/**	 * Find and replace dialog. *	 * @class * @extends ve.ui.ToolbarDialog *	 * @constructor * @param {Object} [config] Configuration options */	ve.ui.MWLinkSuggestionDialog = function VeUiMWLinkSuggestionDialog( config ) { // Parent constructor ve.ui.MWLinkSuggestionDialog.super.call( this, config ); };

/* Inheritance */

OO.inheritClass( ve.ui.MWLinkSuggestionDialog, ve.ui.FindAndReplaceDialog );

ve.ui.MWLinkSuggestionDialog.static.name = 'linkSuggestion';

/* Methods */

/**	 * @inheritdoc */	ve.ui.MWLinkSuggestionDialog.prototype.initialize = function { var navigateGroup, replaceGroup, doneButton; this.selectedLinkIndex = 0; this.links = [];

// Parent method ve.ui.MWLinkSuggestionDialog.super.prototype.initialize.call( this );

this.replaceButton.setLabel( 'Add link' ); //this.suggestionsSelect = new OO.ui.SelectWidget; this.selectedLinkLabel = new OO.ui.LabelWidget; this.resultsLabel = new OO.ui.LabelWidget; this.nextLinkButton = new OO.ui.ButtonWidget( {			classes: [ 've-ui-findAndReplaceDialog-cell' ],			label: "Next Link"		} ); this.previousLinkButton = new OO.ui.ButtonWidget( {			classes: [ 've-ui-findAndReplaceDialog-cell', ],			label: "Previous Link"		} ); this.navigateLinksGroup = new OO.ui.ButtonGroupWidget( {			classes: [ 've-ui-findAndReplaceDialog-cell', 've-linkSuggestion-linkNav-cell' ],			items: [				this.previousLinkButton,				this.selectedLinkLabel,				this.nextLinkButton			]		} ); navigateGroup = new OO.ui.ButtonGroupWidget( {			classes: [ 've-ui-findAndReplaceDialog-cell', 've-linkSuggestion-posNav-cell'],			items: [				this.previousButton,				this.resultsLabel,				this.nextButton			]		} ); replaceGroup = new OO.ui.ButtonGroupWidget( {			classes: [ 've-ui-findAndReplaceDialog-cell' ],			items: [				this.replaceButton			]		} ); // 'Done' button closes the dialog, but is not currently appended anywhere... doneButton = new OO.ui.ButtonWidget( {			classes: [ 've-ui-findAndReplaceDialog-cell' ],			label: ve.msg( 'visualeditor-find-and-replace-done' )		} );

// Events doneButton.connect( this, { click: 'close' } ); this.nextLinkButton.connect(this, {click: 'getNextLink'}); this.previousLinkButton.connect(this, {click: 'getPreviousLink'});

//this.suggestionsSelect.connect( this, { choose: 'onFindChange' } );

// Initialization this.$content.addClass( 've-ui-findAndReplaceDialog-content' ); this.$body .empty .append(				//this.suggestionsSelect.$element,				this.navigateLinksGroup.$element,				navigateGroup.$element,				replaceGroup.$element			); };

/**	 * @inheritdoc */	ve.ui.MWLinkSuggestionDialog.prototype.getSetupProcess = function ( data ) { data = data || {}; return ve.ui.MWLinkSuggestionDialog.super.prototype.getSetupProcess.call( this, data ) .next( function {				var title = ve.init.target.pageName;

//this.suggestionsSelect.clearItems;

$.ajax( '//tools.wmflabs.org/navlink-recommendation/' + title, {					dataType: 'jsonp'				} ).then( function ( response ) {					return response.recommendations;				} ).then( this.onSuggestionsReady.bind( this ) ); }, this );	};

/**	 * Suggestions from the API are ready *	 * @param {Array} recommendations List of recommendations */	/*ve.ui.MWLinkSuggestionDialog.prototype.onSuggestionsReady = function ( recommendations ) { var i, l, title, ranges, items = [];

for ( i = 0, l = recommendations.length; i < l; i++ ) { title = mw.Title.newFromText( recommendations[ i ].target ); if ( title.getNamespaceId ) { continue; }			ranges = this.findUnlinkedTitle( title ); if ( ranges.length ) { items.push( new OO.ui.OptionWidget( { label: title.getNameText, data: title } ) );			}		}		this.suggestionsSelect.addItems( items ); this.updateSize; };*/	ve.ui.MWLinkSuggestionDialog.prototype.onSuggestionsReady = function ( recommendations ) { var i, l, title, ranges;

for ( i = 0, l = recommendations.length; i < l; i++ ) { title = mw.Title.newFromText( recommendations[ i ].target ); if ( title.getNamespaceId ) { continue; }			ranges = this.findUnlinkedTitle( title ); if ( ranges.length ) { this.links.push(title); }		}		this.selectedLinkIndex = 0; this.updateLinkButtons; this.onFindChange; //this.suggestionsSelect.addItems( items ); this.updateSize; };	/**	 * Find an article title in the document that is unlinked, and whole word *	 * @param {mw.title} title Title object * @return {ve.Range[]} Ranges */	ve.ui.MWLinkSuggestionDialog.prototype.findUnlinkedTitle = function ( title ) { var linkFound = false, surfaceModel = this.surface.getModel, documentModel = surfaceModel.getDocument, text = title.getNameText, ranges = documentModel.findText( text, { wholeWord: true } );

// Filter out already linked words. // If one of the linked words is the requested title, return no results. ranges = ranges.filter( function ( range ) {			var link = surfaceModel.getLinearFragment( range ).getAnnotations.getAnnotationsByName( 'link/mwInternal' ).get( 0 );

if ( link && link.compareTo( ve.dm.MWInternalLinkAnnotation.static.newFromTitle( title ) ) ) { linkFound = true; }			return !link; } );		return linkFound ? [] : ranges;	};

/**	 * @inheritdoc */	ve.ui.MWLinkSuggestionDialog.prototype.getReadyProcess = function ( data ) { return ve.ui.MWLinkSuggestionDialog.super.super.prototype.getReadyProcess.call( this, data ) .next( function {				// focus			}, this ); };	/**	 * @inheritdoc */	ve.ui.MWLinkSuggestionDialog.prototype.updateLinkButtons = function { this.nextLinkButton.setDisabled(this.selectedLinkIndex >= this.links.length -1); this.previousLinkButton.setDisabled(this.selectedLinkIndex<=0); };	/**	 * @inheritdoc */	ve.ui.MWLinkSuggestionDialog.prototype.getNextLink = function { if(this.selectedLinkIndex0){ this.selectedLinkIndex-=1; }		this.onFindChange; };

/**	 * @inheritdoc */	ve.ui.MWLinkSuggestionDialog.prototype.onFindChange = function { this.updateFragments; this.renderFragments; this.highlightFocused( true ); };

/**	 * @inheritdoc */	ve.ui.MWLinkSuggestionDialog.prototype.updateFragments = function { var i, l,			surfaceModel = this.surface.getModel, ranges = [], //item = this.suggestionsSelect.getSelectedItem, item = this.links[this.selectedLinkIndex]; findText = item && item.getNameText; this.selectedLinkLabel.setLabel(item && item.getNameText);

this.fragments = []; if ( findText ) { ranges = this.findUnlinkedTitle( mw.Title.newFromText( findText ) ); for ( i = 0, l = ranges.length; i < l; i++ ) { this.fragments.push( surfaceModel.getLinearFragment( ranges[ i ], true, true ) ); }		}		this.results = this.fragments.length; this.focusedIndex = 0; this.nextButton.setDisabled( !this.results ); this.previousButton.setDisabled( !this.results ); this.replaceButton.setDisabled( !this.results ); this.updateLinkButtons; };

/**	 * @inheritdoc */	ve.ui.MWLinkSuggestionDialog.prototype.highlightFocused = function { // Parent method ve.ui.MWLinkSuggestionDialog.super.prototype.highlightFocused.apply( this, arguments );

if ( this.results ) { this.resultsLabel.setLabel(				ve.msg( 'visualeditor-find-and-replace-results', this.focusedIndex + 1, this.results )			); } else { this.resultsLabel.setLabel( '' ); return; }	};

/**	 * @inheritdoc */	ve.ui.MWLinkSuggestionDialog.prototype.replace = function ( index ) { this.fragments[ index ].annotateContent(			'set',			ve.dm.MWInternalLinkAnnotation.static.newFromTitle( this.links[this.selectedLinkIndex] )		); };

/* Registration */

ve.ui.windowFactory.register( ve.ui.MWLinkSuggestionDialog );

/* Toolbar */

ve.ui.MWLinkSuggestionDialogTool = function VeUiMWLinkSuggestionDialogTool { ve.ui.MWLinkSuggestionDialogTool.super.apply( this, arguments ); };	OO.inheritClass( ve.ui.MWLinkSuggestionDialogTool, ve.ui.ToolbarDialogTool ); ve.ui.MWLinkSuggestionDialogTool.static.name = 'linkSuggestion'; ve.ui.MWLinkSuggestionDialogTool.static.group = 'dialog'; ve.ui.MWLinkSuggestionDialogTool.static.icon = 'find'; ve.ui.MWLinkSuggestionDialogTool.static.title = OO.ui.deferMsg( 'visualeditor-dialog-command-help-title' ); ve.ui.MWLinkSuggestionDialogTool.static.autoAddToCatchall = false; ve.ui.MWLinkSuggestionDialogTool.static.autoAddToGroup = false; ve.ui.MWLinkSuggestionDialogTool.static.commandName = 'linkSuggestion'; ve.ui.toolFactory.register( ve.ui.MWLinkSuggestionDialogTool );

ve.init.mw.DesktopArticleTarget.static.toolbarGroups.push( { include: [ 'linkSuggestion' ] } );

/* Command */

ve.ui.commandRegistry.register(		new ve.ui.Command( 'linkSuggestion', 'window', 'toggle', { args: [ 'linkSuggestion' ] } )	);

/* Auto-open */

mw.hook( 've.activationComplete' ).add( function {		var surface = ve.init.target.getSurface,			command = ve.init.target.commandRegistry.lookup( 'linkSuggestion' );

command.execute( surface ); } );

} ); } );