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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
<script lang="ts">
    import { VegaLite } from "svelte-vega";
    import type { View } from "svelte-vega";
    import DataTable, {
        Head,
        Body,
        Row,
        Cell,
        Pagination,
    } from "@smui/data-table";
    import Select, { Option } from "@smui/select";
    import IconButton from "@smui/icon-button";
    import Button from "@smui/button";
    import { Label } from "@smui/common";
    import Checkbox from '@smui/checkbox';
    import Radio from '@smui/radio';
    import FormField from '@smui/form-field';
    import Tooltip, { Wrapper } from '@smui/tooltip';
    import LayoutGrid, { Cell as LGCell } from "@smui/layout-grid";
    import Card, { Content } from '@smui/card';

    import HelpTooltip from "./HelpTooltip.svelte";
    import { topic_chosen } from './stores/cur_topic_store.js';
    import { new_evidence } from './stores/new_evidence_store.js';
    import { open_evidence } from './stores/open_evidence_store.js';

    export let data;
    export let cluster;
    export let clusters = null;
    export let model;
    export let show_vis = true;
    export let show_checkboxes = true;
    export let table_width_pct = 80;
    export let rowsPerPage = 10;
    export let evidence;
    export let table_id;
    export let use_model = true;

    let N_COMMENTS = 500;
    let show_num_ratings = false;
    let show_your_decision_ratings = false;
    let show_step2_info = false;

    let comment_table_style;
    if (show_checkboxes) {
        comment_table_style = ""
    } else {
        comment_table_style = "comment_table_small"
    }

    // Handle Altair selections
    let selected_comment_id = 0;
    window.addEventListener("popstate", function (event) {
    //your code goes here on location change 
        let cur_url = window.location.href;
        let cur_url_elems = cur_url.split("#");
        // console.log(cur_url_elems)
        if (cur_url_elems.length > 0) {
            let path = cur_url_elems[2];
            if (path == "comment") {
                let comment_id = cur_url_elems[1].split("/")[0];
                console.log("comment_id", comment_id)
                selected_comment_id = parseInt(comment_id);
                let table_ind = null;
                for (let i = 0; i < items.length; i++) {
                    if (items[i]["id"] == selected_comment_id) {
                        table_ind = i;
                        break;
                    }
                }
                currentPage = table_ind / rowsPerPage;
            } else if (path == "topic") {
                let topic = cur_url_elems[1].split("/")[0];
                topic_chosen.update((value) => topic); // update in store
            }
            // window.history.replaceState({}, document.title, "/"); // remove URL parameter
        }
    });

    // Cluster Overview Plot
    let cluster_overview_data = null;
    let cluster_overview_spec = null;
    let cluster_overview_view = null;
    if (show_vis) {
        let cluster_overview_json = data["cluster_overview_plot_json"];
        cluster_overview_data =
            cluster_overview_json["datasets"][
                cluster_overview_json["layer"][0]["data"]["name"]
            ];
        cluster_overview_spec = cluster_overview_json;
        cluster_overview_view = null;
    }
    
    type ClusterComment = {
        id: number;
        comment: string;
        user_decision: string;
        user_rating: number;
        system_decision: string;
        system_rating: number;
        user_color: string;
        system_color: string;
        error_type: string;
        error_color: string;
        judgment: string;
        toxicity_category: string;
    };
    let items: ClusterComment[];

    let selected = [];

    // Pagination
    let currentPage = 0;
    $: start = currentPage * rowsPerPage;
    $: end = Math.min(start + rowsPerPage, items.length);
    $: slice = items.slice(start, end);
    $: lastPage = Math.max(Math.ceil(items.length / rowsPerPage) - 1, 0);

    $: if (currentPage > lastPage) {
        currentPage = lastPage;
    }

    let set_length = 0;
    // if (typeof(data["cluster_comments"] == "string")) {
    if (show_checkboxes) {
        items = JSON.parse(data["cluster_comments"]);
        set_length = data["topic_df_ids"].length;
    } else {
        items = data["cluster_comments"];
        set_length = items.length;
    }
    // console.log(set_length);
    
    let cur_open_evidence;
    open_evidence.subscribe(value => {
		cur_open_evidence = value;
	});

    function saveToEvidence() {
        new_evidence.update((value) => []); // clear prior evidence
        selected.forEach(function(s) {
            if (!cur_open_evidence.includes(s)) {
                new_evidence.update((value) => s); // update in store
            }
        });
        selected = [];

        // Clear highlighted rows
        let rows = document.getElementsByTagName("tr");
        let row_list = Array.prototype.slice.call(rows);
        row_list.forEach(function(r) {
            r.classList.remove("mdc-data-table__row--selected");
        });

        let checkbox_header_divs = document.getElementsByClassName("mdc-data-table__header-row-checkbox");
        let checkbox_header_list = Array.prototype.slice.call(checkbox_header_divs);
        checkbox_header_list.forEach(function(c) {
            let c_input = c.getElementsByTagName("input");
            for (let i = 0; i < c_input.length; i++) {
                c_input[i].setAttribute("data-indeterminate", "false");
                c_input[i].indeterminate = false;
            }
        });
    }

    function handleAdd(comment_to_remove) {
        new_evidence.update((value) => []); // clear prior evidence
        if (!cur_open_evidence.includes(comment_to_remove)) {
            new_evidence.update((value) => comment_to_remove); // update in store
        }
    }

    function handleRemove(comment_to_remove) {
        // Update local open evidence
        cur_open_evidence = cur_open_evidence.filter(item => item.comment != comment_to_remove)
        // Update open evidence in store
        open_evidence.update((value) => cur_open_evidence);
    }
