3 var Marionette
= require('backbone.marionette');
4 var _
= require('lodash');
5 var apiClient
= require('../../components/api-client');
6 var ModalView
= require('./modal');
7 var template
= require('./tmpl/notification-settings-view.hbs');
8 var userNotifications
= require('../../components/user-notifications');
9 var FeaturesView
= require('./notification-features-collection-view');
12 all
: 'All: Notify me for all messages',
13 announcement
: 'Announcements: Notify me for mentions/announcements (email for all messages)',
14 mute
: "Mute: Notify me only when I'm directly mentioned"
17 var View
= Marionette
.LayoutView
.extend({
20 'click #close-settings': 'destroySettings',
21 'change #notification-options': 'formChange'
27 options
: '#notification-options',
28 nonstandard
: '#nonstandard',
29 notifyFeatures
: '#notify-features'
32 notifyFeatures
: '#notify-features'
35 initialize: function() {
36 // TODO: this should go to the userRoom endpoint as a get
37 // or better yet should be a live field on the room
39 .get('/settings/notifications')
41 .then(function(settings
) {
42 this.model
.set(settings
);
45 this.listenTo(this, 'menuItemClicked', this.menuItemClicked
);
48 getNotificationOption: function() {
49 var model
= this.model
;
51 if (!model
.attributes
.hasOwnProperty('mode')) {
55 var value
= model
.get('mode');
56 var lurk
= model
.get('lurk');
60 var defaultDescription
=
64 (lurk
? 'unread item count off' : 'unread item count on');
68 nonStandard
= lurk
=== true;
71 nonStandard
: nonStandard
,
72 description
: nonStandard
? defaultDescription
: null
77 nonStandard
= lurk
=== true;
80 selectValue
: 'announcement',
81 nonStandard
: nonStandard
,
82 description
: nonStandard
? defaultDescription
: null
86 nonStandard
= lurk
=== false;
90 nonStandard
: nonStandard
,
91 description
: nonStandard
? defaultDescription
: null
98 description
: 'Custom legacy setting (details below)'
104 var val
= this.getNotificationOption();
107 if (val
.nonStandard
) {
108 this.ui
.nonstandard
.show();
110 this.ui
.nonstandard
.hide();
113 var isDefault
= this.model
.get('default');
116 this.setOption('default');
118 if (val
.nonStandard
) {
119 this.setOption('', val
.description
);
121 this.setOption(val
.selectValue
);
125 this.setOption('', 'Please wait...');
126 this.ui
.nonstandard
.hide();
130 if (this.featuresView
) {
131 count
= this.featuresView
.resetFromHash(this.model
.attributes
);
137 this.ui
.notifyFeatures
.show();
139 this.ui
.notifyFeatures
.hide();
143 setOption: function(val
, text
) {
144 var selectInput
= this.ui
.options
;
148 var defaultOptgroup
= document
.createElement('optgroup');
149 defaultOptgroup
.label
= 'Use Default';
150 selectInput
.append(defaultOptgroup
);
152 var defaultOption
= document
.createElement('option');
153 defaultOption
.value
= 'default';
154 defaultOption
.textContent
= this.getDefaulDescription();
155 if (val
=== 'default') {
156 defaultOption
.selected
= true;
159 defaultOptgroup
.appendChild(defaultOption
);
161 var overrideOptGroup
= document
.createElement('optgroup');
162 overrideOptGroup
.label
= 'Override';
163 selectInput
.append(overrideOptGroup
);
165 Object
.keys(OPTIONS
).forEach(function(key
) {
166 var text
= OPTIONS
[key
];
168 var option
= document
.createElement('option');
170 option
.textContent
= text
;
171 var selected
= key
=== val
;
172 option
.selected
= selected
;
176 overrideOptGroup
.appendChild(option
);
180 var option
= document
.createElement('option');
182 option
.textContent
= text
;
183 option
.selected
= true;
184 option
.style
.display
= 'none';
185 overrideOptGroup
.appendChild(option
);
189 getDefaulDescription: function() {
190 var model
= this.model
;
191 var defaultSettings
= model
.get('defaultSettings');
192 if (!defaultSettings
) return 'Default settings';
194 if (!defaultSettings
.mode
|| !OPTIONS
[defaultSettings
.mode
]) return 'Legacy settings';
195 return OPTIONS
[defaultSettings
.mode
];
198 onRender: function() {
199 this.featuresView
= new FeaturesView({});
200 this.getRegion('notifyFeatures').show(this.featuresView
);
204 formChange: function(e
) {
205 if (e
) e
.preventDefault();
206 var mode
= this.ui
.options
.val();
207 var oldIsDefault
= this.model
.get('default');
209 var effectiveMode
= mode
;
210 if (mode
=== 'default') {
211 var defaultSettings
= this.model
.get('defaultSettings');
212 effectiveMode
= defaultSettings
&& defaultSettings
.mode
;
214 this.featuresView
.resetFromMode(effectiveMode
);
218 noChange
= mode
=== 'default';
220 noChange
= mode
=== this.model
.get('mode');
223 this.dialog
.toggleButtonClass('apply', 'modal--default__footer__btn--neutral', noChange
);
224 this.dialog
.toggleButtonClass('apply', 'modal--default__footer__btn', !noChange
);
227 destroySettings: function() {
232 serializeData: function() {
234 notificationsBlocked
: userNotifications
.isAccessDenied()
238 menuItemClicked: function(button
) {
241 window
.location
.href
= '#notification-defaults';
244 this.applyChangeAndClose();
249 applyChangeAndClose: function() {
250 var mode
= this.ui
.options
.val();
251 // TODO: this should go to the userRoom endpoint as a patch
253 .put('/settings/notifications', { mode
: mode
})
262 module
.exports
= ModalView
.extend({
263 initialize: function(options
) {
266 title
: 'Notification Settings',
269 action
: 'set-defaults',
271 text
: 'Configure Defaults',
272 className
: 'modal--default__footer__link'
278 className
: 'modal--default__footer__btn--neutral'
285 ModalView
.prototype.initialize
.call(this, options
);
286 this.view
= new View(options
);