attachment.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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'), 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: Table.api.formatter.search},
  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. Table.api.bindevent(table);
  63. // 附件归类
  64. $(document).on('click', '.btn-classify', function () {
  65. var ids = Table.api.selectedids(table);
  66. Layer.open({
  67. title: __('Classify'),
  68. content: Template("typetpl", {}),
  69. btn: [__('OK')],
  70. yes: function (index, layero) {
  71. var category = $("select[name='category']", layero).val();
  72. Fast.api.ajax({
  73. url: "general/attachment/classify",
  74. type: "post",
  75. data: {category: category, ids: ids.join(',')},
  76. }, function () {
  77. table.bootstrapTable('refresh', {});
  78. Layer.close(index);
  79. });
  80. },
  81. success: function (layero, index) {
  82. }
  83. });
  84. });
  85. },
  86. select: function () {
  87. // 初始化表格参数配置
  88. Table.api.init({
  89. extend: {
  90. index_url: 'general/attachment/select',
  91. }
  92. });
  93. var urlArr = [];
  94. var multiple = Backend.api.query('multiple');
  95. multiple = multiple == 'true' ? true : false;
  96. var table = $("#table");
  97. table.on('check.bs.table uncheck.bs.table check-all.bs.table uncheck-all.bs.table', function (e, row) {
  98. if (e.type == 'check' || e.type == 'uncheck') {
  99. row = [row];
  100. } else {
  101. urlArr = [];
  102. }
  103. $.each(row, function (i, j) {
  104. if (e.type.indexOf("uncheck") > -1) {
  105. var index = urlArr.indexOf(j.url);
  106. if (index > -1) {
  107. urlArr.splice(index, 1);
  108. }
  109. } else {
  110. urlArr.indexOf(j.url) == -1 && urlArr.push(j.url);
  111. }
  112. });
  113. });
  114. // 初始化表格
  115. table.bootstrapTable({
  116. url: $.fn.bootstrapTable.defaults.extend.index_url,
  117. sortName: 'id',
  118. showToggle: false,
  119. showExport: false,
  120. maintainSelected: true,
  121. columns: [
  122. [
  123. {field: 'state', checkbox: multiple, visible: multiple, operate: false},
  124. {field: 'id', title: __('Id')},
  125. {field: 'category', title: __('Category'), formatter: Table.api.formatter.label, searchList: Config.categoryList},
  126. {field: 'admin_id', title: __('Admin_id'), formatter: Table.api.formatter.search, visible: false},
  127. {field: 'user_id', title: __('User_id'), formatter: Table.api.formatter.search, visible: false},
  128. {field: 'url', title: __('Preview'), formatter: Controller.api.formatter.thumb, operate: false},
  129. {field: 'filename', title: __('Filename'), sortable: true, formatter: Controller.api.formatter.filename, operate: 'like'},
  130. {field: 'imagewidth', title: __('Imagewidth'), operate: false, sortable: true},
  131. {field: 'imageheight', title: __('Imageheight'), operate: false, sortable: true},
  132. {
  133. field: 'mimetype', title: __('Mimetype'), sortable: true, operate: 'LIKE %...%',
  134. process: function (value, arg) {
  135. return value.replace(/\*/g, '%');
  136. }
  137. },
  138. {field: 'createtime', title: __('Createtime'), formatter: Table.api.formatter.datetime, datetimeFormat: 'YYYY-MM-DD', operate: 'RANGE', addclass: 'datetimerange', sortable: true},
  139. {
  140. field: 'operate', title: __('Operate'), events: {
  141. 'click .btn-chooseone': function (e, value, row, index) {
  142. Fast.api.close({url: row.url, multiple: multiple});
  143. },
  144. }, formatter: function () {
  145. return '<a href="javascript:;" class="btn btn-danger btn-chooseone btn-xs"><i class="fa fa-check"></i> ' + __('Choose') + '</a>';
  146. }
  147. }
  148. ]
  149. ]
  150. });
  151. // 选中多个
  152. $(document).on("click", ".btn-choose-multi", function () {
  153. Fast.api.close({url: urlArr.join(","), multiple: multiple});
  154. });
  155. // 为表格绑定事件
  156. Table.api.bindevent(table);
  157. require(['upload'], function (Upload) {
  158. Upload.api.upload($("#toolbar .faupload"), function () {
  159. $(".btn-refresh").trigger("click");
  160. });
  161. });
  162. },
  163. add: function () {
  164. //上传完成后刷新父窗口
  165. $(".faupload").data("upload-complete", function (files) {
  166. window.parent.$(".btn-refresh").trigger("click");
  167. });
  168. Controller.api.bindevent();
  169. },
  170. edit: function () {
  171. Controller.api.bindevent();
  172. },
  173. api: {
  174. bindevent: function () {
  175. Form.api.bindevent($("form[role=form]"));
  176. },
  177. formatter: {
  178. thumb: function (value, row, index) {
  179. if (row.mimetype.indexOf("image") > -1) {
  180. return '<a href="' + row.fullurl + '" target="_blank"><img src="' + row.fullurl + row.thumb_style + '" alt="" style="max-height:60px;max-width:120px"></a>';
  181. } else {
  182. return '<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>';
  183. }
  184. },
  185. url: function (value, row, index) {
  186. return '<a href="' + row.fullurl + '" target="_blank" class="label bg-green">' + row.url + '</a>';
  187. },
  188. filename: function (value, row, index) {
  189. return '<div style="width:180px;margin:0 auto;text-align:center;overflow:hidden;white-space: nowrap;text-overflow: ellipsis;">' + Table.api.formatter.search.call(this, value, row, index) + '</div>';
  190. },
  191. }
  192. }
  193. };
  194. return Controller;
  195. });