mediawiki-extensions-Visual.../modules/ve/ui/ve.ui.Window.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

348 lines
7.7 KiB
JavaScript

/*!
* VisualEditor UserInterface Window class.
*
* @copyright 2011-2013 VisualEditor Team and others; see AUTHORS.txt
* @license The MIT License (MIT); see LICENSE.txt
*/
/**
* UserInterface window.
*
* @class
* @abstract
* @extends ve.Element
* @mixins ve.EventEmitter
*
* @constructor
* @param {ve.ui.Surface} surface
* @param {Object} [config] Config options
* @emits initialize
*/
ve.ui.Window = function VeUiWindow( surface, config ) {
// Parent constructor
ve.Element.call( this, config );
// Mixin constructors
ve.EventEmitter.call( this );
// Properties
this.surface = surface;
this.visible = false;
this.opening = false;
this.closing = false;
this.frame = null;
this.$frame = this.$$( '<div>' );
// Initialization
this.$
.addClass( 've-ui-window' )
.append( this.$frame );
this.frame = new ve.ui.Frame( { 'stylesheets': this.constructor.static.stylesheets } );
this.$frame
.addClass( 've-ui-window-frame' )
.append( this.frame.$ );
// Events
this.frame.connect( this, { 'initialize': 'onFrameInitialize' } );
};
/* Inheritance */
ve.inheritClass( ve.ui.Window, ve.Element );
ve.mixinClass( ve.ui.Window, ve.EventEmitter );
/* Events */
/**
* @event initialize
*/
/**
* @event setup
* @param {ve.ui.Window} win Window that's been setup
*/
/**
* @event open
* @param {ve.ui.Window} win Window that's been opened
*/
/**
* @event close
* @param {ve.ui.Window} win Window that's been closed
* @param {string} action Action that caused the window to be closed
*/
/* Static Properties */
/**
* List of stylesheet URLs to load.
*
* @static
* @inheritable
* @property {Array}
*/
ve.ui.Window.static.stylesheets = [];
/**
* Symbolic name of icon.
*
* @static
* @inheritable
* @property {string}
*/
ve.ui.Window.static.icon = 'window';
/**
* Localized message for title.
*
* @static
* @inheritable
* @property {string}
*/
ve.ui.Window.static.titleMessage = null;
/* Static Methods */
/**
* Add a stylesheet to be loaded into the window's frame.
*
* @method
* @param {string[]} paths List of absolute stylesheet paths
*/
ve.ui.Window.static.addStylesheetFiles = function ( paths ) {
if ( !this.hasOwnProperty( 'stylesheets' ) ) {
this.stylesheets = this.stylesheets.slice( 0 );
}
this.stylesheets.push.apply( this.stylesheets, paths );
};
/**
* Add a stylesheet from a local directory.
*
* @method
* @param {string[]} files Names of stylesheet files
*/
ve.ui.Window.static.addLocalStylesheets = function ( files ) {
var i, len,
base = ve.init.platform.getModulesUrl(),
paths = [];
// Prepend base path to each file name
for ( i = 0, len = files.length; i < len; i++ ) {
// Extract the module name and convert it to a path
paths[i] = base + '/' + files[i];
}
this.addStylesheetFiles( paths );
};
/* Methods */
/**
* Handle frame initialize event.
*
* @method
*/
ve.ui.Window.prototype.onFrameInitialize = function () {
this.initialize();
this.emit( 'initialize' );
};
/**
* Handle the window being initialized.
*
* @method
*/
ve.ui.Window.prototype.initialize = function () {
// Properties
this.$title = this.$$( '<div class="ve-ui-window-title"></div>' );
if ( this.constructor.static.titleMessage ) {
this.$title.text( ve.msg( this.constructor.static.titleMessage ) );
}
this.$icon = this.$$( '<div class="ve-ui-window-icon"></div>' )
.addClass( 've-ui-icon-' + this.constructor.static.icon );
this.$head = this.$$( '<div class="ve-ui-window-head"></div>' );
this.$body = this.$$( '<div class="ve-ui-window-body"></div>' );
this.$foot = this.$$( '<div class="ve-ui-window-foot"></div>' );
this.$overlay = this.$$( '<div class="ve-ui-window-overlay"></div>' );
// Initialization
this.frame.$content.append(
this.$head.append( this.$icon, this.$title ),
this.$body,
this.$foot,
this.$overlay
);
};
/**
* Handle the window being opened.
*
* Any changes to the document in that need to be done prior to opening should be made here.
*
* To be notified after this method is called, listen to the `setup` event.
*
* @method
*/
ve.ui.Window.prototype.onSetup = function () {
// This is a stub, override functionality in child classes
};
/**
* Handle the window being opened.
*
* Any changes to the window that need to be done prior to opening should be made here.
*
* To be notified after this method is called, listen to the `open` event.
*
* @method
*/
ve.ui.Window.prototype.onOpen = function () {
// This is a stub, override functionality in child classes
};
/**
* Handle the window being closed.
*
* Any changes to the document that need to be done prior to closing should be made here.
*
* To be notified after this method is called, listen to the `close` event.
*
* @method
* @param {string} action Action that caused the window to be closed
*/
ve.ui.Window.prototype.onClose = function () {
// This is a stub, override functionality in child classes
};
/**
* Check if window is visible.
*
* @method
* @returns {boolean} Window is visible
*/
ve.ui.Window.prototype.isVisible = function () {
return this.visible;
};
/**
* Get the window frame.
*
* @method
* @returns {ve.ui.Frame} Frame of window
*/
ve.ui.Window.prototype.getFrame = function () {
return this.frame;
};
/**
*
* @method
*/
ve.ui.Window.prototype.setSize = function ( width, height ) {
if ( !this.frame.$content ) {
return;
}
this.frame.$.css( {
'width': width === undefined ? 'auto' : width,
'height': height === undefined ? 'auto' : height
} );
};
/**
*
* @method
*/
ve.ui.Window.prototype.fitHeightToContents = function ( min, max ) {
var height = this.frame.$content.outerHeight();
this.frame.$.css(
'height', Math.max( min || 0, max === undefined ? height : Math.min( max, height ) )
);
};
/**
*
*/
ve.ui.Window.prototype.fitWidthToContents = function ( min, max ) {
var width = this.frame.$content.outerWidth();
this.frame.$.css(
'width', Math.max( min || 0, max === undefined ? width : Math.min( max, width ) )
);
};
/**
*
* @method
*/
ve.ui.Window.prototype.setPosition = function ( left, top ) {
this.$.css( { 'left': left, 'top': top } );
};
/**
* Open window.
*
* @method
* @emits setup
* @emits open
*/
ve.ui.Window.prototype.open = function () {
if ( !this.opening ) {
this.opening = true;
this.onSetup();
this.emit( 'setup' );
this.$.show();
this.visible = true;
this.frame.$.focus();
this.frame.run( ve.bind( function () {
this.onOpen();
this.opening = false;
this.emit( 'open' );
}, this ) );
}
};
/**
* Close window.
*
* This method guards against recursive calling internally. This protects against changes made while
* closing the window which themselves would cause the window to be closed from causing an infinate
* loop.
*
* @method
* @param {string} action Action that caused the window to be closed
* @emits close
*/
ve.ui.Window.prototype.close = function ( action ) {
if ( !this.closing ) {
this.closing = true;
this.$.hide();
this.visible = false;
this.onClose( action );
this.frame.$content.find( ':focus' ).blur();
this.surface.getView().focus();
this.emit( 'close', action );
// Note that focussing the surface view calls an on focus event, which in turn will
// try to close the window again, hence we put this.closing = false right at the bottom
this.closing = false;
}
};
/* Initialization */
ve.ui.Window.static.addLocalStylesheets( [
've/ui/styles/ve.ui.css',
've/ui/styles/ve.ui.Frame.css',
've/ui/styles/ve.ui.Window.css',
've/ui/styles/ve.ui.Element.css',
've/ui/styles/ve.ui.Layout.css',
've/ui/styles/ve.ui.Widget.css',
( document.createElementNS && document.createElementNS( 'http://www.w3.org/2000/svg', 'svg' ).createSVGRect ?
've/ui/styles/ve.ui.Icons-vector.css' :
've/ui/styles/ve.ui.Icons-raster.css' )
] );