index.js 4.7 KB

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