Project

General

Profile

1
var sorting = null;
2

    
3
window.addEvent('domready', function () {
4
	var repositories = $$('tbody tr');
5
	var filter = $('filter');
6
	sorting = new Fx.Sort(repositories, {duration: 0});
7
	var nameSorting = $('nameSorting');
8
	var countrySorting = $('countrySorting');
9
	var sizeSorting = $('sizeSorting');
10
	var institutionSorting = $('institutionSorting');
11
	var compatibilitySorting = $('compatibilitySorting');
12
	var checks = $$('input.checkfilter');
13
	
14
	//checkboxes
15
	checks.addEvent('click', filtering);
16

    
17
	repositories.each(function (repository) {
18
		repository.nameContainer = repository.getElement('td.name a');
19
		repository.nameValue = repository.nameContainer.get('html');
20
		repository.lowerCaseNameValue = repository.nameValue.toLowerCase();
21
		repository.countryContainer = repository.getElement('td.country');
22
		repository.countryValue = repository.countryContainer.get('html');
23
		repository.lowerCaseCountryValue = repository.countryValue.toLowerCase();
24
		repository.sizeContainer = repository.getElement('td.size');
25
		repository.sizeValue = repository.sizeContainer.get('html');
26
		repository.lowerCaseSizeValue = repository.sizeValue.toLowerCase();
27
		repository.numericSizeValue = (repository.sizeContainer.getElement('strong') == null) ? 0 : parseInt(repository.sizeContainer.getElement('strong').get('html'));
28
		repository.institutionContainer = repository.getElement('td.institution');
29
		repository.institutionValue = repository.institutionContainer.get('html');
30
		repository.lowerCaseInstitutionValue = repository.institutionValue.toLowerCase();
31
		repository.compatibilityContainer = repository.getElement('td.compatibility span');
32
		repository.compatibilityValue = repository.compatibilityContainer.get('html');
33
		repository.lowerCaseCompatibilityValue = repository.compatibilityValue.toLowerCase();
34
	});
35
	// filtering
36
	filter.addEvent('keyup', filtering);
37

    
38
	// sorting
39
	sorting.ascending = true;
40
	sorting.currentSorting = null;
41
	nameSorting.addEvent('click', function () {
42
		sorting.ascending = (sorting.currentSorting == nameSorting) ? (!sorting.ascending) : true;
43
		sorting.currentSorting = nameSorting;
44
		var oldOrder = repositories.clone();
45
		repositories.sort(function (repository1, repository2) {
46
			return ((repository1.lowerCaseNameValue < repository2.lowerCaseNameValue) ? -1 : ((repository1.lowerCaseNameValue > repository2.lowerCaseNameValue) ? 1 : 0)) * (sorting.ascending ? 1 : -1);
47
		});
48
		var newOrder = new Array();
49
		for (var i = 0; i < repositories.length; i++)
50
			newOrder[i] = oldOrder.indexOf(repositories[i]);
51
		sorting.rearrangeDOM(newOrder);
52
		return false;
53
	});
54
	countrySorting.addEvent('click', function () {
55
		sorting.ascending = (sorting.currentSorting == countrySorting) ? (!sorting.ascending) : true;
56
		sorting.currentSorting = countrySorting;
57
		var oldOrder = repositories.clone();
58
		repositories.sort(function (repository1, repository2) {
59
			return ((repository1.lowerCaseCountryValue < repository2.lowerCaseCountryValue) ? -1 : ((repository1.lowerCaseCountryValue > repository2.lowerCaseCountryValue) ? 1 : 0)) * (sorting.ascending ? 1 : -1);
60
		});
61
		var newOrder = new Array();
62
		for (var i = 0; i < repositories.length; i++)
63
			newOrder[i] = oldOrder.indexOf(repositories[i]);
64
		sorting.rearrangeDOM(newOrder);
65
		return false;
66
	});
67
	sizeSorting.addEvent('click', function () {
68
		sorting.ascending = (sorting.currentSorting == sizeSorting) ? (!sorting.ascending) : true;
69
		sorting.currentSorting = sizeSorting;
70
		var oldOrder = repositories.clone();
71
		repositories.sort(function (repository1, repository2) {
72
			return (repository1.numericSizeValue - repository2.numericSizeValue) * (sorting.ascending ? 1 : -1);
73
		});
74
		var newOrder = new Array();
75
		for (var i = 0; i < repositories.length; i++)
76
			newOrder[i] = oldOrder.indexOf(repositories[i]);
77
		sorting.rearrangeDOM(newOrder);
78
		return false;
79
	});
80
	institutionSorting.addEvent('click', function () {
81
		sorting.ascending = (sorting.currentSorting == institutionSorting) ? (!sorting.ascending) : true;
82
		sorting.currentSorting = institutionSorting;
83
		var oldOrder = repositories.clone();
84
		repositories.sort(function (repository1, repository2) {
85
			return ((repository1.lowerCaseInstitutionValue < repository2.lowerCaseInstitutionValue) ? -1 : ((repository1.lowerCaseInstitutionValue > repository2.lowerCaseInstitutionValue) ? 1 : 0)) * (sorting.ascending ? 1 : -1);
86
		});
87
		var newOrder = new Array();
88
		for (var i = 0; i < repositories.length; i++)
89
			newOrder[i] = oldOrder.indexOf(repositories[i]);
90
		sorting.rearrangeDOM(newOrder);
91
		return false;
92
	});
93
	compatibilitySorting.addEvent('click', function () {
94
		sorting.ascending = (sorting.currentSorting == compatibilitySorting) ? (!sorting.ascending) : true;
95
		sorting.currentSorting = compatibilitySorting;
96
		var oldOrder = repositories.clone();
97
		repositories.sort(function (repository1, repository2) {
98
			return ((repository1.lowerCaseCompatibilityValue < repository2.lowerCaseCompatibilityValue) ? -1 : ((repository1.lowerCaseCompatibilityValue > repository2.lowerCaseCompatibilityValue) ? 1 : 0)) * (sorting.ascending ? 1 : -1);
99
		});
100
		var newOrder = new Array();
101
		for (var i = 0; i < repositories.length; i++)
102
			newOrder[i] = oldOrder.indexOf(repositories[i]);
103
		sorting.rearrangeDOM(newOrder);
104
		return false;
105
	});
106
	nameSorting.click();
107
});
108

    
109
function filtering() {
110
	var filter = $('filter');
111
	var term = filter.value.trim().toLowerCase();
112
	var checks = $$('input.checkfilter');
113
	var checked = new Array();
114
	for (var i = 0; i < checks.length; i++) {
115
		if (checks[i].checked)
116
			checked.push(checks[i]);
117
	}
118
	var checkedValues = checked.map(function(el){return $(el).get('value')});
119
	var repositories = $$('tbody tr');
120
	if (term == '') {
121
		repositories.each(function (repository) {
122
			repository.nameContainer.set('html', repository.nameValue);
123
			repository.countryContainer.set('html', repository.countryValue);
124
			repository.sizeContainer.set('html', repository.sizeValue);
125
			repository.institutionContainer.set('html', repository.institutionValue);
126
			repository.compatibilityContainer.set('html', repository.compatibilityValue);
127
			//check its type and if it is in the checked values or there are no checked values then show it
128
			if(checkedValues.length == 0 || checkedValues.indexOf(repository.getElement('input').get('value')) != -1)
129
				repository.show();
130
			else
131
				repository.hide();
132
		});
133
	} else {
134
		repositories.each(function (repository) {
135
			var nameIndex = repository.lowerCaseNameValue.indexOf(term);
136
			repository.nameContainer.set('html', (nameIndex == -1) ? repository.nameValue : (repository.nameValue.substr(0, nameIndex) + '<span class="filterMatch">' + repository.nameValue.substr(nameIndex, term.length) + '</span>' + repository.nameValue.substr(nameIndex + term.length)));
137
			var countryIndex = repository.lowerCaseCountryValue.indexOf(term);
138
			repository.countryContainer.set('html', (countryIndex == -1) ? repository.countryValue : (repository.countryValue.substr(0, countryIndex) + '<span class="filterMatch">' + repository.countryValue.substr(countryIndex, term.length) + '</span>' + repository.countryValue.substr(countryIndex + term.length)));
139
			var sizeIndex = repository.lowerCaseSizeValue.indexOf(term);
140
			repository.sizeContainer.set('html', (sizeIndex == -1) ? repository.sizeValue : (repository.sizeValue.substr(0, sizeIndex) + '<span class="filterMatch">' + repository.sizeValue.substr(sizeIndex, term.length) + '</span>' + repository.sizeValue.substr(sizeIndex + term.length)));
141
			var institutionIndex = repository.lowerCaseInstitutionValue.indexOf(term);
142
			repository.institutionContainer.set('html', (institutionIndex == -1) ? repository.institutionValue : (repository.institutionValue.substr(0, institutionIndex) + '<span class="filterMatch">' + repository.institutionValue.substr(institutionIndex, term.length) + '</span>' + repository.institutionValue.substr(institutionIndex + term.length)));
143
			var compatibilityIndex = repository.lowerCaseCompatibilityValue.indexOf(term);
144
			repository.compatibilityContainer.set('html', (compatibilityIndex == -1) ? repository.compatibilityValue : (repository.compatibilityValue.substr(0, compatibilityIndex) + '<span class="filterMatch">' + repository.compatibilityValue.substr(compatibilityIndex, term.length) + '</span>' + repository.compatibilityValue.substr(compatibilityIndex + term.length)));
145
			if ((nameIndex == -1) && (countryIndex == -1) && (sizeIndex == -1) && (institutionIndex == -1) && (compatibilityIndex == -1))
146
				repository.hide();
147
			else
148
				//check its type and if it is in the checked values or there are no checked values then show it
149
				if(checkedValues.length == 0 || checkedValues.indexOf(repository.getElement('input').get('value')) != -1)
150
					repository.show();
151
				else
152
					repository.hide();
153
		});
154
	}
155
	sorting.rearrangeDOM();
156
}
157

    
(25-25/31)