User:Atundo/prova2.js

/* Translate the following to your language: */ if (!mw.messages.exists( 've-EasyLinkDialog-title' )) { mw.messages.set({		've-EasyLinkDialog-title': 'Easy Link',		've-EasyLinkDialog-from-label': 'From:',		've-EasyLinkDialog-to-label': 'To:',		've-EasyLinkDialog-from-placeholder': 'From text',		've-EasyLinkDialog-to-placeholder': 'To text',		've-EasyLinkDialog-replaceAll': 'Analyze (HTML)',		've-EasyLinkDialog-replace': 'Analyze (Wikitext)',		've-EasyLinkDialog-matchcase': 'Match case',		've-EasyLinkDialog-replace-complete': 'Found and replaced $1 occurrences',		've-EasyLinkTool-ToolbarButton': 'Semantic links'	}); } /* end of translations */

/*! * VisualEditor EasyLink gadget */ function getWikitext{ ve.init.target.serialize(		ve.init.target.getSurface.getDom, function (wikitext) {			console.warn("###WIKITEXT###");			console.log(wikitext);		}	); }

function getHTML{ var DOMObj = ve.init.target.getSurface.getDom; console.warn("###outerHTML###"); console.log(DOMObj.body.outerHTML); console.warn("###outerTEXT###"); console.log(DOMObj.body.outerText); }

function extractText{ var nodes = []; var model = ve.init.target.getSurface.getModel; function getTextNodes( obj ) { var i;		for ( i = 0; i < obj.children.length; i++ ) { if ( obj.children[i].type == 'text'){ nodes.push(obj.children[i]); }

if ( obj.children[i].children ) { getTextNodes( obj.children[i] ); }		}	}	getTextNodes(ve.init.target.getSurface.getModel.getDocument.getDocumentNode); return nodes; }

function searchAndReplace( fromText, toText, replaceAll, matchCase ) { var textNodes = extractText; var model = ve.init.target.getSurface.getModel; var firstIndex = 0; var numReplacements = 0; for (var nodeI = 0; nodeI < textNodes.length; nodeI++) { var node = textNodes[nodeI]; var nodeRange = node.getRange; var nodeText = model.getLinearFragment(nodeRange).getText;

var fromIndex = matchCase? nodeText.toUpperCase.indexOf( fromText.toUpperCase, firstIndex ) : nodeText.indexOf( fromText, firstIndex ); if ( fromIndex == -1 ) { firstIndex = 0; continue; }		var start = nodeRange.from+fromIndex; var end = start+fromText.length; if (!replaceAll && model.selection.start > start) { continue;//skip replacements before selection }		var removeRange = new ve.Range( start, end ); var transaction = ve.dm.Transaction.newFromReplacement(			ve.init.target.getSurface.getView.getDocument.model, 			removeRange, 			toText		); var newSelection = new ve.Range(0,0); if (!replaceAll) { newSelection = new ve.Range( start, start+toText.length ); }		ve.init.target.getSurface.getView.changeModel(transaction, newSelection); numReplacements++; if (!replaceAll) { break; }		firstIndex = fromIndex + toText.length; nodeI = nodeI -1; }	if (numReplacements === 0 || replaceAll) { mw.notify( mw.msg( 've-EasyLinkDialog-replace-complete', numReplacements ) ); } }

ve.ui.EasyLinkDialog = function( manager, config ) { // Parent constructor ve.ui.EasyLinkDialog.super.call( this, manager, config );

}; /* Inheritance */

OO.inheritClass( ve.ui.EasyLinkDialog, ve.ui.FragmentDialog );

ve.ui.EasyLinkDialog.prototype.getActionProcess = function ( action ) { var fromVal = this.fromInput.getValue, toVal = this.toInput.getValue, matchCase = this.matchCaseCheckbox.getValue;

if ( action === 'replace' ) { return new OO.ui.Process( function {			//searchAndReplace( fromVal, toVal, false, matchCase );			getWikitext;		}, this ); } else if ( action === 'replace-all' ) { return new OO.ui.Process( function {			//searchAndReplace( fromVal, toVal, true, matchCase );			getHTML;			this.close;		}, this ); }	return ve.ui.MWMediaDialog.super.prototype.getActionProcess.call( this, action ); };

ve.ui.EasyLinkDialog.prototype.getBodyHeight = function { return 200; };

/* Static Properties */ ve.ui.EasyLinkDialog.static.name = 'semanticlinks'; ve.ui.EasyLinkDialog.static.title = mw.msg( 've-EasyLinkDialog-title' ); ve.ui.EasyLinkDialog.static.size = 'medium';

ve.ui.EasyLinkDialog.static.actions = [ {		'action': 'replace', 'label': mw.msg( 've-EasyLinkDialog-replace' ), 'flags': [ 'constructive' ], 'modes': 'insert' },	{		'label': OO.ui.deferMsg( 'visualeditor-dialog-action-cancel' ), 'flags': 'safe', 'modes': [ 'edit', 'insert', 'select' ] },	{		'action': 'replace-all', 'label': mw.msg( 've-EasyLinkDialog-replaceAll' ), 'flags': [ 'constructive' ], 'modes': 'insert' } ];

ve.ui.EasyLinkDialog.prototype.initialize = function { ve.ui.EasyLinkDialog.super.prototype.initialize.call( this ); this.panel = new OO.ui.PanelLayout( { '$': this.$, 'scrollable': true, 'padded': true } ); this.inputsFieldset = new OO.ui.FieldsetLayout( {		'$': this.$	} ); // input from this.fromInput = new OO.ui.TextInputWidget(		{ '$': this.$, 'multiline': false, 'placeholder': mw.msg( 've-EasyLinkDialog-from-placeholder' ) }	); //input to	this.toInput = new OO.ui.TextInputWidget(		{ '$': this.$, 'multiline': false, 'placeholder': mw.msg( 've-EasyLinkDialog-to-placeholder' ) }	); this.fromField = new OO.ui.FieldLayout( this.fromInput, {		'$': this.$,		'label': mw.msg( 've-EasyLinkDialog-from-label' )	} ); this.toField = new OO.ui.FieldLayout( this.toInput, {		'$': this.$,		'label': mw.msg( 've-EasyLinkDialog-to-label' )	} );

this.matchCaseCheckbox = new OO.ui.CheckboxInputWidget( {		'$': this.$	} ); var matchCaseField = new OO.ui.FieldLayout( this.matchCaseCheckbox, {		'$': this.$,		'align': 'inline',		'label': mw.msg( 've-EasyLinkDialog-matchcase' )	} );

this.inputsFieldset.$element.append(		this.fromField.$element,		this.toField.$element,		matchCaseField.$element	); this.panel.$element.append(	this.inputsFieldset.$element ); this.$body.append( this.panel.$element );

};

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

//-- replace tool -- function EasyLinkTool( toolGroup, config ) { OO.ui.Tool.call( this, toolGroup, config ); } OO.inheritClass( EasyLinkTool, OO.ui.Tool ); EasyLinkTool.static.name = 'EasyLinkTool'; EasyLinkTool.static.icon = 'settings'; EasyLinkTool.static.title = mw.msg('ve-EasyLinkTool-ToolbarButton'); EasyLinkTool.prototype.onSelect = function { this.toolbar.getSurface.execute( 'window', 'open', 'search', null ); }; EasyLinkTool.prototype.onUpdateState = function { this.setActive( false ); }; ve.ui.toolFactory.register( EasyLinkTool );