react_editable_content 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (70) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +18 -0
  3. data/.travis.yml +7 -0
  4. data/Gemfile +20 -0
  5. data/Gemfile.lock +129 -0
  6. data/Rakefile +20 -0
  7. data/Readme.md +44 -0
  8. data/app/assets/images/icon_edit.png +0 -0
  9. data/app/assets/javascripts/mercury.js +463 -0
  10. data/app/assets/javascripts/mercury/uploader.js.coffee +248 -0
  11. data/app/assets/stylesheets/mercury.css +23 -0
  12. data/app/controller/editable_content/editables_controller.rb +21 -0
  13. data/app/controller/mercury/images_controller.rb +24 -0
  14. data/app/helpers/editable_content/application_helper.rb +39 -0
  15. data/app/models/editable_content/editable.rb +6 -0
  16. data/app/models/mercury/image.rb +21 -0
  17. data/app/views/editable_content/_edit_link.html.erb +3 -0
  18. data/app/views/layouts/mercury.html.erb +23 -0
  19. data/config/routes.rb +9 -0
  20. data/lib/editable_content/engine.rb +17 -0
  21. data/lib/editable_content/railtie.rb +10 -0
  22. data/lib/generators/editable_content/install/install_generator.rb +22 -0
  23. data/lib/generators/editable_content/install/templates/create_editable_content_editables.rb +11 -0
  24. data/lib/generators/editable_content/install/templates/create_mercury_images.rb +8 -0
  25. data/lib/generators/editable_content/install/templates/lib/mercury/authentication.rb +9 -0
  26. data/lib/mercury/authentication.rb +9 -0
  27. data/lib/react_editable_content.rb +6 -0
  28. data/react_editable_content.gemspec +25 -0
  29. data/test/controllers/editable_content/editable_controller_test.rb +37 -0
  30. data/test/dummy/Rakefile +7 -0
  31. data/test/dummy/app/controllers/application_controller.rb +3 -0
  32. data/test/dummy/app/helpers/application_helper.rb +2 -0
  33. data/test/dummy/app/views/layouts/application.html.erb +14 -0
  34. data/test/dummy/config.ru +4 -0
  35. data/test/dummy/config/application.rb +44 -0
  36. data/test/dummy/config/boot.rb +10 -0
  37. data/test/dummy/config/database.yml +22 -0
  38. data/test/dummy/config/environment.rb +5 -0
  39. data/test/dummy/config/environments/development.rb +25 -0
  40. data/test/dummy/config/environments/production.rb +49 -0
  41. data/test/dummy/config/environments/test.rb +35 -0
  42. data/test/dummy/config/initializers/backtrace_silencers.rb +7 -0
  43. data/test/dummy/config/initializers/inflections.rb +10 -0
  44. data/test/dummy/config/initializers/mime_types.rb +5 -0
  45. data/test/dummy/config/initializers/secret_token.rb +7 -0
  46. data/test/dummy/config/initializers/session_store.rb +8 -0
  47. data/test/dummy/config/locales/en.yml +5 -0
  48. data/test/dummy/config/routes.rb +58 -0
  49. data/test/dummy/db/migrate/20140908144759_create_editable_content_editables.rb +11 -0
  50. data/test/dummy/db/migrate/20140908144760_create_mercury_images.rb +8 -0
  51. data/test/dummy/db/schema.rb +33 -0
  52. data/test/dummy/db/test.sqlite3 +0 -0
  53. data/test/dummy/lib/mercury/authentication.rb +9 -0
  54. data/test/dummy/public/404.html +26 -0
  55. data/test/dummy/public/422.html +26 -0
  56. data/test/dummy/public/500.html +26 -0
  57. data/test/dummy/public/favicon.ico +0 -0
  58. data/test/dummy/public/javascripts/application.js +2 -0
  59. data/test/dummy/public/javascripts/controls.js +965 -0
  60. data/test/dummy/public/javascripts/dragdrop.js +974 -0
  61. data/test/dummy/public/javascripts/effects.js +1123 -0
  62. data/test/dummy/public/javascripts/prototype.js +6001 -0
  63. data/test/dummy/public/javascripts/rails.js +202 -0
  64. data/test/dummy/public/stylesheets/.gitkeep +0 -0
  65. data/test/dummy/script/rails +6 -0
  66. data/test/dummy/test/fixtures/editables.yml +9 -0
  67. data/test/dummy/test/fixtures/rails.png +0 -0
  68. data/test/helpers/editables_helper_test.rb +78 -0
  69. data/test/test_helper.rb +27 -0
  70. metadata +167 -0
