channel.js 21 KB

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