PageRenderTime 83ms CodeModel.GetById 40ms app.highlight 10ms RepoModel.GetById 31ms app.codeStats 0ms

/kai/public/javascripts/rst_helpers.js

https://bitbucket.org/bbangert/kai/
JavaScript | 129 lines | 112 code | 8 blank | 9 comment | 30 complexity | 54b6f76eae78820816bb70e4a5ce7adf MD5 | raw file
  1function getCaretPosition(element){
  2  var caret = 0;
  3  if ('selectionStart' in element) {  // W3
  4    var selectionStart = element.selectionStart;
  5    var selectionEnd = element.selectionEnd;
  6    return selectionStart;
  7  } else {  // IE
  8    // Walk up the tree looking for this textarea's document node.
  9    var doc = element;
 10    while (doc.parentNode) {
 11      doc = doc.parentNode;
 12    }
 13    if (!doc.selection || !doc.selection.createRange) {
 14      // Not IE?
 15      return null;
 16    }
 17    var range = doc.selection.createRange();
 18    if (range.parentElement() != element) {
 19      // Cursor not in this textarea.
 20      return null;
 21    }
 22    var newRange = doc.body.createTextRange();
 23
 24    var collapsed = (range.text == '');
 25    newRange.moveToElementText(element);
 26    if (!collapsed) {
 27      newRange.setEndPoint('EndToEnd', range);
 28    }
 29    newRange.setEndPoint('EndToStart', range);
 30    var startPrefix = newRange.text;
 31    return startPrefix.length;
 32  }
 33}
 34/* 
 35http://stackoverflow.com/questions/29709/how-do-i-get-the-coordinates-of-the-caret-in-text-boxes
 36*/
 37
 38function insertAtCaret(myField, myValue) {
 39    
 40    /* selecion model - ie */
 41    if (document.selection) {
 42        myField.focus();
 43        sel = document.selection.createRange();
 44        sel.text = myValue;
 45    }
 46    
 47    /* field.selectionstart/end  firefox */ 
 48    else if (myField.selectionStart || myField.selectionStart == '0' ) {
 49        
 50        var startPos = myField.selectionStart;
 51        var endPos = myField.selectionEnd;
 52        myField.value = myField.value.substring(0, startPos)
 53            + myValue
 54            + myField.value.substring(endPos, myField.value.length);
 55        
 56        myField.selectionStart = startPos + myValue.length;
 57        myField.selectionEnd = startPos + myValue.length;
 58        myField.focus();
 59        } 
 60        
 61        // cursor not active/present
 62    else {
 63        myField.value += myValue;
 64    }
 65}
 66var contentbox = document.getElementById("snippet_form_content");
 67contentbox.focus();
 68if(contentbox.addEventListener ) {
 69    contentbox.addEventListener('keydown',this.keyHandler,false);
 70} else if(contentbox.attachEvent ) {
 71    contentbox.attachEvent('onkeydown',this.keyHandler); /* damn IE hack */
 72}
 73
 74var onchange_timer = null;
 75function keyHandler(e) {
 76    var TABKEY = 9;
 77    var ENTER = 13;
 78    if(e.keyCode == TABKEY) {
 79        setTimeout(function(){
 80            var sc = contentbox.scrollTop;
 81            var caret = getCaretPosition(contentbox);
 82            var val = contentbox.value.substr(0, caret);
 83            var i = val.lastIndexOf('\n');
 84            var v = '    ';
 85            switch((val.length - i - 1) % 4){
 86            case 1:
 87                v = '   ';
 88                break;
 89            case 2:
 90                v = '  ';
 91                break;
 92            case 3:
 93                v = ' ';
 94                break;
 95            }
 96            insertAtCaret(contentbox, v);
 97            contentbox.scrollTop = sc;
 98        },0)
 99        if(e.preventDefault) {
100            e.preventDefault();
101        }
102        return false;
103    } else if (e.keyCode == ENTER) {
104        setTimeout(function(){
105            var sc = contentbox.scrollTop;
106            var caret = getCaretPosition(contentbox);
107            var val = contentbox.value.substr(0, caret);
108            var i = val.lastIndexOf('\n');
109            if (i > 0 && i+1 < caret) {
110                var curline = contentbox.value.substring(i+1, caret);
111                var spaces = curline.replace(/^(\s+).*/, '$1');
112                var v = '';
113                if (spaces.match(/^\s+$/)) {
114                    v = '\n' + spaces;
115                } else {
116                    v = '\n';
117                }
118                insertAtCaret(contentbox, v);
119                contentbox.scrollTop = sc;
120            } else {
121                insertAtCaret(contentbox, '\n');
122                contentbox.scrollTop = sc;
123            }
124        },0)
125        if(e.preventDefault) {
126            e.preventDefault();
127        }        
128    }
129}