gallery.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /*
  2. * This file contains the javascript code for our gallery
  3. */
  4. // variables for all of the templates so we only have to compile
  5. // them once on page load and can then use the same compiled
  6. // templates many times
  7. var albums_template, photos_template, photo_template, slideshow_template;
  8. // variables to store the current displayed album and photo
  9. var current_album = gallery.albums[0];
  10. var current_photo = current_album.photos[0];
  11. // a helper function that instantiates a template
  12. // and displays the results in the content div
  13. function showTemplate(template, data) {
  14. var html = template(data);
  15. $("#content").html(html);
  16. }
  17. // document read gets called when the whole document
  18. // is loaded, so we put most of the code that needs to run
  19. // in here
  20. $(document).ready(function () {
  21. //
  22. // compile all of our templates ready for use
  23. //
  24. var source = $("#albums-template").html();
  25. albums_template = Handlebars.compile(source);
  26. source = $("#photos-template").html();
  27. photos_template = Handlebars.compile(source);
  28. source = $("#photo-template").html();
  29. photo_template = Handlebars.compile(source);
  30. source = $("#slideshow-template").html();
  31. slideshow_template = Handlebars.compile(source);
  32. //
  33. // clicking on the albums tab shows the
  34. // thumbnails of all the albums
  35. //
  36. $("#albums-tab").click(function () {
  37. // displays the albums template
  38. showTemplate(albums_template, gallery);
  39. // make the albums tab the active one
  40. // first make the currently active tab inactive
  41. $(".nav-tabs .active").removeClass("active");
  42. // then make albums tab active
  43. $("#albums-tab").addClass("active");
  44. // add a click callback to each album
  45. // thumbnail which displays the photos
  46. // template on that album
  47. // (I have written out the code for this
  48. // function for clarity but it is actually
  49. // pretty much the same as the photos tab
  50. // function so we could acutally just
  51. // call $(".photo-thumbnail").click() )
  52. $(".album-thumbnail").click(function () {
  53. // get the index (position in the array)
  54. // of the album we clicked on
  55. // "this" is the element that was clicked on
  56. // data("id") gets the attribute data-id
  57. // (which we set to the index of the album in
  58. // the array - @index)
  59. var index = $(this).data("id");
  60. // set the current album to this album
  61. current_album = gallery.albums[index];
  62. // displays the photos template
  63. showTemplate(photos_template, current_album);
  64. // add an on click al all the photo thumbnails
  65. // which displays the photo in a modal popup
  66. $(".photo-thumbnail").click(function () {
  67. // get the index (position in the array)
  68. // of the photo we clicked on
  69. // "this" is the element that was clicked on
  70. // data("id") gets the attribute data-id
  71. // (which we set to the index of the photo in
  72. // the array - @index)
  73. var index = $(this).data("id");
  74. // set the current photo to this photo
  75. current_photo = current_album.photos[index];
  76. // displays the single photo template
  77. showTemplate(photo_template, current_photo);
  78. });
  79. });
  80. });
  81. //
  82. // clicking on the photos tab shows all of the
  83. // photos in the current album
  84. //
  85. $("#photos-tab").click(function () {
  86. // displays the photos template
  87. showTemplate(photos_template, current_album);
  88. // make the photos tab the active one
  89. // first make the currently active tab inactive
  90. $(".nav-tabs .active").removeClass("active");
  91. // then make photos tab active
  92. $("#photos-tab").addClass("active");
  93. // add an on click al all the photo thumbnails
  94. // which displays the photo in a modal popup
  95. $(".photo-thumbnail").click(function () {
  96. // get the index (position in the array)
  97. // of the photo we clicked on
  98. // "this" is the element that was clicked on
  99. // data("id") gets the attribute data-id
  100. // (which we set to the index of the photo in
  101. // the array - @index)
  102. var index = $(this).data("id");
  103. // set the current photo to this photo
  104. current_photo = current_album.photos[index];
  105. // displays the single photo template
  106. showTemplate(photo_template, current_photo);
  107. });
  108. });
  109. //
  110. // clicking on the slideshow tab displays the
  111. // current album as a slide show
  112. //
  113. $("#slideshow-tab").click(function () {
  114. // display the slideshow template using the
  115. // current album
  116. showTemplate(slideshow_template, current_album);
  117. // make the slideshow tab the active one
  118. // first make the currently active tab inactive
  119. $(".nav-tabs .active").removeClass("active");
  120. // then make slideshow tab active
  121. $("#slideshow-tab").addClass("active");
  122. });
  123. // start the page by showing the albums view
  124. // we do this by virtually clicking on the
  125. // albums tab
  126. $("#albums-tab").click();
  127. });