main.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  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. initHashNav();
  32. changePages();
  33. };
  34. function initLayout(){
  35. _$root.css({
  36. 'margin-top':(_win.h-_$root.height())/2
  37. });
  38. };
  39. function initData(){
  40. _active_pages_keys = Object.keys(_ACTIVE_PAGES);
  41. console.log("_active_pages_keys", _active_pages_keys);
  42. // for (var p = 0; p < _ACTIVE_PAGES.length; p++) {
  43. // if(typeof _ACTIVE_PAGES[p][0].page !== "undefined"){
  44. // _active_pages[(_ACTIVE_PAGES[p][0].page+1)/2] = p;
  45. // }
  46. // else if(typeof _ACTIVE_PAGES[p][1].page !== "undefined"){
  47. // _active_pages[_ACTIVE_PAGES[p][1].page/2] = p;
  48. // }
  49. // }
  50. };
  51. // _ __
  52. // / | / /___ __ __
  53. // / |/ / __ `/ | / /
  54. // / /| / /_/ /| |/ /
  55. // /_/ |_/\__,_/ |___/
  56. function initRailway(){
  57. var $ul = $('<ul>').appendTo(_$railway);
  58. var $li, adp, bgl, bgr, pl, pr;
  59. // loop through double pages
  60. for (var dp = 0; dp <= _total_dp_len; dp++) {
  61. // console.log("-- DOUBLE PAGE --", dp);
  62. $li = $('<li>')
  63. .addClass('double-page-'+dp)
  64. .appendTo($ul);
  65. // check if current double page is active or not by checking if we have info for it in pages.json
  66. // adp = typeof _active_pages[dp] !== "undefined" ? _active_pages[dp] : false;
  67. adp = typeof _ACTIVE_PAGES[dp] !== "undefined" ? dp : false;
  68. // console.log("adp",adp);
  69. if(adp !== false){
  70. // if active double page
  71. $li.addClass('active');
  72. // find for left and right pages if we have background info
  73. bgl = typeof _ACTIVE_PAGES[adp][0].bg !== "undefined" ? _bgs_prefix+"thumbs/"+_ACTIVE_PAGES[adp][0].bg : false;
  74. bgr = typeof _ACTIVE_PAGES[adp][1].bg !== "undefined" ? _bgs_prefix+"thumbs/"+_ACTIVE_PAGES[adp][1].bg : false;
  75. }else{
  76. // if not active double page, no background
  77. bgl = bgr = false;
  78. }
  79. // get the real page left and right numbers from double page number
  80. pr = dp*2;
  81. pl = pr -1;
  82. // add left page only if not first page (cover)
  83. if(dp!=0)
  84. $('<span>')
  85. .addClass('page page-'+(pl)+' double-page-'+dp+' page-left')
  86. .addClass(bgl ? 'active' : 0)
  87. .css({'background-image': bgl ? "url("+bgl+")" : "none"})
  88. .appendTo($li);
  89. // add right page only if not last page (backcover)
  90. if(dp<_total_dp_len)
  91. $('<span>')
  92. .addClass('page page-'+pr+' double-page-'+dp+' page-right')
  93. .addClass(bgr ? 'active' : 0)
  94. .css({'background-image': bgr ? "url("+bgr+")" : "none"})
  95. .appendTo($li);
  96. }
  97. $('li.active', _$railway).on('click', function(event) {
  98. dp = $(this).attr('class').match(/double-page-(\d+)/)[1];
  99. _current_dp_key = dp; //_active_pages[dp];
  100. changePages();
  101. });
  102. }
  103. // ____
  104. // / __ \____ _____ ____ _____
  105. // / /_/ / __ `/ __ `/ _ \/ ___/
  106. // / ____/ /_/ / /_/ / __(__ )
  107. // /_/ \__,_/\__, /\___/____/
  108. // /____/
  109. function nextPages(){
  110. var i = _active_pages_keys.indexOf(_current_dp_key) +1;
  111. if(i <= _active_pages_keys.length-1){
  112. _current_dp_key = _active_pages_keys[i];
  113. changePages();
  114. }
  115. };
  116. function prevPages(){
  117. var i = _active_pages_keys.indexOf(_current_dp_key)-1;
  118. if(i >= 0){
  119. _current_dp_key = _active_pages_keys[i];
  120. changePages();
  121. }
  122. };
  123. function changePages(){
  124. console.log("changePages", _current_dp_key);
  125. $('li',_$railway).removeClass('current');
  126. if(typeof _ACTIVE_PAGES[_current_dp_key] !== "undefined"){
  127. _current_dp_ob = _ACTIVE_PAGES[_current_dp_key];
  128. updatePagesDisplay();
  129. updatePaginationDisplay();
  130. updateRailwayDisplay();
  131. updateHash();
  132. updatePagesOverlays();
  133. }
  134. };
  135. function updatePagesDisplay(){
  136. if (typeof _current_dp_ob[0].bg !== "undefined") {
  137. var pl = _current_dp_ob[0];
  138. // change display of main page left
  139. _$page_left
  140. .css({'background-image': "url("+_bgs_prefix+pl.bg+")"})
  141. .removeClass(pageClassToRemove)
  142. .addClass('page-'+pl.page);
  143. // change pagination
  144. _$pagi_l_txt.text(pl.page);
  145. }else{
  146. _$page_left.css({'background-image': "none"});
  147. _$pagi_l_txt.text("");
  148. }
  149. if (typeof _current_dp_ob[1].bg !== "undefined") {
  150. var pr = _current_dp_ob[1];
  151. _$page_right
  152. .css({'background-image': "url("+_bgs_prefix+pr.bg+")"})
  153. .removeClass(pageClassToRemove)
  154. .addClass('page-'+pr.page);
  155. _$pagi_r_txt.text(pr.page);
  156. }else{
  157. _$page_right.css({'background-image': "none"});
  158. _$pagi_r_txt.text("");
  159. }
  160. };
  161. function pageClassToRemove(){
  162. var c = $(this).attr('class').match(/page-+\d+/);
  163. if(c && c.length)
  164. return c[0];
  165. };
  166. function updatePaginationDisplay(){
  167. // hide or show nav arrows for first annd last double page
  168. var i = _active_pages_keys.indexOf(_current_dp_key);
  169. if( i == 0){
  170. _$pagi_l_arrow.addClass('hidden');
  171. }else{
  172. _$pagi_l_arrow.removeClass('hidden')
  173. }
  174. if(i == _active_pages_keys.length-1){
  175. _$pagi_r_arrow.addClass('hidden');
  176. }else{
  177. _$pagi_r_arrow.removeClass('hidden')
  178. }
  179. };
  180. function updateRailwayDisplay(){
  181. // make railway db thumb current
  182. $('li.double-page-'+_current_dp_key).addClass('current');
  183. };
  184. // ____ __
  185. // / __ \_ _____ _____/ /___ ___ _______
  186. // / / / / | / / _ \/ ___/ / __ `/ / / / ___/
  187. // / /_/ /| |/ / __/ / / / /_/ / /_/ (__ )
  188. // \____/ |___/\___/_/ /_/\__,_/\__, /____/
  189. // /____/
  190. function updatePagesOverlays(){
  191. console.log("updatePagesOverlays");
  192. // remove all pages overlays
  193. resetOverlays();
  194. // loop through left and right pages from the current double page
  195. var lr = 0,
  196. p_ob,
  197. $p,
  198. map;
  199. for(var i in _current_dp_ob){
  200. p_ob = _current_dp_ob[i];
  201. console.log("p_ob", p_ob);
  202. // first left, then right
  203. $p = lr == 0 ? _$maps_left : _$maps_right;
  204. // loop through overlays from each page
  205. for(var m in p_ob.overlays){
  206. map = p_ob.overlays[m];
  207. console.log('map', map);
  208. $p.append(
  209. $('<div>')
  210. .addClass("map")
  211. .attr('overlay', map.over)
  212. .attr('debug', _SETTINGS.debug ? map.debug : _SETTINGS.debug)
  213. .css({
  214. "left":map.x+"mm",
  215. "top":map.y+"mm",
  216. "width":map.w+"mm",
  217. "height":map.h+"mm"
  218. })
  219. .bind("mouseover", function(e){
  220. $(this)
  221. .parents('.page').find('.overlay')
  222. .css({
  223. 'background-image':'url(images/overlays/'+$(this).attr('overlay')+')'
  224. });
  225. })
  226. .bind("mouseout", function(e){
  227. $(this)
  228. .parents('.page').find('.overlay')
  229. .css({'background-image':'none'});
  230. })
  231. );
  232. if(typeof map.repere !== "undefined"){
  233. $p.append(
  234. $('<div>')
  235. .addClass('repere')
  236. .css({
  237. "left":map.repere_x+"mm",
  238. "top":map.repere_y+"mm",
  239. "width":map.repere_w+"mm",
  240. "height":map.repere_h+"mm",
  241. 'background-image':'url(images/reperes/'+map.repere+')'
  242. })
  243. );
  244. }
  245. }
  246. lr++;
  247. }
  248. };
  249. function resetOverlays(){
  250. $('.overlay, .maps','.page').css({'background-image':'none'}).children().remove();
  251. }
  252. // ___ __
  253. // / | ____ _____/ /_ ____ __________
  254. // / /| | / __ \/ ___/ __ \/ __ \/ ___/ ___/
  255. // / ___ |/ / / / /__/ / / / /_/ / / (__ )
  256. // /_/ |_/_/ /_/\___/_/ /_/\____/_/ /____/
  257. function initHashNav(){
  258. console.log('initHashNav');
  259. h = window.location.hash;
  260. console.log('hash',h);
  261. if(h)
  262. _current_dp_key = h.match(/^#(\d+)$/)[1];
  263. }
  264. function updateHash(){
  265. window.location.hash = _current_dp_key;
  266. };
  267. // __ __ __ __
  268. // / //_/__ __ __/ /_ ____ ____ __________/ /
  269. // / ,< / _ \/ / / / __ \/ __ \/ __ `/ ___/ __ /
  270. // / /| / __/ /_/ / /_/ / /_/ / /_/ / / / /_/ /
  271. // /_/ |_\___/\__, /_.___/\____/\__,_/_/ \__,_/
  272. // /____/
  273. function initkeyboard(){
  274. keyboardJS.bind('right', function(event) {
  275. // console.log(event);
  276. nextPages();
  277. });
  278. keyboardJS.bind('left', function(event) {
  279. // console.log(event);
  280. prevPages();
  281. });
  282. };
  283. // ____ _ __ _
  284. // / __ \____ _____ _(_)___ ____ _/ /_(_)___ ____
  285. // / /_/ / __ `/ __ `/ / __ \/ __ `/ __/ / __ \/ __ \
  286. // / ____/ /_/ / /_/ / / / / / /_/ / /_/ / /_/ / / / /
  287. // /_/ \__,_/\__, /_/_/ /_/\__,_/\__/_/\____/_/ /_/
  288. // /____/
  289. function initPagination(){
  290. $('.wrapper-l .l', _$pagination).on('click', function(event) {
  291. prevPages();
  292. });
  293. $('.wrapper-r .l', _$pagination).on('click', function(event) {
  294. nextPages();
  295. });
  296. };
  297. init();
  298. });