rails_admin_json_editor 0.0.10 → 0.0.11

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: ad72da56dc8061a7defaef666a3b38caae3300d7
4
- data.tar.gz: 4b609cd5acecf4031b3c8e6a6bbec2322df8f9f0
3
+ metadata.gz: ab7c7e9e4e142a95e906554500aedb4047a2879a
4
+ data.tar.gz: d0ed47aae309d99de0a47e6f66e00cedf92b6efa
5
5
  SHA512:
6
- metadata.gz: 5ee2f715498d65c1eeb0ebd86ae214e4e2fcc1bf5a8283363f2c2f9110f9f2e1a1814e09760d2f6c1ed56406c7c68ec56b388f3b41d472261b46ee4754f35bbd
7
- data.tar.gz: b3eb519dab6d9af2b28b81b9795ae38dc207072a678611641a10ff47a4f4fa59861e8204b70bd9cfdcf63e1a83bdf0976489630c425fb1ba76f996bfadadfc28
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 = $('[ref=json-editor]').data('json-result');
13
- var jsonComponentTypes = $('[ref=json-editor]').data('json-component-types');
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 dynamic component-type components
17
+ // Setup templates for models
20
18
  var components = {};
21
- _.each(jsonComponentTypes, function(c) {
22
- components['component-type-' + c.type] = {
23
- template: '#template-component-type-' + c.type,
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.props).length === 0 || confirm("Are you sure?")) {
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, type) {
59
+ addComponent: function(event, target, model) {
62
60
  event.preventDefault();
63
61
 
64
- var clonedProps = _.clone(this.component.props);
65
- if(!clonedProps[target]) {
66
- clonedProps[target] = [];
62
+ var clonedproperties = _.clone(this.component.properties);
63
+ if(!clonedproperties[target]) {
64
+ clonedproperties[target] = [];
67
65
  }
68
66
 
69
67
  var obj = {
70
- type: type,
71
- props: {}
68
+ model_name: model.name,
69
+ properties: {}
72
70
  };
73
71
 
74
- clonedProps[target].push(obj);
75
- this.parentComponents[this.parentIndex].props = clonedProps;
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 clonedProps = _.clone(this.component.props);
84
- clonedProps[fieldName] = json;
85
- this.parentComponents[this.parentIndex].props = clonedProps;
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.props &&
90
- this.component.props[fieldName] &&
91
- this.component.props[fieldName][recordLabel] &&
92
- this.component.props[fieldName][recordLabel] === recordName;
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
- nestedComponentTypeIsAllowed: function(componentType, allowedComponentTypes) {
96
- return _.contains(allowedComponentTypes, componentType)
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
- componentTypes: jsonComponentTypes,
105
+ scheme: jsonScheme,
108
106
  showJson: false
109
107
  },
110
108
  methods: {
111
- addComponent: function(e, type) {
109
+ addComponent: function(e, model) {
110
+ console.log('addComponent', model);
112
111
  e.preventDefault();
113
112
 
114
113
  var obj = {
115
- type: type,
116
- props: {}
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-component-types='<%= raw field.components.to_json %>'>
7
+ data-json-scheme='{ "models":<%= raw field.models.to_json %> }'>
8
8
 
9
- <% field.components.each do |component| %>
10
- <script type="text/x-template" id="template-component-type-<%= component.type %>">
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
- <%= component.label %>
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, component.help, class: "help-block" unless component.help.nil? %>
35
+ <%= content_tag :p, model.help, class: "help-block" unless model.help.nil? %>
35
36
 
36
- <% component.fields.each do |f| %>
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.props.<%= f.name %>" type="text" class="form-control" />
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.props.<%= f.name %>" class="form-control"></textarea>
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.props.<%= f.name %>">
65
+ <div v-repeat="nestedComponent: component.properties.<%= f.name %>">
65
66
  <div
66
- v-component="component-type-{{ nestedComponent.type }}"
67
- v-with="component: nestedComponent, parentComponents: component.props.<%= f.name %>, parentIndex: $index">
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 type allowed -->
72
- <% if f.allowed_nested_component_types.count == 1 %>
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="componentType: $root.componentTypes"
76
- v-show="nestedComponentTypeIsAllowed(componentType.type, <%= f.allowed_nested_component_types.map { |nct| nct.to_s } %>)"
77
- v-on="click: addComponent($event, '<%= f.name %>', componentType.type)">
78
- Add {{ componentType.label | lowercase }}
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 types allowed -->
83
- <% if f.allowed_nested_component_types.count > 1 %>
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="componentType: $root.componentTypes">
88
+ <li v-repeat="model: $root.scheme.models">
88
89
  <a href="#"
89
- v-if="nestedComponentTypeIsAllowed(componentType.type, <%= f.allowed_nested_component_types.map { |nct| nct.to_s } %>)"
90
- v-on="click: addComponent($event, '<%= f.name %>', componentType.type)">
91
- {{ componentType.label }}
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="component-type-{{ component.type }}" v-with="component: component, parentComponents: components, parentIndex: $index"></div>
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="componentType: componentTypes">
119
- <a href="#" v-on="click: addComponent($event, componentType.type)">{{ componentType.label }}</a>
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>
@@ -1,3 +1,3 @@
1
1
  module RailsAdminJsonEditor
2
- VERSION = "0.0.10"
2
+ VERSION = "0.0.11"
3
3
  end
@@ -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 :components do
23
- @components
22
+ register_instance_option :models do
23
+ @models
24
24
  end
25
25
 
26
- def setup
27
- @components = []
26
+ def schema
27
+ @models = []
28
28
  yield if block_given?
29
29
  end
30
30
 
31
- def component(type)
32
- component = Component.new(type)
31
+ def model(m)
32
+ model = Model.new(m)
33
33
 
34
- yield(component) if block_given?
34
+ yield(model) if block_given?
35
35
 
36
- @components << component
36
+ @models << model
37
37
  end
38
38
 
39
- class Component
40
- attr_accessor :type, :fields
39
+ class Model
40
+ attr_accessor :name, :fields
41
41
  attr_accessor :label, :help
42
42
 
43
- def initialize(type)
44
- @type = type
43
+ def initialize(model)
44
+ @name = model.name.demodulize
45
45
  @fields = []
46
- @label = type.to_s.humanize
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 :allowed_nested_component_types
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
- if options[:components].nil? && options[:component].nil?
80
- raise "At least one component should be set for JsonEditor::Field with type => :list"
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
- @allowed_nested_component_types = options[:components].nil? ? [options[:component]] : options[:components]
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.10
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-24 00:00:00.000000000 Z
11
+ date: 2015-01-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler