index.js 12 KB

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