-
Notifications
You must be signed in to change notification settings - Fork 41
/
modals.coffee
185 lines (160 loc) · 5.02 KB
/
modals.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
registeredAutoFormHooks = ['cmForm']
defaultFormId = 'cmForm'
cmOnSuccessCallback = null
AutoForm.addHooks 'cmForm',
onSuccess: ->
$('#afModal').modal('hide')
collectionObj = (name) ->
name.split('.').reduce (o, x) ->
o[x]
, window
Template.autoformModals.rendered = ->
$('#afModal').modal(show: false)
onEscKey = (e) ->
if e.keyCode == 27
$('#afModal').modal 'hide'
$('#afModal').on 'shown.bs.modal', ->
$(window).bind 'keyup', onEscKey
$('#afModal').on 'hidden.bs.modal', ->
$(window).unbind 'keyup', onEscKey
AutoForm.resetForm(Session.get('cmFormId') or defaultFormId)
sessionKeys = [
'cmCollection',
'cmOperation',
'cmDoc',
'cmButtonHtml',
'cmFields',
'cmOmitFields',
'cmButtonContent',
'cmTitle',
'cmButtonClasses',
'cmPrompt',
'cmTemplate',
'cmLabelClass',
'cmInputColClass',
'cmPlaceholder',
'cmFormId',
'cmAutoformType',
'cmMeteorMethod',
'cmCloseButtonContent',
'cmCloseButtonClasses'
]
delete Session.keys[key] for key in sessionKeys
Template.autoformModals.events
'click button:not(.close)': () ->
collection = Session.get 'cmCollection'
operation = Session.get 'cmOperation'
if operation != 'insert'
_id = Session.get('cmDoc')._id
if operation == 'remove'
collectionObj(collection).remove _id, (e)->
if e
alert 'Sorry, this could not be deleted.'
else
$('#afModal').modal('hide')
cmOnSuccessCallback?()
helpers =
cmCollection: () ->
Session.get 'cmCollection'
cmOperation: () ->
Session.get 'cmOperation'
cmDoc: () ->
Session.get 'cmDoc'
cmButtonHtml: () ->
Session.get 'cmButtonHtml'
cmFields: () ->
Session.get 'cmFields'
cmOmitFields: () ->
Session.get 'cmOmitFields'
cmButtonContent: () ->
Session.get 'cmButtonContent'
cmCloseButtonContent: () ->
Session.get 'cmCloseButtonContent'
cmTitle: () ->
Session.get 'cmTitle'
cmButtonClasses: () ->
Session.get 'cmButtonClasses'
cmCloseButtonClasses: () ->
Session.get 'cmCloseButtonClasses'
cmPrompt: () ->
Session.get 'cmPrompt'
cmTemplate: () ->
Session.get 'cmTemplate'
cmLabelClass: () ->
Session.get 'cmLabelClass'
cmInputColClass: () ->
Session.get 'cmInputColClass'
cmPlaceholder: () ->
Session.get 'cmPlaceholder'
cmFormId: () ->
Session.get('cmFormId') or defaultFormId
cmAutoformType: () ->
if Session.get 'cmMeteorMethod'
'method'
else
Session.get 'cmOperation'
cmModalDialogClass: () ->
Session.get 'cmModalDialogClass'
cmModalContentClass: () ->
Session.get 'cmModalContentClass'
cmMeteorMethod: () ->
Session.get 'cmMeteorMethod'
title: () ->
StringTemplate.compile '{{{cmTitle}}}', helpers
prompt: () ->
StringTemplate.compile '{{{cmPrompt}}}', helpers
buttonContent: () ->
StringTemplate.compile '{{{cmButtonContent}}}', helpers
closeButtonContent: () ->
StringTemplate.compile '{{{cmCloseButtonContent}}}', helpers
Template.autoformModals.helpers helpers
Template.afModal.events
'click *': (e, t) ->
e.preventDefault()
html = t.$('*').html()
Session.set 'cmCollection', t.data.collection
Session.set 'cmOperation', t.data.operation
Session.set 'cmFields', t.data.fields
Session.set 'cmOmitFields', t.data.omitFields
Session.set 'cmButtonHtml', html
Session.set 'cmTitle', t.data.title or html
Session.set 'cmTemplate', t.data.template
Session.set 'cmLabelClass', t.data.labelClass or t.data['label-class']
Session.set 'cmInputColClass', t.data.inputColClass or t.data['input-col-class']
Session.set 'cmPlaceholder', if t.data.placeholder is true then 'schemaLabel' else ''
Session.set 'cmFormId', t.data.formId
Session.set 'cmMeteorMethod', t.data.meteormethod
Session.set 'cmModalDialogClass', t.data.dialogClass
Session.set 'cmModalContentClass', t.data.contentClass
cmOnSuccessCallback = t.data.onSuccess
if not _.contains registeredAutoFormHooks, t.data.formId
AutoForm.addHooks t.data.formId,
onSuccess: ->
$('#afModal').modal 'hide'
registeredAutoFormHooks.push t.data.formId
if t.data.doc
Session.set 'cmDoc', collectionObj(t.data.collection).findOne _id: t.data.doc
if t.data.buttonContent
Session.set 'cmButtonContent', t.data.buttonContent
else if t.data.operation == 'insert'
Session.set 'cmButtonContent', 'Create'
else if t.data.operation == 'update'
Session.set 'cmButtonContent', 'Update'
else if t.data.operation == 'remove'
Session.set 'cmButtonContent', 'Delete'
if t.data.buttonClasses
Session.set 'cmButtonClasses', t.data.buttonClasses
else if t.data.operation == 'remove'
Session.set 'cmButtonClasses', 'btn btn-danger'
else
Session.set 'cmButtonClasses', 'btn btn-primary'
Session.set 'cmCloseButtonContent', t.data.closeButtonContent or ''
Session.set 'cmCloseButtonClasses', t.data.closeButtonClasses or 'btn btn-default'
if t.data.prompt
Session.set 'cmPrompt', t.data.prompt
else if t.data.operation == 'remove'
Session.set 'cmPrompt', 'Are you sure?'
else
Session.set 'cmPrompt', ''
$('#afModal').data('bs.modal').options.backdrop = t.data.backdrop or true
$('#afModal').modal 'show'