formagic 0.3.4 → 0.3.7

Sign up to get free protection for your applications and to get access to all the features.
Files changed (41) hide show
  1. checksums.yaml +4 -4
  2. data/Gemfile.lock +2 -2
  3. data/app/assets/javascripts/formagic.coffee +2 -0
  4. data/app/assets/javascripts/formagic/form.coffee +9 -39
  5. data/app/assets/javascripts/formagic/group.coffee +1 -11
  6. data/app/assets/javascripts/formagic/inputs/array.coffee +8 -26
  7. data/app/assets/javascripts/formagic/inputs/checkbox.coffee +3 -20
  8. data/app/assets/javascripts/formagic/inputs/color.coffee +2 -16
  9. data/app/assets/javascripts/formagic/inputs/date.coffee +6 -24
  10. data/app/assets/javascripts/formagic/inputs/datetime.coffee +6 -27
  11. data/app/assets/javascripts/formagic/inputs/document.coffee +9 -25
  12. data/app/assets/javascripts/formagic/inputs/documents.coffee +11 -32
  13. data/app/assets/javascripts/formagic/inputs/documents_reorder.coffee +1 -12
  14. data/app/assets/javascripts/formagic/inputs/file.coffee +2 -22
  15. data/app/assets/javascripts/formagic/inputs/hash.coffee +1 -4
  16. data/app/assets/javascripts/formagic/inputs/hidden.coffee +2 -19
  17. data/app/assets/javascripts/formagic/inputs/html.coffee +2 -18
  18. data/app/assets/javascripts/formagic/inputs/image.coffee +3 -2
  19. data/app/assets/javascripts/formagic/inputs/list.coffee +2 -21
  20. data/app/assets/javascripts/formagic/inputs/list_reorder.coffee +1 -12
  21. data/app/assets/javascripts/formagic/inputs/list_typeahead.coffee +1 -13
  22. data/app/assets/javascripts/formagic/inputs/markdown.coffee +2 -18
  23. data/app/assets/javascripts/formagic/inputs/password.coffee +2 -14
  24. data/app/assets/javascripts/formagic/inputs/redactor.coffee +2 -18
  25. data/app/assets/javascripts/formagic/inputs/redactor_character.coffee +1 -13
  26. data/app/assets/javascripts/formagic/inputs/redactor_images.coffee +0 -13
  27. data/app/assets/javascripts/formagic/inputs/select.coffee +2 -21
  28. data/app/assets/javascripts/formagic/inputs/select2.coffee +0 -13
  29. data/app/assets/javascripts/formagic/inputs/select2_multiple.coffee +90 -0
  30. data/app/assets/javascripts/formagic/inputs/select2_single.coffee +57 -0
  31. data/app/assets/javascripts/formagic/inputs/string.coffee +13 -34
  32. data/app/assets/javascripts/formagic/inputs/text.coffee +2 -13
  33. data/app/assets/javascripts/formagic/inputs/url.coffee +3 -12
  34. data/app/assets/stylesheets/formagic.scss +2 -0
  35. data/app/assets/stylesheets/formagic/actions.scss +9 -0
  36. data/app/assets/stylesheets/formagic/form.scss +16 -24
  37. data/app/assets/stylesheets/formagic/nested-form.scss +3 -9
  38. data/app/assets/stylesheets/formagic/select.scss +24 -5
  39. data/app/assets/stylesheets/formagic/url.scss +5 -0
  40. data/lib/formagic/version.rb +1 -1
  41. metadata +6 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: aa7cb7f17971ebb1b61ef50c143718be75dea339
4
- data.tar.gz: 8a5d957fe67c1fe2814755ead538a0bb1a6cd45f
3
+ metadata.gz: d5205dd5e3ddafa7276e8a5161583b6e333b617b
4
+ data.tar.gz: d907a83d3a27bd871fc63daac5652df3b5462a81
5
5
  SHA512:
6
- metadata.gz: 8d2a1265776cf8ce993c42de3922bd28970d247a95bbe9bf4279261a49284ab77778eed58a945f1cb20257fdc18135f5372ed51b4820afab62aadd9c525657fc
7
- data.tar.gz: 46f52e276d4bb925dad2169e0d47098a19d16c9624743d948701750e9f651ec6a4df797c5ff1cdeee214029d5273d1b777f504f0a14a042a8784161d97750206
6
+ metadata.gz: a72c34af4b24689d63558c71486fbab97dc6c2f9fb63e87abf4e5180a51a52ac4c2f86c443cea35ea4905399f66a02bad293a70b9bfc0869e0bd6e9b58434912
7
+ data.tar.gz: d6284b2fdfa70629ef52a90e1fdf50df8d366057a42d92cb3fb81555c6226e4449bec5f9c134a9ea362c1c48168afe1a2eea928c4f8317cd2dcd63c248e24dcc
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- formagic (0.3.4)
4
+ formagic (0.3.7)
5
5
  bourbon (>= 3.2)
