events.js 71 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674
  1. var feedbackData;
  2. function gup( name, url ) {
  3. if (!url) url = location.href;
  4. name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  5. var regexS = "[\\?&]"+name+"=([^&#]*)";
  6. var regex = new RegExp( regexS );
  7. var results = regex.exec( url );
  8. return results == null ? null : results[1];
  9. }
  10. function getSpotifyInfo(title, cb, artist) {
  11. var q = "";
  12. q = title;
  13. if (artist !== undefined) {
  14. q += " artist:" + artist;
  15. }
  16. $.ajax({
  17. type: "GET",
  18. url: 'https://api.spotify.com/v1/search?q=' + encodeURIComponent(q) + '&type=track',
  19. applicationType: "application/json",
  20. contentType: "json",
  21. success: function (data) {
  22. cb(data);
  23. }
  24. });
  25. }
  26. function executeCommand(command, params){
  27. if (command === "help" || command === "commands") {
  28. $('#helpModal').modal('show');
  29. return true;
  30. } else if (command === "volume") {
  31. if (params.length === 1) {
  32. var volume = Number(params[0]);
  33. if (volume >= 0 || volume <= 100) {
  34. if (volume === 0) {
  35. $("#volume-icon").removeClass("fa-volume-down").addClass("fa-volume-off")
  36. } else {
  37. $("#volume-icon").removeClass("fa-volume-off").addClass("fa-volume-down")
  38. }
  39. $("#volume-slider").slider("setValue", volume);
  40. if (YTPlayer !== undefined) {
  41. YTPlayer.setVolume(volume);
  42. localStorage.setItem("volume", volume);
  43. }
  44. return true;
  45. }
  46. }
  47. } else if(command === "mute"){
  48. $("#volume-slider").slider("setValue", 0);
  49. $("#volume-icon").removeClass("fa-volume-down").addClass("fa-volume-off");
  50. if (YTPlayer !== undefined) {
  51. YTPlayer.setVolume(0);
  52. localStorage.setItem("volume", 0);
  53. }
  54. } else if(command === "ban"){
  55. var user = params[0];
  56. var time = params[1];
  57. var reason = params[2];
  58. Meteor.call("banUser", user, time, reason, function(err, res){
  59. if(err){
  60. console.log(err);
  61. }
  62. });
  63. } else if(command === "silence"){
  64. var user = params[0];
  65. var time = params[1];
  66. Meteor.call("muteUser", user, time, function(err, res){
  67. if(err){
  68. console.log(err);
  69. }
  70. });
  71. } else if(command === "unban"){
  72. var user = params[0];
  73. Meteor.call("unbanUser", user, function(err, res){
  74. if(err){
  75. console.log(err);
  76. }
  77. });
  78. } else if(command === "unsilence"){
  79. var user = params[0];
  80. Meteor.call("unsilenceUser", user, function(err, res){
  81. if(err){
  82. console.log(err);
  83. }
  84. });
  85. } else if(command === "pause"){
  86. Meteor.call("pauseRoom", Session.get("type"), function(err, res){
  87. if(err){
  88. console.log(err);
  89. }
  90. });
  91. } else if(command === "resume"){
  92. Meteor.call("resumeRoom", Session.get("type"), function(err, res){
  93. if(err){
  94. console.log(err);
  95. }
  96. });
  97. } else if(command === "shuffle"){
  98. Meteor.call("shufflePlaylist", Session.get("type"), function(err, res){
  99. if(err){
  100. console.log(err);
  101. }
  102. });
  103. } else if(command === "skip"){
  104. Meteor.call("skipSong", Session.get("type"), function(err, res){
  105. if(err){
  106. console.log(err);
  107. }
  108. });
  109. }
  110. }
  111. function sendMessage() {
  112. var message = $("#chat-input").val();
  113. if (!$("#chat-input").hasClass("disabled")) {
  114. if (message.length > 0 && message[0] !== " ") {
  115. if (message[0] === "/") {
  116. message = message.split("");
  117. message.shift();
  118. message = message.join("");
  119. var params = message.split(" ");
  120. params = params.map(function(param) {
  121. return param.replace(/\r?\n|\r/g, "");
  122. });
  123. var command = params.shift();
  124. command = command.replace(/\r?\n|\r/g, "");
  125. if (executeCommand(command, params)) {
  126. $("#chat-input").val("");
  127. } else {
  128. $("#chat-input").val("");
  129. }
  130. } else {
  131. $("#chat-input").addClass("disabled");
  132. $("#chat-input").attr("disabled", "");
  133. Meteor.call("sendMessage", Session.get("type"), message, function (err, res) {
  134. if(err){
  135. $("#chat-input").val("");
  136. $("#chat-input").removeAttr("disabled");
  137. $("#chat-input").removeClass("disabled");
  138. }
  139. if (res) {
  140. $("#chat-input").val("");
  141. $("#chat-input").removeAttr("disabled");
  142. $("#chat-input").removeClass("disabled");
  143. }
  144. });
  145. }
  146. }
  147. }
  148. }
  149. function sendMessageGlobal() {
  150. var message = $("#chat-message").val();
  151. if (!$("#chat-message").hasClass("disabled")) {
  152. if (message.length > 0 && message[0] !== " ") {
  153. if (message[0] === "/") {
  154. message = message.split("");
  155. message.shift();
  156. message = message.join("");
  157. var params = message.split(" ");
  158. var command = params.shift();
  159. command = command.replace(/\r?\n|\r/g, "");
  160. if (executeCommand(command, params)) {
  161. $("#chat-message").val("");
  162. } else {
  163. $("#chat-message").val("");
  164. }
  165. } else {
  166. Meteor.call("sendMessage", "global", message, function (err, res) {
  167. if (res) {
  168. $("#chat-message").val("");
  169. }
  170. });
  171. }
  172. }
  173. }
  174. }
  175. Template.admin.events({
  176. "click a": function(e) {
  177. var target = $(e.target);
  178. if (target.hasClass("activate-alert-button") || target.parent().hasClass("activate-alert-button")) {
  179. var id = target.data("id") || target.parent().data("id");
  180. Meteor.call("activateAlert", id);
  181. } else if (target.hasClass("deactivate-alert-button") || target.parent().hasClass("deactivate-alert-button")) {
  182. var id = target.data("id") || target.parent().data("id");
  183. Meteor.call("deactivateAlert", id);
  184. }
  185. },
  186. "click #croom_create": function() {
  187. Meteor.call("createRoom", $("#croom_display").val(), $("#croom_tag").val(), $("#croom_private").prop("checked"), $("#croom_desc").val(), function (err, res) {
  188. if (err) {
  189. var $toastContent = $('<span><strong>Room not added.</strong> ' + err.reason + '</span>');
  190. Materialize.toast($toastContent, 8000);
  191. } else {
  192. window.location = "/" + $("#croom_tag").val();
  193. }
  194. });
  195. },
  196. "click #submit-alert": function(){
  197. var alertDesc = $("#alert-desc").val()
  198. if(alertDesc !== ""){
  199. Meteor.call("addAlert", alertDesc);
  200. }
  201. },
  202. "click .delete-alert-button": function(e) {
  203. var target = $(e.target);
  204. var id = $(e.target).data("id") || target.parent().data("id");
  205. Meteor.call("deleteAlert", id);
  206. },
  207. "click #rrating": function(){
  208. Meteor.call("resetRating");
  209. }
  210. });
  211. Template.feedback.events({
  212. "click #feedback_submit": function(){
  213. if(Meteor.userId()){
  214. if($("#feedback_message").val().length !== 0 && $("#feedback_message").hasClass("invalid") === false){
  215. Meteor.call("sendFeedback", $("#feedback_message").val());
  216. $("#feedback_message").val("");
  217. $("#createFeedback").closeModal()
  218. } else{
  219. var $toastContent = $('<span><strong>Feedback not sent.</strong> Possible reasons include:<ul><li>- Empty Feedback Message</li><li>- Feedback is more than 500 words</li></ul></span>');
  220. Materialize.toast($toastContent, 8000);
  221. }
  222. } else {
  223. var $toastContent = $('<span><strong>Feedback not sent.</strong> You must be logged in.</span>');
  224. Materialize.toast($toastContent, 4000);
  225. }
  226. },
  227. "click .upvote": function(){
  228. var message = $(this).parent("card").prevObject[0].message;
  229. Meteor.call("upvoteFeedback", message);
  230. },
  231. "click #delete": function(){
  232. var message = $(this).parent("card").prevObject[0].message;
  233. Meteor.call("deleteFeedback", message);
  234. },
  235. "click #edit": function(){
  236. $("#editModal").click()
  237. var data = Feedback.findOne({"message": $(this).parent("card").prevObject[0].message});
  238. feedbackData = data.message;
  239. $("#edit_feedback_message").val(data.message);
  240. },
  241. "click #edit_feedback_submit": function(){
  242. var oldMessage = feedbackData;
  243. var newMessage = $("#edit_feedback_message").val()
  244. $("#edit_feedback_message").val("")
  245. Meteor.call("updateFeedback", oldMessage, newMessage);
  246. $("#editFeedback").closeModal();
  247. }
  248. });
  249. Template.header.events({
  250. "click .logout": function(e){
  251. e.preventDefault();
  252. Meteor.logout();
  253. if (hpSound !== undefined) {
  254. hpSound.stop();
  255. }
  256. },
  257. "click #profile": function(){
  258. window.location = "/u/" + Meteor.user().profile.username;
  259. }
  260. });
  261. Template.login.events({
  262. "submit form": function(e){
  263. e.preventDefault();
  264. Session.set("github", false);
  265. var username = $("#username").val()
  266. var password = $("#password").val();
  267. Meteor.loginWithPassword(username, password, function(err) {
  268. if (err) {
  269. var $toastContent = $('<span><strong>Oh snap!</strong> ' + err.reason + '</span>');
  270. Materialize.toast($toastContent, 8000);
  271. } else {
  272. window.location.href = "/";
  273. }
  274. });
  275. },
  276. "click #github-login": function(){
  277. Meteor.loginWithGithub({loginStyle: "redirect"}, function(err, res) {
  278. console.log(err, res);
  279. });
  280. }
  281. });
  282. Template.playlist.events({
  283. "keyup #search-playlist": function(){
  284. if($("#search-playlist").val().length === 0){
  285. $(".pl-item").show();
  286. $("#up-next").show();
  287. } else {
  288. $("#up-next").hide();
  289. $(".pl-item").hide();
  290. var input = $("#search-playlist").val().toLowerCase();
  291. $(".pl-item .pl-title").each(function(i, el){
  292. console.log($(el).text());
  293. if($(el).text().toLowerCase().indexOf(input) !== -1){
  294. $(el).parent(".pl-item").show();
  295. }
  296. })
  297. $(".pl-item #pl-artist").each(function(i, el){
  298. console.log($(el).text());
  299. if($(el).text().toLowerCase().indexOf(input) !== -1){
  300. $(el).parent(".pl-item").show();
  301. }
  302. })
  303. }
  304. },
  305. "click #pl-item": function(){
  306. console.log($(this).text());
  307. }
  308. });
  309. Template.profile.events({
  310. //Edit real name
  311. "click #edit-name": function(){
  312. $("#name").hide();
  313. $("#name-div").show();
  314. $("#edit-name").hide();
  315. $("#cancel-edit").show();
  316. },
  317. "click #submit-name": function(){
  318. var user = Meteor.user();
  319. $("#name").show();
  320. $("#name-div").hide();
  321. $("#edit-name").show();
  322. $("#cancel-edit").hide();
  323. var realname = $("#input-name").val();
  324. var username = user.profile.username;
  325. $("#name").text("Name: " + realname);
  326. $("#input-name").val("")
  327. Meteor.call("updateRealName", realname);
  328. },
  329. "click #cancel-edit": function(){
  330. $("#name").show();
  331. $("#name-div").hide();
  332. $("#edit-name").show();
  333. $("#cancel-edit").hide();
  334. $("#input-name").val("");
  335. },
  336. //Edit username
  337. "click #edit-username": function(){
  338. $("#username").hide();
  339. $("#username-div").show();
  340. $("#edit-username").hide();
  341. $("#cancel-username").show();
  342. },
  343. "click #submit-username": function(){
  344. var user = Meteor.user()
  345. $("#username").show();
  346. $("#username-div").hide();
  347. $("#edit-username").show();
  348. $("#cancel-username").hide();
  349. var username = user.username;
  350. var newUserName = $("#input-username").val();
  351. $("#profile-name").text(newUserName)
  352. $("#username").text("Username: " + newUserName);
  353. $("#input-username").val("")
  354. Meteor.call("updateUserName", newUserName);
  355. window.location = "/u/" + newUserName;
  356. },
  357. "click #cancel-username": function(){
  358. $("#username").show();
  359. $("#username-div").hide();
  360. $("#edit-username").show();
  361. $("#cancel-username").hide();
  362. $("#input-username").val("");
  363. },
  364. // Admins only Edit Rank
  365. "click #edit-rank": function() {
  366. $("#rank").hide();
  367. $("#rank-div").show();
  368. $("#edit-rank").hide();
  369. $("#cancel-rank").show();
  370. },
  371. "click #submit-rank": function() {
  372. $("#rank").show();
  373. $("#rank-div").hide();
  374. $("#edit-rank").show();
  375. $("#cancel-rank").hide();
  376. var newRank = $("#select-rank option:selected").val();
  377. var username = Session.get("username");
  378. console.log(username, newRank);
  379. },
  380. "click #cancel-rank": function() {
  381. $("#rank").show();
  382. $("#rank-div").hide();
  383. $("#edit-rank").show();
  384. $("#cancel-rank").hide();
  385. }
  386. });
  387. var seekerBarInterval = undefined;
  388. Template.queues.events({
  389. /* TODO Add undo delete button */
  390. "input #id": function() {
  391. console.log("Change!");
  392. $("#previewPlayerContainer").addClass("hide-preview");
  393. },
  394. "input #img": function() {
  395. var url = $("#img").val();
  396. console.log(url);
  397. Session.set("image_url", url);
  398. },
  399. "click .preview-button": function(e){
  400. Session.set("song", this);
  401. $("#previewModal").openModal();
  402. },
  403. "click #previewImageButton": function() {
  404. $("#preview-image").attr("src", Session.get("song").img);
  405. },
  406. "click .edit-queue-button": function(e){
  407. Session.set("song", this);
  408. Session.set("genre", $(e.target).data("genre"));
  409. Session.set("type", "queue");
  410. $("#mid").val(this.mid).change();
  411. $("#artist").val(this.artist).change();
  412. $("#title").val(this.title).change();
  413. $("#img").val(this.img).change();
  414. $("#id").val(this.id).change();
  415. $("#likes").val(this.likes).change();
  416. $("#dislikes").val(this.dislikes).change();
  417. $("#duration").val(this.duration).change();
  418. $("#skip-duration").val(this.skipDuration).change();
  419. $("#genres").val(this.genres).change();
  420. $("#genres").material_select();
  421. $("#previewPlayerContainer").addClass("hide-preview");
  422. Session.set("image_url", this.img);
  423. Session.set("editing", true);
  424. $("#editModal").openModal({
  425. complete : function() {
  426. Session.set("editing", false);
  427. if (YTPlayer !== undefined && YTPlayer.stopVideo !== undefined) {
  428. YTPlayer.stopVideo();
  429. }
  430. }
  431. });
  432. },
  433. "click .add-song-button": function(e){
  434. var genre = $(e.target).data("genre") || $(e.target).parent().data("genre");
  435. Meteor.call("addSongToPlaylist", this, function(err) {
  436. console.log(err);
  437. if (err) {
  438. var $toastContent = $('<span><strong>Song not added.</strong> ' + err.reason + '</span>');
  439. Materialize.toast($toastContent, 8000);
  440. }
  441. });
  442. },
  443. "click .deny-song-button": function(e){
  444. var genre = $(e.target).data("genre") || $(e.target).parent().data("genre");
  445. Meteor.call("removeSongFromQueue", this.mid);
  446. },
  447. "click #play": function() {
  448. var duration = Session.get("song").duration;
  449. var d = moment.duration(parseInt(duration), 'seconds');
  450. $("#time-total").text(d.minutes() + ":" + ("0" + d.seconds()).slice(-2));
  451. $("#previewPlayerContainer").removeClass("hide-preview");
  452. var song = Session.get("song");
  453. var id = song.id;
  454. var volume = localStorage.getItem("volume") || 20;
  455. if (song.duration !== 0) {
  456. $("#play").attr("disabled", true);
  457. $("#stop").attr("disabled", false);
  458. $("#pause").attr("disabled", false);
  459. $("#forward").attr("disabled", false);
  460. if (YTPlayer === undefined) {
  461. YTPlayer = new YT.Player("previewPlayer", {
  462. height: 540,
  463. width: 568,
  464. videoId: id,
  465. playerVars: {autoplay: 1, controls: 0, iv_load_policy: 3, showinfo: 0, fs: 0},
  466. events: {
  467. 'onReady': function(event) {
  468. event.target.seekTo(Number(song.skipDuration));
  469. event.target.playVideo();
  470. event.target.setVolume(volume);
  471. },
  472. 'onStateChange': function(event){
  473. if (event.data == YT.PlayerState.PAUSED) {
  474. if (seekerBarInterval !== undefined) {
  475. Meteor.clearInterval(seekerBarInterval);
  476. seekerBarInterval = undefined;
  477. }
  478. }
  479. // if (event.data == YT.PlayerState.UNSTARTED) {
  480. // if (seekerBarInterval !== undefined) {
  481. // Meteor.clearInterval(seekerBarInterval);
  482. // seekerBarInterval = undefined;
  483. // }
  484. // $(".seeker-bar").css({width: "0"});
  485. // $("#time-elapsed").text("0:00");
  486. // $("#previewPlayerContainer").addClass("hide-preview");
  487. // console.log("HIDE MEY STACY!!!!")
  488. // }
  489. if (event.data == YT.PlayerState.PLAYING) {
  490. seekerBarInterval = Meteor.setInterval(function() {
  491. var duration = Session.get("song").duration;
  492. var timeElapsed = YTPlayer.getCurrentTime();
  493. var skipDuration = Session.get("song").skipDuration;
  494. if (duration <= (timeElapsed - skipDuration)) {
  495. YTPlayer.stopVideo();
  496. $("#play").attr("disabled", false);
  497. $("#stop").attr("disabled", true);
  498. $("#pause").attr("disabled", true);
  499. $("#forward").attr("disabled", true);
  500. $("#previewPlayerContainer").addClass("hide-preview");
  501. $(".seeker-bar").css({width: "0"});
  502. $("#time-elapsed").text("0:00");
  503. Meteor.clearInterval(seekerBarInterval);
  504. } else {
  505. var percentComplete = (timeElapsed - skipDuration) / duration * 100;
  506. $(".seeker-bar").css({width: percentComplete + "%"});
  507. var d = moment.duration(timeElapsed - skipDuration, 'seconds');
  508. $("#time-elapsed").text(d.minutes() + ":" + ("0" + d.seconds()).slice(-2));
  509. }
  510. }, 100);
  511. $("#play").attr("disabled", true);
  512. $("#stop").attr("disabled", false);
  513. $("#pause").attr("disabled", false);
  514. $("#forward").attr("disabled", false);
  515. } else {
  516. $("#play").attr("disabled", false);
  517. $("#stop").attr("disabled", true);
  518. $("#pause").attr("disabled", true);
  519. $("#forward").attr("disabled", true);
  520. }
  521. }
  522. }
  523. });
  524. } else {
  525. if (YTPlayer.getPlayerState() === 2) {
  526. YTPlayer.playVideo();
  527. } else {
  528. console.log(id, song.skipDuration, song.duration);
  529. YTPlayer.loadVideoById(id);
  530. YTPlayer.seekTo(Number(song.skipDuration));
  531. }
  532. }
  533. $("#previewPlayerContainer").removeClass("hide-preview");
  534. }
  535. },
  536. "click #stop": function() {
  537. $("#play").attr("disabled", false);
  538. $("#stop").attr("disabled", true);
  539. $("#pause").attr("disabled", true);
  540. $("#forward").attr("disabled", true);
  541. if (previewEndSongTimeout !== undefined) {
  542. Meteor.clearTimeout(previewEndSongTimeout);
  543. }
  544. if (YTPlayer !== undefined && YTPlayer.stopVideo !== undefined) {
  545. YTPlayer.stopVideo();
  546. }
  547. },
  548. "click #pause": function() {
  549. $("#play").attr("disabled", false);
  550. $("#stop").attr("disabled", false);
  551. $("#pause").attr("disabled", true);
  552. $("#forward").attr("disabled", true);
  553. if (previewEndSongTimeout !== undefined) {
  554. Meteor.clearTimeout(previewEndSongTimeout);
  555. }
  556. if (YTPlayer !== undefined && YTPlayer.pauseVideo !== undefined) {
  557. YTPlayer.pauseVideo();
  558. }
  559. },
  560. "click #forward": function() {
  561. var error = false;
  562. if (YTPlayer !== undefined) {
  563. var duration = Number(Session.get("song").duration) | 0;
  564. var skipDuration = Number(Session.get("song").skipDuration) | 0;
  565. if (YTPlayer.getDuration() < duration + skipDuration) {
  566. var $toastContent = $('<span><strong>Error.</strong> The song duration is longer than the length of the video.</span>');
  567. Materialize.toast($toastContent, 8000);
  568. error = true;
  569. } else {
  570. YTPlayer.seekTo(skipDuration + duration - 10);
  571. }
  572. }
  573. if (!error) {
  574. if (previewEndSongTimeout !== undefined) {
  575. Meteor.clearTimeout(previewEndSongTimeout);
  576. }
  577. previewEndSongTimeout = Meteor.setTimeout(function() {
  578. if (YTPlayer !== undefined) {
  579. YTPlayer.stopVideo();
  580. }
  581. $("#play").attr("disabled", false);
  582. $("#stop").attr("disabled", true);
  583. $("#pause").attr("disabled", true);
  584. $("#forward").attr("disabled", true);
  585. $("#previewPlayerContainer").addClass("hide-preview");
  586. }, 10000);
  587. }
  588. },
  589. "click #get-spotify-info": function() {
  590. var search = $("#title").val();
  591. var artistName = $("#artist").val();
  592. getSpotifyInfo(search, function(data) {
  593. for(var i in data){
  594. for(var j in data[i].items){
  595. if(search.indexOf(data[i].items[j].name) !== -1 && artistName.indexOf(data[i].items[j].artists[0].name) !== -1){
  596. $("#img").val(data[i].items[j].album.images[0].url).change();
  597. $("#duration").val(data[i].items[j].duration_ms / 1000).change();
  598. return;
  599. }
  600. }
  601. }
  602. }, artistName);
  603. },
  604. "click #save-song-button": function() {
  605. var newSong = {};
  606. newSong.mid = $("#mid").val();
  607. newSong.id = $("#id").val();
  608. newSong.likes = Number($("#likes").val());
  609. newSong.dislikes = Number($("#dislikes").val());
  610. newSong.title = $("#title").val();
  611. newSong.artist = $("#artist").val();
  612. newSong.img = $("#img").val();
  613. newSong.duration = Number($("#duration").val());
  614. newSong.skipDuration = $("#skip-duration").val();
  615. newSong.requestedBy = Session.get("song").requestedBy;
  616. newSong.genres = $("#genres").val() || [];
  617. if(newSong.skipDuration === undefined){
  618. newSong.skipDuration = 0;
  619. }
  620. Meteor.call("updateQueueSong", newSong.mid, newSong, function(err, res) {
  621. if (err) {
  622. var $toastContent = $('<span><strong>Song not saved.</strong> ' + err.reason + '</span>');
  623. Materialize.toast($toastContent, 8000);
  624. } else {
  625. $("#editModal").closeModal();
  626. var $toastContent = $('<span><strong>Song saved!</strong> No errors were found.</span>');
  627. Materialize.toast($toastContent, 4000);
  628. Session.set("song", newSong);
  629. }
  630. });
  631. }
  632. });
  633. Template.manageStation.events({
  634. /* TODO Add undo delete button */
  635. "click #editDescButton": function() {
  636. var parts = location.href.split('/');
  637. parts.pop();
  638. var id = parts.pop();
  639. var type = id.toLowerCase();
  640. var editingDesc = Session.get("editingDesc");
  641. if (!editingDesc) {
  642. Session.set("editingDesc", !editingDesc);
  643. } else {
  644. var newDesc = $("#editDesc").val();
  645. Meteor.call("editRoomDesc", type, newDesc);
  646. Session.set("editingDesc", !editingDesc);
  647. }
  648. },
  649. "input #id": function() {
  650. $("#previewPlayerContainer").addClass("hide-preview");
  651. },
  652. "input #img": function() {
  653. var url = $("#img").val();
  654. Session.set("image_url", url);
  655. },
  656. "click .preview-button": function(e){
  657. Session.set("song", this);
  658. $("#previewModal").openModal();
  659. },
  660. "click #previewImageButton": function() {
  661. $("#preview-image").attr("src", Session.get("song").img);
  662. },
  663. "click .edit-song-button": function(e){
  664. Session.set("song", this);
  665. Session.set("genre", $(e.target).data("genre"));
  666. $("#mid").val(this.mid).change();
  667. $("#artist").val(this.artist).change();
  668. $("#title").val(this.title).change();
  669. $("#img").val(this.img).change();
  670. $("#id").val(this.id).change();
  671. $("#likes").val(this.likes).change();
  672. $("#dislikes").val(this.dislikes).change();
  673. $("#duration").val(this.duration).change();
  674. $("#skip-duration").val(this.skipDuration).change();
  675. $("#previewPlayerContainer").addClass("hide-preview");
  676. Session.set("image_url", this.img);
  677. Session.set("editing", true);
  678. $("#editModal").openModal({
  679. complete : function() {
  680. Session.set("editing", false);
  681. if (YTPlayer !== undefined && YTPlayer.stopVideo !== undefined) {
  682. YTPlayer.stopVideo();
  683. }
  684. }
  685. });
  686. },
  687. "click .remove-song-button": function(e){
  688. var genre = $(e.target).data("genre") || $(e.target).parent().data("genre");
  689. Meteor.call("removeSongFromPlaylist", genre, this.mid);
  690. },
  691. "click #play": function() {
  692. var duration = Session.get("song").duration;
  693. var d = moment.duration(parseInt(duration), 'seconds');
  694. $("#time-total").text(d.minutes() + ":" + ("0" + d.seconds()).slice(-2));
  695. $("#previewPlayerContainer").removeClass("hide-preview");
  696. var song = Session.get("song");
  697. var id = song.id;
  698. var volume = localStorage.getItem("volume") || 20;
  699. if (song.duration !== 0) {
  700. $("#play").attr("disabled", true);
  701. $("#stop").attr("disabled", false);
  702. $("#pause").attr("disabled", false);
  703. $("#forward").attr("disabled", false);
  704. if (YTPlayer === undefined) {
  705. YTPlayer = new YT.Player("previewPlayer", {
  706. height: 540,
  707. width: 568,
  708. videoId: id,
  709. playerVars: {autoplay: 1, controls: 0, iv_load_policy: 3, showinfo: 0, fs: 0},
  710. events: {
  711. 'onReady': function(event) {
  712. event.target.seekTo(Number(song.skipDuration));
  713. event.target.playVideo();
  714. event.target.setVolume(volume);
  715. },
  716. 'onStateChange': function(event){
  717. if (event.data == YT.PlayerState.PAUSED) {
  718. if (seekerBarInterval !== undefined) {
  719. Meteor.clearInterval(seekerBarInterval);
  720. seekerBarInterval = undefined;
  721. }
  722. }
  723. // if (event.data == YT.PlayerState.UNSTARTED) {
  724. // if (seekerBarInterval !== undefined) {
  725. // Meteor.clearInterval(seekerBarInterval);
  726. // seekerBarInterval = undefined;
  727. // }
  728. // $(".seeker-bar").css({width: "0"});
  729. // $("#time-elapsed").text("0:00");
  730. // $("#previewPlayerContainer").addClass("hide-preview");
  731. // }
  732. if (event.data == YT.PlayerState.PLAYING) {
  733. seekerBarInterval = Meteor.setInterval(function() {
  734. var duration = Session.get("song").duration;
  735. var timeElapsed = YTPlayer.getCurrentTime();
  736. var skipDuration = Session.get("song").skipDuration;
  737. if (duration <= (timeElapsed - skipDuration)) {
  738. YTPlayer.stopVideo();
  739. $("#play").attr("disabled", false);
  740. $("#stop").attr("disabled", true);
  741. $("#pause").attr("disabled", true);
  742. $("#forward").attr("disabled", true);
  743. $("#previewPlayerContainer").addClass("hide-preview");
  744. $(".seeker-bar").css({width: "0"});
  745. $("#time-elapsed").text("0:00");
  746. Meteor.clearInterval(seekerBarInterval);
  747. } else {
  748. var percentComplete = (timeElapsed - skipDuration) / duration * 100;
  749. $(".seeker-bar").css({width: percentComplete + "%"});
  750. var d = moment.duration(timeElapsed - skipDuration, 'seconds');
  751. $("#time-elapsed").text(d.minutes() + ":" + ("0" + d.seconds()).slice(-2));
  752. }
  753. }, 100);
  754. $("#play").attr("disabled", true);
  755. $("#stop").attr("disabled", false);
  756. $("#pause").attr("disabled", false);
  757. $("#forward").attr("disabled", false);
  758. } else {
  759. $("#play").attr("disabled", false);
  760. $("#stop").attr("disabled", true);
  761. $("#pause").attr("disabled", true);
  762. $("#forward").attr("disabled", true);
  763. }
  764. }
  765. }
  766. });
  767. } else {
  768. if (YTPlayer.getPlayerState() === 2) {
  769. YTPlayer.playVideo();
  770. } else {
  771. console.log(id, song.skipDuration, song.duration);
  772. YTPlayer.loadVideoById(id);
  773. YTPlayer.seekTo(Number(song.skipDuration));
  774. }
  775. }
  776. $("#previewPlayerContainer").removeClass("hide-preview");
  777. }
  778. },
  779. "click #stop": function() {
  780. $("#play").attr("disabled", false);
  781. $("#stop").attr("disabled", true);
  782. $("#pause").attr("disabled", true);
  783. $("#forward").attr("disabled", true);
  784. if (previewEndSongTimeout !== undefined) {
  785. Meteor.clearTimeout(previewEndSongTimeout);
  786. }
  787. if (YTPlayer !== undefined && YTPlayer.stopVideo !== undefined) {
  788. YTPlayer.stopVideo();
  789. }
  790. },
  791. "click #pause": function() {
  792. $("#play").attr("disabled", false);
  793. $("#stop").attr("disabled", false);
  794. $("#pause").attr("disabled", true);
  795. $("#forward").attr("disabled", true);
  796. if (previewEndSongTimeout !== undefined) {
  797. Meteor.clearTimeout(previewEndSongTimeout);
  798. }
  799. if (YTPlayer !== undefined && YTPlayer.pauseVideo !== undefined) {
  800. YTPlayer.pauseVideo();
  801. }
  802. },
  803. "click #forward": function() {
  804. var error = false;
  805. if (YTPlayer !== undefined) {
  806. var duration = Number(Session.get("song").duration) | 0;
  807. var skipDuration = Number(Session.get("song").skipDuration) | 0;
  808. if (YTPlayer.getDuration() < duration + skipDuration) {
  809. var $toastContent = $('<span><strong>Error.</strong> The song duration is longer than the length of the video.</span>');
  810. Materialize.toast($toastContent, 8000);
  811. error = true;
  812. } else {
  813. YTPlayer.seekTo(skipDuration + duration - 10);
  814. }
  815. }
  816. if (!error) {
  817. if (previewEndSongTimeout !== undefined) {
  818. Meteor.clearTimeout(previewEndSongTimeout);
  819. }
  820. previewEndSongTimeout = Meteor.setTimeout(function() {
  821. if (YTPlayer !== undefined) {
  822. YTPlayer.stopVideo();
  823. }
  824. $("#play").attr("disabled", false);
  825. $("#stop").attr("disabled", true);
  826. $("#pause").attr("disabled", true);
  827. $("#forward").attr("disabled", true);
  828. $("#previewPlayerContainer").addClass("hide-preview");
  829. }, 10000);
  830. }
  831. },
  832. "click #get-spotify-info": function() {
  833. var search = $("#title").val();
  834. var artistName = $("#artist").val();
  835. getSpotifyInfo(search, function(data) {
  836. for(var i in data){
  837. for(var j in data[i].items){
  838. if(search.indexOf(data[i].items[j].name) !== -1 && artistName.indexOf(data[i].items[j].artists[0].name) !== -1){
  839. $("#img").val(data[i].items[j].album.images[0].url).change();
  840. $("#duration").val(data[i].items[j].duration_ms / 1000).change();
  841. return;
  842. }
  843. }
  844. }
  845. }, artistName);
  846. },
  847. "click #save-song-button": function() {
  848. var newSong = {};
  849. newSong.mid = $("#mid").val();
  850. newSong.id = $("#id").val();
  851. newSong.likes = Number($("#likes").val());
  852. newSong.dislikes = Number($("#dislikes").val());
  853. newSong.title = $("#title").val();
  854. newSong.artist = $("#artist").val();
  855. newSong.img = $("#img").val();
  856. newSong.duration = Number($("#duration").val());
  857. newSong.skipDuration = $("#skip-duration").val();
  858. newSong.requestedBy = Session.get("song").requestedBy;
  859. newSong.genres = $("#genres").val();
  860. Meteor.call("updatePlaylistSong", newSong.mid, newSong, function(err, res) {
  861. console.log(err, res);
  862. if (err) {
  863. var $toastContent = $('<span><strong>Song not saved.</strong> ' + err.reason + '</span>');
  864. Materialize.toast($toastContent, 8000);
  865. } else {
  866. var $toastContent = $('<span><strong>Song saved!</strong> No errors were found.</span>');
  867. Materialize.toast($toastContent, 4000);
  868. Session.set("song", newSong);
  869. }
  870. });
  871. },
  872. "click .remove-report-button": function(){
  873. var parts = location.href.split('/');
  874. parts.pop();
  875. var id = parts.pop();
  876. var query = {room: id.toLowerCase()};
  877. var obj = $(this)[0];
  878. Meteor.call("removeReport", query, obj);
  879. },
  880. "click #deleteRoom": function(){
  881. var type = location.href.split("/")[3];
  882. Meteor.call("deleteRoom", type, function(err,res){
  883. window.location.href = "/";
  884. })
  885. }
  886. });
  887. Template.manageSongs.events({
  888. /* TODO Add undo delete button */
  889. "change #show_genres_cb": function() {
  890. var selected = $("#show_genres_cb").is(":checked");
  891. Session.set("showGenres", selected);
  892. },
  893. "change #show_no_genres_cb": function() {
  894. var selected = $("#show_no_genres_cb").is(":checked");
  895. Session.set("showNoGenres", selected);
  896. },
  897. "input #id": function() {
  898. $("#previewPlayerContainer").addClass("hide-preview");
  899. },
  900. "input #img": function() {
  901. var url = $("#img").val();
  902. Session.set("image_url", url);
  903. },
  904. "click .preview-button": function(e){
  905. Session.set("song", this);
  906. $("#previewModal").openModal();
  907. },
  908. "click #previewImageButton": function() {
  909. $("#preview-image").attr("src", Session.get("song").img);
  910. },
  911. "click .edit-song-button": function(e){
  912. Session.set("song", this);
  913. Session.set("genre", $(e.target).data("genre"));
  914. $("#mid").val(this.mid).change();
  915. $("#artist").val(this.artist).change();
  916. $("#title").val(this.title).change();
  917. $("#img").val(this.img).change();
  918. $("#id").val(this.id).change();
  919. $("#likes").val(this.likes).change();
  920. $("#dislikes").val(this.dislikes).change();
  921. $("#duration").val(this.duration).change();
  922. $("#skip-duration").val(this.skipDuration).change();
  923. $("#genres").val(this.genres).change();
  924. $("#genres").material_select();
  925. $("#previewPlayerContainer").addClass("hide-preview");
  926. Session.set("image_url", this.img);
  927. Session.set("editing", true);
  928. $("#editModal").openModal({
  929. complete : function() {
  930. Session.set("editing", false);
  931. if (YTPlayer !== undefined && YTPlayer.stopVideo !== undefined) {
  932. YTPlayer.stopVideo();
  933. }
  934. }
  935. });
  936. },
  937. "click .remove-song-button": function(){
  938. console.log(this.mid);
  939. console.log("TEST!");
  940. Meteor.call("deleteSong", this.mid);
  941. },
  942. "click #play": function() {
  943. var duration = Session.get("song").duration;
  944. var d = moment.duration(parseInt(duration), 'seconds');
  945. $("#time-total").text(d.minutes() + ":" + ("0" + d.seconds()).slice(-2));
  946. $("#previewPlayerContainer").removeClass("hide-preview");
  947. var song = Session.get("song");
  948. var id = song.id;
  949. var volume = localStorage.getItem("volume") || 20;
  950. if (song.duration !== 0) {
  951. $("#play").attr("disabled", true);
  952. $("#stop").attr("disabled", false);
  953. $("#pause").attr("disabled", false);
  954. $("#forward").attr("disabled", false);
  955. if (YTPlayer === undefined) {
  956. YTPlayer = new YT.Player("previewPlayer", {
  957. height: 540,
  958. width: 568,
  959. videoId: id,
  960. playerVars: {autoplay: 1, controls: 0, iv_load_policy: 3, showinfo: 0, fs: 0},
  961. events: {
  962. 'onReady': function(event) {
  963. event.target.seekTo(Number(song.skipDuration));
  964. event.target.playVideo();
  965. event.target.setVolume(volume);
  966. },
  967. 'onStateChange': function(event){
  968. if (event.data == YT.PlayerState.PAUSED) {
  969. if (seekerBarInterval !== undefined) {
  970. Meteor.clearInterval(seekerBarInterval);
  971. seekerBarInterval = undefined;
  972. }
  973. }
  974. // if (event.data == YT.PlayerState.UNSTARTED) {
  975. // if (seekerBarInterval !== undefined) {
  976. // Meteor.clearInterval(seekerBarInterval);
  977. // seekerBarInterval = undefined;
  978. // }
  979. // $(".seeker-bar").css({width: "0"});
  980. // $("#time-elapsed").text("0:00");
  981. // $("#previewPlayerContainer").addClass("hide-preview");
  982. // }
  983. if (event.data == YT.PlayerState.PLAYING) {
  984. seekerBarInterval = Meteor.setInterval(function() {
  985. var duration = Session.get("song").duration;
  986. var timeElapsed = YTPlayer.getCurrentTime();
  987. var skipDuration = Session.get("song").skipDuration;
  988. if (duration <= (timeElapsed - skipDuration)) {
  989. YTPlayer.stopVideo();
  990. $("#play").attr("disabled", false);
  991. $("#stop").attr("disabled", true);
  992. $("#pause").attr("disabled", true);
  993. $("#forward").attr("disabled", true);
  994. $("#previewPlayerContainer").addClass("hide-preview");
  995. $(".seeker-bar").css({width: "0"});
  996. $("#time-elapsed").text("0:00");
  997. Meteor.clearInterval(seekerBarInterval);
  998. } else {
  999. var percentComplete = (timeElapsed - skipDuration) / duration * 100;
  1000. $(".seeker-bar").css({width: percentComplete + "%"});
  1001. var d = moment.duration(timeElapsed - skipDuration, 'seconds');
  1002. $("#time-elapsed").text(d.minutes() + ":" + ("0" + d.seconds()).slice(-2));
  1003. }
  1004. }, 100);
  1005. $("#play").attr("disabled", true);
  1006. $("#stop").attr("disabled", false);
  1007. $("#pause").attr("disabled", false);
  1008. $("#forward").attr("disabled", false);
  1009. } else {
  1010. $("#play").attr("disabled", false);
  1011. $("#stop").attr("disabled", true);
  1012. $("#pause").attr("disabled", true);
  1013. $("#forward").attr("disabled", true);
  1014. }
  1015. }
  1016. }
  1017. });
  1018. } else {
  1019. if (YTPlayer.getPlayerState() === 2) {
  1020. YTPlayer.playVideo();
  1021. } else {
  1022. console.log(id, song.skipDuration, song.duration);
  1023. YTPlayer.loadVideoById(id);
  1024. YTPlayer.seekTo(Number(song.skipDuration));
  1025. }
  1026. }
  1027. $("#previewPlayerContainer").removeClass("hide-preview");
  1028. }
  1029. },
  1030. "click #stop": function() {
  1031. $("#play").attr("disabled", false);
  1032. $("#stop").attr("disabled", true);
  1033. $("#pause").attr("disabled", true);
  1034. $("#forward").attr("disabled", true);
  1035. if (previewEndSongTimeout !== undefined) {
  1036. Meteor.clearTimeout(previewEndSongTimeout);
  1037. }
  1038. if (YTPlayer !== undefined && YTPlayer.stopVideo !== undefined) {
  1039. YTPlayer.stopVideo();
  1040. }
  1041. },
  1042. "click #pause": function() {
  1043. $("#play").attr("disabled", false);
  1044. $("#stop").attr("disabled", false);
  1045. $("#pause").attr("disabled", true);
  1046. $("#forward").attr("disabled", true);
  1047. if (previewEndSongTimeout !== undefined) {
  1048. Meteor.clearTimeout(previewEndSongTimeout);
  1049. }
  1050. if (YTPlayer !== undefined && YTPlayer.pauseVideo !== undefined) {
  1051. YTPlayer.pauseVideo();
  1052. }
  1053. },
  1054. "click #forward": function() {
  1055. var error = false;
  1056. if (YTPlayer !== undefined) {
  1057. var duration = Number(Session.get("song").duration) | 0;
  1058. var skipDuration = Number(Session.get("song").skipDuration) | 0;
  1059. if (YTPlayer.getDuration() < duration + skipDuration) {
  1060. var $toastContent = $('<span><strong>Error.</strong> The song duration is longer than the length of the video.</span>');
  1061. Materialize.toast($toastContent, 8000);
  1062. error = true;
  1063. } else {
  1064. YTPlayer.seekTo(skipDuration + duration - 10);
  1065. }
  1066. }
  1067. if (!error) {
  1068. if (previewEndSongTimeout !== undefined) {
  1069. Meteor.clearTimeout(previewEndSongTimeout);
  1070. }
  1071. previewEndSongTimeout = Meteor.setTimeout(function() {
  1072. if (YTPlayer !== undefined) {
  1073. YTPlayer.stopVideo();
  1074. }
  1075. $("#play").attr("disabled", false);
  1076. $("#stop").attr("disabled", true);
  1077. $("#pause").attr("disabled", true);
  1078. $("#forward").attr("disabled", true);
  1079. $("#previewPlayerContainer").addClass("hide-preview");
  1080. }, 10000);
  1081. }
  1082. },
  1083. "click #get-spotify-info": function() {
  1084. var search = $("#title").val();
  1085. var artistName = $("#artist").val();
  1086. getSpotifyInfo(search, function(data) {
  1087. for(var i in data){
  1088. for(var j in data[i].items){
  1089. if(search.indexOf(data[i].items[j].name) !== -1 && artistName.indexOf(data[i].items[j].artists[0].name) !== -1){
  1090. $("#img").val(data[i].items[j].album.images[0].url).change();
  1091. $("#duration").val(data[i].items[j].duration_ms / 1000).change();
  1092. return;
  1093. }
  1094. }
  1095. }
  1096. }, artistName);
  1097. },
  1098. "click #save-song-button": function() {_
  1099. var newSong = {};
  1100. newSong.mid = $("#mid").val();
  1101. newSong.id = $("#id").val();
  1102. newSong.likes = Number($("#likes").val());
  1103. newSong.dislikes = Number($("#dislikes").val());
  1104. newSong.title = $("#title").val();
  1105. newSong.artist = $("#artist").val();
  1106. newSong.img = $("#img").val();
  1107. newSong.duration = Number($("#duration").val());
  1108. newSong.skipDuration = $("#skip-duration").val();
  1109. newSong.requestedBy = Session.get("song").requestedBy;
  1110. newSong.genres = $("#genres").val() || [];
  1111. Meteor.call("updatePlaylistSong", newSong.mid, newSong, function(err, res) {
  1112. console.log(err, res);
  1113. if (err) {
  1114. var $toastContent = $('<span><strong>Song not saved.</strong> ' + err.reason + '</span>');
  1115. Materialize.toast($toastContent, 8000);
  1116. } else {
  1117. var $toastContent = $('<span><strong>Song saved!</strong> No errors were found.</span>');
  1118. Materialize.toast($toastContent, 4000);
  1119. Session.set("song", newSong);
  1120. }
  1121. });
  1122. }
  1123. });
  1124. Template.register.events({
  1125. "submit form": function(e){
  1126. e.preventDefault();
  1127. var username = $("#username").val();
  1128. var email = $("#email").val();
  1129. var password = $("#password").val();
  1130. var acceptedTermsAndPrivacy = $("#termsPrivacyBTN").is(":checked");
  1131. var captchaData = grecaptcha.getResponse();
  1132. if (acceptedTermsAndPrivacy) {
  1133. Meteor.call("createUserMethod", {username: username, email: email, password: password}, captchaData, function(err, res) {
  1134. grecaptcha.reset();
  1135. if (err) {
  1136. console.log(err);
  1137. var $toastContent = $('<span><strong>Oh snap!</strong> ' + err.reason + '</span>');
  1138. Materialize.toast($toastContent, 8000);
  1139. } else {
  1140. Meteor.loginWithPassword(username, password);
  1141. Accounts.onLogin(function(){
  1142. window.location.href = "/";
  1143. })
  1144. }
  1145. });
  1146. } else {
  1147. var $toastContent = $('<span><strong>Oh snap!</strong> Please read and accept the Terms and Conditions and the Privacy Policy.</span>');
  1148. Materialize.toast($toastContent, 8000);
  1149. }
  1150. },
  1151. "click #github-login": function(){
  1152. Meteor.loginWithGithub({loginStyle: "redirect"}, function(err, res) {
  1153. console.log(err, res);
  1154. });
  1155. }
  1156. });
  1157. Template.news.events({
  1158. "click #createArticleButton": function() {
  1159. var title = $("#title").val();
  1160. var content = $("#content").val();
  1161. var anonymous = $("#anonymous").is(":checked");
  1162. Meteor.call("createArticle", {title: title, content: content, anonymous: anonymous}, function(err, res) {
  1163. if (err) {
  1164. var $toastContent = $('<span><strong>Article not created.</strong> ' + err.reason + '</span>');
  1165. Materialize.toast($toastContent, 8000);
  1166. } else {
  1167. $('#createArticle').closeModal()
  1168. $("#title").val("").change();
  1169. $("#content").val("").change();
  1170. $("#anonymous").prop("checked", false).change();
  1171. }
  1172. });
  1173. }
  1174. });
  1175. Template.room.events({
  1176. "input #volume_slider": function() {
  1177. console.log("Test3");
  1178. var volume = Number($("#volume_slider").val());
  1179. localStorage.setItem("volume", volume);
  1180. if (YTPlayer !== undefined) {
  1181. YTPlayer.setVolume(volume);
  1182. }
  1183. },
  1184. "click #add-song-modal-button": function() {
  1185. Session.set("songResults", []);
  1186. },
  1187. "click #return-button": function() {
  1188. Session.set("editingSong", false);
  1189. },
  1190. "click #removeSong": function(e) {
  1191. var id = $(e.target).data("result");
  1192. var songs = Session.get("songResults");
  1193. var currentSong;
  1194. songs = songs.filter(function(song) {
  1195. return id !== song.id;
  1196. });
  1197. Session.set("songResults", []);
  1198. Session.set("songResults", songs);
  1199. },
  1200. "click #addSong": function(e) {
  1201. var id = $(e.target).data("result");
  1202. var songs = Session.get("songResults");
  1203. var currentSong;
  1204. songs.forEach(function(song) {
  1205. if (song.id === id) {
  1206. currentSong = song;
  1207. }
  1208. });
  1209. Session.set("editingSong", true);
  1210. var title = currentSong.title;
  1211. var artist = currentSong.artist;
  1212. var img = currentSong.img;
  1213. getSpotifyInfo(title.replace(/\[.*\]/g, ""), function (data) {
  1214. if (data.tracks.items.length > 0) {
  1215. title = data.tracks.items[0].name;
  1216. var artists = [];
  1217. img = data.tracks.items[0].album.images[0].url;
  1218. data.tracks.items[0].artists.forEach(function (artist) {
  1219. artists.push(artist.name);
  1220. });
  1221. artist = artists.join(", ");
  1222. $("#title").val(title).change();
  1223. $("#artist").val(artist).change();
  1224. $("#img").val(img).change();
  1225. $("#id").val(id).change();
  1226. $("#genres").val(null).change();
  1227. } else {
  1228. $("#title").val(title).change();
  1229. $("#artist").val(artist).change();
  1230. $("#img").val(img).change();
  1231. $("#id").val(id).change();
  1232. $("#genres").val(null).change();
  1233. // I give up for now... Will fix this later. -Kris
  1234. }
  1235. });
  1236. },
  1237. "click #import-playlist-button": function () {
  1238. if (!Session.get("importingPlaylist")) {
  1239. Session.set("songResults", []);
  1240. var playlist_link = $("#playlist-url").val();
  1241. var playlist_id = gup("list", playlist_link);
  1242. var ytImportQueue = [];
  1243. var totalVideos = 0;
  1244. var videosInvalid = 0;
  1245. var videosInQueue = 0;
  1246. var videosInPlaylist = 0;
  1247. var ranOnce = false;
  1248. Session.set("importingPlaylist", true);
  1249. $("#import-playlist-button").attr("disabled", "");
  1250. $("#import-playlist-button").addClass("disabled");
  1251. $("#playlist-url").attr("disabled", "");
  1252. $("#playlist-url").addClass("disabled");
  1253. $("#import-progress").css({width: "0%"});
  1254. function makeAPICall(playlist_id, nextPageToken) {
  1255. if (nextPageToken !== undefined) {
  1256. nextPageToken = "&pageToken=" + nextPageToken;
  1257. } else {
  1258. nextPageToken = "";
  1259. }
  1260. $.ajax({
  1261. type: "GET",
  1262. url: "https://www.googleapis.com/youtube/v3/playlistItems?part=snippet&maxResults=50&playlistId=" + playlist_id + nextPageToken + "&key=AIzaSyAgBdacEWrHCHVPPM4k-AFM7uXg-Q__YXY",
  1263. applicationType: "application/json",
  1264. contentType: "json",
  1265. success: function (data) {
  1266. if (!ranOnce) {
  1267. ranOnce = true;
  1268. totalVideos = data.pageInfo.totalResults;
  1269. }
  1270. var nextToken = data.nextPageToken;
  1271. for (var i in data.items) {
  1272. var item = data.items[i];
  1273. if (item.snippet.thumbnails !== undefined) {
  1274. var genre = Session.get("type");
  1275. if (Playlists.find({
  1276. type: genre,
  1277. "songs.id": item.snippet.resourceId.videoId
  1278. }, {songs: {$elemMatch: {id: item.snippet.resourceId.videoId}}}).count() !== 0) {
  1279. videosInPlaylist++;
  1280. } else if (Queues.find({
  1281. type: genre,
  1282. "songs.id": item.snippet.resourceId.videoId
  1283. }, {songs: {$elemMatch: {id: item.snippet.resourceId.videoId}}}).count() !== 0) {
  1284. videosInQueue++;
  1285. } else {
  1286. var percentage = ytImportQueue.length / (totalVideos - videosInvalid) * 100;
  1287. $("#import-progress").css({width: percentage + "%"});
  1288. ytImportQueue.push({title: item.snippet.title, artist: item.snippet.channelTitle, id: item.snippet.resourceId.videoId, image: item.snippet.thumbnails.medium.url});
  1289. }
  1290. } else {
  1291. videosInvalid++;
  1292. }
  1293. }
  1294. if (nextToken !== undefined) {
  1295. makeAPICall(playlist_id, nextToken);
  1296. } else {
  1297. /*$("#playlist-import-queue > div > i").click(function () {
  1298. var title = $(this).parent().find("div > .song-result-title").text();
  1299. for (var i in ytImportQueue) {
  1300. if (ytImportQueue[i].title === title) {
  1301. ytImportQueue.splice(i, 1);
  1302. }
  1303. }
  1304. $(this).parent().remove();
  1305. Session.set("YTImportQueue", ytImportQueue);
  1306. });*/
  1307. Session.set("importingPlaylist", false);
  1308. $("#import-progress").css({width: "100%"});
  1309. $("#import-playlist-button").removeAttr("disabled");
  1310. $("#import-playlist-button").removeClass("disabled");
  1311. $("#playlist-url").removeAttr("disabled");
  1312. $("#playlist-url").removeClass("disabled");
  1313. Session.set("YTImportQueue", ytImportQueue);
  1314. Session.set("songResults", ytImportQueue);
  1315. }
  1316. },
  1317. error: function() {
  1318. Session.set("importingPlaylist", false);
  1319. $("#import-progress").css({width: "0%"});
  1320. $("#import-playlist-button").removeAttr("disabled");
  1321. $("#import-playlist-button").removeClass("disabled");
  1322. $("#playlist-url").removeAttr("disabled");
  1323. $("#playlist-url").removeClass("disabled");
  1324. }
  1325. })
  1326. }
  1327. makeAPICall(playlist_id);
  1328. }
  1329. },
  1330. "click #confirm-import": function () {
  1331. var YTImportQueue = Session.get("YTImportQueue");
  1332. $("#import-playlist-button").attr("disabled", "");
  1333. $("#import-playlist-button").addClass("disabled");
  1334. $("#playlist-url").attr("disabled", "");
  1335. $("#playlist-url").addClass("disabled");
  1336. $("#import-progress").css({width: "0%"});
  1337. var failed = 0;
  1338. var success = 0;
  1339. var processed = 0;
  1340. var total = YTImportQueue.length;
  1341. YTImportQueue.forEach(function (song) {
  1342. var songData = {type: "YouTube", id: song.id, title: song.title, artist: "", img: "", genres: [Session.get("type")]};
  1343. Meteor.call("addSongToQueue", songData, function (err, res) {
  1344. if (err) {
  1345. console.log(err);
  1346. failed++;
  1347. } else {
  1348. success++;
  1349. }
  1350. processed++;
  1351. var percentage = processed / total * 100;
  1352. $("#import-progress").css({width: percentage + "%"});
  1353. });
  1354. });
  1355. $("#import-playlist-button").removeAttr("disabled");
  1356. $("#import-playlist-button").removeClass("disabled");
  1357. $("#playlist-url").removeAttr("disabled", "");
  1358. $("#playlist-url").removeClass("disabled");
  1359. Session.set("songResults", []);
  1360. Session.set("YTImportQueue", [])
  1361. },
  1362. "click #global-chat-tab": function () {
  1363. $("#global-chat-tab").removeClass("unread-messages");
  1364. },
  1365. "click #sync": function () {
  1366. if (Session.get("currentSong") !== undefined) {
  1367. var room = Rooms.findOne({type: Session.get("type")});
  1368. if (room !== undefined) {
  1369. var timeIn = Date.now() - Session.get("currentSong").started - room.timePaused;
  1370. var skipDuration = Number(Session.get("currentSong").skipDuration) | 0;
  1371. if (YTPlayer !== undefined) {
  1372. YTPlayer.seekTo(skipDuration + timeIn / 1000);
  1373. }
  1374. }
  1375. }
  1376. },
  1377. "click #lock": function () {
  1378. Meteor.call("lockRoom", Session.get("type"));
  1379. var $parent = $("#lock").parent();
  1380. $("#lock").remove();
  1381. $parent.append('<a id="unlock"><i class="material-icons">lock_open</i></a>')
  1382. },
  1383. "click #unlock": function () {
  1384. Meteor.call("unlockRoom", Session.get("type"));
  1385. var $parent = $("#unlock").parent();
  1386. $("#unlock").remove();
  1387. $parent.append('<a id="lock"><i class="material-icons">lock_outline</i></a>')
  1388. },
  1389. "click #submit": function () {
  1390. sendMessageGlobal();
  1391. Meteor.setTimeout(function () {
  1392. $(".chat-ul").scrollTop(100000);
  1393. }, 1000)
  1394. },
  1395. "keyup #chat-message": function (e) {
  1396. if (e.type === "keyup" && e.which === 13) {
  1397. e.preventDefault();
  1398. if (!$('#chat-message').data('dropdownshown')) {
  1399. sendMessageGlobal();
  1400. Meteor.setTimeout(function () {
  1401. $(".chat-ul").scrollTop(100000);
  1402. }, 1000)
  1403. }
  1404. }
  1405. },
  1406. "click #like": function (e) {
  1407. $("#like").blur();
  1408. Meteor.call("likeSong", Session.get("currentSong").mid);
  1409. },
  1410. "click #dislike": function (e) {
  1411. $("#dislike").blur();
  1412. Meteor.call("dislikeSong", Session.get("currentSong").mid);
  1413. },
  1414. "click #vote-skip": function () {
  1415. Meteor.call("voteSkip", Session.get("type"), function (err, res) {
  1416. $("#vote-skip").addClass("disabled");
  1417. if(err){
  1418. var $toastContent = $('<span><strong>Vote not submitted</strong> ' + err.reason + '</span>');
  1419. Materialize.toast($toastContent, 4000);
  1420. }
  1421. });
  1422. },
  1423. "click #report-prev": function (e) {
  1424. if (Session.get("previousSong") !== undefined) {
  1425. Session.set("reportPrevious", true);
  1426. $("#report-prev").prop("disabled", true);
  1427. $("#report-curr").prop("disabled", false);
  1428. }
  1429. },
  1430. "click #report-curr": function (e) {
  1431. Session.set("reportPrevious", false);
  1432. $("#report-prev").prop("disabled", false);
  1433. $("#report-curr").prop("disabled", true);
  1434. },
  1435. "click #report-modal": function () {
  1436. Session.set("currentSongR", Session.get("currentSong"));
  1437. Session.set("previousSongR", Session.get("previousSong"));
  1438. },
  1439. "click #add-song-button": function (e) {
  1440. e.preventDefault();
  1441. parts = location.href.split('/');
  1442. var roomType = parts.pop();
  1443. var genre = roomType.toLowerCase();
  1444. var type = $("#type").val();
  1445. id = $("#id").val();
  1446. var title = $("#title").val();
  1447. var artist = $("#artist").val();
  1448. var genres = $("#genres").val() || [];
  1449. var songData = {type: type, id: id, title: title, artist: artist, genres: genres};
  1450. if (Songs.find({"id": songData.id}).count() > 0) {
  1451. var $toastContent = $('<span><strong>Song not added.</strong> This song has already been added.</span>');
  1452. Materialize.toast($toastContent, 8000);
  1453. } else if (Queues.find({"id": songData.id}).count() > 0) {
  1454. var $toastContent = $('<span><strong>Song not added.</strong> This song has already been requested.</span>');
  1455. Materialize.toast($toastContent, 3000);
  1456. } else {
  1457. Meteor.call("addSongToQueue", songData, function (err, res) {
  1458. console.log(err, res);
  1459. if (err) {
  1460. var $toastContent = $('<span><strong>Song not added.</strong> ' + err.reason + '</span>');
  1461. Materialize.toast($toastContent, 3000);
  1462. } else {
  1463. var $toastContent = $('<span><strong>Song added.</strong> Your song has succesfully been added to the queue.</span>');
  1464. Materialize.toast($toastContent, 3000);
  1465. $('#add_song_modal').closeModal();
  1466. Session.set("editingSong", false);
  1467. }
  1468. });
  1469. }
  1470. },
  1471. "click #toggle-video": function (e) {
  1472. e.preventDefault();
  1473. if (Session.get("mediaHidden")) {
  1474. $("#media-container").removeClass("hidden");
  1475. $("#toggle-video").text("Hide video");
  1476. Session.set("mediaHidden", false);
  1477. } else {
  1478. $("#media-container").addClass("hidden");
  1479. $("#toggle-video").text("Show video");
  1480. Session.set("mediaHidden", true);
  1481. }
  1482. },
  1483. "click #return": function (e) {
  1484. $("#add-info").hide();
  1485. $("#search-info").show();
  1486. },
  1487. "click #search-song": function () {
  1488. var songs = [];
  1489. Session.set("songResults", songs);
  1490. $.ajax({
  1491. type: "GET",
  1492. url: "https://www.googleapis.com/youtube/v3/search?part=snippet&q=" + $("#song-input").val() + "&key=AIzaSyAgBdacEWrHCHVPPM4k-AFM7uXg-Q__YXY&type=video&maxResults=25",
  1493. applicationType: "application/json",
  1494. contentType: "json",
  1495. success: function (data) {
  1496. for (var i in data.items) {
  1497. var item = data.items[i];
  1498. console.log(item);
  1499. songs.push({title: item.snippet.title, artist: item.snippet.channelTitle, id: item.id.videoId, image: item.snippet.thumbnails.medium.url});
  1500. }
  1501. Session.set("songResults", songs);
  1502. /*$("#song-results > div").click(function () {
  1503. $("#search-info").hide();
  1504. $("#add-info").show();
  1505. var title = $(this).find("div > .song-result-title").text();
  1506. for (var i in songs) {
  1507. if (songs[i].title === title) {
  1508. var songObj = {
  1509. id: songs[i].id,
  1510. title: songs[i].title,
  1511. type: "youtube"
  1512. };
  1513. $("#title").val(songObj.title);
  1514. $("#artist").val("");
  1515. $("#id").val(songObj.id);
  1516. getSpotifyInfo(songObj.title.replace(/\[.*\]/g, ""), function (data) {
  1517. if (data.tracks.items.length > 0) {
  1518. $("#title").val(data.tracks.items[0].name);
  1519. var artists = [];
  1520. $("#img").val(data.tracks.items[0].album.images[2].url);
  1521. data.tracks.items[0].artists.forEach(function (artist) {
  1522. artists.push(artist.name);
  1523. });
  1524. $("#artist").val(artists.join(", "));
  1525. }
  1526. });
  1527. }
  1528. }
  1529. })*/
  1530. }
  1531. })
  1532. },
  1533. "click #volume-icon": function () {
  1534. var volume = 0;
  1535. var slider = $("#volume-slider").slider();
  1536. $("#volume-icon").removeClass("fa-volume-down").addClass("fa-volume-off")
  1537. if (YTPlayer !== undefined) {
  1538. YTPlayer.setVolume(volume);
  1539. localStorage.setItem("volume", volume);
  1540. $("#volume-slider").slider("setValue", volume);
  1541. }
  1542. },
  1543. "click #play": function () {
  1544. Meteor.call("resumeRoom", Session.get("type"));
  1545. var $parent = $("#play").parent();
  1546. $("#play").remove();
  1547. $parent.append('<a id="pause"><i class="material-icons">pause</i></a>')
  1548. },
  1549. "click #pause": function () {
  1550. Meteor.call("pauseRoom", Session.get("type"));
  1551. var $parent = $("#pause").parent();
  1552. $("#pause").remove();
  1553. $parent.append('<a id="play"><i class="material-icons">play_arrow</i></a>')
  1554. },
  1555. "click #skip": function () {
  1556. Meteor.call("skipSong", Session.get("type"));
  1557. },
  1558. "click #shuffle": function () {
  1559. Meteor.call("shufflePlaylist", Session.get("type"));
  1560. },
  1561. "change input": function (e) {
  1562. /*if (e.target && e.target.id) {
  1563. var partsOfId = e.target.id.split("-");
  1564. partsOfId[1] = partsOfId[1].charAt(0).toUpperCase() + partsOfId[1].slice(1);
  1565. var camelCase = partsOfId.join("");
  1566. Session.set(camelCase, e.target.checked);
  1567. }*/
  1568. },
  1569. "click #report-song-button": function () {
  1570. var room = Session.get("type");
  1571. var reportData = {};
  1572. reportData.song = Session.get("currentSong").mid;
  1573. reportData.type = [];
  1574. reportData.reason = [];
  1575. $(".report-layer-1 > div > input:checked").each(function (){
  1576. reportData.type.push(this.id);
  1577. });
  1578. $(".report-layer-2 input:checked").each(function () {
  1579. reportData.reason.push(this.id);
  1580. });
  1581. console.log(reportData);
  1582. Meteor.call("submitReport", room, reportData, function () {
  1583. $("report_modal").closeModal();
  1584. });
  1585. },
  1586. "change #si_or_pl": function () {
  1587. Session.set("songResults", []);
  1588. Session.set("si_or_pl", $("#si_or_pl").val());
  1589. },
  1590. "click #close-modal-a": function () {
  1591. $("#select_single").attr("selected", true);
  1592. $("#search-info").show();
  1593. $("#playlist-import").hide();
  1594. },
  1595. "click #admin-dropdown a": function(){
  1596. Meteor.setTimeout(function(){
  1597. $(".dropdown-button").click();
  1598. }, 10);
  1599. }
  1600. });
  1601. // Settings Template
  1602. Template.settings.events({
  1603. "click #save-settings": function() {
  1604. Meteor.call("updateSettings", $("#showRating").is(":checked"), function(err,res){
  1605. var $toastContent = $('<span><strong>Settings Saved!</strong> No errors were found.</span>');
  1606. Materialize.toast($toastContent, 3000);
  1607. });
  1608. },
  1609. "click #delete-account": function(){
  1610. $("#delete-account").text("Click to confirm");
  1611. $("#delete-account").click(function(){
  1612. var bool = confirm("Are you sure you want to delete your account?");
  1613. if(bool) {
  1614. Meteor.call("deleteAccount");
  1615. } else{
  1616. $("#delete-account").text("Delete");
  1617. }
  1618. })
  1619. },
  1620. "click #change-password": function(){
  1621. var oldPassword = $("#old-password").val();
  1622. var newPassword= $("#new-password").val();
  1623. var confirmPassword = $("#confirm-password").val();
  1624. if(newPassword === confirmPassword){
  1625. Accounts.changePassword(oldPassword, newPassword, function(err){
  1626. if(err){
  1627. $("#old-password").val("");
  1628. $("#new-password").val("");
  1629. $("#confirm-password").val("");
  1630. $("<div class='alert alert-danger alert-dismissible' role='alert' style='margin-bottom: 0'><button type='button' class='close' data-dismiss='alert' aria-label='Close'><span aria-hidden='true'><i class='fa fa-times'></i></span></button><strong>Oh Snap! </strong>" + err.reason + "</div>").prependTo($("#head")).delay(7000).fadeOut(1000, function() { $(this).remove(); });
  1631. } else {
  1632. $("#old-password").val("");
  1633. $("#new-password").val("");
  1634. $("#confirm-password").val("");
  1635. $("<div class='alert alert-success alert-dismissible' role='alert' style='margin-bottom: 0'><button type='button' class='close' data-dismiss='alert' aria-label='Close'><span aria-hidden='true'><i class='fa fa-times'></i></span></button><strong>Hooray!</strong> You changed your password successfully.</div>").prependTo($("#head")).delay(7000).fadeOut(1000, function() { $(this).remove(); });
  1636. }
  1637. });
  1638. }
  1639. }
  1640. });
  1641. var previewEndSongTimeout = undefined;