rails_admin_json_editor 0.0.6 → 0.0.7

Sign up to get free protection for your applications and to get access to all the features.
@@ -1 +1,80 @@
1
1
  //= require rails_admin_json_editor/vue.0.11.4
2
+ //= require rails_admin_json_editor/lodash.2.4.1
3
+
4
+ $(document).on('rails_admin.dom_ready', function() {
5
+ // TODO: Make this possible for multiple instances
6
+
7
+ // Get data
8
+ var data = $('[ref=json-editor]').data('json');
9
+ if(!data) {
10
+ data = { components: [] };
11
+ }
12
+
13
+ // Re-initialize temporary vars
14
+ data.tmp = { showJson: false };
15
+
16
+ // Re-initialMake sure all components have their (possibly new) defaults
17
+ var componentTmpDefaults = { expanded: true };
18
+ data.components = _.map(data.components, function(component) {
19
+ component.tmp = _.clone(componentTmpDefaults);
20
+ return component;
21
+ });
22
+
23
+ // Make sure we let Vue update the json-field, but do it here to prevent data-loss when js is disabled
24
+ $('[ref=json-editor] [v-model^="$root.$data"]').val('');
25
+
26
+ // Let's go
27
+ var componentsVM = new Vue({
28
+ el: '[ref=json-editor]',
29
+ data: data,
30
+ methods: {
31
+ addComponent: function(e) {
32
+ e.preventDefault();
33
+ var type = e.target.getAttribute('component-type');
34
+ this.components.push({
35
+ type: type,
36
+ props: {},
37
+ tmp: _.clone(componentTmpDefaults)
38
+ })
39
+ },
40
+ removeComponent: function(index) {
41
+ if(confirm("Are you sure?")) {
42
+ this.components.$remove(index);
43
+ }
44
+ },
45
+ moveComponentUp: function(index) {
46
+ var from = index;
47
+ var to = index - 1;
48
+ var element = this.components[from];
49
+ this.components.splice(from, 1);
50
+ this.components.splice(to, 0, element);
51
+ },
52
+ moveComponentDown: function(index) {
53
+ var from = index;
54
+ var to = index + 1;
55
+ var element = this.components[from];
56
+ this.components.splice(from, 1);
57
+ this.components.splice(to, 0, element);
58
+ },
59
+ onChangePicker: function(e, index, fieldName) {
60
+ var el = e.target;
61
+ var value = el.options[el.selectedIndex].getAttribute('data-json');
62
+ var json = JSON.parse(value);
63
+
64
+ var props = {};
65
+ props[fieldName] = json;
66
+
67
+ var data = $.extend({}, componentsVM.components[index], {
68
+ props: props
69
+ });
70
+
71
+ componentsVM.components.$set(index, data)
72
+ },
73
+ pickerOptionIsSelected: function(component, fieldName, recordLabel, recordName) {
74
+ return component.props[fieldName]
75
+ && component.props[fieldName][recordLabel]
76
+ && component.props[fieldName][recordLabel] == recordName;
77
+ }
78
+ }
79
+ });
80
+ });
@@ -0,0 +1,10 @@
1
+ body.rails_admin .form-horizontal.denser .json-editor {
2
+ .component {
3
+ padding-bottom: 15px;
4
+ margin-bottom: 15px;
5
+ }
6
+
7
+ .form-control {
8
+ width: 90%;
9
+ }
10
+ }
@@ -1,53 +1,73 @@
1
- <script type="text/javascript">
2
- var recordPickers = {};
3
- </script>
1
+ <%= javascript_include_tag "rails_admin_json_editor/rails_admin_json_editor" %>
2
+ <%= stylesheet_link_tag "rails_admin_json_editor/rails_admin_json_editor", media: 'all' %>
4
3
 
5
- <div ref="components_json" class="components_json">
4
+ <div ref="json-editor"
5
+ class="json-editor"
6
+ data-json='<%= raw(field.value.blank? ? '{ "components":[] }' : field.value) %>'>
6
7
 
7
8
  <!-- List content -->
