Forks

Revisions

gist: 103078 Download_button fork
public
Public Clone URL: git://gist.github.com/103078.git
Embed All Files: show embed
Text only #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
@implementation CPTextView : CPView
{
    DOMElement FIXME_textArea;
    
    id _delegate;
    
    CPScrollView _scrollView;
    CPView _contentView;
    
    JSObject _existingSelectStart;
    
    BOOL _alreadyFired;
}
 
-(id)initWithFrame:(CGRect)aFrame
{
    self = [super initWithFrame: aFrame];
    
    FIXME_textArea = document.createElement("textarea");
    
    FIXME_textArea.style.width = "100%";
    FIXME_textArea.style.height = "100%";
    
    if(document.selection)
        FIXME_textArea.style.overflow = "auto";
    else
        FIXME_textArea.style.overflow = "hidden";
        
    FIXME_textArea.style.position = "absolute";
    
    FIXME_textArea.style.left = "0";
    FIXME_textArea.style.top = "0";
    FIXME_textArea.style.border = "0";
    FIXME_textArea.style.margin = "0";
    FIXME_textArea.style.padding = "0";
    
    FIXME_textArea.style.backgroundColor = "rgb(245, 245, 245)";
    FIXME_textArea.style.fontSize = "14px";
    FIXME_textArea.style.fontFamily = "Arial";
     
    FIXME_textArea.style.resize = "none";
    FIXME_textArea.style.outlineStyle = "none";
 
    FIXME_textArea.onkeydown = function() {
        [self textDidChange: self];
    };
        
    FIXME_textArea.onkeypress = function() {
        [self textDidChange: self];
    };
 
    FIXME_textArea.onkeyup = function() {
        [self textDidChange: self];
    };
 
    if(document.attachEvent)
    {
        FIXME_textArea.onfocus = function() {
            _existingSelectStart = document.body.onselectstart;
            document.body.onselectstart = function() { };
        };
        
        FIXME_textArea.onblur = function() {
            if(_existingSelectStart)
                document.body.onselectstart = _existingSelectStart;
        };
    }
    
    _DOMElement.appendChild(FIXME_textArea);
 
    return self;
}
 
- (void)setDelegate:(id)delegate
{
    _delegate = delegate;
}
 
- (id)delegate
{
    return _delegate;
}
 
- (void)textDidChange:(id)sender
{
    [_delegate textViewDidChange: self];
 
    if (!_contentView)
        return;
    
    var bounds = [_contentView bounds];
 
    FIXME_textArea.style.height = CGRectGetHeight(bounds) + "px";
    FIXME_textArea.style.height = MAX(CGRectGetHeight(bounds), FIXME_textArea.scrollHeight) + "px";
    
    [self setFrameSize:CGSizeMake(CGRectGetWidth(bounds), parseInt(FIXME_textArea.style.height, 10))];
    [self scrollToCaret];
    
    [[CPRunLoop currentRunLoop] performSelectors];
}
 
- (void)scrollToCaret
{
    if(![_scrollView verticalScroller] || [[_scrollView verticalScroller] isHidden])
        return;
            
    if(FIXME_textArea.selectionStart)
    {
        var start = FIXME_textArea.selectionStart,
            end = FIXME_textArea.selectionEnd;
 
        var imposter = document.createElement('div'),
            referenceSpan = document.createElement('span'),
            stringValue = FIXME_textArea.value;
        
        imposter.style.overflow = "hidden";
        imposter.style.fontSize = "14px";
        imposter.style.padding = "0";
        imposter.style.margin = "0";
        imposter.style.height = FIXME_textArea.style.height;
        imposter.style.width = getComputedStyle(FIXME_textArea, "").getPropertyValue('width');
        imposter.style.fontFamily = getComputedStyle(FIXME_textArea, "").getPropertyValue('font-family');
        
        for(var i=0; i<start; i++)
        {
            referenceSpan.innerHTML = stringValue.charAt(i).replace("\n", "<br />");
            imposter.appendChild(referenceSpan.cloneNode(true));
        }
    
        while (imposter.childNodes[start - 1] && imposter.childNodes[start - 1].innerHTML == " ")
            start--;
                
        document.body.appendChild(imposter);
    
        var caretOffsetTop = imposter.childNodes[start - 1].offsetTop - imposter.offsetTop,
            caretHeight = imposter.childNodes[start-1].offsetHeight;
        
        document.body.removeChild(imposter);
    }
    else if(document.selection)
    {
        FIXME_textArea.focus();
        var range = document.selection.createRange();
        
        window.range = range;
        if(range.parentElement() != FIXME_textArea)
            return;
                            
        var caretOffsetTop = range.offsetTop + _DOMElement.offsetTop - 18,
            caretHeight = 18;
    }
    else
        return;
 
    [self scrollRectToVisible:CGRectMake(1, caretOffsetTop, 1, caretHeight)];
}
 
- (CPString)stringValue
{
    return FIXME_textArea.value;
}
 
- (void)setStringValue:(CPString)aString
{
    if(aString)
        FIXME_textArea.value = aString;
    else
        FIXME_textArea.value = "";
    
    [self textDidChange: self];
}
 
- (void)focus
{
    window.setTimeout(function(){
        FIXME_textArea.focus();
    }, 0);
}
 
- (void)viewDidMoveToSuperview
{
    _scrollView = [self enclosingScrollView];
    _contentView = [_scrollView contentView];
}
 
- (void)setFrameSize:(CGSize)aSize
{
    [super setFrameSize:aSize];
    
    [self scrollToCaret];
}
 
@end