index.js 14 KB

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