Initial commit

This commit is contained in:
zhongjin
2020-06-15 10:58:47 +08:00
commit 4f1dfe7564
8590 changed files with 1516878 additions and 0 deletions
@@ -0,0 +1,35 @@
var Backbone = require('backbone');
var ConfigModel = require('builder/data/config-model');
var LegendDefinitionModel = require('builder/data/legends/legend-bubble-definition-model');
describe('data/legends/legend-bubble-defintion-model', function () {
var style;
var layerDef1;
beforeEach(function () {
var configModel = new ConfigModel({
base_url: '/u/pepe'
});
layerDef1 = new Backbone.Model({
id: 'fa6cf872-fffa-4301-9a60-849cedba7864',
table_name: 'foo',
autoStyle: false
});
style = new Backbone.Model();
style.hasNoneStyles = function () {
return false;
};
layerDef1.styleModel = style;
this.model = new LegendDefinitionModel(null, {
configModel: configModel,
layerDefinitionModel: layerDef1,
vizId: 'v-123'
});
spyOn(this.model, '_inheritStyleColor');
spyOn(this.model, 'save');
});
});
@@ -0,0 +1,39 @@
var _ = require('underscore');
var Backbone = require('backbone');
var ConfigModel = require('builder/data/config-model');
var LegendDefinitionModel = require('builder/data/legends/legend-choropleth-definition-model');
describe('data/legends/legend-choropleth-defintion-model', function () {
beforeEach(function () {
var configModel = new ConfigModel({
base_url: '/u/pepe'
});
var layerDef1 = new Backbone.Model({
id: 'fa6cf872-fffa-4301-9a60-849cedba7864',
table_name: 'foo'
});
this.model = new LegendDefinitionModel(null, {
configModel: configModel,
layerDefinitionModel: layerDef1,
vizId: 'v-123'
});
});
it('should toJSON properly', function () {
var attrs = this.model.toJSON();
expect(attrs.definition).toBeDefined();
expect(_.isEmpty(attrs.definition)).toBe(true);
this.model.set({prefix: 'foo'});
attrs = this.model.toJSON();
expect(attrs.definition.prefix).toBeDefined();
expect(attrs.definition.suffix).toBeUndefined();
this.model.set({suffix: 'bar'});
attrs = this.model.toJSON();
expect(attrs.definition.prefix).toBe('foo');
expect(attrs.definition.suffix).toBe('bar');
});
});
@@ -0,0 +1,39 @@
var Backbone = require('backbone');
var ConfigModel = require('builder/data/config-model');
var LegendCustomChoroplethDefinitionModel = require('builder/data/legends/legend-custom-choropleth-definition-model');
describe('data/legends/legend-choropleth-defintion-model', function () {
beforeEach(function () {
var configModel = new ConfigModel({
base_url: '/u/pepe'
});
var layerDef1 = new Backbone.Model({
id: 'fa6cf872-fffa-4301-9a60-849cedba7864',
table_name: 'foo'
});
this.model = new LegendCustomChoroplethDefinitionModel(null, {
configModel: configModel,
layerDefinitionModel: layerDef1,
vizId: 'v-123'
});
});
it('should toJSON properly', function () {
var attrs = this.model.toJSON();
expect(attrs.definition).toBeDefined();
expect(attrs.definition.prefix).toBeUndefined();
expect(attrs.definition.suffix).toBeUndefined();
expect(attrs.definition.left_label).toBeUndefined();
expect(attrs.definition.right_Label).toBeUndefined();
expect(attrs.definition.colors.length).toBe(0);
this.model.set({ prefix: 'foo', suffix: 'bar', leftLabel: 'low', rightLabel: 'high' });
attrs = this.model.toJSON();
expect(attrs.definition.prefix).toBe('foo');
expect(attrs.definition.suffix).toBe('bar');
expect(attrs.definition.left_label).toBe('low');
expect(attrs.definition.right_label).toBe('high');
});
});
@@ -0,0 +1,120 @@
var _ = require('underscore');
var Backbone = require('backbone');
var ConfigModel = require('builder/data/config-model');
var LegendDefinitionModel = require('builder/data/legends/legend-custom-definition-model');
var StyleHelper = require('builder/helpers/style');
describe('data/legends/legend-custom-defintion-model', function () {
beforeEach(function () {
var configModel = new ConfigModel({
base_url: '/u/pepe'
});
var layerDef1 = new Backbone.Model({
id: 'fa6cf872-fffa-4301-9a60-849cedba7864',
table_name: 'foo'
});
this.model = new LegendDefinitionModel({
items: [
{
title: '',
color: '#fabada'
},
{
title: 'Foo',
color: '#f4b4d4',
icon: 'http://image.io/building.png'
},
{
title: 'Bar',
color: '#f4b4d4',
image: 'http://image.io/logo.svg'
}
]
}, {
configModel: configModel,
layerDefinitionModel: layerDef1,
vizId: 'v-123'
});
});
describe('.toJSON', function () {
it('should toJSON properly', function () {
var attrs = this.model.toJSON();
expect(attrs.definition).toBeDefined();
expect(_.isArray(attrs.definition.categories)).toBe(true);
expect(attrs.definition.categories.length).toBe(3);
expect(attrs.definition.categories[0].title).toBe('');
expect(attrs.definition.categories[1].title).toContain('Foo');
expect(attrs.definition.categories[1].icon).toContain('http://image.io/building.png');
expect(attrs.definition.categories[2].icon).toContain('http://image.io/logo.svg');
});
it('should convert legend item title to String', function () {
var items = this.model.get('items');
items.push({
title: 1,
color: '#000111'
});
var attrs = this.model.toJSON();
expect(attrs.definition.categories.length).toBe(4);
expect(attrs.definition.categories[3].title).toBe('1');
items.push({
title: null,
color: '#000111'
});
attrs = this.model.toJSON();
expect(attrs.definition.categories.length).toBe(5);
expect(attrs.definition.categories[4].title).toBe('');
});
});
describe('inheritStyleCategories', function () {
it('should return proper output when fixed provided', function () {
var style = new Backbone.Model({
fill: {
color: {
fixed: '#fabada',
image: 'icon.svg'
}
}
});
var items = StyleHelper.getStyleCategories(style);
expect(items.length).toBe(1);
expect(items[0].color).toEqual('#fabada');
expect(items[0].icon).toEqual('icon.svg');
expect(items[0].title).toEqual('');
});
it('should return proper output when range provided', function () {
var style = new Backbone.Model({
fill: {
color: {
range: ['#fabada', '#c0ffee', '#0ff1ce'],
domain: ['Asturias', 'Starbucks', ''],
images: ['', 'starbucks.png', '']
}
}
});
var items = StyleHelper.getStyleCategories(style);
expect(items.length).toBe(3);
expect(items[0].color).toEqual('#fabada');
expect(items[0].title).toEqual('Asturias');
expect(items[0].icon).toEqual('');
expect(items[1].color).toEqual('#c0ffee');
expect(items[1].title).toEqual('Starbucks');
expect(items[1].icon).toEqual('starbucks.png');
expect(items[2].color).toEqual('#0ff1ce');
expect(items[2].title).toEqual('editor.legend.legend-form.others');
expect(items[2].icon).toEqual('');
});
});
});
@@ -0,0 +1,140 @@
var Backbone = require('backbone');
var LegendDefinitionCollection = require('builder/data/legends/legend-definitions-collection');
var ConfigModel = require('builder/data/config-model');
describe('data/legends/legend-definitions-collection', function () {
var legendDefCollection;
var layerDefCollection;
var vizJSON;
beforeEach(function () {
var configModel = new ConfigModel({
base_url: '/u/pepe'
});
var styleModel = new Backbone.Model({
fill: {
size: {
attribute: 'n_anclajes',
range: [5, 20]
},
color: {
bins: 3,
range: ['#ffeda0', '#feb24c', '#f03b20'],
attribute: 'n_anclajes'
}
}
});
var layerDef1 = new Backbone.Model({
id: 'fa6cf872-fffa-4301-9a60-849cedba7864',
table_name: 'foo'
});
layerDef1.styleModel = styleModel;
var layerDef2 = new Backbone.Model({
id: 'fa6cf872-fffa-4301-9a60-849cedba7865',
table_name: 'bar'
});
layerDef2.styleModel = styleModel;
layerDefCollection = new Backbone.Collection([]);
layerDefCollection.add(layerDef1);
layerDefCollection.add(layerDef2);
vizJSON = {
layers: [
{
id: 'fa6cf872-fffa-4301-9a60-849cedba7864',
type: 'CartoDB',
legends: [
{
type: 'bubble',
title: 'My Bubble Legend',
fill_color: '#FABADA'
},
{
type: 'category',
title: 'My Category Legend',
prefix: 'prefix',
sufix: 'sufix'
},
{
type: 'choropleth',
title: 'My Choropleth Legend',
prefix: 'prefix',
sufix: 'sufix'
},
{
type: 'custom',
title: 'My Custom Legend',
items: [
{ name: 'Category 1', type: 'color', color: '#CACACA' },
{ name: 'Category 2', type: 'color', color: '#FABADA' }
]
}
]
},
{
id: 'fa6cf872-fffa-4301-9a60-849cedba7865',
type: 'CartoDB',
legends: [
{
type: 'bubble',
title: 'My Bubble Legend',
fill_color: '#FABADA'
},
{
type: 'category',
title: 'My Category Legend',
prefix: 'prefix',
sufix: 'sufix'
}
]
}
]
};
legendDefCollection = new LegendDefinitionCollection([], {
configModel: configModel,
layerDefinitionsCollection: layerDefCollection,
vizId: 'v-123'
});
legendDefCollection.resetByData(vizJSON);
});
it('should be populated properly', function () {
expect(legendDefCollection.length).toBe(6);
expect(legendDefCollection.at(0).get('type')).toBe('bubble');
expect(legendDefCollection.at(0).layerDefinitionModel.get('id')).toBe('fa6cf872-fffa-4301-9a60-849cedba7864');
expect(legendDefCollection.at(2).get('type')).toBe('choropleth');
expect(legendDefCollection.at(2).layerDefinitionModel.get('id')).toBe('fa6cf872-fffa-4301-9a60-849cedba7864');
expect(legendDefCollection.at(5).get('type')).toBe('category');
expect(legendDefCollection.at(5).layerDefinitionModel.get('id')).toBe('fa6cf872-fffa-4301-9a60-849cedba7865');
});
it('should search by layerDefModel properly', function () {
var layerDefModel = layerDefCollection.at(0);
var legends = legendDefCollection.findByLayerDefModel(layerDefModel);
expect(legends.length).toBe(4);
expect(legends[0].get('type')).toBe('bubble');
expect(legends[1].get('type')).toBe('category');
expect(legends[2].get('type')).toBe('choropleth');
});
it('should search by type properly', function () {
var layerDefModel = layerDefCollection.at(0);
var legend = legendDefCollection.findByLayerDefModelAndType(layerDefModel, 'choropleth');
expect(legend.layerDefinitionModel).toBe(layerDefModel);
expect(legend.get('title')).toBe('My Choropleth Legend');
});
it('should search by types properly', function () {
var layerDefModel = layerDefCollection.at(1);
var legends = legendDefCollection.findByLayerDefModelAndTypes(layerDefModel, ['category', 'choropleth', 'custom']);
expect(legends.length).toBe(1);
expect(legends[0].get('type')).toBe('category');
expect(legends[0].layerDefinitionModel).toBe(layerDefModel);
});
});
@@ -0,0 +1,94 @@
var _ = require('underscore');
var Backbone = require('backbone');
var ConfigModel = require('builder/data/config-model');
var LegendDefinitionModel = require('builder/data/legends/legend-torque-definition-model');
var StyleHelper = require('builder/helpers/style');
describe('data/legends/legend-torque-defintion-model', function () {
beforeEach(function () {
var configModel = new ConfigModel({
base_url: '/u/pepe'
});
var layerDef1 = new Backbone.Model({
id: 'fa6cf872-fffa-4301-9a60-849cedba7864',
table_name: 'foo'
});
this.model = new LegendDefinitionModel({
items: [
{
title: 'Carto',
color: '#fabada'
},
{
title: 'Foo',
color: '#99cc66'
},
{
title: null,
color: '#996600'
}
]
}, {
configModel: configModel,
layerDefinitionModel: layerDef1,
vizId: 'v-123'
});
});
it('should toJSON properly', function () {
var attrs = this.model.toJSON();
expect(attrs.definition).toBeDefined();
expect(_.isArray(attrs.definition.categories)).toBe(true);
expect(attrs.definition.categories.length).toBe(3);
expect(attrs.definition.categories[0].title).toBe('Carto');
expect(attrs.definition.categories[1].title).toContain('Foo');
expect(attrs.definition.categories[2].title).toBe('');
});
describe('inheritStyleCategories', function () {
it('should return proper output when fixed provided', function () {
var style = new Backbone.Model({
fill: {
color: {
fixed: '#fabada',
image: 'icon.svg'
}
}
});
var items = StyleHelper.getStyleCategories(style);
expect(items.length).toBe(1);
expect(items[0].color).toEqual('#fabada');
expect(items[0].icon).toEqual('icon.svg');
expect(items[0].title).toEqual('');
});
it('should return proper output when range provided', function () {
var style = new Backbone.Model({
fill: {
color: {
range: ['#fabada', '#c0ffee', '#0ff1ce'],
domain: ['Asturias', 'Starbucks', ''],
images: ['', 'starbucks.png', '']
}
}
});
var items = StyleHelper.getStyleCategories(style);
expect(items.length).toBe(3);
expect(items[0].color).toEqual('#fabada');
expect(items[0].title).toEqual('Asturias');
expect(items[0].icon).toEqual('');
expect(items[1].color).toEqual('#c0ffee');
expect(items[1].title).toEqual('Starbucks');
expect(items[1].icon).toEqual('starbucks.png');
expect(items[2].color).toEqual('#0ff1ce');
expect(items[2].title).toEqual('editor.legend.legend-form.others');
expect(items[2].icon).toEqual('');
});
});
});
@@ -0,0 +1,86 @@
var Backbone = require('backbone');
var LegendsState = require('builder/data/legends/legends-state');
describe('data/legends/legends-state', function () {
beforeAll(function () {
this.layerDefinitionModel = new Backbone.Model({
id: 'l-1'
});
this.layerDefinitionsCollection = new Backbone.Collection();
this.layerDefinitionsCollection.add(this.layerDefinitionModel);
this.legendDefinitionsCollection = new Backbone.Collection();
var legendModel = new Backbone.Model({
type: 'choropleth',
title: 'foo',
conf: {
columns: ['title']
}
});
legendModel.layerDefinitionModel = this.layerDefinitionModel;
this.legendDefinitionsCollection.add(legendModel);
LegendsState.init(this.layerDefinitionsCollection, this.legendDefinitionsCollection);
});
it('should be populated properly', function () {
var instance = LegendsState.getInstance();
expect(instance['l-1']).toBeDefined();
expect(instance['l-1']['color']).toBeDefined();
expect(instance['l-1']['color']['title']).toBe('foo');
expect(instance['l-1']['size']).toBeUndefined();
});
describe('get', function () {
it('should return existing state properly', function () {
var state = LegendsState.get(this.layerDefinitionModel, 'choropleth');
expect(state.title).toBeDefined();
expect(state.title).toBe('foo');
});
it('should return undefined for non existint state', function () {
var state = LegendsState.get(this.layerDefinitionModel, 'bubble');
expect(state).toBeUndefined();
});
});
describe('set', function () {
it('should set properly', function () {
LegendsState.set(this.layerDefinitionModel, 'bubble', {title: 'bar'});
var instance = LegendsState.getInstance();
expect(instance['l-1']['size']).toBeDefined();
expect(instance['l-1']['size']['title']).toBe('bar');
});
it('should set properly for new layers', function () {
LegendsState.set(new Backbone.Model({id: 'l-2'}), 'custom', {title: 'pop'});
var instance = LegendsState.getInstance();
expect(instance['l-2']['color']).toBeDefined();
expect(instance['l-2']['color']['title']).toBe('pop');
});
});
describe('events', function () {
it('add layer', function () {
var layer = new Backbone.Model({id: 'l-200'});
this.layerDefinitionsCollection.add(layer);
var instance = LegendsState.getInstance();
expect(instance['l-200']).toBeDefined();
});
it('remove layer', function () {
var layer = this.layerDefinitionsCollection.at(0);
this.layerDefinitionsCollection.remove(layer);
var instance = LegendsState.getInstance();
expect(instance['l-1']).toBeUndefined();
});
});
});