attachment.js 13 KB

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