index.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573
  1. $(function(){
  2. "use strict";
  3. if(location.host != purl(__HOSTNAME__).attr('host')){
  4. location.href = __HOSTNAME__;
  5. }
  6. Pomo.domain = 'messages';
  7. Pomo.unescapeStrings = true;
  8. var _ = window._ = function(text){
  9. try{
  10. var t = Pomo.getText(text);
  11. return t.translation;
  12. }catch(e){
  13. return text;
  14. }
  15. },
  16. dialogs = $('#dialogs').children('div'),
  17. memos,
  18. news,
  19. templates = [],
  20. logout = function(){
  21. $.removeCookie('user',{
  22. path: '/'
  23. });
  24. $.removeCookie('key',{
  25. path: '/'
  26. });
  27. $.removeCookie('token',{
  28. path: '/'
  29. });
  30. $.removeCookie('PHPSESSID',{
  31. path: '/'
  32. });
  33. $.ajax(__HOSTNAME__+'site/api/',{
  34. data: {
  35. action: 'logout'
  36. },
  37. complete: function(){
  38. location.reload();
  39. },
  40. dataType: 'json'
  41. });
  42. },
  43. LANG = navigator.language,
  44. lang = Pomo.load(
  45. __HOSTNAME__+'site/api?action=lang',{
  46. format: 'po',
  47. mode: 'ajax'
  48. }
  49. ),
  50. lang_keys = (function(){
  51. var keys = [];
  52. $('body').find('*').contents().filter(function(){
  53. return this.nodeType === 3;
  54. }).each(function(){
  55. keys.push({
  56. node: this,
  57. key: this.nodeValue
  58. });
  59. });
  60. return keys;
  61. })(),
  62. has_key = function(node){
  63. for(var i in lang_keys){
  64. if(node === lang_keys[i].node){
  65. return true;
  66. }
  67. }
  68. return false;
  69. },
  70. get_key = function(node){
  71. for(var i in lang_keys){
  72. if(node === lang_keys[i].node){
  73. return lang_keys[i].key;
  74. }
  75. }
  76. return false;
  77. },
  78. translate = function(parent){
  79. $(parent).find('*').contents().filter(function(){
  80. return this.nodeType === 3;
  81. }).each(function(){
  82. if(!has_key(this)){
  83. lang_keys.push({
  84. node: this,
  85. key: this.nodeValue
  86. });
  87. }
  88. this.nodeValue = _(get_key(this));
  89. });
  90. $(parent).find('input[type=submit],input[type=button]').each(function(){
  91. if(this.tagName == 'INPUT' && this.type == 'submit'){
  92. if(!has_key(this)){
  93. lang_keys.push({
  94. node: this,
  95. key: this.value
  96. });
  97. }
  98. this.value = _(get_key(this));
  99. }
  100. });
  101. };
  102. lang.ready(function(){
  103. $('script[id^=template-]').each(function(){
  104. templates[this.id.substr(9)] = Handlebars.compile($(this).html());
  105. });
  106. Handlebars.registerHelper('html',function(body){
  107. return new Handlebars.SafeString(body.replace(/(\b(https?|ftps?|file|irc):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig,"<a class='link' href='$1'>$1</a>"));
  108. });
  109. $('form').submit(function(){
  110. var form = $(this),
  111. btn = form.children('input[type=submit]'),
  112. action = form.children('input[type=hidden][name=action]').val();
  113. $.ajax(__HOSTNAME__+'site/api/?'+form.serialize(),{
  114. success: function(d){
  115. if(d.log){
  116. console.log(d.log);
  117. }
  118. btn.removeAttr('disabled');
  119. if(d.code === 0){
  120. switch(action){
  121. case 'oper':
  122. form.find('input[name=password]').val('');
  123. alert(_('Oper updated'));
  124. break;
  125. default:
  126. location.reload();
  127. }
  128. }else{
  129. alert(d.message);
  130. }
  131. },
  132. error: function(xhr,msg,e){
  133. console.error(e);
  134. alert(_("Could not submit the form")+": "+msg);
  135. btn.removeAttr('disabled');
  136. },
  137. dataType: 'json'
  138. });
  139. btn.attr('disabled','disabled');
  140. return false;
  141. }).children('input[type=hidden][name=action]').removeAttr('disabled');
  142. $('#logout').click(logout);
  143. $('#newpass-button').click(function(){
  144. $('#newpass-diag').dialog('open');
  145. });
  146. $('#roles-button').click(function(){
  147. $('#roles-diag').dialog('open');
  148. });
  149. $('#rehash-servers').click(function(){
  150. $.ajax(__HOSTNAME__+'site/api/?action=rehash',{
  151. success: function(d){
  152. if(d.log){
  153. console.log(d.log);
  154. }
  155. alert(d.message);
  156. $('#rehash-servers').removeAttr('disabled');
  157. },
  158. error: function(xhr,msg,e){
  159. console.error(e);
  160. alert(_("Could not rehash the servers")+": "+msg);
  161. $('#rehash-servers').removeAttr('disabled');
  162. },
  163. dataType: 'json'
  164. });
  165. $(this).attr('disabled','disabled');
  166. return false;
  167. });
  168. $('#2-factor-disable').click(function(){
  169. var btn = $(this);
  170. $.ajax(__HOSTNAME__+'site/api/?action=2-factor-delete',{
  171. success: function(d){
  172. if(d.log){
  173. console.log(d.log);
  174. }
  175. alert(d.message);
  176. btn.removeAttr('disabled');
  177. location.reload();
  178. },
  179. error: function(xhr,msg,e){
  180. console.error(e);
  181. alert("Could not disable 2-factor: "+msg);
  182. btn.removeAttr('disabled');
  183. },
  184. dataType: 'json'
  185. });
  186. $(this).attr('disabled','disabled');
  187. return false;
  188. });
  189. $('#sync-pass').click(function(){
  190. var btn = $(this);
  191. $.ajax(__HOSTNAME__+'site/api/?action=sync-pass',{
  192. success: function(d){
  193. if(d.log){
  194. console.log(d.log);
  195. }
  196. alert(d.message);
  197. btn.removeAttr('disabled');
  198. },
  199. error: function(xhr,msg,e){
  200. console.error(e);
  201. alert(_("Could not synchronize your password")+": "+msg);
  202. btn.removeAttr('disabled');
  203. },
  204. dataType: 'json'
  205. });
  206. btn.attr('disabled','disabled');
  207. return false;
  208. });
  209. $('#persona-register').hover(function(){
  210. $(this).addClass('ui-state-hover');
  211. },function(){
  212. $(this).removeClass('ui-state-hover');
  213. }).click(function(){
  214. if(confirm(_("This is an admin only feature. Continue?"))){
  215. navigator.id.request({
  216. siteName: 'Omninet'
  217. });
  218. }
  219. });
  220. if(navigator.id){
  221. navigator.id.watch({
  222. loggedInUser: $.cookie('personaUser'),
  223. onlogin: function(assertion){
  224. $.ajax({
  225. type: 'post',
  226. url: __HOSTNAME__+'site/api/?action=persona-login',
  227. data: {
  228. assertion: assertion
  229. },
  230. success: function(d){
  231. if(d.code !== 0){
  232. if(d.message){
  233. console.log(d.message);
  234. alert(d.message);
  235. }
  236. }
  237. location.reload();
  238. },
  239. error: function(xhr,s,e){
  240. navigator.id.logout();
  241. alert(_("Login failure")+": " + e);
  242. }
  243. });
  244. },
  245. onlogout: function(){
  246. //$('#logout').click();
  247. }
  248. });
  249. }
  250. $('button[id^=persona-remove-]').each(function(){
  251. var id = this.id.substr(15),
  252. btn = $(this);
  253. btn.click(function(){
  254. $.ajax(__HOSTNAME__+'site/api/?action=persona-remove&id='+id,{
  255. success: function(d){
  256. if(d.log){
  257. console.log(d.log);
  258. }
  259. if(d.message){
  260. alert(d.message);
  261. }
  262. location.reload();
  263. },
  264. error: function(xhr,msg,e){
  265. console.error(e);
  266. alert(_("Could not remove persona address")+": "+msg);
  267. btn.removeAttr('disabled');
  268. },
  269. dataType: 'json'
  270. });
  271. btn.attr('disabled','disabled');
  272. return false;
  273. });
  274. });
  275. $('.server-opers,.server-owner,.server-children,.server-parent').click(function(){
  276. $(this).next().toggle();
  277. }).next().hide();
  278. $('.button,button,input[type=button],input[type=submit]').button();
  279. $('.tabs').tabs({
  280. activate: function(e,ui){
  281. var url = $.url(),
  282. params = url.data.param.query;
  283. params.tab = ui.newPanel.attr('id');
  284. History.pushState({},document.title,url.attr('path')+'?'+$.param(params)+url.attr('anchor'));
  285. },
  286. create: function(e,ui){
  287. $(window).trigger('statechange');
  288. },
  289. heightStyle: 'fill'
  290. }).addClass('transparent').each(function(){
  291. var tabs = $(this);
  292. tabs.parent().resize(function(){
  293. tabs.tabs('refresh');
  294. });
  295. });
  296. dialogs.dialog({
  297. modal: true,
  298. draggable: false,
  299. autoOpen: false,
  300. width: 500
  301. });
  302. $('.menu').menu();
  303. $(window).on('statechange',function(){
  304. var url = $.url(),
  305. tab = url.param('tab'),
  306. params = url.data.param.query,
  307. tabel = $('.tabs').children('ul').children('li').children('a[href="#'+tab+'"]');
  308. if(tab && tabel.length == 1){
  309. $('.tabs').tabs('option','active',tabel.parent().index());
  310. }else{
  311. var href = $('.tabs').children('ul').children('li').children('a');
  312. if(href.length > 0){
  313. href = href.get(0).href;
  314. }else{
  315. href = '';
  316. }
  317. params.tab = $.url(href).attr('fragment');
  318. History.pushState({},document.title,url.attr('path')+'?'+$.param(params)+url.attr('anchor'));
  319. }
  320. }).trigger('statechange').resize(function(){
  321. dialogs.each(function(){
  322. var d = $(this);
  323. if(d.dialog('isOpen')){
  324. d.dialog("option", "position", "center");
  325. }
  326. });
  327. var b = $('#user-menu-button');
  328. if(b.length > 0){
  329. $('#user-menu').offset({
  330. top: b.offset().top
  331. });
  332. }
  333. });
  334. $('#login-diag,#verify-diag').dialog('option',{
  335. closeOnEscape: false,
  336. close: function(){
  337. location.href = 'http://omnimaga.org';
  338. },
  339. position:{
  340. my: "center",
  341. at: "center",
  342. of: window
  343. }
  344. }).dialog('open');
  345. $('#verify-diag').dialog('option','close',logout);
  346. $('.accordion').accordion({
  347. collapsible: true,
  348. active: false,
  349. heightStyle: 'content'
  350. }).css('max-height','500px');
  351. $('.tree').treegrid({
  352. initialState: 'collapsed'
  353. });
  354. $('#user-menu-button').click(function(){
  355. $('#user-menu').show();
  356. });
  357. $('#user-menu').css({
  358. position: 'fixed',
  359. right: '0'
  360. }).hover(function(){},function(){
  361. $(this).hide();
  362. }).click(function(){
  363. $(this).hide();
  364. }).hide();
  365. if(!Modernizr.inputtypes.date){
  366. $('input[type=date]').datepicker({
  367. dateFormat: 'yy-mm-dd'
  368. });
  369. }
  370. if(!Modernizr.inputtypes.datetime){
  371. $('input[type=datetime]').datetimepicker({
  372. dateFormat: 'yy-mm-dd',
  373. timeFormat:'HH:mm:ssZ'
  374. });
  375. }
  376. if(!Modernizr.inputtypes.number){
  377. $('input[type=number]').spinner();
  378. }
  379. $('body').show();
  380. window.ServerPing = function(){
  381. console.log(_("Server Ping"));
  382. $.ajax(__HOSTNAME__+'site/api/?action=ping',{
  383. success: function(d){
  384. if(d.log){
  385. console.log(d.log);
  386. }
  387. if(d.message){
  388. alert(d.message);
  389. }
  390. if(d.code!==0){
  391. location.reload();
  392. }
  393. },
  394. error: function(xhr,msg,e){
  395. console.error(e);
  396. alert(_("Could not ping server")+": "+msg);
  397. location.reload();
  398. },
  399. dataType: 'json'
  400. });
  401. setTimeout(window.ServerPing,1000*60*5); // Every 5 minutes
  402. };
  403. window.FetchMemos = function(once){
  404. console.log(_("Fetching Memos"));
  405. $.ajax(__HOSTNAME__+'site/api/?action=get-memos',{
  406. success: function(d){
  407. if(d.log){
  408. console.log(d.log);
  409. }
  410. if(d.message){
  411. alert(d.message);
  412. }
  413. if(d.code!==0){
  414. location.reload();
  415. }
  416. var i,
  417. m;
  418. if(d.memos){
  419. for(i in d.memos){
  420. m = d.memos[i];
  421. m.date = m.date.year+'-'+m.date.month+'-'+m.date.day+' '+m.date.time;
  422. d.memos[i] = m;
  423. }
  424. if(typeof memos != 'undefined' && !once && ($(d.memos).not(memos).length !== 0 || $(memos).not(d.memos).length !== 0)){
  425. alert('New memo');
  426. }
  427. memos = d.memos;
  428. }
  429. $('#memos').html(templates.memos(d)).find('button').button();
  430. translate('#memos');
  431. $('body').resize();
  432. },
  433. error: function(xhr,msg,e){
  434. console.error(e);
  435. alert(_("Could not contact server")+": "+msg);
  436. location.reload();
  437. },
  438. dataType: 'json'
  439. });
  440. if(!once){
  441. setTimeout(window.ServerPing,1000*60); // Every minute
  442. }
  443. };
  444. window.DeleteMemoFromButton = function(){
  445. window.DeleteMemo($(this).parent());
  446. };
  447. window.ReplyToMemoFromButton = function(){
  448. window.ReplyToMemo($(this).parent());
  449. };
  450. window.ReplyToMemo = function(div){
  451. var from = div.find('.memo-from').text().trim();
  452. $('#memo-diag').dialog('open').find('input[name=to]').val(from);
  453. $('#memo-diag').find('input[name=message]').select();
  454. };
  455. window.DeleteMemos = function(){
  456. window.DeleteMemo($('<div>').attr('id','memo-all'),function(){
  457. window.FetchMemos(true);
  458. });
  459. };
  460. window.DeleteMemo = function(div,callback){
  461. var id = $(div).attr('id').substr(5);
  462. console.log(_("Deleting memo")+": "+id);
  463. $.ajax(__HOSTNAME__+'site/api/?action=delete-memo&id='+id,{
  464. success: function(d){
  465. if(d.log){
  466. console.log(d.log);
  467. }
  468. if(d.message){
  469. alert(d.message);
  470. }
  471. if(d.code!==0){
  472. location.reload();
  473. }
  474. div.remove();
  475. if(typeof callback != 'undefined'){
  476. callback();
  477. }
  478. },
  479. error: function(xhr,msg,e){
  480. console.error(e);
  481. alert(_("Could not ping server")+": "+msg);
  482. location.reload();
  483. },
  484. dataType: 'json'
  485. });
  486. };
  487. window.FetchNews = function(once){
  488. console.log(_("Fetching News"));
  489. $.ajax(__HOSTNAME__+'site/api/?action=get-news',{
  490. success: function(d){
  491. if(d.log){
  492. console.log(d.log);
  493. }
  494. if(d.message){
  495. alert(d.message);
  496. }
  497. if(d.code!==0){
  498. location.reload();
  499. }
  500. var i,
  501. n;
  502. if(d.news){
  503. d.news = d.news.reverse();
  504. for(i in d.news){
  505. n = d.news[i];
  506. n.date = n.date.year+'-'+n.date.month+'-'+n.date.day+' '+n.date.time;
  507. d.news[i] = n;
  508. }
  509. if(typeof news != 'undefined' && !once && ($(d.news).not(news).length !== 0 || $(news).not(d.news).length !== 0)){
  510. alert(_('New news item'));
  511. }
  512. news = d.news;
  513. }
  514. $('#news').html(templates.news(d)).find('button').button();
  515. translate('#news');
  516. $('body').resize();
  517. },
  518. error: function(xhr,msg,e){
  519. console.error(e);
  520. alert(_("Could not contact server")+": "+msg);
  521. location.reload();
  522. },
  523. dataType: 'json'
  524. });
  525. if(!once){
  526. setTimeout(window.ServerPing,1000*60); // Every minute
  527. }
  528. };
  529. window.FetchChannels = function(once){
  530. console.log(_("Fetching Channels"));
  531. $.ajax(__HOSTNAME__+'site/api/?action=get-channels',{
  532. success: function(d){
  533. if(d.log){
  534. console.log(d.log);
  535. }
  536. if(d.message){
  537. alert(d.message);
  538. }
  539. if(d.code!==0){
  540. location.reload();
  541. }
  542. var i,
  543. n;
  544. if(d.channels){
  545. for(i in d.channels){
  546. n = d.channels[i];
  547. d.channels[i] = n;
  548. }
  549. }
  550. $('#channels').html(templates.channels(d)).find('button').button();
  551. translate('#channels');
  552. $('body').resize();
  553. },
  554. error: function(xhr,msg,e){
  555. console.error(e);
  556. alert(_("Could not contact server")+": "+msg);
  557. location.reload();
  558. },
  559. dataType: 'json'
  560. });
  561. if(!once){
  562. setTimeout(window.ServerPing,1000*60); // Every minute
  563. }
  564. };
  565. setInterval(function(){
  566. if(LANG != window.navigator.language){
  567. console.log(_('Language change detected'));
  568. location.reload();
  569. }
  570. },1000);
  571. $('body').resize();
  572. });
  573. });