3 var context = require('gitter-web-client-context');
4 var Marionette = require('backbone.marionette');
5 var ModalView = require('./modal');
6 var apiClient = require('../../components/api-client');
7 var roomSettingsTemplate = require('./tmpl/room-settings-view.hbs');
8 var Promise = require('bluebird');
9 var toggleClass = require('../../utils/toggle-class');
11 var View = Marionette.ItemView.extend({
12 template: roomSettingsTemplate,
15 githubOnly: '#github-only',
16 welcomeMessage: '#room-welcome-message',
17 welcomeMessagePreviewButton: '#preview-welcome-message',
18 welcomeMessagePreviewContainer: '#welcome-message-preview-container',
19 editWelcomeMessageButton: '#close-welcome-message-preview',
20 errorMessage: '#error-message'
24 'click #close-settings': 'destroySettings',
25 'click @ui.welcomeMessagePreviewButton': 'previewWelcomeMessage',
26 'click @ui.editWelcomeMessageButton': 'editWelcomeMessage'
29 initialize: function() {
30 this.listenTo(this, 'menuItemClicked', this.menuItemClicked, this);
33 .then(({ welcomeMessage = { text: '', html: '' } }) => {
34 if (welcomeMessage.text.length) {
35 this.initWithMessage(welcomeMessage);
37 this.initEmptyTextArea();
40 .catch(this.showError.bind(this));
43 destroySettings: function() {
48 menuItemClicked: function(action) {
57 var hasGithub = (this.model.get('providers') || []).indexOf('github') !== -1;
59 this.ui.githubOnly.attr('checked', true);
61 this.ui.githubOnly.removeAttr('checked');
65 initEmptyTextArea: function() {
66 this.ui.welcomeMessage.attr('placeholder', 'Add a new welcome message here');
69 initWithMessage: function(msg) {
70 this.ui.welcomeMessage.val(msg.text);
73 onRender: function() {
77 previewWelcomeMessage: function(e) {
79 this.setPreviewLoadingState();
80 toggleClass(this.el, 'preview', true);
82 this.ui.welcomeMessage[0].classList.add('hidden');
83 this.fetchRenderedHTML().then(
85 this.ui.welcomeMessagePreviewContainer.html(html);
86 toggleClass(this.ui.welcomeMessagePreviewContainer[0], 'loading', false);
91 editWelcomeMessage: function(e) {
93 this.setPreviewLoadingState();
94 toggleClass(this.el, 'preview', false);
96 this.ui.welcomeMessage[0].classList.remove('hidden');
99 setPreviewLoadingState: function() {
100 this.ui.welcomeMessagePreviewContainer.html('Loading ...');
101 toggleClass(this.ui.welcomeMessagePreviewContainer[0], 'loading', true);
104 fetchRenderedHTML: function() {
107 '/private/markdown-preview',
108 { text: this.getWelcomeMessageContent() },
111 .catch(this.showError.bind(this));
114 getWelcomeMessageContent: function() {
115 return this.ui.welcomeMessage.val();
118 formSubmit: function() {
119 var providers = this.ui.githubOnly.is(':checked') ? ['github'] : [];
120 var welcomeMessageContent = this.getWelcomeMessageContent();
123 apiClient.room.put('', { providers: providers }),
124 apiClient.room.post('/meta', {
125 welcomeMessage: welcomeMessageContent
129 function(updatedTroupe /*, metaResponse*/) {
130 context.setTroupe(updatedTroupe);
131 this.destroySettings();
134 .catch(this.showError.bind(this));
137 // FIXME: Don't swallow an error
138 // eslint-disable-next-line no-unused-vars
139 showError: function(err) {
140 this.ui.errorMessage[0].classList.remove('hidden');
141 this.ui.welcomeMessage.attr('disabled', true);
145 var Modal = ModalView.extend({
146 initialize: function(options) {
147 options = options || {};
148 options.title = options.title || 'Room Settings';
149 ModalView.prototype.initialize.apply(this, arguments);
150 this.view = new View(options);
153 { action: 'submit', pull: 'right', text: 'Submit', className: 'modal--default__footer__btn' }
157 module.exports = Modal;