2 * View model for the changes list
4 * @class mw.rcfilters.dm.ChangesListViewModel
5 * @mixins OO.EventEmitter
7 * @param {jQuery} $initialFieldset The initial server-generated legacy form content
10 var ChangesListViewModel
= function MwRcfiltersDmChangesListViewModel( $initialFieldset
) {
12 OO
.EventEmitter
.call( this );
15 this.newChangesExist
= false;
16 this.liveUpdate
= false;
17 this.unseenWatchedChanges
= false;
19 this.extractNextFrom( $initialFieldset
);
23 OO
.initClass( ChangesListViewModel
);
24 OO
.mixinClass( ChangesListViewModel
, OO
.EventEmitter
);
31 * The list of changes is now invalid (out of date)
36 * @param {jQuery|string} $changesListContent List of changes
37 * @param {jQuery} $fieldset Server-generated form
38 * @param {string} noResultsDetails Type of no result error
39 * @param {boolean} isInitialDOM Whether the previous dom variables are from the initial page load
40 * @param {boolean} fromLiveUpdate These are new changes fetched via Live Update
42 * The list of changes has been updated
46 * @event newChangesExist
47 * @param {boolean} newChangesExist
49 * The existence of changes newer than those currently displayed has changed.
53 * @event liveUpdateChange
54 * @param {boolean} enable
56 * The state of the 'live update' feature has changed.
62 * Invalidate the list of changes
66 ChangesListViewModel
.prototype.invalidate = function () {
69 this.emit( 'invalidate' );
74 * Update the model with an updated list of changes
76 * @param {jQuery|string} changesListContent
77 * @param {jQuery} $fieldset
78 * @param {string} noResultsDetails Type of no result error
79 * @param {boolean} [isInitialDOM] Using the initial (already attached) DOM elements
80 * @param {boolean} [separateOldAndNew] Whether a logical separation between old and new changes is needed
83 ChangesListViewModel
.prototype.update = function ( changesListContent
, $fieldset
, noResultsDetails
, isInitialDOM
, separateOldAndNew
) {
84 var from = this.nextFrom
;
86 this.extractNextFrom( $fieldset
);
87 this.checkForUnseenWatchedChanges( changesListContent
);
88 this.emit( 'update', changesListContent
, $fieldset
, noResultsDetails
, isInitialDOM
, separateOldAndNew
? from : null );
92 * Specify whether new changes exist
94 * @param {boolean} newChangesExist
95 * @fires newChangesExist
97 ChangesListViewModel
.prototype.setNewChangesExist = function ( newChangesExist
) {
98 if ( newChangesExist
!== this.newChangesExist
) {
99 this.newChangesExist
= newChangesExist
;
100 this.emit( 'newChangesExist', newChangesExist
);
105 * @return {boolean} Whether new changes exist
107 ChangesListViewModel
.prototype.getNewChangesExist = function () {
108 return this.newChangesExist
;
112 * Extract the value of the 'from' parameter from a link in the field set
114 * @param {jQuery} $fieldset
116 ChangesListViewModel
.prototype.extractNextFrom = function ( $fieldset
) {
117 var data
= $fieldset
.find( '.rclistfrom > a, .wlinfo' ).data( 'params' );
118 if ( data
&& data
.from ) {
119 this.nextFrom
= data
.from;
124 * @return {string} The 'from' parameter that can be used to query new changes
126 ChangesListViewModel
.prototype.getNextFrom = function () {
127 return this.nextFrom
;
131 * Toggle the 'live update' feature on/off
133 * @param {boolean} enable
135 ChangesListViewModel
.prototype.toggleLiveUpdate = function ( enable
) {
136 enable
= enable
=== undefined ? !this.liveUpdate
: enable
;
137 if ( enable
!== this.liveUpdate
) {
138 this.liveUpdate
= enable
;
139 this.emit( 'liveUpdateChange', this.liveUpdate
);
144 * @return {boolean} The 'live update' feature is enabled
146 ChangesListViewModel
.prototype.getLiveUpdate = function () {
147 return this.liveUpdate
;
151 * Check if some of the given changes watched and unseen
153 * @param {jQuery|string} changeslistContent
155 ChangesListViewModel
.prototype.checkForUnseenWatchedChanges = function ( changeslistContent
) {
156 this.unseenWatchedChanges
= changeslistContent
!== 'NO_RESULTS' &&
157 changeslistContent
.find( '.mw-changeslist-line-watched' ).length
> 0;
161 * @return {boolean} Whether some of the current changes are watched and unseen
163 ChangesListViewModel
.prototype.hasUnseenWatchedChanges = function () {
164 return this.unseenWatchedChanges
;
167 module
.exports
= ChangesListViewModel
;