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
//! Generic view that is always displayed in Roost

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

use css_rs_macro::css;
#[cfg(target_arch = "wasm32")]
use js_sys::Reflect;
use url::percent_encoding::{utf8_percent_encode, PATH_SEGMENT_ENCODE_SET};
use virtual_dom_rs::prelude::*;
#[cfg(target_arch = "wasm32")]
use wasm_bindgen::JsCast;

#[cfg(target_arch = "wasm32")]
use crate::state::Msg;
use crate::store::Store;
use crate::views::{DirView, FileView};

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

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

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

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

        let root_name = "(root)";
        let separator = " / ";
        let mut parent_dirs: Vec<VirtualNode> = entry_path.ancestors().filter(|a| *a != Path::new("")).map(|ancestor| {
            if ancestor == entry_path {
                if ancestor != Path::new("./") {
                    let mut entry_name = ancestor.file_name().unwrap().to_str().unwrap().to_string();
                    if state.is_dir() {
                        entry_name.push_str(" /");
                    }
                    html! {
                        <span>
                            { entry_name }
                        </span>
                    }
                } else {
                    html! {
                        <span>{ root_name }{ separator }</span>
                    }
                }
            } else {
                let ancestor_path = ancestor.as_os_str().to_str().unwrap();
                match ancestor.file_name() {
                    Some(ancestor_name) => {
                        let decoded_path = &ancestor_path[2..];
                        let encoded_path = utf8_percent_encode(&decoded_path, PATH_SEGMENT_ENCODE_SET).to_string();
                        let path = format!("{}", encoded_path);
                        html! {
                            <span><a href=path>{ ancestor_name.to_str().unwrap() }</a>{ separator }</span>
                        }
                    }
                    None => {
                        let path = match patch {
                            Some(patch) => format!("/patch/{}", patch),
                            None        => "/".to_string()
                        };
                        html! {
                            <span><a href=path>{ root_name }</a>{ separator }</span>
                        }
                    }
                }
            }
        }).collect();
        parent_dirs.reverse();

        let selected_patch = state.selected_patch();
        let patches: Vec<VirtualNode> = state
            .patches()
            .iter()
            .map(|patch| {
                let title = format!(
                    "{}\n\n{}\n{}",
                    {
                        #[cfg(target_arch = "wasm32")]
                        {
                            &patch.name
                        }
                        #[cfg(not(target_arch = "wasm32"))]
                        {
                            patch.name.replace('\"', "&quot;")
                        }
                    },
                    patch.authors[0],
                    patch.timestamp.to_rfc2822()
                );

                let name = if patch.name.len() > 15 {
                    let (short_name, _remaining) = patch.name.split_at(15);
                    let mut name = short_name.to_string();
                    name.push_str("...");
                    name
                } else {
                    patch.name.to_string()
                };

                match selected_patch {
                    Some(selected_patch) => {
                        if patch.hash == *selected_patch {
                            html! {
                                <option
                                    title=title
                                    value=patch.hash
                                    selected="selected"
                                >{ name }</option>
                            }
                        } else {
                            html! {
                                <option
                                    title=title
                                    value=patch.hash
                                >{ name }</option>
                            }
                        }
                    }
                    None => html! {
                        <option
                            title=title
                            value=patch.hash
                        >{ name }</option>
                    },
                }
            })
            .collect();

        let content = if state.is_dir() {
            DirView::new(Rc::clone(&self.store)).render()
        } else {
            FileView::new(Rc::clone(&self.store)).render()
        };

        #[cfg(target_arch = "wasm32")]
        let store = Rc::clone(&self.store);

        html! {
            <div class=CONTENT_CSS>
                { parent_dirs }
                <label style="float: right;">
                    Selected Patch:
                    <select
                        onchange=move |event: web_sys::Event| {
                            let target = event
                                .target()
                                .unwrap()
                                .dyn_into::<web_sys::Element>()
                                .unwrap();
                            let value = Reflect::get(&target, &"value".into())
                                .unwrap()
                                .as_string()
                                .unwrap();
                            let encoded_path = store.borrow().encoded_path();
                            let path = if value == "current-state" {
                                if encoded_path.is_empty() {
                                    "/".to_string()
                                } else {
                                    encoded_path.to_string()
                                }
                            } else {
                                if encoded_path.is_empty() {
                                    format!("/patch/{}", &value)
                                } else {
                                    format!("/patch/{}/{}", &value, encoded_path)
                                }
                            };
                            store.borrow_mut().msg(&Msg::SetPath(path));
                        }
                    >{ patches }</select>
                </label>
                { content }
            </div>
        }
    }
}

static CONTENT_CSS: &'static str = css! {r#"
:host {
    font-family: 'Open Sans', sans-serif;
    border: 1px solid Black;
    padding: 20px;
    margin: auto;
    margin-top: 20px;
    margin-bottom: 20px;
    width: 65%;
}
"#};