index.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628
  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. $('body').show();
  386. window.ServerPing = function(){
  387. console.log(_("Server Ping"));
  388. $.ajax(__HOSTNAME__+'site/api/?action=ping',{
  389. success: function(d){
  390. if(d.log){
  391. console.log(d.log);
  392. }
  393. if(d.message){
  394. alert(d.message);
  395. }
  396. if(d.code!==0){
  397. location.reload();
  398. }
  399. },
  400. error: function(xhr,msg,e){
  401. console.error(e);
  402. alert(_("Could not ping server")+": "+msg);
  403. location.reload();
  404. },
  405. dataType: 'json'
  406. });
  407. setTimeout(window.ServerPing,1000*60*5); // Every 5 minutes
  408. };
  409. window.FetchMemos = function(once){
  410. console.log(_("Fetching Memos"));
  411. $.ajax(__HOSTNAME__+'site/api/?action=get-memos',{
  412. success: function(d){
  413. if(d.log){
  414. console.log(d.log);
  415. }
  416. if(d.message){
  417. alert(d.message);
  418. }
  419. if(d.code!==0){
  420. location.reload();
  421. }
  422. var i,
  423. m;
  424. if(d.memos){
  425. for(i in d.memos){
  426. m = d.memos[i];
  427. m.date = m.date.year+'-'+m.date.month+'-'+m.date.day+' '+m.date.time;
  428. d.memos[i] = m;
  429. }
  430. if(typeof memos != 'undefined' && !once && ($(d.memos).not(memos).length !== 0 || $(memos).not(d.memos).length !== 0)){
  431. alert('New memo');
  432. }
  433. memos = d.memos;
  434. }
  435. $('#memos').html(templates.memos(d)).find('button').button();
  436. translate('#memos');
  437. $('body').resize();
  438. },
  439. error: function(xhr,msg,e){
  440. console.error(e);
  441. alert(_("Could not contact server")+": "+msg);
  442. location.reload();
  443. },
  444. dataType: 'json'
  445. });
  446. if(!once){
  447. setTimeout(window.ServerPing,1000*60); // Every minute
  448. }
  449. };
  450. window.ReplyToMemo = function(from){
  451. $('#memo-diag').dialog('open').find('input[name=to]').val(from);
  452. $('#memo-diag').find('input[name=message]').select();
  453. };
  454. window.DeleteMemos = function(){
  455. window.DeleteMemo('all',function(){
  456. window.FetchMemos(true);
  457. });
  458. };
  459. window.DeleteMemo = function(id,callback){
  460. console.log(_("Deleting memo")+": "+id);
  461. $.ajax(__HOSTNAME__+'site/api/?action=delete-memo&id='+id,{
  462. success: function(d){
  463. if(d.log){
  464. console.log(d.log);
  465. }
  466. if(d.message){
  467. alert(d.message);
  468. }
  469. if(d.code!==0){
  470. location.reload();
  471. }
  472. $('#memo-'+id).remove();
  473. if(typeof callback != 'undefined'){
  474. callback();
  475. }
  476. },
  477. error: function(xhr,msg,e){
  478. console.error(e);
  479. alert(_("Could not ping server")+": "+msg);
  480. location.reload();
  481. },
  482. dataType: 'json'
  483. });
  484. };
  485. window.FetchNews = function(once){
  486. console.log(_("Fetching News"));
  487. $.ajax(__HOSTNAME__+'site/api/?action=get-news',{
  488. success: function(d){
  489. if(d.log){
  490. console.log(d.log);
  491. }
  492. if(d.message){
  493. alert(d.message);
  494. }
  495. if(d.code!==0){
  496. location.reload();
  497. }
  498. var i,
  499. n;
  500. if(d.news){
  501. d.news = d.news.reverse();
  502. for(i in d.news){
  503. n = d.news[i];
  504. n.date = n.date.year+'-'+n.date.month+'-'+n.date.day+' '+n.date.time;
  505. d.news[i] = n;
  506. }
  507. if(typeof news != 'undefined' && !once && ($(d.news).not(news).length !== 0 || $(news).not(d.news).length !== 0)){
  508. alert(_('New news item'));
  509. }
  510. news = d.news;
  511. }
  512. $('#news').html(templates.news(d)).find('button').button();
  513. translate('#news');
  514. $('body').resize();
  515. },
  516. error: function(xhr,msg,e){
  517. console.error(e);
  518. alert(_("Could not contact server")+": "+msg);
  519. location.reload();
  520. },
  521. dataType: 'json'
  522. });
  523. if(!once){
  524. setTimeout(window.ServerPing,1000*60); // Every minute
  525. }
  526. };
  527. window.FetchChannels = function(once){
  528. console.log(_("Fetching Channels"));
  529. $.ajax(__HOSTNAME__+'site/api/?action=get-channels',{
  530. success: function(d){
  531. if(d.log){
  532. console.log(d.log);
  533. }
  534. if(d.message){
  535. alert(d.message);
  536. }
  537. if(d.code!==0){
  538. location.reload();
  539. }
  540. var i,
  541. n;
  542. if(d.channels){
  543. for(i in d.channels){
  544. n = d.channels[i];
  545. d.channels[i] = n;
  546. }
  547. }
  548. $('#channels').html(templates.channels(d)).find('button').button();
  549. translate('#channels');
  550. $('body').resize();
  551. },
  552. error: function(xhr,msg,e){
  553. console.error(e);
  554. alert(_("Could not contact server")+": "+msg);
  555. location.reload();
  556. },
  557. dataType: 'json'
  558. });
  559. if(!once){
  560. setTimeout(window.ServerPing,1000*60); // Every minute
  561. }
  562. };
  563. window.DeleteChannel = function(channel){
  564. if(confirm(_('Are you sure you want to delete channel')+' '+channel)){
  565. console.log(_("Deleting channel")+": "+channel);
  566. $.ajax(__HOSTNAME__+'site/api/?action=delete-channel',{
  567. data: {
  568. channel: channel
  569. },
  570. success: function(d){
  571. if(d.log){
  572. console.log(d.log);
  573. }
  574. if(d.message){
  575. alert(d.message);
  576. }
  577. if(d.code!==0){
  578. location.reload();
  579. }
  580. $('[id=channel-'+channel+']').remove();
  581. if(typeof callback != 'undefined'){
  582. callback();
  583. }
  584. },
  585. error: function(xhr,msg,e){
  586. console.error(e);
  587. alert(_("Could not ping server")+": "+msg);
  588. location.reload();
  589. },
  590. dataType: 'json'
  591. });
  592. }
  593. };
  594. window.RegisterChannel = function(channel){
  595. console.log(_("Registering channel")+": "+channel);
  596. $.ajax(__HOSTNAME__+'site/api/?action=register-channel',{
  597. data: {
  598. channel: channel
  599. },
  600. success: function(d){
  601. if(d.log){
  602. console.log(d.log);
  603. }
  604. if(d.message){
  605. alert(d.message);
  606. }
  607. if(d.code!==0){
  608. location.reload();
  609. }
  610. window.FetchChannels(true);
  611. },
  612. error: function(xhr,msg,e){
  613. console.error(e);
  614. alert(_("Could not ping server")+": "+msg);
  615. location.reload();
  616. },
  617. dataType: 'json'
  618. });
  619. };
  620. setInterval(function(){
  621. if(LANG != window.navigator.language){
  622. console.log(_('Language change detected'));
  623. location.reload();
  624. }
  625. },1000);
  626. $('body').resize();
  627. });
  628. });