You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

470 lines
17 KiB

  1. (function () {
  2. /***
  3. * 配送区域表格
  4. * @param param
  5. * @constructor
  6. */
  7. function Delivery(param) {
  8. this.tableElement = param.table;
  9. this.RegionalChoice = new RegionalChoice(param.regional, param.datas);
  10. this.initCreateRegion();
  11. this.clickEditEvent();
  12. this.clickDeleteEvent();
  13. this.clickMethodEvent();
  14. }
  15. Delivery.prototype = {
  16. /**
  17. * 初始化添加区域事件
  18. */
  19. initCreateRegion: function () {
  20. var _this = this;
  21. $(_this.tableElement).find('.add-region').click(function () {
  22. // 渲染地域
  23. var str = '';
  24. $(_this.tableElement).find('input[type=hidden]').each(function (index, item) {
  25. str += $(item).val() + ',';
  26. });
  27. var alreadyIds = str.length > 0 ? str.substring(0, str.length - 1).split(',') : [];
  28. if (alreadyIds.length === 373) {
  29. layer.msg('已经选择了所有区域~');
  30. return false;
  31. }
  32. _this.RegionalChoice.render(alreadyIds);
  33. _this.showRegionalModal(function () {
  34. // 弹窗交互完成
  35. var Checked = _this.RegionalChoice.getCheckedContent();
  36. Checked.ids.length > 0 && _this.appendRulesTr(Checked.content, Checked.ids);
  37. });
  38. });
  39. },
  40. /**
  41. * 创建可配送区域规则
  42. */
  43. appendRulesTr: function (regionStr, checkedIds) {
  44. var $html = $(
  45. '<tr>' +
  46. '<td class="text-left">' +
  47. ' <p class="selected-content">' +
  48. ' ' + regionStr +
  49. ' </p>' +
  50. ' <p class="">' +
  51. ' <a class="edit" href="javascript:;">编辑</a>' +
  52. ' <a class="delete" href="javascript:;">删除</a>' +
  53. ' </p>' +
  54. ' <input type="hidden" name="row[area][]" value="' + checkedIds + '">' +
  55. '</td>' +
  56. '<td>' +
  57. ' <input type="number" name="row[first][]" min="1" value="1" required>' +
  58. '</td>' +
  59. '<td>' +
  60. ' <input type="number" name="row[first_fee][]" min="0" value="0.00" required>' +
  61. '</td>' +
  62. '<td>' +
  63. ' <input type="number" name="row[additional][]" min="1" value="1">' +
  64. '</td>' +
  65. '<td>' +
  66. ' <input type="number" name="row[additional_fee][]" min="0" value="0.00">' +
  67. '</td>' +
  68. '</tr>'
  69. );
  70. $(this.tableElement).children("tbody").find('tr:last').before($html);
  71. },
  72. /**
  73. * 显示区域选择窗口
  74. * @param callback
  75. */
  76. showRegionalModal: function (callback) {
  77. var _this = this;
  78. var area = [$(window).width() > 800 ? '800px' : '95%', $(window).height() > 600 ? '600px' : '95%'];
  79. layer.open({
  80. type: 1,
  81. shade: false,
  82. title: '选择可配送区域',
  83. btn: ['确定', '取消'],
  84. area: area, //宽高
  85. content: $('.regional-choice'),
  86. yes: function (index) {
  87. callback && callback();
  88. layer.close(index);
  89. },
  90. end: function () {
  91. // 销毁已选中区域
  92. _this.RegionalChoice.destroy();
  93. }
  94. });
  95. },
  96. /**
  97. * 编辑区域事件
  98. */
  99. clickEditEvent: function () {
  100. var _this = this
  101. , $table = $(_this.tableElement);
  102. $table.on('click', '.edit', function () {
  103. // 渲染地域
  104. var $html = $(this).parent().parent()
  105. , $content = $html.find('.selected-content')
  106. , $input = $html.find('input[type=hidden]');
  107. _this.RegionalChoice.render([], $input.val().split(','));
  108. // 显示地区选择弹窗
  109. _this.showRegionalModal(function () {
  110. // 弹窗交互完成
  111. var Checked = _this.RegionalChoice.getCheckedContent();
  112. if (Checked.ids.length > 0) {
  113. $content.html(Checked.content);
  114. $input.val(Checked.ids);
  115. }
  116. });
  117. });
  118. },
  119. /**
  120. * 删除区域事件
  121. */
  122. clickDeleteEvent: function () {
  123. var $table = $(this.tableElement);
  124. $table.on('click', '.delete', function () {
  125. var $delete = $(this);
  126. layer.confirm('确定要删除吗?', function (index) {
  127. $delete.parents('tr').remove();
  128. layer.close(index);
  129. });
  130. });
  131. },
  132. /**
  133. * 切换计费方式
  134. */
  135. clickMethodEvent: function () {
  136. $('input:radio[name="delivery[method]"]').change(function (e) {
  137. var $first = $('.first')
  138. , $additional = $('.additional');
  139. if (e.currentTarget.value === '20')
  140. $first.text('首重 (Kg)') && $additional.text('续重 (Kg)');
  141. else
  142. $first.text('首件 (个)') && $additional.text('续件 (个)');
  143. });
  144. },
  145. };
  146. window.Delivery = Delivery;
  147. })(window);
  148. //******* 地区选择插件 *******//
  149. (function () {
  150. /***
  151. * 地区选择插件
  152. * @param container
  153. * @param datas
  154. * @constructor
  155. */
  156. function RegionalChoice(container, datas) {
  157. this.container = container;
  158. this.datas = datas;
  159. this.initInterface(); // 初始化地域界面
  160. }
  161. RegionalChoice.prototype = {
  162. /**
  163. * 条件渲染
  164. * @param alreadyIds 已存在的区域ID: 用于新增
  165. * @param checkedIds 已选中的区域ID: 用于编辑
  166. * @param
  167. */
  168. render: function (alreadyIds, checkedIds) {
  169. alreadyIds = alreadyIds || [];
  170. alreadyIds.length > 0 && this.setAlready(alreadyIds);
  171. checkedIds = checkedIds || [];
  172. checkedIds.length > 0 && this.setChecked(checkedIds);
  173. },
  174. /**
  175. * 初始化地域界面
  176. */
  177. initInterface: function () {
  178. var _this = this;
  179. $(_this.container).append(
  180. $('<div/>', {
  181. class: 'place-div'
  182. }).append(
  183. $('<div/>', {}).append(
  184. $('<div/>', {
  185. class: 'checkbtn'
  186. })
  187. .append(
  188. $('<label/>', {})
  189. // 全选框
  190. .append(
  191. $('<input/>', {
  192. type: 'checkbox',
  193. change: function () {
  194. var checked = $(this).is(':checked'),
  195. $allCheckbox = $('.place').find('input[type=checkbox]');
  196. $('.ratio').html('');
  197. $allCheckbox.prop('checked', checked);
  198. }
  199. })
  200. )
  201. .append(' 全国')
  202. )
  203. .append(
  204. $('<a/>', {
  205. class: 'clearCheck',
  206. text: '清空',
  207. click: function () {
  208. _this.destroy();
  209. }
  210. })
  211. )
  212. ).append(
  213. // 省份
  214. $('<div/>', {
  215. class: 'place clearfloat'
  216. }).append(function () {
  217. return _this.getSmallPlace();
  218. }())
  219. )
  220. )
  221. );
  222. },
  223. /**
  224. * 遍历省份
  225. * @returns {jQuery}
  226. * @constructor
  227. */
  228. getSmallPlace: function () {
  229. var _this = this;
  230. return $('<div/>', {
  231. class: 'smallplace clearfloat'
  232. }).append(
  233. $.map(_this.datas, function (item) {
  234. return $('<div/>', {
  235. class: 'place-tooltips'
  236. })
  237. .append(
  238. $('<label/>', {})
  239. .append(
  240. $('<input/>', {
  241. id: item.id,
  242. type: 'checkbox',
  243. class: 'province',
  244. change: function () {
  245. var $this = $(this)
  246. , small = $this.parent().next('.citys').find('input')
  247. , $placeTooltips = $this.parents('.place-tooltips');
  248. if ($this.prop('checked')) {
  249. small.prop('checked', true);
  250. $placeTooltips.find('.ratio').html('(' + small.length + '/' + small.length + ')');
  251. } else {
  252. small.prop('checked', false);
  253. $placeTooltips.find('.ratio').html('');
  254. }
  255. }
  256. })
  257. )
  258. .append(
  259. // 省份名称
  260. $('<span/>', {
  261. class: 'province_name',
  262. text: item.name
  263. })
  264. )
  265. .append(function () {
  266. // 城市数量
  267. if (item.city) {
  268. return $('<span/>', {
  269. class: 'ratio'
  270. })
  271. }
  272. })
  273. ).append(function () {
  274. // 城市列表
  275. if (item.city) {
  276. return $('<div/>', {
  277. class: 'citys'
  278. }).append(
  279. $('<i/>', {
  280. class: 'jt'
  281. }).append($('<i/>', {}))
  282. ).append(
  283. _this.getSmallCitys(item.city)
  284. )
  285. }
  286. })
  287. })
  288. )
  289. },
  290. /**
  291. * 遍历城市
  292. * @param datas
  293. * @returns {jQuery}
  294. * @constructor
  295. */
  296. getSmallCitys: function (datas) {
  297. return $('<div/>', {
  298. class: 'row-div clearfloat'
  299. }).append(
  300. $.map(datas, function (item) {
  301. return $('<p/>', {}).append(
  302. $('<label/>', {}).append(
  303. $('<input/>', {
  304. id: item.id,
  305. type: 'checkbox',
  306. name: 'city[]',
  307. class: 'city',
  308. change: function () {
  309. var $citys = $(this).parents('.citys')
  310. , $placeTooltips = $(this).parents('.place-tooltips')
  311. , tf = $citys.find('input:checked').length
  312. , $province = $placeTooltips.find('.province')
  313. , $ratio = $placeTooltips.find('.ratio');
  314. if (tf > 0) {
  315. $province.prop('checked', true);
  316. $ratio.html('(' + tf + '/' + $citys.find('input').length + ')');
  317. } else if (tf === 0) {
  318. $province.prop('checked', false);
  319. $ratio.html('');
  320. }
  321. }
  322. })
  323. ).append(
  324. $('<span/>', {
  325. text: item.name
  326. })
  327. )
  328. )
  329. })
  330. )
  331. },
  332. /**
  333. * 获取已选中的省市id
  334. * @returns {array}
  335. * @constructor
  336. */
  337. getCheckedIds: function () {
  338. var checkedIds = [];
  339. $('input[type=checkbox][name="city[]"]:checked').each(function (index, item) {
  340. checkedIds.push(item.id);
  341. });
  342. return checkedIds;
  343. },
  344. /**
  345. * 获取已选中的省市id (树状)
  346. * @returns {Array}
  347. */
  348. getCheckedTree: function () {
  349. var _this = this;
  350. // 遍历省份
  351. var data = [];
  352. $('input.province:checked').each(function (index, province) {
  353. var $this = $(this)
  354. , $citys = $this.parent().next()
  355. , $cityInputChecked = $citys.find('input.city:checked')
  356. , cityData = []
  357. , cityTotal = Object.keys(_this.datas[province.id].city).length;
  358. // 遍历城市
  359. if (cityTotal !== $cityInputChecked.length) {
  360. $cityInputChecked.each(function (index, item) {
  361. cityData.push({id: item.id, name: $(this).next().text()});
  362. });
  363. }
  364. data.push({
  365. id: province.id,
  366. name: $this.next().text(),
  367. city: cityData
  368. });
  369. });
  370. return data;
  371. },
  372. /**
  373. * 获取已选中地区内容
  374. * @returns {{content: string, checkedIds: *|Array}}
  375. */
  376. getCheckedContent: function () {
  377. // 获取已选中的省市id
  378. var dataTree = this.getCheckedTree()
  379. , checkedIds = this.getCheckedIds()
  380. , content = '';
  381. if (checkedIds.length === 373) {
  382. content = '全国';
  383. } else {
  384. var str = '';
  385. dataTree.forEach(function (item) {
  386. str += item.name;
  387. if (item.city.length > 0) {
  388. var cityStr = '';
  389. item.city.forEach(function (city) {
  390. cityStr += city.name + '、';
  391. });
  392. str += ' (<span class="am-link-muted">'
  393. + cityStr.substring(0, cityStr.length - 1) + '</span>)';
  394. }
  395. str += '、';
  396. });
  397. content = str.substring(0, str.length - 1);
  398. }
  399. return {
  400. content: content,
  401. ids: checkedIds
  402. };
  403. },
  404. /**
  405. * 批量选中
  406. * @param checkedIds 已选中的区域ID: 用于编辑
  407. * @constructor
  408. */
  409. setChecked: function (checkedIds) {
  410. var $place = $('.place-div');
  411. $.each(checkedIds, function (i, id) {
  412. $place.find('#' + id).trigger('click');
  413. });
  414. },
  415. /**
  416. * 批量删除已存在的区域
  417. * @param alreadyIds 已存在的区域ID: 用于新增
  418. * @constructor
  419. */
  420. setAlready: function (alreadyIds) {
  421. var $place = $('.place-div');
  422. $.each(alreadyIds, function (i, id) {
  423. var $p = $place.find('#' + id).parent().parent()
  424. , $siblings = $p.siblings();
  425. $siblings.length > 0 ? $p.remove() : $p.closest('.place-tooltips').remove();
  426. });
  427. },
  428. /**
  429. * 清空
  430. */
  431. destroy: function () {
  432. var $place = $('.place-div');
  433. $place.find('input[type=checkbox]').prop('checked', false);
  434. $place.find('.ratio').html('');
  435. }
  436. };
  437. window.RegionalChoice = RegionalChoice;
  438. })(window);