mercury-rails 0.6.0 → 0.7.0

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.
Files changed (61) hide show
  1. data/app/assets/javascripts/mercury.js +1 -0
  2. data/app/assets/javascripts/mercury/lightview.js.coffee +26 -12
  3. data/app/assets/javascripts/mercury/locales/ar.locale.js.coffee +3 -5
  4. data/app/assets/javascripts/mercury/locales/da.locale.js.coffee +3 -5
  5. data/app/assets/javascripts/mercury/locales/de.locale.js.coffee +4 -6
  6. data/app/assets/javascripts/mercury/locales/es.locale.js.coffee +3 -5
  7. data/app/assets/javascripts/mercury/locales/example.local.js.coffee +3 -1
  8. data/app/assets/javascripts/mercury/locales/fr.locale.js.coffee +3 -5
  9. data/app/assets/javascripts/mercury/locales/it.locale.js.coffee +3 -5
  10. data/app/assets/javascripts/mercury/locales/ko.local.js.coffee +4 -6
  11. data/app/assets/javascripts/mercury/locales/nl.locale.js.coffee +3 -5
  12. data/app/assets/javascripts/mercury/locales/pt.locale.js.coffee +3 -5
  13. data/app/assets/javascripts/mercury/locales/sv.local.js.coffee +3 -5
  14. data/app/assets/javascripts/mercury/locales/swedish_chef.locale.js.coffee +3 -5
  15. data/app/assets/javascripts/mercury/locales/zh.local.js.coffee +3 -5
  16. data/app/assets/javascripts/mercury/mercury.js.coffee +1 -1
  17. data/app/assets/javascripts/mercury/modal.js.coffee +38 -23
  18. data/app/assets/javascripts/mercury/modals/htmleditor.js.coffee +1 -0
  19. data/app/assets/javascripts/mercury/modals/insertcharacter.js.coffee +1 -0
  20. data/app/assets/javascripts/mercury/modals/insertlink.js.coffee +111 -60
  21. data/app/assets/javascripts/mercury/modals/insertmedia.js.coffee +80 -35
  22. data/app/assets/javascripts/mercury/modals/insertsnippet.js.coffee +1 -0
  23. data/app/assets/javascripts/mercury/modals/inserttable.js.coffee +59 -51
  24. data/app/assets/javascripts/mercury/page_editor.js.coffee +1 -1
  25. data/app/assets/javascripts/mercury/regions/full.coffee +1 -1
  26. data/app/assets/javascripts/mercury/regions/image.js.coffee +1 -1
  27. data/app/assets/javascripts/mercury/regions/markdown.coffee +1 -1
  28. data/app/assets/javascripts/mercury/regions/simple.js.coffee +1 -1
  29. data/app/assets/javascripts/mercury/regions/snippets.js.coffee +1 -1
  30. data/app/assets/javascripts/mercury/table_editor.js.coffee +16 -0
  31. data/app/assets/javascripts/mercury/toolbar.js.coffee +2 -2
  32. data/app/assets/stylesheets/mercury.css +0 -5
  33. data/app/assets/stylesheets/mercury/bootstrap-ish.css +1367 -0
  34. data/app/assets/stylesheets/mercury/bootstrap-overrides.css +60 -0
  35. data/app/assets/stylesheets/mercury/lightview.css +2 -52
  36. data/app/assets/stylesheets/mercury/mercury.css +2 -2
  37. data/app/assets/stylesheets/mercury/modal.css +13 -31
  38. data/app/assets/stylesheets/mercury/uploader.css +3 -3
  39. data/app/views/mercury/modals/htmleditor.html +2 -6
  40. data/app/views/mercury/modals/link.html +70 -64
  41. data/app/views/mercury/modals/media.html +80 -71
  42. data/app/views/mercury/modals/table.html +79 -73
  43. data/app/views/mercury/snippets/example/options.html.erb +11 -30
  44. data/features/loading/loading.feature +1 -1
  45. data/features/regions/full/inserting_links.feature +4 -4
  46. data/features/regions/full/inserting_media.feature +3 -3
  47. data/features/step_definitions/mercury_steps.rb +5 -5
  48. data/features/support/env.rb +1 -1
  49. data/lib/mercury/version.rb +1 -1
  50. data/spec/javascripts/mercury/lightview_spec.js.coffee +136 -129
  51. data/spec/javascripts/mercury/modal_spec.js.coffee +143 -139
  52. data/spec/javascripts/mercury/modals/insertlink_spec.js.coffee +105 -31
  53. data/spec/javascripts/mercury/modals/insertmedia_spec.js.coffee +76 -38
  54. data/spec/javascripts/mercury/modals/inserttable_spec.js.coffee +32 -35
  55. data/spec/javascripts/mercury/toolbar_spec.js.coffee +8 -0
  56. data/spec/javascripts/templates/mercury/modals/insertlink.html +58 -21
  57. data/spec/javascripts/templates/mercury/modals/insertmedia.html +69 -27
  58. data/spec/javascripts/templates/mercury/modals/inserttable.html +66 -20
  59. metadata +5 -5
  60. data/app/assets/stylesheets/mercury/buttons.css +0 -73
  61. data/app/assets/stylesheets/mercury/form.css +0 -125
