2
0

events.js 71 KB

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