-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathWOImage.swift
More file actions
97 lines (82 loc) · 2.84 KB
/
WOImage.swift
File metadata and controls
97 lines (82 loc) · 2.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
//
// WOImage.swift
// SwiftObjects
//
// Created by Helge Hess on 15.05.18.
// Copyright © 2018-2026 ZeeZide. All rights reserved.
//
/**
* This element renders an <img> tag, possibly pointing to dynamically
* generated URLs.
*
* Sample:
*
* Banner: WOImage {
* src = "/images/banner.gif";
* border = 0;
* }
*
* Renders:
*
* <img src="/images/banner.gif" border="0" />
*
* Bindings (WOLinkGenerator for image resource):
* ```
* src [in] - string
* filename [in] - string
* framework [in] - string
* actionClass [in] - string
* directActionName [in] - string
* queryDictionary [in] - Map<String,String>
* ?wosid [in] - boolean (constant!)
* - all bindings starting with a ? are stored as query parameters.
* ```
*
* Regular bindings:
* ```
* disableOnMissingLink [in] - boolean
* ```
*/
open class WOImage : WOHTMLDynamicElement {
// TBD: support 'data' binding URLs (also needs mimeType and should have 'key'
// bindings). Also: WOResourceManager.flushDataCache().
// I think this generates the data and puts it into the
// WOResourceManager. If it can't find the RM data, it most likely needs
// to regenerate it using an *action* (technically 'data' is the same
// like using 'action'?! [+ caching]).
let link : WOLinkGenerator?
let disabled : WOAssociation?
let disableOnMissingLink : WOAssociation?
required
public init(name: String = "", bindings: inout Bindings,
template: WOElement? = nil)
{
disabled = bindings.removeValue(forKey: "disabled")
disableOnMissingLink = bindings.removeValue(forKey: "disableOnMissingLink")
link = WOLinkGenerator.resourceLinkGenerator(keyedOn: "src", for: &bindings)
super.init(name: name, bindings: &bindings, template: template)
}
override
open func append(to response: WOResponse, in context: WOContext) throws {
guard !context.isRenderingDisabled else { return }
let cursor = context.cursor
if disabled?.boolValue(in: cursor) ?? false { return }
let url = link?.fullHref(in: context)
if url == nil && (disableOnMissingLink?.boolValue(in: cursor) ?? false) {
return
}
try response.appendBeginTag("img")
if let url = url { try response.appendAttribute("src", url) }
try appendExtraAttributes(to: response, in: context)
try response.appendBeginTagClose(context.closeAllElements)
}
// MARK: - Description
override open func appendToDescription(_ ms: inout String) {
super.appendToDescription(&ms)
if let link = link { ms += " src=\(link)" }
WODynamicElement.appendBindingsToDescription(&ms,
"disabled", disabled,
"disableOnMissingLink", disableOnMissingLink
)
}
}