Нет описания

edit.js 20KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  1. /**
  2. * Created by TT on 2017/10/23.
  3. */
  4. ;
  5. spa_define(function () {
  6. return $.use(["spa", "util", "form", "upload"], function (spa, util, fb, upload) {
  7. return {
  8. modal: function (data) {
  9. var root = spa.findInModal(".sys_result_new");
  10. var ca = {ready: true, items: []},
  11. ba = {ready: true, items: []};
  12. var form = fb.build(root.find(".newForm"), {
  13. industryList: ca,
  14. subjectList: ca,
  15. researcherList: ba,
  16. researcher: {
  17. keyPressInterval: 1,
  18. hide: function () {
  19. this.shown = false;
  20. this.ctn.removeClass("open");
  21. this.lastQuerying = null;
  22. this.lastQueryed = null;
  23. },
  24. select: function () {
  25. if (this.shown) {
  26. var item = this.menu.find(".active");
  27. this.rv = item.attr('code');
  28. this.caption = item.attr("caption");
  29. var caption = this.caption.toString();
  30. var name = caption.substr(0, caption.indexOf(","));
  31. this.ve.val("");
  32. var res = this.rv + "," + name;
  33. part2(name, form.val().researcherList, res, 20);
  34. form.val({researcherList: oJudge});
  35. return this.hide();
  36. }
  37. }
  38. },
  39. orgName: {
  40. keyPressInterval: 1,
  41. hide: function () {
  42. this.shown = false;
  43. this.ctn.removeClass("open");
  44. this.lastQuerying = null;
  45. this.lastQueryed = null;
  46. }
  47. },
  48. newSubject: {
  49. keyPressInterval: 1,
  50. hide: function () {
  51. this.shown = false;
  52. this.ctn.removeClass("open");
  53. this.lastQuerying = null;
  54. this.lastQueryed = null;
  55. },
  56. select: function () {
  57. if (this.shown) {
  58. var item = this.menu.find(".active");
  59. this.rv = item.attr('code');
  60. this.caption = item.attr("caption");
  61. var caption = this.caption.toString();
  62. this.ve.val("");
  63. part(caption, form.val().subjectList, 20);
  64. form.val({subjectList: oJudge});
  65. return this.hide();
  66. }
  67. }
  68. },
  69. newIndustry: {
  70. keyPressInterval: 1,
  71. hide: function () {
  72. this.shown = false;
  73. this.ctn.removeClass("open");
  74. this.lastQuerying = null;
  75. this.lastQueryed = null;
  76. },
  77. select: function () {
  78. if (this.shown) {
  79. var item = this.menu.find(".active");
  80. this.rv = item.attr('code');
  81. this.caption = item.attr("caption");
  82. var caption = this.caption.toString();
  83. this.ve.val("");
  84. part(caption, form.val().industryList, 20);
  85. form.val({industryList: oJudge});
  86. return this.hide();
  87. }
  88. }
  89. }
  90. });
  91. var oValue;
  92. var oJudge;
  93. var trim = function (str) {
  94. return str.replace(/(^\s*)|(\s*$)/g, "");
  95. };
  96. var saveBtn = root.find(".opt-save"),
  97. headArea = root.find(".head-ctn"),
  98. save = function () {
  99. // console.log($(".org").find("input").val());
  100. var pic = [];
  101. $('#fileList').find('img').each(function () {
  102. pic.push($(this).attr("name"));
  103. });
  104. form.val({pic: oString(pic), researchers: form.val().researcherList});
  105. form.val({
  106. industry: oString(form.val().industryList),
  107. subject: oString(form.val().subjectList),
  108. orgName: $(".org").find("input").val()
  109. });
  110. if (form.val().name == null) {
  111. util.alert("请输入成果名称");
  112. return;
  113. }
  114. else form.doPost("../ajax/resResult/update", function () {
  115. spa.closeModal();
  116. if (data.hand) {
  117. data.hand();
  118. }
  119. // location.reload();
  120. }, {});
  121. };
  122. util.get("../ajax/resResult/queryResearcher", {id: data.data.id}, function (res) {
  123. if (res) {
  124. var arr = [];
  125. for (var m = 0; m < res.length; m++) {
  126. var caption = res[m].name;
  127. var code = res[m].professorId + "," + res[m].name;
  128. ba.items.push({code: code, caption: caption});
  129. arr.push(code);
  130. }
  131. form.val({researcherList: arr});
  132. }
  133. });
  134. form.val(data.data);
  135. $(".org").find("input").val(data.data.orgName);
  136. if (data.data.industry) {
  137. form.val({industryList: split1(data.data.industry)});
  138. }
  139. if (data.data.subject) {
  140. form.val({subjectList: split1(data.data.subject)});
  141. }
  142. var part = function (one, list, num) {
  143. oValue = one;
  144. oJudge = list || [];
  145. if (oJudge.length >= num) {
  146. util.alert("最多" + num + "个");
  147. return;
  148. }
  149. var repeat,
  150. b;
  151. if (!oValue) {
  152. util.alert('提示', '请先填写内容');
  153. return;
  154. }
  155. if (oValue.length > 15) {
  156. util.alert('提示', '添加内容不能超过15个字');
  157. return;
  158. } else {
  159. var oValueList = oValue.split(","),
  160. length = oValueList.length;
  161. for (var j = 0; j < length; j++) {
  162. for (var n = j + 1; n < oValueList.length + 1;) {
  163. if (oValueList[j] == oValueList[n]) {
  164. oValueList.remove(n);
  165. repeat = false;
  166. } else {
  167. n++;
  168. }
  169. }
  170. }
  171. for (var j = 0; j < oValueList.length;) {
  172. for (var i = 0; i < oJudge.length; i++) {
  173. if (oValueList[j] == oJudge[i]) {
  174. oValueList.remove(j);
  175. repeat = false;
  176. b = true;
  177. }
  178. }
  179. if (b) {
  180. b = false
  181. } else j++;
  182. }
  183. if (repeat == false) {
  184. util.alert('提示', '添加内容不能重复');
  185. }
  186. for (var m = 0; m < oValueList.length; m++) {
  187. ca.items.push({code: oValueList[m], caption: oValueList[m]});
  188. oJudge.push(oValueList[m]);
  189. }
  190. }
  191. };
  192. var part2 = function (one, list, value, num) {
  193. oValue = one;
  194. oJudge = list || [];
  195. if (oJudge.length >= num) {
  196. util.alert("最多" + num + "个");
  197. return;
  198. }
  199. // var repeat,
  200. // b;
  201. if (!oValue) {
  202. util.alert('提示', '请先填写内容');
  203. return;
  204. }
  205. if (oValue.length > 50) {
  206. util.alert('提示', '添加内容不能超过50个字');
  207. return;
  208. } else {
  209. // var oValueList = oValue.split(","),
  210. // length = oValueList.length;
  211. // for (var j = 0; j < length; j++) {
  212. // for (var n = j + 1; n < oValueList.length + 1;) {
  213. // if (oValueList[j] == oValueList[n]) {
  214. // oValueList.remove(n);
  215. // repeat = false;
  216. // } else {
  217. // n++;
  218. // }
  219. // }
  220. // }
  221. // for (var j = 0; j < oValueList.length;) {
  222. // for (var i = 0; i < oJudge.length; i++) {
  223. // if (oValueList[j] == oJudge[i]) {
  224. // oValueList.remove(j);
  225. // repeat = false;
  226. // b = true;
  227. // }
  228. // }
  229. // if (b) {
  230. // b = false
  231. // } else j++;
  232. // }
  233. // if (repeat == false) {
  234. // util.alert('提示', '添加内容不能重复');
  235. // }
  236. // for (var m = 0; m < oValueList.length; m++) {
  237. ba.items.push({code: value, caption: oValue});
  238. oJudge.push(value);
  239. // }
  240. }
  241. };
  242. Array.prototype.remove = function (obj) {
  243. for (var i = 0; i < this.length; i++) {
  244. var temp = this[i];
  245. if (!isNaN(obj)) {
  246. temp = i;
  247. }
  248. if (temp == obj) {
  249. for (var j = i; j < this.length; j++) {
  250. this[j] = this[j + 1];
  251. }
  252. this.length = this.length - 1;
  253. }
  254. }
  255. };
  256. function split1(data) {
  257. var index = data.split(",");
  258. var arr = [];
  259. for (var m = 0; m < index.length; m++) {
  260. ca.items.push({code: index[m], caption: index[m]});
  261. arr.push(index[m]);
  262. }
  263. return arr;
  264. }
  265. function oString(data) {
  266. var arry = new Array();
  267. if (data) {
  268. for (var i = 0; i < data.length; i++) {
  269. arry.push(data[i]);
  270. }
  271. }
  272. return arry.join(",");
  273. }
  274. root.find(".modal-ctrl .icon-times").on("click", function () {
  275. spa.closeModal();
  276. });
  277. root.find(".opt-industry").on("click", function () {
  278. var ind = $(".ind").find("input");
  279. part(trim(ind.val()), form.val().industryList, 20);
  280. ind.val("");
  281. form.val({newIndustry: "", industryList: oJudge});
  282. });
  283. root.find(".opt-subject").on("click", function () {
  284. var sub = $(".sub").find("input");
  285. part(trim(sub.val()), form.val().subjectList, 20);
  286. sub.val("");
  287. form.val({newSubject: "", subjectList: oJudge});
  288. });
  289. root.find(".opt-res").on("click", function () {
  290. var res = $(".typeahead").find("input");
  291. var value = "################################," + trim(res.val());
  292. part2(trim(res.val()), form.val().researcherList, value, 20);
  293. res.val("");
  294. form.val({researcherList: oJudge});
  295. });
  296. var $list = $('#fileList'),
  297. ratio = window.devicePixelRatio || 1,
  298. thumbnailWidth = 100 * ratio,
  299. thumbnailHeight = 100 * ratio,
  300. uploader;
  301. uploader = WebUploader.create({
  302. auto: true,
  303. server: '../ajax/cachedFileUpload',
  304. pick: '#filePicker',
  305. duplicate: true,
  306. // fileNumLimit:5,
  307. // fileSingleSizeLimit: 2*1024*1024,
  308. accept: {
  309. title: 'Images',
  310. extensions: 'gif,jpg,jpeg,bmp,png',
  311. mimeTypes: 'image/jpg,image/jpeg,image/png'
  312. }
  313. });
  314. uploader.on('fileQueued', function (file) {
  315. if ($(".file-item").length < 5) {
  316. var $li = $(
  317. '<div id="' + file.id + '" class="file-item thumbnail">' +
  318. '<img>' +
  319. '</div>'
  320. ),
  321. $img = $li.find('img');
  322. $list.append($li);
  323. if ($('#fileList').find('img').length > 4) {
  324. $('#filePicker').hide();
  325. }
  326. }
  327. });
  328. uploader.on('uploadSuccess', function (file, data) {
  329. $('#' + file.id).addClass('upload-state-done');
  330. $.ajax({
  331. type: "post",
  332. url: "../ajax/image/researchResult",
  333. async: false,
  334. data: {fn: data.data[0].cacheKey},
  335. contentType: "application/x-www-form-urlencoded"
  336. }).done(function (rd) {
  337. if (rd.success) {
  338. $('#' + file.id).find('img').attr('name', rd.data);
  339. }
  340. });
  341. uploader.makeThumb(file, function (error, src) {
  342. var $img = $("#" + file.id).find('img');
  343. if (error) {
  344. $img.replaceWith('<span>不能预览</span>');
  345. return;
  346. }
  347. $img.attr('src', src);
  348. }, thumbnailWidth, thumbnailHeight);
  349. if ($('#fileList').find('img').length < 5) {
  350. $('#filePicker').show();
  351. }
  352. if (uploader.getStats().progressNum == 0 || uploader.getStats().queueNum == 0) {
  353. saveBtn.on("click", save);
  354. }
  355. });
  356. uploader.on('uploadComplete', function (file) {
  357. var $li = $('#' + file.id);
  358. $li.find('.progress').remove();
  359. $('<div class="file-panel">' +
  360. '<span class="cancel icon-remove-sign">删除</span>' +
  361. '</div>').appendTo($li);
  362. });
  363. $list.on("click", ".cancel", function () {
  364. $(this).parent().parent().remove();
  365. if ($('#fileList').find('img').length < 5) {
  366. $('#filePicker').show();
  367. }
  368. uploader.refresh();
  369. });
  370. uploader.on('uploadProgress', function (file, percentage) {
  371. var $li = $('#' + file.id),
  372. $percent = $li.find('.progress span');
  373. // 避免重复创建
  374. if (!$percent.length) {
  375. $percent = $('<p class="progress"><span></span></p>')
  376. .appendTo($li)
  377. .find('span');
  378. }
  379. $percent.css('width', percentage * 100 + '%');
  380. $('#filePicker').hide();
  381. saveBtn.unbind("click");
  382. });
  383. uploader.on('error', function (type) {
  384. switch (type) {
  385. case 'Q_EXCEED_NUM_LIMIT':
  386. alert("错误:上传文件数量过多!");
  387. break;
  388. case 'Q_EXCEED_SIZE_LIMIT':
  389. alert("错误:文件总大小超出限制!");
  390. break;
  391. case 'F_EXCEED_SIZE':
  392. alert("错误:文件大小超出限制!");
  393. break;
  394. case 'Q_TYPE_DENIED':
  395. alert("错误:禁止上传该类型文件!");
  396. break;
  397. default:
  398. alert('错误代码:' + type);
  399. break;
  400. }
  401. });
  402. if (data.data.pic) {
  403. var image = split1(data.data.pic);
  404. for (var i = 0; i < image.length; i++) {
  405. var $li = $(
  406. '<div class="file-item thumbnail">' +
  407. '<img src="http://www.ekexiu.com/data/researchResult/" id="oimg" />' +
  408. '</div>'
  409. ),
  410. $btn = $('<div class="file-panel">' +
  411. '<span class="cancel icon-remove-sign">删除</span>' +
  412. '</div>').appendTo($li),
  413. $img = $li.find('img');
  414. $list.append($li);
  415. $img.attr('src', "http://www.ekexiu.com/data/researchResult/" + data.data.id + ".jpg");
  416. // $img.attr('src', "../data/images/researchResult/" + image[i]);
  417. $img.attr('name', image[i]);
  418. }
  419. if ($('#fileList').find('img').length > 4) {
  420. $('#filePicker').hide();
  421. }
  422. }
  423. saveBtn.on("click", save);
  424. }
  425. }
  426. });
  427. });