|
1 | | -jQuery(document).ready(function ($) { |
2 | | - $('.modal').modal(); |
3 | | - |
4 | | - $('.modal-trigger').on('click', function (event) { |
5 | | - event.stopPropagation(); |
6 | | - let targetModalId = $(this).data('target'); |
7 | | - $('#' + targetModalId).modal('open'); |
8 | | - }); |
| 1 | +class ToolkitLogs { |
| 2 | + constructor() { |
| 3 | + this.$logsIframe = $('.logs-iframe'); |
| 4 | + } |
9 | 5 |
|
10 | | - $('.visibility-icon').on('click', function () { |
11 | | - if ($(this).hasClass('on')) { |
12 | | - $('.off').removeClass('hide'); |
13 | | - $(this).parent('.input-field').find('input[type=text]').attr('type', 'password'); |
| 6 | + handleLogFileClick(event) { |
| 7 | + const $target = $(event.currentTarget); |
| 8 | + const file = $target.data('file'); |
14 | 9 |
|
15 | | - } else { |
16 | | - $('.on').removeClass('hide'); |
17 | | - $(this).parent('.input-field').find('input[type=password]').attr('type', 'text'); |
18 | | - } |
| 10 | + this.$logsIframe.attr('src', `${window.location.origin}/toolkit/logs/view?logFile=${file}`); |
| 11 | + $target.siblings().removeClass('active'); |
| 12 | + $target.addClass('active'); |
| 13 | + } |
19 | 14 |
|
20 | | - $(this).addClass('hide'); |
21 | | - }); |
| 15 | + events() { |
| 16 | + $('.logFile-item').on('click', this.handleLogFileClick.bind(this)); |
| 17 | + } |
22 | 18 |
|
23 | | - $('.collapsible').collapsible(); |
| 19 | + init() { |
| 20 | + this.events(); |
| 21 | + } |
| 22 | +} |
24 | 23 |
|
25 | | - $('.collapsible-header').hover( |
26 | | - function () { |
27 | | - const icon = $(this).find('.status-icon'); |
28 | | - icon.data('original', icon.text()); |
29 | | - $(icon).parent().addClass("chevron"); |
30 | | - icon.text('chevron_right'); |
31 | | - }, |
32 | | - function () { |
33 | | - const icon = $(this).find('.status-icon'); |
34 | | - $(icon).parent().removeClass("chevron"); |
35 | | - icon.text(icon.data('original')); |
36 | | - } |
37 | | - ); |
| 24 | +class ToolkitEmails { |
| 25 | + constructor() { |
| 26 | + this.$deleteModal = $('#deleteModal'); |
| 27 | + this.$confirmDelete = $('#confirmDelete'); |
| 28 | + } |
38 | 29 |
|
39 | | - $('.logFile-item').on('click', function (){ |
40 | | - $('.logs-iframe').attr('src', window.location.origin + "/toolkit/logs/view?logDate=" + $(this).data("file")); |
41 | | - $(this).siblings().removeClass('active'); |
42 | | - $(this).addClass('active'); |
43 | | - }); |
| 30 | + handleDeleteClick(event) { |
| 31 | + event.stopPropagation(); |
| 32 | + const $target = $(event.currentTarget); |
| 33 | + const deleteUrl = $target.data('delete-url'); |
| 34 | + this.$confirmDelete.attr('href', deleteUrl); |
| 35 | + this.$deleteModal.modal('open'); |
| 36 | + } |
44 | 37 |
|
45 | | - $('.table-item').on('click', function (){ |
46 | | - $('.table-iframe').attr('src', window.location.origin + "/toolkit/database/view?table=" + $(this).data("name")); |
47 | | - $(this).siblings().removeClass('active'); |
48 | | - $(this).addClass('active'); |
49 | | - }); |
| 38 | + events() { |
| 39 | + $('.delete-trigger').on('click', this.handleDeleteClick.bind(this)); |
| 40 | + } |
50 | 41 |
|
51 | | - let editor; |
| 42 | + init() { |
| 43 | + this.events(); |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +class ToolkitDatabase { |
| 48 | + constructor() { |
| 49 | + this.$jsonEditorContainer = $('#jsoneditor'); |
| 50 | + this.$createTableModal = $('#createTableModal'); |
| 51 | + this.$rowActionModal = $('#rowActionModal'); |
| 52 | + this.$rowDeleteModal = $('#rowDelete'); |
| 53 | + this.$modalConfirm = $('#modal-confirm'); |
| 54 | + } |
52 | 55 |
|
53 | | - function setupModal(modal, action, title, data = {}, options = { mode: 'form', search: false }) { |
54 | | - modal.data('action', action); |
| 56 | + openModal(modal, actionUrl, title, data = {}, options = { mode: 'form', search: false }) { |
| 57 | + modal.data('action', actionUrl); |
55 | 58 | modal.find('.modal-title').text(title); |
56 | | - $("#jsoneditor").empty(); |
57 | | - editor = new JSONEditor(document.getElementById("jsoneditor"), options); |
58 | | - editor.set(data); |
| 59 | + |
| 60 | + this.$jsonEditorContainer.empty(); |
| 61 | + window.editor = new JSONEditor(this.$jsonEditorContainer[0], options); |
| 62 | + window.editor.set(data); |
| 63 | + |
59 | 64 | modal.modal('open'); |
60 | 65 | } |
61 | 66 |
|
62 | | - function submitForm(modal, additionalFields = {}) { |
| 67 | + submitForm(modal, additionalFields = {}) { |
63 | 68 | const $form = $('<form>', { method: 'POST', action: modal.data('action') }); |
64 | 69 |
|
65 | | - $form.append($('<input>', { type: 'hidden', name: 'data', value: JSON.stringify(editor.get()) })); |
| 70 | + $form.append($('<input>', { |
| 71 | + type: 'hidden', |
| 72 | + name: 'data', |
| 73 | + value: JSON.stringify(window.editor.get()) |
| 74 | + })); |
66 | 75 |
|
67 | | - Object.entries(additionalFields).forEach(([name, value]) => { |
| 76 | + for (const [name, value] of Object.entries(additionalFields)) { |
68 | 77 | $form.append($('<input>', { type: 'hidden', name, value })); |
69 | | - }); |
| 78 | + } |
70 | 79 |
|
71 | 80 | $('body').append($form); |
72 | 81 | $form.submit(); |
73 | 82 | } |
74 | 83 |
|
75 | | - $('.row-add').on('click', function(){ |
| 84 | + handleTableItemClick(event) { |
| 85 | + const $target = $(event.currentTarget); |
| 86 | + const table = $target.data('name'); |
76 | 87 |
|
77 | | - let rowData = $('.row-data').first().data('row'); |
| 88 | + $('.table-iframe').attr('src', `${window.location.origin}/toolkit/database/view?table=${table}`); |
| 89 | + $target.siblings().removeClass('active'); |
| 90 | + $target.addClass('active'); |
| 91 | + } |
| 92 | + |
| 93 | + handleRowAddOrCreate(event) { |
| 94 | + const $target = $(event.currentTarget); |
| 95 | + const isTableCreate = $target.hasClass('create-table'); |
| 96 | + const modal = isTableCreate ? this.$createTableModal : this.$rowActionModal; |
| 97 | + const actionUrl = '/toolkit/database/create'; |
| 98 | + const title = isTableCreate ? 'Creating Table' : $target.data('modal-title'); |
78 | 99 |
|
79 | 100 | let json = {}; |
| 101 | + let options = { mode: 'tree', search: false }; |
| 102 | + |
| 103 | + if (!isTableCreate) { |
| 104 | + const rowData = $('.row-data').first().data('row'); |
| 105 | + options.mode = rowData ? 'form' : 'tree'; |
| 106 | + |
| 107 | + if (rowData) { |
| 108 | + for (const key of Object.keys(rowData)) { |
| 109 | + if (key !== 'id') { |
| 110 | + json[key] = ''; |
| 111 | + } |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + const tableName = modal.data('table'); |
| 116 | + modal.data('original-name', tableName); |
| 117 | + } else { |
| 118 | + modal.removeData('original-name'); |
| 119 | + } |
| 120 | + |
| 121 | + this.openModal(modal, actionUrl, title, json, options); |
| 122 | + } |
| 123 | + |
| 124 | + handleRowEdit(event) { |
| 125 | + const data = { ...$(event.currentTarget).parent().data('row') }; |
| 126 | + const title = $(event.currentTarget).data('modal-title'); |
| 127 | + |
| 128 | + this.$rowActionModal.data('id', data.id); |
| 129 | + delete data.id; |
80 | 130 |
|
81 | | - let options = { mode: 'form', search: false }; |
| 131 | + this.openModal(this.$rowActionModal, '/toolkit/database/update', title, data); |
| 132 | + } |
| 133 | + |
| 134 | + handleRowDelete(event) { |
| 135 | + const deleteUrl = $(event.currentTarget).data('url'); |
| 136 | + this.$modalConfirm.attr('href', deleteUrl); |
| 137 | + this.$rowDeleteModal.modal('open'); |
| 138 | + } |
82 | 139 |
|
83 | | - if(rowData){ |
84 | | - json = Object.fromEntries( |
85 | | - Object.keys(rowData) |
86 | | - .filter(key => key !== 'id') |
87 | | - .map(key => [key, '']) |
88 | | - ); |
89 | | - }else{ |
90 | | - options.mode = 'tree'; |
| 140 | + handleRowAction() { |
| 141 | + const originalName = this.$rowActionModal.data('original-name'); |
| 142 | + |
| 143 | + const additionalFields = { |
| 144 | + table: this.$rowActionModal.data('table'), |
| 145 | + rowId: this.$rowActionModal.data('id'), |
| 146 | + 'csrf-token': this.$rowActionModal.data('csrf') |
| 147 | + }; |
| 148 | + |
| 149 | + if (originalName) { |
| 150 | + additionalFields.originalName = originalName; |
91 | 151 | } |
92 | 152 |
|
93 | | - setupModal($('#rowActionModal'), 'create', $(this).data('modal-title'), json, options); |
94 | | - }); |
| 153 | + this.submitForm(this.$rowActionModal, additionalFields); |
| 154 | + } |
95 | 155 |
|
96 | | - $('.row-edit').on('click', function(){ |
97 | | - let data = $(this).parent().data("row"); |
98 | | - let editModal = $('#rowActionModal'); |
99 | | - editModal.data('id', data.id); |
100 | | - delete data.id; |
101 | | - setupModal(editModal, 'update', $(this).data('modal-title'), data); |
102 | | - }); |
103 | | - |
104 | | - $('.row-action').on('click', function () { |
105 | | - let $rowActionModal = $('#rowActionModal'); |
106 | | - submitForm($rowActionModal, { |
107 | | - table: $rowActionModal.data('table'), |
108 | | - rowId: $rowActionModal.data('id'), |
109 | | - 'csrf-token': $rowActionModal.data('csrf'), |
| 156 | + handleSubmitTable() { |
| 157 | + this.submitForm(this.$createTableModal, { |
| 158 | + table: this.$createTableModal.find('#tableName').val(), |
| 159 | + 'csrf-token': this.$createTableModal.data('csrf') |
110 | 160 | }); |
111 | | - }); |
| 161 | + } |
112 | 162 |
|
113 | | - $('.create-table').on('click', function () { |
114 | | - setupModal($('#createTableModal'), 'database/create', 'Creating Table', {}, { mode: 'tree', search: false }); |
115 | | - }); |
| 163 | + events() { |
| 164 | + $('.table-item').on('click', this.handleTableItemClick.bind(this)); |
| 165 | + $('.row-add').on('click', this.handleRowAddOrCreate.bind(this)); |
| 166 | + $('.row-edit').on('click', this.handleRowEdit.bind(this)); |
| 167 | + $('.row-delete').on('click', this.handleRowDelete.bind(this)); |
| 168 | + $('.row-action').on('click', this.handleRowAction.bind(this)); |
| 169 | + $('.create-table').on('click', this.handleRowAddOrCreate.bind(this)); |
| 170 | + $('.submit-table').on('click', this.handleSubmitTable.bind(this)); |
| 171 | + } |
116 | 172 |
|
117 | | - $('.submit-table').on('click', function () { |
118 | | - let $createTableModal = $('#createTableModal'); |
119 | | - submitForm($createTableModal, { |
120 | | - table: $createTableModal.find('#tableName').val(), |
121 | | - 'csrf-token': $createTableModal.data('csrf'), |
122 | | - }); |
123 | | - }); |
| 173 | + init() { |
| 174 | + this.events(); |
| 175 | + } |
| 176 | +} |
124 | 177 |
|
125 | | - $('.sidenav').sidenav(); |
126 | | -}); |
| 178 | +jQuery(document).ready(function ($) { |
| 179 | + window.editor = undefined; |
127 | 180 |
|
| 181 | + $('.modal').modal(); |
| 182 | + $('.collapsible').collapsible(); |
| 183 | + $('.sidenav').sidenav(); |
128 | 184 |
|
| 185 | + new ToolkitLogs().init(); |
| 186 | + new ToolkitEmails().init(); |
| 187 | + new ToolkitDatabase().init(); |
| 188 | +}); |
0 commit comments