All files / mobile.startup/watchstar WatchstarPageList.js

95.45% Statements 42/44
75% Branches 6/8
100% Functions 11/11
95.45% Lines 42/44

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 1591x 1x 1x 1x 1x 1x 1x                                       3x 3x     1x                   2x     2x 2x   2x   2x 2x   2x 4x     4x   2x     2x       2x 2x               3x                             4x       4x                     3x 3x 3x 7x 7x   3x               3x     3x         3x   7x 7x           7x   7x 7x                   7x                   1x  
var PageList = require( '../PageList' ),
	watchstar = require( './watchstar' ),
	user = mw.user,
	util = require( '../util' ),
	Page = require( '../Page' ),
	mfExtend = require( '../mfExtend' ),
	WatchstarGateway = require( './WatchstarGateway' );
 
/**
 * @typedef {Object.<PageTitle, PageID>} PageTitleToPageIDMap
 */
 
/**
 * List of items page view
 *
 * @class WatchstarPageList
 * @uses Page
 * @uses WatchstarGateway
 * @uses Watchstar
 * @extends PageList
 *
 * @fires WatchstarPageList#unwatch
 * @fires WatchstarPageList#watch
 * @param {Object} options Configuration options
 */
function WatchstarPageList( options ) {
	this.wsGateway = new WatchstarGateway( options.api );
	PageList.apply( this, arguments );
}
 
mfExtend( WatchstarPageList, PageList, {
	/**
	 * @memberof WatchstarPageList
	 * @instance
	 * @mixes PageList#defaults
	 * @property {Object} defaults Default options hash.
	 * @property {mw.Api} defaults.api
	 */
	postRender() {
		var
			self = this,
			$items,
			pages,
			ids = [],
			titles = [];
 
		PageList.prototype.postRender.apply( this );
 
		$items = this.queryUnitializedItems();
		pages = this.parsePagesFromItems( $items );
 
		Object.keys( pages ).forEach( ( title ) => {
			var id = pages[title];
			// Favor IDs since they're short and unlikely to exceed URL length
			// limits when batched.
			if ( id && id !== '0' ) {
				// ID is present and valid.
				ids.push( id );
			} else {
				// Only titles are available for missing pages.
				titles.push( title );
			}
		} );
 
		return this.getPages( ids, titles )
			.then( ( statuses ) => self.renderItems( $items, statuses ) );
	},
 
	/**
	 * @param {jQuery.Element} $items
	 * @param {WatchStatusMap} statuses
	 */
	queryUnitializedItems() {
		return this.$el.find( 'li:not(.with-watchstar)' );
	},
 
	/**
	 * Retrieve pages
	 *
	 * @memberof WatchstarPageList
	 * @instance
	 * @param {PageID[]} ids
	 * @param {PageTitle[]} titles
	 * @return {jQuery.Deferred<WatchStatusMap>}
	 */
	getPages( ids, titles ) {
		// Rendering Watchstars for anonymous users is not useful. Short-circuit
		// the request.
		Iif ( user.isAnon() ) {
			return util.Deferred().resolve( {} );
		}
 
		return this.wsGateway.getStatuses( ids, titles );
	},
 
	/**
	 * @param {jQuery.Element} $items
	 * @return {PageTitleToPageIDMap}
	 * @memberof WatchstarPageList
	 * @instance
	 */
	parsePagesFromItems( $items ) {
		var
			self = this,
			pages = {};
		$items.each( ( _, item ) => {
			var $item = self.$el.find( item );
			pages[$item.attr( 'title' )] = $item.data( 'id' );
		} );
		return pages;
	},
 
	/**
	 * @param {jQuery.Element} $items
	 * @param {WatchStatusMap} statuses
	 */
	renderItems( $items, statuses ) {
		var self = this;
 
		// Rendering Watchstars for anonymous users is not useful. Nothing to do.
		Iif ( user.isAnon() ) {
			return;
		}
 
		// Create watch stars for each entry in list
		$items.each( ( _, item ) => {
			var
				$item = self.$el.find( item ),
				page = new Page( {
					// FIXME: Set sections so we don't hit the api (hacky)
					sections: [],
					title: $item.attr( 'title' ),
					id: $item.data( 'id' )
				} ),
				watched = statuses[page.getTitle()];
 
			self._appendWatchstar( $item, page, watched );
			$item.addClass( 'with-watchstar' );
		} );
	},
 
	/**
	 * @param {jQuery.Object} $item
	 * @param {Page} page
	 * @param {WatchStatus} watched
	 */
	_appendWatchstar( $item, page, watched ) {
		watchstar( {
			// WatchstarPageList.getPages() already retrieved the status of
			// each page. Explicitly set the watch state so another request
			// will not be issued by the Watchstar.
			isWatched: watched,
			page
		} ).appendTo( $item );
	}
} );
 
module.exports = WatchstarPageList;