8
- <div v-repeat="components" class="component">
9
+ <div v-repeat="component: components" class="component">
9
10
  <% field.components.each do |component| %>
10
- <div v-if="type == '<%= component.type %>'">
11
- <legend><%= component.label %></legend>
12
- <%= content_tag :p, component.help, class: "help-block" unless component.help.nil? %>
11
+ <div v-if="component.type == '<%= component.type %>'">
12
+ <legend>
13
+ <%= component.label %>
14
+ <div class="btn-group btn-group-sm pull-right">
15
+ <button v-on="click: moveComponentUp($index)" type="button" class="btn btn-default {{ $index == 0 ? 'disabled' : '' }}">
16
+ <i class="icon-circle-arrow-up"></i>
17
+ </button>
18
+ <button v-on="click: moveComponentDown($index)" type="button" class="btn btn-default {{ $index == components.length - 1 ? 'disabled' : '' }}">
19
+ <i class="icon-circle-arrow-down"></i>
20
+ </button>
21
+ <button v-on="click: component.tmp.expanded = !component.tmp.expanded" type="button" class="btn btn-default">
22
+ <i class="{{ component.tmp.expanded ? 'icon-resize-small' : 'icon-resize-full' }}"></i>
23
+ </button>
24
+ <button v-on="click:removeComponent($index)" type="button" class="btn btn-default">
25
+ <i class="icon-remove"></i>
26
+ </button>
27
+ </div>
28
+ </legend>
29
+
30
+ <div v-show="component.tmp.expanded">
31
+ <%= content_tag :p, component.help, class: "help-block" unless component.help.nil? %>
13
32
 
14
- <% component.fields.each do |f| %>
15
- <div class="control-group">
16
- <%= label_tag f.name, f.label, class: "control-label" %>
33
+ <% component.fields.each do |f| %>
34
+ <div class="control-group row">
35
+ <%= label_tag f.name, f.label, class: "col-sm-2 control-label" %>
17
36
 
18
- <div class="controls">
19
- <% if f.type == :string %>
20
- <%= text_field_tag f.name, nil, 'v-model' => "props.#{f.name}" %>
21
- <% end %>
37
+ <div class="controls col-sm-10">
38
+ <% if f.type == :string %>
39
+ <%= text_field_tag f.name, nil, class: 'form-control', 'v-model' => "component.props.#{f.name}" %>
40
+ <% end %>
22
41
 
23
- <% if f.type == :text %>
24
- <%= text_area_tag f.name, nil, 'v-model' => "props.#{f.name}" %>
25
- <% end %>
42
+ <% if f.type == :text %>
43
+ <%= text_area_tag f.name, nil, class: 'form-control', 'v-model' => "component.props.#{f.name}" %>
44
+ <% end %>
26
45
 
27
- <% if f.type == :picker %>
28
- <select v-on="change: onChangePicker($event, $index, '<%= f.name %>')" placeholder="Zoeken">
46
+ <% if f.type == :picker %>
47
+ <select v-on="change: onChangePicker($event, $index, '<%= f.name %>')" class="form-control record-picker">
29
48
  <option value=""></option>
30
49
  <% f.picker_records.each do |record| %>
31
50
  <option
32
- v-attr="selected: pickerOptionIsSelected(this, '<%= f.name %>', '<%= f.picker_label %>', '<%= record.send(f.picker_label) %>')"
51
+ v-attr="selected: pickerOptionIsSelected(component, '<%= f.name %>', '<%= f.picker_label %>', '<%= record.send(f.picker_label) %>')"
33
52
  value="<%= record.send(f.picker_label) %>"
34
53
  data-json="<%= record.to_json %>">
35
54
  <%= record.send(f.picker_label) %>
36
55
  </option>
37
56
  <% end %>
38
57
  </select>
39
- <% end %>
58
+ <% end %>
40
59
 
41
- <%= content_tag :p, f.help, class: "help-block" unless f.help.nil? %>
60
+ <%= content_tag :p, f.help, class: "help-block" unless f.help.nil? %>
61
+ </div>
42
62
  </div>
