-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
200 lines (184 loc) · 8.88 KB
/
index.html
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<meta name="theme-color" content="#009688">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0-rc.2/css/materialize.min.css">
<link rel="stylesheet" href="main.css">
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.2.0/css/all.css" integrity="sha384-hWVjflwFxL6sNzntih27bfxkr27PmbbK/iSvJ+a4+0owXq79v+lsFkW54bOGbiDQ" crossorigin="anonymous">
<script src="https://cdn.jsdelivr.net/npm/vue@2.5.16/dist/vue.js"></script>
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
<title>Search Github API</title>
</head>
<body>
<div id="app">
<div class="container">
<nav v-if="show">
<div class="nav-wrapper teal">
<div class="col s12"><p>Found <span>{{total}}</span> {{ total == 1 ? 'repository' : 'repositories'}}</p></div>
</div>
</nav>
<div class="header">
<form v-on:submit="searchRepo">
<div class="input-field">
<input id="keyword" v-model="keyword" type="text" class="validate">
<label for="keyword"><i class="fab fa-github-alt"></i> search repository</label>
</div>
<button class="btn waves-effect waves-light" type="submit" name="action">Submit
<i class="material-icons right">send</i>
</button>
</form>
</div>
<div class="progress" v-if="load">
<div class="indeterminate"></div>
</div>
<ul v-if="show" class="collection">
<li v-for="repo in repos" class="collection-item avatar">
<div class="left-cont">
<img v-bind:src="''+repo.owner.avatar_url+''" alt="" class="circle"/>
<a v-bind:href="''+repo.url+''"><span class="title">{{repo.owner.login}}</span></a>
<p>{{repo.description}}</p>
</div>
<ul>
<li v-if="repo.language"><p v-bind:class="''+repo.language+''"><i class="fas fa-circle"></i><span>{{repo.language}}</span></p></li>
<li><p><i class="fas fa-star"></i><a v-bind:href="'https://github.com/'+repo.full_name+'/stargazers'" target="_blank">{{repo.stargazers_count}}</a></p></li>
<li v-if="repo.watchers" class="watch"><p><i class="fas fa-eye"></i><span>{{repo.watchers}}</span> </p></li>
<li class="date"><p><span class="new badge">Last updated {{repo.updated_at | formatDate}}</span></p></li>
</ul>
</li>
</ul>
<ul v-if="pagination" class="pagination">
<li v-if="page == 1 " class="disabled"><button v-on:click="prevPage()" disabled><i class="material-icons">chevron_left</i></button></li>
<li v-if="page != 1 " class="waves-effect"><button v-on:click="prevPage()"><i class="material-icons">chevron_left</i></button></li>
<!-- Pagination loop -->
<li v-for="n in maxPage" :key="n" v-bind:class="{'waves-effect': (page != n),'active': (page === n)}" :key="n"><button v-on:click="repoPage(n)">{{n}}</button></li>
<!-- <li v-if="page < 10 " v-for="n in 10" v-bind:class="{'waves-effect': (page != n),'active': (page === n)}"><button v-on:click="repoPage(n)">{{n}}</button></li> -->
<li v-if="page != total " class="waves-effect"><button v-on:click="nextPage()"><i class="material-icons">chevron_right</i></button></li>
<li v-if="page == total " class="disabled"><button v-on:click="nextPage()" disabled><i class="material-icons">chevron_right</i></button></li>
</ul>
</div>
</div>
<script>
Vue.filter('formatDate', function(value) {
if (value) {
return moment(String(value)).startOf('hour').fromNow()
}
});
var vm = new Vue({
el: '#app',
data: {
status: '',
keyword: '',
total: '',
repos: [],
show: false,
pagination: false,
error: false,
load: false,
page: 1,
n: 0,
maxPage: 10
},
methods: {
searchRepo: function(event) {
if(event){
event.preventDefault()
this.page = 1
}
var vm = this
var searchWord = this.keyword
var page = this.page
console.log('Current Page: '+page)
var query = 'https://api.github.com/search/repositories?sort=stars&per_page=10&page='+page+'&q='
vm.load = true
axios.get(query + searchWord )
.then(function (response) {
var total = response.data.total_count
var pages = Math.round(response.data.total_count/10)
vm.total = total
vm.repos = response.data.items
// console.log(pages)
// console.log(vm.repos)
if( total == 0) {
vm.error= true
vm.show = false
vm.pagination = false
vm.load = false
vm.keyword = ''
var toastHTML = '<span>Repository not found</span><button class="btn-flat toast-action"><i class="fas fa-exclamation-circle"></i></button>';
M.toast({html: toastHTML});
}else{
vm.error= false
vm.show = true
vm.load = false
vm.pagination = true
}
if(total <= 10){
vm.pagination = false
}
})
.catch(function (error) {
vm.status = 'An error occured.' + error;
})
},
repoPage: function(arg) {
this.page = arg
this.searchRepo()
this.backToTop()
},
nextPage: function() {
this.page = this.page + 1
this.searchRepo()
this.backToTop()
this.incrementPagination()
},
prevPage: function() {
vm.page = this.page - 1
this.searchRepo()
this.backToTop()
if(this.page >= 10){
this.decreasePagination()
}
},
backToTop: function() {
setTimeout(
function()
{
$('html,body').stop().animate({
scrollTop: 0
}, 1500, 'swing');
}, 500);
},
incrementPagination: function() {
if(this.page > this.maxPage) {
vm.n = this.n + 11
this.maxPage = this.maxPage + 10
// vm.n = 10
}else {
vm.n = 0
}
console.log('n: '+this.n)
console.log('Max page: '+this.maxPage)
},
decreasePagination: function() {
if(this.page < this.maxPage) {
vm.n = this.n - 11
this.maxPage = this.maxPage - 10
// vm.n = 10
}else {
vm.n = 0
}
console.log('n: '+this.n)
console.log('Max page: '+this.maxPage)
}
}
});
</script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0-rc.2/js/materialize.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.12.0/moment.min.js"></script>
<link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
</body>
</html>