forked from codeforboston/finda
-
Notifications
You must be signed in to change notification settings - Fork 23
/
facet.js
257 lines (231 loc) · 9.11 KB
/
facet.js
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
define(function(require, exports, module) {
'use strict';
// compile all the templates
var flight = require('flight');
var Handlebars = require('handlebars');
var _ = require('lodash');
var $ = require('jquery');
var welcomeTemplate = require('text!templates/welcome.html');
var inputTemplate = require('text!templates/input.html');
var formTemplate = require('text!templates/form.html');
var facetTemplate = require('text!templates/facet.html');
var facetControlsTemplate = require('text!templates/facetControls.html');
var extraResourcesTemplate = require('text!templates/extraResources.html');
var assessmentTemplate = require('text!templates/assessment.html');
var templates = {
welcome: Handlebars.compile(welcomeTemplate),
input: Handlebars.compile(inputTemplate),
form: Handlebars.compile(formTemplate),
facet: Handlebars.compile(facetTemplate),
facetControls: Handlebars.compile(facetControlsTemplate),
extraResources: Handlebars.compile(extraResourcesTemplate),
assessment: Handlebars.compile(assessmentTemplate)
};
module.exports = flight.component(function () {
this.configureFacets = function(ev, config) {
this.facetConfig = config.facets;
};
// -1 is start with intro question that is not a facet
this.facetOffset = -1;
this.meetsFacetDependency = function(facetData, key, dependency) {
return _.find(facetData, function(facets) {
return _.find(facets, function(facet) {
return facet.value === dependency && facet.selected;
});
});
};
this.getFacetConfig = function(key, attr) {
if (this.facetConfig[key]) {
return this.facetConfig[key][attr];
}
};
this.displayFacets = function(ev, facetData) {
// cache facet data so that you can call it internally instead of waiting
// for event from data facet
if (facetData) {
this.facetData = facetData;
} else {
facetData = this.facetData;
}
// Remove search facets that are not part of the survey
delete facetData.county;
this.noSelectionsAvailable = false;
// show first question if you're looking for a treatment facility
if (this.facetOffset === -1) {
this.$node.html(templates.welcome());
this.on('.js-next-prev', 'click', this.nextPrevHandler);
this.on('.js-no-treatment', 'click', this.showNoTreatment);
this.on('.js-not-sure-treatment', 'click', this.showNoTreatment);
return;
}
var facets = _.keys(facetData);
var key = facets[this.facetOffset];
if (!this.showAllFacets) {
if (this.facetOffset >= facets.length) {
this.$node.find('.js-offer-results[data-offer-results=true]').click();
return;
}
// does the facet have a dependency?
if (key) {
var dependency = this.getFacetConfig(key, 'dependency');
if (dependency) {
if (!this.meetsFacetDependency(facetData, key, dependency)) {
this.setFacetOffset(this.facetOffset + 1);
return;
}
}
}
var newFacetData = {};
newFacetData[key] = facetData[key];
facetData = newFacetData;
}
var facet = _.chain(facetData).map(
_.bind(function(values, key) {
// only one facet available that has no facilities
if (values.length === 1 && values[0].count === 0) {
this.noSelectionsAvailable = true;
}
var hasSelected = _.some(values, 'selected');
var configKey = this.showAllFacets ? 'title' : 'survey_title';
// render a template for each facet
return templates.facet({
title: this.getFacetConfig(key, configKey),
key: key,
// render the form for each value of the facet
form: templates.form({
key: key,
has_selected: hasSelected,
inputs: _.chain(values).filter('count').map(templates.input).
value()
})
});
}, this)).value().join('');
var previousOffset;
if (typeof this.facetHistory === 'object') {
previousOffset = this.facetHistory[this.facetHistory.length - 1] || -1;
} else {
previousOffset = -1;
}
this.$node.html(
facet +
templates.facetControls({
showResults: this.showAllFacets,
facetOffset: this.facetOffset + 1,
previousFacetOffset: previousOffset
})
);
this.on('.js-next-prev', 'click', this.nextPrevHandler);
this.on('.js-offer-results', 'click', this.showResultsHandler);
if (this.noSelectionsAvailable === true) {
// click button to advance to the next facet.
// NOTE(chaserx): I couldn't find a way to use `facetOffset` without
// creating infinite loop.
this.$node.find('button.btn-next').trigger('click');
this.facetHistory.pop();
this.noSelectionsAvailable = false;
return;
}
};
this.showResultsHandler = function(ev) {
// can be true or false
var offerResults = $(ev.target).data('offerResults');
this.showAllFacets = offerResults;
if (this.showAllFacets) {
this.showResults();
$(document).trigger('uiShowResults', {});
} else {
$('#facets').removeClass('control-sidebar');
$('#facets').addClass('control-survey');
$(document).trigger('uiHideResults', {});
}
this.displayFacets();
};
this.showResults = function() {
$('#facets').addClass('control-sidebar');
$('#facets').removeClass('control-survey');
};
this.nextPrevHandler = function(ev) {
if (typeof this.facetHistory === 'undefined') {
this.facetHistory = [];
}
var clickedEl = $(ev.target);
var facility_type = $(ev.target).data('facility-type');
var jump_to_results = $(ev.target).data('jump-to-results');
var offset = parseInt(clickedEl.data('nextFacetOffset'), 10);
if (clickedEl.is('.previous')) {
var facet = _.keys(this.facetData)[offset];
$(document).trigger('uiClearFacets', {facet: facet});
this.setFacetOffset(this.facetHistory.pop());
} else {
if (facility_type) {
$(document).trigger('uiClearFacets', {facet: 'facility_type'});
$(document).trigger('uiClearFacets', {facet: 'out_patient'});
$(document).trigger('uiClearFacets', {facet: 'gender'});
$(document).trigger('uiClearFacets', {facet: 'pregnancy'});
$(document).trigger('uiClearFacets', {facet: 'age'});
$(document).trigger('uiClearFacets', {facet: 'insurance'});
$(document).trigger('uiFilterFacet', {
facet: 'facility_type',
selected: [facility_type]
});
if (facility_type == 'outpatient_offered') {
// outpatient offered settings appear to be injected based on selecting things in the first panel
// so we'll give it 1/10 of a second to do what it needs do and then trigger another jump
var that=this;
window.setTimeout((function(){that.setFacetOffsetWithObject(that)}),100);
}
}
var lastItem = this.facetHistory[this.facetHistory.length - 1];
if (lastItem !== this.facetOffset) {
this.facetHistory.push(this.facetOffset);
}
this.setFacetOffset(offset);
if (jump_to_results) {
$(document).trigger('uiShowResults', {});
}
}
};
this.setFacetOffset = function(offset) {
this.facetOffset = offset;
this.displayFacets();
};
this.setFacetOffsetWithObject = function(obj,offset) { obj.setFacetOffset(1); }
this.clearFacets = function(ev) {
ev.preventDefault();
var facet = $(ev.target).data('facet');
$(document).trigger('uiClearFacets', {facet: facet});
};
this.selectFacet = function(ev) {
var $form = $(ev.target).parents('form'),
facet = $form.data('facet'),
selected = _.map($form.serializeArray(),
'name');
window.setTimeout(function() {
$(document).trigger('uiFilterFacet', {
facet: facet,
selected: selected
});
}, 0);
};
this.showNoTreatment = function() {
this.$node.html(templates.assessment());
};
// defaultAttrs is now deprecated in favor of 'attributes', but our
// version of flight still uses this.
this.defaultAttrs({
clearFacetsSelector : ".clear-facets"
});
this.after('initialize', function() {
this.on('change', this.selectFacet);
this.on(document, 'config', this.configureFacets);
this.on(document, 'dataFacets', this.displayFacets);
this.on(document, 'uiShowResults', this.showResults);
this.on(document, 'uiFacetChangeRequest', function(ev, facet) {
var input = $('input[name=' + facet.name + ']');
input.prop('checked', true);
input.trigger('change');
});
this.on('click', { clearFacetsSelector : this.clearFacets });
});
});
});