PageRenderTime 26ms CodeModel.GetById 33ms RepoModel.GetById 1ms app.codeStats 0ms

/examples/fragment-advanced/index.php

https://github.com/kgiff/jquery-bbq
PHP | 307 lines | 193 code | 60 blank | 54 comment | 8 complexity | 59cabbe6778b9df6cb1d71af24de27e1 MD5 | raw file
  1. <?PHP
  2. include "../index.php";
  3. $shell['title3'] = "hashchange ยป Advanced";
  4. $shell['h2'] = 'Cached AJAX + fragment + history + bookmarking = Tasty!';
  5. // ========================================================================== //
  6. // SCRIPT
  7. // ========================================================================== //
  8. ob_start();
  9. ?>
  10. $(function(){
  11. // For each .bbq widget, keep a data object containing a mapping of
  12. // url-to-container for caching purposes.
  13. $('.bbq').each(function(){
  14. $(this).data( 'bbq', {
  15. cache: {
  16. // If url is '' (no fragment), display this div's content.
  17. '': $(this).find('.bbq-default')
  18. }
  19. });
  20. });
  21. // For all links inside a .bbq widget, push the appropriate state onto the
  22. // history when clicked.
  23. $('.bbq a[href^=#]').live( 'click', function(e){
  24. var state = {},
  25. // Get the id of this .bbq widget.
  26. id = $(this).closest( '.bbq' ).attr( 'id' ),
  27. // Get the url from the link's href attribute, stripping any leading #.
  28. url = $(this).attr( 'href' ).replace( /^#/, '' );
  29. // Set the state!
  30. state[ id ] = url;
  31. $.bbq.pushState( state );
  32. // And finally, prevent the default link click behavior by returning false.
  33. return false;
  34. });
  35. // Bind an event to window.onhashchange that, when the history state changes,
  36. // iterates over all .bbq widgets, getting their appropriate url from the
  37. // current state. If that .bbq widget's url has changed, display either our
  38. // cached content or fetch new content to be displayed.
  39. $(window).bind( 'hashchange', function(e) {
  40. // Iterate over all .bbq widgets.
  41. $('.bbq').each(function(){
  42. var that = $(this),
  43. // Get the stored data for this .bbq widget.
  44. data = that.data( 'bbq' ),
  45. // Get the url for this .bbq widget from the hash, based on the
  46. // appropriate id property. In jQuery 1.4, you should use e.getState()
  47. // instead of $.bbq.getState().
  48. url = $.bbq.getState( that.attr( 'id' ) ) || '';
  49. // If the url hasn't changed, do nothing and skip to the next .bbq widget.
  50. if ( data.url === url ) { return; }
  51. // Store the url for the next time around.
  52. data.url = url;
  53. // Remove .bbq-current class from any previously "current" link(s).
  54. that.find( 'a.bbq-current' ).removeClass( 'bbq-current' );
  55. // Hide any visible ajax content.
  56. that.find( '.bbq-content' ).children( ':visible' ).hide();
  57. // Add .bbq-current class to "current" nav link(s), only if url isn't empty.
  58. url && that.find( 'a[href="#' + url + '"]' ).addClass( 'bbq-current' );
  59. if ( data.cache[ url ] ) {
  60. // Since the widget is already in the cache, it doesn't need to be
  61. // created, so instead of creating it again, let's just show it!
  62. data.cache[ url ].show();
  63. } else {
  64. // Show "loading" content while AJAX content loads.
  65. that.find( '.bbq-loading' ).show();
  66. // Create container for this url's content and store a reference to it in
  67. // the cache.
  68. data.cache[ url ] = $( '<div class="bbq-item"/>' )
  69. // Append the content container to the parent container.
  70. .appendTo( that.find( '.bbq-content' ) )
  71. // Load external content via AJAX. Note that in order to keep this
  72. // example streamlined, only the content in .infobox is shown. You'll
  73. // want to change this based on your needs.
  74. .load( url, function(){
  75. // Content loaded, hide "loading" content.
  76. that.find( '.bbq-loading' ).hide();
  77. });
  78. }
  79. });
  80. })
  81. // Since the event is only triggered when the hash changes, we need to trigger
  82. // the event now, to handle the hash the page may have loaded with.
  83. $(window).trigger( 'hashchange' );
  84. });
  85. <?
  86. $shell['script'] = ob_get_contents();
  87. ob_end_clean();
  88. // ========================================================================== //
  89. // HTML HEAD ADDITIONAL
  90. // ========================================================================== //
  91. ob_start();
  92. ?>
  93. <script type="text/javascript" src="../../jquery.ba-bbq.js"></script>
  94. <script type="text/javascript" language="javascript">
  95. <?= $shell['script']; ?>
  96. $(function(){
  97. // Syntax highlighter.
  98. SyntaxHighlighter.highlight();
  99. });
  100. </script>
  101. <style type="text/css" title="text/css">
  102. /*
  103. bg: #FDEBDC
  104. bg1: #FFD6AF
  105. bg2: #FFAB59
  106. orange: #FF7F00
  107. brown: #913D00
  108. lt. brown: #C4884F
  109. */
  110. .bbq {
  111. margin-bottom: 1em;
  112. clear: both;
  113. }
  114. .bbq-content {
  115. border: 1px solid #913D00;
  116. border-top: none;
  117. padding: 8px;
  118. margin: 0;
  119. float: left;
  120. width: 682px;
  121. height: 152px;
  122. -moz-border-radius-bottomleft: 10px;
  123. -moz-border-radius-bottomright: 10px;
  124. -webkit-border-bottom-left-radius: 10px;
  125. -webkit-border-bottom-right-radius: 10px;
  126. }
  127. .bbq-item h1 {
  128. margin: 0;
  129. font-size: 180%;
  130. }
  131. .bbq-item p {
  132. font-size: 150%;
  133. margin: 5px 0 0;
  134. }
  135. .bbq-item img {
  136. border: 1px solid #913D00;
  137. float: right;
  138. margin-left: 10px;
  139. }
  140. a.bbq-current {
  141. font-weight: 700;
  142. text-decoration: none;
  143. }
  144. .bbq-nav {
  145. padding: 0.3em;
  146. color: #C4884F;
  147. border: 1px solid #C4884F;
  148. background: #FFD6AF;
  149. clear: both;
  150. text-align: center;
  151. }
  152. .bbq-nav-top {
  153. margin-bottom: 0;
  154. -moz-border-radius-topleft: 10px;
  155. -moz-border-radius-topright: 10px;
  156. -webkit-border-top-left-radius: 10px;
  157. -webkit-border-top-right-radius: 10px;
  158. }
  159. #page {
  160. width: 700px;
  161. }
  162. </style>
  163. <?
  164. $shell['html_head'] = ob_get_contents();
  165. ob_end_clean();
  166. // ========================================================================== //
  167. // HTML BODY
  168. // ========================================================================== //
  169. ob_start();
  170. ?>
  171. <?= $shell['donate'] ?>
  172. <p>
  173. With <a href="http://benalman.com/projects/jquery-bbq-plugin/">jQuery BBQ</a> you can keep track of state, history and allow bookmarking while dynamically modifying the page via AJAX and/or DHTML.. just click the links, use your browser's back and next buttons, reload the page.. and when you're done playing, check out the code!
  174. </p>
  175. <p>
  176. In this example, window.location.hash is used to store a serialized data object representing the state of multiple "widgets". Due to the flexibility of $.bbq.pushState(), a widget doesn't need to know the state of any other widget to push a state change onto the history, only their state needs to be specifed and it will be merged in, creating a new history entry and a page state that is bookmarkable. Of course, if you only want to keep track of a single item on the page, you might want to check out the <a href="../fragment-basic/">basic window.onhashchange</a> example.
  177. </p>
  178. <h3>This div.bbq widget has id "bbq1"</h3>
  179. <div class="bbq" id="bbq1">
  180. <div class="bbq-nav bbq-nav-top">
  181. <a href="#burger.html">Burgers</a> |
  182. <a href="#chicken.html">Chicken</a> |
  183. <a href="#kebabs.html">Kebabs</a>
  184. </div>
  185. <div class="bbq-content">
  186. <!-- This will be shown while loading AJAX content. You'll want to get an image that suits your design at http://ajaxload.info/ -->
  187. <div class="bbq-loading" style="display:none;">
  188. <img src="/shell/images/ajaxload-15-white.gif" alt="Loading"/> Loading content...
  189. </div>
  190. <!-- This content will be shown if no path is specified in the URL fragment. -->
  191. <div class="bbq-default bbq-item">
  192. <img src="bbq.jpg" width="200" height="150">
  193. <h1>jQuery BBQ</h1>
  194. <p>Click a nav item above to load some delicious AJAX content! Also, once
  195. the content loads, feel free to further explore our savory delights by
  196. clicking any inline links you might see.</p>
  197. </div>
  198. </div>
  199. <div style="clear:both;"></div>
  200. </div>
  201. <h3>This div.bbq widget has id "bbq2"</h3>
  202. <div class="bbq" id="bbq2">
  203. <div class="bbq-nav bbq-nav-top">
  204. <a href="#kielbasa.html">Kielbasa</a> |
  205. <a href="#ribs.html">Ribs</a> |
  206. <a href="#steak.html">Steak</a>
  207. </div>
  208. <div class="bbq-content">
  209. <!-- This will be shown while loading AJAX content. You'll want to get an image that suits your design at http://ajaxload.info/ -->
  210. <div class="bbq-loading" style="display:none;">
  211. <img src="/shell/images/ajaxload-15-white.gif" alt="Loading"/> Loading content...
  212. </div>
  213. <!-- This content will be shown if no path is specified in the URL fragment. -->
  214. <div class="bbq-default bbq-item">
  215. <img src="bbq.jpg" width="200" height="150">
  216. <h1>jQuery BBQ</h1>
  217. <p>And there's plenty more where that came from! Don't forget to click
  218. here for some down-home AJAX content, cooked special, just for this
  219. content area. You just can't have too much of a good thing!</p>
  220. </div>
  221. </div>
  222. <div style="clear:both;"></div>
  223. </div>
  224. <h3>The code</h3>
  225. <p>Note that a lot of the following code is very similar to the <a href="../fragment-basic/">basic window.onhashchange</a> example. That's intentional! They're functionally very similar, but while this version is far more robust, it is somewhat more complex. Look at both to see which meets your needs, and don't be afraid to adapt. Also, if you want to see a robust AND simple implementation, be sure to check out the <a href="../fragment-jquery-ui-tabs/">jQuery UI Tabs</a> example.</p>
  226. <pre class="brush:js">
  227. <?= htmlspecialchars( $shell['script'] ); ?>
  228. </pre>
  229. <?
  230. $shell['html_body'] = ob_get_contents();
  231. ob_end_clean();
  232. // ========================================================================== //
  233. // DRAW SHELL
  234. // ========================================================================== //
  235. draw_shell();
  236. ?>