@@ -0,0 +1,248 @@
1
+ @Mercury.uploader = (file, options) ->
2
+ Mercury.uploader.show(file, options) if Mercury.config.uploading.enabled
3
+ return Mercury.uploader
4
+
5
+ jQuery.extend Mercury.uploader,
6
+
7
+ show: (file, @options = {}) ->
8
+ @file = new Mercury.uploader.File(file)
9
+ if @file.errors
10
+ alert("Error: #{@file.errors}")
11
+ return
12
+ return unless @supported()
13
+
14
+ Mercury.trigger('focus:window')
15
+ @initialize()
16
+ @appear()
17
+
18
+
19
+ initialize: ->
20
+ return if @initialized
21
+ @build()
22
+ @bindEvents()
23
+ @initialized = true
24
+
25
+
26
+ supported: ->
27
+ xhr = new XMLHttpRequest
28
+
29
+ if window.Uint8Array && window.ArrayBuffer && !XMLHttpRequest.prototype.sendAsBinary
30
+ XMLHttpRequest::sendAsBinary = (datastr) ->
31
+ ui8a = new Uint8Array(datastr.length)
32
+ ui8a[index] = (datastr.charCodeAt(index) & 0xff) for data, index in datastr
33
+ @send(ui8a.buffer)
34
+
35
+ return !!(xhr.upload && xhr.sendAsBinary && (Mercury.uploader.fileReaderSupported() || Mercury.uploader.formDataSupported()))
36
+
37
+ fileReaderSupported: ->
38
+ !!(window.FileReader)
39
+
40
+ formDataSupported: ->
41
+ !!(window.FormData)
42
+
43
+ build: ->
44
+ @element = jQuery('<div>', {class: 'mercury-uploader', style: 'display:none'})
45
+ @element.append('<div class="mercury-uploader-preview"><b><img/></b></div>')
46
+ @element.append('<div class="mercury-uploader-details"></div>')
47
+ @element.append('<div class="mercury-uploader-progress"><span></span><div class="mercury-uploader-indicator"><div><b>0%</b></div></div></div>')
48
+
49
+ @updateStatus('Processing...')
50
+
51
+ @overlay = jQuery('<div>', {class: 'mercury-uploader-overlay', style: 'display:none'})
52
+
53
+ @element.appendTo(jQuery(@options.appendTo).get(0) ? 'body')
54
+ @overlay.appendTo(jQuery(@options.appendTo).get(0) ? 'body')
55
+
56
+
57
+ bindEvents: ->
58
+ Mercury.on 'resize', => @position()
59
+
60
+
61
+ appear: ->
62
+ @fillDisplay()
63
+ @position()
64
+
65
+ @overlay.show()
66
+ @overlay.animate {opacity: 1}, 200, 'easeInOutSine', =>
67
+ @element.show()
68
+ @element.animate {opacity: 1}, 200, 'easeInOutSine', =>
69
+ @visible = true
70
+ @loadImage()
71
+
72
+
73
+ position: ->
74
+ width = @element.outerWidth()
75
+ height = @element.outerHeight()
76
+
77
+ @element.css {
78
+ top: (Mercury.displayRect.height - height) / 2
79
+ left: (Mercury.displayRect.width - width) / 2
80
+ }
81
+
82
+
83
+ fillDisplay: ->
84
+ details = [
85
+ Mercury.I18n('Name: %s', @file.name),
86
+ Mercury.I18n('Size: %s', @file.readableSize),
87
+ Mercury.I18n('Type: %s', @file.type)
88
+ ]
89
+ @element.find('.mercury-uploader-details').html(details.join('<br/>'))
90
+
91
+
92
+ loadImage: ->
93
+ if Mercury.uploader.fileReaderSupported()
94
+ @file.readAsDataURL (result) =>
95
+ @element.find('.mercury-uploader-preview b').html(jQuery('<img>', {src: result}))
96
+ @upload()
97
+ else
98
+ @upload()
99
+
100
+
101
+ upload: ->
102
+ xhr = new XMLHttpRequest
103
+ jQuery.each ['onloadstart', 'onprogress', 'onload', 'onabort', 'onerror'], (index, eventName) =>
104
+ xhr.upload[eventName] = (event) => @uploaderEvents[eventName].call(@, event)
105
+ xhr.onload = (event) =>
106
+ if (event.currentTarget.status >= 400)
107
+ @updateStatus('Error: Unable to upload the file')
108
+ Mercury.notify('Unable to process response: %s', event.currentTarget.status)
109
+ @hide()
110
+ else
111
+ try
112
+ response =
113
+ if Mercury.config.uploading.handler
114
+ Mercury.config.uploading.handler(event.target.responseText)
115
+ else
116
+ jQuery.parseJSON(event.target.responseText)
117
+ src = response.url || response.image.url
118
+ throw 'Malformed response from server.' unless src
119
+ Mercury.trigger('action', {action: 'insertImage', value: {src: src}})
120
+ @hide()
121
+ catch error
122
+ @updateStatus('Error: Unable to upload the file')
123
+ Mercury.notify('Unable to process response: %s', error)
124
+ @hide()
125
+
126
+ size = ""
127
+ if Mercury.region.element and Mercury.region.element.data('size')
128
+ size = Mercury.region.element.data('size')
129
+ xhr.open('post', Mercury.config.uploading.url + "?size=" + size, true)
130
+ xhr.setRequestHeader('Accept', 'application/json, text/javascript, text/html, application/xml, text/xml, */*')
131
+ xhr.setRequestHeader('X-Requested-With', 'XMLHttpRequest')
132
+ xhr.setRequestHeader(Mercury.config.csrfHeader, Mercury.csrfToken)
133
+
134
+ # Homespun multipart uploads. Chrome 18, Firefox 11.
135
+ #
136
+ if Mercury.uploader.fileReaderSupported()
137
+ @file.readAsBinaryString (result) =>
138
+
139
+ multipart = new Mercury.uploader.MultiPartPost(Mercury.config.uploading.inputName, @file, result)
140
+
141
+ # update the content size so we can calculate
142
+ @file.updateSize(multipart.delta)
143
+
144
+ # set the content type and send
145
+ xhr.setRequestHeader('Content-Type', 'multipart/form-data; boundary=' + multipart.boundary)
146
+ xhr.sendAsBinary(multipart.body)
147
+
148
+ # FormData based. Safari 5.1.2.
149
+ #
150
+ else
151
+ formData = new FormData()
152
+ formData.append(Mercury.config.uploading.inputName, @file.file, @file.file.name)
153
+
154
+ xhr.send(formData)
155
+
156
+
157
+
158
+ updateStatus: (message, loaded) ->
159
+ @element.find('.mercury-uploader-progress span').html(Mercury.I18n(message).toString())
160
+ if loaded
161
+ percent = Math.floor(loaded * 100 / @file.size) + '%'
162
+ @element.find('.mercury-uploader-indicator div').css({width: percent})
163
+ @element.find('.mercury-uploader-indicator b').html(percent).show()
164
+
165
+
166
+ hide: (delay = 0) ->
167
+ setTimeout =>
168
+ @element.animate {opacity: 0}, 200, 'easeInOutSine', =>
169
+ @overlay.animate {opacity: 0}, 200, 'easeInOutSine', =>
170
+ @overlay.hide()
171
+ @element.hide()
172
+ @reset()
173
+ @visible = false
174
+ Mercury.trigger('focus:frame')
175
+ , delay * 1000
176
+
177
+
178
+ reset: ->
179
+ @element.find('.mercury-uploader-preview b').html('')
180
+ @element.find('.mercury-uploader-indicator div').css({width: 0})
181
+ @element.find('.mercury-uploader-indicator b').html('0%').hide()
182
+ @updateStatus('Processing...')
183
+
184
+
185
+ uploaderEvents:
186
+ onloadstart: -> @updateStatus('Uploading...')
187
+
188
+ onprogress: (event) -> @updateStatus('Uploading...', event.loaded)
189
+
190
+ onabort: ->
191
+ @updateStatus('Aborted')
192
+ @hide(1)
193
+
194
+ onload: ->
195
+ @updateStatus('Successfully uploaded...', @file.size)
196
+
197
+ onerror: ->
198
+ @updateStatus('Error: Unable to upload the file')
199
+ @hide(3)
200
+
201
+
202
+
203
+ class Mercury.uploader.File
204
+
205
+ constructor: (@file) ->
206
+ @fullSize = @size = @file.size || @file.fileSize
207
+ @readableSize = @size.toBytes()
208
+ @name = @file.name || @file.fileName
209
+ @type = @file.type || @file.fileType
210
+
211
+ # add any errors if we need to
212
+ errors = []
213
+ errors.push(Mercury.I18n('Too large')) if @size >= Mercury.config.uploading.maxFileSize
214
+ errors.push(Mercury.I18n('Unsupported format')) unless Mercury.config.uploading.allowedMimeTypes.indexOf(@type) > -1
215
+ @errors = errors.join(' / ') if errors.length
216
+
217
+
218
+ readAsDataURL: (callback = null) ->
219
+ reader = new FileReader()
220
+ reader.readAsDataURL(@file)
221
+ reader.onload = => callback(reader.result) if callback
222
+
223
+
224
+ readAsBinaryString: (callback = null) ->
225
+ reader = new FileReader()
226
+ reader.readAsBinaryString(@file)
227
+ reader.onload = => callback(reader.result) if callback
228
+
229
+
230
+ updateSize: (delta) ->
231
+ @fullSize = @size + delta
232
+
233
+
234
+
235
+ class Mercury.uploader.MultiPartPost
236
+
237
+ constructor: (@inputName, @file, @contents, @formInputs = {}) ->
238
+ @boundary = 'Boundaryx20072377098235644401115438165x'
239
+ @body = ''
240
+ @buildBody()
241
+ @delta = @body.length - @file.size
242
+
243
+
244
+ buildBody: ->
245
+ boundary = '--' + @boundary
246
+ for own name, value of @formInputs
247
+ @body += "#{boundary}\r\nContent-Disposition: form-data; name=\"#{name}\"\r\n\r\n#{unescape(encodeURIComponent(value))}\r\n"
248
+ @body += "#{boundary}\r\nContent-Disposition: form-data; name=\"#{@inputName}\"; filename=\"#{@file.name}\"\r\nContent-Type: #{@file.type}\r\nContent-Transfer-Encoding: binary\r\n\r\n#{@contents}\r\n#{boundary}--"
@@ -0,0 +1,23 @@
1
+ /*!
2
+ * Mercury Editor is a Coffeescript and jQuery based WYSIWYG editor. Documentation and other useful information can be
3
+ * found at https://github.com/jejacks0n/mercury
4
+ *
5
+ * Copyright (c) 2011 Jeremy Jackson
6
+ *
7
+ * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
8
+ * documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
9
+ * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit
10
+ * persons to whom the Software is furnished to do so, subject to the following conditions:
11
+ *
12
+ * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
13
+ * Software.
14
+ *
15
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
16
+ * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
17
+ * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
18
+ * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
19
+ *
20
+ *= require_self
21
+ *= require mercury/bootstrap-ish
22
+ *= require mercury/mercury
23
+ */
@@ -0,0 +1,21 @@
1
+ module EditableContent
2
+ class EditablesController < ::ApplicationController
3
+ include Mercury::Authentication
4
+
5
+ def update
6
+ if can_edit?
7
+ params[:content].each do |key, value|
8
+ editable = Editable.find_by_key key
9
+ if value[:type] == "image" and !value[:attributes][:src].start_with?("/assets")
10
+ editable.update_attribute(:src, value[:attributes][:src])
11
+ else
12
+ editable.update_attribute(:text, value[:value])
13
+ end
14
+ end
15
+ render json: {status: "ok"}
16
+ else
17
+ render nothing: true, status: :unauthorized
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,24 @@
1
+ class Mercury::ImagesController < MercuryController
2
+
3
+ respond_to :json
4
+
5
+ # POST /images.json
6
+ def create
7
+
8
+ @image = Mercury::Image.new
9
+ @image.size = "300x300"
10
+ @image.size = params[:size] if params[:size] and !params[:size].empty?
11
+ @image.image = params.require(:image).permit(:image)[:image]
12
+
13
+ @image.save
14
+ respond_with @image
15
+ end
16
+
17
+ # DELETE /images/1.json
18
+ def destroy
19
+ @image = Mercury::Image.find(params[:id])
20
+ @image.destroy
21
+ respond_with @image
22
+ end
23
+
24
+ end
@@ -0,0 +1,39 @@
1
+ module EditableContent::ApplicationHelper
2
+ #extend ApplicationHelper
3
+ include Mercury::Authentication
4
+
5
+ def editable_content(name = nil, &block)
6
+ contents = capture(&block)
7
+ key = Digest::MD5.hexdigest(name ? name : contents)
8
+ e = ::EditableContent::Editable.find_by_key(key) || ::EditableContent::Editable.create(:key => key, :text => contents)
9
+ contents = e.text if !e.text.empty?
10
+
11
+ if can_edit?
12
+ return content_tag(:div, contents.html_safe, {:id => key, 'data-mercury' => 'full'})
13
+ else
14
+ return contents.html_safe
15
+ end
16
+ end
17
+
18
+ def editable_image_tag(src, *args)
19
+ options = args.extract_options!
20
+ size = options[:size]
21
+ if !size
22
+ raise "The attribute :size must be specified on editable_image_tag"
23
+ end
24
+
25
+ key = Digest::MD5.hexdigest(src)
26
+ if @editable = ::EditableContent::Editable.where(:key => key).first
27
+ src = @editable.src
28
+ else
29
+ @editable = ::EditableContent::Editable.create(:key => key, :src => src)
30
+ end
31
+
32
+ if can_edit?
33
+ image_tag(src, options.merge({:id => key, "data-size" => size, "data-mercury" => "image"}))
34
+ else
35
+ image_tag(src, options)
36
+ end
37
+ end
38
+
39
+ end
@@ -0,0 +1,6 @@
1
+ module EditableContent
2
+ class Editable < ActiveRecord::Base
3
+ self.table_name = "editable_content_editables"
4
+ validates_uniqueness_of :key
5
+ end
6
+ end
@@ -0,0 +1,21 @@
1
+ class Mercury::Image < ActiveRecord::Base
2
+
3
+ self.table_name = :mercury_images
4
+
5
+ has_attached_file :image, :styles => lambda { |image| { :resized => image.instance.size } }
6
+ validates_attachment_content_type :image, :content_type => %w(image/jpeg image/jpg image/png image/gif)
7
+
8
+ attr_accessor :size
9
+
10
+ def serializable_hash(options = nil)
11
+ options ||= {}
12
+ options[:methods] ||= []
13
+ options[:methods] << :url
14
+ super(options)
15
+ end
16
+
17
+ def url
18
+ self.image(:resized)
19
+ end
20
+
21
+ end
@@ -0,0 +1,3 @@
1
+ <% if can_edit? %>
2
+ <%= link_to image_tag("icon_edit.png"), "/editor" + request.path[1..-1], id: "edit-link", style: "position: fixed; right:10px; top: 10px; padding:5px 5px 10px 10px; background:rgba(255,255,255,.7); border-radius:10px;", data: { no_turbolink: true } %>
3
+ <% end %>
@@ -0,0 +1,23 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <meta name="viewport" content="width=device-width, maximum-scale=1.0, initial-scale=1.0">
5
+ <%= csrf_meta_tags %>
6
+ <title>Mercury Editor</title>
7
+ <%= stylesheet_link_tag 'mercury' %>
8
+ <%= javascript_include_tag 'jquery-1.7', 'mercury' %>
9
+ </head>
10
+ <body>
11
+ <script type="text/javascript">
12
+ // Set to the url that you want to save any given page to, leave null for default handling.
13
+ var saveUrl = '/editable';
14
+
15
+ // Instantiate the PageEditor
16
+ new Mercury.PageEditor(saveUrl, {
17
+ saveStyle: 'form', // 'form', or 'json' (default json)
18
+ saveMethod: null, // 'PUT', or 'POST', (create, vs. update -- default PUT)
19
+ visible: true // boolean - if the interface should start visible or not
20
+ });
21
+ </script>
22
+ </body>
23
+ </html>
@@ -0,0 +1,9 @@
1
+ Rails.application.routes.draw do
2
+ namespace :mercury do
3
+ resources :images
4
+ end
5
+
6
+ put 'editable' => 'editable_content/editables#update'
7
+
8
+ mount Mercury::Engine => '/'
9
+ end
@@ -0,0 +1,17 @@
1
+ #require 'mercury-rails'
2
+
3
+ module EditableContent
4
+ class Engine < ::Rails::Engine
5
+ #isolate_namespace EditableContent
6
+
7
+ initializer :assets, :group => :all do |app|
8
+ app.config.assets.precompile += %w( jquery-1.7.js )
9
+ end
10
+
11
+ # Require mercury authentication module and potentially other aspects later (so they can be overridden).
12
+ initializer 'mercury.add_lib' do |app|
13
+ require 'mercury/authentication'
14
+ end
15
+
16
+ end
17
+ end