attachment.js 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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: Controller.api.formatter.filename, 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 multiple = Backend.api.query('multiple');
  71. multiple = multiple == 'true' ? true : false;
  72. var table = $("#table");
  73. table.on('check.bs.table uncheck.bs.table check-all.bs.table uncheck-all.bs.table', function (e, row) {
  74. if (e.type == 'check' || e.type == 'uncheck') {
  75. row = [row];
  76. } else {
  77. urlArr = [];
  78. }
  79. $.each(row, function (i, j) {
  80. if (e.type.indexOf("uncheck") > -1) {
  81. var index = urlArr.indexOf(j.url);
  82. if (index > -1) {
  83. urlArr.splice(index, 1);
  84. }
  85. } else {
  86. urlArr.indexOf(j.url) == -1 && urlArr.push(j.url);
  87. }
  88. });
  89. });
  90. // 初始化表格
  91. table.bootstrapTable({
  92. url: $.fn.bootstrapTable.defaults.extend.index_url,
  93. sortName: 'id',
  94. showToggle: false,
  95. showExport: false,
  96. maintainSelected: true,
  97. columns: [
  98. [
  99. {field: 'state', checkbox: multiple, visible: multiple, operate: false},
  100. {field: 'id', title: __('Id')},
  101. {field: 'admin_id', title: __('Admin_id'), formatter: Table.api.formatter.search, visible: false},
  102. {field: 'user_id', title: __('User_id'), formatter: Table.api.formatter.search, visible: false},
  103. {field: 'url', title: __('Preview'), formatter: Controller.api.formatter.thumb, operate: false},
  104. {field: 'filename', title: __('Filename'), formatter: Controller.api.formatter.filename, operate: 'like'},
  105. {field: 'imagewidth', title: __('Imagewidth'), operate: false},
  106. {field: 'imageheight', title: __('Imageheight'), operate: false},
  107. {
  108. field: 'mimetype', title: __('Mimetype'), operate: 'LIKE %...%',
  109. process: function (value, arg) {
  110. return value.replace(/\*/g, '%');
  111. }
  112. },
  113. {field: 'createtime', title: __('Createtime'), formatter: Table.api.formatter.datetime, datetimeFormat: 'YYYY-MM-DD', operate: 'RANGE', addclass: 'datetimerange', sortable: true},
  114. {
  115. field: 'operate', title: __('Operate'), events: {
  116. 'click .btn-chooseone': function (e, value, row, index) {
  117. Fast.api.close({url: row.url, multiple: multiple});
  118. },
  119. }, formatter: function () {
  120. return '<a href="javascript:;" class="btn btn-danger btn-chooseone btn-xs"><i class="fa fa-check"></i> ' + __('Choose') + '</a>';
  121. }
  122. }
  123. ]
  124. ]
  125. });
  126. // 选中多个
  127. $(document).on("click", ".btn-choose-multi", function () {
  128. Fast.api.close({url: urlArr.join(","), multiple: multiple});
  129. });
  130. // 为表格绑定事件
  131. Table.api.bindevent(table);
  132. require(['upload'], function (Upload) {
  133. Upload.api.upload($("#toolbar .faupload"), function () {
  134. $(".btn-refresh").trigger("click");
  135. });
  136. });
  137. },
  138. add: function () {
  139. //上传完成后刷新父窗口
  140. $(".faupload").data("upload-complete", function (files) {
  141. window.parent.$(".btn-refresh").trigger("click");
  142. });
  143. Controller.api.bindevent();
  144. },
  145. edit: function () {
  146. Controller.api.bindevent();
  147. },
  148. api: {
  149. bindevent: function () {
  150. Form.api.bindevent($("form[role=form]"));
  151. },
  152. formatter: {
  153. thumb: function (value, row, index) {
  154. if (row.mimetype.indexOf("image") > -1) {
  155. return '<a href="' + row.fullurl + '" target="_blank"><img src="' + row.fullurl + row.thumb_style + '" alt="" style="max-height:90px;max-width:120px"></a>';
  156. } else {
  157. 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>';
  158. }
  159. },
  160. url: function (value, row, index) {
  161. return '<a href="' + row.fullurl + '" target="_blank" class="label bg-green">' + row.url + '</a>';
  162. },
  163. filename: function (value, row, index) {
  164. 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>';
  165. },
  166. }
  167. }
  168. };
  169. return Controller;
  170. });