events.js 70 KB

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