From a7efe19a1532dba65685526be9b3b8e75da236db Mon Sep 17 00:00:00 2001 From: Brandon Amos Date: Mon, 23 Feb 2015 18:54:15 -0500 Subject: [PATCH 1/7] Fix link to @aliounedia's profile in README. Found with https://github.com/bamos/girl --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index c295aedc..14551b79 100755 --- a/README.md +++ b/README.md @@ -27,7 +27,7 @@ See CONTRIBUTING.md. * [AdriĆ  Mercader](http://amercader.net/) * [Dominik Moritz](https://github.com/domoritz) * [Friedrich Lindenberg](http://pudo.org/) -* [Alioune Dia](http://https://github.com/aliounedia) +* [Alioune Dia](https://github.com/aliounedia) * [kielni](https://github.com/kielni) * And [many more](https://github.com/okfn/recline/graphs/contributors) From bafe84060cd0ec4ccc9c7ad30c65f80c974c86da Mon Sep 17 00:00:00 2001 From: Matt Fullerton Date: Sat, 21 Mar 2015 10:38:42 +0100 Subject: [PATCH 2/7] [docs/demos] Fixes #400, Typos --- _includes/views-list.html | 12 +- docs/tutorial-multiview.markdown | 239 +++++++++++++++++++++++++++++++ docs/tutorial-views.markdown | 8 +- docs/tutorials.html | 5 + 4 files changed, 254 insertions(+), 10 deletions(-) create mode 100644 docs/tutorial-multiview.markdown diff --git a/_includes/views-list.html b/_includes/views-list.html index 36bb87ca..9cb68d3b 100644 --- a/_includes/views-list.html +++ b/_includes/views-list.html @@ -1,7 +1,7 @@ -* Grid (simple) -* Grid (SlickGrid) -* Map +* Grid (simple) +* Grid (SlickGrid) +* Map * Choropleth Map -* Graph -* Timeline -* Multiview (combines views) \ No newline at end of file +* Graph +* Timeline +* Multiview (combines views) \ No newline at end of file diff --git a/docs/tutorial-multiview.markdown b/docs/tutorial-multiview.markdown new file mode 100644 index 00000000..13df5205 --- /dev/null +++ b/docs/tutorial-multiview.markdown @@ -0,0 +1,239 @@ +--- +layout: container +title: Library - Multiview Tutorial +recline-deps: true +root: ../ +--- + + + +
+The full source code along with all dependencies for the tutorial can be found at this GitHub repository. Do not try to assemble a working example from the code snippets in this page! See it in action via GitHub Pages. + The code is almost identical to that used for the site demo, with the advantage of the code being separated out of the main recline website. You can also see Multiview in action in many CKAN-based data catalogs (resource views) and in the OKFN Labs DataDeck
+ +### Multiview + +Multiview, as its name suggests, combines multiple [Recline views](views.html) into one visualization. The different views are synced to one dataset. The [technical documentation for Multiview](src/view.multiview.html) details the nuts and bolts. + +When building a Multiview from scratch, it is advised to start by getting each individual view to work satisfactorily before combining into a Multiview, to aid debugging. + +### Preparing your page + +Before writing any code with Recline, you need to do the following preparation steps on your page: + +* [Download ReclineJS]({{page.root}}download.html) (downloading the master, developer code is recommended as this example is based on that and all dependencies should be available) or the all-in-one demo code. +* Include the Multiview CSS as well as the CSS for each view in the head section of your document, as well as any 3rd party CSS for each view e.g.: + {% highlight html %} + + + + + + + + + + + + + + + + + {% endhighlight %} + +* Include the relevant Javascript files somewhere on the page (preferably before body close tag). You will need to include any necessary Javascript dependencies for each view as well, e.g.: + {% highlight html %} + + + + + + + + + + + + + + + + + + + + + + + + + + {% endhighlight %} + +### Creating a Dataset + +Here's the function to create an example dataset we are going to work with: + +{% highlight javascript %} +function createDemoDataset() { + var dataset = new recline.Model.Dataset({ + records: [ + {id: 0, date: '2011-01-01', x: 1, y: 2, z: 3, country: 'DE', sometext: 'first', lat:52.56, lon:13.40}, + {id: 1, date: '2011-02-02', x: 2, y: 4, z: 24, country: 'UK', sometext: 'second', lat:54.97, lon:-1.60}, + {id: 2, date: '2011-03-03', x: 3, y: 6, z: 9, country: 'US', sometext: 'third', lat:40.00, lon:-75.5}, + {id: 3, date: '2011-04-04', x: 4, y: 8, z: 6, country: 'UK', sometext: 'fourth', lat:57.27, lon:-6.20}, + {id: 4, date: '2011-05-04', x: 5, y: 10, z: 15, country: 'UK', sometext: 'fifth', lat:51.58, lon:0}, + {id: 5, date: '2011-06-02', x: 6, y: 12, z: 18, country: 'DE', sometext: 'sixth', lat:51.04, lon:7.9} + ], + // let's be really explicit about fields + // Plus take opportunity to set date to be a date field and set some labels + fields: [ + {id: 'id'}, + {id: 'date', type: 'date'}, + {id: 'x', type: 'number'}, + {id: 'y', type: 'number'}, + {id: 'z', type: 'number'}, + {id: 'country', 'label': 'Country'}, + {id: 'sometext', 'label': 'Some text'}, + {id: 'lat'}, + {id: 'lon'} + ] + }); + return dataset; +} +{% endhighlight %} + +In this data we have 6 documents / rows. Each document is a javascript object +containing keys and values (note that all values here are 'simple' but there is +no reason you cannot have objects as values allowing you to nest data. + +### Setting up the Multiview + +To create a Multiview, we first create each view that we want to include, and include these in an array. A function to do everything for SlickGrid, Graph and Map views is shown below: + +{% highlight javascript %} +var createMultiView = function(dataset, state) { + // remove existing multiview if present + var reload = false; + if (window.multiView) { + window.multiView.remove(); + window.multiView = null; + reload = true; + } + + var $el = $('
'); + $el.appendTo(window.explorerDiv); + + // customize the subviews for the MultiView + var views = [ + { + id: 'grid', + label: 'Grid', + view: new recline.View.SlickGrid({ + model: dataset, + state: { + gridOptions: { + editable: true, + // Enable support for row add + enabledAddRow: true, + // Enable support for row delete + enabledDelRow: true, + // Enable support for row ReOrder + enableReOrderRow:true, + autoEdit: false, + enableCellNavigation: true + }, + columnsEditor: [ + { column: 'date', editor: Slick.Editors.Date }, + { column: 'sometext', editor: Slick.Editors.Text } + ] + } + }) + }, + { + id: 'graph', + label: 'Graph', + view: new recline.View.Graph({ + model: dataset + + }) + }, + { + id: 'map', + label: 'Map', + view: new recline.View.Map({ + model: dataset + }) + } + ]; + + var multiView = new recline.View.MultiView({ + model: dataset, + el: $el, + state: state, + views: views + }); + return multiView; +} +{% endhighlight %} + +To tie it all together: +{% highlight javascript %} +jQuery(function($) { + window.multiView = null; + window.explorerDiv = $('.data-explorer-here'); + + // create the demo dataset + var dataset = createDemoDataset(); + // now create the multiview + // this is rather more elaborate than the minimum as we configure the + // MultiView in various ways (see function below) + window.multiview = createMultiView(dataset); + + // last, we'll demonstrate binding to changes in the dataset + // this will print out a summary of each change onto the page in the + // changelog section + dataset.records.bind('all', function(name, obj) { + var $info = $('
'); + $info.html(name + ': ' + JSON.stringify(obj.toJSON())); + $('.changelog').append($info); + $('.changelog').show(); + }); +}); +{% endhighlight %} + +The HTML is very simple: +{% highlight html %} +
+ + +
+

Changes

+
+ +
+
+
+{% endhighlight %} diff --git a/docs/tutorial-views.markdown b/docs/tutorial-views.markdown index 8f850c30..05dfc922 100644 --- a/docs/tutorial-views.markdown +++ b/docs/tutorial-views.markdown @@ -21,15 +21,15 @@ Views display Recline datasets in different ways. This page covers the interesti Before writing any code with Recline, you need to do the following preparation steps on your page: -1. [Download ReclineJS]({{page.root}}download.html) and relevant dependencies. -2. Include the relevant CSS in the head section of your document: +* [Download ReclineJS]({{page.root}}download.html) and relevant dependencies. +* Include the relevant CSS in the head section of your document: {% highlight html %} {% endhighlight %} -3. Include the relevant Javascript files somewhere on the page (preferably before body close tag): +* Include the relevant Javascript files somewhere on the page (preferably before body close tag): {% highlight html %} @@ -50,7 +50,7 @@ You're now ready to start working with Recline. ### Creating a Dataset -Here's some example data We are going to work with: +Here's some example data we are going to work with: {% highlight javascript %} {% include data.js %} diff --git a/docs/tutorials.html b/docs/tutorials.html index 6ef1e832..35dae7df 100644 --- a/docs/tutorials.html +++ b/docs/tutorials.html @@ -53,6 +53,11 @@ root: ../

Views Quickstart - Grids, Graphs, Maps & Timelines

+

Doing more with maps

