(function ($$)()

in salesforce/canvas/public/javascripts/canvas-all.js [643:866]


(function ($$) {
  var storage = (function () {
    function isLocalStorage() {
      try {
        return 'sessionStorage' in window && window.sessionStorage !== null;
      } catch (e) {
        return false;
      }
    }
    return {
      get: function get(key) {
        if (isLocalStorage()) {
          return sessionStorage.getItem(key);
        }
        return $$.cookies.get(key);
      },
      set: function set(key, value) {
        if (isLocalStorage()) {
          return sessionStorage.setItem(key, value);
        }
        return $$.cookies.set(key, value);
      },
      remove: function remove(key) {
        if (isLocalStorage()) {
          return sessionStorage.removeItem(key);
        }
        return $$.cookies.remove(key);
      },
    };
  })();
  var module = (function () {
    var accessToken, instUrl, instId, tOrigin, childWindow;
    function init() {
      accessToken = storage.get('access_token');
      storage.remove('access_token');
    }
    function query(params) {
      var r = [],
        n;
      if (!$$.isUndefined(params)) {
        for (n in params) {
          if (params.hasOwnProperty(n)) {
            r.push(n + '=' + params[n]);
          }
        }
        return '?' + r.join('&');
      }
      return '';
    }
    function refresh() {
      storage.set('access_token', accessToken);
      self.location.reload();
    }
    function login(ctx) {
      var uri;
      ctx = ctx || {};
      uri = ctx.uri || '/rest/oauth2';
      ctx.params = ctx.params || { state: '' };
      ctx.params.state =
        ctx.params.state || ctx.callback || window.location.pathname;
      ctx.params.display = ctx.params.display || 'popup';
      ctx.params.redirect_uri = $$.startsWithHttp(
        ctx.params.redirect_uri,
        encodeURIComponent(
          window.location.protocol +
            '//' +
            window.location.hostname +
            ':' +
            window.location.port
        ) + ctx.params.redirect_uri
      );
      uri = uri + query(ctx.params);
      console.log('LOGIN URI IS', uri);
      childWindow = window.open(
        uri,
        'OAuth',
        'status=0,toolbar=0,menubar=0,resizable=0,scrollbars=1,top=50,left=50,height=500,width=680'
      );
    }
    function token(t) {
      if (arguments.length === 0) {
        if (!$$.isNil(accessToken)) {
          return accessToken;
        }
      } else {
        accessToken = t;
      }
      return accessToken;
    }
    function instanceUrl(i) {
      if (arguments.length === 0) {
        if (!$$.isNil(instUrl)) {
          return instUrl;
        }
        instUrl = storage.get('instance_url');
      } else {
        if (i === null) {
          storage.remove('instance_url');
          instUrl = null;
        } else {
          storage.set('instance_url', i);
          instUrl = i;
        }
      }
      return instUrl;
    }
    function parseHash(hash) {
      var i, nv, nvp, n, v;
      if (!$$.isNil(hash)) {
        if (hash.indexOf('#') === 0) {
          hash = hash.substr(1);
        }
        nvp = hash.split('&');
        for (i = 0; i < nvp.length; i += 1) {
          nv = nvp[i].split('=');
          n = nv[0];
          v = decodeURIComponent(nv[1]);
          if ('access_token' === n) {
            token(v);
          } else {
            if ('instance_url' === n) {
              instanceUrl(v);
            } else {
              if ('target_origin' === n) {
                tOrigin = decodeURIComponent(v);
              } else {
                if ('instance_id' === n) {
                  instId = v;
                }
              }
            }
          }
        }
      }
    }
    function checkChildWindowStatus() {
      if (!childWindow || childWindow.closed) {
        refresh();
      }
    }
    function childWindowUnloadNotification(hash) {
      var retry = 0,
        maxretries = 10;
      function cws() {
        retry++;
        if (!childWindow || childWindow.closed) {
          refresh();
        } else {
          if (retry < maxretries) {
            setTimeout(cws, 50);
          }
        }
      }
      parseHash(hash);
      setTimeout(cws, 50);
    }
    function logout() {
      token(null);
    }
    function loggedin() {
      return !$$.isNil(token());
    }
    function loginUrl() {
      var i,
        nvs,
        nv,
        q = self.location.search;
      if (q) {
        q = q.substring(1);
        nvs = q.split('&');
        for (i = 0; i < nvs.length; i += 1) {
          nv = nvs[i].split('=');
          if ('loginUrl' === nv[0]) {
            return decodeURIComponent(nv[1]) + '/services/oauth2/authorize';
          }
        }
      }
      return 'https://login.salesforce.com/services/oauth2/authorize';
    }
    function targetOrigin(to) {
      if (!$$.isNil(to)) {
        tOrigin = to;
        return to;
      }
      if (!$$.isNil(tOrigin)) {
        return tOrigin;
      }
      parseHash(document.location.hash);
      return tOrigin;
    }
    function instanceId(id) {
      if (!$$.isNil(id)) {
        instId = id;
        return id;
      }
      if (!$$.isNil(instId)) {
        return instId;
      }
      parseHash(document.location.hash);
      return instId;
    }
    function client() {
      return {
        oauthToken: token(),
        instanceId: instanceId(),
        targetOrigin: targetOrigin(),
      };
    }
    return {
      init: init,
      login: login,
      logout: logout,
      loggedin: loggedin,
      loginUrl: loginUrl,
      token: token,
      instance: instanceUrl,
      client: client,
      checkChildWindowStatus: checkChildWindowStatus,
      childWindowUnloadNotification: childWindowUnloadNotification,
    };
  })();
  $$.module('Sfdc.canvas.oauth', module);
  $$.oauth.init();
})(Sfdc.canvas);