zet-lib 1.0.55 → 1.0.56

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/lib/zRoleRouter.js +102 -19
  2. package/package.json +1 -1
@@ -144,34 +144,101 @@ router.post('/create', async (req, res) => {
144
144
  })
145
145
 
146
146
  router.post('/tab-access', async (req, res) => {
147
- console.log(req.body)
148
147
  let json = Util.jsonSuccess()
149
148
  let html = ''
149
+ const checkedFn = (obj, name, index) => {
150
+ return obj.hasOwnProperty(name) && obj[name][index] ? 'checked' : ''
151
+ }
150
152
  try {
151
153
  const table = req.body.table
154
+ const id = req.body.id
155
+ let tabRole = await connection.result({
156
+ table: 'zrole',
157
+ where: {
158
+ id: id,
159
+ },
160
+ })
161
+ let mytabs = tabRole.tabs || {}
162
+ let mytabsRole = mytabs[table] || {}
152
163
  let result = await connection.result({
153
164
  table: 'zfields',
154
165
  where: {
155
166
  table: table,
156
167
  },
157
168
  })
158
- html = `<table class="table table-hover"><thead><tr><th>Tab</th><th>View</th><th>Create</th><th>Edit</th><th>Delete</th></tr></thead>`
159
- result.tabs.map((item) => {
169
+ html += `<form id="tabform">`
170
+ html += `<table class="table table-hover"><thead><tr><th>Tab</th><th>View <input type="checkbox" onclick="tabChecks(this,'viewtab')" id="viewtabs" /></th><th>Create <input type="checkbox" id="createtabs" onclick="tabChecks(this,'createtab')" /></th><th>Edit <input type="checkbox" id="edittabs" onclick="tabChecks(this,'edittab')" /></th><th>Delete <input type="checkbox" onclick="tabChecks(this,'deletetab')" id="deletetabs" /></th></tr></thead><tbody></tbody>`
171
+ result.tabs.map((item, index) => {
160
172
  html += `<tr>
161
173
  <td>${item}</td>
162
- <td><input type="checkbox" name="${table}___${item}___view" /></td>
163
- <td><input type="checkbox" name="${table}___${item}___create" /></td>
164
- <td><input type="checkbox" name="${table}___${item}___edit" /></td>
165
- <td><input type="checkbox" name="${table}___${item}___delete" /></td>
174
+ <td><input type="checkbox" class="viewtab" name="view___${index}" ${checkedFn(mytabsRole, 'view', index)} /></td>
175
+ <td><input type="checkbox" class="createtab" name="create___${index}" ${checkedFn(mytabsRole, 'create', index)} /></td>
176
+ <td><input type="checkbox" class="edittab" name="edit___${index}" ${checkedFn(mytabsRole, 'edit', index)} /></td>
177
+ <td><input type="checkbox" class="deletetab" name="delete___${index}" ${checkedFn(mytabsRole, 'delete', index)} /></td>
166
178
  </tr>`
167
179
  })
168
- html += `</table>`
180
+ html += `</tbody></table>`
181
+ html += `</form>`
169
182
  } catch (e) {
170
183
  json = Util.flashError(e.toString())
171
184
  }
172
185
  res.send(html)
173
186
  })
174
187
 
188
+ router.post('/post-access', async (req, res) => {
189
+ let json = Util.jsonSuccess()
190
+ let table = req.body.table || ''
191
+ if (table == '') {
192
+ res.json(Util.flashError('Table is empty!'))
193
+ return false
194
+ }
195
+ let tabs = req.body.tabs || []
196
+ let id = req.body.id
197
+ let obj = tabs.reduce((acc, item) => {
198
+ acc[item.name] = item.value
199
+ return acc
200
+ }, {})
201
+ let mytabs = {}
202
+ let l = req.body.l
203
+ let roles = {}
204
+ let arr = ['view', 'create', 'edit', 'delete']
205
+ for (var i = 0; i < l; i++) {
206
+ arr.map((item) => {
207
+ if (!roles.hasOwnProperty(item)) {
208
+ roles[item] = []
209
+ }
210
+ if (obj.hasOwnProperty(`${item}___${i}`)) {
211
+ roles[item].push(true)
212
+ } else {
213
+ roles[item].push(false)
214
+ }
215
+ })
216
+ }
217
+
218
+ try {
219
+ let tabRole = await connection.result({
220
+ table: 'zrole',
221
+ where: {
222
+ id: id,
223
+ },
224
+ })
225
+ mytabs = tabRole.tabs || {}
226
+ mytabs[table] = roles
227
+ await connection.update({
228
+ table: 'zrole',
229
+ data: {
230
+ tabs: JSON.stringify(mytabs),
231
+ },
232
+ where: {
233
+ id: id,
234
+ },
235
+ })
236
+ } catch (err) {
237
+ json = Util.jsonError(err + '')
238
+ }
239
+ res.json(json)
240
+ })
241
+
175
242
  router.delete('/delete/:id', async (req, res) => {
176
243
  let json = Util.jsonSuccess()
177
244
  let id = parseInt(req.params.id)
@@ -222,10 +289,7 @@ const body = `<div class="">
222
289
  <label class="control-label col-md-2" for="role-role_name">Role Name</label>
223
290
  <div class="col-md-10"><select id="roleName" class="form-control form-select mb-3" name="name">
224
291
  <% for(var i = 0;i < results.length;i++){ %>
225
- <option value="<%- results[i].id %>"
226
- <% if(id == results[i].id){ %> selected=""
227
- <% } %>
228
- ><%- results[i].name %></option>
292
+ <option value="<%- results[i].id %>" <% if(id == results[i].id){ %> selected="" <% } %> ><%- results[i].name %></option>
229
293
  <% } %>
230
294
  </select></div>
231
295
  </div>
@@ -248,9 +312,7 @@ const body = `<div class="">
248
312
  </td>
249
313
  <% for(var x = 0;x < actions.length;x++) { %>
250
314
  <td><input name="params[<%= ename %>][<%= actions[x] %>]" class="<%= actions[x] %>"
251
- <% if(json && json.hasOwnProperty(ename) && json[ename].indexOf(actions[x]) >= 0) { %> <%= 'checked="checked"' %>
252
- <% } %>
253
- title="Role for <%= routes[i] %> <%= actions[x] %>" type="checkbox">
315
+ <% if(json && json.hasOwnProperty(ename) && json[ename].indexOf(actions[x]) >= 0) { %> <%= 'checked="checked"' %> <% } %> title="Role for <%= routes[i] %> <%= actions[x] %>" type="checkbox">
254
316
  </td>
255
317
  <% } %>
256
318
  </tr>
@@ -305,11 +367,10 @@ const body = `<div class="">
305
367
  </div>
306
368
  </div>
307
369
  </div>
308
-
309
370
  <div class="modal fade" id="tabaccess" tabindex="-1" aria-labelledby="tabaccessModalLabel" aria-hidden="true">
310
371
  <div class="modal-dialog">
311
372
  <div class="modal-content">
312
- <div class="modal-header"><h5 class="modal-title" id="tabaccesstitle">Tab Access </h5>
373
+ <div class="modal-header"><h5 class="modal-title" id="tabaccesstitle">Tab Access <span id="titleaccess"></span></h5>
313
374
  <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
314
375
  </div>
315
376
  <div class="modal-body body-access">
@@ -317,7 +378,7 @@ const body = `<div class="">
317
378
  </div>
318
379
  <div class="modal-footer">
319
380
  <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
320
- <button type="button" class="btn btn-primary btn-access">Save changes</button>
381
+ <button type="button" class="btn btn-primary btn-save-tab">Save changes</button>
321
382
  </div>
322
383
  </div>
323
384
  </div>
@@ -334,6 +395,13 @@ const js = `<script>
334
395
  $("input." + elm).prop("checked", false);
335
396
  }
336
397
  }
398
+ function tabChecks(that,elm) {
399
+ if ($(that).is(":checked")) {
400
+ $("input." + elm).prop("checked", true);
401
+ } else {
402
+ $("input." + elm).prop("checked", false);
403
+ }
404
+ }
337
405
  $("#roleName").on('change', function () {
338
406
  location.href = "/zrole?id=" + $(this).val();
339
407
  });
@@ -383,11 +451,26 @@ const js = `<script>
383
451
  }
384
452
 
385
453
  function handleAccess(table) {
386
- ajaxPost('/zrole/tab-access',{table:table}, ((data) => {
454
+ $(".btn-save-tab").attr("data-table", table);
455
+ ajaxPost('/zrole/tab-access',{table:table, id: "<%- id %>"}, ((data) => {
387
456
  $(".body-access").html(data);
388
457
  }))
389
458
  }
390
459
  $(".handle-access").on("click", (e)=> handleAccess(e.target.text));
460
+ $(".btn-save-tab").on("click",(e) => {
461
+ let queryform = $("#tabform").serializeArray();
462
+ ajaxPost('/zrole/post-access', {
463
+ tabs: queryform,
464
+ l : $(".viewtab").length,
465
+ id: "<%- id %>",
466
+ table : e.target.getAttribute('data-table')
467
+ },((data) => {
468
+ toastrForm(data);
469
+ if(data.status == 1) {
470
+ setTimeout(() => {location.href= '';},2000);
471
+ }
472
+ }))
473
+ })
391
474
  </script>
392
475
  `
393
476
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "zet-lib",
3
- "version": "1.0.55",
3
+ "version": "1.0.56",
4
4
  "description": "zet is a library that part of zet generator.",
5
5
  "engines": {
6
6
  "node": ">=18"