All files / src/ce ve.ce.SurfaceObserver.js

92.72% Statements 51/55
85% Branches 17/20
100% Functions 14/14
92.72% Lines 51/55

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                            1x   326x 326x 326x 326x 326x 326x 326x         1x             1x 106x           1x 297x 297x 297x           1x 413x 413x               1x 413x         413x       413x 146x                   1x 751x 413x 413x 413x             1x 4x           1x 4x           1x 1637x           1x 1986x               1x 11x                   1x 3634x 44x     3590x 3590x         3590x   3590x         587x                     1x 146x                   1x 29x     29x    
/*!
 * VisualEditor ContentEditable Surface class.
 *
 * @copyright See AUTHORS.txt
 */
 
/**
 * ContentEditable surface observer.
 *
 * @class
 *
 * @constructor
 * @param {ve.ce.Surface} surface Surface to observe
 */
ve.ce.SurfaceObserver = function VeCeSurfaceObserver( surface ) {
	// Properties
	this.surface = surface;
	this.domDocument = surface.attachedRoot.getElementDocument();
	this.polling = false;
	this.disabled = false;
	this.timeoutId = null;
	this.pollInterval = 250; // ms
	this.rangeState = null;
};
 
/* Inheritance */
 
OO.initClass( ve.ce.SurfaceObserver );
 
/* Methods */
 
/**
 * Clear polling data.
 */
ve.ce.SurfaceObserver.prototype.clear = function () {
	this.rangeState = null;
};
 
/**
 * Detach from the document view
 */
ve.ce.SurfaceObserver.prototype.detach = function () {
	this.surface = null;
	this.domDocument = null;
	this.rangeState = null;
};
 
/**
 * Start the setTimeout synchronisation loop
 */
ve.ce.SurfaceObserver.prototype.startTimerLoop = function () {
	this.polling = true;
	this.timerLoop( true ); // Will not sync immediately, because timeoutId should be null
};
 
/**
 * Loop once with `setTimeout`
 *
 * @param {boolean} firstTime Wait before polling
 */
ve.ce.SurfaceObserver.prototype.timerLoop = function ( firstTime ) {
	Iif ( this.timeoutId ) {
		// In case we're not running from setTimeout
		clearTimeout( this.timeoutId );
		this.timeoutId = null;
	}
	Iif ( !firstTime ) {
		this.pollOnce();
	}
	// Only reach this point if pollOnce does not throw an exception
	if ( this.pollInterval !== null ) {
		this.timeoutId = this.setTimeout(
			this.timerLoop.bind( this ),
			this.pollInterval
		);
	}
};
 
/**
 * Stop polling
 */
ve.ce.SurfaceObserver.prototype.stopTimerLoop = function () {
	if ( this.polling === true ) {
		this.polling = false;
		clearTimeout( this.timeoutId );
		this.timeoutId = null;
	}
};
 
/**
 * Disable the surface observer
 */
ve.ce.SurfaceObserver.prototype.disable = function () {
	this.disabled = true;
};
 
/**
 * Enable the surface observer
 */
ve.ce.SurfaceObserver.prototype.enable = function () {
	this.disabled = false;
};
 
/**
 * Poll for changes.
 */
ve.ce.SurfaceObserver.prototype.pollOnce = function () {
	this.pollOnceInternal( true );
};
 
/**
 * Poll to update SurfaceObserver, but don't signal any changes back to the Surface
 */
ve.ce.SurfaceObserver.prototype.pollOnceNoCallback = function () {
	this.pollOnceInternal( false );
};
 
/**
 * Poll to update SurfaceObserver, but only check for selection changes
 *
 * Used as an optimisation when you know the content hasn't changed
 */
ve.ce.SurfaceObserver.prototype.pollOnceSelection = function () {
	this.pollOnceInternal( true, true );
};
 
/**
 * Poll for changes.
 *
 * @private
 * @param {boolean} signalChanges If there changes are observed, call Surface#handleObservedChange
 * @param {boolean} selectionOnly Check for selection changes only
 */
ve.ce.SurfaceObserver.prototype.pollOnceInternal = function ( signalChanges, selectionOnly ) {
	if ( !this.domDocument || this.disabled ) {
		return;
	}
 
	var oldState = this.rangeState;
	var newState = new ve.ce.RangeState(
		oldState,
		this.surface.attachedRoot,
		selectionOnly
	);
	this.rangeState = newState;
 
	if ( signalChanges && (
		newState.contentChanged ||
		newState.branchNodeChanged ||
		newState.selectionChanged
	) ) {
		this.surface.handleObservedChanges( oldState, newState );
	}
};
 
/**
 * Wrapper for setTimeout, for ease of debugging
 *
 * @param {Function} callback
 * @param {number} timeout Timeout ms
 * @return {number} Timeout ID
 */
ve.ce.SurfaceObserver.prototype.setTimeout = function ( callback, timeout ) {
	return setTimeout( callback, timeout );
};
 
/**
 * Get the range last observed.
 *
 * Used when you have just polled, but don't want to wait for a 'rangeChange' event.
 *
 * @return {ve.Range|null}
 */
ve.ce.SurfaceObserver.prototype.getRange = function () {
	Iif ( !this.rangeState ) {
		return null;
	}
	return this.rangeState.veRange;
};