Skip to content

Commit c58effd

Browse files
committed
Use custom json-schema build
1 parent 965640e commit c58effd

File tree

5 files changed

+52
-34
lines changed

5 files changed

+52
-34
lines changed

elm-package.json

+4-2
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,19 @@
55
"license": "BSD3",
66
"source-directories": [
77
"src",
8+
"../json-schema/src",
89
"../json-form/src"
910
],
1011
"exposed-modules": [ ],
1112
"dependencies": {
1213
"1602/elm-feather": "2.2.0 <= v < 3.0.0",
13-
"1602/json-schema": "4.1.0 <= v < 5.0.0",
1414
"1602/json-value": "3.0.2 <= v < 3.0.3",
15+
"NoRedInk/elm-decode-pipeline": "3.0.0 <= v < 4.0.0",
1516
"elm-lang/core": "5.0.0 <= v < 6.0.0",
1617
"elm-lang/dom": "1.1.1 <= v < 2.0.0",
1718
"elm-lang/html": "2.0.0 <= v < 3.0.0",
18-
"elm-lang/svg": "2.0.0 <= v < 3.0.0"
19+
"elm-lang/svg": "2.0.0 <= v < 3.0.0",
20+
"zwilias/elm-utf-tools": "1.0.1 <= v < 2.0.0"
1921
},
2022
"elm-version": "0.18.0 <= v < 0.19.0"
2123
}

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "json-form-custom-element",
33
"private": false,
4-
"version": "1.3.3",
4+
"version": "1.3.4",
55
"description": "JSON Schema based form generator",
66
"main": "dist/custom-element.js",
77
"repository": "git://github.com/1602/json-form.git",

src/CustomElement.elm

+35-24
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,21 @@ port module CustomElement exposing (init, subscriptions, update, view)
33
--import Html.Attributes exposing (class, classList)
44
--import Html.Events exposing (onClick)
55

6+
import Dict
67
import Html exposing (Html, div, h3, text)
78
import Json.Decode exposing (Value, decodeValue)
89
import Json.Encode as Encode
910
import Json.Form
1011
import Json.Form.Config exposing (Config)
11-
import Json.Schema.Definitions
12-
import Json.Value as JsonValue exposing (decoder)
12+
import Json.Schema.Definitions exposing (Schema)
13+
import Json.Value as JsonValue exposing (JsonValue, decoder)
1314

1415

1516
type alias Model =
1617
{ form : Json.Form.Model
1718
, config : Config
1819
, schema : Json.Schema.Definitions.Schema
19-
, editedValue : Maybe JsonValue.JsonValue
20+
, value : Maybe JsonValue.JsonValue
2021
}
2122

2223

@@ -39,12 +40,30 @@ init v =
3940
|> decodeValue (Json.Decode.field "value" JsonValue.decoder)
4041
|> Result.toMaybe
4142
in
42-
{ form = Json.Form.init config schema value
43-
, editedValue = value
44-
, schema = schema
45-
, config = config
46-
}
47-
! []
43+
initForm schema value config
44+
45+
46+
initForm : Schema -> Maybe JsonValue -> Config -> ( Model, Cmd Msg )
47+
initForm schema value config =
48+
let
49+
form =
50+
value |> Json.Form.init config schema
51+
in
52+
( { form = form
53+
, value = form.value
54+
, schema = schema
55+
, config = config
56+
}
57+
, [ ( "value"
58+
, form.value
59+
|> Maybe.map JsonValue.encode
60+
|> Maybe.withDefault Encode.null
61+
)
62+
, ( "isValid", form.errors |> Dict.isEmpty |> Encode.bool )
63+
]
64+
|> Encode.object
65+
|> valueUpdated
66+
)
4867

4968

