| 1 | ( function () { |
| 2 | /* global moment:false */ |
| 3 | /** |
| 4 | * Notification item data structure. |
| 5 | * |
| 6 | * @class |
| 7 | * @mixes OO.EventEmitter |
| 8 | * @mixes OO.SortedEmitterList |
| 9 | * |
| 10 | * @constructor |
| 11 | * @param {number} id Notification id, |
| 12 | * @param {Object} [config] Configuration object |
| 13 | * @param {string} [config.iconUrl] A URL for the given icon. |
| 14 | * @param {string} [config.iconType] A string noting the icon type. |
| 15 | * @param {Object} [config.content] The message object defining the text for the header and, |
| 16 | * optionally, the body of the notification. |
| 17 | * @param {string} [config.content.header=''] The header text of the notification |
| 18 | * @param {string} [config.content.body=''] The body text of the notification |
| 19 | * @param {string} [config.category] The category of this notification. The category identifies |
| 20 | * where the notification originates from. |
| 21 | * @param {string} [config.type='message'] The notification type 'message' or 'alert' |
| 22 | * @param {boolean} [config.read=false] State the read state of the option |
| 23 | * @param {boolean} [config.seen=false] State the seen state of the option |
| 24 | * @param {string} [config.timestamp] Notification timestamp in ISO 8601 format |
| 25 | * @param {string} [config.primaryUrl] Notification primary link in raw url format |
| 26 | * @param {boolean} [config.foreign=false] This notification is from a foreign source |
| 27 | * @param {boolean} [config.bundled=false] This notification is part of a bundle |
| 28 | * @param {number[]} [config.bundledIds] IDs of notifications bundled with this one |
| 29 | * @param {string} [config.modelName='local'] The name of the model this item belongs to |
| 30 | * @param {string} [config.source] The source this notification is coming from, if it is foreign |
| 31 | * @param {Object[]} [config.secondaryUrls] An array of objects defining the secondary URLs |
| 32 | * for this notification. The secondary URLs are expected to have this structure: |
| 33 | * ``` |
| 34 | * { |
| 35 | * "iconType": "userAvatar", // A symbolic name for the icon. |
| 36 | * // Will render as oo-ui-icon-* class. |
| 37 | * "label": "", // The label for the link |
| 38 | * "prioritized": true/false, // Prioritized links are outside of the popup |
| 39 | * // menu, whenever possible. |
| 40 | * "url": "..." // The url for the secondary link |
| 41 | * } |
| 42 | * ``` |
| 43 | */ |
| 44 | mw.echo.dm.NotificationItem = function MwEchoDmNotificationItem( id, config ) { |
| 45 | config = config || {}; |
| 46 | |
| 47 | // Mixin constructor |
| 48 | OO.EventEmitter.call( this ); |
| 49 | |
| 50 | // Properties |
| 51 | this.id = id; |
| 52 | this.modelName = config.modelName || 'local'; |
| 53 | this.content = Object.assign( { header: '', body: '' }, config.content ); |
| 54 | this.category = config.category || ''; |
| 55 | this.type = config.type || 'message'; |
| 56 | this.foreign = !!config.foreign; |
| 57 | this.bundled = !!config.bundled; |
| 58 | this.source = config.source || ''; |
| 59 | this.iconType = config.iconType; |
| 60 | this.iconUrl = config.iconUrl; |
| 61 | |
| 62 | this.read = !!config.read; |
| 63 | this.seen = !!config.seen; |
| 64 | |
| 65 | this.timestamp = config.timestamp || moment.utc().format( 'YYYY-MM-DD[T]HH:mm:ss[Z]' ); |
| 66 | this.setPrimaryUrl( config.primaryUrl ); |
| 67 | this.setSecondaryUrls( config.secondaryUrls ); |
| 68 | this.bundledIds = config.bundledIds; |
| 69 | }; |
| 70 | |
| 71 | /* Initialization */ |
| 72 | |
| 73 | OO.initClass( mw.echo.dm.NotificationItem ); |
| 74 | OO.mixinClass( mw.echo.dm.NotificationItem, OO.EventEmitter ); |
| 75 | |
| 76 | /* Events */ |
| 77 | |
| 78 | /** |
| 79 | * Item details have changed or were updated |
| 80 | * |
| 81 | * @event mw.echo.dm.NotificationItem#update |
| 82 | */ |
| 83 | |
| 84 | /* Methods */ |
| 85 | |
| 86 | /** |
| 87 | * Get NotificationItem id |
| 88 | * |
| 89 | * @return {string} NotificationItem Id |
| 90 | */ |
| 91 | mw.echo.dm.NotificationItem.prototype.getId = function () { |
| 92 | return this.id; |
| 93 | }; |
| 94 | |
| 95 | /** |
| 96 | * Get NotificationItem content header |
| 97 | * |
| 98 | * @return {string} NotificationItem content |
| 99 | */ |
| 100 | mw.echo.dm.NotificationItem.prototype.getContentHeader = function () { |
| 101 | return this.content.header; |
| 102 | }; |
| 103 | |
| 104 | /** |
| 105 | * Get NotificationItem content body |
| 106 | * |
| 107 | * @return {string} NotificationItem content body |
| 108 | */ |
| 109 | mw.echo.dm.NotificationItem.prototype.getContentBody = function () { |
| 110 | return this.content.body; |
| 111 | }; |
| 112 | |
| 113 | /** |
| 114 | * Get NotificationItem category |
| 115 | * |
| 116 | * @return {string} NotificationItem category |
| 117 | */ |
| 118 | mw.echo.dm.NotificationItem.prototype.getCategory = function () { |
| 119 | return this.category; |
| 120 | }; |
| 121 | |
| 122 | /** |
| 123 | * Get NotificationItem type |
| 124 | * |
| 125 | * @return {string} NotificationItem type |
| 126 | */ |
| 127 | mw.echo.dm.NotificationItem.prototype.getType = function () { |
| 128 | return this.type; |
| 129 | }; |
| 130 | |
| 131 | /** |
| 132 | * Check whether this notification item is read |
| 133 | * |
| 134 | * @return {boolean} Notification item is read |
| 135 | */ |
| 136 | mw.echo.dm.NotificationItem.prototype.isRead = function () { |
| 137 | return this.read; |
| 138 | }; |
| 139 | |
| 140 | /** |
| 141 | * Check whether this notification item is seen |
| 142 | * |
| 143 | * @return {boolean} Notification item is seen |
| 144 | */ |
| 145 | mw.echo.dm.NotificationItem.prototype.isSeen = function () { |
| 146 | return this.seen; |
| 147 | }; |
| 148 | |
| 149 | /** |
| 150 | * Check whether this notification item is foreign |
| 151 | * |
| 152 | * @return {boolean} Notification item is foreign |
| 153 | */ |
| 154 | mw.echo.dm.NotificationItem.prototype.isForeign = function () { |
| 155 | return this.foreign; |
| 156 | }; |
| 157 | |
| 158 | /** |
| 159 | * Check whether this notification item is part of a bundle |
| 160 | * |
| 161 | * @return {boolean} Notification item is part of a bundle |
| 162 | */ |
| 163 | mw.echo.dm.NotificationItem.prototype.isBundled = function () { |
| 164 | return this.bundled; |
| 165 | }; |
| 166 | |
| 167 | /** |
| 168 | * Set this notification item as foreign |
| 169 | * |
| 170 | * @param {boolean} isForeign Notification item is foreign |
| 171 | */ |
| 172 | mw.echo.dm.NotificationItem.prototype.setForeign = function ( isForeign ) { |
| 173 | this.foreign = isForeign; |
| 174 | }; |
| 175 | |
| 176 | /** |
| 177 | * Toggle the read state of the widget |
| 178 | * |
| 179 | * @param {boolean} [read] The current read state. If not given, the state will |
| 180 | * become the opposite of its current state. |
| 181 | * @fires mw.echo.dm.NotificationItem#update |
| 182 | * @fires OO.EventEmitter#sortChange |
| 183 | */ |
| 184 | mw.echo.dm.NotificationItem.prototype.toggleRead = function ( read ) { |
| 185 | read = read !== undefined ? read : !this.read; |
| 186 | if ( this.read !== read ) { |
| 187 | this.read = read; |
| 188 | this.emit( 'update' ); |
| 189 | this.emit( 'sortChange' ); |
| 190 | } |
| 191 | }; |
| 192 | |
| 193 | /** |
| 194 | * Toggle the seen state of the widget |
| 195 | * |
| 196 | * @param {boolean} [seen] The current seen state. If not given, the state will |
| 197 | * become the opposite of its current state. |
| 198 | * @fires mw.echo.dm.NotificationItem#update |
| 199 | */ |
| 200 | mw.echo.dm.NotificationItem.prototype.toggleSeen = function ( seen ) { |
| 201 | seen = seen !== undefined ? seen : !this.seen; |
| 202 | if ( |
| 203 | this.seen !== seen && |
| 204 | // Do not change the state of a read item, since its |
| 205 | // seen state (never 'unseen') never changes |
| 206 | !this.isRead() |
| 207 | ) { |
| 208 | this.seen = seen; |
| 209 | this.emit( 'update' ); |
| 210 | } |
| 211 | }; |
| 212 | |
| 213 | /** |
| 214 | * Get the notification timestamp |
| 215 | * |
| 216 | * @return {number} Notification timestamp in MediaWiki timestamp format |
| 217 | */ |
| 218 | mw.echo.dm.NotificationItem.prototype.getTimestamp = function () { |
| 219 | return this.timestamp; |
| 220 | }; |
| 221 | |
| 222 | /** |
| 223 | * Set the notification link |
| 224 | * |
| 225 | * @param {string} link Notification url |
| 226 | */ |
| 227 | mw.echo.dm.NotificationItem.prototype.setPrimaryUrl = function ( link ) { |
| 228 | this.primaryUrl = link; |
| 229 | }; |
| 230 | |
| 231 | /** |
| 232 | * Get the notification link |
| 233 | * |
| 234 | * @return {string} Notification url |
| 235 | */ |
| 236 | mw.echo.dm.NotificationItem.prototype.getPrimaryUrl = function () { |
| 237 | return this.primaryUrl; |
| 238 | }; |
| 239 | |
| 240 | /** |
| 241 | * Get the notification icon URL |
| 242 | * |
| 243 | * @return {string} Notification icon URL |
| 244 | */ |
| 245 | mw.echo.dm.NotificationItem.prototype.getIconUrl = function () { |
| 246 | return this.iconUrl; |
| 247 | }; |
| 248 | |
| 249 | /** |
| 250 | * Get the notification icon type |
| 251 | * |
| 252 | * @return {string} Notification icon type |
| 253 | */ |
| 254 | mw.echo.dm.NotificationItem.prototype.getIconType = function () { |
| 255 | return this.iconType; |
| 256 | }; |
| 257 | |
| 258 | /** |
| 259 | * Set the notification's secondary links |
| 260 | * See constructor documentation for the structure of these links objects. |
| 261 | * |
| 262 | * @param {Object[]} links Secondary url definitions |
| 263 | */ |
| 264 | mw.echo.dm.NotificationItem.prototype.setSecondaryUrls = function ( links ) { |
| 265 | this.secondaryUrls = links || []; |
| 266 | }; |
| 267 | |
| 268 | /** |
| 269 | * Get the notification's secondary links |
| 270 | * |
| 271 | * @return {Object[]} Secondary url definitions |
| 272 | */ |
| 273 | mw.echo.dm.NotificationItem.prototype.getSecondaryUrls = function () { |
| 274 | return this.secondaryUrls; |
| 275 | }; |
| 276 | |
| 277 | /** |
| 278 | * Get the notification's source |
| 279 | * |
| 280 | * @return {string} Notification source |
| 281 | */ |
| 282 | mw.echo.dm.NotificationItem.prototype.getSource = function () { |
| 283 | return this.source; |
| 284 | }; |
| 285 | |
| 286 | /** |
| 287 | * Get the notification's model name |
| 288 | * |
| 289 | * @return {string} Notification model name |
| 290 | */ |
| 291 | mw.echo.dm.NotificationItem.prototype.getModelName = function () { |
| 292 | return this.modelName; |
| 293 | }; |
| 294 | |
| 295 | /** |
| 296 | * Get the all ids contained in this notification |
| 297 | * |
| 298 | * @return {number[]} |
| 299 | */ |
| 300 | mw.echo.dm.NotificationItem.prototype.getAllIds = function () { |
| 301 | return [ this.getId() ].concat( this.bundledIds || [] ); |
| 302 | }; |
| 303 | |
| 304 | }() ); |