index.js 3.7 KB

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