Home | History | Annotate | Download | only in html
      1 /*
      2  * Copyright (C) 2009 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 "DataGridColumn.h"
     31 #include "HTMLDataGridElement.h"
     32 #include "HTMLDataGridColElement.h"
     33 #include "HTMLNames.h"
     34 #include "MappedAttribute.h"
     35 #include "Text.h"
     36 
     37 namespace WebCore {
     38 
     39 using namespace HTMLNames;
     40 
     41 HTMLDataGridColElement::HTMLDataGridColElement(const QualifiedName& name, Document* doc)
     42     : HTMLElement(name, doc)
     43     , m_dataGrid(0)
     44 {
     45 }
     46 
     47 HTMLDataGridElement* HTMLDataGridColElement::findDataGridAncestor() const
     48 {
     49     if (parent() && parent()->hasTagName(datagridTag))
     50         return static_cast<HTMLDataGridElement*>(parent());
     51     return 0;
     52 }
     53 
     54 void HTMLDataGridColElement::ensureColumn()
     55 {
     56     if (m_column)
     57         return;
     58     m_column = DataGridColumn::create(getAttribute(idAttributeName()), label(), type(), primary(), sortable());
     59 }
     60 
     61 void HTMLDataGridColElement::insertedIntoTree(bool deep)
     62 {
     63     HTMLElement::insertedIntoTree(deep);
     64     if (dataGrid()) // We're connected to a datagrid already.
     65         return;
     66     m_dataGrid = findDataGridAncestor();
     67     if (dataGrid() && dataGrid()->dataSource()->isDOMDataGridDataSource()) {
     68         ensureColumn();
     69         m_dataGrid->columns()->add(column()); // FIXME: Deal with ordering issues (complicated, since columns can be made outside the DOM).
     70     }
     71 }
     72 
     73 void HTMLDataGridColElement::removedFromTree(bool deep)
     74 {
     75     HTMLElement::removedFromTree(deep);
     76     if (dataGrid() && dataGrid()->dataSource()->isDOMDataGridDataSource()) {
     77         HTMLDataGridElement* grid = findDataGridAncestor();
     78         if (!grid && column()) {
     79             dataGrid()->columns()->remove(column());
     80             m_dataGrid = 0;
     81         }
     82     }
     83 }
     84 
     85 String HTMLDataGridColElement::label() const
     86 {
     87     return getAttribute(labelAttr);
     88 }
     89 
     90 void HTMLDataGridColElement::setLabel(const String& label)
     91 {
     92     setAttribute(labelAttr, label);
     93 }
     94 
     95 String HTMLDataGridColElement::type() const
     96 {
     97     return getAttribute(typeAttr);
     98 }
     99 
    100 void HTMLDataGridColElement::setType(const String& type)
    101 {
    102     setAttribute(typeAttr, type);
    103 }
    104 
    105 unsigned short HTMLDataGridColElement::sortable() const
    106 {
    107     if (!hasAttribute(sortableAttr))
    108         return 2;
    109     return getAttribute(sortableAttr).toInt(0);
    110 }
    111 
    112 void HTMLDataGridColElement::setSortable(unsigned short sortable)
    113 {
    114     setAttribute(sortableAttr, String::number(sortable));
    115 }
    116 
    117 unsigned short HTMLDataGridColElement::sortDirection() const
    118 {
    119     String sortDirection = getAttribute(sortdirectionAttr);
    120     if (equalIgnoringCase(sortDirection, "ascending"))
    121         return 1;
    122     if (equalIgnoringCase(sortDirection, "descending"))
    123         return 2;
    124     return 0;
    125 }
    126 
    127 void HTMLDataGridColElement::setSortDirection(unsigned short sortDirection)
    128 {
    129     // FIXME: Check sortable rules.
    130     if (sortDirection == 0)
    131         setAttribute(sortdirectionAttr, "natural");
    132     else if (sortDirection == 1)
    133         setAttribute(sortdirectionAttr, "ascending");
    134     else if (sortDirection == 2)
    135         setAttribute(sortdirectionAttr, "descending");
    136 }
    137 
    138 bool HTMLDataGridColElement::primary() const
    139 {
    140     return hasAttribute(primaryAttr);
    141 }
    142 
    143 void HTMLDataGridColElement::setPrimary(bool primary)
    144 {
    145     setAttribute(primaryAttr, primary ? "" : 0);
    146 }
    147 
    148 void HTMLDataGridColElement::parseMappedAttribute(MappedAttribute* attr)
    149 {
    150     HTMLElement::parseMappedAttribute(attr);
    151 
    152     if (!column())
    153         return;
    154 
    155     if (attr->name() == labelAttr)
    156         column()->setLabel(label());
    157     else if (attr->name() == typeAttr)
    158         column()->setType(type());
    159     else if (attr->name() == primaryAttr)
    160         column()->setPrimary(primary());
    161     else if (attr->name() == sortableAttr)
    162         column()->setSortable(sortable());
    163     else if (attr->name() == sortdirectionAttr)
    164         column()->setSortDirection(sortDirection());
    165     else if (attr->name() == idAttributeName())
    166         column()->setId(getAttribute(idAttributeName()));
    167 }
    168 
    169 } // namespace WebCore
    170 
    171 #endif
    172