index.html 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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:800">
  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. </body>
  94. <!--
  95. The javascript code to instantiate the template
  96. -->
  97. <script type="text/javascript">
  98. // get the source code for our two template
  99. // and compile them
  100. var source = $("#album-template").html();
  101. var template = Handlebars.compile(source);
  102. source = $("#modal-template").html();
  103. var modal_template = Handlebars.compile(source);
  104. // define the data for the template
  105. // we define an objects which contains an
  106. // array of other objects. This array will be used
  107. // to create multiple images
  108. var data = {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. // render the data into the template
  141. var html = template(data);
  142. // put the rendered template into the DOM
  143. $('#content').html(html);
  144. // a function to display a clicked
  145. // image as a modal
  146. // this is added as a click callback
  147. // to the thumbnails
  148. function displayModal(event){
  149. // get the index (position in the array)
  150. // of the photo we clicked on
  151. // "this" is the element that was clicked on
  152. // data("id") gets the attribute data-id
  153. // (which we set to the index of the photo in
  154. // the array - @index)
  155. var imageNumber = $(this).data("id");
  156. // get the image out of the array using the image
  157. // number and render it using the modal template
  158. var html = modal_template(data.images[imageNumber]);
  159. // put the modal template in the DOM
  160. $('#modal-container').html(html);
  161. // show the modal
  162. $("#imageModal").modal('show');
  163. }
  164. // display the modal when you click on a thumbnail
  165. $('.thumbnail').click(displayModal);
  166. // the search functionality
  167. // this happens when a key is pressed
  168. // inside the search box
  169. $('#searchbox').keypress(function (e) {
  170. // check if the key that was pressed
  171. // is the return key (it has id 13)
  172. // and only do the search if it is
  173. if (e.which == 13) {
  174. // get the search text which is the
  175. // contents of the search box
  176. var search_text = $('#searchbox').val();
  177. // print the search box
  178. // (this is an example of using
  179. // console.log for debugging)
  180. console.log(search_text)
  181. // create a new array of data with only
  182. // the data that contains the search string
  183. var filteredData = {
  184. // use the filter function which returns
  185. // a new array that contains only the
  186. // elements of data.images for which
  187. // the function returns true
  188. images: data.images.filter(function(d){
  189. // return true if the title contains
  190. // the search text
  191. if (d.title.search(search_text) > -1){
  192. return true;
  193. }
  194. // return true if the author contains
  195. // the search text
  196. if (d.author.search(search_text) > -1){
  197. return true;
  198. }
  199. // if we reach here it means we haven't
  200. // found a match so return false
  201. return false;
  202. })
  203. };
  204. // pass the newly filtered data into
  205. // the template to generate new html
  206. var html = template(filteredData);
  207. $('#content').html(html);
  208. // display the modal when you click on a thumbnail
  209. $('.thumbnail').click(displayModal);
  210. }
  211. });
  212. </script>
  213. </body>