2013-04-02 19:33:22 +00:00
|
|
|
/*!
|
|
|
|
* VisualEditor ContentEditable View class.
|
|
|
|
*
|
|
|
|
* @copyright 2011-2013 VisualEditor Team and others; see AUTHORS.txt
|
|
|
|
* @license The MIT License (MIT); see LICENSE.txt
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generic base class for CE views.
|
|
|
|
*
|
|
|
|
* @abstract
|
2013-10-09 20:09:59 +00:00
|
|
|
* @extends OO.ui.Element
|
2013-10-15 11:58:04 +00:00
|
|
|
* @mixins OO.EventEmitter
|
2013-04-02 19:33:22 +00:00
|
|
|
*
|
|
|
|
* @constructor
|
|
|
|
* @param {ve.dm.Model} model Model to observe
|
2013-09-25 10:21:09 +00:00
|
|
|
* @param {Object} [config] Configuration options
|
2013-04-02 19:33:22 +00:00
|
|
|
*/
|
ve.Element refactor
Objectives:
* Move ve.ui.Element to ve.Element
* Make CE nodes inherit from ve.Element
Changes:
ve.ui.Element.js, ve.Element.js
* Move and rename
* Move ve.ui.get$$ to ve.Element.static.get$$
* Add static getDocument and getWindow methods
* Add instance getElementDocument and getElementWindow methods
* Add getTagName method, which by default reads the static tagName property, but when overridden can return a tag name based on other factors
*.php
* Updated file link
ve.ce.*Annotation.js, ve.ce.*Node.js, ve.ce.View.js, ve.ce.Document
* Added config options pass through
* Replaced passing elements through constructor with defining static tag names
* Added getTagName overrides where needed that derive tag name from model
* Refactore dom wrapper methods, now consistently using getTagName
ve.ce.Surface.js
* Removed static initialization (not needed)
ve.dm.Model.js, ve.ui.Window.js
* Added missing docs
ve.ui.GroupElement.js, ve.ui.Layout.js, ve.ui.Widget.js,
* Updated class name for elements
ve.ui.Frame.js, ve.ui.LookupInputWidget.js
* Updated location of get$$
ve.ui.js
* Move get$$ to ve.Element
ve.js
* Add auto-init of static properties to mixinClass
Change-Id: I39ae14966456903728e4d9e53f806ddce9ca2b70
2013-05-13 20:52:59 +00:00
|
|
|
ve.ce.View = function VeCeView( model, config ) {
|
|
|
|
// Setting this property before calling the parent constructor allows overriden #getTagName
|
|
|
|
// methods in view classes to have access to the model when they are called for the first time
|
2013-10-09 20:09:59 +00:00
|
|
|
// inside of OO.ui.Element
|
ve.Element refactor
Objectives:
* Move ve.ui.Element to ve.Element
* Make CE nodes inherit from ve.Element
Changes:
ve.ui.Element.js, ve.Element.js
* Move and rename
* Move ve.ui.get$$ to ve.Element.static.get$$
* Add static getDocument and getWindow methods
* Add instance getElementDocument and getElementWindow methods
* Add getTagName method, which by default reads the static tagName property, but when overridden can return a tag name based on other factors
*.php
* Updated file link
ve.ce.*Annotation.js, ve.ce.*Node.js, ve.ce.View.js, ve.ce.Document
* Added config options pass through
* Replaced passing elements through constructor with defining static tag names
* Added getTagName overrides where needed that derive tag name from model
* Refactore dom wrapper methods, now consistently using getTagName
ve.ce.Surface.js
* Removed static initialization (not needed)
ve.dm.Model.js, ve.ui.Window.js
* Added missing docs
ve.ui.GroupElement.js, ve.ui.Layout.js, ve.ui.Widget.js,
* Updated class name for elements
ve.ui.Frame.js, ve.ui.LookupInputWidget.js
* Updated location of get$$
ve.ui.js
* Move get$$ to ve.Element
ve.js
* Add auto-init of static properties to mixinClass
Change-Id: I39ae14966456903728e4d9e53f806ddce9ca2b70
2013-05-13 20:52:59 +00:00
|
|
|
this.model = model;
|
|
|
|
|
2013-04-02 19:33:22 +00:00
|
|
|
// Parent constructor
|
2013-10-09 20:09:59 +00:00
|
|
|
OO.ui.Element.call( this, config );
|
ve.Element refactor
Objectives:
* Move ve.ui.Element to ve.Element
* Make CE nodes inherit from ve.Element
Changes:
ve.ui.Element.js, ve.Element.js
* Move and rename
* Move ve.ui.get$$ to ve.Element.static.get$$
* Add static getDocument and getWindow methods
* Add instance getElementDocument and getElementWindow methods
* Add getTagName method, which by default reads the static tagName property, but when overridden can return a tag name based on other factors
*.php
* Updated file link
ve.ce.*Annotation.js, ve.ce.*Node.js, ve.ce.View.js, ve.ce.Document
* Added config options pass through
* Replaced passing elements through constructor with defining static tag names
* Added getTagName overrides where needed that derive tag name from model
* Refactore dom wrapper methods, now consistently using getTagName
ve.ce.Surface.js
* Removed static initialization (not needed)
ve.dm.Model.js, ve.ui.Window.js
* Added missing docs
ve.ui.GroupElement.js, ve.ui.Layout.js, ve.ui.Widget.js,
* Updated class name for elements
ve.ui.Frame.js, ve.ui.LookupInputWidget.js
* Updated location of get$$
ve.ui.js
* Move get$$ to ve.Element
ve.js
* Add auto-init of static properties to mixinClass
Change-Id: I39ae14966456903728e4d9e53f806ddce9ca2b70
2013-05-13 20:52:59 +00:00
|
|
|
|
|
|
|
// Mixin constructors
|
2013-10-15 11:58:04 +00:00
|
|
|
OO.EventEmitter.call( this );
|
2013-04-02 19:33:22 +00:00
|
|
|
|
|
|
|
// Properties
|
|
|
|
this.live = false;
|
|
|
|
|
2013-05-13 22:21:42 +00:00
|
|
|
// Events
|
|
|
|
this.connect( this, {
|
|
|
|
'setup': 'onSetup',
|
|
|
|
'teardown': 'onTeardown'
|
|
|
|
} );
|
|
|
|
|
2013-04-02 19:33:22 +00:00
|
|
|
// Initialization
|
2013-05-18 03:49:25 +00:00
|
|
|
this.renderAttributes();
|
2013-04-02 19:33:22 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/* Inheritance */
|
|
|
|
|
2013-10-09 20:09:59 +00:00
|
|
|
OO.inheritClass( ve.ce.View, OO.ui.Element );
|
ve.Element refactor
Objectives:
* Move ve.ui.Element to ve.Element
* Make CE nodes inherit from ve.Element
Changes:
ve.ui.Element.js, ve.Element.js
* Move and rename
* Move ve.ui.get$$ to ve.Element.static.get$$
* Add static getDocument and getWindow methods
* Add instance getElementDocument and getElementWindow methods
* Add getTagName method, which by default reads the static tagName property, but when overridden can return a tag name based on other factors
*.php
* Updated file link
ve.ce.*Annotation.js, ve.ce.*Node.js, ve.ce.View.js, ve.ce.Document
* Added config options pass through
* Replaced passing elements through constructor with defining static tag names
* Added getTagName overrides where needed that derive tag name from model
* Refactore dom wrapper methods, now consistently using getTagName
ve.ce.Surface.js
* Removed static initialization (not needed)
ve.dm.Model.js, ve.ui.Window.js
* Added missing docs
ve.ui.GroupElement.js, ve.ui.Layout.js, ve.ui.Widget.js,
* Updated class name for elements
ve.ui.Frame.js, ve.ui.LookupInputWidget.js
* Updated location of get$$
ve.ui.js
* Move get$$ to ve.Element
ve.js
* Add auto-init of static properties to mixinClass
Change-Id: I39ae14966456903728e4d9e53f806ddce9ca2b70
2013-05-13 20:52:59 +00:00
|
|
|
|
2013-10-15 11:58:04 +00:00
|
|
|
OO.mixinClass( ve.ce.View, OO.EventEmitter );
|
2013-04-02 19:33:22 +00:00
|
|
|
|
|
|
|
/* Events */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @event live
|
2013-05-22 10:52:53 +00:00
|
|
|
* @param {boolean} live The view is being set live
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @event setup
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @event teardown
|
2013-04-02 19:33:22 +00:00
|
|
|
*/
|
|
|
|
|
2013-09-23 12:58:17 +00:00
|
|
|
/* Static members */
|
2013-04-02 19:33:22 +00:00
|
|
|
|
|
|
|
/**
|
2013-11-19 08:32:37 +00:00
|
|
|
* Allowed attributes for DOM elements, in the same format as ve.dm.Model#storeHtmlAttributes
|
2013-04-02 19:33:22 +00:00
|
|
|
*
|
|
|
|
* This list includes attributes that are generally safe to include in HTML loaded from a
|
|
|
|
* foreign source and displaying it inside the browser. It doesn't include any event attributes,
|
|
|
|
* for instance, which would allow arbitrary JavaScript execution. This alone is not enough to
|
|
|
|
* make HTML safe to display, but it helps.
|
|
|
|
*
|
|
|
|
* TODO: Rather than use a single global list, set these on a per-view basis to something that makes
|
|
|
|
* sense for that view in particular.
|
|
|
|
*
|
|
|
|
* @static
|
2013-11-19 08:32:37 +00:00
|
|
|
* @property {boolean|string|RegExp|Array|Object}
|
2013-04-02 19:33:22 +00:00
|
|
|
* @inheritable
|
|
|
|
*/
|
2013-08-27 01:37:50 +00:00
|
|
|
ve.ce.View.static.renderHtmlAttributes = [
|
2013-04-02 19:33:22 +00:00
|
|
|
'abbr', 'about', 'align', 'alt', 'axis', 'bgcolor', 'border', 'cellpadding', 'cellspacing',
|
|
|
|
'char', 'charoff', 'cite', 'class', 'clear', 'color', 'colspan', 'datatype', 'datetime',
|
|
|
|
'dir', 'face', 'frame', 'headers', 'height', 'href', 'id', 'itemid', 'itemprop', 'itemref',
|
|
|
|
'itemscope', 'itemtype', 'lang', 'noshade', 'nowrap', 'property', 'rbspan', 'rel',
|
|
|
|
'resource', 'rev', 'rowspan', 'rules', 'scope', 'size', 'span', 'src', 'start', 'style',
|
|
|
|
'summary', 'title', 'type', 'typeof', 'valign', 'value', 'width'
|
|
|
|
];
|
|
|
|
|
|
|
|
/* Methods */
|
|
|
|
|
2013-10-21 15:12:54 +00:00
|
|
|
/**
|
|
|
|
* Get an HTML document from the model, to use for URL resolution.
|
|
|
|
*
|
|
|
|
* The default implementation returns null; subclasses should override this if they can provide
|
|
|
|
* a resolution document.
|
|
|
|
*
|
|
|
|
* @see #getResolvedAttribute
|
|
|
|
* @returns {HTMLDocument|null} HTML document to use for resolution, or null if not available
|
|
|
|
*/
|
|
|
|
ve.ce.View.prototype.getModelHtmlDocument = function () {
|
|
|
|
return null;
|
|
|
|
};
|
|
|
|
|
2013-05-13 22:21:42 +00:00
|
|
|
/**
|
|
|
|
* Handle setup event.
|
|
|
|
*
|
|
|
|
* @method
|
|
|
|
*/
|
|
|
|
ve.ce.View.prototype.onSetup = function () {
|
2013-11-01 19:45:59 +00:00
|
|
|
this.$element.data( 'view', this );
|
2013-05-13 22:21:42 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle teardown event.
|
|
|
|
*
|
|
|
|
* @method
|
|
|
|
*/
|
|
|
|
ve.ce.View.prototype.onTeardown = function () {
|
2013-11-01 19:45:59 +00:00
|
|
|
this.$element.removeData( 'view' );
|
2013-05-13 22:21:42 +00:00
|
|
|
};
|
|
|
|
|
2013-04-02 19:33:22 +00:00
|
|
|
/**
|
|
|
|
* Get the model the view observes.
|
|
|
|
*
|
|
|
|
* @method
|
2013-10-21 13:43:09 +00:00
|
|
|
* @returns {ve.dm.Model} Model the view observes
|
2013-04-02 19:33:22 +00:00
|
|
|
*/
|
|
|
|
ve.ce.View.prototype.getModel = function () {
|
|
|
|
return this.model;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if the view is attached to the live DOM.
|
|
|
|
*
|
|
|
|
* @method
|
|
|
|
* @returns {boolean} View is attached to the live DOM
|
|
|
|
*/
|
|
|
|
ve.ce.View.prototype.isLive = function () {
|
|
|
|
return this.live;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set live state.
|
|
|
|
*
|
|
|
|
* @method
|
|
|
|
* @param {boolean} live The view has been attached to the live DOM (use false on detach)
|
2013-10-22 17:54:59 +00:00
|
|
|
* @fires live
|
|
|
|
* @fires setup
|
|
|
|
* @fires teardown
|
2013-04-02 19:33:22 +00:00
|
|
|
*/
|
|
|
|
ve.ce.View.prototype.setLive = function ( live ) {
|
|
|
|
this.live = live;
|
|
|
|
this.emit( 'live' );
|
2013-05-13 22:21:42 +00:00
|
|
|
if ( this.live ) {
|
|
|
|
this.emit( 'setup' );
|
|
|
|
} else {
|
|
|
|
this.emit( 'teardown' );
|
|
|
|
}
|
2013-04-02 19:33:22 +00:00
|
|
|
};
|
2013-05-08 07:38:50 +00:00
|
|
|
|
ve.Element refactor
Objectives:
* Move ve.ui.Element to ve.Element
* Make CE nodes inherit from ve.Element
Changes:
ve.ui.Element.js, ve.Element.js
* Move and rename
* Move ve.ui.get$$ to ve.Element.static.get$$
* Add static getDocument and getWindow methods
* Add instance getElementDocument and getElementWindow methods
* Add getTagName method, which by default reads the static tagName property, but when overridden can return a tag name based on other factors
*.php
* Updated file link
ve.ce.*Annotation.js, ve.ce.*Node.js, ve.ce.View.js, ve.ce.Document
* Added config options pass through
* Replaced passing elements through constructor with defining static tag names
* Added getTagName overrides where needed that derive tag name from model
* Refactore dom wrapper methods, now consistently using getTagName
ve.ce.Surface.js
* Removed static initialization (not needed)
ve.dm.Model.js, ve.ui.Window.js
* Added missing docs
ve.ui.GroupElement.js, ve.ui.Layout.js, ve.ui.Widget.js,
* Updated class name for elements
ve.ui.Frame.js, ve.ui.LookupInputWidget.js
* Updated location of get$$
ve.ui.js
* Move get$$ to ve.Element
ve.js
* Add auto-init of static properties to mixinClass
Change-Id: I39ae14966456903728e4d9e53f806ddce9ca2b70
2013-05-13 20:52:59 +00:00
|
|
|
/**
|
2013-11-01 19:45:59 +00:00
|
|
|
* Render an HTML attribute list onto this.$element
|
ve.Element refactor
Objectives:
* Move ve.ui.Element to ve.Element
* Make CE nodes inherit from ve.Element
Changes:
ve.ui.Element.js, ve.Element.js
* Move and rename
* Move ve.ui.get$$ to ve.Element.static.get$$
* Add static getDocument and getWindow methods
* Add instance getElementDocument and getElementWindow methods
* Add getTagName method, which by default reads the static tagName property, but when overridden can return a tag name based on other factors
*.php
* Updated file link
ve.ce.*Annotation.js, ve.ce.*Node.js, ve.ce.View.js, ve.ce.Document
* Added config options pass through
* Replaced passing elements through constructor with defining static tag names
* Added getTagName overrides where needed that derive tag name from model
* Refactore dom wrapper methods, now consistently using getTagName
ve.ce.Surface.js
* Removed static initialization (not needed)
ve.dm.Model.js, ve.ui.Window.js
* Added missing docs
ve.ui.GroupElement.js, ve.ui.Layout.js, ve.ui.Widget.js,
* Updated class name for elements
ve.ui.Frame.js, ve.ui.LookupInputWidget.js
* Updated location of get$$
ve.ui.js
* Move get$$ to ve.Element
ve.js
* Add auto-init of static properties to mixinClass
Change-Id: I39ae14966456903728e4d9e53f806ddce9ca2b70
2013-05-13 20:52:59 +00:00
|
|
|
*
|
2013-05-18 03:49:25 +00:00
|
|
|
* If no attributeList is given, the attribute list stored in the linear model will be used.
|
ve.Element refactor
Objectives:
* Move ve.ui.Element to ve.Element
* Make CE nodes inherit from ve.Element
Changes:
ve.ui.Element.js, ve.Element.js
* Move and rename
* Move ve.ui.get$$ to ve.Element.static.get$$
* Add static getDocument and getWindow methods
* Add instance getElementDocument and getElementWindow methods
* Add getTagName method, which by default reads the static tagName property, but when overridden can return a tag name based on other factors
*.php
* Updated file link
ve.ce.*Annotation.js, ve.ce.*Node.js, ve.ce.View.js, ve.ce.Document
* Added config options pass through
* Replaced passing elements through constructor with defining static tag names
* Added getTagName overrides where needed that derive tag name from model
* Refactore dom wrapper methods, now consistently using getTagName
ve.ce.Surface.js
* Removed static initialization (not needed)
ve.dm.Model.js, ve.ui.Window.js
* Added missing docs
ve.ui.GroupElement.js, ve.ui.Layout.js, ve.ui.Widget.js,
* Updated class name for elements
ve.ui.Frame.js, ve.ui.LookupInputWidget.js
* Updated location of get$$
ve.ui.js
* Move get$$ to ve.Element
ve.js
* Add auto-init of static properties to mixinClass
Change-Id: I39ae14966456903728e4d9e53f806ddce9ca2b70
2013-05-13 20:52:59 +00:00
|
|
|
*
|
2013-05-18 03:49:25 +00:00
|
|
|
* @param {Object[]} [attributeList] HTML attribute list, see ve.dm.Converter#buildHtmlAttributeList
|
ve.Element refactor
Objectives:
* Move ve.ui.Element to ve.Element
* Make CE nodes inherit from ve.Element
Changes:
ve.ui.Element.js, ve.Element.js
* Move and rename
* Move ve.ui.get$$ to ve.Element.static.get$$
* Add static getDocument and getWindow methods
* Add instance getElementDocument and getElementWindow methods
* Add getTagName method, which by default reads the static tagName property, but when overridden can return a tag name based on other factors
*.php
* Updated file link
ve.ce.*Annotation.js, ve.ce.*Node.js, ve.ce.View.js, ve.ce.Document
* Added config options pass through
* Replaced passing elements through constructor with defining static tag names
* Added getTagName overrides where needed that derive tag name from model
* Refactore dom wrapper methods, now consistently using getTagName
ve.ce.Surface.js
* Removed static initialization (not needed)
ve.dm.Model.js, ve.ui.Window.js
* Added missing docs
ve.ui.GroupElement.js, ve.ui.Layout.js, ve.ui.Widget.js,
* Updated class name for elements
ve.ui.Frame.js, ve.ui.LookupInputWidget.js
* Updated location of get$$
ve.ui.js
* Move get$$ to ve.Element
ve.js
* Add auto-init of static properties to mixinClass
Change-Id: I39ae14966456903728e4d9e53f806ddce9ca2b70
2013-05-13 20:52:59 +00:00
|
|
|
*/
|
2013-05-18 03:49:25 +00:00
|
|
|
ve.ce.View.prototype.renderAttributes = function ( attributeList ) {
|
|
|
|
ve.dm.Converter.renderHtmlAttributeList(
|
|
|
|
attributeList || this.model.getHtmlAttributes(),
|
2013-11-01 19:45:59 +00:00
|
|
|
this.$element,
|
2013-09-26 02:07:22 +00:00
|
|
|
this.constructor.static.renderHtmlAttributes,
|
|
|
|
true // computed attributes
|
2013-05-18 03:49:25 +00:00
|
|
|
);
|
ve.Element refactor
Objectives:
* Move ve.ui.Element to ve.Element
* Make CE nodes inherit from ve.Element
Changes:
ve.ui.Element.js, ve.Element.js
* Move and rename
* Move ve.ui.get$$ to ve.Element.static.get$$
* Add static getDocument and getWindow methods
* Add instance getElementDocument and getElementWindow methods
* Add getTagName method, which by default reads the static tagName property, but when overridden can return a tag name based on other factors
*.php
* Updated file link
ve.ce.*Annotation.js, ve.ce.*Node.js, ve.ce.View.js, ve.ce.Document
* Added config options pass through
* Replaced passing elements through constructor with defining static tag names
* Added getTagName overrides where needed that derive tag name from model
* Refactore dom wrapper methods, now consistently using getTagName
ve.ce.Surface.js
* Removed static initialization (not needed)
ve.dm.Model.js, ve.ui.Window.js
* Added missing docs
ve.ui.GroupElement.js, ve.ui.Layout.js, ve.ui.Widget.js,
* Updated class name for elements
ve.ui.Frame.js, ve.ui.LookupInputWidget.js
* Updated location of get$$
ve.ui.js
* Move get$$ to ve.Element
ve.js
* Add auto-init of static properties to mixinClass
Change-Id: I39ae14966456903728e4d9e53f806ddce9ca2b70
2013-05-13 20:52:59 +00:00
|
|
|
};
|
2013-10-21 15:12:54 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a resolved URL from a model attribute.
|
|
|
|
*
|
|
|
|
* @abstract
|
|
|
|
* @method
|
|
|
|
* @param {string} key Attribute name whose value is a URL
|
|
|
|
* @returns {string} URL resolved according to the document's base
|
|
|
|
*/
|
|
|
|
ve.ce.View.prototype.getResolvedAttribute = function ( key ) {
|
|
|
|
var plainValue = this.model.getAttribute( key ),
|
|
|
|
doc = this.getModelHtmlDocument();
|
|
|
|
return doc && typeof plainValue === 'string' ? ve.resolveUrl( plainValue, doc ) : plainValue;
|
|
|
|
};
|