index.js 17 KB

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