index.js 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869
  1. // TODO - Add initial page loading and handlers
  2. (function($,History,console){
  3. var State = History.getState(),
  4. Old = {},
  5. Key = null,
  6. flags = [],
  7. templates = [],
  8. flag = window.flag = function(name,value){
  9. if(exists(value)){
  10. flags[name] = value;
  11. }else{
  12. return exists(flags[name])?flags[name]:false;
  13. }
  14. },
  15. settings = {},
  16. exists = function(v){
  17. return typeof v != 'undefined';
  18. },
  19. get = window.get = function(s){
  20. return settings[s];
  21. },
  22. set = window.set = function(s,v){
  23. settings[s] = v;
  24. return v;
  25. },
  26. setKey = window.setKey = function(key){
  27. if(key !== null){
  28. console.log('Key change to '+key);
  29. Key = key;
  30. var d = new Date();
  31. d.setTime(d.getTime()+get('expire'));
  32. $.cookie('key',key,{
  33. expires: d
  34. });
  35. }else{
  36. console.log('Key deleted');
  37. Key = null;
  38. $.removeCookie('key');
  39. }
  40. },
  41. getKey = window.getKey = function(){
  42. return Key;
  43. },
  44. templateHash = function(type,name){
  45. for(var i in templates){
  46. if(templates[i].name == name && templates[i].type == type){
  47. return templates[i].hash;
  48. }
  49. }
  50. return false;
  51. },
  52. template = window.template = function(type,name,template,hash){
  53. var id = (function(type,name){
  54. for(var i in templates){
  55. if(templates[i].name == name && templates[i].type == type){
  56. return i;
  57. }
  58. }
  59. return false;
  60. })(type,name);
  61. if(exists(template)){
  62. if(template === null){
  63. if(id!==false){
  64. templates.splice(id,1);
  65. }
  66. $.localStorage('templates',templates);
  67. console.log('Dropping template for '+type+':'+name);
  68. return '';
  69. }else{
  70. var o = {
  71. name: name,
  72. template: template,
  73. type: type,
  74. hash: typeof hash == 'undefined'?'':hash
  75. };
  76. if(id===false){
  77. console.log('Storing new template for '+type+':'+name);
  78. templates.push(o);
  79. }else{
  80. console.log('Replacing old template for '+type+':'+name);
  81. templates[id] = o;
  82. }
  83. $.localStorage('templates',templates);
  84. }
  85. }else if(id!==false){
  86. console.log('Using cached template for '+type+':'+name);
  87. return templates[id].template;
  88. }else{
  89. console.log('No cached template stored for: '+type+':'+name);
  90. return '';
  91. }
  92. },
  93. apiCall = window.apiCall = function(data,callback,background){
  94. console.log('apiCall('+data.type+'-'+data.id+')');
  95. if(!flag('error')){
  96. if(exists(background)&&!background){
  97. loading(true);
  98. }
  99. data.get = 'api';
  100. data.back = State.data.back;
  101. data.timestamp = +new Date;
  102. $.get(location.href,data,function(d){
  103. if(exists(d['error'])){
  104. error(d);
  105. }
  106. if(exists(d.state)){
  107. d.state.title = (d.state.title+'').capitalize();
  108. if(location.href.substr(location.href.lastIndexOf('/')+1) != d.state.url && d.state.url !== ''){
  109. console.log('Forced redirection to '+d.state.url);
  110. flag('handled',true);
  111. d.state.data.back = State.data.back;
  112. History.replaceState(d.state.data,d.state.title,d.state.url);
  113. callback = function(){
  114. location.reload();
  115. };
  116. }
  117. document.title = d.state.title;
  118. loading(false);
  119. }
  120. if(exists(callback)){
  121. console.log('Running apiCall callback');
  122. try{
  123. callback(d);
  124. }catch(e){
  125. error(e);
  126. }
  127. }
  128. },'json');
  129. }
  130. },
  131. loadState = window.loadState = function(href,callback){
  132. console.log('loadState('+href+')');
  133. if(!flag('error')){
  134. loading(true);
  135. var data = {
  136. get:'state',
  137. timestamp: +new Date,
  138. back: location.href
  139. };
  140. ajax = $.ajax(href,{
  141. data: data,
  142. async: true,
  143. type: 'GET',
  144. success: function(d){
  145. if(exists(d['error'])){
  146. error(d);
  147. }else{
  148. d.state.title = d.state.title.capitalize();
  149. d.state.url = href;
  150. console.log('pushState: '+d.state.title+'['+href+']');
  151. flag('handled',true);
  152. History.pushState(d.state.data,d.state.title,href);
  153. getNewState();
  154. }
  155. if(exists(callback)){
  156. callback(d);
  157. }
  158. if(!exists(d['error'])){
  159. flag('handled',true);
  160. stateChange();
  161. }
  162. },
  163. error: function(x,t,e){
  164. error({
  165. error: '['+t+'] '+e
  166. });
  167. if(exists(callback)){
  168. callback({
  169. error: '['+t+'] '+e
  170. });
  171. }
  172. },
  173. dataType: 'json'
  174. });
  175. }
  176. },
  177. replaceState = window.replaceState = function(href,callback){
  178. console.log('apiState('+href+')');
  179. if(!flag('error')){
  180. loading(true);
  181. var data = {
  182. get:'state',
  183. timestamp: +new Date,
  184. back: State.data.back
  185. };
  186. $.ajax(href,{
  187. data: data,
  188. async: true,
  189. type: 'GET',
  190. success: function(d){
  191. if(exists(d['error'])){
  192. error(d);
  193. }else{
  194. d.state.title = d.state.title.capitalize();
  195. d.state.url = href;
  196. flag('handled',true);
  197. console.log('pushState: '+d.state.title+'['+href+']');
  198. History.replaceState(d.state.data,d.state.title,href);
  199. getNewState();
  200. }
  201. if(exists(callback)){
  202. callback(d);
  203. }
  204. console.log(d.state.title);
  205. if(!exists(d['error'])){
  206. flag('handled',true);
  207. stateChange();
  208. }
  209. },
  210. error: function(x,t,e){
  211. error({
  212. error: '['+t+'] '+e
  213. });
  214. if(exists(callback)){
  215. callback({
  216. error: '['+t+'] '+e
  217. });
  218. }
  219. },
  220. dataType: 'json'
  221. });
  222. }
  223. },
  224. error = window.error = function(e,callback){
  225. if(!flag('error')){
  226. flag('error',true);
  227. var msg = '['+State.url+'] '+(typeof e.error != 'undefined'?e.error:e);
  228. console.error((msg.trim()+"\n"+(exists(e.state)?JSON.stringify(e.state):'')).trim());
  229. alert(msg.trim(),'Error',callback);
  230. console.trace();
  231. console.log(e);
  232. }
  233. },
  234. getNewState = function(state){
  235. State = History.getState();
  236. console.log("State change: ["+State.title+"] "+JSON.stringify(State.data));
  237. if(!window.location.origin){
  238. window.location.origin = window.location.protocol + "//" + window.location.hostname + (window.location.port ? ':' + window.location.port: '');
  239. }
  240. },
  241. stateChange = function(){
  242. getNewState();
  243. if(!equal(State.data,Old)){
  244. switch(State.data.type){
  245. case 'page':case 'user':case 'project':case 'issue':case 'scrum':
  246. apiCall(State.data,function(d){
  247. if(!exists(d.error)){
  248. if(exists(d.context)){
  249. if(!exists(d.context.key)&&Key!==null){
  250. console.log('Context detected console.logout');
  251. setKey(null);
  252. }
  253. render.topbar(template('topbars',d.topbar.template),d.topbar.context);
  254. if(exists(d.template)){
  255. console.log('Using template: '+d.template.type+':'+d.template.name);
  256. d.template = template(d.template.type,d.template.name);
  257. render.content(d.template,d.context);
  258. }else{
  259. console.log('No template used');
  260. }
  261. $(window).resize();
  262. loading(false);
  263. }else if(d.state.url != location.href){
  264. console.error('No context given');
  265. console.log(d);
  266. back();
  267. }
  268. }else{
  269. error(d);
  270. back();
  271. }
  272. flag('handled',false);
  273. });
  274. break;
  275. case 'action':break;
  276. default:
  277. error({
  278. url: State.url,
  279. error: "Unable to make a request of type "+State.data.type
  280. });
  281. }
  282. Old = State.data;
  283. }else{
  284. console.log(State.data,Old);
  285. console.warn('Stopped double load of '+Old.type+'-'+Old.id);
  286. loading(false);
  287. flag('handled',false);
  288. }
  289. },
  290. equal = function(o1,o2){
  291. for(var i in o1){
  292. if(!exists(o2[i])||o2[i]!=o1[i]){
  293. return false;
  294. }
  295. }
  296. for(i in o2){
  297. if(!exists(o1[i])||o2[i]!=o1[i]){
  298. return false;
  299. }
  300. }
  301. return true;
  302. },
  303. render = window.render = {
  304. topbar: function(t,c){
  305. $('#topbar').html(Handlebars.compile(t)(c));
  306. if(State.url == location.origin+'/page-index'){
  307. $('#topbar').find('.topbar-history').hide();
  308. }
  309. $('#topbar').addClass('overflow-hide');
  310. render.refresh('#topbar');
  311. },
  312. content: function(t,c){
  313. $(document).unbind('ready');
  314. $('#content').html(
  315. Handlebars.compile(t)(c)
  316. );
  317. render.form('#content');
  318. render.accordions('#content');
  319. render.refresh('#content');
  320. },
  321. refresh: function(selector){
  322. render.links(selector);
  323. render.buttons(selector);
  324. render.menus(selector);
  325. render.time(selector);
  326. $(window).resize();
  327. },
  328. time: function(selector){
  329. $(selector).find('time.timeago').each(function(){
  330. var time = new Date($(this).text()*1000);
  331. $(this).replaceWith(
  332. $('<abbr>').attr({
  333. 'title': time.toISOString(),
  334. 'style': $(this).attr('style')
  335. }).addClass($(this).attr('class')).timeago()
  336. );
  337. });
  338. },
  339. accordions: function(selector){
  340. $(selector).find('.accordion').each(function(){
  341. var icons = {};
  342. if($(this).children('.icons').length == 1){
  343. icons = JSON.parse($(this).children('.icons').text());
  344. }
  345. $(this).children('.icons').remove();
  346. $(this).accordion({
  347. collapsible: true,
  348. icons: icons,
  349. active: false
  350. });
  351. });
  352. },
  353. buttons: function(selector){
  354. $(selector).find('.button').button();
  355. $(selector).find('input[type=submit]').button();
  356. $(selector).find('input[type=button]').button();
  357. $(selector).find('button').button();
  358. $(selector).find('.more').off().each(function(){
  359. var t = $(this);
  360. if(!$.hasData(t)){
  361. t.data('type',t.text().trim());
  362. t.data('at',t.prev().children().length);
  363. t.text('Load More');
  364. }
  365. if(t.prev().children().length < 10){
  366. t.hide();
  367. }
  368. }).click(function(){
  369. var t = $(this),
  370. data = {
  371. type: 'action',
  372. id: 'more',
  373. pid: State.data.id,
  374. url: State.url,
  375. title: State.title,
  376. topbar: false,
  377. no_state: true,
  378. of: t.data('type'),
  379. at: Number(t.data('at'))
  380. };
  381. data.start = t.data('at');
  382. apiCall(data,function(d){
  383. var tmplt = Handlebars.compile(template('pages','comment')),
  384. i;
  385. if(d.messages.length < 10){
  386. t.hide();
  387. }
  388. for(i in d.messages){
  389. try{
  390. t.prev().append(
  391. tmplt(d.messages[i])
  392. );
  393. }catch(e){
  394. console.log("Could not load message: ",d.messages[i],e);
  395. }
  396. render.refresh(t.prev());
  397. }
  398. t.data('at',t.prev().children().length);
  399. },true);
  400. }).button();
  401. render.comment.buttons(selector);
  402. },
  403. comment: {
  404. buttons: function(selector){
  405. $(selector).find('.comment').each(function(){
  406. var context = JSON.parse($(this).text());
  407. $(this).text(context.text);
  408. $(this).button();
  409. $(this).click(function(){
  410. render.comment.dialog(context.id,context.type,context.title);
  411. });
  412. });
  413. },
  414. dialog: function(id,type,title){
  415. loading(true);
  416. flag('ignore_statechange',true);
  417. $('#comment').find('form').find('input[name=comment_type]').val(type);
  418. $('#comment').find('form').find('input[name=comment_id]').val(id);
  419. $('#comment').find('form').find('textarea[name=message]').val('');
  420. $('#comment').dialog({
  421. close: function(){
  422. $('#comment').find('form').find('input[name=comment_type]').val('');
  423. $('#comment').find('form').find('input[name=comment_id]').val('');
  424. flag('ignore_statechange',false);
  425. loading(false);
  426. },
  427. resizable: false,
  428. draggable: false,
  429. title: title,
  430. buttons: [
  431. {
  432. text: 'Ok',
  433. class: 'recommend-force',
  434. click: function(){
  435. var diag = $(this),
  436. context = diag.find('form').serializeObject();
  437. if(context.message !== ''){
  438. context.type = 'action';
  439. context.id = 'comment';
  440. context.url = State.url;
  441. context.title = State.title;
  442. apiCall(context,function(d){
  443. if(!exists(d.error)){
  444. diag.dialog('close');
  445. flag('ignore_statechange',false);
  446. $('.topbar-current').click();
  447. }
  448. });
  449. }
  450. }
  451. },{
  452. text: 'Cancel',
  453. class: 'cancel-force',
  454. click: function(){
  455. $(this).dialog('close');
  456. }
  457. }
  458. ]
  459. });
  460. },
  461. },
  462. menus: function(selector){
  463. $(selector).find('.menu').css({
  464. 'list-style':'none'
  465. }).menu({
  466. icons:{
  467. submenu: "ui-icon-circle-triangle-e"
  468. }
  469. }).removeClass('ui-corner-all').addClass('ui-corner-bottom').parent().click(function(e){
  470. e.stopPropagation();
  471. });
  472. },
  473. form: function(selector){
  474. $(selector).find('#form').width('320px').children();
  475. render.inputs(selector);
  476. },
  477. inputs: function(selector){
  478. $(selector).find('input[type=text],input[type=password]').each(function(){
  479. var input = $(this),
  480. height = input.height()>=17?17:input.height();
  481. input.siblings('.input-clear').remove();
  482. input.off('focus').off('blur').after(
  483. $('<div>').css({
  484. position: 'absolute',
  485. right: $(window).width() - (input.outerWidth() + input.position().left)+2,
  486. top: input.position().top+2,
  487. 'background-image': 'url(img/headers/icons/clear.png)',
  488. 'background-position': 'center',
  489. 'background-size': height+'px '+height+'px',
  490. 'background-repeat': 'no-repeat',
  491. width: input.height(),
  492. height: input.height(),
  493. cursor: 'pointer'
  494. }).hide().addClass('input-clear').mousedown(function(){
  495. input.val('');
  496. })
  497. );
  498. input.focus(function(){
  499. input.next().show();
  500. }).blur(function(e){
  501. input.next().hide();
  502. });
  503. });
  504. $(selector).find('input[type=text],input[type=password],textarea').each(function(){
  505. var input = $(this);
  506. if(input.hasClass('fill-width')){
  507. input.css('width','calc(100% - '+(input.outerWidth()-input.width())+'px)');
  508. }
  509. });
  510. },
  511. dialog: function(selector,title){
  512. $(selector).dialog({
  513. close: function(){
  514. flag('error',false);
  515. var fn = $(this).data('callback');
  516. if(exists(fn)){
  517. fn();
  518. }
  519. loading(false);
  520. },
  521. resizable: false,
  522. draggable: false,
  523. title: title,
  524. buttons: [
  525. {
  526. text: 'Ok',
  527. class: 'recommend-force',
  528. click: function(){
  529. $(this).dialog('close');
  530. }
  531. }
  532. ]
  533. });
  534. },
  535. links: function(selector){
  536. $(selector).find('a').each(function(){
  537. var href = this.href;
  538. if(href.indexOf('#')!=-1&&(href.indexOf(location.origin)!=-1||href.indexOf('#')==0)){
  539. href = href.substr(href.indexOf('#')+1);
  540. $(this).click(function(e){
  541. try{
  542. if(($(this).hasClass('topbar-home') || $(this).hasClass('topbar-back'))&&$(window).width()<767){
  543. $('#topbar').children('div.topbar-right,div.topbar-left').toggle();
  544. $('#topbar').toggleClass('overflow-hide');
  545. $(window).resize();
  546. }else if($(this).hasClass('topbar-history')){
  547. back();
  548. }else if($(this).hasClass('topbar-current')){
  549. replaceState(href);
  550. }else{
  551. loadState(href);
  552. }
  553. }catch(error){
  554. console.error(error);
  555. }
  556. e.preventDefault();
  557. return false;
  558. });
  559. }
  560. });
  561. }
  562. },
  563. back = window.back = function(reload){
  564. console.log('reload',exists(reload));
  565. if(exists(State.data.back)){
  566. flag('handled',true);
  567. if(!History.back()){
  568. loadState(State.data.back);
  569. }else{
  570. stateChange();
  571. }
  572. }else if(State.data.type != 'page' || State.data.id != 'index'){
  573. if(exists(reload)){
  574. console.log('FORCING RELOAD');
  575. location.assign('page-index');
  576. }else{
  577. console.log('FORCING LOADSTATE');
  578. loadState('page-index');
  579. }
  580. }else{
  581. location.reload();
  582. }
  583. },
  584. alert = function(text,title,callback){
  585. if(exists(text)){
  586. title=exists(title)?title:'';
  587. callback=exists(callback)?callback:function(){};
  588. $('#dialog').text(text).data('callback',callback);
  589. render.dialog('#dialog',title,callback);
  590. }
  591. },
  592. hasFocus = function(){
  593. if(typeof document.hasFocus === 'undefined'){
  594. document.hasFocus = function(){
  595. return document.visibilityState == 'visible';
  596. }
  597. }
  598. return document.hasFocus();
  599. },
  600. notify = window.notify = function(title,text,onclick,onclose){
  601. var notification;
  602. if(exists(window['Notification'])&&!exists(window.webkitNotifications)&&!flag('default_notify')&&!hasFocus()){
  603. if(Notification.permission === 'denied'){
  604. flag('default_notify',true);
  605. notify(title,text,onclick,onclose);
  606. }else if(Notification.permission === 'granted'){
  607. notification = new Notification(title,{
  608. body: text,
  609. icon: location.origin+'/img/favicon.ico'
  610. });
  611. notification.onclick = onclick;
  612. notification.onclose = onclose;
  613. }else{
  614. Notification.requestPermission(function(p){
  615. console.log('permission for notify: '+p);
  616. Notification.permission = p;
  617. notify(title,text,onclick,onclose);
  618. });
  619. }
  620. }else if(exists(window.navigator.mozNotification)&&!hasFocus()){
  621. notification = window.navigator.mozNotification.createNotification(title,text,location.origin+'/img/favicon.ico');
  622. notification.onclick = onclick;
  623. notification.onclose = onclose;
  624. notification.show();
  625. }else{
  626. $('#notification-container').notify('create',{
  627. title: title,
  628. text: text,
  629. click: onclick,
  630. close: onclose
  631. });
  632. }
  633. },
  634. loading = function(state){
  635. if(!flag('ignore_statechange')){
  636. state = exists(state)?state:false;
  637. console.log('loading state '+state);
  638. if(!flag('error') && !state){
  639. $('#loading').hide();
  640. }else if(state){
  641. $('#loading').show();
  642. }
  643. }
  644. },
  645. debug = window.debug = {
  646. hardReload: function(){
  647. debug.clearCache();
  648. location.reload();
  649. },
  650. clearCache: function(){
  651. templates = [];
  652. $.localStorage('templates',null);
  653. console.log('Templates cleared.');
  654. },
  655. manifesto: function(){
  656. if(!flag('manifesto')){
  657. if(window.applicationCache){
  658. if(window.applicationCache.status==window.applicationCache.UNCACHED){
  659. $('head').append(
  660. $('<script>').attr({
  661. 'type': 'text/javascript',
  662. 'src': 'http://manifesto.ericdelabar.com/manifesto.js?x="+(Math.random())'
  663. })
  664. );
  665. (function wait(){
  666. if($('#cacheStatus').length === 0){
  667. setTimeout(wait,10);
  668. }else{
  669. $('#cacheStatus').niceScroll();
  670. }
  671. })();
  672. }else{
  673. alert("Manifest file is valid.");
  674. }
  675. }else{
  676. alert("This browser does not support HTML5 Offline Application Cache.");
  677. }
  678. flag('manifesto',true);
  679. }
  680. },
  681. firebug: function(){
  682. if(!flag('firebug-lite')){
  683. $('head').append(
  684. $('<script>').attr({
  685. 'type': 'text/javascript',
  686. 'src': 'https://getfirebug.com/firebug-lite.js#startOpened',
  687. 'id': 'FirebugLite'
  688. })
  689. );
  690. $('<image>').attr('src','https://getfirebug.com/releases/lite/latest/skin/xp/sprite.png');
  691. flag('firebug-lite',true);
  692. }
  693. }
  694. },
  695. getTemplates = function(callback){
  696. var fn = function(type,callback){
  697. $.get('api.php',{
  698. type: 'manifest',
  699. id: type
  700. },function(d){
  701. if(!exists(d.error)){
  702. var count = d.manifest.length;
  703. for(var i in d.manifest){
  704. console.log('Loading template('+(Number(i)+1)+'/'+d.manifest.length+'): '+d.manifest[i].name);
  705. if(templateHash(type,d.manifest[i].name) !== d.manifest[i].hash){
  706. $.get('api.php',{
  707. type: 'template',
  708. id: type,
  709. name: d.manifest[i].name
  710. },function(d){
  711. template(d.type,d.name,d.template,d.hash);
  712. $.localStorage('templates',templates);
  713. count--;
  714. console.log('Loaded template('+count+' left): '+d.name);
  715. },'json');
  716. }else{
  717. count--;
  718. }
  719. }
  720. setTimeout(function wait_for_templates(){
  721. if(count === 0){
  722. console.log('getTemplates callback');
  723. callback();
  724. }else{
  725. setTimeout(wait_for_templates,10);
  726. }
  727. },10);
  728. }else{
  729. error(d.error);
  730. }
  731. },'json');
  732. };
  733. fn('topbars',function(){
  734. fn('pages',function(){
  735. callback();
  736. });
  737. });
  738. };
  739. if(exists($.cookie('key'))){
  740. setKey($.cookie('key'));
  741. }else{
  742. setKey(null);
  743. }
  744. $(document).ready(function(){
  745. if(exists(window['Notification'])&&exists(Notification.permission)&&Notification.permission !== 'granted'){
  746. Notification.requestPermission();
  747. }
  748. $.ajaxSetup({
  749. async: false,
  750. cache: false,
  751. timeout: 30000 // 30 seconds
  752. });
  753. $(document).ajaxError(function(event, request, settings) {
  754. error({error:'Request timed out'});
  755. });
  756. if(!exists($.support.touch)){
  757. $.support.touch = 'ontouchstart' in window || 'onmsgesturechange' in window;
  758. }
  759. $('#content,#topbar').click(function(){
  760. $('.menu').hide();
  761. });
  762. $(window).resize(function(){
  763. if($(window).width()>767){
  764. $('#topbar div.topbar-right, #topbar div.topbar-left').css({
  765. 'display': ''
  766. });
  767. }
  768. render.inputs('#content');
  769. render.inputs('#topbar');
  770. });
  771. $.get(location.href,{
  772. get: 'settings',
  773. timestamp: +new Date,
  774. back: false,
  775. no_state: true
  776. },function(d){
  777. if(!exists(d.error)){
  778. settings = d.settings;
  779. if(d.version != $.localStorage('version')){
  780. $.localStorage('version',d.version);
  781. $.localStorage('templates',null);
  782. templates = [];
  783. }else{
  784. templates = $.localStorage('templates');
  785. if(templates === null){
  786. templates = [];
  787. }
  788. }
  789. getTemplates(function(){
  790. replaceState(location.href);
  791. (function notifications(){
  792. var context = State;
  793. context.type = 'action';
  794. context.id = 'notifications';
  795. context.url = State.url;
  796. context.title = State.title;
  797. context.topbar = false;
  798. context.no_state = true;
  799. apiCall(context,function(d){
  800. if(!exists(d.error)){
  801. if(d.count>0 && $.localStorage('last_pm_check') < d.timestamp){
  802. notify('Alert','You have '+d.count+' new message'+(d.count>1?'s':''),function(){
  803. loadState('page-messages');
  804. });
  805. }
  806. $('.topbar-notifications').css('display',d.count>0?'block':'').text('('+d.count+')');
  807. $.localStorage('last_pm_check',d.timestamp);
  808. }
  809. setTimeout(notifications,5*1000); // every 5 seconds
  810. },true);
  811. })();
  812. });
  813. }else{
  814. error(d.error);
  815. }
  816. },'json');
  817. $(window).on('statechange',function(){
  818. if(!flag('handled')){
  819. console.log('unhandled state change event');
  820. console.trace();
  821. stateChange();
  822. }else{
  823. flag('handled',false);
  824. }
  825. });
  826. var getState = History.getState;
  827. History.getState = function(flag){
  828. if(exists(flag)){
  829. return State;
  830. }else{
  831. return getState.call(History);
  832. }
  833. };
  834. $('#notification-container').notify();
  835. $('body').css('margin-top','48px');
  836. setTimeout(function(){
  837. $('body').css('margin-top','0px').show();
  838. },100);
  839. });
  840. shortcut.add('f12',function(){
  841. debug.firebug();
  842. });
  843. shortcut.add('Ctrl+f12',function(){
  844. debug.manifesto();
  845. });
  846. shortcut.add('Shift+f12',function(){
  847. debug.clearCache();
  848. });
  849. $.fn.serializeObject = function(){
  850. var o = {},
  851. a = this.serializeArray();
  852. $.each(a,function(){
  853. if(o[this.name] !== undefined){
  854. if(!o[this.name].push){
  855. o[this.name] = [o[this.name]];
  856. }
  857. o[this.name].push(this.value || '');
  858. }else{
  859. o[this.name] = this.value || '';
  860. }
  861. });
  862. return o;
  863. };
  864. String.prototype.capitalize = function(lower) {
  865. return (lower?this.toLowerCase():this).replace(/(?:^|\s)\S/g, function(a){
  866. return a.toUpperCase();
  867. });
  868. };
  869. })(jQuery,History,console);