@@ -34,7 +34,7 @@
34
34
  #
35
35
  @Mercury ||= {}
36
36
  jQuery.extend @Mercury,
37
- version: '0.6.0'
37
+ version: '0.7.0'
38
38
 
39
39
  # Mercury object namespaces
40
40
  Regions: Mercury.Regions || {}
@@ -1,19 +1,27 @@
1
1
  @Mercury.modal = (url, options = {}) ->
2
- Mercury.modal.show(url, options)
3
- return Mercury.modal
2
+ Mercury.modal.instance ||= new Mercury.Modal(url, options)
3
+ Mercury.modal.instance.show(url, options)
4
+ return Mercury.modal.instance
4
5
 
5
- jQuery.extend Mercury.modal,
6
- minWidth: 400
7
6
 
8
- show: (@url, @options = {}) ->
7
+ class @Mercury.Modal
8
+
9
+ constructor: (@url, @options = {}) ->
10
+
11
+
12
+ show: (url = null, options = null) ->
13
+ @url = url || @url
14
+ @options = options || @options
15
+ @options.minWidth ||= 400
16
+
9
17
  Mercury.trigger('focus:window')
10
- @initialize()
18
+ @initializeModal()
11
19
  if @visible then @update() else @appear()
12
20
  if @options.content
13
- setTimeout 500, => @loadContent(@options.content)
21
+ setTimeout((=> @loadContent(@options.content)), 500)
14
22
 
15
23
 
16
- initialize: ->
24
+ initializeModal: ->
17
25
  return if @initialized
18
26
  @build()
19
27
  @bindEvents()
@@ -51,7 +59,7 @@ jQuery.extend Mercury.modal,
51
59
  @loadContent(content)
52
60
 
53
61
  jQuery(document).on 'keydown', (event) =>
54
- @hide() if event.keyCode == 27 && @visible
62
+ @hide() if event.keyCode == 27 && @visible
55
63
 
56
64
 
57
65
  appear: ->
@@ -81,16 +89,16 @@ jQuery.extend Mercury.modal,
81
89
 
82
90
  height = @contentElement.outerHeight() + titleHeight
83
91
 
84
- width = @minWidth if width < @minWidth
85
- height = Mercury.displayRect.fullHeight - 20 if height > Mercury.displayRect.fullHeight - 20 || @options.fullHeight
92
+ width = @options.minWidth if width < @options.minWidth
93
+ height = Mercury.displayRect.fullHeight if height > Mercury.displayRect.fullHeight || @options.fullHeight
86
94
 
87
95
  @element.stop().animate {left: (Mercury.displayRect.width - width) / 2, width: width, height: height}, 200, 'easeInOutSine', =>
88
96
  @contentElement.css({visibility: 'visible', display: 'block'})
89
97
  if @contentPane.length
90
98
  @contentElement.css({height: height - titleHeight, overflow: 'visible'})
91
- controlHeight = if @contentControl.length then @contentControl.outerHeight() else 0
92
- @contentPane.css({height: height - titleHeight - controlHeight - 40})
93
- @contentPane.find('.mercury-display-pane').css({width: width - 40})
99
+ controlHeight = if @contentControl.length then @contentControl.outerHeight() + 10 else 0
100
+ @contentPane.css({height: height - titleHeight - controlHeight - 20})
101
+ @contentPane.find('.mercury-display-pane').css({width: width - 20})
94
102
  else
