]> saetta.ns0.it Git - rust/zakform/commitdiff
Added File field. refactoring
authorAndrea Zagli <azagli@libero.it>
Sat, 7 Oct 2023 09:25:12 +0000 (11:25 +0200)
committerAndrea Zagli <azagli@libero.it>
Sat, 7 Oct 2023 09:25:12 +0000 (11:25 +0200)
src/fields.rs

index 57ac3350386d71f9dad653259c677165878d0e81..f06b5cfe89187b4b4d554666e93525346284525e 100644 (file)
@@ -1140,3 +1140,164 @@ _ => {},
                self.form = String::from(form);
        }
 }
+
+#[derive(Default)]
+pub struct FieldFile {
+       field: Field,
+       accept: String,
+}
+
+impl TField for FieldFile {
+       fn get_name(&self) -> String {
+               String::from(&self.field.name)
+       }
+
+       fn get_label(&self) -> String {
+               String::from(&self.field.label)
+       }
+
+       fn set_label(&mut self, label: &str) {
+               self.field.label = String::from(label);
+       }
+
+       fn get_disabled(&self) -> bool {
+               self.field.disabled
+       }
+
+       fn set_disabled(&mut self, disabled: bool) {
+               self.field.disabled = disabled;
+       }
+
+       fn get_invisible(&self) -> bool {
+               self.field.invisible
+       }
+
+       fn set_invisible(&mut self, invisible: bool) {
+               self.field.invisible = invisible;
+       }
+
+       fn get_help(&self) -> String {
+               String::from(&self.field.help)
+       }
+
+       fn set_help(&mut self, help: &str) {
+               self.field.help = String::from(help);
+       }
+
+       fn get_value(&self) -> String {
+               String::from(&self.field.value)
+       }
+
+       fn set_value(&mut self, value: &str) {
+               self.field.value = String::from(value);
+       }
+
+       fn get_class(&self) -> String {
+               String::from(&self.field.class)
+       }
+
+       fn set_class(&mut self, class: &str) {
+               self.field.class = String::from(class);
+       }
+
+       fn get_to_load(&self) -> bool {
+               self.field.to_load
+       }
+
+       fn set_to_load(&mut self, load: bool) {
+               // always false
+       }
+
+       fn get_to_save(&self) -> bool {
+               self.field.to_save
+       }
+
+       fn set_to_save(&mut self, save: bool) {
+               // always false
+       }
+
+       fn get_to_render(&self) -> bool {
+               self.field.to_render
+       }
+
+       fn set_to_render(&mut self, render: bool) {
+               self.field.to_render = render;
+       }
+
+       fn get_db_type(&self) -> FieldDbType {
+               self.field.db_type
+       }
+
+       fn set_db_type(&mut self, db_type: FieldDbType) {
+               self.field.db_type = db_type;
+       }
+
+       fn add_filter(&mut self, filter: super::filters::Filter) {
+               self.field.filters.push(filter);
+       }
+
+       fn filter(&mut self) {
+               for f in &self.field.filters {
+                       self.field.value = (f)(&self.field.value);
+               }
+       }
+
+       fn set_template(&mut self, template: &str) {
+               match self.field.tmpl.add_raw_template("field", template) {
+                       Err(e) => { println!("{:?}", e) },
+                       _ => {},
+               };
+       }
+
+       fn render(&self) -> String {
+               let mut s = String::new();
+               if !self.field.to_render {
+                       return s;
+               }
+
+               let mut context = tera::Context::new();
+               context.insert("name", &self.field.name);
+               context.insert("label", &self.field.label);
+               context.insert("disabled", &self.field.disabled);
+               context.insert("invisible", &self.field.invisible);
+               context.insert("help", &self.field.help);
+               context.insert("class", &self.field.class);
+               context.insert("accept", &self.accept);
+               s.push_str(self.field.tmpl.render("field", &context).unwrap().as_str());
+
+               s
+       }
+
+       fn any(&mut self) -> &mut dyn Any {
+               self
+       }
+}
+
+impl FieldFile {
+       pub fn new(name: &str) -> Self {
+               let mut f: FieldFile = Default::default();
+
+               f.field.name = String::from(name);
+               f.field.label = String::from(name);
+               f.field.to_render = true;
+
+               match f.field.tmpl.add_raw_template("field", r#"{% if label %}<div class="mb-3">
+ <label for="{{ name }}" class="form-label">{{ label }}</label>{% endif %}
+ <input type="file" class="form-control{% if help %} is-invalid{% endif %}{% if class %} {{ class }}{% endif %}" name="{{ name }}" id="{{ name }}" {% if disabled %} readonly {% endif %} {% if invisible %}style="display: none;"{% endif %}{% if accept %} accept="{{ accept }}"{% endif %}/>
+ {% if help %}<div id="helpBox_{{ name }}_" class="invalid-feedback">{{ help }}</div>{% endif %}
+ {% if label %}</div>{% endif %}"#) {
+Err(e) => { println!("{:?}", e) },
+_ => {},
+};
+
+               f
+       }
+
+       pub fn get_accept(&self) -> String {
+               String::from(&self.accept)
+       }
+
+       pub fn set_accept(&mut self, accept: &str) {
+               self.accept = String::from(accept);
+       }
+}