Ejemplo n.º 1
0
        public bool Validate(T obj, EditorField <T> editorFieldInfo)
        {
            void SetError(string message)
            {
                _textBox.Styles["border-color"] = "#aa0000";
                _field.SetError(message);
            }

            void ClearError()
            {
                _textBox.Styles["border-color"] = null;
                _field.SetError("");
            }

            object value = _textBox.GetType().GetProperty("Text").GetValue(_textBox);

            var validators = new List <Func <object, (bool, string)> >(editorFieldInfo.Validators);

            if (_type == "email")
            {
                validators.Add(e => (string.IsNullOrEmpty((string)e) || Utils.ValidateEmail(e.ToString()), "Invalid email"));
            }

            if (_type == "url")
            {
                validators.Add(e => (string.IsNullOrEmpty((string)e) || Utils.ValidateUrl(e.ToString()), "Invalid url"));
            }

            (bool, string)? error = validators
                                    .Select(e => e(value))
                                    .Where(e => !e.Item1)
                                    .Select(e => ((bool, string)?)e)
                                    .FirstOrDefault();

            if (error is { Item1: false })
Ejemplo n.º 2
0
        public object Render(T obj, EditorField <T> editorFieldInfo, Action preview)
        {
            object value = editorFieldInfo.GetValue(obj);

            var options = GetOptions(editorFieldInfo);

            var selectedOption = options.Where(e => Equals(e.Value, value)).Select(e => e.Label).FirstOrDefault();

            _selectBox = new Controls.SelectBox(options.Select(e => e.Label).ToArray())
            {
                SelectedOption = selectedOption
            };

            _selectBox.SelectionChanged += (sender, args) => preview();

            _selectBox.Enabled = editorFieldInfo.Enabled;

            return(_field = new Field(
                       editorFieldInfo.Label,
                       _selectBox,
                       editorFieldInfo.Description,
                       editorFieldInfo.Helper,
                       editorFieldInfo.Required
                       ));
        }
Ejemplo n.º 3
0
        public object Render(T obj, EditorField <T> editorFieldInfo, Action updated)
        {
            var value = Convert.ToString(editorFieldInfo.GetValue(obj));

            if (_multiLine)
            {
                _textBox = new Controls.TextArea(value);
                _textBox.Styles["width"] = "-webkit-fill-available";
                ((Controls.TextArea)_textBox).TextInput += (sender, args) => updated();
            }
            else
            {
                _textBox = new Controls.TextBox(value);
                ((Controls.TextBox)_textBox).TextInput += (sender, args) => updated();
            }

            if (_fixedFont)
            {
                _textBox.HtmlElement.SetAttribute("class", "fixed-font");
            }

            _textBox.Enabled = editorFieldInfo.Enabled;

            return(_field = new Field(
                       editorFieldInfo.Label,
                       _textBox,
                       editorFieldInfo.Description,
                       editorFieldInfo.Helper,
                       editorFieldInfo.Required
                       ));
        }
Ejemplo n.º 4
0
        public bool Validate(T obj, EditorField <T> editorFieldInfo)
        {
            void SetError(string message)
            {
                _numberBox.Styles["border-color"] = "#aa0000";
                _field.SetError(message);
            }

            void ClearError()
            {
                _numberBox.Styles["border-color"] = null;
                _field.SetError("");
            }

            double val = 0;

            if (!editorFieldInfo.Type.IsNullable() && string.IsNullOrEmpty(_numberBox.Text) ||
                !string.IsNullOrEmpty(_numberBox.Text) && !double.TryParse(_numberBox.Text, out val))
            {
                SetError("Invalid number");
                return(false);
            }

            (bool, string)? error = editorFieldInfo.Validators
                                    .Select(e => e(Convert.ChangeType(val, editorFieldInfo.Type)))
                                    .Where(e => e.Item1)
                                    .Select(e => ((bool, string)?)e)
                                    .FirstOrDefault();

            if (error is { Item1: false })
Ejemplo n.º 5
0
        public bool Validate(T obj, EditorField <T> editorField)
        {
            void SetError(string message)
            {
                _dateBox.Styles["border-color"] = "#aa0000";
                _field.SetError(message);
            }

            void ClearError()
            {
                _dateBox.Styles["border-color"] = null;
                _field.SetError("");
            }

            DateTime val = DateTime.MinValue;

            if (!editorField.Type.IsNullable() && string.IsNullOrEmpty(_dateBox.Text) ||
                !string.IsNullOrEmpty(_dateBox.Text) && !DateTime.TryParseExact(_dateBox.Text, new[] { "yyyy-MM-dd" }, null, DateTimeStyles.None, out val))
            {
                SetError("Invalid date");
                return(false);
            }

            (bool, string)? error = editorField.Validators
                                    .Select(e => e(val))
                                    .Where(e => e.Item1)
                                    .Select(e => ((bool, string)?)e)
                                    .FirstOrDefault();

            if (error is { Item1: false })
Ejemplo n.º 6
0
        public bool Validate(T obj, EditorField <T> editorFieldInfo)
        {
            void SetError(string message)
            {
                //_imageProxyBox.TextBox.Styles["border-color"] = "#aa0000";
                _field.SetError(message);
            }

            void ClearError()
            {
                //_imageProxyBox.TextBox.Styles["border-color"] = null;
                _field.SetError("");
            }

            if (_imageProxyBox.IsUploading)
            {
                return(false);
            }

            object value = _imageProxyBox.GetType().GetProperty("Text").GetValue(_imageProxyBox);

            (bool, string)? error = editorFieldInfo.Validators
                                    .Select(e => e(value))
                                    .Where(e => !e.Item1)
                                    .Select(e => ((bool, string)?)e)
                                    .FirstOrDefault();

            if (error is { Item1: false })
Ejemplo n.º 7
0
        public object Render(T obj, EditorField <T> editorField, Action preview)
        {
            object   _value = editorField.GetValue(obj);
            DateTime?value;

            if (_value == null)
            {
                value = null;
            }
            else if (_value is DateTimeOffset dto)
            {
                value = dto.DateTime;
            }
            else if (_value is DateTime dt)
            {
                value = dt;
            }
            else
            {
                throw new ArgumentException("Not a DateTime or DateTimeOffset.");
            }

            _dateBox = new DateBox(value)
            {
            };
            //_dateBox.HtmlElement.SetAttribute("placeholder", "YYYY-MM-DD");

            _dateBox.TextInput += (sender, args) => preview();

            _dateBox.Enabled = editorField.Enabled;

            return(_field = new Field(editorField.Label, _dateBox, editorField.Description, editorField.Helper, editorField.Required));
        }
Ejemplo n.º 8
0
        public void Save(T obj, EditorField <T> editorFieldInfo)
        {
            var col = _selectBox.SelectedOptions.Select(e => ((Option)e).Value).ToList();
            var mi  = typeof(Utils).GetMethod(nameof(Utils.ConvertFactory));

            editorFieldInfo.SetValue(obj, mi.MakeGenericMethod(_elementType).Invoke(null, new object[] { col }));
        }
Ejemplo n.º 9
0
        public object Render(T obj, EditorField <T> editorFieldInfo, Action preview)
        {
            var container = new Div(_dumpContainer);

            container.SetClass("entity-editor-literal");
            _dumpContainer.Content = _contentFormatter.Format(editorFieldInfo.GetValue(obj), emptyContent: "-");
            return(_field = new Field(editorFieldInfo.Label, container, editorFieldInfo.Description, editorFieldInfo.Helper));
        }
Ejemplo n.º 10
0
        public void Save(T obj, EditorField <T> editorFieldInfo)
        {
            string value = (string)_textBox.GetType().GetProperty("Text").GetValue(_textBox);

            if (_trim)
            {
                value = value.Trim();
            }

            editorFieldInfo.SetValue(obj, value);
        }
Ejemplo n.º 11
0
        private Option[] GetOptions(EditorField <T> editorFieldInfo)
        {
            var isNullable = editorFieldInfo.Type.IsNullable();

            if (isNullable)
            {
                return((new[] { new Option("", null) }).Concat(_options).ToArray());
            }
            else
            {
                return(_options);
            }
        }
Ejemplo n.º 12
0
        public object Render(T obj, EditorField <T> editorFieldInfo, Action updated)
        {
            var value = Convert.ToString(editorFieldInfo.GetValue(obj));

            _codeBox            = new Controls.CodeEditor(value, _language);
            _codeBox.TextInput += (sender, args) => updated();

            return(_field = new Field(
                       editorFieldInfo.Label,
                       _codeBox,
                       editorFieldInfo.Description,
                       editorFieldInfo.Helper,
                       editorFieldInfo.Required
                       ));
        }
Ejemplo n.º 13
0
        public object Render(T obj, EditorField <T> editorFieldInfo, Action preview)
        {
            object value        = editorFieldInfo.GetValue(obj);
            double?initialValue = value == null ? (double?)null : Convert.ToDouble(value);

            _numberBox = new NumberBox(initialValue, _decimals)
            {
            };

            _numberBox.TextInput += (sender, args) => preview();

            _numberBox.Enabled = editorFieldInfo.Enabled;

            return(_field = new Field(editorFieldInfo.Label, _numberBox, editorFieldInfo.Description, editorFieldInfo.Helper, editorFieldInfo.Required));
        }
Ejemplo n.º 14
0
        public void Save(T obj, EditorField <T> editorField)
        {
            var type = editorField.Type;

            if ((Nullable.GetUnderlyingType(editorField.Type) is { } ut))
            {
                if (string.IsNullOrEmpty(_dateBox.Text))
                {
                    editorField.SetValue(obj, null);
                    return;
                }
                type = ut;

                DateTime?selectedDate = _dateBox.SelectedDate;
                if (selectedDate == null)
                {
                    editorField.SetValue(obj, null);
                    return;
                }

                if (type == typeof(DateTimeOffset))
                {
                    editorField.SetValue(obj, new DateTimeOffset(selectedDate !.Value));
                }
                else
                {
                    editorField.SetValue(obj, selectedDate !.Value);
                }
                return;
            }
            try
            {
                DateTime?selectedDate = _dateBox.SelectedDate;

                if (type == typeof(DateTimeOffset))
                {
                    editorField.SetValue(obj, new DateTimeOffset(selectedDate !.Value));
                }
                else
                {
                    editorField.SetValue(obj, selectedDate !.Value);
                }
            }
            catch (Exception)
            {
                //ignore
            }
        }
Ejemplo n.º 15
0
        public object Render(T obj, EditorField <T> editorField, Action preview)
        {
            object value = editorField.GetValue(obj);

            var labelText = value?.ToString() ?? "";

            _dataListBox = new DataListBox(_options)
            {
                Text = labelText
            };

            _dataListBox.TextInput += (sender, args) => preview();

            _dataListBox.Enabled = editorField.Enabled;

            return(_field = new Field(editorField.Label, _dataListBox, editorField.Description, editorField.Helper, editorField.Required));
        }
Ejemplo n.º 16
0
        public object Render(T obj, EditorField <T> editorFieldInfo, Action preview)
        {
            object value = editorFieldInfo.GetValue(obj);

            _selectBox = new AsyncDataListBox(_queryOptions, _findOption)
            {
            };

            if (value != null)
            {
                _selectBox.SetValueAsync(value).GetAwaiter().GetResult();
            }

            _selectBox.Updated += (_) => preview();

            return(_field = new Field(editorFieldInfo.Label, _selectBox, editorFieldInfo.Description, editorFieldInfo.Helper, editorFieldInfo.Required));
        }
Ejemplo n.º 17
0
        public object Render(T obj, EditorField <T> editorField, Action preview)
        {
            var value = Convert.ToBoolean(editorField.GetValue(obj));

            _checkBox = new CheckBox(editorField.Label, value);

            _checkBox.Click += (sender, args) => preview();

            var _container = new Div(_checkBox);

            _container.SetClass("entity-editor-bool");

            _wrapper = new DumpContainer(_container);

            _checkBox.Enabled = editorField.Enabled;

            return(_wrapper);
        }
Ejemplo n.º 18
0
        public object Render(T obj, EditorField <T> editorFieldInfo, Action preview)
        {
            object value = editorFieldInfo.GetValue(obj);

            _elementType = editorFieldInfo.Type.GenericTypeArguments.Single();

            int[] _selectedIndexes = null;

            if (value is IEnumerable selectedItems)
            {
                _selectedIndexes = selectedItems.Cast <object>().Select(e => Array.IndexOf(_options.Select(f => f.Value).ToArray(), e)).Where(e => e != -1).ToArray();
            }

            _selectBox = new MultiSelectBox(_options, _selectedIndexes);
            _selectBox.SelectionChanged += (sender, args) => preview();

            _selectBox.Enabled = editorFieldInfo.Enabled;

            return(_field = new Field(editorFieldInfo.Label, _selectBox, editorFieldInfo.Description, editorFieldInfo.Helper, editorFieldInfo.Required));
        }
Ejemplo n.º 19
0
        public object Render(T obj, EditorField <T> editorFieldInfo, Action updated)
        {
            var value = Convert.ToString(editorFieldInfo.GetValue(obj));

            _fileBox            = new FileBox(value, _initialCatalog);
            _fileBox.TextInput += (sender, args) => updated();

            if (editorFieldInfo.Required)
            {
                _fileBox.TextBox.HtmlElement.SetAttribute("required", "required");
            }

            return(_field = new Field(
                       editorFieldInfo.Label,
                       _fileBox,
                       editorFieldInfo.Description,
                       editorFieldInfo.Helper,
                       editorFieldInfo.Required
                       ));
        }
Ejemplo n.º 20
0
        public void Save(T obj, EditorField <T> editorFieldInfo)
        {
            var type = editorFieldInfo.Type;

            if (Nullable.GetUnderlyingType(editorFieldInfo.Type) is Type t)
            {
                if (string.IsNullOrEmpty(_numberBox.Text))
                {
                    editorFieldInfo.SetValue(obj, null);
                    return;
                }
                type = t;
            }
            try
            {
                editorFieldInfo.SetValue(obj, Convert.ChangeType(double.Parse(_numberBox.Text), type));
            }
            catch (Exception)
            {
                editorFieldInfo.SetValue(obj, Convert.ChangeType(0, type));
            }
        }
Ejemplo n.º 21
0
        public bool Validate(T obj, EditorField <T> editorFieldInfo)
        {
            void SetError(string message)
            {
                _selectBox.Styles["border-color"] = "#aa0000";
                _field.SetError(message);
            }

            void ClearError()
            {
                _selectBox.Styles["border-color"] = null;
                _field.SetError("");
            }

            var option = _selectBox.SelectedOption;

            (bool, string)? error = editorFieldInfo.Validators
                                    .Select(e => e(option?.Value))
                                    .Where(e => !e.Item1)
                                    .Select(e => ((bool, string)?)e)
                                    .FirstOrDefault();

            if (error is { Item1: false })
Ejemplo n.º 22
0
        public object Render(T obj, EditorField <T> editorFieldInfo, Action preview)
        {
            var label = Convert.ToString(editorFieldInfo.GetValue(obj));

            Control link;

            if (_fetchUrl != null)
            {
                var url = _fetchUrl(obj);
                link = new Controls.Hyperlink(label, url);
            }
            else if (_onClick != null)
            {
                link = new Controls.Hyperlink(label, (_) => _onClick(obj));
            }
            else
            {
                link = new Controls.Hyperlink(label, label);
            }

            link.HtmlElement.SetAttribute("class", "entity-editor-link");

            return(new Field(editorFieldInfo.Label, link, editorFieldInfo.Description, editorFieldInfo.Helper));
        }
Ejemplo n.º 23
0
 public object Render(T obj, EditorField <T> editorFieldInfo, Action preview)
 {
     return(null);
 }
Ejemplo n.º 24
0
 public bool Validate(T obj, EditorField <T> instruction)
 {
     return(true);
 }
Ejemplo n.º 25
0
 public void Save(T obj, EditorField <T> editorFieldInfo)
 {
     //ignore
 }
Ejemplo n.º 26
0
 public void Save(T obj, EditorField <T> editorFieldInfo)
 {
     editorFieldInfo.SetValue(obj, _selectBox.SelectedOption?.Value);
 }
Ejemplo n.º 27
0
 public bool Validate(T obj, EditorField <T> editorFieldInfo)
 {
     return(true);
 }
Ejemplo n.º 28
0
        public void Save(T obj, EditorField <T> editorFieldInfo)
        {
            string value = (string)_codeBox.GetType().GetProperty("Text").GetValue(_codeBox);

            editorFieldInfo.SetValue(obj, value);
        }
Ejemplo n.º 29
0
        public void Save(T obj, EditorField <T> editorFieldInfo)
        {
            var options = GetOptions(editorFieldInfo);

            editorFieldInfo.SetValue(obj, options[_selectBox.SelectedIndex].Value);
        }
Ejemplo n.º 30
0
 public void Save(T obj, EditorField <T> editorField)
 {
     editorField.SetValue(obj, _checkBox.Checked);
 }