Debian Codegraph @

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

raw symbols SCIP
1 ( function () {
2 /**
3 * Sorted list widget. This is a group widget that sorts its items
4 * according to a given sorting callback.
5 *
6 * @class
7 * @extends OO.ui.Widget
8 * @mixes OO.SortedEmitterList
9 *
10 * @constructor
11 * @param {Function} sortingCallback Callback that compares two items.
12 * @param {Object} [config] Configuration options
13 * @param {jQuery} [config.$group] The container element created by the class. If this configuration
14 * is omitted, the group element will use a generated `<div>`.
15 * @param {jQuery} [config.$overlay] A jQuery element functioning as an overlay
16 * for popups.
17 * @param {number} [config.timestamp=0] A fallback timestamp for the list, usually representing
18 * the timestamp of the latest item.
19 * @param {boolean} [config.animated=false] Animate the sorting of items
20 */
21 mw.echo.ui.SortedListWidget = function MwEchoUiSortedListWidget( sortingCallback, config ) {
22 config = config || {};
23
24 // Parent constructor
25 mw.echo.ui.SortedListWidget.super.call( this, config );
26 // Mixin constructor
27 OO.SortedEmitterList.call( this, sortingCallback );
28
29 // Properties
30 this.$group = null;
31 this.$overlay = config.$overlay;
32 this.timestamp = config.timestamp || 0;
33
34 this.animated = !!config.animated;
35
36 // Initialization
37 this.setGroupElement( config.$group || $( '<div>' ) );
38
39 this.$group.addClass( 'mw-echo-ui-sortedListWidget-group' );
40 this.$element
41 .addClass( 'mw-echo-ui-sortedListWidget' )
42 .append( this.$group );
43 };
44
45 /* Initialization */
46
47 OO.inheritClass( mw.echo.ui.SortedListWidget, OO.ui.Widget );
48 OO.mixinClass( mw.echo.ui.SortedListWidget, OO.SortedEmitterList );
49
50 /* Methods */
51
52 /**
53 * @inheritdoc
54 */
55 mw.echo.ui.SortedListWidget.prototype.onItemSortChange = function ( item ) {
56 if ( this.animated ) {
57 // Create a fake widget with cloned contents
58 const fakeWidget = new mw.echo.ui.ClonedNotificationItemWidget(
59 item.$element.clone( true ),
60 {
61 id: item.getId() + '.42',
62 // HACK: We are assuming that the item sort change
63 // is triggered when the item is marked read/unread
64 // This is a generally correct assumption, but it may
65 // cause issues when the case is unclear. We should try
66 // and come up with a good way to measure the previous
67 // state of the item instead
68 read: !item.isRead(),
69 foreign: item.isForeign(),
70 timestamp: item.getTimestamp()
71 }
72 );
73
74 // remove real item from item list, without touching the DOM
75 this.removeItems( [ item ] );
76
77 // insert real item, hidden
78 item.$element.hide();
79 this.addItems( [ item, fakeWidget ] );
80
81 // fade out fake
82 // FIXME: Use CSS transition
83 // eslint-disable-next-line no-jquery/no-fade
84 fakeWidget.$element.fadeOut( 400, () => {
85 // remove fake
86 this.removeItems( [ fakeWidget ] );
87 // fade-in real item
88 // eslint-disable-next-line no-jquery/no-fade
89 item.$element.fadeIn( 400 );
90 } );
91 } else {
92 // Mixin method
93 OO.SortedEmitterList.prototype.onItemSortChange.call( this, item );
94 }
95 };
96 /**
97 * Set the group element.
98 *
99 * If an element is already set, items will be moved to the new element.
100 *
101 * @param {jQuery} $group Element to use as group
102 */
103 mw.echo.ui.SortedListWidget.prototype.setGroupElement = function ( $group ) {
104 this.$group = $group;
105 for ( let i = 0, len = this.items.length; i < len; i++ ) {
106 this.$group.append( this.items[ i ].$element );
107 }
108 };
109
110 /**
111 * Get an item by its id.
112 *
113 * @param {number} id Item id to search for
114 * @return {OO.ui.Element|null} Item with equivalent data, `null` if none exists
115 */
116 mw.echo.ui.SortedListWidget.prototype.getItemFromId = function ( id ) {
117 const hash = OO.getHash( id );
118
119 for ( let i = 0, len = this.items.length; i < len; i++ ) {
120 const item = this.items[ i ];
121 if ( hash === OO.getHash( item.getId() ) ) {
122 return item;
123 }
124 }
125
126 return null;
127 };
128
129 /**
130 * Get an item by its data.
131 *
132 * @param {string} data Item data to search for
133 * @return {OO.ui.Element|null} Item with equivalent data, `null` if none exists
134 */
135 mw.echo.ui.SortedListWidget.prototype.findItemFromData = function ( data ) {
136 const hash = OO.getHash( data );
137
138 for ( let i = 0, len = this.items.length; i < len; i++ ) {
139 const item = this.items[ i ];
140 if ( hash === OO.getHash( item.getData() ) ) {
141 return item;
142 }
143 }
144
145 return null;
146 };
147
148 /**
149 * Remove items.
150 *
151 * @param {OO.EventEmitter[]} items Items to remove
152 * @chainable
153 * @return {mw.echo.ui.SortedListWidget}
154 * @fires OO.EmitterList#remove
155 */
156 mw.echo.ui.SortedListWidget.prototype.removeItems = function ( items ) {
157 if ( !Array.isArray( items ) ) {
158 items = [ items ];
159 }
160
161 if ( items.length > 0 ) {
162 // Remove specific items
163 for ( let i = 0; i < items.length; i++ ) {
164 const item = items[ i ];
165 const index = this.items.indexOf( item );
166 if ( index !== -1 ) {
167 item.setElementGroup( null );
168 item.$element.detach();
169 }
170 }
171 }
172
173 return OO.SortedEmitterList.prototype.removeItems.call( this, items );
174 };
175
176 /**
177 * Utility method to insert an item into the list, and
178 * connect it to aggregate events.
179 *
180 * Don't call this directly unless you know what you're doing.
181 * Use #addItems instead.
182 *
183 * @private
184 * @param {OO.EventEmitter} item Items to add
185 * @param {number} index Index to add items at
186 * @return {number} The index the item was added at
187 */
188 mw.echo.ui.SortedListWidget.prototype.insertItem = function ( item, index ) {
189 // Call parent and get the normalized index
190 index = OO.SortedEmitterList.prototype.insertItem.call( this, item, index );
191
192 item.setElementGroup( this );
193
194 this.attachItemToDom( item, index );
195
196 return index;
197 };
198
199 /**
200 * Move an item from its current position to a new index.
201 *
202 * The item is expected to exist in the list. If it doesn't,
203 * the method will throw an exception.
204 *
205 * @private
206 * @param {OO.EventEmitter} item Items to add
207 * @param {number} index Index to move the item to
208 * @return {number} The index the item was moved to
209 * @throws {Error} If item is not in the list
210 */
211 mw.echo.ui.SortedListWidget.prototype.moveItem = function ( item, index ) {
212 // Call parent and get the normalized index
213 index = OO.SortedEmitterList.prototype.moveItem.call( this, item, index );
214
215 this.attachItemToDom( item, index );
216
217 return index;
218 };
219
220 /**
221 * Attach the item to the Dom in its intended position, based
222 * on the given index.
223 *
224 * @param {OO.EventEmitter} item Item
225 * @param {number} index Index to insert the item into
226 */
227 mw.echo.ui.SortedListWidget.prototype.attachItemToDom = function ( item, index ) {
228 if ( index === undefined || index < 0 || index >= this.items.length - 1 ) {
229 this.$group.append( item.$element.get( 0 ) );
230 } else if ( index === 0 ) {
231 this.$group.prepend( item.$element.get( 0 ) );
232 } else {
233 this.items[ index + 1 ].$element.before( item.$element.get( 0 ) );
234 }
235 };
236
237 /**
238 * Clear all items
239 *
240 * @chainable
241 * @return {mw.echo.ui.SortedListWidget}
242 * @fires OO.EmitterList#clear
243 */
244 mw.echo.ui.SortedListWidget.prototype.clearItems = function () {
245 for ( let i = 0, len = this.items.length; i < len; i++ ) {
246 const item = this.items[ i ];
247 item.setElementGroup( null );
248 item.$element.detach();
249 }
250
251 // Mixin method
252 return OO.SortedEmitterList.prototype.clearItems.call( this );
253 };
254
255 /**
256 * Get the timestamp of the list by taking the latest notification
257 * timestamp.
258 *
259 * @return {string} Latest timestamp
260 */
261 mw.echo.ui.SortedListWidget.prototype.getTimestamp = function () {
262 const items = this.getItems();
263
264 return (
265 items.length > 0 ?
266 items[ 0 ].getTimestamp() :
267 this.timestamp
268 );
269 };
270
271 }() );