Enable RCFilters app on Watchlist
[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 };
18
19 /* Initialization */
20 OO.initClass( mw.rcfilters.dm.ChangesListViewModel );
21 OO.mixinClass( mw.rcfilters.dm.ChangesListViewModel, OO.EventEmitter );
22
23 /* Events */
24
25 /**
26 * @event invalidate
27 *
28 * The list of changes is now invalid (out of date)
29 */
30
31 /**
32 * @event update
33 * @param {jQuery|string} $changesListContent List of changes
34 * @param {jQuery} $fieldset Server-generated form
35 * @param {boolean} isInitialDOM Whether the previous dom variables are from the initial page load
36 * @param {boolean} fromLiveUpdate These are new changes fetched via Live Update
37 *
38 * The list of changes has been updated
39 */
40
41 /**
42 * @event newChangesExist
43 * @param {boolean} newChangesExist
44 *
45 * The existence of changes newer than those currently displayed has changed.
46 */
47
48 /**
49 * @event liveUpdateChange
50 * @param {boolean} enable
51 *
52 * The state of the 'live update' feature has changed.
53 */
54
55 /* Methods */
56
57 /**
58 * Invalidate the list of changes
59 *
60 * @fires invalidate
61 */
62 mw.rcfilters.dm.ChangesListViewModel.prototype.invalidate = function () {
63 if ( this.valid ) {
64 this.valid = false;
65 this.emit( 'invalidate' );
66 }
67 };
68
69 /**
70 * Update the model with an updated list of changes
71 *
72 * @param {jQuery|string} changesListContent
73 * @param {jQuery} $fieldset
74 * @param {boolean} [isInitialDOM] Using the initial (already attached) DOM elements
75 * @param {boolean} [separateOldAndNew] Whether a logical separation between old and new changes is needed
76 * @fires update
77 */
78 mw.rcfilters.dm.ChangesListViewModel.prototype.update = function ( changesListContent, $fieldset, isInitialDOM, separateOldAndNew ) {
79 var from = this.nextFrom;
80 this.valid = true;
81 if ( mw.rcfilters.featureFlags.liveUpdate ) {
82 this.extractNextFrom( $fieldset );
83 }
84 this.emit( 'update', changesListContent, $fieldset, isInitialDOM, separateOldAndNew ? from : null );
85 };
86
87 /**
88 * Specify whether new changes exist
89 *
90 * @param {boolean} newChangesExist
91 * @fires newChangesExist
92 */
93 mw.rcfilters.dm.ChangesListViewModel.prototype.setNewChangesExist = function ( newChangesExist ) {
94 if ( newChangesExist !== this.newChangesExist ) {
95 this.newChangesExist = newChangesExist;
96 this.emit( 'newChangesExist', newChangesExist );
97 }
98 };
99
100 /**
101 * @return {boolean} Whether new changes exist
102 */
103 mw.rcfilters.dm.ChangesListViewModel.prototype.getNewChangesExist = function () {
104 return this.newChangesExist;
105 };
106
107 /**
108 * Extract the value of the 'from' parameter from a link in the field set
109 *
110 * @param {jQuery} $fieldset
111 */
112 mw.rcfilters.dm.ChangesListViewModel.prototype.extractNextFrom = function ( $fieldset ) {
113 this.nextFrom = $fieldset.find( '.rclistfrom > a' ).data( 'params' ).from;
114 };
115
116 /**
117 * @return {string} The 'from' parameter that can be used to query new changes
118 */
119 mw.rcfilters.dm.ChangesListViewModel.prototype.getNextFrom = function () {
120 return this.nextFrom;
121 };
122
123 /**
124 * Toggle the 'live update' feature on/off
125 *
126 * @param {boolean} enable
127 */
128 mw.rcfilters.dm.ChangesListViewModel.prototype.toggleLiveUpdate = function ( enable ) {
129 enable = enable === undefined ? !this.liveUpdate : enable;
130 if ( enable !== this.liveUpdate ) {
131 this.liveUpdate = enable;
132 this.emit( 'liveUpdateChange', this.liveUpdate );
133 }
134 };
135
136 /**
137 * @return {boolean} The 'live update' feature is enabled
138 */
139 mw.rcfilters.dm.ChangesListViewModel.prototype.getLiveUpdate = function () {
140 return this.liveUpdate;
141 };
142
143 }( mediaWiki ) );