index.js 19 KB

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