-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
129 lines (100 loc) · 2.74 KB
/
main.js
File metadata and controls
129 lines (100 loc) · 2.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
//Array of apps on Github
let apps_url =
[
"nextcloud/calendar",
"nextcloud/contacts",
"nextcloud/deck",
"nextcloud/news",
"nextcloud/notes",
"nextcloud/passman",
"nextcloud/polls",
"nextcloud/spreed",
]
$(function(){
//Fetch the data from github
(function(){
//For each app
apps_url.forEach(function(element){
//Generate the URL
let URL = "https://api.github.com/repos/" + element;
//Make the ajax request
$.ajax(URL)
.done(function(data) {
//Add the data to the table object
table.apps.push(data);
//Sort by name
table.sort("name",true);
})
})
})()
//Sorting when there is a click on a header in the table
$("th").click(function(e){
//Get the name of the header to sort by
let name = e.target.innerHTML;
//Match the header text with the correct selector for the data object
let selector = "";
if(name == "Name") selector="name";
else if (name == "Stars") selector = "stargazers_count";
else if (name == "Watchers") selector = "subscribers_count";
//Sort the table according to the selector
table.sort(selector);
})
let table = new Vue({
el: '#table',
data: {
//Array containing the info from Github
apps: [],
//Value the array is currently sorted by
sort_by: "name",
//Which direction the array is sorted
descending: true
},
methods:{
//Make first letter in string upper case
toUpper: function(s){
return s.charAt(0).toUpperCase()+ s.slice(1);
},
//Function for creating the link to Github
getURL:function(app){
return "https://github.com/"+ app.full_name;
},
//Function for sorting the table
sort:function(sort_by,ascending){
//If we are asked to sort by the same thing as last time
//we should change he direction.
//Else the direction should be ascending
if(this.sort_by===sort_by){
this.ascending= !this.ascending;
}
else this.ascending = true;
//If the parameter is parsed, we should honor it
if(ascending!==undefined){
this.ascending=ascending;
}
//Create an variable to alter the direction
let direction_factor = 1;
//If we sort by name, we have to change it to make it correct
if(sort_by==="name") direction_factor *= -1;
//If the direction is not ascending, we change the direction factor
if(!this.ascending) direction_factor *= -1;
//Update the state
this.sort_by=sort_by;
//Sort the array
this.apps.sort(function(e1,e2){
//Rename to selector for convenience
let selector = sort_by;
//Do the comparison
if(e1[selector]<e2[selector]){
return 1*direction_factor;
}
else if(e1[selector]>e2[selector]){
return -1*direction_factor;
}
else{
return 0;
}
})
}
}
})
})