Advertisement
Guest User

popup.js

a guest
Feb 21st, 2012
269
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var backgroundPage;
  2.  
  3. function ToggleMusicOff() {
  4.     backgroundPage.ToggleMusic();
  5.     $('#ToggleMusicOffWrapper').hide();
  6.     $('#ToggleMusicOnWrapper').show();
  7. }
  8.  
  9. function ToggleMusicOn() {
  10.     backgroundPage.ToggleMusic();
  11.     $('#ToggleMusicOnWrapper').hide();
  12.     $('#ToggleMusicOffWrapper').show();
  13. }
  14.  
  15. function ShuffleMusic() {
  16.     backgroundPage.ShuffleMusic();
  17.     PopulateGrid();
  18. }
  19.  
  20. function NextSong() {
  21.     backgroundPage.NextSong();
  22.     PopulateGrid();
  23.     SetGridCaption();
  24. }
  25.  
  26. function GetPlaylist() {
  27.     var playlist = [];
  28.     try {
  29.         playlist = JSON.parse(localStorage.getItem('playlist'));
  30.     }
  31.     catch (exception) {
  32.     }
  33.  
  34.     if (playlist == null) { //RobW: == null is equal to === null and === undefined
  35.         playlist = [];
  36.     }
  37.  
  38.     return playlist;
  39. }
  40.  
  41. function PopulateGrid() {
  42.     var playlist = GetPlaylist();
  43.  
  44.     var $SongGrid = $('#SongGrid');
  45.     $SongGrid.clearGridData();
  46.  
  47.     for (var i = 0; i < playlist.length; i++) {
  48.         var datarow = { songname: '<a href="#" title="' + playlist[i].URL + '" class="clickTip"><img src="images/clipboard--plus.png" style="margin-bottom: -2px" /> </a>' + '<a href="#" onclick="RemoveSong(\'' + playlist[i].ID + '\')"><img src="images/cross.png" style="margin-bottom: -2px" /></a>' + playlist[i].Name, dateadded: "2012-04-20" };
  49.         $SongGrid.addRowData(playlist[i].ID, datarow);
  50.     }
  51.  
  52.     // on click tooltip with custom content
  53.     $('a.clickTip').aToolTip({
  54.         clickIt: true
  55.     });
  56. }
  57.  
  58. function RemoveSong(songID) {
  59.     var playlist = GetPlaylist();
  60.  
  61.     var indexOfSong = -1;
  62.     $.each(playlist, function (index, value) {
  63.         if (value.ID == songID) {
  64.             indexOfSong = index;
  65.             return false; //RobW: Why look further if we have already found the id..?
  66.         }
  67.     });
  68.  
  69.     if (indexOfSong != -1) {
  70.         playlist.splice(indexOfSong, 1);
  71.         localStorage.setItem('playlist', JSON.stringify(playlist));
  72.         PopulateGrid();
  73.     }
  74. }
  75.  
  76. function ClearPlaylist() {
  77.     localStorage.setItem('playlist', []);
  78.     PopulateGrid();
  79. }
  80.  
  81. function BackupPlaylist() {
  82.     var playlist = localStorage.getItem('playlist');
  83.     localStorage.setItem('backup', playlist);
  84. }
  85.  
  86. function LoadPlaylist() {
  87.     var playlist = localStorage.getItem('backup');
  88.     localStorage.setItem('playlist', playlist);
  89.     PopulateGrid();
  90. }
  91.  
  92. function EnqueueSong() {
  93.     var playlist = GetPlaylist();
  94.  
  95.     var songUrl = $('#songUrlInput').val();
  96.     var songID = GetIdFromUrl(songUrl);
  97.  
  98.     var songObject = {};
  99.     songObject.URL = songUrl;
  100.     songObject.ID = songID;
  101.  
  102.     $.getJSON('http://gdata.youtube.com/feeds/api/videos/' + songID + '?v=2&alt=json-in-script&callback=?', function (data) {
  103.         songObject.Name = data.entry.title.$t;
  104.  
  105.         playlist.push(songObject);
  106.         localStorage.setItem('playlist', JSON.stringify(playlist));
  107.  
  108.         //If there is now a song to initialize our playlist with, go ahead.
  109.         if (playlist.length) { //RobW: Removed " == 1". If playlist != 0, then we can also play..
  110.             backgroundPage.Initialize(playlist);
  111.         }
  112.  
  113.         PopulateGrid();
  114.     });
  115. };
  116.  
  117. function textWidth(text, fontSize) {
  118.     var calc = '<span style="display:none; font-size: ' + fontSize + 'px">' + text + '</span>';
  119.     $('body').append(calc);
  120.     var width = $('body').find('span:last').width();
  121.     $('body').find('span:last').remove();
  122.     return width;
  123. };
  124.  
  125. $(document).ready(function () {
  126.     /*RobW: ctrl can be detected using event.ctrlKey
  127.     var ctrlDown = false;
  128.     var ctrlKey = 17, vKey = 86, cKey = 67;
  129.  
  130.     $(document).keydown(function (e) {
  131.         if (e.keyCode == ctrlKey) {
  132.             ctrlDown = true;
  133.         }
  134.         else if (ctrlDown && e.keyCode == cKey) {
  135.             $('#aToolTipCloseBtn').click();
  136.         }
  137.     }).keyup(function (e) {
  138.         if (e.keyCode == ctrlKey) ctrlDown = false;
  139.     });*/
  140.     $(document).keydown(function(e) {
  141.         if (e.ctrlKey && e.which == 67 /* = c key */) {
  142.             $('#aToolTipCloseBtn').click();
  143.         }
  144.     });
  145. });
  146.  
  147. $(document).ready(function () {
  148.     /* RobW: This section makes no sense! At init, #Right contains an empty table. Setting a fixed height is awful. */
  149.     var rightColumnWidth = $('#Right').width();
  150.     var rightColumnHeight = $('#Right').height();
  151.     var songnameColumnWidth = (rightColumnWidth * 2) / 3;
  152.     var dateaddedColumnWidth = rightColumnWidth / 3;
  153.  
  154.     $("#SongGrid").jqGrid({
  155.         align: 'center',
  156.         colNames: ['Song Name', 'Date Added'],
  157.         colModel: [
  158.       { name: 'songname', index: 'songname', width: songnameColumnWidth },
  159.       { name: 'dateadded', index: 'dateadded', width: dateaddedColumnWidth },
  160.     ],
  161.         rowNum: 10,
  162.         rowList: [10, 20, 30],
  163.         sortname: 'songname',
  164.         sortorder: 'desc',
  165.         gridview: true,
  166.         caption: 'Your Songs',
  167.         width: rightColumnWidth,
  168.         height: 'auto'
  169.     });
  170.  
  171.     $('#MenuContent').css('min-height', rightColumnHeight + 51);
  172.  
  173.     $("#songUrlInput").keyup(function (event) {
  174.         var validator = IsValidSong(this.value);
  175.  
  176.         if (validator.isValidSong == true) {
  177.             EnqueueSong();
  178.         }
  179.  
  180.         if (validator.msg != "Invalid") {
  181.             $(this).attr('placeholder', validator.msg);
  182.             this.value = '';
  183.             $(this).blur();
  184.             window.setTimeout(
  185.                 function () {
  186.                     $('#songUrlInput').attr('placeholder', "Moar Music! Gimmie dat link.");
  187.                 },
  188.                 2000);
  189.         }
  190.     });
  191.  
  192.     chrome.extension.onConnect.addListener(function (port) {
  193.         port.onMessage.addListener(function (msg) {
  194.             if (msg.playerState == "Buffering") {
  195.                 SetGridCaption("Buffering: " + GetPlaylist()[0].Name);
  196.             }
  197.             else if (msg.playerState == "Playing") {
  198.                 $("#SongGrid").setCaption("Playing: " + GetPlaylist()[0].Name);
  199.             }
  200.             else if (msg.playerState == "Paused") {
  201.                 SetGridCaption("Paused: " + GetPlaylist()[0].Name);
  202.             }
  203.         });
  204.     });
  205.  
  206.     $("#ToggleMusicOnWrapper").click(ToggleMusicOn);
  207.     $("#ToggleMusicOffWrapper").click(ToggleMusicOff);
  208.     $("#ShuffleMusic").click(ShuffleMusic);
  209.     $("#NextSong").click(NextSong);
  210.  
  211.     PopulateGrid();
  212.     backgroundPage = chrome.extension.getBackgroundPage();
  213.  
  214.     var playlist = GetPlaylist();
  215.     if (playlist.length > 0) {
  216.         backgroundPage.Initialize(playlist);
  217.     }
  218. });
  219.  
  220. function SetGridCaption(caption) {
  221.     var fontSize = 18;
  222.  
  223.     while (textWidth(caption, fontSize) > 330) {
  224.         fontSize--;
  225.     }
  226.  
  227.     $("#SongGrid").setCaption("");
  228.     $('.ui-jqgrid-title').css('padding-left', 0);
  229.     $('.ui-jqgrid-title').css('font-size', fontSize + 'px');
  230.     $("#SongGrid").setCaption(caption);
  231. }
  232.  
  233. function IsValidSong(url) {
  234.     var validator = new Object();
  235.     validator.isValidSong = true;
  236.     validator.msg = "Thanks!";
  237.  
  238.     //var regExp = /^.*((youtu.be\/)|(v\/)|(\/u\/\w\/)|(embed\/)|(watch\?))\??v?=?([^#\&\?]*).*/;
  239.     //var match = url.match(regExp);
  240.     //if (match && match[7].length == 11) {
  241.     var regExp = /https?:\/\/(?:[0-9A-Z-]+\.)?(?:youtu\.be\/|youtube\.com\S*[^\w\-\s])([\w\-]{11})(?=[^\w\-]|$)(?![?=&+%\w]*(?:['"][^<>]*>|<\/a>))[?=&+%\w]*/i;
  242.     var match = regExp.exec(url);
  243.     if (match) {
  244.         var playlist = GetPlaylist();
  245.  
  246.         $.each(playlist, function (index, value) {
  247.             if (value.ID == match[1]) {
  248.                 validator.isValidSong = false;
  249.                 validator.msg = "I already know that one!";
  250.             }
  251.         });
  252.     }
  253.     else {
  254.         validator.isValidSong = false;
  255.         validator.msg = "Invalid";
  256.     }
  257.  
  258.     return validator;
  259. }
  260.  
  261. function GetIdFromUrl(url) {
  262.     //var regExp = /^.*((youtu.be\/)|(v\/)|(\/u\/\w\/)|(embed\/)|(watch\?))\??v?=?([^#\&\?]*).*/;
  263.     //var match = url.match(regExp);
  264.     //if (match && match[7].length == 11) {
  265.     var regExp = /https?:\/\/(?:[0-9A-Z-]+\.)?(?:youtu\.be\/|youtube\.com\S*[^\w\-\s])([\w\-]{11})(?=[^\w\-]|$)(?![?=&+%\w]*(?:['"][^<>]*>|<\/a>))[?=&+%\w]*/i;
  266.     var match = regExp.exec(url);
  267.     if (match) {        
  268.         return match[1];
  269.     }
  270.     else {
  271.         alert("Failed to read youtube URL. Did you enter a valid link?");
  272.     }
  273. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement