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 );
|
2011-12-09 01:28:44 +00:00
|
|
|
// Properties
|
2011-12-09 23:04:55 +00:00
|
|
|
this.$clearButton = $( '<div class="es-inspector-button es-inspector-clearButton"></div>' )
|
|
|
|
.prependTo( this.$ );
|
2012-06-20 01:20:28 +00:00
|
|
|
this.$.prepend(
|
|
|
|
$( '<div class="es-inspector-title"></div>' )
|
|
|
|
.text( ve.msg( 'visualeditor-linkinspector-title' ) )
|
|
|
|
);
|
|
|
|
this.$locationLabel = $( '<label></label>' )
|
|
|
|
.text( ve.msg( 'visualeditor-linkinspector-label-pagetitle' ) )
|
|
|
|
.appendTo( this.$form );
|
2011-12-09 01:28:44 +00:00
|
|
|
this.$locationInput = $( '<input type="text">' ).appendTo( this.$form );
|
2011-12-09 23:11:49 +00:00
|
|
|
this.initialValue = null;
|
2011-12-09 01:28:44 +00:00
|
|
|
|
|
|
|
// Events
|
|
|
|
var _this = this;
|
|
|
|
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-06-20 01:20:28 +00:00
|
|
|
var surfaceModel = _this.context.getSurfaceView().getModel(),
|
|
|
|
annotations = _this.getSelectedLinkAnnotations();
|
|
|
|
// If link annotation exists, clear it.
|
|
|
|
for ( var hash in annotations ) {
|
|
|
|
surfaceModel.annotate( 'clear', annotations[hash] );
|
|
|
|
}
|
2012-03-16 22:16:15 +00:00
|
|
|
|
2011-12-09 22:00:29 +00:00
|
|
|
_this.$locationInput.val( '' );
|
2011-12-09 01:28:44 +00:00
|
|
|
_this.context.closeInspector();
|
|
|
|
} );
|
2011-12-09 23:11:49 +00:00
|
|
|
this.$locationInput.bind( 'mousedown keydown cut paste', function() {
|
|
|
|
setTimeout( function() {
|
|
|
|
if ( _this.$locationInput.val() !== _this.initialValue ) {
|
|
|
|
_this.$acceptButton.removeClass( 'es-inspector-button-disabled' );
|
|
|
|
} else {
|
|
|
|
_this.$acceptButton.addClass( 'es-inspector-button-disabled' );
|
|
|
|
}
|
|
|
|
}, 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() {
|
|
|
|
var annotations = this.getSelectedLinkAnnotations();
|
|
|
|
for ( var hash in annotations ) {
|
|
|
|
// 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() {
|
|
|
|
var surfaceView = this.context.getSurfaceView(),
|
|
|
|
surfaceModel = surfaceView.getModel(),
|
|
|
|
documentModel = surfaceModel.getDocument(),
|
|
|
|
data = documentModel.getData( surfaceModel.getSelection() ),
|
|
|
|
str = '',
|
|
|
|
max = Math.min( data.length, 255 );
|
|
|
|
for ( var i = 0; i < max; i++ ) {
|
|
|
|
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-02-06 23:50:56 +00:00
|
|
|
ve.ui.LinkInspector.prototype.onOpen = function() {
|
2012-06-20 01:20:28 +00:00
|
|
|
var annotation = this.getAnnotationFromSelection();
|
2012-06-21 04:52:42 +00:00
|
|
|
var 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 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' );
|
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' );
|
|
|
|
}
|
|
|
|
|
2011-12-09 21:04:50 +00:00
|
|
|
var _this = this;
|
|
|
|
setTimeout( function() {
|
|
|
|
_this.$locationInput.focus().select();
|
|
|
|
}, 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-21 01:58:49 +00:00
|
|
|
var surfaceView = this.context.getSurfaceView();
|
2011-12-09 23:04:55 +00:00
|
|
|
if ( accept ) {
|
2012-06-20 01:20:28 +00:00
|
|
|
var target = this.$locationInput.val();
|
|
|
|
if ( target === this.initialValue || !target ) {
|
2011-12-09 23:04:55 +00:00
|
|
|
return;
|
|
|
|
}
|
2012-06-21 01:58:49 +00:00
|
|
|
var surfaceModel = surfaceView.getModel(),
|
2012-06-20 01:20:28 +00:00
|
|
|
annotations = this.getSelectedLinkAnnotations();
|
|
|
|
|
|
|
|
// Clear link annotation if it exists
|
|
|
|
for ( var hash in annotations ) {
|
|
|
|
surfaceModel.annotate( 'clear', annotations[hash] );
|
|
|
|
}
|
|
|
|
|
|
|
|
var annotation;
|
|
|
|
// Figure out if this is an internal or external link
|
|
|
|
// TODO better logic
|
2012-06-21 04:51:07 +00:00
|
|
|
if ( target.match( /^(https?:)?\/\// ) ) {
|
2012-06-20 01:20:28 +00:00
|
|
|
// External link
|
|
|
|
annotation = {
|
|
|
|
'type': 'link/extLink',
|
|
|
|
'data': { 'href': target }
|
|
|
|
};
|
|
|
|
} else {
|
|
|
|
// Internal link
|
|
|
|
annotation = {
|
|
|
|
'type': 'link/wikiLink',
|
|
|
|
'data': { 'title': target }
|
|
|
|
};
|
|
|
|
}
|
|
|
|
surfaceModel.annotate( 'set', annotation );
|
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
|
|
|
};
|
|
|
|
|
|
|
|
/* Inheritance */
|
|
|
|
|
2012-02-06 23:50:56 +00:00
|
|
|
ve.extendClass( ve.ui.LinkInspector, ve.ui.Inspector );
|