6
6
  normalize-rails (>= 3.0)
7
7
 
@@ -13,7 +13,7 @@ GEM
13
13
  thor (~> 0.19)
14
14
  normalize-rails (3.0.3)
15
15
  rake (10.4.2)
16
- sass (3.4.19)
16
+ sass (3.4.20)
17
17
  thor (0.19.1)
18
18
 
19
19
  PLATFORMS
@@ -16,6 +16,8 @@
16
16
  #= require ./formagic/inputs/password
17
17
  #= require ./formagic/inputs/select
18
18
  #= require ./formagic/inputs/select2
19
+ #= require ./formagic/inputs/select2_single
20
+ #= require ./formagic/inputs/select2_multiple
19
21
  #= require ./formagic/inputs/time
20
22
  #= require ./formagic/inputs/url
21
23
  #= require ./formagic/inputs/array
@@ -1,35 +1,27 @@
1
1
  # -----------------------------------------------------------------------------
2
2
  # Author: Alexander Kravets <alex@slatestudio.com>,
3
3
  # Slate Studio (http://www.slatestudio.com)
4
- #
5
- # Coding Guide:
6
- # https://github.com/thoughtbot/guides/tree/master/style/coffeescript
7
- # -----------------------------------------------------------------------------
8
-
9
4
  # -----------------------------------------------------------------------------
10
5
  # FORM
11
6
  # -----------------------------------------------------------------------------
12
- #
13
7
  # Generates form based on provided configuration schema. If schema is not
14
8
  # provided generates default form based on object keys. This uses Rails
15
9
  # conventions for managing names for attributes, arrays, hashs and nested
16
10
  # objects.
17
- #
18
11
  # -----------------------------------------------------------------------------
19
-
20
12
  class @Form
21
13
  constructor: (@object, @config) ->
22
14
  @groups = []
23
15
  @inputs = {}
24
- @$el = $(@config.rootEl || "<form class='form'>")
16
+ defaultEl = "<form class='form' onsubmit='return false;'>"
17
+ @$el = $(@config.rootEl || defaultEl)
25
18
  @schema = @_get_schema()
26
19
  @isRemoved = false
27
20
 
28
21
  @_build_schema(@schema, @$el)
29
22
  @_add_nested_form_remove_button()
30
23
 
31
-
32
- # PRIVATE ===============================================
24
+ # PRIVATE ===================================================================
33
25
 
34
26
  _get_schema: ->
35
27
  schema = @config.formSchema
@@ -37,14 +29,12 @@ class @Form
37
29
  schema ?= @_generate_default_schema()
38
30
  return schema
39
31
 
40
-
41
32
  _generate_default_schema: ->
42
33
  schema = {}
43
34
  for key, value of @object
44
35
  schema[key] = @_generate_default_input_config(key, value)
45
36
  return schema
46
37
 
47
-
48
38
  _generate_default_input_config: (fieldName, value) ->
49
39
  config = {}
50
40
 
@@ -64,8 +54,7 @@ class @Form
64
54
 
65
55
  return config
66
56
 
67
-
68
- # INPUTS ================================================
57
+ # INPUTS ====================================================================
69
58
 
70
59
  _build_schema: (schema, $el) ->
71
60
  for fieldName, config of schema
@@ -79,7 +68,6 @@ class @Form
79
68
  input = @_generate_input(fieldName, config)
80
69
  $el.append input.$el
81
70
 
82
-
83
71
  _generate_inputs_group: (klassName, groupConfig) ->
84
72
  $group =$ """<div class='group #{ klassName }' />"""
85
73
 
@@ -100,7 +88,6 @@ class @Form
100
88
 
101
89
  return group
102
90
 
103
-
104
91
  _generate_input: (fieldName, inputConfig) ->
105
92
  if @object
106
93
  value = @object[fieldName]
@@ -116,7 +103,6 @@ class @Form
116
103
 
117
104
  return input
118
105
 
119
-
120
106
  _render_input: (name, config, value) ->
