chr 0.2.1 → 0.2.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +0 -1
- data/Gruntfile.coffee +50 -16
- data/app/assets/javascripts/chr.coffee +9 -16
- data/app/assets/javascripts/chr/core/chr.coffee +38 -20
- data/app/assets/javascripts/chr/core/item.coffee +30 -24
- data/app/assets/javascripts/chr/core/list.coffee +30 -60
- data/app/assets/javascripts/chr/core/list_config.coffee +65 -0
- data/app/assets/javascripts/chr/core/list_pagination.coffee +27 -0
- data/app/assets/javascripts/chr/core/list_reorder.coffee +75 -0
- data/app/assets/javascripts/chr/core/list_search.coffee +41 -0
- data/app/assets/javascripts/chr/core/module.coffee +55 -32
- data/app/assets/javascripts/chr/core/utils.coffee +34 -13
- data/app/assets/javascripts/chr/core/view.coffee +70 -97
- data/app/assets/javascripts/chr/form/form.coffee +63 -49
- data/app/assets/javascripts/chr/form/input-checkbox.coffee +40 -27
- data/app/assets/javascripts/chr/form/input-color.coffee +26 -8
- data/app/assets/javascripts/chr/form/input-date.coffee +0 -0
- data/app/assets/javascripts/chr/form/input-file.coffee +81 -46
- data/app/assets/javascripts/chr/form/input-form.coffee +162 -0
- data/app/assets/javascripts/chr/form/input-form_reorder.coffee +67 -0
- data/app/assets/javascripts/chr/form/input-hidden.coffee +27 -11
- data/app/assets/javascripts/chr/form/input-list.coffee +60 -56
- data/app/assets/javascripts/chr/form/input-list_reorder.coffee +37 -0
- data/app/assets/javascripts/chr/form/input-password.coffee +31 -0
- data/app/assets/javascripts/chr/form/input-select.coffee +61 -35
- data/app/assets/javascripts/chr/form/input-string.coffee +55 -25
- data/app/assets/javascripts/chr/form/input-text.coffee +22 -5
- data/app/assets/javascripts/chr/store/mongosteen-array-store.coffee +1 -1
- data/app/assets/javascripts/chr/vendor/ace.js +18280 -0
- data/app/assets/javascripts/chr/vendor/marked.js +1272 -0
- data/app/assets/javascripts/chr/vendor/mode-html.js +2436 -0
- data/app/assets/javascripts/chr/vendor/mode-markdown.js +2820 -0
- data/app/assets/javascripts/chr/vendor/redactor.fixedtoolbar.js +110 -0
- data/app/assets/javascripts/input-html.coffee +78 -0
- data/app/assets/javascripts/input-markdown.coffee +88 -0
- data/app/assets/javascripts/input-redactor.coffee +66 -0
- data/app/assets/stylesheets/_chr.scss +6 -6
- data/app/assets/stylesheets/_input-redactor.scss +34 -0
- data/app/assets/stylesheets/core/_mixins.scss +75 -0
- data/app/assets/stylesheets/form/_input-checkbox.scss +18 -0
- data/app/assets/stylesheets/form/{_input_color.scss → _input-color.scss} +0 -0
- data/app/assets/stylesheets/form/{_input_file.scss → _input-file.scss} +1 -0
- data/app/assets/stylesheets/form/{_nested_form.scss → _input-form.scss} +0 -0
- data/app/assets/stylesheets/form/{_input_list.scss → _input-list.scss} +0 -0
- data/app/assets/stylesheets/form/_input-string.scss +8 -0
- data/bower.json +3 -2
- data/{app/assets/javascripts/chr-dist.js → dist/chr.js} +1472 -1337
- data/dist/input-ace.js +24936 -0
- data/dist/input-redactor.js +156 -0
- data/lib/chr/version.rb +1 -1
- data/package.json +2 -2
- metadata +29 -13
- data/app/assets/javascripts/chr/core/list-pagination.coffee +0 -26
- data/app/assets/javascripts/chr/core/list-reorder.coffee +0 -70
- data/app/assets/javascripts/chr/core/list-search.coffee +0 -37
- data/app/assets/javascripts/chr/form/nested-form.coffee +0 -164
- data/app/assets/stylesheets/form/_input_checkbox.scss +0 -91
- data/app/assets/stylesheets/form/_input_string.scss +0 -8
@@ -1,39 +1,50 @@
|
|
1
1
|
# -----------------------------------------------------------------------------
|
2
|
-
#
|
3
|
-
#
|
4
|
-
#
|
2
|
+
# Author: Alexander Kravets <alex@slatestudio.com>,
|
3
|
+
# Slate Studio (http://www.slatestudio.com)
|
4
|
+
#
|
5
|
+
# Coding Guide:
|
6
|
+
# https://github.com/thoughtbot/guides/tree/master/style/coffeescript
|
5
7
|
# -----------------------------------------------------------------------------
|
6
8
|
|
7
|
-
|
9
|
+
# -----------------------------------------------------------------------------
|
10
|
+
# FORM
|
11
|
+
# -----------------------------------------------------------------------------
|
12
|
+
# Generates form based on provided configuration schema. If schema is not
|
13
|
+
# provided generates default form based on object keys. This uses Rails
|
14
|
+
# conventions for managing names for attributes, arrays, hashs and nested
|
15
|
+
# objects.
|
16
|
+
#
|
17
|
+
# -----------------------------------------------------------------------------
|
8
18
|
|
9
19
|
class @Form
|
10
20
|
constructor: (@object, @config) ->
|
11
21
|
@groups = []
|
12
22
|
@inputs = {}
|
13
23
|
@$el = $(@config.rootEl || '<form>')
|
14
|
-
@schema = @
|
24
|
+
@schema = @_get_schema()
|
15
25
|
@isRemoved = false
|
16
26
|
|
17
|
-
@
|
18
|
-
@
|
27
|
+
@_build_schema(@schema, @$el)
|
28
|
+
@_add_nested_form_remove_button()
|
19
29
|
|
20
|
-
#
|
21
|
-
# SCHEMA
|
22
|
-
#
|
23
30
|
|
24
|
-
|
31
|
+
# PRIVATE ===============================================
|
32
|
+
|
33
|
+
_get_schema: ->
|
25
34
|
schema = @config.formSchema
|
26
35
|
if @object
|
27
|
-
schema ?= @
|
36
|
+
schema ?= @_generate_default_schema()
|
28
37
|
return schema
|
29
38
|
|
30
|
-
|
39
|
+
|
40
|
+
_generate_default_schema: ->
|
31
41
|
schema = {}
|
32
42
|
for key, value of @object
|
33
|
-
schema[key] = @
|
43
|
+
schema[key] = @_generate_default_input_config(key, value)
|
34
44
|
return schema
|
35
45
|
|
36
|
-
|
46
|
+
|
47
|
+
_generate_default_input_config: (fieldName, value) ->
|
37
48
|
config = {}
|
38
49
|
|
39
50
|
if fieldName[0] == '_'
|
@@ -52,28 +63,29 @@ class @Form
|
|
52
63
|
|
53
64
|
return config
|
54
65
|
|
55
|
-
#
|
56
|
-
# INPUTS
|
57
|
-
#
|
58
66
|
|
59
|
-
|
67
|
+
# INPUTS ================================================
|
68
|
+
|
69
|
+
_build_schema: (schema, $el) ->
|
60
70
|
for fieldName, config of schema
|
61
71
|
if config.type == 'group'
|
62
|
-
group = @
|
72
|
+
group = @_generate_inputs_group(fieldName, config)
|
63
73
|
$el.append group.$el
|
64
74
|
else
|
65
|
-
input = @
|
75
|
+
input = @_generate_input(fieldName, config)
|
66
76
|
$el.append input.$el
|
67
77
|
|
68
|
-
|
78
|
+
|
79
|
+
_generate_inputs_group: (klassName, groupConfig) ->
|
69
80
|
$group =$ """<div class='group #{ klassName }' />"""
|
70
81
|
if groupConfig.inputs
|
71
|
-
@
|
82
|
+
@_build_schema(groupConfig.inputs, $group)
|
72
83
|
group = { $el: $group, klassName: klassName, onInitialize: groupConfig.onInitialize }
|
73
84
|
@groups.push group
|
74
85
|
return group
|
75
86
|
|
76
|
-
|
87
|
+
|
88
|
+
_generate_input: (fieldName, inputConfig) ->
|
77
89
|
if @object
|
78
90
|
value = @object[fieldName]
|
79
91
|
else
|
@@ -82,49 +94,45 @@ class @Form
|
|
82
94
|
value ?= ''
|
83
95
|
|
84
96
|
inputName = inputConfig.name || fieldName
|
85
|
-
input = @
|
97
|
+
input = @_render_input(inputName, inputConfig, value)
|
86
98
|
@inputs[fieldName] = input
|
87
99
|
return input
|
88
100
|
|
89
|
-
|
101
|
+
|
102
|
+
_render_input: (name, config, value) ->
|
90
103
|
inputConfig = $.extend {}, config
|
91
104
|
|
92
|
-
inputConfig.label ?=
|
105
|
+
inputConfig.label ?= name.titleize()
|
93
106
|
inputConfig.type ?= 'string'
|
94
107
|
inputConfig.klass ?= 'stacked'
|
95
108
|
inputConfig.klassName = name
|
96
109
|
|
97
|
-
inputClass =
|
98
|
-
inputClass ?=
|
110
|
+
inputClass = chr.formInputs[inputConfig.type]
|
111
|
+
inputClass ?= chr.formInputs['string']
|
99
112
|
|
100
113
|
inputName = if @config.namePrefix then "#{ @config.namePrefix }[#{ name }]" else "[#{ name }]"
|
101
114
|
|
102
115
|
# add prefix for nested form inputs
|
103
116
|
if inputConfig.type == 'form'
|
104
|
-
inputConfig.namePrefix = inputName.replace("[#{ name }]", "[#{name}_attributes]")
|
117
|
+
inputConfig.namePrefix = inputName.replace("[#{ name }]", "[#{ name }_attributes]")
|
105
118
|
else
|
106
119
|
inputConfig.namePrefix = @config.namePrefix
|
107
120
|
|
108
121
|
return new inputClass(inputName, value, inputConfig, @object)
|
109
122
|
|
110
|
-
_titleizeLabel: (value) ->
|
111
|
-
value.titleize().replace('Id', 'ID')
|
112
123
|
|
113
|
-
#
|
114
|
-
# NESTED
|
115
|
-
#
|
124
|
+
# NESTED ================================================
|
116
125
|
|
117
|
-
|
126
|
+
_add_nested_form_remove_button: ->
|
118
127
|
if @config.removeButton
|
119
|
-
# add
|
128
|
+
# add hidden input to the form
|
120
129
|
fieldName = '_destroy'
|
121
|
-
input = @
|
130
|
+
input = @_render_input(fieldName, { type: 'hidden' }, false)
|
122
131
|
@inputs[fieldName] = input
|
123
132
|
@$el.append input.$el
|
124
|
-
#
|
133
|
+
# remove button
|
125
134
|
@$removeButton =$ """<a href='#' class='nested-form-delete'>Delete</a>"""
|
126
135
|
@$el.append @$removeButton
|
127
|
-
# handle click event
|
128
136
|
@$removeButton.on 'click', (e) =>
|
129
137
|
e.preventDefault()
|
130
138
|
if confirm('Are you sure?')
|
@@ -133,6 +141,7 @@ class @Form
|
|
133
141
|
@isRemoved = true
|
134
142
|
@config.onRemove?(this)
|
135
143
|
|
144
|
+
|
136
145
|
_forms: ->
|
137
146
|
forms = [ @ ]
|
138
147
|
addNestedForms = (form) ->
|
@@ -144,39 +153,41 @@ class @Form
|
|
144
153
|
|
145
154
|
return forms
|
146
155
|
|
147
|
-
|
148
|
-
# PUBLIC
|
149
|
-
#
|
156
|
+
|
157
|
+
# PUBLIC ================================================
|
150
158
|
|
151
159
|
destroy: ->
|
160
|
+
group.destroy?() for group in @groups
|
161
|
+
input.destroy?() for name, input of @inputs
|
152
162
|
@$el.remove()
|
153
163
|
|
164
|
+
|
154
165
|
serialize: (obj={}) ->
|
155
|
-
#
|
166
|
+
# serialize everything except file inputs
|
156
167
|
obj[input.name] = input.value for input in @$el.serializeArray()
|
157
168
|
|
158
169
|
for form in @_forms()
|
159
|
-
#
|
170
|
+
# serialize file inputs for all forms (including nested)
|
160
171
|
for name, input of form.inputs
|
161
172
|
if input.config.type == 'file' or input.config.type == 'image'
|
162
173
|
file = input.$input.get()[0].files[0]
|
163
174
|
obj["__FILE__#{ input.name }"] = file
|
164
|
-
|
165
|
-
# remove flag so carrierwave does not catch _old_ value
|
166
|
-
if not file and not input.filename then obj[input.removeName()] = 'true'
|
175
|
+
if input.isEmpty() then obj[input.removeName()] = 'true'
|
167
176
|
|
168
|
-
#
|
177
|
+
# remove fields with ignoreOnSubmission
|
169
178
|
for name, input of form.inputs
|
170
179
|
if input.config.ignoreOnSubmission
|
171
180
|
delete obj[name]
|
172
181
|
|
173
182
|
return obj
|
174
183
|
|
184
|
+
|
175
185
|
hash: (hash={}) ->
|
176
186
|
for name, input of @inputs
|
177
187
|
input.hash(hash)
|
178
188
|
return hash
|
179
189
|
|
190
|
+
|
180
191
|
initializePlugins: ->
|
181
192
|
for group in @groups
|
182
193
|
group.onInitialize?(@, group)
|
@@ -184,6 +195,7 @@ class @Form
|
|
184
195
|
for name, input of @inputs
|
185
196
|
input.initialize()
|
186
197
|
|
198
|
+
|
187
199
|
showValidationErrors: (errors) ->
|
188
200
|
@hideValidationErrors()
|
189
201
|
for inputName, messages of errors
|
@@ -191,10 +203,12 @@ class @Form
|
|
191
203
|
firstMessage = messages[0]
|
192
204
|
input.showErrorMessage(firstMessage)
|
193
205
|
|
206
|
+
|
194
207
|
hideValidationErrors: ->
|
195
208
|
for inputName, input of @inputs
|
196
209
|
input.hideErrorMessage()
|
197
210
|
|
211
|
+
|
198
212
|
updateValues: (object) ->
|
199
213
|
for name, value of object
|
200
214
|
if @inputs[name]
|
@@ -1,69 +1,82 @@
|
|
1
|
+
# -----------------------------------------------------------------------------
|
2
|
+
# Author: Alexander Kravets <alex@slatestudio.com>,
|
3
|
+
# Slate Studio (http://www.slatestudio.com)
|
4
|
+
#
|
5
|
+
# Coding Guide:
|
6
|
+
# https://github.com/thoughtbot/guides/tree/master/style/coffeescript
|
7
|
+
# -----------------------------------------------------------------------------
|
8
|
+
|
1
9
|
# -----------------------------------------------------------------------------
|
2
10
|
# INPUT CHECKBOX
|
3
11
|
# -----------------------------------------------------------------------------
|
4
12
|
class @InputCheckbox extends InputString
|
5
|
-
|
13
|
+
constructor: (@name, @value, @config, @object) ->
|
14
|
+
@_create_el()
|
15
|
+
@_add_input()
|
16
|
+
@_add_label()
|
17
|
+
|
18
|
+
return this
|
19
|
+
|
20
|
+
|
21
|
+
# PRIVATE ===============================================
|
22
|
+
|
23
|
+
_create_el: ->
|
24
|
+
@$el =$ "<label for='#{ @name }' class='input-#{ @config.type } input-#{ @config.klass } #{ @config.klassName }'>"
|
25
|
+
|
26
|
+
|
27
|
+
_safe_value: ->
|
6
28
|
if not @value or @value == 'false' or @value == 0 or @value == '0'
|
7
29
|
return false
|
8
30
|
else
|
9
31
|
return true
|
10
32
|
|
11
|
-
|
12
|
-
|
13
|
-
#
|
33
|
+
|
34
|
+
_add_input: ->
|
35
|
+
# for boolean checkbox to be serialized correctly we need a hidden false
|
36
|
+
# value which is used by default and overriden by checked value
|
14
37
|
@$false_hidden_input =$ "<input type='hidden' name='#{ @name }' value='false' />"
|
15
38
|
@$el.append @$false_hidden_input
|
16
39
|
|
17
|
-
@$input =$ "<input type='checkbox'
|
40
|
+
@$input =$ "<input type='checkbox' id='#{ @name }' name='#{ @name }' value='true' #{ if @_safe_value() then 'checked' else '' } />"
|
18
41
|
@$el.append @$input
|
19
42
|
|
20
|
-
#
|
21
|
-
# PUBLIC
|
22
|
-
#
|
23
|
-
|
24
|
-
constructor: (@name, @value, @config, @object) ->
|
25
|
-
@$el =$ "<label for='#{ @name }' class='input-#{ @config.type } input-#{ @config.klass } #{ @config.klassName }'>"
|
26
43
|
|
27
|
-
|
28
|
-
@_addLabel()
|
29
|
-
|
30
|
-
return this
|
44
|
+
# PUBLIC ================================================
|
31
45
|
|
32
46
|
updateValue: (@value) ->
|
33
|
-
@$input.prop('checked', @
|
47
|
+
@$input.prop('checked', @_safe_value())
|
48
|
+
|
34
49
|
|
35
50
|
hash: (hash={}) ->
|
36
51
|
hash[@config.klassName] = @$input.prop('checked')
|
37
52
|
return hash
|
38
53
|
|
39
|
-
|
54
|
+
|
55
|
+
chr.formInputs['checkbox'] = InputCheckbox
|
56
|
+
|
40
57
|
|
41
58
|
# -----------------------------------------------------------------------------
|
42
59
|
# INPUT CHECKBOX SWITCH
|
43
60
|
# -----------------------------------------------------------------------------
|
44
61
|
class @InputCheckboxSwitch extends InputCheckbox
|
45
|
-
|
62
|
+
|
63
|
+
# PRIVATE ===============================================
|
64
|
+
|
65
|
+
_add_input: ->
|
46
66
|
@$switch =$ "<div class='switch'>"
|
47
67
|
@$el.append @$switch
|
48
68
|
|
49
69
|
@$false_hidden_input =$ "<input type='hidden' name='#{ @name }' value='false' />"
|
50
70
|
@$switch.append @$false_hidden_input
|
51
71
|
|
52
|
-
@$input =$ "<input type='checkbox'
|
72
|
+
@$input =$ "<input type='checkbox' id='#{ @name }' name='#{ @name }' value='true' #{ if @_safe_value() then 'checked' else '' } />"
|
53
73
|
@$switch.append @$input
|
54
74
|
|
55
75
|
@$checkbox =$ "<div class='checkbox'>"
|
56
76
|
@$switch.append @$checkbox
|
57
77
|
|
58
|
-
constructor: (@name, @value, @config, @object) ->
|
59
|
-
@$el =$ "<label for='#{ @name }' class='input-#{ @config.type } input-#{ @config.klass } #{ @config.klassName }'>"
|
60
|
-
|
61
|
-
@_addLabel()
|
62
|
-
@_addInput()
|
63
|
-
|
64
|
-
return this
|
65
78
|
|
66
|
-
|
79
|
+
chr.formInputs['switch'] = InputCheckboxSwitch
|
67
80
|
|
68
81
|
|
69
82
|
|
@@ -1,33 +1,51 @@
|
|
1
|
+
# -----------------------------------------------------------------------------
|
2
|
+
# Author: Alexander Kravets <alex@slatestudio.com>,
|
3
|
+
# Slate Studio (http://www.slatestudio.com)
|
4
|
+
#
|
5
|
+
# Coding Guide:
|
6
|
+
# https://github.com/thoughtbot/guides/tree/master/style/coffeescript
|
7
|
+
# -----------------------------------------------------------------------------
|
8
|
+
|
1
9
|
# -----------------------------------------------------------------------------
|
2
10
|
# INPUT COLOR
|
3
11
|
# -----------------------------------------------------------------------------
|
4
12
|
class @InputColor extends InputString
|
5
|
-
|
13
|
+
|
14
|
+
# PRIVATE ===============================================
|
15
|
+
|
16
|
+
_add_color_preview: ->
|
6
17
|
@$colorPreview =$ "<div class='preview'>"
|
7
18
|
@$el.append @$colorPreview
|
8
19
|
|
9
|
-
|
20
|
+
|
21
|
+
_update_color_preview: ->
|
10
22
|
@$colorPreview.css { 'background-color': "##{ @$input.val() }" }
|
11
23
|
|
12
|
-
|
24
|
+
|
25
|
+
_validate_input_value: ->
|
13
26
|
if (/^(?:[0-9a-f]{3}){1,2}$/i).test(@$input.val())
|
14
27
|
@hideErrorMessage()
|
15
28
|
else
|
16
29
|
@showErrorMessage('Invalid hex value')
|
17
30
|
|
31
|
+
|
32
|
+
# PUBLIC ================================================
|
33
|
+
|
18
34
|
initialize: ->
|
19
|
-
@
|
20
|
-
|
35
|
+
@$input.attr('placeholder', @config.placeholder || 'e.g. #eee')
|
36
|
+
|
37
|
+
@_add_color_preview()
|
38
|
+
@_update_color_preview()
|
21
39
|
|
22
40
|
@$input.on 'change keyup', (e) =>
|
23
41
|
@hideErrorMessage()
|
24
|
-
@
|
25
|
-
@
|
42
|
+
@_validate_input_value()
|
43
|
+
@_update_color_preview()
|
26
44
|
|
27
45
|
@config.onInitialize?(this)
|
28
46
|
|
29
47
|
|
30
|
-
|
48
|
+
chr.formInputs['color'] = InputColor
|
31
49
|
|
32
50
|
|
33
51
|
|
File without changes
|
@@ -1,81 +1,116 @@
|
|
1
|
+
# -----------------------------------------------------------------------------
|
2
|
+
# Author: Alexander Kravets <alex@slatestudio.com>,
|
3
|
+
# Slate Studio (http://www.slatestudio.com)
|
4
|
+
#
|
5
|
+
# Coding Guide:
|
6
|
+
# https://github.com/thoughtbot/guides/tree/master/style/coffeescript
|
7
|
+
# -----------------------------------------------------------------------------
|
8
|
+
|
1
9
|
# -----------------------------------------------------------------------------
|
2
10
|
# INPUT FILE
|
3
11
|
# -----------------------------------------------------------------------------
|
4
12
|
class @InputFile extends InputString
|
5
|
-
|
6
|
-
|
7
|
-
if @filename
|
8
|
-
@$link =$ "<a href='#{ @value.url }' target='_blank' title='#{ @filename }'>#{ @filename }</a>"
|
9
|
-
@$el.append @$link
|
10
|
-
@$el.removeClass 'empty'
|
13
|
+
constructor: (@name, @value, @config, @object) ->
|
14
|
+
@_create_el()
|
11
15
|
|
12
|
-
|
16
|
+
@_add_label()
|
17
|
+
@_add_input()
|
18
|
+
@_update_state()
|
19
|
+
|
20
|
+
return this
|
21
|
+
|
22
|
+
|
23
|
+
# PRIVATE ===============================================
|
24
|
+
|
25
|
+
_create_el: ->
|
26
|
+
@$el =$ "<div class='input-#{ @config.type } input-#{ @config.klass } #{ @config.klassName }'>"
|
27
|
+
|
28
|
+
|
29
|
+
_add_input: ->
|
30
|
+
@$link =$ "<a href='#' target='_blank' title=''></a>"
|
31
|
+
@$el.append(@$link)
|
32
|
+
|
33
|
+
@$input =$ "<input type='file' name='#{ @name }' id='#{ @name }'>"
|
13
34
|
@$el.append @$input
|
14
35
|
|
15
|
-
|
16
|
-
# NOTE: this is Rails (CarrierWave) approach to remove files, might not be
|
17
|
-
# generic, so we should consider to move it to store.
|
18
|
-
if @filename
|
19
|
-
removeInputName = @removeName()
|
36
|
+
@_add_remove_checkbox()
|
20
37
|
|
21
|
-
@$removeLabel =$ "<label for='#{ removeInputName }'>Remove</label>"
|
22
|
-
@$link.after @$removeLabel
|
23
38
|
|
24
|
-
|
25
|
-
|
39
|
+
_add_remove_checkbox: ->
|
40
|
+
removeInputName = @removeName()
|
41
|
+
@$removeLabel =$ "<label for='#{ removeInputName }'>Remove</label>"
|
42
|
+
@$hiddenRemoveInput =$ "<input type='hidden' name='#{ removeInputName }' value='false'>"
|
43
|
+
@$removeInput =$ "<input type='checkbox' name='#{ removeInputName }' id='#{ removeInputName }' value='true'>"
|
44
|
+
@$link.after(@$removeLabel)
|
45
|
+
@$link.after(@$removeInput)
|
46
|
+
@$link.after(@$hiddenRemoveInput)
|
26
47
|
|
27
|
-
@$link.after @$removeInput
|
28
|
-
@$link.after @$hiddenRemoveInput
|
29
48
|
|
30
|
-
|
31
|
-
@$
|
49
|
+
_update_inputs: ->
|
50
|
+
@$link.html(@filename).attr('title', @filename).attr('href', @value.url)
|
51
|
+
|
52
|
+
|
53
|
+
_update_state: (@filename=null) ->
|
54
|
+
@$input.val('')
|
55
|
+
@$removeInput.prop('checked', false)
|
32
56
|
|
33
|
-
# NOTE: carrierwave filename workaround
|
34
|
-
@filename = null
|
35
57
|
if @value.url
|
36
58
|
@filename = _last(@value.url.split('/'))
|
37
|
-
if @filename == '_old_' then @filename = null
|
59
|
+
if @filename == '_old_' then @filename = null # carrierwave filename workaround
|
60
|
+
|
61
|
+
if @filename
|
62
|
+
@$el.removeClass('empty')
|
63
|
+
@_update_inputs()
|
64
|
+
else
|
65
|
+
@$el.addClass('empty')
|
38
66
|
|
39
|
-
@_addLabel()
|
40
|
-
@_addInput()
|
41
|
-
@_addRemoveCheckbox()
|
42
67
|
|
43
|
-
|
68
|
+
# PUBLIC ================================================
|
69
|
+
|
70
|
+
# when no file uploaded and no file selected, send remove flag so
|
71
|
+
# carrierwave does not catch _old_ value
|
72
|
+
isEmpty: ->
|
73
|
+
( ! @$input.get()[0].files[0] && ! @filename )
|
74
|
+
|
44
75
|
|
45
|
-
removeName: ->
|
76
|
+
removeName: ->
|
77
|
+
@name.reverse().replace('[', '[remove_'.reverse()).reverse()
|
46
78
|
|
47
|
-
updateValue: (@value) ->
|
48
|
-
# TODO: this method required to enable version switch for objects history
|
49
79
|
|
80
|
+
updateValue: (@value, @object) ->
|
81
|
+
@_update_state()
|
82
|
+
|
83
|
+
|
84
|
+
chr.formInputs['file'] = InputFile
|
50
85
|
|
51
|
-
_chrFormInputs['file'] = InputFile
|
52
86
|
|
53
87
|
# -----------------------------------------------------------------------------
|
54
88
|
# INPUT FILE IMAGE
|
55
89
|
# -----------------------------------------------------------------------------
|
90
|
+
# Config options:
|
91
|
+
# thumbnail(object) - method that returns thumbnail for input
|
92
|
+
# -----------------------------------------------------------------------------
|
56
93
|
class @InputFileImage extends InputFile
|
57
|
-
|
58
|
-
@$
|
59
|
-
|
60
|
-
@$link =$ "<a href='#{ @value.url }' target='_blank' title='#{ @filename }'>#{ @filename }</a>"
|
61
|
-
@$el.append @$link
|
94
|
+
_add_input: ->
|
95
|
+
@$link =$ "<a href='#' target='_blank' title=''></a>"
|
96
|
+
@$el.append @$link
|
62
97
|
|
63
|
-
|
64
|
-
|
98
|
+
@$thumb =$ "<img src='' />"
|
99
|
+
@$el.append @$thumb
|
65
100
|
|
66
|
-
|
67
|
-
|
101
|
+
@$input =$ "<input type='file' name='#{ @name }' id='#{ @name }' />"
|
102
|
+
@$el.append @$input
|
68
103
|
|
69
|
-
|
70
|
-
@$el.append @$thumb
|
104
|
+
@_add_remove_checkbox()
|
71
105
|
|
72
|
-
@$el.removeClass 'empty'
|
73
106
|
|
74
|
-
|
75
|
-
@$
|
107
|
+
_update_inputs: ->
|
108
|
+
@$link.html(@filename).attr('title', @filename).attr('href', @value.url)
|
109
|
+
image_thumb_url = if @config.thumbnail then @config.thumbnail(@object) else @value.url
|
110
|
+
@$thumb.attr('src', image_thumb_url).attr('alt', @filename)
|
76
111
|
|
77
112
|
|
78
|
-
|
113
|
+
chr.formInputs['image'] = InputFileImage
|
79
114
|
|
80
115
|
|
81
116
|
|