-
Notifications
You must be signed in to change notification settings - Fork 24
/
sync-url-options.js
186 lines (174 loc) · 6.55 KB
/
sync-url-options.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
// utility to keep query string parameters in sync with some inputs (and vice versa)
// currently uses jquery but otherwise it maybe should be released separately of dc.graph
// Copyright 2016-2017 AT&T Intellectual Property
// License: Apache v2
var sync_url_options = (function() {
if(!querystring)
throw new Error('need querystring library');
function read_query(type, val) {
switch(type) {
case 'boolean':
return val === 'true';
case 'number':
return +val;
case 'string':
return val;
case 'array':
return val.split(querystring.listsep());
default: throw new Error('unsupported query type ' + type);
}
}
function write_query(type, val) {
switch(type) {
case 'array':
return val.join(querystring.listsep());
case 'boolean':
case 'number':
case 'string':
return '' + val;
default: throw new Error('unsupported query type ' + type);
}
}
function query_type(val) {
return Array.isArray(val) ? 'array' : val === null ? 'string' : typeof val;
}
// we could probably depend on _, but _.pick is the only thing we need atm
function pick(object, fields) {
return fields.reduce(function(reduced, key) {
if(key in object)
reduced[key] = object[key];
return reduced;
}, {});
}
function option_synchronizer(options, domain, args) {
var qs = querystring.parse();
var settings = {};
var _output = function(m) {
querystring.update(m);
};
function interesting_params(qs) {
var interesting = Object.keys(options)
.filter(function(k) {
return qs[options[k].query] !== write_query(query_type(options[k].default), options[k].default);
}).map(function(k) {
return options[k].query || k;
});
return pick(qs, interesting);
}
function update_interesting(qs) {
_output(interesting_params(qs));
}
function do_option(key, opt, callback) {
settings[key] = opt.default;
var query = opt.query = opt.query || key;
var type = query_type(opt.default);
if(query in qs)
settings[key] = read_query(type, qs[query]);
function update_setting(opt, val) {
settings[key] = val;
if(opt.query) {
qs[opt.query] = write_query(type, val);
update_interesting(qs);
}
}
if(opt.values) { // generate <select> options
var select = d3.select(opt.selector);
var opts = select.selectAll('option').data(opt.values);
opts.enter().append('option').attr({
value: function(x) { return x; },
selected: function(x) { return x === settings[key]; }
}).text(function(x) { return x; });
select
.property('value', settings[key]);
}
if(opt.selector) {
switch(type) {
case 'boolean':
if(!opt.set)
opt.set = function(val) {
$(opt.selector)
.prop('checked', val);
};
if(!opt.subscribe)
opt.subscribe = function(k) {
$(opt.selector)
.change(function() {
var val = $(this).is(':checked');
k(val);
});
};
break;
case 'number':
case 'string':
if(!opt.set)
opt.set = function(val) {
$(opt.selector)
.val(val);
};
if(!opt.subscribe)
opt.subscribe = function(k) {
$(opt.selector)
.change(function() {
var val = $(this).val();
k(val);
});
};
break;
default: throw new Error('unsupported selector type ' + type);
}
}
if(opt.set)
opt.set(settings[key]);
opt.update = function(val, manual) {
update_setting(opt, val);
callback && callback(val, manual);
};
if(opt.subscribe)
opt.subscribe(opt.update);
}
for(var key in options) {
var callback = function(opt, val, manual) {
args[0] = val;
if(opt.exert && (manual || !opt.dont_exert_after_subscribe))
opt.exert.apply(opt, args);
if(domain && domain.on_exert)
domain.on_exert(opt);
};
if(typeof options[key] !== 'object' || options[key] === null)
options[key] = {
default: options[key]
};
do_option(key, options[key], callback.bind(null, options[key]));
}
return {
vals: settings,
exert: function() {
for(var key in options)
if(options[key].exert) {
args[0] = settings[key];
options[key].exert.apply(options[key], args);
}
},
output: function(_) {
if(!arguments.length)
return _output;
_output = _;
return this;
},
update: function(k, v, do_ui) {
if(do_ui)
options[k].set(v);
options[k].update(v, true);
},
what_if_url: function(overrides) {
var qs2 = Object.assign({}, qs, overrides);
return querystring.get_url(interesting_params(qs2));
}
};
}
return function(options, domain /* ... arguments for exert ... */) {
var args = Array.prototype.slice.call(arguments, 2);
args.unshift(0);
return option_synchronizer(options, domain, args);
};
})();