43
- </div>
44
- <% end %>
63
+ <% end %>
64
+ </div>
45
65
  </div>
46
66
  <% end %>
47
67
  </div>
48
68
 
49
69
  <!-- Dropdown to add new content -->
50
- <div class="dropdown">
70
+ <div class="dropdown pull-left">
51
71
  <a class="dropdown-toggle btn btn-info" data-toggle="dropdown" href="#">Component toevoegen <b class="caret"></b></a>
52
72
 
53
73
  <ul class="dropdown-menu">
@@ -58,42 +78,15 @@
58
78
  </div>
59
79
 
60
80
  <!-- Hidden field to store JSON -->
61
- <%= form.text_area field.name, 'v-model' => 'components | json', cols:50, rows:10, style: 'margin-top:40px' %>
62
- </div>
63
-
64
- <script type="text/javascript">
65
- var componentsVM = new Vue({
66
- el: '[ref=components_json]',
67
- data: {
68
- components: <%= raw(field.value.empty? ? "[]" : field.value) %>
69
- },
70
- methods: {
71
- addComponent: function(e) {
72
- e.preventDefault();
73
- var type = e.target.getAttribute('component-type');
74
- this.components.push({ type:type, props:{} })
75
- },
76
- onChangePicker: function(e, index, fieldName) {
77
- var el = e.target;
78
- var value = el.options[el.selectedIndex].getAttribute('data-json');
79
- var json = JSON.parse(value);
80
-
81
- var props = {};
82
- props[fieldName] = json;
83
-
84
- var data = $.extend({}, componentsVM.components[index], {
85
- props: props
86
- });
81
+ <div>
82
+ <% if Rails.env.development? %>
83
+ <button v-on="click: tmp.showJson = !tmp.showJson" type="button" class="btn btn-default pull-right">
84
+ <i class="icon-list-alt"></i>
85
+ </button>
86
+ <% end %>
87
87
 
88
- componentsVM.components.$set(index, data)
89
- },
90
- pickerOptionIsSelected: function(component, fieldName, recordLabel, recordName) {
91
- return component.$data
92
- && component.$data.props
93
- && component.$data.props[fieldName]
94
- && component.$data.props[fieldName][recordLabel] == recordName
95
- && component.$data.props[fieldName][recordLabel] == recordName;
96
- }
97
- }
98
- });
99
- </script>
88
+ <div v-show="tmp.showJson">
89
+ <%= form.text_area field.name, value: field.value, 'v-model' => '$root.$data | json', style: 'width:100%; height:400px; margin-top:20px' %>
90
+ </div>
91
+ </div>
92
+ </div>
@@ -0,0 +1 @@
1
+ Rails.application.config.assets.precompile += ["rails_admin_json_editor/rails_admin_json_editor.js", "rails_admin_json_editor/rails_admin_json_editor.css"]
@@ -1,3 +1,3 @@
1
1
  module RailsAdminJsonEditor
2
- VERSION = "0.0.6"
2
+ VERSION = "0.0.7"
3
3
  end
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.6
4
+ version: 0.0.7
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-21 00:00:00.000000000 Z
11
+ date: 2015-01-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -65,9 +65,12 @@ files:
65
65
  - LICENSE.txt
66
66
  - README.md
67
67
  - Rakefile
68
+ - app/assets/javascripts/rails_admin_json_editor/lodash.2.4.1.js
68
69
  - app/assets/javascripts/rails_admin_json_editor/rails_admin_json_editor.js
69
70
  - app/assets/javascripts/rails_admin_json_editor/vue.0.11.4.js
71
+ - app/assets/stylesheets/rails_admin_json_editor/rails_admin_json_editor.css.scss
70
72
  - app/views/rails_admin_json_editor/main/_form_json_editor.html.erb
73
+ - config/initializers/assets.rb
71
74
  - lib/rails_admin_json_editor.rb
72
75
  - lib/rails_admin_json_editor/version.rb
73
76
  - rails_admin_json_editor.gemspec