index.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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. if (!window.location.origin) {
  109. window.location.origin = window.location.protocol + "//" + window.location.hostname + (window.location.port ? ':' + window.location.port: '');
  110. }
  111. },
  112. equal = function(o1,o2){
  113. for(var i in o1){
  114. if(!exists(o2[i])||o2[i]!=o1[i]){
  115. return false;
  116. }
  117. }
  118. for(i in o2){
  119. if(!exists(o1[i])||o2[i]!=o1[i]){
  120. return false;
  121. }
  122. }
  123. return true;
  124. },
  125. render = window.render = {
  126. topbar: function(t,c){
  127. $('#topbar').html(Handlebars.compile(t)(c));
  128. render.links('#topbar');
  129. },
  130. content: function(t,c){
  131. $('#content').html(
  132. Handlebars.compile(t)(c)
  133. );
  134. render.links('#content');
  135. },
  136. links: function(selector){
  137. $(selector).find('a').each(function(){
  138. var href = this.href;
  139. if(href.indexOf('#')!=-1&&(href.indexOf(location.origin)!=-1||href.indexOf('#')==0)){
  140. href = href.substr(href.indexOf('#')+1);
  141. $(this).click(function(e){
  142. try{
  143. if(($(this).hasClass('topbar-home') || $(this).hasClass('topbar-back'))&&$(window).width()<767){
  144. $('#topbar').children('div.topbar-right,div.topbar-left').toggle();
  145. }else if($(this).hasClass('topbar-history')){
  146. History.back();
  147. }else{
  148. loadState(href);
  149. }
  150. }catch(error){
  151. console.error(error);
  152. }
  153. e.preventDefault();
  154. return false;
  155. });
  156. }
  157. });
  158. }
  159. };
  160. if(exists($.cookie('key'))){
  161. setKey($.cookie('key'));
  162. }else{
  163. setKey(null);
  164. }
  165. $(document).ready(function(){
  166. $(window).on('statechange',function(){
  167. getNewState();
  168. if(!equal(State.data,Old)){
  169. document.title = State.title;
  170. switch(State.data.type){
  171. case 'page':case 'user':
  172. apiCall(State.data,function(d){
  173. if(exists(d.context)){
  174. if(!exists(d.context.key)&&Key!==null){
  175. console.log('Context detected logout');
  176. setKey(null);
  177. }
  178. render.topbar(d.topbar.template,d.topbar.context);
  179. render.content(d.template,d.context);
  180. $('#loading').hide();
  181. }else{
  182. console.error('No context given');
  183. History.back();
  184. }
  185. });
  186. break;
  187. case 'action':break;
  188. default:
  189. error({
  190. url: State.url,
  191. error: "Something went wrong!"
  192. });
  193. }
  194. Old = State.data;
  195. }else{
  196. console.log(State.data,Old);
  197. console.warn('Stopped double load of '+Old.type+'-'+Old.id);
  198. $('#loading').hide();
  199. }
  200. });
  201. if($.isEmptyObject(State.data)){
  202. History.replaceState({
  203. type: 'page',
  204. id: 'index'
  205. },'Bugs','page-index');
  206. console.log('Forcing default state.');
  207. }else{
  208. flag('load',true);
  209. }
  210. var data = {
  211. get: 'settings',
  212. timestamp: +new Date
  213. };
  214. $.get(location.href,data,function(d){
  215. settings = d;
  216. apiState(location.href,function(){
  217. if(flag('load')){
  218. State.data = {
  219. type: '',
  220. data: ''
  221. };
  222. }
  223. flag('load',false);
  224. });
  225. },'json');
  226. $('#content').niceScroll({
  227. cursorwidth: 10
  228. });
  229. document.body.addEventListener('touchstart',function(e){
  230. e.preventDefault();
  231. });
  232. });
  233. $(window).resize(function(){
  234. if($(window).width()>767){
  235. $('#topbar div.topbar-right, #topbar div.topbar-left').css({
  236. 'display': ''
  237. });
  238. }
  239. });
  240. shortcut.add('f12',function(){
  241. if(!flag('firebug-lite')){
  242. (function(F,i,r,e,b,u,g,L,I,T,E){
  243. if(F.getElementById(b))
  244. return;
  245. E=F[i+'NS']&&F.documentElement.namespaceURI;
  246. E=E?F[i+'NS'](E,'script'):F[i]('script');
  247. E[r]('id',b);
  248. E[r]('src',I+g+T);
  249. E[r](b,u);
  250. (F[e]('head')[0]||F[e]('body')[0]).appendChild(E);
  251. E=new Image;
  252. E[r]('src',I+L);
  253. })(document,'createElement','setAttribute','getElementsByTagName','FirebugLite','4','firebug-lite.js','releases/lite/latest/skin/xp/sprite.png','https://getfirebug.com/','#startOpened');
  254. flag('firebug-lite',true);
  255. }
  256. });
  257. $.fn.serializeObject = function(){
  258. var o = {},
  259. a = this.serializeArray();
  260. $.each(a,function(){
  261. if(o[this.name] !== undefined){
  262. if(!o[this.name].push){
  263. o[this.name] = [o[this.name]];
  264. }
  265. o[this.name].push(this.value || '');
  266. }else{
  267. o[this.name] = this.value || '';
  268. }
  269. });
  270. return o;
  271. };
  272. $.ajaxSetup({
  273. async: false
  274. });
  275. })(jQuery,History);