~ubuntu-branches/ubuntu/maverick/webdeveloper/maverick-proposed

« back to all changes in this revision

Viewing changes to source/content_common/webdeveloper/generated/table_sort.js

  • Committer: Bazaar Package Importer
  • Author(s): Luca Falavigna
  • Date: 2009-01-04 12:25:00 UTC
  • mfrom: (1.1.5 upstream)
  • Revision ID: james.westby@ubuntu.com-20090104122500-agtub7j8tfr3s09t
Tags: 1.1.6+repack-1ubuntu1
* Merge from Debian experimental, remaining Ubuntu changes:
  + debian/control:
    - Build firefox-webdeveloper and adjust it for Firefox.
    - Do not conflict/replaces with firefox-developer.
    - Add Vcs-Bzr header.
  + debian/rules:
    - Adjust makebuilddir to build firefox-webdeveloper package.
  + Rename iceweasel-{webdeveloper.links,firefox-webdeveloper.install} to
    firefox-{webdeveloper.links,firefox-webdeveloper.install}

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
var webdeveloper_currentTableSortColumn  = 0;
 
2
var webdeveloper_previousTableSortColumn = 0;
 
3
var webdeveloper_sortAscending           = false;
 
4
 
 
5
// Makes all tables on the page with a sortable class sortable
 
6
function webdeveloper_makeTablesSortable()
 
7
{
 
8
    var tableHeaderLength = null;
 
9
    var tableHeaderList   = null;
 
10
    var tableList         = webdeveloper_evaluateXPath(document, "//table[@class='sortable']");
 
11
    var tableLength       = tableList.length;
 
12
 
 
13
    // Loop through the tables
 
14
    for(var i = 0; i < tableLength; i++)
 
15
    {
 
16
        tableHeaderList   = tableList[i].getElementsByTagName("th");
 
17
        tableHeaderLength = tableHeaderList.length;
 
18
 
 
19
        // Loop through the table headers
 
20
        for(var j = 0; j < tableHeaderLength; j++)
 
21
        {
 
22
            tableHeaderList[j].addEventListener("click", webdeveloper_sortTable, false);
 
23
        }
 
24
    }
 
25
}
 
26
 
 
27
// Sorts a table
 
28
function webdeveloper_sortTable(event)
 
29
{
 
30
    // If the event is set
 
31
    if(event)
 
32
    {
 
33
        var newTableRowList = new Array();
 
34
        var tableHeader     = event.target;
 
35
        var node            = tableHeader;
 
36
        var styleSheet      = document.getElementById("webdeveloper-generated-content-stylesheet");
 
37
        var tableHeaderRow  = tableHeader.parentNode;
 
38
        var table           = tableHeaderRow.parentNode;
 
39
        var tableRow        = null;
 
40
        var tableRowList    = table.childNodes;
 
41
        var tableRowLength  = tableRowList.length;
 
42
 
 
43
        webdeveloper_currentTableSortColumn = 0;
 
44
 
 
45
        // Loop through the previous siblings
 
46
        while((node = node.previousSibling) != null)
 
47
        {
 
48
            webdeveloper_currentTableSortColumn++;
 
49
        }
 
50
 
 
51
        // If the current sort column is the same as the previous sort column
 
52
        if(webdeveloper_currentTableSortColumn == webdeveloper_previousTableSortColumn)
 
53
        {
 
54
            webdeveloper_sortAscending = !webdeveloper_sortAscending;
 
55
        }
 
56
        else
 
57
        {
 
58
            var previousTableHeader = tableHeaderRow.cells[webdeveloper_previousTableSortColumn];
 
59
 
 
60
            webdeveloper_previousTableSortColumn = webdeveloper_currentTableSortColumn;
 
61
            webdeveloper_sortAscending           = true;
 
62
 
 
63
            // If the previous table header has a class attribute
 
64
            if(previousTableHeader.hasAttribute("class"))
 
65
            {
 
66
                previousTableHeader.removeAttribute("class");
 
67
            }
 
68
        }
 
69
 
 
70
        // If sorting in ascending order
 
71
        if(webdeveloper_sortAscending)
 
72
        {
 
73
            tableHeader.setAttribute("class", "sort-ascending");
 
74
        }
 
75
        else
 
76
        {
 
77
            tableHeader.setAttribute("class", "sort-descending");
 
78
        }
 
79
 
 
80
        // Loop through the table rows excluding the header row
 
81
        for(var i = 1; i < tableRowLength; i++)
 
82
        {
 
83
            newTableRowList[i - 1] = tableRowList[i];
 
84
        }
 
85
 
 
86
        newTableRowList.sort(webdeveloper_sortTableRows);
 
87
        webdeveloper_removeAllChildElements(table);
 
88
        table.appendChild(tableHeaderRow);
 
89
 
 
90
        // Loop through the sorted table rows
 
91
        for(i = 0; i < newTableRowList.length; i++)
 
92
        {
 
93
            tableRow = newTableRowList[i];
 
94
 
 
95
            // If this is an even row
 
96
            if(i % 2 == 0)
 
97
            {
 
98
                // If this row has a class attribute
 
99
                if(tableRow.hasAttribute("class"))
 
100
                {
 
101
                    tableRow.removeAttribute("class");
 
102
                }
 
103
            }
 
104
            else
 
105
            {
 
106
                tableRow.setAttribute("class", "shaded");
 
107
            }
 
108
 
 
109
            table.appendChild(tableRow);
 
110
        }
 
111
 
 
112
        // Disable and enable the style sheet to force the styles to reapply
 
113
        if(styleSheet)
 
114
        {
 
115
            styleSheet.disabled = true;
 
116
            styleSheet.disabled = false;
 
117
        }
 
118
    }
 
119
}
 
120
 
 
121
// Sorts two table rows
 
122
function webdeveloper_sortTableRows(rowOne, rowTwo)
 
123
{
 
124
    var sortValue = 1;
 
125
 
 
126
    // If row one and row two are set
 
127
    if(rowOne && rowTwo)
 
128
    {
 
129
        var columnOne = webdeveloper_getElementText(rowOne.cells[webdeveloper_currentTableSortColumn]);
 
130
        var columnTwo = webdeveloper_getElementText(rowTwo.cells[webdeveloper_currentTableSortColumn]);
 
131
 
 
132
        // If the columns are equal
 
133
        if(columnOne == columnTwo)
 
134
        {
 
135
            return 0;
 
136
        }
 
137
        else if(columnOne < columnTwo)
 
138
        {
 
139
            sortValue = -1;
 
140
        }
 
141
    }
 
142
 
 
143
    // If sorting in descending order
 
144
    if(!webdeveloper_sortAscending)
 
145
    {
 
146
        sortValue *= -1;
 
147
    }
 
148
 
 
149
    return sortValue;
 
150
}
 
151
 
 
152
webdeveloper_makeTablesSortable();
 
 
b'\\ No newline at end of file'