Post Message API

Setting up

Setting up the Xpertdoc Portal iframe, the current examples looks for a template called "Dev Template" on http://portal.xpertdoc.com. Refer to Querying Data in order to change the template found.

var PortalMessageApi = (function () {
    function PortalMessageApi(iframeElement) {
        this.iframeElement = iframeElement;
        this.callbackArray = Array();
        var eventMethod = window.addEventListener ? "addEventListener" : "attachEvent";
        var eventer = window[eventMethod];
        var messageEvent = eventMethod === "attachEvent" ? "onmessage" : "message";
        eventer(messageEvent, function (e) {
            if (e.source != window) {
            }
        }, false);
    }
    PortalMessageApi.prototype.registerEventListener = function (eventName, callback) {
        if (eventName == "FieldValueChanged" && this.callbackArray.indexOf(callback) < 0) {
            this.callbackArray.push(callback);
        }
    };
    PortalMessageApi.prototype.postMessage = function (eventName, message) {
        this.iframeElement.contentWindow.postMessage(JSON.stringify({ eventName: eventName, message: message }), "*");
    };
    return PortalMessageApi;
}());
var MessageApi = (function () {
  function MessageApi() {
  }
  
  MessageApi.run = function () {
    $.ajax({
      url: encodeURI("http://portal.xpertdoc.com/odata/Templates?$filter=Name eq 'Dev Template'"),
      dataType: "json",
      success: function (templates) {
        var frameSrc = "http://portal.xpertdoc.com/TemplateManager/Template/Execute/" + templates.value[0].TemplateId + "?parentId=" + templates.value[0].TemplateGroupId + "&framed=true";
        $("#portalFrame").attr("src", frameSrc);
      },
      
      error: function (jqXHR, textStatus, errorThrown) {
        alert("An unknown error occurred while trying to frame the portal: " + errorThrown);
      }
    });
    
    MessageApi.registerEventListener();
    MessageApi.registerCommands();
  };
  
  // Register the Event Listener
 	MessageApi.registerEventListener = function () {
    var eventMethod = window.addEventListener ? "addEventListener" : "attachEvent";
    var eventer = window[eventMethod];
    var messageEvent = eventMethod === "attachEvent" ? "onmessage" : "message";
  };
  
  // Register the commands
  MessageApi.registerCommands = function () {
    var postMessage = function (message) {
      $("#portalFrame")[0].contentWindow.postMessage(JSON.stringify(message), "*");
    };
    
    //	Examples
    
    // Once the GetFieldsButton is pressed, all Fields in the Xpertdoc SmartForm will be
    // returned
    $("#getFieldsButton").click(function () {
      postMessage({ eventName: "GetFields" });
    });
    
    // Once the SetFieldValueButton is pressed, changes the value of the targeted Field
    // in the Xpertdoc SmartForm
    $("#setFieldValueButton").click(function () {
      var fieldName = $("#fieldNameText").val();
      var fieldValue = $("#fieldValueText").val();
      postMessage({ eventName: "SetFieldValue", message: { fieldName: fieldName, value: fieldValue } });
    });
  };

  return MessageApi;
}());

$(function () {
  MessageApi.run();
});