attachment.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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. columns: [
  137. [
  138. {field: 'state', checkbox: multiple, visible: multiple, operate: false},
  139. {field: 'id', title: __('Id')},
  140. {field: 'category', title: __('Category'), operate: 'in', formatter: Table.api.formatter.label, searchList: Config.categoryList},
  141. {field: 'admin_id', title: __('Admin_id'), formatter: Table.api.formatter.search, visible: false},
  142. {field: 'user_id', title: __('User_id'), formatter: Table.api.formatter.search, visible: false},
  143. {field: 'url', title: __('Preview'), formatter: Controller.api.formatter.thumb, operate: false},
  144. {field: 'filename', title: __('Filename'), sortable: true, formatter: Controller.api.formatter.filename, operate: 'like'},
  145. {field: 'imagewidth', title: __('Imagewidth'), operate: false, sortable: true},
  146. {field: 'imageheight', title: __('Imageheight'), operate: false, sortable: true},
  147. {
  148. field: 'mimetype', title: __('Mimetype'), sortable: true, operate: 'LIKE %...%',
  149. process: function (value, arg) {
  150. return value.replace(/\*/g, '%');
  151. },
  152. formatter: Controller.api.formatter.mimetype
  153. },
  154. {field: 'createtime', title: __('Createtime'), formatter: Table.api.formatter.datetime, datetimeFormat: 'YYYY-MM-DD', operate: 'RANGE', addclass: 'datetimerange', sortable: true},
  155. {
  156. field: 'operate', title: __('Operate'), events: {
  157. 'click .btn-chooseone': function (e, value, row, index) {
  158. Fast.api.close({url: row.url, multiple: multiple});
  159. },
  160. }, formatter: function () {
  161. return '<a href="javascript:;" class="btn btn-danger btn-chooseone btn-xs"><i class="fa fa-check"></i> ' + __('Choose') + '</a>';
  162. }
  163. }
  164. ]
  165. ]
  166. });
  167. // 绑定过滤事件
  168. $('.filter-type ul li a', table.closest(".panel-intro")).on('click', function (e) {
  169. $(this).closest("ul").find("li").removeClass("active");
  170. $(this).closest("li").addClass("active");
  171. var field = 'mimetype';
  172. var value = $(this).data("value") || '';
  173. var object = $("[name='" + field + "']", table.closest(".bootstrap-table").find(".commonsearch-table"));
  174. if (object.prop('tagName') == "SELECT") {
  175. $("option[value='" + value + "']", object).prop("selected", true);
  176. } else {
  177. object.val(value);
  178. }
  179. table.trigger("uncheckbox");
  180. table.bootstrapTable('refresh', {pageNumber: 1});
  181. });
  182. // 选中多个
  183. $(document).on("click", ".btn-choose-multi", function () {
  184. Fast.api.close({url: urlArr.join(","), multiple: multiple});
  185. });
  186. // 为表格绑定事件
  187. Table.api.bindevent(table);
  188. require(['upload'], function (Upload) {
  189. Upload.api.upload($("#toolbar .faupload"), function () {
  190. $(".btn-refresh").trigger("click");
  191. });
  192. });
  193. },
  194. add: function () {
  195. //上传完成后刷新父窗口
  196. $(".faupload").data("upload-complete", function (files) {
  197. window.parent.$(".btn-refresh").trigger("click");
  198. });
  199. Controller.api.bindevent();
  200. },
  201. edit: function () {
  202. Controller.api.bindevent();
  203. },
  204. api: {
  205. bindevent: function () {
  206. Form.api.bindevent($("form[role=form]"));
  207. },
  208. formatter: {
  209. thumb: function (value, row, index) {
  210. var html = '';
  211. if (row.mimetype.indexOf("image") > -1) {
  212. html = '<a href="' + row.fullurl + '" target="_blank"><img src="' + row.fullurl + row.thumb_style + '" alt="" style="max-height:60px;max-width:120px"></a>';
  213. } else {
  214. 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>';
  215. }
  216. return '<div style="width:120px;margin:0 auto;text-align:center;overflow:hidden;white-space: nowrap;text-overflow: ellipsis;">' + html + '</div>';
  217. },
  218. url: function (value, row, index) {
  219. return '<a href="' + row.fullurl + '" target="_blank" class="label bg-green">' + row.url + '</a>';
  220. },
  221. filename: function (value, row, index) {
  222. 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>';
  223. },
  224. mimetype: function (value, row, index) {
  225. 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>';
  226. },
  227. }
  228. }
  229. };
  230. return Controller;
  231. });