|
| 1 | +var ds = "{{ addslashes(DIRECTORY_SEPARATOR) }}"; |
| 2 | +var home_dir = ds + "{{ (Config::get('lfm.allow_multi_user')) ? Auth::user()->user_field : '' }}"; |
| 3 | +var shared_folder = ds + "{{ Config::get('lfm.shared_folder_name') }}"; |
| 4 | +var image_url = "{{ Config::get('lfm.images_url') }}"; |
| 5 | +var file_url = "{{ Config::get('lfm.files_url') }}"; |
| 6 | + |
| 7 | +$(document).ready(function () { |
| 8 | + bootbox.setDefaults({locale:"{{ Lang::get('laravel-filemanager::lfm.locale-bootbox') }}"}); |
| 9 | + // load folders |
| 10 | + loadFolders(); |
| 11 | + loadItems(); |
| 12 | + setOpenFolders(); |
| 13 | +}); |
| 14 | + |
| 15 | +// ====================== |
| 16 | +// == Navbar actions == |
| 17 | +// ====================== |
| 18 | + |
| 19 | +$('#to-previous').click(function () { |
| 20 | + var working_dir = $('#working_dir').val(); |
| 21 | + var last_ds = working_dir.lastIndexOf(ds); |
| 22 | + var previous_dir = working_dir.substring(0, last_ds); |
| 23 | + $('#working_dir').val(previous_dir); |
| 24 | + loadItems(); |
| 25 | + setOpenFolders(); |
| 26 | +}); |
| 27 | + |
| 28 | +$('#add-folder').click(function () { |
| 29 | + bootbox.prompt("{{ Lang::get('laravel-filemanager::lfm.message-name') }}", function (result) { |
| 30 | + if (result !== null) { |
| 31 | + createFolder(result); |
| 32 | + } |
| 33 | + }); |
| 34 | +}); |
| 35 | + |
| 36 | +$('#upload-btn').click(function () { |
| 37 | + var options = { |
| 38 | + beforeSubmit: showRequest, |
| 39 | + success: showResponse |
| 40 | + }; |
| 41 | + |
| 42 | + function showRequest(formData, jqForm, options) { |
| 43 | + $('#upload-btn').html('<i class="fa fa-refresh fa-spin"></i> {{ Lang::get("laravel-filemanager::lfm.btn-uploading") }}'); |
| 44 | + return true; |
| 45 | + } |
| 46 | + |
| 47 | + function showResponse(responseText, statusText, xhr, $form) { |
| 48 | + $('#uploadModal').modal('hide'); |
| 49 | + $('#upload-btn').html('{{ Lang::get("laravel-filemanager::lfm.btn-upload") }}'); |
| 50 | + if (responseText != 'OK'){ |
| 51 | + notify(responseText); |
| 52 | + } |
| 53 | + $('#upload').val(''); |
| 54 | + loadItems(); |
| 55 | + } |
| 56 | + |
| 57 | + $('#uploadForm').ajaxSubmit(options); |
| 58 | + return false; |
| 59 | +}); |
| 60 | + |
| 61 | +$('#thumbnail-display').click(function () { |
| 62 | + $('#show_list').val(0); |
| 63 | + loadItems(); |
| 64 | +}); |
| 65 | + |
| 66 | +$('#list-display').click(function () { |
| 67 | + $('#show_list').val(1); |
| 68 | + loadItems(); |
| 69 | +}); |
| 70 | + |
| 71 | +// ====================== |
| 72 | +// == Folder actions == |
| 73 | +// ====================== |
| 74 | + |
| 75 | +$(document).on('click', '.folder-item', function (e) { |
| 76 | + clickFolder($(this).data('id')); |
| 77 | +}); |
| 78 | + |
| 79 | +function clickFolder(new_dir) { |
| 80 | + $('#working_dir').val(new_dir); |
| 81 | + setOpenFolders(); |
| 82 | + loadItems(); |
| 83 | +} |
| 84 | + |
| 85 | +function dir_starts_with(str) { |
| 86 | + return $('#working_dir').val().indexOf(str) === 0; |
| 87 | +} |
| 88 | + |
| 89 | +function setOpenFolders() { |
| 90 | + var folders = $('.folder-item'); |
| 91 | + |
| 92 | + for (var i = folders.length - 1; i >= 0; i--) { |
| 93 | + // close folders that are not parent |
| 94 | + if (! dir_starts_with($(folders[i]).data('id'))) { |
| 95 | + $(folders[i]).children('i').removeClass('fa-folder-open').addClass('fa-folder'); |
| 96 | + } else { |
| 97 | + $(folders[i]).children('i').removeClass('fa-folder').addClass('fa-folder-open'); |
| 98 | + } |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +// ==================== |
| 103 | +// == Ajax actions == |
| 104 | +// ==================== |
| 105 | + |
| 106 | +function loadFolders() { |
| 107 | + $.ajax({ |
| 108 | + type: 'GET', |
| 109 | + dataType: 'html', |
| 110 | + url: '/laravel-filemanager/folders', |
| 111 | + data: { |
| 112 | + working_dir: $('#working_dir').val(), |
| 113 | + show_list: $('#show_list').val(), |
| 114 | + type: $('#type').val() |
| 115 | + }, |
| 116 | + cache: false |
| 117 | + }).done(function (data) { |
| 118 | + $('#tree1').html(data); |
| 119 | + }); |
| 120 | +} |
| 121 | + |
| 122 | +function loadItems() { |
| 123 | + var working_dir = $('#working_dir').val(); |
| 124 | + console.log('Current working_dir : ' + working_dir); |
| 125 | + |
| 126 | + $.ajax({ |
| 127 | + type: 'GET', |
| 128 | + dataType: 'html', |
| 129 | + url: '/laravel-filemanager/jsonitems', |
| 130 | + data: { |
| 131 | + working_dir: working_dir, |
| 132 | + show_list: $('#show_list').val(), |
| 133 | + type: $('#type').val() |
| 134 | + }, |
| 135 | + cache: false |
| 136 | + }).done(function (data) { |
| 137 | + $('#content').html(data); |
| 138 | + $('#nav-buttons').removeClass('hidden'); |
| 139 | + $('.dropdown-toggle').dropdown(); |
| 140 | + setOpenFolders(); |
| 141 | + }); |
| 142 | +} |
| 143 | + |
| 144 | +function createFolder(folder_name) { |
| 145 | + $.ajax({ |
| 146 | + type: 'GET', |
| 147 | + dataType: 'text', |
| 148 | + url: '/laravel-filemanager/newfolder', |
| 149 | + data: { |
| 150 | + name: folder_name, |
| 151 | + working_dir: $('#working_dir').val(), |
| 152 | + type: $('#type').val() |
| 153 | + }, |
| 154 | + cache: false |
| 155 | + }).done(function (data) { |
| 156 | + if (data == 'OK') { |
| 157 | + loadFolders(); |
| 158 | + loadItems(); |
| 159 | + setOpenFolders(); |
| 160 | + } else { |
| 161 | + notify(data); |
| 162 | + } |
| 163 | + }); |
| 164 | +} |
| 165 | + |
| 166 | +function rename(item_name) { |
| 167 | + bootbox.prompt({ |
| 168 | + title: "{{ Lang::get('laravel-filemanager::lfm.message-rename') }}", |
| 169 | + value: item_name, |
| 170 | + callback: function (result) { |
| 171 | + if (result !== null) { |
| 172 | + $.ajax({ |
| 173 | + type: 'GET', |
| 174 | + dataType: 'text', |
| 175 | + url: '/laravel-filemanager/rename', |
| 176 | + data: { |
| 177 | + file: item_name, |
| 178 | + working_dir: $('#working_dir').val(), |
| 179 | + new_name: result, |
| 180 | + type: $('#type').val() |
| 181 | + }, |
| 182 | + cache: false |
| 183 | + }).done(function (data) { |
| 184 | + if (data == 'OK') { |
| 185 | + loadItems(); |
| 186 | + loadFolders(); |
| 187 | + } else { |
| 188 | + notify(data); |
| 189 | + } |
| 190 | + }); |
| 191 | + } |
| 192 | + } |
| 193 | + }); |
| 194 | +} |
| 195 | + |
| 196 | +function trash(item_name) { |
| 197 | + bootbox.confirm("{{ Lang::get('laravel-filemanager::lfm.message-delete') }}", function (result) { |
| 198 | + if (result == true) { |
| 199 | + $.ajax({ |
| 200 | + type: 'GET', |
| 201 | + dataType: 'text', |
| 202 | + url: '/laravel-filemanager/delete', |
| 203 | + data: { |
| 204 | + working_dir: $('#working_dir').val(), |
| 205 | + items: item_name, |
| 206 | + type: $('#type').val() |
| 207 | + }, |
| 208 | + cache: false |
| 209 | + }).done(function (data) { |
| 210 | + if (data != 'OK') { |
| 211 | + notify(data); |
| 212 | + } else { |
| 213 | + if ($('#working_dir').val() === home_dir || $('#working_dir').val() === shared_folder) { |
| 214 | + loadFolders(); |
| 215 | + } |
| 216 | + loadItems(); |
| 217 | + } |
| 218 | + }); |
| 219 | + } |
| 220 | + }); |
| 221 | +} |
| 222 | + |
| 223 | +function cropImage(image_name) { |
| 224 | + $.ajax({ |
| 225 | + type: 'GET', |
| 226 | + dataType: 'text', |
| 227 | + url: '/laravel-filemanager/crop', |
| 228 | + data: { |
| 229 | + img: image_name, |
| 230 | + working_dir: $('#working_dir').val(), |
| 231 | + type: $('#type').val() |
| 232 | + }, |
| 233 | + cache: false |
| 234 | + }).done(function (data) { |
| 235 | + $('#nav-buttons').addClass('hidden'); |
| 236 | + $('#content').html(data); |
| 237 | + }); |
| 238 | +} |
| 239 | + |
| 240 | +function resizeImage(image_name) { |
| 241 | + $.ajax({ |
| 242 | + type: 'GET', |
| 243 | + dataType: 'text', |
| 244 | + url: '/laravel-filemanager/resize', |
| 245 | + data: { |
| 246 | + img: image_name, |
| 247 | + working_dir: $('#working_dir').val(), |
| 248 | + type: $('#type').val() |
| 249 | + }, |
| 250 | + cache: false |
| 251 | + }).done(function (data) { |
| 252 | + $('#nav-buttons').addClass('hidden'); |
| 253 | + $('#content').html(data); |
| 254 | + }); |
| 255 | +} |
| 256 | + |
| 257 | +function download(file_name) { |
| 258 | + location.href = '/laravel-filemanager/download?' |
| 259 | + + 'working_dir=' |
| 260 | + + $('#working_dir').val() |
| 261 | + + '&type=' |
| 262 | + + $('#type').val() |
| 263 | + + '&file=' |
| 264 | + + file_name; |
| 265 | +} |
| 266 | + |
| 267 | +// ================================== |
| 268 | +// == Ckeditor, Bootbox, preview == |
| 269 | +// ================================== |
| 270 | + |
| 271 | +function useFile(file) { |
| 272 | + var path = $('#working_dir').val(); |
| 273 | + |
| 274 | + var item_url = image_url; |
| 275 | + |
| 276 | + @if ("Images" !== $file_type) |
| 277 | + item_url = file_url; |
| 278 | + @endif |
| 279 | + |
| 280 | + if (path.indexOf(ds) === 0) { |
| 281 | + path = path.substring(1); |
| 282 | + } |
| 283 | + |
| 284 | + if (path != ds) { |
| 285 | + item_url = item_url + path + ds; |
| 286 | + } |
| 287 | + |
| 288 | + function getUrlParam(paramName) { |
| 289 | + var reParam = new RegExp('(?:[\?&]|&)' + paramName + '=([^&]+)', 'i'); |
| 290 | + var match = window.location.search.match(reParam); |
| 291 | + return ( match && match.length > 1 ) ? match[1] : null; |
| 292 | + } |
| 293 | + |
| 294 | + var field_name = getUrlParam('field_name'); |
| 295 | + var url = item_url + file; |
| 296 | + url = url.replace(/\\/g,"/"); |
| 297 | + |
| 298 | + if (window.opener || window.tinyMCEPopup || field_name || getUrlParam('CKEditorCleanUpFuncNum') || getUrlParam('CKEditor')) { |
| 299 | + if (window.tinyMCEPopup) { |
| 300 | + // use TinyMCE > 3.0 integration method |
| 301 | + var win = tinyMCEPopup.getWindowArg("window"); |
| 302 | + win.document.getElementById(tinyMCEPopup.getWindowArg("input")).value = url; |
| 303 | + if (typeof(win.ImageDialog) != "undefined") { |
| 304 | + // Update image dimensions |
| 305 | + if (win.ImageDialog.getImageData) |
| 306 | + win.ImageDialog.getImageData(); |
| 307 | + |
| 308 | + // Preview if necessary |
| 309 | + if (win.ImageDialog.showPreviewImage) |
| 310 | + win.ImageDialog.showPreviewImage(url); |
| 311 | + } |
| 312 | + tinyMCEPopup.close(); |
| 313 | + return; |
| 314 | + } |
| 315 | + |
| 316 | + // tinymce 4 and colorbox |
| 317 | + if (field_name) { |
| 318 | + parent.document.getElementById(field_name).value = url; |
| 319 | + |
| 320 | + if(typeof parent.tinyMCE !== "undefined") { |
| 321 | + parent.tinyMCE.activeEditor.windowManager.close(); |
| 322 | + } |
| 323 | + if(typeof parent.$.fn.colorbox !== "undefined") { |
| 324 | + parent.$.fn.colorbox.close(); |
| 325 | + } |
| 326 | + |
| 327 | + } else if(getUrlParam('CKEditor')) { |
| 328 | + // use CKEditor 3.0 + integration method |
| 329 | + if (window.opener) { |
| 330 | + // Popup |
| 331 | + window.opener.CKEDITOR.tools.callFunction(getUrlParam('CKEditorFuncNum'), url); |
| 332 | + } else { |
| 333 | + // Modal (in iframe) |
| 334 | + parent.CKEDITOR.tools.callFunction(getUrlParam('CKEditorFuncNum'), url); |
| 335 | + parent.CKEDITOR.tools.callFunction(getUrlParam('CKEditorCleanUpFuncNum')); |
| 336 | + } |
| 337 | + } else { |
| 338 | + // use FCKEditor 2.0 integration method |
| 339 | + if (typeof data != 'undefined' && data['Properties']['Width'] != '') { |
| 340 | + var p = url; |
| 341 | + var w = data['Properties']['Width']; |
| 342 | + var h = data['Properties']['Height']; |
| 343 | + window.opener.SetUrl(p,w,h); |
| 344 | + } else { |
| 345 | + window.opener.SetUrl(url); |
| 346 | + } |
| 347 | + } |
| 348 | + |
| 349 | + if (window.opener) { |
| 350 | + window.close(); |
| 351 | + } |
| 352 | + } else { |
| 353 | + $.prompt(lg.fck_select_integration); |
| 354 | + } |
| 355 | + |
| 356 | + window.close(); |
| 357 | +} |
| 358 | +//end useFile |
| 359 | + |
| 360 | +function notImp() { |
| 361 | + bootbox.alert('Not yet implemented!');; |
| 362 | +} |
| 363 | + |
| 364 | +function notify(x) { |
| 365 | + bootbox.alert(x); |
| 366 | +} |
| 367 | + |
| 368 | +function fileView(x) { |
| 369 | + var rnd = makeRandom(); |
| 370 | + var img_src = image_url + $('#working_dir').val() + ds + x; |
| 371 | + var img = "<img class='img img-responsive center-block' src='" + img_src + "'>"; |
| 372 | + $('#fileview_body').html(img); |
| 373 | + $('#fileViewModal').modal(); |
| 374 | +} |
| 375 | + |
| 376 | +function makeRandom() { |
| 377 | + var text = ''; |
| 378 | + var possible = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'; |
| 379 | + |
| 380 | + for (var i = 0; i < 20; i++) { |
| 381 | + text += possible.charAt(Math.floor(Math.random() * possible.length)); |
| 382 | + } |
| 383 | + return text; |
| 384 | +} |
| 385 | + |
0 commit comments