onCreated.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  1. var StationSubscription = undefined;
  2. var resizeSeekerbarInterval;
  3. Template.banned.onCreated(function() {
  4. if (Session.get("rTimeInterval") !== undefined) {
  5. Meteor.clearInterval(Session.get("rTimeInterval"))
  6. }
  7. Session.set("rTimeInterval", Meteor.setInterval(function() {
  8. Session.set("time", new Date().getTime());
  9. }, 10000));
  10. Session.set("ban", Meteor.user().punishments.ban);
  11. });
  12. Template.home.onCreated(function() {
  13. if (Session.get("minterval") !== undefined) {
  14. Meteor.clearInterval(Session.get("minterval"));
  15. }
  16. if (resizeSeekerbarInterval !== undefined) {
  17. Meteor.clearInterval(resizeSeekerbarInterval);
  18. resizeSeekerbarInterval = undefined;
  19. }
  20. if (StationSubscription !== undefined) {
  21. StationSubscription.stop();
  22. }
  23. Session.set("type", undefined);
  24. });
  25. Template.login.onCreated(function() {
  26. Session.set("github", true);
  27. Accounts.onLoginFailure(function() {
  28. if (Session.get("github") === true) {
  29. 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>');
  30. $(".landing").before(errAlert);
  31. Meteor.setTimeout(function() {
  32. errAlert.fadeOut(5000, function() {
  33. errAlert.remove();
  34. });
  35. }, 10000);
  36. }
  37. });
  38. });
  39. Template.admin.onCreated(function() {
  40. Meteor.subscribe("allAlerts");
  41. });
  42. Template.feedback.onCreated(function(){
  43. Meteor.subscribe("feedback");
  44. })
  45. Template.profile.onCreated(function() {
  46. var parts = Router.current().url.split('/');
  47. var username = parts.pop();
  48. Session.set("loaded", false);
  49. Meteor.subscribe("userProfiles", username.toLowerCase(), function() {
  50. if (Meteor.users.find({"profile.usernameL": username.toLowerCase()}).count() === 0) {
  51. window.location = "/";
  52. } else {
  53. var data = Meteor.users.findOne({"profile.usernameL": username.toLowerCase()});
  54. Session.set("real_name", data.profile.realname);
  55. Session.set("username", data.profile.username);
  56. Session.set("first_joined", data.createdAt);
  57. Session.set("rank", data.profile.rank);
  58. Session.set("songs_requested", data.profile.statistics.songsRequested);
  59. Session.set("liked", data.profile.liked);
  60. Session.set("disliked", data.profile.disliked);
  61. Session.set("loaded", true);
  62. }
  63. });
  64. });
  65. Template.queues.onCreated(function() {
  66. var tag = document.createElement("script");
  67. tag.src = "https://www.youtube.com/iframe_api";
  68. var firstScriptTag = document.getElementsByTagName('script')[0];
  69. firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
  70. YTPlayer = undefined;
  71. $(document).keydown(function(evt){
  72. if (evt.keyCode==83 && (evt.ctrlKey)){
  73. evt.preventDefault();
  74. if (Session.get("editing") === true) {
  75. $("#save-song-button").click();
  76. }
  77. }
  78. });
  79. });
  80. Template.manageStation.onCreated(function() {
  81. var tag = document.createElement("script");
  82. tag.src = "https://www.youtube.com/iframe_api";
  83. var firstScriptTag = document.getElementsByTagName('script')[0];
  84. firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
  85. YTPlayer = undefined;
  86. $(document).keydown(function(evt){
  87. if (evt.keyCode==83 && (evt.ctrlKey)){
  88. evt.preventDefault();
  89. if (Session.get("editing") === true) {
  90. $("#save-song-button").click();
  91. }
  92. }
  93. });
  94. });
  95. Template.manageSongs.onCreated(function() {
  96. Session.set("showNoGenres", false);
  97. Session.set("showGenres", true);
  98. var tag = document.createElement("script");
  99. tag.src = "https://www.youtube.com/iframe_api";
  100. var firstScriptTag = document.getElementsByTagName('script')[0];
  101. firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
  102. YTPlayer = undefined;
  103. $(document).keydown(function(evt){
  104. if (evt.keyCode==83 && (evt.ctrlKey)){
  105. evt.preventDefault();
  106. if (Session.get("editing") === true) {
  107. $("#save-song-button").click();
  108. }
  109. }
  110. });
  111. });
  112. Template.register.onCreated(function() {
  113. Accounts.onLoginFailure(function() {
  114. 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>');
  115. $(".landing").before(errAlert);
  116. Meteor.setTimeout(function() {
  117. errAlert.fadeOut(5000, function() {
  118. errAlert.remove();
  119. });
  120. }, 10000);
  121. });
  122. });
  123. Template.room.onCreated(function () {
  124. Chat.after.find(function(userId, selector) {
  125. if (selector.type === "global") {
  126. if (!$("#global-chat-tab").hasClass("active")) {
  127. $("#global-chat-tab").addClass("unread-messages");
  128. }
  129. } else if(selector.type === Session.get("type")) {
  130. if (!$("#chat-tab").hasClass("active")) {
  131. $("#chat-tab").addClass("unread-messages");
  132. }
  133. }
  134. });
  135. Session.set("reportSong", false);
  136. Session.set("reportTitle", false);
  137. Session.set("reportAuthor", false);
  138. Session.set("reportDuration", false);
  139. Session.set("reportAudio", false);
  140. Session.set("reportAlbumart", false);
  141. Session.set("reportOther", false);
  142. Session.set("si_or_pl", "singleVideo");
  143. Session.set("editingSong", false);
  144. var parts = location.href.split('/');
  145. var id = parts.pop();
  146. var type = id.toLowerCase();
  147. if (resizeSeekerbarInterval !== undefined) {
  148. Meteor.clearInterval(resizeSeekerbarInterval);
  149. resizeSeekerbarInterval = undefined;
  150. }
  151. YTPlayer = undefined;
  152. Session.set("videoHidden", false);
  153. var tag = document.createElement("script");
  154. tag.src = "https://www.youtube.com/iframe_api";
  155. var firstScriptTag = document.getElementsByTagName('script')[0];
  156. firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
  157. Session.set("singleVideo", true);
  158. var currentSong = undefined;
  159. var currentSongR = undefined;
  160. function getTimeElapsed() {
  161. var type = Session.get("type");
  162. if (currentSong !== undefined) {
  163. var room = Rooms.findOne({type: type});
  164. if (room !== undefined) {
  165. return Date.now() - currentSong.started - room.timePaused;
  166. }
  167. }
  168. return 0;
  169. }
  170. function getSongInfo(songData){
  171. Session.set("title", songData.title);
  172. Session.set("artist", songData.artist);
  173. Session.set("id", songData.id);
  174. $("#song-img").attr("src", songData.img);
  175. Session.set("duration", parseInt(songData.duration));
  176. var d = moment.duration(parseInt(songData.duration), 'seconds');
  177. $("#time-total").text(d.minutes() + ":" + ("0" + d.seconds()).slice(-2));
  178. Session.set("timeFormat", d.minutes() + ":" + ("0" + d.seconds()).slice(-2));
  179. document.title = Session.get("title") + " - " + Session.get("artist") + " - Musare";
  180. }
  181. function resizeSeekerbar() {
  182. if (Session.get("state") === "playing") {
  183. $(".seeker-bar").width(((getTimeElapsed() / 1000) / Session.get("duration") * 100) + "%");
  184. }
  185. }
  186. function startSong() {
  187. $("#time-elapsed").text("0:00");
  188. $("#vote-skip").attr("disabled", false);
  189. if (currentSong !== undefined) {
  190. if (YTPlayer !== undefined && YTPlayer.stopVideo !== undefined) YTPlayer.stopVideo();
  191. var volume = localStorage.getItem("volume") || 20;
  192. $("#player").show();
  193. function loadVideo() {
  194. if (!Session.get("YTLoaded")) {
  195. Session.set("loadVideoTimeout", Meteor.setTimeout(function () {
  196. loadVideo();
  197. }, 500));
  198. } else {
  199. if (YTPlayer === undefined) {
  200. YTPlayer = new YT.Player("player", {
  201. height: 270,
  202. width: 480,
  203. videoId: currentSong.id,
  204. playerVars: {controls: 0, iv_load_policy: 3, rel: 0, showinfo: 0},
  205. events: {
  206. 'onReady': function (event) {
  207. if (currentSong.skipDuration === undefined) {
  208. currentSong.skipDuration = 0;
  209. }
  210. event.target.seekTo(Number(currentSong.skipDuration) + getTimeElapsed() / 1000);
  211. event.target.playVideo();
  212. event.target.setVolume(volume);
  213. resizeSeekerbar();
  214. },
  215. 'onStateChange': function (event) {
  216. if (Session.get("YTLoaded")) {
  217. if (event.data == YT.PlayerState.PAUSED && Session.get("state") === "playing") {
  218. event.target.seekTo(Number(currentSong.skipDuration) + getTimeElapsed() / 1000);
  219. event.target.playVideo();
  220. }
  221. if (event.data == YT.PlayerState.PLAYING && Session.get("state") === "paused") {
  222. event.target.seekTo(Number(currentSong.skipDuration) + getTimeElapsed() / 1000);
  223. event.target.pauseVideo();
  224. }
  225. }
  226. }
  227. }
  228. });
  229. } else {
  230. YTPlayer.loadVideoById(currentSong.id);
  231. if (currentSong.skipDuration === undefined) {
  232. currentSong.skipDuration = 0;
  233. }
  234. YTPlayer.seekTo(Number(currentSong.skipDuration) + getTimeElapsed() / 1000);
  235. $("#vote-skip").removeClass("disabled");
  236. }
  237. Session.set("pauseVideo", false);
  238. getSongInfo(currentSong);
  239. }
  240. }
  241. loadVideo();
  242. }
  243. }
  244. Session.set("loaded", false);
  245. Meteor.subscribe("rooms", function() {
  246. var parts = location.href.split('/');
  247. var id = parts.pop();
  248. var type = id.toLowerCase();
  249. Session.set("type", type);
  250. if (Rooms.find({type: type}).count() !== 1) {
  251. window.location = "/";
  252. } else {
  253. StationSubscription = Meteor.subscribe(type);
  254. Session.set("loaded", true);
  255. Session.set("minterval", Meteor.setInterval(function () {
  256. var room = Rooms.findOne({type: type});
  257. if (room !== undefined) {
  258. if (room.state === "paused" || Session.get("pauseVideo")) {
  259. Session.set("state", "paused");
  260. // TODO Fix issue where sometimes nothing loads with the YT is not defined error. The error points to around this.
  261. if (YTPlayer !== undefined && YTPlayer.getPlayerState !== undefined && YTPlayer.getPlayerState() === 1) {
  262. YTPlayer.pauseVideo();
  263. }
  264. } else {
  265. Session.set("state", "playing");
  266. if (YTPlayer !== undefined && YTPlayer.getPlayerState !== undefined && YTPlayer.getPlayerState() !== 1) {
  267. YTPlayer.playVideo();
  268. }
  269. }
  270. }
  271. if (currentSongR === undefined || room.currentSong.started !== currentSongR.started) {
  272. Session.set("previousSong", currentSong);
  273. currentSongR = room.currentSong;
  274. currentSong = room.currentSong.song;
  275. currentSong.started = room.currentSong.started;
  276. Session.set("currentSong", currentSong);
  277. Meteor.clearTimeout(Session.get("loadVideoTimeout"));
  278. startSong();
  279. }
  280. if (currentSong !== undefined) {
  281. if (room !== undefined) {
  282. var duration = (Date.now() - currentSong.started - room.timePaused) / 1000;
  283. var song_duration = currentSong.duration;
  284. if (song_duration <= duration) {
  285. Session.set("pauseVideo", true);
  286. }
  287. var d = moment.duration(duration, 'seconds');
  288. if (Session.get("state") === "playing") {
  289. $("#time-elapsed").text(d.minutes() + ":" + ("0" + d.seconds()).slice(-2));
  290. }
  291. }
  292. }
  293. }, 100));
  294. resizeSeekerbarInterval = Meteor.setInterval(function () {
  295. resizeSeekerbar();
  296. }, 500)
  297. }
  298. });
  299. Meteor.setTimeout(function(){
  300. $("#playlist-slideout").on("click", function(){
  301. if($("#chat-slide-out").css("right") === "0px"){
  302. $("#chat-slideout").sideNav("hide");
  303. }
  304. else if($("#users-slide-out").css("right") === "0px"){
  305. $("#users-slideout").sideNav("hide");
  306. }
  307. var marginRightWidth = ($(document).width() - $(".container").width()) / 2 + "px";
  308. $(".room-container").css("margin-right", "370px")
  309. if($("#playlist-slide-out").css("right") === "0px"){
  310. $(".room-container").css("margin-right", marginRightWidth);
  311. }
  312. });
  313. $("#chat-slideout").on("click", function(){
  314. if($("#playlist-slide-out").css("right") === "0px"){
  315. $("#playlist-slideout").sideNav("hide");
  316. }
  317. else if($("#users-slide-out").css("right") === "0px"){
  318. $("#users-slideout").sideNav("hide");
  319. }
  320. var marginRightWidth = ($(document).width() - $(".container").width()) / 2 + "px";
  321. $(".chat-ul").scrollTop(1000000);
  322. $(".room-container").css("margin-right", "370px")
  323. if($("#chat-slide-out").css("right") === "0px"){
  324. $(".room-container").css("margin-right", marginRightWidth);
  325. }
  326. });
  327. $("#users-slideout").on("click", function(){
  328. if($("#playlist-slide-out").css("right") === "0px"){
  329. $("#playlist-slideout").sideNav("hide");
  330. }
  331. else if($("#chat-slide-out").css("right") === "0px"){
  332. $("#chat-slideout").sideNav("hide");
  333. }
  334. var marginRightWidth = ($(document).width() - $(".container").width()) / 2 + "px";
  335. $(".room-container").css("margin-right", "370px")
  336. if($("#users-slide-out").css("right") === "0px"){
  337. $(".room-container").css("margin-right", marginRightWidth);
  338. }
  339. });
  340. $("body").on("click", function(e){
  341. });
  342. $(window).on("resize", function(){
  343. var marginRightWidth = ($(document).width() - $(".container").width()) / 2 + "px";
  344. $(".container").css("margin-right", marginRightWidth);
  345. })
  346. }, 1000);
  347. });
  348. Template.settings.onCreated(function() {
  349. $(document).ready(function() {
  350. var user = Meteor.user();
  351. function initSettings() {
  352. if (user !== undefined) {
  353. if (user.profile.settings && user.profile.settings.showRating === true) {
  354. function setChecked() {
  355. $("#showRating").prop("checked", true);
  356. if (!$("#showRating").prop("checked")) {
  357. Meteor.setTimeout(function() {
  358. setChecked();
  359. }, 100);
  360. }
  361. }
  362. setChecked();
  363. }
  364. } else {
  365. Meteor.setTimeout(function() {
  366. initSettings();
  367. }, 500);
  368. }
  369. }
  370. initSettings();
  371. });
  372. });