events.js 69 KB

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