5069
type Msg
@@ -76,11 +95,7 @@ update message model =
7695
|> decodeValue Json.Schema.Definitions.decoder
7796
|> Result.withDefault Json.Schema.Definitions.blankSchema
7897
in
79-
{ model
80-
| schema = schema
81-
, form = Json.Form.init model.config schema model.editedValue
82-
}
83-
! []
98+
initForm schema model.value model.config
8499

85100
ChangeValue v ->
86101
let
@@ -89,18 +104,14 @@ update message model =
89104
|> decodeValue JsonValue.decoder
90105
|> Result.toMaybe
91106
in
92-
{ model
93-
| editedValue = value
94-
, form = Json.Form.init model.config model.schema value
95-
}
96-
! []
107+
initForm model.schema value model.config
97108

98109
JsonFormMsg msg ->
99110
let
100111
( ( m, cmd ), exMsg ) =
101112
Json.Form.update msg model.form
102113

103-
( editedValue, exCmd ) =
114+
( value, exCmd ) =
104115
case exMsg of
105116
Json.Form.UpdateValue v isValid ->
106117
( v
@@ -112,15 +123,15 @@ update message model =
112123
)
113124
, ( "isValid", Encode.bool isValid )
114125
]
115-
|> value
126+
|> valueUpdated
116127
)
117128

118129
_ ->
119-
( model.editedValue, Cmd.none )
130+
( model.value, Cmd.none )
120131
in
121132
{ model
122133
| form = m
123-
, editedValue = editedValue
134+
, value = value
124135
}
125136
! [ cmd |> Cmd.map JsonFormMsg, exCmd ]
126137

@@ -135,7 +146,7 @@ view model =
135146
port valueChange : (Value -> msg) -> Sub msg
136147

137148

138-
port value : Value -> Cmd msg
149+
port valueUpdated : Value -> Cmd msg
139150

140151

141152
port schemaChange : (Value -> msg) -> Sub msg

src/custom-element.js

+10-6
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ const css = require('../../json-form/stylesheets/standalone.css').toString();
33

44

55
function readAttribute(node, name, defValue) {
6+
if (!node.hasAttribute(name)) {
7+
return defValue;
8+
}
9+
610
try {
711
return JSON.parse(node.getAttribute(name));
812
} catch (e) {
@@ -31,8 +35,9 @@ customElements.define('json-form',
3135
this._appRoot = appRoot;
3236

3337
this._schema = readAttribute(this, 'schema', {});
34-
this._value = readAttribute(this, 'value', null);
38+
this._value = readAttribute(this, 'value', {});
3539
this._config = readAttribute(this, 'config');
40+
this._muteAttributeChange = false;
3641

3742
}
3843

@@ -43,19 +48,18 @@ customElements.define('json-form',
4348
config: this._config
4449
});
4550
this.app = app;
46-
this.muteAttributeChange = false;
4751

48-
app.ports.value.subscribe(({ value, isValid }) => {
52+
app.ports.valueUpdated.subscribe(({ value, isValid }) => {
4953
const event = new CustomEvent('change', { detail: { value, isValid } });
50-
this.muteAttributeChange = true;
54+
this._muteAttributeChange = true;
5155
this.setAttribute('value', JSON.stringify(value));
52-
this.muteAttributeChange = false;
5356
this.dispatchEvent(event);
5457
});
5558
}
5659

5760
attributeChangedCallback(name, oldValue, newValue) {
58-
if (this.muteAttributeChange) {
61+
if (this._muteAttributeChange) {
62+
this._muteAttributeChange = false;
5963
return;
6064
}
6165

src/index.html

+2-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
},
2020
"type": "boolean",
2121
"title": "enable",
22+
"default": false,
2223
"description": "Enable editing"
2324
},
2425
"form": {
@@ -212,7 +213,7 @@
212213
</style>
213214
</head>
214215
<body>
215-
<json-form></json-form>
216+
<json-form></json-form>
216217
<json-viewer></json-viewer>
217218
</body>
218219
</html>

0 commit comments

Comments
 (0)