95
103
  @contentElement.css({height: height - titleHeight, overflow: 'auto'})
96
104
 
@@ -105,15 +113,15 @@ jQuery.extend Mercury.modal,
105
113
  width = @element.width()
106
114
  height = @element.height()
107
115
 
108
- width = @minWidth if width < @minWidth
109
- height = Mercury.displayRect.fullHeight - 20 if height > Mercury.displayRect.fullHeight - 20 || @options.fullHeight
116
+ width = @options.minWidth if width < @options.minWidth
117
+ height = Mercury.displayRect.fullHeight if height > Mercury.displayRect.fullHeight || @options.fullHeight
110
118
 
111
119
  titleHeight = @titleElement.outerHeight()
112
120
  if @contentPane && @contentPane.length
113
121
  @contentElement.css({height: height - titleHeight, overflow: 'visible'})
114
- controlHeight = if @contentControl.length then @contentControl.outerHeight() else 0
115
- @contentPane.css({height: height - titleHeight - controlHeight - 40})
116
- @contentPane.find('.mercury-display-pane').css({width: width - 40})
122
+ controlHeight = if @contentControl.length then @contentControl.outerHeight() + 10 else 0
123
+ @contentPane.css({height: height - titleHeight - controlHeight - 20})
124
+ @contentPane.find('.mercury-display-pane').css({width: width - 20})
117
125
  else
118
126
  @contentElement.css({height: height - titleHeight, overflow: 'auto'})
119
127
 
@@ -151,7 +159,7 @@ jQuery.extend Mercury.modal,
151
159
 
152
160
 
153
161
  loadContent: (data, options = null) ->
154
- @initialize()
162
+ @initializeModal()
155
163
  @options = options || @options
156
164
  @setTitle()
157
165
  @loaded = true
@@ -166,9 +174,17 @@ jQuery.extend Mercury.modal,
166
174
  @options.afterLoad.call(@) if @options.afterLoad
167
175
  if @options.handler
168
176
  if Mercury.modalHandlers[@options.handler]
169
- Mercury.modalHandlers[@options.handler].call(@)
177
+ if typeof(Mercury.modalHandlers[@options.handler]) == 'function'
178
+ Mercury.modalHandlers[@options.handler].call(@)
179
+ else
180
+ jQuery.extend(@, Mercury.modalHandlers[@options.handler])
181
+ @initialize()
170
182
  else if Mercury.lightviewHandlers[@options.handler]
171
- Mercury.lightviewHandlers[@options.handler].call(@)
183
+ if typeof(Mercury.lightviewHandlers[@options.handler]) == 'function'
184
+ Mercury.lightviewHandlers[@options.handler].call(@)
185
+ else
186
+ jQuery.extend(@, Mercury.lightviewHandlers[@options.handler])
187
+ @initialize()
172
188
 
173
189
  @element.localize(Mercury.locale()) if Mercury.config.localization.enabled
174
190
  @resize()
@@ -188,7 +204,6 @@ jQuery.extend Mercury.modal,
188
204
  hide: ->
189
205
  return if @showing
190
206
  @options = {}
191
- @initialized = false
192
207
 
193
208
  Mercury.trigger('focus:frame')
194
209
  @element.hide()
@@ -1,4 +1,5 @@
1
1
  @Mercury.modalHandlers.htmlEditor = ->
2
+
2
3
  # fill the text area with the content
3
4
  content = Mercury.region.content(null, true, false)
4
5
  @element.find('textarea').val(content)
@@ -1,4 +1,5 @@
1
1
  @Mercury.modalHandlers.insertCharacter = ->
2
+
2
3
  @element.find('.character').on 'click', (event) =>
3
4
  Mercury.trigger('action', {action: 'insertHTML', value: "&#{jQuery(event.target).attr('data-entity')};"})
4
5
  @hide()
