index.js 22 KB

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