· 6 years ago · Oct 23, 2019, 02:00 PM
1if API is undefined or API is null
2 API = window.API = '/api'
3
4
5if BUS is undefined or BUS is null
6 BUS = window.BUS = new Vue()
7
8
9main_app_data =
10 data: ->
11 _data =
12 table_base:
13 name: 'Список мероприятий'
14 item_type: 'event'
15 head: [
16 'id'
17 'Наименование'
18 'Департамент'
19 'В Плане'
20 'Бюджет'
21 'Лимит'
22 ],
23 data: []
24 _data
25
26 computed: {}
27 methods: {}
28 created: ->
29 return
30
31 mounted: ->
32 return
33
34
35table_base_app_data =
36 data: ->
37 _data =
38 table: {
39 name: ''
40 item_type: null
41 head: []
42 data: []
43 }
44 _data
45
46 props: [
47 "table"
48 ]
49
50 computed: {}
51 methods:
52 show_form_to_table: ->
53 BUS.$emit 'show_form_to_table_base'
54 return
55
56 edit_row: (row) ->
57 BUS.$emit 'show_form_to_table_base_and_edit_row', row
58 return
59
60 delete_row: (row) ->
61 self = @
62 postJsonWithData API, {action: 'delete_event', options: row}, (response) ->
63 if response.status
64 index = self.table.data.findIndex (item) -> if row.id is item.id then true else false
65 if index isnt null and index > -1
66 self.table.data.splice index, 1
67 return
68 return
69
70 add_row: (row) ->
71 self = @
72 postJsonWithData API, {action: 'add_new_event', options: row}, (response) ->
73 if response.status
74 self.table.data.push response.data
75 return
76 return
77
78 update_row: (row) ->
79 self = @
80 postJsonWithData API, {action: 'update_event', options: row}, (response) ->
81 if response.status
82 index = self.table.data.findIndex (item) -> if row.id == item.id then true else false
83 if index isnt null and index > -1
84 for key, value of row
85 self.table.data[index][key] = value
86 return
87 return
88
89 load_and_show_data: ->
90 self = @
91 postJsonWithData API, {action: 'load_table_base'}, (response) ->
92 if response.status
93 self.table.data = response.data
94 return
95 return
96
97 created: ->
98 return
99
100 mounted: ->
101 BUS.$on 'send_to_table_base', @add_row.bind(@)
102 BUS.$on 'update_to_table_base', @update_row.bind(@)
103 @load_and_show_data()
104 return
105
106
107form_to_table_base_app_data =
108 data: ->
109 _data =
110 title: ''
111 cls:
112 show: false
113 fade: false
114 in: false
115 form:
116 name: ''
117 department: ''
118 in_plane: false
119 budget: ''
120 limit: ''
121 _data
122
123 props: []
124 computed: {}
125 methods:
126 show_form: ->
127 @cls =
128 show: true
129 fade: true
130 in: true
131 return
132
133 close_form: ->
134 @cls =
135 show: false
136 fade: false
137 in: false
138
139 return
140
141 reset_form: ->
142 @form =
143 name: ''
144 department: ''
145 in_plane: false
146 budget: ''
147 limit: ''
148
149 save_form: ->
150 self = @
151 for key in ['budget', 'limit']
152 number = parseFloat @form[key]
153 if isNaN(number)
154 @form[key] = null
155 else
156 @form[key] = number
157
158 if @form.id?
159 @update_row @form, (result) ->
160 if result
161 self.close_form()
162 self.reset_form()
163 return
164
165 else
166 @save_row @form, (result) ->
167 if result
168 self.close_form()
169 self.reset_form()
170 return
171
172 update_row: (row, cb) ->
173 BUS.$emit 'update_to_table_base', row
174 cb true
175 return
176
177 save_row: (row, cb) ->
178 BUS.$emit 'send_to_table_base', row
179 cb true
180 return
181
182 show_form_to_new_row: ->
183 @title = 'Добавить мероприятие'
184 @reset_form()
185 @show_form()
186 return
187
188 show_form_and_edit_row: (row) ->
189 @title = 'Изменить мероприятие'
190 for key, value of row
191 @form[key] = value
192
193 @show_form()
194 return
195
196 created: ->
197 return
198
199 mounted: ->
200 BUS.$on 'show_form_to_table_base', @show_form_to_new_row.bind(@)
201 BUS.$on 'show_form_to_table_base_and_edit_row', @show_form_and_edit_row.bind(@)
202 return
203
204
205initApplication = ->
206 table_base_template_dom = document.querySelector '[data-vue-template="table base"]'
207 if table_base_template_dom
208 table_base_app_data.template = table_base_template_dom.innerHTML
209 Vue.component 'table-base-app', table_base_app_data
210
211 form_to_table_base_template_dom = document.querySelector '[data-vue-template="form to table base"]'
212 if form_to_table_base_template_dom
213 form_to_table_base_app_data.template = form_to_table_base_template_dom.innerHTML
214 Vue.component 'form-to-table-base-app', form_to_table_base_app_data
215 form_to_table_base_app = new Vue
216 el: '#form-to-table-base-app'
217
218 main_app_template_dom = document.querySelector '[data-vue-template="main app"]'
219 if main_app_template_dom
220 main_app_data.template = main_app_template_dom.innerHTML
221 Vue.component 'main-app', main_app_data
222 main_app = new Vue
223 el: '#main-app'
224 return
225
226
227document.addEventListener 'DOMContentLoaded', (event) ->
228 initApplication()
229 return