attachment.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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: 'url', title: __('Preview'), formatter: Controller.api.formatter.thumb, operate: false},
  27. {field: 'url', title: __('Url'), formatter: Controller.api.formatter.url},
  28. {field: 'imagewidth', title: __('Imagewidth'), sortable: true},
  29. {field: 'imageheight', title: __('Imageheight'), sortable: true},
  30. {field: 'imagetype', title: __('Imagetype'), formatter: Table.api.formatter.search},
  31. {field: 'storage', title: __('Storage'), formatter: Table.api.formatter.search},
  32. {
  33. field: 'filesize', title: __('Filesize'), operate: 'BETWEEN', sortable: true, formatter: function (value, row, index) {
  34. var size = parseFloat(value);
  35. var i = Math.floor(Math.log(size) / Math.log(1024));
  36. return (size / Math.pow(1024, i)).toFixed(i < 2 ? 0 : 2) * 1 + ' ' + ['B', 'KB', 'MB', 'GB', 'TB'][i];
  37. }
  38. },
  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 table = $("#table");
  69. // 初始化表格
  70. table.bootstrapTable({
  71. url: $.fn.bootstrapTable.defaults.extend.index_url,
  72. sortName: 'id',
  73. showToggle: false,
  74. showExport: false,
  75. columns: [
  76. [
  77. {field: 'state', checkbox: true,},
  78. {field: 'id', title: __('Id')},
  79. {field: 'admin_id', title: __('Admin_id'), visible: false},
  80. {field: 'user_id', title: __('User_id'), visible: false},
  81. {field: 'url', title: __('Preview'), formatter: Controller.api.formatter.thumb, operate: false},
  82. {field: 'imagewidth', title: __('Imagewidth'), operate: false},
  83. {field: 'imageheight', title: __('Imageheight'), operate: false},
  84. {
  85. field: 'mimetype', title: __('Mimetype'), operate: 'LIKE %...%',
  86. process: function (value, arg) {
  87. return value.replace(/\*/g, '%');
  88. }
  89. },
  90. {field: 'createtime', title: __('Createtime'), formatter: Table.api.formatter.datetime, operate: 'RANGE', addclass: 'datetimerange', sortable: true},
  91. {
  92. field: 'operate', title: __('Operate'), events: {
  93. 'click .btn-chooseone': function (e, value, row, index) {
  94. var multiple = Backend.api.query('multiple');
  95. multiple = multiple == 'true' ? true : false;
  96. Fast.api.close({url: row.url, multiple: multiple});
  97. },
  98. }, formatter: function () {
  99. return '<a href="javascript:;" class="btn btn-danger btn-chooseone btn-xs"><i class="fa fa-check"></i> ' + __('Choose') + '</a>';
  100. }
  101. }
  102. ]
  103. ]
  104. });
  105. // 选中多个
  106. $(document).on("click", ".btn-choose-multi", function () {
  107. var urlArr = new Array();
  108. $.each(table.bootstrapTable("getAllSelections"), function (i, j) {
  109. urlArr.push(j.url);
  110. });
  111. var multiple = Backend.api.query('multiple');
  112. multiple = multiple == 'true' ? true : false;
  113. Fast.api.close({url: urlArr.join(","), multiple: multiple});
  114. });
  115. // 为表格绑定事件
  116. Table.api.bindevent(table);
  117. require(['upload'], function (Upload) {
  118. Upload.api.plupload($("#toolbar .plupload"), function () {
  119. $(".btn-refresh").trigger("click");
  120. });
  121. });
  122. },
  123. add: function () {
  124. Controller.api.bindevent();
  125. },
  126. edit: function () {
  127. Controller.api.bindevent();
  128. },
  129. api: {
  130. bindevent: function () {
  131. Form.api.bindevent($("form[role=form]"));
  132. },
  133. formatter: {
  134. thumb: function (value, row, index) {
  135. if (row.mimetype.indexOf("image") > -1) {
  136. var style = row.storage == 'upyun' ? '!/fwfh/120x90' : '';
  137. return '<a href="' + row.fullurl + '" target="_blank"><img src="' + row.fullurl + style + '" alt="" style="max-height:90px;max-width:120px"></a>';
  138. } else {
  139. return '<a href="' + row.fullurl + '" target="_blank"><img src="https://tool.fastadmin.net/icon/' + row.imagetype + '.png" alt=""></a>';
  140. }
  141. },
  142. url: function (value, row, index) {
  143. return '<a href="' + row.fullurl + '" target="_blank" class="label bg-green">' + value + '</a>';
  144. },
  145. }
  146. }
  147. };
  148. return Controller;
  149. });