rails_admin_json_editor 0.0.10 → 0.0.11
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ab7c7e9e4e142a95e906554500aedb4047a2879a
|
4
|
+
data.tar.gz: d0ed47aae309d99de0a47e6f66e00cedf92b6efa
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: beeca194fbaa93c46ed3bdc696fac28529c96e9e03de4c231dd04273e3077d29a8e55e11e977564b751bb8b961f2dfa2de4ab5a21a7a043c4257f2c5c45642c1
|
7
|
+
data.tar.gz: 7b2bea1fdefcef7ca6ee5f3f649132a6778facfb6768bce71c66d1a2a36ce70f3c8fd487f391bca08299f2ab29c71248938008cb752ba4c8ce894c951e061813
|
@@ -6,21 +6,19 @@ var vm;
|
|
6
6
|
$(document).on('rails_admin.dom_ready', function() {
|
7
7
|
// TODO: Make this possible for multiple instances
|
8
8
|
|
9
|
-
// Vue.config.debug = true
|
10
|
-
|
11
9
|
// Get data
|
12
|
-
var jsonResult
|
13
|
-
var
|
10
|
+
var jsonResult = $('[ref=json-editor]').data('json-result');
|
11
|
+
var jsonScheme = $('[ref=json-editor]').data('json-scheme');
|
14
12
|
|
15
13
|
if(!jsonResult) {
|
16
14
|
jsonResult = { components: [] };
|
17
15
|
}
|
18
16
|
|
19
|
-
// Setup
|
17
|
+
// Setup templates for models
|
20
18
|
var components = {};
|
21
|
-
_.each(
|
22
|
-
components['
|
23
|
-
template: '#template-
|
19
|
+
_.each(jsonScheme.models, function(model) {
|
20
|
+
components['fields-for-' + model.name] = {
|
21
|
+
template: '#template-fields-for-' + model.name,
|
24
22
|
data: function() {
|
25
23
|
return {
|
26
24
|
expanded: true
|
@@ -32,7 +30,7 @@ $(document).on('rails_admin.dom_ready', function() {
|
|
32
30
|
},
|
33
31
|
moveDownEnabled: function() {
|
34
32
|
return this.parentIndex < this.parentComponents.length - 1;
|
35
|
-
}
|
33
|
+
},
|
36
34
|
},
|
37
35
|
methods: {
|
38
36
|
moveUp: function() {
|
@@ -52,27 +50,27 @@ $(document).on('rails_admin.dom_ready', function() {
|
|
52
50
|
},
|
53
51
|
|
54
52
|
remove: function() {
|
55
|
-
if(_.values(this.component.
|
53
|
+
if(_.values(this.component.properties).length === 0 || confirm("Are you sure?")) {
|
56
54
|
this.parentComponents.$remove(this.parentIndex);
|
57
55
|
}
|
58
56
|
},
|
59
57
|
|
60
58
|
// TODO: DRY up with $root.addComponent
|
61
|
-
addComponent: function(event, target,
|
59
|
+
addComponent: function(event, target, model) {
|
62
60
|
event.preventDefault();
|
63
61
|
|
64
|
-
var
|
65
|
-
if(!
|
66
|
-
|
62
|
+
var clonedproperties = _.clone(this.component.properties);
|
63
|
+
if(!clonedproperties[target]) {
|
64
|
+
clonedproperties[target] = [];
|
67
65
|
}
|
68
66
|
|
69
67
|
var obj = {
|
70
|
-
|
71
|
-
|
68
|
+
model_name: model.name,
|
69
|
+
properties: {}
|
72
70
|
};
|
73
71
|
|
74
|
-
|
75
|
-
this.parentComponents[this.parentIndex].
|
72
|
+
clonedproperties[target].push(obj);
|
73
|
+
this.parentComponents[this.parentIndex].properties = clonedproperties;
|
76
74
|
},
|
77
75
|
|
78
76
|
onChangePicker: function(event, fieldName) {
|
@@ -80,20 +78,20 @@ $(document).on('rails_admin.dom_ready', function() {
|
|
80
78
|
var value = el.options[el.selectedIndex].getAttribute('data-json');
|
81
79
|
var json = JSON.parse(value);
|
82
80
|
|
83
|
-
var
|
84
|
-
|
85
|
-
this.parentComponents[this.parentIndex].
|
81
|
+
var clonedproperties = _.clone(this.component.properties);
|
82
|
+
clonedproperties[fieldName] = json;
|
83
|
+
this.parentComponents[this.parentIndex].properties = clonedproperties;
|
86
84
|
},
|
87
85
|
|
88
86
|
pickerOptionIsSelected: function(fieldName, recordLabel, recordName) {
|
89
|
-
return this.component.
|
90
|
-
this.component.
|
91
|
-
this.component.
|
92
|
-
this.component.
|
87
|
+
return this.component.properties &&
|
88
|
+
this.component.properties[fieldName] &&
|
89
|
+
this.component.properties[fieldName][recordLabel] &&
|
90
|
+
this.component.properties[fieldName][recordLabel] === recordName;
|
93
91
|
},
|
94
92
|
|
95
|
-
|
96
|
-
return _.contains(
|
93
|
+
nestedModelIsAllowed: function(model, allowedModels) {
|
94
|
+
return _.contains(allowedModels, model.name)
|
97
95
|
}
|
98
96
|
}
|
99
97
|
};
|
@@ -104,16 +102,17 @@ $(document).on('rails_admin.dom_ready', function() {
|
|
104
102
|
el: '[ref=json-editor]',
|
105
103
|
data: {
|
106
104
|
components: jsonResult.components,
|
107
|
-
|
105
|
+
scheme: jsonScheme,
|
108
106
|
showJson: false
|
109
107
|
},
|
110
108
|
methods: {
|
111
|
-
addComponent: function(e,
|
109
|
+
addComponent: function(e, model) {
|
110
|
+
console.log('addComponent', model);
|
112
111
|
e.preventDefault();
|
113
112
|
|
114
113
|
var obj = {
|
115
|
-
|
116
|
-
|
114
|
+
model_name: model.name,
|
115
|
+
properties: {}
|
117
116
|
};
|
118
117
|
|
119
118
|
this.components.push(obj);
|
@@ -4,13 +4,14 @@
|
|
4
4
|
<div ref="json-editor"
|
5
5
|
class="json-editor"
|
6
6
|
data-json-result='<%= raw(field.value.blank? ? '{ "components":[] }' : field.value) %>'
|
7
|
-
data-json-
|
7
|
+
data-json-scheme='{ "models":<%= raw field.models.to_json %> }'>
|
8
8
|
|
9
|
-
<% field.
|
10
|
-
<script type="text/x-template" id="template-
|
9
|
+
<% field.models.each do |model| %>
|
10
|
+
<script type="text/x-template" id="template-fields-for-<%= model.name %>">
|
11
11
|
<div class="component">
|
12
12
|
<legend>
|
13
|
-
<%=
|
13
|
+
<%= model.label %>
|
14
|
+
|
14
15
|
<div class="btn-group btn-group-sm pull-right">
|
15
16
|
<button v-on="click: moveUp" type="button" class="btn btn-default {{ moveUpEnabled ? '' : 'disabled' }}">
|
16
17
|
<i class="icon-circle-arrow-up"></i>
|
@@ -31,19 +32,19 @@
|
|
31
32
|
</legend>
|
32
33
|
|
33
34
|
<div v-show="expanded">
|
34
|
-
<%= content_tag :p,
|
35
|
+
<%= content_tag :p, model.help, class: "help-block" unless model.help.nil? %>
|
35
36
|
|
36
|
-
<%
|
37
|
+
<% model.fields.each do |f| %>
|
37
38
|
<div class="control-group row">
|
38
39
|
<label class="col-sm-2 control-label"><%= f.label %></label>
|
39
40
|
|
40
41
|
<div class="controls col-sm-10">
|
41
42
|
<% if f.type == :string %>
|
42
|
-
<input v-model="component.
|
43
|
+
<input v-model="component.properties.<%= f.name %>" type="text" class="form-control" />
|
43
44
|
<% end %>
|
44
45
|
|
45
46
|
<% if f.type == :text %>
|
46
|
-
<textarea v-model="component.
|
47
|
+
<textarea v-model="component.properties.<%= f.name %>" class="form-control"></textarea>
|
47
48
|
<% end %>
|
48
49
|
|
49
50
|
<% if f.type == :picker %>
|
@@ -61,34 +62,34 @@
|
|
61
62
|
<% end %>
|
62
63
|
|
63
64
|
<% if f.type == :list %>
|
64
|
-
<div v-repeat="nestedComponent: component.
|
65
|
+
<div v-repeat="nestedComponent: component.properties.<%= f.name %>">
|
65
66
|
<div
|
66
|
-
v-component="
|
67
|
-
v-with="component: nestedComponent, parentComponents: component.
|
67
|
+
v-component="fields-for-{{ nestedComponent.model_name }}"
|
68
|
+
v-with="component: nestedComponent, parentComponents: component.properties.<%= f.name %>, parentIndex: $index">
|
68
69
|
</div>
|
69
70
|
</div>
|
70
71
|
|
71
|
-
<!-- Add nested component -- one
|
72
|
-
<% if f.
|
72
|
+
<!-- Add nested component -- one model allowed -->
|
73
|
+
<% if f.allowed_nested_models.count == 1 %>
|
73
74
|
<a href="#"
|
74
75
|
class="btn btn-info"
|
75
|
-
v-repeat="
|
76
|
-
v-show="
|
77
|
-
v-on="click: addComponent($event, '<%= f.name %>',
|
78
|
-
Add {{
|
76
|
+
v-repeat="model: $root.scheme.models"
|
77
|
+
v-show="nestedModelIsAllowed(model, <%= f.allowed_nested_models.map { |nct| nct.to_s } %>)"
|
78
|
+
v-on="click: addComponent($event, '<%= f.name %>', model)">
|
79
|
+
Add {{ model.label | lowercase }}
|
79
80
|
</a>
|
80
81
|
<% end %>
|
81
82
|
|
82
|
-
<!-- Add nested component -- multiple
|
83
|
-
<% if f.
|
83
|
+
<!-- Add nested component -- multiple models allowed -->
|
84
|
+
<% if f.allowed_nested_models.count > 1 %>
|
84
85
|
<div class="dropdown">
|
85
86
|
<a class="dropdown-toggle btn btn-info" data-toggle="dropdown" href="#">Add component <b class="caret"></b></a>
|
86
87
|
<ul class="dropdown-menu">
|
87
|
-
<li v-repeat="
|
88
|
+
<li v-repeat="model: $root.scheme.models">
|
88
89
|
<a href="#"
|
89
|
-
v-if="
|
90
|
-
v-on="click: addComponent($event, '<%= f.name %>',
|
91
|
-
{{
|
90
|
+
v-if="nestedModelIsAllowed(model, <%= f.allowed_nested_models.map { |nct| nct.to_s } %>)"
|
91
|
+
v-on="click: addComponent($event, '<%= f.name %>', model)">
|
92
|
+
{{ model.label }}
|
92
93
|
</a>
|
93
94
|
</li>
|
94
95
|
</ul>
|
@@ -107,7 +108,7 @@
|
|
107
108
|
|
108
109
|
<!-- List all components and their fields -->
|
109
110
|
<div v-repeat="component: components">
|
110
|
-
<div v-component="
|
111
|
+
<div v-component="fields-for-{{ component.model_name }}" v-with="component: component, parentComponents: components, parentIndex: $index"></div>
|
111
112
|
</div>
|
112
113
|
|
113
114
|
<!-- Dropdown to add new content -->
|
@@ -115,8 +116,8 @@
|
|
115
116
|
<a class="dropdown-toggle btn btn-info" data-toggle="dropdown" href="#">Add component <b class="caret"></b></a>
|
116
117
|
|
117
118
|
<ul class="dropdown-menu">
|
118
|
-
<li v-repeat="
|
119
|
-
<a href="#" v-on="click: addComponent($event,
|
119
|
+
<li v-repeat="model: scheme.models">
|
120
|
+
<a href="#" v-on="click: addComponent($event, model)">{{ model.label }}</a>
|
120
121
|
</li>
|
121
122
|
</ul>
|
122
123
|
</div>
|
@@ -19,31 +19,31 @@ module RailsAdmin
|
|
19
19
|
bindings[:view].render partial: "rails_admin_json_editor/main/form_json_editor", locals: {field: self, form: bindings[:form]}
|
20
20
|
end
|
21
21
|
|
22
|
-
register_instance_option :
|
23
|
-
@
|
22
|
+
register_instance_option :models do
|
23
|
+
@models
|
24
24
|
end
|
25
25
|
|
26
|
-
def
|
27
|
-
@
|
26
|
+
def schema
|
27
|
+
@models = []
|
28
28
|
yield if block_given?
|
29
29
|
end
|
30
30
|
|
31
|
-
def
|
32
|
-
|
31
|
+
def model(m)
|
32
|
+
model = Model.new(m)
|
33
33
|
|
34
|
-
yield(
|
34
|
+
yield(model) if block_given?
|
35
35
|
|
36
|
-
@
|
36
|
+
@models << model
|
37
37
|
end
|
38
38
|
|
39
|
-
class
|
40
|
-
attr_accessor :
|
39
|
+
class Model
|
40
|
+
attr_accessor :name, :fields
|
41
41
|
attr_accessor :label, :help
|
42
42
|
|
43
|
-
def initialize(
|
44
|
-
@
|
43
|
+
def initialize(model)
|
44
|
+
@name = model.name.demodulize
|
45
45
|
@fields = []
|
46
|
-
@label =
|
46
|
+
@label = @name.humanize
|
47
47
|
end
|
48
48
|
|
49
49
|
def field(name, type, options = {})
|
@@ -68,7 +68,7 @@ module RailsAdmin
|
|
68
68
|
attr_accessor :label, :help
|
69
69
|
attr_accessor :picker_label
|
70
70
|
attr_accessor :picker_records
|
71
|
-
attr_accessor :
|
71
|
+
attr_accessor :allowed_nested_models
|
72
72
|
|
73
73
|
def initialize(name, type, options = {})
|
74
74
|
@name = name
|
@@ -76,11 +76,13 @@ module RailsAdmin
|
|
76
76
|
@label = name.to_s.humanize
|
77
77
|
|
78
78
|
if type == :list
|
79
|
-
|
80
|
-
|
79
|
+
allowed = options[:models].nil? ? [options[:model]] : options[:models]
|
80
|
+
|
81
|
+
if allowed.nil?
|
82
|
+
raise "At least one model should be set for JsonEditor::Field with type => :list"
|
81
83
|
end
|
82
84
|
|
83
|
-
@
|
85
|
+
@allowed_nested_models = allowed.map { |m| m.name.demodulize }
|
84
86
|
end
|
85
87
|
end
|
86
88
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rails_admin_json_editor
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.11
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jasper Haggenburg
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-01-
|
11
|
+
date: 2015-01-25 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|