index.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  1. define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {
  2. var Controller = {
  3. index: function () {
  4. // 初始化表格参数配置
  5. Table.api.init({
  6. extend: {
  7. index_url: 'department/index/index',
  8. add_url: 'department/index/add',
  9. edit_url: 'department/index/edit',
  10. del_url: 'department/index/del',
  11. multi_url: 'department/index/multi',
  12. dragsort_url: '',
  13. table: 'department',
  14. }
  15. });
  16. var table = $("#table");
  17. // 初始化表格
  18. table.bootstrapTable({
  19. url: $.fn.bootstrapTable.defaults.extend.index_url,
  20. pk: 'id',
  21. sortName: 'weigh',
  22. sortOrder: 'desc',
  23. pagination: false,
  24. escape: false,
  25. fixedColumns: true,
  26. fixedRightNumber: 1,
  27. showExport:false,
  28. showToggle:false,
  29. showColumns: false,
  30. columns: [
  31. [
  32. {checkbox: true},
  33. // {field: 'id', title: __('编号')},
  34. {field: 'name', title: __('Name'), align: 'left'},
  35. // {
  36. // field: 'weigh',
  37. // title: __('Weigh'),
  38. // // formatter: function (value, row, index) {
  39. // // return '<input type="text" class="form-control text-center text-weigh" data-id="' + row.id + '" value="' + value + '" style="width:50px;margin:0 auto;" />';
  40. // // },
  41. // // events: {
  42. // // "dblclick .text-weigh": function (e) {
  43. // // e.preventDefault();
  44. // // e.stopPropagation();
  45. // // return false;
  46. // // }
  47. // // }
  48. // },
  49. {
  50. field: 'createtime',
  51. title: __('Createtime'),
  52. visible: false,
  53. operate: 'RANGE',
  54. addclass: 'datetimerange',
  55. formatter: Table.api.formatter.datetime
  56. },
  57. {
  58. field: 'updatetime',
  59. title: __('Updatetime'),
  60. visible: false,
  61. operate: 'RANGE',
  62. addclass: 'datetimerange',
  63. formatter: Table.api.formatter.datetime
  64. },
  65. {field: 'status', title: __('Status'), formatter: Table.api.formatter.status},
  66. // {
  67. // field: 'tags',
  68. // title: __('标签属性'),
  69. // operate: "LIKE",
  70. // align: 'left',
  71. // formatter: function (value, row, index) {
  72. // return Table.api.formatter.flag.call(this, row['tags'], row, index);
  73. // }
  74. // },
  75. {
  76. field: 'id',
  77. title: '<a href="javascript:;" class="btn btn-success btn-xs btn-toggle"><i class="fa fa-chevron-up"></i></a>',
  78. operate: false,
  79. formatter: Controller.api.formatter.subnode
  80. },
  81. {
  82. field: 'operate',
  83. title: __('Operate'),
  84. table: table,
  85. width: 160,
  86. events: Table.api.events.operate,
  87. formatter: Table.api.formatter.buttons,
  88. buttons: [
  89. {
  90. name: 'add',
  91. text: __('Children'),
  92. classname: 'btn btn-info btn-xs btn-dialog',
  93. icon: 'fa fa-plus',
  94. url: 'department/index/add/parent_id/{ids}'
  95. },
  96. {
  97. name: 'dispatch',
  98. text: '编辑',
  99. icon: 'fa fa-pencil',
  100. title: '编辑',
  101. classname: 'btn btn-success btn-xs btn-magic btn-dialog',
  102. url: 'department/index/edit',
  103. },{
  104. name: 'dispatch',
  105. text: '删除',
  106. icon: 'fa fa-trash',
  107. title: '删除',
  108. classname: 'btn btn-danger btn-xs btn-magic btn-ajax',
  109. url: 'department/index/del',
  110. confirm: '确定要删除吗?',
  111. success: function (data, ret) {
  112. $(".btn-refresh").trigger("click");
  113. },
  114. error: function (data, ret) {
  115. Layer.alert(ret.msg);
  116. return false;
  117. }
  118. }
  119. // {
  120. // name: 'index',
  121. // text: __('Employee'),
  122. // classname: 'btn btn-info btn-xs btn-dialog',
  123. // icon: 'fa fa-users',
  124. // url: 'department/admin/index/department_id/{ids}'
  125. // }
  126. ]
  127. }
  128. ]
  129. ],
  130. search: false,
  131. commonSearch: false
  132. });
  133. // 绑定TAB事件
  134. $('.panel-heading a[data-toggle="tab"]').on('shown.bs.tab', function (e) {
  135. var field = $(this).closest("ul").data("field");
  136. var value = $(this).data("value");
  137. var options = table.bootstrapTable('getOptions');
  138. options.pageNumber = 1;
  139. options.queryParams = function (params) {
  140. params.model_id = value;
  141. return params;
  142. };
  143. table.bootstrapTable('refresh', {});
  144. return false;
  145. });
  146. $(document).on("change", ".text-weigh", function () {
  147. $(this).data("params", {weigh: $(this).val()});
  148. Table.api.multi('', [$(this).data("id")], table, this);
  149. return false;
  150. });
  151. //当内容渲染完成后
  152. table.on('post-body.bs.table', function (e, settings, json, xhr) {
  153. //默认隐藏所有子节点
  154. $("a.btn[data-id][data-pid][data-pid!=0]").closest("tr").hide();
  155. $(".btn-node-sub.disabled[data-pid!=0]").closest("tr").hide();
  156. //显示隐藏子节点
  157. $(".btn-node-sub").off("click").on("click", function (e) {
  158. var status = $(this).data("shown") || $("a.btn[data-pid='" + $(this).data("id") + "']:visible").size() > 0 ? true : false;
  159. $("a.btn[data-pid='" + $(this).data("id") + "']").each(function () {
  160. $(this).closest("tr").toggle(!status);
  161. if (!$(this).hasClass("disabled")) {
  162. $(this).trigger("click");
  163. }
  164. });
  165. $(this).data("shown", !status);
  166. return false;
  167. });
  168. });
  169. //展开隐藏一级
  170. $(document.body).on("click", ".btn-toggle", function (e) {
  171. $("a.btn[data-id][data-pid][data-pid!=0].disabled").closest("tr").hide();
  172. var that = this;
  173. var show = $("i", that).hasClass("fa-chevron-down");
  174. $("i", that).toggleClass("fa-chevron-down", !show);
  175. $("i", that).toggleClass("fa-chevron-up", show);
  176. $("a.btn[data-id][data-pid][data-pid!=0]").closest("tr").toggle(show);
  177. $(".btn-node-sub[data-pid=0]").data("shown", show);
  178. });
  179. //展开隐藏全部
  180. $(document.body).on("click", ".btn-toggle-all", function (e) {
  181. var that = this;
  182. var show = $("i", that).hasClass("fa-plus");
  183. $("i", that).toggleClass("fa-plus", !show);
  184. $("i", that).toggleClass("fa-minus", show);
  185. $(".btn-node-sub.disabled[data-pid!=0]").closest("tr").toggle(show);
  186. $(".btn-node-sub[data-pid!=0]").data("shown", show);
  187. });
  188. // 为表格绑定事件
  189. Table.api.bindevent(table);
  190. },
  191. add: function () {
  192. Controller.api.bindevent();
  193. $("input[name='row[type]'][value=list]").trigger("click");
  194. $("select[name='row[model_id]']").trigger("change");
  195. },
  196. edit: function () {
  197. Controller.api.bindevent();
  198. $("input[name='row[type]']:checked").trigger("fa.event.typeupdated", "edit");
  199. },
  200. admin: function () {
  201. // 初始化表格参数配置
  202. Table.api.init({
  203. extend: {
  204. index_url: 'cms/channel/admin',
  205. dragsort_url: '',
  206. table: 'channel_admin',
  207. }
  208. });
  209. var table = $("#table");
  210. // 初始化表格
  211. table.bootstrapTable({
  212. url: $.fn.bootstrapTable.defaults.extend.index_url,
  213. pk: 'id',
  214. sortName: 'weigh',
  215. pagination: false,
  216. escape: false,
  217. columns: [
  218. [
  219. {
  220. field: 'superadmin', title: __('Type'), formatter: function (value, row, index) {
  221. return row.superadmin ? "<span class='label label-danger'>超级管理员</span>" : "<span class='label label-success'>普通管理员</span>";
  222. }
  223. },
  224. {field: 'username', title: __('Username')},
  225. {field: 'nickname', title: __('Nickname')},
  226. {field: 'channels', title: __('Channels')},
  227. {
  228. field: 'operate',
  229. title: __('Operate'),
  230. table: table,
  231. formatter: Table.api.formatter.buttons,
  232. buttons: [
  233. {
  234. name: 'authorization',
  235. text: __('Authorization'),
  236. classname: 'btn btn-xs btn-success btn-authorization',
  237. icon: 'fa fa-list',
  238. url: 'cms/channel/admin/act/authorization',
  239. visible: function (row) {
  240. return !row.superadmin;
  241. },
  242. },
  243. {
  244. name: 'remove',
  245. text: __('Remove'),
  246. classname: 'btn btn-xs btn-danger btn-remove btn-ajax',
  247. icon: 'fa fa-times',
  248. url: 'cms/channel/admin/act/remove',
  249. visible: function (row) {
  250. return row.channels > 0;
  251. },
  252. confirm: __('Are you sure you want to remove this item?'),
  253. success: function (ret) {
  254. $(".btn-refresh").trigger("click");
  255. }
  256. }
  257. ]
  258. }
  259. ]
  260. ],
  261. search: false,
  262. commonSearch: false
  263. });
  264. // 为表格绑定事件
  265. Table.api.bindevent(table);
  266. require(['jstree'], function () {
  267. //全选和展开
  268. $(document).on("click", "#checkall", function () {
  269. $("#channeltree").jstree($(this).prop("checked") ? "check_all" : "uncheck_all");
  270. });
  271. $(document).on("click", "#expandall", function () {
  272. $("#channeltree").jstree($(this).prop("checked") ? "open_all" : "close_all");
  273. });
  274. // 点击授权
  275. $(document).on("click", ".btn-authorization", function () {
  276. var row = Table.api.getrowbyindex(table, $(this).data("row-index"));
  277. Fast.api.ajax($(this).attr("href"), function (data, ret) {
  278. Layer.open({
  279. id: "auth",
  280. type: 1,
  281. title: __('Authorization'),
  282. btn: [__('Save')],
  283. area: ["600px", "400px"],
  284. content: Template("authorizationtpl", {}),
  285. success: function () {
  286. $('#channeltree').jstree({
  287. "themes": {
  288. "stripes": true
  289. },
  290. "checkbox": {
  291. "keep_selected_style": false,
  292. },
  293. "types": {
  294. "channel": {
  295. "icon": "fa fa-th",
  296. },
  297. "list": {
  298. "icon": "fa fa-list",
  299. },
  300. "link": {
  301. "icon": "fa fa-link",
  302. },
  303. "disabled": {
  304. "check_node": false,
  305. "uncheck_node": false
  306. }
  307. },
  308. 'plugins': ["types", "checkbox"],
  309. "core": {
  310. "multiple": true,
  311. 'check_callback': true,
  312. "data": data
  313. }
  314. });
  315. },
  316. yes: function (index, o) {
  317. var selected = $("#channeltree", o).jstree("get_selected");
  318. if (selected.length <= 0) {
  319. Layer.msg(__('You must choose at least one channel'), {id: "aaafd"});
  320. } else {
  321. Fast.api.ajax({
  322. url: "cms/channel/admin/act/save/ids/" + row.id,
  323. data: {"ids": selected.join(",")}
  324. }, function (data, ret) {
  325. $(".btn-refresh").trigger("click");
  326. Layer.close(index);
  327. });
  328. }
  329. }
  330. });
  331. return false;
  332. });
  333. return false;
  334. });
  335. });
  336. },
  337. api: {
  338. formatter: {
  339. title: function (value, row, index) {
  340. return !row.ismenu || row.status == 'hidden' ? "<span class='text-muted'>" + value + "</span>" : value;
  341. },
  342. name: function (value, row, index) {
  343. return !row.ismenu || row.status == 'hidden' ? "<span class='text-muted'>" + value + "</span>" : value;
  344. },
  345. icon: function (value, row, index) {
  346. return '<span class="' + (!row.ismenu || row.status == 'hidden' ? 'text-muted' : '') + '"><i class="' + value + '"></i></span>';
  347. },
  348. subnode: function (value, row, index) {
  349. return '<a href="javascript:;" data-toggle="tooltip" title="' + __('Toggle sub menu') + '" data-id="' + row.id + '" data-pid="' + row.pid + '" class="btn btn-xs '
  350. + (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>';
  351. },
  352. },
  353. bindevent: function () {
  354. Form.api.bindevent($("form[role=form]"));
  355. require.config({
  356. paths: {
  357. 'tagsinput': '../addons/wscrm/js/jquery.tagsinput',
  358. 'autocomplete': '../addons/wscrm/js/jquery.autocomplete',
  359. },
  360. shim: {
  361. 'autocomplete': {
  362. deps: ['jquery'],
  363. exports: '$.fn.extend'
  364. },
  365. 'tagsinput': {
  366. deps: ['jquery', 'autocomplete', 'css!../addons/wscrm/css/jquery.tagsinput.min.css'],
  367. exports: '$.fn.extend'
  368. }
  369. }
  370. }
  371. );
  372. require(['tagsinput'], function () {
  373. //标签输入
  374. var elem = "#c-tags";
  375. var tags = $(elem);
  376. tags.tagsInput({
  377. width: 'auto',
  378. defaultText: '输入后空格确认',
  379. minInputWidth: 110,
  380. height: '36px',
  381. placeholderColor: '#999',
  382. onChange: function (row) {
  383. if (typeof callback === 'function') {
  384. } else {
  385. $(elem + "_addTag").focus();
  386. $(elem + "_tag").trigger("blur.autocomplete").focus();
  387. }
  388. },
  389. autocomplete: {
  390. url: 'wscrm/tag/autocomplete?type=customer',
  391. minChars: 1,
  392. menuClass: 'autocomplete-tags'
  393. }
  394. });
  395. });
  396. }
  397. }
  398. };
  399. return Controller;
  400. });