client.js 77 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065
  1. Meteor.startup(function() {
  2. reCAPTCHA.config({
  3. publickey: '6LcVxg0TAAAAAE18vBiH00UAyaJggsmLm890SjZl'
  4. });
  5. Avatar.setOptions({
  6. fallbackType: "initials",
  7. defaultImageUrl: "http://static.boredpanda.com/blog/wp-content/uploads/2014/04/amazing-fox-photos-182.jpg",
  8. generateCSS: true,
  9. imageSizes: {
  10. 'header': 40
  11. }
  12. });
  13. });
  14. /* Global Helpers */
  15. Handlebars.registerHelper("isAdmin", function(argument){
  16. if (Meteor.user() && Meteor.user().profile) {
  17. return Meteor.user().profile.rank === "admin";
  18. } else {
  19. return false;
  20. }
  21. });
  22. Handlebars.registerHelper("isModerator", function(argument){
  23. if (Meteor.user() && Meteor.user().profile && (Meteor.user().profile.rank === "admin" || Meteor.user().profile.rank === "moderator")) {
  24. return true;
  25. } else {
  26. return false;
  27. }
  28. });
  29. Handlebars.registerHelper("initials", function(argument){
  30. var user = Meteor.user();
  31. if (user !== undefined) {
  32. return user.profile.username[0].toUpperCase();
  33. } else {
  34. return "";
  35. }
  36. });
  37. Deps.autorun(function() {
  38. Meteor.subscribe("queues");
  39. Meteor.subscribe("reports");
  40. Meteor.subscribe("chat");
  41. Meteor.subscribe("playlists");
  42. Meteor.subscribe("alerts");
  43. Meteor.subscribe("userData", Meteor.userId());
  44. });
  45. var ban_interval = Meteor.setInterval(function() {
  46. var userId = Meteor.userId();
  47. if (userId !== undefined) {
  48. var userData = Meteor.user();
  49. if (localStorage.getItem("banned") === "true") {
  50. if (userData !== undefined && userData !== null && userData.punishments !== undefined && userData.punishments.ban !== undefined) {
  51. var ban = userData.punishments.ban;
  52. if (new Date(ban.bannedUntil).getTime() <= new Date().getTime()) {
  53. Meteor.call("isBanned", function(err, res) {
  54. if (res === false) {
  55. localStorage.setItem("banned", false);
  56. Meteor._reload.reload();
  57. }
  58. });
  59. }
  60. } else {
  61. localStorage.setItem("banned", false);
  62. Meteor._reload.reload();
  63. }
  64. } else {
  65. if (userData !== undefined && userData !== null && userData.punishments !== undefined && userData.punishments.ban !== undefined) {
  66. localStorage.setItem("banned", true);
  67. Meteor._reload.reload();
  68. }
  69. }
  70. }
  71. }, 1000);
  72. var minterval;
  73. var hpSound = undefined;
  74. var songsArr = [];
  75. var ytArr = [];
  76. var _sound = undefined;
  77. var parts = location.href.split('/');
  78. var id = parts.pop();
  79. var type = id.toLowerCase();
  80. var resizeSeekerbarInterval;
  81. var station_c = undefined;
  82. var songMID;
  83. UI.registerHelper("formatTime", function(seconds) {
  84. var d = moment.duration(parseInt(seconds), 'seconds');
  85. return d.minutes() + ":" + ("0" + d.seconds()).slice(-2);
  86. });
  87. /*UI.registerHelper("formatTimeFromNow", function(time) {
  88. var d = moment(time);
  89. return d.fromNow();
  90. });*/
  91. function getSpotifyInfo(title, cb, artist) {
  92. var q = "";
  93. q = title;
  94. if (artist !== undefined) {
  95. q += " artist:" + artist;
  96. }
  97. $.ajax({
  98. type: "GET",
  99. url: 'https://api.spotify.com/v1/search?q=' + encodeURIComponent(q) + '&type=track',
  100. applicationType: "application/json",
  101. contentType: "json",
  102. success: function (data) {
  103. cb(data);
  104. }
  105. });
  106. }
  107. Template.settings.events({
  108. "click #save-settings": function() {
  109. Meteor.call("updateSettings", $("#showRating").is(":checked"));
  110. },
  111. "click #delete-account": function(){
  112. $("#delete-account").text("Click to confirm");
  113. $("#delete-account").click(function(){
  114. var bool = confirm("Are you sure you want to delete your account?");
  115. if(bool) {
  116. Meteor.call("deleteAccount", Meteor.userId());
  117. }
  118. })
  119. },
  120. "click #change-password": function(){
  121. var oldPassword = $("#old-password").val();
  122. var newPassword= $("#new-password").val();
  123. var confirmPassword = $("#confirm-password").val();
  124. if(newPassword === confirmPassword){
  125. Accounts.changePassword(oldPassword, newPassword, function(err){
  126. if(err){
  127. $("#old-password").val("");
  128. $("#new-password").val("");
  129. $("#confirm-password").val("");
  130. $("<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(); });
  131. } else {
  132. $("#old-password").val("");
  133. $("#new-password").val("");
  134. $("#confirm-password").val("");
  135. $("<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(); });
  136. }
  137. });
  138. }
  139. }
  140. });
  141. Template.profile.events({
  142. //Edit real name
  143. "click #edit-name": function(){
  144. $("#name").hide();
  145. $("#name-div").show();
  146. $("#edit-name").hide();
  147. $("#cancel-edit").show();
  148. },
  149. "click #submit-name": function(){
  150. var user = Meteor.user();
  151. $("#name").show();
  152. $("#name-div").hide();
  153. $("#edit-name").show();
  154. $("#cancel-edit").hide();
  155. var realname = $("#input-name").val();
  156. var username = user.profile.username;
  157. $("#name").text("Name: " + realname);
  158. $("#input-name").val("")
  159. Meteor.call("updateRealName", username, realname);
  160. },
  161. "click #cancel-edit": function(){
  162. $("#name").show();
  163. $("#name-div").hide();
  164. $("#edit-name").show();
  165. $("#cancel-edit").hide();
  166. $("#input-name").val("");
  167. },
  168. //Edit username
  169. "click #edit-username": function(){
  170. $("#username").hide();
  171. $("#username-div").show();
  172. $("#edit-username").hide();
  173. $("#cancel-username").show();
  174. },
  175. "click #submit-username": function(){
  176. var user = Meteor.user()
  177. $("#username").show();
  178. $("#username-div").hide();
  179. $("#edit-username").show();
  180. $("#cancel-username").hide();
  181. var username = user.username;
  182. var newUserName = $("#input-username").val();
  183. $("#profile-name").text(newUserName)
  184. $("#username").text("Username: " + newUserName);
  185. $("#input-username").val("")
  186. Meteor.call("updateUserName", username, newUserName);
  187. },
  188. "click #cancel-username": function(){
  189. $("#username").show();
  190. $("#username-div").hide();
  191. $("#edit-username").show();
  192. $("#cancel-username").hide();
  193. $("#input-username").val("");
  194. },
  195. // Admins only Edit Rank
  196. "click #edit-rank": function() {
  197. $("#rank").hide();
  198. $("#rank-div").show();
  199. $("#edit-rank").hide();
  200. $("#cancel-rank").show();
  201. },
  202. "click #submit-rank": function() {
  203. $("#rank").show();
  204. $("#rank-div").hide();
  205. $("#edit-rank").show();
  206. $("#cancel-rank").hide();
  207. var newRank = $("#select-rank option:selected").val();
  208. var username = Session.get("username");
  209. console.log(username, newRank);
  210. },
  211. "click #cancel-rank": function() {
  212. $("#rank").show();
  213. $("#rank-div").hide();
  214. $("#edit-rank").show();
  215. $("#cancel-rank").hide();
  216. }
  217. })
  218. Template.profile.helpers({
  219. "real_name": function(){
  220. return Session.get("real_name");
  221. },
  222. "username": function() {
  223. return Session.get("username")
  224. },
  225. "first_joined": function() {
  226. return moment(Session.get("first_joined")).format("DD/MM/YYYY HH:mm:ss");
  227. },
  228. "rank": function() {
  229. return Session.get("rank");
  230. },
  231. loaded: function() {
  232. return Session.get("loaded");
  233. },
  234. likedSongs: function(){
  235. var likedArr = [];
  236. Session.get("liked").forEach(function(mid){
  237. Rooms.find().forEach(function(room){
  238. Playlists.find({type: room.type}).forEach(function(pl){
  239. for(var i in pl.songs){
  240. if(pl.songs[i].mid === mid){
  241. likedArr.push({title: pl.songs[i].title, artist: pl.songs[i].artist, room: room.display});
  242. }
  243. }
  244. });
  245. })
  246. });
  247. return likedArr;
  248. },
  249. dislikedSongs: function(){
  250. var dislikedArr = [];
  251. Session.get("disliked").forEach(function(mid){
  252. Rooms.find().forEach(function(room){
  253. Playlists.find({type: room.type}).forEach(function(pl){
  254. for(var i in pl.songs){
  255. if(pl.songs[i].mid === mid){
  256. dislikedArr.push({title: pl.songs[i].title, artist: pl.songs[i].artist, room: room.display});
  257. }
  258. }
  259. });
  260. })
  261. });
  262. return dislikedArr;
  263. },
  264. isUser: function(){
  265. var parts = Router.current().url.split('/');
  266. var username = parts.pop();
  267. if(username === Meteor.user().profile.username){
  268. return true;
  269. }
  270. }
  271. });
  272. Template.profile.onCreated(function() {
  273. var parts = Router.current().url.split('/');
  274. var username = parts.pop();
  275. Session.set("loaded", false);
  276. Meteor.subscribe("userProfiles", username.toLowerCase(), function() {
  277. if (Meteor.users.find({"profile.usernameL": username.toLowerCase()}).count() === 0) {
  278. window.location = "/";
  279. } else {
  280. var data = Meteor.users.findOne({"profile.usernameL": username.toLowerCase()});
  281. Session.set("real_name", data.profile.realname);
  282. Session.set("username", data.profile.username);
  283. Session.set("first_joined", data.createdAt);
  284. Session.set("rank", data.profile.rank);
  285. Session.set("liked", data.profile.liked);
  286. Session.set("disliked", data.profile.disliked);
  287. Session.set("loaded", true);
  288. }
  289. });
  290. });
  291. Template.settings.helpers({
  292. username: function() {
  293. if (Meteor.user() !== undefined) {
  294. return Meteor.user().profile.username;
  295. } else {
  296. return "";
  297. }
  298. }
  299. });
  300. Template.settings.onCreated(function() {
  301. $(document).ready(function() {
  302. var user = Meteor.user();
  303. function initSettings() {
  304. if (user !== undefined) {
  305. if (user.profile.settings && user.profile.settings.showRating === true) {
  306. function setChecked() {
  307. $("#showRating").prop("checked", true);
  308. if (!$("#showRating").prop("checked")) {
  309. Meteor.setTimeout(function() {
  310. setChecked();
  311. }, 100);
  312. }
  313. }
  314. setChecked();
  315. }
  316. } else {
  317. Meteor.setTimeout(function() {
  318. initSettings();
  319. }, 500);
  320. }
  321. }
  322. initSettings();
  323. });
  324. });
  325. curPath=function(){var c=window.location.pathname;var b=c.slice(0,-1);var a=c.slice(-1);if(b==""){return"/"}else{if(a=="/"){return b}else{return c}}};
  326. Handlebars.registerHelper('active', function(path) {
  327. return curPath() == path ? 'active' : '';
  328. });
  329. Template.header.helpers({
  330. currentUser: function() {
  331. return Meteor.user();
  332. },
  333. userId: function() {
  334. return Meteor.userId();
  335. }
  336. });
  337. Template.header.events({
  338. "click .logout": function(e){
  339. e.preventDefault();
  340. Meteor.logout();
  341. if (hpSound !== undefined) {
  342. hpSound.stop();
  343. }
  344. }
  345. });
  346. Template.register.onCreated(function() {
  347. Accounts.onLoginFailure(function() {
  348. var errAlert = $('<div style="margin-bottom: 0" class="alert alert-danger" role="alert"><strong>Oh Snap!</strong> Something went wrong when trying to register with GitHub. Maybe an account with that username already exists?</div>');
  349. $(".landing").before(errAlert);
  350. Meteor.setTimeout(function() {
  351. errAlert.fadeOut(5000, function() {
  352. errAlert.remove();
  353. });
  354. }, 10000);
  355. });
  356. });
  357. Template.login.onCreated(function() {
  358. Session.set("github", true);
  359. Accounts.onLoginFailure(function() {
  360. if (Session.get("github") === true) {
  361. var errAlert = $('<div style="margin-bottom: 0" class="alert alert-danger" role="alert"><strong>Oh Snap!</strong> Something went wrong when trying to log in with GitHub.</div>');
  362. $(".landing").before(errAlert);
  363. Meteor.setTimeout(function() {
  364. errAlert.fadeOut(5000, function() {
  365. errAlert.remove();
  366. });
  367. }, 10000);
  368. }
  369. });
  370. });
  371. Template.register.events({
  372. "submit form": function(e){
  373. e.preventDefault();
  374. var username = e.target.registerUsername.value;
  375. var email = e.target.registerEmail.value;
  376. var password = e.target.registerPassword.value;
  377. var captchaData = grecaptcha.getResponse();
  378. Meteor.call("createUserMethod", {username: username, email: email, password: password}, captchaData, function(err, res) {
  379. grecaptcha.reset();
  380. if (err) {
  381. console.log(err);
  382. var errAlert = $('<div style="margin-bottom: 0" class="alert alert-danger" role="alert"><strong>Oh Snap!</strong> ' + err.reason + '</div>');
  383. $(".landing").before(errAlert);
  384. Meteor.setTimeout(function() {
  385. errAlert.fadeOut(5000, function() {
  386. errAlert.remove();
  387. });
  388. }, 5000);
  389. } else {
  390. Meteor.loginWithPassword(username, password);
  391. Accounts.onLogin(function(){
  392. window.location.href = "/";
  393. })
  394. }
  395. });
  396. },
  397. "click #github-login": function(){
  398. Meteor.loginWithGithub({loginStyle: "redirect"}, function(err, res) {
  399. console.log(err, res);
  400. });
  401. }
  402. });
  403. Template.login.events({
  404. "submit form": function(e){
  405. e.preventDefault();
  406. Session.set("github", false);
  407. var username = e.target.loginUsername.value;
  408. var password = e.target.loginPassword.value;
  409. Meteor.loginWithPassword(username, password, function(err) {
  410. if (err) {
  411. var errAlert = $('<div style="margin-bottom: 0" class="alert alert-danger" role="alert"><strong>Oh Snap!</strong> ' + err.reason + '</div>');
  412. $(".landing").before(errAlert);
  413. Meteor.setTimeout(function() {
  414. errAlert.fadeOut(5000, function() {
  415. errAlert.remove();
  416. });
  417. }, 5000);
  418. } else {
  419. window.location.href = "/";
  420. }
  421. });
  422. },
  423. "click #github-login": function(){
  424. Meteor.loginWithGithub({loginStyle: "redirect"}, function(err, res) {
  425. console.log(err, res);
  426. });
  427. }
  428. });
  429. Template.dashboard.helpers({
  430. rooms: function() {
  431. return Rooms.find({});
  432. },
  433. currentSong: function() {
  434. var type = this.type;
  435. var room = Rooms.findOne({type: type});
  436. if (room !== undefined) {
  437. return room.currentSong;
  438. } else {
  439. return {};
  440. }
  441. }
  442. });
  443. Template.dashboard.onCreated(function() {
  444. if (_sound !== undefined) _sound.stop();
  445. if (minterval !== undefined) {
  446. Meteor.clearInterval(minterval);
  447. }
  448. if (resizeSeekerbarInterval !== undefined) {
  449. Meteor.clearInterval(resizeSeekerbarInterval);
  450. resizeSeekerbarInterval = undefined;
  451. }
  452. if (station_c !== undefined) {
  453. station_c.stop();
  454. }
  455. Session.set("type", undefined);
  456. });
  457. function executeCommand(command, params){
  458. if (command === "help" || command === "commands") {
  459. $('#helpModal').modal('show');
  460. return true;
  461. } else if (command === "volume") {
  462. if (params.length === 1) {
  463. var volume = Number(params[0]);
  464. if (volume >= 0 || volume <= 100) {
  465. if (volume === 0) {
  466. $("#volume-icon").removeClass("fa-volume-down").addClass("fa-volume-off")
  467. } else {
  468. $("#volume-icon").removeClass("fa-volume-off").addClass("fa-volume-down")
  469. }
  470. $("#volume-slider").slider("setValue", volume);
  471. if (yt_player !== undefined) {
  472. yt_player.setVolume(volume);
  473. localStorage.setItem("volume", volume);
  474. } else if (_sound !== undefined) {
  475. //_sound
  476. var volume = volume / 100;
  477. _sound.setVolume(volume);
  478. localStorage.setItem("volume", volume * 100);
  479. }
  480. return true;
  481. }
  482. }
  483. } else if(command === "mute"){
  484. $("#volume-slider").slider("setValue", 0);
  485. $("#volume-icon").removeClass("fa-volume-down").addClass("fa-volume-off");
  486. if (yt_player !== undefined) {
  487. yt_player.setVolume(0);
  488. localStorage.setItem("volume", 0);
  489. } else if (_sound !== undefined) {
  490. //_sound
  491. _sound.setVolume(0);
  492. localStorage.setItem("volume", 0);
  493. }
  494. } else if(command === "ban"){
  495. var user = params[0];
  496. var time = params[1];
  497. var reason = params[2];
  498. Meteor.call("banUser", user, time, reason, function(err, res){
  499. if(err){
  500. console.log(err);
  501. }
  502. });
  503. } else if(command === "silence"){
  504. var user = params[0];
  505. var time = params[1];
  506. Meteor.call("muteUser", user, time, function(err, res){
  507. if(err){
  508. console.log(err);
  509. }
  510. });
  511. } else if(command === "unban"){
  512. var user = params[0];
  513. Meteor.call("unbanUser", user, function(err, res){
  514. if(err){
  515. console.log(err);
  516. }
  517. });
  518. } else if(command === "unsilence"){
  519. var user = params[0];
  520. Meteor.call("unsilenceUser", user, function(err, res){
  521. if(err){
  522. console.log(err);
  523. }
  524. });
  525. } else if(command === "pause"){
  526. Meteor.call("pauseRoom", Session.get("type"), function(err, res){
  527. if(err){
  528. console.log(err);
  529. }
  530. });
  531. } else if(command === "resume"){
  532. Meteor.call("resumeRoom", Session.get("type"), function(err, res){
  533. if(err){
  534. console.log(err);
  535. }
  536. });
  537. } else if(command === "shuffle"){
  538. Meteor.call("shufflePlaylist", Session.get("type"), function(err, res){
  539. if(err){
  540. console.log(err);
  541. }
  542. });
  543. } else if(command === "skip"){
  544. Meteor.call("skipSong", Session.get("type"), function(err, res){
  545. if(err){
  546. console.log(err);
  547. }
  548. });
  549. }
  550. }
  551. function sendMessage() {
  552. var message = $("#chat-input").val();
  553. if (!$("#chat-input").hasClass("disabled")) {
  554. if (message.length > 0 && message[0] !== " ") {
  555. if (message[0] === "/") {
  556. message = message.split("");
  557. message.shift();
  558. message = message.join("");
  559. var params = message.split(" ");
  560. params = params.map(function(param) {
  561. return param.replace(/\r?\n|\r/g, "");
  562. });
  563. var command = params.shift();
  564. command = command.replace(/\r?\n|\r/g, "");
  565. if (executeCommand(command, params)) {
  566. $("#chat-input").val("");
  567. } else {
  568. $("#chat-input").val("");
  569. }
  570. } else {
  571. $("#chat-input").addClass("disabled");
  572. $("#chat-input").attr("disabled", "");
  573. Meteor.call("sendMessage", Session.get("type"), message, function (err, res) {
  574. if (res) {
  575. $("#chat-input").val("");
  576. $("#chat-input").removeAttr("disabled");
  577. $("#chat-input").removeClass("disabled");
  578. }
  579. });
  580. }
  581. }
  582. }
  583. }
  584. function sendMessageGlobal() {
  585. var message = $("#global-chat-input").val();
  586. if (!$("#global-chat-input").hasClass("disabled")) {
  587. if (message.length > 0 && message[0] !== " ") {
  588. if (message[0] === "/") {
  589. message = message.split("");
  590. message.shift();
  591. message = message.join("");
  592. var params = message.split(" ");
  593. var command = params.shift();
  594. command = command.replace(/\r?\n|\r/g, "");
  595. if (executeCommand(command, params)) {
  596. $("#global-chat-input").val("");
  597. } else {
  598. $("#global-chat-input").val("");
  599. }
  600. } else {
  601. $("#global-chat-input").addClass("disabled");
  602. $("#global-chat-input").attr("disabled", "");
  603. Meteor.call("sendMessage", "global", message, function (err, res) {
  604. if (res) {
  605. $("#global-chat-input").val("");
  606. }
  607. $("#global-chat-input").removeClass("disabled");
  608. $("#global-chat-input").removeAttr("disabled");
  609. });
  610. }
  611. }
  612. }
  613. }
  614. Template.room.events({
  615. "click #chat-tab": function() {
  616. $("#chat-tab").removeClass("unread-messages");
  617. },
  618. "click #global-chat-tab": function() {
  619. $("#global-chat-tab").removeClass("unread-messages");
  620. },
  621. "click #sync": function() {
  622. if (Session.get("currentSong") !== undefined) {
  623. var room = Rooms.findOne({type: Session.get("type")});
  624. if (room !== undefined) {
  625. var timeIn = Date.now() - Session.get("currentSong").started - room.timePaused;
  626. var skipDuration = Number(Session.get("currentSong").skipDuration) | 0;
  627. if (yt_player !== undefined) {
  628. yt_player.seekTo(skipDuration + timeIn / 1000);
  629. }
  630. else if (_sound !== undefined) {
  631. _sound.seekTo(skipDuration * 1000 + timeIn);
  632. }
  633. }
  634. }
  635. },
  636. "click #lock": function() {
  637. Meteor.call("lockRoom", Session.get("type"));
  638. },
  639. "click #unlock": function() {
  640. Meteor.call("unlockRoom", Session.get("type"));
  641. },
  642. "click #side-panel": function(e) {
  643. Meteor.setTimeout(function() {
  644. var elem = document.getElementById('chat');
  645. var elem1 = document.getElementById('global-chat');
  646. elem.scrollTop = elem.scrollHeight;
  647. elem1.scrollTop = elem1.scrollHeight;
  648. }, 1);
  649. },
  650. "click #submit": function() {
  651. sendMessage();
  652. },
  653. "click #global-submit": function() {
  654. sendMessageGlobal();
  655. },
  656. "keyup #chat-input": function(e) {
  657. if (e.type === "keyup" && e.which === 13) {
  658. e.preventDefault();
  659. if (!$('#chat-input').data('dropdownshown')) {
  660. sendMessage();
  661. }
  662. }
  663. },
  664. "keyup #global-chat-input": function(e) {
  665. if (e.type === "keyup" && e.which === 13) {
  666. e.preventDefault();
  667. if (!$('#global-chat-input').data('dropdownshown')) {
  668. sendMessageGlobal();
  669. }
  670. }
  671. },
  672. "click #like": function(e) {
  673. $("#like").blur();
  674. Meteor.call("likeSong", Session.get("currentSong").mid);
  675. },
  676. "click #dislike": function(e) {
  677. $("#dislike").blur();
  678. Meteor.call("dislikeSong", Session.get("currentSong").mid);
  679. },
  680. "click #vote-skip": function(){
  681. Meteor.call("voteSkip", type, function(err, res) {
  682. $("#vote-skip").attr("disabled", true);
  683. });
  684. songMID = Session.get("currentSong").mid;
  685. },
  686. "click #report-prev": function(e) {
  687. if (Session.get("previousSong") !== undefined) {
  688. Session.set("reportPrevious", true);
  689. $("#report-prev").prop("disabled", true);
  690. $("#report-curr").prop("disabled", false);
  691. }
  692. },
  693. "click #report-curr": function(e) {
  694. Session.set("reportPrevious", false);
  695. $("#report-prev").prop("disabled", false);
  696. $("#report-curr").prop("disabled", true);
  697. },
  698. "click #report-modal": function() {
  699. Session.set("currentSongR", Session.get("currentSong"));
  700. Session.set("previousSongR", Session.get("previousSong"));
  701. },
  702. "click #add-song-button": function(e){
  703. e.preventDefault();
  704. parts = location.href.split('/');
  705. var roomType = parts.pop();
  706. var genre = roomType.toLowerCase();
  707. var type = $("#type").val();
  708. id = $("#id").val();
  709. var title = $("#title").val();
  710. var artist = $("#artist").val();
  711. var img = $("#img").val();
  712. var songData = {type: type, id: id, title: title, artist: artist, img: img};
  713. if(Playlists.find({type: genre, "songs.id": songData.id}, {songs: {$elemMatch: {id: songData.id}}}).count() !== 0) {
  714. $("<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() { $(this).remove(); });
  715. } else if(Queues.find({type: genre, "songs.id": songData.id}, {songs: {$elemMatch: {id: songData.id}}}).count() !== 0) {
  716. $("<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() { $(this).remove(); });
  717. } else{
  718. Meteor.call("addSongToQueue", genre, songData, function(err, res) {
  719. console.log(err, res);
  720. if (err) {
  721. $("<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() { $(this).remove(); });
  722. } else {
  723. $("<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() { $(this).remove(); });
  724. }
  725. });
  726. }
  727. $("#close-modal-a").click();
  728. },
  729. "click #toggle-video": function(e){
  730. e.preventDefault();
  731. if (Session.get("mediaHidden")) {
  732. $("#media-container").removeClass("hidden");
  733. $("#toggle-video").text("Hide video");
  734. Session.set("mediaHidden", false);
  735. } else {
  736. $("#media-container").addClass("hidden");
  737. $("#toggle-video").text("Show video");
  738. Session.set("mediaHidden", true);
  739. }
  740. },
  741. "click #return": function(e){
  742. $("#add-info").hide();
  743. $("#search-info").show();
  744. },
  745. "click #search-song": function(){
  746. $("#song-results").empty();
  747. var search_type = $("#search_type").val();
  748. if (search_type === "YouTube") {
  749. $.ajax({
  750. type: "GET",
  751. url: "https://www.googleapis.com/youtube/v3/search?part=snippet&q=" + $("#song-input").val() + "&key=AIzaSyAgBdacEWrHCHVPPM4k-AFM7uXg-Q__YXY",
  752. applicationType: "application/json",
  753. contentType: "json",
  754. success: function(data){
  755. for(var i in data.items){
  756. $("#song-results").append("<p>" + data.items[i].snippet.title + "</p>");
  757. ytArr.push({title: data.items[i].snippet.title, id: data.items[i].id.videoId});
  758. }
  759. $("#song-results p").click(function(){
  760. $("#search-info").hide();
  761. $("#add-info").show();
  762. var title = $(this).text();
  763. for(var i in ytArr){
  764. if(ytArr[i].title === title){
  765. var songObj = {
  766. id: ytArr[i].id,
  767. title: ytArr[i].title,
  768. type: "youtube"
  769. };
  770. $("#title").val(songObj.title);
  771. $("#artist").val("");
  772. $("#id").val(songObj.id);
  773. $("#type").val("YouTube");
  774. getSpotifyInfo(songObj.title.replace(/\[.*\]/g, ""), function(data) {
  775. if (data.tracks.items.length > 0) {
  776. $("#title").val(data.tracks.items[0].name);
  777. var artists = [];
  778. $("#img").val(data.tracks.items[0].album.images[1].url);
  779. data.tracks.items[0].artists.forEach(function(artist) {
  780. artists.push(artist.name);
  781. });
  782. $("#artist").val(artists.join(", "));
  783. }
  784. });
  785. }
  786. }
  787. })
  788. }
  789. })
  790. } else if (search_type === "SoundCloud") {
  791. SC.get('/tracks', { q: $("#song-input").val()}, function(tracks) {
  792. for(var i in tracks){
  793. $("#song-results").append("<p>" + tracks[i].title + "</p>")
  794. songsArr.push({title: tracks[i].title, id: tracks[i].id, duration: tracks[i].duration / 1000});
  795. }
  796. $("#song-results p").click(function(){
  797. $("#search-info").hide();
  798. $("#add-info").show();
  799. var title = $(this).text();
  800. for(var i in songsArr){
  801. if(songsArr[i].title === title){
  802. var id = songsArr[i].id;
  803. var duration = songsArr[i].duration;
  804. var songObj = {
  805. title: songsArr[i].title,
  806. id: id,
  807. duration: duration,
  808. type: "soundcloud"
  809. }
  810. $("#title").val(songObj.title);
  811. // Set ID field
  812. $("#id").val(songObj.id);
  813. $("#type").val("SoundCloud");
  814. getSpotifyInfo(songObj.title.replace(/\[.*\]/g, ""), function(data) {
  815. if (data.tracks.items.length > 0) {
  816. $("#title").val(data.tracks.items[0].name);
  817. var artists = [];
  818. data.tracks.items[0].artists.forEach(function(artist) {
  819. artists.push(artist.name);
  820. });
  821. $("#artist").val(artists.join(", "));
  822. }
  823. // Set title field again if possible
  824. // Set artist if possible
  825. });
  826. }
  827. }
  828. })
  829. });
  830. }
  831. },
  832. "click #close-modal-a": function(){
  833. $("#search-info").show();
  834. $("#add-info").hide();
  835. },
  836. "click #volume-icon": function(){
  837. var volume = 0;
  838. var slider = $("#volume-slider").slider();
  839. $("#volume-icon").removeClass("fa-volume-down").addClass("fa-volume-off")
  840. if (yt_player !== undefined) {
  841. yt_player.setVolume(volume);
  842. localStorage.setItem("volume", volume);
  843. $("#volume-slider").slider("setValue", volume);
  844. } else if (_sound !== undefined) {
  845. _sound.setVolume(volume);
  846. localStorage.setItem("volume", volume);
  847. $("#volume-slider").slider("setValue", volume);
  848. }
  849. },
  850. "click #play": function() {
  851. Meteor.call("resumeRoom", type);
  852. },
  853. "click #pause": function() {
  854. Meteor.call("pauseRoom", type);
  855. },
  856. "click #skip": function() {
  857. Meteor.call("skipSong", type);
  858. },
  859. "click #shuffle": function() {
  860. Meteor.call("shufflePlaylist", type);
  861. },
  862. "change input": function(e) {
  863. if (e.target && e.target.id) {
  864. var partsOfId = e.target.id.split("-");
  865. partsOfId[1] = partsOfId[1].charAt(0).toUpperCase() + partsOfId[1].slice(1);
  866. var camelCase = partsOfId.join("");
  867. Session.set(camelCase, e.target.checked);
  868. }
  869. },
  870. "click #report-song-button": function() {
  871. var room = Session.get("type");
  872. var reportData = {};
  873. reportData.song = Session.get("currentSong").mid;
  874. reportData.type = [];
  875. reportData.reason = [];
  876. $(".report-layer-1 > .checkbox input:checked").each(function(){
  877. reportData.type.push(this.id);
  878. if (this.id == "report-other") {
  879. var otherText = $(".other-textarea").val();
  880. }
  881. });
  882. $(".report-layer-2 input:checked").each(function(){
  883. reportData.reason.push(this.id);
  884. });
  885. console.log(reportData);
  886. Meteor.call("submitReport", room, reportData, Session.get("id"), function() {
  887. $("#close-modal-r").click();
  888. });
  889. }
  890. });
  891. Template.banned.helpers({
  892. bannedAt: function() {
  893. if (Session.get("ban") !== undefined) {
  894. return Session.get("ban").bannedAt;
  895. }
  896. },
  897. bannedBy: function() {
  898. if (Session.get("ban") !== undefined) {
  899. return Session.get("ban").bannedBy;
  900. }
  901. },
  902. bannedUntil: function() {
  903. if (Session.get("ban") !== undefined) {
  904. return Session.get("ban").bannedUntil;
  905. }
  906. },
  907. bannedReason: function() {
  908. if (Session.get("ban") !== undefined) {
  909. return Session.get("ban").bannedReason;
  910. }
  911. }
  912. });
  913. Template.banned.onCreated(function() {
  914. if (rTimeInterval !== undefined) {
  915. Meteor.clearInterval(rTimeInterval)
  916. }
  917. rTimeInterval = Meteor.setInterval(function() {
  918. Session.set("time", new Date().getTime());
  919. }, 10000);
  920. Session.set("ban", Meteor.user().punishments.ban);
  921. });
  922. Template.registerHelper("rtime", function(date) {
  923. Session.get("time");
  924. if (date) {
  925. return moment(date).fromNow();
  926. }
  927. });
  928. var rTimeInterval = undefined;
  929. Template.room.onRendered(function() {
  930. if (rTimeInterval !== undefined) {
  931. Meteor.clearInterval(rTimeInterval)
  932. }
  933. rTimeInterval = Meteor.setInterval(function() {
  934. Session.set("time", new Date().getTime());
  935. }, 10000);
  936. $(document).ready(function() {
  937. function makeSlider(){
  938. var slider = $("#volume-slider").slider();
  939. var volume = Number(localStorage.getItem("volume"));
  940. $("#volume-slider").slider("setValue", volume);
  941. if (slider.length === 0) {
  942. Meteor.setTimeout(function() {
  943. makeSlider();
  944. }, 500);
  945. } else {
  946. if (volume === 0) {
  947. $("#volume-icon").removeClass("fa-volume-down").addClass("fa-volume-off")
  948. } else {
  949. $("#volume-icon").removeClass("fa-volume-off").addClass("fa-volume-down")
  950. }
  951. slider.on("slide", function(val) {
  952. if (val.value === 0) {
  953. $("#volume-icon").removeClass("fa-volume-down").addClass("fa-volume-off")
  954. } else {
  955. $("#volume-icon").removeClass("fa-volume-off").addClass("fa-volume-down")
  956. }
  957. if (yt_player !== undefined) {
  958. yt_player.setVolume(val.value);
  959. localStorage.setItem("volume", val.value);
  960. } else if (_sound !== undefined) {
  961. //_sound
  962. var volume = val.value / 100;
  963. _sound.setVolume(volume);
  964. localStorage.setItem("volume", val.value);
  965. }
  966. });
  967. }
  968. }
  969. makeSlider();
  970. });
  971. });
  972. Template.alerts.helpers({
  973. alerts: function() {
  974. return Alerts.find({active: true});
  975. }
  976. });
  977. Template.room.helpers({
  978. chat: function() {
  979. Meteor.setTimeout(function() {
  980. var elem = document.getElementById('chat');
  981. if (elem !== undefined && elem !== null) {
  982. elem.scrollTop = elem.scrollHeight;
  983. }
  984. }, 100);
  985. return Chat.find({type: Session.get("type")}, {sort: {time: -1}, limit: 50 }).fetch().reverse();
  986. },
  987. globalChat: function() {
  988. Meteor.setTimeout(function() {
  989. var elem = document.getElementById('global-chat');
  990. if (elem !== undefined && elem !== null) {
  991. elem.scrollTop = elem.scrollHeight;
  992. }
  993. }, 100);
  994. return Chat.find({type: "global"}, {sort: {time: -1}, limit: 50 }).fetch().reverse();
  995. },
  996. likes: function() {
  997. var playlist = Playlists.findOne({type: Session.get("type")});
  998. var likes = 0;
  999. playlist.songs.forEach(function(song) {
  1000. if (Session.get("currentSong") && song.mid === Session.get("currentSong").mid) {
  1001. likes = song.likes;
  1002. return;
  1003. }
  1004. });
  1005. return likes;
  1006. },
  1007. dislikes: function() {
  1008. var playlist = Playlists.findOne({type: Session.get("type")});
  1009. var dislikes = 0;
  1010. playlist.songs.forEach(function(song) {
  1011. if (Session.get("currentSong") && song.mid === Session.get("currentSong").mid) {
  1012. dislikes = song.dislikes;
  1013. return;
  1014. }
  1015. });
  1016. return dislikes;
  1017. },
  1018. liked: function() {
  1019. if (Meteor.userId()) {
  1020. var currentSong = Session.get("currentSong");
  1021. if (currentSong && Meteor.user().profile.liked.indexOf(currentSong.mid) !== -1) {
  1022. return "active";
  1023. } else {
  1024. return "";
  1025. }
  1026. } else {
  1027. "";
  1028. }
  1029. },
  1030. disliked: function() {
  1031. if (Meteor.userId()) {
  1032. var currentSong = Session.get("currentSong");
  1033. if (currentSong && Meteor.user().profile.disliked.indexOf(currentSong.mid) !== -1) {
  1034. return "active";
  1035. } else {
  1036. return "";
  1037. }
  1038. } else {
  1039. "";
  1040. }
  1041. },
  1042. type: function() {
  1043. var parts = location.href.split('/');
  1044. var id = parts.pop().toLowerCase();
  1045. return Rooms.findOne({type: id}).display;
  1046. },
  1047. users: function() {
  1048. var parts = location.href.split('/');
  1049. var id = parts.pop().toLowerCase();
  1050. return Rooms.findOne({type: id}).users;
  1051. },
  1052. title: function(){
  1053. return Session.get("title");
  1054. },
  1055. artist: function(){
  1056. return Session.get("artist");
  1057. },
  1058. loaded: function() {
  1059. return Session.get("loaded");
  1060. },
  1061. paused: function() {
  1062. return Session.get("state") === "paused";
  1063. },
  1064. private: function() {
  1065. return Rooms.findOne({type: Session.get("type")}).private === true;
  1066. },
  1067. report: function() {
  1068. return Session.get("reportObj");
  1069. },
  1070. reportSong: function() {
  1071. return Session.get("reportSong");
  1072. },
  1073. reportTitle: function() {
  1074. return Session.get("reportTitle");
  1075. },
  1076. reportAuthor: function() {
  1077. return Session.get("reportAuthor");
  1078. },
  1079. reportDuration: function() {
  1080. return Session.get("reportDuration");
  1081. },
  1082. reportAudio: function() {
  1083. return Session.get("reportAudio");
  1084. },
  1085. reportAlbumart: function() {
  1086. return Session.get("reportAlbumart");
  1087. },
  1088. reportOther: function() {
  1089. return Session.get("reportOther");
  1090. },
  1091. currentSong: function() {
  1092. return Session.get("currentSong");
  1093. },
  1094. previousSong: function() {
  1095. return Session.get("previousSong");
  1096. },
  1097. currentSongR: function() {
  1098. return Session.get("currentSongR");
  1099. },
  1100. previousSongR: function() {
  1101. return Session.get("previousSongR");
  1102. },
  1103. reportingSong: function() {
  1104. if (Session.get("reportPrevious")) {
  1105. return Session.get("previousSongR");
  1106. } else {
  1107. return Session.get("currentSongR");
  1108. }
  1109. },
  1110. votes: function(){
  1111. return Rooms.findOne({type: Session.get("type")}).votes;
  1112. }
  1113. });
  1114. var allAlertSub = undefined;
  1115. Template.alertsDashboard.onCreated(function() {
  1116. if (allAlertSub === undefined) {
  1117. allAlertSub = Meteor.subscribe("allAlerts");
  1118. }
  1119. });
  1120. Template.alertsDashboard.helpers({
  1121. "activeAlerts": function() {
  1122. return Alerts.find({active: true});
  1123. },
  1124. "inactiveAlerts": function() {
  1125. return Alerts.find({active: false});
  1126. }
  1127. });
  1128. Template.alertsDashboard.events({
  1129. "click #calart-create": function() {
  1130. Meteor.call("addAlert", $("#calert-description").val(), $("#calert-priority").val().toLowerCase(), function (err, res) {
  1131. if (err) {
  1132. alert("Error " + err.error + ": " + err.reason);
  1133. } else {
  1134. $("#calert-description").val("");
  1135. }
  1136. });
  1137. },
  1138. "click #ralert-button": function() {
  1139. Meteor.call("removeAlerts");
  1140. }
  1141. });
  1142. Template.admin.helpers({
  1143. queueCount: function(display) {
  1144. display = display.toLowerCase();
  1145. var queues = Queues.findOne({type:display});
  1146. return queues && "songs" in queues ? queues.songs.length : 0;
  1147. },
  1148. queues: function() {
  1149. var queues = Queues.find({}).fetch();
  1150. return queues;
  1151. },
  1152. usersOnline: function(){
  1153. Meteor.call("getUserNum", function(err, num){
  1154. if(err){
  1155. console.log(err);
  1156. }
  1157. Session.set("userNum", num);
  1158. });
  1159. return Session.get("userNum");
  1160. },
  1161. allUsers: function(){
  1162. Meteor.call("getTotalUsers", function(err, num){
  1163. Session.set("allUsers", num);
  1164. })
  1165. return Session.get("allUsers");
  1166. },
  1167. playlists: function() {
  1168. var playlists = Playlists.find({}).fetch();
  1169. playlists.map(function(playlist) {
  1170. if (Rooms.find({type: playlist.type}).count() !== 1) {
  1171. return;
  1172. } else {
  1173. playlist.display = Rooms.findOne({type: playlist.type}).display;
  1174. return playlist;
  1175. }
  1176. });
  1177. return playlists;
  1178. },
  1179. reportsCount: function(room) {
  1180. room = room.toLowerCase();
  1181. var reports = Reports.findOne({room:room});
  1182. return reports && "report" in reports ? reports.report.length : 0;
  1183. }
  1184. });
  1185. Template.admin.events({
  1186. "click #croom_create": function() {
  1187. Meteor.call("createRoom", $("#croom_display").val(), $("#croom_tag").val(), function (err, res) {
  1188. if (err) {
  1189. alert("Error " + err.error + ": " + err.reason);
  1190. } else {
  1191. window.location = "/" + $("#croom_tag").val();
  1192. }
  1193. });
  1194. },
  1195. "click a": function(e){
  1196. var id = e.currentTarget.id;
  1197. console.log(id.toLowerCase());
  1198. Session.set("playlistToEdit", id);
  1199. },
  1200. "click #croom_create": function() {
  1201. Meteor.call("createRoom", $("#croom_display").val(), $("#croom_tag").val(), $("#two").prop("checked"), function (err, res) {
  1202. if (err) {
  1203. alert("Error " + err.error + ": " + err.reason);
  1204. } else {
  1205. window.location = "/" + $("#croom_tag").val();
  1206. }
  1207. });
  1208. },
  1209. "click #rreset_confirm": function(){
  1210. $('#confirmModal').modal('hide');
  1211. Meteor.call("resetRating");
  1212. }
  1213. });
  1214. Template.stations.helpers({
  1215. playlist: function() {
  1216. var query = {type: Session.get("playlistToEdit").toLowerCase()};
  1217. var playlists = Playlists.find(query).fetch();
  1218. console.log(Session.get("playlistToEdit"), query, playlists);
  1219. return playlists;
  1220. },
  1221. whichStation: function(){
  1222. return Session.get("playlistToEdit");
  1223. }
  1224. });
  1225. Template.queues.helpers({
  1226. queues: function() {
  1227. var queues = Queues.find({}).fetch();
  1228. queues.map(function(queue) {
  1229. if (Rooms.find({type: queue.type}).count() !== 1) {
  1230. return;
  1231. } else {
  1232. queue.display = Rooms.findOne({type: queue.type}).display;
  1233. return queue;
  1234. }
  1235. });
  1236. return queues;
  1237. }
  1238. })
  1239. var yt_player = undefined;
  1240. var _sound = undefined;
  1241. var previewEndSongTimeout = undefined;
  1242. Template.stations.events({
  1243. "click .preview-button": function(e){
  1244. Session.set("song", this);
  1245. },
  1246. "click #previewImageButton": function() {
  1247. $("#preview-image").attr("src", Session.get("song").img);
  1248. },
  1249. "click .edit-queue-button": function(e){
  1250. Session.set("song", this);
  1251. Session.set("genre", $(e.target).data("genre"));
  1252. Session.set("type", "queue");
  1253. $("#type").val(this.type);
  1254. $("#mid").val(this.mid);
  1255. $("#artist").val(this.artist);
  1256. $("#title").val(this.title);
  1257. $("#img").val(this.img);
  1258. $("#id").val(this.id);
  1259. $("#likes").val(this.likes);
  1260. $("#dislikes").val(this.dislikes);
  1261. $("#duration").val(this.duration);
  1262. $("#skip-duration").val(this.skipDuration);
  1263. },
  1264. "click .edit-playlist-button": function(e){
  1265. Session.set("song", this);
  1266. Session.set("genre", $(e.target).data("genre"));
  1267. Session.set("type", "playlist");
  1268. $("#type").val(this.type);
  1269. $("#mid").val(this.mid);
  1270. $("#artist").val(this.artist);
  1271. $("#title").val(this.title);
  1272. $("#img").val(this.img);
  1273. $("#id").val(this.id);
  1274. $("#likes").val(this.likes);
  1275. $("#dislikes").val(this.dislikes);
  1276. $("#duration").val(this.duration);
  1277. $("#skip-duration").val(this.skipDuration);
  1278. },
  1279. "click .add-song-button": function(e){
  1280. var genre = $(e.target).data("genre") || $(e.target).parent().data("genre");
  1281. Meteor.call("addSongToPlaylist", genre, this);
  1282. },
  1283. "click .deny-song-button": function(e){
  1284. var genre = $(e.target).data("genre") || $(e.target).parent().data("genre");
  1285. Meteor.call("removeSongFromQueue", genre, this.mid);
  1286. },
  1287. "click .remove-song-button": function(e){
  1288. var genre = $(e.target).data("genre") || $(e.target).parent().data("genre");
  1289. Meteor.call("removeSongFromPlaylist", genre, this.mid);
  1290. },
  1291. "click #moveSong": function(e){
  1292. var genre = $(e.target).data("genre") || $(e.target).parent().data("genre");
  1293. if (genre !== Session.get(genre)) {
  1294. 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});
  1295. Meteor.call("removeSongFromPlaylist", Session.get("genre"), Session.get("song").mid);
  1296. }else {
  1297. console.log("Something Went Wrong?!");
  1298. return false;
  1299. }
  1300. },
  1301. "click #copySong": function(e){
  1302. var genre = $(e.target).data("genre") || $(e.target).parent().data("genre");
  1303. 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});
  1304. },
  1305. "click .copyMove-button": function(e){
  1306. Session.set("song", this);
  1307. Session.set("genre", $(e.target).data("genre"));
  1308. },
  1309. "click #play": function() {
  1310. $("#play").attr("disabled", true);
  1311. $("#stop").attr("disabled", false);
  1312. var song = Session.get("song");
  1313. var id = song.id;
  1314. var type = song.type;
  1315. var volume = localStorage.getItem("volume") || 20;
  1316. if (type === "YouTube") {
  1317. if (yt_player === undefined) {
  1318. yt_player = new YT.Player("previewPlayer", {
  1319. height: 540,
  1320. width: 568,
  1321. videoId: id,
  1322. playerVars: {controls: 0, iv_load_policy: 3, showinfo: 0},
  1323. events: {
  1324. 'onReady': function(event) {
  1325. event.target.seekTo(Number(song.skipDuration));
  1326. event.target.playVideo();
  1327. event.target.setVolume(volume);
  1328. },
  1329. 'onStateChange': function(event){
  1330. if (event.data == YT.PlayerState.PAUSED) {
  1331. event.target.playVideo();
  1332. }
  1333. if (event.data == YT.PlayerState.PLAYING) {
  1334. $("#play").attr("disabled", true);
  1335. $("#stop").attr("disabled", false);
  1336. } else {
  1337. $("#play").attr("disabled", false);
  1338. $("#stop").attr("disabled", true);
  1339. }
  1340. }
  1341. }
  1342. });
  1343. } else {
  1344. yt_player.loadVideoById(id);
  1345. yt_player.seekTo(Number(song.skipDuration));
  1346. }
  1347. $("#previewPlayer").show();
  1348. } else if (type === "SoundCloud") {
  1349. SC.stream("/tracks/" + song.id, function(sound) {
  1350. _sound = sound;
  1351. sound.setVolume(volume / 100);
  1352. sound.play();
  1353. });
  1354. }
  1355. if (previewEndSongTimeout !== undefined) {
  1356. Meteor.clearTimeout(previewEndSongTimeout);
  1357. }
  1358. previewEndSongTimeout = Meteor.setTimeout(function() {
  1359. if (yt_player !== undefined) {
  1360. yt_player.stopVideo();
  1361. }
  1362. if (_sound !== undefined) {
  1363. _sound.stop();
  1364. }
  1365. $("#play").attr("disabled", false);
  1366. $("#stop").attr("disabled", true);
  1367. $("#previewPlayer").hide();
  1368. }, song.duration * 1000);
  1369. },
  1370. "click #stop": function() {
  1371. $("#play").attr("disabled", false);
  1372. $("#stop").attr("disabled", true);
  1373. if (previewEndSongTimeout !== undefined) {
  1374. Meteor.clearTimeout(previewEndSongTimeout);
  1375. }
  1376. if (yt_player !== undefined) {
  1377. yt_player.stopVideo();
  1378. }
  1379. if (_sound !== undefined) {
  1380. _sound.stop();
  1381. }
  1382. },
  1383. "click #forward": function() {
  1384. var error = false;
  1385. if (yt_player !== undefined) {
  1386. var duration = Number(Session.get("song").duration) | 0;
  1387. var skipDuration = Number(Session.get("song").skipDuration) | 0;
  1388. if (yt_player.getDuration() < duration + skipDuration) {
  1389. alert("The duration of the YouTube video is smaller than the duration.");
  1390. error = true;
  1391. } else {
  1392. yt_player.seekTo(skipDuration + duration - 10);
  1393. }
  1394. }
  1395. if (_sound !== undefined) {
  1396. _sound.seekTo((skipDuration + duration - 10) * 1000);
  1397. }
  1398. if (!error) {
  1399. if (previewEndSongTimeout !== undefined) {
  1400. Meteor.clearTimeout(previewEndSongTimeout);
  1401. }
  1402. previewEndSongTimeout = Meteor.setTimeout(function() {
  1403. if (yt_player !== undefined) {
  1404. yt_player.stopVideo();
  1405. }
  1406. if (_sound !== undefined) {
  1407. _sound.stop();
  1408. }
  1409. $("#play").attr("disabled", false);
  1410. $("#stop").attr("disabled", true);
  1411. $("#previewPlayer").hide();
  1412. }, 10000);
  1413. }
  1414. },
  1415. "click #get-spotify-info": function() {
  1416. var search = $("#title").val();
  1417. var artistName = $("#artist").val();
  1418. getSpotifyInfo(search, function(data) {
  1419. for(var i in data){
  1420. for(var j in data[i].items){
  1421. if(search.indexOf(data[i].items[j].name) !== -1 && artistName.indexOf(data[i].items[j].artists[0].name) !== -1){
  1422. $("#img").val(data[i].items[j].album.images[1].url);
  1423. $("#duration").val(data[i].items[j].duration_ms / 1000);
  1424. return;
  1425. }
  1426. }
  1427. }
  1428. }, artistName);
  1429. },
  1430. "click #save-song-button": function() {
  1431. var newSong = {};
  1432. newSong.id = $("#id").val();
  1433. newSong.likes = Number($("#likes").val());
  1434. newSong.dislikes = Number($("#dislikes").val());
  1435. newSong.title = $("#title").val();
  1436. newSong.artist = $("#artist").val();
  1437. newSong.img = $("#img").val();
  1438. newSong.type = $("#type").val();
  1439. newSong.duration = Number($("#duration").val());
  1440. newSong.skipDuration = $("#skip-duration").val();
  1441. if(newSong.skipDuration === undefined){
  1442. newSong.skipDuration = 0;
  1443. };
  1444. if (Session.get("type") === "playlist") {
  1445. Meteor.call("updatePlaylistSong", Session.get("genre"), Session.get("song"), newSong, function() {
  1446. $('#editModal').modal('hide');
  1447. });
  1448. } else {
  1449. Meteor.call("updateQueueSong", Session.get("genre"), Session.get("song"), newSong, function() {
  1450. $('#editModal').modal('hide');
  1451. });
  1452. }
  1453. },
  1454. "click .delete-room": function(){
  1455. var typeDel = $(this)[0].type;
  1456. Meteor.call("deleteRoom", typeDel);
  1457. }
  1458. });
  1459. Template.queues.events({
  1460. "click .preview-button": function(e){
  1461. Session.set("song", this);
  1462. },
  1463. "click #previewImageButton": function() {
  1464. $("#preview-image").attr("src", Session.get("song").img);
  1465. },
  1466. "click .edit-queue-button": function(e){
  1467. Session.set("song", this);
  1468. Session.set("genre", $(e.target).data("genre"));
  1469. Session.set("type", "queue");
  1470. $("#type").val(this.type);
  1471. $("#mid").val(this.mid);
  1472. $("#artist").val(this.artist);
  1473. $("#title").val(this.title);
  1474. $("#img").val(this.img);
  1475. $("#id").val(this.id);
  1476. $("#likes").val(this.likes);
  1477. $("#dislikes").val(this.dislikes);
  1478. $("#duration").val(this.duration);
  1479. $("#skip-duration").val(this.skipDuration);
  1480. },
  1481. "click .add-song-button": function(e){
  1482. var genre = $(e.target).data("genre") || $(e.target).parent().data("genre");
  1483. Meteor.call("addSongToPlaylist", genre, this);
  1484. },
  1485. "click .deny-song-button": function(e){
  1486. var genre = $(e.target).data("genre") || $(e.target).parent().data("genre");
  1487. Meteor.call("removeSongFromQueue", genre, this.mid);
  1488. },
  1489. "click #play": function() {
  1490. $("#play").attr("disabled", true);
  1491. $("#stop").attr("disabled", false);
  1492. var song = Session.get("song");
  1493. var id = song.id;
  1494. var type = song.type;
  1495. var volume = localStorage.getItem("volume") || 20;
  1496. if (type === "YouTube") {
  1497. if (yt_player === undefined) {
  1498. yt_player = new YT.Player("previewPlayer", {
  1499. height: 540,
  1500. width: 568,
  1501. videoId: id,
  1502. playerVars: {autoplay: 1, controls: 0, iv_load_policy: 3, showinfo: 0},
  1503. events: {
  1504. 'onReady': function(event) {
  1505. event.target.seekTo(Number(song.skipDuration));
  1506. event.target.playVideo();
  1507. event.target.setVolume(volume);
  1508. },
  1509. 'onStateChange': function(event){
  1510. if (event.data == YT.PlayerState.PAUSED) {
  1511. event.target.playVideo();
  1512. }
  1513. if (event.data == YT.PlayerState.PLAYING) {
  1514. $("#play").attr("disabled", true);
  1515. $("#stop").attr("disabled", false);
  1516. } else {
  1517. $("#play").attr("disabled", false);
  1518. $("#stop").attr("disabled", true);
  1519. }
  1520. }
  1521. }
  1522. });
  1523. } else {
  1524. yt_player.loadVideoById(id);
  1525. yt_player.seekTo(Number(song.skipDuration));
  1526. }
  1527. $("#previewPlayer").show();
  1528. } else if (type === "SoundCloud") {
  1529. SC.stream("/tracks/" + song.id, function(sound) {
  1530. _sound = sound;
  1531. sound.setVolume(volume / 100);
  1532. sound.play();
  1533. });
  1534. }
  1535. if (previewEndSongTimeout !== undefined) {
  1536. Meteor.clearTimeout(previewEndSongTimeout);
  1537. }
  1538. previewEndSongTimeout = Meteor.setTimeout(function() {
  1539. if (yt_player !== undefined) {
  1540. yt_player.stopVideo();
  1541. }
  1542. if (_sound !== undefined) {
  1543. _sound.stop();
  1544. }
  1545. $("#play").attr("disabled", false);
  1546. $("#stop").attr("disabled", true);
  1547. $("#previewPlayer").hide();
  1548. }, song.duration * 1000);
  1549. },
  1550. "click #stop": function() {
  1551. $("#play").attr("disabled", false);
  1552. $("#stop").attr("disabled", true);
  1553. if (previewEndSongTimeout !== undefined) {
  1554. Meteor.clearTimeout(previewEndSongTimeout);
  1555. }
  1556. if (yt_player !== undefined) {
  1557. yt_player.stopVideo();
  1558. }
  1559. if (_sound !== undefined) {
  1560. _sound.stop();
  1561. }
  1562. },
  1563. "click #forward": function() {
  1564. var error = false;
  1565. if (yt_player !== undefined) {
  1566. var duration = Number(Session.get("song").duration) | 0;
  1567. var skipDuration = Number(Session.get("song").skipDuration) | 0;
  1568. if (yt_player.getDuration() < duration + skipDuration) {
  1569. alert("The duration of the YouTube video is smaller than the duration.");
  1570. error = true;
  1571. } else {
  1572. yt_player.seekTo(skipDuration + duration - 10);
  1573. }
  1574. }
  1575. if (_sound !== undefined) {
  1576. _sound.seekTo((skipDuration + duration - 10) * 1000);
  1577. }
  1578. if (!error) {
  1579. if (previewEndSongTimeout !== undefined) {
  1580. Meteor.clearTimeout(previewEndSongTimeout);
  1581. }
  1582. previewEndSongTimeout = Meteor.setTimeout(function() {
  1583. if (yt_player !== undefined) {
  1584. yt_player.stopVideo();
  1585. }
  1586. if (_sound !== undefined) {
  1587. _sound.stop();
  1588. }
  1589. $("#play").attr("disabled", false);
  1590. $("#stop").attr("disabled", true);
  1591. $("#previewPlayer").hide();
  1592. }, 10000);
  1593. }
  1594. },
  1595. "click #get-spotify-info": function() {
  1596. var search = $("#title").val();
  1597. var artistName = $("#artist").val();
  1598. getSpotifyInfo(search, function(data) {
  1599. for(var i in data){
  1600. for(var j in data[i].items){
  1601. if(search.indexOf(data[i].items[j].name) !== -1 && artistName.indexOf(data[i].items[j].artists[0].name) !== -1){
  1602. $("#img").val(data[i].items[j].album.images[1].url);
  1603. $("#duration").val(data[i].items[j].duration_ms / 1000);
  1604. return;
  1605. }
  1606. }
  1607. }
  1608. }, artistName);
  1609. },
  1610. "click #save-song-button": function() {
  1611. var newSong = {};
  1612. newSong.id = $("#id").val();
  1613. newSong.likes = Number($("#likes").val());
  1614. newSong.dislikes = Number($("#dislikes").val());
  1615. newSong.title = $("#title").val();
  1616. newSong.artist = $("#artist").val();
  1617. newSong.img = $("#img").val();
  1618. newSong.type = $("#type").val();
  1619. newSong.duration = Number($("#duration").val());
  1620. newSong.skipDuration = $("#skip-duration").val();
  1621. if(newSong.skipDuration === undefined){
  1622. newSong.skipDuration = 0;
  1623. };
  1624. if (Session.get("type") === "playlist") {
  1625. Meteor.call("updatePlaylistSong", Session.get("genre"), Session.get("song"), newSong, function() {
  1626. $('#editModal').modal('hide');
  1627. });
  1628. } else {
  1629. Meteor.call("updateQueueSong", Session.get("genre"), Session.get("song"), newSong, function() {
  1630. $('#editModal').modal('hide');
  1631. });
  1632. }
  1633. }
  1634. });
  1635. Template.stations.onCreated(function() {
  1636. var tag = document.createElement("script");
  1637. tag.src = "https://www.youtube.com/iframe_api";
  1638. var firstScriptTag = document.getElementsByTagName('script')[0];
  1639. firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
  1640. yt_player = undefined;
  1641. _sound = undefined;
  1642. });
  1643. Template.queues.onCreated(function() {
  1644. var tag = document.createElement("script");
  1645. tag.src = "https://www.youtube.com/iframe_api";
  1646. var firstScriptTag = document.getElementsByTagName('script')[0];
  1647. firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
  1648. yt_player = undefined;
  1649. _sound = undefined;
  1650. });
  1651. Template.stations.onRendered(function() {
  1652. $("#previewModal").on("hidden.bs.modal", function() {
  1653. if (previewEndSongTimeout !== undefined) {
  1654. Meteor.clearTimeout(previewEndSongTimeout);
  1655. }
  1656. $("#play").attr("disabled", false);
  1657. $("#stop").attr("disabled", true);
  1658. if (yt_player !== undefined) {
  1659. $("#previewPlayer").hide();
  1660. yt_player.seekTo(0);
  1661. yt_player.stopVideo();
  1662. }
  1663. if (_sound !== undefined) {
  1664. _sound.stop();
  1665. }
  1666. });
  1667. $(document).ready(function() {
  1668. function makeSlider(){
  1669. var slider = $("#volume-slider").slider();
  1670. var volume = localStorage.getItem("volume") || 20;
  1671. $("#volume-slider").slider("setValue", volume);
  1672. if (slider.length === 0) {
  1673. Meteor.setTimeout(function() {
  1674. makeSlider();
  1675. }, 500);
  1676. } else {
  1677. slider.on("slide", function(val) {
  1678. localStorage.setItem("volume", val.value);
  1679. if (yt_player !== undefined) {
  1680. yt_player.setVolume(val.value);
  1681. } else if (_sound !== undefined) {
  1682. var volume = val.value / 100;
  1683. _sound.setVolume(volume);
  1684. }
  1685. });
  1686. }
  1687. }
  1688. makeSlider();
  1689. });
  1690. });
  1691. Template.queues.onRendered(function() {
  1692. $("#previewModal").on("hidden.bs.modal", function() {
  1693. if (previewEndSongTimeout !== undefined) {
  1694. Meteor.clearTimeout(previewEndSongTimeout);
  1695. }
  1696. $("#play").attr("disabled", false);
  1697. $("#stop").attr("disabled", true);
  1698. if (yt_player !== undefined) {
  1699. $("#previewPlayer").hide();
  1700. yt_player.seekTo(0);
  1701. yt_player.stopVideo();
  1702. }
  1703. if (_sound !== undefined) {
  1704. _sound.stop();
  1705. }
  1706. });
  1707. $(document).ready(function() {
  1708. function makeSlider(){
  1709. var slider = $("#volume-slider").slider();
  1710. var volume = localStorage.getItem("volume") || 20;
  1711. $("#volume-slider").slider("setValue", volume);
  1712. if (slider.length === 0) {
  1713. Meteor.setTimeout(function() {
  1714. makeSlider();
  1715. }, 500);
  1716. } else {
  1717. slider.on("slide", function(val) {
  1718. localStorage.setItem("volume", val.value);
  1719. if (yt_player !== undefined) {
  1720. yt_player.setVolume(val.value);
  1721. } else if (_sound !== undefined) {
  1722. var volume = val.value / 100;
  1723. _sound.setVolume(volume);
  1724. }
  1725. });
  1726. }
  1727. }
  1728. makeSlider();
  1729. });
  1730. });
  1731. Template.playlist.helpers({
  1732. playlist_songs: function() {
  1733. parts = location.href.split('/');
  1734. id = parts.pop();
  1735. type = id.toLowerCase();
  1736. var data = Playlists.findOne({type: type});
  1737. if (data !== undefined) {
  1738. data.songs.map(function(song) {
  1739. if (Session.get("currentSong") !== undefined && song.mid === Session.get("currentSong").mid) {
  1740. song.current = true;
  1741. } else {
  1742. song.current = false;
  1743. }
  1744. return song;
  1745. });
  1746. return data.songs;
  1747. } else {
  1748. return [];
  1749. }
  1750. }
  1751. });
  1752. Template.playlist.events({
  1753. "keyup #search-playlist": function(){
  1754. if($("#search-playlist").val().length === 0){
  1755. $(".pl-item").show();
  1756. } else {
  1757. $(".pl-item").hide();
  1758. var input = $("#search-playlist").val().toLowerCase();
  1759. $(".pl-item strong").each(function(i, el){
  1760. if($(el).text().toLowerCase().indexOf(input) !== -1){
  1761. $(el).parent(".pl-item").show();
  1762. }
  1763. })
  1764. $(".pl-item #pl-artist").each(function(i, el){
  1765. if($(el).text().toLowerCase().indexOf(input) !== -1){
  1766. $(el).parent(".pl-item").show();
  1767. }
  1768. })
  1769. }
  1770. },
  1771. "click #pl-item": function(){
  1772. console.log($(this).text());
  1773. }
  1774. })
  1775. Meteor.subscribe("rooms");
  1776. Template.room.onCreated(function () {
  1777. Chat.after.find(function(userId, selector) {
  1778. if (selector.type === "global") {
  1779. if (!$("#global-chat-tab").hasClass("active")) {
  1780. $("#global-chat-tab").addClass("unread-messages");
  1781. }
  1782. } else if(selector.type === Session.get("type")) {
  1783. if (!$("#chat-tab").hasClass("active")) {
  1784. $("#chat-tab").addClass("unread-messages");
  1785. }
  1786. }
  1787. });
  1788. Session.set("reportSong", false);
  1789. Session.set("reportTitle", false);
  1790. Session.set("reportAuthor", false);
  1791. Session.set("reportDuration", false);
  1792. Session.set("reportAudio", false);
  1793. Session.set("reportAlbumart", false);
  1794. Session.set("reportOther", false);
  1795. if (resizeSeekerbarInterval !== undefined) {
  1796. Meteor.clearInterval(resizeSeekerbarInterval);
  1797. resizeSeekerbarInterval = undefined;
  1798. }
  1799. yt_player = undefined;
  1800. _sound = undefined;
  1801. Session.set("videoHidden", false);
  1802. var tag = document.createElement("script");
  1803. tag.src = "https://www.youtube.com/iframe_api";
  1804. var firstScriptTag = document.getElementsByTagName('script')[0];
  1805. firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
  1806. var currentSong = undefined;
  1807. var currentSongR = undefined;
  1808. function getTimeElapsed() {
  1809. if (currentSong !== undefined) {
  1810. var room = Rooms.findOne({type: type});
  1811. if (room !== undefined) {
  1812. return Date.now() - currentSong.started - room.timePaused;
  1813. }
  1814. }
  1815. return 0;
  1816. }
  1817. function getSongInfo(songData){
  1818. Session.set("title", songData.title);
  1819. Session.set("artist", songData.artist);
  1820. Session.set("id", songData.id);
  1821. $("#song-img").attr("src", songData.img);
  1822. Session.set("duration", parseInt(songData.duration));
  1823. var d = moment.duration(parseInt(songData.duration), 'seconds');
  1824. $("#time-total").text(d.minutes() + ":" + ("0" + d.seconds()).slice(-2));
  1825. Session.set("timeFormat", d.minutes() + ":" + ("0" + d.seconds()).slice(-2));
  1826. }
  1827. function resizeSeekerbar() {
  1828. if (Session.get("state") === "playing") {
  1829. $("#seeker-bar").width(((getTimeElapsed() / 1000) / Session.get("duration") * 100) + "%");
  1830. }
  1831. }
  1832. function startSong() {
  1833. $("#time-elapsed").text("0:00");
  1834. $("#vote-skip").attr("disabled", false);
  1835. if (currentSong !== undefined) {
  1836. if (_sound !== undefined) _sound.stop();
  1837. if (yt_player !== undefined && yt_player.stopVideo !== undefined) yt_player.stopVideo();
  1838. var volume = localStorage.getItem("volume") || 20;
  1839. if (currentSong.type === "SoundCloud") {
  1840. if ($("#soundcloud-image").length !== 1) {
  1841. //$("#media-container").append('<img alt="Not loading" src="/soundcloud-image.png" class="embed-responsive-item" id="soundcloud-image" />');
  1842. $("#media-container").append('<h1 id="soundcloud-image">We have temporarily disabled the playing of SoundCloud songs. We are sorry for this inconvenience.</h1>');
  1843. }
  1844. if ($("#player").length === 1) {
  1845. $("#player").hide();
  1846. }
  1847. $("#soundcloud-image").show();
  1848. //getSongInfo(currentSong);
  1849. /*SC.stream("/tracks/" + currentSong.id, function(sound){
  1850. _sound = sound;
  1851. sound.setVolume(volume / 100);
  1852. sound.play();
  1853. var interval = setInterval(function() {
  1854. if (sound.getState() === "playing") {
  1855. sound.seek(getTimeElapsed());
  1856. window.clearInterval(interval);
  1857. }
  1858. }, 200);
  1859. Session.set("duration", parseInt(currentSong.duration));
  1860. var d = moment.duration(parseInt(currentSong.duration), 'seconds');
  1861. $("#time-total").text(d.minutes() + ":" + ("0" + d.seconds()).slice(-2));
  1862. resizeSeekerbar();
  1863. });*/
  1864. } else {
  1865. if ($("#player").length !== 1) {
  1866. $("#media-container").append('<div id="player" class="embed-responsive-item"></div>');
  1867. }
  1868. if ($("#soundcloud-image").length === 1) {
  1869. $("#soundcloud-image").hide();
  1870. }
  1871. $("#player").show();
  1872. function loadVideo() {
  1873. if (YT !== undefined && YT.loaded === 0 && YT.loading === 1) {
  1874. Session.set("loadVideoTimeout", Meteor.setTimeout(function() {
  1875. loadVideo();
  1876. }, 500));
  1877. } else {
  1878. if (yt_player === undefined) {
  1879. yt_player = new YT.Player("player", {
  1880. height: 540,
  1881. width: 960,
  1882. videoId: currentSong.id,
  1883. playerVars: {controls: 0, iv_load_policy: 3, rel: 0, showinfo: 0},
  1884. events: {
  1885. 'onReady': function(event) {
  1886. if(currentSong.skipDuration === undefined){
  1887. currentSong.skipDuration = 0;
  1888. }
  1889. event.target.seekTo(Number(currentSong.skipDuration) + getTimeElapsed() / 1000);
  1890. event.target.playVideo();
  1891. event.target.setVolume(volume);
  1892. resizeSeekerbar();
  1893. },
  1894. 'onStateChange': function(event){
  1895. if (YT !== undefined) {
  1896. if (event.data == YT.PlayerState.PAUSED && Session.get("state") === "playing") {
  1897. event.target.seekTo(Number(currentSong.skipDuration) + getTimeElapsed() / 1000);
  1898. event.target.playVideo();
  1899. }
  1900. if (event.data == YT.PlayerState.PLAYING && Session.get("state") === "paused") {
  1901. event.target.seekTo(Number(currentSong.skipDuration) + getTimeElapsed() / 1000);
  1902. event.target.pauseVideo();
  1903. }
  1904. }
  1905. }
  1906. }
  1907. });
  1908. } else {
  1909. yt_player.loadVideoById(currentSong.id);
  1910. if(currentSong.skipDuration === undefined){
  1911. currentSong.skipDuration = 0;
  1912. }
  1913. yt_player.seekTo(Number(currentSong.skipDuration) + getTimeElapsed() / 1000);
  1914. }
  1915. Session.set("pauseVideo", false);
  1916. getSongInfo(currentSong);
  1917. }
  1918. };
  1919. loadVideo();
  1920. }
  1921. }
  1922. }
  1923. Session.set("loaded", false);
  1924. Meteor.subscribe("rooms", function() {
  1925. var parts = location.href.split('/');
  1926. var id = parts.pop();
  1927. var type = id.toLowerCase();
  1928. Session.set("type", type);
  1929. if (Rooms.find({type: type}).count() !== 1) {
  1930. window.location = "/";
  1931. } else {
  1932. station_c = Meteor.subscribe(type);
  1933. Session.set("loaded", true);
  1934. minterval = Meteor.setInterval(function () {
  1935. var room = Rooms.findOne({type: type});
  1936. if (room !== undefined) {
  1937. if (room.state === "paused" || Session.get("pauseVideo")) {
  1938. Session.set("state", "paused");
  1939. // TODO Fix issue where sometimes nothing loads with the YT is not defined error. The error points to around this.
  1940. if (yt_player !== undefined && yt_player.getPlayerState !== undefined && yt_player.getPlayerState() === 1) {
  1941. yt_player.pauseVideo();
  1942. } else if (_sound !== undefined && _sound.getState().indexOf("playing") !== -1) {
  1943. _sound.pause();
  1944. }
  1945. } else {
  1946. Session.set("state", "playing");
  1947. if (yt_player !== undefined && yt_player.getPlayerState !== undefined && yt_player.getPlayerState() !== 1) {
  1948. yt_player.playVideo();
  1949. } else if (_sound !== undefined && _sound.getState().indexOf("paused") !== -1) {
  1950. _sound.play();
  1951. }
  1952. }
  1953. }
  1954. if (currentSongR === undefined || room.currentSong.started !== currentSongR.started) {
  1955. Session.set("previousSong", currentSong);
  1956. currentSongR = room.currentSong;
  1957. currentSong = room.currentSong.song;
  1958. currentSong.started = room.currentSong.started;
  1959. Session.set("currentSong", currentSong);
  1960. Meteor.clearTimeout(Session.get("loadVideoTimeout"));
  1961. startSong();
  1962. }
  1963. if (currentSong !== undefined) {
  1964. if (room !== undefined) {
  1965. var duration = (Date.now() - currentSong.started - room.timePaused) / 1000;
  1966. var song_duration = currentSong.duration;
  1967. if (song_duration <= duration) {
  1968. Session.set("pauseVideo", true);
  1969. }
  1970. var d = moment.duration(duration, 'seconds');
  1971. if (Session.get("state") === "playing") {
  1972. $("#time-elapsed").text(d.minutes() + ":" + ("0" + d.seconds()).slice(-2));
  1973. }
  1974. }
  1975. }
  1976. }, 100);
  1977. resizeSeekerbarInterval = Meteor.setInterval(function () {
  1978. resizeSeekerbar();
  1979. }, 500);
  1980. }
  1981. });
  1982. });