121
107
  inputConfig = $.extend {}, config
122
108
 
@@ -144,8 +130,7 @@ class @Form
144
130
 
145
131
  return new inputClass(inputName, value, inputConfig, @object)
146
132
 
147
-
148
- # NESTED ================================================
133
+ # NESTED ====================================================================
149
134
 
150
135
  _add_nested_form_remove_button: ->
151
136
  if @config.removeButton
@@ -171,7 +156,6 @@ class @Form
171
156
  @isRemoved = true
172
157
  @config.onRemove?(this)
173
158
 
174
-
175
159
  _forms: ->
176
160
  forms = [ @ ]
177
161
 
@@ -187,15 +171,13 @@ class @Form
187
171
 
188
172
  return forms
189
173
 
190
-
191
- # PUBLIC ================================================
174
+ # PUBLIC ====================================================================
192
175
 
193
176
  destroy: ->
194
177
  group.destroy?() for group in @groups
195
178
  input.destroy?() for name, input of @inputs
196
179
  @$el.remove()
197
180
 
198
-
199
181
  serialize: (obj={}) ->
200
182
  # serialize everything except file inputs
201
183
  obj[input.name] = input.value for input in @$el.serializeArray()
@@ -216,18 +198,13 @@ class @Form
216
198
  if input.config.ignoreOnSubmission
217
199
  delete obj[input.name]
218
200
 
219
- # for k, v of obj
220
- # console.log k
221
-
222
201
  return obj
223
202
 
224
-
225
203
  hash: (hash={}) ->
226
204
  for name, input of @inputs
227
205
  input.hash(hash)
228
206
  return hash
229
207
 
230
-
231
208
  initializePlugins: ->
232
209
  for group in @groups
233
210
  group.onInitialize?(@, group)
@@ -235,7 +212,6 @@ class @Form
235
212
  for name, input of @inputs
236
213
  input.initialize()
237
214
 
238
-
239
215
  showValidationErrors: (errors) ->
240
216
  @hideValidationErrors()
241
217
  for inputName, messages of errors
@@ -251,17 +227,11 @@ class @Form
251
227
  firstMessage = messages[0]
252
228
  input.showErrorMessage(firstMessage)
253
229
 
254
-
255
230
  hideValidationErrors: ->
256
231
  for inputName, input of @inputs
257
232
  input.hideErrorMessage()
258
233
 
259
-
260
- updateValues: (object) ->
261
- for name, value of object
234
+ updateValues: (@object) ->
235
+ for name, value of @object
262
236
  if @inputs[name]
263
- @inputs[name].updateValue(value, object)
264
-
265
-
266
-
267
-
237
+ @inputs[name].updateValue(value, @object)
@@ -1,19 +1,12 @@
1
1
  # -----------------------------------------------------------------------------
2
2
  # Author: Alexander Kravets <alex@slatestudio.com>,
3
3
  # Slate Studio (http://www.slatestudio.com)
4
- #
5
- # Coding Guide:
6
- # https://github.com/thoughtbot/guides/tree/master/style/coffeescript
7
- # -----------------------------------------------------------------------------
8
-
9
4
  # -----------------------------------------------------------------------------
10
5
  # EXPANDABLE GROUP
11
6
  # -----------------------------------------------------------------------------
12
- #
7
+ # TODO: refactor this to be used with group-panel
13
8
  # Usage: onInitialize: (form, group) -> new ExpandableGroup(form, group, 'Details')
14
- #
15
9
  # -----------------------------------------------------------------------------
16
-
17
10
  @_expandableGroupStateCache = {}
18
11
 
19
12
  class @ExpandableGroup
@@ -28,7 +21,6 @@ class @ExpandableGroup
28
21
  @_cacheExpanderState()
29
22
  e.preventDefault()
30
23
 
31
-
32
24
  _restoreExpanderFromCache: ->
33
25
  if _expandableGroupStateCache.__hash
34
26
 
@@ -39,12 +31,10 @@ class @ExpandableGroup
39
31
  if _expandableGroupStateCache.__hash.endsWith 'new'
40
32
  @$expander.removeClass 'hidden'
41
33
 
42
-
43
34
  _cacheExpanderState: ->
44
35
  _expandableGroupStateCache.__hash = window.location.hash
45
36
  _expandableGroupStateCache[@_groupId()] = @group.$el.is(':visible')
46
37
 
47
-
48
38
  _groupId: ->
49
39
  groupIndex = $('form').find(".group.#{ @group.klassName }").index(@group.$el)
