Merge "registration: Only allow one extension to set a specific config setting"
[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} isDatabaseTimeout Whether this is an error state due to a database query
37 * @param {boolean} isInitialDOM Whether the previous dom variables are from the initial page load
38 * @param {boolean} fromLiveUpdate These are new changes fetched via Live Update
39 *
40 * The list of changes has been updated
41 */
42
43 /**
44 * @event newChangesExist
45 * @param {boolean} newChangesExist
46 *
47 * The existence of changes newer than those currently displayed has changed.
48 */
49
50 /**
51 * @event liveUpdateChange
52 * @param {boolean} enable
53 *
54 * The state of the 'live update' feature has changed.
55 */
56
57 /* Methods */
58
59 /**
60 * Invalidate the list of changes
61 *
62 * @fires invalidate
63 */
64 mw.rcfilters.dm.ChangesListViewModel.prototype.invalidate = function () {
65 if ( this.valid ) {
66 this.valid = false;
67 this.emit( 'invalidate' );
68 }
69 };
70
71 /**
72 * Update the model with an updated list of changes
73 *
74 * @param {jQuery|string} changesListContent
75 * @param {jQuery} $fieldset
76 * @param {boolean} isDatabaseTimeout Whether this is an error state due to a database query
77 * timeout.
78 * @param {boolean} [isInitialDOM] Using the initial (already attached) DOM elements
79 * @param {boolean} [separateOldAndNew] Whether a logical separation between old and new changes is needed
80 * @fires update
81 */
82 mw.rcfilters.dm.ChangesListViewModel.prototype.update = function ( changesListContent, $fieldset, isDatabaseTimeout, isInitialDOM, separateOldAndNew ) {
83 var from = this.nextFrom;
84 this.valid = true;
85 this.extractNextFrom( $fieldset );
86 this.checkForUnseenWatchedChanges( changesListContent );
87 this.emit( 'update', changesListContent, $fieldset, isDatabaseTimeout, isInitialDOM, separateOldAndNew ? from : null );
88 };
89
90 /**
91 * Specify whether new changes exist
92 *
93 * @param {boolean} newChangesExist
94 * @fires newChangesExist
95 */
96 mw.rcfilters.dm.ChangesListViewModel.prototype.setNewChangesExist = function ( newChangesExist ) {
97 if ( newChangesExist !== this.newChangesExist ) {
98 this.newChangesExist = newChangesExist;
99 this.emit( 'newChangesExist', newChangesExist );
100 }
101 };
102
103 /**
104 * @return {boolean} Whether new changes exist
105 */
106 mw.rcfilters.dm.ChangesListViewModel.prototype.getNewChangesExist = function () {
107 return this.newChangesExist;
108 };
109
110 /**
111 * Extract the value of the 'from' parameter from a link in the field set
112 *
113 * @param {jQuery} $fieldset
114 */
115 mw.rcfilters.dm.ChangesListViewModel.prototype.extractNextFrom = function ( $fieldset ) {
116 var data = $fieldset.find( '.rclistfrom > a, .wlinfo' ).data( 'params' );
117 this.nextFrom = data ? data.from : null;
118 };
119
120 /**
121 * @return {string} The 'from' parameter that can be used to query new changes
122 */
123 mw.rcfilters.dm.ChangesListViewModel.prototype.getNextFrom = function () {
124 return this.nextFrom;
125 };
126
127 /**
128 * Toggle the 'live update' feature on/off
129 *
130 * @param {boolean} enable
131 */
132 mw.rcfilters.dm.ChangesListViewModel.prototype.toggleLiveUpdate = function ( enable ) {
133 enable = enable === undefined ? !this.liveUpdate : enable;
134 if ( enable !== this.liveUpdate ) {
135 this.liveUpdate = enable;
136 this.emit( 'liveUpdateChange', this.liveUpdate );
137 }
138 };
139
140 /**
141 * @return {boolean} The 'live update' feature is enabled
142 */
143 mw.rcfilters.dm.ChangesListViewModel.prototype.getLiveUpdate = function () {
144 return this.liveUpdate;
145 };
146
147 /**
148 * Check if some of the given changes watched and unseen
149 *
150 * @param {jQuery|string} changeslistContent
151 */
152 mw.rcfilters.dm.ChangesListViewModel.prototype.checkForUnseenWatchedChanges = function ( changeslistContent ) {
153 this.unseenWatchedChanges = changeslistContent !== 'NO_RESULTS' &&
154 changeslistContent.find( '.mw-changeslist-line-watched' ).length > 0;
155 };
156
157 /**
158 * @return {boolean} Whether some of the current changes are watched and unseen
159 */
160 mw.rcfilters.dm.ChangesListViewModel.prototype.hasUnseenWatchedChanges = function () {
161 return this.unseenWatchedChanges;
162 };
163 }( mediaWiki ) );