index.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677
  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. $('#sync-groups').click(function(){
  215. var btn = $(this);
  216. $.ajax(__HOSTNAME__+'site/api/?action=sync-groups',{
  217. success: function(d){
  218. if(d.log){
  219. console.log(d.log);
  220. }
  221. alert(d.message);
  222. btn.removeAttr('disabled');
  223. },
  224. error: function(xhr,msg,e){
  225. console.error(e);
  226. alert(_("Could not synchronize your groups")+": "+msg);
  227. btn.removeAttr('disabled');
  228. },
  229. dataType: 'json'
  230. });
  231. btn.attr('disabled','disabled');
  232. return false;
  233. });
  234. $('#persona-register').hover(function(){
  235. $(this).addClass('ui-state-hover');
  236. },function(){
  237. $(this).removeClass('ui-state-hover');
  238. }).click(function(){
  239. if(confirm(_("This is an admin only feature. Continue?"))){
  240. navigator.id.request({
  241. siteName: 'Omninet'
  242. });
  243. }
  244. });
  245. if(navigator.id){
  246. navigator.id.watch({
  247. loggedInUser: $.cookie('personaUser'),
  248. onlogin: function(assertion){
  249. $.ajax({
  250. type: 'post',
  251. url: __HOSTNAME__+'site/api/?action=persona-login',
  252. data: {
  253. assertion: assertion
  254. },
  255. success: function(d){
  256. if(d.code !== 0){
  257. if(d.message){
  258. console.log(d.message);
  259. alert(d.message);
  260. }
  261. }
  262. location.reload();
  263. },
  264. error: function(xhr,s,e){
  265. navigator.id.logout();
  266. alert(_("Login failure")+": " + e);
  267. }
  268. });
  269. },
  270. onlogout: function(){
  271. //$('#logout').click();
  272. }
  273. });
  274. }
  275. $('button[id^=persona-remove-]').each(function(){
  276. var id = this.id.substr(15),
  277. btn = $(this);
  278. btn.click(function(){
  279. $.ajax(__HOSTNAME__+'site/api/?action=persona-remove&id='+id,{
  280. success: function(d){
  281. if(d.log){
  282. console.log(d.log);
  283. }
  284. if(d.message){
  285. alert(d.message);
  286. }
  287. location.reload();
  288. },
  289. error: function(xhr,msg,e){
  290. console.error(e);
  291. alert(_("Could not remove persona address")+": "+msg);
  292. btn.removeAttr('disabled');
  293. },
  294. dataType: 'json'
  295. });
  296. btn.attr('disabled','disabled');
  297. return false;
  298. });
  299. });
  300. $('.server-opers,.server-owner,.server-children,.server-parent').click(function(){
  301. $(this).next().toggle();
  302. }).next().hide();
  303. $('.button,button,input[type=button],input[type=submit]').button();
  304. $('.tabs').tabs({
  305. activate: function(e,ui){
  306. var url = $.url(),
  307. params = url.data.param.query;
  308. params.tab = ui.newPanel.attr('id');
  309. History.pushState({},document.title,url.attr('path')+'?'+$.param(params)+url.attr('anchor'));
  310. },
  311. create: function(e,ui){
  312. $(window).trigger('statechange');
  313. },
  314. heightStyle: 'fill'
  315. }).addClass('transparent').each(function(){
  316. var tabs = $(this);
  317. tabs.parent().resize(function(){
  318. tabs.tabs('refresh');
  319. });
  320. });
  321. dialogs.dialog({
  322. modal: true,
  323. draggable: false,
  324. autoOpen: false,
  325. width: 500
  326. });
  327. $('.menu').menu();
  328. $(window).on('statechange',function(){
  329. var url = $.url(),
  330. tab = url.param('tab'),
  331. params = url.data.param.query,
  332. tabel = $('.tabs').children('ul').children('li').children('a[href="#'+tab+'"]');
  333. if(tab && tabel.length == 1){
  334. $('.tabs').tabs('option','active',tabel.parent().index());
  335. }else{
  336. var href = $('.tabs').children('ul').children('li').children('a');
  337. if(href.length > 0){
  338. href = href.get(0).href;
  339. }else{
  340. href = '';
  341. }
  342. params.tab = $.url(href).attr('fragment');
  343. History.pushState({},document.title,url.attr('path')+'?'+$.param(params)+url.attr('anchor'));
  344. }
  345. }).trigger('statechange').resize(function(){
  346. dialogs.each(function(){
  347. var d = $(this);
  348. if(d.dialog('isOpen')){
  349. d.dialog("option", "position", "center");
  350. }
  351. });
  352. var b = $('#user-menu-button');
  353. if(b.length > 0){
  354. $('#user-menu').offset({
  355. top: b.offset().top
  356. });
  357. }
  358. });
  359. $('#login-diag,#verify-diag').dialog('option',{
  360. closeOnEscape: false,
  361. close: function(){
  362. location.href = 'http://omnimaga.org';
  363. },
  364. position:{
  365. my: "center",
  366. at: "center",
  367. of: window
  368. }
  369. }).dialog('open');
  370. if(typeof $.cookie('user') != 'undefined'){
  371. $('#login').find('input[name=username]').val($.cookie('user'));
  372. }
  373. if(typeof $.cookie('type') != 'undefined'){
  374. $('#login').find('select[name=type]').val($.cookie('type'));
  375. }
  376. $('#verify-diag').dialog('option','close',logout);
  377. $('.accordion').accordion({
  378. collapsible: true,
  379. active: false,
  380. heightStyle: 'content'
  381. }).css('max-height','500px');
  382. $('.tree').treegrid({
  383. initialState: 'collapsed'
  384. });
  385. $('#user-menu-button').click(function(){
  386. $('#user-menu').show();
  387. });
  388. $('#user-menu').css({
  389. position: 'fixed',
  390. right: '0'
  391. }).hover(function(){},function(){
  392. $(this).hide();
  393. }).click(function(){
  394. $(this).hide();
  395. }).hide();
  396. if(!Modernizr.inputtypes.date){
  397. $('input[type=date]').datepicker({
  398. dateFormat: 'yy-mm-dd'
  399. });
  400. }
  401. if(!Modernizr.inputtypes.datetime){
  402. $('input[type=datetime]').datetimepicker({
  403. dateFormat: 'yy-mm-dd',
  404. timeFormat:'HH:mm:ssZ'
  405. });
  406. }
  407. if(!Modernizr.inputtypes.number){
  408. $('input[type=number]').spinner();
  409. }
  410. window.ServerPing = function(){
  411. console.log(_("Server Ping"));
  412. $.ajax(__HOSTNAME__+'site/api/?action=ping',{
  413. success: function(d){
  414. if(d.log){
  415. console.log(d.log);
  416. }
  417. if(d.message){
  418. alert(d.message);
  419. }
  420. if(d.code!==0){
  421. location.reload();
  422. }
  423. },
  424. error: function(xhr,msg,e){
  425. console.error(e);
  426. alert(_("Could not ping server")+": "+msg);
  427. location.reload();
  428. },
  429. dataType: 'json'
  430. });
  431. setTimeout(window.ServerPing,1000*60*5); // Every 5 minutes
  432. };
  433. window.FetchMemos = function(once){
  434. console.log(_("Fetching Memos"));
  435. $.ajax(__HOSTNAME__+'site/api/?action=get-memos',{
  436. success: function(d){
  437. if(d.log){
  438. console.log(d.log);
  439. }
  440. if(d.message){
  441. alert(d.message);
  442. }
  443. if(d.code!==0){
  444. location.reload();
  445. }
  446. var i,
  447. m;
  448. if(d.memos){
  449. for(i in d.memos){
  450. m = d.memos[i];
  451. m.date = m.date.year+'-'+m.date.month+'-'+m.date.day+' '+m.date.time;
  452. d.memos[i] = m;
  453. }
  454. if(typeof memos != 'undefined' && !once && ($(d.memos).not(memos).length !== 0 || $(memos).not(d.memos).length !== 0)){
  455. alert('New memo');
  456. }
  457. memos = d.memos;
  458. }
  459. $('#memos').html(templates.memos(d)).find('button').button();
  460. translate('#memos');
  461. $('body').resize();
  462. },
  463. error: function(xhr,msg,e){
  464. console.error(e);
  465. alert(_("Could not contact server")+": "+msg);
  466. location.reload();
  467. },
  468. dataType: 'json'
  469. });
  470. if(!once){
  471. setTimeout(window.ServerPing,1000*60); // Every minute
  472. }
  473. };
  474. window.ReplyToMemo = function(from){
  475. $('#memo-diag').dialog('open').find('input[name=to]').val(from);
  476. $('#memo-diag').find('input[name=message]').select();
  477. };
  478. window.DeleteMemos = function(){
  479. window.DeleteMemo('all',function(){
  480. window.FetchMemos(true);
  481. });
  482. };
  483. window.DeleteMemo = function(id,callback){
  484. console.log(_("Deleting memo")+": "+id);
  485. $.ajax(__HOSTNAME__+'site/api/?action=delete-memo&id='+id,{
  486. success: function(d){
  487. if(d.log){
  488. console.log(d.log);
  489. }
  490. if(d.message){
  491. alert(d.message);
  492. }
  493. if(d.code!==0){
  494. location.reload();
  495. }
  496. $('#memo-'+id).remove();
  497. if(typeof callback != 'undefined'){
  498. callback();
  499. }
  500. },
  501. error: function(xhr,msg,e){
  502. console.error(e);
  503. alert(_("Could not ping server")+": "+msg);
  504. location.reload();
  505. },
  506. dataType: 'json'
  507. });
  508. };
  509. window.FetchNews = function(once){
  510. console.log(_("Fetching News"));
  511. $.ajax(__HOSTNAME__+'site/api/?action=get-news',{
  512. success: function(d){
  513. if(d.log){
  514. console.log(d.log);
  515. }
  516. if(d.message){
  517. alert(d.message);
  518. }
  519. if(d.code!==0){
  520. location.reload();
  521. }
  522. var i,
  523. n;
  524. if(d.news){
  525. d.news = d.news.reverse();
  526. for(i in d.news){
  527. n = d.news[i];
  528. n.date = n.date.year+'-'+n.date.month+'-'+n.date.day+' '+n.date.time;
  529. d.news[i] = n;
  530. }
  531. if(typeof news != 'undefined' && !once && ($(d.news).not(news).length !== 0 || $(news).not(d.news).length !== 0)){
  532. alert(_('New news item'));
  533. }
  534. news = d.news;
  535. }
  536. $('#news').html(templates.news(d)).find('button').button();
  537. translate('#news');
  538. $('body').resize();
  539. },
  540. error: function(xhr,msg,e){
  541. console.error(e);
  542. alert(_("Could not contact server")+": "+msg);
  543. location.reload();
  544. },
  545. dataType: 'json'
  546. });
  547. if(!once){
  548. setTimeout(window.ServerPing,1000*60); // Every minute
  549. }
  550. };
  551. window.FetchChannels = function(){
  552. console.log(_("Fetching Channels"));
  553. $.ajax(__HOSTNAME__+'site/api/?action=get-channels',{
  554. success: function(d){
  555. if(d.log){
  556. console.log(d.log);
  557. }
  558. if(d.message){
  559. alert(d.message);
  560. }
  561. if(d.code!==0){
  562. location.reload();
  563. }
  564. var i,
  565. n;
  566. if(d.channels){
  567. for(i in d.channels){
  568. n = d.channels[i];
  569. d.channels[i] = n;
  570. }
  571. }
  572. $('#channels').html(templates.channels(d)).find('button').button();
  573. translate('#channels');
  574. $('#channels').find('.tree').treegrid({
  575. initialState: 'collapsed'
  576. });
  577. $('body').resize();
  578. },
  579. error: function(xhr,msg,e){
  580. console.error(e);
  581. alert(_("Could not contact server")+": "+msg);
  582. location.reload();
  583. },
  584. dataType: 'json'
  585. });
  586. };
  587. window.DeleteChannel = function(channel){
  588. if(confirm(_('Are you sure you want to delete channel')+' '+channel)){
  589. console.log(_("Deleting channel")+": "+channel);
  590. $.ajax(__HOSTNAME__+'site/api/?action=delete-channel',{
  591. data: {
  592. channel: channel
  593. },
  594. success: function(d){
  595. if(d.log){
  596. console.log(d.log);
  597. }
  598. if(d.message){
  599. alert(d.message);
  600. }
  601. if(d.code!==0){
  602. location.reload();
  603. }
  604. $('[id=channel-'+channel+']').remove();
  605. if(typeof callback != 'undefined'){
  606. callback();
  607. }
  608. },
  609. error: function(xhr,msg,e){
  610. console.error(e);
  611. alert(_("Could not ping server")+": "+msg);
  612. location.reload();
  613. },
  614. dataType: 'json'
  615. });
  616. }
  617. };
  618. window.RegisterChannel = function(channel){
  619. console.log(_("Registering channel")+": "+channel);
  620. $.ajax(__HOSTNAME__+'site/api/?action=register-channel',{
  621. data: {
  622. channel: channel
  623. },
  624. success: function(d){
  625. if(d.log){
  626. console.log(d.log);
  627. }
  628. if(d.message){
  629. alert(d.message);
  630. }
  631. if(d.code!==0){
  632. location.reload();
  633. }
  634. window.FetchChannels(true);
  635. },
  636. error: function(xhr,msg,e){
  637. console.error(e);
  638. alert(_("Could not ping server")+": "+msg);
  639. location.reload();
  640. },
  641. dataType: 'json'
  642. });
  643. };
  644. window.ModifyChannelAccess = function(channel,user,id){
  645. var d = $('#channel-flags-diag');
  646. if(typeof user != 'undefined'){
  647. d.find('input[name=user]').val(user);
  648. }else{
  649. d.find('input[name=user]').val('');
  650. }
  651. d.find('input[type=checkbox]').prop('checked',false);
  652. if(typeof id != 'undefined'){
  653. $('div[id=channel-'+channel+']>table').find('tr.treegrid-parent-'+id).each(function(){
  654. var flag = this.className.substr(9,1);
  655. d.find('input[name^=flags]').each(function(){
  656. if(this.name.substr(6,1) == flag){
  657. $(this).prop('checked',true);
  658. }
  659. });
  660. });
  661. }
  662. d.find('input[name=channel]').val(channel);
  663. d.dialog('open');
  664. };
  665. setInterval(function(){
  666. if(LANG != window.navigator.language){
  667. console.log(_('Language change detected'));
  668. location.reload();
  669. }
  670. },1000);
  671. if(typeof delayedload == 'function'){
  672. delayedload();
  673. }
  674. $('body').show();
  675. $('body').resize();
  676. });
  677. });