index.js 16 KB

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