<commit_before>include('../view.js');
include('observable.js');

/**
 * @class
 */
uki.view.Focusable = {
    // dom: function() {
    //     return null; // should implement
    // },
    
    _focusable: true, // default value
    
    focusable: uki.newProp('_focusable', function(v) {
        this._focusable = v;
        if (this._focusableInput) this._focusableInput.style.display = v ? '' : 'none';
    }),
    
    disabled: uki.newProp('_disabled', function(d) {
        this._disabled = d;
        if (d) this.blur();
        this._focusableInput.disabled = d;
        if (this._updateBg) this._updateBg();
    }),    
    
    _initFocusable: function(preCreatedInput) {
        if (!this._focusable) return;
        
        var input = preCreatedInput;
        if (!input) {
            input = uki.createElement(
                'input', 
                uki.view.Base[PROTOTYPE].defaultCss + "border:none;padding:0;overflow:hidden;width:1px;height:1px;padding:1px;" + 
                "font-size:1px;left:-9999em;top:50%;background:transparent;outline:none;opacity:0;"
            );
            this.dom().appendChild(input);
        }
        this._focusableInput = input;
        this._hasFocus = false;
        this._firstFocus = true;
        var _this = this,
            needsRefocus = doc.attachEvent;
            
        uki.dom.bind(input, 'focus', function(e) {
            if (_this._hasFocus) return;
            _this._hasFocus = true;
            _this._focus(e);
            _this._firstFocus = false;
            _this.trigger('focus', {domEvent: e, source: _this});
        });
        
        uki.dom.bind(input, 'blur', function(e) {
            if (!_this._hasFocus) return;
            _this._hasFocus = false;
            _this._blur(e);
            _this.trigger('blur', {domEvent: e, source: this});
        });
        
        if (!preCreatedInput) this.bind('mousedown', function(e) {
            // if (!_this.hasFocus()) {
                setTimeout(function() {_this._focusableInput.focus();}, 1);
                // _this._focusableInput.focus();
            // } else {
                // needsRefocus && setTimeout(function() {_this._focusableInput.focus();}, 1);
            // }
            // e.stopPropagation();
            // e.domEvent.preventDefault ? e.domEvent.preventDefault() : e.domEvent.returnValue = false;
        });
    },
    
    _focus: function(e) {
    },
    
    _blur: function(e) {
    },
    
    focus: function() {
        this._focusableInput.focus();
    },
    
    blur: function() {
        this._focusableInput.blur()
    },
    
    hasFocus: function() {
        return this._hasFocus;
    },
    
    _bindToDom: function(name) {
        if (!this._focusableInput || 'keyup keydown keypress'.indexOf(name) == -1) return false;
        
        return uki.view.Observable._bindToDom.call(this, name, this._focusableInput);
    }
    

};<commit_msg>remove useless comments, bind focus and blur events to input<commit_after>include('../view.js');
include('observable.js');

/**
 * @class
 */
uki.view.Focusable = {
    // dom: function() {
    //     return null; // should implement
    // },
    
    _focusable: true, // default value
    
    focusable: uki.newProp('_focusable', function(v) {
        this._focusable = v;
        if (this._focusableInput) this._focusableInput.style.display = v ? '' : 'none';
    }),
    
    disabled: uki.newProp('_disabled', function(d) {
        this._disabled = d;
        if (d) this.blur();
        this._focusableInput.disabled = d;
        if (this._updateBg) this._updateBg();
    }),    
    
    _initFocusable: function(preCreatedInput) {
        if (!this._focusable) return;
        
        var input = preCreatedInput;
        if (!input) {
            input = uki.createElement(
                'input', 
                uki.view.Base[PROTOTYPE].defaultCss + "border:none;padding:0;overflow:hidden;width:1px;height:1px;padding:1px;" + 
                "font-size:1px;left:-9999em;top:50%;background:transparent;outline:none;opacity:0;"
            );
            this.dom().appendChild(input);
        }
        this._focusableInput = input;
        this._hasFocus = false;
        this._firstFocus = true;
        var _this = this,
            needsRefocus = doc.attachEvent;
            
        uki.dom.bind(input, 'focus', function(e) {
            if (_this._hasFocus) return;
            _this._hasFocus = true;
            _this._focus(e);
            _this._firstFocus = false;
            _this.trigger('focus', {domEvent: e, source: _this});
        });
        
        uki.dom.bind(input, 'blur', function(e) {
            if (!_this._hasFocus) return;
            _this._hasFocus = false;
            _this._blur(e);
            _this.trigger('blur', {domEvent: e, source: this});
        });
        
        if (!preCreatedInput) this.bind('mousedown', function(e) {
            setTimeout(function() {_this._focusableInput.disabled || _this._focusableInput.focus();}, 1);
        });
    },
    
    _focus: function(e) {
    },
    
    _blur: function(e) {
    },
    
    focus: function() {
        this._focusableInput.focus();
    },
    
    blur: function() {
        this._focusableInput.blur()
    },
    
    hasFocus: function() {
        return this._hasFocus;
    },
    
    _bindToDom: function(name) {
        if (!this._focusableInput || 'keyup keydown keypress focus blur'.indexOf(name) == -1) return false;
        
        return uki.view.Observable._bindToDom.call(this, name, this._focusableInput);
    }
    

};
{'fraction_non_alphanumeric': 0.1438270502054672, 'fraction_numerical': 0.0051813471502590676, 'mean_word_length': 1.888544891640867, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6621845', 'n_tokens_mistral': 1735, 'n_tokens_neox': 1690, 'n_words': 285}