master_thesis-gui /js/model/attrmodel.js

Language Javascript Lines 202
MD5 Hash 99357e1c26386b8eaa2e37eb5e9604a6
Repository https://bitbucket.org/worldsayshi/master_thesis-gui.git View Raw File View Project SPDX
| Open JSFiddle
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
// Collection of all attributes and as such keeper of most of the data.
// Other collections not contained here try to merely keep references (id's)
// the these attributes/facets/properties.
var Attributes = Backbone.Collection.extend({
	initialize: function (opts,solrInterface) {
		var self = this;
		solrInterface.on('change:fieldgroups',function (model,groups) {
			groups = removeNullElementsInGroups(groups);
			self.fieldGroupsChanged(groups);
		});
		solrInterface.on('change:facet_counts',function (model,facet_counts) {
		    self.setAttributeValues(facet_counts);
		});
	},
	setAttributeValues: function (new_value_counts) {
		_.each(_.keys(new_value_counts),function (attr_name) {
			if(this.get(attr_name))
			{this.get(attr_name).supplyValues(new_value_counts[attr_name]);}
		},this);
	},
	// - Remove attributes not in new list
	// - Add attributes not in old list
	// - Update smartly those that remain (keep values, overwrite group nr)
	fieldGroupsChanged: function (groups) {
		assert(this instanceof Attributes,"Context");
		var new_facets = {};
		_.each(groups,function (group,i) {
			return _.each(group,function (attr_name,j) {
				
				new_facets[attr_name] = true;
				var typeAndName = this.parseFieldName(attr_name);
				
				var attribute = this.get(attr_name) || new Attribute({id:attr_name,type:typeAndName.type});
				
				if(!this.get(attr_name)){this.add(attribute,{silent:true});}
				
				attribute.set({
						name:typeAndName.pres_name,
						type:typeAndName.type,
						group_nr:i,
						internal_group_nr:j
					});
			},this); 
		},this);
		// Remove if not in new groups
		this.each(function(model) {
			if(!new_facets[model.id]){
				if(model)
				this.trigger('attribute-dont-want-to-be-filter',model.id);
				this.remove(model);
			}
		},this);
		
		// This doesn't work as expected: It sometimes replaces models!
		//this.update(_.values(new_facets),{silent:true});
		// remove this and silent above?
		this.trigger('update',this);
	},
	/* parseFieldName
	 * A bit of a hacky way to pass additional type and unit information about the 
	 * facet from an ad hoc type-inferring script of the Solr configuration;
	 * type info is embedded it in the facet name.
	 * This function extract type info and a label name from the raw field name.
	 * */
	parseFieldName: function (raw_attr_name) {
		var type = 'ne';
		var pres_name = '';
		if (raw_attr_name.substring(0, 2) === "p_") {
			raw_attr_name = raw_attr_name.substring(2);
		}
		if (raw_attr_name.substring(0, 2) === "e_") {
			raw_attr_name = raw_attr_name.substring(2);
			type = 'e';
		}
		// Sometimes there is an additional 'p_' here?
		if (raw_attr_name.substring(0, 2) === "p_") {
			raw_attr_name = raw_attr_name.substring(2);
		}
		if(type=='e'){
			var nameAndUnit = raw_attr_name.split(/__/);
			pres_name = nameAndUnit[0].replace(/_/,' ')
						+' ('+nameAndUnit[1].replace(/_/,' ')+')';
		} else {
			pres_name = raw_attr_name.replace(/_/,' ');
		}
		return {
			type: type,
			pres_name:pres_name
		};
	}
});
 
// Contain most data defining the Attribute/Property/Facet(/Filter)
// Grouping and Filter placement is maintained in separate collections  
var Attribute = Backbone.Model.extend({
	initialize: function () {
		assert(this.get('type'));
		this.set('allowUpdateValues',false);
		this.setUpTools();
		if(this.get('type')=='e'){
			this.set('range',[0,0],{silent:true});
		}
	},
	setUpTools: function() {
		var self = this;
		var lockTool = new LockValues({id:'lockTool',parentModel:self});
		var removeTool = new RemoveFilter({id:'removeTool',parentModel:self});
		var toolbar = new ToolsCollection([
			lockTool,removeTool
		]);
		this.set('toolbar',toolbar);
	},
	useAsFilter: function() {
		this.expand();
		this.collection.trigger('attribute-wants-to-be-filter',this.id);
	},
	dontUseAsFilter: function() {
		this.collection.trigger('attribute-dont-want-to-be-filter',this.id);
	},
	// expand when used as a filter
	expand: function() {
		this.set('in_focus',true);
		this.collection.trigger('focus-change',this.id);
	},
	collapse: function () {
		this.set('in_focus',false);
	},
	// Should currently not be called directly but through the application model
	selectValue: function (name,silent) {
		this.get('values').get(name).select();
		if(!silent){
			this.trigger('change',this);
		}
	},
	selectRangeSilently: function (range) {
		this.set('range',range,{silent:true});
	},
	supplyValues: function (new_counts) {
		if(this.get('values')){
			if(!this.get('allowUpdateValues')){
				return;
			}
			this.get('values').update(new_counts,{parse:true,silent:true});
			this.trigger('change',this);
		} else {
			this.set('values',new Values(new_counts,{parse:true,type:this.get('type')}));
		}
		this.get('values').sort({silent:true});
		//this.assertValuesSorted();
	},
	assertValuesSorted: function () {
		var oldVal = new Value({id:"0"});
		this.get('values').each(function (val) {
			assert(oldVal.get('id')<=val.get('id'));
			oldVal=val;
		},this);
	},
	dropValues: function() {
		this.set('values',undefined);
	},
});

// Value collection held by an attribute
var Values = Backbone.Collection.extend({
	initialize: function (data,options) {
		this.type=options.type;
		if(data){
			this.reset(data,{parse:true});
		}
	},
	parse: function (data) {
		return _.map(data,function (elem,key) {
			return new Value({id:key,count:elem});
		},this);
	},
	comparator: function (a,b) {
		if(this.type=='ne'){
			return a.get('count')>b.get('count');
		} else {
			return parseInt(a.get('id'))-parseInt(b.get('id'));
		}
	},
	toList: function () {
		return this.map(function (elem) {
			if(this.get('type')=='e'){
				return parseInt(elem.get('id'));
			} else {
				return elem.get('id');
			}
		},this);
	}
});

// A single value of a facet
var Value = Backbone.Model.extend({
	// Toggle selection
	select: function () {
		this.set('selected',!this.get('selected'));
	}
});
Back to Top