book.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. define(['jquery', 'bootstrap', 'backend', 'table', 'form', 'clipboard'], function ($, undefined, Backend, Table, Form, Clipboard) {
  2. var contentArr = [];
  3. var Controller = {
  4. index: function () {
  5. // 初始化表格参数配置
  6. Table.api.init({
  7. extend: {
  8. index_url: 'booklist/book/index',
  9. add_url: 'booklist/book/add',
  10. edit_url: 'booklist/book/edit',
  11. del_url: 'booklist/book/del',
  12. multi_url: 'booklist/book/multi',
  13. table: 'book_list',
  14. }
  15. });
  16. var table = $("#table");
  17. if (Config.limitShow == true) {
  18. //渠道商
  19. // 初始化表格
  20. table.bootstrapTable({
  21. url: $.fn.bootstrapTable.defaults.extend.index_url,
  22. pk: 'id',
  23. sortName: 'id',
  24. showColumns: false,
  25. showToggle: false,
  26. showExport: false,
  27. searchFormVisible: false,
  28. search: false,
  29. commonSearch: false,
  30. columns: [
  31. [
  32. {checkbox: true},
  33. {field: 'id', title: __('Id')},
  34. {field: 'url', title: __('Url'), formatter: Controller.api.source_url, operate: false},
  35. {field: 'title', title: __('Title')},
  36. {field: 'name', title: __('Name')},
  37. {field: 'status', title: __('Status'), visible:false, searchList: {"normal":__('normal'),"hidden":__('hidden'),"deleted":__('deleted')}},
  38. {field: 'status_text', title: __('Status'), operate:false},
  39. {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
  40. {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
  41. {
  42. field: 'status',
  43. title: __('Operate'),
  44. operate: false,
  45. formatter: function (value) {
  46. if (value == 'normal') {
  47. return '<a href="javascript:;" class="btn btn-click btn-operate-copy" title="复制" data-table-id="table" data-field-index="8" data-row-index="0" data-button-index="0">复制&nbsp;&nbsp;<i class="fa fa-copy copy"></i></a>';
  48. } else {
  49. return '-';
  50. }
  51. }
  52. }
  53. ]
  54. ]
  55. });
  56. var clipboard = new Clipboard('.btn-operate-copy', {
  57. target: function (trigger) {
  58. var stxt = $(trigger).parent().parent().find('.url_text i').data('clipboard-text');
  59. trigger = document.querySelector('#many_copy_dom');
  60. $('#many_copy_dom').html(stxt);
  61. return trigger;
  62. }
  63. });
  64. clipboard.on('success',function (data) {
  65. Toastr.success('复制成功');
  66. }).on('error',function (data) {
  67. Toastr.error('复制失败');
  68. });
  69. } else {
  70. // 初始化表格
  71. table.bootstrapTable({
  72. url: $.fn.bootstrapTable.defaults.extend.index_url,
  73. pk: 'id',
  74. sortName: 'id',
  75. columns: [
  76. [
  77. {checkbox: true},
  78. {field: 'id', title: __('Id')},
  79. {field: 'title', title: __('Title')},
  80. {field: 'name', title: __('Name')},
  81. {field: 'status', title: __('Status'), visible:false, searchList: {"normal":__('normal'),"hidden":__('hidden'),"deleted":__('deleted')}},
  82. {field: 'status_text', title: __('Status'), operate:false},
  83. {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
  84. {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
  85. {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate,buttons:Controller.api.formatter.showBtn()}
  86. ]
  87. ]
  88. });
  89. }
  90. // 为表格绑定事件
  91. Table.api.bindevent(table);
  92. },
  93. add: function () {
  94. Controller.api.bindevent();
  95. },
  96. edit: function () {
  97. contentArr = JSON.parse(Config.row.content);
  98. if (contentArr.length > 0) {
  99. var html = '';
  100. for (var j = 0; j < contentArr.length; j++) {
  101. var arr = contentArr[j];
  102. var id = arr.id, image = arr.image, description = arr.description;
  103. html += '<div class="page-per" id="content-page'+id+'">'+
  104. '<p><img class="content-image" src="'+image+'" alt="图片"><span>'+description+'</span></p>'+
  105. '<div class="no-border btn-opreate" data-row_id="'+id+'">'+
  106. '<span><a href="javascript:;" class="btn btn-modify"><i class="fa fa-pencil-square-o" aria-hidden="true"></i>编辑</a></span>'+
  107. '<span><a href="javascript:;" class="btn btn-deleted"><i class="fa fa-trash-o" aria-hidden="true"></i>删除</a></span>'+
  108. '</div>'+
  109. '</div>';
  110. }
  111. $(".img_txt_list").append(html);
  112. $("#content_json").val(JSON.stringify(contentArr));
  113. }
  114. Controller.api.bindevent();
  115. },
  116. api: {
  117. bindevent: function () {
  118. Form.api.bindevent($("form[role=form]"));
  119. //弹出选择
  120. $(document).on('click', '#select-resources', function () {
  121. Fast.api.open('booklist/media/select', '选择', {
  122. callback: function (rows) {
  123. if (rows.length > 0) {
  124. for (var m=0; m < rows.length; m++) {
  125. parseData(rows[m]);
  126. }
  127. }
  128. }
  129. });
  130. });
  131. //弹出新增
  132. $(document).on('click', '#add-resources', function () {
  133. Fast.api.open('booklist/media/add?from_window=new', '新增', {
  134. callback: function (data) {
  135. parseData(data);
  136. }
  137. });
  138. });
  139. //初始化结果
  140. var parseData = function (row) {
  141. var id = row.id;
  142. //判断是否存在
  143. if (contentArr.length > 0) {
  144. for(var i = 0; i< contentArr.length; i++) {
  145. if (contentArr[i].id == id) {
  146. //已存在 不在添加
  147. Toastr.error("书单已存在该资源,请勿重复选择");
  148. return false;
  149. break;
  150. }
  151. }
  152. }
  153. var html = '<div class="page-per" id="content-page'+id+'">'+
  154. '<p><img class="content-image" src="'+row.image+'" alt="图片"><span>'+row.description+'</span></p>'+
  155. '<div class="no-border btn-opreate" data-row_id="'+id+'">'+
  156. '<span><a href="javascript:;" class="btn btn-modify"><i class="fa fa-pencil-square-o" aria-hidden="true"></i>编辑</a></span>'+
  157. '<span><a href="javascript:;" class="btn btn-deleted"><i class="fa fa-trash-o" aria-hidden="true"></i>删除</a></span>'+
  158. '</div>'+
  159. '</div>';
  160. $(".img_txt_list").append(html);
  161. contentArr.push(row);
  162. addContent();
  163. };
  164. var addContent = function() {
  165. $("#content_json").val(JSON.stringify(contentArr));
  166. };
  167. var delContent = function(id)
  168. {
  169. for(var i = 0; i< contentArr.length; i++) {
  170. if (contentArr[i].id == id) {
  171. contentArr.splice(i, 1);
  172. $("#content-page"+id).remove();
  173. break;
  174. }
  175. }
  176. $("#content_json").val(JSON.stringify(contentArr));
  177. };
  178. //编辑
  179. $(document).on('click', '.btn-modify', function () {
  180. var id = $(this).parent().parent().data('row_id');
  181. Fast.api.open('booklist/media/edit/ids/'+id+'?from_window=edit', '编辑', {
  182. callback: function (data) {
  183. delContent(id);
  184. parseData(data);
  185. }
  186. });
  187. });
  188. //删除
  189. $(document).on('click', '.btn-deleted', function () {
  190. var id = $(this).parent().parent().data('row_id');
  191. delContent(id);
  192. });
  193. $(document).on('click', '.btn-operate-copy', function () {
  194. $(this).parent().parent().find('.url_text').click();
  195. //$(this).parent().parent().find('.url_text').trigger("click");
  196. });
  197. },
  198. source_url: function (value, row, index) {
  199. var html = [];
  200. value = row.url;
  201. if (row.status == "normal") {
  202. html.push('<div class="link_txt"></div><div class="url_text">' + value + '&nbsp;<i class="fa fa-copy copy" data-clipboard-text="' + value + '"></i>');
  203. } else {
  204. html.push('<div class="link_txt">该书单已关闭不可使用</div>');
  205. }
  206. return html.join('<br>');
  207. },
  208. formatter: {
  209. showBtn: function(){
  210. return [
  211. {name: 'clear_cache', text: '清除书单缓存', title: '清除书单缓存', classname: 'btn btn-xs btn-danger btn-ajax', url: '/admin/booklist/book/clearcache', callback:function(data){}}
  212. ];
  213. },
  214. }
  215. }
  216. };
  217. return Controller;
  218. });