Debian Codegraph @

extensions/Echo/modules/ui/mw.echo.ui.PageFilterWidget.js

raw symbols SCIP
1 ( function () {
2 /**
3 * A widget that displays wikis and their pages to choose a filter
4 *
5 * @class
6 * @extends OO.ui.SelectWidget
7 *
8 * @constructor
9 * @param {mw.echo.dm.FiltersModel} filterModel Filters model
10 * @param {string} source Symbolic name for the source
11 * @param {Object} [config] Configuration object
12 * @param {string} [config.title] The title of this page group, usually
13 * the name of the wiki that the pages belong to
14 * @param {number} [config.unreadCount] Number of unread notifications
15 * @param {number} [config.initialSelection] The page title of the option to select initially
16 */
17 mw.echo.ui.PageFilterWidget = function MwEchoUiPageFilterWidget( filterModel, source, config ) {
18 config = config || {};
19
20 // Parent constructor
21 mw.echo.ui.PageFilterWidget.super.call( this, config );
22
23 this.model = filterModel;
24 this.source = source;
25 // This is to be able to fetch and recognize this widget
26 // according to its source. The source is, in this case,
27 // unique per filter widget.
28 this.data = this.source;
29 this.totalCount = config.unreadCount || this.model.getSourceTotalCount( this.source );
30
31 this.initialSelection = config.initialSelection;
32
33 // Title option
34 this.title = new mw.echo.ui.PageNotificationsOptionWidget( {
35 label: config.title,
36 title: config.title,
37 count: this.totalCount,
38 isCapped: true,
39 data: null,
40 classes: [ 'mw-echo-ui-pageFilterWidget-title' ]
41 } );
42
43 // Initialization
44 this.populateDataFromModel();
45 this.$element
46 .addClass( 'mw-echo-ui-pageFilterWidget' )
47 .attr( 'aria-label', mw.msg( 'echo-specialpage-pagefilterwidget-aria-label' ) );
48 };
49
50 /* Initialization */
51
52 OO.inheritClass( mw.echo.ui.PageFilterWidget, OO.ui.SelectWidget );
53
54 /**
55 * Set the total count of this group
56 *
57 * @return {number} Total count
58 */
59 mw.echo.ui.PageFilterWidget.prototype.getTotalCount = function () {
60 return this.totalCount;
61 };
62
63 /**
64 * Populate the widget from the model
65 */
66 mw.echo.ui.PageFilterWidget.prototype.populateDataFromModel = function () {
67 const optionWidgets = [],
68 sourcePages = this.model.getSourcePages( this.source );
69
70 for ( const title in sourcePages ) {
71 const isUserPage = sourcePages[ title ].ns === mw.config.get( 'wgNamespaceIds' ).user;
72 const widget = new mw.echo.ui.PageNotificationsOptionWidget( {
73 label: isUserPage ? sourcePages[ title ].unprefixed : title,
74 title: isUserPage ? sourcePages[ title ].unprefixed : title,
75 icon: isUserPage ? 'userAvatar' : 'article',
76 count: sourcePages[ title ].count,
77 data: title,
78 classes: [ 'mw-echo-ui-pageFilterWidget-page' ]
79 } );
80 optionWidgets.push( widget );
81
82 if ( this.initialSelection === title ) {
83 widget.setSelected( true );
84 }
85 }
86
87 this.setItems( optionWidgets );
88 };
89
90 /**
91 * Get the source associated with this filter
92 *
93 * @return {string} Source symbolic name
94 */
95 mw.echo.ui.PageFilterWidget.prototype.getSource = function () {
96 return this.source;
97 };
98
99 /**
100 * Get the title item
101 *
102 * @return {mw.echo.ui.PageNotificationsOptionWidget} Page title item
103 */
104 mw.echo.ui.PageFilterWidget.prototype.getTitleItem = function () {
105 return this.title;
106 };
107
108 /**
109 * Set the page items in this widget, in order
110 *
111 * @param {mw.echo.ui.PageNotificationsOptionWidget[]} items Item widgets to order and insert
112 */
113 mw.echo.ui.PageFilterWidget.prototype.setItems = function ( items ) {
114 this.clearItems();
115 for ( let i = 0; i < items.length; i++ ) {
116 const index = this.findInsertionIndex( items[ i ] );
117 this.addItems( [ items[ i ] ], index );
118 }
119
120 // Add the title on top
121 this.addItems( [ this.title ], 0 );
122 };
123
124 /**
125 * Find the proper insertion index for ordering when inserting items
126 *
127 * @private
128 * @param {mw.echo.ui.PageNotificationsOptionWidget} item Item widget
129 * @return {number} Insertion index
130 */
131 mw.echo.ui.PageFilterWidget.prototype.findInsertionIndex = function ( item ) {
132 return OO.binarySearch(
133 this.items,
134 ( otherItem ) => this.sortingFunction( item, otherItem ),
135 true
136 );
137 };
138
139 /**
140 * Sorting function for item insertion
141 *
142 * @private
143 * @param {mw.echo.ui.PageNotificationsOptionWidget} item Item widget
144 * @param {mw.echo.ui.PageNotificationsOptionWidget} otherItem Another item widget
145 * @return {number} Ordering value
146 */
147 mw.echo.ui.PageFilterWidget.prototype.sortingFunction = function ( item, otherItem ) {
148 return Number( otherItem.getCount() ) - Number( item.getCount() );
149 };
150 }() );