Skip to content

Commit 2cab6d3

Browse files
committed
Improve dialogs with keyboard and auto focus
1 parent eb6f922 commit 2cab6d3

3 files changed

Lines changed: 63 additions & 15 deletions

File tree

client/resources/templates/dialogs/prompt.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ <h4 class="modal-title"><%- options.title || "Input" %></h4>
99
</div>
1010
<div class="modal-footer">
1111
<button class="btn btn-default action-close">Close</button>
12-
<button class="btn btn-success action-prompt">OK</button>
12+
<button class="btn btn-success action-confirm">OK</button>
1313
</div>
1414
</div>
1515
</div>

client/utils/dialogs.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,9 @@ define([
3636
return Dialogs.open(null, {
3737
"message": message,
3838
"dialog": "prompt",
39-
"default": defaultmsg
39+
"default": defaultmsg,
40+
"autoFocus": true,
41+
"valueSelector": "selectorPrompt"
4042
});
4143
},
4244

client/views/dialogs/base.js

Lines changed: 59 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,17 @@ define([
1010
template: null,
1111
dialog: null,
1212
open: true,
13-
big: false,
14-
className: ""
13+
autoFocus: false,
14+
keyboard: true,
15+
className: "",
16+
valueSelector: true
1517
},
1618
events: {
19+
"keydown": "keydown",
1720
"hidden.bs.modal": "hidden",
21+
"shown.bs.modal": "shown",
1822
"click .action-close": "close",
19-
"click .action-confirm": "actionConfirm",
20-
"click .action-prompt": "actionPrompt"
23+
"click .action-confirm": "actionConfirm"
2124
},
2225
template: function() {
2326
if (this.options.template != null) return this.options.template;
@@ -31,9 +34,14 @@ define([
3134

3235
initialize: function(options) {
3336
DialogView.__super__.initialize.apply(this, arguments);
34-
if (this.options.big) this.$el.addClass("modal-big");
3537
this.$el.addClass(this.options.className);
38+
3639
this.value = null;
40+
41+
if (this.options.keyboard) {
42+
$(document).keydown(_.bind(this.keydown, this));
43+
}
44+
3745
return this;
3846
},
3947

@@ -74,25 +82,63 @@ define([
7482
},
7583

7684
/*
77-
* (event) action: confirm
85+
* (event) Modal is shown
86+
*/
87+
shown: function() {
88+
if (this.options.autoFocus) {
89+
this.$("input").focus();
90+
}
91+
},
92+
93+
/*
94+
* (event) action: confirm: close dialog with value from selector
7895
*/
7996
actionConfirm: function(e) {
8097
if (e != null) {
8198
e.preventDefault();
8299
}
83-
this.value = true;
100+
this.value = this._getValue();
84101
this.close();
85102
},
86103

87104
/*
88-
* (event) action: prompt
105+
* Selector for prompt dialog
89106
*/
90-
actionPrompt: function(e) {
91-
if (e != null) {
92-
e.preventDefault();
107+
selectorPrompt: function() {
108+
return this.$(".input").val();
109+
},
110+
111+
/*
112+
* Return final value
113+
*/
114+
_getValue: function() {
115+
var selector = this.options.valueSelector;
116+
if (_.isFunction(selector)) {
117+
this.value = selector(this);
118+
} else if (_.isString(selector)) {
119+
this.value = this[selector]();
120+
} else {
121+
this.value = selector;
122+
}
123+
return this.value;
124+
},
125+
126+
/*
127+
* (event) keydown
128+
*/
129+
keydown: function(e) {
130+
if (!this.options.keyboard) return;
131+
132+
var key = e.keyCode || e.which;
133+
134+
// Enter: valid
135+
if (key == 13) {
136+
this.actionConfirm(e);
137+
} else
138+
// Esc: close
139+
if (key == 27) {
140+
this.close(e);
93141
}
94-
this.value = this.$(".input").val();
95-
this.close();
96142
}
97143
}, {
98144
current: null,

0 commit comments

Comments
 (0)