index.html 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. <!--
  2. This example extends our array example so that you can
  3. display an image by clicking on it.
  4. It uses two templates, one for viewing thumbnails and
  5. one for viewing a single image.
  6. NB I have found a bug in this code since doing the video
  7. see if you can find it and even fix it.
  8. -->
  9. <head>
  10. <!-- include our libraries and css files -->
  11. <script src="js/jquery-2.1.4.min.js"></script>
  12. <script src="js/handlebars-v3.0.3.js"></script>
  13. <script src="js/bootstrap.min.js"></script>
  14. <link href="css/bootstrap.css" rel="stylesheet">
  15. <link href="css/gallery.css" rel="stylesheet">
  16. </head>
  17. <body role="document">
  18. <div class="container">
  19. <!-- the page title -->
  20. <div class="page-header">
  21. <h1>My photo albums </h1>
  22. </div>
  23. <!--
  24. the search box.
  25. It is given the navbar-form class so that bootstrap
  26. put it in the top navbar
  27. -->
  28. <div class="navbar-form navbar-right">
  29. <input id="searchbox" type="text" class="form-control" placeholder="Search...">
  30. </div>
  31. <!--
  32. We don't actually use these navigation tabs in this example, but
  33. I've included them so that the top nav looks right, and
  34. we can extend the example later to use them
  35. -->
  36. <ul class="nav nav-tabs">
  37. <li role="" id="detailsbtn"><a href="#">Details</a></li>
  38. <li role="" id="modalbtn"><a href="#">Modal</a></li>
  39. </ul>
  40. <br /><br />
  41. <!--
  42. the content of the web page starts off empty
  43. because we will fill it later from the template
  44. We use container-fluid because our template uses
  45. the bootstrap grid
  46. -->
  47. <div id="content" class="container-fluid" role="main">
  48. </div>
  49. <!--
  50. this div will contain the modal dialog which will
  51. display the large image
  52. -->
  53. <div id="modal-container">
  54. </div>
  55. </div>
  56. <!--
  57. This template displays many photos in an album.
  58. Is uses the {{#each }} template tag which repeats the
  59. innter bit of the template for each element of an array
  60. -->
  61. <script id="album-template" type="text/x-handlebars-template">
  62. <div class="row">
  63. {{#each images}}
  64. <div class="col-xs-12 col-md-3">
  65. <div class="thumbnail" data-id="{{@index}}">
  66. <img class="crop-img" src="{{src}}" alt="" />
  67. <div class="author">
  68. <h3> {{title}} </h3>
  69. <p> {{author}} </p>
  70. </div>
  71. </div>
  72. </div> <!-- / col -->
  73. {{/each}}
  74. </div> <!-- / row -->
  75. </script>
  76. <!--
  77. This second template displays a modal pop-up of the image.
  78. It has quite a bit of boilder plate HTML code but in
  79. essence it just displays and image tag within a
  80. set of divs that define the modal
  81. -->
  82. <script id="modal-template" type="text/x-handlebars-template">
  83. <div id="imageModal" class="modal fade" role="dialog">
  84. <div class="modal-dialog" style="width:800px">
  85. <div class="modal-content">
  86. <div class="modal-body">
  87. <img style="width:100%" src="{{src}}" />
  88. </div>
  89. </div>
  90. </div>
  91. </div>
  92. </script>
  93. <!--
  94. The javascript code to instantiate the template
  95. -->
  96. <script type="text/javascript">
  97. // get the source code for our two template
  98. // and compile them
  99. var source = $("#album-template").html();
  100. var template = Handlebars.compile(source);
  101. source = $("#modal-template").html();
  102. var modal_template = Handlebars.compile(source);
  103. // define the data for the template
  104. // we define an objects which contains an
  105. // array of other objects. This array will be used
  106. // to create multiple images
  107. var data = {
  108. images: [
  109. {
  110. src: "https://upload.wikimedia.org/wikipedia/commons/thumb/3/38/Shopping_Center_Magna_Plaza_Amsterdam_2014.jpg/600px-Shopping_Center_Magna_Plaza_Amsterdam_2014.jpg",
  111. title: "Shopping Center Magna Plaza Amsterdam 2014",
  112. author: "Tuxyso"
  113. },
  114. {
  115. src: "https://upload.wikimedia.org/wikipedia/commons/thumb/9/97/The_Earth_seen_from_Apollo_17.jpg/600px-The_Earth_seen_from_Apollo_17.jpg",
  116. title: "The Earth seen from Apollo 17",
  117. author: "Ed g2s"
  118. },
  119. {
  120. src: "https://upload.wikimedia.org/wikipedia/commons/thumb/6/68/Barnard_33.jpg/300px-Barnard_33.jpg",
  121. title: "horse nebula",
  122. author: "John Smith"
  123. },
  124. {
  125. src: "http://upload.wikimedia.org/wikipedia/commons/thumb/2/24/Wwii_woman_worker-edit.jpg/300px-Wwii_woman_worker-edit.jpg",
  126. title: "wwii woman worker",
  127. author: "Another author"
  128. },
  129. {
  130. src: "http://upload.wikimedia.org/wikipedia/commons/thumb/1/15/Lijiang_Yunnan_China-Naxi-people-carrying-baskets-01.jpg/300px-Lijiang_Yunnan_China-Naxi-people-carrying-baskets-01.jpg",
  131. title: "Lijiang Yunnan China Naxi people carrying baskets-",
  132. author: "Wikimedia Commons"
  133. },
  134. {
  135. src: "http://upload.wikimedia.org/wikipedia/commons/thumb/1/1c/Stift_G%C3%B6ttweig_Gobelinzimmer_01.JPG/300px-Stift_G%C3%B6ttweig_Gobelinzimmer_01.JPG",
  136. title: "Marco's house",
  137. author: "Wikimedia Commons"
  138. }
  139. ]
  140. };
  141. // render the data into the template
  142. var html = template(data);
  143. // put the rendered template into the DOM
  144. $('#content').html(html);
  145. // a function to display a clicked
  146. // image as a modal
  147. // this is added as a click callback
  148. // to the thumbnails
  149. function displayModal(event) {
  150. // get the index (position in the array)
  151. // of the photo we clicked on
  152. // "this" is the element that was clicked on
  153. // data("id") gets the attribute data-id
  154. // (which we set to the index of the photo in
  155. // the array - @index)
  156. var imageNumber = $(this).data("id");
  157. // get the image out of the array using the image
  158. // number and render it using the modal template
  159. var html = modal_template(data.images[imageNumber]);
  160. // put the modal template in the DOM
  161. $('#modal-container').html(html);
  162. // show the modal
  163. $("#imageModal").modal('show');
  164. }
  165. // display the modal when you click on a thumbnail
  166. $('.thumbnail').click(displayModal);
  167. // the search functionality
  168. // this happens when a key is pressed
  169. // inside the search box
  170. $('#searchbox').keypress(function (e) {
  171. // check if the key that was pressed
  172. // is the return key (it has id 13)
  173. // and only do the search if it is
  174. if (e.which == 13) {
  175. // get the search text which is the
  176. // contents of the search box
  177. var search_text = $('#searchbox').val();
  178. // print the search box
  179. // (this is an example of using
  180. // console.log for debugging)
  181. console.log(search_text)
  182. // create a new array of data with only
  183. // the data that contains the search string
  184. var filteredData = {
  185. // use the filter function which returns
  186. // a new array that contains only the
  187. // elements of data.images for which
  188. // the function returns true
  189. images: data.images.filter(function (d) {
  190. // return true if the title contains
  191. // the search text
  192. if (d.title.search(search_text) > -1) {
  193. return true;
  194. }
  195. // return true if the author contains
  196. // the search text
  197. if (d.author.search(search_text) > -1) {
  198. return true;
  199. }
  200. // if we reach here it means we haven't
  201. // found a match so return false
  202. return false;
  203. })
  204. };
  205. // pass the newly filtered data into
  206. // the template to generate new html
  207. var html = template(filteredData);
  208. $('#content').html(html);
  209. // display the modal when you click on a thumbnail
  210. $('.thumbnail').click(displayModal);
  211. }
  212. });
  213. </script>
  214. </body>