apeeyush/Data-Analytics-Log-Manager

View on GitHub
app/assets/javascripts/enable_tab.js

Summary

Maintainability
A
0 mins
Test Coverage
// Used to enable the use of TAB in textarea
// This is useful when editing the JSON Query directly as people are used to pressing TAB for indentation
// Used by DataInteractive only but still included in application.js
function enableTab(id) {
  var el = document.getElementById(id);
  if (el !== null) {
    el.onkeydown = function(e) {
        if (e.keyCode === 9) { // tab was pressed

            // get caret position/selection
            var val = this.value,
                start = this.selectionStart,
                end = this.selectionEnd;

            // set textarea value to: text before caret + tab + text after caret
            this.value = val.substring(0, start) + '\t' + val.substring(end);

            // put caret at right position again
            this.selectionStart = this.selectionEnd = start + 1;

            // prevent the focus lose
            return false;

        }
    };
  }
}