<!--Content built using /OpenForum/Javascript/PageBuilder/default-page.builder.js 0.0.7 Gloomy November builder and renderer DefaultRenderer v1.08 2021 Covid release-->
<h3>OFX</h3>

<p>OFX wraps ajax calls into a fluent style interface.</p>

<hr/>
<h3>Get examples</h3>

<!--Extension ScriptClip script="OFX.get(url).onSuccess(fn).go();" --><div class='row'><div class='columns large-10'><textarea style='border: none; background-color: black; color: lightgreen; font-family: monospace;'  id='5C810296' rows='2' >OFX.get(url).onSuccess(fn).go();</textarea></div><div class='columns large-2'><a href='#' title='Copy to clipboard' onClick='OpenForum.copyElement("5C810296"); return false;'><img src='/OpenForum/Images/icons/png/page_copy.png' /></a></div></div><script>document.getElementById('5C810296').setAttribute('readonly', 'readonly');</script><!--End Extension ScriptClip script="OFX.get(url).onSuccess(fn).go();" -->
<!--Extension ScriptClip script="OFX.get(url).withAction(action).go();" --><div class='row'><div class='columns large-10'><textarea style='border: none; background-color: black; color: lightgreen; font-family: monospace;'  id='CB7E20FD' rows='2' >OFX.get(url).withAction(action).go();</textarea></div><div class='columns large-2'><a href='#' title='Copy to clipboard' onClick='OpenForum.copyElement("CB7E20FD"); return false;'><img src='/OpenForum/Images/icons/png/page_copy.png' /></a></div></div><script>document.getElementById('CB7E20FD').setAttribute('readonly', 'readonly');</script><!--End Extension ScriptClip script="OFX.get(url).withAction(action).go();" -->
<!--Extension ScriptClip script="OFX.get(url).withAction(action).withData( dataString or {param: key, para: key}).onSuccess(fn).onError(fn).go();" --><div class='row'><div class='columns large-10'><textarea style='border: none; background-color: black; color: lightgreen; font-family: monospace;'  id='4953F141' rows='2' >OFX.get(url).withAction(action).withData( dataString or {param: key, para: key}).onSuccess(fn).onError(fn).go();</textarea></div><div class='columns large-2'><a href='#' title='Copy to clipboard' onClick='OpenForum.copyElement("4953F141"); return false;'><img src='/OpenForum/Images/icons/png/page_copy.png' /></a></div></div><script>document.getElementById('4953F141').setAttribute('readonly', 'readonly');</script><!--End Extension ScriptClip script="OFX.get(url).withAction(action).withData( dataString or {param: key, para: key}).onSuccess(fn).onError(fn).go();" -->

<hr/>
<h3>Example post</h3>

<!--Extension ScriptClip script="OFX.post(url).withData({param: key, para: key}).onSuccess(fn).go();" --><div class='row'><div class='columns large-10'><textarea style='border: none; background-color: black; color: lightgreen; font-family: monospace;'  id='C0F82E01' rows='2' >OFX.post(url).withData({param: key, para: key}).onSuccess(fn).go();</textarea></div><div class='columns large-2'><a href='#' title='Copy to clipboard' onClick='OpenForum.copyElement("C0F82E01"); return false;'><img src='/OpenForum/Images/icons/png/page_copy.png' /></a></div></div><script>document.getElementById('C0F82E01').setAttribute('readonly', 'readonly');</script><!--End Extension ScriptClip script="OFX.post(url).withData({param: key, para: key}).onSuccess(fn).go();" -->
<!--Extension ScriptClip script="OFX.post(url).withAction(action).withData( dataString or {param: key, para: key}).onSuccess(fn).go();" --><div class='row'><div class='columns large-10'><textarea style='border: none; background-color: black; color: lightgreen; font-family: monospace;'  id='BB995E0F' rows='2' >OFX.post(url).withAction(action).withData( dataString or {param: key, para: key}).onSuccess(fn).go();</textarea></div><div class='columns large-2'><a href='#' title='Copy to clipboard' onClick='OpenForum.copyElement("BB995E0F"); return false;'><img src='/OpenForum/Images/icons/png/page_copy.png' /></a></div></div><script>document.getElementById('BB995E0F').setAttribute('readonly', 'readonly');</script><!--End Extension ScriptClip script="OFX.post(url).withAction(action).withData( dataString or {param: key, para: key}).onSuccess(fn).go();" -->
<!--Extension ScriptClip script="OFX.post(url).withAction(action).withData( dataString or {param: key, para: key}).onSuccess(fn).onError(fn).go();" --><div class='row'><div class='columns large-10'><textarea style='border: none; background-color: black; color: lightgreen; font-family: monospace;'  id='C7272D03' rows='2' >OFX.post(url).withAction(action).withData( dataString or {param: key, para: key}).onSuccess(fn).onError(fn).go();</textarea></div><div class='columns large-2'><a href='#' title='Copy to clipboard' onClick='OpenForum.copyElement("C7272D03"); return false;'><img src='/OpenForum/Images/icons/png/page_copy.png' /></a></div></div><script>document.getElementById('C7272D03').setAttribute('readonly', 'readonly');</script><!--End Extension ScriptClip script="OFX.post(url).withAction(action).withData( dataString or {param: key, para: key}).onSuccess(fn).onError(fn).go();" -->

