jquery.textinput.js 4.41 KB
/**
 * @fileOverview jqueryui插件封装
 * @author:Hbomb(zhouqq@yoho.cn)
 * @date:2012-07-09
 */
define('lib/util/jquery.textinput', [ "jquery"], function(require)
{
    var jQuery = require("jquery");
    (function($)
    {
        $.fn.extend({
                    insertContent : function(myValue, t)
                    {
                        var $t = $(this)[0];
                        
                        if (document.selection)
                        {// ie
                            this.focus();
                            var sel = document.selection.createRange();
                            sel.text = myValue;
                            this.focus();
                            sel.moveStart('character', -l);
                            var wee = sel.text.length;
                            if (arguments.length == 2)
                            {
                                var l = $t.value.length;
                                sel.moveEnd("character", wee + t);
                                t <= 0 ? sel.moveStart("character", wee - 2 * t
                                        - myValue.length) : sel.moveStart(
                                        "character", wee - t - myValue.length);
                                
                                sel.select();
                            }
                        }
                        else if ($t.selectionStart || $t.selectionStart == '0')
                        {
                            var startPos = $t.selectionStart;
                            var endPos = $t.selectionEnd;
                            var scrollTop = $t.scrollTop;
                            $t.value = $t.value.substring(0, startPos)
                                    + myValue
                                    + $t.value.substring(endPos,
                                            $t.value.length);
                            this.focus();
                            $t.selectionStart = startPos + myValue.length;
                            $t.selectionEnd = startPos + myValue.length;
                            $t.scrollTop = scrollTop;
                            if (arguments.length == 2)
                            {
                                $t.setSelectionRange(startPos - t,
                                        $t.selectionEnd + t);
                                this.focus();
                            }
                        }
                        else
                        {
                            this.value += myValue;
                            this.focus();
                        }
                    }
                });
    })(jQuery);
    
    jQuery.fn.extend({
        getCurPos : function()
        {
            var e = jQuery(this).get(0);
            e.focus();
            if (e.selectionStart)
            { // FF
                return e.selectionStart;
            }
            if (document.selection)
            { // IE
                var r = document.selection.createRange();
                if (r == null)
                {
                    return e.value.length;
                }
                var re = e.createTextRange();
                var rc = re.duplicate();
                re.moveToBookmark(r.getBookmark());
                rc.setEndPoint('EndToStart', re);
                return rc.text.length;
            }
            return e.value.length;
        },
        setCurPos : function(pos)
        {
            var e = jQuery(this).get(0);
            e.focus();
            if (e.setSelectionRange)
            {
                e.setSelectionRange(pos, pos);
            }
            else if (e.createTextRange)
            {
                var range = e.createTextRange();
                range.collapse(true);
                range.moveEnd('character', pos);
                range.moveStart('character', pos);
                range.select();
            }
        },
        setSelect: function(start,end)
        {
            var e = jQuery(this).get(0);
            e.focus();
            if (e.setSelectionRange)
            {
                e.setSelectionRange(start, end);
            }
            else if (e.createTextRange)
            {
                var range = e.createTextRange();
                range.collapse(true);
                range.moveEnd('character', end);
                range.moveStart('character', start);
                range.select();
            }
        }
        
    });
});