main2.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. import yzPage from './yzPage';
  2. import yzToolbar from './yzToolbar'
  3. import $ from 'jquery';
  4. let pageManager = function (data, cfg) {
  5. // 所有页面的集合
  6. this.pages = [];
  7. // 当前页面对象
  8. this.currentPage = null;
  9. // 头部信息
  10. this.header = {
  11. layoutType: data.layoutType,
  12. noBoth: data.noBoth,
  13. noCount: data.noCount,
  14. noMode: data.noMode,
  15. paperSize: data.paperSize,
  16. title: data.title,
  17. height: 120
  18. };
  19. // 页面唯一id,自增
  20. this.pageIndex = 0;
  21. // 最小高度为0,也就是客观题的高度
  22. this.minHeight = (cfg && cfg.minHeight) || 40;
  23. this.subjectInfo = {};
  24. // 初始化的试卷框的信息
  25. this.initData = this._initBorderData(data.scanPpStructQueModelList);
  26. // 所有dom的容器
  27. this.domId = ('lsiten_manager_'+Math.random()).replace('.','_');
  28. this.domBody = null;
  29. // 顶部工具栏
  30. this.toolBar = new yzToolbar();
  31. };
  32. // 初始化框数据,循环所有吧小题,判断是否在客观题的框里
  33. pageManager.prototype._initBorderData = function (data) {
  34. // 存储客观题区块index最大的index和该index下的小问个数
  35. let subjectBlock = {};
  36. let blockindex = 0;
  37. for(let index in data) {
  38. // 如果有inDetail,则是客观题
  39. let item = data[index];
  40. if (item.inDetail) {
  41. item.isSubject = false;
  42. let subjectData = item.inDetail.split('-');
  43. item.blockindex = subjectData[0];
  44. item.blocknum = subjectData[1];
  45. if(subjectBlock[item.rectNo]) {
  46. let block = subjectBlock[item.rectNo][blockindex];
  47. if (block.index !== item.blockindex) {
  48. if (item.blockindex % 3 === 1) {
  49. blockindex++;
  50. subjectBlock[item.rectNo][blockindex] = {
  51. index: item.blockindex,
  52. maxnum: item.blocknum,
  53. minHeight: this.minHeight
  54. }
  55. } else {
  56. block.index = item.blockindex;
  57. block.maxnum = block.maxnum > item.blocknum ? block.maxnum: item.blocknum;
  58. }
  59. }
  60. } else {
  61. // 初始化block信息
  62. blockindex = 0;
  63. subjectBlock[item.rectNo] = {};
  64. subjectBlock[item.rectNo][blockindex] = {
  65. index: item.blockindex,
  66. maxnum: item.blocknum,
  67. minHeight: this.minHeight
  68. }
  69. }
  70. } else {
  71. item.isSubject = true;
  72. }
  73. }
  74. let subjectBlockInfo = this.subjectInfo;
  75. for (let key in subjectBlock) {
  76. if (!subjectBlockInfo[key]) {
  77. subjectBlockInfo[key] = {
  78. height: 0,
  79. rowDetail: JSON.parse(JSON.stringify(subjectBlock[key]))
  80. };
  81. for (let bkey in subjectBlock[key]) {
  82. subjectBlockInfo[key].height += (subjectBlock[key][bkey].maxnum * this.minHeight);
  83. subjectBlockInfo[key].index = subjectBlock[key][bkey].index;
  84. }
  85. }
  86. }
  87. return data;
  88. }
  89. pageManager.prototype._addPage = function () {
  90. this.pageIndex++;
  91. let pageOptions = {
  92. hasHeader: false,
  93. maxHeight: 700
  94. };
  95. // 判读该页面是否有头部框,如果noBoth为1
  96. if (this.header.layoutType) {
  97. pageOptions.hasHeader = parseInt(this.header.noBoth) === 1 ? this.pageIndex % parseInt(this.header.layoutType) === 1 : this.pageIndex === 1;
  98. } else {
  99. throw 'this Manager does not initiailize the data';
  100. }
  101. pageOptions.hasHeader && (pageOptions.header = this.header);
  102. let newPage = new yzPage(pageOptions);
  103. this.pages.push(newPage);
  104. this.currentPage = newPage;
  105. }
  106. /**
  107. * function 数据转换
  108. * author: lsiten
  109. * date: 2018-4-8
  110. */
  111. pageManager.prototype.transformData = function () {
  112. // 第一步,以小题为唯一框合并数据
  113. let mergeData = this._mergeData();
  114. // 第二步,以page高为临界条件拆分框
  115. this._splitToPage(mergeData);
  116. }
  117. /**
  118. * funtion 合并数据
  119. * 注意,数据跨栏不能间隔
  120. */
  121. pageManager.prototype._mergeData = function () {
  122. let data = this.initData;
  123. let length = data.length;
  124. let i = 1; // 计数
  125. let _height = -1;
  126. let _lastId = -1;
  127. let item = null;
  128. let mergeArr = [];
  129. let itemTemp = {
  130. height: -1,
  131. rectNo: -1,
  132. isSubject: true,
  133. cdelete: 0,
  134. minHeight: -1,
  135. content: null
  136. };
  137. for (let index in data) {
  138. item = data[index];
  139. item.minHeight = parseInt(item.minHeight) > 0 ? parseInt(item.minHeight): this.minHeight;
  140. if(-1 == itemTemp.rectNo) { //初始化
  141. itemTemp.rectNo = item.rectNo;
  142. if(!item.isSubject) {
  143. // 客观题泽重置为最小高度
  144. item.minHeight = this.minHeight;
  145. this.subjectInfo[item.rectNo] && (itemTemp.height = this.subjectInfo[item.rectNo].height, itemTemp.rowDetail = this.subjectInfo[item.rectNo].rowDetail);
  146. } else {
  147. itemTemp.height = item.minHeight;
  148. }
  149. itemTemp.border = item.border;
  150. itemTemp.isSubject = item.isSubject;
  151. itemTemp.questions = [item];
  152. } else {
  153. if(itemTemp.rectNo === item.rectNo) {
  154. if (item.isSubject) {
  155. itemTemp.height += item.minHeight;
  156. // 如果有一个小题不是客观题,则该框就不是客观题框
  157. itemTemp.isSubject = true;
  158. delete itemTemp.rowDetail;
  159. itemTemp.questions.push(item);
  160. } else {
  161. // 客观题泽重置为最小高度
  162. item.minHeight = this.minHeight;
  163. itemTemp.questions.push(item);
  164. }
  165. } else {
  166. mergeArr.push(JSON.parse(JSON.stringify(itemTemp)));
  167. if(!item.isSubject) {
  168. // 客观题泽重置为最小高度
  169. item.minHeight = this.minHeight;
  170. this.subjectInfo[item.rectNo] && (itemTemp.height = this.subjectInfo[item.rectNo].height, itemTemp.rowDetail = this.subjectInfo[item.rectNo].rowDetail)
  171. } else {
  172. itemTemp.height = item.minHeight;
  173. }
  174. itemTemp.rectNo = item.rectNo;
  175. itemTemp.border = item.border;
  176. itemTemp.isSubject = item.isSubject;
  177. itemTemp.questions = [item];
  178. }
  179. if(i === (length-1)) { //保存最后一个
  180. mergeArr.push(JSON.parse(JSON.stringify(itemTemp)));
  181. }
  182. // 计数器
  183. i++;
  184. }
  185. }
  186. return mergeArr;
  187. }
  188. /**
  189. * function 根据page的临界条件去
  190. * @param mData [array] 合并后的数据
  191. */
  192. pageManager.prototype._splitToPage = function (mData) {
  193. for (let i =0; i < mData.length; i++) {
  194. this._itemToPage(mData[i], 0);
  195. }
  196. }
  197. /**
  198. * function 根据page的临界条件去
  199. * @param item [object] 合并后的数据
  200. */
  201. pageManager.prototype._itemToPage = function (item) {
  202. !this.currentPage && this._addPage();
  203. let domOption = JSON.parse(JSON.stringify(item));
  204. if ( item.height > this.currentPage.gap) {
  205. let gap = this.currentPage.gap;
  206. if (gap > 0) {
  207. let holdHeight = gap;
  208. //1、 cHoldQuestions 当前页可以容纳的小题数组
  209. //2、 questionsTemp 当前页容纳不了的小题数组
  210. let cHoldQuestions = [];
  211. let questionsTemp = [];
  212. // 下一页的高度
  213. let nextBoxHeight = 0;
  214. item.questions.filter(domItem => {
  215. gap -= domItem.minHeight;
  216. if (gap >= 0) {
  217. cHoldQuestions.push(domItem);
  218. } else {
  219. // 如果该页容纳不了该小问,则该小问在下一页显示
  220. nextBoxHeight += domItem.minHeight;
  221. questionsTemp.push(domItem);
  222. }
  223. })
  224. item.questions = questionsTemp;
  225. domOption.height = holdHeight;
  226. domOption.questions = cHoldQuestions;
  227. if (domOption.questions.length > 0) {
  228. item.height -= domOption.height;
  229. item.height < nextBoxHeight && (item.height = nextBoxHeight);
  230. this.currentPage.addDom(domOption);
  231. }
  232. if (item.height > this.minHeight) {
  233. domOption.isDrag = false;
  234. } else {
  235. domOption.isDrag = true;
  236. }
  237. // 如果有最小高度,则说明是刷新状态
  238. if(item.minHeight) {
  239. item.minHeight -= domOption.height;
  240. item.minHeight = item.minHeight <= 0 ? 0 : item.minHeight;
  241. }
  242. this.currentPage.gap = 0;
  243. this.currentPage = null;
  244. if (item.height > this.minHeight) {
  245. // 如果有最小高度,则说明是刷新状态
  246. if(item.minHeight) {
  247. item.minHeight <= 0 && (item.cdelete = 1);
  248. }
  249. this._itemToPage(item);
  250. }
  251. } else {
  252. this.currentPage.gap = 0;
  253. this.currentPage = null;
  254. this._itemToPage(item);
  255. }
  256. } else {
  257. // 如果有最小高度,则说明是刷新状态
  258. if(item.minHeight) {
  259. item.minHeight -= item.height;
  260. item.minHeight = item.minHeight <= 0 ? 0 : item.minHeight;
  261. }
  262. domOption.isDrag = true;
  263. this.currentPage.addDom(domOption);
  264. item.height = 0;
  265. this.currentPage.gap === 0 && (this.currentPage = null);
  266. }
  267. };
  268. /**
  269. * function 将数据转换为vdom的形式
  270. */
  271. pageManager.prototype.renderToDom = function () {
  272. let pages = this.pages;
  273. this.domBody = $('<div id="' +this.domId+ '" class="manager-selection"></div>');
  274. for (let index in pages) {
  275. pages[index].renderToDom();
  276. this.domBody.append(pages[index].vDom);
  277. }
  278. console.log(this.pages);
  279. $("#pages-box").replaceWith(this.domBody);
  280. $('body').on('refreshPage', () => {
  281. this._refreshPage();
  282. });
  283. this.refreshToolBar();
  284. $('body').on('getQuestion', (e,pageNum,domId) => {
  285. this.refreshToolBar(...[pageNum,domId]);
  286. });
  287. };
  288. /**
  289. * function 刷新页面
  290. */
  291. pageManager.prototype._refreshPage = function () {
  292. let data = [];
  293. let pages = this.pages;
  294. let nowRectNo = -1;
  295. let length = 0;
  296. for (let i in pages) {
  297. let doms = pages[i].doms;
  298. for (let j in doms) {
  299. let item = {
  300. border: doms[j].border,
  301. height: doms[j].height,
  302. isSubject: doms[j].isSubject,
  303. questions: doms[j].questions,
  304. rectNo: doms[j].rectNo,
  305. minHeight: doms[j].minHeight,
  306. content: doms[j].content
  307. };
  308. if(parseInt(item.rectNo) === nowRectNo) {
  309. data[(length-1)].questions.push.apply(data[(length-1)].questions, item.questions);
  310. data[(length-1)].height += item.height;
  311. } else {
  312. nowRectNo = parseInt(item.rectNo);
  313. length++;
  314. !doms[j].isSubject && (item.rowDetail = doms[j].rowDetail);
  315. data.push(item);
  316. }
  317. }
  318. }
  319. this.pageIndex = 0;
  320. this.pages = [];
  321. this.currentPage = null;
  322. this._splitToPage(data);
  323. $(".manager-selection").replaceWith('<div id="pages-box"></div>');
  324. $('body').off('refreshPage');
  325. this.renderToDom();
  326. };
  327. /*
  328. * function 刷新顶部菜单栏
  329. */
  330. pageManager.prototype.refreshToolBar = function(pNum = null,id) {
  331. // 如果有参数说明要编辑题目
  332. if(pNum){
  333. this.toolBar.setSubjectTitle(pNum);
  334. $('#toolbar').remove();
  335. }
  336. this.domBody.append(this.toolBar.toolbarContent());
  337. };
  338. export default pageManager