main.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  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. .attr('link', typeof map.link !== "undefined" ? map.link : null)
  214. .css({
  215. "left":map.x+"mm",
  216. "top":map.y+"mm",
  217. "width":map.w+"mm",
  218. "height":map.h+"mm"
  219. })
  220. .bind("mouseover", function(e){
  221. $(this)
  222. .parents('.page').find('.overlay')
  223. .css({
  224. 'background-image':'url(images/overlays/'+$(this).attr('overlay')+')'
  225. });
  226. })
  227. .bind("mouseout", function(e){
  228. $(this)
  229. .parents('.page').find('.overlay')
  230. .css({'background-image':'none'});
  231. })
  232. .bind("click", function(e){
  233. var link = $(this).attr('link');
  234. if (link) {
  235. _current_dp_key = parseInt(link);
  236. changePages();
  237. }
  238. })
  239. );
  240. if(typeof map.repere !== "undefined"){
  241. $p.append(
  242. $('<div>')
  243. .addClass('repere')
  244. .css({
  245. "left":map.repere_x+"mm",
  246. "top":map.repere_y+"mm",
  247. "width":map.repere_w+"mm",
  248. "height":map.repere_h+"mm",
  249. 'background-image':'url(images/reperes/'+map.repere+')'
  250. })
  251. );
  252. }
  253. }
  254. lr++;
  255. }
  256. };
  257. function resetOverlays(){
  258. $('.overlay, .maps','.page').css({'background-image':'none'}).children().remove();
  259. }
  260. // ___ __
  261. // / | ____ _____/ /_ ____ __________
  262. // / /| | / __ \/ ___/ __ \/ __ \/ ___/ ___/
  263. // / ___ |/ / / / /__/ / / / /_/ / / (__ )
  264. // /_/ |_/_/ /_/\___/_/ /_/\____/_/ /____/
  265. function initHashNav(){
  266. console.log('initHashNav');
  267. h = window.location.hash;
  268. console.log('hash',h);
  269. if(h)
  270. _current_dp_key = h.match(/^#(\d+)$/)[1];
  271. }
  272. function updateHash(){
  273. window.location.hash = _current_dp_key;
  274. };
  275. // __ __ __ __
  276. // / //_/__ __ __/ /_ ____ ____ __________/ /
  277. // / ,< / _ \/ / / / __ \/ __ \/ __ `/ ___/ __ /
  278. // / /| / __/ /_/ / /_/ / /_/ / /_/ / / / /_/ /
  279. // /_/ |_\___/\__, /_.___/\____/\__,_/_/ \__,_/
  280. // /____/
  281. function initkeyboard(){
  282. keyboardJS.bind('right', function(event) {
  283. // console.log(event);
  284. nextPages();
  285. });
  286. keyboardJS.bind('left', function(event) {
  287. // console.log(event);
  288. prevPages();
  289. });
  290. };
  291. // ____ _ __ _
  292. // / __ \____ _____ _(_)___ ____ _/ /_(_)___ ____
  293. // / /_/ / __ `/ __ `/ / __ \/ __ `/ __/ / __ \/ __ \
  294. // / ____/ /_/ / /_/ / / / / / /_/ / /_/ / /_/ / / / /
  295. // /_/ \__,_/\__, /_/_/ /_/\__,_/\__/_/\____/_/ /_/
  296. // /____/
  297. function initPagination(){
  298. $('.wrapper-l .l', _$pagination).on('click', function(event) {
  299. prevPages();
  300. });
  301. $('.wrapper-r .l', _$pagination).on('click', function(event) {
  302. nextPages();
  303. });
  304. };
  305. init();
  306. });