Merge branch 'hotfix/21.56.9' into master
[gitter.git] / public / js / views / chat / chat-input-buttons.js
blob1ddc8ad99b3fecece0143d6b9498fc939da3e580
1 'use strict';
3 var Marionette = require('backbone.marionette');
4 var template = require('./tmpl/chat-input-buttons.hbs');
5 var platformKeys = require('../../utils/platform-keys');
6 var cocktail = require('backbone.cocktail');
7 var KeyboardEventsMixin = require('../keyboard-events-mixin');
8 var isMobile = require('../../utils/is-mobile');
9 require('../behaviors/tooltip');
11 var ChatInputButtons = Marionette.ItemView.extend({
12   template: template,
14   behaviors: {
15     Tooltip: {
16       '.js-toggle-compose-mode': {
17         titleFn: 'getComposeModeTitle',
18         placement: 'left',
19         customUpdateEvent: 'updateComposeTooltipText'
20       },
21       '.js-markdown-help': { titleFn: 'getShowMarkdownTitle', placement: 'left' }
22     }
23   },
25   ui: {
26     composeToggle: '.js-toggle-compose-mode'
27   },
29   events: {
30     'click .js-toggle-compose-mode': 'toggleComposeMode'
31   },
33   modelEvents: {
34     'change:isComposeModeEnabled': 'composeModeChanged'
35   },
37   keyboardEvents: {
38     'chat.toggle': 'toggleComposeMode'
39   },
41   getComposeModeTitle: function() {
42     var mode = this.model.get('isComposeModeEnabled') ? 'chat' : 'compose';
43     return 'Switch to ' + mode + ' mode (' + platformKeys.cmd + ' + /)';
44   },
46   getShowMarkdownTitle: function() {
47     return 'Markdown help (' + platformKeys.cmd + ' + ' + platformKeys.gitter + ' + m)';
48   },
50   toggleComposeMode: function() {
51     // compose mode is always off for mobile
52     if (isMobile()) return;
54     this.model.set('isComposeModeEnabled', !this.model.get('isComposeModeEnabled'));
55   },
57   composeModeChanged: function() {
58     this.ui.composeToggle[0].setAttribute(
59       'data-compose-mode',
60       this.model.get('isComposeModeEnabled')
61     );
62     this.ui.composeToggle[0].setAttribute('aria-label', this.getComposeModeTitle());
63     this.trigger('updateComposeTooltipText');
64   },
66   serializeData: function() {
67     var data = this.model.toJSON();
69     data.composeModeMessage = this.getComposeModeTitle();
70     data.markdownHelpMessage = this.getShowMarkdownTitle();
72     return data;
73   }
74 });
76 cocktail.mixin(ChatInputButtons, KeyboardEventsMixin);
78 module.exports = ChatInputButtons;