/*
 * Copyright (c) Perforce Software, Inc. 1997-2016
 * Licensed Materials - All Rights Reserved.
 */

/*
 * By default this example is accessing all the resources locally
 * (localhost). In this case, the two iframes will reference the
 * same Diagrammer JSF component (same session).
 * 
 * NOTE: How to simulate complete client isolation.
 * One way of simulating component isolation using one single servlet 
 * container is as follows:
 * 
 * 1. Update your 'hosts' file to include 'fake' domains
 * Check "http://en.wikipedia.org/wiki/Hosts_file" for details on where 
 * to locate the 'hosts' file on your platform.
 * Append the following entries to the file:
 *   127.0.0.1  mainApp.domain0.com
 *   127.0.0.1  client1.domain1.com
 *   127.0.0.1  client2.domain2.com
 * 
 * 2. Modify this file (managedhub.js) and update the appropriate 
 * locations in order to set the variables 'subdomain1' and 'subdomain2' 
 * accordingly:
 *   var subdomain1 = "client1.domain1.com:8080";
 *   var subdomain2 = "client2.domain2.com:8080";
 * 
 * 3. Launch the Tomcat servlet container and ensure that the domain
 * redirection is working properly by opening the following URIs:
 *   http://client1.domain1.com/faces/diagrammer.jsp
 *   http://client2.domain2.com/faces/diagrammer.jsp
 * 
 * 4. Open the application using the following URI:
 *   http://mainApp.domain0.com/managedhub.html
 * 
 * To the browser, client1.domain1.com and client2.domain2.com are two
 * distinct servers. OpenAjax IframeContainer is transparently tunneling 
 * the communication between iframes.
 */

/* 
 * ManagerHub Security Callbacks 
 */
function onMHPublish(topic, data, publishContainer, subscribeContainer) {
  /* Callback for publish requests. This example approves all publish requests. */
  return true;
}
function onMHSubscribe(topic, container) {
  /* Callback for subscribe requests. This example approves all subscribe requests. */ 
  return true;
}
function onMHUnsubscribe(topic, container) {
  /* Callback for unsubscribe requests. This example approves all subscribe requests. */ 
  return true;
}
function onMHSecurityAlert(source, alertType) {
  /* Callback for security alerts */
}

/* 
 * Application initializes in response to document load event 
 */
function loadEventHandler() {
  var mashupArea = document.getElementById("mashupArea");

  /* 
   * Create a ManagedHub instance 
   */
  var managedHub = new OpenAjax.hub.ManagedHub(
    { 
      onPublish:       onMHPublish,
      onSubscribe:     onMHSubscribe,
      onUnsubscribe:   onMHUnsubscribe,
      onSecurityAlert: onMHSecurityAlert 
    }
  );

  /*
   * Create IframeContainer for 'client1'
   * 
   * NOTE: In order to completely isolate the IframeContainer ('client1')
   * from the manager and from all other client applications, the 'uri'
   * parameter of the 'container1' must have a different subdomain from
   * the main application and any other clients. 
   * For simplicity, this example is initially setting the main and client 
   * applications URI to the same server 'localhost' (window.location.host)
   * See note at the top of this file.
   */
  createIframeContainer(window.location.host, 'client1', managedHub);

  /*
   * Create IframeContainer for 'client2'
   * 
   * NOTE: In order to completely isolate the IframeContainer ('client2')
   * from the manager and from all other client applications, the 'uri'
   * parameter of the 'container2' must have a different subdomain from
   * the main application and any other clients. 
   * For simplicity, this example is initially setting the main and client 
   * applications URI to the same server 'localhost' (window.location.host)
   * See note at the top of this file.
   */
  createIframeContainer(window.location.host, 'client2', managedHub);
}


/*
 * Function to create an IframeContainer for a client.
 * 
 * Parameter 'subdomain' : identifies a remote servlet container
 * Parameter 'name'      : identifies the IframeContainer
 * Parameter 'managedHub': the managed hub where the container is created
 */
function createIframeContainer(subdomain, name, managedHub) {
  var mashupArea = document.getElementById("mashupArea");
  var div = document.createElement( "div" );
  mashupArea.appendChild(div);

  /* 
   * ClientHub Security Callbacks
   */
  function onClientSecurityAlert(source, alertType) {
    /* Handle client-side security alerts */  
  }
  function onClientConnect(container) {
    /* Called when client connects */   
  }
  function onClientDisconnect(container) {
    /* Called when client disconnects */ 
  }
  
  /*
   * Compute the URI for the iframe tunnel, required by the
   * IframeContainer to handle same origin policy restrictions.
   * 
   * The tunnel URI must be from the same origin as the page
   * which instantiates the IframeContainer.
   */
  var hostURI = "http://" 
                + window.location.host + "/"
                + window.location.pathname.split("/")[1];

  var container = new OpenAjax.hub.IframeContainer(managedHub, name,
    {
      Container: {
        onSecurityAlert: onClientSecurityAlert,
        onConnect:       onClientConnect,
        onDisconnect:    onClientDisconnect
      },
      IframeContainer: {
        // DOM element that is parent of this container:
        parent:      div, 
        // Container's iframe will have these CSS styles:
        iframeAttrs: { style: { border:"black solid 1px", width:"100%", height:"480px" }},
        // Container's iframe loads the following URL:
        uri:         "http://" + subdomain + "/jsf-diagrammer-oah/faces/" + name + ".jsp",
        // Tunnel URL required by IframeHubClient. This particular tunnel URL 
        // is the one that corresponds to release/all/OpenAjaxManagedHub-all.js:
        tunnelURI: hostURI + "/data/openajax/all/tunnel.html"
      }
    }
  );
}