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

67.79% Statements 80/118
50% Branches 26/52
60% Functions 12/20
68.37% Lines 80/117

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                                  1x   6x     6x 6x 6x 6x 6x 6x 6x 6x     6x       6x         1x                                 1x 149x           149x   66x       149x           1x   55x   13x             1x 64x     64x   64x 21x 21x     4x 4x 4x 4x     17x 17x                     43x   17x 17x       64x                       1x                                                                                                                     1x 239x               1x 64x                 1x                         1x                 1x 85x 85x 85x   85x 64x 64x 53x 11x     64x 21x   64x     85x                 1x 21x 21x 21x   21x 21x     21x 21x   21x               21x 21x 21x     21x                   21x               1x 3x                 1x         1x   2x 2x     2x     2x     2x    
/*!
 * 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 () {
	const selectedNode = this.surface.getModel().getSelectedNode();
 
	// Reset debouncing state
	this.afterContextChangeTimeout = null;
 
	if ( this.isVisible() ) {
		if ( !this.isEmpty() ) {
			if ( this.isInspectable() ) {
				// Change state: menu -> menu
				// Make a copy of items so setupMenuItems can compare it
				const previousItems = this.items.slice();
				this.teardownMenuItems();
				this.setupMenuItems( previousItems );
				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 ) {
	const 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( ( data ) => {
			this.isOpening = false;
			if ( data.state === 'setup' ) {
				if ( !this.isVisible() ) {
					// Change state: closed -> inspector
					this.toggle( true );
				}
				if ( !this.isEmpty() ) {
					// Change state: menu -> inspector
					this.toggleMenu( false );
				}
			}
			this.updateDimensionsDebounced();
		} )
		.then( ( opened ) => {
			opened.then( ( closed ) => {
				closed.always( () => {
					// Don't try to close the inspector if a second
					// opening has already been triggered
					if ( this.isOpening ) {
						return;
					}
 
					this.inspector = null;
 
					// Reenable observer
					observer.startTimerLoop();
 
					if ( this.isInspectable() ) {
						// Change state: inspector -> menu
						this.toggleMenu( true );
						this.updateDimensionsDebounced();
					} else {
						// Change state: inspector -> closed
						this.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( ( source ) => source.name !== name );
 
	this.onContextChange();
};
 
/**
 * @inheritdoc Also adds the `embeddable` property to each object.
 */
ve.ui.LinearContext.prototype.getRelatedSources = function () {
	const surfaceModel = this.surface.getModel(),
		selection = surfaceModel.getSelection();
	let selectedModels = [];
 
	if ( !this.relatedSources ) {
		this.relatedSources = [];
		if ( selection instanceof ve.dm.LinearSelection ) {
			selectedModels = this.surface.getView().getSelectedModels();
		} else Iif ( selection instanceof ve.dm.TableSelection ) {
			selectedModels = [ surfaceModel.getSelectedNode() ];
		}
		if ( selectedModels.length ) {
			this.relatedSources = this.getRelatedSourcesFromModels( selectedModels );
		}
		this.relatedSources.push( ...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 ) {
	const models = [],
		relatedSources = [],
		items = ve.ui.contextItemFactory.getRelatedItems( selectedModels );
 
	items.forEach( ( item ) => {
		Iif ( !item.model.isInspectable() ) {
			return;
		}
		Eif ( ve.ui.contextItemFactory.isExclusive( item.name ) ) {
			models.push( item.model );
		}
		relatedSources.push( {
			type: 'item',
			embeddable: ve.ui.contextItemFactory.isEmbeddable( item.name ),
			name: item.name,
			model: item.model
		} );
	} );
 
	const tools = ve.ui.toolFactory.getRelatedItems( selectedModels );
	tools.forEach( ( tool ) => {
		Iif ( !tool.model.isInspectable() ) {
			return;
		}
		Iif ( models.indexOf( tool.model ) === -1 ) {
			const toolClass = ve.ui.toolFactory.lookup( tool.name );
			relatedSources.push( {
				type: 'tool',
				embeddable: !toolClass || toolClass.static.makesEmbeddableContextItem,
				name: tool.name,
				model: tool.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 );
};