mediawiki-extensions-Visual.../modules/ve/init/ve.init.Target.js
Rob Moen 81c9b72db5 Introduce dialogs
Major changes:

demos/ve/index.php
* Renamed ve-demo-content to ve-demo-editor

ve.init.mw.ViewPageTarget, ve.init.sa.Target
* Added handlers for dialog events

ve.ui.*Dialog.js
* Added skeleton classes for dialogs

ve.init.Target.js
* Create abstract class methods for Target.

ve.init.sa.Target.js
* Create Standalone target view methods.

ve.init.mw.Target.js
* Added MW specific target view methods.
* Integration action buttons are now added to the edit
view in the toolbar.

ve.Surface.js
* Simplified constructor, now requiring a target which contains the container

* Other changes include some documentation and code cleanup.

Bug: 39597
Change-Id: Iff39266bdd3052f34bda254ca407030dbbc81f26
2013-03-06 14:19:15 -08:00

134 lines
2.6 KiB
JavaScript

/*!
* VisualEditor Initialization Target class.
*
* @copyright 2011-2013 VisualEditor Team and others; see AUTHORS.txt
* @license The MIT License (MIT); see LICENSE.txt
*/
/**
* Generic Initialization target.
*
* @class
* @abstract
* @extends ve.EventEmitter
*
* @constructor
* @param {jQuery} $container Conainter to render target into
*/
ve.init.Target = function VeInitTarget( $container ) {
// Parent constructor
ve.EventEmitter.call( this );
// Properties
this.$ = $container;
this.dialogs = {};
this.currentDialogName = null;
};
/* Inheritance */
ve.inheritClass( ve.init.Target, ve.EventEmitter );
/* Events */
/**
* @event addDialog
* @param {string} name Name of dialog
*/
/**
* @event openDialog
* @param {string} name Name of dialog
*/
/**
* @event closeDialog
* @param {string} name Name of dialog
*/
/* Methods */
/**
* Add a dialog to the target.
*
* @method
* @param {string} name Name of dialog
* @param {ve.ui.Dialog} dialog Dialog to add
* @throws {Error} If dialog is already registered using `name`
* @emits addDialog
*/
ve.init.Target.prototype.addDialog = function ( name, dialog ) {
// Prevent duplicate names being used
if ( name in this.dialogs ) {
throw new Error( 'Dialog already registered: ' + name );
}
this.dialogs[name] = dialog;
this.emit( 'addDialog', name );
};
/**
* Open a dialog.
*
* @method
* @param {string} name Name of dialog
* @throws {Error} If no dialog exists for `name`
* @emits openDialog
*/
ve.init.Target.prototype.openDialog = function ( name ) {
var dialog = this.dialogs[name];
// Validate dialog
if ( !dialog ) {
throw new Error( 'Dialog not found: ' + name );
}
// Bypass when not changing anything
if ( name === this.currentDialogName ) {
return;
}
// Close current dialog
if ( this.currentDialogName && this.dialogs[this.currentDialogName].isVisible() ) {
this.closeDialog();
}
this.currentDialogName = name;
dialog.show();
this.emit( 'openDialog', name );
};
/**
* Close any open dialog.
*
* @method
* @param {string} name Name of dialog
* @emits closeDialog
*/
ve.init.Target.prototype.closeDialog = function () {
var name = this.currentDialogName;
this.currentDialogName = null;
this.dialogs[name].hide();
this.emit( 'closeDialog', name );
};
/**
* Get a dialog by name.
*
* @method
* @param {string} name Name of dialog
* @returns {ve.ui.Dialog} Dialog associated with `name`
* @throws {Error} If no dialog exists for `name`
*/
ve.init.Target.prototype.getDialog = function ( name ) {
var dialog = this.dialogs[name];
// Validate dialog
if ( !dialog ) {
throw new Error( 'Dialog not found: ' + name );
}
return dialog;
};