channel.js 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  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. name: 'editor',
  125. classname: 'btn btn-xs btn-warning btn-dialog',
  126. icon: 'fa fa-user-circle-o',
  127. title: __('Assign editors'),
  128. text: __('Assign editors'),
  129. url: function (row) {
  130. return 'cms/channel/add_chief?ids=' + row.id;
  131. },
  132. visible: function (row) {
  133. if (row.pid == 1) {
  134. return true;
  135. }
  136. },
  137. callback: function (data) {
  138. Layer.alert("接收到回传数据:" + JSON.stringify(data), {title: "回传数据"});
  139. },
  140. },
  141. ]
  142. }
  143. ]
  144. ],
  145. search: false,
  146. commonSearch: false
  147. });
  148. // 绑定TAB事件
  149. $('.panel-heading a[data-toggle="tab"]').on('shown.bs.tab', function (e) {
  150. var field = $(this).closest("ul").data("field");
  151. var value = $(this).data("value");
  152. var options = table.bootstrapTable('getOptions');
  153. options.pageNumber = 1;
  154. options.queryParams = function (params) {
  155. params.model_id = value;
  156. return params;
  157. };
  158. table.bootstrapTable('refresh', {});
  159. return false;
  160. });
  161. $(document).on("change", ".text-weigh", function () {
  162. $(this).data("params", {weigh: $(this).val()});
  163. Table.api.multi('', [$(this).data("id")], table, this);
  164. return false;
  165. });
  166. //当内容渲染完成后
  167. table.on('post-body.bs.table', function (e, settings, json, xhr) {
  168. //默认隐藏所有子节点
  169. //$("a.btn[data-id][data-pid][data-pid!=0]").closest("tr").hide();
  170. //$(".btn-node-sub.disabled[data-pid!=0]").closest("tr").hide();
  171. //显示隐藏子节点
  172. $(".btn-node-sub").off("click").on("click", function (e) {
  173. var status = $(this).data("shown") || $("a.btn[data-pid='" + $(this).data("id") + "']:visible").length > 0 ? true : false;
  174. $("a.btn[data-pid='" + $(this).data("id") + "']").each(function () {
  175. $(this).closest("tr").toggle(!status);
  176. if (!$(this).hasClass("disabled")) {
  177. $(this).trigger("click");
  178. }
  179. });
  180. $(this).data("shown", !status);
  181. return false;
  182. });
  183. });
  184. //展开隐藏一级
  185. $(document.body).on("click", ".btn-toggle", function (e) {
  186. $("a.btn[data-id][data-pid][data-pid!=0].disabled").closest("tr").hide();
  187. var that = this;
  188. var show = $("i", that).hasClass("fa-chevron-down");
  189. $("i", that).toggleClass("fa-chevron-down", !show);
  190. $("i", that).toggleClass("fa-chevron-up", show);
  191. $("a.btn[data-id][data-pid][data-pid!=0]").closest("tr").toggle(show);
  192. $(".btn-node-sub[data-pid=0]").data("shown", show);
  193. });
  194. //展开隐藏全部
  195. $(document.body).on("click", ".btn-toggle-all", function (e) {
  196. var that = this;
  197. var show = $("i", that).hasClass("fa-plus");
  198. $("i", that).toggleClass("fa-plus", !show);
  199. $("i", that).toggleClass("fa-minus", show);
  200. $(".btn-node-sub.disabled[data-pid!=0]").closest("tr").toggle(show);
  201. $(".btn-node-sub[data-pid!=0]").data("shown", show);
  202. });
  203. // 为表格绑定事件
  204. Table.api.bindevent(table);
  205. },
  206. add: function () {
  207. Controller.api.bindevent();
  208. $("input[name='row[type]'][value=list]").trigger("click");
  209. $("select[name='row[model_id]']").trigger("change");
  210. },
  211. edit: function () {
  212. Controller.api.bindevent();
  213. $("input[name='row[type]']:checked").trigger("fa.event.typeupdated", "edit");
  214. var data = Config.linkdata;
  215. if (typeof data.type !== 'undefined') {
  216. $("#c-outlink").prop("readonly", true);
  217. $("#outlink-extend").html(Template("outlinktpl", data));
  218. }
  219. },
  220. add_chief: function () {
  221. Controller.api.bindevent();
  222. },
  223. admin: function () {
  224. // 初始化表格参数配置
  225. Table.api.init({
  226. extend: {
  227. index_url: 'cms/channel/admin',
  228. dragsort_url: '',
  229. table: 'channel_admin',
  230. }
  231. });
  232. var table = $("#table");
  233. // 初始化表格
  234. table.bootstrapTable({
  235. url: $.fn.bootstrapTable.defaults.extend.index_url,
  236. pk: 'id',
  237. sortName: 'weigh',
  238. pagination: false,
  239. escape: false,
  240. columns: [
  241. [
  242. {
  243. field: 'superadmin', title: __('Type'), formatter: function (value, row, index) {
  244. return row.superadmin ? "<span class='label label-danger'>超级管理员</span>" : "<span class='label label-success'>普通管理员</span>";
  245. }
  246. },
  247. {field: 'username', title: __('Username')},
  248. {field: 'nickname', title: __('Nickname')},
  249. {field: 'channels', title: __('Channels')},
  250. {
  251. field: 'operate',
  252. title: __('Operate'),
  253. table: table,
  254. formatter: Table.api.formatter.buttons,
  255. buttons: [
  256. {
  257. name: 'authorization',
  258. text: __('Authorization'),
  259. classname: 'btn btn-xs btn-success btn-authorization',
  260. icon: 'fa fa-list',
  261. url: 'cms/channel/admin/act/authorization',
  262. visible: function (row) {
  263. return !row.superadmin;
  264. },
  265. },
  266. {
  267. name: 'remove',
  268. text: __('Remove'),
  269. classname: 'btn btn-xs btn-danger btn-remove btn-ajax',
  270. icon: 'fa fa-times',
  271. url: 'cms/channel/admin/act/remove',
  272. visible: function (row) {
  273. return row.channels > 0;
  274. },
  275. confirm: __('Are you sure you want to remove this item?'),
  276. success: function (ret) {
  277. $(".btn-refresh").trigger("click");
  278. }
  279. }
  280. ]
  281. }
  282. ]
  283. ],
  284. search: false,
  285. commonSearch: false
  286. });
  287. // 为表格绑定事件
  288. Table.api.bindevent(table);
  289. require(['jstree'], function () {
  290. //全选和展开
  291. $(document).on("click", "#checkall", function () {
  292. $("#channeltree").jstree($(this).prop("checked") ? "check_all" : "uncheck_all");
  293. });
  294. $(document).on("click", "#expandall", function () {
  295. $("#channeltree").jstree($(this).prop("checked") ? "open_all" : "close_all");
  296. });
  297. // 点击授权
  298. $(document).on("click", ".btn-authorization", function () {
  299. var row = Table.api.getrowbyindex(table, $(this).data("row-index"));
  300. Fast.api.ajax($(this).attr("href"), function (data, ret) {
  301. Layer.open({
  302. id: "auth",
  303. type: 1,
  304. title: __('Authorization'),
  305. btn: [__('Save')],
  306. area: ["600px", "400px"],
  307. content: Template("authorizationtpl", {}),
  308. success: function () {
  309. $('#channeltree').jstree({
  310. "themes": {
  311. "stripes": true
  312. },
  313. "checkbox": {
  314. "keep_selected_style": false,
  315. },
  316. "types": {
  317. "channel": {
  318. "icon": "fa fa-th",
  319. },
  320. "list": {
  321. "icon": "fa fa-list",
  322. },
  323. "link": {
  324. "icon": "fa fa-link",
  325. },
  326. "disabled": {
  327. "check_node": false,
  328. "uncheck_node": false
  329. }
  330. },
  331. 'plugins': ["types", "checkbox"],
  332. "core": {
  333. "multiple": true,
  334. 'check_callback': true,
  335. "data": data
  336. }
  337. });
  338. },
  339. yes: function (index, o) {
  340. var selected = $("#channeltree", o).jstree("get_selected");
  341. if (selected.length <= 0) {
  342. Layer.msg(__('You must choose at least one channel'), {id: "aaafd"});
  343. } else {
  344. Fast.api.ajax({
  345. url: "cms/channel/admin/act/save/ids/" + row.id,
  346. data: {"ids": selected.join(",")}
  347. }, function (data, ret) {
  348. $(".btn-refresh").trigger("click");
  349. Layer.close(index);
  350. });
  351. }
  352. }
  353. });
  354. return false;
  355. });
  356. return false;
  357. });
  358. });
  359. },
  360. api: {
  361. formatter: {
  362. title: function (value, row, index) {
  363. return !row.ismenu || row.status == 'hidden' ? "<span class='text-muted'>" + value + "</span>" : value;
  364. },
  365. name: function (value, row, index) {
  366. return !row.ismenu || row.status == 'hidden' ? "<span class='text-muted'>" + value + "</span>" : value;
  367. },
  368. icon: function (value, row, index) {
  369. return '<span class="' + (!row.ismenu || row.status == 'hidden' ? 'text-muted' : '') + '"><i class="' + value + '"></i></span>';
  370. },
  371. subnode: function (value, row, index) {
  372. return '<a href="javascript:;" data-toggle="tooltip" title="' + __('Toggle sub menu') + '" data-id="' + row.id + '" data-pid="' + row.pid + '" class="btn btn-xs '
  373. + (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>';
  374. }
  375. },
  376. bindevent: function () {
  377. $.validator.config({
  378. rules: {
  379. single: function (element) {
  380. return !$("#c-name").val().match(/\n/);
  381. },
  382. channelname: function (element) {
  383. if (element.value.toString().match(/^\d+$/)) {
  384. return __('Can not be only digital');
  385. }
  386. return $.ajax({
  387. url: 'cms/channel/check_element_available',
  388. type: 'POST',
  389. data: {id: $("#c-name").val(), name: element.name, value: element.value},
  390. dataType: 'json'
  391. });
  392. },
  393. diyname: function (element) {
  394. if (element.value.toString().match(/^\d+$/)) {
  395. return __('Can not be only digital');
  396. }
  397. if (!element.value.toString().match(/^[a-zA-Z0-9\-_]+$/)) {
  398. return __('Please input character or digital');
  399. }
  400. return $.ajax({
  401. url: 'cms/channel/check_element_available',
  402. type: 'POST',
  403. data: {id: $("#channel-id").val(), name: element.name, value: element.value},
  404. dataType: 'json'
  405. });
  406. }
  407. }
  408. });
  409. //不可见的元素不验证
  410. $("form[role=form]").data("validator-options", {ignore: ':hidden'});
  411. //获取栏目拼音
  412. var si;
  413. $(document).on("keyup", "#c-name", function () {
  414. var value = $(this).val();
  415. if (value != '' && !value.match(/\n/)) {
  416. clearTimeout(si);
  417. si = setTimeout(function () {
  418. Fast.api.ajax({
  419. loading: false,
  420. url: "cms/ajax/get_title_pinyin",
  421. data: {title: value}
  422. }, function (data, ret) {
  423. $("#c-diyname").val(data.pinyin.substr(0, 100));
  424. return false;
  425. }, function (data, ret) {
  426. return false;
  427. });
  428. }, 200);
  429. }
  430. });
  431. $(document).on("click fa.event.typeupdated", "input[name='row[type]']", function (e, ref) {
  432. $(".tf").addClass("hidden");
  433. $(".tf.tf-" + $(this).val()).removeClass("hidden");
  434. if (typeof ref == 'undefined') {
  435. $("select[name='row[model_id]']").trigger("change");
  436. }
  437. if ($(this).val() == 'link') {
  438. //$("#parent_id option").prop("disabled", false);
  439. }
  440. });
  441. Form.api.bindevent($("form[role=form]"));
  442. $(document).on("change", "select[name='row[model_id]']", function () {
  443. // var parentChannel = $("#parent_id");
  444. // $("option[value=0]", parentChannel).prop("selected", true);
  445. // $("option[data-model]", parentChannel).prop("disabled", true);
  446. // $("option[data-model='" + $(this).val() + "']", parentChannel).prop("disabled", false);
  447. var data = $("option:selected", this).data();
  448. var type = $("input[name='row[type]']:checked").val();
  449. $("input[name='row[channeltpl]']").val(data.channeltpl).prev().val(data.channeltpl);
  450. $("input[name='row[listtpl]']").val(data.listtpl).prev().val(data.listtpl);
  451. $("input[name='row[showtpl]']").val(data.showtpl).prev().val(data.showtpl);
  452. });
  453. $(document).on("click", ".btn-select-link", function () {
  454. var url = $(this).data("url");
  455. parent.Fast.api.open(url, "选择链接", {
  456. callback: function (data) {
  457. $("#c-outlink").val(data.url).prop("readonly", true);
  458. $("#outlink-extend").html(Template("outlinktpl", data));
  459. $("input[name='row[linktype]']").val(data.type);
  460. $("input[name='row[linkid]']").val(data.source_id);
  461. }
  462. });
  463. });
  464. $(document).on("click", ".btn-remove-link", function () {
  465. $("#c-outlink").val('').prop("readonly", false);
  466. $("#outlink-extend").html('');
  467. $("input[name='row[linktype]']").val('');
  468. $("input[name='row[linkid]']").val('0');
  469. });
  470. }
  471. }
  472. };
  473. return Controller;
  474. });