3 * View model for the changes list
5 * @class mw.rcfilters.dm.ChangesListViewModel
6 * @mixins OO.EventEmitter
8 * @param {jQuery} $initialFieldset The initial server-generated legacy form content
11 var ChangesListViewModel
= function MwRcfiltersDmChangesListViewModel( $initialFieldset
) {
13 OO
.EventEmitter
.call( this );
16 this.newChangesExist
= false;
17 this.liveUpdate
= false;
18 this.unseenWatchedChanges
= false;
20 this.extractNextFrom( $initialFieldset
);
24 OO
.initClass( ChangesListViewModel
);
25 OO
.mixinClass( ChangesListViewModel
, OO
.EventEmitter
);
32 * The list of changes is now invalid (out of date)
37 * @param {jQuery|string} $changesListContent List of changes
38 * @param {jQuery} $fieldset Server-generated form
39 * @param {string} noResultsDetails Type of no result error
40 * @param {boolean} isInitialDOM Whether the previous dom variables are from the initial page load
41 * @param {boolean} fromLiveUpdate These are new changes fetched via Live Update
43 * The list of changes has been updated
47 * @event newChangesExist
48 * @param {boolean} newChangesExist
50 * The existence of changes newer than those currently displayed has changed.
54 * @event liveUpdateChange
55 * @param {boolean} enable
57 * The state of the 'live update' feature has changed.
63 * Invalidate the list of changes
67 ChangesListViewModel
.prototype.invalidate = function () {
70 this.emit( 'invalidate' );
75 * Update the model with an updated list of changes
77 * @param {jQuery|string} changesListContent
78 * @param {jQuery} $fieldset
79 * @param {string} noResultsDetails Type of no result error
80 * @param {boolean} [isInitialDOM] Using the initial (already attached) DOM elements
81 * @param {boolean} [separateOldAndNew] Whether a logical separation between old and new changes is needed
84 ChangesListViewModel
.prototype.update = function ( changesListContent
, $fieldset
, noResultsDetails
, isInitialDOM
, separateOldAndNew
) {
85 var from = this.nextFrom
;
87 this.extractNextFrom( $fieldset
);
88 this.checkForUnseenWatchedChanges( changesListContent
);
89 this.emit( 'update', changesListContent
, $fieldset
, noResultsDetails
, isInitialDOM
, separateOldAndNew
? from : null );
93 * Specify whether new changes exist
95 * @param {boolean} newChangesExist
96 * @fires newChangesExist
98 ChangesListViewModel
.prototype.setNewChangesExist = function ( newChangesExist
) {
99 if ( newChangesExist
!== this.newChangesExist
) {
100 this.newChangesExist
= newChangesExist
;
101 this.emit( 'newChangesExist', newChangesExist
);
106 * @return {boolean} Whether new changes exist
108 ChangesListViewModel
.prototype.getNewChangesExist = function () {
109 return this.newChangesExist
;
113 * Extract the value of the 'from' parameter from a link in the field set
115 * @param {jQuery} $fieldset
117 ChangesListViewModel
.prototype.extractNextFrom = function ( $fieldset
) {
118 var data
= $fieldset
.find( '.rclistfrom > a, .wlinfo' ).data( 'params' );
119 if ( data
&& data
.from ) {
120 this.nextFrom
= data
.from;
125 * @return {string} The 'from' parameter that can be used to query new changes
127 ChangesListViewModel
.prototype.getNextFrom = function () {
128 return this.nextFrom
;
132 * Toggle the 'live update' feature on/off
134 * @param {boolean} enable
136 ChangesListViewModel
.prototype.toggleLiveUpdate = function ( enable
) {
137 enable
= enable
=== undefined ? !this.liveUpdate
: enable
;
138 if ( enable
!== this.liveUpdate
) {
139 this.liveUpdate
= enable
;
140 this.emit( 'liveUpdateChange', this.liveUpdate
);
145 * @return {boolean} The 'live update' feature is enabled
147 ChangesListViewModel
.prototype.getLiveUpdate = function () {
148 return this.liveUpdate
;
152 * Check if some of the given changes watched and unseen
154 * @param {jQuery|string} changeslistContent
156 ChangesListViewModel
.prototype.checkForUnseenWatchedChanges = function ( changeslistContent
) {
157 this.unseenWatchedChanges
= changeslistContent
!== 'NO_RESULTS' &&
158 changeslistContent
.find( '.mw-changeslist-line-watched' ).length
> 0;
162 * @return {boolean} Whether some of the current changes are watched and unseen
164 ChangesListViewModel
.prototype.hasUnseenWatchedChanges = function () {
165 return this.unseenWatchedChanges
;
168 module
.exports
= ChangesListViewModel
;