domenica 22 luglio 2007

HttpView2's Canvas/Painter system

Most of my work on HV2 involves the creation of a new Canvas/Painter system. This is heavily inspired by Seaside's Canvas system, but has a couple of key differences.

Seaside's Canvas system is based around three classes.
The first, WACanvas, has the basic infrastructure for producing HTML tags.
The second, WAHtmlCanvas, is a WACanvas subclass that adds support for "static" HTML tags (such as <p>, <div>, etc.).
The last class is the main one you're going to use in Seaside: WARenderCanvas. This is a subclass of WAHtmlCanvas that adds support for the "dynamic" tags of HTML: forms, input fields etc, and is the most integrated with the other parts of Seaside.

I've pondered whether to reuse Seaside's Canvas system or not, and in the end I've decided against it: adapting it to HV2 would entail more work than creating a new system from scratch. Also, having another implementation of a Canvas system allows exploring other possible solutions to the problem of creating HTML from Smalltalk code.

HV2's Canvas/Painter system is similar to Seaside's Canvas one. There's a canvas class, currently named HVXHTMLCanvas, whose purpose is to produce HTML tags on a stream.

The user should use HVXHTMLCanvas directly, but should instead use an HVPainter instance. This painter class delegates most of the work to the canvas, but adds some high-level behavior like autonaming of form fields and automated retrieval of the values of the fields of a POST request.
Here's a snippet from a test application I'm building to test HV2's Canvas/Painter system:
painter form withFieldset: 
[summary := painter textField
label: 'Summary';
value: model summary.
location := painter painter textField
label: 'Location';
value: model location.
"..."
description := painter painter textField
label: 'Description';
value: model description.
button := painter submitButton value: 'Save'].
"..."
button ifPressed:
[model summary: summary value.
model location: location value.
model description: description value.
"..."
self redirectToDefault].

A nice change from HV2's old Builder system is that the form-autonaming feature of the new Canvas/Painter system may be easily overridden by the user: in the previous system, I could have used
painter textField
label: 'Summary';
name: 'summary';
value: model summary.

to create a text input field whose name would have been 'summary' instead of the name computed by HV2's Painter.

Nessun commento: