All files / mobile.startup Page.js

53.84% Statements 14/26
74.28% Branches 26/35
58.33% Functions 7/12
53.84% Lines 14/26

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  1x 1x                                                                         52x 52x                                             52x 2x                   3x                                   10x                 10x                     10x 10x                                               2x                                                   7x                                         1x  
var
	HTML = mw.html,
	util = require( './util' );
 
/**
 * Mobile page view object
 */
class Page {
	/**
	 * @param {Object} options Configuration options
	 * @param {number} options.id Page ID. The default value of 0 represents a
	 * new or missing page. Be sure to override it to avoid side effects.
	 * @param {string} options.title Title of the page. It includes prefix where needed and
	 * is human readable, e.g. Talk:The man who lived.
	 * @param {Object} options.titleObj
	 * @param {string} options.displayTitle HTML title of the page for display. Falls back
	 * to defaults.title (escaped) if no value is provided. Must be safe HTML!
	 * @param {number} options.namespaceNumber the number of the
	 *  namespace the page belongs to
	 * @param {Object} options.protection List of permissions as returned by API,
	 * e.g. [{ edit: ['*'] }]
	 * @param {string} options.url
	 * @param {string} options.wikidataDescription
	 * @param {boolean} options.isMainPage Whether the page is the Main Page.
	 * @param {boolean} options.isMissing Whether the page exists in the wiki.
	 * @param {Date} [options.lastModified]
	 * @param {string} options.anchor
	 * @param {string} [options.relevantTitle] associated with page.
	 *  For example Special:WhatLinksHere/Foo would be associated with the page `Foo`.
	 * @param {number} options.revId  Revision ID. See `wgRevisionId`.
	 * @param {boolean} options.isWatched Whether the page is being watched
	 * @param {Object} options.thumbnail thumbnail definition corresponding to page image
	 * @param {boolean} options.thumbnail.isLandscape whether the image is in
	 *  landscape format
	 * @param {number} options.thumbnail.width of image in pixels.
	 * @param {number} options.thumbnail.height of image in pixels.
	 * @param {string} options.thumbnail.source url for image
	 */
	constructor( options ) {
		const title = options.title || '';
		util.extend( this, {
			id: options.id || 0,
			// FIXME: Deprecate title property as it can be derived from titleObj
			// using getPrefixedText
			title,
			relevantTitle: options.relevantTitle || title,
			titleObj: options.titleObj,
			displayTitle: options.displayTitle || HTML.escape( title ),
			namespaceNumber: options.namespaceNumber || 0,
			protection: options.protection,
			url: options.url || mw.util.getUrl( title ),
			wikidataDescription: options.wikidataDescription,
			_isMainPage: options.isMainPage || false,
			isMissing: ( options.isMissing !== undefined ) ?
				options.isMissing : options.id === 0,
			lastModified: options.lastModified,
			anchor: options.anchor,
			revId: options.revId,
			_isWatched: options.isWatched,
			thumbnail: ( Object.prototype.hasOwnProperty.call( options, 'thumbnail' ) ) ?
				options.thumbnail : false
		} );
 
		if ( this.thumbnail && this.thumbnail.width ) {
			this.thumbnail.isLandscape = this.thumbnail.width > this.thumbnail.height;
		}
	}
 
	/**
	 * Retrieve the title that should be displayed to the user
	 *
	 * @return {string} HTML
	 */
	getDisplayTitle() {
		return this.displayTitle;
	}
	/**
	 * Determine if current page is in a specified namespace
	 *
	 * @param {string} namespace Name of namespace
	 * @return {boolean}
	 */
	inNamespace( namespace ) {
		return this.namespaceNumber === mw.config.get( 'wgNamespaceIds' )[namespace];
	}
 
	/**
	 * Determines if content model is wikitext
	 *
	 * @return {boolean}
	 */
	isWikiText() {
		return mw.config.get( 'wgPageContentModel' ) === 'wikitext';
	}
 
	/**
	 * Check if the visual editor is available on this page
	 *
	 * @return {boolean}
	 */
	isVEAvailable() {
		return !!mw.config.get( 'wgVisualEditorConfig' ) &&
			!mw.config.get( 'wgVisualEditorDisabledByHook' ) &&
			this.isWikiText();
	}
 
	/**
	 * Check if the visual editor in visual mode is available on this page
	 *
	 * @return {boolean}
	 */
	isVEVisualAvailable() {
		Eif ( !this.isVEAvailable() ) {
			return false;
		}
		var config = mw.config.get( 'wgVisualEditorConfig' );
		var visualEditorNamespaces = config.namespaces || [];
 
		return visualEditorNamespaces.indexOf( mw.config.get( 'wgNamespaceNumber' ) ) !== -1;
	}
 
	/**
	 * Check if the visual editor in source mode is available on this page
	 *
	 * @return {boolean}
	 */
	isVESourceAvailable() {
		return this.isVEAvailable() &&
			mw.config.get( 'wgMFEnableVEWikitextEditor' );
	}
 
	/**
	 * Checks whether the current page is the main page
	 *
	 * @return {boolean}
	 */
	isMainPage() {
		return this._isMainPage;
	}
	/**
	 * Checks whether the current page is watched
	 *
	 * @return {boolean}
	 */
	isWatched() {
		return this._isWatched;
	}
 
	/**
	 * Return the latest revision id for this page
	 *
	 * @return {number}
	 */
	getRevisionId() {
		return this.revId;
	}
 
	/**
	 * Return prefixed page title
	 *
	 * @return {string}
	 */
	getTitle() {
		return this.title;
	}
 
	/**
	 * return namespace id
	 *
	 * @return {number} namespace Number
	 */
	getNamespaceId() {
		var nsId,
			args = this.title.split( ':' );
 
		if ( args[1] ) {
			nsId = mw.config.get( 'wgNamespaceIds' )[ args[0].toLowerCase().replace( ' ', '_' ) ] || 0;
		} else {
			nsId = 0;
		}
		return nsId;
	}
}
 
module.exports = Page;