All files / src/init/sa ve.init.sa.Platform.js

62.59% Statements 82/131
50% Branches 17/34
41.66% Functions 10/24
60.8% Lines 76/125

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   4x     4x 4x 4x 4x 4x         1x         1x         1x         1x                                                                                                                         1x 2x       1x 2x             1x           1x                 1x             1x                                               1x               1x 12x 3x 3x 6x   3x   9x 9x                   1x 7x 2x 4x 4x               5x 5x         7x     1x 8x           1x 1x 1x             1x 3x 1x     2x 3x                         1x               1x           1x           1x         1x             1x 2x 2x 2x 2x 2x 2x   2x       2x       2x 2x 2x 2x 2x 2x 2x       2x       2x   2x 6x 2x   4x       4x   4x             2x    
/*!
 * VisualEditor Standalone Initialization Platform class.
 *
 * @copyright See AUTHORS.txt
 */
 
/**
 * Initialization Standalone platform.
 *
 *     @example
 *     var platform = new ve.init.sa.Platform( ve.messagePaths );
 *     platform.initialize().done( function () {
 *         $( document.body ).append( $( '<p>' ).text(
 *             platform.getMessage( 'visualeditor' )
 *         ) );
 *     } );
 *
 * @class
 * @extends ve.init.Platform
 *
 * @constructor
 * @param {string[]} [messagePaths] Message folder paths
 */
ve.init.sa.Platform = function VeInitSaPlatform( messagePaths ) {
	// Parent constructor
	ve.init.Platform.call( this );
 
	// Properties
	this.externalLinkUrlProtocolsRegExp = /^https?:\/\//i;
	this.unanchoredExternalLinkUrlProtocolsRegExp = /https?:\/\//i;
	this.messagePaths = messagePaths || [];
	this.parsedMessages = {};
	this.userLanguages = [ 'en' ];
};
 
/* Inheritance */
 
OO.inheritClass( ve.init.sa.Platform, ve.init.Platform );
 
/* Methods */
 
/** @inheritdoc */
ve.init.sa.Platform.prototype.getExternalLinkUrlProtocolsRegExp = function () {
	return this.externalLinkUrlProtocolsRegExp;
};
 
/** @inheritdoc */
ve.init.sa.Platform.prototype.getUnanchoredExternalLinkUrlProtocolsRegExp = function () {
	return this.unanchoredExternalLinkUrlProtocolsRegExp;
};
 
/** @inheritdoc */
ve.init.sa.Platform.prototype.notify = function ( message, title ) {
	var $notification = $( '<div>' ).addClass( 've-init-notification' );
 
	if ( title ) {
		$notification.append(
			// Never appends strings directly
			// eslint-disable-next-line no-jquery/no-append-html
			$( '<div>' ).addClass( 've-init-notification-title' ).append(
				typeof title === 'string' ? document.createTextNode( title ) : title
			)
		);
	}
	$notification.append(
		// Never appends strings directly
		// eslint-disable-next-line no-jquery/no-append-html
		$( '<div>' ).addClass( 've-init-notification-message' ).append(
			typeof message === 'string' ? document.createTextNode( message ) : message
		)
	);
 
	var $notificationWrapper = $( '<div>' ).addClass( 've-init-notification-wrapper' );
	$notificationWrapper.append( $notification );
 
	if ( !this.$notifications ) {
		this.$notifications = $( '<div>' ).addClass( 've-init-notifications' );
		$( document.body ).append( this.$notifications );
	}
 
	var closeId;
 
	function remove() {
		$notificationWrapper.remove();
	}
	function collapse() {
		$notificationWrapper.addClass( 've-init-notification-collapse' );
		setTimeout( remove, 250 );
	}
	function close() {
		clearTimeout( closeId );
		$notificationWrapper.removeClass( 've-init-notification-open' );
		$notificationWrapper.css( 'height', $notificationWrapper[ 0 ].clientHeight );
		setTimeout( collapse, 250 );
	}
	function open() {
		$notificationWrapper.addClass( 've-init-notification-open' );
		closeId = setTimeout( close, 5000 );
	}
 
	var rAF = window.requestAnimationFrame || setTimeout;
	rAF( open );
 
	$notification.on( 'click', close );
 
	this.$notifications.append( $notificationWrapper );
};
 
/**
 * Get message folder paths
 *
 * @return {string[]} Message folder paths
 */
ve.init.sa.Platform.prototype.getMessagePaths = function () {
	return this.messagePaths;
};
 
/** @inheritdoc */
ve.init.sa.Platform.prototype.addMessages = function ( messages ) {
	$.i18n().load( messages, $.i18n().locale );
};
 
/**
 * @method
 * @inheritdoc
 */
ve.init.sa.Platform.prototype.getMessage = $.i18n;
 
/**
 * @method
 * @inheritdoc
 */
ve.init.sa.Platform.prototype.parseNumber = function ( value ) {
	// TODO: Support separated numbers such as (en)123,456.78 or (fr)123.456,78
	return parseFloat( value );
};
 
/**
 * @method
 * @inheritdoc
 */
ve.init.sa.Platform.prototype.formatNumber = function ( number ) {
	return number.toLocaleString();
};
 
/**
 * @inheritdoc
 */
