Advertisement
MrSiir

App Finder Spotify Code

Dec 9th, 2011
1,075
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. "use strict";
  2. var request = sp.require("sp://import/scripts/request");
  3. var dom = sp.require("sp://import/scripts/dom");
  4. var hermesScheme = sp.require("assets/js/appstore-proto");
  5.  
  6. /**
  7.  * Setting variables
  8.  */
  9. var carouselSpeed = 5000; //ms
  10.  
  11. /**
  12.  * Private variables
  13.  */
  14.  var COUNTRY = sp.core.country;
  15.  
  16. /**
  17.  * Class for the App Market
  18.  */
  19. var appMarket = {
  20.     init: function()
  21.     {
  22.         appMarket.buildApps();
  23.         Banner.init();
  24.  
  25.         sp.installer.addEventListener('favoriteRemoved', function (app) {
  26.             appMarket.removeFavourite(app.data);
  27.         });
  28.         sp.installer.addEventListener('favoriteAdded', function (app) {
  29.             appMarket.addFavourite(app.data);
  30.         });
  31.     },
  32.     buildApps: function()
  33.     {
  34.         var li = document.querySelectorAll("li");
  35.         for(var i=0; li.length - 1 >= i; i++ ) {
  36.             var appElement = li[i];
  37.             var appId = appElement.id;
  38.  
  39.             /* Beware of ugly hardcoded market fix */
  40.             if (appId === 'tunigo' && COUNTRY === 'SE') {
  41.                 appElement.style.display = 'inline-block';
  42.             }
  43.             if (appId === 'dagbladet' && COUNTRY === 'NO') {
  44.                 appElement.style.display = 'inline-block';
  45.             }
  46.  
  47.             var isFavourite = sp.installer.isApplicationFavorite(appId);
  48.  
  49.             if (isFavourite) {
  50.                 var button = appElement.querySelector('button');
  51.                 button.disabled = true;
  52.                 button.innerHTML = 'added';
  53.                 button.classList.remove('primary');
  54.             }
  55.  
  56.             appElement.addEventListener('click', function(e) {
  57.                 var appId = this.id;
  58.                 var element = e.target;
  59.                 var tag = e.target.tagName;
  60.  
  61.                 if (tag !== 'BUTTON' && tag !== 'LI') {
  62.                     window.location.href = 'spotify:app:' + appId;
  63.                 }
  64.                 if (tag === 'BUTTON' && element.disabled === false) {
  65.                     sp.installer.addApplicationFavorite(appId);
  66.                 }
  67.             }, false);
  68.         }
  69.     },
  70.     addFavourite: function(appId)
  71.     {
  72.         var appElement = document.querySelector('#' + appId);
  73.         var appButton = appElement.querySelector('button');
  74.         appButton.disabled = true;
  75.         appButton.innerHTML = 'added';
  76.         appButton.classList.remove('primary');
  77.     },
  78.     removeFavourite: function(appId)
  79.     {
  80.         var appElement = document.querySelector('#' + appId);
  81.         var appButton = appElement.querySelector('button');
  82.         appButton.disabled = false;
  83.         appButton.innerHTML = 'add';
  84.         appButton.classList.add('primary');
  85.     },
  86.     getHermes: function () {
  87.         var postObj = {
  88.             market: "SE",
  89.             platform: 0
  90.         };
  91.  
  92.         sp.core.getHermes("GET", "hm://appstore/app/list/",
  93.             [
  94.                 ["RequestHeader", postObj]
  95.             ],
  96.             {
  97.                 onSuccess: function() {console.log('hej')},
  98.                 onFailure: function() {console.log('woot')},
  99.                 onComplete: function() {}
  100.             }
  101.         );
  102.     }
  103. }
  104.  
  105. /**
  106.  * Class for a array of top-banners
  107.  **/
  108. var Banner = {
  109.  
  110.     init: function()
  111.     {
  112.         var header = document.querySelector('header');
  113.  
  114.         var bannerActive = false;
  115.         header.onmouseover = function() { bannerActive = true;}
  116.         header.onmouseout = function() { bannerActive = false;}
  117.         var bannerInterval = window.setInterval(function() {
  118.             if (bannerActive === false) {
  119.                 Banner.next();
  120.             }
  121.         }, carouselSpeed);
  122.  
  123.         header.addEventListener('click', function(e) {
  124.             var target = e.target;
  125.             if (target.id === 'nextimage') {
  126.                 Banner.next();
  127.             } else if (target.id === 'previousimage') {
  128.                 Banner.previous();
  129.             } else if (target.dataset['appid'] === 'spotify') {
  130.                 return;
  131.             } else {
  132.                 window.location = 'spotify:app:' + target.dataset['appid'];
  133.             }
  134.         });
  135.     },
  136.     /**
  137.      * Set a class "focus" on the next banner image
  138.      **/
  139.     next: function()
  140.     {
  141.         var focus = false;
  142.         var banners = document.querySelectorAll(".banner-images");
  143.         for(var i=0; banners.length - 1 >= i; i++)
  144.         {
  145.             if (focus){
  146.                 banners[i].classList.add('focus');
  147.                 focus = false;
  148.                 break;
  149.             }
  150.             if(banners[i].classList.contains('focus')){
  151.                 focus = true;
  152.                 banners[i].classList.remove('focus');
  153.             }
  154.         }
  155.         if (focus){
  156.             banners[0].classList.add('focus');
  157.             focus = false;
  158.         }
  159.     },
  160.     /**
  161.     * Set a class "focus" on the previous banner image
  162.     **/
  163.     previous: function(){
  164.         var focus = false;
  165.         var banners = document.querySelectorAll(".banner-images");
  166.         for(var i= banners.length -1 ; i >= 0; i-- )
  167.         {
  168.             if (focus){
  169.                 banners[i].classList.add('focus');
  170.                 focus = false;
  171.                 break;
  172.             }
  173.             if(banners[i].classList.contains('focus')){
  174.                 focus = true;
  175.                 banners[i].classList.remove('focus');
  176.             }
  177.         }
  178.         if (focus){
  179.             banners[banners.length -1].classList.add('focus');
  180.             focus = false;
  181.         }
  182.  
  183.     }
  184. };
  185.  
  186. /**
  187.  * Exports
  188.  */
  189. exports.init = appMarket.init;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement