index.js 20 KB

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