2012-07-19 00:11:26 +00:00
|
|
|
/**
|
|
|
|
* VisualEditor user interface LinkInspector class.
|
2012-07-19 21:25:16 +00:00
|
|
|
*
|
2012-07-19 00:11:26 +00:00
|
|
|
* @copyright 2011-2012 VisualEditor Team and others; see AUTHORS.txt
|
|
|
|
* @license The MIT License (MIT); see LICENSE.txt
|
|
|
|
*/
|
|
|
|
|
2011-12-09 01:28:44 +00:00
|
|
|
/**
|
2012-02-06 23:50:56 +00:00
|
|
|
* Creates an ve.ui.LinkInspector object.
|
2012-06-20 01:20:28 +00:00
|
|
|
*
|
2011-12-09 01:28:44 +00:00
|
|
|
* @class
|
|
|
|
* @constructor
|
2012-02-06 23:50:56 +00:00
|
|
|
* @param {ve.ui.Toolbar} toolbar
|
2011-12-09 01:28:44 +00:00
|
|
|
*/
|
2012-02-06 23:50:56 +00:00
|
|
|
ve.ui.LinkInspector = function( toolbar, context ) {
|
2011-12-09 01:28:44 +00:00
|
|
|
// Inheritance
|
2012-02-06 23:50:56 +00:00
|
|
|
ve.ui.Inspector.call( this, toolbar, context );
|
2012-07-03 20:28:10 +00:00
|
|
|
|
2011-12-09 01:28:44 +00:00
|
|
|
// Properties
|
2012-07-17 18:53:42 +00:00
|
|
|
this.$clearButton = $( '<div class="es-inspector-button es-inspector-clearButton"></div>', context.inspectorDoc )
|
2011-12-09 23:04:55 +00:00
|
|
|
.prependTo( this.$ );
|
2012-06-20 01:20:28 +00:00
|
|
|
this.$.prepend(
|
2012-07-17 18:53:42 +00:00
|
|
|
$( '<div class="es-inspector-title"></div>', context.inspectorDoc )
|
2012-06-20 01:20:28 +00:00
|
|
|
.text( ve.msg( 'visualeditor-linkinspector-title' ) )
|
|
|
|
);
|
2012-07-17 18:53:42 +00:00
|
|
|
this.$locationLabel = $( '<label></label>', context.inspectorDoc )
|
2012-06-20 01:20:28 +00:00
|
|
|
.text( ve.msg( 'visualeditor-linkinspector-label-pagetitle' ) )
|
|
|
|
.appendTo( this.$form );
|
2012-07-17 18:53:42 +00:00
|
|
|
this.$locationInput = $( '<input type="text">', context.inspectorDoc ).appendTo( this.$form );
|
2011-12-09 23:11:49 +00:00
|
|
|
this.initialValue = null;
|
2011-12-09 01:28:44 +00:00
|
|
|
|
|
|
|
// Events
|
2012-07-03 20:28:10 +00:00
|
|
|
var inspector = this;
|
2011-12-09 01:28:44 +00:00
|
|
|
this.$clearButton.click( function() {
|
2011-12-09 23:04:55 +00:00
|
|
|
if ( $(this).is( '.es-inspector-button-disabled' ) ) {
|
|
|
|
return;
|
|
|
|
}
|
2012-03-16 22:16:15 +00:00
|
|
|
|
2012-07-19 03:40:49 +00:00
|
|
|
var hash,
|
2012-07-03 20:28:10 +00:00
|
|
|
surfaceModel = inspector.context.getSurfaceView().getModel(),
|
|
|
|
annotations = inspector.getSelectedLinkAnnotations();
|
2012-06-20 01:20:28 +00:00
|
|
|
// If link annotation exists, clear it.
|
2012-07-19 03:40:49 +00:00
|
|
|
for ( hash in annotations ) {
|
2012-06-20 01:20:28 +00:00
|
|
|
surfaceModel.annotate( 'clear', annotations[hash] );
|
|
|
|
}
|
2012-07-03 20:28:10 +00:00
|
|
|
inspector.$locationInput.val( '' );
|
|
|
|
inspector.context.closeInspector();
|
2011-12-09 01:28:44 +00:00
|
|
|
} );
|
2011-12-09 23:11:49 +00:00
|
|
|
this.$locationInput.bind( 'mousedown keydown cut paste', function() {
|
|
|
|
setTimeout( function() {
|
2012-07-03 20:28:10 +00:00
|
|
|
if ( inspector.$locationInput.val() !== inspector.initialValue ) {
|
|
|
|
inspector.$acceptButton.removeClass( 'es-inspector-button-disabled' );
|
2011-12-09 23:11:49 +00:00
|
|
|
} else {
|
2012-07-03 20:28:10 +00:00
|
|
|
inspector.$acceptButton.addClass( 'es-inspector-button-disabled' );
|
2011-12-09 23:11:49 +00:00
|
|
|
}
|
|
|
|
}, 0 );
|
|
|
|
} );
|
2011-12-09 01:28:44 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/* Methods */
|
|
|
|
|
2012-06-20 01:20:28 +00:00
|
|
|
ve.ui.LinkInspector.prototype.getSelectedLinkAnnotations = function(){
|
2011-12-09 01:28:44 +00:00
|
|
|
var surfaceView = this.context.getSurfaceView(),
|
|
|
|
surfaceModel = surfaceView.getModel(),
|
|
|
|
documentModel = surfaceModel.getDocument(),
|
2012-06-20 01:20:28 +00:00
|
|
|
data = documentModel.getData( surfaceModel.getSelection() );
|
|
|
|
|
2011-12-09 01:28:44 +00:00
|
|
|
if ( data.length ) {
|
2012-06-20 01:20:28 +00:00
|
|
|
if ( ve.isPlainObject( data[0][1] ) ) {
|
|
|
|
return ve.dm.Document.getMatchingAnnotations( data[0][1], /link\/.*/ );
|
2011-12-09 01:28:44 +00:00
|
|
|
}
|
2012-06-20 01:20:28 +00:00
|
|
|
}
|
|
|
|
return {};
|
|
|
|
};
|
|
|
|
|
|
|
|
ve.ui.LinkInspector.prototype.getAnnotationFromSelection = function() {
|
2012-07-19 03:40:49 +00:00
|
|
|
var hash,
|
|
|
|
annotations = this.getSelectedLinkAnnotations();
|
|
|
|
for ( hash in annotations ) {
|
2012-06-20 01:20:28 +00:00
|
|
|
// Use the first one with a recognized type (there should only be one, but this is just in case)
|
|
|
|
if ( annotations[hash].type === 'link/wikiLink' || annotations[hash].type === 'link/extLink' ) {
|
|
|
|
return annotations[hash];
|
2011-12-09 01:28:44 +00:00
|
|
|
}
|
|
|
|
}
|
2011-12-09 23:04:55 +00:00
|
|
|
return null;
|
2011-12-09 01:28:44 +00:00
|
|
|
};
|
|
|
|
|
2012-06-21 02:43:46 +00:00
|
|
|
// TODO: This should probably be somewhere else but I needed this here for now.
|
|
|
|
ve.ui.LinkInspector.prototype.getSelectionText = function() {
|
2012-07-19 03:40:49 +00:00
|
|
|
var i,
|
|
|
|
surfaceView = this.context.getSurfaceView(),
|
2012-06-21 02:43:46 +00:00
|
|
|
surfaceModel = surfaceView.getModel(),
|
|
|
|
documentModel = surfaceModel.getDocument(),
|
|
|
|
data = documentModel.getData( surfaceModel.getSelection() ),
|
|
|
|
str = '',
|
|
|
|
max = Math.min( data.length, 255 );
|
2012-07-19 03:40:49 +00:00
|
|
|
for ( i = 0; i < max; i++ ) {
|
2012-06-21 02:43:46 +00:00
|
|
|
if ( ve.isArray( data[i] ) ) {
|
|
|
|
str += data[i][0];
|
2012-06-21 04:52:42 +00:00
|
|
|
} else if( typeof data[i] === 'string' ) {
|
2012-06-21 02:43:46 +00:00
|
|
|
str += data[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return str;
|
|
|
|
};
|
|
|
|
|
2012-07-03 20:28:10 +00:00
|
|
|
/*
|
|
|
|
* Method called prior to opening inspector which fixes up
|
|
|
|
* selection to contain the complete annotated link range
|
|
|
|
* OR unwrap outer whitespace from selection.
|
|
|
|
*/
|
|
|
|
ve.ui.LinkInspector.prototype.prepareOpen = function() {
|
|
|
|
var surfaceView = this.context.getSurfaceView(),
|
|
|
|
surfaceModel = surfaceView.getModel(),
|
|
|
|
doc = surfaceModel.getDocument(),
|
|
|
|
annotation = this.getAnnotationFromSelection(),
|
|
|
|
selection = surfaceModel.getSelection(),
|
|
|
|
newSelection;
|
|
|
|
|
|
|
|
if ( annotation !== null ) {
|
|
|
|
// Ensure that the entire annotated range is selected
|
|
|
|
newSelection = doc.getAnnotatedRangeFromOffset( selection.start, annotation );
|
|
|
|
// Apply selection direction to new selection
|
|
|
|
if ( selection.from > selection.start ) {
|
|
|
|
newSelection.flip();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// No annotation, trim outer space from range
|
|
|
|
newSelection = doc.trimOuterSpaceFromRange( selection );
|
|
|
|
}
|
|
|
|
|
|
|
|
surfaceModel.change( null, newSelection );
|
|
|
|
};
|
|
|
|
|
2012-02-06 23:50:56 +00:00
|
|
|
ve.ui.LinkInspector.prototype.onOpen = function() {
|
2012-06-25 22:40:16 +00:00
|
|
|
var annotation = this.getAnnotationFromSelection(),
|
2012-07-03 20:28:10 +00:00
|
|
|
initialValue = '';
|
2012-06-20 01:20:28 +00:00
|
|
|
if ( annotation === null ) {
|
2012-06-21 02:43:46 +00:00
|
|
|
this.$locationInput.val( this.getSelectionText() );
|
2011-12-09 23:04:55 +00:00
|
|
|
this.$clearButton.addClass( 'es-inspector-button-disabled' );
|
2012-06-20 01:20:28 +00:00
|
|
|
} else if ( annotation.type === 'link/wikiLink' ) {
|
|
|
|
// Internal link
|
2012-06-21 04:52:42 +00:00
|
|
|
initialValue = annotation.data.title || '';
|
|
|
|
this.$locationInput.val( initialValue );
|
2012-06-20 01:20:28 +00:00
|
|
|
this.$clearButton.removeClass( 'es-inspector-button-disabled' );
|
|
|
|
} else {
|
|
|
|
// External link
|
2012-06-21 19:40:28 +00:00
|
|
|
initialValue = annotation.data.href || '';
|
2012-06-21 04:52:42 +00:00
|
|
|
this.$locationInput.val( initialValue );
|
2012-06-20 01:20:28 +00:00
|
|
|
this.$clearButton.removeClass( 'es-inspector-button-disabled' );
|
2011-12-09 01:28:44 +00:00
|
|
|
}
|
2012-06-20 01:20:28 +00:00
|
|
|
|
2012-06-21 04:52:42 +00:00
|
|
|
this.initialValue = initialValue;
|
|
|
|
if ( this.$locationInput.val().length === 0 ) {
|
|
|
|
this.$acceptButton.addClass( 'es-inspector-button-disabled' );
|
|
|
|
} else {
|
|
|
|
this.$acceptButton.removeClass( 'es-inspector-button-disabled' );
|
|
|
|
}
|
2012-07-03 20:28:10 +00:00
|
|
|
|
|
|
|
setTimeout( ve.proxy( function() {
|
|
|
|
this.$locationInput.focus().select();
|
|
|
|
}, this ), 0 );
|
2011-12-09 01:28:44 +00:00
|
|
|
};
|
|
|
|
|
2012-02-06 23:50:56 +00:00
|
|
|
ve.ui.LinkInspector.prototype.onClose = function( accept ) {
|
2012-06-25 22:40:16 +00:00
|
|
|
var surfaceView = this.context.getSurfaceView(),
|
|
|
|
surfaceModel = surfaceView.getModel(),
|
|
|
|
annotations = this.getSelectedLinkAnnotations(),
|
|
|
|
target = this.$locationInput.val(),
|
|
|
|
hash, annotation;
|
2011-12-09 23:04:55 +00:00
|
|
|
if ( accept ) {
|
2012-06-20 01:20:28 +00:00
|
|
|
if ( target === this.initialValue || !target ) {
|
2011-12-09 23:04:55 +00:00
|
|
|
return;
|
|
|
|
}
|
2012-06-20 01:20:28 +00:00
|
|
|
// Clear link annotation if it exists
|
2012-06-25 22:40:16 +00:00
|
|
|
for ( hash in annotations ) {
|
2012-06-20 01:20:28 +00:00
|
|
|
surfaceModel.annotate( 'clear', annotations[hash] );
|
|
|
|
}
|
2012-06-25 22:40:16 +00:00
|
|
|
surfaceModel.annotate( 'set', ve.ui.LinkInspector.getAnnotationForTarget( target ) );
|
2011-12-09 01:28:44 +00:00
|
|
|
}
|
2012-06-21 01:58:49 +00:00
|
|
|
// Restore focus
|
|
|
|
surfaceView.getDocument().getDocumentNode().$.focus();
|
2011-12-09 01:28:44 +00:00
|
|
|
};
|
|
|
|
|
2012-06-25 22:40:16 +00:00
|
|
|
ve.ui.LinkInspector.getAnnotationForTarget = function( target ) {
|
2012-06-25 23:25:09 +00:00
|
|
|
var title;
|
2012-06-25 22:40:16 +00:00
|
|
|
// Figure out if this is an internal or external link
|
|
|
|
if ( target.match( /^(https?:)?\/\// ) ) {
|
|
|
|
// External link
|
|
|
|
return {
|
|
|
|
'type': 'link/extLink',
|
|
|
|
'data': { 'href': target }
|
|
|
|
};
|
|
|
|
} else {
|
|
|
|
// Internal link
|
2012-06-25 23:25:09 +00:00
|
|
|
// TODO in the longer term we'll want to have autocompletion and existence&validity
|
|
|
|
// checks using AJAX
|
|
|
|
try {
|
|
|
|
title = new mw.Title( target );
|
|
|
|
if ( title.getNamespaceId() === 6 || title.getNamespaceId() === 14 ) {
|
|
|
|
// File: or Category: link
|
|
|
|
// We have to prepend a colon so this is interpreted as a link
|
|
|
|
// rather than an image inclusion or categorization
|
|
|
|
target = ':' + target;
|
|
|
|
}
|
|
|
|
} catch ( e ) { }
|
|
|
|
|
2012-06-25 22:40:16 +00:00
|
|
|
return {
|
|
|
|
'type': 'link/wikiLink',
|
|
|
|
'data': { 'title': target }
|
|
|
|
};
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2011-12-09 01:28:44 +00:00
|
|
|
/* Inheritance */
|
|
|
|
|
2012-02-06 23:50:56 +00:00
|
|
|
ve.extendClass( ve.ui.LinkInspector, ve.ui.Inspector );
|