attachment.js 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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: 'admin_id', title: __('Admin_id'), visible: false, addClass: "selectpage", extend: "data-source='auth/admin/index' data-field='nickname'"},
  25. {field: 'user_id', title: __('User_id'), visible: false, addClass: "selectpage", extend: "data-source='user/user/index' data-field='nickname'"},
  26. {field: 'preview', title: __('Preview'), formatter: Controller.api.formatter.thumb, operate: false},
  27. {field: 'url', title: __('Url'), formatter: Controller.api.formatter.url, visible: false},
  28. {field: 'filename', title: __('Filename'), formatter: Table.api.formatter.search, operate: 'like'},
  29. {
  30. field: 'filesize', title: __('Filesize'), operate: 'BETWEEN', sortable: true, formatter: function (value, row, index) {
  31. var size = parseFloat(value);
  32. var i = Math.floor(Math.log(size) / Math.log(1024));
  33. return (size / Math.pow(1024, i)).toFixed(i < 2 ? 0 : 2) * 1 + ' ' + ['B', 'KB', 'MB', 'GB', 'TB'][i];
  34. }
  35. },
  36. {field: 'imagewidth', title: __('Imagewidth'), sortable: true},
  37. {field: 'imageheight', title: __('Imageheight'), sortable: true},
  38. {field: 'imagetype', title: __('Imagetype'), formatter: Table.api.formatter.search, operate: 'like'},
  39. {field: 'storage', title: __('Storage'), formatter: Table.api.formatter.search, operate: 'like'},
  40. {field: 'mimetype', title: __('Mimetype'), formatter: Table.api.formatter.search},
  41. {
  42. field: 'createtime',
  43. title: __('Createtime'),
  44. formatter: Table.api.formatter.datetime,
  45. operate: 'RANGE',
  46. addclass: 'datetimerange',
  47. sortable: true
  48. },
  49. {
  50. field: 'operate',
  51. title: __('Operate'),
  52. table: table,
  53. events: Table.api.events.operate,
  54. formatter: Table.api.formatter.operate
  55. }
  56. ]
  57. ],
  58. });
  59. // 为表格绑定事件
  60. Table.api.bindevent(table);
  61. },
  62. select: function () {
  63. // 初始化表格参数配置
  64. Table.api.init({
  65. extend: {
  66. index_url: 'general/attachment/select',
  67. }
  68. });
  69. var urlArr = [];
  70. var table = $("#table");
  71. table.on('check.bs.table uncheck.bs.table check-all.bs.table uncheck-all.bs.table', function (e, row) {
  72. if (e.type == 'check' || e.type == 'uncheck') {
  73. row = [row];
  74. } else {
  75. urlArr = [];
  76. }
  77. $.each(row, function (i, j) {
  78. if (e.type.indexOf("uncheck") > -1) {
  79. var index = urlArr.indexOf(j.url);
  80. if (index > -1) {
  81. urlArr.splice(index, 1);
  82. }
  83. } else {
  84. urlArr.indexOf(j.url) == -1 && urlArr.push(j.url);
  85. }
  86. });
  87. });
  88. // 初始化表格
  89. table.bootstrapTable({
  90. url: $.fn.bootstrapTable.defaults.extend.index_url,
  91. sortName: 'id',
  92. showToggle: false,
  93. showExport: false,
  94. columns: [
  95. [
  96. {field: 'state', checkbox: true},
  97. {field: 'id', title: __('Id')},
  98. {field: 'admin_id', title: __('Admin_id'), formatter: Table.api.formatter.search, visible: false},
  99. {field: 'user_id', title: __('User_id'), formatter: Table.api.formatter.search, visible: false},
  100. {field: 'url', title: __('Preview'), formatter: Controller.api.formatter.thumb, operate: false},
  101. {field: 'filename', title: __('Filename'), formatter: Table.api.formatter.search, operate: 'like'},
  102. {field: 'imagewidth', title: __('Imagewidth'), operate: false},
  103. {field: 'imageheight', title: __('Imageheight'), operate: false},
  104. {
  105. field: 'mimetype', title: __('Mimetype'), operate: 'LIKE %...%',
  106. process: function (value, arg) {
  107. return value.replace(/\*/g, '%');
  108. }
  109. },
  110. {field: 'createtime', title: __('Createtime'), formatter: Table.api.formatter.datetime, operate: 'RANGE', addclass: 'datetimerange', sortable: true},
  111. {
  112. field: 'operate', title: __('Operate'), events: {
  113. 'click .btn-chooseone': function (e, value, row, index) {
  114. var multiple = Backend.api.query('multiple');
  115. multiple = multiple == 'true' ? true : false;
  116. Fast.api.close({url: row.url, multiple: multiple});
  117. },
  118. }, formatter: function () {
  119. return '<a href="javascript:;" class="btn btn-danger btn-chooseone btn-xs"><i class="fa fa-check"></i> ' + __('Choose') + '</a>';
  120. }
  121. }
  122. ]
  123. ]
  124. });
  125. // 选中多个
  126. $(document).on("click", ".btn-choose-multi", function () {
  127. // var urlArr = [];
  128. // $.each(table.bootstrapTable("getAllSelections"), function (i, j) {
  129. // urlArr.push(j.url);
  130. // });
  131. var multiple = Backend.api.query('multiple');
  132. multiple = multiple == 'true' ? true : false;
  133. Fast.api.close({url: urlArr.join(","), multiple: multiple});
  134. });
  135. // 为表格绑定事件
  136. Table.api.bindevent(table);
  137. require(['upload'], function (Upload) {
  138. Upload.api.upload($("#toolbar .faupload"), function () {
  139. $(".btn-refresh").trigger("click");
  140. });
  141. });
  142. },
  143. add: function () {
  144. //上传完成后刷新父窗口
  145. $(".faupload").data("upload-complete", function (files) {
  146. window.parent.$(".btn-refresh").trigger("click");
  147. });
  148. Controller.api.bindevent();
  149. },
  150. edit: function () {
  151. Controller.api.bindevent();
  152. },
  153. api: {
  154. bindevent: function () {
  155. Form.api.bindevent($("form[role=form]"));
  156. },
  157. formatter: {
  158. thumb: function (value, row, index) {
  159. if (row.mimetype.indexOf("image") > -1) {
  160. var style = row.storage === 'upyun' ? '!/fwfh/120x90' : '';
  161. return '<a href="' + row.fullurl + '" target="_blank"><img src="' + row.fullurl + style + '" alt="" style="max-height:90px;max-width:120px"></a>';
  162. } else {
  163. return '<a href="' + row.fullurl + '" target="_blank"><img src="' + Fast.api.fixurl("ajax/icon") + "?suffix=" + row.imagetype + '" alt=""></a>';
  164. }
  165. },
  166. url: function (value, row, index) {
  167. return '<a href="' + row.fullurl + '" target="_blank" class="label bg-green">' + row.url + '</a>';
  168. },
  169. }
  170. }
  171. };
  172. return Controller;
  173. });