forked from riot/examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsearch.tag
executable file
·167 lines (151 loc) · 3.35 KB
/
search.tag
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
<search>
<form onsubmit={ search }>
<label>
<span>
Search a movie
</span>
<input
oninput={ search }
onchange={ search }
ref='s'
placeholder='28 Days Later..'
type='search' />
</label>
<form>
<div if={ isLoading } class='loader'>
<img src='puff.svg' />
</div>
<p class='error' if={ error }>{ error }</p>
<div if={ results.length }>
<ul>
<li each={ results }>
<a href="http://www.imdb.com/title/{ imdbID }" target="_blank">{ Title }</a>
<span>{ Year }</span>
</li>
</ul>
</div>
<script>
var lastSearch = null,
/**
* Reset tag attributes to hide the errors and cleaning the results list
*/
reset = function() {
this.results = []
this.error = false
}.bind(this)
/**
* Debounce the api requests
*/
doApiRequest = debounce(function(search) {
fetch('http://www.omdbapi.com/?s=' + search)
.then(function(res) {
return res.json()
})
.then(function(data) {
reset()
if (this.refs.s.value) {
if (data.Search) this.results = data.Search
else this.error = data.Error
}
this.isLoading = false
this.update()
}.bind(this))
}.bind(this), 300, false)
/**
* Public api/methods
*/
this.results = []
/**
* Search callback
*/
search(e) {
var search = this.refs.s.value
if (!search) {
reset()
} else if (lastSearch != search) {
reset()
this.isLoading = true
doApiRequest(search)
}
lastSearch = search
}
</script>
<style>
:scope {
position: absolute;
top: 2rem;
left: 50%;
transform: translate(-50%, 0);
}
.loader {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
.loader {
margin: 6rem 0 0;
}
.error {
color: #FFFAAA;
margin: 1rem 0;
}
label {
align-items: center;
display: flex;
flex-direction: column;
font-size: 1.6rem;
}
label span {
text-shadow: 1px 1px 2px rgba(0,0,0,0.8);
}
input {
margin: 1rem 0 0;
font-size: 1.6rem;
font-weight: 300;
padding: 0.8rem 1rem;
color: white;
border: 1px solid rgba(255, 255, 255, 0.05);
background: rgba(255, 255, 255, 0.05);
transition: all 0.3s;
box-shadow: 1px 1px 2px rgba(0,0,0, 0.3);
-moz-appearance:none;
-webkit-appearance:none;
outline: none;
}
input:focus {
border: 1px solid transparent;
background: rgba(255, 255, 255, 0.08);
}
ul {
padding: 0;
margin: 1rem 0 2rem;
}
ul li {
padding: 0.6rem 1rem;
margin: 1px 0;
line-height: 1.4rem;
display: flex;
justify-content: space-between;
align-items: center;
align-content: space-between;
text-shadow: 1px 1px 2px rgba(0,0,0,0.8);
background: rgba(255, 255, 255, 0.08);
box-shadow: 0 0 2px rgba(0,0,0, 0.3);
box-sizing: border-box;
}
ul li:hover,
ul li:active,
ul li:focus {
background: rgba(255, 255, 255, 0.1);
}
ul li a {
margin: 0 0.6rem 0 0;
text-decoration: none;
color: white;
}
ul li span {
opacity: 0.5;
}
</style>
</search>