Merge "Don't hard-code Preferences page name"
[lhc/web/wiklou.git] / resources / src / mediawiki.rcfilters / dm / mw.rcfilters.dm.ChangesListViewModel.js
1 ( function ( mw ) {
2 /**
3 * View model for the changes list
4 *
5 * @mixins OO.EventEmitter
6 *
7 * @constructor
8 */
9 mw.rcfilters.dm.ChangesListViewModel = function MwRcfiltersDmChangesListViewModel() {
10 // Mixin constructor
11 OO.EventEmitter.call( this );
12
13 this.valid = true;
14 this.newChangesExist = false;
15 this.nextFrom = null;
16 this.liveUpdate = false;
17 this.unseenWatchedChanges = false;
18 };
19
20 /* Initialization */
21 OO.initClass( mw.rcfilters.dm.ChangesListViewModel );
22 OO.mixinClass( mw.rcfilters.dm.ChangesListViewModel, OO.EventEmitter );
23
24 /* Events */
25
26 /**
27 * @event invalidate
28 *
29 * The list of changes is now invalid (out of date)
30 */
31
32 /**
33 * @event update
34 * @param {jQuery|string} $changesListContent List of changes
35 * @param {jQuery} $fieldset Server-generated form
36 * @param {boolean} isInitialDOM Whether the previous dom variables are from the initial page load
37 * @param {boolean} fromLiveUpdate These are new changes fetched via Live Update
38 *
39 * The list of changes has been updated
40 */
41
42 /**
43 * @event newChangesExist
44 * @param {boolean} newChangesExist
45 *
46 * The existence of changes newer than those currently displayed has changed.
47 */
48
49 /**
50 * @event liveUpdateChange
51 * @param {boolean} enable
52 *
53 * The state of the 'live update' feature has changed.
54 */
55
56 /* Methods */
57
58 /**
59 * Invalidate the list of changes
60 *
61 * @fires invalidate
62 */
63 mw.rcfilters.dm.ChangesListViewModel.prototype.invalidate = function () {
64 if ( this.valid ) {
65 this.valid = false;
66 this.emit( 'invalidate' );
67 }
68 };
69
70 /**
71 * Update the model with an updated list of changes
72 *
73 * @param {jQuery|string} changesListContent
74 * @param {jQuery} $fieldset
75 * @param {boolean} [isInitialDOM] Using the initial (already attached) DOM elements
76 * @param {boolean} [separateOldAndNew] Whether a logical separation between old and new changes is needed
77 * @fires update
78 */
79 mw.rcfilters.dm.ChangesListViewModel.prototype.update = function ( changesListContent, $fieldset, isInitialDOM, separateOldAndNew ) {
80 var from = this.nextFrom;
81 this.valid = true;
82 if ( mw.rcfilters.featureFlags.liveUpdate ) {
83 this.extractNextFrom( $fieldset );
84 }
85 this.checkForUnseenWatchedChanges( changesListContent );
86 this.emit( 'update', changesListContent, $fieldset, isInitialDOM, separateOldAndNew ? from : null );
87 };
88
89 /**
90 * Specify whether new changes exist
91 *
92 * @param {boolean} newChangesExist
93 * @fires newChangesExist
94 */
95 mw.rcfilters.dm.ChangesListViewModel.prototype.setNewChangesExist = function ( newChangesExist ) {
96 if ( newChangesExist !== this.newChangesExist ) {
97 this.newChangesExist = newChangesExist;
98 this.emit( 'newChangesExist', newChangesExist );
99 }
100 };
101
102 /**
103 * @return {boolean} Whether new changes exist
104 */
105 mw.rcfilters.dm.ChangesListViewModel.prototype.getNewChangesExist = function () {
106 return this.newChangesExist;
107 };
108
109 /**
110 * Extract the value of the 'from' parameter from a link in the field set
111 *
112 * @param {jQuery} $fieldset
113 */
114 mw.rcfilters.dm.ChangesListViewModel.prototype.extractNextFrom = function ( $fieldset ) {
115 this.nextFrom = $fieldset.find( '.rclistfrom > a' ).data( 'params' ).from;
116 };
117
118 /**
119 * @return {string} The 'from' parameter that can be used to query new changes
120 */
121 mw.rcfilters.dm.ChangesListViewModel.prototype.getNextFrom = function () {
122 return this.nextFrom;
123 };
124
125 /**
126 * Toggle the 'live update' feature on/off
127 *
128 * @param {boolean} enable
129 */
130 mw.rcfilters.dm.ChangesListViewModel.prototype.toggleLiveUpdate = function ( enable ) {
131 enable = enable === undefined ? !this.liveUpdate : enable;
132 if ( enable !== this.liveUpdate ) {
133 this.liveUpdate = enable;
134 this.emit( 'liveUpdateChange', this.liveUpdate );
135 }
136 };
137
138 /**
139 * @return {boolean} The 'live update' feature is enabled
140 */
141 mw.rcfilters.dm.ChangesListViewModel.prototype.getLiveUpdate = function () {
142 return this.liveUpdate;
143 };
144
145 /**
146 * Check if some of the given changes watched and unseen
147 *
148 * @param {jQuery|string} changeslistContent
149 */
150 mw.rcfilters.dm.ChangesListViewModel.prototype.checkForUnseenWatchedChanges = function ( changeslistContent ) {
151 this.unseenWatchedChanges = changeslistContent !== 'NO_RESULTS' &&
152 changeslistContent.find( '.mw-changeslist-line-watched' ).length > 0;
153 };
154
155 /**
156 * @return {boolean} Whether some of the current changes are watched and unseen
157 */
158 mw.rcfilters.dm.ChangesListViewModel.prototype.hasUnseenWatchedChanges = function () {
159 return this.unseenWatchedChanges;
160 };
161 }( mediaWiki ) );