File: src\internal\ui\Survey\component.ts
/// <reference path="../../packages.ts" />
/// <reference path="../../api/modalities/callbackModality.ts" />
/// <reference path="../../telemetry/awa/eventWriter.ts" />
/// <reference path="../../telemetry/telemetry.ts" />
/// <reference path="../../utils/modality.ts" />
/// <reference path="../constants.ts" />
/// <reference path="config.ts" />
/// <reference path="surveyOfferBuilder.ts" />
/// <reference path="surveyResponseBuilder.ts" />
/// <reference path="surveyRequestConfig.ts" />
/// <reference path="surveyModel.ts" />
/// <reference path="surveyConfig.ts" />
/// <reference path="survey.ts" />
/**
* @module UI
* @submodule UI Survey
* @namespace ui
*/
/**
* Survey UI Module
* @class survey
* @static
*/
namespace internal.ui.survey {
const LOAD_TIMEOUT_MS = 15000; // Use this to overwrite the default loaded timeoutÂ
const REPORT_HEIGHT_CHANGE = true; // set to true to report height change events
const DEFAULT_SURVEY_URL = "https://survey.support.services.microsoft.com/viewsurvey.html";
const DEFAULT_VIEW_MODE = "Wizard";
const DEFAULT_SURVEY_PLATFORM_ENVIRONMENT = "Prod";
// Parameters
const SURVEY_SURVEY_ID = "surveyId";
const SURVEY_RESPONSE_ID = "responseId";
const SURVEY_LOCALE_ID = "locale";
const SURVEY_FLIGHT_ID = "flightId";
const SURVEY_PREVIEW = "preview";
const SURVEY_VIEW_MODE = "viewMode";
const SURVEY_PLATFORM_ENVIRONMENT = "platformEnvironment";
const SURVEY_CONTEXT = "context";
const DEFAULT_XFRAME_PROXY_PATH = "xframeproxy.html";
/**
* Factory method for creating a survey instance
* @method createSurvey
* @param {ui.survey.SurveyConfig} config A {{#crossLink "ui.survey.SurveyConfig"}}{{/crossLink}} class to configure this instance and all subsequent survey calls
* @return {ui.survey.Survey} A {{#crossLink "ui.survey.Survey"}}{{/crossLink}} instance
* @example
* var surveyConfig = {
* "surveyId": "integrationsurvey",
* "locale": "en-us"
* };
*
* var survey = window.MsSupportSdk.ui.survey.createSurvey(surveyConfig);
*/
export function createSurvey(config: SurveyConfig): Survey {
return new Survey(config);
}
export class Component extends UIComponent {
surveyConfig: SurveyConfig;
responseId: string;
constructor(surveyConfig: SurveyConfig, config: Config, responseId: string) {
if (!surveyConfig || !config || !(surveyConfig.surveyId && surveyConfig.locale)) {
throw new Error("You must pass a product Id, Survey Id and locale when creating a survey component.");
}
super(config, LOAD_TIMEOUT_MS, REPORT_HEIGHT_CHANGE);
this.surveyConfig = surveyConfig;
this.responseId = responseId;
}
public getComponentUrl(): string {
return getSurveyUrl(this.surveyConfig, <Config>this.config, this.responseId);
}
public getComponentSdk(): ComponentSDK {
return {};
}
// Override this method in the UI component to use a different xframe proxy url if needed
public getXframeProxyUrl(): string {
return `${utils.getOriginFromUrl(this.getComponentUrl())}/${DEFAULT_XFRAME_PROXY_PATH}/`;
}
}
function getSurveyUrl(surveyConfig: SurveyConfig, config: Config, responseId: string): string {
let values: any = {};
if (surveyConfig.surveyId && utils.isString(surveyConfig.surveyId)) {
values[SURVEY_SURVEY_ID] = surveyConfig.surveyId;
}
if (responseId && utils.isString(responseId)) {
values[SURVEY_RESPONSE_ID] = responseId;
}
if (surveyConfig.locale && utils.isString(surveyConfig.locale)) {
values[SURVEY_LOCALE_ID] = surveyConfig.locale;
}
if (surveyConfig.flightId && utils.isString(surveyConfig.flightId)) {
values[SURVEY_FLIGHT_ID] = surveyConfig.flightId;
}
if (config.preview === true) {
values[SURVEY_PREVIEW] = true;
}
// Default to wizard view
let viewModeValue = DEFAULT_VIEW_MODE;
if (config.viewMode && utils.isString(config.viewMode)) {
viewModeValue = config.viewMode;
}
values[SURVEY_VIEW_MODE] = viewModeValue;
let surveyUrl = DEFAULT_SURVEY_URL; // Default to production
if (config.environment && utils.isString(config.environment)) {
surveyUrl = config.environment;
}
values[SURVEY_PLATFORM_ENVIRONMENT] = DEFAULT_SURVEY_PLATFORM_ENVIRONMENT; // Default to production
if (config.surveyPlatformEnvironment && utils.isString(config.surveyPlatformEnvironment)) {
values[SURVEY_PLATFORM_ENVIRONMENT] = config.surveyPlatformEnvironment;
}
if (surveyConfig.partnerContext) {
values[SURVEY_CONTEXT] = JSON.stringify(surveyConfig.partnerContext); // Convert to JSON
}
return utils.appendParams(surveyUrl, values);
}
}