index.js 12 KB

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