github.com/justinjmoses/evergreen@v0.0.0-20170530173719-1d50e381ff0d/public/static/js/services/location_hash.js (about)

     1  var mciServices = mciServices || {};
     2  
     3  mciServices.locationHash = angular.module('mciServices.locationHash', []);
     4  
     5  /* Wrapper around location hash for MCI. Angular's $location has some
     6   * weirdnesses and doesn't map quite well to how we use hashes. */
     7  mciServices.locationHash.factory('$locationHash', function($window) {
     8    var locationHash = {
     9      get : function() {
    10        var hash = $window.location.hash.substr(1); // Get rid of leading '#'
    11        if (hash.charAt(0) == '/') {
    12          hash = hash.substr(1);
    13        }
    14  
    15        // If hash doesn't have any key-value pairs, return empty
    16        if (hash.length == 0 || hash.indexOf('=') == -1) {
    17          return {};
    18        }
    19  
    20        // First split by '&', then by '=', then decodeURI on each resulting
    21        // string
    22        var keyValuePairs = _.map(hash.split('&'), function(str) {
    23          var pair = _.map(str.split('='), decodeURIComponent);
    24          return pair;
    25        });
    26  
    27        var ret = {};
    28        _.each(keyValuePairs, function(pair) {
    29          ret[pair[0]] = pair[1];
    30        });
    31  
    32        return ret;
    33      },
    34      set : function(obj) {
    35        var str = '';
    36        // { a : 1, b : 'hello world' } => "a=1&b=hello%20world"
    37        _.each(obj, function(value, key) {
    38          if (str.length > 0) {
    39            str += '&';
    40          }
    41  
    42          str += encodeURIComponent(key) + '=' + encodeURIComponent(value);
    43        });
    44  
    45        $window.location.hash = str;
    46      }
    47    };
    48  
    49    return locationHash;
    50  });