channel.js 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  1. define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {
  2. //设置弹窗宽高
  3. Fast.config.openArea = ['80%', '80%'];
  4. var Controller = {
  5. index: function () {
  6. // 初始化表格参数配置
  7. Table.api.init({
  8. extend: {
  9. index_url: 'cms/channel/index',
  10. add_url: 'cms/channel/add',
  11. edit_url: 'cms/channel/edit',
  12. del_url: 'cms/channel/del',
  13. multi_url: 'cms/channel/multi',
  14. dragsort_url: '',
  15. table: 'cms_channel',
  16. }
  17. });
  18. var table = $("#table");
  19. // 初始化表格
  20. table.bootstrapTable({
  21. url: $.fn.bootstrapTable.defaults.extend.index_url,
  22. pk: 'id',
  23. sortName: 'weigh',
  24. pagination: false,
  25. escape: false,
  26. fixedColumns: true,
  27. fixedRightNumber: 1,
  28. columns: [
  29. [
  30. {checkbox: true},
  31. {field: 'id', title: __('Id')},
  32. {
  33. field: 'type',
  34. title: __('Type'),
  35. custom: {channel: 'info', list: 'success', link: 'primary', home_button: 'warning', footer_button: 'danger'},
  36. formatter: Table.api.formatter.flag
  37. },
  38. {field: 'model_name', title: __('Model_name'), operate: false},
  39. {field: 'name', title: __('Name'), align: 'left'},
  40. {
  41. field: 'url', title: __('Url'), operate: false, formatter: function (value, row, index) {
  42. return '<a href="' + value + '" target="_blank" class="btn btn-default btn-xs"><i class="fa fa-link"></i></a>';
  43. }
  44. },
  45. {field: 'flag', title: __('Flag'), operate: 'find_in_set', searchList: Config.flagList, formatter: Table.api.formatter.flag},
  46. {
  47. field: 'spiders', title: __('Spiders'), visible: Config.spiderRecord || false, operate: false, formatter: function (value, row, index) {
  48. if (!$.isArray(value) || value.length === 0) {
  49. return '-';
  50. }
  51. var html = [];
  52. $.each(value, function (i, j) {
  53. var color = 'default', title = '暂无来访记录';
  54. if (j.status === 'today') {
  55. color = 'danger';
  56. title = "今天有来访记录";
  57. } else if (j.status === 'pass') {
  58. color = 'success';
  59. title = "最后来访日期:" + j.date;
  60. }
  61. html.push('<span class="label label-' + color + '" data-toggle="tooltip" data-title="' + j.title + ' ' + title + '">' + j.title.substr(0, 1) + '</span>');
  62. });
  63. return html.join(" ");
  64. }
  65. },
  66. {field: 'items', title: __('Items')},
  67. {field: 'vip', title: __('Vip')},
  68. {
  69. field: 'weigh',
  70. title: __('Weigh'),
  71. formatter: function (value, row, index) {
  72. return '<input type="text" class="form-control text-center text-weigh" data-id="' + row.id + '" value="' + value + '" style="width:50px;margin:0 auto;" />';
  73. },
  74. events: {
  75. "dblclick .text-weigh": function (e) {
  76. e.preventDefault();
  77. e.stopPropagation();
  78. return false;
  79. }
  80. }
  81. },
  82. {
  83. field: 'createtime',
  84. title: __('Createtime'),
  85. visible: false,
  86. operate: 'RANGE',
  87. addclass: 'datetimerange',
  88. formatter: Table.api.formatter.datetime
  89. },
  90. {
  91. field: 'updatetime',
  92. title: __('Updatetime'),
  93. visible: false,
  94. operate: 'RANGE',
  95. addclass: 'datetimerange',
  96. formatter: Table.api.formatter.datetime
  97. },
  98. {field: 'iscontribute', title: __('Iscontribute'), searchList: {"1": __('Yes'), "0": __('No')}, formatter: Table.api.formatter.toggle},
  99. {field: 'isnav', title: __('Isnav'), searchList: {"1": __('Yes'), "0": __('No')}, formatter: Table.api.formatter.toggle},
  100. {field: 'status', title: __('Status'), formatter: Table.api.formatter.status},
  101. {
  102. field: 'id',
  103. title: '<a href="javascript:;" class="btn btn-success btn-xs btn-toggle"><i class="fa fa-chevron-up"></i></a>',
  104. operate: false,
  105. formatter: Controller.api.formatter.subnode
  106. },
  107. {
  108. field: 'operate',
  109. title: __('Operate'),
  110. clickToSelect: false,
  111. table: table,
  112. width: 170,
  113. events: Table.api.events.operate,
  114. formatter: Table.api.formatter.operate,
  115. buttons: [
  116. {
  117. name: 'addsub',
  118. text: '添加子栏目',
  119. classname: 'btn btn-info btn-xs btn-dialog',
  120. icon: 'fa fa-plus',
  121. url: 'cms/channel/add/parent_id/{ids}'
  122. }
  123. ]
  124. }
  125. ]
  126. ],
  127. search: false,
  128. commonSearch: false
  129. });
  130. // 绑定TAB事件
  131. $('.panel-heading a[data-toggle="tab"]').on('shown.bs.tab', function (e) {
  132. var field = $(this).closest("ul").data("field");
  133. var value = $(this).data("value");
  134. var options = table.bootstrapTable('getOptions');
  135. options.pageNumber = 1;
  136. options.queryParams = function (params) {
  137. params.model_id = value;
  138. return params;
  139. };
  140. table.bootstrapTable('refresh', {});
  141. return false;
  142. });
  143. $(document).on("change", ".text-weigh", function () {
  144. $(this).data("params", {weigh: $(this).val()});
  145. Table.api.multi('', [$(this).data("id")], table, this);
  146. return false;
  147. });
  148. //当内容渲染完成后
  149. table.on('post-body.bs.table', function (e, settings, json, xhr) {
  150. //默认隐藏所有子节点
  151. //$("a.btn[data-id][data-pid][data-pid!=0]").closest("tr").hide();
  152. //$(".btn-node-sub.disabled[data-pid!=0]").closest("tr").hide();
  153. //显示隐藏子节点
  154. $(".btn-node-sub").off("click").on("click", function (e) {
  155. var status = $(this).data("shown") || $("a.btn[data-pid='" + $(this).data("id") + "']:visible").length > 0 ? true : false;
  156. $("a.btn[data-pid='" + $(this).data("id") + "']").each(function () {
  157. $(this).closest("tr").toggle(!status);
  158. if (!$(this).hasClass("disabled")) {
  159. $(this).trigger("click");
  160. }
  161. });
  162. $(this).data("shown", !status);
  163. return false;
  164. });
  165. });
  166. //展开隐藏一级
  167. $(document.body).on("click", ".btn-toggle", function (e) {
  168. $("a.btn[data-id][data-pid][data-pid!=0].disabled").closest("tr").hide();
  169. var that = this;
  170. var show = $("i", that).hasClass("fa-chevron-down");
  171. $("i", that).toggleClass("fa-chevron-down", !show);
  172. $("i", that).toggleClass("fa-chevron-up", show);
  173. $("a.btn[data-id][data-pid][data-pid!=0]").closest("tr").toggle(show);
  174. $(".btn-node-sub[data-pid=0]").data("shown", show);
  175. });
  176. //展开隐藏全部
  177. $(document.body).on("click", ".btn-toggle-all", function (e) {
  178. var that = this;
  179. var show = $("i", that).hasClass("fa-plus");
  180. $("i", that).toggleClass("fa-plus", !show);
  181. $("i", that).toggleClass("fa-minus", show);
  182. $(".btn-node-sub.disabled[data-pid!=0]").closest("tr").toggle(show);
  183. $(".btn-node-sub[data-pid!=0]").data("shown", show);
  184. });
  185. // 为表格绑定事件
  186. Table.api.bindevent(table);
  187. },
  188. add: function () {
  189. Controller.api.bindevent();
  190. $("input[name='row[type]'][value=list]").trigger("click");
  191. $("select[name='row[model_id]']").trigger("change");
  192. },
  193. edit: function () {
  194. Controller.api.bindevent();
  195. $("input[name='row[type]']:checked").trigger("fa.event.typeupdated", "edit");
  196. var data = Config.linkdata;
  197. if (typeof data.type !== 'undefined') {
  198. $("#c-outlink").prop("readonly", true);
  199. $("#outlink-extend").html(Template("outlinktpl", data));
  200. }
  201. },
  202. admin: function () {
  203. // 初始化表格参数配置
  204. Table.api.init({
  205. extend: {
  206. index_url: 'cms/channel/admin',
  207. dragsort_url: '',
  208. table: 'channel_admin',
  209. }
  210. });
  211. var table = $("#table");
  212. // 初始化表格
  213. table.bootstrapTable({
  214. url: $.fn.bootstrapTable.defaults.extend.index_url,
  215. pk: 'id',
  216. sortName: 'weigh',
  217. pagination: false,
  218. escape: false,
  219. columns: [
  220. [
  221. {
  222. field: 'superadmin', title: __('Type'), formatter: function (value, row, index) {
  223. return row.superadmin ? "<span class='label label-danger'>超级管理员</span>" : "<span class='label label-success'>普通管理员</span>";
  224. }
  225. },
  226. {field: 'username', title: __('Username')},
  227. {field: 'nickname', title: __('Nickname')},
  228. {field: 'channels', title: __('Channels')},
  229. {
  230. field: 'operate',
  231. title: __('Operate'),
  232. table: table,
  233. formatter: Table.api.formatter.buttons,
  234. buttons: [
  235. {
  236. name: 'authorization',
  237. text: __('Authorization'),
  238. classname: 'btn btn-xs btn-success btn-authorization',
  239. icon: 'fa fa-list',
  240. url: 'cms/channel/admin/act/authorization',
  241. visible: function (row) {
  242. return !row.superadmin;
  243. },
  244. },
  245. {
  246. name: 'remove',
  247. text: __('Remove'),
  248. classname: 'btn btn-xs btn-danger btn-remove btn-ajax',
  249. icon: 'fa fa-times',
  250. url: 'cms/channel/admin/act/remove',
  251. visible: function (row) {
  252. return row.channels > 0;
  253. },
  254. confirm: __('Are you sure you want to remove this item?'),
  255. success: function (ret) {
  256. $(".btn-refresh").trigger("click");
  257. }
  258. }
  259. ]
  260. }
  261. ]
  262. ],
  263. search: false,
  264. commonSearch: false
  265. });
  266. // 为表格绑定事件
  267. Table.api.bindevent(table);
  268. require(['jstree'], function () {
  269. //全选和展开
  270. $(document).on("click", "#checkall", function () {
  271. $("#channeltree").jstree($(this).prop("checked") ? "check_all" : "uncheck_all");
  272. });
  273. $(document).on("click", "#expandall", function () {
  274. $("#channeltree").jstree($(this).prop("checked") ? "open_all" : "close_all");
  275. });
  276. // 点击授权
  277. $(document).on("click", ".btn-authorization", function () {
  278. var row = Table.api.getrowbyindex(table, $(this).data("row-index"));
  279. Fast.api.ajax($(this).attr("href"), function (data, ret) {
  280. Layer.open({
  281. id: "auth",
  282. type: 1,
  283. title: __('Authorization'),
  284. btn: [__('Save')],
  285. area: ["600px", "400px"],
  286. content: Template("authorizationtpl", {}),
  287. success: function () {
  288. $('#channeltree').jstree({
  289. "themes": {
  290. "stripes": true
  291. },
  292. "checkbox": {
  293. "keep_selected_style": false,
  294. },
  295. "types": {
  296. "channel": {
  297. "icon": "fa fa-th",
  298. },
  299. "list": {
  300. "icon": "fa fa-list",
  301. },
  302. "link": {
  303. "icon": "fa fa-link",
  304. },
  305. "disabled": {
  306. "check_node": false,
  307. "uncheck_node": false
  308. }
  309. },
  310. 'plugins': ["types", "checkbox"],
  311. "core": {
  312. "multiple": true,
  313. 'check_callback': true,
  314. "data": data
  315. }
  316. });
  317. },
  318. yes: function (index, o) {
  319. var selected = $("#channeltree", o).jstree("get_selected");
  320. if (selected.length <= 0) {
  321. Layer.msg(__('You must choose at least one channel'), {id: "aaafd"});
  322. } else {
  323. Fast.api.ajax({
  324. url: "cms/channel/admin/act/save/ids/" + row.id,
  325. data: {"ids": selected.join(",")}
  326. }, function (data, ret) {
  327. $(".btn-refresh").trigger("click");
  328. Layer.close(index);
  329. });
  330. }
  331. }
  332. });
  333. return false;
  334. });
  335. return false;
  336. });
  337. });
  338. },
  339. api: {
  340. formatter: {
  341. title: function (value, row, index) {
  342. return !row.ismenu || row.status == 'hidden' ? "<span class='text-muted'>" + value + "</span>" : value;
  343. },
  344. name: function (value, row, index) {
  345. return !row.ismenu || row.status == 'hidden' ? "<span class='text-muted'>" + value + "</span>" : value;
  346. },
  347. icon: function (value, row, index) {
  348. return '<span class="' + (!row.ismenu || row.status == 'hidden' ? 'text-muted' : '') + '"><i class="' + value + '"></i></span>';
  349. },
  350. subnode: function (value, row, index) {
  351. return '<a href="javascript:;" data-toggle="tooltip" title="' + __('Toggle sub menu') + '" data-id="' + row.id + '" data-pid="' + row.pid + '" class="btn btn-xs '
  352. + (row.haschild == 1 || row.ismenu == 1 ? 'btn-success' : 'btn-default disabled') + ' btn-node-sub"><i class="fa fa-' + (row.haschild == 1 || row.ismenu == 1 ? 'sitemap' : 'list') + '"></i></a>';
  353. }
  354. },
  355. bindevent: function () {
  356. $.validator.config({
  357. rules: {
  358. single: function (element) {
  359. return !$("#c-name").val().match(/\n/);
  360. },
  361. channelname: function (element) {
  362. if (element.value.toString().match(/^\d+$/)) {
  363. return __('Can not be only digital');
  364. }
  365. return $.ajax({
  366. url: 'cms/channel/check_element_available',
  367. type: 'POST',
  368. data: {id: $("#c-name").val(), name: element.name, value: element.value},
  369. dataType: 'json'
  370. });
  371. },
  372. diyname: function (element) {
  373. if (element.value.toString().match(/^\d+$/)) {
  374. return __('Can not be only digital');
  375. }
  376. if (!element.value.toString().match(/^[a-zA-Z0-9\-_]+$/)) {
  377. return __('Please input character or digital');
  378. }
  379. return $.ajax({
  380. url: 'cms/channel/check_element_available',
  381. type: 'POST',
  382. data: {id: $("#channel-id").val(), name: element.name, value: element.value},
  383. dataType: 'json'
  384. });
  385. }
  386. }
  387. });
  388. //不可见的元素不验证
  389. $("form[role=form]").data("validator-options", {ignore: ':hidden'});
  390. //获取栏目拼音
  391. var si;
  392. $(document).on("keyup", "#c-name", function () {
  393. var value = $(this).val();
  394. if (value != '' && !value.match(/\n/)) {
  395. clearTimeout(si);
  396. si = setTimeout(function () {
  397. Fast.api.ajax({
  398. loading: false,
  399. url: "cms/ajax/get_title_pinyin",
  400. data: {title: value}
  401. }, function (data, ret) {
  402. $("#c-diyname").val(data.pinyin.substr(0, 100));
  403. return false;
  404. }, function (data, ret) {
  405. return false;
  406. });
  407. }, 200);
  408. }
  409. });
  410. $(document).on("click fa.event.typeupdated", "input[name='row[type]']", function (e, ref) {
  411. $(".tf").addClass("hidden");
  412. $(".tf.tf-" + $(this).val()).removeClass("hidden");
  413. if (typeof ref == 'undefined') {
  414. $("select[name='row[model_id]']").trigger("change");
  415. }
  416. if ($(this).val() == 'link') {
  417. //$("#parent_id option").prop("disabled", false);
  418. }
  419. });
  420. Form.api.bindevent($("form[role=form]"));
  421. $(document).on("change", "select[name='row[model_id]']", function () {
  422. // var parentChannel = $("#parent_id");
  423. // $("option[value=0]", parentChannel).prop("selected", true);
  424. // $("option[data-model]", parentChannel).prop("disabled", true);
  425. // $("option[data-model='" + $(this).val() + "']", parentChannel).prop("disabled", false);
  426. var data = $("option:selected", this).data();
  427. var type = $("input[name='row[type]']:checked").val();
  428. $("input[name='row[channeltpl]']").val(data.channeltpl).prev().val(data.channeltpl);
  429. $("input[name='row[listtpl]']").val(data.listtpl).prev().val(data.listtpl);
  430. $("input[name='row[showtpl]']").val(data.showtpl).prev().val(data.showtpl);
  431. });
  432. $(document).on("click", ".btn-select-link", function () {
  433. var url = $(this).data("url");
  434. parent.Fast.api.open(url, "选择链接", {
  435. callback: function (data) {
  436. $("#c-outlink").val(data.url).prop("readonly", true);
  437. $("#outlink-extend").html(Template("outlinktpl", data));
  438. $("input[name='row[linktype]']").val(data.type);
  439. $("input[name='row[linkid]']").val(data.source_id);
  440. }
  441. });
  442. });
  443. $(document).on("click", ".btn-remove-link", function () {
  444. $("#c-outlink").val('').prop("readonly", false);
  445. $("#outlink-extend").html('');
  446. $("input[name='row[linktype]']").val('');
  447. $("input[name='row[linkid]']").val('0');
  448. });
  449. }
  450. }
  451. };
  452. return Controller;
  453. });