2012-05-21 19:37:30 +00:00
|
|
|
/**
|
|
|
|
* Creates an ve.ui.LinkInspector object.
|
|
|
|
*
|
|
|
|
* @class
|
|
|
|
* @constructor
|
|
|
|
* @param {ve.ui.Toolbar} toolbar
|
|
|
|
*/
|
|
|
|
ve.ui.LinkInspector = function( toolbar, context ) {
|
|
|
|
// Inheritance
|
|
|
|
ve.ui.Inspector.call( this, toolbar, context );
|
|
|
|
// Properties
|
|
|
|
this.$clearButton = $( '<div class="es-inspector-button es-inspector-clearButton"></div>' )
|
|
|
|
.prependTo( this.$ );
|
|
|
|
this.$.prepend( '<div class="es-inspector-title">Edit link</div>' );
|
|
|
|
this.$locationLabel = $( '<label>Page title</label>' ).appendTo( this.$form );
|
|
|
|
this.$locationInput = $( '<input type="text">' ).appendTo( this.$form );
|
|
|
|
this.initialValue = null;
|
|
|
|
|
|
|
|
// Events
|
|
|
|
var _this = this;
|
|
|
|
this.$clearButton.click( function() {
|
|
|
|
if ( $(this).is( '.es-inspector-button-disabled' ) ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-06-13 21:31:33 +00:00
|
|
|
var surfaceModel = _this.context.getSurfaceView().getModel(),
|
|
|
|
annotations = _this.getSelectedLinkAnnotations();
|
2012-06-12 20:52:42 +00:00
|
|
|
// If link annotation exists, clear it.
|
2012-06-13 21:31:33 +00:00
|
|
|
for ( var hash in annotations ) {
|
|
|
|
surfaceModel.annotate( 'clear', annotations[hash] );
|
2012-06-12 20:52:42 +00:00
|
|
|
}
|
2012-05-21 19:37:30 +00:00
|
|
|
|
|
|
|
_this.$locationInput.val( '' );
|
|
|
|
_this.context.closeInspector();
|
|
|
|
} );
|
|
|
|
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 );
|
|
|
|
} );
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Methods */
|
|
|
|
|
2012-06-13 21:31:33 +00:00
|
|
|
ve.ui.LinkInspector.prototype.getSelectedLinkAnnotations = function(){
|
2012-05-21 19:37:30 +00:00
|
|
|
var surfaceView = this.context.getSurfaceView(),
|
|
|
|
surfaceModel = surfaceView.getModel(),
|
|
|
|
documentModel = surfaceModel.getDocument(),
|
2012-06-07 00:49:41 +00:00
|
|
|
data = documentModel.getData( surfaceModel.getSelection() );
|
2012-06-11 21:50:52 +00:00
|
|
|
|
2012-05-21 19:37:30 +00:00
|
|
|
if ( data.length ) {
|
2012-06-11 21:50:52 +00:00
|
|
|
if ( ve.isPlainObject( data[0][1] ) ) {
|
2012-06-13 21:31:33 +00:00
|
|
|
var annotations = ve.dm.Document.getMatchingAnnotations( data[0][1], /link\/.*/ );
|
|
|
|
for ( var hash in annotations ) {
|
|
|
|
return annotations[hash];
|
2012-06-07 00:49:41 +00:00
|
|
|
}
|
2012-05-21 19:37:30 +00:00
|
|
|
}
|
|
|
|
}
|
2012-06-11 21:50:52 +00:00
|
|
|
return ;
|
|
|
|
};
|
|
|
|
|
|
|
|
ve.ui.LinkInspector.prototype.getTitleFromSelection = function() {
|
2012-06-13 21:31:33 +00:00
|
|
|
var annotations = this.getSelectedLinkAnnotations();
|
|
|
|
for ( var hash in annotations ) {
|
|
|
|
// Use the first one that has a title (there should only be one, but this is just in case)
|
|
|
|
if ( annotations[hash].data && annotations[hash].data.title ) {
|
|
|
|
return annotations[hash].data.title;
|
|
|
|
}
|
2012-06-11 21:50:52 +00:00
|
|
|
}
|
2012-05-21 19:37:30 +00:00
|
|
|
return null;
|
|
|
|
};
|
|
|
|
|
|
|
|
ve.ui.LinkInspector.prototype.onOpen = function() {
|
|
|
|
var title = this.getTitleFromSelection();
|
|
|
|
if ( title !== null ) {
|
|
|
|
this.$locationInput.val( title );
|
|
|
|
this.$clearButton.removeClass( 'es-inspector-button-disabled' );
|
|
|
|
} else {
|
|
|
|
this.$locationInput.val( '' );
|
|
|
|
this.$clearButton.addClass( 'es-inspector-button-disabled' );
|
|
|
|
}
|
|
|
|
this.$acceptButton.addClass( 'es-inspector-button-disabled' );
|
|
|
|
this.initialValue = this.$locationInput.val();
|
|
|
|
var _this = this;
|
|
|
|
setTimeout( function() {
|
|
|
|
_this.$locationInput.focus().select();
|
|
|
|
}, 0 );
|
|
|
|
};
|
|
|
|
|
|
|
|
ve.ui.LinkInspector.prototype.onClose = function( accept ) {
|
|
|
|
if ( accept ) {
|
|
|
|
var title = this.$locationInput.val();
|
|
|
|
if ( title === this.getTitleFromSelection() || !title ) {
|
|
|
|
return;
|
|
|
|
}
|
2012-06-11 21:50:52 +00:00
|
|
|
var surfaceModel = this.context.getSurfaceView().getModel(),
|
2012-06-13 21:31:33 +00:00
|
|
|
annotations = this.getSelectedLinkAnnotations();
|
2012-06-11 21:50:52 +00:00
|
|
|
|
2012-06-12 20:52:42 +00:00
|
|
|
// Clear link annotation if it exists
|
2012-06-13 21:31:33 +00:00
|
|
|
for ( var hash in annotations ) {
|
|
|
|
surfaceModel.annotate( 'clear', annotations[hash] );
|
2012-06-12 20:52:42 +00:00
|
|
|
}
|
2012-06-11 19:55:16 +00:00
|
|
|
surfaceModel.annotate( 'set', { 'type': 'link/wikiLink', 'data': { 'title': title } } );
|
2012-05-21 19:37:30 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Inheritance */
|
|
|
|
|
|
|
|
ve.extendClass( ve.ui.LinkInspector, ve.ui.Inspector );
|