attachment.js 8.9 KB

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