index.js 13 KB

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