logo

abstract class

web::Page

sys::Obj
  web::Weblet
    web::Page
   1  //
   2  // Copyright (c) 2007, Brian Frank and Andy Frank
   3  // Licensed under the Academic Free License version 3.0
   4  //
   5  // History:
   6  //   31 Jul 07  Andy Frank  Creation
   7  //
   8  
   9  **
  10  ** Page is a Weblet designed to render Widgets.
  11  **
  12  abstract class Page : Weblet
  13  {
  14  
  15  //////////////////////////////////////////////////////////////////////////
  16  // Methods
  17  //////////////////////////////////////////////////////////////////////////
  18  
  19    **
  20    ** Render the markup for this widget. This method *must*
  21    ** use the `headElem` and `bodyElem` fields for rendering
  22    ** the markup in order to produce a valid HTML file.
  23    ** Never use 'res.out' directly.
  24    **
  25    abstract Void render()
  26  
  27    **
  28    ** Handle a form submission.
  29    **
  30    virtual Void submit()
  31    {
  32    }
  33  
  34  //////////////////////////////////////////////////////////////////////////
  35  // Weblet
  36  //////////////////////////////////////////////////////////////////////////
  37  
  38    override Void get()
  39    {
  40      try
  41      {
  42        head := Buf.make
  43        body := Buf.make
  44  
  45        headElem = WebOutStream.make(head.out)
  46        bodyElem = WebOutStream.make(body.out)
  47  
  48        // cache locals
  49        Thread.locals["web.widget.head"] = headElem
  50        Thread.locals["web.widget.body"] = bodyElem
  51  
  52        // render the page
  53        render
  54  
  55        // finish the page
  56        // TODO - get encoding from OutStream
  57        res.headers["Content-Type"] = "text/html; charset=UTF-8"
  58        res.headers["Content-Encoding"] = "UTF-8"
  59        res.out.prolog
  60        res.out.docType
  61        res.out.html
  62        res.out.head
  63        res.out.writeBuf(head.flip)
  64        res.out.headEnd
  65        res.out.body
  66        res.out.writeBuf(body.flip)
  67        res.out.bodyEnd
  68        res.out.htmlEnd
  69      }
  70      finally
  71      {
  72        // remove locals
  73        Thread.locals.remove("web.widget.head")
  74        Thread.locals.remove("web.widget.body")
  75      }
  76    }
  77  
  78    override Void post()
  79    {
  80      isForm := req.headers["Content-Type"] == "application/x-www-form-urlencoded"
  81      action := req.uri.query["action"]
  82  
  83      if (action != null)
  84      {
  85        this.trap(action, [,])
  86      }
  87      else if (isForm)
  88      {
  89        submit
  90      }
  91      else res.sendError(404)
  92    }
  93  
  94  //////////////////////////////////////////////////////////////////////////
  95  // Fields
  96  //////////////////////////////////////////////////////////////////////////
  97  
  98    **
  99    ** The buffered WebOutStream for the <head> element.
 100    **
 101    readonly WebOutStream headElem
 102  
 103    **
 104    ** The buffered WebOutStream for the <body> element.
 105    **
 106    readonly WebOutStream bodyElem
 107  
 108  }