attachment.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. define(['jquery', 'bootstrap', 'backend', 'form', 'table'], function ($, undefined, Backend, Form, Table) {
  2. var Controller = {
  3. index: function () {
  4. // 初始化表格参数配置
  5. Table.api.init({
  6. extend: {
  7. index_url: 'general/attachment/index',
  8. add_url: 'general/attachment/add',
  9. edit_url: 'general/attachment/edit',
  10. del_url: 'general/attachment/del',
  11. multi_url: 'general/attachment/multi',
  12. table: 'attachment'
  13. }
  14. });
  15. var table = $("#table");
  16. // 初始化表格
  17. table.bootstrapTable({
  18. url: $.fn.bootstrapTable.defaults.extend.index_url,
  19. sortName: 'id',
  20. columns: [
  21. [
  22. {field: 'state', checkbox: true},
  23. {field: 'id', title: __('Id')},
  24. {field: 'category', title: __('Category'), operate: 'in', formatter: Table.api.formatter.label, searchList: Config.categoryList},
  25. {field: 'admin_id', title: __('Admin_id'), visible: false, addClass: "selectpage", extend: "data-source='auth/admin/index' data-field='nickname'"},
  26. {field: 'user_id', title: __('User_id'), visible: false, addClass: "selectpage", extend: "data-source='user/user/index' data-field='nickname'"},
  27. {field: 'preview', title: __('Preview'), formatter: Controller.api.formatter.thumb, operate: false},
  28. {field: 'url', title: __('Url'), formatter: Controller.api.formatter.url, visible: false},
  29. {field: 'filename', title: __('Filename'), sortable: true, formatter: Controller.api.formatter.filename, operate: 'like'},
  30. {
  31. field: 'filesize', title: __('Filesize'), operate: 'BETWEEN', sortable: true, formatter: function (value, row, index) {
  32. var size = parseFloat(value);
  33. var i = Math.floor(Math.log(size) / Math.log(1024));
  34. return (size / Math.pow(1024, i)).toFixed(i < 2 ? 0 : 2) * 1 + ' ' + ['B', 'KB', 'MB', 'GB', 'TB'][i];
  35. }
  36. },
  37. {field: 'imagewidth', title: __('Imagewidth'), sortable: true},
  38. {field: 'imageheight', title: __('Imageheight'), sortable: true},
  39. {field: 'imagetype', title: __('Imagetype'), sortable: true, formatter: Table.api.formatter.search, operate: 'like'},
  40. {field: 'storage', title: __('Storage'), formatter: Table.api.formatter.search, operate: 'like'},
  41. {field: 'mimetype', title: __('Mimetype'), formatter: Controller.api.formatter.mimetype},
  42. {
  43. field: 'createtime',
  44. title: __('Createtime'),
  45. formatter: Table.api.formatter.datetime,
  46. operate: 'RANGE',
  47. addclass: 'datetimerange',
  48. sortable: true,
  49. width: 150
  50. },
  51. {
  52. field: 'operate',
  53. title: __('Operate'),
  54. table: table,
  55. events: Table.api.events.operate,
  56. formatter: Table.api.formatter.operate
  57. }
  58. ]
  59. ],
  60. });
  61. // 绑定过滤事件
  62. $('.filter-type ul li a', table.closest(".panel-intro")).on('click', function (e) {
  63. $(this).closest("ul").find("li").removeClass("active");
  64. $(this).closest("li").addClass("active");
  65. var field = 'mimetype';
  66. var value = $(this).data("value") || '';
  67. var object = $("[name='" + field + "']", table.closest(".bootstrap-table").find(".commonsearch-table"));
  68. if (object.prop('tagName') == "SELECT") {
  69. $("option[value='" + value + "']", object).prop("selected", true);
  70. } else {
  71. object.val(value);
  72. }
  73. table.trigger("uncheckbox");
  74. table.bootstrapTable('refresh', {pageNumber: 1});
  75. });
  76. // 为表格绑定事件
  77. Table.api.bindevent(table);
  78. // 附件归类
  79. $(document).on('click', '.btn-classify', function () {
  80. var ids = Table.api.selectedids(table);
  81. Layer.open({
  82. title: __('Classify'),
  83. content: Template("typetpl", {}),
  84. btn: [__('OK')],
  85. yes: function (index, layero) {
  86. var category = $("select[name='category']", layero).val();
  87. Fast.api.ajax({
  88. url: "general/attachment/classify",
  89. type: "post",
  90. data: {category: category, ids: ids.join(',')},
  91. }, function () {
  92. table.bootstrapTable('refresh', {});
  93. Layer.close(index);
  94. });
  95. },
  96. success: function (layero, index) {
  97. }
  98. });
  99. });
  100. },
  101. select: function () {
  102. // 初始化表格参数配置
  103. Table.api.init({
  104. extend: {
  105. index_url: 'general/attachment/select',
  106. }
  107. });
  108. var urlArr = [];
  109. var multiple = Backend.api.query('multiple');
  110. multiple = multiple == 'true' ? true : false;
  111. var table = $("#table");
  112. table.on('check.bs.table uncheck.bs.table check-all.bs.table uncheck-all.bs.table', function (e, row) {
  113. if (e.type == 'check' || e.type == 'uncheck') {
  114. row = [row];
  115. } else {
  116. urlArr = [];
  117. }
  118. $.each(row, function (i, j) {
  119. if (e.type.indexOf("uncheck") > -1) {
  120. var index = urlArr.indexOf(j.url);
  121. if (index > -1) {
  122. urlArr.splice(index, 1);
  123. }
  124. } else {
  125. urlArr.indexOf(j.url) == -1 && urlArr.push(j.url);
  126. }
  127. });
  128. });
  129. // 初始化表格
  130. table.bootstrapTable({
  131. url: $.fn.bootstrapTable.defaults.extend.index_url,
  132. sortName: 'id',
  133. showToggle: false,
  134. showExport: false,
  135. maintainSelected: true,
  136. fixedColumns: true,
  137. fixedRightNumber: 1,
  138. columns: [
  139. [
  140. {field: 'state', checkbox: multiple, visible: multiple, operate: false},
  141. {field: 'id', title: __('Id')},
  142. {field: 'category', title: __('Category'), operate: 'in', formatter: Table.api.formatter.label, searchList: Config.categoryList},
  143. {field: 'admin_id', title: __('Admin_id'), formatter: Table.api.formatter.search, visible: false},
  144. {field: 'user_id', title: __('User_id'), formatter: Table.api.formatter.search, visible: false},
  145. {field: 'url', title: __('Preview'), formatter: Controller.api.formatter.thumb, operate: false},
  146. {field: 'filename', title: __('Filename'), sortable: true, formatter: Controller.api.formatter.filename, operate: 'like'},
  147. {field: 'imagewidth', title: __('Imagewidth'), operate: false, sortable: true},
  148. {field: 'imageheight', title: __('Imageheight'), operate: false, sortable: true},
  149. {
  150. field: 'mimetype', title: __('Mimetype'), sortable: true, operate: 'LIKE %...%',
  151. process: function (value, arg) {
  152. return value.replace(/\*/g, '%');
  153. },
  154. formatter: Controller.api.formatter.mimetype
  155. },
  156. {field: 'createtime', title: __('Createtime'), width: 120, formatter: Table.api.formatter.datetime, datetimeFormat: 'YYYY-MM-DD', operate: 'RANGE', addclass: 'datetimerange', sortable: true},
  157. {
  158. field: 'operate', title: __('Operate'), width: 85, events: {
  159. 'click .btn-chooseone': function (e, value, row, index) {
  160. Fast.api.close({url: row.url, multiple: multiple});
  161. },
  162. }, formatter: function () {
  163. return '<a href="javascript:;" class="btn btn-danger btn-chooseone btn-xs"><i class="fa fa-check"></i> ' + __('Choose') + '</a>';
  164. }
  165. }
  166. ]
  167. ]
  168. });
  169. // 绑定过滤事件
  170. $('.filter-type ul li a', table.closest(".panel-intro")).on('click', function (e) {
  171. $(this).closest("ul").find("li").removeClass("active");
  172. $(this).closest("li").addClass("active");
  173. var field = 'mimetype';
  174. var value = $(this).data("value") || '';
  175. var object = $("[name='" + field + "']", table.closest(".bootstrap-table").find(".commonsearch-table"));
  176. if (object.prop('tagName') == "SELECT") {
  177. $("option[value='" + value + "']", object).prop("selected", true);
  178. } else {
  179. object.val(value);
  180. }
  181. table.trigger("uncheckbox");
  182. table.bootstrapTable('refresh', {pageNumber: 1});
  183. });
  184. // 选中多个
  185. $(document).on("click", ".btn-choose-multi", function () {
  186. Fast.api.close({url: urlArr.join(","), multiple: multiple});
  187. });
  188. // 为表格绑定事件
  189. Table.api.bindevent(table);
  190. require(['upload'], function (Upload) {
  191. $("#toolbar .faupload").data("category", function (file) {
  192. var category = $("ul.nav-tabs[data-field='category'] li.active a").data("value");
  193. return category;
  194. });
  195. Upload.api.upload($("#toolbar .faupload"), function () {
  196. $(".btn-refresh").trigger("click");
  197. });
  198. });
  199. },
  200. add: function () {
  201. //上传完成后刷新父窗口
  202. $(".faupload").data("upload-complete", function (files) {
  203. setTimeout(function () {
  204. window.parent.$(".btn-refresh").trigger("click");
  205. }, 100);
  206. });
  207. // 获取上传类别
  208. $("#faupload-third,#faupload-third-chunking").data("category", function (file) {
  209. return $("#category-third").val();
  210. });
  211. // 获取上传类别
  212. $("#faupload-local,#faupload-local-chunking").data("category", function (file) {
  213. return $("#category-local").val();
  214. });
  215. Controller.api.bindevent();
  216. },
  217. edit: function () {
  218. Controller.api.bindevent();
  219. },
  220. api: {
  221. bindevent: function () {
  222. Form.api.bindevent($("form[role=form]"));
  223. },
  224. formatter: {
  225. thumb: function (value, row, index) {
  226. var html = '';
  227. if (row.mimetype.indexOf("image") > -1) {
  228. html = '<a href="' + row.fullurl + '" target="_blank"><img src="' + row.fullurl + row.thumb_style + '" alt="" style="max-height:60px;max-width:120px"></a>';
  229. } else {
  230. html = '<a href="' + row.fullurl + '" target="_blank"><img src="' + Fast.api.fixurl("ajax/icon") + "?suffix=" + row.imagetype + '" alt="" style="max-height:90px;max-width:120px"></a>';
  231. }
  232. return '<div style="width:120px;margin:0 auto;text-align:center;overflow:hidden;white-space: nowrap;text-overflow: ellipsis;">' + html + '</div>';
  233. },
  234. url: function (value, row, index) {
  235. return '<a href="' + row.fullurl + '" target="_blank" class="label bg-green">' + row.url + '</a>';
  236. },
  237. filename: function (value, row, index) {
  238. return '<div style="width:150px;margin:0 auto;text-align:center;overflow:hidden;white-space: nowrap;text-overflow: ellipsis;">' + Table.api.formatter.search.call(this, value, row, index) + '</div>';
  239. },
  240. mimetype: function (value, row, index) {
  241. return '<div style="width:80px;margin:0 auto;text-align:center;overflow:hidden;white-space: nowrap;text-overflow: ellipsis;">' + Table.api.formatter.search.call(this, value, row, index) + '</div>';
  242. },
  243. }
  244. }
  245. };
  246. return Controller;
  247. });