g-Off.net | Geoffrey Foster


18
Mar/09
0

Cappuccino/Objective-J and URLTextField

I wanted to display a link in a Cappuccino web application but there doesn't seem to be a very "nice" way of doing that so I wrote up the URLTextField class. It's basically a regular CPTextField but the CPTextFields DOMElement is wrapped in another DOMElement that provides the anchor tag. Use it like a normal CPTextField and call setStringValue: with the text you'd like displayed for the link, and use setLink: with a valid URL to set the link. A convenience initWithFrame:link:title: is also available that sets it all up from the get-go.

// Copyright (c) 2009 Geoffrey Foster
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
 
@import <AppKit/CPTextField.j>
 
@implementation URLTextField : CPTextField
{
    CPString _link;
    DOMElement _DOMAnchorElement;
}
 
- (id)initWithFrame:(CGRect)aFrame
{
    return [self initWithFrame:aFrame link:@"" title:@""];
}
 
- (id)initWithFrame:(CGRect)aFrame link:(CPString)link title:(CPString)title
{
    if(self = [super initWithFrame:aFrame])
    {
		_link = link;
		[super setStringValue:title];
 
        _DOMAnchorElement = document.createElement("a");
        _DOMAnchorElement.href = link;
        _DOMAnchorElement.title = title;
 
        var element = _DOMElement.removeChild(_DOMTextElement);
        element.style.cursor = "pointer";
        _DOMAnchorElement.appendChild(element);
        _DOMElement.appendChild(_DOMAnchorElement);
	}
	return self;
}
 
- (void)setLink:(CPString)link
{
    _link = link;
    _DOMAnchorElement.href = _link;
}
- (CPString)getLink
{
    return _link;
}
 
- (void)setStringValue:(CPString)anObject
{
    [super setStringValue:anObject];
    _DOMAnchorElement.title = anObject;
    [super sizeToFit];
}
 
@end
Share and Enjoy:
  • Digg
  • Facebook
  • Google Bookmarks
  • LinkedIn
  • Reddit
  • Twitter
Comments (0) Trackbacks (0)

No comments yet.

Leave a comment


No trackbacks yet.