elfinder.callback.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. /*
  2. * elFinder Integration
  3. *
  4. * Copyright (c) 2010-2018, Alexey Sukhotin. All rights reserved.
  5. */
  6. // $Id: elfinder.callback.js 106 2011-02-26 08:19:56Z ph0enix $
  7. (function($) {
  8. /**
  9. * @class elFinder command "search"
  10. * Find files
  11. *
  12. * @author Dmitry (dio) Levashov
  13. **/
  14. elFinder.prototype.commands.search = function() {
  15. this.title = 'Find files';
  16. this.options = {ui: 'searchbutton'}
  17. this.alwaysEnabled = true;
  18. this.updateOnSelect = false;
  19. /**
  20. * Return command status.
  21. * Search does not support old api.
  22. *
  23. * @return Number
  24. **/
  25. this.getstate = function() {
  26. return 0;
  27. }
  28. /**
  29. * Send search request to backend.
  30. *
  31. * @param String search string
  32. * @return $.Deferred
  33. **/
  34. this.exec = function(q) {
  35. var fm = this.fm;
  36. if (typeof(q) == 'string' && q) {
  37. return fm.request({
  38. data: {cmd: 'search', elfinder_search_q: q},
  39. notify: {type: 'search', cnt: 1, hideCnt: true}
  40. });
  41. }
  42. fm.getUI('toolbar').find('.' + fm.res('class', 'searchbtn') + ' :text').focus();
  43. return $.Deferred().reject();
  44. }
  45. }
  46. elFinder.prototype.commands.test134 = function() {
  47. this.title = 'Test Command';
  48. //this.options = {ui : 'uploadbutton'}
  49. this.alwaysEnabled = true;
  50. this.updateOnSelect = false;
  51. this.state = 0;
  52. this.getstate = function() {
  53. return 0;
  54. }
  55. /**
  56. * Send search request to backend.
  57. *
  58. * @param String search string
  59. * @return $.Deferred
  60. **/
  61. this.exec = function(q) {
  62. var fm = this.fm;
  63. alert(Drupal.t('test command. arg=') + q);
  64. return $.Deferred().reject();
  65. }
  66. }
  67. $().ready(function() {
  68. var uiopts = elFinder.prototype._options.uiOptions.toolbar;
  69. var newOpts = new Array();
  70. var disabledCommands = Drupal.settings.elfinder.disabledCommands;
  71. for (var i in uiopts) {
  72. var optsGroup = uiopts[i];
  73. var newOptsGroup = Array();
  74. for (var j in optsGroup) {
  75. var found = false;
  76. for (var k in disabledCommands) {
  77. if (disabledCommands[k] == optsGroup[j]) {
  78. found = true;
  79. }
  80. }
  81. if (found == false) {
  82. newOptsGroup.push(optsGroup[j]);
  83. }
  84. }
  85. if (i == 0) {
  86. newOptsGroup.push('up');
  87. }
  88. if (newOptsGroup.length >= 1) {
  89. newOpts.push(newOptsGroup);
  90. }
  91. }
  92. /*elFinder.prototype._options.contextmenu.files.push('|');
  93. elFinder.prototype._options.contextmenu.files.push('rename'); */
  94. var contextMenuCwd = elFinder.prototype._options.contextmenu.cwd;
  95. var contextMenuFiles = elFinder.prototype._options.contextmenu.files;
  96. var contextMenuNavbar = elFinder.prototype._options.contextmenu.navbar;
  97. var newContextMenuCwd = Array();
  98. var newContextMenuFiles = Array();
  99. var newContextMenuNavbar = Array();
  100. for (var i in contextMenuCwd) {
  101. var found = false;
  102. for (var k in disabledCommands) {
  103. if (disabledCommands[k] == contextMenuCwd[i]) {
  104. found = true;
  105. }
  106. }
  107. if (found == false && contextMenuCwd[i] != '|') {
  108. newContextMenuCwd.push(contextMenuCwd[i]);
  109. }
  110. }
  111. for (var i in contextMenuFiles) {
  112. var found = false;
  113. for (var k in disabledCommands) {
  114. if (disabledCommands[k] == contextMenuFiles[i]) {
  115. found = true;
  116. }
  117. }
  118. if (found == false && contextMenuFiles[i] != '|') {
  119. newContextMenuFiles.push(contextMenuFiles[i]);
  120. }
  121. }
  122. for (var i in contextMenuNavbar) {
  123. var found = false;
  124. for (var k in disabledCommands) {
  125. if (disabledCommands[k] == contextMenuNavbar[i]) {
  126. found = true;
  127. }
  128. }
  129. if (found == false && contextMenuNavbar[i] != '|') {
  130. newContextMenuNavbar.push(contextMenuNavbar[i]);
  131. }
  132. }
  133. elFinder.prototype._options.uiOptions.toolbar = newOpts;
  134. elFinder.prototype._options.contextmenu.cwd = newContextMenuCwd;
  135. elFinder.prototype._options.contextmenu.files = newContextMenuFiles;
  136. elFinder.prototype._options.contextmenu.navbar = newContextMenuNavbar;
  137. //elFinder.prototype._options.ui.push('mouseover');
  138. });
  139. $().ready(function() {
  140. if (Drupal.settings.elfinder) {
  141. var editorApp = Drupal.settings.elfinder.editorApp;
  142. var elfinderOpts = {
  143. url: Drupal.settings.elfinder.connectorUrl,
  144. lang: Drupal.settings.elfinder.langCode,
  145. rememberLastDir: Drupal.settings.elfinder.rememberLastDir,
  146. closeOnEditorCallback: false,
  147. customData: {token: Drupal.settings.elfinder.token},
  148. commandsOptions_: {}
  149. }
  150. // help tab rendering issues in admin theme
  151. if (Drupal.settings.elfinder.browserMode != 'default' && elFinder.prototype._options.commandsOptions.help) {
  152. final_options = [];
  153. for (var i in elFinder.prototype._options.commandsOptions.help.view) {
  154. var option = elFinder.prototype._options.commandsOptions.help.view[i];
  155. if (option != 'help') {
  156. final_options.push(option);
  157. }
  158. }
  159. elFinder.prototype._options.commandsOptions.help['view'] = final_options;
  160. }
  161. /* Pushing all settings to elFinder */
  162. $.extend(elfinderOpts, Drupal.settings.elfinder);
  163. if (editorApp && typeof window[Drupal.settings.elfinder.editorCallback] == 'function') {
  164. elfinderOpts.editorCallback = window[Drupal.settings.elfinder.editorCallback];
  165. }
  166. if (editorApp && typeof window[Drupal.settings.elfinder.editorCallback] == 'function') {
  167. elfinderOpts.getFileCallback = window[Drupal.settings.elfinder.editorCallback];
  168. }
  169. if (elfinderOpts.api21) {
  170. console.log('2.1 api');
  171. elfinderOpts['commandsOptions']['info'] = {
  172. custom: {}
  173. };
  174. /* elfinderOpts['commandsOptions']['quicklook'] = {
  175. officeOnlineMimes : ['application/msword', 'application/vnd.ms-word', 'application/vnd.ms-excel', 'application/vnd.ms-powerpoint', 'application/vnd.openxmlformats-officedocument.wordprocessingml.document', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'],
  176. }*/
  177. var disabledCommands = Drupal.settings.elfinder.disabledCommands,
  178. viewDesc = $.inArray('desc', disabledCommands) == -1 ? true : false,
  179. editDesc = $.inArray('editdesc', disabledCommands) == -1 ? true : false,
  180. viewOwner = $.inArray('owner', disabledCommands) == -1 ? true : false,
  181. viewDownloads = $.inArray('downloadcount', disabledCommands) == -1 ? true : false;
  182. if (viewDesc || editDesc) {
  183. // Key is the same as your command name
  184. elfinderOpts['commandsOptions']['info']['custom']['desc'] = {
  185. // Field label
  186. label: Drupal.t('Description'),
  187. // HTML Template
  188. tpl: '<div class="elfinder-info-desc"><span class="elfinder-info-spinner"></span></div><div class="elfinder-info-save"></div>',
  189. // Action that sends the request to the server and get the description
  190. action: function(file, filemanager, dialog) {
  191. console.log('desc action');
  192. console.log('fm');
  193. console.log(file.mime);
  194. // Use the @filemanager object to issue a request
  195. filemanager.request({
  196. // Issuing the custom 'desc' command, targetting the selected file
  197. data: {cmd: 'desc', target: file.hash,},
  198. preventDefault: true,
  199. })
  200. // If the request fails, populate the field with 'Unknown'
  201. .fail(function() {
  202. console.log('desc fail');
  203. dialog.find('.elfinder-info-desc').html(filemanager.i18n('unknown'));
  204. })
  205. // When the request is successful, show the description
  206. .done(function(data) {
  207. console.log('desc done');
  208. dialog.find('.elfinder-info-desc').html(data.desc);
  209. if (editDesc) {
  210. //filemanager.lockfiles({files : [file.hash]})
  211. dialog.find('.elfinder-info-desc').html('<textarea cols="20" rows="5" id="elfinder-fm-file-desc" class="ui-widget ui-widget-content">' + data.desc + '</textarea>');
  212. $('.elfinder-info-save').append('<input type="button" id="elfinder-fm-file-desc-btn-save" class="ui-widget ui-button" value="' + filemanager.i18n('btnSave') + '" />');
  213. var btnSave = $('#elfinder-fm-file-desc-btn-save', dialog).button();
  214. console.log(btnSave);
  215. btnSave.click(function() {
  216. filemanager.lockfiles({files: [file.hash]});
  217. filemanager.request({
  218. data: {cmd: 'desc', target: file.hash, content: $('#elfinder-fm-file-desc').val()},
  219. notify: {type: 'desc', cnt: 1}
  220. })
  221. .always(function() {
  222. filemanager.unlockfiles({files: [file.hash]});
  223. });
  224. });
  225. }
  226. });
  227. }
  228. };
  229. }
  230. if (viewOwner) {
  231. // Key is the same as your command name
  232. elfinderOpts['commandsOptions']['info']['custom']['owner'] = {
  233. // Field label
  234. label: Drupal.t('Owner'),
  235. // HTML Template
  236. tpl: '<div class="elfinder-info-owner"><span class="elfinder-info-spinner"></span></div>',
  237. // Action that sends the request to the server and get the description
  238. action: function(file, filemanager, dialog) {
  239. console.log('owner action');
  240. // Use the @filemanager object to issue a request
  241. filemanager.request({
  242. // Issuing the custom 'desc' command, targetting the selected file
  243. data: {cmd: 'owner', target: file.hash,},
  244. preventDefault: true,
  245. })
  246. // If the request fails, populate the field with 'Unknown'
  247. .fail(function() {
  248. console.log('owner fail');
  249. dialog.find('.elfinder-info-owner').html(filemanager.i18n('unknown'));
  250. })
  251. // When the request is successful, show the description
  252. .done(function(data) {
  253. console.log('owner done');
  254. dialog.find('.elfinder-info-owner').html(data.owner);
  255. });
  256. }
  257. };
  258. elFinder.prototype._options.uiOptions.cwd.listView.columns.push('owner');
  259. elFinder.prototype._options.uiOptions.cwd.listView.columnsCustomName['owner'] = Drupal.t('Owner');
  260. }
  261. if (viewDownloads) {
  262. elfinderOpts['commandsOptions']['info']['custom']['downloadcount'] = {
  263. // Field label
  264. label: Drupal.t('Downloads'),
  265. // HTML Template
  266. tpl: '<div class="elfinder-info-downloadcount"><span class="elfinder-info-spinner"></span></div>',
  267. // Action that sends the request to the server and get the description
  268. action: function(file, filemanager, dialog) {
  269. // Use the @filemanager object to issue a request
  270. filemanager.request({
  271. // Issuing the custom 'desc' command, targetting the selected file
  272. data: {cmd: 'downloadcount', target: file.hash,},
  273. preventDefault: true,
  274. })
  275. // If the request fails, populate the field with 'Unknown'
  276. .fail(function() {
  277. dialog.find('.elfinder-info-downloadcount').html(0);
  278. })
  279. // When the request is successful, show the description
  280. .done(function(data) {
  281. dialog.find('.elfinder-info-downloadcount').html(data.desc);
  282. });
  283. }
  284. };
  285. }
  286. // console.log(elfinderOpts);
  287. }
  288. var fm = $('#finder').elfinder(elfinderOpts);
  289. var instance = fm.elfinder('instance');
  290. // console.log(typeof instance == 'object' && typeof instance.toast == 'function');
  291. if (typeof instance == 'object' && typeof instance.toast == 'function') {
  292. instance.bind('load', function(event) {
  293. var messages = '#elfinder-messages .messages';
  294. $(messages).each(function(index, value) {
  295. var mode = 'info';
  296. if ($(value).hasClass('warning')) {
  297. mode = 'warning';
  298. } else if ($(value).hasClass('error')) {
  299. mode = 'error';
  300. }
  301. instance.toast({
  302. msg: $(value).html(),
  303. hideDuration: 500,
  304. showDuration: 300,
  305. timeOut: 1000,
  306. mode: mode
  307. });
  308. });
  309. });
  310. } else {
  311. $('#elfinder-messages').addClass('legacy');
  312. }
  313. //$('#finder').toast({msg: '123'});
  314. var h;
  315. if (elfinderOpts.browserMode != 'backend') {
  316. h = ($(window).height());
  317. } else {
  318. h = ($('#page').height());
  319. }
  320. $(window).resize(function() {
  321. if ($('#finder').height() != h) {
  322. $('#finder').height(h).resize();
  323. }
  324. });
  325. $(window).trigger('resize');
  326. }
  327. });
  328. })(jQuery);