main.js 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. $(document).ready(function() {
  2. console.log("ready");
  3. var _win = {
  4. w:$(document).width(),
  5. h:$(document).height()
  6. },
  7. _$root = $('#root'),
  8. _total_dp_len = 58,
  9. _active_pages_keys = [],
  10. _current_dp_key = "0", _current_dp_ob, _first_dp = true, _last_dp = false,
  11. _$page_left = $('#page-left'), _$page_right = $('#page-right'),
  12. _$overlay_left = $('#page-left .overlay'), _$overlay_right = $('#page-right .overlay'),
  13. _$maps_left = $('#page-left .maps'), _$maps_right = $('#page-right .maps'),
  14. _bgs_prefix = "images/pages/",
  15. _$railway = $('#railway'),
  16. _$pagination = $('#pagination'),
  17. _$pagi_l_arrow = $('.wrapper-l .l', _$pagination), _$pagi_r_arrow = $('.wrapper-r .l', _$pagination),
  18. _$pagi_l_txt = $('.wrapper-l .p', _$pagination), _$pagi_r_txt = $('.wrapper-r .p', _$pagination);
  19. // ____ _ __
  20. // / _/___ (_) /_
  21. // / // __ \/ / __/
  22. // _/ // / / / / /_
  23. // /___/_/ /_/_/\__/
  24. function init(){
  25. // console.log("init", _ACTIVE_PAGES[0].bg);
  26. initLayout();
  27. initData();
  28. initRailway();
  29. initkeyboard();
  30. initPagination();
  31. changePages();
  32. };
  33. function initLayout(){
  34. _$root.css({
  35. 'margin-top':(_win.h-_$root.height())/2
  36. });
  37. };
  38. function initData(){
  39. _active_pages_keys = Object.keys(_ACTIVE_PAGES);
  40. console.log("_active_pages_keys", _active_pages_keys);
  41. // for (var p = 0; p < _ACTIVE_PAGES.length; p++) {
  42. // if(typeof _ACTIVE_PAGES[p][0].page !== "undefined"){
  43. // _active_pages[(_ACTIVE_PAGES[p][0].page+1)/2] = p;
  44. // }
  45. // else if(typeof _ACTIVE_PAGES[p][1].page !== "undefined"){
  46. // _active_pages[_ACTIVE_PAGES[p][1].page/2] = p;
  47. // }
  48. // }
  49. };
  50. // _ __
  51. // / | / /___ __ __
  52. // / |/ / __ `/ | / /
  53. // / /| / /_/ /| |/ /
  54. // /_/ |_/\__,_/ |___/
  55. function initRailway(){
  56. var $ul = $('<ul>').appendTo(_$railway);
  57. var $li, adp, bgl, bgr, pl, pr;
  58. // loop through double pages
  59. for (var dp = 0; dp <= _total_dp_len; dp++) {
  60. // console.log("-- DOUBLE PAGE --", dp);
  61. $li = $('<li>')
  62. .addClass('double-page-'+dp)
  63. .appendTo($ul);
  64. // check if current double page is active or not by checking if we have info for it in pages.json
  65. // adp = typeof _active_pages[dp] !== "undefined" ? _active_pages[dp] : false;
  66. adp = typeof _ACTIVE_PAGES[dp] !== "undefined" ? dp : false;
  67. // console.log("adp",adp);
  68. if(adp !== false){
  69. // if active double page
  70. $li.addClass('active');
  71. // find for left and right pages if we have background info
  72. bgl = typeof _ACTIVE_PAGES[adp][0].bg !== "undefined" ? _bgs_prefix+"thumbs/"+_ACTIVE_PAGES[adp][0].bg : false;
  73. bgr = typeof _ACTIVE_PAGES[adp][1].bg !== "undefined" ? _bgs_prefix+"thumbs/"+_ACTIVE_PAGES[adp][1].bg : false;
  74. }else{
  75. // if not active double page, no background
  76. bgl = bgr = false;
  77. }
  78. // get the real page left and right numbers from double page number
  79. pr = dp*2;
  80. pl = pr -1;
  81. // add left page only if not first page (cover)
  82. if(dp!=0)
  83. $('<span>')
  84. .addClass('page page-'+(pl)+' double-page-'+dp+' page-left')
  85. .addClass(bgl ? 'active' : 0)
  86. .css({'background-image': bgl ? "url("+bgl+")" : "none"})
  87. .appendTo($li);
  88. // add right page only if not last page (backcover)
  89. if(dp<_total_dp_len)
  90. $('<span>')
  91. .addClass('page page-'+pr+' double-page-'+dp+' page-right')
  92. .addClass(bgr ? 'active' : 0)
  93. .css({'background-image': bgr ? "url("+bgr+")" : "none"})
  94. .appendTo($li);
  95. }
  96. $('li.active', _$railway).on('click', function(event) {
  97. dp = $(this).attr('class').match(/double-page-(\d+)/)[1];
  98. _current_dp_key = dp; //_active_pages[dp];
  99. changePages();
  100. });
  101. }
  102. // ____
  103. // / __ \____ _____ ____ _____
  104. // / /_/ / __ `/ __ `/ _ \/ ___/
  105. // / ____/ /_/ / /_/ / __(__ )
  106. // /_/ \__,_/\__, /\___/____/
  107. // /____/
  108. function nextPages(){
  109. var i = _active_pages_keys.indexOf(_current_dp_key) +1;
  110. if(i <= _active_pages_keys.length-1){
  111. _current_dp_key = _active_pages_keys[i];
  112. changePages();
  113. }
  114. };
  115. function prevPages(){
  116. var i = _active_pages_keys.indexOf(_current_dp_key)-1;
  117. if(i >= 0){
  118. _current_dp_key = _active_pages_keys[i];
  119. changePages();
  120. }
  121. };
  122. function changePages(){
  123. console.log("changePages", _current_dp_key);
  124. $('li',_$railway).removeClass('current');
  125. if(typeof _ACTIVE_PAGES[_current_dp_key] !== "undefined"){
  126. _current_dp_ob = _ACTIVE_PAGES[_current_dp_key];
  127. if (typeof _current_dp_ob[0].bg !== "undefined") {
  128. var pl = _current_dp_ob[0];
  129. // change display of main page left
  130. _$page_left
  131. .css({'background-image': "url("+_bgs_prefix+pl.bg+")"})
  132. .removeClass(pageClassToRemove)
  133. .addClass('page-'+pl.page);
  134. // change pagination
  135. _$pagi_l_txt.text(pl.page);
  136. }else{
  137. _$page_left.css({'background-image': "none"});
  138. _$pagi_l_txt.text("");
  139. }
  140. if (typeof _current_dp_ob[1].bg !== "undefined") {
  141. var pr = _current_dp_ob[1];
  142. _$page_right
  143. .css({'background-image': "url("+_bgs_prefix+pr.bg+")"})
  144. .removeClass(pageClassToRemove)
  145. .addClass('page-'+pr.page);
  146. _$pagi_r_txt.text(pr.page);
  147. }else{
  148. _$page_right.css({'background-image': "none"});
  149. _$pagi_r_txt.text("");
  150. }
  151. // hide or show nav arrows for first annd last double page
  152. var i = _active_pages_keys.indexOf(_current_dp_key);
  153. if( i == 0){
  154. _$pagi_l_arrow.addClass('hidden');
  155. }else{
  156. _$pagi_l_arrow.removeClass('hidden')
  157. }
  158. if(i == _active_pages_keys.length-1){
  159. _$pagi_r_arrow.addClass('hidden');
  160. }else{
  161. _$pagi_r_arrow.removeClass('hidden')
  162. }
  163. // make railway db thumb current
  164. $('li.double-page-'+_current_dp_key).addClass('current');
  165. }
  166. // updatePagesOverlays();
  167. };
  168. function pageClassToRemove(){
  169. var c = $(this).attr('class').match(/page-+\d+/);
  170. if(c && c.length)
  171. return c[0];
  172. };
  173. function updatePagesOverlays(){
  174. console.log("updatePagesOverlays");
  175. // remove all pages overlays
  176. $('.overlay, .maps',_$page_left).children().remove();
  177. $('.overlay, .maps',_$page_right).children().remove();
  178. console.log("Overlays", _ACTIVE_PAGES[_current_dp_key].overlays);
  179. // build page_left overlays
  180. for (var map in _ACTIVE_PAGES[_current_dp_key].overlays) {
  181. if (_ACTIVE_PAGES[_current_dp_key].overlays.hasOwnProperty(map)) {
  182. map = _ACTIVE_PAGES[_current_dp_key].overlays[map];
  183. console.log("Map",map);
  184. _$maps_left.append(
  185. $('<div>')
  186. .addClass("map")
  187. .attr('overlay', map.over)
  188. .css({
  189. "left":map.x,
  190. "top":map.y,
  191. "width":map.w,
  192. "height":map.h
  193. })
  194. .bind("mouseover", function(e){
  195. $(this)
  196. .parents('.page').find('.overlay')
  197. .css({
  198. // 'background-color':"red",
  199. 'background-image':'url(images/vectos/'+$(this).attr('overlay')+')'
  200. });
  201. })
  202. );
  203. }
  204. }
  205. };
  206. // __ __ __ __
  207. // / //_/__ __ __/ /_ ____ ____ __________/ /
  208. // / ,< / _ \/ / / / __ \/ __ \/ __ `/ ___/ __ /
  209. // / /| / __/ /_/ / /_/ / /_/ / /_/ / / / /_/ /
  210. // /_/ |_\___/\__, /_.___/\____/\__,_/_/ \__,_/
  211. // /____/
  212. function initkeyboard(){
  213. keyboardJS.bind('right', function(event) {
  214. // console.log(event);
  215. nextPages();
  216. });
  217. keyboardJS.bind('left', function(event) {
  218. // console.log(event);
  219. prevPages();
  220. });
  221. };
  222. // ____ _ __ _
  223. // / __ \____ _____ _(_)___ ____ _/ /_(_)___ ____
  224. // / /_/ / __ `/ __ `/ / __ \/ __ `/ __/ / __ \/ __ \
  225. // / ____/ /_/ / /_/ / / / / / /_/ / /_/ / /_/ / / / /
  226. // /_/ \__,_/\__, /_/_/ /_/\__,_/\__/_/\____/_/ /_/
  227. // /____/
  228. function initPagination(){
  229. $('.wrapper-l .l', _$pagination).on('click', function(event) {
  230. prevPages();
  231. });
  232. $('.wrapper-r .l', _$pagination).on('click', function(event) {
  233. nextPages();
  234. });
  235. };
  236. init();
  237. });