50
40
  return "#{ @group.klassName }-#{ groupIndex }"
@@ -1,11 +1,6 @@
1
1
  # -----------------------------------------------------------------------------
2
2
  # Author: Alexander Kravets <alex@slatestudio.com>,
3
3
  # Slate Studio (http://www.slatestudio.com)
4
- #
5
- # Coding Guide:
6
- # https://github.com/thoughtbot/guides/tree/master/style/coffeescript
7
- # -----------------------------------------------------------------------------
8
-
9
4
  # -----------------------------------------------------------------------------
10
5
  # INPUT ARRAY
11
6
  # -----------------------------------------------------------------------------
@@ -21,14 +16,17 @@
21
16
  #= require formagic/inputs/list_reorder
22
17
  #
23
18
  # -----------------------------------------------------------------------------
24
-
25
19
  class @InputArray extends InputString
26
- # PRIVATE ===============================================
20
+
21
+ # PRIVATE ===================================================================
27
22
 
28
23
  _add_input: ->
29
24
  # hidden input that stores ids, we use __LIST__ prefix to identify
30
25
  # ARRAY input type and process it's value while form submission.
31
- name = if @config.namePrefix then "#{ @config.namePrefix }[__LIST__#{ @config.klassName }]" else "[__LIST__#{ @config.klassName }]"
26
+ name = if @config.namePrefix
27
+ "#{ @config.namePrefix }[__LIST__#{ @config.klassName }]"
28
+ else
29
+ "[__LIST__#{ @config.klassName }]"
32
30
 
33
31
  @$input =$ "<input type='hidden' name='#{ name }' value='' />"
34
32
  @$el.append @$input
@@ -44,23 +42,19 @@ class @InputArray extends InputString
44
42
  @_render_items()
45
43
  @_update_input_value()
46
44
 
47
-
48
45
  _values: ->
49
46
  @$items.children('li').map((i, el) -> $(el).data('value')).get()
50
47
 
51
-
52
48
  _update_input_value: ->
53
49
  input_value = @_values().join('|||')
54
50
 
55
51
  @$input.val(input_value)
56
52
  @$input.trigger('change')
57
53
 
58
-
59
54
  _remove_item: ($el) ->
60
55
  $el.parent().remove()
61
56
  @_update_input_value()
62
57
 
63
-
64
58
  _render_items: ->
65
59
  @$items.html('')
66
60
 
@@ -71,7 +65,6 @@ class @InputArray extends InputString
71
65
  for v in @value
72
66
  @_render_item(v)
73
67
 
74
-
75
68
  _render_item: (o) ->
76
69
  value = _escapeHtml(o)
