Merge "Skin: Make skins aware of their registered skin 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 this.extractNextFrom( $fieldset );
83 this.checkForUnseenWatchedChanges( changesListContent );
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 var data = $fieldset.find( '.rclistfrom > a, .wlinfo' ).data( 'params' );
114 this.nextFrom = data ? data.from : null;
115 };
116
117 /**
118 * @return {string} The 'from' parameter that can be used to query new changes
119 */
120 mw.rcfilters.dm.ChangesListViewModel.prototype.getNextFrom = function () {
121 return this.nextFrom;
122 };
123
124 /**
125 * Toggle the 'live update' feature on/off
126 *
127 * @param {boolean} enable
128 */
129 mw.rcfilters.dm.ChangesListViewModel.prototype.toggleLiveUpdate = function ( enable ) {
130 enable = enable === undefined ? !this.liveUpdate : enable;
131 if ( enable !== this.liveUpdate ) {
132 this.liveUpdate = enable;
133 this.emit( 'liveUpdateChange', this.liveUpdate );
134 }
135 };
136
137 /**
138 * @return {boolean} The 'live update' feature is enabled
139 */
140 mw.rcfilters.dm.ChangesListViewModel.prototype.getLiveUpdate = function () {
141 return this.liveUpdate;
142 };
143
144 /**
145 * Check if some of the given changes watched and unseen
146 *
147 * @param {jQuery|string} changeslistContent
148 */
149 mw.rcfilters.dm.ChangesListViewModel.prototype.checkForUnseenWatchedChanges = function ( changeslistContent ) {
150 this.unseenWatchedChanges = changeslistContent !== 'NO_RESULTS' &&
151 changeslistContent.find( '.mw-changeslist-line-watched' ).length > 0;
152 };
153
154 /**
155 * @return {boolean} Whether some of the current changes are watched and unseen
156 */
157 mw.rcfilters.dm.ChangesListViewModel.prototype.hasUnseenWatchedChanges = function () {
158 return this.unseenWatchedChanges;
159 };
160 }( mediaWiki ) );