index.js 23 KB

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