iosSelect.js 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092
  1. /**
  2. * IosSelect
  3. * @param {number} level 选择的层级 1 2 3 4 5 最多支持5层
  4. * @param {...Array} data [oneLevelArray[, twoLevelArray[, threeLevelArray]]]
  5. * @param {Object} options
  6. * @param {string=} options.container 组件插入到该元素下 可选
  7. * @param {Function} options.callback 选择完毕后的回调函数
  8. * @param {string=} options.title 选择框title
  9. * @param {number=} options.itemHeight 每一项的高度,默认 35
  10. * @param {number=} options.itemShowCount 组件展示的项数,默认 7,可选3,5,7,9,不过不是3,5,7,9则展示7项
  11. * @param {number=} options.headerHeight 组件标题栏高度 默认 44
  12. * @param {css=} options.cssUnit px或者rem 默认是px
  13. * @param {string=} options.addClassName 组件额外类名 用于自定义样式
  14. * @param {...Array=} options.relation 数组 [oneTwoRelation, twoThreeRelation, threeFourRelation, fourFiveRelation] 默认值:[0, 0, 0, 0]
  15. * @param {number=} options.relation.oneTwoRelation 第一列和第二列是否通过parentId关联
  16. * @param {number=} options.relation.twoThreeRelation 第二列和第三列是否通过parentId关联
  17. * @param {number=} options.relation.threeFourRelation 第三列和第四列是否通过parentId关联
  18. * @param {number=} options.relation.fourFiveRelation 第四列和第五列是否通过parentId关联
  19. * @param {string=} options.oneLevelId 第一级选中id
  20. * @param {string=} options.twoLevelId 第二级选中id
  21. * @param {string=} options.threeLevelId 第三级选中id
  22. * @param {string=} options.fourLevelId 第四级选中id
  23. * @param {string=} options.fiveLevelId 第五级选中id
  24. * @param {boolean=} options.showLoading 如果你的数据是异步加载的,可以使用该参数设置为true,下拉菜单会有加载中的效果
  25. */
  26. (function() {
  27. iosSelectUtil = {
  28. isArray: function(arg1) {
  29. return Object.prototype.toString.call(arg1) === '[object Array]';
  30. },
  31. isFunction: function(arg1) {
  32. return typeof arg1 === 'function';
  33. },
  34. attrToData: function(dom, index) {
  35. var obj = {};
  36. for (var p in dom.dataset) {
  37. obj[p] = dom.dataset[p];
  38. }
  39. obj['dom'] = dom;
  40. obj['atindex'] = index;
  41. return obj;
  42. },
  43. attrToHtml: function(obj) {
  44. var html = '';
  45. for (var p in obj) {
  46. html += 'data-' + p + '="' + obj[p] + '"';
  47. }
  48. return html;
  49. }
  50. };
  51. // Layer
  52. function Layer(html, opts) {
  53. if (!(this instanceof Layer)) {
  54. return new Layer(html, opts);
  55. }
  56. this.html = html;
  57. this.opts = opts;
  58. var el = document.createElement('div');
  59. el.className = 'olay';
  60. // var layer_el = $('<div class="layer"></div>');
  61. var layer_el = document.createElement('div');
  62. layer_el.className = 'layer';
  63. this.el = el;
  64. this.layer_el = layer_el;
  65. this.init();
  66. }
  67. Layer.prototype = {
  68. init: function() {
  69. this.layer_el.innerHTML = this.html;
  70. if (this.opts.container && document.querySelector(this.opts.container)) {
  71. document.querySelector(this.opts.container).appendChild(this.el);
  72. }
  73. else {
  74. document.body.appendChild(this.el);
  75. }
  76. this.el.appendChild(this.layer_el);
  77. this.el.style.height = Math.max(document.documentElement.getBoundingClientRect().height, window.innerHeight);
  78. if (this.opts.className) {
  79. this.el.className += ' ' + this.opts.className;
  80. }
  81. this.bindEvent();
  82. },
  83. bindEvent: function() {
  84. var sureDom = this.el.querySelectorAll('.sure');
  85. var closeDom = this.el.querySelectorAll('.close');
  86. var self = this;
  87. for (var i = 0, len = sureDom.length; i < len; i++) {
  88. sureDom[i].addEventListener('click', function(e) {
  89. self.close();
  90. });
  91. }
  92. for (var i = 0, len = closeDom.length; i < len; i++) {
  93. closeDom[i].addEventListener('click', function(e) {
  94. self.close();
  95. });
  96. }
  97. },
  98. close: function() {
  99. if (this.el) {
  100. this.el.parentNode.removeChild(this.el);
  101. this.el = null;
  102. }
  103. }
  104. }
  105. function IosSelect(level, data, options) {
  106. if (!iosSelectUtil.isArray(data) || data.length === 0) {
  107. return;
  108. }
  109. this.data = data;
  110. this.level = level || 1;
  111. this.options = options;
  112. this.typeBox = 'one-level-box';
  113. if (this.level === 1) {
  114. this.typeBox = 'one-level-box';
  115. }
  116. else if (this.level === 2) {
  117. this.typeBox = 'two-level-box';
  118. }
  119. else if (this.level === 3) {
  120. this.typeBox = 'three-level-box';
  121. }
  122. else if (this.level === 4) {
  123. this.typeBox = 'four-level-box';
  124. }
  125. else if (this.level === 5) {
  126. this.typeBox = 'five-level-box';
  127. }
  128. this.callback = options.callback;
  129. this.title = options.title || '';
  130. this.options.itemHeight = options.itemHeight || 35;
  131. this.options.itemShowCount = [3, 5, 7, 9].indexOf(options.itemShowCount) !== -1? options.itemShowCount: 7;
  132. this.options.coverArea1Top = Math.floor(this.options.itemShowCount / 2);
  133. this.options.coverArea2Top = Math.ceil(this.options.itemShowCount / 2);
  134. this.options.headerHeight = options.headerHeight || 44;
  135. this.options.relation = iosSelectUtil.isArray(this.options.relation)? this.options.relation: [];
  136. this.options.oneTwoRelation = this.options.relation[0];
  137. this.options.twoThreeRelation = this.options.relation[1];
  138. this.options.threeFourRelation = this.options.relation[2];
  139. this.options.fourFiveRelation = this.options.relation[3];
  140. if (this.options.cssUnit !== 'px' && this.options.cssUnit !== 'rem') {
  141. this.options.cssUnit = 'px';
  142. }
  143. this.setBase();
  144. this.init();
  145. };
  146. IosSelect.prototype = {
  147. init: function() {
  148. this.initLayer();
  149. // 选中元素的信息
  150. this.selectOneObj = {};
  151. this.selectTwoObj = {};
  152. this.selectThreeObj = {};
  153. this.selectFourObj = {};
  154. this.selectFiveObj = {};
  155. this.setOneLevel(this.options.oneLevelId, this.options.twoLevelId, this.options.threeLevelId, this.options.fourLevelId, this.options.fiveLevelId);
  156. },
  157. initLayer: function() {
  158. var self = this;
  159. var all_html = [
  160. '<header style="height: ' + this.options.headerHeight + this.options.cssUnit + '; line-height: ' + this.options.headerHeight + this.options.cssUnit + '" class="iosselect-header">',
  161. '<h2 id="iosSelectTitle"></h2>',
  162. '<a style="height: ' + this.options.headerHeight + this.options.cssUnit + '; line-height: ' + this.options.headerHeight + this.options.cssUnit + '" href="javascript:void(0)" class="close">取消</a>',
  163. '<a style="height: ' + this.options.headerHeight + this.options.cssUnit + '; line-height: ' + this.options.headerHeight + this.options.cssUnit + '" href="javascript:void(0)" class="sure">确定</a>',
  164. '</header>',
  165. '<section class="iosselect-box">',
  166. '<div class="one-level-contain" id="oneLevelContain">',
  167. '<ul class="select-one-level">',
  168. '</ul>',
  169. '</div>',
  170. '<div class="two-level-contain" id="twoLevelContain">',
  171. '<ul class="select-two-level">',
  172. '</ul>',
  173. '</div>',
  174. '<div class="three-level-contain" id="threeLevelContain">',
  175. '<ul class="select-three-level">',
  176. '</ul>',
  177. '</div>',
  178. '<div class="four-level-contain" id="fourLevelContain">',
  179. '<ul class="select-four-level">',
  180. '</ul>',
  181. '</div>',
  182. '<div class="five-level-contain" id="fiveLevelContain">',
  183. '<ul class="select-five-level">',
  184. '</ul>',
  185. '</div>',
  186. '</section>',
  187. '<hr class="cover-area1"/>',
  188. '<hr class="cover-area2"/>',
  189. // '<div class="time-fuhao">-</div>',
  190. '<div class="ios-select-loading-box" id="iosSelectLoadingBox">',
  191. '<div class="ios-select-loading"></div>',
  192. '</div>'
  193. ].join('\r\n');
  194. this.iosSelectLayer = new Layer(all_html, {
  195. className: 'ios-select-widget-box ' + this.typeBox + (this.options.addClassName? ' ' + this.options.addClassName: ''),
  196. container: this.options.container || ''
  197. });
  198. this.iosSelectTitleDom = this.iosSelectLayer.el.querySelector('#iosSelectTitle');
  199. this.iosSelectLoadingBoxDom = this.iosSelectLayer.el.querySelector('#iosSelectLoadingBox');
  200. if (this.options.title) {
  201. this.iosSelectTitleDom.innerHTML = this.options.title;
  202. }
  203. if (this.options.headerHeight && this.options.itemHeight) {
  204. this.coverArea1Dom = this.iosSelectLayer.el.querySelector('.cover-area1');
  205. this.coverArea1Dom.style.top = this.options.headerHeight + this.options.itemHeight * this.options.coverArea1Top + this.options.cssUnit;
  206. this.coverArea2Dom = this.iosSelectLayer.el.querySelector('.cover-area2');
  207. this.coverArea2Dom.style.top = this.options.headerHeight + this.options.itemHeight * this.options.coverArea2Top + this.options.cssUnit;
  208. }
  209. this.oneLevelContainDom = this.iosSelectLayer.el.querySelector('#oneLevelContain');
  210. this.twoLevelContainDom = this.iosSelectLayer.el.querySelector('#twoLevelContain');
  211. this.threeLevelContainDom = this.iosSelectLayer.el.querySelector('#threeLevelContain');
  212. this.fourLevelContainDom = this.iosSelectLayer.el.querySelector('#fourLevelContain');
  213. this.fiveLevelContainDom = this.iosSelectLayer.el.querySelector('#fiveLevelContain');
  214. this.oneLevelUlContainDom = this.iosSelectLayer.el.querySelector('.select-one-level');
  215. this.twoLevelUlContainDom = this.iosSelectLayer.el.querySelector('.select-two-level');
  216. this.threeLevelUlContainDom = this.iosSelectLayer.el.querySelector('.select-three-level');
  217. this.fourLevelUlContainDom = this.iosSelectLayer.el.querySelector('.select-four-level');
  218. this.fiveLevelUlContainDom = this.iosSelectLayer.el.querySelector('.select-five-level');
  219. this.iosSelectLayer.el.querySelector('.layer').style.height = this.options.itemHeight * this.options.itemShowCount + this.options.headerHeight + this.options.cssUnit;
  220. this.oneLevelContainDom.style.height = this.options.itemHeight * this.options.itemShowCount + this.options.cssUnit;
  221. this.offsetTop = document.body.scrollTop;
  222. document.body.classList.add('ios-select-body-class');
  223. window.scrollTo(0, 0);
  224. this.scrollOne = new IScroll('#oneLevelContain', {
  225. probeType: 3,
  226. bounce: false
  227. });
  228. this.scrollOne.on('scrollStart', function() {
  229. Array.prototype.slice.call(self.oneLevelContainDom.querySelectorAll('li')).forEach(function(v, i, o) {
  230. if (v.classList.contains('at')) {
  231. v.classList.remove('at');
  232. } else if (v.classList.contains('side1')) {
  233. v.classList.remove('side1');
  234. } else if (v.classList.contains('side2')) {
  235. v.classList.remove('side2');
  236. }
  237. });
  238. });
  239. this.scrollOne.on('scroll', function() {
  240. var pa = Math.abs(this.y / self.baseSize) / self.options.itemHeight;
  241. var plast = 1;
  242. plast = Math.round(pa) + 1;
  243. Array.prototype.slice.call(self.oneLevelContainDom.querySelectorAll('li')).forEach(function(v, i, o) {
  244. if (v.classList.contains('at')) {
  245. v.classList.remove('at');
  246. } else if (v.classList.contains('side1')) {
  247. v.classList.remove('side1');
  248. } else if (v.classList.contains('side2')) {
  249. v.classList.remove('side2');
  250. }
  251. });
  252. self.changeClassName(self.oneLevelContainDom, plast);
  253. });
  254. this.scrollOne.on('scrollEnd', function() {
  255. var pa = Math.abs(this.y / self.baseSize) / self.options.itemHeight;
  256. var plast = 1;
  257. var to = 0;
  258. if (Math.ceil(pa) === Math.round(pa)) {
  259. to = Math.ceil(pa) * self.options.itemHeight * self.baseSize;
  260. plast = Math.ceil(pa) + 1;
  261. } else {
  262. to = Math.floor(pa) * self.options.itemHeight * self.baseSize;
  263. plast = Math.floor(pa) + 1;
  264. }
  265. self.scrollOne.scrollTo(0, -to, 0);
  266. Array.prototype.slice.call(self.oneLevelContainDom.querySelectorAll('li')).forEach(function(v, i, o) {
  267. if (v.classList.contains('at')) {
  268. v.classList.remove('at');
  269. } else if (v.classList.contains('side1')) {
  270. v.classList.remove('side1');
  271. } else if (v.classList.contains('side2')) {
  272. v.classList.remove('side2');
  273. }
  274. });
  275. var pdom = self.changeClassName(self.oneLevelContainDom, plast);
  276. self.selectOneObj = iosSelectUtil.attrToData(pdom, plast);
  277. if (self.level > 1 && self.options.oneTwoRelation === 1) {
  278. self.setTwoLevel(self.selectOneObj.id, self.selectTwoObj.id, self.selectThreeObj.id, self.selectFourObj.id, self.selectFiveObj.id);
  279. }
  280. });
  281. this.scrollOne.on('scrollCancel', function() {
  282. var pa = Math.abs(this.y / self.baseSize) / self.options.itemHeight;
  283. var plast = 1;
  284. var to = 0;
  285. if (Math.ceil(pa) === Math.round(pa)) {
  286. to = Math.ceil(pa) * self.options.itemHeight * self.baseSize;
  287. plast = Math.ceil(pa) + 1;
  288. } else {
  289. to = Math.floor(pa) * self.options.itemHeight * self.baseSize;
  290. plast = Math.floor(pa) + 1;
  291. }
  292. self.scrollOne.scrollTo(0, -to, 0);
  293. Array.prototype.slice.call(self.oneLevelContainDom.querySelectorAll('li')).forEach(function(v, i, o) {
  294. if (v.classList.contains('at')) {
  295. v.classList.remove('at');
  296. } else if (v.classList.contains('side1')) {
  297. v.classList.remove('side1');
  298. } else if (v.classList.contains('side2')) {
  299. v.classList.remove('side2');
  300. }
  301. });
  302. var pdom = self.changeClassName(self.oneLevelContainDom, plast);
  303. self.selectOneObj = iosSelectUtil.attrToData(pdom, plast);
  304. if (self.level > 1 && self.options.oneTwoRelation === 1) {
  305. self.setTwoLevel(self.selectOneObj.id, self.selectTwoObj.id, self.selectThreeObj.id, self.selectFourObj.id, self.selectFiveObj.id);
  306. }
  307. });
  308. if (this.level >= 2) {
  309. this.twoLevelContainDom.style.height = this.options.itemHeight * this.options.itemShowCount + this.options.cssUnit;
  310. this.scrollTwo = new IScroll('#twoLevelContain', {
  311. probeType: 3,
  312. bounce: false
  313. });
  314. this.scrollTwo.on('scrollStart', function() {
  315. Array.prototype.slice.call(self.twoLevelContainDom.querySelectorAll('li')).forEach(function(v, i, o) {
  316. if (v.classList.contains('at')) {
  317. v.classList.remove('at');
  318. } else if (v.classList.contains('side1')) {
  319. v.classList.remove('side1');
  320. } else if (v.classList.contains('side2')) {
  321. v.classList.remove('side2');
  322. }
  323. });
  324. });
  325. this.scrollTwo.on('scroll', function() {
  326. var pa = Math.abs(this.y / self.baseSize) / self.options.itemHeight;
  327. var plast = 0;
  328. plast = Math.round(pa) + 1;
  329. Array.prototype.slice.call(self.twoLevelContainDom.querySelectorAll('li')).forEach(function(v, i, o) {
  330. if (v.classList.contains('at')) {
  331. v.classList.remove('at');
  332. } else if (v.classList.contains('side1')) {
  333. v.classList.remove('side1');
  334. } else if (v.classList.contains('side2')) {
  335. v.classList.remove('side2');
  336. }
  337. });
  338. self.changeClassName(self.twoLevelContainDom, plast);
  339. });
  340. this.scrollTwo.on('scrollEnd', function() {
  341. var pa = Math.abs(this.y / self.baseSize) / self.options.itemHeight;
  342. var plast = 1;
  343. var to = 0;
  344. if (Math.ceil(pa) === Math.round(pa)) {
  345. to = Math.ceil(pa) * self.options.itemHeight * self.baseSize;
  346. plast = Math.ceil(pa) + 1;
  347. } else {
  348. to = Math.floor(pa) * self.options.itemHeight * self.baseSize;
  349. plast = Math.floor(pa) + 1;
  350. }
  351. self.scrollTwo.scrollTo(0, -to, 0);
  352. Array.prototype.slice.call(self.twoLevelContainDom.querySelectorAll('li')).forEach(function(v, i, o) {
  353. if (v.classList.contains('at')) {
  354. v.classList.remove('at');
  355. } else if (v.classList.contains('side1')) {
  356. v.classList.remove('side1');
  357. } else if (v.classList.contains('side2')) {
  358. v.classList.remove('side2');
  359. }
  360. });
  361. var pdom = self.changeClassName(self.twoLevelContainDom, plast);
  362. self.selectTwoObj = iosSelectUtil.attrToData(pdom, plast);
  363. if (self.level > 2 && self.options.twoThreeRelation === 1) {
  364. self.setThreeLevel(self.selectOneObj.id, self.selectTwoObj.id, self.selectThreeObj.id, self.selectFourObj.id, self.selectFiveObj.id);
  365. }
  366. });
  367. this.scrollTwo.on('scrollCancel', function() {
  368. var pa = Math.abs(this.y / self.baseSize) / self.options.itemHeight;
  369. var plast = 1;
  370. var to = 0;
  371. if (Math.ceil(pa) === Math.round(pa)) {
  372. to = Math.ceil(pa) * self.options.itemHeight * self.baseSize;
  373. plast = Math.ceil(pa) + 1;
  374. } else {
  375. to = Math.floor(pa) * self.options.itemHeight * self.baseSize;
  376. plast = Math.floor(pa) + 1;
  377. }
  378. self.scrollTwo.scrollTo(0, -to, 0);
  379. Array.prototype.slice.call(self.twoLevelContainDom.querySelectorAll('li')).forEach(function(v, i, o) {
  380. if (v.classList.contains('at')) {
  381. v.classList.remove('at');
  382. } else if (v.classList.contains('side1')) {
  383. v.classList.remove('side1');
  384. } else if (v.classList.contains('side2')) {
  385. v.classList.remove('side2');
  386. }
  387. });
  388. var pdom = self.changeClassName(self.twoLevelContainDom, plast);
  389. self.selectTwoObj = iosSelectUtil.attrToData(pdom, plast);
  390. if (self.level > 2 && self.options.twoThreeRelation === 1) {
  391. self.setThreeLevel(self.selectOneObj.id, self.selectTwoObj.id, self.selectThreeObj.id, self.selectFourObj.id, self.selectFiveObj.id);
  392. }
  393. });
  394. }
  395. if (this.level >= 3) {
  396. this.threeLevelContainDom.style.height = this.options.itemHeight * this.options.itemShowCount + this.options.cssUnit;
  397. this.scrollThree = new IScroll('#threeLevelContain', {
  398. probeType: 3,
  399. bounce: false
  400. });
  401. this.scrollThree.on('scrollStart', function() {
  402. Array.prototype.slice.call(self.threeLevelContainDom.querySelectorAll('li')).forEach(function(v, i, o) {
  403. if (v.classList.contains('at')) {
  404. v.classList.remove('at');
  405. } else if (v.classList.contains('side1')) {
  406. v.classList.remove('side1');
  407. } else if (v.classList.contains('side2')) {
  408. v.classList.remove('side2');
  409. }
  410. });
  411. });
  412. this.scrollThree.on('scroll', function() {
  413. var pa = Math.abs(this.y / self.baseSize) / self.options.itemHeight;
  414. var plast = 0;
  415. plast = Math.round(pa) + 1;
  416. Array.prototype.slice.call(self.threeLevelContainDom.querySelectorAll('li')).forEach(function(v, i, o) {
  417. if (v.classList.contains('at')) {
  418. v.classList.remove('at');
  419. } else if (v.classList.contains('side1')) {
  420. v.classList.remove('side1');
  421. } else if (v.classList.contains('side2')) {
  422. v.classList.remove('side2');
  423. }
  424. });
  425. self.changeClassName(self.threeLevelContainDom, plast);
  426. });
  427. this.scrollThree.on('scrollEnd', function() {
  428. var pa = Math.abs(this.y / self.baseSize) / self.options.itemHeight;
  429. var plast = 1;
  430. var to = 0;
  431. if (Math.ceil(pa) === Math.round(pa)) {
  432. to = Math.ceil(pa) * self.options.itemHeight * self.baseSize;
  433. plast = Math.ceil(pa) + 1;
  434. } else {
  435. to = Math.floor(pa) * self.options.itemHeight * self.baseSize;
  436. plast = Math.floor(pa) + 1;
  437. }
  438. self.scrollThree.scrollTo(0, -to, 0);
  439. Array.prototype.slice.call(self.threeLevelContainDom.querySelectorAll('li')).forEach(function(v, i, o) {
  440. if (v.classList.contains('at')) {
  441. v.classList.remove('at');
  442. } else if (v.classList.contains('side1')) {
  443. v.classList.remove('side1');
  444. } else if (v.classList.contains('side2')) {
  445. v.classList.remove('side2');
  446. }
  447. });
  448. var pdom = self.changeClassName(self.threeLevelContainDom, plast);
  449. self.selectThreeObj = iosSelectUtil.attrToData(pdom, plast);
  450. if (self.level >= 4 && self.options.threeFourRelation === 1) {
  451. self.setFourLevel(self.selectOneObj.id, self.selectTwoObj.id, self.selectThreeObj.id, self.selectFourObj.id, self.selectFiveObj.id);
  452. }
  453. });
  454. this.scrollThree.on('scrollCancel', function() {
  455. var pa = Math.abs(this.y / self.baseSize) / self.options.itemHeight;
  456. var plast = 1;
  457. var to = 0;
  458. if (Math.ceil(pa) === Math.round(pa)) {
  459. to = Math.ceil(pa) * self.options.itemHeight * self.baseSize;
  460. plast = Math.ceil(pa) + 1;
  461. } else {
  462. to = Math.floor(pa) * self.options.itemHeight * self.baseSize;
  463. plast = Math.floor(pa) + 1;
  464. }
  465. self.scrollThree.scrollTo(0, -to, 0);
  466. Array.prototype.slice.call(self.threeLevelContainDom.querySelectorAll('li')).forEach(function(v, i, o) {
  467. if (v.classList.contains('at')) {
  468. v.classList.remove('at');
  469. } else if (v.classList.contains('side1')) {
  470. v.classList.remove('side1');
  471. } else if (v.classList.contains('side2')) {
  472. v.classList.remove('side2');
  473. }
  474. });
  475. var pdom = self.changeClassName(self.threeLevelContainDom, plast);
  476. self.selectThreeObj = iosSelectUtil.attrToData(pdom, plast);
  477. if (self.level >= 4 && self.options.threeFourRelation === 1) {
  478. self.setFourLevel(self.selectOneObj.id, self.selectTwoObj.id, self.selectThreeObj.id, self.selectFourObj.id, self.selectFiveObj.id);
  479. }
  480. });
  481. }
  482. if (this.level >= 4) {
  483. this.fourLevelContainDom.style.height = this.options.itemHeight * this.options.itemShowCount + this.options.cssUnit;
  484. this.scrollFour = new IScroll('#fourLevelContain', {
  485. probeType: 3,
  486. bounce: false
  487. });
  488. this.scrollFour.on('scrollStart', function() {
  489. Array.prototype.slice.call(self.fourLevelContainDom.querySelectorAll('li')).forEach(function(v, i, o) {
  490. if (v.classList.contains('at')) {
  491. v.classList.remove('at');
  492. } else if (v.classList.contains('side1')) {
  493. v.classList.remove('side1');
  494. } else if (v.classList.contains('side2')) {
  495. v.classList.remove('side2');
  496. }
  497. });
  498. });
  499. this.scrollFour.on('scroll', function() {
  500. var pa = Math.abs(this.y / self.baseSize) / self.options.itemHeight;
  501. var plast = 0;
  502. plast = Math.round(pa) + 1;
  503. Array.prototype.slice.call(self.fourLevelContainDom.querySelectorAll('li')).forEach(function(v, i, o) {
  504. if (v.classList.contains('at')) {
  505. v.classList.remove('at');
  506. } else if (v.classList.contains('side1')) {
  507. v.classList.remove('side1');
  508. } else if (v.classList.contains('side2')) {
  509. v.classList.remove('side2');
  510. }
  511. });
  512. self.changeClassName(self.fourLevelContainDom, plast);
  513. });
  514. this.scrollFour.on('scrollEnd', function() {
  515. var pa = Math.abs(this.y / self.baseSize) / self.options.itemHeight;
  516. var plast = 1;
  517. var to = 0;
  518. if (Math.ceil(pa) === Math.round(pa)) {
  519. to = Math.ceil(pa) * self.options.itemHeight * self.baseSize;
  520. plast = Math.ceil(pa) + 1;
  521. } else {
  522. to = Math.floor(pa) * self.options.itemHeight * self.baseSize;
  523. plast = Math.floor(pa) + 1;
  524. }
  525. self.scrollFour.scrollTo(0, -to, 0);
  526. Array.prototype.slice.call(self.fourLevelContainDom.querySelectorAll('li')).forEach(function(v, i, o) {
  527. if (v.classList.contains('at')) {
  528. v.classList.remove('at');
  529. } else if (v.classList.contains('side1')) {
  530. v.classList.remove('side1');
  531. } else if (v.classList.contains('side2')) {
  532. v.classList.remove('side2');
  533. }
  534. });
  535. var pdom = self.changeClassName(self.fourLevelContainDom, plast);
  536. self.selectFourObj = iosSelectUtil.attrToData(pdom, plast);
  537. if (self.level >= 5 && self.options.fourFiveRelation === 1) {
  538. self.setFiveLevel(self.selectOneObj.id, self.selectTwoObj.id, self.selectThreeObj.id, self.selectFourObj.id, self.selectFiveObj.id);
  539. }
  540. });
  541. this.scrollFour.on('scrollCancel', function() {
  542. var pa = Math.abs(this.y / self.baseSize) / self.options.itemHeight;
  543. var plast = 1;
  544. var to = 0;
  545. if (Math.ceil(pa) === Math.round(pa)) {
  546. to = Math.ceil(pa) * self.options.itemHeight * self.baseSize;
  547. plast = Math.ceil(pa) + 1;
  548. } else {
  549. to = Math.floor(pa) * self.options.itemHeight * self.baseSize;
  550. plast = Math.floor(pa) + 1;
  551. }
  552. self.scrollFour.scrollTo(0, -to, 0);
  553. Array.prototype.slice.call(self.fourLevelContainDom.querySelectorAll('li')).forEach(function(v, i, o) {
  554. if (v.classList.contains('at')) {
  555. v.classList.remove('at');
  556. } else if (v.classList.contains('side1')) {
  557. v.classList.remove('side1');
  558. } else if (v.classList.contains('side2')) {
  559. v.classList.remove('side2');
  560. }
  561. });
  562. var pdom = self.changeClassName(self.fourLevelContainDom, plast);
  563. self.selectFourObj = iosSelectUtil.attrToData(pdom, plast);
  564. if (self.level >= 5 && self.options.fourFiveRelation === 1) {
  565. self.setFiveLevel(self.selectOneObj.id, self.selectTwoObj.id, self.selectThreeObj.id, self.selectFourObj.id, self.selectFiveObj.id);
  566. }
  567. });
  568. }
  569. if (this.level >= 5) {
  570. this.fiveLevelContainDom.style.height = this.options.itemHeight * this.options.itemShowCount + this.options.cssUnit;
  571. this.scrollFive = new IScroll('#fiveLevelContain', {
  572. probeType: 3,
  573. bounce: false
  574. });
  575. this.scrollFive.on('scrollStart', function() {
  576. Array.prototype.slice.call(self.fiveLevelContainDom.querySelectorAll('li')).forEach(function(v, i, o) {
  577. if (v.classList.contains('at')) {
  578. v.classList.remove('at');
  579. } else if (v.classList.contains('side1')) {
  580. v.classList.remove('side1');
  581. } else if (v.classList.contains('side2')) {
  582. v.classList.remove('side2');
  583. }
  584. });
  585. });
  586. this.scrollFive.on('scroll', function() {
  587. var pa = Math.abs(this.y / self.baseSize) / self.options.itemHeight;
  588. var plast = 0;
  589. plast = Math.round(pa) + 1;
  590. Array.prototype.slice.call(self.fiveLevelContainDom.querySelectorAll('li')).forEach(function(v, i, o) {
  591. if (v.classList.contains('at')) {
  592. v.classList.remove('at');
  593. } else if (v.classList.contains('side1')) {
  594. v.classList.remove('side1');
  595. } else if (v.classList.contains('side2')) {
  596. v.classList.remove('side2');
  597. }
  598. });
  599. self.changeClassName(self.fiveLevelContainDom, plast);
  600. });
  601. this.scrollFive.on('scrollEnd', function() {
  602. var pa = Math.abs(this.y / self.baseSize) / self.options.itemHeight;
  603. var plast = 1;
  604. var to = 0;
  605. if (Math.ceil(pa) === Math.round(pa)) {
  606. to = Math.ceil(pa) * self.options.itemHeight * self.baseSize;
  607. plast = Math.ceil(pa) + 1;
  608. } else {
  609. to = Math.floor(pa) * self.options.itemHeight * self.baseSize;
  610. plast = Math.floor(pa) + 1;
  611. }
  612. self.scrollFive.scrollTo(0, -to, 0);
  613. Array.prototype.slice.call(self.fiveLevelContainDom.querySelectorAll('li')).forEach(function(v, i, o) {
  614. if (v.classList.contains('at')) {
  615. v.classList.remove('at');
  616. } else if (v.classList.contains('side1')) {
  617. v.classList.remove('side1');
  618. } else if (v.classList.contains('side2')) {
  619. v.classList.remove('side2');
  620. }
  621. });
  622. var pdom = self.changeClassName(self.fiveLevelContainDom, plast);
  623. self.selectFiveObj = iosSelectUtil.attrToData(pdom, plast);
  624. });
  625. this.scrollFive.on('scrollCancel', function() {
  626. var pa = Math.abs(this.y / self.baseSize) / self.options.itemHeight;
  627. var plast = 1;
  628. var to = 0;
  629. if (Math.ceil(pa) === Math.round(pa)) {
  630. to = Math.ceil(pa) * self.options.itemHeight * self.baseSize;
  631. plast = Math.ceil(pa) + 1;
  632. } else {
  633. to = Math.floor(pa) * self.options.itemHeight * self.baseSize;
  634. plast = Math.floor(pa) + 1;
  635. }
  636. self.scrollFive.scrollTo(0, -to, 0);
  637. Array.prototype.slice.call(self.fiveLevelContainDom.querySelectorAll('li')).forEach(function(v, i, o) {
  638. if (v.classList.contains('at')) {
  639. v.classList.remove('at');
  640. } else if (v.classList.contains('side1')) {
  641. v.classList.remove('side1');
  642. } else if (v.classList.contains('side2')) {
  643. v.classList.remove('side2');
  644. }
  645. });
  646. var pdom = self.changeClassName(self.fiveLevelContainDom, plast);
  647. self.selectFiveObj = iosSelectUtil.attrToData(pdom, plast);
  648. });
  649. }
  650. // 取消 确认 事件
  651. this.closeBtnDom = this.iosSelectLayer.el.querySelector('.close');
  652. this.closeBtnDom.addEventListener('click', function(e) {
  653. if (document.body.classList.contains('ios-select-body-class')) {
  654. document.body.classList.remove('ios-select-body-class');
  655. }
  656. window.scrollTo(0, self.offsetTop);
  657. });
  658. this.selectBtnDom = this.iosSelectLayer.el.querySelector('.sure');
  659. this.selectBtnDom.addEventListener('click', function(e) {
  660. if (document.body.classList.contains('ios-select-body-class')) {
  661. document.body.classList.remove('ios-select-body-class');
  662. }
  663. window.scrollTo(0, self.offsetTop);
  664. self.callback && self.callback(self.selectOneObj, self.selectTwoObj, self.selectThreeObj, self.selectFourObj, self.selectFiveObj);
  665. });
  666. },
  667. loadingShow: function() {
  668. this.options.showLoading && (this.iosSelectLoadingBoxDom.style.display = 'block');
  669. },
  670. loadingHide: function() {
  671. this.iosSelectLoadingBoxDom.style.display = 'none';
  672. },
  673. getOneLevel: function() {
  674. return this.data[0];
  675. },
  676. setOneLevel: function(oneLevelId, twoLevelId, threeLevelId, fourLevelId, fiveLevelId) {
  677. if (iosSelectUtil.isArray(this.data[0])){
  678. var oneLevelData = this.getOneLevel();
  679. this.renderOneLevel(oneLevelId, twoLevelId, threeLevelId, fourLevelId, fiveLevelId, oneLevelData);
  680. }
  681. else if (iosSelectUtil.isFunction(this.data[0])) {
  682. this.loadingShow();
  683. this.data[0](function(oneLevelData) {
  684. this.loadingHide();
  685. this.renderOneLevel(oneLevelId, twoLevelId, threeLevelId, fourLevelId, fiveLevelId, oneLevelData);
  686. }.bind(this));
  687. }
  688. else {
  689. throw new Error('data format error');
  690. }
  691. },
  692. renderOneLevel: function(oneLevelId, twoLevelId, threeLevelId, fourLevelId, fiveLevelId, oneLevelData) {
  693. var hasAtId = oneLevelData.some(function(v, i, o) {
  694. return v.id == oneLevelId;
  695. });
  696. if (!hasAtId) {
  697. oneLevelId = oneLevelData[0]['id'];
  698. }
  699. var oneHtml = '';
  700. var self = this;
  701. var plast = 0;
  702. oneHtml += this.getWhiteItem();
  703. oneLevelData.forEach(function(v, i, o) {
  704. if (v.id == oneLevelId) {
  705. oneHtml += '<li style="height: ' + this.options.itemHeight + this.options.cssUnit + '; line-height: ' + this.options.itemHeight + this.options.cssUnit +';" ' + iosSelectUtil.attrToHtml(v) + ' class="at">' + v.value + '</li>';
  706. plast = i + 1;
  707. } else {
  708. oneHtml += '<li style="height: ' + this.options.itemHeight + this.options.cssUnit + '; line-height: ' + this.options.itemHeight + this.options.cssUnit +';"' + iosSelectUtil.attrToHtml(v) + '>' + v.value + '</li>';
  709. }
  710. }.bind(this));
  711. oneHtml += this.getWhiteItem();
  712. this.oneLevelUlContainDom.innerHTML = oneHtml;
  713. this.scrollOne.refresh();
  714. this.scrollOne.scrollToElement('li:nth-child(' + plast + ')', 0);
  715. if (this.level >= 2) {
  716. this.setTwoLevel(oneLevelId, twoLevelId, threeLevelId, fourLevelId, fiveLevelId);
  717. }
  718. var pdom = this.changeClassName(this.oneLevelContainDom, plast);
  719. this.selectOneObj = iosSelectUtil.attrToData(pdom, this.getAtIndexByPlast(plast));
  720. },
  721. getTwoLevel: function(oneLevelId) {
  722. var twoLevelData = [];
  723. if (this.options.oneTwoRelation === 1) {
  724. this.data[1].forEach(function(v, i, o) {
  725. if (v['parentId'] === oneLevelId) {
  726. twoLevelData.push(v);
  727. }
  728. });
  729. } else {
  730. twoLevelData = this.data[1];
  731. }
  732. return twoLevelData;
  733. },
  734. setTwoLevel: function(oneLevelId, twoLevelId, threeLevelId, fourLevelId, fiveLevelId) {
  735. if (iosSelectUtil.isArray(this.data[1])) {
  736. var twoLevelData = this.getTwoLevel(oneLevelId);
  737. this.renderTwoLevel(oneLevelId, twoLevelId, threeLevelId, fourLevelId, fiveLevelId, twoLevelData);
  738. }
  739. else if (iosSelectUtil.isFunction(this.data[1])) {
  740. this.loadingShow();
  741. this.data[1](oneLevelId, function(twoLevelData) {
  742. this.loadingHide();
  743. this.renderTwoLevel(oneLevelId, twoLevelId, threeLevelId, fourLevelId, fiveLevelId, twoLevelData);
  744. }.bind(this));
  745. }
  746. else {
  747. throw new Error('data format error');
  748. }
  749. },
  750. renderTwoLevel: function(oneLevelId, twoLevelId, threeLevelId, fourLevelId, fiveLevelId, twoLevelData) {
  751. var plast = 0;
  752. var hasAtId = twoLevelData.some(function(v, i, o) {
  753. return v.id == twoLevelId;
  754. });
  755. if (!hasAtId) {
  756. twoLevelId = twoLevelData[0]['id'];
  757. }
  758. var twoHtml = '';
  759. var self = this;
  760. twoHtml += this.getWhiteItem();
  761. twoLevelData.forEach(function(v, i, o) {
  762. if (v.id == twoLevelId) {
  763. twoHtml += '<li style="height: ' + this.options.itemHeight + this.options.cssUnit + '; line-height: ' + this.options.itemHeight + this.options.cssUnit +';"' + iosSelectUtil.attrToHtml(v) + ' class="at">' + v.value + '</li>';
  764. plast = i + 1;
  765. } else {
  766. twoHtml += '<li style="height: ' + this.options.itemHeight + this.options.cssUnit + '; line-height: ' + this.options.itemHeight + this.options.cssUnit +';"' + iosSelectUtil.attrToHtml(v) + '>' + v.value + '</li>';
  767. }
  768. }.bind(this));
  769. twoHtml += this.getWhiteItem();
  770. this.twoLevelUlContainDom.innerHTML = twoHtml;
  771. this.scrollTwo.refresh();
  772. this.scrollTwo.scrollToElement(':nth-child(' + plast + ')', 0);
  773. if (this.level >= 3) {
  774. this.setThreeLevel(oneLevelId, twoLevelId, threeLevelId, fourLevelId, fiveLevelId);
  775. }
  776. var pdom = this.changeClassName(this.twoLevelContainDom, plast);
  777. this.selectTwoObj = iosSelectUtil.attrToData(pdom, this.getAtIndexByPlast(plast));
  778. },
  779. getThreeLevel: function(oneLevelId, twoLevelId) {
  780. var threeLevelData = [];
  781. if (this.options.twoThreeRelation === 1) {
  782. this.data[2].forEach(function(v, i, o) {
  783. if (v['parentId'] === twoLevelId) {
  784. threeLevelData.push(v);
  785. }
  786. });
  787. } else {
  788. threeLevelData = this.data[2];
  789. }
  790. return threeLevelData;
  791. },
  792. setThreeLevel: function(oneLevelId, twoLevelId, threeLevelId, fourLevelId, fiveLevelId) {
  793. if (iosSelectUtil.isArray(this.data[2])) {
  794. var threeLevelData = this.getThreeLevel(oneLevelId, twoLevelId);
  795. this.renderThreeLevel(oneLevelId, twoLevelId, threeLevelId, fourLevelId, fiveLevelId, threeLevelData);
  796. }
  797. else if (iosSelectUtil.isFunction(this.data[2])) {
  798. this.loadingShow();
  799. this.data[2](oneLevelId, twoLevelId, function(threeLevelData) {
  800. this.loadingHide();
  801. this.renderThreeLevel(oneLevelId, twoLevelId, threeLevelId, fourLevelId, fiveLevelId, threeLevelData);
  802. }.bind(this));
  803. }
  804. else {
  805. throw new Error('data format error');
  806. }
  807. },
  808. renderThreeLevel: function(oneLevelId, twoLevelId, threeLevelId, fourLevelId, fiveLevelId, threeLevelData) {
  809. var plast = 0;
  810. var hasAtId = threeLevelData.some(function(v, i, o) {
  811. return v.id == threeLevelId;
  812. });
  813. if (!hasAtId) {
  814. threeLevelId = threeLevelData[0]['id'];
  815. }
  816. var threeHtml = '';
  817. var self = this;
  818. threeHtml += this.getWhiteItem();
  819. threeLevelData.forEach(function(v, i, o) {
  820. if (v.id == threeLevelId) {
  821. threeHtml += '<li style="height: ' + this.options.itemHeight + this.options.cssUnit + '; line-height: ' + this.options.itemHeight + this.options.cssUnit +';"' + iosSelectUtil.attrToHtml(v) + ' class="at">' + v.value + '</li>';
  822. plast = i + 1;
  823. } else {
  824. threeHtml += '<li style="height: ' + this.options.itemHeight + this.options.cssUnit + '; line-height: ' + this.options.itemHeight + this.options.cssUnit +';"' + iosSelectUtil.attrToHtml(v) + '>' + v.value + '</li>';
  825. }
  826. }.bind(this));
  827. threeHtml += this.getWhiteItem();
  828. this.threeLevelUlContainDom.innerHTML = threeHtml;
  829. this.scrollThree.refresh();
  830. this.scrollThree.scrollToElement(':nth-child(' + plast + ')', 0);
  831. if (this.level >= 4) {
  832. this.setFourLevel(oneLevelId, twoLevelId, threeLevelId, fourLevelId, fiveLevelId);
  833. }
  834. var pdom = this.changeClassName(this.threeLevelContainDom, plast);
  835. this.selectThreeObj = iosSelectUtil.attrToData(pdom, this.getAtIndexByPlast(plast));
  836. },
  837. getFourLevel: function(oneLevelId, twoLevelId, threeLevelId) {
  838. var fourLevelData = [];
  839. if (this.options.threeFourRelation === 1) {
  840. this.data[3].forEach(function(v, i, o) {
  841. if (v['parentId'] === threeLevelId) {
  842. fourLevelData.push(v);
  843. }
  844. });
  845. } else {
  846. fourLevelData = this.data[3];
  847. }
  848. return fourLevelData;
  849. },
  850. setFourLevel: function(oneLevelId, twoLevelId, threeLevelId, fourLevelId, fiveLevelId) {
  851. if (iosSelectUtil.isArray(this.data[3])) {
  852. var fourLevelData = this.getFourLevel(oneLevelId, twoLevelId, threeLevelId);
  853. this.renderFourLevel(oneLevelId, twoLevelId, threeLevelId, fourLevelId, fiveLevelId, fourLevelData);
  854. }
  855. else if (iosSelectUtil.isFunction(this.data[3])) {
  856. this.loadingShow();
  857. this.data[3](oneLevelId, twoLevelId, threeLevelId, function(fourLevelData) {
  858. this.loadingHide();
  859. this.renderFourLevel(oneLevelId, twoLevelId, threeLevelId, fourLevelId, fiveLevelId, fourLevelData);
  860. }.bind(this));
  861. }
  862. else {
  863. throw new Error('data format error');
  864. }
  865. },
  866. renderFourLevel: function(oneLevelId, twoLevelId, threeLevelId, fourLevelId, fiveLevelId, fourLevelData) {
  867. var plast = 0;
  868. var hasAtId = fourLevelData.some(function(v, i, o) {
  869. return v.id == fourLevelId;
  870. });
  871. if (!hasAtId) {
  872. fourLevelId = fourLevelData[0]['id'];
  873. }
  874. var fourHtml = '';
  875. var self = this;
  876. fourHtml += this.getWhiteItem();
  877. fourLevelData.forEach(function(v, i, o) {
  878. if (v.id == fourLevelId) {
  879. fourHtml += '<li style="height: ' + this.options.itemHeight + this.options.cssUnit + '; line-height: ' + this.options.itemHeight + this.options.cssUnit +';"' + iosSelectUtil.attrToHtml(v) + ' class="at">' + v.value + '</li>';
  880. plast = i + 1;
  881. } else {
  882. fourHtml += '<li style="height: ' + this.options.itemHeight + this.options.cssUnit + '; line-height: ' + this.options.itemHeight + this.options.cssUnit +';"' + iosSelectUtil.attrToHtml(v) + '>' + v.value + '</li>';
  883. }
  884. }.bind(this));
  885. fourHtml += this.getWhiteItem();
  886. this.fourLevelUlContainDom.innerHTML = fourHtml;
  887. this.scrollFour.refresh();
  888. this.scrollFour.scrollToElement(':nth-child(' + plast + ')', 0);
  889. if (this.level >= 5) {
  890. this.setFiveLevel(oneLevelId, twoLevelId, threeLevelId, fourLevelId, fiveLevelId);
  891. }
  892. var pdom = this.changeClassName(this.fourLevelContainDom, plast);
  893. this.selectFourObj = iosSelectUtil.attrToData(pdom, this.getAtIndexByPlast(plast));
  894. },
  895. getFiveLevel: function(oneLevelId, twoLevelId, threeLevelId, fourLevelId) {
  896. var fiveLevelData = [];
  897. if (this.options.fourFiveRelation === 1) {
  898. this.data[4].forEach(function(v, i, o) {
  899. if (v['parentId'] === fourLevelId) {
  900. fiveLevelData.push(v);
  901. }
  902. });
  903. } else {
  904. fiveLevelData = this.data[4];
  905. }
  906. return fiveLevelData;
  907. },
  908. setFiveLevel: function(oneLevelId, twoLevelId, threeLevelId, fourLevelId, fiveLevelId) {
  909. if (iosSelectUtil.isArray(this.data[4])) {
  910. var fiveLevelData = this.getFiveLevel(oneLevelId, twoLevelId, threeLevelId, fourLevelId);
  911. this.renderFiveLevel(oneLevelId, twoLevelId, threeLevelId, fourLevelId, fiveLevelId, fiveLevelData);
  912. }
  913. else if (iosSelectUtil.isFunction(this.data[4])) {
  914. this.loadingShow();
  915. this.data[4](oneLevelId, twoLevelId, threeLevelId, fourLevelId, function(fiveLevelData) {
  916. this.loadingHide();
  917. this.renderFiveLevel(oneLevelId, twoLevelId, threeLevelId, fourLevelId, fiveLevelId, fiveLevelData);
  918. }.bind(this));
  919. }
  920. else {
  921. throw new Error('data format error');
  922. }
  923. },
  924. renderFiveLevel: function(oneLevelId, twoLevelId, threeLevelId, fourLevelId, fiveLevelId, fiveLevelData) {
  925. var plast = 0;
  926. var hasAtId = fiveLevelData.some(function(v, i, o) {
  927. return v.id == fiveLevelId;
  928. });
  929. if (!hasAtId) {
  930. fiveLevelId = fiveLevelData[0]['id'];
  931. }
  932. var fiveHtml = '';
  933. var self = this;
  934. fiveHtml += this.getWhiteItem();
  935. fiveLevelData.forEach(function(v, i, o) {
  936. if (v.id == fiveLevelId) {
  937. fiveHtml += '<li style="height: ' + this.options.itemHeight + this.options.cssUnit + '; line-height: ' + this.options.itemHeight + this.options.cssUnit +';"' + iosSelectUtil.attrToHtml(v) + ' class="at">' + v.value + '</li>';
  938. plast = i + 1;
  939. } else {
  940. fiveHtml += '<li style="height: ' + this.options.itemHeight + this.options.cssUnit + '; line-height: ' + this.options.itemHeight + this.options.cssUnit +';"' + iosSelectUtil.attrToHtml(v) + '>' + v.value + '</li>';
  941. }
  942. }.bind(this));
  943. fiveHtml += this.getWhiteItem();
  944. this.fiveLevelUlContainDom.innerHTML = fiveHtml;
  945. this.scrollFive.refresh();
  946. this.scrollFive.scrollToElement(':nth-child(' + plast + ')', 0);
  947. var pdom = this.changeClassName(this.fiveLevelContainDom, plast);
  948. this.selectFiveObj = iosSelectUtil.attrToData(pdom, this.getAtIndexByPlast(plast));
  949. },
  950. getWhiteItem: function() {
  951. var whiteItemHtml = '';
  952. whiteItemHtml += '<li style="height: ' + this.options.itemHeight +this.options.cssUnit + '; line-height: ' + this.options.itemHeight +this.options.cssUnit + '"></li>';
  953. if (this.options.itemShowCount > 3) {
  954. whiteItemHtml += '<li style="height: ' + this.options.itemHeight +this.options.cssUnit + '; line-height: ' + this.options.itemHeight +this.options.cssUnit + '"></li>';
  955. }
  956. if (this.options.itemShowCount > 5) {
  957. whiteItemHtml += '<li style="height: ' + this.options.itemHeight +this.options.cssUnit + '; line-height: ' + this.options.itemHeight +this.options.cssUnit + '"></li>';
  958. }
  959. if (this.options.itemShowCount > 7) {
  960. whiteItemHtml += '<li style="height: ' + this.options.itemHeight +this.options.cssUnit + '; line-height: ' + this.options.itemHeight +this.options.cssUnit + '"></li>';
  961. }
  962. return whiteItemHtml;
  963. },
  964. changeClassName: function(levelContainDom, plast) {
  965. var pdom;
  966. if (this.options.itemShowCount === 3) {
  967. pdom = levelContainDom.querySelector('li:nth-child(' + (plast + 1) + ')');
  968. pdom.classList.add('at');
  969. }
  970. else if (this.options.itemShowCount === 5) {
  971. pdom = levelContainDom.querySelector('li:nth-child(' + (plast + 2) + ')');
  972. pdom.classList.add('at');
  973. levelContainDom.querySelector('li:nth-child(' + (plast + 1) + ')').classList.add('side1');
  974. levelContainDom.querySelector('li:nth-child(' + (plast + 3) + ')').classList.add('side1');
  975. }
  976. else if (this.options.itemShowCount === 7) {
  977. pdom = levelContainDom.querySelector('li:nth-child(' + (plast + 3) + ')');
  978. pdom.classList.add('at');
  979. levelContainDom.querySelector('li:nth-child(' + (plast + 2) + ')').classList.add('side1');
  980. levelContainDom.querySelector('li:nth-child(' + (plast + 1) + ')').classList.add('side2');
  981. levelContainDom.querySelector('li:nth-child(' + (plast + 4) + ')').classList.add('side1');
  982. levelContainDom.querySelector('li:nth-child(' + (plast + 5) + ')').classList.add('side2');
  983. }
  984. else if (this.options.itemShowCount === 9) {
  985. pdom = levelContainDom.querySelector('li:nth-child(' + (plast + 4) + ')');
  986. pdom.classList.add('at');
  987. levelContainDom.querySelector('li:nth-child(' + (plast + 3) + ')').classList.add('side1');
  988. levelContainDom.querySelector('li:nth-child(' + (plast + 2) + ')').classList.add('side2');
  989. levelContainDom.querySelector('li:nth-child(' + (plast + 5) + ')').classList.add('side1');
  990. levelContainDom.querySelector('li:nth-child(' + (plast + 6) + ')').classList.add('side2');
  991. }
  992. return pdom;
  993. },
  994. getAtIndexByPlast: function(plast) {
  995. return plast + Math.ceil(this.options.itemShowCount / 2);
  996. },
  997. setBase: function() {
  998. if (this.options.cssUnit === 'rem') {
  999. var dltDom = document.documentElement;
  1000. var dltStyle = window.getComputedStyle(dltDom, null);
  1001. var dltFontSize = dltStyle.fontSize;
  1002. try {
  1003. this.baseSize = /\d+(?:\.\d+)?/.exec(dltFontSize)[0];
  1004. }
  1005. catch(e) {
  1006. this.baseSize = 1;
  1007. }
  1008. }
  1009. else {
  1010. this.baseSize = 1;
  1011. }
  1012. }
  1013. }
  1014. if (typeof module != 'undefined' && module.exports) {
  1015. module.exports = IosSelect;
  1016. } else if (typeof define == 'function' && define.amd) {
  1017. define(function() {
  1018. return IosSelect;
  1019. });
  1020. } else {
  1021. window.IosSelect = IosSelect;
  1022. }
  1023. })();