events.js 69 KB

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