script.js 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812
  1. var lfm_route = location.origin + location.pathname;
  2. var show_list;
  3. var sort_type = 'alphabetic';
  4. var multi_selection_enabled = false;
  5. var selected = [];
  6. var items = [];
  7. $.fn.fab = function (options) {
  8. var menu = this;
  9. menu.addClass('fab-wrapper');
  10. var toggler = $('<a>')
  11. .addClass('fab-button fab-toggle')
  12. .append($('<i>').addClass('fas fa-plus'))
  13. .click(function () {
  14. menu.toggleClass('fab-expand');
  15. });
  16. menu.append(toggler);
  17. options.buttons.forEach(function (button) {
  18. toggler.before(
  19. $('<a>').addClass('fab-button fab-action')
  20. .attr('data-label', button.label)
  21. .attr('id', button.attrs.id)
  22. .append($('<i>').addClass(button.icon))
  23. .click(function () {
  24. menu.removeClass('fab-expand');
  25. })
  26. );
  27. });
  28. };
  29. $(document).ready(function () {
  30. $('#fab').fab({
  31. buttons: [
  32. {
  33. icon: 'fas fa-upload',
  34. label: lang['nav-upload'],
  35. attrs: {id: 'upload'}
  36. },
  37. {
  38. icon: 'fas fa-folder',
  39. label: lang['nav-new'],
  40. attrs: {id: 'add-folder'}
  41. }
  42. ]
  43. });
  44. actions.reverse().forEach(function (action) {
  45. $('#nav-buttons > ul').prepend(
  46. $('<li>').addClass('nav-item').append(
  47. $('<a>').addClass('nav-link d-none')
  48. .attr('data-action', action.name)
  49. .attr('data-multiple', action.multiple)
  50. .append($('<i>').addClass('fas fa-fw fa-' + action.icon))
  51. .append($('<span>').text(action.label))
  52. )
  53. );
  54. });
  55. sortings.forEach(function (sort) {
  56. $('#nav-buttons .dropdown-menu').append(
  57. $('<a>').addClass('dropdown-item').attr('data-sortby', sort.by)
  58. .append($('<i>').addClass('fas fa-fw fa-' + sort.icon))
  59. .append($('<span>').text(sort.label))
  60. .click(function() {
  61. sort_type = sort.by;
  62. loadItems();
  63. })
  64. );
  65. });
  66. loadFolders();
  67. performLfmRequest('errors')
  68. .done(function (response) {
  69. JSON.parse(response).forEach(function (message) {
  70. $('#alerts').append(
  71. $('<div>').addClass('alert alert-warning')
  72. .append($('<i>').addClass('fas fa-exclamation-circle'))
  73. .append(' ' + message)
  74. );
  75. });
  76. });
  77. $(window).on('dragenter', function(){
  78. $('#uploadModal').modal('show');
  79. });
  80. if (usingWysiwygEditor()) {
  81. $('#multi_selection_toggle').hide();
  82. }
  83. });
  84. // ======================
  85. // == Navbar actions ==
  86. // ======================
  87. $('#multi_selection_toggle').click(function () {
  88. multi_selection_enabled = !multi_selection_enabled;
  89. $('#multi_selection_toggle i')
  90. .toggleClass('fa-times', multi_selection_enabled)
  91. .toggleClass('fa-check-double', !multi_selection_enabled);
  92. if (!multi_selection_enabled) {
  93. clearSelected();
  94. }
  95. });
  96. $('#to-previous').click(function () {
  97. var previous_dir = getPreviousDir();
  98. if (previous_dir == '') return;
  99. goTo(previous_dir);
  100. });
  101. function toggleMobileTree(should_display) {
  102. if (should_display === undefined) {
  103. should_display = !$('#tree').hasClass('in');
  104. }
  105. $('#tree').toggleClass('in', should_display);
  106. }
  107. $('#show_tree').click(function (e) {
  108. toggleMobileTree();
  109. });
  110. $('#main').click(function (e) {
  111. if ($('#tree').hasClass('in')) {
  112. toggleMobileTree(false);
  113. }
  114. });
  115. $(document).on('click', '#add-folder', function () {
  116. dialog(lang['message-name'], '', createFolder);
  117. });
  118. $(document).on('click', '#upload', function () {
  119. $('#uploadModal').modal('show');
  120. });
  121. $(document).on('click', '[data-display]', function() {
  122. show_list = $(this).data('display');
  123. loadItems();
  124. });
  125. $(document).on('click', '[data-action]', function() {
  126. window[$(this).data('action')]($(this).data('multiple') ? getSelectedItems() : getOneSelectedElement());
  127. });
  128. // ==========================
  129. // == Multiple Selection ==
  130. // ==========================
  131. function toggleSelected (e) {
  132. if (!multi_selection_enabled) {
  133. selected = [];
  134. }
  135. var sequence = $(e.target).closest('a').data('id');
  136. var element_index = selected.indexOf(sequence);
  137. if (element_index === -1) {
  138. selected.push(sequence);
  139. } else {
  140. selected.splice(element_index, 1);
  141. }
  142. updateSelectedStyle();
  143. }
  144. function clearSelected () {
  145. selected = [];
  146. multi_selection_enabled = false;
  147. updateSelectedStyle();
  148. }
  149. function updateSelectedStyle() {
  150. items.forEach(function (item, index) {
  151. $('[data-id=' + index + ']')
  152. .find('.square')
  153. .toggleClass('selected', selected.indexOf(index) > -1);
  154. });
  155. toggleActions();
  156. }
  157. function getOneSelectedElement(orderOfItem) {
  158. var index = orderOfItem !== undefined ? orderOfItem : selected[0];
  159. return items[index];
  160. }
  161. function getSelectedItems() {
  162. return selected.reduce(function (arr_objects, id) {
  163. arr_objects.push(getOneSelectedElement(id));
  164. return arr_objects
  165. }, []);
  166. }
  167. function toggleActions() {
  168. var one_selected = selected.length === 1;
  169. var many_selected = selected.length >= 1;
  170. var only_image = getSelectedItems()
  171. .filter(function (item) { return !item.is_image; })
  172. .length === 0;
  173. var only_file = getSelectedItems()
  174. .filter(function (item) { return !item.is_file; })
  175. .length === 0;
  176. $('[data-action=use]').toggleClass('d-none', !(many_selected && only_file));
  177. $('[data-action=rename]').toggleClass('d-none', !one_selected);
  178. $('[data-action=preview]').toggleClass('d-none', !(many_selected && only_file));
  179. $('[data-action=move]').toggleClass('d-none', !many_selected);
  180. $('[data-action=download]').toggleClass('d-none', !(many_selected && only_file));
  181. $('[data-action=resize]').toggleClass('d-none', !(one_selected && only_image));
  182. $('[data-action=crop]').toggleClass('d-none', !(one_selected && only_image));
  183. $('[data-action=trash]').toggleClass('d-none', !many_selected);
  184. $('[data-action=open]').toggleClass('d-none', !one_selected || only_file);
  185. $('#multi_selection_toggle').toggleClass('d-none', usingWysiwygEditor() || !many_selected);
  186. $('#actions').toggleClass('d-none', selected.length === 0);
  187. $('#fab').toggleClass('d-none', selected.length !== 0);
  188. }
  189. // ======================
  190. // == Folder actions ==
  191. // ======================
  192. $(document).on('click', '#tree a', function (e) {
  193. goTo($(e.target).closest('a').data('path'));
  194. toggleMobileTree(false);
  195. });
  196. function goTo(new_dir) {
  197. $('#working_dir').val(new_dir);
  198. loadItems();
  199. }
  200. function getPreviousDir() {
  201. var working_dir = $('#working_dir').val();
  202. return working_dir.substring(0, working_dir.lastIndexOf('/'));
  203. }
  204. function setOpenFolders() {
  205. $('#tree [data-path]').each(function (index, folder) {
  206. // close folders that are not parent
  207. var should_open = ($('#working_dir').val() + '/').startsWith($(folder).data('path') + '/');
  208. $(folder).children('i')
  209. .toggleClass('fa-folder-open', should_open)
  210. .toggleClass('fa-folder', !should_open);
  211. });
  212. $('#tree .nav-item').removeClass('active');
  213. $('#tree [data-path="' + $('#working_dir').val() + '"]').parent('.nav-item').addClass('active');
  214. }
  215. // ====================
  216. // == Ajax actions ==
  217. // ====================
  218. function performLfmRequest(url, parameter, type) {
  219. var data = defaultParameters();
  220. if (parameter != null) {
  221. $.each(parameter, function (key, value) {
  222. data[key] = value;
  223. });
  224. }
  225. return $.ajax({
  226. type: 'GET',
  227. beforeSend: function(request) {
  228. var token = getUrlParam('token');
  229. if (token !== null) {
  230. request.setRequestHeader("Authorization", 'Bearer ' + token);
  231. }
  232. },
  233. dataType: type || 'text',
  234. url: lfm_route + '/' + url,
  235. data: data,
  236. cache: false
  237. }).fail(function (jqXHR, textStatus, errorThrown) {
  238. displayErrorResponse(jqXHR);
  239. });
  240. }
  241. function displayErrorResponse(jqXHR) {
  242. notify('<div style="max-height:50vh;overflow: scroll;">' + jqXHR.responseText + '</div>');
  243. }
  244. var refreshFoldersAndItems = function (data) {
  245. loadFolders();
  246. if (data != 'OK') {
  247. data = Array.isArray(data) ? data.join('<br/>') : data;
  248. notify(data);
  249. }
  250. };
  251. var hideNavAndShowEditor = function (data) {
  252. $('#nav-buttons > ul').addClass('d-none');
  253. $('#content').html(data);
  254. $('#pagination').removeClass('preserve_actions_space')
  255. clearSelected();
  256. }
  257. function loadFolders() {
  258. performLfmRequest('folders', {}, 'html')
  259. .done(function (data) {
  260. $('#tree').html(data);
  261. loadItems();
  262. });
  263. }
  264. function generatePaginationHTML(el, args) {
  265. var template = '<li class="page-item"><\/li>';
  266. var linkTemplate = '<a class="page-link"><\/a>';
  267. var currentPage = args.currentPage;
  268. var totalPage = args.totalPage;
  269. var rangeStart = args.rangeStart;
  270. var rangeEnd = args.rangeEnd;
  271. var i;
  272. // Disable page range, display all the pages
  273. if (args.pageRange === null) {
  274. for (i = 1; i <= totalPage; i++) {
  275. var button = $(template)
  276. .attr('data-num', i)
  277. .append($(linkTemplate).html(i));
  278. if (i == currentPage) {
  279. button.addClass('active');
  280. }
  281. el.append(button);
  282. }
  283. return;
  284. }
  285. if (rangeStart <= 3) {
  286. for (i = 1; i < rangeStart; i++) {
  287. var button = $(template)
  288. .attr('data-num', i)
  289. .append($(linkTemplate).html(i));
  290. if (i == currentPage) {
  291. button.addClass('active');
  292. }
  293. el.append(button);
  294. }
  295. } else {
  296. var button = $(template)
  297. .attr('data-num', 1)
  298. .append($(linkTemplate).html(1));
  299. el.append(button);
  300. var button = $(template)
  301. .addClass('disabled')
  302. .append($(linkTemplate).html('...'));
  303. el.append(button);
  304. }
  305. for (i = rangeStart; i <= rangeEnd; i++) {
  306. var button = $(template)
  307. .attr('data-num', i)
  308. .append($(linkTemplate).html(i));
  309. if (i == currentPage) {
  310. button.addClass('active');
  311. }
  312. el.append(button);
  313. }
  314. if (rangeEnd >= totalPage - 2) {
  315. for (i = rangeEnd + 1; i <= totalPage; i++) {
  316. var button = $(template)
  317. .attr('data-num', i)
  318. .append($(linkTemplate).html(i));
  319. el.append(button);
  320. }
  321. } else {
  322. var button = $(template)
  323. .addClass('disabled')
  324. .append($(linkTemplate).html('...'));
  325. el.append(button);
  326. var button = $(template)
  327. .attr('data-num', totalPage)
  328. .append($(linkTemplate).html(totalPage));
  329. el.append(button);
  330. }
  331. }
  332. function createPagination(paginationSetting) {
  333. var el = $('<ul class="pagination" role="navigation"></ul>');
  334. var currentPage = paginationSetting.current_page;
  335. var pageRange = 5;
  336. var totalPage = Math.ceil(paginationSetting.total / paginationSetting.per_page);
  337. var rangeStart = currentPage - pageRange;
  338. var rangeEnd = currentPage + pageRange;
  339. if (rangeEnd > totalPage) {
  340. rangeEnd = totalPage;
  341. rangeStart = totalPage - pageRange * 2;
  342. rangeStart = rangeStart < 1 ? 1 : rangeStart;
  343. }
  344. if (rangeStart <= 1) {
  345. rangeStart = 1;
  346. rangeEnd = Math.min(pageRange * 2 + 1, totalPage);
  347. }
  348. generatePaginationHTML(el, {
  349. totalPage: totalPage,
  350. currentPage: currentPage,
  351. pageRange: pageRange,
  352. rangeStart: rangeStart,
  353. rangeEnd: rangeEnd
  354. });
  355. $('#pagination').append(el);
  356. }
  357. function loadItems(page) {
  358. loading(true);
  359. performLfmRequest('jsonitems', {show_list: show_list, sort_type: sort_type, page: page || 1}, 'html')
  360. .done(function (data) {
  361. selected = [];
  362. var response = JSON.parse(data);
  363. var working_dir = response.working_dir;
  364. items = response.items;
  365. var hasItems = items.length !== 0;
  366. var hasPaginator = !!response.paginator;
  367. $('#empty').toggleClass('d-none', hasItems);
  368. $('#content').html('').removeAttr('class');
  369. $('#pagination').html('').removeAttr('class');
  370. if (hasItems) {
  371. $('#content').addClass(response.display);
  372. $('#pagination').addClass('preserve_actions_space');
  373. items.forEach(function (item, index) {
  374. var template = $('#item-template').clone()
  375. .removeAttr('id class')
  376. .attr('data-id', index)
  377. .click(toggleSelected)
  378. .dblclick(function (e) {
  379. if (item.is_file) {
  380. use(getSelectedItems());
  381. } else {
  382. goTo(item.url);
  383. }
  384. });
  385. if (item.thumb_url) {
  386. var image = $('<div>').css('background-image', 'url("' + item.thumb_url + '?timestamp=' + item.time + '")');
  387. } else {
  388. var icon = $('<div>').addClass('ico');
  389. var image = $('<div>').addClass('mime-icon ico-' + item.icon).append(icon);
  390. }
  391. template.find('.square').append(image);
  392. template.find('.item_name').text(item.name);
  393. template.find('time').text((new Date(item.time * 1000)).toLocaleString());
  394. $('#content').append(template);
  395. });
  396. }
  397. if (hasPaginator) {
  398. createPagination(response.paginator);
  399. $('#pagination a').on('click', function(event) {
  400. event.preventDefault();
  401. loadItems($(this).closest('li')[0].getAttribute('data-num'));
  402. return false;
  403. });
  404. }
  405. $('#nav-buttons > ul').removeClass('d-none');
  406. $('#working_dir').val(working_dir);
  407. console.log('Current working_dir : ' + working_dir);
  408. var breadcrumbs = [];
  409. var validSegments = working_dir.split('/').filter(function (e) { return e; });
  410. validSegments.forEach(function (segment, index) {
  411. if (index === 0) {
  412. // set root folder name as the first breadcrumb
  413. breadcrumbs.push($("[data-path='/" + segment + "']").text());
  414. } else {
  415. breadcrumbs.push(segment);
  416. }
  417. });
  418. $('#current_folder').text(breadcrumbs[breadcrumbs.length - 1]);
  419. $('#breadcrumbs > ol').html('');
  420. breadcrumbs.forEach(function (breadcrumb, index) {
  421. var li = $('<li>').addClass('breadcrumb-item').text(breadcrumb);
  422. if (index === breadcrumbs.length - 1) {
  423. li.addClass('active').attr('aria-current', 'page');
  424. } else {
  425. li.click(function () {
  426. // go to corresponding path
  427. goTo('/' + validSegments.slice(0, 1 + index).join('/'));
  428. });
  429. }
  430. $('#breadcrumbs > ol').append(li);
  431. });
  432. var atRootFolder = getPreviousDir() == '';
  433. $('#to-previous').toggleClass('d-none invisible-lg', atRootFolder);
  434. $('#show_tree').toggleClass('d-none', !atRootFolder).toggleClass('d-block', atRootFolder);
  435. setOpenFolders();
  436. loading(false);
  437. toggleActions();
  438. });
  439. }
  440. function loading(show_loading) {
  441. $('#loading').toggleClass('d-none', !show_loading);
  442. }
  443. function createFolder(folder_name) {
  444. performLfmRequest('newfolder', {name: folder_name})
  445. .done(refreshFoldersAndItems);
  446. }
  447. // ==================================
  448. // == File Actions ==
  449. // ==================================
  450. function rename(item) {
  451. dialog(lang['message-rename'], item.name, function (new_name) {
  452. performLfmRequest('rename', {
  453. file: item.name,
  454. new_name: new_name
  455. }).done(refreshFoldersAndItems);
  456. });
  457. }
  458. function trash(items) {
  459. notify(lang['message-delete'], function () {
  460. performLfmRequest('delete', {
  461. items: items.map(function (item) { return item.name; })
  462. }).done(refreshFoldersAndItems)
  463. });
  464. }
  465. function crop(item) {
  466. performLfmRequest('crop', {img: item.name})
  467. .done(hideNavAndShowEditor);
  468. }
  469. function resize(item) {
  470. performLfmRequest('resize', {img: item.name})
  471. .done(hideNavAndShowEditor);
  472. }
  473. function download(items) {
  474. items.forEach(function (item, index) {
  475. var data = defaultParameters();
  476. data['file'] = item.name;
  477. var token = getUrlParam('token');
  478. if (token) {
  479. data['token'] = token;
  480. }
  481. setTimeout(function () {
  482. location.href = lfm_route + '/download?' + $.param(data);
  483. }, index * 100);
  484. });
  485. }
  486. function open(item) {
  487. goTo(item.url);
  488. }
  489. function preview(items) {
  490. var carousel = $('#carouselTemplate').clone().attr('id', 'previewCarousel').removeClass('d-none');
  491. var imageTemplate = carousel.find('.carousel-item').clone().removeClass('active');
  492. var indicatorTemplate = carousel.find('.carousel-indicators > li').clone().removeClass('active');
  493. carousel.children('.carousel-inner').html('');
  494. carousel.children('.carousel-indicators').html('');
  495. carousel.children('.carousel-indicators,.carousel-control-prev,.carousel-control-next').toggle(items.length > 1);
  496. items.forEach(function (item, index) {
  497. var carouselItem = imageTemplate.clone()
  498. .addClass(index === 0 ? 'active' : '');
  499. if (item.thumb_url) {
  500. carouselItem.find('.carousel-image').css('background-image', 'url(\'' + item.url + '?timestamp=' + item.time + '\')');
  501. } else {
  502. carouselItem.find('.carousel-image').css('width', '50vh').append($('<div>').addClass('mime-icon ico-' + item.icon));
  503. }
  504. carouselItem.find('.carousel-label').attr('target', '_blank').attr('href', item.url)
  505. .append(item.name)
  506. .append($('<i class="fas fa-external-link-alt ml-2"></i>'));
  507. carousel.children('.carousel-inner').append(carouselItem);
  508. var carouselIndicator = indicatorTemplate.clone()
  509. .addClass(index === 0 ? 'active' : '')
  510. .attr('data-slide-to', index);
  511. carousel.children('.carousel-indicators').append(carouselIndicator);
  512. });
  513. // carousel swipe control
  514. var touchStartX = null;
  515. carousel.on('touchstart', function (event) {
  516. var e = event.originalEvent;
  517. if (e.touches.length == 1) {
  518. var touch = e.touches[0];
  519. touchStartX = touch.pageX;
  520. }
  521. }).on('touchmove', function (event) {
  522. var e = event.originalEvent;
  523. if (touchStartX != null) {
  524. var touchCurrentX = e.changedTouches[0].pageX;
  525. if ((touchCurrentX - touchStartX) > 60) {
  526. touchStartX = null;
  527. carousel.carousel('prev');
  528. } else if ((touchStartX - touchCurrentX) > 60) {
  529. touchStartX = null;
  530. carousel.carousel('next');
  531. }
  532. }
  533. }).on('touchend', function () {
  534. touchStartX = null;
  535. });
  536. // end carousel swipe control
  537. notify(carousel);
  538. }
  539. function move(items) {
  540. performLfmRequest('move', { items: items.map(function (item) { return item.name; }) })
  541. .done(refreshFoldersAndItems);
  542. }
  543. function getUrlParam(paramName) {
  544. var reParam = new RegExp('(?:[\?&]|&)' + paramName + '=([^&]+)', 'i');
  545. var match = window.location.search.match(reParam);
  546. return ( match && match.length > 1 ) ? match[1] : null;
  547. }
  548. function use(items) {
  549. function useTinymce3(url) {
  550. if (!usingTinymce3()) { return; }
  551. var win = tinyMCEPopup.getWindowArg("window");
  552. win.document.getElementById(tinyMCEPopup.getWindowArg("input")).value = url;
  553. if (typeof(win.ImageDialog) != "undefined") {
  554. // Update image dimensions
  555. if (win.ImageDialog.getImageData) {
  556. win.ImageDialog.getImageData();
  557. }
  558. // Preview if necessary
  559. if (win.ImageDialog.showPreviewImage) {
  560. win.ImageDialog.showPreviewImage(url);
  561. }
  562. }
  563. tinyMCEPopup.close();
  564. }
  565. function useTinymce4AndColorbox(url) {
  566. if (!usingTinymce4AndColorbox()) { return; }
  567. parent.document.getElementById(getUrlParam('field_name')).value = url;
  568. if(typeof parent.tinyMCE !== "undefined") {
  569. parent.tinyMCE.activeEditor.windowManager.close();
  570. }
  571. if(typeof parent.$.fn.colorbox !== "undefined") {
  572. parent.$.fn.colorbox.close();
  573. }
  574. }
  575. function useTinymce5(url){
  576. if (!usingTinymce5()) { return; }
  577. parent.postMessage({
  578. mceAction: 'insert',
  579. content: url
  580. });
  581. parent.postMessage({ mceAction: 'close' });
  582. }
  583. function useCkeditor3(url) {
  584. if (!usingCkeditor3()) { return; }
  585. if (window.opener) {
  586. // Popup
  587. window.opener.CKEDITOR.tools.callFunction(getUrlParam('CKEditorFuncNum'), url);
  588. } else {
  589. // Modal (in iframe)
  590. parent.CKEDITOR.tools.callFunction(getUrlParam('CKEditorFuncNum'), url);
  591. parent.CKEDITOR.tools.callFunction(getUrlParam('CKEditorCleanUpFuncNum'));
  592. }
  593. }
  594. function useFckeditor2(url) {
  595. if (!usingFckeditor2()) { return; }
  596. var p = url;
  597. var w = data['Properties']['Width'];
  598. var h = data['Properties']['Height'];
  599. window.opener.SetUrl(p,w,h);
  600. }
  601. var url = items[0].url;
  602. var callback = getUrlParam('callback');
  603. var useFileSucceeded = true;
  604. if (usingWysiwygEditor()) {
  605. useTinymce3(url);
  606. useTinymce4AndColorbox(url);
  607. useTinymce5(url);
  608. useCkeditor3(url);
  609. useFckeditor2(url);
  610. } else if (callback && window[callback]) {
  611. window[callback](getSelectedItems());
  612. } else if (callback && parent[callback]) {
  613. parent[callback](getSelecteditems());
  614. } else if (window.opener) { // standalone button or other situations
  615. window.opener.SetUrl(getSelectedItems());
  616. } else {
  617. useFileSucceeded = false;
  618. }
  619. if (useFileSucceeded) {
  620. if (window.opener) {
  621. window.close();
  622. }
  623. } else {
  624. console.log('window.opener not found');
  625. // No editor found, open/download file using browser's default method
  626. window.open(url);
  627. }
  628. }
  629. //end useFile
  630. // ==================================
  631. // == WYSIWYG Editors Check ==
  632. // ==================================
  633. function usingTinymce3() {
  634. return !!window.tinyMCEPopup;
  635. }
  636. function usingTinymce4AndColorbox() {
  637. return !!getUrlParam('field_name');
  638. }
  639. function usingTinymce5(){
  640. return !!getUrlParam('editor');
  641. }
  642. function usingCkeditor3() {
  643. return !!getUrlParam('CKEditor') || !!getUrlParam('CKEditorCleanUpFuncNum');
  644. }
  645. function usingFckeditor2() {
  646. return window.opener && typeof data != 'undefined' && data['Properties']['Width'] != '';
  647. }
  648. function usingWysiwygEditor() {
  649. return usingTinymce3() || usingTinymce4AndColorbox() || usingTinymce5() || usingCkeditor3() || usingFckeditor2();
  650. }
  651. // ==================================
  652. // == Others ==
  653. // ==================================
  654. function defaultParameters() {
  655. return {
  656. working_dir: $('#working_dir').val(),
  657. type: $('#type').val()
  658. };
  659. }
  660. function notImp() {
  661. notify('Not yet implemented!');
  662. }
  663. function notify(body, callback) {
  664. $('#notify').find('.btn-primary').toggle(callback !== undefined);
  665. $('#notify').find('.btn-primary').unbind().click(callback);
  666. $('#notify').modal('show').find('.modal-body').html(body);
  667. }
  668. function dialog(title, value, callback) {
  669. $('#dialog').find('input').val(value);
  670. $('#dialog').on('shown.bs.modal', function () {
  671. $('#dialog').find('input').focus();
  672. });
  673. $('#dialog').find('.btn-primary').unbind().click(function (e) {
  674. callback($('#dialog').find('input').val());
  675. });
  676. $('#dialog').modal('show').find('.modal-title').text(title);
  677. }