Skip to content

js insert smiley replacement

Derek Jones edited this page Jul 5, 2012 · 3 revisions

The smiley helper is totally awesome. However I found a little bug. On the textarea, the smiley is always inserted at the end of the text. So I suggest this replacement to let the smiley inserted on the cursor position.

Step 1: Copy smiley_helper.php from system/helper to system/application/helper, so your replacement won't affect to another installation.

Step 2: Replace the function js_insert_smiley with this

function js_insert_smiley($form_name = '', $form_field = '')
    {
        return <<<EOF
[removed]
    function insert_smiley(id) {
        id = ' ' + id + ' ';
        var obj = document.getElementById('{$form_field}');
        if(typeof(document.selection)!='undefined') 
        {
            obj.focus();
            var range = document.selection.createRange();
            if(range.parentElement() != obj) {
                return false;
            }
            var orig = obj.value.replace(/rn/g, "n");
            range.text = id;
            var actual = tmp = obj.value.replace(/rn/g, "n");
            for(var diff = 0; diff < orig.length; diff++) {
                if(orig.charAt(diff) != actual.charAt(diff)) break;
            }
            for(var index = 0, start = 0; (tmp = tmp.replace(id, "")) && (index <= diff); index = start + id.length) {
                start = actual.indexOf(id, index);
            }
        } else {
            var startPos = obj.selectionStart;
            var endPos = obj.selectionEnd;
            obj.value = obj.value.substr(0, startPos) + id + obj.value.substr(endPos, obj.value.length);
        }
    }
    
    // Original insert_smiley function
    // function insert_smiley (smiley)
    // {
    //     document.{$form_name}.{$form_field}.value += " " + smiley;
    // }
[removed]
EOF;
    }
Clone this wiki locally