/ckan/public/scripts/vendor/recline/recline.js
JavaScript | 4617 lines | 4146 code | 182 blank | 289 comment | 183 complexity | 66532048416b4a9eeffdf56331eef71e MD5 | raw file
Large files files are truncated, but you can click here to view the full file
- this.recline = this.recline || {};
- this.recline.Backend = this.recline.Backend || {};
- this.recline.Backend.Ckan = this.recline.Backend.Ckan || {};
- (function($, my) {
- // ## CKAN Backend
- //
- // This provides connection to the CKAN DataStore (v2)
- //
- // General notes
- //
- // * Every dataset must have an id equal to its resource id on the CKAN instance
- // * You should set the CKAN API endpoint for requests by setting API_ENDPOINT value on this module (recline.Backend.Ckan.API_ENDPOINT)
- my.__type__ = 'ckan';
- // Default CKAN API endpoint used for requests (you can change this but it will affect every request!)
- my.API_ENDPOINT = 'http://datahub.io/api';
- // ### fetch
- my.fetch = function(dataset) {
- var wrapper = my.DataStore();
- var dfd = $.Deferred();
- var jqxhr = wrapper.search({resource_id: dataset.id, limit: 0});
- jqxhr.done(function(results) {
- // map ckan types to our usual types ...
- var fields = _.map(results.result.fields, function(field) {
- field.type = field.type in CKAN_TYPES_MAP ? CKAN_TYPES_MAP[field.type] : field.type;
- return field;
- });
- var out = {
- fields: fields,
- useMemoryStore: false
- };
- dfd.resolve(out);
- });
- return dfd.promise();
- };
- // only put in the module namespace so we can access for tests!
- my._normalizeQuery = function(queryObj, dataset) {
- var actualQuery = {
- resource_id: dataset.id,
- q: queryObj.q,
- limit: queryObj.size || 10,
- offset: queryObj.from || 0
- };
- if (queryObj.sort && queryObj.sort.length > 0) {
- var _tmp = _.map(queryObj.sort, function(sortObj) {
- return sortObj.field + ' ' + (sortObj.order || '');
- });
- actualQuery.sort = _tmp.join(',');
- }
- return actualQuery;
- }
- my.query = function(queryObj, dataset) {
- var actualQuery = my._normalizeQuery(queryObj, dataset);
- var wrapper = my.DataStore();
- var dfd = $.Deferred();
- var jqxhr = wrapper.search(actualQuery);
- jqxhr.done(function(results) {
- var out = {
- total: results.result.total,
- hits: results.result.records,
- };
- dfd.resolve(out);
- });
- return dfd.promise();
- };
- // ### DataStore
- //
- // Simple wrapper around the CKAN DataStore API
- //
- // @param endpoint: CKAN api endpoint (e.g. http://datahub.io/api)
- my.DataStore = function(endpoint) {
- var that = {
- endpoint: endpoint || my.API_ENDPOINT
- };
- that.search = function(data) {
- var searchUrl = that.endpoint + '/3/action/datastore_search';
- var jqxhr = $.ajax({
- url: searchUrl,
- data: data,
- dataType: 'json'
- });
- return jqxhr;
- }
- return that;
- }
- var CKAN_TYPES_MAP = {
- 'int4': 'integer',
- 'int8': 'integer',
- 'float8': 'float',
- 'text': 'string',
- 'json': 'object',
- 'timestamp': 'date'
- };
- }(jQuery, this.recline.Backend.Ckan));
- this.recline = this.recline || {};
- this.recline.Backend = this.recline.Backend || {};
- this.recline.Backend.CSV = this.recline.Backend.CSV || {};
- (function(my) {
- // ## fetch
- //
- // 3 options
- //
- // 1. CSV local fileobject -> HTML5 file object + CSV parser
- // 2. Already have CSV string (in data) attribute -> CSV parser
- // 2. online CSV file that is ajax-able -> ajax + csv parser
- //
- // All options generates similar data and give a memory store outcome
- my.fetch = function(dataset) {
- var dfd = $.Deferred();
- if (dataset.file) {
- var reader = new FileReader();
- var encoding = dataset.encoding || 'UTF-8';
- reader.onload = function(e) {
- var rows = my.parseCSV(e.target.result, dataset);
- dfd.resolve({
- records: rows,
- metadata: {
- filename: dataset.file.name
- },
- useMemoryStore: true
- });
- };
- reader.onerror = function (e) {
- alert('Failed to load file. Code: ' + e.target.error.code);
- };
- reader.readAsText(dataset.file, encoding);
- } else if (dataset.data) {
- var rows = my.parseCSV(dataset.data, dataset);
- dfd.resolve({
- records: rows,
- useMemoryStore: true
- });
- } else if (dataset.url) {
- $.get(dataset.url).done(function(data) {
- var rows = my.parseCSV(data, dataset);
- dfd.resolve({
- records: rows,
- useMemoryStore: true
- });
- });
- }
- return dfd.promise();
- };
- // Converts a Comma Separated Values string into an array of arrays.
- // Each line in the CSV becomes an array.
- //
- // Empty fields are converted to nulls and non-quoted numbers are converted to integers or floats.
- //
- // @return The CSV parsed as an array
- // @type Array
- //
- // @param {String} s The string to convert
- // @param {Object} options Options for loading CSV including
- // @param {Boolean} [trim=false] If set to True leading and trailing whitespace is stripped off of each non-quoted field as it is imported
- // @param {String} [separator=','] Separator for CSV file
- // Heavily based on uselesscode's JS CSV parser (MIT Licensed):
- // http://www.uselesscode.org/javascript/csv/
- my.parseCSV= function(s, options) {
- // Get rid of any trailing \n
- s = chomp(s);
- var options = options || {};
- var trm = (options.trim === false) ? false : true;
- var separator = options.separator || ',';
- var delimiter = options.delimiter || '"';
- var cur = '', // The character we are currently processing.
- inQuote = false,
- fieldQuoted = false,
- field = '', // Buffer for building up the current field
- row = [],
- out = [],
- i,
- processField;
- processField = function (field) {
- if (fieldQuoted !== true) {
- // If field is empty set to null
- if (field === '') {
- field = null;
- // If the field was not quoted and we are trimming fields, trim it
- } else if (trm === true) {
- field = trim(field);
- }
- // Convert unquoted numbers to their appropriate types
- if (rxIsInt.test(field)) {
- field = parseInt(field, 10);
- } else if (rxIsFloat.test(field)) {
- field = parseFloat(field, 10);
- }
- }
- return field;
- };
- for (i = 0; i < s.length; i += 1) {
- cur = s.charAt(i);
- // If we are at a EOF or EOR
- if (inQuote === false && (cur === separator || cur === "\n")) {
- field = processField(field);
- // Add the current field to the current row
- row.push(field);
- // If this is EOR append row to output and flush row
- if (cur === "\n") {
- out.push(row);
- row = [];
- }
- // Flush the field buffer
- field = '';
- fieldQuoted = false;
- } else {
- // If it's not a delimiter, add it to the field buffer
- if (cur !== delimiter) {
- field += cur;
- } else {
- if (!inQuote) {
- // We are not in a quote, start a quote
- inQuote = true;
- fieldQuoted = true;
- } else {
- // Next char is delimiter, this is an escaped delimiter
- if (s.charAt(i + 1) === delimiter) {
- field += delimiter;
- // Skip the next char
- i += 1;
- } else {
- // It's not escaping, so end quote
- inQuote = false;
- }
- }
- }
- }
- }
- // Add the last field
- field = processField(field);
- row.push(field);
- out.push(row);
- return out;
- };
- // Converts an array of arrays into a Comma Separated Values string.
- // Each array becomes a line in the CSV.
- //
- // Nulls are converted to empty fields and integers or floats are converted to non-quoted numbers.
- //
- // @return The array serialized as a CSV
- // @type String
- //
- // @param {Array} a The array of arrays to convert
- // @param {Object} options Options for loading CSV including
- // @param {String} [separator=','] Separator for CSV file
- // Heavily based on uselesscode's JS CSV parser (MIT Licensed):
- // http://www.uselesscode.org/javascript/csv/
- my.serializeCSV= function(a, options) {
- var options = options || {};
- var separator = options.separator || ',';
- var delimiter = options.delimiter || '"';
- var cur = '', // The character we are currently processing.
- field = '', // Buffer for building up the current field
- row = '',
- out = '',
- i,
- j,
- processField;
- processField = function (field) {
- if (field === null) {
- // If field is null set to empty string
- field = '';
- } else if (typeof field === "string" && rxNeedsQuoting.test(field)) {
- // Convert string to delimited string
- field = delimiter + field + delimiter;
- } else if (typeof field === "number") {
- // Convert number to string
- field = field.toString(10);
- }
- return field;
- };
- for (i = 0; i < a.length; i += 1) {
- cur = a[i];
- for (j = 0; j < cur.length; j += 1) {
- field = processField(cur[j]);
- // If this is EOR append row to output and flush row
- if (j === (cur.length - 1)) {
- row += field;
- out += row + "\n";
- row = '';
- } else {
- // Add the current field to the current row
- row += field + separator;
- }
- // Flush the field buffer
- field = '';
- }
- }
- return out;
- };
- var rxIsInt = /^\d+$/,
- rxIsFloat = /^\d*\.\d+$|^\d+\.\d*$/,
- // If a string has leading or trailing space,
- // contains a comma double quote or a newline
- // it needs to be quoted in CSV output
- rxNeedsQuoting = /^\s|\s$|,|"|\n/,
- trim = (function () {
- // Fx 3.1 has a native trim function, it's about 10x faster, use it if it exists
- if (String.prototype.trim) {
- return function (s) {
- return s.trim();
- };
- } else {
- return function (s) {
- return s.replace(/^\s*/, '').replace(/\s*$/, '');
- };
- }
- }());
- function chomp(s) {
- if (s.charAt(s.length - 1) !== "\n") {
- // Does not end with \n, just return string
- return s;
- } else {
- // Remove the \n
- return s.substring(0, s.length - 1);
- }
- }
- }(this.recline.Backend.CSV));
- this.recline = this.recline || {};
- this.recline.Backend = this.recline.Backend || {};
- this.recline.Backend.DataProxy = this.recline.Backend.DataProxy || {};
- (function($, my) {
- my.__type__ = 'dataproxy';
- // URL for the dataproxy
- my.dataproxy_url = 'http://jsonpdataproxy.appspot.com';
- // Timeout for dataproxy (after this time if no response we error)
- // Needed because use JSONP so do not receive e.g. 500 errors
- my.timeout = 5000;
- // ## load
- //
- // Load data from a URL via the [DataProxy](http://github.com/okfn/dataproxy).
- //
- // Returns array of field names and array of arrays for records
- my.fetch = function(dataset) {
- var data = {
- url: dataset.url,
- 'max-results': dataset.size || dataset.rows || 1000,
- type: dataset.format || ''
- };
- var jqxhr = $.ajax({
- url: my.dataproxy_url,
- data: data,
- dataType: 'jsonp'
- });
- var dfd = $.Deferred();
- _wrapInTimeout(jqxhr).done(function(results) {
- if (results.error) {
- dfd.reject(results.error);
- }
- dfd.resolve({
- records: results.data,
- fields: results.fields,
- useMemoryStore: true
- });
- })
- .fail(function(arguments) {
- dfd.reject(arguments);
- });
- return dfd.promise();
- };
- // ## _wrapInTimeout
- //
- // Convenience method providing a crude way to catch backend errors on JSONP calls.
- // Many of backends use JSONP and so will not get error messages and this is
- // a crude way to catch those errors.
- var _wrapInTimeout = function(ourFunction) {
- var dfd = $.Deferred();
- var timer = setTimeout(function() {
- dfd.reject({
- message: 'Request Error: Backend did not respond after ' + (my.timeout / 1000) + ' seconds'
- });
- }, my.timeout);
- ourFunction.done(function(arguments) {
- clearTimeout(timer);
- dfd.resolve(arguments);
- })
- .fail(function(arguments) {
- clearTimeout(timer);
- dfd.reject(arguments);
- })
- ;
- return dfd.promise();
- }
- }(jQuery, this.recline.Backend.DataProxy));
- this.recline = this.recline || {};
- this.recline.Backend = this.recline.Backend || {};
- this.recline.Backend.ElasticSearch = this.recline.Backend.ElasticSearch || {};
- (function($, my) {
- my.__type__ = 'elasticsearch';
- // ## ElasticSearch Wrapper
- //
- // A simple JS wrapper around an [ElasticSearch](http://www.elasticsearch.org/) endpoints.
- //
- // @param {String} endpoint: url for ElasticSearch type/table, e.g. for ES running
- // on http://localhost:9200 with index twitter and type tweet it would be:
- //
- // <pre>http://localhost:9200/twitter/tweet</pre>
- //
- // @param {Object} options: set of options such as:
- //
- // * headers - {dict of headers to add to each request}
- // * dataType: dataType for AJAx requests e.g. set to jsonp to make jsonp requests (default is json requests)
- my.Wrapper = function(endpoint, options) {
- var self = this;
- this.endpoint = endpoint;
- this.options = _.extend({
- dataType: 'json'
- },
- options);
- // ### mapping
- //
- // Get ES mapping for this type/table
- //
- // @return promise compatible deferred object.
- this.mapping = function() {
- var schemaUrl = self.endpoint + '/_mapping';
- var jqxhr = makeRequest({
- url: schemaUrl,
- dataType: this.options.dataType
- });
- return jqxhr;
- };
- // ### get
- //
- // Get record corresponding to specified id
- //
- // @return promise compatible deferred object.
- this.get = function(id) {
- var base = this.endpoint + '/' + id;
- return makeRequest({
- url: base,
- dataType: 'json'
- });
- };
- // ### upsert
- //
- // create / update a record to ElasticSearch backend
- //
- // @param {Object} doc an object to insert to the index.
- // @return deferred supporting promise API
- this.upsert = function(doc) {
- var data = JSON.stringify(doc);
- url = this.endpoint;
- if (doc.id) {
- url += '/' + doc.id;
- }
- return makeRequest({
- url: url,
- type: 'POST',
- data: data,
- dataType: 'json'
- });
- };
- // ### delete
- //
- // Delete a record from the ElasticSearch backend.
- //
- // @param {Object} id id of object to delete
- // @return deferred supporting promise API
- this.delete = function(id) {
- url = this.endpoint;
- url += '/' + id;
- return makeRequest({
- url: url,
- type: 'DELETE',
- dataType: 'json'
- });
- };
- this._normalizeQuery = function(queryObj) {
- var self = this;
- var queryInfo = (queryObj && queryObj.toJSON) ? queryObj.toJSON() : _.extend({}, queryObj);
- var out = {
- constant_score: {
- query: {}
- }
- };
- if (!queryInfo.q) {
- out.constant_score.query = {
- match_all: {}
- };
- } else {
- out.constant_score.query = {
- query_string: {
- query: queryInfo.q
- }
- };
- }
- if (queryInfo.filters && queryInfo.filters.length) {
- out.constant_score.filter = {
- and: []
- };
- _.each(queryInfo.filters, function(filter) {
- out.constant_score.filter.and.push(self._convertFilter(filter));
- });
- }
- return out;
- },
- // convert from Recline sort structure to ES form
- // http://www.elasticsearch.org/guide/reference/api/search/sort.html
- this._normalizeSort = function(sort) {
- var out = _.map(sort, function(sortObj) {
- var _tmp = {};
- var _tmp2 = _.clone(sortObj);
- delete _tmp2['field'];
- _tmp[sortObj.field] = _tmp2;
- return _tmp;
- });
- return out;
- },
- this._convertFilter = function(filter) {
- var out = {};
- out[filter.type] = {}
- if (filter.type === 'term') {
- out.term[filter.field] = filter.term.toLowerCase();
- } else if (filter.type === 'geo_distance') {
- out.geo_distance[filter.field] = filter.point;
- out.geo_distance.distance = filter.distance;
- out.geo_distance.unit = filter.unit;
- }
- return out;
- },
- // ### query
- //
- // @return deferred supporting promise API
- this.query = function(queryObj) {
- var esQuery = (queryObj && queryObj.toJSON) ? queryObj.toJSON() : _.extend({}, queryObj);
- esQuery.query = this._normalizeQuery(queryObj);
- delete esQuery.q;
- delete esQuery.filters;
- if (esQuery.sort && esQuery.sort.length > 0) {
- esQuery.sort = this._normalizeSort(esQuery.sort);
- }
- var data = {source: JSON.stringify(esQuery)};
- var url = this.endpoint + '/_search';
- var jqxhr = makeRequest({
- url: url,
- data: data,
- dataType: this.options.dataType
- });
- return jqxhr;
- }
- };
- // ## Recline Connectors
- //
- // Requires URL of ElasticSearch endpoint to be specified on the dataset
- // via the url attribute.
- // ES options which are passed through to `options` on Wrapper (see Wrapper for details)
- my.esOptions = {};
- // ### fetch
- my.fetch = function(dataset) {
- var es = new my.Wrapper(dataset.url, my.esOptions);
- var dfd = $.Deferred();
- es.mapping().done(function(schema) {
- if (!schema){
- dfd.reject({'message':'Elastic Search did not return a mapping'});
- return;
- }
- // only one top level key in ES = the type so we can ignore it
- var key = _.keys(schema)[0];
- var fieldData = _.map(schema[key].properties, function(dict, fieldName) {
- dict.id = fieldName;
- return dict;
- });
- dfd.resolve({
- fields: fieldData
- });
- })
- .fail(function(arguments) {
- dfd.reject(arguments);
- });
- return dfd.promise();
- };
- // ### save
- my.save = function(changes, dataset) {
- var es = new my.Wrapper(dataset.url, my.esOptions);
- if (changes.creates.length + changes.updates.length + changes.deletes.length > 1) {
- var dfd = $.Deferred();
- msg = 'Saving more than one item at a time not yet supported';
- alert(msg);
- dfd.reject(msg);
- return dfd.promise();
- }
- if (changes.creates.length > 0) {
- return es.upsert(changes.creates[0]);
- }
- else if (changes.updates.length >0) {
- return es.upsert(changes.updates[0]);
- } else if (changes.deletes.length > 0) {
- return es.delete(changes.deletes[0].id);
- }
- };
- // ### query
- my.query = function(queryObj, dataset) {
- var dfd = $.Deferred();
- var es = new my.Wrapper(dataset.url, my.esOptions);
- var jqxhr = es.query(queryObj);
- jqxhr.done(function(results) {
- var out = {
- total: results.hits.total,
- };
- out.hits = _.map(results.hits.hits, function(hit) {
- if (!('id' in hit._source) && hit._id) {
- hit._source.id = hit._id;
- }
- return hit._source;
- });
- if (results.facets) {
- out.facets = results.facets;
- }
- dfd.resolve(out);
- }).fail(function(errorObj) {
- var out = {
- title: 'Failed: ' + errorObj.status + ' code',
- message: errorObj.responseText
- };
- dfd.reject(out);
- });
- return dfd.promise();
- };
- // ### makeRequest
- //
- // Just $.ajax but in any headers in the 'headers' attribute of this
- // Backend instance. Example:
- //
- // <pre>
- // var jqxhr = this._makeRequest({
- // url: the-url
- // });
- // </pre>
- var makeRequest = function(data, headers) {
- var extras = {};
- if (headers) {
- extras = {
- beforeSend: function(req) {
- _.each(headers, function(value, key) {
- req.setRequestHeader(key, value);
- });
- }
- };
- }
- var data = _.extend(extras, data);
- return $.ajax(data);
- };
- }(jQuery, this.recline.Backend.ElasticSearch));
- this.recline = this.recline || {};
- this.recline.Backend = this.recline.Backend || {};
- this.recline.Backend.GDocs = this.recline.Backend.GDocs || {};
- (function($, my) {
- my.__type__ = 'gdocs';
- // ## Google spreadsheet backend
- //
- // Fetch data from a Google Docs spreadsheet.
- //
- // Dataset must have a url attribute pointing to the Gdocs or its JSON feed e.g.
- // <pre>
- // var dataset = new recline.Model.Dataset({
- // url: 'https://docs.google.com/spreadsheet/ccc?key=0Aon3JiuouxLUdGlQVDJnbjZRSU1tUUJWOUZXRG53VkE#gid=0'
- // },
- // 'gdocs'
- // );
- //
- // var dataset = new recline.Model.Dataset({
- // url: 'https://spreadsheets.google.com/feeds/list/0Aon3JiuouxLUdDQwZE1JdV94cUd6NWtuZ0IyWTBjLWc/od6/public/values?alt=json'
- // },
- // 'gdocs'
- // );
- // </pre>
- //
- // @return object with two attributes
- //
- // * fields: array of Field objects
- // * records: array of objects for each row
- my.fetch = function(dataset) {
- var dfd = $.Deferred();
- var urls = my.getGDocsAPIUrls(dataset.url);
- // TODO cover it with tests
- // get the spreadsheet title
- (function () {
- var titleDfd = $.Deferred();
- $.getJSON(urls.spreadsheet, function (d) {
- titleDfd.resolve({
- spreadsheetTitle: d.feed.title.$t
- });
- });
- return titleDfd.promise();
- }()).then(function (response) {
- // get the actual worksheet data
- $.getJSON(urls.worksheet, function(d) {
- var result = my.parseData(d);
- var fields = _.map(result.fields, function(fieldId) {
- return {id: fieldId};
- });
- dfd.resolve({
- metadata: {
- title: response.spreadsheetTitle +" :: "+ result.worksheetTitle,
- spreadsheetTitle: response.spreadsheetTitle,
- worksheetTitle : result.worksheetTitle
- },
- records : result.records,
- fields : fields,
- useMemoryStore: true
- });
- });
- });
- return dfd.promise();
- };
- // ## parseData
- //
- // Parse data from Google Docs API into a reasonable form
- //
- // :options: (optional) optional argument dictionary:
- // columnsToUse: list of columns to use (specified by field names)
- // colTypes: dictionary (with column names as keys) specifying types (e.g. range, percent for use in conversion).
- // :return: tabular data object (hash with keys: field and data).
- //
- // Issues: seems google docs return columns in rows in random order and not even sure whether consistent across rows.
- my.parseData = function(gdocsSpreadsheet, options) {
- var options = options || {};
- var colTypes = options.colTypes || {};
- var results = {
- fields : [],
- records: []
- };
- var entries = gdocsSpreadsheet.feed.entry || [];
- var key;
- var colName;
- // percentage values (e.g. 23.3%)
- var rep = /^([\d\.\-]+)\%$/;
- for(key in entries[0]) {
- // it's barely possible it has inherited keys starting with 'gsx$'
- if(/^gsx/.test(key)) {
- colName = key.substr(4);
- results.fields.push(colName);
- }
- }
- // converts non numberical values that should be numerical (22.3%[string] -> 0.223[float])
- results.records = _.map(entries, function(entry) {
- var row = {};
- _.each(results.fields, function(col) {
- var _keyname = 'gsx$' + col;
- var value = entry[_keyname].$t;
- var num;
-
- // TODO cover this part of code with test
- // TODO use the regexp only once
- // if labelled as % and value contains %, convert
- if(colTypes[col] === 'percent' && rep.test(value)) {
- num = rep.exec(value)[1];
- value = parseFloat(num) / 100;
- }
- row[col] = value;
- });
- return row;
- });
- results.worksheetTitle = gdocsSpreadsheet.feed.title.$t;
- return results;
- };
- // Convenience function to get GDocs JSON API Url from standard URL
- my.getGDocsAPIUrls = function(url) {
- // https://docs.google.com/spreadsheet/ccc?key=XXXX#gid=YYY
- var regex = /.*spreadsheet\/ccc?.*key=([^#?&+]+).*gid=([\d]+).*/;
- var matches = url.match(regex);
- var key;
- var worksheet;
- var urls;
-
- if(!!matches) {
- key = matches[1];
- // the gid in url is 0-based and feed url is 1-based
- worksheet = parseInt(matches[2]) + 1;
- urls = {
- worksheet : 'https://spreadsheets.google.com/feeds/list/'+ key +'/'+ worksheet +'/public/values?alt=json',
- spreadsheet: 'https://spreadsheets.google.com/feeds/worksheets/'+ key +'/public/basic?alt=json'
- }
- }
- else {
- // we assume that it's one of the feeds urls
- key = url.split('/')[5];
- // by default then, take first worksheet
- worksheet = 1;
- urls = {
- worksheet : 'https://spreadsheets.google.com/feeds/list/'+ key +'/'+ worksheet +'/public/values?alt=json',
- spreadsheet: 'https://spreadsheets.google.com/feeds/worksheets/'+ key +'/public/basic?alt=json'
- }
- }
- return urls;
- };
- }(jQuery, this.recline.Backend.GDocs));
- this.recline = this.recline || {};
- this.recline.Backend = this.recline.Backend || {};
- this.recline.Backend.Memory = this.recline.Backend.Memory || {};
- (function($, my) {
- my.__type__ = 'memory';
- // ## Data Wrapper
- //
- // Turn a simple array of JS objects into a mini data-store with
- // functionality like querying, faceting, updating (by ID) and deleting (by
- // ID).
- //
- // @param data list of hashes for each record/row in the data ({key:
- // value, key: value})
- // @param fields (optional) list of field hashes (each hash defining a field
- // as per recline.Model.Field). If fields not specified they will be taken
- // from the data.
- my.Store = function(data, fields) {
- var self = this;
- this.data = data;
- if (fields) {
- this.fields = fields;
- } else {
- if (data) {
- this.fields = _.map(data[0], function(value, key) {
- return {id: key};
- });
- }
- }
- this.update = function(doc) {
- _.each(self.data, function(internalDoc, idx) {
- if(doc.id === internalDoc.id) {
- self.data[idx] = doc;
- }
- });
- };
- this.delete = function(doc) {
- var newdocs = _.reject(self.data, function(internalDoc) {
- return (doc.id === internalDoc.id);
- });
- this.data = newdocs;
- };
- this.save = function(changes, dataset) {
- var self = this;
- var dfd = $.Deferred();
- // TODO _.each(changes.creates) { ... }
- _.each(changes.updates, function(record) {
- self.update(record);
- });
- _.each(changes.deletes, function(record) {
- self.delete(record);
- });
- dfd.resolve();
- return dfd.promise();
- },
- this.query = function(queryObj) {
- var dfd = $.Deferred();
- var numRows = queryObj.size || this.data.length;
- var start = queryObj.from || 0;
- var results = this.data;
-
- results = this._applyFilters(results, queryObj);
- results = this._applyFreeTextQuery(results, queryObj);
- // TODO: this is not complete sorting!
- // What's wrong is we sort on the *last* entry in the sort list if there are multiple sort criteria
- _.each(queryObj.sort, function(sortObj) {
- var fieldName = sortObj.field;
- results = _.sortBy(results, function(doc) {
- var _out = doc[fieldName];
- return _out;
- });
- if (sortObj.order == 'desc') {
- results.reverse();
- }
- });
- var facets = this.computeFacets(results, queryObj);
- var out = {
- total: results.length,
- hits: results.slice(start, start+numRows),
- facets: facets
- };
- dfd.resolve(out);
- return dfd.promise();
- };
- // in place filtering
- this._applyFilters = function(results, queryObj) {
- var filters = queryObj.filters;
- // register filters
- var filterFunctions = {
- term : term,
- range : range,
- geo_distance : geo_distance
- };
- var dataParsers = {
- number : function (e) { return parseFloat(e, 10); },
- string : function (e) { return e.toString() },
- date : function (e) { return new Date(e).valueOf() }
- };
- // filter records
- return _.filter(results, function (record) {
- var passes = _.map(filters, function (filter) {
- return filterFunctions[filter.type](record, filter);
- });
- // return only these records that pass all filters
- return _.all(passes, _.identity);
- });
- // filters definitions
- function term(record, filter) {
- var parse = dataParsers[filter.fieldType];
- var value = parse(record[filter.field]);
- var term = parse(filter.term);
- return (value === term);
- }
- function range(record, filter) {
- var parse = dataParsers[filter.fieldType];
- var value = parse(record[filter.field]);
- var start = parse(filter.start);
- var stop = parse(filter.stop);
- return (value >= start && value <= stop);
- }
- function geo_distance() {
- // TODO code here
- }
- };
- // we OR across fields but AND across terms in query string
- this._applyFreeTextQuery = function(results, queryObj) {
- if (queryObj.q) {
- var terms = queryObj.q.split(' ');
- results = _.filter(results, function(rawdoc) {
- var matches = true;
- _.each(terms, function(term) {
- var foundmatch = false;
- _.each(self.fields, function(field) {
- var value = rawdoc[field.id];
- if (value !== null) {
- value = value.toString();
- } else {
- // value can be null (apparently in some cases)
- value = '';
- }
- // TODO regexes?
- foundmatch = foundmatch || (value.toLowerCase() === term.toLowerCase());
- // TODO: early out (once we are true should break to spare unnecessary testing)
- // if (foundmatch) return true;
- });
- matches = matches && foundmatch;
- // TODO: early out (once false should break to spare unnecessary testing)
- // if (!matches) return false;
- });
- return matches;
- });
- }
- return results;
- };
- this.computeFacets = function(records, queryObj) {
- var facetResults = {};
- if (!queryObj.facets) {
- return facetResults;
- }
- _.each(queryObj.facets, function(query, facetId) {
- // TODO: remove dependency on recline.Model
- facetResults[facetId] = new recline.Model.Facet({id: facetId}).toJSON();
- facetResults[facetId].termsall = {};
- });
- // faceting
- _.each(records, function(doc) {
- _.each(queryObj.facets, function(query, facetId) {
- var fieldId = query.terms.field;
- var val = doc[fieldId];
- var tmp = facetResults[facetId];
- if (val) {
- tmp.termsall[val] = tmp.termsall[val] ? tmp.termsall[val] + 1 : 1;
- } else {
- tmp.missing = tmp.missing + 1;
- }
- });
- });
- _.each(queryObj.facets, function(query, facetId) {
- var tmp = facetResults[facetId];
- var terms = _.map(tmp.termsall, function(count, term) {
- return { term: term, count: count };
- });
- tmp.terms = _.sortBy(terms, function(item) {
- // want descending order
- return -item.count;
- });
- tmp.terms = tmp.terms.slice(0, 10);
- });
- return facetResults;
- };
- this.transform = function(editFunc) {
- var toUpdate = recline.Data.Transform.mapDocs(this.data, editFunc);
- // TODO: very inefficient -- could probably just walk the documents and updates in tandem and update
- _.each(toUpdate.updates, function(record, idx) {
- self.data[idx] = record;
- });
- return this.save(toUpdate);
- };
- };
- }(jQuery, this.recline.Backend.Memory));
- this.recline = this.recline || {};
- this.recline.Data = this.recline.Data || {};
- (function(my) {
- // adapted from https://github.com/harthur/costco. heather rules
- my.Transform = {};
- my.Transform.evalFunction = function(funcString) {
- try {
- eval("var editFunc = " + funcString);
- } catch(e) {
- return {errorMessage: e+""};
- }
- return editFunc;
- };
- my.Transform.previewTransform = function(docs, editFunc, currentColumn) {
- var preview = [];
- var updated = my.Transform.mapDocs($.extend(true, {}, docs), editFunc);
- for (var i = 0; i < updated.docs.length; i++) {
- var before = docs[i]
- , after = updated.docs[i]
- ;
- if (!after) after = {};
- if (currentColumn) {
- preview.push({before: before[currentColumn], after: after[currentColumn]});
- } else {
- preview.push({before: before, after: after});
- }
- }
- return preview;
- };
- my.Transform.mapDocs = function(docs, editFunc) {
- var edited = []
- , deleted = []
- , failed = []
- ;
-
- var updatedDocs = _.map(docs, function(doc) {
- try {
- var updated = editFunc(_.clone(doc));
- } catch(e) {
- failed.push(doc);
- return;
- }
- if(updated === null) {
- updated = {_deleted: true};
- edited.push(updated);
- deleted.push(doc);
- }
- else if(updated && !_.isEqual(updated, doc)) {
- edited.push(updated);
- }
- return updated;
- });
-
- return {
- updates: edited,
- docs: updatedDocs,
- deletes: deleted,
- failed: failed
- };
- };
- }(this.recline.Data))
- // # Recline Backbone Models
- this.recline = this.recline || {};
- this.recline.Model = this.recline.Model || {};
- (function($, my) {
- // ## <a id="dataset">Dataset</a>
- my.Dataset = Backbone.Model.extend({
- constructor: function Dataset() {
- Backbone.Model.prototype.constructor.apply(this, arguments);
- },
- // ### initialize
- initialize: function() {
- _.bindAll(this, 'query');
- this.backend = null;
- if (this.get('backend')) {
- this.backend = this._backendFromString(this.get('backend'));
- } else { // try to guess backend ...
- if (this.get('records')) {
- this.backend = recline.Backend.Memory;
- }
- }
- this.fields = new my.FieldList();
- this.records = new my.RecordList();
- this._changes = {
- deletes: [],
- updates: [],
- creates: []
- };
- this.facets = new my.FacetList();
- this.recordCount = null;
- this.queryState = new my.Query();
- this.queryState.bind('change', this.query);
- this.queryState.bind('facet:add', this.query);
- // store is what we query and save against
- // store will either be the backend or be a memory store if Backend fetch
- // tells us to use memory store
- this._store = this.backend;
- if (this.backend == recline.Backend.Memory) {
- this.fetch();
- }
- },
- // ### fetch
- //
- // Retrieve dataset and (some) records from the backend.
- fetch: function() {
- var self = this;
- var dfd = $.Deferred();
- if (this.backend !== recline.Backend.Memory) {
- this.backend.fetch(this.toJSON())
- .done(handleResults)
- .fail(function(arguments) {
- dfd.reject(arguments);
- });
- } else {
- // special case where we have been given data directly
- handleResults({
- records: this.get('records'),
- fields: this.get('fields'),
- useMemoryStore: true
- });
- }
- function handleResults(results) {
- var out = self._normalizeRecordsAndFields(results.records, results.fields);
- if (results.useMemoryStore) {
- self._store = new recline.Backend.Memory.Store(out.records, out.fields);
- }
- self.set(results.metadata);
- self.fields.reset(out.fields);
- self.query()
- .done(function() {
- dfd.resolve(self);
- })
- .fail(function(arguments) {
- dfd.reject(arguments);
- });
- }
- return dfd.promise();
- },
- // ### _normalizeRecordsAndFields
- //
- // Get a proper set of fields and records from incoming set of fields and records either of which may be null or arrays or objects
- //
- // e.g. fields = ['a', 'b', 'c'] and records = [ [1,2,3] ] =>
- // fields = [ {id: a}, {id: b}, {id: c}], records = [ {a: 1}, {b: 2}, {c: 3}]
- _normalizeRecordsAndFields: function(records, fields) {
- // if no fields get them from records
- if (!fields && records && records.length > 0) {
- // records is array then fields is first row of records ...
- if (records[0] instanceof Array) {
- fields = records[0];
- records = records.slice(1);
- } else {
- fields = _.map(_.keys(records[0]), function(key) {
- return {id: key};
- });
- }
- }
- // fields is an array of strings (i.e. list of field headings/ids)
- if (fields && fields.length > 0 && typeof fields[0] === 'string') {
- // Rename duplicate fieldIds as each field name needs to be
- // unique.
- var seen = {};
- fields = _.map(fields, function(field, index) {
- // cannot use trim as not supported by IE7
- var fieldId = field.replace(/^\s+|\s+$/g, '');
- if (fieldId === '') {
- fieldId = '_noname_';
- field = fieldId;
- }
- while (fieldId in seen) {
- seen[field] += 1;
- fieldId = field + seen[field];
- }
- if (!(field in seen)) {
- seen[field] = 0;
- }
- // TODO: decide whether to keep original name as label ...
- // return { id: fieldId, label: field || fieldId }
- return { id: fieldId };
- });
- }
- // records is provided as arrays so need to zip together with fields
- // NB: this requires you to have fields to match arrays
- if (records && records.length > 0 && records[0] instanceof Array) {
- records = _.map(records, function(doc) {
- var tmp = {};
- _.each(fields, function(field, idx) {
- tmp[field.id] = doc[idx];
- });
- return tmp;
- });
- }
- return {
- fields: fields,
- records: records
- };
- },
- save: function() {
- var self = this;
- // TODO: need to reset the changes ...
- return this._store.save(this._changes, this.toJSON());
- },
- transform: function(editFunc) {
- var self = this;
- if (!this._store.transform) {
- alert('Transform is not supported with this backend: ' + this.get('backend'));
- return;
- }
- this.trigger('recline:flash', {message: "Updating all visible docs. This could take a while...", persist: true, loader: true});
- this._store.transform(editFunc).done(function() {
- // reload data as records have changed
- self.query();
- self.trigger('recline:flash', {message: "Records updated successfully"});
- });
- },
- // ### query
- //
- // AJAX method with promise API to get records from the backend.
- //
- // It will query based on current query state (given by this.queryState)
- // updated by queryObj (if provided).
- //
- // Resulting RecordList are used to reset this.records and are
- // also returned.
- query: function(queryObj) {
- var self = this;
- var dfd = $.Deferred();
- this.trigger('query:start');
- if (queryObj) {
- this.queryState.set(queryObj, {silent: true});
- }
- var actualQuery = this.queryState.toJSON();
- this._store.query(actualQuery, this.toJSON())
- .done(function(queryResult) {
- self._handleQueryResult(queryResult);
- self.trigger('query:done');
- dfd.resolve(self.records);
- })
- .fail(function(arguments) {
- self.trigger('query:fail', arguments);
- dfd.reject(arguments);
- });
- return dfd.promise();
- },
- _handleQueryResult: function(queryResult) {
- var self = this;
- self.recordCount = queryResult.total;
- var docs = _.map(queryResult.hits, function(hit) {
- var _doc = new my.Record(hit);
- _doc.fields = self.fields;
- _doc.bind('change', function(doc) {
- self._changes.updates.push(doc.toJSON());
- });
- _doc.bind('destroy', function(doc) {
- self._changes.deletes.push(doc.toJSON());
- });
- return _doc;
- });
- self.records.reset(docs);
- if (queryResult.facets) {
- var facets = _.map(queryResult.facets, function(facetResult, facetId) {
- facetResult.id = facetId;
- return new my.Facet(facetResult);
- });
- self.facets.reset(facets);
- }
- },
- toTemplateJSON: function() {
- var data = this.toJSON();
- data.recordCount = this.recordCount;
- data.fields = this.fields.toJSON();
- return data;
- },
- // ### getFieldsSummary
- //
- // Get a summary for each field in the form of a `Facet`.
- //
- // @return null as this is async function. Provides deferred/promise interface.
- getFieldsSummary: function() {
- var self = this;
- var query = new my.Query();
- query.set({size: 0});
- this.fields.each(function(field) {
- query.addFacet(field.id);
- });
- var dfd = $.Deferred();
- this._store.query(query.toJSON(), this.toJSON()).done(function(queryResult) {
- if (queryResult.facets) {
- _.each(queryResult.facets, function(facetResult, facetId) {
- facetResult.id = facetId;
- var facet = new my.Facet(facetResult);
- // TODO: probably want replace rather than reset (i.e. just replace the facet with this id)
- self.fields.get(facetId).facets.reset(facet);
- });
- }
- dfd.resolve(queryResult);
- });
- return dfd.promise();
- },
- // Deprecated (as of v0.5) - use record.summary()
- recordSummary: function(record) {
- return record.summary();
- },
- // ### _backendFromString(backendString)
- //
- // See backend argument to initialize for details
- _backendFromString: function(backendString) {
- var parts = backendString.split('.');
- // walk through the specified path xxx.yyy.zzz to get the final object which should be backend class
- var current = window;
- for(ii=0;ii<parts.length;ii++) {
- if (!current) {
- break;
- }
- current = current[parts[ii]];
- }
- if (current) {
- return current;
- }
- // alternatively we just had a simple string
- var backend = null;
- if (recline && recline.Backend) {
- _.each(_.keys(recline.Backend), function(name) {
- if (name.toLowerCase() === backendString.toLowerCase()) {
- backend = recline.Backend[name];
- }
- });
- }
- return backend;
- }
- });
- // ## <a id="record">A Record</a>
- //
- // A single record (or row) in the dataset
- my.Record = Backbone.Model.extend({
- constructor: function Record() {
- Backbone.Model.prototype.constructor.apply(this, arguments);
- },
- // ### initialize
- //
- // Create a Record
- //
- // You usually will not do this directly but will have records created by
- // Dataset e.g. in query method
- //
- // Certain methods require presence of a fields attribute (identical to that on Dataset)
- initialize: function() {
- _.bindAll(this, 'getFieldValue');
- },
- // ### getFieldValue
- //
- // For the provided Field get the corresponding rendered computed data value
- // for this record.
- getFieldValue: function(field) {
- val = this.getFieldValueUnrendered(field);
- if (field.renderer) {
- val = field.renderer(val, field, this.toJSON());
- }
- return val;
- },
- // ### getFieldValueUnrendered
- //
- // For the provided Field get the corresponding computed data value
- // for this record.
- getFieldValueUnrendered: function(field) {
- var val = this.get(field.id);
- if (field.deriver) {
- val = field.deriver(val, field, this);
- }
- return val;
- },
- // ### summary
- //
- // Get a simple html summary of this record in form of key/value list
- summary: function(record) {
- var self = this;
- var html = '<div class="recline-record-summary">';
- this.fields.each(function(field) {
- if (field.id != 'id') {
- html += '<div class="' + field.id + '"><strong>' + field.get('label') + '</strong>: ' + self.getFieldValue(field) + '</div>';
- }
- });
- html += '</div>';
- return html;
- },
- // Override Backbone save, fetch and destroy so they do nothing
- // Instead, Dataset object that created this Record should take care of
- // handling these changes (discovery will occur via event notifications)
- // WARNING: these will not persist *unless* you call save on Dataset
- fetch: function() {},
- save: function() {},
- destroy: function() { this.trigger('destroy', this); }
- });
- // ## A Backbone collection of Records
- my.RecordList = Backbone.Collection.extend({
- constructor: function RecordList() {
- Backbone.Collection.prototype.constructor.apply(this, arguments);
- },
- model: my.Record
- });
- // ## <a id="field">A Field (aka Column) on a Dataset</a>
- my.Field = Backbone.Model.extend({
- constructor: function Field() {
- Backbone.Model.prototype.constructor.apply(this, arguments);
- },
- // ### defaults - define default values
- defaults: {
- label: null,
- type: 'string',
- format: null,
- is_derived: false
- },
- // ### initialize
- //
- // @param {Object} data: standard Backbone model attributes
- //
- // @param {Object} options: renderer and/or deriver functions.
- initialize: function(data, options) {
- // if a hash not passed in the first argument throw error
- if ('0' in data) {
- throw new Error('Looks like you did not pass a proper hash with id to Field constructor');
- }
- if (this.attributes.label === null) {
- this.set({label: this.id});
- }
- if (options) {
- this.renderer = options.renderer;
- this.deriver = options.deriver;
- }
- if (!this.renderer) {
- this.renderer = this.defaultRenderers[this.get('type')];
- }
- this.facets = new my.FacetList();
- },
- defaultRenderers: {
- object: function(val, field, doc) {
- return JSON.stringify(val);
- },
- geo_point: function(val, field, doc) {
- return JSON.stringify(val);
- },
- 'float': function(val, field, doc) {
- var format = field.get('format');
- if (format === 'percentage') {
- return val + '%';
- }
- return val;
- },
- 'string': function(val, field, doc) {
- var format = field.get('format');
- if (format === 'markdown') {
- if (typeof Showdown !== 'undefined') {
- var showdown = new Showdown.converter();
- out = showdown.makeHtml(val);
- return out;
- } else {
- return val;
- }
- } else if (format == 'plain') {
- return val;
- } else {
- // as this is the default and default type is string may get things
- // here that are not actually strings
- if (val && typeof val === 'string') {
- val = val.replace(/(https?:\/\/[^ ]+)/g, '<a href="$1">$1</a>');
- }
- return val
- }
- }
- }
- });
- my.FieldList = Backbone.Collection.extend({
- constructor: function FieldList() {
- Backbone.Collection.prototype.constructor.apply(this, arguments);
- },
- model: my.Field
- });
- // ## <a id="query">Query</a>
- my.Query = Backbone.Model.extend({
- constructor: function Query() {
- Backbone.Model.prototype.constructor.apply(this, arguments);
- },
- defaults: function() {
- return {
- size: 100,
- from: 0,
- q: '',
- facets: {},
- filters: []
- };
- },
- _filterTemplates: {
- term: {
- type: 'term',
- // TODO do we need this attribute here?
- field: '',
- term: ''
- },
- range: {
- type: 'range',
- start: '',
- stop: ''
- },
- geo_distance: {
- type: 'geo_distance',
- distance: 10,
- unit: 'km',
- point: {
- lon: 0,
- lat: 0
- }
- }
- },
- // ### addFilter
- //
- // Add a new filter (appended to the list of filters)
- //
- // @param filter an object specifying the filter - see _filterTemplates for examples. If only type is provided will generate a filter by cloning _filterTemplates
- addFilter: function(filter) {
- // crude deep copy
- var ourfilter = JSON.parse(JSON.stringify(filter));
- // not full specified so use template and over-write
- // 3 as for 'type', 'field' and 'fieldType'
- if (_.keys(filter).length <= 3) {
- ourfilter = _.extend(this._filterTemplates[filter.type], ourfilter);
- }
- var filters = this.get('filters');
- filters.push(ourfilter);
- this.trigger('change:filters:new-blank');
- },
- updateFilter: function(index, value) {
- },
- // ### removeFilter
- //
- // Remove a filter from filters at index filterIndex
- removeFilter: function(filterIndex) {
- var filters = this.get('filters');
- filters.splice(filterIndex, 1);
- this.set({filters: filters});
- this.trigger('change');
- },
- // ### addFacet
- //
- // Add a Facet to this query
- //
- // See <http://www.elasticsearch.org/guide/reference/api/search/facets/>
- addFacet: function(fieldId) {
- var facets = this.get('facets');
- // Assume id and fieldId should be the same (TODO: this need not be true if we want to add two different type of facets on same field)
- if (_.contains(_.keys(facets), fieldId)) {
- return;
- }
- facets[fieldId] = {
- terms: { field: fieldId }
- };
- this.set({facets: facets}, {silent: true});
- this.trigger('facet:add', this);
- },
- addHistogramFacet: function(fieldId) {
- var facets = this.get('facets');
- facets[fieldId] = {
- date_histogram: {
- field: fieldId,
- interval: 'day'
- }
- };
- this.set({facets: facets}, {silent: true});
- this.trigger('facet:add', this);
- }
- });
- // ## <a id="facet">A Facet (Result)</a>
- my.Facet = Backbone.Model.extend({
- constructor: function Facet() {
- Backbone.Model.prototype.constructor.apply(this, arguments);
- },
- defaults: function() {
- return {
- _type: 'terms',
- total: 0,
- other: 0,
- missing: 0,
- term…
Large files files are truncated, but you can click here to view the full file