2
0

onCreated.js 16 KB

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