Add playground context map support.
authorDavid I. Lehn <dlehn@digitalbazaar.com>
Thu, 15 Aug 2013 15:01:36 -0400
changeset 1888 e22c82545d17
parent 1887 cb0a81750ee0
child 1889 616995774b5e
Add playground context map support.

Support was added for the playground to use a documentLoader that has a
mapping between specific user specified contexts and a modified context
to load. Currently this is used to map the use of the non-existent
schema.org context to a temporary at w3id.org and hosted at json-ld.org.

A bit of UI feedback is provided to let users know which contexts were
mapped to modified values.
playground/index.html
playground/playground.js
--- a/playground/index.html	Thu Aug 15 06:22:29 2013 -0700
+++ b/playground/index.html	Thu Aug 15 15:01:36 2013 -0400
@@ -179,6 +179,23 @@
           </div><!-- /.tab-content -->
         </div><!-- /.tabbable -->
       </div>
+      <div id="using-context-map" class="hide alert alert-note">
+        <p>
+        NOTE: Context map below is in use. One or more of the context URLs
+        was loaded from a modified value.
+        </p>
+        <table class="table table-condensed">
+          <thead>
+            <tr>
+              <td>Original</td>
+              <td>Modified</td>
+            </tr>
+          </thead>
+          <tbody>
+            <!-- dynamic rows -->
+          </tbody>
+        </table>
+      </div>
       <hr>
       <div id="footer">
         <p id="copyright">
--- a/playground/playground.js	Thu Aug 15 06:22:29 2013 -0700
+++ b/playground/playground.js	Thu Aug 15 15:01:36 2013 -0400
@@ -20,6 +20,16 @@
   // colorize delay
   playground.colorizeTimeout = null;
 
+  // map of original to modifed contexts
+  playground.contextMap = {
+    // FIXME: remove schema.org support once they serve a JSON-LD context
+    'http://schema.org': 'https://w3id.org/schema.org',
+    'http://schema.org/': 'https://w3id.org/schema.org'
+  };
+
+  // map of currently active mapped contexts for user feedback use
+  playground.activeContextMap = {};
+
   /**
    * Escapes text that will affect HTML markup.
    *
@@ -267,6 +277,9 @@
     $('#markup-errors').text('');
     $('#param-errors').text('');
     $('#processing-errors').text('');
+    $('#using-context-map').hide();
+    $('#using-context-map table tbody').empty();
+    playground.activeContextMap = {};
     var errors = false;
     var markup = $('#markup').val();
 
@@ -464,8 +477,24 @@
 
   // event handlers
   $(document).ready(function() {
-    // use jquery document loader
-    jsonld.useDocumentLoader('jquery', $);
+    // Add custom document loader that uses a context URL map.
+    var jqueryDocumentLoader = jsonld.documentLoaders.jquery($);
+    // FIXME: add UI to let users control and set context mapping
+    jsonld.documentLoader = function(url, callback) {
+      if(url in playground.contextMap) {
+        $('#using-context-map').show();
+        var modified = playground.contextMap[url];
+        if(!(modified in playground.activeContextMap)) {
+          var row = $('<tr>')
+            .append('<td>' + url + '</td>')
+            .append('<td>' + modified + '</td>');
+          $('#using-context-map table tbody').append(row);
+          playground.activeContextMap[url] = modified;
+        }
+        url = modified;
+      }
+      jqueryDocumentLoader(url, callback);
+    };
 
     // set up buttons to load examples
     $('.button').each(function(idx) {