/
ra.widgets.coffee
360 lines (303 loc) · 13.6 KB
/
ra.widgets.coffee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
$(document).on 'rails_admin.dom_ready', (e, content) ->
content = if content then content else $('form')
if content.length # don't waste time otherwise
# colorpicker
content.find('[data-color]').each ->
that = this
$(this).ColorPicker
color: $(that).val()
onShow: (el) ->
$(el).fadeIn(500)
false
onHide: (el) ->
$(el).fadeOut(500)
false
onChange: (hsb, hex, rgb) ->
$(that).val(hex)
$(that).css('backgroundColor', '#' + hex)
# datetime picker
$.fn.datetimepicker.defaults.icons =
time: 'fa fa-clock-o'
date: 'fa fa-calendar'
up: 'fa fa-chevron-up'
down: 'fa fa-chevron-down'
previous: 'fa fa-angle-double-left'
next: 'fa fa-angle-double-right'
today: 'fa fa-dot-circle-o'
clear: 'fa fa-trash'
close: 'fa fa-times'
content.find('[data-datetimepicker]').each ->
options = $(this).data('options')
$.extend(options, {locale: RailsAdmin.I18n.locale})
$(this).datetimepicker options
# enumeration
content.find('[data-enumeration]').each ->
if $(this).is('[multiple]')
$(this).filteringMultiselect $(this).data('options')
else
$(this).filteringSelect $(this).data('options')
# fileupload
content.find('[data-fileupload]').each ->
parent = $(this).closest('.controls')
parent.find('.btn-remove-image').on 'click', ->
$(this).siblings('[type=checkbox]').click()
parent.find('.toggle').toggle('slow')
$(this).toggleClass('btn-danger btn-info')
false
# fileupload-preview
content.find('[data-fileupload]').change ->
input = this
image_container = $("#" + input.id).parent().children(".preview")
unless image_container.length
image_container = $("#" + input.id).parent().prepend($('<img />').addClass('preview').addClass('img-thumbnail')).find('img.preview')
image_container.parent().find('img:not(.preview)').hide()
ext = $("#" + input.id).val().split('.').pop().toLowerCase()
if input.files and input.files[0] and $.inArray(ext, ['gif','png','jpg','jpeg','bmp']) != -1
reader = new FileReader()
reader.onload = (e) ->
image_container.attr "src", e.target.result
reader.readAsDataURL input.files[0]
image_container.show()
else
image_container.hide()
# multiple-fileupload
content.find('[data-multiple-fileupload]').each ->
$(this).closest('.controls').find('.btn-remove-image').on('click', ->
$(this).siblings('[type=checkbox]').click()
$(this).parent('.toggle').toggle('slow')
$(this).toggleClass('btn-danger btn-info')
false
).end().sortable(items: '.sortables')
# multiple-fileupload-preview
content.find('[data-multiple-fileupload]').change ->
input = this
$("#" + input.id).parent().children(".preview").remove()
for file in input.files
ext = file.name.split('.').pop().toLowerCase()
if $.inArray(ext, ['gif','png','jpg','jpeg','bmp']) == -1
continue
image_container = $('<img />').addClass('preview').addClass('img-thumbnail')
do (image_container) ->
reader = new FileReader()
reader.onload = (e) ->
image_container.attr "src", e.target.result
reader.readAsDataURL file
$("#" + input.id).parent().append($('<div></div>').addClass('preview').append(image_container))
# filtering-multiselect
content.find('[data-filteringmultiselect]').each ->
$(this).filteringMultiselect $(this).data('options')
if $(this).parents("#modal").length # hide link if we already are inside a dialog (endless issues on nested dialogs with JS)
$(this).siblings('.btn').remove()
else
$(this).parents('.control-group').first().remoteForm()
# filtering-select
content.find('[data-filteringselect]').each ->
$(this).filteringSelect $(this).data('options')
if $(this).parents("#modal").length # hide link if we already are inside a dialog (endless issues on nested dialogs with JS)
$(this).siblings('.btn').remove()
else
$(this).parents('.control-group').first().remoteForm()
# nested-many
content.find('[data-nestedmany]').each ->
field = $(this).parents('.control-group').first()
nav = field.find('> .controls > .nav')
tab_content = field.find('> .tab-content')
toggler = field.find('> .controls > .btn-group > .toggler')
# add each nested field to a tab-pane and reference it in the nav
tab_content.children('.fields:not(.tab-pane)').addClass('tab-pane').each ->
$(this).attr('id', 'unique-id-' + (new Date().getTime()) + Math.floor(Math.random()*100000)) # some elements are created on the same ms
nav.append('<li><a data-toggle="tab" href="#' + this.id + '">' + $(this).children('.object-infos').data('object-label') + '</a></li>')
# only if no tab is set to active
if nav.find("> li.active").length == 0
# init first tab, toggler and tab_content/tabs visibility
nav.find("> li > a[data-toggle='tab']:first").tab('show')
if nav.children().length == 0
nav.hide()
tab_content.hide()
toggler.addClass('disabled').removeClass('active').children('i').addClass('icon-chevron-right')
else
if toggler.hasClass('active')
nav.show()
tab_content.show()
toggler.children('i').addClass('icon-chevron-down')
else
nav.hide()
tab_content.hide()
toggler.children('i').addClass('icon-chevron-right')
# nested-one
content.find('[data-nestedone]').each ->
field = $(this).parents('.control-group').first()
nav = field.find("> .controls > .nav")
tab_content = field.find("> .tab-content")
toggler = field.find('> .controls > .btn-group > .toggler')
tab_content.children(".fields:not(.tab-pane)").addClass('tab-pane active').each ->
# Convert the "add nested field" button to just showing the title of the new model
field.find('> .controls .add_nested_fields').removeClass('add_nested_fields').html( $(this).children('.object-infos').data('object-label') )
nav.append('<li><a data-toggle="tab" href="#' + this.id + '">' + $(this).children('.object-infos').data('object-label') + '</a></li>')
first_tab = nav.find("> li > a[data-toggle='tab']:first")
first_tab.tab('show')
field.find("> .controls > [data-target]:first").html('<i class="icon-white"></i> ' + first_tab.html())
nav.hide()
if nav.children().length == 0
nav.hide()
tab_content.hide()
toggler.addClass('disabled').removeClass('active').children('i').addClass('icon-chevron-right')
else
if toggler.hasClass('active')
toggler.children('i').addClass('icon-chevron-down')
tab_content.show()
else
toggler.children('i').addClass('icon-chevron-right')
tab_content.hide()
# polymorphic-association
content.find('[data-polymorphic]').each ->
type_select = $(this)
field = type_select.parents('.control-group').first()
object_select = field.find('select').last()
urls = type_select.data('urls')
type_select.on 'change', (e) ->
selected_type = type_select.val().toLowerCase()
selected_data = $("##{selected_type}-js-options").data('options')
object_select.data('options', selected_data)
object_select.filteringSelect("destroy")
object_select.filteringSelect selected_data
# simplemde
goSimpleMDEs = ->
content.find('[data-richtext=simplemde]').not('.simplemded').each (index, domEle) ->
options = $(this).data('options')
instance_config = options.instance_config
new window.SimpleMDE($.extend(true, {
element: document.getElementById(this.id),
autosave: {
uniqueId: this.id
}
}, instance_config))
$(this).addClass('simplemded')
$editors = content.find('[data-richtext=simplemde]').not('.simplemded')
if $editors.length
if not window.SimpleMDE
options = $editors.first().data('options')
$('head').append('<link href="' + options['css_location'] + '" rel="stylesheet" media="all" type="text\/css">')
$.getScript options['js_location'], (script, textStatus, jqXHR) ->
goSimpleMDEs()
else
goSimpleMDEs()
# ckeditor
goCkeditors = ->
content.find('[data-richtext=ckeditor]').not('.ckeditored').each (index, domEle) ->
try
if instance = window.CKEDITOR.instances[this.id]
instance.destroy(true)
window.CKEDITOR.replace(this, $(this).data('options'))
$(this).addClass('ckeditored')
$editors = content.find('[data-richtext=ckeditor]').not('.ckeditored')
if $editors.length
if not window.CKEDITOR
options = $editors.first().data('options')
window.CKEDITOR_BASEPATH = options['base_location']
$.getScript options['jspath'], (script, textStatus, jqXHR) =>
goCkeditors()
else
goCkeditors()
#codemirror
goCodeMirrors = (array) =>
array.each (index, domEle) ->
options = $(this).data('options')
textarea = this
$.getScript options['locations']['mode'], (script, textStatus, jqXHR) ->
options = $(domEle).data('options')
$('head').append('<link href="' + options['locations']['theme'] + '" rel="stylesheet" media="all" type="text\/css">')
CodeMirror.fromTextArea(textarea,options['options'])
$(textarea).addClass('codemirrored')
array = content.find('[data-richtext=codemirror]').not('.codemirrored')
if array.length
@array = array
if not window.CodeMirror
options = $(array[0]).data('options')
$('head').append('<link href="' + options['csspath'] + '" rel="stylesheet" media="all" type="text\/css">')
$.getScript options['jspath'], (script, textStatus, jqXHR) =>
goCodeMirrors(@array)
else
goCodeMirrors(@array)
# bootstrap_wysihtml5
goBootstrapWysihtml5s = (array, config_options) =>
array.each ->
$(@).addClass('bootstrap-wysihtml5ed')
$(@).closest('.controls').addClass('well')
$(@).wysihtml5(config_options)
array = content.find('[data-richtext=bootstrap-wysihtml5]').not('.bootstrap-wysihtml5ed')
if array.length
@array = array
options = $(array[0]).data('options')
config_options = $.parseJSON(options['config_options'])
if not window.wysihtml5
$('head').append('<link href="' + options['csspath'] + '" rel="stylesheet" media="all" type="text\/css">')
$.getScript options['jspath'], (script, textStatus, jqXHR) =>
goBootstrapWysihtml5s(@array, config_options)
else
goBootstrapWysihtml5s(@array, config_options)
# froala_wysiwyg
goFroalaWysiwygs = (array) =>
array.each ->
options = $(@).data('options')
config_options = $.parseJSON(options['config_options'])
if config_options
if !config_options['inlineMode']
config_options['inlineMode'] = false
else
config_options = { inlineMode: false }
uploadEnabled =
if config_options['imageUploadURL']
config_options['imageUploadParams'] =
authenticity_token: $('meta[name=csrf-token]').attr('content')
$(@).addClass('froala-wysiwyged')
$(@).froalaEditor(config_options)
if uploadEnabled
$(@).on 'froalaEditor.image.error', (e, editor, error) ->
alert("error uploading image: " + error.message);
# Custom error message returned from the server.
if error.code == 0
# Bad link.
else if error.code == 1
# No link in upload response.
else if error.code == 2
# Error during image upload.
else if error.code == 3
# Parsing response failed.
else if error.code == 4
# Image too large.
else if error.code == 5
# Invalid image type.
else if error.code == 6
# Image can be uploaded only to same domain in IE 8 and IE 9.
else if error.code == 7
else
return
.on('froalaEditor.image.removed', (e, editor, $img) ->
# Set the image source to the image delete params.
editor.options.imageDeleteParams =
src: $img.attr('src')
authenticity_token: $('meta[name=csrf-token]').attr('content')
# Make the delete request.
editor.deleteImage $img
return
).on('editable.imageDeleteSuccess', (e, editor, data) ->
# handle success
).on 'editable.imageDeleteError', (e, editor, error) ->
# handle error
alert("error deleting image: " + error.message);
array = content.find('[data-richtext=froala-wysiwyg]').not('.froala-wysiwyged')
if array.length
options = $(array[0]).data('options')
if not $.isFunction($.fn.editable)
$('head').append('<link href="' + options['csspath'] + '" rel="stylesheet" media="all" type="text\/css">')
$.getScript options['jspath'], (script, textStatus, jqXHR) =>
goFroalaWysiwygs(array)
else
goFroalaWysiwygs(array)
# action_text
content.find('trix-editor').each ->
if !window.Trix
options = $(this).data('options')
$('head').append('<link href="' + options['csspath'] + '" rel="stylesheet" media="all" type="text\/css">')
$.getScript options['jspath']