2013-07-08 21:20:31 +00:00
|
|
|
/*!
|
|
|
|
* VisualEditor UserInterface LanguageInspector class.
|
|
|
|
*
|
|
|
|
* @copyright 2011-2013 VisualEditor Team and others; see AUTHORS.txt
|
|
|
|
* @license The MIT License (MIT); see LICENSE.txt
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Language inspector.
|
|
|
|
*
|
|
|
|
* @class
|
|
|
|
* @extends ve.ui.AnnotationInspector
|
|
|
|
*
|
|
|
|
* @constructor
|
2013-11-05 00:29:50 +00:00
|
|
|
* @param {ve.ui.WindowSet} windowSet Window set this inspector is part of
|
2013-09-25 10:21:09 +00:00
|
|
|
* @param {Object} [config] Configuration options
|
2013-07-08 21:20:31 +00:00
|
|
|
*/
|
2013-10-09 19:59:03 +00:00
|
|
|
ve.ui.LanguageInspector = function VeUiLanguageInspector( windowSet, config ) {
|
2013-07-08 21:20:31 +00:00
|
|
|
// Parent constructor
|
2013-10-09 19:59:03 +00:00
|
|
|
ve.ui.AnnotationInspector.call( this, windowSet, config );
|
2013-07-08 21:20:31 +00:00
|
|
|
|
2013-08-10 09:27:20 +00:00
|
|
|
// Placeholder for the dm properties:
|
2013-09-23 18:41:34 +00:00
|
|
|
this.lang = '';
|
|
|
|
this.dir = '';
|
2013-08-10 09:27:20 +00:00
|
|
|
|
|
|
|
// Placeholder for the annotation:
|
|
|
|
this.annotation = null;
|
2013-07-08 21:20:31 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/* Inheritance */
|
|
|
|
|
2013-10-11 21:44:09 +00:00
|
|
|
OO.inheritClass( ve.ui.LanguageInspector, ve.ui.AnnotationInspector );
|
2013-07-08 21:20:31 +00:00
|
|
|
|
|
|
|
/* Static properties */
|
|
|
|
|
2013-08-27 23:28:29 +00:00
|
|
|
ve.ui.LanguageInspector.static.name = 'language';
|
|
|
|
|
2013-07-08 21:20:31 +00:00
|
|
|
ve.ui.LanguageInspector.static.icon = 'language';
|
|
|
|
|
|
|
|
ve.ui.LanguageInspector.static.titleMessage = 'visualeditor-languageinspector-title';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Annotation models this inspector can edit.
|
|
|
|
*
|
|
|
|
* @static
|
|
|
|
* @property {Function[]}
|
|
|
|
*/
|
|
|
|
ve.ui.LanguageInspector.static.modelClasses = [ ve.dm.LanguageAnnotation ];
|
|
|
|
|
|
|
|
/* Methods */
|
|
|
|
|
|
|
|
/**
|
2013-11-05 00:29:50 +00:00
|
|
|
* Get the annotation.
|
2013-07-08 21:20:31 +00:00
|
|
|
*
|
|
|
|
* @method
|
2013-11-05 00:29:50 +00:00
|
|
|
* @returns {ve.dm.LanguageAnnotation} Language annotation
|
2013-07-08 21:20:31 +00:00
|
|
|
*/
|
2013-11-05 00:29:50 +00:00
|
|
|
ve.ui.LanguageInspector.prototype.getAnnotation = function () {
|
|
|
|
return this.annotation;
|
2013-07-08 21:20:31 +00:00
|
|
|
};
|
2013-08-10 09:27:20 +00:00
|
|
|
|
|
|
|
/**
|
2013-11-05 00:29:50 +00:00
|
|
|
* @inheritdoc
|
2013-08-10 09:27:20 +00:00
|
|
|
*/
|
2013-11-05 00:29:50 +00:00
|
|
|
ve.ui.LanguageInspector.prototype.getAnnotationFromText = function () {
|
|
|
|
return new ve.dm.LanguageAnnotation( {
|
2013-08-27 23:28:29 +00:00
|
|
|
'type': 'meta/language',
|
2013-11-05 00:29:50 +00:00
|
|
|
'attributes': {
|
|
|
|
'lang': this.lang,
|
|
|
|
'dir': this.dir
|
|
|
|
}
|
2013-08-10 09:27:20 +00:00
|
|
|
} );
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2013-11-05 00:29:50 +00:00
|
|
|
* Gets a language from the annotation.
|
2013-08-10 09:27:20 +00:00
|
|
|
*
|
|
|
|
* @method
|
2013-11-05 00:29:50 +00:00
|
|
|
* @param {ve.dm.LanguageAnnotation} annotation Language annotation
|
|
|
|
* @returns {string} Language
|
2013-08-10 09:27:20 +00:00
|
|
|
*/
|
2013-11-05 00:29:50 +00:00
|
|
|
ve.ui.LanguageInspector.prototype.getLanguageFromAnnotation = function ( annotation ) {
|
|
|
|
if ( annotation instanceof ve.dm.LanguageAnnotation ) {
|
|
|
|
return annotation.getAttribute( 'lang' );
|
|
|
|
}
|
|
|
|
return '';
|
2013-08-10 09:27:20 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2013-11-05 00:29:50 +00:00
|
|
|
* Set the annotation.
|
2013-08-10 09:27:20 +00:00
|
|
|
*
|
2013-11-05 00:29:50 +00:00
|
|
|
* Form values will be updated from information within the annotation.
|
|
|
|
*
|
|
|
|
* @param {ve.dm.LanguageAnnotation} annotation Annotation to set
|
2013-08-10 09:27:20 +00:00
|
|
|
* @chainable
|
|
|
|
*/
|
|
|
|
ve.ui.LanguageInspector.prototype.setAnnotation = function ( annotation ) {
|
2013-08-26 03:15:42 +00:00
|
|
|
var langCode, langDir, annData;
|
2013-08-10 09:27:20 +00:00
|
|
|
|
|
|
|
// Validate the given annotation:
|
2013-08-27 23:45:38 +00:00
|
|
|
if ( annotation ) {
|
|
|
|
// Give precedence to dir value if it already exists
|
|
|
|
// in the annotation:
|
|
|
|
langDir = annotation.getAttribute( 'dir' );
|
|
|
|
|
|
|
|
// Set language according to the one in the given annotation
|
|
|
|
// or leave blank if element has no language set
|
|
|
|
langCode = annotation.getAttribute( 'lang' );
|
|
|
|
} else {
|
|
|
|
// No annotation (empty text or collapsed fragment on empty line)
|
2013-09-23 18:41:34 +00:00
|
|
|
langCode = this.lang;
|
|
|
|
langDir = this.dir;
|
2013-08-27 23:45:38 +00:00
|
|
|
}
|
2013-08-10 09:27:20 +00:00
|
|
|
|
|
|
|
// If language exists, but dir is undefined/null,
|
|
|
|
// fix the dir in terms of language:
|
2013-09-23 18:41:34 +00:00
|
|
|
if ( langCode && !langDir && $.uls ) {
|
2013-08-26 03:15:42 +00:00
|
|
|
langDir = $.uls.data.getDir( langCode );
|
2013-08-10 09:27:20 +00:00
|
|
|
}
|
|
|
|
|
2013-08-26 03:15:42 +00:00
|
|
|
// Set the annotation data:
|
|
|
|
annData = {
|
2013-09-23 18:41:34 +00:00
|
|
|
'type': 'meta/language',
|
2013-08-26 03:15:42 +00:00
|
|
|
'attributes': {}
|
|
|
|
};
|
|
|
|
|
|
|
|
if ( langCode ) {
|
|
|
|
annData.attributes.lang = langCode;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( langDir ) {
|
|
|
|
annData.attributes.dir = langDir;
|
|
|
|
}
|
2013-08-10 09:27:20 +00:00
|
|
|
|
|
|
|
// Update the widget:
|
2013-08-26 03:15:42 +00:00
|
|
|
this.targetInput.setAttributes( langCode, langDir );
|
2013-08-10 09:27:20 +00:00
|
|
|
|
2013-08-26 03:15:42 +00:00
|
|
|
// Update inspector properties:
|
|
|
|
this.lang = langCode;
|
|
|
|
this.dir = langDir;
|
|
|
|
|
|
|
|
// Set up the annotation:
|
|
|
|
this.annotation = new ve.dm.LanguageAnnotation( annData );
|
2013-08-10 09:27:20 +00:00
|
|
|
|
|
|
|
return this;
|
|
|
|
};
|
|
|
|
|
2013-07-08 21:20:31 +00:00
|
|
|
/**
|
|
|
|
* @inheritdoc
|
|
|
|
*/
|
2013-11-05 00:29:50 +00:00
|
|
|
ve.ui.LanguageInspector.prototype.initialize = function () {
|
|
|
|
// Parent method
|
|
|
|
ve.ui.AnnotationInspector.prototype.initialize.call( this );
|
|
|
|
|
|
|
|
// Properties
|
|
|
|
this.targetInput = new ve.ui.LanguageInputWidget( {
|
|
|
|
'$': this.$,
|
|
|
|
'$overlay': this.surface.$localOverlay
|
2013-07-08 21:20:31 +00:00
|
|
|
} );
|
2013-11-05 00:29:50 +00:00
|
|
|
|
|
|
|
// Initialization
|
|
|
|
this.$form.append( this.targetInput.$element );
|
2013-07-08 21:20:31 +00:00
|
|
|
};
|
|
|
|
|
2013-08-10 09:27:20 +00:00
|
|
|
/**
|
2013-11-05 00:29:50 +00:00
|
|
|
* @inheritdoc
|
2013-08-10 09:27:20 +00:00
|
|
|
*/
|
2013-11-05 00:29:50 +00:00
|
|
|
ve.ui.LanguageInspector.prototype.setup = function ( data ) {
|
|
|
|
// Parent method
|
|
|
|
ve.ui.AnnotationInspector.prototype.setup.call( this, data );
|
|
|
|
|
|
|
|
var fragDOM,
|
|
|
|
fragment = this.surface.getModel().getFragment( null, true );
|
|
|
|
|
|
|
|
// Get the fragment documentView object (the real DOM object):
|
|
|
|
fragDOM = this.surface.getView().documentView.getNodeFromOffset( fragment.getRange( true ).start );
|
|
|
|
|
|
|
|
// Set initial parameters according to parent of the DOM object.
|
|
|
|
// This will be called only if the annotation doesn't already exist, setting
|
|
|
|
// the default value as the current language/dir of the selected text.
|
|
|
|
if ( fragDOM ) {
|
|
|
|
this.lang = fragDOM.$element.closest( '[lang]' ).attr( 'lang' );
|
|
|
|
this.dir = fragDOM.$element.closest( '[dir]' ).css( 'direction' );
|
2013-08-10 09:27:20 +00:00
|
|
|
}
|
2013-11-05 00:29:50 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritdoc
|
|
|
|
*/
|
|
|
|
ve.ui.LanguageInspector.prototype.ready = function () {
|
|
|
|
// Parent method
|
|
|
|
ve.ui.AnnotationInspector.prototype.ready.call( this );
|
|
|
|
|
|
|
|
// Initialization
|
|
|
|
this.setAnnotation( this.initialAnnotation );
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritdoc
|
|
|
|
*/
|
|
|
|
ve.ui.LanguageInspector.prototype.teardown = function ( data ) {
|
|
|
|
// Read the annotation values from the widget:
|
|
|
|
var attrs = this.targetInput.getAttributes();
|
|
|
|
|
|
|
|
// Set the annotation with the new attributes:
|
|
|
|
this.annotation = new ve.dm.LanguageAnnotation( {
|
|
|
|
'type': 'meta/language',
|
|
|
|
'attributes': attrs
|
|
|
|
} );
|
|
|
|
|
|
|
|
// Parent method
|
|
|
|
ve.ui.AnnotationInspector.prototype.teardown.call( this, data );
|
2013-08-10 09:27:20 +00:00
|
|
|
};
|
|
|
|
|
2013-07-08 21:20:31 +00:00
|
|
|
/* Registration */
|
|
|
|
|
2013-08-27 23:28:29 +00:00
|
|
|
ve.ui.inspectorFactory.register( ve.ui.LanguageInspector );
|