2
0

events.js 88 KB

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