0001    //
0002    //  Example.swift
0003    //  Dynamo
0004    //
0005    //  Created by John Holdsworth on 11/07/2015.
0006    //  Copyright (c) 2015 John Holdsworth. All rights reserved.
0007    //
0008    //  $Id: //depot/Dynamo/Sources/Example.swift#2 $
0009    //
0010    //  Repo: https://github.com/johnno1962/Dynamo
0011    //
0012    
0013    import Foundation
0014    
0015    // MARK: Example web application
0016    
0017    /**
0018        Example web application or "swiftlet" testing form sbumission.
0019     */
0020    
0021    public class ExampleAppSwiftlet: HTMLApplicationSwiftlet {
0022    
0023        /**
0024            This is the entry point for most application swiftlets after browser parameters have been parsed.
0025         */
0026    
0027        override public func processRequest( out: DynamoHTTPConnection, pathInfo: String, parameters: [String:String], cookies: [String:String] ) {
0028            out.print( html( nil ) + head( title( "Table Example" ) +
0029                style( "body, table { font: 10pt Arial" ) ) + body( nil ) )
0030    
0031            if parameters["width"] == nil {
0032                out.print( h3( "Quick table creation example" ) )
0033                out.print(
0034                    form( ["method":"GET"],
0035                        table(
0036                            tr( td( "Title: " ) + td( input( ["type":"textfield", "name":"title"] ) ) ) +
0037                            tr( td( "Width: " ) + td( input( ["type":"textfield", "name":"width"] ) ) ) +
0038                            tr( td( "Height: " ) + td( input( ["type":"textfield", "name":"height"] ) ) ) +
0039                            tr( td( ["colspan":"2"], input( ["type": "submit", "value": "Generate"] )) )
0040                        )
0041                    )
0042                )
0043            }
0044            else if out.method == "GET" {
0045                if let title = parameters["title"] {
0046                    out.print( h2( title ) )
0047                }
0048    
0049                out.print( h3( "Enter table values" ) + form( ["method": "POST"], nil ) + table( nil ) )
0050    
0051                if let width = parameters["width"]?.toInt(), height = parameters["height"]?.toInt() {
0052                    for y in 0..<height {
0053                        out.print( tr( nil ) )
0054                        for x in 0..<width {
0055                            out.print( td( input( ["type":"textfield", "name":"x\(x)y\(y)", "size":"5"] ) ) )
0056                        }
0057                        out.print( _tr() )
0058                    }
0059                }
0060    
0061                out.print( _table()+p()+input( ["type": "submit", "value": "Complete"] )+_form() )
0062            }
0063            else {
0064                out.print( h3( "Your table:" ) + table( ["border":"1"], nil ) )
0065    
0066                if let width = parameters["width"]?.toInt(), height = parameters["height"]?.toInt() {
0067                    for y in 0..<height {
0068                        out.print( tr( nil ) )
0069                        for x in 0..<width {
0070                            out.print( td( parameters["x\(x)y\(y)"]! ) )
0071                        }
0072                        out.print( _tr() )
0073                    }
0074                }
0075    
0076                out.print( _table() )
0077            }
0078    
0079            out.print( p() + backButton() )
0080        }
0081    
0082    }
0083