mediawiki-extensions-Visual.../modules/ve-mw/dm/nodes/ve.dm.MWBlockImageNode.js
Roan Kattouw 92c38eab85 The great directory split of 2013
Move all MW-specific files into the ve-mw directory, in preparation
for moving them out into a separate repo.

All MW-specific files were moved into a parallel directory structure
in modules/ve-mw . Files with both generic and MW-specific things were
split up. Files in ve/init/mw/ were moved to ve-mw/init/ rather than
ve-mw/init/mw ; they're still named ve.init.mw.* but we should change
that. Some of the test files for core classes had MW-specific test cases,
so those were split up and the test runner was duplicated; we should
refactor our tests to use data providers so we can add cases more easily.

Split files:
* ve.ce.Node.css
* ve.ce.ContentBranchNode.test.js (MWEntityNode)
* ve.ce.Document.test.js (some core test cases genericized)
* ve.dm.InternalList.test.js (uses mwReference test document)
* ve.dm.SurfaceFragment.test.js, ve.ui.FormatAction.test.js
** Made core tests use heading instead of mwHeading
** Updated core tests because normal headings don't break out of lists
** Moved test runners into ve.test.utils.js
* ve.ui.Icons-*.css
* ve.ui.Dialog.css (MW parts into ve.ui.MWDialog.css)
* ve.ui.Tool.css
* ve.ui.Widget.css (move ve-ui-rtl and ve-ui-ltr to ve.ui.css)

ve.dm.Converter.test.js: Moved runner functions into ve.test.utils.js

ve.dm.example.js:
* Refactored createExampleDocument so mwExample can use it
* Removed wgExtensionAssetsPath detection, moved into mw-preload.js
* Genericized withMeta example document (original version copied to mwExample)
* Moved references example document to mwExample

ve.dm.mwExample.js:
* Move withMeta and references example documents from ve.dm.example.js
* Add createExampleDocument function

ve-mw/test/index.php: Runner for MW-specific tests only

ve-mw/test/mw-preload.js: Sets VE_TESTDIR for Special:JavaScriptTest only

ve.ui.Window.js:
* Remove magic path interpolation in addLocalStyleSheets()
* Pass full(er) paths to addLocalStyleSheets(), here and in subclasses

ve.ui.MWDialog.js: Subclass of Dialog that adds MW versions of stylesheets

ve.ui.MW*Dialog.js:
* Subclass MWDialog rather than Dialog
* Load both core and MW versions of stylesheets that have both

ve.ui.PagedDialog.js: Converted to a mixin rather than an abstract base class
* Don't inherit ve.ui.Dialog
* Rather than overriding initialize(), provide initializePages() which the
  host class is supposed to call from its initialize()
* Rename onOutlineSelect to onPageOutlineSelect

ve.ui.MWMetaDialog.js, ve.ui.MWTransclusionDialog.js:
* Use PagedDialog as a mixin rather than a base class, inherit MWDialog

bullet-icon.png: Unused, deleted

Stuff we should do later:
* Refactor tests to use data providers
* Write utility function for SVG compat check
* Separate omnibus CSS files such as ve.ui.Widget.css
* Separate omnibus RL modules
* Use icon classes in ViewPageTarget

Change-Id: I1b28f8ba7f2d2513e5c634927a854686fb9dd5a5
2013-07-02 20:51:38 -07:00

219 lines
6.2 KiB
JavaScript

/*!
* VisualEditor DataModel MWBlockImageNode class.
*
* @copyright 2011-2013 VisualEditor Team and others; see AUTHORS.txt
* @license The MIT License (MIT); see LICENSE.txt
*/
/**
* DataModel MediaWiki image node.
*
* @class
* @extends ve.dm.BranchNode
* @constructor
* @param {number} [length] Length of content data in document
* @param {Object} [element] Reference to element in linear model
*/
ve.dm.MWBlockImageNode = function VeDmMWBlockImageNode( length, element ) {
ve.dm.BranchNode.call( this, 0, element );
};
/* Inheritance */
ve.inheritClass( ve.dm.MWBlockImageNode, ve.dm.BranchNode );
/* Static Properties */
ve.dm.MWBlockImageNode.static.name = 'mwBlockImage';
ve.dm.MWBlockImageNode.static.storeHtmlAttributes = {
'blacklist': [ 'typeof', 'class', 'src', 'resource', 'width', 'height', 'href', 'rel' ]
};
ve.dm.MWBlockImageNode.static.handlesOwnChildren = true;
ve.dm.MWBlockImageNode.static.childNodeTypes = [ 'mwImageCaption' ];
ve.dm.MWBlockImageNode.static.matchTagNames = [ 'figure' ];
ve.dm.MWBlockImageNode.static.matchRdfaTypes = [
'mw:Image',
'mw:Image/Thumb',
'mw:Image/Frame',
'mw:Image/Frameless'
];
ve.dm.MWBlockImageNode.static.toDataElement = function ( domElements, converter ) {
var $figure = $( domElements[0] ),
$a = $figure.children( 'a' ).eq( 0 ),
$img = $a.children( 'img' ).eq( 0 ),
$caption = $figure.children( 'figcaption' ).eq( 0 ),
typeofAttr = $figure.attr( 'typeof' ),
classes = $figure.attr( 'class' ),
recognizedClasses = [],
attributes = {
href: $a.attr( 'href' ),
src: $img.attr( 'src' ),
width: $img.attr( 'width' ),
height: $img.attr( 'height' ),
resource: $img.attr( 'resource' ),
originalClasses: classes
};
// Extract individual classes
classes = typeof classes === 'string' ? classes.trim().split( /\s+/ ) : [];
// Type
switch ( typeofAttr ) {
case 'mw:Image/Thumb':
attributes.type = 'thumb';
break;
case 'mw:Image/Frame':
attributes.type = 'frame';
break;
case 'mw:Image/Frameless':
attributes.type = 'frameless';
break;
case 'mw:Image':
attributes.type = 'none';
break;
}
// Horizontal alignment
if ( classes.indexOf( 'mw-halign-left' ) !== -1 ) {
attributes.align = 'left';
recognizedClasses.push( 'mw-halign-left' );
} else if ( classes.indexOf( 'mw-halign-right' ) !== -1 ) {
attributes.align = 'right';
recognizedClasses.push( 'mw-halign-right' );
} else if ( classes.indexOf( 'mw-halign-center' ) !== -1 ) {
attributes.align = 'center';
recognizedClasses.push( 'mw-halign-center' );
} else if ( classes.indexOf( 'mw-halign-none' ) !== -1 ) {
attributes.align = 'none';
recognizedClasses.push( 'mw-halign-none' );
} else {
attributes.align = 'default';
}
// Default-size
if ( classes.indexOf( 'mw-default-size' ) !== -1 ) {
attributes.defaultSize = true;
recognizedClasses.push( 'mw-default-size' );
}
// Store unrecognized classes so we can restore them on the way out
attributes.unrecognizedClasses = ve.simpleArrayDifference( classes, recognizedClasses );
if ( $caption.length === 0 ) {
return [
{ 'type': 'mwBlockImage', 'attributes': attributes },
{ 'type': 'mwImageCaption' },
{ 'type': '/mwImageCaption' },
{ 'type': '/mwBlockImage' }
];
} else {
return [ { 'type': 'mwBlockImage', 'attributes': attributes } ].
concat( converter.getDataFromDomRecursionClean( $caption[0], { 'type': 'mwImageCaption' } ) ).
concat( [ { 'type': '/mwBlockImage' } ] );
}
};
// TODO: Consider using jQuery instead of pure JS.
// TODO: At this moment node is not resizable but when it will be then adding defaultSize class
// should be more conditional.
ve.dm.MWBlockImageNode.static.toDomElements = function ( data, doc, converter ) {
var dataElement = data[0],
figure = doc.createElement( 'figure' ),
a = doc.createElement( 'a' ),
img = doc.createElement( 'img' ),
wrapper = doc.createElement( 'div' ),
classes = [],
originalClasses = dataElement.attributes.originalClasses,
captionData = data.slice( 1, -1 );
// Type
switch ( dataElement.attributes.type ) {
case 'thumb':
figure.setAttribute( 'typeof', 'mw:Image/Thumb' );
break;
case 'frame':
figure.setAttribute( 'typeof', 'mw:Image/Frame' );
break;
case 'frameless':
figure.setAttribute( 'typeof', 'mw:Image/Frameless' );
break;
case 'none':
figure.setAttribute( 'typeof', 'mw:Image' );
break;
}
// Default-size
if ( dataElement.attributes.defaultSize === true ) {
classes.push( 'mw-default-size' );
}
// Horizontal alignment
switch ( dataElement.attributes.align ) {
case 'left':
classes.push( 'mw-halign-left' );
break;
case 'right':
classes.push( 'mw-halign-right' );
break;
case 'center':
classes.push( 'mw-halign-center' );
break;
case 'none':
classes.push( 'mw-halign-none' );
break;
}
if ( dataElement.attributes.unrecognizedClasses ) {
classes = ve.simpleArrayUnion( classes, dataElement.attributes.unrecognizedClasses );
}
if (
originalClasses &&
ve.compare( originalClasses.trim().split( /\s+/ ).sort(), classes.sort() )
) {
figure.className = originalClasses;
} else if ( classes.length > 0 ) {
figure.className = classes.join( ' ' );
}
a.setAttribute( 'href', dataElement.attributes.href );
img.setAttribute( 'src', dataElement.attributes.src );
img.setAttribute( 'width', dataElement.attributes.width );
img.setAttribute( 'height', dataElement.attributes.height );
img.setAttribute( 'resource', dataElement.attributes.resource );
figure.appendChild( a );
a.appendChild( img );
// If length of captionData is smaller or equal to 2 it means that there is no caption or that
// it is empty - in both cases we are going to skip appending <figcaption>.
if ( captionData.length > 2 ) {
converter.getDomSubtreeFromData( data.slice( 1, -1 ), wrapper );
while ( wrapper.firstChild ) {
figure.appendChild( wrapper.firstChild );
}
}
return [ figure ];
};
/* Methods */
/**
* Get the caption node of the image.
*
* @method
* @return {ve.dm.MWImageCaptionNode|null} Caption node, if present
*/
ve.dm.MWBlockImageNode.prototype.getCaptionNode = function() {
var node = this.children[0];
return node instanceof ve.dm.MWImageCaptionNode ? node : null;
};
/* Registration */
ve.dm.modelRegistry.register( ve.dm.MWBlockImageNode );