Sha256: 253f05cd2004140e63c73a204baa4a280bb92aa01d8d5f2f09c0c7873751732d

Contents?: true

Size: 1.79 KB

Versions: 39

Compression:

Stored size: 1.79 KB

Contents

pageflow.Visited = function(entryId, pages, events, cookies) {
  var cookieName = '_pageflow_visited';

  var unvisitedPages = [];

  function init() {
    pageflow.cookieNotice.request();

    if (!cookies.hasItem(cookieName)) {
      storeVisitedPageIds(getAllIds());
    }
    else {
      var visitedIds = getVisitedPageIds();
      unvisitedPages = _.difference(getAllIds(), visitedIds);
    }

    events.on('page:change', function (page) {
      var id = page.getPermaId();
      var ids = getVisitedPageIds();

      if (ids.indexOf(id) < 0) {
        ids.push(id);
      }

      storeVisitedPageIds(ids);
    });
  }

  function migrateLegacyCookie() {
    var legacyCookieName = '_pageflow_' + entryId + '_visited';

    if (cookies.hasItem(legacyCookieName)) {
      var ids = getCookieIds(legacyCookieName);
      storeVisitedPageIds(_.uniq(ids));

      cookies.removeItem(legacyCookieName);
    }
  }

  function getAllIds() {
    return pages.map(function(page) {
      return page.perma_id;
    });
  }

  function storeVisitedPageIds(ids) {
    cookies.setItem(cookieName, ids, Infinity, location.pathname);
  }

  function getVisitedPageIds() {
    return getCookieIds(cookieName);
  }

  function getCookieIds(name) {
    if (cookies.hasItem(name) && !!cookies.getItem(name)) {
      return cookies.getItem(name).split(',').map(function(id) {
        return parseInt(id, 10);
      });
    }
    return [];
  }

  return {
    init: function() {
      migrateLegacyCookie();
      init();
    },

    getUnvisitedPages: function() {
      return unvisitedPages;
    }
  };
};

pageflow.Visited.setup = function() {
  pageflow.visited = new pageflow.Visited(
    pageflow.entryId,
    pageflow.pages,
    pageflow.events,
    pageflow.cookies
  );

  if (pageflow.Visited.enabled) {
    pageflow.visited.init();
  }
};

Version data entries

39 entries across 39 versions & 1 rubygems

Version Path
pageflow-14.5.2 app/assets/javascripts/pageflow/visited.js
pageflow-15.0.2 app/assets/javascripts/pageflow/visited.js
pageflow-15.0.1 app/assets/javascripts/pageflow/visited.js
pageflow-15.0.0 app/assets/javascripts/pageflow/visited.js
pageflow-14.5.1 app/assets/javascripts/pageflow/visited.js
pageflow-14.5.0 app/assets/javascripts/pageflow/visited.js
pageflow-15.0.0.rc2 app/assets/javascripts/pageflow/visited.js
pageflow-15.0.0.rc1 app/assets/javascripts/pageflow/visited.js
pageflow-15.0.0.beta4 app/assets/javascripts/pageflow/visited.js
pageflow-15.0.0.beta3 app/assets/javascripts/pageflow/visited.js
pageflow-14.4.0 app/assets/javascripts/pageflow/visited.js
pageflow-15.0.0.beta2 app/assets/javascripts/pageflow/visited.js
pageflow-15.0.0.beta1 app/assets/javascripts/pageflow/visited.js
pageflow-14.3.0 app/assets/javascripts/pageflow/visited.js
pageflow-14.2.1 app/assets/javascripts/pageflow/visited.js
pageflow-14.2.0 app/assets/javascripts/pageflow/visited.js
pageflow-14.1.1 app/assets/javascripts/pageflow/visited.js
pageflow-14.1.0 app/assets/javascripts/pageflow/visited.js
pageflow-14.0.0 app/assets/javascripts/pageflow/visited.js
pageflow-14.0.0.rc2 app/assets/javascripts/pageflow/visited.js