PageRenderTime 58ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/static/javascripts/app.js

https://github.com/EricSchles/hackerunion.org
JavaScript | 1308 lines | 1116 code | 158 blank | 34 comment | 95 complexity | 019eba070763bd7b340692deaa48e0c5 MD5 | raw file
Possible License(s): GPL-3.0
  1. $.expr[":"].contains = $.expr.createPseudo(function(arg) {
  2. return function( elem ) {
  3. return $(elem).text().toUpperCase().indexOf(arg.toUpperCase()) >= 0;
  4. };
  5. });
  6. function getParameterByName(name) {
  7. name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
  8. var regexS = "[\\?&#]" + name + "=([^&#]*)";
  9. var regex = new RegExp(regexS);
  10. var results = regex.exec(window.location.search);
  11. if(results === null) return "";
  12. else return decodeURIComponent(results[1].replace(/\+/g, " "));
  13. }
  14. function isValidEmailAddress(emailAddress) {
  15. var pattern = new RegExp(/^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i);
  16. return pattern.test(emailAddress);
  17. };
  18. ;(function ($, window, undefined) {
  19. 'use strict';
  20. var $doc = $(document),
  21. Modernizr = window.Modernizr;
  22. $(document).ready(function() {
  23. $.fn.foundationAlerts ? $doc.foundationAlerts() : null;
  24. $.fn.foundationButtons ? $doc.foundationButtons() : null;
  25. $.fn.foundationAccordion ? $doc.foundationAccordion() : null;
  26. $.fn.foundationNavigation ? $doc.foundationNavigation() : null;
  27. $.fn.foundationTopBar ? $doc.foundationTopBar() : null;
  28. $.fn.foundationCustomForms ? $doc.foundationCustomForms() : null;
  29. $.fn.foundationTabs ? $doc.foundationTabs({callback : $.foundation.customForms.appendCustomMarkup}) : null;
  30. $.fn.foundationTooltips ? $doc.foundationTooltips() : null;
  31. $.fn.foundationMagellan ? $doc.foundationMagellan() : null;
  32. $.fn.foundationClearing ? $doc.foundationClearing() : null;
  33. $.fn.placeholder ? $('input, textarea').placeholder() : null;
  34. // Hack to make the tabs work, zurb doesn't seem
  35. // to be working correctly here
  36. var History = window.History;
  37. $('.talk-tab').find('a').click(function() {
  38. History.replaceState({state:'talk'}, "Hacker Union (Talk)", "?state=talk");
  39. });
  40. $('.event-tab').find('a').click(function() {
  41. History.replaceState({state:'event'}, "Hacker Union (Events)", "?state=event");
  42. });
  43. $('.intro-tab').find('a').click(function() {
  44. History.replaceState({state:'intro'}, "Hacker Union (Help)", "?state=intro");
  45. });
  46. $('.leader-tab').find('a').click(function() {
  47. History.replaceState({state:'leader'}, "Hacker Union (Admin)", "?state=leader");
  48. });
  49. var state = getParameterByName("state");
  50. if(state !== "") {
  51. if(state === "talk") {
  52. $('.talk-tab').find('a').click();
  53. }
  54. if(state === "event") {
  55. $('.event-tab').find('a').click();
  56. }
  57. if(state === "intro") {
  58. $('.intro-tab').find('a').click();
  59. }
  60. if(state === "leader") {
  61. $('.leader-tab').find('a').click();
  62. }
  63. }
  64. var cleanURL = function(url) {
  65. return url.replace(/\/\/+/g,'\/');
  66. };
  67. $('.fake-save').click(function() {
  68. window.location = window.location.href;
  69. });
  70. $('.talk-content-input').redactor({minHeight: 100, source: false});
  71. $('.event-content-input').redactor({minHeight: 100, source: false});
  72. $('.edit-event-content-input').redactor({minHeight: 100, source: false});
  73. $('.comment-content-input').redactor({minHeight: 100, source: false});
  74. $('.pending-content-input').redactor({minHeight: 100, source: false});
  75. $('.intro-content-input').redactor({minHeight: 100, source: false});
  76. $('.edit-bulletin-input').redactor({minHeight: 100, source: false});
  77. $('.project-content-input').redactor({minHeight: 100, source: false});
  78. $('.edit-project-content-input').redactor({minHeight: 100, source: false});
  79. var clearEventModal = function() {
  80. $('.event-time-start-input').val("");
  81. $('.event-time-end-input').val("");
  82. $('.event-where-input').val("");
  83. $('.event-title-input').val("");
  84. $('.event-content-input').setCode("");
  85. };
  86. var edit_id = -1;
  87. var togglePromote = function(event) {
  88. if (!$(event.target).data('leader')) {
  89. if ($(event.target).closest('.post').hasClass('promoted')) {
  90. return false;
  91. }
  92. }
  93. $.ajax({
  94. type: "POST",
  95. url: cleanURL(window.location.pathname + '/posts/' + $(event.target).data('id') + '/promote/'),
  96. success: function(data) {
  97. if ($(event.target).data('leader')) {
  98. $(event.target).closest('.post').toggleClass('promoted');
  99. $(event.target).closest('.post').find('.icon-promoted').toggleClass('hidden');
  100. $(event.target).closest('.promote').toggleClass('active');
  101. } else {
  102. alert("Your promotion request has been submitted to the moderators.");
  103. updateSidebar(data.value.sidebar);
  104. }
  105. }
  106. });
  107. return false;
  108. };
  109. var makeLeader = function(event) {
  110. if (!confirm("Make this recruit a guide?")) {
  111. return false;
  112. }
  113. $.ajax({
  114. type: "POST",
  115. url: cleanURL('/accounts/guide/'),
  116. data: {
  117. username: $(event.target).data('username'),
  118. grant: true
  119. },
  120. success: function(data) {
  121. updateSidebar(data.value.sidebar);
  122. }
  123. });
  124. return false;
  125. };
  126. var removeMentee = function(event) {
  127. if (!confirm("Stop guiding this user?")) {
  128. return false;
  129. }
  130. $.ajax({
  131. type: "POST",
  132. url: cleanURL('/accounts/abandon/'),
  133. data: {
  134. user: $(event.target).data('user')
  135. },
  136. success: function(data) {
  137. updateSidebar(data.value.sidebar);
  138. }
  139. });
  140. return false;
  141. };
  142. var makeModerator = function(event) {
  143. if (!confirm("Make this recruit a moderator?")) {
  144. return false;
  145. }
  146. $.ajax({
  147. type: "POST",
  148. url: cleanURL('/accounts/moderator/'),
  149. data: {
  150. username: $(event.target).data('username'),
  151. grant: true
  152. },
  153. success: function(data) {
  154. updateSidebar(data.value.sidebar);
  155. }
  156. });
  157. return false;
  158. };
  159. var banMentee = function(event) {
  160. if (prompt("Permanently ban this user? Type \"BAN\" to proceed.") != "BAN") {
  161. return false;
  162. }
  163. $.ajax({
  164. type: "POST",
  165. url: cleanURL('/accounts/ban/'),
  166. data: {
  167. username: $(event.target).data('username')
  168. },
  169. success: function(data) {
  170. updateSidebar(data.value.sidebar);
  171. }
  172. });
  173. return false;
  174. };
  175. var toggleFollow = function(event, url) {
  176. if(!url) {
  177. url = cleanURL(window.location.pathname + '/posts/' + $(event.target).data('id') + '/follow/');
  178. }
  179. $.ajax({
  180. type: "POST",
  181. url: url,
  182. success: function(data) {
  183. $(event.target).closest('.attending').toggleClass('active');
  184. var newCount;
  185. if(data.value.followed) {
  186. newCount = parseInt($(event.target).closest('.event').find('.hacker-count').text(), 10) + 1;
  187. $(event.target).closest('.event').find('.hacker-count').text(newCount + " ");
  188. $(event.target).closest('.event').find('.faces').append("<span><img src='" + data.value.url + "' class='has-tip tip-top' data-id='" + data.value.id + "' title='" + data.value.username + "'/></span>");
  189. } else {
  190. newCount = parseInt($(event.target).closest('.event').find('.hacker-count').text(), 10) - 1;
  191. $(event.target).closest('.event').find('.hacker-count').text(newCount + " ");
  192. $(event.target).closest('.event').find('.faces').find("*[data-id='" + data.value.id + "']").closest('span').remove();
  193. }
  194. if (newCount == 1) {
  195. $('.hacker-count-label').text('hacker is in. ');
  196. } else if(newCount === 0 || newCount == 2) {
  197. $('.hacker-count-label').text('hackers are in. ');
  198. }
  199. }
  200. });
  201. return false;
  202. };
  203. $('.event-perma-attending').click(function(event) {
  204. toggleFollow(event, cleanURL(window.location.pathname.substr(0, window.location.pathname.indexOf('/posts/')) + '/posts/' + $(event.target).data('id') + '/follow/'));
  205. });
  206. $('.project-attending').click(function(event) {
  207. $.ajax({
  208. type: "POST",
  209. url: cleanURL(window.location.pathname.substr(0, window.location.pathname.indexOf('/posts/')) + '/posts/' + $(event.target).data('id') + '/follow/'),
  210. success: function(data) {
  211. $(event.target).closest('.attending').toggleClass('active');
  212. var newCount;
  213. if(data.value.followed) {
  214. newCount = parseInt($(event.target).closest('.project').find('.hacker-count').text(), 10) + 1;
  215. $(event.target).closest('.project').find('.hacker-count').text(newCount + " ");
  216. $(event.target).closest('.project').find('.faces').append("<span><img src='" + data.value.url + "' class='has-tip tip-top' data-id='" + data.value.id + "' title='" + data.value.username + "'/></span>");
  217. } else {
  218. newCount = parseInt($(event.target).closest('.project').find('.hacker-count').text(), 10) - 1;
  219. $(event.target).closest('.project').find('.hacker-count').text(newCount + " ");
  220. $(event.target).closest('.project').find('.faces').find("*[data-id='" + data.value.id + "']").closest('span').remove();
  221. }
  222. if (newCount == 1) {
  223. $('.hacker-count-label').text('hacker is in. ');
  224. } else if(newCount === 0 || newCount == 2) {
  225. $('.hacker-count-label').text('hackers are in. ');
  226. }
  227. }
  228. });
  229. return false;
  230. });
  231. var introEditModal = function(event) {
  232. // populate the modal with the correct nah-m sayuns.
  233. var $post = $(event.target).closest('.post');
  234. edit_id = $post.data('id');
  235. $('.edit-bulletin-input').setCode($post.find('.post-content').data('content'));
  236. $('.save-post').unbind().click(function() {
  237. if(validateEdit()) {
  238. $.ajax({
  239. type: "POST",
  240. url: cleanURL(window.location.pathname + '/intro/' + edit_id + '/'),
  241. data: {
  242. content: $('.edit-bulletin-input').getCode()
  243. },
  244. success: function(data) {
  245. $('#editBulletin').trigger('reveal:close');
  246. updateIntroFeed(data);
  247. }
  248. });
  249. }
  250. });
  251. $('.delete-post').unbind().click(function() {
  252. $.ajax({
  253. type: "POST",
  254. url: cleanURL(window.location.pathname + '/intro/' + edit_id + '/'),
  255. data: {
  256. content: $('.edit-bulletin-input').getCode(),
  257. delete: true
  258. },
  259. success: function(data) {
  260. $('#editBulletin').trigger('reveal:close');
  261. updateIntroFeed(data);
  262. }
  263. });
  264. });
  265. };
  266. $('.all-talk').click(function() {
  267. $('.talk-filter').find('.selected').removeClass('selected');
  268. $(this).addClass('selected');
  269. $('.talk').show();
  270. });
  271. $('.promoted-talk').click(function() {
  272. $('.talk-filter').find('.selected').removeClass('selected');
  273. $(this).addClass('selected');
  274. $('.talk').hide();
  275. $('.talk.promoted').show();
  276. });
  277. $('.all-event').click(function() {
  278. $('.event-filter').find('.selected').removeClass('selected');
  279. $(this).addClass('selected');
  280. $('.event').show();
  281. });
  282. $('.promoted-event').click(function() {
  283. $('.event-filter').find('.selected').removeClass('selected');
  284. $(this).addClass('selected');
  285. $('.event').hide();
  286. $('.event.promoted').show();
  287. });
  288. var talkEditModal = function(event) {
  289. // populate the modal with the correct nah-m sayuns.
  290. var $post = $(event.target).closest('.post');
  291. edit_id = $post.data('id');
  292. $('.edit-bulletin-input').setCode($post.find('.post-content').data('content'));
  293. $('.save-post').unbind().click(function() {
  294. if(validateEdit()) {
  295. $.ajax({
  296. type: "POST",
  297. url: cleanURL(window.location.pathname + '/talk/' + edit_id + '/'),
  298. data: {
  299. content: $('.edit-bulletin-input').getCode()
  300. },
  301. success: function(data) {
  302. $('#editBulletin').trigger('reveal:close');
  303. updateTalkFeed(data);
  304. }
  305. });
  306. }
  307. });
  308. $('.delete-post').unbind().click(function() {
  309. $.ajax({
  310. type: "POST",
  311. url: cleanURL(window.location.pathname + '/talk/' + edit_id + '/'),
  312. data: {
  313. content: $('.edit-bulletin-input').getCode(),
  314. delete: true
  315. },
  316. success: function(data) {
  317. $('#editBulletin').trigger('reveal:close');
  318. updateTalkFeed(data);
  319. }
  320. });
  321. });
  322. };
  323. var eventEditModal = function(event) {
  324. // populate the modal with the correct nah-m sayuns.
  325. var $post = $(event.target).closest('.post');
  326. edit_id = $post.data('id');
  327. $('.edit-event-content-input').setCode($post.find('.post-content').data('content'));
  328. $('.edit-event-title-input').val($post.find('.title').text());
  329. var start = $post.find('.start-date');
  330. var end = $post.find('.end-date');
  331. $('.edit-event-day-start').val(start.data('date'));
  332. $('.edit-event-time-start').val(start.data('time'));
  333. $('.edit-event-day-end').val(end.data('date'));
  334. $('.edit-event-time-end').val(end.data('time'));
  335. $('.edit-event-where-input').val($post.find('.where').text());
  336. // open the modal.
  337. // figure out how to submit currently
  338. $('.save-event').unbind().click(function() {
  339. if(validateEventEdit()) {
  340. $.ajax({
  341. type: "POST",
  342. url: cleanURL(window.location.pathname + '/event/' + edit_id + '/'),
  343. data: {
  344. 'start': $('.edit-event-day-start').val().trim() + ' ' + $('.edit-event-time-start').val().trim(),
  345. 'end': $('.edit-event-day-end').val().trim() + ' ' + $('.edit-event-time-end').val().trim(),
  346. 'where': $('.edit-event-where-input').val(),
  347. 'title': $('.edit-event-title-input').val(),
  348. 'content': $('.edit-event-content-input').getCode()
  349. },
  350. success: function(data) {
  351. $('#editEvent').trigger('reveal:close');
  352. $('.event-feed').html(data.value.feed);
  353. bindEventButtons();
  354. }
  355. });
  356. }
  357. });
  358. $('.delete-event').unbind().click(function() {
  359. $.ajax({
  360. type: "POST",
  361. url: cleanURL(window.location.pathname + '/event/' + edit_id + '/'),
  362. data: {
  363. 'start': $('.edit-event-day-start').val().trim() + ' ' + $('.edit-event-time-start').val().trim(),
  364. 'end': $('.edit-event-day-end').val().trim() + ' ' + $('.edit-event-time-end').val().trim(),
  365. 'where': $('.edit-event-where-input').val(),
  366. 'title': $('.edit-event-title-input').val(),
  367. 'content': $('.edit-event-content-input').getCode(),
  368. delete: true
  369. },
  370. success: function(data) {
  371. $('#editEvent').trigger('reveal:close');
  372. $('.event-feed').html(data.value.feed);
  373. bindEventButtons();
  374. }
  375. });
  376. });
  377. };
  378. $('.project-edit').click(function(event) {
  379. var $post = $(event.target).closest('.project');
  380. edit_id = $(event.target).data('id');
  381. $('.edit-project-title-input').val($post.find('.project-title').data("title"));
  382. $('.edit-project-content-input').setCode($post.find('.project-content').html());
  383. $('.edit-project-image-input').val($post.find('.project-picture').attr('src'));
  384. });
  385. $('.delete-project').click(function() {
  386. $.ajax({
  387. type: "POST",
  388. url: cleanURL(window.location.pathname.substr(0, window.location.pathname.indexOf('/posts/')) + '/projects/' + edit_id + '/'),
  389. data: {
  390. 'title': $('.edit-project-title-input').val(),
  391. 'content': $('.edit-project-content-input').getCode(),
  392. 'url': $('.edit-project-image-input').val(),
  393. delete: true
  394. },
  395. success: function(data) {
  396. $('#editProject').trigger('reveal:close');
  397. window.location = cleanURL(window.location.pathname.substr(0, window.location.pathname.indexOf('/posts/')) + '/projects/');
  398. }
  399. });
  400. });
  401. $('.save-project').click(function() {
  402. if(validateEmpty([$('.edit-project-title-input')], [$('.edit-project-content-input')])) {
  403. $.ajax({
  404. type: "POST",
  405. url: cleanURL(window.location.pathname.substr(0, window.location.pathname.indexOf('/posts/')) + '/projects/' + edit_id + '/'),
  406. data: {
  407. 'title': $('.edit-project-title-input').val(),
  408. 'content': $('.edit-project-content-input').getCode(),
  409. 'url': $('.edit-project-image-input').val()
  410. },
  411. success: function(data) {
  412. $('#editProject').trigger('reveal:close');
  413. location.reload();
  414. }
  415. });
  416. }
  417. });
  418. var bindTalkButtons = function() {
  419. $('.talk-promote').unbind().click(togglePromote);
  420. $('.talk-edit').click(talkEditModal);
  421. };
  422. var bindEventButtons = function() {
  423. $('.event-promote').unbind().click(togglePromote);
  424. $('.event-attending').unbind().click(toggleFollow);
  425. $('.event-edit').unbind().click(eventEditModal);
  426. $('a.recommend').unbind().click(function (event) {
  427. var post_id = $(event.target).data('id');
  428. $('.group-message-content').val($(this).data("message") + ': http://' + cleanURL(window.location.host + "/" + window.location.pathname + (post_id ? ('/posts/' + $(event.target).data('id') + "/") : "")));
  429. $('#sendGroupMessage').reveal();
  430. });
  431. };
  432. var bindIntroButtons = function() {
  433. $('.intro-edit').click(introEditModal);
  434. };
  435. var updateSidebar = function(data) {
  436. $('.sidebar').html(data);
  437. reloadFoundation();
  438. bindUserMessage();
  439. bindSidebar();
  440. };
  441. var bindSidebar = function() {
  442. $('.request-active').unbind().click(request_handler(4));
  443. $('.request-deny').unbind().click(request_handler(3));
  444. $('.request-approve').unbind().click(request_handler(2));
  445. $('.make-leader').unbind().click(makeLeader);
  446. $('.remove-mentee').unbind().click(removeMentee);
  447. $('.ban-mentee').unbind().click(banMentee);
  448. $('.make-moderator').unbind().click(makeModerator);
  449. $('.official-notice-toggle').unbind().checkbox(manageNotices({ "official": true }), manageNotices({ "official": false }));
  450. $('.discuss-notice-toggle').unbind().checkbox(manageNotices({ "discuss": true }), manageNotices({ "discuss": false }));
  451. $('.announce-notice-toggle').unbind().checkbox(manageNotices({ "announce": true }), manageNotices({ "announce": false }));
  452. $('.digest-notice-toggle').unbind().checkbox(manageDigest(true, true), manageDigest(false, true));
  453. $('.digest-notice-select').unbind().change(manageDigest(true));
  454. };
  455. var manageDigest = function(subscribe, toggle) {
  456. return function(nxt) {
  457. if (toggle) {
  458. if (!subscribe) {
  459. if (!confirm("Unsubscribe from the curated digest?")) {
  460. return true;
  461. }
  462. $('.digest-notice-select').attr("disabled", true);
  463. } else {
  464. $('.digest-notice-select').removeAttr("disabled");
  465. }
  466. }
  467. $.ajax({
  468. type: "POST",
  469. data: {
  470. digest: subscribe ? parseInt($('.digest-notice-select').val()) : 1
  471. },
  472. url: cleanURL('/accounts/settings/notice'),
  473. success: function(data) {
  474. }
  475. });
  476. return subscribe;
  477. };
  478. };
  479. var manageNotices = function(opts) {
  480. return function(nxt) {
  481. var subscribe = false;
  482. $.each(opts, function (k, v) {
  483. subscribe = v;
  484. return false;
  485. });
  486. if (!subscribe) {
  487. if (!confirm("Unsubscribe from this mailing list?")) {
  488. return true;
  489. }
  490. }
  491. $.ajax({
  492. type: "POST",
  493. data: opts,
  494. url: cleanURL('/accounts/settings/notice'),
  495. success: function(data) {
  496. }
  497. });
  498. return subscribe;
  499. };
  500. };
  501. var updateIntroFeed = function(data) {
  502. $('.intro-feed').html(data.value.feed);
  503. $('.intro-content-input').setCode('');
  504. bindIntroButtons();
  505. };
  506. var updateTalkFeed = function(data) {
  507. $('.talk-feed').html(data.value.feed);
  508. $('.talk-content-input').setCode('');
  509. $('.talk-leader-toggle').removeAttr('checked');
  510. bindTalkButtons();
  511. };
  512. var reloadFoundation = function() {
  513. $(document).unbind("click.fndtn");
  514. $(document).foundationTabs("events");
  515. $(document).foundationButtons();
  516. $(document).foundationTooltips();
  517. $(document).foundationAlerts();
  518. };
  519. var request_handler = function(resp) {
  520. return function(event) {
  521. var req = $(event.target).data('request');
  522. if (req) {
  523. switch(parseInt(req)) {
  524. case 1:
  525. case 3:
  526. if (!confirm("Approval will make you this person's guide. Continue?")) {
  527. return false;
  528. }
  529. break;
  530. default:
  531. break;
  532. }
  533. }
  534. $.ajax({
  535. type: "POST",
  536. url: cleanURL(window.location.pathname + '/lead/' + $(event.target).data('pk') + '/' + $(event.target).data('name') + '/'),
  537. data: {
  538. 'response': resp
  539. },
  540. success: function(data) {
  541. if (resp == 4) {
  542. // TODO: decide on what to do with "active" requests
  543. $(event.target).closest('.post').addClass('leadership-active');
  544. } else {
  545. $(event.target).closest('.post').fadeOut();
  546. }
  547. updateSidebar(data.value.sidebar);
  548. }
  549. });
  550. return false;
  551. };
  552. };
  553. var validateEmail = function(selector) {
  554. if(isValidEmailAddress(selector.val()))
  555. return true;
  556. selector.addClass('error');
  557. return false;
  558. };
  559. var validateEmails = function(selector) {
  560. var valid = true;
  561. $.each(selector.val().split(", "), function(i, v) {
  562. if(isValidEmailAddress(v))
  563. return true;
  564. selector.addClass('error');
  565. valid = false;
  566. return false;
  567. });
  568. return valid;
  569. };
  570. var validateEmpty = function(selectorList, redactorSelectorList) {
  571. var allGood = true;
  572. for (var i in selectorList) {
  573. if (selectorList[i].val().trim() === "") {
  574. allGood = false;
  575. selectorList[i].addClass("error");
  576. }
  577. }
  578. for (i in redactorSelectorList) {
  579. var rSelector = redactorSelectorList[i];
  580. if (rSelector.getText().trim() === "" && rSelector.getCode().indexOf("img") < 0 && rSelector.getCode().indexOf("iframe") < 0) {
  581. allGood = false;
  582. rSelector.addClass("error");
  583. }
  584. }
  585. return allGood;
  586. };
  587. var validateTalk = function() {
  588. $('.talk-content-input').removeClass('error');
  589. return validateEmpty([], [$('.talk-content-input')]);
  590. };
  591. var validateIntro = function() {
  592. $('.intro-content-input').removeClass('error');
  593. return validateEmpty([], [$('.intro-content-input')]);
  594. };
  595. var validateEvent = function() {
  596. $('#createEvent').find('.error').removeClass('error');
  597. var nonEmpty = validateEmpty([$('.event-title-input'), $('.event-time-start'),$('.event-day-start'), $('.event-time-end'),$('.event-day-end'), $('.event-where-input')], [$('.event-content-input')]);
  598. if (!nonEmpty) return false;
  599. var $where = $('.event-where-input');
  600. if ($where.val().length < 3) {
  601. $where.addClass('error');
  602. return false;
  603. }
  604. var startTime = new Date($('.event-day-start').val().trim() + ' ' + $('.event-time-start').val().trim());
  605. var endTime = new Date($('.event-day-end').val().trim() + ' ' + $('.event-time-end').val().trim());
  606. if (startTime < endTime) return true;
  607. $('.event-time-start').addClass('error');
  608. $('.event-day-start').addClass('error');
  609. $('.event-time-end').addClass('error');
  610. $('.event-day-end').addClass('error');
  611. return false;
  612. };
  613. var validateEventEdit = function() {
  614. $('#eventEditModal').find('.error').removeClass('error');
  615. var nonEmpty = validateEmpty([$('.edit-event-title-input'), $('.edit-event-time-start'),$('.edit-event-day-start'), $('.edit-event-time-end'),$('.edit-event-day-end'), $('.edit-event-where-input')], [$('.edit-event-content-input')]);
  616. if (!nonEmpty) return false;
  617. var $where = $('.edit-event-where-input');
  618. if ($where.val().length < 3) {
  619. $where.addClass('error');
  620. return false;
  621. }
  622. var startTime = new Date($('.edit-event-day-start').val().trim() + ' ' + $('.edit-event-time-start').val().trim());
  623. var endTime = new Date($('.edit-event-day-end').val().trim() + ' ' + $('.edit-event-time-end').val().trim());
  624. if (startTime < endTime) return true;
  625. $('.edit-event-time-start').addClass('error');
  626. $('.edit-event-day-start').addClass('error');
  627. $('.edit-event-time-end').addClass('error');
  628. $('.edit-event-day-end').addClass('error');
  629. return false;
  630. };
  631. var validateEdit = function() {
  632. $('.edit-bulletin-input').removeClass('error');
  633. return validateEmpty([], [$('.edit-bulletin-input')]);
  634. };
  635. var validateModal = function(modalSelector) {
  636. $(modalSelector).find('.error').removeClass('error');
  637. return validateEmpty([$(modalSelector).find('textarea')], []);
  638. };
  639. var submitReferral = function() {
  640. $('.referral-email-input').removeClass('error');
  641. if(validateEmpty([$('.referral-email-input')], []) && validateEmails($('.referral-email-input'))) {
  642. $.ajax({
  643. type: "POST",
  644. data: {
  645. 'target': $('.referral-email-input').val()
  646. },
  647. url: cleanURL('/accounts/invite/'),
  648. success: function(data) {
  649. $('.referral-email-input').val();
  650. $('#confirmInvite').reveal();
  651. }
  652. });
  653. }
  654. return false;
  655. };
  656. var submitInvite = function() {
  657. $('.invite-email-input').removeClass('error');
  658. if(validateEmpty([$('.invite-email-input')], []) && validateEmail($('.invite-email-input'))) {
  659. $.ajax({
  660. type: "POST",
  661. data: {
  662. 'target': $('.invite-email-input').val()
  663. },
  664. url: cleanURL('/accounts/invite/'),
  665. success: function(data) {
  666. $('.invite-email-input').val("");
  667. $('#confirmInvite').reveal();
  668. }
  669. });
  670. }
  671. return false;
  672. };
  673. var banUser = function() {
  674. $.ajax({
  675. type: "POST",
  676. data: {
  677. 'username': $('.ban-user-input').select2("val")
  678. },
  679. url: cleanURL('/accounts/ban/'),
  680. success: function(data) {
  681. $('.ban-user-input').select2("val", "");
  682. $('#confirmBan').reveal();
  683. updateSidebar(data.value.sidebar);
  684. }
  685. });
  686. return false;
  687. };
  688. var doMentee = function() {
  689. $.ajax({
  690. type: "POST",
  691. data: {
  692. 'mentee': $('.transfer-mentee-input').select2("val"),
  693. 'leader': $('.transfer-leader-input').select2("val")
  694. },
  695. url: cleanURL('/accounts/transfer/'),
  696. success: function(data) {
  697. $('.transfer-mentee-input').select2("val", "");
  698. $('.transfer-leader-input').select2("val", "");
  699. $('#confirmGeneral').reveal();
  700. updateSidebar(data.value.sidebar);
  701. }
  702. });
  703. return false;
  704. };
  705. var doModerator = function(grant) {
  706. return function() {
  707. $.ajax({
  708. type: "POST",
  709. data: {
  710. 'username': $('.moderator-user-input').select2("val"),
  711. 'grant': grant
  712. },
  713. url: cleanURL('/accounts/moderator/'),
  714. success: function(data) {
  715. $('.moderator-user-input').select2("val", "");
  716. $('#confirmGeneral').reveal();
  717. updateSidebar(data.value.sidebar);
  718. }
  719. });
  720. return false;
  721. };
  722. };
  723. var doGuide = function(grant) {
  724. return function() {
  725. $.ajax({
  726. type: "POST",
  727. data: {
  728. 'username': $('.guide-user-input').select2("val"),
  729. 'grant': grant
  730. },
  731. url: cleanURL('/accounts/guide/'),
  732. success: function(data) {
  733. $('.guide-user-input').select2("val", "");
  734. $('#confirmGeneral').reveal();
  735. updateSidebar(data.value.sidebar);
  736. }
  737. });
  738. return false;
  739. };
  740. };
  741. var injectMarkup = function() {
  742. $.ajax({
  743. type: "POST",
  744. data: {
  745. 'markup': $('.markup-content').val()
  746. },
  747. url: cleanURL('/accounts/markup/'),
  748. success: function(data) {
  749. $('.markup-content').val();
  750. $('#confirmGeneral').reveal();
  751. }
  752. });
  753. return false;
  754. };
  755. var submitMessage = function(toArray, message, successCallback) {
  756. $('.group-message-content').removeClass('error');
  757. var from = "";
  758. if ($('.group-message-email-input').length) {
  759. $('.group-message-email-input').removeClass('error');
  760. from = $('.group-message-email-input').val();
  761. if(!validateEmail($('.group-message-email-input'))) return;
  762. }
  763. if(validateEmpty([$('.group-message-content')], [])) {
  764. $.ajax({
  765. type: "POST",
  766. data: {
  767. 'to': toArray,
  768. 'from': from,
  769. 'message': message
  770. },
  771. url: cleanURL('/accounts/message/'),
  772. success: function(data) {
  773. successCallback();
  774. }
  775. });
  776. }
  777. };
  778. $('.group-message-submit').click(function() {
  779. submitMessage($('.group-message-select').select2("val"), $('.group-message-content').val(), function() {
  780. $('#sendGroupMessage').trigger('reveal:close');
  781. $('.group-message-content').val('');
  782. });
  783. });
  784. var bindUserMessage = function() {
  785. $('.user-message').unbind().click( function(event) {
  786. $('#sendGroupMessage').reveal();
  787. $(".member-message-select,.group-message-select").select2("val", [$(event.target).data('username')]);
  788. });
  789. };
  790. bindUserMessage();
  791. var bindLeaderTemplates = function() {
  792. $('.group-message-template').change(function() {
  793. var $this = $(this);
  794. if ($this.val()) {
  795. $this.closest(".reveal-modal").find("textarea").val($this.val());
  796. }
  797. return true;
  798. });
  799. };
  800. bindLeaderTemplates();
  801. $('.member-message-submit').click(function() {
  802. submitMessage($('.member-message-select').select2("val"), $('.group-message-content').val(), function() {
  803. $('#sendGroupMessage').trigger('reveal:close');
  804. $('.group-message-content').val('');
  805. $(".member-message-select").select2("val", []);
  806. });
  807. });
  808. $('.pair-request').click(function() {
  809. $.ajax({
  810. type: "POST",
  811. data: {
  812. 'content': "I would like @" + $(this).data('username') + " to be my mentor"
  813. },
  814. url: cleanURL('/' + $(this).data('chapter') + '/lead/pair/'),
  815. success: function() {
  816. $("#confirmRequest").reveal();
  817. }
  818. });
  819. });
  820. $('.referral-submit').click(submitReferral);
  821. $('.referral-email-input').keydown(function(e) {
  822. if (e.keyCode == 13) {
  823. e.preventDefault();
  824. return submitReferral();
  825. }
  826. });
  827. $('.invite-submit').click(submitInvite);
  828. $('.invite-email-input').keydown(function(e) {
  829. if (e.keyCode == 13) {
  830. e.preventDefault();
  831. return submitInvite();
  832. }
  833. });
  834. $('.ban-user-submit').click(banUser);
  835. $('.transfer-mentee-submit').click(doMentee);
  836. $('.add-moderator-submit').click(doModerator(true));
  837. $('.remove-moderator-submit').click(doModerator(false));
  838. $('.add-guide-submit').click(doGuide(true));
  839. $('.remove-guide-submit').click(doGuide(false));
  840. $('.inject-markup-submit').click(injectMarkup);
  841. $('.righteous').click(function() {
  842. $(this).closest('.reveal-modal').trigger('reveal:close');
  843. });
  844. $('.talk-submit').click(function() {
  845. if(validateTalk()) {
  846. $.ajax({
  847. type: "POST",
  848. data: {
  849. 'content': $('.talk-content-input').getCode(),
  850. 'is_announcement': false,
  851. 'is_official': $('.talk-leader-toggle').is(':checked') || false
  852. },
  853. url: cleanURL(window.location.pathname + '/talk/'),
  854. success: updateTalkFeed
  855. });
  856. }
  857. return false;
  858. });
  859. $('.pending-submit').click(function() {
  860. $.ajax({
  861. type: "POST",
  862. data: {
  863. 'content': $('.pending-content-input').getCode(),
  864. 'is_announcement': false
  865. },
  866. url: '/pending/',
  867. success: function(data) {
  868. $('.pending-feed').html(data.value.feed);
  869. $('.pending-content-input').setCode('');
  870. }
  871. });
  872. return false;
  873. });
  874. $('.event-submit').click(function() {
  875. if(validateEvent()) {
  876. $.ajax({
  877. type: "POST",
  878. data: {
  879. 'start': $('.event-day-start').val().trim() + ' ' + $('.event-time-start').val().trim(),
  880. 'end': $('.event-day-end').val().trim() + ' ' + $('.event-time-end').val().trim(),
  881. 'where': $('.event-where-input').val(),
  882. 'title': $('.event-title-input').val(),
  883. 'content': $('.event-content-input').getCode()
  884. },
  885. url: cleanURL(window.location.pathname + '/event/'),
  886. success: function(data) {
  887. $('.event-feed').html(data.value.feed);
  888. $('#createEvent').trigger('reveal:close');
  889. bindEventButtons();
  890. clearEventModal();
  891. }
  892. });
  893. }
  894. return false;
  895. });
  896. $('.new-mentor-submit').click(function() {
  897. if(validateModal('#newMentor')) {
  898. $.ajax({
  899. type: "POST",
  900. data: {
  901. 'content': $('.new-mentor-content').val()
  902. },
  903. url: cleanURL(window.location.pathname + '/lead/pair/'),
  904. success: function(data) {
  905. $('#newMentor').trigger('reveal:close');
  906. $('.new-mentor-content').val('');
  907. updateSidebar(data.value.sidebar);
  908. }
  909. });
  910. }
  911. return false;
  912. });
  913. $('.mentor-help-submit').click(function() {
  914. if(validateModal('#help')) {
  915. $.ajax({
  916. type: "POST",
  917. data: {
  918. 'content': $('.mentor-help-content').val()
  919. },
  920. url: cleanURL(window.location.pathname + '/lead/help/'),
  921. success: function(data) {
  922. $('#help').trigger('reveal:close');
  923. $('.mentor-help-content').val('');
  924. updateSidebar(data.value.sidebar);
  925. }
  926. });
  927. }
  928. return false;
  929. });
  930. $('.become-mentor-submit').click(function() {
  931. if(validateModal('#becomeLeader')) {
  932. $.ajax({
  933. type: "POST",
  934. data: {
  935. 'content': $('.become-mentor-content').val()
  936. },
  937. url: cleanURL(window.location.pathname + '/lead/volunteer/'),
  938. success: function(data) {
  939. $('#becomeLeader').trigger('reveal:close');
  940. $('.become-mentor-content').val('');
  941. updateSidebar(data.value.sidebar);
  942. }
  943. });
  944. }
  945. return false;
  946. });
  947. $('.intro-submit').click(function() {
  948. if(validateIntro()) {
  949. $.ajax({
  950. type: "POST",
  951. data: {
  952. 'content': $('.intro-content-input').getCode()
  953. },
  954. url: cleanURL(window.location.pathname + '/intro/'),
  955. success: updateIntroFeed
  956. });
  957. }
  958. return false;
  959. });
  960. $('.comment-submit').click(function() {
  961. $.ajax({
  962. type: "POST",
  963. data: {
  964. 'content': $('.comment-content-input').val()
  965. },
  966. url: cleanURL(window.location.pathname + '/comment/'),
  967. success: function(data) {
  968. $('.comment-feed').html(data.value.feed);
  969. $('.comment-content-input').setCode('');
  970. }
  971. });
  972. return false;
  973. });
  974. $('.close-guide').click(function() {
  975. $.ajax({
  976. type: "POST",
  977. data: {
  978. 'remove': 1
  979. },
  980. url: '/accounts/guide/',
  981. success: function(data) {
  982. }
  983. });
  984. $('.to-do').fadeOut();
  985. return false;
  986. });
  987. $('.invite-button').click(function() {
  988. $(this).addClass("active");
  989. $('.no-invite-button').removeClass("active");
  990. $('.no-invite-content').fadeOut('fast', function() {
  991. $('.invite-content').fadeIn('fast');
  992. });
  993. });
  994. $('.no-invite-button').click(function() {
  995. $(this).addClass("active");
  996. $('.invite-button').removeClass("active");
  997. $('.invite-content').fadeOut('fast', function() {
  998. $('.no-invite-content').fadeIn("fast");
  999. });
  1000. });
  1001. $('.add-comment-toggle').click(function() {
  1002. $('.add-comment-form').fadeToggle();
  1003. });
  1004. $('.open-leader').click(function() {
  1005. $('.sidebar').find('.leader-message').click();
  1006. });
  1007. $('.open-events').click(function() {
  1008. $('.event-tab').find('a').click();
  1009. });
  1010. $('.open-introductions').click(function() {
  1011. $('.intro-tab').find('a').click();
  1012. });
  1013. $('.open-talk').click(function() {
  1014. $('.talk-tab').find('a').click();
  1015. });
  1016. var filterMembers = function() {
  1017. $('.box').removeClass('hide-item');
  1018. if($('.search-by-name').val().trim() !== "") {
  1019. var $boxClosest = $('.box').closest('.box');
  1020. $boxClosest.find(".name:not(:contains('" + $('.search-by-name').val() + "'))").closest('.box').addClass('hide-item');
  1021. $boxClosest.find(".username:contains('" + $('.search-by-name').val() + "')").closest('.box').removeClass('hide-item');
  1022. }
  1023. if($('.ambassadors-only').is(':checked')) {
  1024. $('.box').not(':has(.ambassador)').closest('.box').addClass('hide-item');
  1025. }
  1026. if($(".member-skill-input").select2("val").length) {
  1027. var $hideSet = $('.box');
  1028. var skills = $(".member-skill-input").select2("val");
  1029. for(var skill in skills) {
  1030. $('.box').each(function() {
  1031. var foundSet = $(this).find(".tags li").filter(function() {
  1032. return $(this).text().trim() === skills[skill];
  1033. });
  1034. if(!foundSet.length) {
  1035. $(this).addClass('hide-item');
  1036. }
  1037. });
  1038. }
  1039. }
  1040. $('.isotope').isotope({ filter: ":not(.hide-item)", transformsEnabled: false });
  1041. };
  1042. $('.ambassadors-only, .member-skill-input').change(filterMembers);
  1043. $('.search-by-name').change(filterMembers);
  1044. $('.search-by-name').keyup(filterMembers);
  1045. $('.members-filter-submit').click(filterMembers);
  1046. // Date Picker
  1047. $('.day-start, .day-end').datepicker();
  1048. // Time Picker
  1049. $(".time-start, .time-end").timePicker();
  1050. function addToggle(container) {
  1051. $('#' + container + ' .toggle-this').click(
  1052. function() {
  1053. var $holder = $("#" + container);
  1054. if ($('.toggle-content', $holder).is(':visible')) {
  1055. $('.toggle-arrow', $holder).text('+');
  1056. $('.toggle-content', $holder).hide();
  1057. } else {
  1058. $('.toggle-arrow', $holder).text('-');
  1059. $('.toggle-content', $holder).show();
  1060. }
  1061. return false;
  1062. });
  1063. }
  1064. addToggle("member-toggle");
  1065. addToggle("event-toggle");
  1066. addToggle("comment-toggle");
  1067. addToggle("application-toggle");
  1068. // Select2 on Members Search
  1069. $(".group-message-select").select2({
  1070. tags: members
  1071. });
  1072. // $(".event-skill-input").select2({
  1073. // tags: skills
  1074. // });
  1075. if(typeof skills !== 'undefined') {
  1076. $(".account-skill-input").select2({
  1077. tags: skills
  1078. });
  1079. $(".account-affiliation-input").select2({
  1080. tags: affiliations
  1081. });
  1082. $(".member-skill-input").select2({
  1083. tags: skills.concat(affiliations, initiatives)
  1084. });
  1085. $('.member-message-select').select2({
  1086. tags: members
  1087. });
  1088. $('.user-select').select2({
  1089. tags: members,
  1090. multiple: false,
  1091. maximumSelectionSize: 1
  1092. });
  1093. }
  1094. bindTalkButtons();
  1095. bindIntroButtons();
  1096. bindEventButtons();
  1097. bindSidebar();
  1098. // Masonry on members page
  1099. // $('.member-grid .row').masonry({
  1100. // itemSelector: '.box',
  1101. // });
  1102. // $('.member-grid').hide();
  1103. $('.isotope').isotope({
  1104. // options
  1105. masonry: {
  1106. columnWidth: 310
  1107. },
  1108. transformsEnabled: false,
  1109. itemSelector : '.box',
  1110. onLayout: function() {
  1111. $(".isotope").css("visibility", "visible");
  1112. $(".isotope-loading").hide();
  1113. }
  1114. });
  1115. var tags = getParameterByName("s");
  1116. if(tags !== "") {
  1117. tags = tags.split();
  1118. $(".member-skill-input").select2("val", tags);
  1119. }
  1120. var name = getParameterByName("n");
  1121. if(name !== "") {
  1122. $(".search-by-name").val(name);
  1123. }
  1124. if(name !== "" || tags !== "") {
  1125. filterMembers();
  1126. $('.toggle-arrow').click();
  1127. }
  1128. // $('.member-grid').show();
  1129. // $('.member-grid').show();
  1130. // // Masonry on members page
  1131. // $('.chapter-grid .row').masonry({
  1132. // itemSelector: '.box',
  1133. // columnWidth: 320,
  1134. // isFitWidth: true
  1135. // });
  1136. // UNCOMMENT THE LINE YOU WANT BELOW IF YOU WANT IE8 SUPPORT AND ARE USING .block-grids
  1137. // $('.block-grid.two-up>li:nth-child(2n+1)').css({clear: 'both'});
  1138. // $('.block-grid.three-up>li:nth-child(3n+1)').css({clear: 'both'});
  1139. // $('.block-grid.four-up>li:nth-child(4n+1)').css({clear: 'both'});
  1140. // $('.block-grid.five-up>li:nth-child(5n+1)').css({clear: 'both'});
  1141. // Hide address bar on mobile devices (except if #hash present, so we don't mess up deep linking).
  1142. if (Modernizr.touch && !window.location.hash) {
  1143. $(window).load(function () {
  1144. setTimeout(function () {
  1145. window.scrollTo(0, 1);
  1146. }, 0);
  1147. });
  1148. }
  1149. });
  1150. })(jQuery, this);