ve.init.sa.Platform.prototype.getHtmlMessage = function ( key ) {
	var $message = $( [] ),
		lastOffset = 0,
		args = arguments,
		message = this.getMessage( key );
	message.replace( /\$[0-9]+/g, function ( placeholder, offset ) {
		$message = $message.add( ve.sanitizeHtml( message.slice( lastOffset, offset ) ) );
		var placeholderIndex = +( placeholder.slice( 1 ) );
		var arg = args[ placeholderIndex ];
		$message = $message.add(
			typeof arg === 'string' ?
				// Arguments come from the code so shouldn't be sanitized
				document.createTextNode( arg ) :
				arg
		);
		lastOffset = offset + placeholder.length;
	} );
	$message = $message.add( ve.sanitizeHtml( message.slice( lastOffset ) ) );
	return $message.toArray();
};
 
/**
 * @inheritdoc
 */
ve.init.sa.Platform.prototype.getConfig = function () {
	/* Standalone has no config yet */
	return null;
};
 
/**
 * @inheritdoc
 */
ve.init.sa.Platform.prototype.getUserConfig = function ( keys ) {
	if ( Array.isArray( keys ) ) {
		var values = {};
		for ( var i = 0, l = keys.length; i < l; i++ ) {
			values[ keys[ i ] ] = this.getUserConfig( keys[ i ] );
		}
		return values;
	} else {
		try {
			return JSON.parse( localStorage.getItem( 've-' + keys ) );
		} catch ( e ) {
			return null;
		}
	}
};
 
/**
 * @inheritdoc
 */
ve.init.sa.Platform.prototype.setUserConfig = function ( keyOrValueMap, value ) {
	if ( typeof keyOrValueMap === 'object' ) {
		for ( var i in keyOrValueMap ) {
			Eif ( Object.prototype.hasOwnProperty.call( keyOrValueMap, i ) ) {
				Iif ( !this.setUserConfig( i, keyOrValueMap[ i ] ) ) {
					// localStorage will fail if the quota is full, so further
					// sets won't work anyway.
					return false;
				}
			}
		}
	} else {
		try {
			localStorage.setItem( 've-' + keyOrValueMap, JSON.stringify( value ) );
		} catch ( e ) {
			return false;
		}
	}
	return true;
};
 
ve.init.sa.Platform.prototype.createSafeStorage = function ( storage ) {
	return new ve.init.sa.SafeStorage( storage );
};
 
/**
 * @inheritdoc
 */
ve.init.sa.Platform.prototype.addParsedMessages = function ( messages ) {
	for ( var key in messages ) {
		this.parsedMessages[ key ] = messages[ key ];
	}
};
 
/**
 * @inheritdoc
 */
ve.init.sa.Platform.prototype.getParsedMessage = function ( key ) {
	if ( Object.prototype.hasOwnProperty.call( this.parsedMessages, key ) ) {
		return this.parsedMessages[ key ];
	}
	// Fallback to regular messages, html escaping applied.
	return this.getMessage( key ).replace( /['"<>&]/g, function ( char ) {
		return {
			'\'': '&#039;',
			'"': '&quot;',
			'<': '&lt;',
			'>': '&gt;',
			'&': '&amp;'
		}[ char ];
	} );
};
 
/**
 * @inheritdoc
 */
ve.init.sa.Platform.prototype.getLanguageCodes = function () {
	return Object.keys( $.uls.data.getAutonyms() );
};
 
/**
 * @method
 * @inheritdoc
 */
ve.init.sa.Platform.prototype.getLanguageName = $.uls.data.getAutonym;
 
/**
 * @method
 * @inheritdoc
 */
ve.init.sa.Platform.prototype.getLanguageAutonym = $.uls.data.getAutonym;
 
/**
 * @method
 * @inheritdoc
 */
ve.init.sa.Platform.prototype.getLanguageDirection = $.uls.data.getDir;
 
/**
 * @inheritdoc
 */
ve.init.sa.Platform.prototype.getUserLanguages = function () {
	return this.userLanguages;
};
 
/**
 * @inheritdoc
 */
ve.init.sa.Platform.prototype.initialize = function () {
	var messagePaths = this.getMessagePaths(),
		locale = $.i18n().locale,
		languages = [ locale, 'en' ], // Always use 'en' as the final fallback
		languagesCovered = {},
		promises = [],
		fallbacks = $.i18n.fallbacks[ locale ];
 
	Iif ( !VisualEditorSupportCheck() ) {
		return ve.createDeferred().reject().promise();
	}
 
	Eif ( !fallbacks ) {
		// Try to find something that has fallbacks (which means it's a language we know about)
		// by stripping things from the end. But collect all the intermediate ones in case we
		// go past languages that don't have fallbacks but do exist.
		var localeParts = locale.split( '-' );
		localeParts.pop();
		while ( localeParts.length && !fallbacks ) {
			var partialLocale = localeParts.join( '-' );
			languages.push( partialLocale );
			fallbacks = $.i18n.fallbacks[ partialLocale ];
			localeParts.pop();
		}
	}
 
	Iif ( fallbacks ) {
		languages = languages.concat( fallbacks );
	}
 
	this.userLanguages = languages;
 
	for ( var i = 0, iLen = languages.length; i < iLen; i++ ) {
		if ( languagesCovered[ languages[ i ] ] ) {
			continue;
		}
		languagesCovered[ languages[ i ] ] = true;
 
		// Lower-case the language code for the filename. jQuery.i18n does not case-fold
		// language codes, so we should not case-fold the second argument in #load.
		var filename = languages[ i ].toLowerCase() + '.json';
 
		for ( var j = 0, jLen = messagePaths.length; j < jLen; j++ ) {
			var deferred = ve.createDeferred();
			$.i18n().load( messagePaths[ j ] + filename, languages[ i ] )
				.always( deferred.resolve );
			promises.push( deferred.promise() );
		}
	}
	return ve.promiseAll( promises );
};