daily pastebin goal
36%
SHARE
TWEET

Untitled

a guest Apr 21st, 2017 54 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2.  * Created by maynagashev on 21.04.17.
  3.  */
  4.  
  5. /**
  6.  * Shared Object for storing current Active Filter settings, used in:
  7.  * @components: moments-list, filter-bar, load-more
  8.  *
  9.  * + historyAPI (write and read new status)
  10.  *
  11.  * Initialization in moments-list's "created" hook
  12.  */
  13.  
  14. (function() {
  15.     var log = window.log.getLogger('moments:so-active-filter'); log.setDefaultLevel('debug');
  16.  
  17.     window.activeFilterSharedObject = {
  18.  
  19.         data: {
  20.             category: '',
  21.             realm:    '',
  22.             cam:      ''
  23.         },
  24.  
  25.         set:  function (filter, resetOthers, disableHistory) {
  26.             var self = window.activeFilterSharedObject, key;
  27.             for (key in self.data) {
  28.                 self.data[key] = (filter.hasOwnProperty(key)) ? filter[key] : ((resetOthers) ? '' : self.data[key]);
  29.             }
  30.             if (!disableHistory) {
  31.                 var history_url = self.updateHistory(self.data);
  32.                 log.debug("[history] set url", history_url);
  33.             }
  34.         },
  35.  
  36.         get:  function () {
  37.             var self = window.activeFilterSharedObject;
  38.             return self.data;
  39.         },
  40.  
  41.         /*
  42.          History API (browser):
  43.          1) update history when filter updated
  44.          2) fetch active filter from document.location when state changes
  45.          */
  46.  
  47.         updateHistory: function (filters) {
  48.  
  49.             function buildUrl(filters) {
  50.                 var url = '/moments';
  51.  
  52.                 filters = _.pick(filters, _.identity);
  53.  
  54.                 if (_.has(filters, 'realm')) {
  55.                     url += '/' + filters.realm;
  56.  
  57.                     if (_.has(filters, 'cam')) {
  58.                         url += '/' + filters.cam;
  59.                     }
  60.                 }
  61.  
  62.                 if (_.has(filters, 'category')) {
  63.                     url += '?category=' + filters.category;
  64.                 }
  65.  
  66.                 return url;
  67.             };
  68.  
  69.             var url = buildUrl(filters);
  70.             history.pushState(null, null, url);
  71.             return url;
  72.         },
  73.  
  74.         fetchFromLocation: function () {
  75.             var self = window.activeFilterSharedObject;
  76.             var url  = document.location.href;
  77.  
  78.             log.debug("[history] changed location to: " + url);
  79.  
  80.             var filter = {
  81.                 category: '',
  82.                 realm:    '',
  83.                 cam:      ''
  84.             }, m;
  85.  
  86.             if (m = url.match(/(realm[0-9]+)/i))
  87.                 filter.realm = m[1];
  88.             if (m = url.match(/(cam[0-9]+)/i))
  89.                 filter.cam = m[1];
  90.             if (m = url.match(/category=([a-z0-9_\-]+)/i))
  91.                 filter.category = m[1];
  92.  
  93.             log.debug("[history] loaded filter from URL:", filter);
  94.             self.set(filter, true, true);
  95.         }
  96.     };
  97.  
  98.     // Event handler when user navigates through Back/Forward history buttons
  99.     window.onpopstate = function () {
  100.         window.activeFilterSharedObject.fetchFromLocation();
  101.     };
  102.  
  103. })();
RAW Paste Data
Top