gaga.infinite.scrollSession.js 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. /*
  2. * Inifinite Scroll Java Script written by gagamel.
  3. *
  4. * Copyright (c) 2016 gagamel (nogdoo.com)
  5. * Dual licensed under the MIT (MIT-LICENSE.txt)
  6. * and GPL (GPL-LICENSE.txt) licenses.
  7. *
  8. * $Date: 2016-04-28 $
  9. *
  10. * 사용 예)
  11. * // HTML 태그는 다음과 같은 구조로 되어 있어야 하며
  12. * // id 명칭은 infiniteContainer, listBoxOuter, listBox 가 필요하다.
  13. * <section id="infiniteContainer">
  14. * <div id="listBoxOuter">
  15. * <ul id="listBox">
  16. * </ul>
  17. * </div>
  18. * </section>
  19. *
  20. * // JQUERY History 파일과 인피니트 스크롤 관련 자바스크립트 파일을 import 되어 있어야 한다.
  21. * <script type="text/javascript" src="/ux/plugins/jquery.history.min.js"></script>
  22. * <script type="text/javascript" src="/ux/plugins/gaga.infinite.scroll.js"></script>
  23. *
  24. * <script type="text/javascript">
  25. * // 인피니트 스크롤에 대한 History 정보를 가져오기 위해 함수를 호출한다.
  26. * // History 정보가 없을 경우 fnGetInfiniteScrollDataList 함수가 호출된다.
  27. * $(function() { gagaInfiniteScroll.getHistory(); });
  28. *
  29. * // 인피니트 스크롤 이벤트 발생 시 데이터 가져오기
  30. * var fnGetInfiniteScrollDataList = function(pageNum) {
  31. * // 콜백함수인 gagaInfiniteScroll.jsonToHtml 에서는 fnDrawInfiniteScrollData 함수를 호출한다.
  32. * gagajf.ajaxSubmitForm(document.searchForm, "/goods/list", "json", gagaInfiniteScroll.jsonToHtml);
  33. * }
  34. *
  35. * // 인피니트 스크롤 이벤트 발생 시 가져온 데이터를 특정 태그에 append
  36. * // fnDrawInfiniteScrollData(result, pageNum) 함수 구현
  37. * // 이 함수 내에서의 로직은
  38. * // 1. 가져온 데이터로 HTML 태그를 만든다.
  39. * // 2. 만든 태그를 append 하는 gagaInfiniteScroll.draw() 함수를 호출
  40. * </script>
  41. */
  42. var gagaInfiniteScroll = {
  43. pageStatus : {
  44. pageNum : [] // [0,1,2...] 로드된 페이지 (Array)
  45. , loadPage : 0 // 로드할 페이지
  46. , loadAlign : 'not' // 로드 상태(prev, next, not)
  47. , historyScroll : 0 //
  48. , nowPage : null // 현재 페이지
  49. , pageUrl : { // page url
  50. }
  51. , sortingType : ''
  52. , sortingTypeNm : ''
  53. , filterHtml : ''
  54. , filterStatHtml : ''
  55. , totalCount : ''
  56. , backScroll : ''
  57. },
  58. obj : {
  59. $ajaxBoxOuter : $('#listBoxOuter')
  60. , $ajaxBox : $('#listBox')
  61. , $window : $(window)
  62. , $loadingBar : '<div style="width: 100%; padding-top: 50px; text-align: center;" id="loadingBar"><img src="/ux/plugins/gaga/loader.gif" style="width:25px; height:25px;"/></div>'
  63. // , $loadingBar : '<div style="width: 100%; padding-top: 50px; text-align: center;" id="loadingBar"><img src="/images/hsmob/common/loading_bar_01.gif" width="24px" height=" 22px"/></div>'
  64. },
  65. scrollCheck : function() {
  66. var obj = this.obj, stat = this.pageStatus;
  67. if (obj.$window.scrollTop() >= obj.$ajaxBox.offset().top + obj.$ajaxBox.height() - obj.$window.height()) { // 더보기
  68. if (stat.loadAlign == 'not') {
  69. var pageLen = this.pageStatus.pageNum.length - 1;
  70. var nextPageNum = this.pageStatus.pageNum[pageLen] + 1;
  71. this.pageStatus.pageNum.push(nextPageNum);
  72. this.pageStatus.loadPage = nextPageNum;
  73. stat.loadAlign = 'next';
  74. this.getDataList();
  75. };
  76. }
  77. },
  78. getDataList : function() {
  79. /*if (this.pageStatus.loadAlign == 'prev') {
  80. this.obj.$ajaxBoxOuter.prepend($(this.obj.$loadingBar));
  81. } else if (this.pageStatus.loadAlign == 'next') {
  82. this.obj.$ajaxBoxOuter.append($(this.obj.$loadingBar));
  83. };*/
  84. $(".wrap-loading").removeClass('display-none');
  85. // 인피니트 스크롤 이벤트에서 호출될 함수(데이터 가져오기 등)
  86. // fnGetInfiniteScrollDataList 함수는 개발단에서 구현해야 한다.
  87. fnGetInfiniteScrollDataList(this.pageStatus.loadPage);
  88. },
  89. getHistory : function() {
  90. var historyData = sessionStorage.getItem(document.location.href);
  91. if(historyData!=null){
  92. historyData = JSON.parse(historyData);
  93. }else{
  94. historyData = {};
  95. }
  96. if (historyData.pageNum === undefined || historyData.pageNum === '' || historyData.htm === undefined || historyData.htm == '') {
  97. this.pageStatus.loadPage = 0;
  98. this.pageStatus.pageNum[0] = 0;
  99. this.pageStatus.loadAlign = 'next';
  100. this.pageStatus.historyScroll = 0;
  101. this.getDataList();
  102. } else {
  103. if( history.scrollRestoration ) window.history.scrollRestoration = 'manual';
  104. this.pageStatus.loadPage = historyData.pageNum;
  105. this.pageStatus.pageNum[0] = historyData.pageNum;
  106. this.pageStatus.historyScroll = historyData.dataIndex;
  107. this.pageStatus.loadAlign = 'next';
  108. this.pageStatus.sortingType = historyData.sortingType;
  109. this.pageStatus.sortingTypeNm = historyData.sortingTypeNm;
  110. this.pageStatus.filterHtml = historyData.filterHtml;
  111. this.pageStatus.filterStatHtml = historyData.filterStatHtml;
  112. this.pageStatus.totalCount = historyData.totalCount;
  113. this.pageStatus.backScroll = historyData.backScroll;
  114. gagaInfiniteScroll.draw(historyData.htm);
  115. };
  116. $(window).on('scroll', function() {
  117. gagaInfiniteScroll.scrollCheck();
  118. });
  119. },
  120. draw : function(htm, scrollTop, containerHeight) {
  121. if (htm == 'not') {
  122. $(".wrap-loading").addClass('display-none');
  123. this.pageStatus.loadAlign = 'top';
  124. } else {
  125. var startH = $('#infiniteContainer').height();
  126. var $addHtm = $(htm);
  127. var $imgs = $addHtm.find('img');
  128. var loadCheck = 0;
  129. var len = $imgs.length;
  130. if (scrollTop && containerHeight) {
  131. } else {
  132. }
  133. $.each($imgs, function(index) {
  134. $imgs.eq(index).on('load',function() {
  135. loadCheck++;
  136. imgLpadComp();
  137. });
  138. });
  139. var imgLpadComp = function() {
  140. gagaInfiniteScroll.pushHistory(
  141. gagaInfiniteScroll.pageStatus.nowPage
  142. , $(window).scrollTop()
  143. , $('#infiniteContainer').height()
  144. , gagaInfiniteScroll.pageStatus.loadPage
  145. , 0
  146. , gagaInfiniteScroll.obj.$ajaxBox.html()
  147. , gagaInfiniteScroll.pageStatus.backScroll
  148. );
  149. if (scrollTop && containerHeight) {
  150. gagaInfiniteScroll.obj.$window.scrollTop.scrollTop(0).scrollTop(scrollTop);
  151. } else {
  152. gagaInfiniteScroll.obj.$window.scrollTop(gagaInfiniteScroll.obj.$window.scrollTop() + 1);
  153. }
  154. };
  155. $(".wrap-loading").addClass('display-none');
  156. // Append HTML
  157. this.obj.$ajaxBox.append($addHtm);
  158. if (scrollTop && containerHeight) {
  159. gagaInfiniteScroll.obj.$window.scrollTop.scrollTop(0).scrollTop(scrollTop);
  160. } else {
  161. $(window).scrollTop($(window).scrollTop() + 1);
  162. }
  163. this.pageStatus.loadAlign = 'not';
  164. }
  165. },
  166. pushHistory : function(page, sScrollTop, sHeight, sPageNum, sDataIndex, sHtml, sBackScroll) {
  167. var historyData = {
  168. state: page
  169. , scroll: sScrollTop
  170. , height: sHeight
  171. , pageUrl: gagaInfiniteScroll.pageStatus.pageUrl
  172. , name: gagaInfiniteScroll.pageStatus.nowPage
  173. , pageNum : sPageNum
  174. , dataIndex : sDataIndex
  175. , htm : sHtml
  176. , sortingType : gagaInfiniteScroll.pageStatus.sortingType
  177. , sortingTypeNm : gagaInfiniteScroll.pageStatus.sortingTypeNm
  178. , filterHtml : gagaInfiniteScroll.pageStatus.filterHtml
  179. , filterStatHtml : gagaInfiniteScroll.pageStatus.filterStatHtml
  180. , totalCount : gagaInfiniteScroll.pageStatus.totalCount
  181. , backScroll : sBackScroll
  182. };
  183. historyData = JSON.stringify(historyData);
  184. sessionStorage.setItem(document.location.href, historyData);
  185. },
  186. pushLinkHistory : function(pageNum, dataIndex, obj) { // 링크에서 사용
  187. var $pageNum = (pageNum || pageNum == 0) ? pageNum : '';
  188. var $dataIndex = (dataIndex || dataIndex == 0) ? dataIndex : '';
  189. var $html = (obj) ? $(obj).parents('#listBox').html() : '';
  190. var historyData = {
  191. state : gagaInfiniteScroll.pageStatus.nowPage
  192. , scroll : $(window).scrollTop()
  193. , height : $('#infiniteContainer').height()
  194. , pageUrl : gagaInfiniteScroll.pageStatus.pageUrl
  195. , name : gagaInfiniteScroll.pageStatus.nowPage
  196. , pageNum : $pageNum
  197. , dataIndex : $dataIndex
  198. , htm : $html
  199. , sortingType : gagaInfiniteScroll.pageStatus.sortingType
  200. , sortingTypeNm : gagaInfiniteScroll.pageStatus.sortingTypeNm
  201. , filterHtml : gagaInfiniteScroll.pageStatus.filterHtml
  202. , filterStatHtml : gagaInfiniteScroll.pageStatus.filterStatHtml
  203. , totalCount : gagaInfiniteScroll.pageStatus.totalCount
  204. , backScroll : gagaInfiniteScroll.pageStatus.backScroll
  205. };
  206. historyData = JSON.stringify(historyData);
  207. sessionStorage.setItem(document.location.href, historyData);
  208. return false;
  209. },
  210. jsonToHtml : function(result) {
  211. // Data 가져온 후 실행될 함수
  212. // fnDrawInfiniteScrollData 함수는 개발단에서 구현해야 한다.
  213. if (result.dataList == "not") // data가 더 이상 없으면
  214. fnDrawInfiniteScrollData('not', result.CURRENT_PAGE - 1);
  215. else
  216. fnDrawInfiniteScrollData(result, result.CURRENT_PAGE - 1);
  217. }
  218. };