/*!
 *
 *  Web Starter Kit
 *  Copyright 2015 Google Inc. All rights reserved.
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *    https://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License
 *
 */
/* eslint-env browser */
(function() {
  'use strict';

  // Check to make sure service workers are supported in the current browser,
  // and that the current page is accessed from a secure origin. Using a
  // service worker from an insecure origin will trigger JS console errors. See
  // http://www.chromium.org/Home/chromium-security/prefer-secure-origins-for-powerful-new-features
  var isLocalhost = Boolean(window.location.hostname === 'localhost' ||
    // [::1] is the IPv6 localhost address.
    window.location.hostname === '[::1]' ||
    // 127.0.0.1/8 is considered localhost for IPv4.
    window.location.hostname.match(
      /^127(?:\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3}$/
    )
  );

  var app = {
    isLoading: true,
    visibleCards: {},
    spinner: document.querySelector('.loader'),
    cardTemplate: document.querySelector('.cardTemplate'),
    container: document.querySelector('.main'),
  };

  if ('serviceWorker' in navigator &&
    (window.location.protocol === 'https:' || isLocalhost)) {
    navigator.serviceWorker.register('service-worker.js')
      .then(function(registration) {
        // updatefound is fired if service-worker.js changes.
        registration.onupdatefound = function() {
          // updatefound is also fired the very first time the SW is installed,
          // and there's no need to prompt for a reload at that point.
          // So check here to see if the page is already controlled,
          // i.e. whether there's an existing service worker.
          if (navigator.serviceWorker.controller) {
            // The updatefound event implies that registration.installing is set:
            // https://slightlyoff.github.io/ServiceWorker/spec/service_worker/index.html#service-worker-container-updatefound-event
            var installingWorker = registration.installing;

            installingWorker.onstatechange = function() {
              switch (installingWorker.state) {
                case 'installed':
                  // At this point, the old content will have been purged and the
                  // fresh content will have been added to the cache.
                  // It's the perfect time to display a "New content is
                  // available; please refresh." message in the page's interface.
                  break;

                case 'redundant':
                  throw new Error('The installing ' +
                    'service worker became redundant.');

                default:
                  // Ignore
              }
            };
          }
        };
      }).catch(function(e) {
        console.error('Error during service worker registration:', e);
      });
  }
  // Your custom JavaScript goes here
  /*****************************************************************************
   *
   * Event listeners for UI elements
   *
   ****************************************************************************/

  document.getElementById('btnRefresh').addEventListener('click', function() {
    app.queryArticles();
    console.log("Refresh");
  });

  app.queryArticles = function() {
    var rssfeed = "https://planet.kde.org/rss20.xml"
    var url = "https://query.yahooapis.com/v1/public/yql?q=select%20title%2Clink%2CpubDate%2Cauthor%20from%20rss%20where%20url%3D'https%3A%2F%2Fplanet.kde.org%2Frss20.xml'&format=json&diagnostics=true&"
    if ('caches' in window) {
      /*
       * Check if the service worker has already cached this data
       * If the service worker has the data, then display the cached
       * data while the app fetches the latest data.
       */
      caches.match(url).then(function(response) {
        if (response) {
          response.json().then(function updateFromCache(json) {
            var results = json.query.results;
            app.updateCards(results);
          });
        }
      });
    }
    // Fetch the latest data.
    var request = new XMLHttpRequest();
    request.onreadystatechange = function() {
      if (request.readyState === XMLHttpRequest.DONE) {
        if (request.status === 200) {
          var response = JSON.parse(request.response);
          var results = response.query.results;
          app.updateCards(results);
        }
      } else {
        //Do Nothing
        console.log('No new available data');
      }
    };
    request.open('GET', url);
    request.send();
  };

  app.updateCards = function(data) {
    for (var i = 0; i < data.item.length; ++i) {
      data.item[i].key = i;
      app.updateArticleCard(data.item[i]);
    };
  };
  app.updateArticleCard = function(data) {
    var dataLastUpdated = new Date(data.created);
    var title = data.title;
    var author = data.author;
    var link = data.link;
    var pubDate = data.pubDate;

    var card = app.visibleCards[data.key];
    if (!card) {
      card = app.cardTemplate.cloneNode(true);
      card.classList.remove('cardTemplate');
      card.removeAttribute('hidden');
      app.container.appendChild(card);
      app.visibleCards[data.key] = card;
    }

    // Verifies the data provide is newer than what's already visible
    // on the card, if it's not bail, if it is, continue and update the
    // time saved in the card
    var cardLastUpdatedElem = card.querySelector('.card-last-updated');
    var cardLastUpdated = cardLastUpdatedElem.textContent;
    if (cardLastUpdated) {
      cardLastUpdated = new Date(cardLastUpdated);
      // Bail if the card has more recent data then the data
      if (dataLastUpdated.getTime() < cardLastUpdated.getTime()) {
        return;
      }
    }
    cardLastUpdatedElem.textContent = data.created;

    card.querySelector('.title').textContent = title;
    card.querySelector('.author').textContent = author;
    card.querySelector('.pubDate').textContent = pubDate;
    card.querySelector('.link').href = link;
    card.querySelector('.share').addEventListener("click", function() {
      if (navigator.share) {
        alert("share funcionando");
        navigator.share({
            title: title,
            url: link,
          })
          .then(() => alert('Successful share'))
          .catch((error) => alert('Error sharing', error));
      } else {
        alert("Click");
      }
    })

    if (app.isLoading) {
      app.spinner.setAttribute('hidden', true);
      app.container.removeAttribute('hidden');
      app.isLoading = false;
    }
  };
  //On the lack of better init logic...
  app.queryArticles();
})();

@@ -6539,32 +6539,33 @@
 lick%22, function(
+e
 ) %7B%0A      if (na
@@ -6562,66 +6562,83 @@
     
-if (navigator.share) %7B%0A        alert(%22share funcionando%22);
+e.stopPropagation();%0A      e.preventDefault();%0A      if (navigator.share) %7B
 %0A   
@@ -6744,21 +6744,27 @@
 n(() =%3E 
-alert
+console.log
 ('Succes
@@ -6805,21 +6805,27 @@
 ror) =%3E 
-alert
+console.log
 ('Error 
@@ -6847,47 +6847,8 @@
 ));%0A
-      %7D else %7B%0A        alert(%22Click%22);%0A
     

Fix share events