Guest User

Untitled

a guest
Jan 22nd, 2018
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.34 KB | None | 0 0
  1. //////// Global variables ////////
  2. /// The current hash, in the format #hash_here
  3. var current_hash = "";
  4. /// The tabs, all of them, every last one. When a new tab is requested,
  5. /// this variable is checked for the existence of that tab. If it does
  6. /// not exist, the content is pulled from the server and dumped in here.
  7. /// If it does exist, the content is pulled from this variable. This
  8. /// way, you pull the minimum amount of data from the server as possible.
  9. // uses the php function to minimize the amount of calls to the server.
  10. //<?php admin::print_js_site(); ?>
  11. var site = {
  12. title : "Farraginous Fumblings",
  13. modules : {
  14. "pages" : {
  15. id : "pages",
  16. title: "Pages",
  17. "pages" : {
  18. view : {
  19. id : "view",
  20. title : "View all pages",
  21. onload : function() { // todo: update pages list
  22. },
  23. content : "The view page",
  24. loaded : true
  25. },
  26. new_page : {
  27. id : "new_page",
  28. title : "Create a new page",
  29. content : "loading...",
  30. loaded : false
  31. }
  32. },
  33. current_page : "view"
  34. }
  35. },
  36. current_module : "pages",
  37. default_module : "pages",
  38.  
  39. has_module : function (id) {
  40. if ( this.modules[id] ) { return true; }
  41. else { return false; }
  42. },
  43. set_module : function (id) {
  44. if ( this.has_module( id ) ) {
  45. this.current_module = id;
  46. }
  47. else { alert("ERROR: set_module: no such module '" + id + "'." ); }
  48. },
  49. get_current_page : function () { return this.modules[this.current_module].pages[this.current_page]; },
  50. get_current_module : function () { return this.modules[this.current_module]; },
  51. has_page : function (id) { if ( this.get_current_module().pages[id] ) { return true; } else { return false; } },
  52. set_page : function (id) {
  53. if ( this.has_page( id ) ) {
  54. this.get_current_module().current_page = id;
  55. if ( this.get_current_module().pages[id].loaded == false ) {
  56. /*$.getJSON( "?admin_ajax=get_page&module="+get_current_module().id+"&page="id, function( data ) {
  57. this.get_current_module().pages[id].loaded = true;
  58. this.get_current_module().pages[id].content = data;
  59. });*/
  60. // get contents, then update.
  61. }
  62. else {
  63. if ( this.get_current_module().pages[id].onload ) {
  64. this.get_current_module().pages[id].onload();
  65. }
  66. update();
  67. }
  68. }
  69. else { alert("ERROR: set_page: no such page '" + id +"' in module '" + this.get_current_module().id + "'." ); }
  70. },
  71.  
  72. url_change : function (name) {
  73. //alert( "hash changed to '" + current_hash + "'." );
  74. name = name.split('/');
  75. if ( name[0] ) {
  76. if ( this.has_module( name[0] ) ) {
  77. if ( name[0] != this.get_current_module().id ) {
  78. this.set_module( name[0] );
  79. if ( name[1] ) {
  80. if ( this.has_page( name[1] ) ) {
  81. alert(" hello sweetie");
  82. this.set_page( name[1] );
  83. update();
  84. }
  85. // TOOD error
  86. else { alert( "Error: page '" + name[1] + "' doesn't exist. Don't know why. Sorry." ); }
  87. }
  88. else {
  89. alert("oh uh");
  90. this.set_page( this.get_current_module().current_page );
  91. update();
  92. }
  93. }
  94. }
  95. else { alert( "Error: module '" + name[0] + "' doesn't exist. Haha." ); }
  96. }
  97. else {
  98. this.set_module( site.default_module );
  99. this.set_page( site.get_current_module().current_page );
  100. update();
  101. }
  102. }
  103. }
  104.  
  105. /*
  106. * ##############################3
  107. * Should load tabs & their pages list. Should only REQUEST a tab's page.
  108. * WHY? Because that way, it'll be neater. Perhaps load all of a tabs pages
  109. * probably not though.
  110. * Need to provide a php ajax for getting a tab and getting all tabs
  111. * Maybe restructure thing to include site variables as well, ie. site.title, site.modules["page"].title
  112. * Yeah, I like that.
  113. * >> I like this better
  114. * site.current_module.title
  115. * site.title
  116. * site.modules["page"].title
  117. * site.current_module.title
  118. * site.current_module.current_page.title
  119. * site.current_module.pages["view"].title
  120. * ##############################
  121. */
  122.  
  123. //////// The startup function ////////
  124. /// Initialises the [tabs] and sets the [current_hash].
  125. $(document).ready( function() {
  126. if ( window.location.hash ) {
  127. current_hash = window.location.hash;
  128. }
  129. site.url_change( window.location.hash.substr(1) );
  130. update();
  131. setInterval( "check_hash()", 150);
  132. });
  133.  
  134. //////// Functions ////////
  135. /// check_hash checks if the hash has changed, and if it has, it calls
  136. /// load_content().
  137. function check_hash() {
  138. if ( window.location.hash == current_hash ) {
  139. return;
  140. }
  141. // else there must be a different hash, so new content to load
  142. current_hash = window.location.hash;
  143. // load the content - .substr removes the # from the hash.
  144. site.url_change( window.location.hash.substr(1) );
  145. }
  146. /// Updates the front-page content.
  147. function update() {
  148. site.get_current_module();
  149. $("._page_title").html( site.get_current_page().title );
  150. $("._page_content").html( site.get_current_page().content );
  151. page_menu = "<ul>";
  152. for( i in site.get_current_module().pages ) {
  153. page_menu += '<li><a href="#'
  154. +site.get_current_module().id + '/'
  155. + site.get_current_module().pages[i].id
  156. + '">'
  157. + site.get_current_module().pages[i].title
  158. + '</a></li>';
  159. }
  160. page_menu += "</ul>";
  161. $("._page_menu").html( page_menu );
  162. module_menu = "<ul>";
  163. for ( i in site.modules ) {
  164. module_menu += '<li><a href="#'+ site.modules[i].id + '">'
  165. + site.modules[i].title + '</a></li>';
  166. }
  167. module_menu += "</ul>";
  168. $("._module_menu").html( module_menu );
  169. $("._module_title").html(
  170. site.get_current_module().title
  171. );
  172. }
Add Comment
Please, Sign In to add comment