This repository has been archived by the owner on Feb 6, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathview.categoriser.js
326 lines (294 loc) · 9.8 KB
/
view.categoriser.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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
Vue.config.devtools = true
$(document).ready(function() {
$("#loading").show();
$("#app").hide();
/* Load these all from the server. See individual files for expected format */
var parties = [];
var advertisers = [];
$.getJSON("datasets/parties.json?v="+Date.now(), (partiesJSON) => { parties = partiesJSON; start(); });
$.getJSON("datasets/mock-advertisers.json?v="+Date.now(), (advertisersJSON) => { advertisers = advertisersJSON; start(); });
function start() {
console.log(parties.length, advertisers.length);
if(parties.length == 0 || advertisers.length == 0) return false;
$("#loading").hide();
$("#app").show();
console.log("All loaded");
Vue.component('party-span', {
template: "#party-span",
props: {
'party': Object,
'chars': {
type: Number,
default: 20
},
'advertiser': Object
}
});
Vue.component('advertiser-table', {
template: "#advertiser-table",
props: {
'advertisers': Object,
'parties': Object,
'politicians': Object,
'suggestengine': Object,
'finished': {
default: false
}
},
methods: {
topparties: function(x) {
return this.parties[1].list.slice(0,x);
},
touch: function(x,prop,$event = null) {
var Component = this;
if($event) {
// console.log("Listening for selection on",$($event.target))
$($event.target).change(() => {
// console.log("Clicked dropdown",$event.target);
$($event.target).off('change');
touchHappened()
});
} else touchHappened();
function touchHappened() {
// console.log("!!!!! I just clicked ",x.advertiser);
x.touchedDate = Date.now();
x.touchedProperty = prop;
if(x) {
// console.log("Touched "+x.touchedProperty+" on "+x.advertiser);
if(x.touchedProperty == 'political') {
if(x.political == 'true') {
// console.log("Nonpartisan "+x.advertiser)
x.affiliation = 'no-party';
} else {
// console.log("Clearing affil for "+x.advertiser)
x.affiliation = '';
}
} else if(x.touchedProperty == 'affiliation') {
if(x.affiliation != '') {
// console.log("Applying political to "+x.advertiser)
x.political = 'true';
} else {
// console.log("Clearing political for "+x.advertiser)
x.political = 'false';
}
}
Component.$forceUpdate();
}
}
}
}
});
var App = new Vue({
el: '#app',
data: {
advertisers: advertisers, // To be loaded from the DB
politicians: [],
parties: parties, // To be loaded from the DB
suggestengine: false,
suggestionDatasets: [
{url:"datasets/candidates-2015.json?v="+Date.now(), data: []},
{url:"datasets/everypolitician-term-56-reduced.json?v="+Date.now(), data: []},
{url:"https://docs.google.com/spreadsheets/d/1my9yleXsOhl-m5KYg1lh1sjwrBNLWZKJ1xgCfNnTvCA/edit#gid=0", data: []}
]
},
mounted: function() {
var App = this;
var parties = [];
var advertisers = [];
$.getJSON("datasets/parties.json?v="+Date.now(), (partiesJSON) => { App.parties = partiesJSON });
$.getJSON("datasets/mock-advertisers.json?v="+Date.now(), (advertisersJSON) => { App.advertisers = advertisersJSON });
// function start() {
// if(App.parties.length == 0 || App.advertisers.length == 0) return false;
// console.log("Ready");
this.generatePartyColorClasses();
// }
// Load suggestion engine datasets
App.suggestionDatasets.forEach(function(dataset,index) {
if(dataset.url.includes("docs.google.com")) {
sheetrock({
url: dataset.url, // Published
query: "select A,B,C,D",
callback: function loadedStoryData(err, opts, dataCSV) {
var dataJSON = dataCSV.rows.map((x)=>x.cells);
dataJSON.shift()
dataJSON.forEach(function(datum,i) {
Object.keys(datum).forEach(function(cell,r) {
if(!isNaN(parseFloat(cell)) && isFinite(cell)) {
dataJSON[i][r] = parseFloat(cell)
}
})
})
store(dataJSON);
}
});
} else {
$.getJSON(dataset.url, function(dataJSON) {
store(dataJSON);
});
}
function store(dataJSON) {
console.log("Loaded "+dataset.url,dataJSON);
App.suggestionDatasets[index].data = dataJSON
if(App.suggestionDatasets.filter((dataset) => dataset.data.length == 0).length == 0) {
console.log("Suggestion Engine ready!");
var returnArr = [];
App.suggestionDatasets.forEach(function(dataset) {
returnArr = returnArr.concat(dataset.data);
})
App.politicians = returnArr;
App.advertisers.forEach(function(advert,index) {
App.suggestParty(advert,index);
})
App.suggestengine = true;
App.$forceUpdate();
}
}
});
},
computed: {
advertisersToClassify: function() {
var result = this.advertisers.filter(function (advertiser) {
return (
advertiser.political == '' ||
(
advertiser.political == 'true' &&
advertiser.affiliation == ''
)
)
})
// console.log("To-do",result);
return result;
},
advertisersClassified: function() {
var result= this.advertisers.filter(function (advertiser) {
return (
advertiser.political == 'false' ||
(
advertiser.political == 'true' &&
advertiser.affiliation != ''
)
)
})
// console.log("Classified",result);
return result.sort((a,b) => b.touchedDate - a.touchedDate );
}
},
methods: {
save: function() {
var Component = this;
$.ajax({
type: 'post',
url: "https://who-targets-me-staging.herokuapp.com/advertisers/",
dataType: 'json',
data: Component.advertisers,
headers: {"Access-Token": "1940d507e36e5034498f24d07d82041208d7d778fabc821cab32b0fc22463edc"}
}).done(function(data) {
console.log(data.status);
console.log("Saved to server",Component.advertisers);
}).fail(function(data) {
console.log(data.status);
console.log("Error saving, try again",Component.advertisers);
});
},
generatePartyColorClasses: function() {
var css = "";
this.parties[1].list.forEach(function(party) {
css +=`
.party-${party.id} {
border: 2px solid ${party.srgb ? '#'+party.srgb : 'gray'} !important;
color: black !important;
}
.party-${party.id}.selected {
background-color: ${party.srgb ? '#'+party.srgb : 'gray'} !important;
color: ${party.srgb ? invertHEX(party.srgb, true) : 'white'} !important;
}`;
});
$(`<style id='party-styles'>${css}</style>`).appendTo("head");
function invertHEX(hex, bw) {
if (hex.indexOf('#') === 0) {
hex = hex.slice(1);
}
// convert 3-digit hex to 6-digits.
if (hex.length === 3) {
hex = hex[0] + hex[0] + hex[1] + hex[1] + hex[2] + hex[2];
}
if (hex.length !== 6) {
throw new Error('Invalid HEX color.');
}
var r = parseInt(hex.slice(0, 2), 16),
g = parseInt(hex.slice(2, 4), 16),
b = parseInt(hex.slice(4, 6), 16);
if (bw) {
// http://stackoverflow.com/a/3943023/112731
return (r * 0.299 + g * 0.587 + b * 0.114) > 186
? '#000000'
: '#FFFFFF';
}
// invert color components
r = (255 - r).toString(16);
g = (255 - g).toString(16);
b = (255 - b).toString(16);
// pad each with zeros and return
return "#" + padZero(r) + padZero(g) + padZero(b);
}
function padZero(str, len) {
len = len || 2;
var zeros = new Array(len).join('0');
return (zeros + str).slice(-len);
}
},
suggestParty: function(advertiser) {
var Component = this;
console.log("Suggesting for "+advertiser.advertiser+", with "+Component.politicians.length+" possible matches")
var likelyParties = new Set();
var matchedEntities = Component.politicians.filter(function(candidate) {
return (
candidate.name == advertiser.advertiser
|| candidate.name.includes(advertiser.advertiser) // E.g. Jeremy Corbyn
|| advertiser.advertiser.includes(candidate.name) // and Jeremy Corbyn MP
|| (
candidate.facebook
&& candidate.facebook != ""
&& (
candidate.facebook.includes(advertiser.advertiser_id) // Might have a weird old one like https://www.facebook.com/Alan-Duncan-150454050066
|| (
advertiser.advertiser_vanity
&& advertiser.advertiser_vanity != ""
&& (
candidate.facebook.includes(advertiser.advertiser_vanity) // Newer profiles will use vanity url
|| advertiser.advertiser_vanity.includes(candidate.facebook) // Newer profiles will use vanity url
)
)
)
|| (
candidate.facebook_vanity
&& candidate.facebook_vanity != ""
&&advertiser.advertiser_vanity
&& advertiser.advertiser_vanity != ""
&& (
candidate.facebook_vanity.includes(advertiser.advertiser_vanity) // Newer profiles will use vanity url
|| advertiser.advertiser_vanity.includes(candidate.facebook_vanity) // Newer profiles will use vanity url
)
)
)
)
});
if(matchedEntities.length && matchedEntities.length > 0) {
// console.log("Matches for "+advertiser.advertiser,matchedEntities)
matchedEntities.forEach(function(entity) {
likelyParties.add(entity.party.toLowerCase());
});
}
likelyParties = Array.from(likelyParties)
var parties = [];
likelyParties.forEach(function(likelyPartyID) {
Component.parties[1].list.forEach(function(checkParty) {
if(likelyPartyID == checkParty.id) parties.push(checkParty);
});
})
advertiser.suggested = parties;
}
}
});
}
});