(function ($$)()

in salesforce/canvas/public/javascripts/canvas-all.js [956:1380]


(function ($$) {
  var pversion,
    cversion = '41.0';
  var module = (function () {
    var purl;
    function getTargetOrigin(to) {
      var h;
      if (to === '*') {
        return to;
      }
      if (!$$.isNil(to)) {
        h = $$.stripUrl(to);
        purl = $$.startsWithHttp(h, purl);
        if (purl) {
          return purl;
        }
      }
      h = $$.document().location.hash;
      if (h) {
        h = decodeURIComponent(h.replace(/^#/, ''));
        purl = $$.startsWithHttp(h, purl);
      }
      return purl;
    }
    function xdCallback(data) {
      if (data) {
        if (submodules[data.type]) {
          submodules[data.type].callback(data);
        }
      }
    }
    var submodules = (function () {
      var cbs = [],
        seq = 0,
        autog = true;
      function postit(clientscb, message) {
        var wrapped, to, c;
        seq = seq > 100 ? 0 : seq + 1;
        cbs[seq] = clientscb;
        wrapped = {
          seq: seq,
          src: 'client',
          clientVersion: cversion,
          parentVersion: pversion,
          body: message,
        };
        c = message && message.config && message.config.client;
        to = getTargetOrigin($$.isNil(c) ? null : c.targetOrigin);
        if ($$.isNil(to)) {
          throw 'ERROR: targetOrigin was not supplied and was not found on the hash tag, this can result from a redirect or link to another page.';
        }
        $$.console.log('posting message ', { message: wrapped, to: to });
        $$.xd.post(wrapped, to, parent);
      }
      function validateClient(client, cb) {
        var msg;
        client = client || ($$.oauth && $$.oauth.client());
        if ($$.isNil(client) || $$.isNil(client.oauthToken)) {
          msg = {
            status: 401,
            statusText: 'Unauthorized',
            parentVersion: pversion,
            payload: 'client or client.oauthToken not supplied',
          };
        }
        if ($$.isNil(client.instanceId) || $$.isNil(client.targetOrigin)) {
          msg = {
            status: 400,
            statusText: 'Bad Request',
            parentVersion: pversion,
            payload: 'client.instanceId or client.targetOrigin not supplied',
          };
        }
        if (!$$.isNil(msg)) {
          if ($$.isFunction(cb)) {
            cb(msg);
            return false;
          } else {
            throw msg;
          }
        }
        return true;
      }
      var event = (function () {
        var subscriptions = {},
          STR_EVT = 'sfdc.streamingapi';
        function validName(name, res) {
          var msg,
            r = $$.validEventName(name, res);
          if (r !== 0) {
            msg = {
              1: 'Event names can only contain one namespace',
              2: 'Namespace has already been reserved',
              3: 'Event name contains invalid characters',
            };
            throw msg[r];
          }
        }
        function findSubscription(event) {
          var s,
            name = event.name;
          if (name === STR_EVT) {
            if (!$$.isNil(subscriptions[name])) {
              s = subscriptions[name][event.params.topic];
            }
          } else {
            s = subscriptions[name];
          }
          if (
            !$$.isNil(s) &&
            ($$.isFunction(s.onData) || $$.isFunction(s.onComplete))
          ) {
            return s;
          }
          return null;
        }
        return {
          callback: function (data) {
            var event = data.payload,
              subscription = findSubscription(event),
              func;
            if (!$$.isNil(subscription)) {
              if (event.method === 'onData') {
                func = subscription.onData;
              } else {
                if (event.method === 'onComplete') {
                  func = subscription.onComplete;
                }
              }
              if (!$$.isNil(func) && $$.isFunction(func)) {
                func(event.payload);
              }
            }
          },
          subscribe: function (client, s) {
            var subs = {};
            if ($$.isNil(s) || !validateClient(client)) {
              throw 'precondition fail';
            }
            $$.each($$.isArray(s) ? s : [s], function (v) {
              if (!$$.isNil(v.name)) {
                validName(v.name, ['canvas', 'sfdc']);
                if (v.name === STR_EVT) {
                  if (!$$.isNil(v.params) && !$$.isNil(v.params.topic)) {
                    if ($$.isNil(subscriptions[v.name])) {
                      subscriptions[v.name] = {};
                    }
                    subscriptions[v.name][v.params.topic] = v;
                  } else {
                    throw '[' + STR_EVT + '] topic is missing';
                  }
                } else {
                  subscriptions[v.name] = v;
                }
                subs[v.name] = { params: v.params };
              } else {
                throw "subscription does not have a 'name'";
              }
            });
            if (!client.isVF) {
              postit(null, {
                type: 'subscribe',
                config: { client: client },
                subscriptions: subs,
              });
            }
          },
          unsubscribe: function (client, s) {
            var subs = {};
            if ($$.isNil(s) || !validateClient(client)) {
              throw 'PRECONDITION FAIL: need fo supply client and event name';
            }
            if ($$.isString(s)) {
              subs[s] = {};
              delete subscriptions[s];
            } else {
              $$.each($$.isArray(s) ? s : [s], function (v) {
                var name = v.name ? v.name : v;
                validName(name, ['canvas', 'sfdc']);
                subs[name] = { params: v.params };
                if (name === STR_EVT) {
                  if (!$$.isNil(subscriptions[name])) {
                    if (!$$.isNil(subscriptions[name][v.params.topic])) {
                      delete subscriptions[name][v.params.topic];
                    }
                    if ($$.size(subscriptions[name]) <= 0) {
                      delete subscriptions[name];
                    }
                  }
                } else {
                  delete subscriptions[name];
                }
              });
            }
            if (!client.isVF) {
              postit(null, {
                type: 'unsubscribe',
                config: { client: client },
                subscriptions: subs,
              });
            }
          },
          publish: function (client, e) {
            if (!$$.isNil(e) && !$$.isNil(e.name)) {
              validName(e.name, ['s1']);
              if (validateClient(client)) {
                postit(null, {
                  type: 'publish',
                  config: { client: client },
                  event: e,
                });
              }
            }
          },
        };
      })();
      var callback = (function () {
        return {
          callback: function (data) {
            if (
              data.status === 401 &&
              $$.isArray(data.payload) &&
              data.payload[0].errorCode &&
              data.payload[0].errorCode === 'INVALID_SESSION_ID'
            ) {
              if ($$.oauth) {
                $$.oauth.logout();
              }
            }
            if ($$.isFunction(cbs[data.seq])) {
              if (!$$.isFunction(cbs[data.seq])) {
                alert('not function');
              }
              cbs[data.seq](data);
            } else {
            }
          },
        };
      })();
      var services = (function () {
        var sr;
        return {
          ajax: function (url, settings) {
            var ccb, config, defaults;
            if (!url) {
              throw 'PRECONDITION ERROR: url required with AJAX call';
            }
            if (!settings || !$$.isFunction(settings.success)) {
              throw "PRECONDITION ERROR: function: 'settings.success' missing.";
            }
            if (!validateClient(settings.client, settings.success)) {
              return;
            }
            ccb = settings.success;
            defaults = {
              method: 'GET',
              async: true,
              contentType: 'application/json',
              headers: {
                Authorization: 'OAuth ' + settings.client.oauthToken,
                Accept: 'application/json',
              },
              data: null,
            };
            config = $$.extend(defaults, settings || {});
            config.success = undefined;
            config.failure = undefined;
            if (config.client.targetOrigin === '*') {
              config.client.targetOrigin = null;
            } else {
              purl = $$.startsWithHttp(config.targetOrigin, purl);
            }
            postit(ccb, { type: 'ajax', url: url, config: config });
          },
          ctx: function (clientscb, client) {
            if (validateClient(client, clientscb)) {
              postit(clientscb, {
                type: 'ctx',
                accessToken: client.oauthToken,
                config: { client: client },
              });
            }
          },
          token: function (t) {
            return $$.oauth && $$.oauth.token(t);
          },
          version: function () {
            return { clientVersion: cversion, parentVersion: pversion };
          },
          signedrequest: function (s) {
            if (arguments.length > 0) {
              sr = s;
            }
            return sr;
          },
          refreshSignedRequest: function (clientscb) {
            var id = window.name.substring('canvas-frame-'.length),
              client = {
                oauthToken: 'null',
                instanceId: id,
                targetOrigin: '*',
              };
            postit(clientscb, {
              type: 'refresh',
              accessToken: client.oauthToken,
              config: { client: client },
            });
          },
          repost: function (refresh) {
            var id = window.name.substring('canvas-frame-'.length),
              client = {
                oauthToken: 'null',
                instanceId: id,
                targetOrigin: '*',
              },
              r = refresh || false;
            postit(null, {
              type: 'repost',
              accessToken: client.oauthToken,
              config: { client: client },
              refresh: r,
            });
          },
        };
      })();
      var frame = (function () {
        return {
          size: function () {
            var docElement = $$.document().documentElement;
            var contentHeight = docElement.scrollHeight,
              pageHeight = docElement.clientHeight,
              scrollTop =
                (docElement && docElement.scrollTop) ||
                $$.document().body.scrollTop,
              contentWidth = docElement.scrollWidth,
              pageWidth = docElement.clientWidth,
              scrollLeft =
                (docElement && docElement.scrollLeft) ||
                $$.document().body.scrollLeft;
            return {
              heights: {
                contentHeight: contentHeight,
                pageHeight: pageHeight,
                scrollTop: scrollTop,
              },
              widths: {
                contentWidth: contentWidth,
                pageWidth: pageWidth,
                scrollLeft: scrollLeft,
              },
            };
          },
          resize: function (client, size) {
            var sh,
              ch,
              sw,
              cw,
              s = { height: '', width: '' },
              docElement = $$.document().documentElement;
            if ($$.isNil(size)) {
              sh = docElement.scrollHeight;
              ch = docElement.clientHeight;
              if (ch !== sh) {
                s.height = sh + 'px';
              }
              sw = docElement.scrollWidth;
              cw = docElement.clientWidth;
              if (sw !== cw) {
                s.width = sw + 'px';
              }
            } else {
              if (!$$.isNil(size.height)) {
                s.height = size.height;
              }
              if (!$$.isNil(size.width)) {
                s.width = size.width;
              }
            }
            if (!$$.isNil(s.height) || !$$.isNil(s.width)) {
              postit(null, {
                type: 'resize',
                config: { client: client },
                size: s,
              });
            }
          },
          autogrow: function (client, b, interval) {
            var ival = $$.isNil(interval) ? 300 : interval;
            autog = $$.isNil(b) ? true : b;
            if (autog === false) {
              return;
            }
            setTimeout(function () {
              submodules.frame.resize(client);
              submodules.frame.autogrow(client, autog);
            }, ival);
          },
        };
      })();
      return {
        services: services,
        frame: frame,
        event: event,
        callback: callback,
      };
    })();
    $$.xd.receive(xdCallback, getTargetOrigin);
    return {
      ctx: submodules.services.ctx,
      ajax: submodules.services.ajax,
      token: submodules.services.token,
      version: submodules.services.version,
      resize: submodules.frame.resize,
      size: submodules.frame.size,
      autogrow: submodules.frame.autogrow,
      subscribe: submodules.event.subscribe,
      unsubscribe: submodules.event.unsubscribe,
      publish: submodules.event.publish,
      signedrequest: submodules.services.signedrequest,
      refreshSignedRequest: submodules.services.refreshSignedRequest,
      repost: submodules.services.repost,
    };
  })();
  $$.module('Sfdc.canvas.client', module);
})(Sfdc.canvas);