index.js 5.6 KB

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