inplace_editing 0.3.3 → 0.3.5
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 +4 -4
- data/README.md +2 -1
- data/app/helpers/inplace_editing_helper.rb +90 -0
- data/app/views/inplace_editing/_image_bg.slim +2 -0
- data/app/views/inplace_editing/edit/_image_bg.html.slim +13 -0
- data/app/views/inplace_editing/read/_image_bg.slim +3 -0
- data/lib/assets/javascripts/inplace_editing.js +129 -5
- data/lib/assets/stylesheets/inplace_editing.scss +11 -0
- data/lib/inplace_editing/version.rb +1 -1
- metadata +4 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c748aeb6c93204e5c049e4eb388607325196857d
|
4
|
+
data.tar.gz: 95c28c31fed009c51440eb682cf31c6d039057bf
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3d3ea3bd17e6dce813522554e5af064d08acd6faf9cf10b0034f261db0c250ee1cdb3ed3b0e9d5f30993fb66d3a90b0f97b1a2f25524a9771eebb88e3cebe814
|
7
|
+
data.tar.gz: 8da550a53c5aa23fc6f30639155287240527436206bdcbb6e45f0cbe55e66126b82d61d064362c1410be0e569d7a13f7f9bfd26212ef4bad8957530132831a81
|
data/README.md
CHANGED
@@ -37,6 +37,21 @@ module InplaceEditingHelper
|
|
37
37
|
{ localized_object: localized_object, property: property, place_holder: place_holder, default_value: default_value, label: label }
|
38
38
|
end
|
39
39
|
|
40
|
+
def image_bg_editor(localized_object, property, container_tag, attrs)
|
41
|
+
if localized_object[property]
|
42
|
+
image = localized_object.public_send(property)
|
43
|
+
image = image.url if image.respond_to?(:url)
|
44
|
+
attrs[:style] = 'background-image:url("' + image + '");' + (attrs[:style] ? attrs[:style] : '')
|
45
|
+
end
|
46
|
+
|
47
|
+
locals = editor_locals(localized_object, property)
|
48
|
+
locals[:tag] = container_tag
|
49
|
+
locals[:attrs] = attrs
|
50
|
+
render layout: 'inplace_editing/image_bg', :locals => locals do |view_locals|
|
51
|
+
yield view_locals
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
40
55
|
def markdown(text, placeholder = '[texto aqui]')
|
41
56
|
return placeholder if !text
|
42
57
|
options = {
|
@@ -59,6 +74,81 @@ module InplaceEditingHelper
|
|
59
74
|
|
60
75
|
markdown.render(text).html_safe
|
61
76
|
end
|
77
|
+
|
78
|
+
def content_tag(*args)
|
79
|
+
if block_given?
|
80
|
+
tag = Tag.new(args[0], args[1] || {})
|
81
|
+
old_buf = @output_buffer
|
82
|
+
@output_buffer = ActionView::OutputBuffer.new
|
83
|
+
value = yield(tag)
|
84
|
+
content = tag.render(@output_buffer.presence || value)
|
85
|
+
@output_buffer = old_buf
|
86
|
+
content
|
87
|
+
else
|
88
|
+
super
|
89
|
+
end
|
90
|
+
end
|
91
|
+
|
92
|
+
class Tag
|
93
|
+
include ActionView::Helpers::CaptureHelper
|
94
|
+
attr_accessor :id
|
95
|
+
attr_reader :name, :css
|
96
|
+
|
97
|
+
def initialize(name, attributes = {})
|
98
|
+
@name = name
|
99
|
+
@attributes = attributes.with_indifferent_access
|
100
|
+
@attributes[:class] = Tag::CSS.new(attributes[:class])
|
101
|
+
end
|
102
|
+
|
103
|
+
def []=(k,v)
|
104
|
+
@attributes[k] = v
|
105
|
+
end
|
106
|
+
|
107
|
+
def [](k)
|
108
|
+
@attributes[k]
|
109
|
+
end
|
110
|
+
|
111
|
+
def render(content)
|
112
|
+
"<#{name}#{render_attributes}>#{content.strip}</#{name}>".html_safe
|
113
|
+
end
|
114
|
+
|
115
|
+
def render_attributes
|
116
|
+
attrs = @attributes.dup
|
117
|
+
if self[:class].empty?
|
118
|
+
attrs.delete :class
|
119
|
+
else
|
120
|
+
attrs[:class] = self[:class].to_s
|
121
|
+
end
|
122
|
+
|
123
|
+
attrs.keys.map do |k|
|
124
|
+
"#{k}='#{ERB::Util.html_escape(attrs[k])}'".html_safe
|
125
|
+
end.join(' ').prepend(' ')
|
126
|
+
end
|
127
|
+
|
128
|
+
class CSS
|
129
|
+
|
130
|
+
def initialize(css)
|
131
|
+
if css.is_a? String
|
132
|
+
@internals = css.split(' ')
|
133
|
+
else
|
134
|
+
@internals = css.to_a
|
135
|
+
end
|
136
|
+
end
|
137
|
+
|
138
|
+
def to_s
|
139
|
+
@internals.uniq.join(' ')
|
140
|
+
end
|
141
|
+
|
142
|
+
def empty?
|
143
|
+
@internals.empty?
|
144
|
+
end
|
145
|
+
|
146
|
+
def <<(name)
|
147
|
+
@internals << name
|
148
|
+
nil
|
149
|
+
end
|
150
|
+
end
|
151
|
+
end
|
62
152
|
end
|
63
153
|
|
64
154
|
# @inplace_editing_mode = tem que ser setado no after_action usando o @can_edit
|
@@ -0,0 +1,13 @@
|
|
1
|
+
- image_src = localized_object[property] ? localized_object.send(property.to_s + '_url') : local_assigns[:default_value]
|
2
|
+
= content_tag tag do |el|
|
3
|
+
- attrs.each {|key_name, value| el[key_name.to_sym] = value }
|
4
|
+
- el[:class] = 'inplace_editor inplace_editor_image_bg ' + (el[:class].present? ? el[:class] : '')
|
5
|
+
= simple_form_for(localized_object, html: { :multipart => true, novalidate: true }, :format => :json, :remote => true, authenticity_token: true) do |f|
|
6
|
+
= f.input_field property, as: :file, label: "Trocar Imagem", style: 'display:none'
|
7
|
+
= f.input property.to_s + "_cache", as: :hidden
|
8
|
+
.editor-container
|
9
|
+
a class="#{image_src.blank? ? 'insert-image' : 'edit-image'}" #{image_src.blank? ? 'Clique para inserir imagem' : ''}
|
10
|
+
.actions-container style="display:none;"
|
11
|
+
input.saveButton type="submit" value="Salvar" /
|
12
|
+
input.cancelButton type="button" value="Cancelar" /
|
13
|
+
= yield(local_assigns)
|
@@ -40,12 +40,45 @@ var InplaceEditingManager = {
|
|
40
40
|
self.bindImage(element);
|
41
41
|
});
|
42
42
|
},
|
43
|
+
bindImageBG: function(imageBGContainer) {
|
44
|
+
return new asyncImageBGForm({
|
45
|
+
containerElement: $(imageBGContainer),
|
46
|
+
formElement: $(imageBGContainer).find('form'),
|
47
|
+
inputFileElement: $(imageBGContainer).find('input[type=file]'),
|
48
|
+
imageChangerElement: $(imageBGContainer).find('a.insert-image, a.edit-image'),
|
49
|
+
errorsGroup: '#{localized_object.class.name.downcase}',
|
50
|
+
events: {
|
51
|
+
imageChanged: function() {
|
52
|
+
$(imageBGContainer).find('a.insert-image').hide();
|
53
|
+
$(imageBGContainer).find('.actions-container').show();
|
54
|
+
$(imageBGContainer).find('.actions-container input').prop('disabled', false);
|
55
|
+
},
|
56
|
+
imageUploading: function() {
|
57
|
+
$(imageBGContainer).find('.actions-container input').prop('disabled', true)
|
58
|
+
},
|
59
|
+
imageUploadSuccess: function() {
|
60
|
+
$(imageBGContainer).find('.actions-container').hide();
|
61
|
+
},
|
62
|
+
imageUploadFail: function() {
|
63
|
+
$(imageBGContainer).find('.actions-container').show();
|
64
|
+
$(imageBGContainer).find('.actions-container input').prop('disabled', false)
|
65
|
+
}
|
66
|
+
}
|
67
|
+
});
|
68
|
+
},
|
69
|
+
tryBindImageBG: function(parentElementOrSelector) {
|
70
|
+
var self = this;
|
71
|
+
$.each($(parentElementOrSelector).find('.inplace_editor_image_bg'), function(index, element) {
|
72
|
+
self.bindImageBG(element);
|
73
|
+
});
|
74
|
+
},
|
43
75
|
bindAll: function(parentElement) {
|
44
76
|
var self = this;
|
45
77
|
if (!parentElement)
|
46
78
|
parentElement = 'body';
|
47
79
|
self.tryBindText(parentElement);
|
48
80
|
self.tryBindImage(parentElement);
|
81
|
+
self.tryBindImageBG(parentElement);
|
49
82
|
}
|
50
83
|
}
|
51
84
|
|
@@ -82,11 +115,6 @@ function asyncImageForm(config) {
|
|
82
115
|
image_form.config.setConfigOrSelector(elementProperties[i]);
|
83
116
|
}
|
84
117
|
|
85
|
-
function defaultPostImageSuccess() {
|
86
|
-
window.location.reload();
|
87
|
-
return false;
|
88
|
-
};
|
89
|
-
|
90
118
|
image_form.config.inputFileElement.change(function(){
|
91
119
|
setImagePreview(this, image_form.config.imagePreviewElement);
|
92
120
|
image_form.config.events.imageChanged(this);
|
@@ -144,4 +172,100 @@ function setImagePreview(input, imageElement, noImageElement) {
|
|
144
172
|
noImageElement.show();
|
145
173
|
imageElement.hide();
|
146
174
|
}
|
175
|
+
}
|
176
|
+
|
177
|
+
function asyncImageBGForm(config) {
|
178
|
+
var image_bg_form = this;
|
179
|
+
this.config = config;
|
180
|
+
this.config.setConfigOrSelector = function(prefix) {
|
181
|
+
var self = this;
|
182
|
+
var key = prefix + 'Element';
|
183
|
+
if (!self.hasOwnProperty(key) || !self[key]) {
|
184
|
+
self[key] = $(self[prefix + 'Selector']);
|
185
|
+
}
|
186
|
+
};
|
187
|
+
// var config = {
|
188
|
+
// containerSelector: '.image_bg_container',
|
189
|
+
// containerElement: null,
|
190
|
+
// formSelector: 'form.async-republic-image',
|
191
|
+
// formElement: null,
|
192
|
+
// inputFileSelector: 'form.async-republic-image .input_addPictureButton',
|
193
|
+
// inputFileElement: null,
|
194
|
+
// imageChangerSelector: 'form.async-republic-image img.preview',
|
195
|
+
// imageChangerElement: null,
|
196
|
+
// errorsGroup: 'republic_image',
|
197
|
+
// events: {
|
198
|
+
// imageChanged: function() {},
|
199
|
+
// imageUploading: function() {},
|
200
|
+
// imageUploadSuccess: function() {},
|
201
|
+
// imageUploadFail: function() {}
|
202
|
+
// }
|
203
|
+
// }
|
204
|
+
|
205
|
+
var elementProperties = ['container', 'form', 'inputFile', 'imageChanger'];
|
206
|
+
for (var i = 0; i < elementProperties.length; i++) {
|
207
|
+
image_bg_form.config.setConfigOrSelector(elementProperties[i]);
|
208
|
+
}
|
209
|
+
|
210
|
+
image_bg_form.config.inputFileElement.change(function(){
|
211
|
+
setImageBGPreview(this, image_bg_form.config.containerElement);
|
212
|
+
image_bg_form.config.events.imageChanged(this);
|
213
|
+
});
|
214
|
+
|
215
|
+
image_bg_form.config.imageChangerElement.click(function(e){
|
216
|
+
if(typeof e.preventDefault === 'function')
|
217
|
+
e.preventDefault();
|
218
|
+
|
219
|
+
image_bg_form.config.inputFileElement.click();
|
220
|
+
return false;
|
221
|
+
});
|
222
|
+
|
223
|
+
image_bg_form.config.formElement.submit(function (event) {
|
224
|
+
event.preventDefault();
|
225
|
+
|
226
|
+
//grab all form data
|
227
|
+
var formData = new FormData($(this)[0]);
|
228
|
+
|
229
|
+
image_bg_form.config.events.imageUploading(this);
|
230
|
+
|
231
|
+
$.ajax({
|
232
|
+
url: $(this).attr('action'),
|
233
|
+
type: $(this).attr('method'),
|
234
|
+
data: formData,
|
235
|
+
cache: false,
|
236
|
+
contentType: false,
|
237
|
+
processData: false,
|
238
|
+
success: function (responseData) {
|
239
|
+
image_bg_form.config.events.imageUploadSuccess(responseData);
|
240
|
+
},
|
241
|
+
error: function () {
|
242
|
+
image_bg_form.config.events.imageUploadFail(this);
|
243
|
+
}
|
244
|
+
});
|
245
|
+
|
246
|
+
return false;
|
247
|
+
});
|
248
|
+
};
|
249
|
+
|
250
|
+
function setImageBGPreview(input, imageElement, noImageElement) {
|
251
|
+
if (input.files && input.files[0]) {
|
252
|
+
var reader = new FileReader();
|
253
|
+
|
254
|
+
reader.onload = function (e) {
|
255
|
+
if (noImageElement)
|
256
|
+
noImageElement.hide();
|
257
|
+
var originalBG = $(imageElement).attr('data-background-image');
|
258
|
+
if (!originalBG)
|
259
|
+
$(imageElement).attr('data-background-image', $(imageElement).css('background-image'));
|
260
|
+
$(imageElement).css('background-image', 'url(' + e.target.result + ')');
|
261
|
+
}
|
262
|
+
|
263
|
+
reader.readAsDataURL(input.files[0]);
|
264
|
+
} else {
|
265
|
+
if (noImageElement)
|
266
|
+
noImageElement.show();
|
267
|
+
var originalBG = $(imageElement).attr('data-background-image');
|
268
|
+
if (originalBG && originalBG.length)
|
269
|
+
$(imageElement).css('background-image', originalBG);
|
270
|
+
}
|
147
271
|
}
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: inplace_editing
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.3.
|
4
|
+
version: 0.3.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Henrique Rangel
|
@@ -57,16 +57,19 @@ files:
|
|
57
57
|
- app/helpers/inplace_editing_helper.rb
|
58
58
|
- app/views/inplace_editing/_element.html.slim
|
59
59
|
- app/views/inplace_editing/_image.html.slim
|
60
|
+
- app/views/inplace_editing/_image_bg.slim
|
60
61
|
- app/views/inplace_editing/_only_valid.html.slim
|
61
62
|
- app/views/inplace_editing/_optional.html.slim
|
62
63
|
- app/views/inplace_editing/_string.html.slim
|
63
64
|
- app/views/inplace_editing/_text.html.slim
|
64
65
|
- app/views/inplace_editing/edit/_element.html.slim
|
65
66
|
- app/views/inplace_editing/edit/_image.html.slim
|
67
|
+
- app/views/inplace_editing/edit/_image_bg.html.slim
|
66
68
|
- app/views/inplace_editing/edit/_string.html.slim
|
67
69
|
- app/views/inplace_editing/edit/_text.html.slim
|
68
70
|
- app/views/inplace_editing/read/_element.html.slim
|
69
71
|
- app/views/inplace_editing/read/_image.html.slim
|
72
|
+
- app/views/inplace_editing/read/_image_bg.slim
|
70
73
|
- app/views/inplace_editing/read/_string.html.slim
|
71
74
|
- app/views/inplace_editing/read/_text.html.slim
|
72
75
|
- bin/console
|