forked from naver/egjs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
infiniteGridService.html
245 lines (228 loc) · 8.83 KB
/
infiniteGridService.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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css"/>
<style>
.cf:before,.cf:after{content:" ";display:table;}
.cf:after{clear:both;}
</style>
</head>
<body>
<nav class="navbar navbar-default">
<div class="container-fluid">
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1" aria-expanded="false">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="#">InfiniteGridService</a>
</div>
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
<ul class="nav navbar-nav">
<li class="active"><a href="#">Link <span class="sr-only">(current)</span></a></li>
<li><a href="#">Link</a></li>
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">Dropdown <span class="caret"></span></a>
<ul class="dropdown-menu">
<li><a href="#">Action</a></li>
<li><a href="#">Another action</a></li>
<li><a href="#">Something else here</a></li>
<li role="separator" class="divider"></li>
<li><a href="#">Separated link</a></li>
<li role="separator" class="divider"></li>
<li><a href="#">One more separated link</a></li>
</ul>
</li>
</ul>
<form class="navbar-form navbar-left" role="search">
<div class="form-group">
<input type="text" class="form-control" placeholder="Search">
</div>
<button type="submit" class="btn btn-default">Submit</button>
</form>
<ul class="nav navbar-nav navbar-right">
<li><a href="#">Link</a></li>
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">Dropdown <span class="caret"></span></a>
<ul class="dropdown-menu">
<li><a href="#">Action</a></li>
<li><a href="#">Another action</a></li>
<li><a href="#">Something else here</a></li>
<li role="separator" class="divider"></li>
<li><a href="#">Separated link</a></li>
</ul>
</li>
</ul>
</div><!-- /.navbar-collapse -->
</div><!-- /.container-fluid -->
</nav>
<div class="jumbotron">
<div class="container">
<h1>Hello, InfiniteGridService!</h1>
<p>This is a simple hero unit, a simple jumbotron-style component for calling extra attention to featured content or information.</p>
</div>
</div>
<div class="container">
<div class="row">
<div id="prepend-loading-bar" class="alert alert-info" role="alert" style="display:none"><span class="glyphicon glyphicon-refresh" aria-hidden="true"> prepending...</span></div>
<div id="grid" class="col-md-12"></div>
<div class="cf"></div>
<div id="append-loading-bar" class="alert alert-info" role="alert" style="display:none"><span class="glyphicon glyphicon-refresh" aria-hidden="true"> appending...</span></div>
</div>
</div>
<script src="../bower_components/jquery/jquery.js"></script>
<script src="../bower_components/get-style-property/get-style-property.js"></script>
<script src="../bower_components/get-size/get-size.js"></script>
<script src="../bower_components/matches-selector/matches-selector.js"></script>
<script src="../bower_components/eventEmitter/EventEmitter.js"></script>
<script src="../bower_components/eventie/eventie.js"></script>
<script src="../bower_components/doc-ready/doc-ready.js"></script>
<script src="../bower_components/fizzy-ui-utils/utils.js"></script>
<script src="../bower_components/outlayer/item.js"></script>
<script src="../bower_components/outlayer/outlayer.js"></script>
<script src="../src/module.js"></script>
<script src="../src/eg.js"></script>
<script src="../src/class.js"></script>
<script src="../src/component.js"></script>
<script src="../src/plugin/persist.js"></script>
<script src="../src/customEvent/scrollEnd.js"></script>
<script src="../src/infiniteGrid.js"></script>
<script src="../src/infiniteGridService.js"></script>
<script>
function getContents(offset, limit) {
var html = "";
var colors = ["63a69f", "f2e1ac", "f2836b", "f2594b", "CD2C24"];
offset = offset || 1;
limit = limit || 80;
var end = offset + limit;
var _random = function(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
for ( ; offset < end; offset ++ ) {
html = html +
'<div class="col-sm-6 col-md-3" data-offset="' + offset + '">' +
'<div class="thumbnail">' +
'<img src="http://dummyimage.com/' + _random(128, 182) + 'x1:1.65/' + colors[_random(0, colors.length - 1)] + '&text=++0x23' + offset + '++" alt="' + offset + '">' +
'<div class="caption">' +
'<h3>Thumbnail label</h3>' +
'<p>...</p>' +
'<p><a href="http://naver.com/" class="btn btn-primary" role="button">더보기</a>' +
'</div>' +
'</div>' +
'</div>';
}
return html;
};
/**
* Grid 내부 아이템의 offset 인덱스 리턴
* @param {String} pos "first"일 경우 첫 아이템의 data-offset 리턴. "last"일 경우 마지막 아이템의 data-offset 리턴
* @return {Number} offset
*/
function getOffset(pos) {
var offset;
if (pos === "first") {
offset = $("#grid").children("div").first().data("offset");
} else if (pos === "last") {
offset = $("#grid").children("div").last().data("offset");
} else
offset = parseInt(offset, 10);
return offset;
};
var contents;
var limit = 80;
var SERVER_URL = "http://fakeserver-dev-auplatform-1.c9.io/infiniteGrid/items";
/*
eg.InfiniteGrideService 인스턴스 생성
InfiniteGridService 대상인 엘리먼트 셀렉터 지정
옵션 설정
*/
var infiniteGridService = new eg.InfiniteGridService("#grid");
/*
"append" 이벤트 핸들러
스크롤 다운 중 최하단 아이템이 화면에 보여질 때(threshold로 보정 가능)
threshold가 300이면 최하단 아이템이 화면에 보여지기 300px 전에 이벤트 발생
로딩바를 보이고 appendAjax 메소드를 호출해 서버에 데이터를 요청함.
응답데이터가 #grid에 append 됨
*/
infiniteGridService.on("append", function() {
var offset = getOffset("last");
$("#append-loading-bar").show();
// var url = SERVER_URL + "?offset=" + (offset + 1) + "&limit=" + limit;
// infiniteGridService.appendAjax(url);
// var options = {
// url: url,
// jsonp: "callback"
// };
// infiniteGridService.appendAjax(options);
var contents = getContents(offset, limit);
infiniteGridService.append(contents);
});
/*
"preppend" 이벤트 핸들러
스크롤 업 중 최상단 아이템이 화면에 보여질 때(threshold로 보정 가능)
threshold가 300이면 최하단 아이템이 화면에 보여지기 300px 전에 이벤트 발생
로딩바를 보이고 prependAjax 메소드를 호출해 서버에 데이터를 요청
응답데이터가 #grid에 preppend 되고 스크롤 위치가 보정
*/
infiniteGridService.on("prepend", function() {
var offset = getOffset("first");
offset = offset - limit;
if( offset > 0) {
$("#prepend-loading-bar").show();
// var url = SERVER_URL + "?offset=" + offset + "&limit=" + limit;
// infiniteGridService.prependAjax(url, function(data) {
// return $(data);
// });
// var options = {
// url: url,
// jsonp: "callback"
// };
// infiniteGridService.prependAjax(options, function(data) {
// return $(data);
// });
var contents = getContents(offset, limit);
infiniteGridService.append(contents);
}
});
/*
"layoutComplete" 이벤트 핸들러
append, preppend가 완료되면 발생
이벤트 파라미터를 통해 append인지 prepend인지 확인 가능
로딩바 감춤
*/
infiniteGridService.on("layoutComplete", function(e) {
if(e.isAppend) {
$("#append-loading-bar").hide();
} else {
$("#prepend-loading-bar").hide();
}
});
/*
아이템의 <a> 클릭시 persist 데이터를 저장
*/
$("#grid").on("click", "a", function() {
infiniteGridService.store();
});
/*
persist 데이터를 복원함
복원하지 못한다면 새로운 데이터를 요청함
*/
if (!infiniteGridService.restore()) {
// var url = SERVER_URL + "?offset=1&limit=" + limit;
// infiniteGridService.appendAjax(url);
// var options = {
// url: url,
// jsonp: "callback"
// };
// infiniteGridService.appendAjax(options);
var contents = getContents(1, limit);
infiniteGridService.append(contents);
}
</script>
</body>
</html>