index.js 18 KB

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