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
//! Displays directory contents

use std::cell::RefCell;
use std::rc::Rc;

use css_rs_macro::css;
use virtual_dom_rs::prelude::*;

use crate::state::Status;
use crate::store::Store;
use crate::views::MdView;

pub struct DirView {
    store: Rc<RefCell<Store>>,
}

impl DirView {
    pub fn new(store: Rc<RefCell<Store>>) -> DirView {
        DirView { store }
    }
}

impl View for DirView {
    fn render(&self) -> VirtualNode {
        let state = self.store.borrow();

        let encoded_path = state.encoded_path();
        let patch = state.selected_patch();

        let dir_entries: Vec<VirtualNode> = match state.dir_contents() {
            Some(entries) => {
                (entries
                    .iter()
                    .map(|entry| {
                        let entry_path = if encoded_path.is_empty() {
                            match patch {
                                Some(patch) => format!("/patch/{}/{}", patch, &entry.name),
                                None => format!("/path/{}", &entry.name),
                            }
                        } else {
                            format!("{}{}", encoded_path, &entry.name)
                        };
                        let style = match entry.status {
                            Some(Status::Modified) => "background: Yellow;",
                            Some(Status::Added) => "background: Green;",
                            Some(Status::Removed) => "background: Red;",
                            Some(Status::Moved) => "background: Teal;",
                            None => "",
                        };
                        let emoji = if entry.is_dir {
                            #[cfg(target_arch = "wasm32")]
                            {
                                "\u{1f4c1}"
                            }
                            #[cfg(not(target_arch = "wasm32"))]
                            {
                                "&#x1f4c1;"
                            }
                        } else {
                            #[cfg(target_arch = "wasm32")]
                            {
                                "\u{1f4c4}"
                            }
                            #[cfg(not(target_arch = "wasm32"))]
                            {
                                "&#x1f4c4;"
                            }
                        };

                        let patch_path = if entry.latest_patch.hash == "current-state" {
                            format!("/path/{}{}", encoded_path, &entry.name)
                        } else {
                            format!(
                                "/patch/{}/{}{}",
                                entry.latest_patch.hash, encoded_path, &entry.name
                            )
                        };
                        html! {
                            <tr style=style>
                                <td
                                    title=entry.name.to_string()
                                    class=TABLE_CELL_CSS
                                >
                                    <a
                                        href=entry_path
                                        class=ENTRY_CSS
                                    >
                                        { emoji } { entry.name.to_string() }
                                    </a>
                                </td>
                                <td
                                    title=entry.latest_patch.name.to_string()
                                    class=TABLE_CELL_CSS
                                >
                                    <a
                                        href=patch_path
                                        class=ENTRY_CSS
                                    >
                                        { entry.latest_patch.name.to_string() }
                                    </a>
                                </td>
                                <td
                                    title=entry.latest_patch.timestamp.to_rfc2822()
                                    class=TABLE_CELL_CSS
                                    style="text-align: right;"
                                >
                                    <a
                                        href=patch_path
                                        class=ENTRY_CSS
                                    >
                                        { entry.latest_patch.timestamp.to_rfc2822() }
                                    </a>
                                </td>
                            </tr>
                        }
                    })
                    .collect())
            }
            None => vec![VirtualNode::text("No directory found")],
        };

        let readme = MdView::new(Rc::clone(&self.store)).render();

        html! {
            <div>
                <table width="100%" style="table-layout: fixed;">
                    { dir_entries }
                </table>
                { readme }
            </div>
        }
    }
}

static ENTRY_CSS: &'static str = css! {"
:host {
    color: DarkSlateGray;
    text-decoration: none;
}

:host:hover {
    color: Blue;
    font-weight: bold;
}
"};

static TABLE_CELL_CSS: &'static str = css! {"
:host {
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}
"};