file_manager_admin.js
327 lines
| 1 | const njtFileManager = { |
| 2 | sunriseCreateCookie(name, value, days) { |
| 3 | if (days) { |
| 4 | var date = new Date(); |
| 5 | date.setTime(date.getTime() + days * 24 * 60 * 60 * 1000); |
| 6 | var expires = "; expires=" + date.toGMTString(); |
| 7 | } else var expires = ""; |
| 8 | document.cookie = name + "=" + value + expires + "; path=/"; |
| 9 | }, |
| 10 | |
| 11 | sunriseReadCookie(name) { |
| 12 | var nameEQ = name + "="; |
| 13 | var ca = document.cookie.split(";"); |
| 14 | for (var i = 0; i < ca.length; i++) { |
| 15 | var c = ca[i]; |
| 16 | while (c.charAt(0) == " ") c = c.substring(1, c.length); |
| 17 | if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length); |
| 18 | } |
| 19 | return null; |
| 20 | }, |
| 21 | |
| 22 | capitalizeFirstLetter(string) { |
| 23 | return string.charAt(0).toUpperCase() + string.slice(1); |
| 24 | }, |
| 25 | |
| 26 | //Setting tab |
| 27 | activeTabSetting() { |
| 28 | var pagenow = "njt-fs-filemanager-settings-tab"; |
| 29 | jQuery("#njt-plugin-tabs a").click(function (event) { |
| 30 | jQuery("#njt-plugin-tabs a").removeClass("nav-tab-active"); |
| 31 | jQuery(".njt-plugin-setting").hide(); |
| 32 | jQuery(this).addClass("nav-tab-active"); |
| 33 | if (jQuery(this).data('tab') == 'njt_fs_roles') { |
| 34 | location.hash = "#user-role-restrictions"; |
| 35 | } else { |
| 36 | var noHashURL = window.location.href.replace(/#.*$/, ''); |
| 37 | window.history.replaceState('', document.title, noHashURL) |
| 38 | } |
| 39 | |
| 40 | // Show current pane |
| 41 | jQuery(".njt-plugin-setting:eq(" + jQuery(this).index() + ")").show(); |
| 42 | njtFileManager.sunriseCreateCookie(pagenow + "_last_tab", jQuery(this).index(), 365); |
| 43 | }); |
| 44 | |
| 45 | //Auto-open tab by cookies |
| 46 | if (njtFileManager.sunriseReadCookie(pagenow + "_last_tab") != null) |
| 47 | jQuery("#njt-plugin-tabs a:eq(" + njtFileManager.sunriseReadCookie(pagenow + "_last_tab") + ")").trigger("click"); |
| 48 | // Open first tab by default |
| 49 | else jQuery("#njt-plugin-tabs a:eq(0)").trigger("click"); |
| 50 | }, |
| 51 | |
| 52 | themeSelector() { |
| 53 | if (jQuery('input[name = "selected-theme"]')) { |
| 54 | const selectedTheme = jQuery('input[name = "selected-theme"]').val() |
| 55 | jQuery('#selector-themes').val(selectedTheme); |
| 56 | } |
| 57 | |
| 58 | jQuery('select#selector-themes').on('change', function () { |
| 59 | const themesValue = jQuery(this).val() |
| 60 | const dataThemes = { |
| 61 | 'action': 'selector_themes', |
| 62 | 'themesValue': themesValue, |
| 63 | 'nonce': wpData.nonce, |
| 64 | } |
| 65 | jQuery.post( |
| 66 | wpData.admin_ajax, |
| 67 | dataThemes, |
| 68 | function (response) { |
| 69 | jQuery('link#themes-selector-css').attr('href', response.data) |
| 70 | }); |
| 71 | }); |
| 72 | }, |
| 73 | |
| 74 | actionSettingFormSubmit() { |
| 75 | jQuery('.njt-settings-form-submit').on('click', function () { |
| 76 | const arraylistUserAccess = []; |
| 77 | jQuery('.fm-list-user-item').each(function () { |
| 78 | if (jQuery(this).is(":checked")) { |
| 79 | arraylistUserAccess.push(jQuery(this).val()); |
| 80 | } |
| 81 | }); |
| 82 | arraylistUserAccess.push('administrator') |
| 83 | jQuery("#list_user_alow_access").val(arraylistUserAccess) |
| 84 | }) |
| 85 | }, |
| 86 | |
| 87 | userHasApproved() { |
| 88 | const arrayUserHasApproved = jQuery('#list_user_has_approved').val() ? jQuery('#list_user_has_approved').val().split(",") : [] |
| 89 | for (itemUserHasApproved of arrayUserHasApproved) { |
| 90 | if (itemUserHasApproved != 'administrator') { |
| 91 | jQuery('input[name = ' + itemUserHasApproved + ']').prop('checked', true); |
| 92 | } |
| 93 | } |
| 94 | }, |
| 95 | |
| 96 | actionSubmitRoleRestrictionst() { |
| 97 | jQuery('#njt-form-user-role-restrictionst').on('click', function () { |
| 98 | const arrayUserRestrictionsAccess = []; |
| 99 | if (!jQuery('.njt-fs-list-user-restrictions').val()) { |
| 100 | alert('Please select a User Role at Setings tab to use this option.') |
| 101 | return false; |
| 102 | } |
| 103 | jQuery('.fm-list-user-restrictions-item').each(function () { |
| 104 | if (jQuery(this).is(":checked")) { |
| 105 | arrayUserRestrictionsAccess.push(jQuery(this).val()); |
| 106 | } |
| 107 | }); |
| 108 | jQuery("#list_user_restrictions_alow_access").val(arrayUserRestrictionsAccess) |
| 109 | |
| 110 | if (jQuery("#hide_paths").val().trim().length > 0) { |
| 111 | const valueHidePaths = jQuery("#hide_paths").val().trim().split("|") |
| 112 | const newValueHidePaths = [] |
| 113 | for (const itemHidePath of valueHidePaths) { |
| 114 | if (itemHidePath.trim().length > 0) { |
| 115 | newValueHidePaths.push(itemHidePath.trim()) |
| 116 | } |
| 117 | } |
| 118 | jQuery("#hide_paths").val(newValueHidePaths.join("|")) |
| 119 | } |
| 120 | |
| 121 | if (jQuery("#lock_files").val().trim().length > 0) { |
| 122 | const valueLockFiles = jQuery("#lock_files").val().trim().split("|") |
| 123 | const newValueLockFiles = [] |
| 124 | for (const itemLockFile of valueLockFiles) { |
| 125 | if (itemLockFile.trim().length > 0) { |
| 126 | newValueLockFiles.push(itemLockFile.trim()) |
| 127 | } |
| 128 | } |
| 129 | jQuery("#lock_files").val(newValueLockFiles.join("|")) |
| 130 | } |
| 131 | |
| 132 | }) |
| 133 | }, |
| 134 | |
| 135 | restrictionsHasApproved() { |
| 136 | const arrayRestrictionsHasApproved = jQuery('#list_restrictions_has_approved').val() ? jQuery('#list_restrictions_has_approved').val().split(",") : [] |
| 137 | for (itemRestrictionsHasApproved of arrayRestrictionsHasApproved) { |
| 138 | jQuery('input[name = ' + itemRestrictionsHasApproved + ']').prop('checked', true); |
| 139 | } |
| 140 | }, |
| 141 | |
| 142 | ajaxRoleRestrictions() { |
| 143 | jQuery('select.njt-fs-list-user-restrictions').on('change', function () { |
| 144 | const valueUserRole = jQuery(this).val() |
| 145 | const dataUserRole = { |
| 146 | 'action': 'get_role_restrictions', |
| 147 | 'valueUserRole': valueUserRole, |
| 148 | 'nonce': wpData.nonce, |
| 149 | } |
| 150 | jQuery.post( |
| 151 | wpData.admin_ajax, |
| 152 | dataUserRole, |
| 153 | function (response) { |
| 154 | const resRestrictionsHasApproved = response.data.disable_operations ? response.data.disable_operations.split(",") : [] |
| 155 | const resPrivateFolderAccess = response.data.private_folder_access ? response.data.private_folder_access : '' |
| 156 | const resPrivateURLFolderAccess = response.data.private_url_folder_access ? response.data.private_url_folder_access : '' |
| 157 | const resHidePaths = response.data.hide_paths ? response.data.hide_paths.replace(/[,]+/g, ' | ') : ''; |
| 158 | const resLockFiles = response.data.lock_files ? response.data.lock_files.replace(/[,]+/g, ' | ') : ''; |
| 159 | const resCanUploadMime = response.data.can_upload_mime ? response.data.can_upload_mime : ''; |
| 160 | jQuery('input.fm-list-user-restrictions-item').prop('checked', false); |
| 161 | for (itemRestrictionsHasApproved of resRestrictionsHasApproved) { |
| 162 | jQuery('input[name = ' + itemRestrictionsHasApproved + ']').prop('checked', true); |
| 163 | } |
| 164 | // Set value for textarea[name='private_folder_access'] |
| 165 | jQuery('textarea#private_folder_access').val(resPrivateFolderAccess) |
| 166 | // Set value for textarea[name='private_url_folder_access'] |
| 167 | jQuery('textarea#private_url_folder_access').val(resPrivateURLFolderAccess) |
| 168 | // Set value for textarea[name='hide_paths'] |
| 169 | jQuery('textarea#hide_paths').val(resHidePaths) |
| 170 | // Set value for textarea[name='lock_files'] |
| 171 | jQuery('textarea#lock_files').val(resLockFiles) |
| 172 | // Set value for textarea[name='can_upload_mime'] |
| 173 | jQuery('textarea#can_upload_mime').val(resCanUploadMime) |
| 174 | }); |
| 175 | }); |
| 176 | }, |
| 177 | clickedCreatRootPath() { |
| 178 | jQuery('.js-creat-root-path').on('click', function () { |
| 179 | const valueRootPath = wpData.ABSPATH |
| 180 | jQuery('textarea#private_folder_access').val(valueRootPath) |
| 181 | }) |
| 182 | }, |
| 183 | |
| 184 | ajaxSaveSettings() { |
| 185 | jQuery('.njt-settings-form-submit').on('click', function () { |
| 186 | const arraylistUserAccess = []; |
| 187 | jQuery(this).addClass('njt-fs-updating-message'); |
| 188 | jQuery('.fm-list-user-item').each(function () { |
| 189 | if (jQuery(this).is(":checked")) { |
| 190 | arraylistUserAccess.push(jQuery(this).val()); |
| 191 | } |
| 192 | }); |
| 193 | arraylistUserAccess.push('administrator') |
| 194 | jQuery("#list_user_alow_access").val(arraylistUserAccess) |
| 195 | const list_user_alow_access = jQuery("#list_user_alow_access").val() |
| 196 | const root_folder_path = jQuery("#root_folder_path").val() |
| 197 | const root_folder_url = jQuery("#root_folder_url").val() |
| 198 | const upload_max_size = jQuery("#upload_max_size").val() |
| 199 | const fm_locale = jQuery("#fm_locale").val() |
| 200 | const enable_htaccess = jQuery("#enable_htaccess").is(":checked") |
| 201 | const enable_trash = jQuery("#enable_trash").is(":checked") |
| 202 | const data = { |
| 203 | 'nonce': wpData.nonce, |
| 204 | 'action': 'njt_fs_save_setting', |
| 205 | 'root_folder_path': root_folder_path, |
| 206 | 'root_folder_url': root_folder_url, |
| 207 | 'list_user_alow_access': list_user_alow_access, |
| 208 | 'upload_max_size': upload_max_size, |
| 209 | 'fm_locale': fm_locale, |
| 210 | 'enable_htaccess': enable_htaccess, |
| 211 | 'enable_trash': enable_trash |
| 212 | |
| 213 | } |
| 214 | const toastr_opt = { |
| 215 | closeButton: true, |
| 216 | showDuration: 300, |
| 217 | hideDuration: 300, |
| 218 | hideMethod: "fadeOut", |
| 219 | positionClass: "toast-top-right njt-fs-toastr" |
| 220 | } |
| 221 | jQuery.post( |
| 222 | wpData.admin_ajax, |
| 223 | data, |
| 224 | function (response) { |
| 225 | const list_access = response.data.njt_fs_file_manager_settings.list_user_alow_access |
| 226 | const index = list_access.indexOf('administrator'); |
| 227 | if (index > -1) { |
| 228 | list_access.splice(index, 1); |
| 229 | } |
| 230 | |
| 231 | if (list_access.length > 0) { |
| 232 | jQuery('.njt-fs-list-user-restrictions').empty() |
| 233 | list_access.forEach((item) => { |
| 234 | jQuery('.njt-fs-list-user-restrictions').append(`<option value="${item}"> ${njtFileManager.capitalizeFirstLetter(item)} </option>`); |
| 235 | }); |
| 236 | jQuery('.njt-text-error').hide() |
| 237 | } else { |
| 238 | jQuery('.njt-fs-list-user-restrictions').empty() |
| 239 | jQuery('.njt-fs-list-user-restrictions').append('<option selected="" disabled="" hidden="">Nothing to choose</option>'); |
| 240 | jQuery('.njt-text-error').show() |
| 241 | } |
| 242 | jQuery('.njt-fs-list-user-restrictions').change() |
| 243 | if (response.success) { |
| 244 | jQuery('.njt-settings-form-submit').removeClass('njt-fs-updating-message'); |
| 245 | toastr.success('Changes Saved', '', toastr_opt) |
| 246 | } else { |
| 247 | jQuery('.njt-settings-form-submit').removeClass('njt-fs-updating-message'); |
| 248 | toastr.error('Please try again later', '', toastr_opt) |
| 249 | } |
| 250 | }); |
| 251 | }) |
| 252 | }, |
| 253 | ajaxSaveSettingsRestrictions() { |
| 254 | jQuery('#njt-form-user-role-restrictionst').on('click', function () { |
| 255 | jQuery(this).addClass('njt-fs-updating-message'); |
| 256 | const njt_fs_list_user_restrictions = jQuery(".njt-fs-list-user-restrictions").val() |
| 257 | const list_user_restrictions_alow_access = jQuery("#list_user_restrictions_alow_access").val() |
| 258 | const private_folder_access = jQuery("#private_folder_access").val() |
| 259 | const private_url_folder_access = jQuery("#private_url_folder_access").val() |
| 260 | const hide_paths = jQuery("#hide_paths").val() |
| 261 | const lock_files = jQuery("#lock_files").val() |
| 262 | const can_upload_mime = jQuery("#can_upload_mime").val() |
| 263 | |
| 264 | const data = { |
| 265 | 'nonce': wpData.nonce, |
| 266 | 'action': 'njt_fs_save_setting_restrictions', |
| 267 | 'njt_fs_list_user_restrictions': njt_fs_list_user_restrictions, |
| 268 | 'list_user_restrictions_alow_access': list_user_restrictions_alow_access, |
| 269 | 'private_folder_access': private_folder_access, |
| 270 | 'private_url_folder_access': private_url_folder_access, |
| 271 | 'hide_paths': hide_paths, |
| 272 | 'lock_files': lock_files, |
| 273 | 'can_upload_mime': can_upload_mime |
| 274 | } |
| 275 | const toastr_opt = { |
| 276 | closeButton: true, |
| 277 | showDuration: 300, |
| 278 | hideDuration: 200, |
| 279 | hideMethod: "fadeOut", |
| 280 | positionClass: "toast-top-right njt-fs-toastr" |
| 281 | } |
| 282 | jQuery.post( |
| 283 | wpData.admin_ajax, |
| 284 | data, |
| 285 | function (response) { |
| 286 | if (response.success) { |
| 287 | jQuery('#njt-form-user-role-restrictionst').removeClass('njt-fs-updating-message'); |
| 288 | toastr.success('Changes Saved', '', toastr_opt) |
| 289 | } else { |
| 290 | jQuery('#njt-form-user-role-restrictionst').removeClass('njt-fs-updating-message'); |
| 291 | toastr.error('Error! Please try again', '', toastr_opt) |
| 292 | } |
| 293 | }); |
| 294 | }) |
| 295 | } |
| 296 | } |
| 297 | |
| 298 | jQuery(document).ready(function () { |
| 299 | if (jQuery("div").hasClass("njt-fs-file-manager")) { |
| 300 | |
| 301 | //set select value |
| 302 | njtFileManager.themeSelector(); |
| 303 | // Start- Setting for `Select User Roles to access` |
| 304 | njtFileManager.actionSettingFormSubmit(); |
| 305 | // Get value to prop checked for input checkbox |
| 306 | njtFileManager.userHasApproved(); |
| 307 | //Setting tab |
| 308 | njtFileManager.activeTabSetting(); |
| 309 | |
| 310 | njtFileManager.actionSubmitRoleRestrictionst(); |
| 311 | // Get value to prop checked for input checkbox |
| 312 | njtFileManager.restrictionsHasApproved(); |
| 313 | //Ajax change value |
| 314 | njtFileManager.ajaxRoleRestrictions(); |
| 315 | //Creat root path default |
| 316 | njtFileManager.clickedCreatRootPath(); |
| 317 | // End- Setting for `Select User Roles Restrictions to access` |
| 318 | |
| 319 | //Ajax settings |
| 320 | njtFileManager.ajaxSaveSettings(); |
| 321 | njtFileManager.ajaxSaveSettingsRestrictions(); |
| 322 | if(jQuery(".elfinder-theme-ext") > 0) { |
| 323 | jQuery(".elfinder-theme-ext").remove() |
| 324 | } |
| 325 | } |
| 326 | }); |
| 327 |