暫無描述

keyword.js 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /**
  2. * Created by TT on 2017/8/23.
  3. */
  4. ;
  5. spa_define(function () {
  6. return $.use(["spa", "util", "form"], function (spa, util, fb) {
  7. return {
  8. modal: function (data) {
  9. var root = spa.findInModal(".sys_paper_keyword");
  10. var ca = {ready: true, items: []};
  11. var form = fb.build(root.find(".newForm"), {
  12. keywordList: ca
  13. });
  14. var oValue;
  15. var oJudge;
  16. var saveBtn = root.find(".opt-save"),
  17. save = function () {
  18. if (form.val().keywordList) {
  19. form.val({
  20. keywords: oString(form.val().keywordList)
  21. });
  22. } else {
  23. form.val({keywords: null});
  24. }
  25. util.post("../ajax/paper/keywords", {
  26. id: data.data.id,
  27. keywords: form.val().keywords
  28. }, function () {
  29. spa.closeModal();
  30. if (data.hand) {
  31. data.hand();
  32. }
  33. }, {});
  34. };
  35. var trim = function (str) { //删除左右两端的空格   
  36. return str.replace(/(^\s*)|(\s*$)/g, "");
  37. };
  38. var part = function (one, list, num) {
  39. oValue = one;
  40. oJudge = list || [];
  41. if (oJudge.length >= num) {
  42. util.alert("最多" + num + "个");
  43. return;
  44. }
  45. var repeat,
  46. b;
  47. if (!oValue) {
  48. util.alert('提示', '请先填写内容');
  49. return;
  50. }
  51. if (oValue.length > 15) {
  52. util.alert('提示', '添加内容不能超过15个字');
  53. return;
  54. } else {
  55. var oValueList = oValue.split(","),
  56. length = oValueList.length;
  57. for (var m = 0;m<oValueList.length;m++) {
  58. oValueList[m] = trim(oValueList[m]);
  59. }
  60. for (var j = 0; j < length; j++) {
  61. for (var n = j + 1; n < oValueList.length + 1;) {
  62. if (oValueList[j] == oValueList[n]) {
  63. oValueList.remove(n);
  64. repeat = false;
  65. } else {
  66. n++;
  67. }
  68. }
  69. }
  70. for (var j = 0; j < oValueList.length;) {
  71. for (var i = 0; i < oJudge.length; i++) {
  72. if (oValueList[j] == oJudge[i]) {
  73. oValueList.remove(j);
  74. repeat = false;
  75. b = true;
  76. }
  77. }
  78. if (b) {
  79. b = false
  80. } else j++;
  81. }
  82. if (repeat == false) {
  83. util.alert('提示', '添加内容不能重复');
  84. }
  85. for (var m = 0; m < oValueList.length; m++) {
  86. ca.items.push({code: oValueList[m], caption: oValueList[m]});
  87. oJudge.push(oValueList[m]);
  88. }
  89. }
  90. };
  91. Array.prototype.remove = function (obj) {
  92. for (var i = 0; i < this.length; i++) {
  93. var temp = this[i];
  94. if (!isNaN(obj)) {
  95. temp = i;
  96. }
  97. if (temp == obj) {
  98. for (var j = i; j < this.length; j++) {
  99. this[j] = this[j + 1];
  100. }
  101. this.length = this.length - 1;
  102. }
  103. }
  104. };
  105. function oString(data) {
  106. var arry = new Array();
  107. if (data) {
  108. for (var i = 0; i < data.length; i++) {
  109. arry.push(data[i]);
  110. }
  111. }
  112. return arry.join(",");
  113. }
  114. var split = function (data) {
  115. var index = data.split(",");
  116. var arr = [];
  117. for (var m = 0; m < index.length; m++) {
  118. ca.items.push({code: index[m], caption: index[m]});
  119. arr.push(index[m]);
  120. }
  121. return arr;
  122. };
  123. if (data.data.keywords) {
  124. form.val({keywordList: split(data.data.keywords)});
  125. }
  126. root.find(".opt-keyword").on("click", function () {
  127. part(form.val().newKeyword, form.val().keywordList, 5);
  128. form.val({newKeyword: "", keywordList: oJudge});
  129. });
  130. root.find(".modal-ctrl .icon-times").on("click", function () {
  131. spa.closeModal();
  132. });
  133. saveBtn.on("click", save);
  134. }
  135. }
  136. });
  137. });