index.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637
  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. if(typeof $.cookie('user') != 'undefined'){
  346. $('#login').find('input[name=username]').val($.cookie('user'));
  347. }
  348. if(typeof $.cookie('type') != 'undefined'){
  349. $('#login').find('select[name=type]').val($.cookie('type'));
  350. }
  351. $('#verify-diag').dialog('option','close',logout);
  352. $('.accordion').accordion({
  353. collapsible: true,
  354. active: false,
  355. heightStyle: 'content'
  356. }).css('max-height','500px');
  357. $('.tree').treegrid({
  358. initialState: 'collapsed'
  359. });
  360. $('#user-menu-button').click(function(){
  361. $('#user-menu').show();
  362. });
  363. $('#user-menu').css({
  364. position: 'fixed',
  365. right: '0'
  366. }).hover(function(){},function(){
  367. $(this).hide();
  368. }).click(function(){
  369. $(this).hide();
  370. }).hide();
  371. if(!Modernizr.inputtypes.date){
  372. $('input[type=date]').datepicker({
  373. dateFormat: 'yy-mm-dd'
  374. });
  375. }
  376. if(!Modernizr.inputtypes.datetime){
  377. $('input[type=datetime]').datetimepicker({
  378. dateFormat: 'yy-mm-dd',
  379. timeFormat:'HH:mm:ssZ'
  380. });
  381. }
  382. if(!Modernizr.inputtypes.number){
  383. $('input[type=number]').spinner();
  384. }
  385. window.ServerPing = function(){
  386. console.log(_("Server Ping"));
  387. $.ajax(__HOSTNAME__+'site/api/?action=ping',{
  388. success: function(d){
  389. if(d.log){
  390. console.log(d.log);
  391. }
  392. if(d.message){
  393. alert(d.message);
  394. }
  395. if(d.code!==0){
  396. location.reload();
  397. }
  398. },
  399. error: function(xhr,msg,e){
  400. console.error(e);
  401. alert(_("Could not ping server")+": "+msg);
  402. location.reload();
  403. },
  404. dataType: 'json'
  405. });
  406. setTimeout(window.ServerPing,1000*60*5); // Every 5 minutes
  407. };
  408. window.FetchMemos = function(once){
  409. console.log(_("Fetching Memos"));
  410. $.ajax(__HOSTNAME__+'site/api/?action=get-memos',{
  411. success: function(d){
  412. if(d.log){
  413. console.log(d.log);
  414. }
  415. if(d.message){
  416. alert(d.message);
  417. }
  418. if(d.code!==0){
  419. location.reload();
  420. }
  421. var i,
  422. m;
  423. if(d.memos){
  424. for(i in d.memos){
  425. m = d.memos[i];
  426. m.date = m.date.year+'-'+m.date.month+'-'+m.date.day+' '+m.date.time;
  427. d.memos[i] = m;
  428. }
  429. if(typeof memos != 'undefined' && !once && ($(d.memos).not(memos).length !== 0 || $(memos).not(d.memos).length !== 0)){
  430. alert('New memo');
  431. }
  432. memos = d.memos;
  433. }
  434. $('#memos').html(templates.memos(d)).find('button').button();
  435. translate('#memos');
  436. $('body').resize();
  437. },
  438. error: function(xhr,msg,e){
  439. console.error(e);
  440. alert(_("Could not contact server")+": "+msg);
  441. location.reload();
  442. },
  443. dataType: 'json'
  444. });
  445. if(!once){
  446. setTimeout(window.ServerPing,1000*60); // Every minute
  447. }
  448. };
  449. window.ReplyToMemo = function(from){
  450. $('#memo-diag').dialog('open').find('input[name=to]').val(from);
  451. $('#memo-diag').find('input[name=message]').select();
  452. };
  453. window.DeleteMemos = function(){
  454. window.DeleteMemo('all',function(){
  455. window.FetchMemos(true);
  456. });
  457. };
  458. window.DeleteMemo = function(id,callback){
  459. console.log(_("Deleting memo")+": "+id);
  460. $.ajax(__HOSTNAME__+'site/api/?action=delete-memo&id='+id,{
  461. success: function(d){
  462. if(d.log){
  463. console.log(d.log);
  464. }
  465. if(d.message){
  466. alert(d.message);
  467. }
  468. if(d.code!==0){
  469. location.reload();
  470. }
  471. $('#memo-'+id).remove();
  472. if(typeof callback != 'undefined'){
  473. callback();
  474. }
  475. },
  476. error: function(xhr,msg,e){
  477. console.error(e);
  478. alert(_("Could not ping server")+": "+msg);
  479. location.reload();
  480. },
  481. dataType: 'json'
  482. });
  483. };
  484. window.FetchNews = function(once){
  485. console.log(_("Fetching News"));
  486. $.ajax(__HOSTNAME__+'site/api/?action=get-news',{
  487. success: function(d){
  488. if(d.log){
  489. console.log(d.log);
  490. }
  491. if(d.message){
  492. alert(d.message);
  493. }
  494. if(d.code!==0){
  495. location.reload();
  496. }
  497. var i,
  498. n;
  499. if(d.news){
  500. d.news = d.news.reverse();
  501. for(i in d.news){
  502. n = d.news[i];
  503. n.date = n.date.year+'-'+n.date.month+'-'+n.date.day+' '+n.date.time;
  504. d.news[i] = n;
  505. }
  506. if(typeof news != 'undefined' && !once && ($(d.news).not(news).length !== 0 || $(news).not(d.news).length !== 0)){
  507. alert(_('New news item'));
  508. }
  509. news = d.news;
  510. }
  511. $('#news').html(templates.news(d)).find('button').button();
  512. translate('#news');
  513. $('body').resize();
  514. },
  515. error: function(xhr,msg,e){
  516. console.error(e);
  517. alert(_("Could not contact server")+": "+msg);
  518. location.reload();
  519. },
  520. dataType: 'json'
  521. });
  522. if(!once){
  523. setTimeout(window.ServerPing,1000*60); // Every minute
  524. }
  525. };
  526. window.FetchChannels = function(once){
  527. console.log(_("Fetching Channels"));
  528. $.ajax(__HOSTNAME__+'site/api/?action=get-channels',{
  529. success: function(d){
  530. if(d.log){
  531. console.log(d.log);
  532. }
  533. if(d.message){
  534. alert(d.message);
  535. }
  536. if(d.code!==0){
  537. location.reload();
  538. }
  539. var i,
  540. n;
  541. if(d.channels){
  542. for(i in d.channels){
  543. n = d.channels[i];
  544. d.channels[i] = n;
  545. }
  546. }
  547. $('#channels').html(templates.channels(d)).find('button').button();
  548. translate('#channels');
  549. $('#channels').find('.tree').treegrid({
  550. initialState: 'collapsed'
  551. });
  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. window.DeleteChannel = function(channel){
  566. if(confirm(_('Are you sure you want to delete channel')+' '+channel)){
  567. console.log(_("Deleting channel")+": "+channel);
  568. $.ajax(__HOSTNAME__+'site/api/?action=delete-channel',{
  569. data: {
  570. channel: channel
  571. },
  572. success: function(d){
  573. if(d.log){
  574. console.log(d.log);
  575. }
  576. if(d.message){
  577. alert(d.message);
  578. }
  579. if(d.code!==0){
  580. location.reload();
  581. }
  582. $('[id=channel-'+channel+']').remove();
  583. if(typeof callback != 'undefined'){
  584. callback();
  585. }
  586. },
  587. error: function(xhr,msg,e){
  588. console.error(e);
  589. alert(_("Could not ping server")+": "+msg);
  590. location.reload();
  591. },
  592. dataType: 'json'
  593. });
  594. }
  595. };
  596. window.RegisterChannel = function(channel){
  597. console.log(_("Registering channel")+": "+channel);
  598. $.ajax(__HOSTNAME__+'site/api/?action=register-channel',{
  599. data: {
  600. channel: channel
  601. },
  602. success: function(d){
  603. if(d.log){
  604. console.log(d.log);
  605. }
  606. if(d.message){
  607. alert(d.message);
  608. }
  609. if(d.code!==0){
  610. location.reload();
  611. }
  612. window.FetchChannels(true);
  613. },
  614. error: function(xhr,msg,e){
  615. console.error(e);
  616. alert(_("Could not ping server")+": "+msg);
  617. location.reload();
  618. },
  619. dataType: 'json'
  620. });
  621. };
  622. setInterval(function(){
  623. if(LANG != window.navigator.language){
  624. console.log(_('Language change detected'));
  625. location.reload();
  626. }
  627. },1000);
  628. delayedload();
  629. $(document).ajaxStart(function(){
  630. $("#loading").show();
  631. }).ajaxStop(function(){
  632. $("#loading").hide();
  633. });
  634. $('body').show();
  635. $('body').resize();
  636. });
  637. });