forked from mynameistechno/finderjs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
example-async.js
158 lines (131 loc) · 3.86 KB
/
example-async.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
'use strict';
var xhr = require('xhr');
var extend = require('xtend');
var finder = require('../index');
var _ = require('../util');
var emitter;
var xhrCnt = 0;
module.exports = createExample;
function createExample(container) {
emitter = finder(container, remoteSource, {
createItemContent: createItemContent
});
// scroll to the right if necessary
emitter.on('column-created', function columnCreated() {
container.scrollLeft = container.scrollWidth - container.clientWidth;
});
}
function remoteSource(parent, cfg, callback) {
var loadingIndicator = createLoadingColumn();
var xhrUid = ++xhrCnt;
var type = 'region';
// determine which column we're on based on previous selection
if (parent) {
if (parent.type === 'region') {
type = 'subregion';
} else if (parent.type === 'subregion') {
type = 'name'; // country
} else { // must be a country
return cfg.emitter.emit('create-column', createSimpleColumn(parent));
}
}
// loading spinner
cfg.emitter.emit('create-column', loadingIndicator);
// xhr request
xhr({
uri: 'https://restcountries.eu/rest/v1/all'
}, function done(err, resp, body) {
var rawData = JSON.parse(body);
var data = uniqueCountryData(rawData, type, parent);
// clear loading spinner
_.remove(loadingIndicator);
// stale request
if (xhrUid !== xhrCnt) {
return;
}
// execute callback
callback(data);
});
}
// transform rest country data for example
function uniqueCountryData(data, type, parent) {
var hash = data.reduce(function each(prev, curr) {
if (!(curr[type] in prev)) {
if (parent) {
if (parent.label === curr[parent.type]) {
prev[curr[type]] = curr;
}
} else if (curr[type]) {
prev[curr[type]] = curr;
}
}
return prev;
}, {});
return Object.keys(hash).map(function each(item) {
return extend(hash[item], {
label: item,
type: type === 'name' ? 'country' : type
});
});
}
// item render
function createItemContent(cfg, item) {
var data = item.children || cfg.data;
var frag = document.createDocumentFragment();
var label = _.el('span');
var iconPrepend = _.el('i');
var iconAppend = _.el('i');
var prependClasses = ['fa'];
var appendClasses = ['fa'];
// prepended icon
if (item.type === 'region') {
prependClasses.push('fa-globe');
} else if (item.type === 'subregion') {
prependClasses.push('fa-compass');
} else {
prependClasses.push('fa-map-marker');
}
_.addClass(iconPrepend, prependClasses);
// text label
label.appendChild(iconPrepend);
label.appendChild(_.text(item.label));
frag.appendChild(label);
// appended icon
if (data) {
appendClasses.push('fa-caret-right');
} else if ('url' in item) {
appendClasses.push('fa-external-link');
}
_.addClass(iconAppend, appendClasses);
frag.appendChild(iconAppend);
return frag;
}
function createLoadingColumn() {
var div = _.el('div.fjs-col.leaf-col');
var row = _.el('div.leaf-row');
var text = _.text('Loading...');
var i = _.el('span');
_.addClass(i, ['fa', 'fa-refresh', 'fa-spin']);
_.append(row, [i, text]);
return _.append(div, row);
}
function createSimpleColumn(item) {
var div = _.el('div.fjs-col.leaf-col');
var row = _.el('div.leaf-row');
var filename = _.text(item.label);
var i = _.el('i');
var capital = _.el('div.meta');
var capitalLabel = _.el('strong');
var population = _.el('div.meta');
var populationLabel = _.el('strong');
_.addClass(i, ['fa', 'fa-info-circle']);
_.append(capitalLabel, _.text('Capital: '));
_.append(capital, [capitalLabel, _.text(item.capital)]);
_.append(populationLabel , _.text('Population: '));
_.append(population, [
populationLabel,
_.text(Number(item.population).toLocaleString())
]);
_.append(row, [i, filename, capital, population]);
return _.append(div, row);
}