コード例 #1
0
ファイル: UiForm.cs プロジェクト: windygu/wastedge-querier
        public static JsValue Show(JavaScriptUi ui, string title, JsValue fields, JsValue validate)
        {
            if (ui == null)
            {
                throw new ArgumentNullException(nameof(ui));
            }

            using (var form = new UiForm())
            {
                if (String.IsNullOrEmpty(title))
                {
                    form.Text = ui.Owner.Text;
                }
                else
                {
                    form.Text = title;
                }

                form.Icon = ui.Owner.Icon;

                if (!fields.IsArray())
                {
                    throw new JavaScriptException("fields must be an array");
                }

                var controls = new List <Field>();

                fields.AsArray().ForEach((index, value) =>
                {
                    var container = form._container;

                    while (container.RowStyles.Count <= index)
                    {
                        container.RowCount++;
                        container.RowStyles.Add(new RowStyle(SizeType.AutoSize));
                    }

                    var field          = value.AsObject();
                    string name        = field.Get("name").ConvertToString();
                    string label       = field.Get("label").ConvertToString();
                    var type           = ParseType(field.Get("type").ConvertToString());
                    ArrayInstance list = null;
                    if (field.HasOwnProperty("list"))
                    {
                        list = field.Get("list").AsArray();
                    }

                    Field control;
                    switch (type)
                    {
                    case FieldType.Text:
                        control = new TextField(name, label);
                        break;

                    case FieldType.CheckBox:
                        control = new CheckBoxField(name, label);
                        break;

                    case FieldType.Numeric:
                        control = new NumericField(name, label);
                        break;

                    case FieldType.Date:
                        control = new DateField(name, label);
                        break;

                    case FieldType.DateTime:
                        control = new DateTimeField(name, label);
                        break;

                    case FieldType.ComboBox:
                        control = new ComboBoxField(name, label, list);
                        break;

                    default:
                        throw new ArgumentOutOfRangeException();
                    }

                    controls.Add(control);

                    if (field.HasOwnProperty("value"))
                    {
                        control.SetValue(field.Get("value"));
                    }

                    if (control.ShowLabel)
                    {
                        var labelControl = new Label
                        {
                            AutoSize  = true,
                            Text      = label,
                            Dock      = DockStyle.Fill,
                            TextAlign = ContentAlignment.MiddleLeft
                        };

                        container.SetRow(labelControl, index);
                        container.Controls.Add(labelControl);
                    }

                    control.Control.Dock     = DockStyle.Fill;
                    control.Control.AutoSize = true;
                    container.SetRow(control.Control, index);
                    container.SetColumn(control.Control, 1);
                    container.Controls.Add(control.Control);
                });

                form._acceptButton.Click += (s, e) =>
                {
                    try
                    {
                        if (validate.IsObject())
                        {
                            var values = BuildValues(ui.Engine, controls);
                            var result = validate.Invoke(values);
                            if (!result.ConvertToBoolean().GetValueOrDefault())
                            {
                                return;
                            }
                        }
                        form.DialogResult = DialogResult.OK;
                    }
                    catch (JavaScriptException exception)
                    {
                        JintDebugger.ExceptionForm.Show(form, exception);
                    }
                    catch (Exception exception)
                    {
                        MessageBox.Show(
                            form,
                            new StringBuilder()
                            .AppendLine("An exception occurred while executing the script:")
                            .AppendLine()
                            .Append(exception.Message).Append(" (").Append(exception.GetType().FullName).AppendLine(")")
                            .AppendLine()
                            .AppendLine(exception.StackTrace)
                            .ToString(),
                            "Error",
                            MessageBoxButtons.OK,
                            MessageBoxIcon.Error
                            );
                    }
                };

                var owner = ui.Owner;

                using (ui.PushOwner(form))
                {
                    if (form.ShowDialog(owner) == DialogResult.OK)
                    {
                        return(BuildValues(ui.Engine, controls));
                    }
                }
            }

            return(JsValue.Null);
        }