diff --git a/README.md b/README.md index ab05522..ec834db 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ This demo offers a simple query builder and a simple HTML render of the JSON response from the [Who's got dirt? API](https://github.com/influencemapping/whos_got_dirt-server/). -[Try it.](http://influencemapping.github.io/whos_got_dirt-demo/) +[Try it!](http://influencemapping.github.io/whos_got_dirt-demo/) ## Development diff --git a/build/build.js b/build/build.js index 1959747..f4aaf03 100644 --- a/build/build.js +++ b/build/build.js @@ -566,48 +566,14 @@ jQuery(function ($) { ); } - // Add event handlers. - $('[data-toggle="tooltip"]').tooltip(); - - $(document).on('change', '.field', function () { - var field = $(this).val(); - var $row = $(this).parents('.row'); - var $value = $(this).parents('.row').find('.value'); - var value = $value.find('input, select').val(); - - $row.find('.operator').html(operatorTemplate(field)); - $value.html(valueTemplate(field, field_operators[field][0], value)); - $row.find('.date').datepicker(); - }); - - $(document).on('change', '.operator select', function () { - var operator = $(this).val(); - var field = $(this).parents('.row').find('.field').val(); - var $value = $(this).parents('.row').find('.value'); - var value = $value.find('input, select').val(); - - $value.html(valueTemplate(field, operator, value)); - if (operator === '|=') { - $value.append('The "is one of" operator accepts a pipe-separated list: for example, gb|ie.'); - } - }); - - $(document).on('click', '.remove', function () { - $(this).parents('.row').remove(); - }); - - $('#add').click(function () { - addField(); - }); - - $('#form').submit(function (event) { + // Returns the JSON payload to send to the API. + // + // @return {String} The JSON payload. + function payload() { var controls = $(this).serializeArray(); var query = {type: 'Person'}; var membership = {}; - $('#loading').css('visibility', 'visible').html(''); - $('.alert-warning').remove(); - // Build the query. for (var i = 0, l = controls.length; i < l; i += 3) { if (controls[i].name == 'endpoints') { @@ -655,18 +621,62 @@ jQuery(function ($) { endpoints.push($(this).val()); }); - // Send the request. - var json = JSON.stringify({ + // Prepare the payload. + return JSON.stringify({ q0: { query: query, endpoints: endpoints } }); + } + // Add event handlers. + $('[data-toggle="tooltip"]').tooltip(); + + $(document).on('change', '.field', function () { + var field = $(this).val(); + var $row = $(this).parents('.row'); + var $value = $(this).parents('.row').find('.value'); + var value = $value.find('input, select').val(); + + $row.find('.operator').html(operatorTemplate(field)); + $value.html(valueTemplate(field, field_operators[field][0], value)); + $row.find('.date').datepicker(); + }); + + $(document).on('change', '.operator select', function () { + var operator = $(this).val(); + var field = $(this).parents('.row').find('.field').val(); + var $value = $(this).parents('.row').find('.value'); + var value = $value.find('input, select').val(); + + $value.html(valueTemplate(field, operator, value)); + if (operator === '|=') { + $value.append('The "is one of" operator accepts a pipe-separated list: for example, gb|ie.'); + } + }); + + $(document).on('click', '.remove', function () { + $(this).parents('.row').remove(); + }); + + $('#add').click(function () { + addField(); + }); + + $('#form').submit(function () { + $('#queries').val(payload()); + }); + + $('#submit').click(function () { + $('#loading').css('visibility', 'visible').html(''); + $('.alert-warning').remove(); + + // Send the request. $.ajax({ dataType: 'json', url: 'https://whosgotdirt.herokuapp.com/entities', - data: {queries: json}, + data: {queries: payload()}, success: function (data) { $('#loading').css('visibility', 'hidden'); render_messages(data['q0']['messages']); @@ -690,8 +700,6 @@ jQuery(function ($) { } } }); - - event.preventDefault(); }); // Setup the page. diff --git a/index.html b/index.html index 1d3356d..6417ccd 100644 --- a/index.html +++ b/index.html @@ -19,7 +19,7 @@

Who's Got Dirt? Demo

This demo queries only entities (people and organizations) and exposes only 5 filters for entities, not all 25 implemented by the Who's got dirt? API and its supported APIs. In addition to entities, the Who's got dirt?1 API supports relations between entities (for example, a person holding an office in an organization) and lists of entities (for example, a corporate grouping).

-
+

Select your filters to search for entities

@@ -101,7 +101,9 @@

Select the APIs to query

- + + +
diff --git a/index.js b/index.js index f5798b2..71ee3c5 100644 --- a/index.js +++ b/index.js @@ -193,48 +193,14 @@ jQuery(function ($) { ); } - // Add event handlers. - $('[data-toggle="tooltip"]').tooltip(); - - $(document).on('change', '.field', function () { - var field = $(this).val(); - var $row = $(this).parents('.row'); - var $value = $(this).parents('.row').find('.value'); - var value = $value.find('input, select').val(); - - $row.find('.operator').html(operatorTemplate(field)); - $value.html(valueTemplate(field, field_operators[field][0], value)); - $row.find('.date').datepicker(); - }); - - $(document).on('change', '.operator select', function () { - var operator = $(this).val(); - var field = $(this).parents('.row').find('.field').val(); - var $value = $(this).parents('.row').find('.value'); - var value = $value.find('input, select').val(); - - $value.html(valueTemplate(field, operator, value)); - if (operator === '|=') { - $value.append('The "is one of" operator accepts a pipe-separated list: for example, gb|ie.'); - } - }); - - $(document).on('click', '.remove', function () { - $(this).parents('.row').remove(); - }); - - $('#add').click(function () { - addField(); - }); - - $('#form').submit(function (event) { + // Returns the JSON payload to send to the API. + // + // @return {String} The JSON payload. + function payload() { var controls = $(this).serializeArray(); var query = {type: 'Person'}; var membership = {}; - $('#loading').css('visibility', 'visible').html(''); - $('.alert-warning').remove(); - // Build the query. for (var i = 0, l = controls.length; i < l; i += 3) { if (controls[i].name == 'endpoints') { @@ -282,18 +248,62 @@ jQuery(function ($) { endpoints.push($(this).val()); }); - // Send the request. - var json = JSON.stringify({ + // Prepare the payload. + return JSON.stringify({ q0: { query: query, endpoints: endpoints } }); + } + // Add event handlers. + $('[data-toggle="tooltip"]').tooltip(); + + $(document).on('change', '.field', function () { + var field = $(this).val(); + var $row = $(this).parents('.row'); + var $value = $(this).parents('.row').find('.value'); + var value = $value.find('input, select').val(); + + $row.find('.operator').html(operatorTemplate(field)); + $value.html(valueTemplate(field, field_operators[field][0], value)); + $row.find('.date').datepicker(); + }); + + $(document).on('change', '.operator select', function () { + var operator = $(this).val(); + var field = $(this).parents('.row').find('.field').val(); + var $value = $(this).parents('.row').find('.value'); + var value = $value.find('input, select').val(); + + $value.html(valueTemplate(field, operator, value)); + if (operator === '|=') { + $value.append('The "is one of" operator accepts a pipe-separated list: for example, gb|ie.'); + } + }); + + $(document).on('click', '.remove', function () { + $(this).parents('.row').remove(); + }); + + $('#add').click(function () { + addField(); + }); + + $('#form').submit(function () { + $('#queries').val(payload()); + }); + + $('#submit').click(function () { + $('#loading').css('visibility', 'visible').html(''); + $('.alert-warning').remove(); + + // Send the request. $.ajax({ dataType: 'json', url: 'https://whosgotdirt.herokuapp.com/entities', - data: {queries: json}, + data: {queries: payload()}, success: function (data) { $('#loading').css('visibility', 'hidden'); render_messages(data['q0']['messages']); @@ -317,8 +327,6 @@ jQuery(function ($) { } } }); - - event.preventDefault(); }); // Setup the page.