mediawiki-extensions-Visual.../modules/ve/ui/ve.ui.Frame.js
Trevor Parscal 2e76271b4e The Great ve.ui.Surface refactor of 2013
Prologue:

Farewell ve.Editor my good chap… Oh, hey there HTML frames - I didn't
see you there! In a world where iframes are outlaws, and symbols like
document and window are global, there were more than a few assumptions
about which document or window was being used. But fear not - for this
commit (probably) tracks them all down, leaving a trail of
iframe-compatible awesomeness in its wake. With the great ve.ui.Surface
now able to be used inside of iframes, let the reference editing
commence. But there, lurking in the darkness is a DM issue so fierce it
may take Roan and/or Ed up to 3 whole hours to sort it out.

Note to Roan and/or Ed:

Editing references seems to work fine, but when saving the page there
are "no changes" which is a reasonable indication to the contrary.

Objectives:

* Make it possible to have multiple surfaces be instantiated, get along
  nicely, and be embedded inside of iframes if needed.
* Make reference content editable within a dialog

Approach:

* Move what's left of ve.Editor to ve.ui.Surface and essentially
  obliterate all use of it
* Make even more stuff inherit from ve.Element (long live this.$$)
* Use the correct document or window anywhere it was being assumed to be
  the top level one
* Resolve stacking order issues by removing the excessive use of z-index
  and introducing global and local overlay elements for each editor
* Add a surface to the reference dialog, load up the reference contents
  and save them back on apply
* Actually destroy what we create in ce and ui surfaces
* Add recursive frame offset calculation method to ve.Element
* Moved ve.ce.Surface's getSelectionRect method to the prototype

Bonus:

* Move ve.ce.DocumentNode.css contents to ve.ce.Node.css (not sure why it
  was separate in the first place, but I'm likely the one to blame)
* Fix blatant lies in documentation
* Whitespace cleanup here and there
* Get rid of ve.ui.Window overlays - not used or needed

Change-Id: Iede83e7d24f7cb249b6ba3dc45d770445b862e08
2013-05-24 14:01:02 +02:00

150 lines
3.8 KiB
JavaScript

/*!
* VisualEditor UserInterface Frame class.
*
* @copyright 2011-2013 VisualEditor Team and others; see AUTHORS.txt
* @license The MIT License (MIT); see LICENSE.txt
*/
/**
* UserInterface iframe abstraction.
*
* @class
* @extends ve.Element
* @mixins ve.EventEmitter
*
* @constructor
* @param {Object} [config] Config options
* @cfg {string[]} [stylesheets] List of stylesheet file names, each relative to ve/ui/styles
*/
ve.ui.Frame = function VeUiFrame( config ) {
// Parent constructor
ve.Element.call( this, config );
// Mixin constructors
ve.EventEmitter.call( this );
// Properties
this.initialized = false;
this.config = config;
// Events
this.$.load( ve.bind( this.onLoad, this ) );
// Initialize
this.$
.addClass( 've-ui-frame' )
.attr( { 'frameborder': 0, 'scrolling': 'no' } );
};
/* Inheritance */
ve.inheritClass( ve.ui.Frame, ve.Element );
ve.mixinClass( ve.ui.Frame, ve.EventEmitter );
/* Static Properties */
ve.ui.Frame.static.tagName = 'iframe';
/* Events */
/**
* @event initialize
*/
/* Methods */
/**
* Handle frame load events.
*
* Once the iframe's stylesheets are loaded, the `initialize` event will be emitted.
*
* Sounds simple right? Read on...
*
* When you create a dynamic iframe using open/write/close, the window.load event for the
* iframe is triggered when you call close, and there's no further load event to indicate that
* everything is actually loaded.
*
* By dynamically adding stylesheet links, we can detect when each link is loaded by testing if we
* have access to each of their `sheet.cssRules` properties. Every 10ms we poll to see if we have
* access to the style's `sheet.cssRules` property yet.
*
* However, because of security issues, we never have such access if the stylesheet came from a
* different site. Thus, we are left with linking to the stylesheets through a style element with
* multiple `@import` statements - which ends up being simpler anyway. Since we created that style,
* we always have access, and its contents are only available when everything is done loading.
*
* @emits initialize
*/
ve.ui.Frame.prototype.onLoad = function () {
var interval, rules,
win = this.$.prop( 'contentWindow' ),
doc = win.document,
style = doc.createElement( 'style' ),
initialize = ve.bind( function () {
this.initialized = true;
this.emit( 'initialize' );
}, this );
// Initialize contents
doc.open();
doc.write(
'<!doctype html>' +
'<html>' +
'<body style="padding:0;margin:0;">' +
'<div class="ve-ui-frame-content"></div>' +
'</body>' +
'</html>'
);
doc.close();
// Import all stylesheets
style.textContent = '@import "' + this.config.stylesheets.join( '";\n@import "' ) + '";';
doc.body.appendChild( style );
// Poll for access to stylesheet content
interval = setInterval( ve.bind( function () {
try {
// MAGIC: only accessible when the stylesheet is loaded
rules = style.sheet.cssRules;
} catch ( e ) {
// Try again in 10ms
return;
}
// If that didn't throw an exception, we're done loading
clearInterval( interval );
// Protect against IE running interval one extra time after clearing
if ( !this.initialized ) {
initialize();
}
}, this ), 10 );
// Properties
this.$$ = ve.Element.static.get$$( doc, this );
this.$content = this.$$( '.ve-ui-frame-content' );
};
/**
* @param {Function} callback
*/
ve.ui.Frame.prototype.run = function ( callback ) {
if ( this.initialized ) {
callback();
} else {
this.once( 'initialize', callback );
}
};
/**
* Sets the size of the frame.
*
* @method
* @param {number} width Frame width in pixels
* @param {number} height Frame height in pixels
* @chainable
*/
ve.ui.Frame.prototype.setSize = function ( width, height ) {
this.$.css( { 'width': width, 'height': height } );
return this;
};