From 5d4166263e570b0e498588116eb4cbbd0f889ebf Mon Sep 17 00:00:00 2001 From: Rufus Pollock Date: Mon, 15 Apr 2013 18:10:36 +0100 Subject: [PATCH] [build][s]: regular build. --- dist/recline.css | 12 ++++ dist/recline.js | 142 ++++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 145 insertions(+), 9 deletions(-) diff --git a/dist/recline.css b/dist/recline.css index e6437a11..c030e764 100644 --- a/dist/recline.css +++ b/dist/recline.css @@ -395,6 +395,18 @@ div.data-table-cell-content-numeric > a.data-table-cell-edit { width: 175px; } +.recline-filter-editor input { + margin-top: 0.5em; +} + +.recline-filter-editor .add-filter { + margin-top: 1em; + margin-bottom: 2em; +} + +.recline-filter-editor .update-filter { + margin-top: 1em; +} /********************************************************** * Fields Widget diff --git a/dist/recline.js b/dist/recline.js index e741bbbc..b6e8d2e7 100644 --- a/dist/recline.js +++ b/dist/recline.js @@ -66,15 +66,25 @@ this.recline.Backend.Ckan = this.recline.Backend.Ckan || {}; var actualQuery = { resource_id: dataset.id, q: queryObj.q, + filters: {}, 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(','); } + + if (queryObj.filters && queryObj.filters.length > 0) { + _.each(queryObj.filters, function(filter) { + if (filter.type === "term") { + actualQuery.filters[filter.field] = filter.term; + } + }); + } return actualQuery; }; @@ -105,15 +115,14 @@ this.recline.Backend.Ckan = this.recline.Backend.Ckan || {}; // // @param endpoint: CKAN api endpoint (e.g. http://datahub.io/api) my.DataStore = function(endpoint) { - var that = { - endpoint: endpoint || my.API_ENDPOINT - }; + var that = {endpoint: endpoint || my.API_ENDPOINT}; + that.search = function(data) { var searchUrl = that.endpoint + '/3/action/datastore_search'; var jqxhr = jQuery.ajax({ url: searchUrl, - data: data, - dataType: 'json' + type: 'POST', + data: JSON.stringify(data) }); return jqxhr; }; @@ -2131,7 +2140,7 @@ my.Flot = Backbone.View.extend({ // convert x to a string and make sure that it is not too long or the // tick labels will overlap // TODO: find a more accurate way of calculating the size of tick labels - var label = self._xaxisLabel(x); + var label = self._xaxisLabel(x) || ""; if (typeof label !== 'string') { label = label.toString(); @@ -4803,7 +4812,8 @@ my.Timeline = Backbone.View.extend({ }); var stateData = _.extend({ startField: null, - endField: null + endField: null, + timelineJSOptions: {} }, options.state ); @@ -4836,9 +4846,8 @@ my.Timeline = Backbone.View.extend({ if (width) { $timeline.width(width); } - var config = {}; var data = this._timelineJSON(); - this.timeline.init(data, this.elementId, config); + this.timeline.init(data, this.elementId, this.state.get("timelineJSOptions")); this._timelineIsInitialized = true }, @@ -5523,3 +5532,118 @@ my.QueryEditor = Backbone.View.extend({ })(jQuery, recline.View); +/*jshint multistr:true */ + +this.recline = this.recline || {}; +this.recline.View = this.recline.View || {}; + +(function($, my) { + +my.ValueFilter = Backbone.View.extend({ + className: 'recline-filter-editor well', + template: ' \ +
\ +

Filters

\ + \ + \ +
\ + {{#filters}} \ + {{{filterRender}}} \ + {{/filters}} \ + {{#filters.length}} \ + \ + {{/filters.length}} \ +
\ +
\ + ', + filterTemplates: { + term: ' \ +
\ +
\ + {{field}} \ + × \ + \ +
\ +
\ + ' + }, + events: { + 'click .js-remove-filter': 'onRemoveFilter', + 'click .js-add-filter': 'onAddFilterShow', + 'submit form.js-edit': 'onTermFiltersUpdate', + 'submit form.js-add': 'onAddFilter' + }, + initialize: function() { + this.el = $(this.el); + _.bindAll(this, 'render'); + this.model.fields.bind('all', this.render); + this.model.queryState.bind('change', this.render); + this.model.queryState.bind('change:filters:new-blank', this.render); + this.render(); + }, + render: function() { + var self = this; + var tmplData = $.extend(true, {}, this.model.queryState.toJSON()); + // we will use idx in list as the id ... + tmplData.filters = _.map(tmplData.filters, function(filter, idx) { + filter.id = idx; + return filter; + }); + tmplData.fields = this.model.fields.toJSON(); + tmplData.filterRender = function() { + return Mustache.render(self.filterTemplates.term, this); + }; + var out = Mustache.render(this.template, tmplData); + this.el.html(out); + }, + updateFilter: function(input) { + var self = this; + var filters = self.model.queryState.get('filters'); + var $input = $(input); + var filterIndex = parseInt($input.attr('data-filter-id'), 10); + var value = $input.val(); + filters[filterIndex].term = value; + }, + onAddFilterShow: function(e) { + e.preventDefault(); + var $target = $(e.target); + $target.hide(); + this.el.find('form.js-add').show(); + }, + onAddFilter: function(e) { + e.preventDefault(); + var $target = $(e.target); + $target.hide(); + var field = $target.find('select.fields').val(); + this.model.queryState.addFilter({type: 'term', field: field}); + }, + onRemoveFilter: function(e) { + e.preventDefault(); + var $target = $(e.target); + var filterId = $target.attr('data-filter-id'); + this.model.queryState.removeFilter(filterId); + }, + onTermFiltersUpdate: function(e) { + var self = this; + e.preventDefault(); + var filters = self.model.queryState.get('filters'); + var $form = $(e.target); + _.each($form.find('input'), function(input) { + self.updateFilter(input); + }); + self.model.queryState.set({filters: filters, from: 0}); + self.model.queryState.trigger('change'); + } +}); + +})(jQuery, recline.View);