index.js 19 KB

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