1 /**
2 * Copyright © DiamondMVC 2019
3 * License: MIT (https://github.com/DiamondMVC/Diamond/blob/master/LICENSE)
4 * Author: Jacob Jensen (bausshf)
5 */
6 module diamond.web.elements.element;
7 
8 /// Wrapper around a html element.
9 abstract class Element
10 {
11   private:
12   /// The attributes of the element.
13   string[string] _attributes;
14   /// The tag name.
15   string _tagName;
16   /// Inner text representation.
17   string _inner;
18   /// The id of the element.
19   string _id;
20   /// The name of the element.
21   string _name;
22 
23   protected:
24   /// Generates the appropriate html for the element.
25   abstract string generateHtml();
26 
27   public:
28   final:
29   /**
30   * Creates a new html element.
31   * Params:
32   *   tagName = The name of the tag.
33   */
34   this(string tagName)
35   {
36     _tagName = tagName;
37   }
38 
39   @property
40   {
41     /// Gets the tag name.
42     string tagName() { return _tagName; }
43 
44     /// Gets the inner text representation.
45     string inner() { return _inner; }
46 
47     /// Sets the inner text representation.
48     void inner(string newInner)
49     {
50       _inner = newInner;
51     }
52 
53     /// Gets the id of the element.
54     string id() { return _id; }
55 
56     /// Sets the id of the element.
57     void id(string newId)
58     {
59       _id = newId;
60     }
61 
62     /// Gets the name of the element.
63     string name() { return _name; }
64 
65     /// Sets the name of the element.
66     void name(string newName)
67     {
68       _name = newName;
69     }
70   }
71 
72   /**
73   * Adds an attribute to the html element.
74   * Params:
75   *   name =  The name of the attribute to add.
76   *   value = The value of the attribute.
77   */
78   void addAttribute(T)(string name, T value)
79   {
80     import std.conv : to;
81 
82     _attributes[name] = to!string(value);
83   }
84 
85   /**
86   * Gets an attribute from the html element.
87   * Params:
88   *   name =          The name of the attribute.
89   *   defaultValue =  The default value to return when the attribute wasn't found.
90   * Returns:
91   *   Returns the attribute's value if found, else the specified default value.
92   */
93   T getAttribute(T)(string name, T defaultValue = T.init)
94   {
95     auto value = _attributes.get(name, null);
96 
97     if (!value)
98     {
99       return defaultValue;
100     }
101 
102     return to!T(value);
103   }
104 
105   /// Gets the html representation of the element.
106   override string toString()
107   {
108     return generateHtml();
109   }
110 
111   /// Gets the attribute html.
112   protected string attributeHtml()
113   {
114     import std..string : format;
115     import std.array : join;
116 
117     string[] result;
118 
119     foreach (key,value; _attributes)
120     {
121       if (value)
122       {
123         result ~= "%s=\"%s\"".format(key,value);
124       }
125       else
126       {
127         result ~= key;
128       }
129     }
130 
131     return result ? result.join(" ") : "";
132   }
133 }