Improve api explorer formatting
authorColeman Watts <coleman@civicrm.org>
Wed, 25 Feb 2015 03:03:47 +0000 (22:03 -0500)
committerColeman Watts <coleman@civicrm.org>
Wed, 25 Feb 2015 03:03:47 +0000 (22:03 -0500)
templates/CRM/Admin/Page/APIExplorer.js
templates/CRM/Admin/Page/APIExplorer.tpl

index 00d880fdade2b7c7a9e12423ab22637533591cd0..5d3276e649e0ce9236d4c8ef5f65544c3ab8e3ae 100644 (file)
     TEXT = ['LIKE', 'NOT LIKE'],
     MULTI = ['IN', 'NOT IN', 'BETWEEN', 'NOT BETWEEN'];
 
+  /**
+   * Call prettyPrint function and perform additional formatting
+   * @param ele
+   */
+  function prettyPrint(ele) {
+    if (typeof window.prettyPrint === 'function') {
+      $(ele).removeClass('prettyprinted').addClass('prettyprint');
+
+      window.prettyPrint();
+
+      // Highlight errors in api result
+      $('span:contains("error_code"),span:contains("error_message")', '#api-result')
+        .siblings('span.str').css('color', '#B00');
+    }
+  }
+
   /**
    * Add a "fields" row
    * @param name
       q.smarty = "{* Smarty does not have a syntax for array literals; assign complex variables from php *}\n" + q.smarty;
     }
     $.each(q, function(type, val) {
-      $('#api-' + type).addClass('prettyprint').removeClass('prettyprinted').text(val);
+      $('#api-' + type).text(val);
     });
-    prettyPrint();
+    prettyPrint('#api-generated pre');
   }
 
   /**
       type: _.includes(action, 'get') ? 'GET' : 'POST',
       dataType: 'text'
     }).done(function(text) {
-      $('#api-result').addClass('prettyprint').removeClass('prettyprinted').text(text);
-      prettyPrint();
+      $('#api-result').text(text);
+      prettyPrint('#api-result');
     });
   }
 
       $('#example-result').block();
       $.get(CRM.url('civicrm/ajax/apiexample', {file: entity + '/' + action}))
         .done(function(result) {
-          $('#example-result').unblock().addClass('prettyprint').removeClass('prettyprinted').text(result);
-          prettyPrint();
+          $('#example-result').unblock().text(result);
+          prettyPrint('#example-result');
         });
     } else {
       $('#example-result').text($('#example-result').attr('title'));
           if (result.code) {
             $('#doc-result').append(docCodeTpl(result));
           }
-          prettyPrint();
+          prettyPrint('.api-doc-code pre');
         });
     } else {
       $('#doc-result').html($('#doc-result').attr('title'));
index 3a8202f800156ec29e105a953683bfb9f7dcdfe1..28e05dbef1f6e60b3377d5efff31606abd0ceb7a 100644 (file)
     <div class="collapsible-title">{ts}Source Code{/ts}</div>
     <div>
       <div class="doc-filename"><%- file %></div>
-      <pre class="prettyprint lang-php linenums"><%- code %></pre>
+      <pre class="lang-php linenums"><%- code %></pre>
     </div>
   </div>
 </script>