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-defaults.hbs');
8 var FeaturesView = require('./notification-features-collection-view');
10 var View = Marionette.LayoutView.extend({
13 'click #close-settings': 'destroySettings',
14 'change @ui.options': 'formChange',
15 'change @ui.override': 'formChange',
16 'change @ui.sendEmailsCheckbox': 'formChange'
22 options: '#notification-options',
23 override: '#override-all',
24 notifyFeatures: '#notify-features',
25 noticeNoOverride: '#notice-no-override',
26 sendEmailsCheckbox: '#send-emails-checkbox'
29 notifyFeatures: '#notify-features'
32 initialize: function() {
33 // TODO: this should go to the userRoom endpoint as a get
34 // or better yet should be a live field on the room
36 .get('/settings/defaultRoomMode,unread_notifications_optout')
38 .then(function(response) {
39 var defaultRoomMode = response.defaultRoomMode || {};
40 // Use a single, flat model for the view
41 defaultRoomMode.unread_notifications_optout = !!response.unread_notifications_optout;
42 this.model.set(defaultRoomMode);
45 this.listenTo(this, 'menuItemClicked', this.menuItemClicked);
49 var selectInput = this.ui.options;
50 selectInput.val(this.model.get('mode'));
53 if (this.featuresView) {
54 count = this.featuresView.resetFromHash(this.model.attributes);
60 this.ui.notifyFeatures.show();
62 this.ui.notifyFeatures.hide();
65 var sendEmails = !this.model.get('unread_notifications_optout');
66 this.ui.sendEmailsCheckbox.prop('checked', sendEmails);
69 onRender: function() {
70 this.featuresView = new FeaturesView({});
71 this.getRegion('notifyFeatures').show(this.featuresView);
75 formChange: function(e) {
76 if (e) e.preventDefault();
77 var mode = this.ui.options.val();
78 var override = !!this.ui.override.is(':checked');
79 var emailOptOut = !this.ui.sendEmailsCheckbox.is(':checked');
81 this.featuresView.resetFromMode(mode);
84 mode === this.model.get('mode') &&
86 emailOptOut === this.model.get('unread_notifications_optout');
88 this.dialog.toggleButtonClass('apply', 'modal--default__footer__btn--neutral', noChange);
89 this.dialog.toggleButtonClass('apply', 'modal--default__footer__btn', !noChange);
92 this.ui.noticeNoOverride.hide('fast');
94 this.ui.noticeNoOverride.show('fast');
98 destroySettings: function() {
103 menuItemClicked: function(button) {
105 case 'room-settings':
106 window.location.href = '#notifications';
109 this.applyChangeAndClose();
114 applyChangeAndClose: function() {
115 var mode = this.ui.options.val();
116 var override = !!this.ui.override.is(':checked');
117 var emailOptOut = !this.ui.sendEmailsCheckbox.is(':checked');
120 .post('/settings/', {
125 unread_notifications_optout: emailOptOut
135 module.exports = ModalView.extend({
136 initialize: function(options) {
139 title: 'Default Notification Settings',
142 action: 'room-settings',
144 text: 'Room Settings',
145 className: 'modal--default__footer__link'
151 className: 'modal--default__footer__btn--neutral'
158 ModalView.prototype.initialize.call(this, options);
159 this.view = new View(options);