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

function clientSecurityAlertHandler( source, alertType ) {
  // Handle security alerts
}

/* Callback that is invoked upon successful connection to the Managed Hub */
function connectCompleted( hubClient, success, error ) {
  if (success) {
    /* Call hubClient.publish(...) to publish messages  */
    /* Call hubClient.subscribe(...) to subscribe to message topics */
    /* Subscribe to 'com.ibm.ilog.samples.topic.objectid' */
    hubClient.subscribe('com.ibm.ilog.samples.topic.objectid', onData);
  }
}

/* Application initializes in response to document load event */
function loadEventHandler() {
  hubClient = new OpenAjax.hub.IframeHubClient({
    HubClient: {
      onSecurityAlert: clientSecurityAlertHandler
    }
  });

  // Connect to the ManagedHub
  hubClient.connect( connectCompleted );
}

/* Ensure that no cyclic messages happen */
function discardData(data) {
  // Cyclic notifications are prevented by storing the currently 
  // selected object identifier inside 'tableId' element
  return document.getElementById('tableId').currentId != data;
}

/* Callback invoked when a message is published at a given topic */
function onData( topic, publisherData, subscriberData ) {
  // Message is published as an Object containing two properties:
  // * payload (the selected object identifier)
  // * hostname (identifies the originator of the message)
  if (publisherData && 
      publisherData.hostname != window.location.host+hubClient.getClientID() && 
      discardData(publisherData.payload)) {
    diagrammer2.performAction("doSelectObject", new Array( publisherData.payload ), false);
  }
}

/* Callback invoked when an object is selected */
function displaySelection( selection ) {
  try {
    var tableId = "";
    var newObjId; 
    if (selection.length == 1) {
      newObjId = selection[0].id;
      tableId = "<table class='psheet'>";
      var props = selection[0].getObjectProperties();
      for (var i in props){
        tableId += "<tr>"
        tableId += "<td class='psheet-cell'>" + i+ "</td>";
        tableId += "<td class='psheet-cell'>" + props[i]+ "</td>";
        tableId += "</tr>";
      }                       
      tableId += "</table>";
    } else {
      newObjId = 'null';
    }
    var span = document.getElementById('tableId');
    span.innerHTML = tableId;
    // Prevent cyclic notifications by storing the currently
    // selected object identifier
    span.currentId = newObjId;
    publishMessage(newObjId);
  } catch (e) {
    alert("An error occurred during property sheet refresh\n"+e)
  }
}

/* Method invoked to publish a message through OpenAjax */
function publishMessage( objectId ) {
  // Message is published as an Object containing two properties:
  // * payload (the selected object identifier)
  // * hostname (identifies the originator of the message)
  if (hubClient.isConnected()) {
    var data = { payload: objectId, hostname: window.location.host+hubClient.getClientID() }; 
    hubClient.publish( 'com.ibm.ilog.samples.topic.objectid', data );
  }
}