All files / src/ui/contexts ve.ui.LinearContext.js

63.55% Statements 75/118
51.92% Branches 27/52
50% Functions 9/18
63.55% Lines 75/118

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339                                  1x   5x     5x 5x 5x 5x 5x 5x 5x 5x     5x       5x         1x                                 1x 148x           148x   65x       148x           1x   55x   13x             1x 65x     65x   65x 21x 21x   4x 4x 4x     17x 17x                     44x   18x 18x       65x                       1x                                                                                                                       1x 244x               1x 65x                 1x                         1x                         1x 89x 89x 89x   89x 65x 65x 53x 12x 1x   65x 23x   65x     89x                 1x 23x 23x 23x     23x 24x     24x 24x   24x             23x 23x 21x     21x                   23x               1x 1x                 1x         1x                            
/*!
 * VisualEditor UserInterface Linear Context class.
 *
 * @copyright See AUTHORS.txt
 */
 
/**
 * UserInterface context.
 *
 * @class
 * @abstract
 * @extends ve.ui.Context
 *
 * @constructor
 * @param {ve.ui.Surface} surface
 * @param {Object} [config] Configuration options
 */
ve.ui.LinearContext = function VeUiLinearContext() {
	// Parent constructor
	ve.ui.LinearContext.super.apply( this, arguments );
 
	// Properties
	this.inspector = null;
	this.inspectors = this.createInspectorWindowManager();
	this.isOpening = false;
	this.lastSelectedNode = null;
	this.afterContextChangeTimeout = null;
	this.afterContextChangeHandler = this.afterContextChange.bind( this );
	this.updateDimensionsDebounced = ve.debounce( this.updateDimensions.bind( this ) );
	this.persistentSources = [];
 
	// Events
	this.surface.getModel().connect( this, {
		contextChange: 'onContextChange',
		documentUpdate: 'onDocumentUpdate'
	} );
	this.inspectors.connect( this, { opening: 'onInspectorOpening' } );
};
 
/* Inheritance */
 
OO.inheritClass( ve.ui.LinearContext, ve.ui.Context );
 
/* Static Properties */
 
/**
 * Handle context change event.
 *
 * While an inspector is opening or closing, all changes are ignored so as to prevent inspectors
 * that change the selection from within their setup or teardown processes changing context state.
 *
 * The response to selection changes is deferred to prevent teardown processes handlers that change
 * the selection from causing this function to recurse. These responses are also debounced for
 * efficiency, so that if there are three selection changes in the same tick, #afterContextChange only
 * runs once.
 *
 * @see #afterContextChange
 */
ve.ui.LinearContext.prototype.onContextChange = function () {
	Iif ( this.inspector && ( this.inspector.isOpening() || this.inspector.isClosing() ) ) {
		// Cancel debounced change handler
		clearTimeout( this.afterContextChangeTimeout );
		this.afterContextChangeTimeout = null;
		this.lastSelectedNode = this.surface.getModel().getSelectedNode();
	} else {
		if ( this.afterContextChangeTimeout === null ) {
			// Ensure change is handled on next cycle
			this.afterContextChangeTimeout = setTimeout( this.afterContextChangeHandler );
		}
	}
	// Purge related items cache
	this.relatedSources = null;
};
 
/**
 * Handle document update event.
 */
ve.ui.LinearContext.prototype.onDocumentUpdate = function () {
	// Only mind this event if the menu is visible
	if ( this.isVisible() && !this.isEmpty() ) {
		// Reuse the debounced context change handler
		this.onContextChange();
	}
};
 
/**
 * Handle debounced context change events.
 */
ve.ui.LinearContext.prototype.afterContextChange = function () {
	var selectedNode = this.surface.getModel().getSelectedNode();
 
	// Reset debouncing state
	this.afterContextChangeTimeout = null;
 
	if ( this.isVisible() ) {
		if ( !this.isEmpty() ) {
			if ( this.isInspectable() ) {
				// Change state: menu -> menu
				this.teardownMenuItems();
				this.setupMenuItems();
				this.updateDimensionsDebounced();
			} else {
				// Change state: menu -> closed
				this.toggleMenu( false );
				this.toggle( false );
			}
		} else Eif (
			this.inspector &&
			( !selectedNode || ( selectedNode !== this.lastSelectedNode ) )
		) {
			// Change state: inspector -> (closed|menu)
			// Unless there is a selectedNode that hasn't changed (e.g. your inspector is editing a node)
			this.inspector.close();
		}
	} else {
		if ( this.isInspectable() ) {
			// Change state: closed -> menu
			this.toggleMenu( true );
			this.toggle( true );
		}
	}
 
	this.lastSelectedNode = selectedNode;
};
 
/**
 * Handle an inspector opening event.
 *
 * @param {OO.ui.Window} win Window that's being opened
 * @param {jQuery.Promise} opening Promise resolved when window is opened; when the promise is
 *   resolved the first argument will be a promise which will be resolved when the window begins
 *   closing, the second argument will be the opening data
 * @param {Object} data Window opening data
 */
ve.ui.LinearContext.prototype.onInspectorOpening = function ( win, opening ) {
	var context = this,
		observer = this.surface.getView().surfaceObserver;
 
	this.isOpening = true;
	this.inspector = win;
 
	// Shut down the SurfaceObserver as soon as possible, so it doesn't get confused
	// by the selection moving around in IE. Will be reenabled when inspector closes.
	// FIXME this should be done in a nicer way, managed by the Surface classes
	observer.pollOnce();
	observer.stopTimerLoop();
 
	opening
		.progress( function ( data ) {
			context.isOpening = false;
			if ( data.state === 'setup' ) {
				if ( !context.isVisible() ) {
					// Change state: closed -> inspector
					context.toggle( true );
				}
				if ( !context.isEmpty() ) {
					// Change state: menu -> inspector
					context.toggleMenu( false );
				}
			}
			context.updateDimensionsDebounced();
		} )
		.then( function ( opened ) {
			opened.then( function ( closed ) {
				closed.always( function () {
					// Don't try to close the inspector if a second
					// opening has already been triggered
					if ( context.isOpening ) {
						return;
					}
 
					context.inspector = null;
 
					// Reenable observer
					observer.startTimerLoop();
 
					if ( context.isInspectable() ) {
						// Change state: inspector -> menu
						context.toggleMenu( true );
						context.updateDimensionsDebounced();
					} else {
						// Change state: inspector -> closed
						context.toggle( false );
					}
				} );
			} );
		} );
};
 
/**
 * Check if context is visible.
 *
 * @return {boolean} Context is visible
 */
ve.ui.LinearContext.prototype.isVisible = function () {
	return this.visible;
};
 
/**
 * Check if current content is inspectable.
 *
 * @return {boolean} Content is inspectable
 */
ve.ui.LinearContext.prototype.isInspectable = function () {
	return !!this.getRelatedSources().length;
};
 
/**
 * Add a persistent source that will stay visible until manually removed.
 *
 * @param {Object} source Object containing `name`, `model` and `config` properties.
 *   See #getRelatedSources.
 */
ve.ui.LinearContext.prototype.addPersistentSource = function ( source ) {
	this.persistentSources.push(
		ve.extendObject( { type: 'persistent' }, source )
	);
 
	this.onContextChange();
};
 
/**
 * Remove a persistent source by name
 *
 * @param {string} name Source name
 */
ve.ui.LinearContext.prototype.removePersistentSource = function ( name ) {
	this.persistentSources = this.persistentSources.filter( function ( source ) {
		return source.name !== name;
	} );
 
	this.onContextChange();
};
 
/**
 * @inheritdoc
 *
 * Also adds the `embeddable` property to each object.
 */
ve.ui.LinearContext.prototype.getRelatedSources = function () {
	var surfaceModel = this.surface.getModel(),
		selection = surfaceModel.getSelection(),
		selectedModels = [];
 
	if ( !this.relatedSources ) {
		this.relatedSources = [];
		if ( selection instanceof ve.dm.LinearSelection ) {
			selectedModels = this.surface.getView().getSelectedModels();
		} else if ( selection instanceof ve.dm.TableSelection ) {
			selectedModels = [ surfaceModel.getSelectedNode() ];
		}
		if ( selectedModels.length ) {
			this.relatedSources = this.getRelatedSourcesFromModels( selectedModels );
		}
		this.relatedSources = this.relatedSources.concat( this.persistentSources );
	}
 
	return this.relatedSources;
};
 
/**
 * Get related for selected models
 *
 * @param {ve.dm.Model[]} selectedModels Models
 * @return {Object[]} See #getRelatedSources
 */
ve.ui.LinearContext.prototype.getRelatedSourcesFromModels = function ( selectedModels ) {
	var models = [],
		relatedSources = [],
		items = ve.ui.contextItemFactory.getRelatedItems( selectedModels );
 
	var i, len;
	for ( i = 0, len = items.length; i < len; i++ ) {
		Iif ( !items[ i ].model.isInspectable() ) {
			continue;
		}
		Eif ( ve.ui.contextItemFactory.isExclusive( items[ i ].name ) ) {
			models.push( items[ i ].model );
		}
		relatedSources.push( {
			type: 'item',
			embeddable: ve.ui.contextItemFactory.isEmbeddable( items[ i ].name ),
			name: items[ i ].name,
			model: items[ i ].model
		} );
	}
	var tools = ve.ui.toolFactory.getRelatedItems( selectedModels );
	for ( i = 0, len = tools.length; i < len; i++ ) {
		Iif ( !tools[ i ].model.isInspectable() ) {
			continue;
		}
		Iif ( models.indexOf( tools[ i ].model ) === -1 ) {
			var toolClass = ve.ui.toolFactory.lookup( tools[ i ].name );
			relatedSources.push( {
				type: 'tool',
				embeddable: !toolClass || toolClass.static.makesEmbeddableContextItem,
				name: tools[ i ].name,
				model: tools[ i ].model
			} );
		}
	}
	return relatedSources;
};
 
/**
 * Get inspector window set.
 *
 * @return {ve.ui.WindowManager}
 */
ve.ui.LinearContext.prototype.getInspectors = function () {
	return this.inspectors;
};
 
/**
 * Create a inspector window manager.
 *
 * @abstract
 * @return {ve.ui.WindowManager} Inspector window manager
 */
ve.ui.LinearContext.prototype.createInspectorWindowManager = null;
 
/**
 * @inheritdoc
 */
ve.ui.LinearContext.prototype.destroy = function () {
	// Disconnect events
	this.surface.getModel().disconnect( this );
	this.inspectors.disconnect( this );
 
	// Destroy inspectors WindowManager
	this.inspectors.destroy();
 
	// Stop timers
	clearTimeout( this.afterContextChangeTimeout );
 
	// Parent method
	return ve.ui.LinearContext.super.prototype.destroy.call( this );
};