Skip to content
This repository was archived by the owner on May 16, 2019. It is now read-only.

Commit c13bb9e

Browse files
committed
Merge pull request #1635 from rmisio/1560
having the Server Connect Modal respond to lang changes
2 parents 01f5c45 + 805c5f0 commit c13bb9e

File tree

6 files changed

+97
-46
lines changed

6 files changed

+97
-46
lines changed

js/start.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,6 @@ var Polyglot = require('node-polyglot'),
6262
extendPolyglot,
6363
newPageNavView,
6464
newSocketView,
65-
//serverConnectModal,
6665
onboardingModal,
6766
pageConnectModal,
6867
launchOnboarding,
@@ -504,7 +503,10 @@ pageConnectModal.on('cancel', () => {
504503
}).render().open();
505504

506505
app.connectHeartbeatSocket();
507-
app.serverConnectModal = new ServerConnectModal().render();
506+
app.serverConnectModal = new ServerConnectModal({
507+
userModel: user
508+
}).render();
509+
508510
app.serverConnectModal.on('connected', () => {
509511
if (profileLoaded) {
510512
// If we've already loaded called loadProfile() and then, we connect

js/templates/serverConfigRow.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<div class="table">
44
<div>
55
<div class="vertAlignMid noOverflow" style="width: 157px;">
6-
<%= ob.name %>
6+
<% print(ob.default ? polyglot.t('serverConnectModal.defaultServerName') : ob.name) %>
77
</div>
88
<div class="vertAlignMid noOverflow">
99
<%= ob.server_ip %>

js/views/baseVw.js

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,29 +10,26 @@ module.exports = Backbone.View.extend({
1010
this._removed = false;
1111
},
1212

13-
// If you are creating child views within your view, call this method
14-
// to register them. This will ensure that they will have their remove
15-
// method called if the parent is removed.
13+
// If you are creating child views within your view, call this method
14+
// to register them. This will ensure that they will have their remove
15+
// method called if the parent is removed.
16+
//
17+
// As of now, this doesn't work if called from initialize() of your view.
18+
// If you do need to register children from there, wrap the call in a timeout:
19+
// setTimeout(() => this.registerChild(myChildVw));
1620
registerChild: function(childView) {
1721
if (this._childViews.indexOf(childView) === -1) {
1822
this._childViews.push(childView);
1923
childView._parentView = this;
2024
}
2125
},
2226

23-
// Opposite of registerChild. This method is automatically
24-
// called by remove. For all practical purposes, you probably
25-
// won't need to call this method directly (as long as you
26-
// are remembering to call this base class's remove when you
27-
// are overriding remove in your own views).
27+
// Opposite of registerChild. This method is automatically
28+
// called by remove. For all practical purposes, you probably
29+
// won't need to call this method directly (as long as you
30+
// are remembering to call this base class's remove when you
31+
// are overriding remove in your own views).
2832
unregisterChild: function(childView) {
29-
/*
30-
var name = '';
31-
if (childView.options && childView.options.name) {
32-
name = childView.options.name;
33-
}
34-
*/
35-
3633
var index;
3734

3835
if ((index = this._childViews.indexOf(childView)) !== -1) {
@@ -52,11 +49,11 @@ module.exports = Backbone.View.extend({
5249
},
5350

5451

55-
// Will call the remove method of any child views.
52+
// Will call the remove method of any child views.
5653
remove: function() {
5754
for (var i=0; i < this._childViews.length; i++) {
58-
// no need to unregister child from parent,
59-
// since the parent is also being removed
55+
// no need to unregister child from parent,
56+
// since the parent is also being removed
6057
this._childViews[i]._unregisterFromParent = false;
6158
this._childViews[i].remove();
6259
}

js/views/serverConfigRowVw.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ module.exports = BaseVw.extend({
1616

1717
initialize: function(options) {
1818
this.options = options || {};
19-
this._state = {
19+
this._state = options.initialState || {
2020
status: 'not-connected'
2121
};
2222

js/views/serverConfigsVw.js

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@ module.exports = BaseVw.extend({
1919
throw new Error('Please provide a ServerConfigs collection.');
2020
}
2121

22+
this.connectionState = {};
23+
this.collection.forEach((md) => {
24+
this.connectionState[md.id] = { status: 'not-connected' }
25+
});
26+
2227
this.listenTo(this.collection, 'update', this.render);
2328
this.listenTo(this.collection, 'remove', this.onRemoveConfig);
2429
},
@@ -29,18 +34,19 @@ module.exports = BaseVw.extend({
2934
__.every(this.configRowViews, (vw) => {
3035
index++;
3136
return vw.model !== md;
32-
33-
3437
});
3538

36-
if (index > -1) this.configRowViews.splice(index, 1);
39+
if (index > -1) {
40+
this.configRowViews.splice(index, 1);
41+
}
3742
},
3843

3944
setConnectionState: function(state) {
4045
// 'state' should be in the form:
4146
// { id: 31 (id of server config model), status: 'connecting' }
4247

43-
var index = -1;
48+
var index = -1,
49+
updatedState;
4450

4551
__.every(this.configRowViews, (vw, i) => {
4652
if (vw.model.id === state.id) {
@@ -52,15 +58,18 @@ module.exports = BaseVw.extend({
5258
});
5359

5460
if (index > -1) {
61+
updatedState = __.omit(state, 'id');
62+
this.connectionState[state.id] = updatedState;
5563
this.configRowViews[index]
56-
.setState(__.omit(state, 'id'));
64+
.setState(updatedState);
5765
}
5866
},
5967

6068
render: function() {
6169
var scrollTop;
6270

6371
this.rendered = true;
72+
this.configRowViews.forEach((vw) => vw.remove());
6473
this.configRowViews = [];
6574
scrollTop = this.$rowsWrap ? this.$rowsWrap[0].scrollTop : 0;
6675

@@ -70,7 +79,10 @@ module.exports = BaseVw.extend({
7079
this.$el.html(t());
7180

7281
this.collection.forEach((md) => {
73-
var vw = new ServerConfigRowVw({ model: md });
82+
var vw = new ServerConfigRowVw({
83+
model: md,
84+
initialState: this.connectionState[md.id]
85+
});
7486

7587
this.listenTo(vw, 'delete', (e) => {
7688
e.view.model.destroy();
@@ -86,8 +98,9 @@ module.exports = BaseVw.extend({
8698

8799
this.listenTo(vw, 'cancel', (e) => {
88100
this.trigger('cancel', { model: e.view.model });
89-
});
101+
});
90102

103+
this.registerChild(vw);
91104
$rows.append( vw.render().el );
92105
this.configRowViews.push(vw);
93106
});

js/views/serverConnectModal.js

Lines changed: 56 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
'use strict';
22

3-
var loadTemplate = require('../utils/loadTemplate'),
3+
var __ = require('underscore'),
4+
loadTemplate = require('../utils/loadTemplate'),
45
app = require('../App.js').getApp(),
56
remote = require('electron').remote,
67
ServerConfigMd = require('../models/serverConfigMd'),
@@ -21,22 +22,60 @@ module.exports = BaseModal.extend({
2122
initialize: function(options) {
2223
this.options = options || {};
2324

24-
this.headerVw = new ServerConnectHeaderVw({
25-
initialState: {
26-
msg: window.polyglot.t('serverConnectModal.serverConfigsHeaderMsg'),
27-
title: window.polyglot.t('serverConnectModal.serverConfigsTitle'),
28-
showNew: true
29-
}
25+
if (!options.userModel) {
26+
throw new Error('Please provide a user model.');
27+
}
28+
29+
this._headerState = {};
30+
31+
this.headerVw = new ServerConnectHeaderVw();
32+
this.setHeaderState({
33+
msg: { translate: 'serverConnectModal.serverConfigsHeaderMsg' },
34+
title: { translate: 'serverConnectModal.serverConfigsTitle' },
35+
showNew: true
3036
});
37+
setTimeout(() => this.registerChild(this.headerVw));
3138

3239
this.serverConfigs = app.serverConfigs;
3340
this.serverConfigsVw = new ServerConfigsVw({
3441
collection: this.serverConfigs
3542
});
43+
setTimeout(() => this.registerChild(this.serverConfigsVw));
3644

3745
this.listenTo(this.serverConfigsVw, 'edit-config', this.onEditConfig);
3846
this.listenTo(this.serverConfigsVw, 'connect', this.onConnectClick);
3947
this.listenTo(this.serverConfigsVw, 'cancel', this.onCancelClick);
48+
49+
options.userModel.on('change:language', () => {
50+
this.setHeaderState(this._headerState);
51+
this.serverConfigFormVw && this.serverConfigFormVw.render();
52+
this.serverConfigsVw.render();
53+
});
54+
},
55+
56+
setHeaderState: function(state) {
57+
// In order to properly translate the msg and title and update the
58+
// translations upon a language change, rather than providing a literal
59+
// string, please provide an object with the Polyglot key in a 'translation'
60+
// property, e.g:
61+
// {
62+
// title: { translate: 'polyglotKey' },
63+
// msg: 'my literal string' // if you dont want to translate
64+
// }
65+
var translatedState;
66+
67+
this._headerState = __.extend(this._headerState, state || {});
68+
translatedState = __.extend({}, this._headerState);
69+
70+
Object.keys(this._headerState).forEach((key) => {
71+
if (['msg', 'title'].indexOf(key) !== -1) {
72+
if (this._headerState[key]['translate']) {
73+
translatedState[key] = window.polyglot.t(this._headerState[key]['translate']);
74+
}
75+
}
76+
});
77+
78+
this.headerVw.setState(translatedState);
4079
},
4180

4281
hideMessageBar: function() {
@@ -55,9 +94,10 @@ module.exports = BaseModal.extend({
5594
if (!this.$jsConfigFormWrap) return;
5695

5796
this.$jsConfigFormWrap.addClass('slide-out');
58-
this.headerVw.setState({
59-
msg: window.polyglot.t('serverConnectModal.serverConfigsHeaderMsg'),
60-
title: window.polyglot.t('serverConnectModal.serverConfigsTitle'),
97+
98+
this.setHeaderState({
99+
msg: { translate: 'serverConnectModal.serverConfigsHeaderMsg' },
100+
title: { translate: 'serverConnectModal.serverConfigsTitle' },
61101
showNew: true
62102
});
63103

@@ -67,25 +107,23 @@ module.exports = BaseModal.extend({
67107
showConfigForm: function(configMd) {
68108
var model = configMd;
69109

70-
this._state = this.options.initialState || {};
71-
72110
if (!model) {
73111
model = new ServerConfigMd();
74112
model.__collection = this.serverConfigs;
75113
}
76114

77-
this.headerVw.setState({
78-
title: model.get('name') || window.polyglot.t('serverConnectModal.newConfigTitle'),
115+
this.setHeaderState({
116+
title: model.get('name') || { translate: 'serverConnectModal.newConfigTitle' },
79117
msg: configMd ?
80-
window.polyglot.t('serverConnectModal.editConfigHeaderMsg') :
81-
window.polyglot.t('serverConnectModal.newConfigHeaderMsg'),
118+
{ translate: 'serverConnectModal.editConfigHeaderMsg' } :
119+
{ translate: 'serverConnectModal.newConfigHeaderMsg' },
82120
showNew: false
83121
});
84122

85123
this.renderServerConfigForm(model);
86124

87125
this.listenTo(model, 'change:name', (md) => {
88-
this.headerVw.setState({
126+
this.setHeaderState({
89127
title: md.get('name')
90128
});
91129
});
@@ -364,6 +402,7 @@ module.exports = BaseModal.extend({
364402
this.serverConfigFormVw = new ServerConfigFormVw({
365403
model: md
366404
});
405+
this.registerChild(this.serverConfigFormVw);
367406

368407
this.$jsConfigFormWrap = this.$jsConfigFormWrap || this.$('.js-config-form-wrap');
369408
this.$jsConfigFormWrap.html(this.serverConfigFormVw.render().el);

0 commit comments

Comments
 (0)