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-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();
|
|
|
|
if ( annotation === null ) {
|
2011-12-09 20:44:41 +00:00
|
|
|
this.$locationInput.val( '' );
|
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
|
|
|
|
this.$locationInput.val( annotation.data.title || '' );
|
|
|
|
this.$clearButton.removeClass( 'es-inspector-button-disabled' );
|
|
|
|
} else {
|
|
|
|
// External link
|
|
|
|
this.$locationInput.val( annotation.data.href || '' );
|
|
|
|
this.$clearButton.removeClass( 'es-inspector-button-disabled' );
|
2011-12-09 01:28:44 +00:00
|
|
|
}
|
2012-06-20 01:20:28 +00:00
|
|
|
|
2011-12-09 23:11:49 +00:00
|
|
|
this.$acceptButton.addClass( 'es-inspector-button-disabled' );
|
|
|
|
this.initialValue = this.$locationInput.val();
|
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 ) {
|
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-20 01:20:28 +00:00
|
|
|
var surfaceModel = this.context.getSurfaceView().getModel(),
|
|
|
|
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
|
|
|
|
if ( target.match( /^https?:\/\// ) ) {
|
|
|
|
// 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
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Inheritance */
|
|
|
|
|
2012-02-06 23:50:56 +00:00
|
|
|
ve.extendClass( ve.ui.LinkInspector, ve.ui.Inspector );
|