Skip to content
This repository has been archived by the owner on Apr 20, 2024. It is now read-only.

Commit

Permalink
Merge pull request #66 from nodes-vapor/feature/improve-tags
Browse files Browse the repository at this point in the history
Improve date group
  • Loading branch information
siemensikkema authored Jun 8, 2018
2 parents 6290f50 + 4521055 commit 4af246b
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 9 deletions.
4 changes: 2 additions & 2 deletions Sources/AdminPanelProvider/Tags/Leaf/CheckboxGroup.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ public final class CheckboxGroup: BasicTag {
arguments.count >= 2,
case .variable(let fieldsetPathNodes, value: let fieldset) = arguments.list[0],
let fieldsetPath = fieldsetPathNodes.last
else {
throw Abort(.internalServerError, reason: "FormTextGroup parse error, expecting: #form:textgroup(\"name\", \"default\", fieldset)")
else {
throw Abort(.internalServerError, reason: "FormTextGroup parse error, expecting: #form:checkboxgroup(\"fieldset.name\", \"default\")")
}

// Retrieve input value, value from fieldset else passed default value
Expand Down
41 changes: 36 additions & 5 deletions Sources/AdminPanelProvider/Tags/Leaf/DateGroup.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@ import Leaf
import Vapor

public final class DateGroup: BasicTag {
public init(){}
public init() {}
public let name = "form:dategroup"

public func run(arguments: ArgumentList) throws -> Node? {
guard
arguments.count >= 2,
case .variable(let fieldsetPathNodes, value: let fieldset) = arguments.list[0],
let fieldsetPath = fieldsetPathNodes.last
else {
throw Abort(.internalServerError, reason: "FormDateGroup parse error, expecting: #form:dategroup(\"name\", \"default\", fieldset)")
else {
throw Abort(.internalServerError, reason: "DateGroup parse error, expecting: #form:dategroup(\"fieldset.name\", \"default\", \"dateFormat\", \"startDate\", \"attributes\")")
}

// Retrieve input value, value from fieldset else passed default value
Expand All @@ -24,15 +24,46 @@ public final class DateGroup: BasicTag {
let dateformat = arguments[2]?.string ?? "dd/mm/yyyy"
let startDate = arguments[3]?.string ?? ""

let attributesHTML: String
if let attributesNode = arguments[4] {
let attributes: [String]
if let attrArray = attributesNode.array {
attributes = attrArray.flatMap {
$0.string
}
} else if let attrStr = attributesNode.string {
attributes = attrStr.components(separatedBy: ",")
} else {
throw Abort(.internalServerError, reason: "DateGroup parse error, expecting: an array or comma separated list of custom attributes")
}

attributesHTML = attributes.joined(separator: ", ")
} else {
attributesHTML = ""
}

var template: [String] = [
"<div class=\"form-group action-wrapper\(hasErrors ? " has-error" : "")\">",
"<div class=\"input-group date\" data-provide=\"datepicker\" data-date-format=\"\(dateformat)\" data-date-start-date=\"\(startDate)\">",
"<input type=\"text\" class=\"form-control\" id='\(fieldsetPath)' name=\"\(fieldsetPath)\" value=\"\(inputValue)\">",
"<input type=\"text\" class=\"form-control\" id='\(fieldsetPath)' name=\"\(fieldsetPath)\" value=\"\(inputValue)\" \(attributesHTML)>",
"<div class=\"input-group-addon\">",
"<span class=\"glyphicon glyphicon-th\"></span>",
"</div></div></div>"
"</div></div>"
]

// If Fieldset has errors then loop through them and add help-blocks
if let errors = errors {
for e in errors {
guard let errorString = e.string else {
continue
}

template.append("<span class='help-block'>\(errorString)</span>")
}
}

template.append("</div>")

if let label = label {
template.insert("<label class=\"control-label\" for=\"\(fieldsetPath)\">\(label)</label>", at: 1)
}
Expand Down
4 changes: 2 additions & 2 deletions Sources/AdminPanelProvider/Tags/Leaf/TextGroup.swift
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public func this(_ lhs: String?, or rhs: String?) -> String {
}

public final class TextGroup: BasicTag {
public init(){}
public init() {}
public let name = "form:textgroup"

public func run(arguments: ArgumentList) throws -> Node? {
Expand All @@ -80,7 +80,7 @@ public final class TextGroup: BasicTag {
case .variable(let fieldsetPathNodes, value: let fieldset) = arguments.list[0],
let fieldsetPath = fieldsetPathNodes.last
else {
throw Abort(.internalServerError, reason: "FormTextGroup parse error, expecting: #form:textgroup(\"name\", \"default\", fieldset)")
throw Abort(.internalServerError, reason: "TextGroup parse error, expecting: #form:textgroup(\"fieldset.name\", \"default\", \"classes\", \"attributes\")")
}

// Retrieve input value, value from fieldset else passed default value
Expand Down

0 comments on commit 4af246b

Please sign in to comment.