1 /*
2 * Copyright (C) 2009, 2010 Apple Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26 #include "config.h"
27
28 #if ENABLE(DATAGRID)
29
30 #include "Attribute.h"
31 #include "DataGridColumn.h"
32 #include "HTMLDataGridColElement.h"
33 #include "HTMLDataGridElement.h"
34 #include "HTMLNames.h"
35 #include "Text.h"
36
37 namespace WebCore {
38
39 using namespace HTMLNames;
40
HTMLDataGridColElement(const QualifiedName & name,Document * document)41 inline HTMLDataGridColElement::HTMLDataGridColElement(const QualifiedName& name, Document* document)
42 : HTMLElement(name, document)
43 , m_dataGrid(0)
44 {
45 }
46
create(const QualifiedName & name,Document * document)47 PassRefPtr<HTMLDataGridColElement> HTMLDataGridColElement::create(const QualifiedName& name, Document* document)
48 {
49 return adoptRef(new HTMLDataGridColElement(name, document));
50 }
51
findDataGridAncestor() const52 HTMLDataGridElement* HTMLDataGridColElement::findDataGridAncestor() const
53 {
54 if (parent() && parent()->hasTagName(datagridTag))
55 return static_cast<HTMLDataGridElement*>(parent());
56 return 0;
57 }
58
ensureColumn()59 void HTMLDataGridColElement::ensureColumn()
60 {
61 if (m_column)
62 return;
63 m_column = DataGridColumn::create(getIdAttribute(), label(), type(), primary(), sortable());
64 }
65
insertedIntoTree(bool deep)66 void HTMLDataGridColElement::insertedIntoTree(bool deep)
67 {
68 HTMLElement::insertedIntoTree(deep);
69 if (dataGrid()) // We're connected to a datagrid already.
70 return;
71 m_dataGrid = findDataGridAncestor();
72 if (dataGrid() && dataGrid()->dataSource()->isDOMDataGridDataSource()) {
73 ensureColumn();
74 m_dataGrid->columns()->add(column()); // FIXME: Deal with ordering issues (complicated, since columns can be made outside the DOM).
75 }
76 }
77
removedFromTree(bool deep)78 void HTMLDataGridColElement::removedFromTree(bool deep)
79 {
80 HTMLElement::removedFromTree(deep);
81 if (dataGrid() && dataGrid()->dataSource()->isDOMDataGridDataSource()) {
82 HTMLDataGridElement* grid = findDataGridAncestor();
83 if (!grid && column()) {
84 dataGrid()->columns()->remove(column());
85 m_dataGrid = 0;
86 }
87 }
88 }
89
label() const90 String HTMLDataGridColElement::label() const
91 {
92 return getAttribute(labelAttr);
93 }
94
setLabel(const String & label)95 void HTMLDataGridColElement::setLabel(const String& label)
96 {
97 setAttribute(labelAttr, label);
98 }
99
type() const100 String HTMLDataGridColElement::type() const
101 {
102 return getAttribute(typeAttr);
103 }
104
setType(const String & type)105 void HTMLDataGridColElement::setType(const String& type)
106 {
107 setAttribute(typeAttr, type);
108 }
109
sortable() const110 unsigned short HTMLDataGridColElement::sortable() const
111 {
112 if (!hasAttribute(sortableAttr))
113 return 2;
114 return getAttribute(sortableAttr).toInt(0);
115 }
116
setSortable(unsigned short sortable)117 void HTMLDataGridColElement::setSortable(unsigned short sortable)
118 {
119 setAttribute(sortableAttr, String::number(sortable));
120 }
121
sortDirection() const122 unsigned short HTMLDataGridColElement::sortDirection() const
123 {
124 String sortDirection = getAttribute(sortdirectionAttr);
125 if (equalIgnoringCase(sortDirection, "ascending"))
126 return 1;
127 if (equalIgnoringCase(sortDirection, "descending"))
128 return 2;
129 return 0;
130 }
131
setSortDirection(unsigned short sortDirection)132 void HTMLDataGridColElement::setSortDirection(unsigned short sortDirection)
133 {
134 // FIXME: Check sortable rules.
135 if (sortDirection == 0)
136 setAttribute(sortdirectionAttr, "natural");
137 else if (sortDirection == 1)
138 setAttribute(sortdirectionAttr, "ascending");
139 else if (sortDirection == 2)
140 setAttribute(sortdirectionAttr, "descending");
141 }
142
primary() const143 bool HTMLDataGridColElement::primary() const
144 {
145 return hasAttribute(primaryAttr);
146 }
147
setPrimary(bool primary)148 void HTMLDataGridColElement::setPrimary(bool primary)
149 {
150 setAttribute(primaryAttr, primary ? "" : 0);
151 }
152
parseMappedAttribute(Attribute * attr)153 void HTMLDataGridColElement::parseMappedAttribute(Attribute* attr)
154 {
155 HTMLElement::parseMappedAttribute(attr);
156
157 if (!column())
158 return;
159
160 if (attr->name() == labelAttr)
161 column()->setLabel(label());
162 else if (attr->name() == typeAttr)
163 column()->setType(type());
164 else if (attr->name() == primaryAttr)
165 column()->setPrimary(primary());
166 else if (attr->name() == sortableAttr)
167 column()->setSortable(sortable());
168 else if (attr->name() == sortdirectionAttr)
169 column()->setSortDirection(sortDirection());
170 else if (isIdAttributeName(attr->name()))
171 column()->setId(getIdAttribute());
172 }
173
174 } // namespace WebCore
175
176 #endif
177