mirror of
				https://github.com/zulip/zulip.git
				synced 2025-11-03 21:43:21 +00:00 
			
		
		
		
	compose_state.js: Extract focus_in_empty_compose().
Wrote some tests for the function also.
This commit is contained in:
		@@ -1,5 +1,6 @@
 | 
			
		||||
var noop = function () {};
 | 
			
		||||
var return_false = function () { return false; };
 | 
			
		||||
var return_true = function () { return true; };
 | 
			
		||||
 | 
			
		||||
set_global('document', {
 | 
			
		||||
    location: {
 | 
			
		||||
@@ -215,3 +216,24 @@ function assert_hidden(sel) {
 | 
			
		||||
                                           trigger: 'new topic button'}),
 | 
			
		||||
                 'subject');
 | 
			
		||||
}());
 | 
			
		||||
 | 
			
		||||
(function test_focus_in_empty_compose() {
 | 
			
		||||
    $('#new_message_content').is = function (attr) {
 | 
			
		||||
        assert.equal(attr, ':focus');
 | 
			
		||||
        return $('#new_message_content').is_focused;
 | 
			
		||||
    };
 | 
			
		||||
 | 
			
		||||
    compose_state.composing = return_true;
 | 
			
		||||
    $('#new_message_content').val('');
 | 
			
		||||
    $('#new_message_content').focus();
 | 
			
		||||
    assert(compose_state.focus_in_empty_compose());
 | 
			
		||||
 | 
			
		||||
    compose_state.composing = return_false;
 | 
			
		||||
    assert(!compose_state.focus_in_empty_compose());
 | 
			
		||||
 | 
			
		||||
    $('#new_message_content').val('foo');
 | 
			
		||||
    assert(!compose_state.focus_in_empty_compose());
 | 
			
		||||
 | 
			
		||||
    $('#new_message_content').blur();
 | 
			
		||||
    assert(!compose_state.focus_in_empty_compose());
 | 
			
		||||
}());
 | 
			
		||||
 
 | 
			
		||||
@@ -18,6 +18,13 @@ exports.composing = function () {
 | 
			
		||||
    return !!message_type;
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
exports.focus_in_empty_compose = function () {
 | 
			
		||||
    return (
 | 
			
		||||
        exports.composing() &&
 | 
			
		||||
        exports.message_content() === "" &&
 | 
			
		||||
        $('#new_message_content').is(':focus'));
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
function get_or_set(fieldname, keep_leading_whitespace) {
 | 
			
		||||
    // We can't hoist the assignment of 'elem' out of this lambda,
 | 
			
		||||
    // because the DOM element might not exist yet when get_or_set
 | 
			
		||||
 
 | 
			
		||||
@@ -7,14 +7,6 @@ function do_narrow_action(action) {
 | 
			
		||||
    return true;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
function focus_in_empty_compose() {
 | 
			
		||||
    return (
 | 
			
		||||
        compose_state.composing() &&
 | 
			
		||||
        compose_state.message_content() === "" &&
 | 
			
		||||
        $('#new_message_content').is(':focus'));
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
function open_reactions() {
 | 
			
		||||
    var message = current_msg_list.selected_message();
 | 
			
		||||
    var target = $(current_msg_list.selected_row()).find(".actions_hover")[0];
 | 
			
		||||
@@ -522,12 +514,12 @@ exports.process_hotkey = function (e, hotkey) {
 | 
			
		||||
        // Note that there is special handling for enter/escape too, but
 | 
			
		||||
        // we handle this in other functions.
 | 
			
		||||
 | 
			
		||||
        if (event_name === 'left_arrow' && focus_in_empty_compose()) {
 | 
			
		||||
        if (event_name === 'left_arrow' && compose_state.focus_in_empty_compose()) {
 | 
			
		||||
            message_edit.edit_last_sent_message();
 | 
			
		||||
            return true;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        if ((event_name === 'up_arrow' || event_name === 'down_arrow') && focus_in_empty_compose()) {
 | 
			
		||||
        if ((event_name === 'up_arrow' || event_name === 'down_arrow') && compose_state.focus_in_empty_compose()) {
 | 
			
		||||
            compose_actions.cancel();
 | 
			
		||||
            // don't return, as we still want it to be picked up by the code below
 | 
			
		||||
        } else if (event_name === "page_up") {
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user