77
70
  listItem =$ """<li data-value='#{ value }'>
@@ -86,21 +79,18 @@ class @InputArray extends InputString
86
79
 
87
80
  @_update_input_value()
88
81
 
89
-
90
82
  _create_string_input_el: (placeholder) ->
91
83
  @$stringInput =$ "<input type='text' placeholder='#{ placeholder }' />"
92
84
  @$el.append @$stringInput
93
85
 
94
-
95
86
  _bind_string_input: ->
96
- @$stringInput.on 'keyup', (e) =>
87
+ @$stringInput.on 'keydown', (e) =>
97
88
  if e.keyCode == 13
98
89
  val = $(e.currentTarget).val()
99
90
  @_render_item(val)
100
91
  $(e.currentTarget).val('')
101
92
 
102
-
103
- # PUBLIC ================================================
93
+ # PUBLIC ====================================================================
104
94
 
105
95
  initialize: ->
106
96
  @config.beforeInitialize?(this)
@@ -117,21 +107,13 @@ class @InputArray extends InputString
117
107
 
118
108
  @config.onInitialize?(this)
119
109
 
120
-
121
110
  updateValue: (@value) ->
122
111
  @_render_items()
123
112
 
124
-
125
113
  hash: (hash={}) ->
126
114
  hash[@config.klassName] = @_values()
127
115
  return hash
128
116
 
129
-
130
117
  include(InputArray, inputListReorder)
131
118
 
132
-
133
119
  chr.formInputs['array'] = InputArray
134
-
135
-
136
-
137
-
@@ -1,11 +1,6 @@
1
1
  # -----------------------------------------------------------------------------
2
2
  # Author: Alexander Kravets <alex@slatestudio.com>,
3
3
  # Slate Studio (http://www.slatestudio.com)
4
- #
5
- # Coding Guide:
6
- # https://github.com/thoughtbot/guides/tree/master/style/coffeescript
7
- # -----------------------------------------------------------------------------
8
-
9
4
  # -----------------------------------------------------------------------------
10
5
  # INPUT CHECKBOX
11
6
  # -----------------------------------------------------------------------------
@@ -18,20 +13,17 @@ class @InputCheckbox extends InputString
18
13
 
19
14
  return this
20
15
 
21
-
22
- # PRIVATE ===============================================
16
+ # PRIVATE ===================================================================
23
17
 
24
18
  _create_el: ->
25
19
  @$el =$ "<label for='#{ @name }' class='form-input input-#{ @config.type } input-#{ @config.klassName }'>"
26
20
 
27
-
28
21
  _safe_value: ->
29
22
  if not @value or @value == 'false' or @value == 0 or @value == '0'
30
23
  return false
31
24
  else
32
25
  return true
33
26
 
34
-
35
27
  _add_input: ->
36
28
  # for boolean checkbox to be serialized correctly we need a hidden false
37
29
  # value which is used by default and overriden by checked value
@@ -41,27 +33,23 @@ class @InputCheckbox extends InputString
41
33
  @$input =$ "<input type='checkbox' id='#{ @name }' name='#{ @name }' value='true' #{ if @_safe_value() then 'checked' else '' } />"
42
34
  @$el.append @$input
43
35
 
44
-
45
- # PUBLIC ================================================
36
+ # PUBLIC ====================================================================
46
37
 
47
38
  updateValue: (@value) ->
48
39
  @$input.prop('checked', @_safe_value())
49
40
 
50
-
51
41
  hash: (hash={}) ->
52
42
  hash[@config.klassName] = @$input.prop('checked')
53
43
  return hash
54
44
 
55
-
56
45
  chr.formInputs['checkbox'] = InputCheckbox
57
46
 
58
-
59
47
  # -----------------------------------------------------------------------------
60
48
  # INPUT CHECKBOX SWITCH
61
49
  # -----------------------------------------------------------------------------
62
50
  class @InputCheckboxSwitch extends InputCheckbox
63
51
 
64
- # PRIVATE ===============================================
52
+ # PRIVATE ===================================================================
65
53
 
66
54
  _add_input: ->
67
55
  @$switch =$ "<div class='switch'>"
@@ -76,9 +64,4 @@ class @InputCheckboxSwitch extends InputCheckbox
76
64
  @$checkbox =$ "<div class='checkbox'>"
77
65
  @$switch.append @$checkbox
78
66
 
79
-
80
67
  chr.formInputs['switch'] = InputCheckboxSwitch
81
-
82
-
83
-
84
-
@@ -1,35 +1,27 @@
1
1
  # -----------------------------------------------------------------------------
2
2
  # Author: Alexander Kravets <alex@slatestudio.com>,
3
3
  # Slate Studio (http://www.slatestudio.com)
4
- #
5
- # Coding Guide:
6
- # https://github.com/thoughtbot/guides/tree/master/style/coffeescript
7
- # -----------------------------------------------------------------------------
8
-
9
4
  # -----------------------------------------------------------------------------
10
5
  # INPUT COLOR
11
6
  # -----------------------------------------------------------------------------
12
7
  class @InputColor extends InputString
13
8
 
14
- # PRIVATE ===============================================
9
+ # PRIVATE ===================================================================
15
10
 
16
11
  _add_color_preview: ->
17
12
  @$colorPreview =$ "<div class='preview'>"
18
13
  @$el.append @$colorPreview
19
14
 
20
-
21
15
  _update_color_preview: ->
22
16
  @$colorPreview.css { 'background-color': "##{ @$input.val() }" }
23
17
 
24
-
25
18
  _validate_input_value: ->
26
19
  if (/^(?:[0-9a-f]{3}){1,2}$/i).test(@$input.val())
27
20
  @hideErrorMessage()
28
21
  else
29
22
  @showErrorMessage('Invalid hex value')
30
23
 
31
-
32
- # PUBLIC ================================================
24
+ # PUBLIC ====================================================================
33
25
 
34
26
  initialize: ->
35
27
  @config.beforeInitialize?(this)
@@ -46,10 +38,4 @@ class @InputColor extends InputString
46
38
 
47
39
  @config.onInitialize?(this)
48
40
 
49
-
50
41
  chr.formInputs['color'] = InputColor
51
-
52
-
53
-
54
-
55
-