"use strict";

var _ = require('lodash'),
    iframePhone = require('iframe-phone'),
    EMBED_FLAG = 'itsi_embed',
    QUERY_SUFFIX = EMBED_FLAG + '=true';

var ItsiEmbed = function() {
    this.phone = new iframePhone.getIFrameEndpoint();

    this.url = window.location.href + '?' + QUERY_SUFFIX;

    this.setLearnerUrl = function(url) {
        if (url) {
            this.url = url + (url.indexOf('?') > 0 ? '&' : '?') + QUERY_SUFFIX;
        }

        this.phone.post('setLearnerUrl', this.url);
    };

    this.phone.addListener('getLearnerUrl', _.bind(this.setLearnerUrl, this));
    this.phone.initialize();
};

module.exports = {
    ItsiEmbed: ItsiEmbed
};

"use strict";

var _ = require('lodash'),
    iframePhone = require('iframe-phone'),
    EMBED_FLAG = 'itsi_embed',
    QUERY_SUFFIX = EMBED_FLAG + '=true';

var ItsiEmbed = function() {
    this.phone = new iframePhone.getIFrameEndpoint();

    this.url = window.location.href + '?' + QUERY_SUFFIX;

    this.interactiveState = { url: this.url };

    this.setLearnerUrl = function(url) {
        if (url) {
            this.url = url + (url.indexOf('?') > 0 ? '&' : '?') + QUERY_SUFFIX;
            this.interactiveState = { url: this.url };
        }

        this.phone.post('setLearnerUrl', this.url);
        this.phone.post('interactiveState', this.interactiveState);
    };

    this.phone.addListener('getLearnerUrl', _.bind(this.setLearnerUrl, this));
    this.phone.addListener('getInteractiveState', _.bind(this.setLearnerUrl, this));
    this.phone.initialize();
};

module.exports = {
    ItsiEmbed: ItsiEmbed
};

Save Interactive State in addition to URL
Save Interactive State in addition to URL

Since calling `setLearnerUrl` does not alter the app behavior, we also
call `interactiveState` and post the current URL in a JSON blob there.
This does get saved on their end, and allows us to devise a redirection
mechanism to this saved URL when the user comes back.
