Prepare for REL1_33 cut, labelling master as 1.34-alpha
[lhc/web/wiklou.git] / resources / src / mediawiki.rcfilters / dm / ChangesListViewModel.js
1 ( function () {
2 /**
3 * View model for the changes list
4 *
5 * @class mw.rcfilters.dm.ChangesListViewModel
6 * @mixins OO.EventEmitter
7 *
8 * @param {jQuery} $initialFieldset The initial server-generated legacy form content
9 * @constructor
10 */
11 var ChangesListViewModel = function MwRcfiltersDmChangesListViewModel( $initialFieldset ) {
12 // Mixin constructor
13 OO.EventEmitter.call( this );
14
15 this.valid = true;
16 this.newChangesExist = false;
17 this.liveUpdate = false;
18 this.unseenWatchedChanges = false;
19
20 this.extractNextFrom( $initialFieldset );
21 };
22
23 /* Initialization */
24 OO.initClass( ChangesListViewModel );
25 OO.mixinClass( ChangesListViewModel, OO.EventEmitter );
26
27 /* Events */
28
29 /**
30 * @event invalidate
31 *
32 * The list of changes is now invalid (out of date)
33 */
34
35 /**
36 * @event update
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
42 *
43 * The list of changes has been updated
44 */
45
46 /**
47 * @event newChangesExist
48 * @param {boolean} newChangesExist
49 *
50 * The existence of changes newer than those currently displayed has changed.
51 */
52
53 /**
54 * @event liveUpdateChange
55 * @param {boolean} enable
56 *
57 * The state of the 'live update' feature has changed.
58 */
59
60 /* Methods */
61
62 /**
63 * Invalidate the list of changes
64 *
65 * @fires invalidate
66 */
67 ChangesListViewModel.prototype.invalidate = function () {
68 if ( this.valid ) {
69 this.valid = false;
70 this.emit( 'invalidate' );
71 }
72 };
73
74 /**
75 * Update the model with an updated list of changes
76 *
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
82 * @fires update
83 */
84 ChangesListViewModel.prototype.update = function ( changesListContent, $fieldset, noResultsDetails, isInitialDOM, separateOldAndNew ) {
85 var from = this.nextFrom;
86 this.valid = true;
87 this.extractNextFrom( $fieldset );
88 this.checkForUnseenWatchedChanges( changesListContent );
89 this.emit( 'update', changesListContent, $fieldset, noResultsDetails, isInitialDOM, separateOldAndNew ? from : null );
90 };
91
92 /**
93 * Specify whether new changes exist
94 *
95 * @param {boolean} newChangesExist
96 * @fires newChangesExist
97 */
98 ChangesListViewModel.prototype.setNewChangesExist = function ( newChangesExist ) {
99 if ( newChangesExist !== this.newChangesExist ) {
100 this.newChangesExist = newChangesExist;
101 this.emit( 'newChangesExist', newChangesExist );
102 }
103 };
104
105 /**
106 * @return {boolean} Whether new changes exist
107 */
108 ChangesListViewModel.prototype.getNewChangesExist = function () {
109 return this.newChangesExist;
110 };
111
112 /**
113 * Extract the value of the 'from' parameter from a link in the field set
114 *
115 * @param {jQuery} $fieldset
116 */
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;
121 }
122 };
123
124 /**
125 * @return {string} The 'from' parameter that can be used to query new changes
126 */
127 ChangesListViewModel.prototype.getNextFrom = function () {
128 return this.nextFrom;
129 };
130
131 /**
132 * Toggle the 'live update' feature on/off
133 *
134 * @param {boolean} enable
135 */
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 );
141 }
142 };
143
144 /**
145 * @return {boolean} The 'live update' feature is enabled
146 */
147 ChangesListViewModel.prototype.getLiveUpdate = function () {
148 return this.liveUpdate;
149 };
150
151 /**
152 * Check if some of the given changes watched and unseen
153 *
154 * @param {jQuery|string} changeslistContent
155 */
156 ChangesListViewModel.prototype.checkForUnseenWatchedChanges = function ( changeslistContent ) {
157 this.unseenWatchedChanges = changeslistContent !== 'NO_RESULTS' &&
158 changeslistContent.find( '.mw-changeslist-line-watched' ).length > 0;
159 };
160
161 /**
162 * @return {boolean} Whether some of the current changes are watched and unseen
163 */
164 ChangesListViewModel.prototype.hasUnseenWatchedChanges = function () {
165 return this.unseenWatchedChanges;
166 };
167
168 module.exports = ChangesListViewModel;
169 }() );