@@ -1,67 +1,117 @@
1
- @Mercury.modalHandlers.insertLink = ->
2
- # make the inputs work with the radio buttons
3
- @element.find('label input').on 'click', ->
4
- jQuery(@).closest('label').next('.selectable').focus()
1
+ @Mercury.modalHandlers.insertLink = {
5
2
 
6
- @element.find('.selectable').on 'focus', ->
7
- jQuery(@).prev('label').find('input[type=radio]').prop("checked", true)
3
+ initialize: ->
4
+ # make the inputs work with the radio buttons
5
+ @element.find('.control-label input').on('click', @onLabelChecked)
6
+ @element.find('.controls .optional, .controls .required').on('focus', @onInputFocused)
8
7
 
9
- # show/hide the link target options on target change
10
- @element.find('#link_target').on 'change', =>
8
+ # show/hide the link target options on target change
9
+ @element.find('#link_target').on('change', => @onChangeTarget())
10
+
11
+ @initializeForm()
12
+
13
+ # build the link on form submission
14
+ @element.find('form').on 'submit', (event) =>
15
+ event.preventDefault()
16
+ @validateForm()
17
+ unless @valid
18
+ @resize()
19
+ return
20
+ @submitForm()
21
+ @hide()
22
+
23
+
24
+ initializeForm: ->
25
+ @fillExistingBookmarks()
26
+
27
+ # get the selection and initialize its information into the form
28
+ return unless Mercury.region && Mercury.region.selection
29
+ selection = Mercury.region.selection()
30
+
31
+ # set the text content
32
+ @element.find('#link_text').val(selection.textContent()) if selection.textContent
33
+
34
+ # if we're editing a link prefill the information
35
+ a = selection.commonAncestor(true).closest('a') if selection && selection.commonAncestor
36
+ return false unless a && a.length
37
+ @editing = a
38
+
39
+ # don't allow changing the content on edit
40
+ @element.find('#link_text_container').hide()
41
+
42
+ # fill in the external url or bookmark select based on what it looks like
43
+ if a.attr('href') && a.attr('href').indexOf('#') == 0
44
+ bookmarkSelect = @element.find('#link_existing_bookmark')
45
+ bookmarkSelect.val(a.attr('href').replace(/[^#]*#/, ''))
46
+ bookmarkSelect.closest('.control-group').find('input[type=radio]').prop('checked', true)
47
+ else
48
+ @element.find('#link_external_url').val(a.attr('href'))
49
+
50
+ # if it has a name, assume it's a bookmark target
51
+ if a.attr('name')
52
+ newBookmarkInput = @element.find('#link_new_bookmark')
53
+ newBookmarkInput.val(a.attr('name'))
54
+ newBookmarkInput.closest('.control-group').find('input[type=radio]').prop('checked', true)
55
+
56
+ # if it has a target, select it, and try to pull options out
57
+ if a.attr('target')
58
+ @element.find('#link_target').val(a.attr('target'))
59
+
60
+ # if it's a popup window
61
+ if a.attr('href') && a.attr('href').indexOf('javascript:void') == 0
62
+ href = a.attr('href')
63
+ @element.find('#link_external_url').val(href.match(/window.open\('([^']+)',/)[1])
64
+ @element.find('#link_target').val('popup')
65
+ @element.find('#link_popup_width').val(href.match(/width=(\d+),/)[1])
66
+ @element.find('#link_popup_height').val(href.match(/height=(\d+),/)[1])
67
+ @element.find('#popup_options').show()
68
+
69
+
70
+ fillExistingBookmarks: ->
71
+ bookmarkSelect = @element.find('#link_existing_bookmark')
72
+ for tag in jQuery('a[name]', window.mercuryInstance.document)
73
+ bookmarkSelect.append(jQuery('<option>', {value: jQuery(tag).attr('name')}).text(jQuery(tag).text()))
74
+
75
+
76
+ onLabelChecked: ->
77
+ forInput = jQuery(@).closest('.control-label').attr('for')
78
+ jQuery(@).closest('.control-group').find("##{forInput}").focus()
79
+
80
+
81
+ onInputFocused: ->
82
+ jQuery(@).closest('.control-group').find('input[type=radio]').prop('checked', true)
83
+
84
+
85
+ onChangeTarget: ->
11
86
  @element.find(".link-target-options").hide()
12
87
  @element.find("##{@element.find('#link_target').val()}_options").show()
13
88
  @resize(true)
14
89
 
15
- # fill the existing bookmark select
16
- bookmarkSelect = @element.find('#link_existing_bookmark')
17
- for link in jQuery('a[name]', window.mercuryInstance.document)
18
- bookmarkSelect.append(jQuery('<option>', {value: jQuery(link).attr('name')}).text(jQuery(link).text()))
19
90
 
20
- # get the selection and initialize its information into the form
21
- if Mercury.region && Mercury.region.selection
22
- selection = Mercury.region.selection()
91
+ addInputError: (input, message) ->
92
+ input.after('<span class="help-inline error-message">' + Mercury.I18n(message) + '</span>').closest('.control-group').addClass('error')
93
+ @valid = false
23
94
 
24
- # if we're editing a link prefill the information
25
- container = selection.commonAncestor(true).closest('a') if selection && selection.commonAncestor
26
- if container && container.length
27
- existingLink = container
28
95
 
29
- # don't allow changing the content on edit
30
- @element.find('#link_text_container').hide()
96
+ clearInputErrors: ->
97
+ @element.find('.control-group.error').removeClass('error').find('.error-message').remove()
98
+ @valid = true
31
99
 
32
- # fill in the external url or bookmark select based on what it looks like
33
- if container.attr('href') && container.attr('href').indexOf('#') == 0
34
- bookmarkSelect.val(container.attr('href').replace(/[^#]*#/, ''))
35
- bookmarkSelect.prev('label').find('input[type=radio]').prop("checked", true)
36
- else
37
- @element.find('#link_external_url').val(container.attr('href'))
38
-
39
- # if it has a name, assume it's a bookmark target
40
- if container.attr('name')
41
- newBookmarkInput = @element.find('#link_new_bookmark')
42
- newBookmarkInput.val(container.attr('name'))
43
- newBookmarkInput.prev('label').find('input[type=radio]').prop("checked", true)
44
-
45
- # if it has a target, select it, and try to pull options out
46
- if container.attr('target')
47
- @element.find('#link_target').val(container.attr('target'))
48
-
49
- # if it's a popup window
50
- if container.attr('href') && container.attr('href').indexOf('javascript:void') == 0
51
- href = container.attr('href')
52
- @element.find('#link_external_url').val(href.match(/window.open\('([^']+)',/)[1])
53
- @element.find('#link_target').val('popup')
54
- @element.find('#link_popup_width').val(href.match(/width=(\d+),/)[1])
55
- @element.find('#link_popup_height').val(href.match(/height=(\d+),/)[1])
56
- @element.find('#popup_options').show()
57
-
58
- # get the text content
59
- @element.find('#link_text').val(selection.textContent()) if selection.textContent
60
100
 
61
- # build the link on form submission
62
- @element.find('form').on 'submit', (event) =>
63
- event.preventDefault()
101
+ validateForm: ->
102
+ @clearInputErrors()
103
+
104
+ type = @element.find('input[name=link_type]:checked').val()
105
+
106
+ el = @element.find("#link_#{type}")
107
+ @addInputError(el, "can't be blank") unless el.val()
108
+
109
+ unless @editing
110
+ el = @element.find('#link_text')
111
+ @addInputError(el, "can't be blank") unless el.val()
64
112
 
113
+
114
+ submitForm: ->
65
115
  content = @element.find('#link_text').val()
66
116
  target = @element.find('#link_target').val()
67
117
  type = @element.find('input[name=link_type]:checked').val()
@@ -74,19 +124,20 @@
74
124
  switch target
75
125
  when 'popup'
76
126
  args = {
77
- width: parseInt(@element.find('#link_popup_width').val()) || 500,
78
- height: parseInt(@element.find('#link_popup_height').val()) || 500,
79
- menubar: 'no',
80
- toolbar: 'no'
127
+ width: parseInt(@element.find('#link_popup_width').val()) || 500,
128
+ height: parseInt(@element.find('#link_popup_height').val()) || 500,
129
+ menubar: 'no',
130
+ toolbar: 'no'
81
131
  }
82
132
  attrs['href'] = "javascript:void(window.open('#{attrs['href']}', 'popup_window', '#{jQuery.param(args).replace(/&/g, ',')}'))"
83
- else attrs['target'] = target if target
133
+ else
134
+ attrs['target'] = target if target
84
135
 
85
136
  value = {tagName: 'a', attrs: attrs, content: content}
86
137
 
87
- if existingLink
88
- Mercury.trigger('action', {action: 'replaceLink', value: value, node: existingLink.get(0)})
138
+ if @editing
139
+ Mercury.trigger('action', {action: 'replaceLink', value: value, node: @editing.get(0)})
89
140
  else
90
141
  Mercury.trigger('action', {action: 'insertLink', value: value})
91
142
 
92
- @hide()
143
+ }
@@ -1,46 +1,97 @@
1
- @Mercury.modalHandlers.insertMedia = ->
2
- # make the inputs work with the radio buttons, and options
3
- @element.find('label input').on 'click', ->
4
- jQuery(@).closest('label').next('.selectable').focus()
1
+ @Mercury.modalHandlers.insertMedia = {
5
2
 
6
- @element.find('.selectable').on 'focus', (event) =>
7
- element = jQuery(event.target)
8
- element.prev('label').find('input[type=radio]').prop("checked", true)
3
+ initialize: ->
4
+ @element.find('.control-label input').on('click', @onLabelChecked)
5
+ @element.find('.controls .optional, .controls .required').on('focus', (event) => @onInputFocused($(event.target)))
6
+
7
+ @focus('#media_image_url')
8
+ @initializeForm()
9
+
10
+ # build the image or embed/iframe on form submission
11
+ @element.find('form').on 'submit', (event) =>
12
+ event.preventDefault()
13
+ @validateForm()
14
+ unless @valid
15
+ @resize()
16
+ return
17
+ @submitForm()
18
+ @hide()
9
19
 
10
- @element.find(".media-options").hide()
11
- @element.find("##{element.attr('id').replace('media_', '')}").show()
12
- @resize(true)
13
20
 
14
- # get the selection and initialize its information into the form
15
- if Mercury.region && Mercury.region.selection
21
+ initializeForm: ->
22
+ # get the selection and initialize its information into the form
23
+ return unless Mercury.region && Mercury.region.selection
16
24
  selection = Mercury.region.selection()
17
25
 
18
26
  # if we're editing an image prefill the information
19
- if selection.is && image = selection.is('img')
27
+ if image = selection.is?('img')
20
28
  @element.find('#media_image_url').val(image.attr('src'))
21
29
  @element.find('#media_image_alignment').val(image.attr('align'))
22
- setTimeout((=> @element.find('#media_image_url').focus()), 300)
30
+ @focus('#media_image_url')
23
31
 
24
32
  # if we're editing an iframe (assume it's a video for now)
25
- if selection.is && iframe = selection.is('iframe')
33
+ if iframe = selection.is?('iframe')
26
34
  src = iframe.attr('src')
27
- if src.indexOf('http://www.youtube.com') > -1
35
+ if /^https?:\/\/www.youtube.com\//i.test(src)
28
36
  # it's a youtube video
29
- @element.find('#media_youtube_url').val("http://youtu.be/#{src.match(/\/embed\/(\w+)/)[1]}")
37
+ @element.find('#media_youtube_url').val("#{src.match(/^https?/)[0]}://youtu.be/#{src.match(/\/embed\/(\w+)/)[1]}")
30
38
  @element.find('#media_youtube_width').val(iframe.width())
31
39
  @element.find('#media_youtube_height').val(iframe.height())
32
- setTimeout((=> @element.find('#media_youtube_url').focus()), 300)
33
- else if src.indexOf('http://player.vimeo.com') > -1
40
+ @focus('#media_youtube_url')
41
+ else if /^https?:\/\/player.vimeo.com\//i.test(src)
34
42
  # it's a vimeo video
35
- @element.find('#media_vimeo_url').val("http://vimeo.com/#{src.match(/\/video\/(\w+)/)[1]}")
43
+ @element.find('#media_vimeo_url').val("#{src.match(/^https?/)[0]}://vimeo.com/#{src.match(/\/video\/(\w+)/)[1]}")
36
44
  @element.find('#media_vimeo_width').val(iframe.width())
37
45
  @element.find('#media_vimeo_height').val(iframe.height())
38
- setTimeout((=> @element.find('#media_vimeo_url').focus()), 300)
46
+ @focus('#media_vimeo_url')
47
+
48
+
49
+ focus: (selector) ->
50
+ setTimeout((=> @element.find(selector).focus()), 300)
51
+
52
+
53
+ onLabelChecked: ->
54
+ forInput = jQuery(@).closest('.control-label').attr('for')
55
+ jQuery(@).closest('.control-group').find("##{forInput}").focus()
56
+
57
+
58
+ onInputFocused: (input) ->
59
+ input.closest('.control-group').find('input[type=radio]').prop('checked', true)
60
+
61
+ return if input.closest('.media-options').length
62
+ @element.find(".media-options").hide()
63
+ @element.find("##{input.attr('id').replace('media_', '')}_options").show()
64
+ @resize(true)
65
+
66
+
67
+ addInputError: (input, message) ->
68
+ input.after('<span class="help-inline error-message">' + Mercury.I18n(message) + '</span>').closest('.control-group').addClass('error')
69
+ @valid = false
70
+
71
+
72
+ clearInputErrors: ->
73
+ @element.find('.control-group.error').removeClass('error').find('.error-message').remove()
74
+ @valid = true
75
+
76
+
77
+ validateForm: ->
78
+ @clearInputErrors()
79
+
80
+ type = @element.find('input[name=media_type]:checked').val()
81
+ el = @element.find("#media_#{type}")
82
+
83
+ switch type
84
+ when 'youtube_url'
85
+ url = @element.find('#media_youtube_url').val()
86
+ @addInputError(el, "is invalid") unless /^https?:\/\/youtu.be\//.test(url)
87
+ when 'vimeo_url'
88
+ url = @element.find('#media_vimeo_url').val()
89
+ @addInputError(el, "is invalid") unless /^https?:\/\/vimeo.com\//.test(url)
90
+ else
91
+ @addInputError(el, "can't be blank") unless el.val()
39
92
 
40
93
 
41
- # build the image or youtube embed on form submission
42
- @element.find('form').on 'submit', (event) =>
43
- event.preventDefault()
94
+ submitForm: ->
44
95
  type = @element.find('input[name=media_type]:checked').val()
45
96
 
46
97
  switch type
@@ -51,15 +102,12 @@
51
102
 
52
103
  when 'youtube_url'
53
104
  url = @element.find('#media_youtube_url').val()
54
- unless /^https?:\/\/youtu.be\//.test(url)
55
- Mercury.notify('Error: The provided youtube share url was invalid.')
56
- return
57
105
  code = url.replace(/https?:\/\/youtu.be\//, '')
58
106
  protocol = 'http'
59
107
  protocol = 'https' if /^https:/.test(url)
60
108
  value = jQuery('<iframe>', {
61
- width: @element.find('#media_youtube_width').val() || 560,
62
- height: @element.find('#media_youtube_height').val() || 349,
109
+ width: parseInt(@element.find('#media_youtube_width').val(), 10) || 560,
110
+ height: parseInt(@element.find('#media_youtube_height').val(), 10) || 349,
63
111
  src: "#{protocol}://www.youtube.com/embed/#{code}?wmode=transparent",
64
112
  frameborder: 0,
65
113
  allowfullscreen: 'true'
@@ -68,18 +116,15 @@
68
116
 
69
117
  when 'vimeo_url'
70
118
  url = @element.find('#media_vimeo_url').val()
71
- unless /^https?:\/\/vimeo.com\//.test(url)
72
- Mercury.notify('Error: The provided vimeo url was invalid.')
73
- return
74
119
  code = url.replace(/^https?:\/\/vimeo.com\//, '')
75
120
  protocol = 'http'
76
121
  protocol = 'https' if /^https:/.test(url)
77
122
  value = jQuery('<iframe>', {
78
- width: @element.find('#media_vimeo_width').val() || 400,
79
- height: @element.find('#media_vimeo_height').val() || 225,
123
+ width: parseInt(@element.find('#media_vimeo_width').val(), 10) || 400,
124
+ height: parseInt(@element.find('#media_vimeo_height').val(), 10) || 225,
80
125
  src: "#{protocol}://player.vimeo.com/video/#{code}?title=1&byline=1&portrait=0&color=ffffff",
81
126
  frameborder: 0
82
127
  })
83
128
  Mercury.trigger('action', {action: 'insertHTML', value: value})
84
129
 
85
- @hide()
130
+ }