From 2527753db7a279170c3976256dbd717d1a9b9f30 Mon Sep 17 00:00:00 2001 From: Mariano Carballal Date: Tue, 24 Mar 2015 11:48:03 -0300 Subject: [PATCH 3/7] Fix #473. Javascript error when dataset column names includes special chars --- src/view.slickgrid.js | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/view.slickgrid.js b/src/view.slickgrid.js index 5e86b4bf..aed90744 100644 --- a/src/view.slickgrid.js +++ b/src/view.slickgrid.js @@ -161,7 +161,12 @@ my.SlickGrid = Backbone.View.extend({ } function sanitizeFieldName(name) { - var sanitized = $(name).text(); + var sanitized; + try{ + sanitized = $(name).text(); + } catch(e){ + sanitized = ''; + } return (name !== sanitized && sanitized !== '') ? sanitized : name; } From bb0d41a2e45cfc535b1304714061476b5cab16c2 Mon Sep 17 00:00:00 2001 From: Mariano Carballal Date: Tue, 31 Mar 2015 18:21:33 -0300 Subject: [PATCH 4/7] Issue #475: Fix to be able to graph columns with percentage values --- dist/recline.js | 2 +- dist/recline.min.js | 2 +- docs/src/view.flot.html | 2 +- docs/src/widget.pager.html | 2 +- docs/src/widget.queryeditor.html | 4 ++-- src/view.flot.js | 2 +- 6 files changed, 7 insertions(+), 7 deletions(-) diff --git a/dist/recline.js b/dist/recline.js index 0b8d2207..dee28c72 100644 --- a/dist/recline.js +++ b/dist/recline.js @@ -1381,7 +1381,7 @@ my.Flot = Backbone.View.extend({ } var yfield = self.model.fields.get(field); - var y = doc.getFieldValueUnrendered(yfield); + var y = parseFloat(doc.getFieldValueUnrendered(yfield)); if (self.state.attributes.graphType == 'bars') { points.push([y, x]); diff --git a/dist/recline.min.js b/dist/recline.min.js index 75568d4a..5a283054 100644 --- a/dist/recline.min.js +++ b/dist/recline.min.js @@ -1,4 +1,4 @@ -this.recline=this.recline||{};this.recline.Backend=this.recline.Backend||{};this.recline.Backend.DataProxy=this.recline.Backend.DataProxy||{};(function(my){"use strict";my.__type__="dataproxy";my.dataproxy_url="//jsonpdataproxy.appspot.com";my.timeout=5e3;var Deferred=typeof jQuery!=="undefined"&&jQuery.Deferred||_.Deferred;my.fetch=function(dataset){var data={url:dataset.url,"max-results":dataset.size||dataset.rows||1e3,type:dataset.format||""};var jqxhr=jQuery.ajax({url:my.dataproxy_url,data:data,dataType:"jsonp"});var dfd=new 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(args){dfd.reject(args)});return dfd.promise()};var _wrapInTimeout=function(ourFunction){var dfd=new Deferred;var timer=setTimeout(function(){dfd.reject({message:"Request Error: Backend did not respond after "+my.timeout/1e3+" seconds"})},my.timeout);ourFunction.done(function(args){clearTimeout(timer);dfd.resolve(args)}).fail(function(args){clearTimeout(timer);dfd.reject(args)});return dfd.promise()}})(this.recline.Backend.DataProxy);this.recline=this.recline||{};this.recline.Backend=this.recline.Backend||{};this.recline.Backend.Memory=this.recline.Backend.Memory||{};(function(my){"use strict";my.__type__="memory";var Deferred=typeof jQuery!=="undefined"&&jQuery.Deferred||_.Deferred;my.Store=function(records,fields){var self=this;this.records=records;this.data=this.records;if(fields){this.fields=fields}else{if(records){this.fields=_.map(records[0],function(value,key){return{id:key,type:"string"}})}}this.update=function(doc){_.each(self.records,function(internalDoc,idx){if(doc.id===internalDoc.id){self.records[idx]=doc}})};this.remove=function(doc){var newdocs=_.reject(self.records,function(internalDoc){return doc.id===internalDoc.id});this.records=newdocs};this.save=function(changes,dataset){var self=this;var dfd=new Deferred;_.each(changes.updates,function(record){self.update(record)});_.each(changes.deletes,function(record){self.remove(record)});dfd.resolve();return dfd.promise()},this.query=function(queryObj){var dfd=new Deferred;var numRows=queryObj.size||this.records.length;var start=queryObj.from||0;var results=this.records;results=this._applyFilters(results,queryObj);results=this._applyFreeTextQuery(results,queryObj);_.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()};this._applyFilters=function(results,queryObj){var filters=queryObj.filters;var filterFunctions={term:term,terms:terms,range:range,geo_distance:geo_distance};var dataParsers={integer:function(e){return parseFloat(e,10)},"float":function(e){return parseFloat(e,10)},number:function(e){return parseFloat(e,10)},string:function(e){return e.toString()},date:function(e){return moment(e).valueOf()},datetime:function(e){return new Date(e).valueOf()}};var keyedFields={};_.each(self.fields,function(field){keyedFields[field.id]=field});function getDataParser(filter){var fieldType=keyedFields[filter.field].type||"string";return dataParsers[fieldType]}return _.filter(results,function(record){var passes=_.map(filters,function(filter){return filterFunctions[filter.type](record,filter)});return _.all(passes,_.identity)});function term(record,filter){var parse=getDataParser(filter);var value=parse(record[filter.field]);var term=parse(filter.term);return value===term}function terms(record,filter){var parse=getDataParser(filter);var value=parse(record[filter.field]);var terms=parse(filter.terms).split(",");return _.indexOf(terms,value)>=0}function range(record,filter){var fromnull=_.isUndefined(filter.from)||filter.from===null||filter.from==="";var tonull=_.isUndefined(filter.to)||filter.to===null||filter.to==="";var parse=getDataParser(filter);var value=parse(record[filter.field]);var from=parse(fromnull?"":filter.from);var to=parse(tonull?"":filter.to);if((!fromnull||!tonull)&&value===""){return false}return(fromnull||value>=from)&&(tonull||value<=to)}function geo_distance(){}};this._applyFreeTextQuery=function(results,queryObj){if(queryObj.q){var terms=queryObj.q.split(" ");var patterns=_.map(terms,function(term){return new RegExp(term.toLowerCase())});results=_.filter(results,function(rawdoc){var matches=true;_.each(patterns,function(pattern){var foundmatch=false;_.each(self.fields,function(field){var value=rawdoc[field.id];if(value!==null&&value!==undefined){value=value.toString()}else{value=""}foundmatch=foundmatch||pattern.test(value.toLowerCase())});matches=matches&&foundmatch});return matches})}return results};this.computeFacets=function(records,queryObj){var facetResults={};if(!queryObj.facets){return facetResults}_.each(queryObj.facets,function(query,facetId){facetResults[facetId]=new recline.Model.Facet({id:facetId}).toJSON();facetResults[facetId].termsall={}});_.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){return-item.count});tmp.terms=tmp.terms.slice(0,10)});return facetResults}}})(this.recline.Backend.Memory);if(!("indexOf"in Array.prototype)){Array.prototype.indexOf=function(find,i){if(i===undefined)i=0;if(i<0)i+=this.length;if(i<0)i=0;for(var n=this.length;ithis.length-1)i=this.length-1;for(i++;i-->0;)if(i in this&&this[i]===find)return i;return-1}}if(!("forEach"in Array.prototype)){Array.prototype.forEach=function(action,that){for(var i=0,n=this.length;i0){if(records[0]instanceof Array){fields=records[0];records=records.slice(1)}else{fields=_.map(_.keys(records[0]),function(key){return{id:key}})}}if(fields&&fields.length>0&&(fields[0]===null||typeof fields[0]!="object")){var seen={};fields=_.map(fields,function(field,index){if(field===null){field=""}else{field=field.toString()}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}return{id:fieldId}})}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;return this._store.save(this._changes,this.toJSON())},query:function(queryObj){var self=this;var dfd=new Deferred;this.trigger("query:start");if(queryObj){var attributes=queryObj;if(queryObj instanceof my.Query){attributes=queryObj.toJSON()}this.queryState.set(attributes,{silent:true})}var actualQuery=this.queryState.toJSON();this._store.query(actualQuery,this.toJSON()).done(function(queryResult){self._handleResult(queryResult);self.trigger("query:done");dfd.resolve(self.records)}).fail(function(args){self.trigger("query:fail",args);dfd.reject(args)});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:function(){var self=this;var query=new my.Query;query.set({size:0});this.fields.each(function(field){query.addFacet(field.id)});var dfd=new 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);self.fields.get(facetId).facets.reset(facet)})}dfd.resolve(queryResult)});return dfd.promise()},recordSummary:function(record){return record.summary()},_backendFromString:function(backendString){var backend=null;if(recline&&recline.Backend){_.each(_.keys(recline.Backend),function(name){if(name.toLowerCase()===backendString.toLowerCase()){backend=recline.Backend[name]}})}return backend}});my.Record=Backbone.Model.extend({constructor:function Record(){Backbone.Model.prototype.constructor.apply(this,arguments)},initialize:function(){_.bindAll(this,"getFieldValue")},getFieldValue:function(field){var val=this.getFieldValueUnrendered(field);if(field&&!_.isUndefined(field.renderer)){val=field.renderer(val,field,this.toJSON())}return val},getFieldValueUnrendered:function(field){if(!field){return""}var val=this.get(field.id);if(field.deriver){val=field.deriver(val,field,this)}return val},summary:function(record){var self=this;var html='
';this.fields.each(function(field){if(field.id!="id"){html+='
'+field.get("label")+": "+self.getFieldValue(field)+"
"}});html+="
";return html},fetch:function(){},save:function(){},destroy:function(){this.trigger("destroy",this)}});my.RecordList=Backbone.Collection.extend({constructor:function RecordList(){Backbone.Collection.prototype.constructor.apply(this,arguments)},model:my.Record});my.Field=Backbone.Model.extend({constructor:function Field(){Backbone.Model.prototype.constructor.apply(this,arguments)},defaults:{label:null,type:"string",format:null,is_derived:false},initialize:function(data,options){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(this.attributes.type.toLowerCase()in this._typeMap){this.attributes.type=this._typeMap[this.attributes.type.toLowerCase()]}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},_typeMap:{text:"string","double":"number","float":"number",numeric:"number","int":"integer",datetime:"date-time",bool:"boolean",timestamp:"date-time",json:"object"},defaultRenderers:{object:function(val,field,doc){return JSON.stringify(val)},geo_point:function(val,field,doc){return JSON.stringify(val)},number: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{if(val&&typeof val==="string"){val=val.replace(/(https?:\/\/[^ ]+)/g,'$1')}return val}}}});my.FieldList=Backbone.Collection.extend({constructor:function FieldList(){Backbone.Collection.prototype.constructor.apply(this,arguments)},model:my.Field});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",field:"",term:""},range:{type:"range",from:"",to:""},geo_distance:{type:"geo_distance",distance:10,unit:"km",point:{lon:0,lat:0}}},addFilter:function(filter){var ourfilter=JSON.parse(JSON.stringify(filter));if(_.keys(filter).length<=3){ourfilter=_.defaults(ourfilter,this._filterTemplates[filter.type])}var filters=this.get("filters");filters.push(ourfilter);this.trigger("change:filters:new-blank")},replaceFilter:function(filter){var filters=this.get("filters");var idx=-1;_.each(this.get("filters"),function(f,key,list){if(filter.field==f.field){idx=key}});if(idx>=0){filters.splice(idx,1);this.set({filters:filters})}this.addFilter(filter)},updateFilter:function(index,value){},removeFilter:function(filterIndex){var filters=this.get("filters");filters.splice(filterIndex,1);this.set({filters:filters});this.trigger("change")},addFacet:function(fieldId,size,silent){var facets=this.get("facets");if(_.contains(_.keys(facets),fieldId)){return}facets[fieldId]={terms:{field:fieldId}};if(!_.isUndefined(size)){facets[fieldId].terms.size=size}this.set({facets:facets},{silent:true});if(!silent){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)},removeFacet:function(fieldId){var facets=this.get("facets");if(!_.contains(_.keys(facets),fieldId)){return}delete facets[fieldId];this.set({facets:facets},{silent:true});this.trigger("facet:remove",this)},clearFacets:function(){var facets=this.get("facets");_.each(_.keys(facets),function(fieldId){delete facets[fieldId]});this.trigger("facet:remove",this)},refreshFacets:function(){this.trigger("facet:add",this)}});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,terms:[]}}});my.FacetList=Backbone.Collection.extend({constructor:function FacetList(){Backbone.Collection.prototype.constructor.apply(this,arguments)},model:my.Facet});my.ObjectState=Backbone.Model.extend({})})(this.recline.Model);this.recline=this.recline||{};this.recline.View=this.recline.View||{};(function($,my){"use strict";my.Flot=Backbone.View.extend({template:'

Hey there!

There\'s no graph here yet because we don\'t know what fields you\'d like to see plotted.

Please tell us by using the menu on the right and a graph will automatically appear.

',initialize:function(options){var self=this;this.graphColors=["#edc240","#afd8f8","#cb4b4b","#4da74d","#9440ed"];_.bindAll(this,"render","redraw","_toolTip","_xaxisLabel");this.needToRedraw=false;this.listenTo(this.model,"change",this.render);this.listenTo(this.model.fields,"reset add",this.render);this.listenTo(this.model.records,"reset add",this.redraw);var stateData=_.extend({group:null,series:[],graphType:"lines-and-points"},options.state);this.state=new recline.Model.ObjectState(stateData);this.previousTooltipPoint={x:null,y:null};this.editor=new my.FlotControls({model:this.model,state:this.state.toJSON()});this.listenTo(this.editor.state,"change",function(){self.state.set(self.editor.state.toJSON());self.redraw()});this.elSidebar=this.editor.$el},render:function(){var self=this;var tmplData=this.model.toTemplateJSON();var htmls=Mustache.render(this.template,tmplData);this.$el.html(htmls);this.$graph=this.$el.find(".panel.graph");this.$graph.on("plothover",this._toolTip);return this},remove:function(){this.editor.remove();Backbone.View.prototype.remove.apply(this,arguments)},redraw:function(){var areWeVisible=!jQuery.expr.filters.hidden(this.el);if(!areWeVisible||this.model.records.length===0){this.needToRedraw=true;return}if(this.state.get("group")&&this.state.get("series")){var series=this.createSeries();var options=this.getGraphOptions(this.state.attributes.graphType,series[0].data.length);this.plot=$.plot(this.$graph,series,options)}},show:function(){if(this.needToRedraw){this.redraw()}},_toolTip:function(event,pos,item){if(item){if(this.previousTooltipPoint.x!==item.dataIndex||this.previousTooltipPoint.y!==item.seriesIndex){this.previousTooltipPoint.x=item.dataIndex;this.previousTooltipPoint.y=item.seriesIndex;$("#recline-flot-tooltip").remove();var x=item.datapoint[0].toFixed(2),y=item.datapoint[1].toFixed(2);if(this.state.attributes.graphType==="bars"){x=item.datapoint[1].toFixed(2),y=item.datapoint[0].toFixed(2)}var content=_.template("<%= group %> = <%= x %>, <%= series %> = <%= y %>",{group:this.state.attributes.group,x:this._xaxisLabel(x),series:item.series.label,y:y});var xLocation,yLocation;if(this.state.attributes.graphType==="bars"){xLocation=item.pageX+15;yLocation=item.pageY-10}else if(this.state.attributes.graphType==="columns"){xLocation=item.pageX+15;yLocation=item.pageY}else{xLocation=item.pageX+10;yLocation=item.pageY-20}$('
'+content+"
").css({top:yLocation,left:xLocation}).appendTo("body").fadeIn(200)}}else{$("#recline-flot-tooltip").remove();this.previousTooltipPoint.x=null;this.previousTooltipPoint.y=null}},_xaxisLabel:function(x){if(this._groupFieldIsDateTime()){x=new Date(parseFloat(x)).toLocaleDateString()}else if(this.xvaluesAreIndex){x=parseInt(x,10);x=this.model.records.models[x].get(this.state.attributes.group)}return x},getGraphOptions:function(typeId,numPoints){var self=this;var groupFieldIsDateTime=self._groupFieldIsDateTime();var xaxis={};if(!groupFieldIsDateTime){xaxis.tickFormatter=function(x){var label=self._xaxisLabel(x)||"";if(typeof label!=="string"){label=label.toString()}if(self.state.attributes.graphType!=="bars"&&label.length>10){label=label.slice(0,10)+"..."}return label}}if(this.xvaluesAreIndex){var numTicks=Math.min(this.model.records.length,15);var increment=this.model.records.length/numTicks;var ticks=[];for(var i=0;i
',templateSeriesEditor:'
',events:{"change form select":"onEditorSubmit","click .editor-add":"_onAddSeries","click .action-remove-series":"removeSeries"},initialize:function(options){var self=this;_.bindAll(this,"render");this.listenTo(this.model.fields,"reset add",this.render);this.state=new recline.Model.ObjectState(options.state);this.render()},render:function(){var self=this;var tmplData=this.model.toTemplateJSON();var htmls=Mustache.render(this.template,tmplData);this.$el.html(htmls);if(this.state.get("graphType")){this._selectOption(".editor-type",this.state.get("graphType"))}if(this.state.get("group")){this._selectOption(".editor-group",this.state.get("group"))}var tmpSeries=[""];if(this.state.get("series").length>0){tmpSeries=this.state.get("series")}_.each(tmpSeries,function(series,idx){self.addSeries(idx);self._selectOption(".editor-series.js-series-"+idx,series)});return this},_selectOption:function(id,value){var options=this.$el.find(id+" select > option");if(options){options.each(function(opt){if(this.value==value){$(this).attr("selected","selected");return false}})}},onEditorSubmit:function(e){var select=this.$el.find(".editor-group select");var $editor=this;var $series=this.$el.find(".editor-series select");var series=$series.map(function(){return $(this).val()});var updatedState={series:$.makeArray(series),group:this.$el.find(".editor-group select").val(),graphType:this.$el.find(".editor-type select").val()};this.state.set(updatedState)},addSeries:function(idx){var data=_.extend({seriesIndex:idx,seriesName:String.fromCharCode(idx+64+1)},this.model.toTemplateJSON());var htmls=Mustache.render(this.templateSeriesEditor,data);this.$el.find(".editor-series-group").append(htmls);return this},_onAddSeries:function(e){e.preventDefault();this.addSeries(this.state.get("series").length)},removeSeries:function(e){e.preventDefault();var $el=$(e.target);$el.parent().parent().remove();this.onEditorSubmit()}})})(jQuery,recline.View);this.recline=this.recline||{};this.recline.View=this.recline.View||{};this.recline.View.Graph=this.recline.View.Flot;this.recline.View.GraphControls=this.recline.View.FlotControls;this.recline=this.recline||{};this.recline.View=this.recline.View||{};(function($,my){"use strict";my.Grid=Backbone.View.extend({tagName:"div",className:"recline-grid-container",initialize:function(modelEtc){var self=this;_.bindAll(this,"render","onHorizontalScroll");this.listenTo(this.model.records,"add reset remove",this.render);this.tempState={};var state=_.extend({hiddenFields:[]},modelEtc.state);this.state=new recline.Model.ObjectState(state)},events:{},setColumnSort:function(order){var sort=[{}];sort[0][this.tempState.currentColumn]={order:order};this.model.query({sort:sort})},hideColumn:function(){var hiddenFields=this.state.get("hiddenFields");hiddenFields.push(this.tempState.currentColumn);this.state.set({hiddenFields:hiddenFields});this.state.trigger("change");this.render()},showColumn:function(e){var hiddenFields=_.without(this.state.get("hiddenFields"),$(e.target).data("column"));this.state.set({hiddenFields:hiddenFields});this.render()},onHorizontalScroll:function(e){var currentScroll=$(e.target).scrollLeft();this.$el.find(".recline-grid thead tr").scrollLeft(currentScroll)},template:'
{{#fields}} {{/fields}}
{{label}}
',toTemplateJSON:function(){var self=this;var modelData=this.model.toJSON();modelData.notEmpty=this.fields.length>0;modelData.fields=this.fields.map(function(field){return field.toJSON()});modelData.lastHeaderWidth=this.scrollbarDimensions.width-2;return modelData},render:function(){var self=this;this.fields=new recline.Model.FieldList(this.model.fields.filter(function(field){return _.indexOf(self.state.get("hiddenFields"),field.id)==-1}));this.scrollbarDimensions=this.scrollbarDimensions||this._scrollbarSize();var numFields=this.fields.length;var fullWidth=self.$el.width()-20-10*numFields-this.scrollbarDimensions.width;var width=parseInt(Math.max(50,fullWidth/numFields),10);var remainder=Math.max(fullWidth-numFields*width,0);this.fields.each(function(field,idx){if(idx===0){field.set({width:width+remainder})}else{field.set({width:width})}});var htmls=Mustache.render(this.template,this.toTemplateJSON());this.$el.html(htmls);this.model.records.forEach(function(doc){var tr=$("");self.$el.find("tbody").append(tr);var newView=new my.GridRow({model:doc,el:tr,fields:self.fields});newView.render()});var $tbody=this.$el.find("tbody")[0];if($tbody.scrollHeight<=$tbody.offsetHeight){this.$el.find("th.last-header").hide()}this.$el.find(".recline-grid").toggleClass("no-hidden",self.state.get("hiddenFields").length===0);this.$el.find(".recline-grid tbody").scroll(this.onHorizontalScroll);return this},_scrollbarSize:function(){var $c=$("
").appendTo("body");var dim={width:$c.width()-$c[0].clientWidth+1,height:$c.height()-$c[0].clientHeight};$c.remove();return dim}});my.GridRow=Backbone.View.extend({initialize:function(initData){_.bindAll(this,"render");this._fields=initData.fields;this.listenTo(this.model,"change",this.render)},template:' {{#cells}}
 
{{{value}}}
{{/cells}} ',events:{"click .data-table-cell-edit":"onEditClick","click .data-table-cell-editor .okButton":"onEditorOK","click .data-table-cell-editor .cancelButton":"onEditorCancel"},toTemplateJSON:function(){var self=this;var doc=this.model;var cellData=this._fields.map(function(field){return{field:field.id,width:field.get("width"),value:doc.getFieldValue(field)}});return{id:this.id,cells:cellData}},render:function(){this.$el.attr("data-id",this.model.id);var html=Mustache.render(this.template,this.toTemplateJSON());this.$el.html(html);return this},cellEditorTemplate:' ', +this.recline=this.recline||{};this.recline.Backend=this.recline.Backend||{};this.recline.Backend.DataProxy=this.recline.Backend.DataProxy||{};(function(my){"use strict";my.__type__="dataproxy";my.dataproxy_url="//jsonpdataproxy.appspot.com";my.timeout=5e3;var Deferred=typeof jQuery!=="undefined"&&jQuery.Deferred||_.Deferred;my.fetch=function(dataset){var data={url:dataset.url,"max-results":dataset.size||dataset.rows||1e3,type:dataset.format||""};var jqxhr=jQuery.ajax({url:my.dataproxy_url,data:data,dataType:"jsonp"});var dfd=new 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(args){dfd.reject(args)});return dfd.promise()};var _wrapInTimeout=function(ourFunction){var dfd=new Deferred;var timer=setTimeout(function(){dfd.reject({message:"Request Error: Backend did not respond after "+my.timeout/1e3+" seconds"})},my.timeout);ourFunction.done(function(args){clearTimeout(timer);dfd.resolve(args)}).fail(function(args){clearTimeout(timer);dfd.reject(args)});return dfd.promise()}})(this.recline.Backend.DataProxy);this.recline=this.recline||{};this.recline.Backend=this.recline.Backend||{};this.recline.Backend.Memory=this.recline.Backend.Memory||{};(function(my){"use strict";my.__type__="memory";var Deferred=typeof jQuery!=="undefined"&&jQuery.Deferred||_.Deferred;my.Store=function(records,fields){var self=this;this.records=records;this.data=this.records;if(fields){this.fields=fields}else{if(records){this.fields=_.map(records[0],function(value,key){return{id:key,type:"string"}})}}this.update=function(doc){_.each(self.records,function(internalDoc,idx){if(doc.id===internalDoc.id){self.records[idx]=doc}})};this.remove=function(doc){var newdocs=_.reject(self.records,function(internalDoc){return doc.id===internalDoc.id});this.records=newdocs};this.save=function(changes,dataset){var self=this;var dfd=new Deferred;_.each(changes.updates,function(record){self.update(record)});_.each(changes.deletes,function(record){self.remove(record)});dfd.resolve();return dfd.promise()},this.query=function(queryObj){var dfd=new Deferred;var numRows=queryObj.size||this.records.length;var start=queryObj.from||0;var results=this.records;results=this._applyFilters(results,queryObj);results=this._applyFreeTextQuery(results,queryObj);_.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()};this._applyFilters=function(results,queryObj){var filters=queryObj.filters;var filterFunctions={term:term,terms:terms,range:range,geo_distance:geo_distance};var dataParsers={integer:function(e){return parseFloat(e,10)},"float":function(e){return parseFloat(e,10)},number:function(e){return parseFloat(e,10)},string:function(e){return e.toString()},date:function(e){return moment(e).valueOf()},datetime:function(e){return new Date(e).valueOf()}};var keyedFields={};_.each(self.fields,function(field){keyedFields[field.id]=field});function getDataParser(filter){var fieldType=keyedFields[filter.field].type||"string";return dataParsers[fieldType]}return _.filter(results,function(record){var passes=_.map(filters,function(filter){return filterFunctions[filter.type](record,filter)});return _.all(passes,_.identity)});function term(record,filter){var parse=getDataParser(filter);var value=parse(record[filter.field]);var term=parse(filter.term);return value===term}function terms(record,filter){var parse=getDataParser(filter);var value=parse(record[filter.field]);var terms=parse(filter.terms).split(",");return _.indexOf(terms,value)>=0}function range(record,filter){var fromnull=_.isUndefined(filter.from)||filter.from===null||filter.from==="";var tonull=_.isUndefined(filter.to)||filter.to===null||filter.to==="";var parse=getDataParser(filter);var value=parse(record[filter.field]);var from=parse(fromnull?"":filter.from);var to=parse(tonull?"":filter.to);if((!fromnull||!tonull)&&value===""){return false}return(fromnull||value>=from)&&(tonull||value<=to)}function geo_distance(){}};this._applyFreeTextQuery=function(results,queryObj){if(queryObj.q){var terms=queryObj.q.split(" ");var patterns=_.map(terms,function(term){return new RegExp(term.toLowerCase())});results=_.filter(results,function(rawdoc){var matches=true;_.each(patterns,function(pattern){var foundmatch=false;_.each(self.fields,function(field){var value=rawdoc[field.id];if(value!==null&&value!==undefined){value=value.toString()}else{value=""}foundmatch=foundmatch||pattern.test(value.toLowerCase())});matches=matches&&foundmatch});return matches})}return results};this.computeFacets=function(records,queryObj){var facetResults={};if(!queryObj.facets){return facetResults}_.each(queryObj.facets,function(query,facetId){facetResults[facetId]=new recline.Model.Facet({id:facetId}).toJSON();facetResults[facetId].termsall={}});_.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){return-item.count});tmp.terms=tmp.terms.slice(0,10)});return facetResults}}})(this.recline.Backend.Memory);if(!("indexOf"in Array.prototype)){Array.prototype.indexOf=function(find,i){if(i===undefined)i=0;if(i<0)i+=this.length;if(i<0)i=0;for(var n=this.length;ithis.length-1)i=this.length-1;for(i++;i-->0;)if(i in this&&this[i]===find)return i;return-1}}if(!("forEach"in Array.prototype)){Array.prototype.forEach=function(action,that){for(var i=0,n=this.length;i0){if(records[0]instanceof Array){fields=records[0];records=records.slice(1)}else{fields=_.map(_.keys(records[0]),function(key){return{id:key}})}}if(fields&&fields.length>0&&(fields[0]===null||typeof fields[0]!="object")){var seen={};fields=_.map(fields,function(field,index){if(field===null){field=""}else{field=field.toString()}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}return{id:fieldId}})}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;return this._store.save(this._changes,this.toJSON())},query:function(queryObj){var self=this;var dfd=new Deferred;this.trigger("query:start");if(queryObj){var attributes=queryObj;if(queryObj instanceof my.Query){attributes=queryObj.toJSON()}this.queryState.set(attributes,{silent:true})}var actualQuery=this.queryState.toJSON();this._store.query(actualQuery,this.toJSON()).done(function(queryResult){self._handleResult(queryResult);self.trigger("query:done");dfd.resolve(self.records)}).fail(function(args){self.trigger("query:fail",args);dfd.reject(args)});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:function(){var self=this;var query=new my.Query;query.set({size:0});this.fields.each(function(field){query.addFacet(field.id)});var dfd=new 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);self.fields.get(facetId).facets.reset(facet)})}dfd.resolve(queryResult)});return dfd.promise()},recordSummary:function(record){return record.summary()},_backendFromString:function(backendString){var backend=null;if(recline&&recline.Backend){_.each(_.keys(recline.Backend),function(name){if(name.toLowerCase()===backendString.toLowerCase()){backend=recline.Backend[name]}})}return backend}});my.Record=Backbone.Model.extend({constructor:function Record(){Backbone.Model.prototype.constructor.apply(this,arguments)},initialize:function(){_.bindAll(this,"getFieldValue")},getFieldValue:function(field){var val=this.getFieldValueUnrendered(field);if(field&&!_.isUndefined(field.renderer)){val=field.renderer(val,field,this.toJSON())}return val},getFieldValueUnrendered:function(field){if(!field){return""}var val=this.get(field.id);if(field.deriver){val=field.deriver(val,field,this)}return val},summary:function(record){var self=this;var html='
';this.fields.each(function(field){if(field.id!="id"){html+='
'+field.get("label")+": "+self.getFieldValue(field)+"
"}});html+="
";return html},fetch:function(){},save:function(){},destroy:function(){this.trigger("destroy",this)}});my.RecordList=Backbone.Collection.extend({constructor:function RecordList(){Backbone.Collection.prototype.constructor.apply(this,arguments)},model:my.Record});my.Field=Backbone.Model.extend({constructor:function Field(){Backbone.Model.prototype.constructor.apply(this,arguments)},defaults:{label:null,type:"string",format:null,is_derived:false},initialize:function(data,options){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(this.attributes.type.toLowerCase()in this._typeMap){this.attributes.type=this._typeMap[this.attributes.type.toLowerCase()]}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},_typeMap:{text:"string","double":"number","float":"number",numeric:"number","int":"integer",datetime:"date-time",bool:"boolean",timestamp:"date-time",json:"object"},defaultRenderers:{object:function(val,field,doc){return JSON.stringify(val)},geo_point:function(val,field,doc){return JSON.stringify(val)},number: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{if(val&&typeof val==="string"){val=val.replace(/(https?:\/\/[^ ]+)/g,'$1')}return val}}}});my.FieldList=Backbone.Collection.extend({constructor:function FieldList(){Backbone.Collection.prototype.constructor.apply(this,arguments)},model:my.Field});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",field:"",term:""},range:{type:"range",from:"",to:""},geo_distance:{type:"geo_distance",distance:10,unit:"km",point:{lon:0,lat:0}}},addFilter:function(filter){var ourfilter=JSON.parse(JSON.stringify(filter));if(_.keys(filter).length<=3){ourfilter=_.defaults(ourfilter,this._filterTemplates[filter.type])}var filters=this.get("filters");filters.push(ourfilter);this.trigger("change:filters:new-blank")},replaceFilter:function(filter){var filters=this.get("filters");var idx=-1;_.each(this.get("filters"),function(f,key,list){if(filter.field==f.field){idx=key}});if(idx>=0){filters.splice(idx,1);this.set({filters:filters})}this.addFilter(filter)},updateFilter:function(index,value){},removeFilter:function(filterIndex){var filters=this.get("filters");filters.splice(filterIndex,1);this.set({filters:filters});this.trigger("change")},addFacet:function(fieldId,size,silent){var facets=this.get("facets");if(_.contains(_.keys(facets),fieldId)){return}facets[fieldId]={terms:{field:fieldId}};if(!_.isUndefined(size)){facets[fieldId].terms.size=size}this.set({facets:facets},{silent:true});if(!silent){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)},removeFacet:function(fieldId){var facets=this.get("facets");if(!_.contains(_.keys(facets),fieldId)){return}delete facets[fieldId];this.set({facets:facets},{silent:true});this.trigger("facet:remove",this)},clearFacets:function(){var facets=this.get("facets");_.each(_.keys(facets),function(fieldId){delete facets[fieldId]});this.trigger("facet:remove",this)},refreshFacets:function(){this.trigger("facet:add",this)}});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,terms:[]}}});my.FacetList=Backbone.Collection.extend({constructor:function FacetList(){Backbone.Collection.prototype.constructor.apply(this,arguments)},model:my.Facet});my.ObjectState=Backbone.Model.extend({})})(this.recline.Model);this.recline=this.recline||{};this.recline.View=this.recline.View||{};(function($,my){"use strict";my.Flot=Backbone.View.extend({template:'

Hey there!

There\'s no graph here yet because we don\'t know what fields you\'d like to see plotted.

Please tell us by using the menu on the right and a graph will automatically appear.

',initialize:function(options){var self=this;this.graphColors=["#edc240","#afd8f8","#cb4b4b","#4da74d","#9440ed"];_.bindAll(this,"render","redraw","_toolTip","_xaxisLabel");this.needToRedraw=false;this.listenTo(this.model,"change",this.render);this.listenTo(this.model.fields,"reset add",this.render);this.listenTo(this.model.records,"reset add",this.redraw);var stateData=_.extend({group:null,series:[],graphType:"lines-and-points"},options.state);this.state=new recline.Model.ObjectState(stateData);this.previousTooltipPoint={x:null,y:null};this.editor=new my.FlotControls({model:this.model,state:this.state.toJSON()});this.listenTo(this.editor.state,"change",function(){self.state.set(self.editor.state.toJSON());self.redraw()});this.elSidebar=this.editor.$el},render:function(){var self=this;var tmplData=this.model.toTemplateJSON();var htmls=Mustache.render(this.template,tmplData);this.$el.html(htmls);this.$graph=this.$el.find(".panel.graph");this.$graph.on("plothover",this._toolTip);return this},remove:function(){this.editor.remove();Backbone.View.prototype.remove.apply(this,arguments)},redraw:function(){var areWeVisible=!jQuery.expr.filters.hidden(this.el);if(!areWeVisible||this.model.records.length===0){this.needToRedraw=true;return}if(this.state.get("group")&&this.state.get("series")){var series=this.createSeries();var options=this.getGraphOptions(this.state.attributes.graphType,series[0].data.length);this.plot=$.plot(this.$graph,series,options)}},show:function(){if(this.needToRedraw){this.redraw()}},_toolTip:function(event,pos,item){if(item){if(this.previousTooltipPoint.x!==item.dataIndex||this.previousTooltipPoint.y!==item.seriesIndex){this.previousTooltipPoint.x=item.dataIndex;this.previousTooltipPoint.y=item.seriesIndex;$("#recline-flot-tooltip").remove();var x=item.datapoint[0].toFixed(2),y=item.datapoint[1].toFixed(2);if(this.state.attributes.graphType==="bars"){x=item.datapoint[1].toFixed(2),y=item.datapoint[0].toFixed(2)}var content=_.template("<%= group %> = <%= x %>, <%= series %> = <%= y %>",{group:this.state.attributes.group,x:this._xaxisLabel(x),series:item.series.label,y:y});var xLocation,yLocation;if(this.state.attributes.graphType==="bars"){xLocation=item.pageX+15;yLocation=item.pageY-10}else if(this.state.attributes.graphType==="columns"){xLocation=item.pageX+15;yLocation=item.pageY}else{xLocation=item.pageX+10;yLocation=item.pageY-20}$('
'+content+"
").css({top:yLocation,left:xLocation}).appendTo("body").fadeIn(200)}}else{$("#recline-flot-tooltip").remove();this.previousTooltipPoint.x=null;this.previousTooltipPoint.y=null}},_xaxisLabel:function(x){if(this._groupFieldIsDateTime()){x=new Date(parseFloat(x)).toLocaleDateString()}else if(this.xvaluesAreIndex){x=parseInt(x,10);x=this.model.records.models[x].get(this.state.attributes.group)}return x},getGraphOptions:function(typeId,numPoints){var self=this;var groupFieldIsDateTime=self._groupFieldIsDateTime();var xaxis={};if(!groupFieldIsDateTime){xaxis.tickFormatter=function(x){var label=self._xaxisLabel(x)||"";if(typeof label!=="string"){label=label.toString()}if(self.state.attributes.graphType!=="bars"&&label.length>10){label=label.slice(0,10)+"..."}return label}}if(this.xvaluesAreIndex){var numTicks=Math.min(this.model.records.length,15);var increment=this.model.records.length/numTicks;var ticks=[];for(var i=0;i
',templateSeriesEditor:'
',events:{"change form select":"onEditorSubmit","click .editor-add":"_onAddSeries","click .action-remove-series":"removeSeries"},initialize:function(options){var self=this;_.bindAll(this,"render");this.listenTo(this.model.fields,"reset add",this.render);this.state=new recline.Model.ObjectState(options.state);this.render()},render:function(){var self=this;var tmplData=this.model.toTemplateJSON();var htmls=Mustache.render(this.template,tmplData);this.$el.html(htmls);if(this.state.get("graphType")){this._selectOption(".editor-type",this.state.get("graphType"))}if(this.state.get("group")){this._selectOption(".editor-group",this.state.get("group"))}var tmpSeries=[""];if(this.state.get("series").length>0){tmpSeries=this.state.get("series")}_.each(tmpSeries,function(series,idx){self.addSeries(idx);self._selectOption(".editor-series.js-series-"+idx,series)});return this},_selectOption:function(id,value){var options=this.$el.find(id+" select > option");if(options){options.each(function(opt){if(this.value==value){$(this).attr("selected","selected");return false}})}},onEditorSubmit:function(e){var select=this.$el.find(".editor-group select");var $editor=this;var $series=this.$el.find(".editor-series select");var series=$series.map(function(){return $(this).val()});var updatedState={series:$.makeArray(series),group:this.$el.find(".editor-group select").val(),graphType:this.$el.find(".editor-type select").val()};this.state.set(updatedState)},addSeries:function(idx){var data=_.extend({seriesIndex:idx,seriesName:String.fromCharCode(idx+64+1)},this.model.toTemplateJSON());var htmls=Mustache.render(this.templateSeriesEditor,data);this.$el.find(".editor-series-group").append(htmls);return this},_onAddSeries:function(e){e.preventDefault();this.addSeries(this.state.get("series").length)},removeSeries:function(e){e.preventDefault();var $el=$(e.target);$el.parent().parent().remove();this.onEditorSubmit()}})})(jQuery,recline.View);this.recline=this.recline||{};this.recline.View=this.recline.View||{};this.recline.View.Graph=this.recline.View.Flot;this.recline.View.GraphControls=this.recline.View.FlotControls;this.recline=this.recline||{};this.recline.View=this.recline.View||{};(function($,my){"use strict";my.Grid=Backbone.View.extend({tagName:"div",className:"recline-grid-container",initialize:function(modelEtc){var self=this;_.bindAll(this,"render","onHorizontalScroll");this.listenTo(this.model.records,"add reset remove",this.render);this.tempState={};var state=_.extend({hiddenFields:[]},modelEtc.state);this.state=new recline.Model.ObjectState(state)},events:{},setColumnSort:function(order){var sort=[{}];sort[0][this.tempState.currentColumn]={order:order};this.model.query({sort:sort})},hideColumn:function(){var hiddenFields=this.state.get("hiddenFields");hiddenFields.push(this.tempState.currentColumn);this.state.set({hiddenFields:hiddenFields});this.state.trigger("change");this.render()},showColumn:function(e){var hiddenFields=_.without(this.state.get("hiddenFields"),$(e.target).data("column"));this.state.set({hiddenFields:hiddenFields});this.render()},onHorizontalScroll:function(e){var currentScroll=$(e.target).scrollLeft();this.$el.find(".recline-grid thead tr").scrollLeft(currentScroll)},template:'
{{#fields}} {{/fields}}
{{label}}
',toTemplateJSON:function(){var self=this;var modelData=this.model.toJSON();modelData.notEmpty=this.fields.length>0;modelData.fields=this.fields.map(function(field){return field.toJSON()});modelData.lastHeaderWidth=this.scrollbarDimensions.width-2;return modelData},render:function(){var self=this;this.fields=new recline.Model.FieldList(this.model.fields.filter(function(field){return _.indexOf(self.state.get("hiddenFields"),field.id)==-1}));this.scrollbarDimensions=this.scrollbarDimensions||this._scrollbarSize();var numFields=this.fields.length;var fullWidth=self.$el.width()-20-10*numFields-this.scrollbarDimensions.width;var width=parseInt(Math.max(50,fullWidth/numFields),10);var remainder=Math.max(fullWidth-numFields*width,0);this.fields.each(function(field,idx){if(idx===0){field.set({width:width+remainder})}else{field.set({width:width})}});var htmls=Mustache.render(this.template,this.toTemplateJSON());this.$el.html(htmls);this.model.records.forEach(function(doc){var tr=$("");self.$el.find("tbody").append(tr);var newView=new my.GridRow({model:doc,el:tr,fields:self.fields});newView.render()});var $tbody=this.$el.find("tbody")[0];if($tbody.scrollHeight<=$tbody.offsetHeight){this.$el.find("th.last-header").hide()}this.$el.find(".recline-grid").toggleClass("no-hidden",self.state.get("hiddenFields").length===0);this.$el.find(".recline-grid tbody").scroll(this.onHorizontalScroll);return this},_scrollbarSize:function(){var $c=$("
").appendTo("body");var dim={width:$c.width()-$c[0].clientWidth+1,height:$c.height()-$c[0].clientHeight};$c.remove();return dim}});my.GridRow=Backbone.View.extend({initialize:function(initData){_.bindAll(this,"render");this._fields=initData.fields;this.listenTo(this.model,"change",this.render)},template:' {{#cells}}
 
{{{value}}}
{{/cells}} ',events:{"click .data-table-cell-edit":"onEditClick","click .data-table-cell-editor .okButton":"onEditorOK","click .data-table-cell-editor .cancelButton":"onEditorCancel"},toTemplateJSON:function(){var self=this;var doc=this.model;var cellData=this._fields.map(function(field){return{field:field.id,width:field.get("width"),value:doc.getFieldValue(field)}});return{id:this.id,cells:cellData}},render:function(){this.$el.attr("data-id",this.model.id);var html=Mustache.render(this.template,this.toTemplateJSON());this.$el.html(html);return this},cellEditorTemplate:' ', onEditClick:function(e){var editing=this.$el.find(".data-table-cell-editor-editor");if(editing.length>0){editing.parents(".data-table-cell-value").html(editing.text()).siblings(".data-table-cell-edit").removeClass("hidden")}$(e.target).addClass("hidden");var cell=$(e.target).siblings(".data-table-cell-value");cell.data("previousContents",cell.text());var templated=Mustache.render(this.cellEditorTemplate,{value:cell.text()});cell.html(templated)},onEditorOK:function(e){var self=this;var cell=$(e.target);var rowId=cell.parents("tr").attr("data-id");var field=cell.parents("td").attr("data-field");var newValue=cell.parents(".data-table-cell-editor").find(".data-table-cell-editor-editor").val();var newData={};newData[field]=newValue;this.model.set(newData);this.trigger("recline:flash",{message:"Updating row...",loader:true});this.model.save().then(function(response){this.trigger("recline:flash",{message:"Row updated successfully",category:"success"})}).fail(function(){this.trigger("recline:flash",{message:"Error saving row",category:"error",persist:true})})},onEditorCancel:function(e){var cell=$(e.target).parents(".data-table-cell-value");cell.html(cell.data("previousContents")).siblings(".data-table-cell-edit").removeClass("hidden")}})})(jQuery,recline.View);this.recline=this.recline||{};this.recline.View=this.recline.View||{};(function($,my){"use strict";my.Map=Backbone.View.extend({template:'
',latitudeFieldNames:["lat","latitude"],longitudeFieldNames:["lon","longitude"],geometryFieldNames:["geojson","geom","the_geom","geometry","spatial","location","geo","lonlat"],initialize:function(options){var self=this;this.visible=this.$el.is(":visible");this.mapReady=false;this.map=null;var stateData=_.extend({geomField:null,lonField:null,latField:null,autoZoom:true,cluster:false},options.state);this.state=new recline.Model.ObjectState(stateData);this._clusterOptions={zoomToBoundsOnClick:true,maxClusterRadius:80,singleMarkerMode:false,skipDuplicateAddTesting:true,animateAddingMarkers:false};this.listenTo(this.model.fields,"change",function(){self._setupGeometryField();self.render()});this.listenTo(this.model.records,"add",function(doc){self.redraw("add",doc)});this.listenTo(this.model.records,"change",function(doc){self.redraw("remove",doc);self.redraw("add",doc)});this.listenTo(this.model.records,"remove",function(doc){self.redraw("remove",doc)});this.listenTo(this.model.records,"reset",function(){self.redraw("reset")});this.menu=new my.MapMenu({model:this.model,state:this.state.toJSON()});this.listenTo(this.menu.state,"change",function(){self.state.set(self.menu.state.toJSON());self.redraw()});this.listenTo(this.state,"change",function(){self.redraw()});this.elSidebar=this.menu.$el},infobox:function(record){var html="";for(var key in record.attributes){if(!(this.state.get("geomField")&&key==this.state.get("geomField"))){html+="
"+key+": "+record.attributes[key]+"
"}}return html},geoJsonLayerOptions:{pointToLayer:function(feature,latlng){var marker=new L.Marker(latlng);marker.bindPopup(feature.properties.popupContent);this.markers.addLayer(marker);return marker},onEachFeature:function(feature,layer){if(feature.properties&&feature.properties.popupContent){layer.bindPopup(feature.properties.popupContent)}}},render:function(){var self=this;var htmls=Mustache.render(this.template,this.model.toTemplateJSON());this.$el.html(htmls);this.$map=this.$el.find(".panel.map");this.redraw();return this},redraw:function(action,doc){var self=this;action=action||"refresh";if(!self._geomReady()){self._setupGeometryField()}if(!self.mapReady){self._setupMap()}if(this._geomReady()&&this.mapReady){this.map.removeLayer(this.features);this.map.removeLayer(this.markers);var countBefore=0;this.features.eachLayer(function(){countBefore++});if(action=="refresh"||action=="reset"){this.features.clearLayers();this.map.removeLayer(this.markers);this.markers=new L.MarkerClusterGroup(this._clusterOptions);this._add(this.model.records.models)}else if(action=="add"&&doc){this._add(doc)}else if(action=="remove"&&doc){this._remove(doc)}if(this.state.get("cluster")){this.map.addLayer(this.markers)}else{this.map.addLayer(this.features)}if(this.state.get("autoZoom")){if(this.visible){this._zoomToFeatures()}else{this._zoomPending=true}}}},show:function(){if(this.map){this.map.invalidateSize();if(this._zoomPending&&this.state.get("autoZoom")){this._zoomToFeatures();this._zoomPending=false}}this.visible=true},hide:function(){this.visible=false},_geomReady:function(){return Boolean(this.state.get("geomField")||this.state.get("latField")&&this.state.get("lonField"))},_add:function(docs){var self=this;if(!(docs instanceof Array))docs=[docs];var count=0;var wrongSoFar=0;_.every(docs,function(doc){count+=1;var feature=self._getGeometryFromRecord(doc);if(typeof feature==="undefined"||feature===null){return true}else if(feature instanceof Object){feature.properties={popupContent:self.infobox(doc),cid:doc.cid};try{self.features.addData(feature)}catch(except){wrongSoFar+=1;var msg="Wrong geometry value";if(except.message)msg+=" ("+except.message+")";if(wrongSoFar<=10){self.trigger("recline:flash",{message:msg,category:"error"})}}}else{wrongSoFar+=1;if(wrongSoFar<=10){self.trigger("recline:flash",{message:"Wrong geometry value",category:"error"})}}return true})},_remove:function(docs){var self=this;if(!(docs instanceof Array))docs=[docs];_.each(docs,function(doc){for(var key in self.features._layers){if(self.features._layers[key].feature.geometry.properties.cid==doc.cid){self.features.removeLayer(self.features._layers[key])}}})},_parseCoordinateString:function(coord){if(typeof coord!="string"){return parseFloat(coord)}var dms=coord.split(/[^-?\.\d\w]+/);var deg=0;var m=0;var toDeg=[1,60,3600];var i;for(i=0;i select > option");if(options){options.each(function(opt){if(this.value==value){$(this).attr("selected","selected");return false}})}}});my.MapMenu=Backbone.View.extend({className:"editor",template:'
',events:{"click .editor-update-map":"onEditorSubmit","change .editor-field-type":"onFieldTypeChange","click #editor-auto-zoom":"onAutoZoomChange","click #editor-cluster":"onClusteringChange"},initialize:function(options){var self=this;_.bindAll(this,"render");this.listenTo(this.model.fields,"change",this.render);this.state=new recline.Model.ObjectState(options.state);this.listenTo(this.state,"change",this.render);this.render()},render:function(){var self=this;var htmls=Mustache.render(this.template,this.model.toTemplateJSON());this.$el.html(htmls);if(this._geomReady()&&this.model.fields.length){if(this.state.get("geomField")){this._selectOption("editor-geom-field",this.state.get("geomField"));this.$el.find("#editor-field-type-geom").attr("checked","checked").change()}else{this._selectOption("editor-lon-field",this.state.get("lonField"));this._selectOption("editor-lat-field",this.state.get("latField"));this.$el.find("#editor-field-type-latlon").attr("checked","checked").change()}}if(this.state.get("autoZoom")){this.$el.find("#editor-auto-zoom").attr("checked","checked")}else{this.$el.find("#editor-auto-zoom").removeAttr("checked")}if(this.state.get("cluster")){this.$el.find("#editor-cluster").attr("checked","checked")}else{this.$el.find("#editor-cluster").removeAttr("checked")}return this},_geomReady:function(){return Boolean(this.state.get("geomField")||this.state.get("latField")&&this.state.get("lonField"))},onEditorSubmit:function(e){e.preventDefault();if(this.$el.find("#editor-field-type-geom").attr("checked")){this.state.set({geomField:this.$el.find(".editor-geom-field > select > option:selected").val(),lonField:null,latField:null})}else{this.state.set({geomField:null,lonField:this.$el.find(".editor-lon-field > select > option:selected").val(),latField:this.$el.find(".editor-lat-field > select > option:selected").val()})}return false},onFieldTypeChange:function(e){if(e.target.value=="geom"){this.$el.find(".editor-field-type-geom").show();this.$el.find(".editor-field-type-latlon").hide()}else{this.$el.find(".editor-field-type-geom").hide();this.$el.find(".editor-field-type-latlon").show()}},onAutoZoomChange:function(e){this.state.set({autoZoom:!this.state.get("autoZoom")})},onClusteringChange:function(e){this.state.set({cluster:!this.state.get("cluster")})},_selectOption:function(id,value){var options=this.$el.find("."+id+" > select > option");if(options){options.each(function(opt){if(this.value==value){$(this).attr("selected","selected");return false}})}}})})(jQuery,recline.View);this.recline=this.recline||{};this.recline.View=this.recline.View||{};(function($,my){"use strict";my.MultiView=Backbone.View.extend({template:'
{{recordCount}} records
',events:{"click .menu-right button":"_onMenuClick","click .navigation button":"_onSwitchView"},initialize:function(options){var self=this;this._setupState(options.state);if(options.views){this.pageViews=options.views}else{this.pageViews=[{id:"grid",label:"Grid",view:new my.SlickGrid({model:this.model,state:this.state.get("view-grid")})},{id:"graph",label:"Graph",view:new my.Graph({model:this.model,state:this.state.get("view-graph")})},{id:"map",label:"Map",view:new my.Map({model:this.model,state:this.state.get("view-map")})},{id:"timeline",label:"Timeline",view:new my.Timeline({model:this.model,state:this.state.get("view-timeline")})}]}if(options.sidebarViews){this.sidebarViews=options.sidebarViews}else{this.sidebarViews=[{id:"filterEditor",label:"Filters",view:new my.FilterEditor({model:this.model})},{id:"fieldsView",label:"Fields",view:new my.Fields({model:this.model})}]}this.render();this._bindStateChanges();this._bindFlashNotifications();if(this.state.get("readOnly")){this.setReadOnly()}if(this.state.get("currentView")){this.updateNav(this.state.get("currentView"))}else{this.updateNav(this.pageViews[0].id)}this._showHideSidebar();this.listenTo(this.model,"query:start",function(){self.notify({loader:true,persist:true})});this.listenTo(this.model,"query:done",function(){self.clearNotifications();self.$el.find(".doc-count").text(self.model.recordCount||"Unknown")});this.listenTo(this.model,"query:fail",function(error){self.clearNotifications();var msg="";if(typeof error=="string"){msg=error}else if(typeof error=="object"){if(error.title){msg=error.title+": "}if(error.message){msg+=error.message}}else{msg="There was an error querying the backend"}self.notify({message:msg,category:"error",persist:true})});this.model.queryState.set(self.state.get("query"),{silent:true})},setReadOnly:function(){this.$el.addClass("recline-read-only")},render:function(){var tmplData=this.model.toTemplateJSON();tmplData.views=this.pageViews;tmplData.sidebarViews=this.sidebarViews;var template=Mustache.render(this.template,tmplData);this.$el.html(template);var $dataViewContainer=this.$el.find(".data-view-container");var $dataSidebar=this.$el.find(".data-view-sidebar");_.each(this.pageViews,function(view,pageName){view.view.render();if(view.view.redraw){view.view.redraw()}$dataViewContainer.append(view.view.el);if(view.view.elSidebar){$dataSidebar.append(view.view.elSidebar)}});_.each(this.sidebarViews,function(view){this["$"+view.id]=view.view.$el;$dataSidebar.append(view.view.el)},this);this.pager=new recline.View.Pager({model:this.model});this.$el.find(".recline-results-info").after(this.pager.el);this.queryEditor=new recline.View.QueryEditor({model:this.model.queryState});this.$el.find(".query-editor-here").append(this.queryEditor.el)},remove:function(){_.each(this.pageViews,function(view){view.view.remove()});_.each(this.sidebarViews,function(view){view.view.remove()});this.pager.remove();this.queryEditor.remove();Backbone.View.prototype.remove.apply(this,arguments)},_showHideSidebar:function(){var $dataSidebar=this.$el.find(".data-view-sidebar");var visibleChildren=$dataSidebar.children().filter(function(){return $(this).css("display")!="none"}).length;if(visibleChildren>0){$dataSidebar.show()}else{$dataSidebar.hide()}},updateNav:function(pageName){this.$el.find(".navigation button").removeClass("active");var $el=this.$el.find('.navigation button[data-view="'+pageName+'"]');$el.addClass("active");_.each(this.pageViews,function(view,idx){if(view.id===pageName){view.view.$el.show();if(view.view.elSidebar){view.view.elSidebar.show()}}else{view.view.$el.hide();if(view.view.elSidebar){view.view.elSidebar.hide()}if(view.view.hide){view.view.hide()}}});this._showHideSidebar();_.each(this.pageViews,function(view,idx){if(view.id===pageName){if(view.view.show){view.view.show()}}})},_onMenuClick:function(e){e.preventDefault();var action=$(e.target).attr("data-action");this["$"+action].toggle();this._showHideSidebar()},_onSwitchView:function(e){e.preventDefault();var viewName=$(e.target).attr("data-view");this.updateNav(viewName);this.state.set({currentView:viewName})},_setupState:function(initialState){var self=this;var qs=my.parseHashQueryString();var query=qs.reclineQuery;query=query?JSON.parse(query):self.model.queryState.toJSON();var graphState=qs["view-graph"]||qs.graph;graphState=graphState?JSON.parse(graphState):{};var stateData=_.extend({query:query,"view-graph":graphState,backend:this.model.backend.__type__,url:this.model.get("url"),dataset:this.model.toJSON(),currentView:null,readOnly:false},initialState);this.state=new recline.Model.ObjectState(stateData)},_bindStateChanges:function(){var self=this;this.listenTo(this.model.queryState,"change",function(){self.state.set({query:self.model.queryState.toJSON()})});_.each(this.pageViews,function(pageView){if(pageView.view.state&&pageView.view.state.bind){var update={};update["view-"+pageView.id]=pageView.view.state.toJSON();self.state.set(update);self.listenTo(pageView.view.state,"change",function(){var update={};update["view-"+pageView.id]=pageView.view.state.toJSON();self.state.set(update,{silent:true});self.state.trigger("change")})}})},_bindFlashNotifications:function(){var self=this;_.each(this.pageViews,function(pageView){self.listenTo(pageView.view,"recline:flash",function(flash){self.notify(flash)})})},notify:function(flash){var tmplData=_.extend({message:"Loading",category:"warning",loader:false},flash);var _template;if(tmplData.loader){_template='
{{message}}  
'}else{_template='
Ɨ {{message}}
'}var _templated=$(Mustache.render(_template,tmplData));_templated=$(_templated).appendTo($(".recline-data-explorer .alert-messages"));if(!flash.persist){setTimeout(function(){$(_templated).fadeOut(1e3,function(){$(this).remove()})},1e3)}},clearNotifications:function(){var $notifications=$(".recline-data-explorer .alert-messages .alert");$notifications.fadeOut(1500,function(){$(this).remove()})}});my.MultiView.restore=function(state){var datasetInfo;if(state.backend==="memory"){datasetInfo={backend:"memory",records:[{stub:"this is a stub dataset because we do not restore memory datasets"}]}}else{datasetInfo=_.extend({url:state.url,backend:state.backend},state.dataset)}var dataset=new recline.Model.Dataset(datasetInfo);var explorer=new my.MultiView({model:dataset,state:state});return explorer};var urlPathRegex=/^([^?]+)(\?.*)?/;my.parseHashUrl=function(hashUrl){var parsed=urlPathRegex.exec(hashUrl);if(parsed===null){return{}}else{return{path:parsed[1],query:parsed[2]||""}}};my.parseQueryString=function(q){if(!q){return{}}var urlParams={},e,d=function(s){return unescape(s.replace(/\+/g," "))},r=/([^&=]+)=?([^&]*)/g;if(q&&q.length&&q[0]==="?"){q=q.slice(1)}while(e=r.exec(q)){urlParams[d(e[1])]=d(e[2])}return urlParams};my.parseHashQueryString=function(){var q=my.parseHashUrl(window.location.hash).query;return my.parseQueryString(q)};my.composeQueryString=function(queryParams){var queryString="?";var items=[];$.each(queryParams,function(key,value){if(typeof value==="object"){value=JSON.stringify(value)}items.push(key+"="+encodeURIComponent(value))});queryString+=items.join("&");return queryString};my.getNewHashForQueryString=function(queryParams){var queryPart=my.composeQueryString(queryParams);if(window.location.hash){return window.location.hash.split("?")[0].slice(1)+queryPart}else{return queryPart}};my.setHashQueryString=function(queryParams){window.location.hash=my.getNewHashForQueryString(queryParams)}})(jQuery,recline.View);this.recline=this.recline||{};this.recline.View=this.recline.View||{};(function($,my){"use strict";my.SlickGrid=Backbone.View.extend({initialize:function(modelEtc){var self=this;this.$el.addClass("recline-slickgrid");this.templates={deleterow:''};_.bindAll(this,"render","onRecordChanged");this.listenTo(this.model.records,"add remove reset",this.render);this.listenTo(this.model.records,"change",this.onRecordChanged);var state=_.extend({hiddenColumns:[],columnsOrder:[],columnsSort:{},columnsWidth:[],columnsEditor:[],options:{},fitColumns:false},modelEtc.state);this.state=new recline.Model.ObjectState(state);this._slickHandler=new Slick.EventHandler;if(this.state.get("gridOptions")&&this.state.get("gridOptions").enabledAddRow!=undefined&&this.state.get("gridOptions").enabledAddRow==true){this.editor=new my.GridControl;this.elSidebar=this.editor.$el;this.listenTo(this.editor.state,"change",function(){this.model.records.add(new recline.Model.Record)})}},onRecordChanged:function(record){if(!this.grid){return}var row_index=this.grid.getData().getModelRow(record);this.grid.invalidateRow(row_index);this.grid.getData().updateItem(record,row_index);this.grid.render()},render:function(){var self=this;var options=_.extend({enableCellNavigation:true,enableColumnReorder:true,explicitInitialization:true,syncColumnCellResize:true,forceFitColumns:this.state.get("fitColumns")},self.state.get("gridOptions"));var columns=[];var formatter=function(row,cell,value,columnDef,dataContext){if(columnDef.id=="del"){return self.templates.deleterow}var field=self.model.fields.get(columnDef.id);if(field.renderer){return field.renderer(value,field,dataContext)}else{return value}};var validator=function(field){return function(value){if(field.type=="date"&&isNaN(Date.parse(value))){return{valid:false,msg:"A date is required, check field field-date-format"}}else{return{valid:true,msg:null}}}};if(this.state.get("gridOptions")&&this.state.get("gridOptions").enableReOrderRow==true){columns.push({id:"#",name:"",width:22,behavior:"selectAndMove",selectable:false,resizable:false,cssClass:"recline-cell-reorder"})}if(this.state.get("gridOptions")&&this.state.get("gridOptions").enabledDelRow==true){columns.push({id:"del",name:"",field:"del",sortable:true,width:38,formatter:formatter,validator:validator})}function sanitizeFieldName(name){var sanitized=$(name).text();return name!==sanitized&&sanitized!==""?sanitized:name}_.each(this.model.fields.toJSON(),function(field){var column={id:field.id,name:sanitizeFieldName(field.label),field:field.id,sortable:true,minWidth:80,formatter:formatter,validator:validator(field)};var widthInfo=_.find(self.state.get("columnsWidth"),function(c){return c.column===field.id});if(widthInfo){column.width=widthInfo.width}var editInfo=_.find(self.state.get("columnsEditor"),function(c){return c.column===field.id});if(editInfo){column.editor=editInfo.editor}else{var typeToEditorMap={string:Slick.Editors.LongText,integer:Slick.Editors.IntegerEditor,number:Slick.Editors.Text,date:Slick.Editors.Text,"boolean":Slick.Editors.YesNoSelectEditor};if(field.type in typeToEditorMap){column.editor=typeToEditorMap[field.type]}else{column.editor=Slick.Editors.LongText}}columns.push(column)});var visibleColumns=_.filter(columns,function(column){return _.indexOf(self.state.get("hiddenColumns"),column.id)===-1});if(this.state.get("columnsOrder")&&this.state.get("columnsOrder").length>0){visibleColumns=visibleColumns.sort(function(a,b){return _.indexOf(self.state.get("columnsOrder"),a.id)>_.indexOf(self.state.get("columnsOrder"),b.id)?1:-1});columns=columns.sort(function(a,b){return _.indexOf(self.state.get("columnsOrder"),a.id)>_.indexOf(self.state.get("columnsOrder"),b.id)?1:-1})}var tempHiddenColumns=[];for(var i=columns.length-1;i>=0;i--){if(_.indexOf(_.pluck(visibleColumns,"id"),columns[i].id)===-1){tempHiddenColumns.push(columns.splice(i,1)[0])}}columns=columns.concat(tempHiddenColumns);function toRow(m){var row={};self.model.fields.each(function(field){var render="";if(!_.isUndefined(m.getFieldValueUnrendered(field))){render=m.getFieldValueUnrendered(field)}row[field.id]=render});return row}function RowSet(){var models=[];var rows=[];this.push=function(model,row){models.push(model);rows.push(row)};this.getLength=function(){return rows.length};this.getItem=function(index){return rows[index]};this.getItemMetadata=function(index){return{}};this.getModel=function(index){return models[index]};this.getModelRow=function(m){return _.indexOf(models,m)};this.updateItem=function(m,i){rows[i]=toRow(m);models[i]=m}}var data=new RowSet;this.model.records.each(function(doc){data.push(doc,toRow(doc))});this.grid=new Slick.Grid(this.el,data,visibleColumns,options);var sortInfo=this.model.queryState.get("sort");if(sortInfo){var column=sortInfo[0].field;var sortAsc=sortInfo[0].order!=="desc";this.grid.setSortColumn(column,sortAsc)}if(this.state.get("gridOptions")&&this.state.get("gridOptions").enableReOrderRow){this._setupRowReordering()}this._slickHandler.subscribe(this.grid.onSort,function(e,args){var order=args.sortAsc?"asc":"desc";var sort=[{field:args.sortCol.field,order:order}];self.model.query({sort:sort})});this._slickHandler.subscribe(this.grid.onColumnsReordered,function(e,args){self.state.set({columnsOrder:_.pluck(self.grid.getColumns(),"id")})});this.grid.onColumnsResized.subscribe(function(e,args){var columns=args.grid.getColumns();var defaultColumnWidth=args.grid.getOptions().defaultColumnWidth;var columnsWidth=[];_.each(columns,function(column){if(column.width!=defaultColumnWidth){columnsWidth.push({column:column.id,width:column.width})}});self.state.set({columnsWidth:columnsWidth})});this._slickHandler.subscribe(this.grid.onCellChange,function(e,args){var grid=args.grid;var model=data.getModel(args.row);var field=grid.getColumns()[args.cell].id;var v={};v[field]=args.item[field];model.set(v)});this._slickHandler.subscribe(this.grid.onClick,function(e,args){try{e.preventDefault()}catch(e){}var cell=0;if(self.state.get("gridOptions")&&self.state.get("gridOptions").enableReOrderRow!=undefined&&self.state.get("gridOptions").enableReOrderRow==true){cell=1}if(args.cell==cell&&self.state.get("gridOptions").enabledDelRow==true){var model=data.getModel(args.row);model.destroy()}});var columnpicker=new Slick.Controls.ColumnPicker(columns,this.grid,_.extend(options,{state:this.state}));if(self.visible){self.grid.init();self.rendered=true}else{self.rendered=false}return this},_setupRowReordering:function(){var self=this;self.grid.setSelectionModel(new Slick.RowSelectionModel);var moveRowsPlugin=new Slick.RowMoveManager({cancelEditOnDrag:true});moveRowsPlugin.onBeforeMoveRows.subscribe(function(e,data){for(var i=0;i',initialize:function(options){var self=this;_.bindAll(this,"render");this.state=new recline.Model.ObjectState;this.render()},render:function(){var self=this;this.$el.html(this.template)},events:{"click .recline-row-add":"addNewRow"},addNewRow:function(e){e.preventDefault();this.state.trigger("change")}})})(jQuery,recline.View);(function($){function SlickColumnPicker(columns,grid,options){var $menu;var columnCheckboxes;var defaults={fadeSpeed:250};function init(){grid.onHeaderContextMenu.subscribe(handleHeaderContextMenu);options=$.extend({},defaults,options);$menu=$('