info.js 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. /*
  2. * elFinder Integration
  3. *
  4. * Copyright (c) 2010-2018, Alexey Sukhotin. All rights reserved.
  5. */
  6. "use strict";
  7. /**
  8. * @class elFinder command "info".
  9. * Display dialog with file properties.
  10. *
  11. * @author Dmitry (dio) Levashov, dio@std42.ru
  12. **/
  13. (function($) {
  14. elFinder.prototype.commands.info = function() {
  15. var m = 'msg',
  16. fm = this.fm,
  17. spclass = 'elfinder-info-spinner',
  18. msg = {
  19. calc: fm.i18n('calc'),
  20. size: fm.i18n('size'),
  21. unknown: fm.i18n('unknown'),
  22. path: fm.i18n('path'),
  23. aliasfor: fm.i18n('aliasfor'),
  24. modify: fm.i18n('modify'),
  25. perms: fm.i18n('perms'),
  26. locked: fm.i18n('locked'),
  27. dim: fm.i18n('dim'),
  28. kind: fm.i18n('kind'),
  29. files: fm.i18n('files'),
  30. folders: fm.i18n('folders'),
  31. items: fm.i18n('items'),
  32. yes: fm.i18n('yes'),
  33. no: fm.i18n('no'),
  34. link: fm.i18n('link'),
  35. owner: fm.i18n('owner'),
  36. desc: fm.i18n('description'),
  37. downloads: fm.i18n('downloads')
  38. };
  39. this.tpl = {
  40. main: '<div class="ui-helper-clearfix elfinder-info-title"><span class="elfinder-cwd-icon {class} ui-corner-all"/>{title}</div><table class="elfinder-info-tb">{content}</table>',
  41. itemTitle: '<strong>{name}</strong><span class="elfinder-info-kind">{kind}</span>',
  42. groupTitle: '<strong>{items}: {num}</strong>',
  43. row: '<tr><td>{label} : </td><td>{value}</td></tr>',
  44. spinner: '<span>{text}</span> <span class="' + spclass + ' ' + spclass + '-' + '{id}"/>'
  45. }
  46. this.alwaysEnabled = true;
  47. this.updateOnSelect = false;
  48. this.shortcuts = [{
  49. pattern: 'ctrl+i'
  50. }];
  51. this.init = function() {
  52. $.each(msg, function(k, v) {
  53. msg[k] = fm.i18n(v);
  54. });
  55. }
  56. this.getstate = function() {
  57. return 0;
  58. }
  59. this.exec = function(hashes) {
  60. var self = this,
  61. fm = this.fm,
  62. tpl = this.tpl,
  63. row = tpl.row,
  64. files = this.files(hashes),
  65. cnt = files.length,
  66. content = [],
  67. view = tpl.main,
  68. l = '{label}',
  69. v = '{value}',
  70. opts = {
  71. title: this.title,
  72. width: 'auto',
  73. modal: true,
  74. close: function() {
  75. $(this).elfinderdialog('destroy');
  76. }
  77. },
  78. count = [],
  79. replSpinner = function(msg) {
  80. dialog.find('.' + spclass).parent().text(msg);
  81. },
  82. replSpinnerById = function(msg, id) {
  83. dialog.find('.' + spclass + '-' + id).parent().html(msg);
  84. },
  85. id = fm.namespace + '-info-' + $.map(files, function(f) {
  86. return f.hash
  87. }).join('-'),
  88. dialog = fm.getUI().find('#' + id),
  89. size, tmb, file, title, dcnt;
  90. var disabledCommands = Drupal.settings.elfinder.disabledCommands,
  91. viewDesc = $.inArray('desc', disabledCommands) == -1 ? true : false,
  92. editDesc = $.inArray('editdesc', disabledCommands) == -1 ? true : false,
  93. viewOwner = $.inArray('owner', disabledCommands) == -1 ? true : false,
  94. viewDownloads = $.inArray('downloadcount', disabledCommands) == -1 ? true : false;
  95. if (!cnt) {
  96. return $.Deferred().reject();
  97. }
  98. if (dialog.length) {
  99. dialog.elfinderdialog('toTop');
  100. return $.Deferred().resolve();
  101. }
  102. if (cnt == 1) {
  103. file = files[0];
  104. view = view.replace('{class}', fm.mime2class(file.mime));
  105. title = tpl.itemTitle.replace('{name}', file.name).replace('{kind}', fm.mime2kind(file));
  106. if (file.tmb) {
  107. tmb = fm.option('tmbUrl') + file.tmb;
  108. }
  109. if (!file.read) {
  110. size = msg.unknown;
  111. } else if (file.mime != 'directory' || file.alias) {
  112. size = fm.formatSize(file.size);
  113. } else {
  114. /* adding spinner id to separate field updates */
  115. size = tpl.spinner.replace('{text}', msg.calc).replace('{id}', 'size');
  116. count.push(file.hash);
  117. }
  118. content.push(row.replace(l, msg.size).replace(v, size));
  119. file.alias && content.push(row.replace(l, msg.aliasfor).replace(v, file.alias));
  120. content.push(row.replace(l, msg.path).replace(v, fm.escape(fm.path(file.hash))));
  121. file.read && content.push(row.replace(l, msg.link).replace(v, '<a href="' + fm.url(file.hash) + '" target="_blank">' + file.name + '</a>'));
  122. if (file.dim) { // old api
  123. content.push(row.replace(l, msg.dim).replace(v, file.dim));
  124. } else if (file.mime.indexOf('image') !== -1) {
  125. content.push(row.replace(l, msg.dim).replace(v, tpl.spinner.replace('{text}', msg.calc).replace('{id}', 'dim')));
  126. fm.request({
  127. data: {cmd: 'dim', target: file.hash},
  128. preventDefault: true
  129. })
  130. .fail(function() {
  131. replSpinnerById(msg.unknown, 'dim');
  132. })
  133. .done(function(data) {
  134. replSpinnerById(data.dim || msg.unknown, 'dim');
  135. });
  136. }
  137. content.push(row.replace(l, msg.modify).replace(v, fm.formatDate(file)));
  138. content.push(row.replace(l, msg.perms).replace(v, fm.formatPermissions(file)));
  139. content.push(row.replace(l, msg.locked).replace(v, file.locked ? msg.yes : msg.no));
  140. /* Don't show fields if command not allowed */
  141. if (viewOwner) {
  142. content.push(row.replace(l, Drupal.t('Owner')).replace(v, tpl.spinner.replace('{text}', msg.calc).replace('{id}', 'owner')));
  143. fm.request({
  144. data: {cmd: 'owner', target: file.hash},
  145. preventDefault: true
  146. })
  147. .fail(function() {
  148. replSpinnerById(msg.unknown, 'owner');
  149. })
  150. .done(function(data) {
  151. replSpinnerById(data.owner || msg.unknown, 'owner');
  152. });
  153. }
  154. /* Don't show fields if command not allowed */
  155. if (viewDownloads) {
  156. content.push(row.replace(l, Drupal.t('Downloads')).replace(v, tpl.spinner.replace('{text}', msg.calc).replace('{id}', 'downloads')));
  157. fm.request({
  158. data: {cmd: 'downloadcount', target: file.hash},
  159. preventDefault: true
  160. })
  161. .fail(function() {
  162. replSpinnerById(msg.unknown, 'downloads');
  163. })
  164. .done(function(data) {
  165. replSpinnerById(data.downloadcount || msg.unknown, 'downloads');
  166. });
  167. }
  168. /* Don't show fields if command not allowed */
  169. if (viewDesc || editDesc) {
  170. var desc = '<div id="elfinder-fm-file-desc">' + msg.calc + '</div>';
  171. if (editDesc) {
  172. desc = '<textarea cols="20" rows="5" id="elfinder-fm-file-desc" class="ui-widget ui-widget-content" disabled="true" >' + msg.calc + '</textarea><input type="button" id="elfinder-fm-file-desc-btn-save" value="' + fm.i18n('btnSave') + '" />';
  173. }
  174. content.push(row.replace(l, Drupal.t('Description')).replace(v, desc));
  175. fm.request({
  176. data: {cmd: 'desc', target: file.hash},
  177. preventDefault: true
  178. })
  179. .done(function(data) {
  180. var fieldDesc = dialog.find('#elfinder-fm-file-desc');
  181. if (editDesc) {
  182. fieldDesc.val(data.desc || '');
  183. fieldDesc.removeAttr('disabled');
  184. } else {
  185. fieldDesc.empty();
  186. fieldDesc.html(data.desc || '');
  187. }
  188. });
  189. }
  190. } else {
  191. view = view.replace('{class}', 'elfinder-cwd-icon-group');
  192. title = tpl.groupTitle.replace('{items}', msg.items).replace('{num}', cnt);
  193. dcnt = $.map(files, function(f) {
  194. return f.mime == 'directory' ? 1 : null
  195. }).length;
  196. if (!dcnt) {
  197. size = 0;
  198. $.each(files, function(h, f) {
  199. var s = parseInt(f.size);
  200. if (s >= 0 && size >= 0) {
  201. size += s;
  202. } else {
  203. size = 'unknown';
  204. }
  205. });
  206. content.push(row.replace(l, msg.kind).replace(v, msg.files));
  207. content.push(row.replace(l, msg.size).replace(v, fm.formatSize(size)));
  208. } else {
  209. content.push(row.replace(l, msg.kind).replace(v, dcnt == cnt ? msg.folders : msg.folders + ' ' + dcnt + ', ' + msg.files + ' ' + (cnt - dcnt)))
  210. content.push(row.replace(l, msg.size).replace(v, tpl.spinner.replace('{text}', msg.calc).replace('{id}', 'size')));
  211. count = $.map(files, function(f) {
  212. return f.hash
  213. });
  214. }
  215. }
  216. view = view.replace('{title}', title).replace('{content}', content.join(''));
  217. dialog = fm.dialog(view, opts);
  218. dialog.attr('id', id);
  219. if (editDesc) {
  220. var inputDesc = $('#elfinder-fm-file-desc', dialog);
  221. var btnSave = $('#elfinder-fm-file-desc-btn-save', dialog).button();
  222. btnSave.click(function() {
  223. fm.lockfiles({files: [file.hash]});
  224. fm.request({
  225. data: {cmd: 'desc', target: file.hash, content: inputDesc.val()},
  226. notify: {type: 'desc', cnt: 1}
  227. })
  228. .always(function() {
  229. fm.unlockfiles({files: [file.hash]})
  230. });
  231. });
  232. }
  233. // load thumbnail
  234. if (tmb) {
  235. $('<img/>')
  236. .load(function() {
  237. dialog.find('.elfinder-cwd-icon').css('background', 'url("' + tmb + '") center center no-repeat');
  238. })
  239. .attr('src', tmb);
  240. }
  241. // send request to count total size
  242. if (count.length) {
  243. fm.request({
  244. data: {cmd: 'size', targets: count},
  245. preventDefault: true
  246. })
  247. .fail(function() {
  248. replSpinnerById(msg.unknown, 'size');
  249. })
  250. .done(function(data) {
  251. var size = parseInt(data.size);
  252. fm.log(data.size)
  253. replSpinnerById(size >= 0 ? fm.formatSize(size) : msg.unknown, 'size');
  254. });
  255. }
  256. }
  257. }
  258. })(jQuery);