index.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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 if($(this).hasClass('topbar-history')){
  144. History.back()
  145. }else{
  146. loadState(href);
  147. }
  148. return false;
  149. });
  150. }
  151. });
  152. }
  153. };
  154. if(exists($.cookie('key'))){
  155. setKey($.cookie('key'));
  156. }else{
  157. setKey(null);
  158. }
  159. $(document).ready(function(){
  160. $(window).on('statechange',function(){
  161. getNewState();
  162. if(!equal(State.data,Old)){
  163. document.title = State.title;
  164. switch(State.data.type){
  165. case 'page':case 'user':
  166. apiCall(State.data,function(d){
  167. if(exists(d.context)){
  168. if(!exists(d.context.key)&&Key!==null){
  169. console.log('Context detected logout');
  170. setKey(null);
  171. }
  172. render.topbar(d.topbar.template,d.topbar.context);
  173. render.content(d.template,d.context);
  174. $('#loading').hide();
  175. }else{
  176. console.error('No context given');
  177. History.back();
  178. }
  179. });
  180. break;
  181. case 'action':break;
  182. default:
  183. error({
  184. url: State.url,
  185. error: "Something went wrong!"
  186. });
  187. }
  188. Old = State.data;
  189. }else{
  190. console.log(State.data,Old);
  191. console.warn('Stopped double load of '+Old.type+'-'+Old.id);
  192. $('#loading').hide();
  193. }
  194. });
  195. if($.isEmptyObject(State.data)){
  196. History.replaceState({
  197. type: 'page',
  198. id: 'index'
  199. },'Bugs','page-index');
  200. console.log('Forcing default state.');
  201. }else{
  202. flag('load',true);
  203. }
  204. var data = {
  205. get: 'settings',
  206. timestamp: +new Date
  207. };
  208. $.get(location.href,data,function(d){
  209. settings = d;
  210. apiState(location.href,function(){
  211. if(flag('load')){
  212. State.data = {
  213. type: '',
  214. data: ''
  215. };
  216. }
  217. flag('load',false);
  218. });
  219. },'json');
  220. });
  221. $(window).resize(function(){
  222. if($(window).width()>767){
  223. $('#topbar div.topbar-right, #topbar div.topbar-left').css({
  224. 'display': ''
  225. });
  226. }
  227. });
  228. $.fn.serializeObject = function(){
  229. var o = {},
  230. a = this.serializeArray();
  231. $.each(a,function(){
  232. if(o[this.name] !== undefined){
  233. if(!o[this.name].push){
  234. o[this.name] = [o[this.name]];
  235. }
  236. o[this.name].push(this.value || '');
  237. }else{
  238. o[this.name] = this.value || '';
  239. }
  240. });
  241. return o;
  242. };
  243. $.ajaxSetup({
  244. async: false
  245. });
  246. })(jQuery,History);