index.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. // TODO - Add initial page loading and handlers
  2. (function($,History){
  3. var State = History.getState(),
  4. Key = null,
  5. flag = false,
  6. settings = {},
  7. exists = function(v){
  8. return typeof v != 'undefined';
  9. },
  10. get = window.get = function(s){
  11. return settings[s];
  12. },
  13. set = window.set = function(s,v){
  14. settings[s] = v;
  15. return v;
  16. },
  17. setKey = window.setKey = function(key){
  18. if(key !== null){
  19. console.log('Key change to '+key);
  20. Key = key;
  21. var d = new Date();
  22. d.setTime(d.getTime()+get('timeout'));
  23. $.cookie('key',key,{
  24. expires: d
  25. });
  26. }else{
  27. console.log('Key deleted');
  28. Key = null;
  29. $.removeCookie('key');
  30. }
  31. },
  32. getKey = window.getKey = function(){
  33. return Key;
  34. },
  35. api = window.apiCall = function(data,callback){
  36. data.get = 'api';
  37. data.timestamp = +new Date;
  38. if(exists(State.data.key)){
  39. data.key = State.data.key;
  40. }
  41. $.get(location.href,data,function(d){
  42. if(exists(d['error'])){
  43. alert(d.error);
  44. }else{
  45. if(location.href.substr(location.href.lastIndexOf('/')+1) != d.state.url){
  46. History.pushState(d.state.data,d.state.title,d.state.url);
  47. }
  48. if(exists(callback)){
  49. callback(d);
  50. }
  51. }
  52. },'json');
  53. },
  54. loadState = window.loadState = function(href,callback){
  55. var data = {
  56. get:'state',
  57. timestamp:+new Date
  58. };
  59. if(Key !== null){
  60. data.key = Key;
  61. }
  62. $.get(href,data,function(d){
  63. History.pushState(d.state.data,document.title,href);
  64. State = History.getState();
  65. if(exists(callback)){
  66. callback();
  67. }
  68. },'json');
  69. },
  70. apiState = window.apiState = function(href,callback){
  71. var data = {
  72. get:'state',
  73. timestamp:+new Date
  74. };
  75. if(Key !== null){
  76. data.key = Key;
  77. }
  78. $.get(href,data,function(d){
  79. History.replaceState(d.state.data,document.title,href);
  80. State = History.getState();
  81. if(exists(callback)){
  82. callback();
  83. }
  84. },'json');
  85. };
  86. if(exists($.cookie('key'))){
  87. setKey($.cookie('key'));
  88. }else{
  89. setKey(null);
  90. }
  91. $(document).ready(function(){
  92. $(window).on('statechange',function(){
  93. var Old = State;
  94. State = History.getState();
  95. if(Key !== null){
  96. State.key = Key;
  97. State.data.key = Key;
  98. }else{
  99. if(exists(State.data['key'])){
  100. Key = State.data.key;
  101. }else if(exists(State['key'])){
  102. Key = State.key;
  103. }
  104. }
  105. if(State.data.type != Old.data.type || State.data.id != Old.data.id){
  106. console.log("State change. "+JSON.stringify(State));
  107. switch(State.data.type){
  108. case 'template':
  109. api(State.data,function(d){
  110. if(Key !== null){
  111. d.context.key = Key;
  112. }
  113. $('#content').html(Handlebars.compile(d.template)(d.context)).mCustomScrollbar('destroy');
  114. $('#content,.scroll').mCustomScrollbar({
  115. theme: 'dark-2',
  116. scrollInertia: 0
  117. });
  118. $('#content').find('a').each(function(){
  119. var href = this.href;
  120. if(href.indexOf(location.origin) != -1 && href.indexOf('#') != -1){
  121. href = href.substr(href.indexOf('#')+1);
  122. console.log('Setting up link to '+href);
  123. $(this).click(function(){
  124. loadState(href);
  125. return false;
  126. });
  127. }
  128. });
  129. });
  130. break;
  131. default:
  132. alert("Something went wrong!\nYour current state:\n"+JSON.stringify(State));
  133. }
  134. }
  135. });
  136. if($.isEmptyObject(State.data)){
  137. History.replaceState({
  138. type: 'template',
  139. id: 'index'
  140. },'Bugs','page-index');
  141. console.log('Forcing default state.');
  142. }else{
  143. flag = true;
  144. }
  145. var data = {
  146. get:'settings',
  147. timestamp:+new Date
  148. };
  149. if(Key !== null){
  150. data.key = Key;
  151. }
  152. $.get(location.href,data,function(d){
  153. settings = d;
  154. apiState(location.href,function(){
  155. if(flag){
  156. State.data = {
  157. type: '',
  158. data: ''
  159. };
  160. }
  161. $(window).trigger('statechange');
  162. });
  163. },'json');
  164. });
  165. $.fn.serializeObject = function(){
  166. var o = {},
  167. a = this.serializeArray();
  168. $.each(a,function(){
  169. if(o[this.name] !== undefined){
  170. if(!o[this.name].push){
  171. o[this.name] = [o[this.name]];
  172. }
  173. o[this.name].push(this.value || '');
  174. }else{
  175. o[this.name] = this.value || '';
  176. }
  177. });
  178. return o;
  179. };
  180. })(jQuery,History);