</script>

<div class="padding-top: 30px;">
    {#if show_vis}
        <div>
            <span class="head_6">Topic overview: {cluster}</span>
            <IconButton 
                class="material-icons grey_button"
                size="normal"
                on:click={() => (show_step2_info = !show_step2_info)}
            >
                help_outline
            </IconButton>
        </div>
        {#if N_COMMENTS < set_length}
            <p>Showing a random sample of {N_COMMENTS} comments (out of {set_length} comments)</p>
        {:else}
            <p>Showing all {set_length} comments</p>
        {/if}

        {#if show_step2_info}
            <LayoutGrid>
                <LGCell span={8}>
                    <div class="card-container">
                        <Card variant="outlined" padded>
                            <p class="mdc-typography--button"><b>Interpreting this visualization</b></p>
                            <ul>
                                <li>
                                    This plot has the same layout as the <b>All Topics</b> visualization, but now, each <b>box</b> in this plot represents an <b>individual comment</b> that belongs to your <b>selected topic area</b>.
                                </li>
                                <li>
                                    The <b>x-axis</b> represents our prediction of <b>your</b> toxicity rating for each comment (we'll call these "your ratings")
                                    <ul>
                                        <li>
                                            The <b>left side</b> (white background) is the <b>Non-toxic</b> side (comments that'll be allowed to remain)
                                        </li>
                                        <li>
                                            The <b>right side</b> (grey background) is the <b>Toxic</b> side (comments that will be deleted)
                                        </li>
                                        <li>
                                            Comment boxes are plotted along the x-axis based on our prediction of your toxicity rating for that comment
                                        </li>
                                    </ul>
                                </li>
                    
                                <li>
                                    The <b>color</b> of the box indicates the <b>system's rating</b> for the same comment; you may want to focus on the <b>red-colored boxes</b> that indicate <b>disagreements</b> between "your ratings" and the system's ratings
                                </li>
                            </ul>
                        </Card>
                    </div>
                </LGCell>
            </LayoutGrid>
        {/if}

        <div class="row">
            <div class="col s8">
                <VegaLite
                    {cluster_overview_data}
                    spec={cluster_overview_spec}
                    bind:view={cluster_overview_view}
                />
            </div>
        </div>
    {/if}
    
    {#if show_checkboxes}
    <h6>Comments</h6>
    {/if}
    <!-- Display options -->
    {#if show_checkboxes}
    <div>
        Numerical ratings:
        <FormField>
            <Radio bind:group={show_num_ratings} value={true} color="secondary" />
            <span slot="label">Show</span>
        </FormField>
        <FormField>
            <Radio bind:group={show_num_ratings} value={false} color="secondary" />
            <span slot="label">Hide</span>
        </FormField>
    </div>
    {#if use_model}
    <div>
        Our prediction of your decision + ratings:
        <FormField>
            <Radio bind:group={show_your_decision_ratings} value={true} color="secondary" />
            <span slot="label">Show</span>
        </FormField>
        <FormField>
            <Radio bind:group={show_your_decision_ratings} value={false} color="secondary" />
            <span slot="label">Hide</span>
        </FormField>
    </div>
    {/if}

    <!-- <Wrapper>
        <IconButton class="material-icons" size="button" disabled>help_outline</IconButton>
        <Tooltip>White = Non-toxic, Grey = Toxic</Tooltip>
    </Wrapper> -->
    {/if}

    {#key evidence}
    <div class="comment_table {comment_table_style}">
        <DataTable
            table$aria-label="Comments in the topic cluster"
            style="width: {table_width_pct}%;"
        >
            <Head>
                <Row>
                    <!-- {#if show_checkboxes}
                    <Cell checkbox>
                        <Checkbox />
                    </Cell>
                    {/if} -->

                    <Cell style="width: 50%">Comment</Cell>

                    {#if show_your_decision_ratings}
                    <Cell>Our prediction<br>of your decision</Cell>
                    {#if show_num_ratings}
                    <Cell>Our prediction<br>of your rating</Cell>
                    {/if}
                    {/if}
                    
                    <Cell>
                        System<br>decision<br>
                        {#if show_checkboxes}
                        <span style="font-size:12px; max-width:125px">White = Non-toxic, <br>Grey = Toxic</span>
                        {/if}
                    </Cell>
                    {#if show_num_ratings}
                    <Cell>System<br>rating</Cell>
                    {/if}

                    {#if show_checkboxes}
                    {#if use_model}
                    <Cell>
                        Potential error<br>type<br>
                        {#if show_checkboxes}
                        <span style="font-size:12px; max-width:125px">Darker red = Greater <br>potential system error</span>
                        {/if}
                    </Cell>

                    <Cell>Potential toxicity<br>categories</Cell>
                    {/if}
                    {/if}

                    <Cell>Do you agree<br>with the system?</Cell>

                    {#if !show_checkboxes}
                    <Cell>Remove</Cell>
                    {/if}

                    {#if show_checkboxes}
                    <Cell>Add<br>Evidence</Cell>
                    {/if}
                </Row>
            </Head>
            <Body>
                {#each slice as item (item.id + table_id)}
                    <Row>
                        <!-- {#if show_checkboxes}
                        <Cell checkbox>
                            <Checkbox
                                bind:group={selected}
                                value={{
                                    "comment": item.comment, 
                                    "user_color": item.user_color, 
                                    "user_decision": item.user_decision, 
                                    "user_rating": item.user_rating, 
                                    "system_color": item.system_color, 
                                    "system_decision": item.system_decision,
                                    "system_rating": item.system_rating, 
                                    "error_type": item.error_type, 
                                    "error_color": item.error_color,
                                    "toxicity_category": item.toxicity_category,
                                    "judgment": item.judgment, 
                                    "id": item.id
                                }}
                                valueKey={item.comment}
                            />
                        </Cell>
                        {/if} -->

                        <Cell>
                            {item.comment}
                        </Cell>

                        {#if show_your_decision_ratings}
                        <Cell style="background-color: {item.user_color}; border-left: 1px solid rgba(0,0,0,.12); border-right: 1px solid rgba(0,0,0,.12); border-collapse: collapse;">
                            {item.user_decision}
                        </Cell>
                        {#if show_num_ratings}
                        <Cell style="background-color: {item.user_color}; border-left: 1px solid rgba(0,0,0,.12); border-right: 1px solid rgba(0,0,0,.12); border-collapse: collapse;">
                            {item.user_rating}
                        </Cell>
                        {/if}
                        {/if}

                        <Cell style="background-color: {item.system_color}; border-left: 1px solid rgba(0,0,0,.12); border-right: 1px solid rgba(0,0,0,.12); border-collapse: collapse;">
                            {item.system_decision}
                        </Cell>
                        {#if show_num_ratings}
                        <Cell style="background-color: {item.system_color}; border-left: 1px solid rgba(0,0,0,.12); border-right: 1px solid rgba(0,0,0,.12); border-collapse: collapse;">
                            {item.system_rating}
                        </Cell>
                        {/if}

                        {#if show_checkboxes}
                        {#if use_model}
                        <Cell style="background-color: {item.error_color}; border-left: 1px solid rgba(0,0,0,.12); border-right: 1px solid rgba(0,0,0,.12); border-collapse: collapse;">
                            {item.error_type}
                        </Cell>

                        <Cell>
                            {item.toxicity_category}
                        </Cell>
                        {/if}
                        {/if}

                        <Cell>
                            <div>
                                <FormField>
                                    <Radio
                                      bind:group={item.judgment}
                                      value={"Agree"}
                                    />
                                    <span slot="label">Agree</span>
                                </FormField>
                            </div>
                            <div>
                                <FormField>
                                    <Radio
                                      bind:group={item.judgment}
                                      value={"Disagree"}
                                    />
                                    <span slot="label">Disagree</span>
                                </FormField>
                            </div>
                        </Cell>

                        {#if !show_checkboxes}
                        <Cell>
                            <IconButton class="material-icons grey_button" on:click={() => handleRemove(item.comment)}>
                                remove_circle_outline
                            </IconButton>
                        </Cell>
                        {/if}

                        {#if show_checkboxes}
                        <Cell>
                            <IconButton class="material-icons grey_button" on:click={() => handleAdd(item)}>
                                add_circle_outline
                            </IconButton>
                        </Cell>
                        {/if}
                    </Row>
                {/each}
            </Body>

            <!-- Table pagination -->
            <Pagination slot="paginate">
                <svelte:fragment slot="rowsPerPage">
                    <Label>Rows Per Page</Label>
                    <Select variant="outlined" bind:value={rowsPerPage} noLabel>
                        <Option value={5}>5</Option>
                        <Option value={10}>10</Option>
                        <Option value={25}>25</Option>
                        <Option value={100}>100</Option>
                    </Select>
                </svelte:fragment>
                <svelte:fragment slot="total">
                    {start + 1}-{end} of {items.length}
                </svelte:fragment>

                <IconButton
                    class="material-icons"
                    action="first-page"
                    title="First page"
                    on:click={() => (currentPage = 0)}
                    disabled={currentPage === 0}>first_page</IconButton
                >
                <IconButton
                    class="material-icons"
                    action="prev-page"
                    title="Prev page"
                    on:click={() => currentPage--}
                    disabled={currentPage === 0}>chevron_left</IconButton
                >
                <IconButton
                    class="material-icons"
                    action="next-page"
                    title="Next page"
                    on:click={() => currentPage++}
                    disabled={currentPage === lastPage}
                    >chevron_right</IconButton
                >
                <IconButton
                    class="material-icons"
                    action="last-page"
                    title="Last page"
                    on:click={() => (currentPage = lastPage)}
                    disabled={currentPage === lastPage}>last_page</IconButton
                >
            </Pagination>
        </DataTable>
    </div>
    {/key}

    <!-- {#if show_checkboxes}
    <div class="spacing_vert">
        <Button on:click={saveToEvidence} disabled={selected.length == 0} variant="outlined">
            <Label>Save {selected.length} to evidence</Label>
        </Button>
    </div>
    {/if} -->
    
    <!-- Old visualization -->
    <!-- {#if show_vis}
        <div style="margin-top: 500px">
            <table>
                <tbody>
                    <tr class="custom-blue">
                        <td class="bold">
                            Compared to the system, YOUR labels are on average...
                        </td>
                        <td>
                            <span class="bold-large"
                                >{data["user_perf_rounded"]} points
                                {data["user_direction"]}</span
                            >
                            for this cluster
                        </td>
                    </tr>
                    <tr>
                        <td class="bold">
                            Compared to the system, OTHER USERS' labels are on
                            average...
                        </td>
                        <td>
                            <span class="bold-large"
                                >{data["other_perf_rounded"]} points
                                {data["other_direction"]}</span
                            >
                            for this cluster (based on {data["n_other_users"]} randomly-sampled
                            users)
                        </td>
                    </tr>
                    <tr>
                        <td class="bold"> Odds ratio </td>
                        <td>
                            <span class="bold-large">{data["odds_ratio"]}</span><br />
                            {data["odds_ratio_explanation"]}
                        </td>
                    </tr>
                </tbody>
            </table>
        
            <h6>Cluster examples</h6>
            <div class="row">
                <div class="col s12">
                    <div id="cluster_results_elem">
                        {@html data["cluster_examples"]}
                    </div>
                </div>
            </div>
        </div>
    {/if} -->
</div>

<style>
    /* Styles for table */
    :global(html) {
        height: auto;
        width: auto;
        position: static;
    }
    :global(#sapper),
    :global(body) {
        display: block;
        height: auto;
    }
</style>