<hr/>
<!--Extension InsertSourceFile pageName="/OpenForum/Javascript/Core/" fileName="open-forum-ofx.js" --><a href="//OpenForum/Javascript/Core//open-forum-ofx.js">//OpenForum/Javascript/Core//open-forum-ofx.js</a><a href="/OpenForum/Editor?pageName=/OpenForum/Javascript/Core/&fileName=open-forum-ofx.js" title="Edit"><img src="/OpenForum/Images/icons/gif/pencil.gif" border="0"></a><xmp class="panel"><!--<ul><li> Author: Nik Cross</li>
<li> Description: A replacement for OpenForum JSON.get and JSON.post</li>
</ul>-->
var OFX = {
  fromJson: function( json ) {
    if(json.method == "get" ) {
      return OFX.get( json.url ).withAction( json.action ).withData( json.data );
    } else if(json.method == "post" ) {
      return OFX.post( json.url ).withAction( json.action ).withData( json.data );
    }
  },

  clearOfflineCache: function() {
    OpenForum.Storage.set("OFX.cache","<a href="/OpenForum/Documentation/OpenForumJavascript/OFX/" target="newTab"><img src='/OpenForum/Images/icons/png/link_go.png' /></a>");
    console.log("OFX cache cleared.");
  },

  sendOfflineCache: function( callback, callbackError ) {
    if(navigator.onLine===false) {
      return;
    }

    var cache = OFX.getOfflineCache();
    if(cache.length==0) {
      return;
    }
    var request = cache.shift();
    OpenForum.Storage.set("OFX.cache",JSON.stringify(cache));
    OFX.fromJson( request ).onSuccess( callback ).onError( callbackError ).go(true);

    if(cache.length!=0) {
      setTimeout( function() { OFX.sendOfflineCache(callback,callbackError); }, 1000 );
    }
  },

  getOfflineCache: function() {
    var cache = OpenForum.Storage.get("OFX.cache");
    if(cache==null) {
      cache = <a href="/OpenForum/Documentation/OpenForumJavascript/OFX/" target="newTab"><img src='/OpenForum/Images/icons/png/link_go.png' /></a>;
    } else {
      try{
        cache = JSON.parse(cache);
      } catch(e) {
        console.log("OFX cache error "+e+". Replacing cache.");
        cache = <a href="/OpenForum/Documentation/OpenForumJavascript/OFX/" target="newTab"><img src='/OpenForum/Images/icons/png/link_go.png' /></a>;
      }
    }
    return cache;
  },

  appendToOfflineCache: function(request) {
    var cache = OFX.getOfflineCache();
    cache.push( request.toJson() );
    OpenForum.Storage.set("OFX.cache",JSON.stringify(cache));
  },

  get: function(url) {
    var GET = function(url) {
      var self = this;
      var action;
      var data;
      var callBack;
      var callOnError;

      self.withAction = function(newAction) {
        action = newAction;
        return self;
      };

      self.withData = function(newData) {
        data = newData;
        return self;
      };

      self.onSuccess = function(newCallBack) {
        callBack = newCallBack;
        return self;
      };

      self.onError = function(newCallOnError) {
        callOnError = newCallOnError;
        return self;
      };

      self.toJson = function() {
        return {
          url: url,
          method: "get",
          action: action,
          data: data,
          requestedTime: new Date().getTime()
        };
      };

      self.go = function( withOfflineCache ) {

        if(navigator.onLine===false && withOfflineCache) {
          OFX.appendToOfflineCache( self );
          return true;
        }

        if(action) {
          if(data) {
            if(typeof data == "object") {
              var dataString = "";
              for(var name in data) {
                if(dataString.length>0) dataString += "&";
                if(typeof data<a href="/OpenForum/Editor?pageName=/OpenForum/Documentation/OpenForumJavascript/OFX/name"target="editor" style="color: red" title="The page /OpenForum/Editor?pageName=/OpenForum/Documentation/OpenForumJavascript/OFX/name does not exist. Click to create it.">name</a> == "object") {
                	dataString += name + "="+ JSON.stringify( data<a href="/OpenForum/Editor?pageName=/OpenForum/Documentation/OpenForumJavascript/OFX/name"target="editor" style="color: red" title="The page /OpenForum/Editor?pageName=/OpenForum/Documentation/OpenForumJavascript/OFX/name does not exist. Click to create it.">name</a> );
                } else {
                	dataString += name + "="+ data<a href="/OpenForum/Editor?pageName=/OpenForum/Documentation/OpenForumJavascript/OFX/name"target="editor" style="color: red" title="The page /OpenForum/Editor?pageName=/OpenForum/Documentation/OpenForumJavascript/OFX/name does not exist. Click to create it.">name</a>;
                }
              }
              data = dataString;
            }
          }
          var get = JSON.get(url,action,data);
          if(callBack) {
            get = get.onSuccess( callBack );
          }
          if(callOnError) {
            get = get.onError(callOnError);
          }
          get.go();
        } else {
          OpenForum.loadFile(url,callBack);
        }
      };
    };
    return new GET(url);
  },

  post: function(url) {
    var POST = function(url) {
      var self = this;
      var action;
      var data;
      var callBack;
      var callOnError;

      self.withAction = function(newAction) {
        action = newAction;
        return self;
      };

      self.withData = function(newData) {
        data = newData;
        return self;
      };

      self.onSuccess = function(newCallBack) {
        callBack = newCallBack;
        return self;
      };

      self.onError = function(newCallOnError) {
        callOnError = newCallOnError;
        return self;
      };

      self.toJson = function() {
        return {
          url: url,
          method: "post",
          action: action,
          data: data,
          requestedTime: new Date().getTime()
        };
      };

      self.go = function( withOfflineCache ) {

        if(navigator.onLine===false && withOfflineCache) {
          OFX.appendToOfflineCache( self );
          return true;
        }

        if(action) {
          if(data) {
            if(typeof data == "object") {
              var dataString = "";
              for(var name in data) {
                if(dataString.length>0) dataString += "&";
                if(typeof data<a href="/OpenForum/Editor?pageName=/OpenForum/Documentation/OpenForumJavascript/OFX/name"target="editor" style="color: red" title="The page /OpenForum/Editor?pageName=/OpenForum/Documentation/OpenForumJavascript/OFX/name does not exist. Click to create it.">name</a> == "object") {
                	dataString += name + "="+ JSON.stringify( data<a href="/OpenForum/Editor?pageName=/OpenForum/Documentation/OpenForumJavascript/OFX/name"target="editor" style="color: red" title="The page /OpenForum/Editor?pageName=/OpenForum/Documentation/OpenForumJavascript/OFX/name does not exist. Click to create it.">name</a> );
                } else {
                	dataString += name + "="+ data<a href="/OpenForum/Editor?pageName=/OpenForum/Documentation/OpenForumJavascript/OFX/name"target="editor" style="color: red" title="The page /OpenForum/Editor?pageName=/OpenForum/Documentation/OpenForumJavascript/OFX/name does not exist. Click to create it.">name</a>;
                }
              }
              data = dataString;
            }
          }
          var post = JSON.post(url,action,data);
          if(callBack) {
            post = post.onSuccess( callBack );
          }
          if(callOnError) {
            post = post.onError(callOnError);
          }
          post.go();
        } else {
          OpenForum.saveFile(url,data);
        }
      };
    };
    return new POST(url);
  }
};
</xmp><!--End Extension InsertSourceFile pageName="/OpenForum/Javascript/Core/" fileName="open-forum-ofx.js" -->
