categryselect.js 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709
  1. app.directive('categrySelect', function() {
  2. return {
  3. restrict: 'E',
  4. // scope: {
  5. // MobileSelect: '=',
  6. // placelable: '='
  7. // },
  8. template: '<div class="contain" style="background:white" class="tabs-top fz-16">' +
  9. '<div class="fixWidth">' +
  10. '<div class="item item-icon-right bordo height_3" id="triggerone" placeholder="{{to.placeholder}}"><span style="font-size:1.4rem;color:#999">请选择分类</span>' +
  11. '<a class="" >' +
  12. '<i class="icon ion-ios-arrow-right font-color-gray" style="font-size: 2rem;"></i>' +
  13. '</a>' +
  14. '</div>' +
  15. '</div>' +
  16. '</div>',
  17. // controller: function($scope, element, attrs) {
  18. controller: ['$scope', '$element', '$attrs', function($scope, $element, $attrs) {
  19. function getClass(dom, string) {
  20. return dom.getElementsByClassName(string);
  21. }
  22. $scope.getcum = function(sliderIndex, data) {
  23. MobileSelect.prototype.updateWheel(sliderIndex, data)
  24. }
  25. //构造器
  26. var MobileSelect = function(config) {
  27. // $scope.trigger = config.trigger.replace("#", "");;
  28. MobileSelect.prototype.mobileSelect;
  29. MobileSelect.prototype.wheelsData = config.wheels;
  30. MobileSelect.prototype.jsonType = false;
  31. MobileSelect.prototype.cascadeJsonData = [];
  32. MobileSelect.prototype.displayJson = [];
  33. MobileSelect.prototype.cascade = false;
  34. MobileSelect.prototype.startY;
  35. MobileSelect.prototype.moveEndY;
  36. MobileSelect.prototype.moveY;
  37. MobileSelect.prototype.oldMoveY;
  38. MobileSelect.prototype.offset = 0;
  39. MobileSelect.prototype.offsetSum = 0;
  40. MobileSelect.prototype.oversizeBorder;
  41. MobileSelect.prototype.curDistance = [];
  42. MobileSelect.prototype.clickStatus = false;
  43. MobileSelect.prototype.isPC = true;
  44. MobileSelect.prototype.init(config);
  45. }
  46. MobileSelect.prototype = {
  47. constructor: MobileSelect,
  48. init: function(config) {
  49. var _this = this;
  50. _this.keyMap = config.keyMap ? config.keyMap : { id: 'id', value: 'value', childs: 'childs' };
  51. _this.checkDataType();
  52. _this.renderWheels(_this.wheelsData, config.cancelBtnText, config.ensureBtnText);
  53. _this.trigger = document.querySelector(config.trigger);
  54. if (!_this.trigger) {
  55. console.error('mobileSelect has been successfully installed, but no trigger found on your page.');
  56. return false;
  57. }
  58. _this.wheel = getClass(_this.mobileSelect, 'wheel');
  59. _this.slider = getClass(_this.mobileSelect, 'selectContainer');
  60. _this.wheels = _this.mobileSelect.querySelector('.wheels');
  61. _this.liHeight = _this.mobileSelect.querySelector('li').offsetHeight;
  62. _this.ensureBtn = _this.mobileSelect.querySelector('.ensure');
  63. _this.cancelBtn = _this.mobileSelect.querySelector('.cancel');
  64. _this.grayLayer = _this.mobileSelect.querySelector('.grayLayer');
  65. _this.popUp = _this.mobileSelect.querySelector('.content');
  66. _this.callback = config.callback ? config.callback : function() {};
  67. _this.transitionEnd = config.transitionEnd ? config.transitionEnd : function() {};
  68. _this.initPosition = config.position ? config.position : [];
  69. _this.titleText = config.title ? config.title : '';
  70. _this.connector = config.connector ? config.connector : ' ';
  71. _this.trigger.style.cursor = 'pointer';
  72. _this.setStyle(config);
  73. _this.setTitle(_this.titleText);
  74. _this.checkIsPC();
  75. _this.checkCascade();
  76. if (_this.cascade) {
  77. _this.initCascade();
  78. }
  79. //定位 初始位置
  80. if (_this.initPosition.length < _this.slider.length) {
  81. var diff = _this.slider.length - _this.initPosition.length;
  82. for (var i = 0; i < diff; i++) {
  83. _this.initPosition.push(0);
  84. }
  85. }
  86. _this.setCurDistance(_this.initPosition);
  87. _this.addListenerAll();
  88. //按钮监听
  89. _this.cancelBtn.addEventListener('click', function() {
  90. _this.mobileSelect.classList.remove('mobileSelect-show');
  91. });
  92. _this.ensureBtn.addEventListener('click', function(event) {
  93. _this.mobileSelect.classList.remove('mobileSelect-show');
  94. var tempValue = '';
  95. // for (var i = 0; i < _this.wheel.length; i++) {
  96. // i == _this.wheel.length - 1 ? tempValue += _this.getInnerHtml(i) : tempValue += _this.getInnerHtml(i) + _this.connector;
  97. tempValue += _this.getInnerHtml(_this.wheel.length - 1);
  98. // }
  99. _this.trigger.innerHTML = tempValue;
  100. _this.callback(_this.getIndexArr(), _this.getValue());
  101. $scope.getselect(_this.getValue());
  102. });
  103. _this.trigger.addEventListener('click', function() {
  104. // _this.mobileSelect.classList.add('mobileSelect-show');
  105. // document.querySelector('#mobileSelect').addClass('mobileSelect-show');
  106. // $('#mobileSelect').addClass('mobileSelect-show');
  107. _this.mobileSelect.classList.add('mobileSelect-show');
  108. // alert(_this.mobileSelect.classList);
  109. });
  110. _this.grayLayer.addEventListener('click', function() {
  111. _this.mobileSelect.classList.remove('mobileSelect-show');
  112. });
  113. _this.popUp.addEventListener('click', function() {
  114. event.stopPropagation();
  115. });
  116. _this.fixRowStyle(); //修正列数
  117. },
  118. setTitle: function(string) {
  119. var _this = this;
  120. _this.titleText = string;
  121. _this.mobileSelect.querySelector('.title').innerHTML = _this.titleText;
  122. },
  123. setStyle: function(config) {
  124. var _this = this;
  125. if (config.ensureBtnColor) {
  126. _this.ensureBtn.style.color = config.ensureBtnColor;
  127. }
  128. if (config.cancelBtnColor) {
  129. _this.cancelBtn.style.color = config.cancelBtnColor;
  130. }
  131. if (config.titleColor) {
  132. _this.title = _this.mobileSelect.querySelector('.title');
  133. _this.title.style.color = config.titleColor;
  134. }
  135. if (config.textColor) {
  136. _this.panel = _this.mobileSelect.querySelector('.panel');
  137. _this.panel.style.color = config.textColor;
  138. }
  139. if (config.titleBgColor) {
  140. _this.btnBar = _this.mobileSelect.querySelector('.btnBar');
  141. _this.btnBar.style.backgroundColor = config.titleBgColor;
  142. }
  143. if (config.bgColor) {
  144. _this.panel = _this.mobileSelect.querySelector('.panel');
  145. _this.shadowMask = _this.mobileSelect.querySelector('.shadowMask');
  146. _this.panel.style.backgroundColor = config.bgColor;
  147. _this.shadowMask.style.background = 'linear-gradient(to bottom, ' + config.bgColor + ', rgba(255, 255, 255, 0), ' + config.bgColor + ')';
  148. }
  149. },
  150. checkIsPC: function() {
  151. var _this = this;
  152. var sUserAgent = navigator.userAgent.toLowerCase();
  153. var bIsIpad = sUserAgent.match(/ipad/i) == "ipad";
  154. var bIsIphoneOs = sUserAgent.match(/iphone os/i) == "iphone os";
  155. var bIsMidp = sUserAgent.match(/midp/i) == "midp";
  156. var bIsUc7 = sUserAgent.match(/rv:1.2.3.4/i) == "rv:1.2.3.4";
  157. var bIsUc = sUserAgent.match(/ucweb/i) == "ucweb";
  158. var bIsAndroid = sUserAgent.match(/android/i) == "android";
  159. var bIsCE = sUserAgent.match(/windows ce/i) == "windows ce";
  160. var bIsWM = sUserAgent.match(/windows mobile/i) == "windows mobile";
  161. if ((bIsIphoneOs || bIsMidp || bIsUc7 || bIsUc || bIsAndroid || bIsCE || bIsWM)) {
  162. _this.isPC = false;
  163. }
  164. },
  165. show: function() {
  166. this.mobileSelect.classList.add('mobileSelect-show');
  167. },
  168. renderWheels: function(wheelsData, cancelBtnText, ensureBtnText) {
  169. var _this = this;
  170. var cancelText = cancelBtnText ? cancelBtnText : '取消';
  171. var ensureText = ensureBtnText ? ensureBtnText : '确认';
  172. _this.mobileSelect = document.createElement("div");
  173. _this.mobileSelect.className = "mobileSelect";
  174. _this.mobileSelect.id = "mobileSelect";
  175. _this.mobileSelect.innerHTML =
  176. '<div class="grayLayer"></div>' +
  177. '<div class="content">' +
  178. '<div class="btnBar">' +
  179. '<div class="fixWidth">' +
  180. '<div class="cancel">' + cancelText + '</div>' +
  181. '<div class="title"></div>' +
  182. '<div class="ensure" ng-click="upselect()">' + ensureText + '</div>' +
  183. '</div>' +
  184. '</div>' +
  185. '<div class="panel">' +
  186. '<div class="fixWidth">' +
  187. '<div class="wheels">' +
  188. '</div>' +
  189. '<div class="selectLine"></div>' +
  190. '<div class="shadowMask"></div>' +
  191. '</div>' +
  192. '</div>' +
  193. '</div>';
  194. document.body.appendChild(_this.mobileSelect);
  195. //根据数据长度来渲染
  196. var tempHTML = '';
  197. for (var i = 0; i < wheelsData.length; i++) {
  198. //列
  199. tempHTML += '<div class="wheel"><ul class="selectContainer">';
  200. if (_this.jsonType) {
  201. for (var j = 0; j < wheelsData[i].data.length; j++) {
  202. //行
  203. tempHTML += '<li data-id="' + wheelsData[i].data[j][_this.keyMap.id] + '">' + wheelsData[i].data[j][_this.keyMap.value] + '</li>';
  204. }
  205. } else {
  206. for (var j = 0; j < wheelsData[i].data.length; j++) {
  207. //行
  208. tempHTML += '<li>' + wheelsData[i].data[j] + '</li>';
  209. }
  210. }
  211. tempHTML += '</ul></div>';
  212. }
  213. _this.mobileSelect.querySelector('.wheels').innerHTML = tempHTML;
  214. },
  215. addListenerAll: function() {
  216. var _this = this;
  217. for (var i = 0; i < _this.slider.length; i++) {
  218. //手势监听
  219. (function(i) {
  220. _this.addListenerWheel(_this.wheel[i], i);
  221. _this.addListenerLi(i);
  222. })(i);
  223. }
  224. },
  225. addListenerWheel: function(theWheel, index) {
  226. var _this = this;
  227. theWheel.addEventListener('touchstart', function() {
  228. _this.touch(event, this.firstChild, index);
  229. }, false);
  230. theWheel.addEventListener('touchend', function() {
  231. _this.touch(event, this.firstChild, index);
  232. }, false);
  233. theWheel.addEventListener('touchmove', function() {
  234. _this.touch(event, this.firstChild, index);
  235. }, false);
  236. if (_this.isPC) {
  237. //如果是PC端则再增加拖拽监听 方便调试
  238. theWheel.addEventListener('mousedown', function() {
  239. _this.dragClick(event, this.firstChild, index);
  240. }, false);
  241. theWheel.addEventListener('mousemove', function() {
  242. _this.dragClick(event, this.firstChild, index);
  243. }, false);
  244. theWheel.addEventListener('mouseup', function() {
  245. _this.dragClick(event, this.firstChild, index);
  246. }, true);
  247. }
  248. },
  249. addListenerLi: function(sliderIndex) {
  250. var _this = this;
  251. var curWheelLi = _this.slider[sliderIndex].getElementsByTagName('li');
  252. for (var j = 0; j < curWheelLi.length; j++) {
  253. (function(j) {
  254. curWheelLi[j].addEventListener('click', function() {
  255. _this.singleClick(this, j, sliderIndex);
  256. }, false);
  257. })(j);
  258. }
  259. },
  260. checkDataType: function() {
  261. var _this = this;
  262. if (typeof(_this.wheelsData[0].data[0]) == 'object') {
  263. _this.jsonType = true;
  264. }
  265. },
  266. checkCascade: function() {
  267. var _this = this;
  268. if (_this.jsonType) {
  269. var node = _this.wheelsData[0].data;
  270. for (var i = 0; i < node.length; i++) {
  271. if (_this.keyMap.childs in node[i] && node[i][_this.keyMap.childs].length > 0) {
  272. _this.cascade = true;
  273. _this.cascadeJsonData = _this.wheelsData[0].data;
  274. break;
  275. }
  276. }
  277. } else {
  278. _this.cascade = false;
  279. }
  280. },
  281. generateArrData: function(targetArr) {
  282. var tempArr = [];
  283. var keyMap_id = this.keyMap.id;
  284. var keyMap_value = this.keyMap.value;
  285. for (var i = 0; i < targetArr.length; i++) {
  286. var tempObj = {};
  287. tempObj[keyMap_id] = targetArr[i][this.keyMap.id];
  288. tempObj[keyMap_value] = targetArr[i][this.keyMap.value];
  289. tempArr.push(tempObj);
  290. }
  291. return tempArr;
  292. },
  293. initCascade: function() {
  294. var _this = this;
  295. _this.displayJson.push(_this.generateArrData(_this.cascadeJsonData));
  296. if (_this.initPosition.length > 0) {
  297. _this.initDeepCount = 0;
  298. _this.initCheckArrDeep(_this.cascadeJsonData[_this.initPosition[0]]);
  299. } else {
  300. _this.checkArrDeep(_this.cascadeJsonData[0]);
  301. }
  302. _this.reRenderWheels();
  303. },
  304. initCheckArrDeep: function(parent) {
  305. var _this = this;
  306. if (parent) {
  307. if (_this.keyMap.childs in parent && parent[_this.keyMap.childs].length > 0) {
  308. _this.displayJson.push(_this.generateArrData(parent[_this.keyMap.childs]));
  309. _this.initDeepCount++;
  310. var nextNode = parent[_this.keyMap.childs][_this.initPosition[_this.initDeepCount]];
  311. if (nextNode) {
  312. _this.initCheckArrDeep(nextNode);
  313. } else {
  314. _this.checkArrDeep(parent[_this.keyMap.childs][0]);
  315. }
  316. }
  317. }
  318. },
  319. checkArrDeep: function(parent) {
  320. //检测子节点深度 修改 displayJson
  321. var _this = this;
  322. if (parent) {
  323. if (_this.keyMap.childs in parent && parent[_this.keyMap.childs].length > 0) {
  324. _this.displayJson.push(_this.generateArrData(parent[_this.keyMap.childs])); //生成子节点数组
  325. _this.checkArrDeep(parent[_this.keyMap.childs][0]); //检测下一个子节点
  326. }
  327. }
  328. },
  329. checkRange: function(index, posIndexArr) {
  330. var _this = this;
  331. // if (_this.getValue()[0]) {
  332. // $scope.getplacedata(_this.getValue()[0].id);
  333. // }
  334. var deleteNum = _this.displayJson.length - 1 - index;
  335. for (var i = 0; i < deleteNum; i++) {
  336. _this.displayJson.pop(); //修改 displayJson
  337. }
  338. var resultNode;
  339. for (var i = 0; i <= index; i++) {
  340. if (i == 0)
  341. resultNode = _this.cascadeJsonData[posIndexArr[0]];
  342. else {
  343. resultNode = resultNode[_this.keyMap.childs][posIndexArr[i]];
  344. }
  345. }
  346. _this.checkArrDeep(resultNode);
  347. //console.log(_this.displayJson);
  348. _this.reRenderWheels();
  349. _this.fixRowStyle();
  350. _this.setCurDistance(_this.resetPostion(index, posIndexArr));
  351. },
  352. resetPostion: function(index, posIndexArr) {
  353. var _this = this;
  354. var tempPosArr = posIndexArr;
  355. var tempCount;
  356. if (_this.slider.length > posIndexArr.length) {
  357. tempCount = _this.slider.length - posIndexArr.length;
  358. for (var i = 0; i < tempCount; i++) {
  359. tempPosArr.push(0);
  360. }
  361. } else if (_this.slider.length < posIndexArr.length) {
  362. tempCount = posIndexArr.length - _this.slider.length;
  363. for (var i = 0; i < tempCount; i++) {
  364. tempPosArr.pop();
  365. }
  366. }
  367. for (var i = index + 1; i < tempPosArr.length; i++) {
  368. tempPosArr[i] = 0;
  369. }
  370. return tempPosArr;
  371. },
  372. reRenderWheels: function() {
  373. var _this = this;
  374. // $scope.getplacedata(_this.getValue()[0].id);
  375. //删除多余的wheel
  376. if (_this.wheel.length > _this.displayJson.length) {
  377. var count = _this.wheel.length - _this.displayJson.length;
  378. for (var i = 0; i < count; i++) {
  379. _this.wheels.removeChild(_this.wheel[_this.wheel.length - 1]);
  380. }
  381. }
  382. for (var i = 0; i < _this.displayJson.length; i++) {
  383. //列
  384. (function(i) {
  385. var tempHTML = '';
  386. if (_this.wheel[i]) {
  387. //console.log('插入Li');
  388. for (var j = 0; j < _this.displayJson[i].length; j++) {
  389. //行
  390. tempHTML += '<li data-id="' + _this.displayJson[i][j][_this.keyMap.id] + '">' + _this.displayJson[i][j][_this.keyMap.value] + '</li>';
  391. }
  392. _this.slider[i].innerHTML = tempHTML;
  393. } else {
  394. var tempWheel = document.createElement("div");
  395. tempWheel.className = "wheel";
  396. tempHTML = '<ul class="selectContainer">';
  397. for (var j = 0; j < _this.displayJson[i].length; j++) {
  398. //行
  399. tempHTML += '<li data-id="' + _this.displayJson[i][j][_this.keyMap.id] + '">' + _this.displayJson[i][j][_this.keyMap.value] + '</li>';
  400. }
  401. tempHTML += '</ul>';
  402. tempWheel.innerHTML = tempHTML;
  403. _this.addListenerWheel(tempWheel, i);
  404. _this.wheels.appendChild(tempWheel);
  405. }
  406. _this.addListenerLi(i);
  407. })(i);
  408. }
  409. },
  410. updateWheels: function(data) {
  411. var _this = this;
  412. if (_this.cascade) {
  413. _this.cascadeJsonData = data;
  414. _this.displayJson = [];
  415. _this.initCascade();
  416. if (_this.initPosition.length < _this.slider.length) {
  417. var diff = _this.slider.length - _this.initPosition.length;
  418. for (var i = 0; i < diff; i++) {
  419. _this.initPosition.push(0);
  420. }
  421. }
  422. _this.setCurDistance(_this.initPosition);
  423. _this.fixRowStyle();
  424. }
  425. },
  426. updateWheel: function(sliderIndex, data) {
  427. var _this = this;
  428. var tempHTML = '';
  429. if (_this.cascade) {
  430. console.error('级联格式不支持updateWheel(),请使用updateWheels()更新整个数据源');
  431. return false;
  432. } else if (_this.jsonType) {
  433. for (var j = 0; j < data.length; j++) {
  434. tempHTML += '<li data-id="' + data[j][_this.keyMap.id] + '">' + data[j][_this.keyMap.value] + '</li>';
  435. }
  436. _this.wheelsData[sliderIndex] = { data: data };
  437. } else {
  438. for (var j = 0; j < data.length; j++) {
  439. tempHTML += '<li>' + data[j] + '</li>';
  440. }
  441. _this.wheelsData[sliderIndex] = data;
  442. }
  443. _this.slider[sliderIndex].innerHTML = tempHTML;
  444. _this.addListenerLi(sliderIndex);
  445. },
  446. fixRowStyle: function() {
  447. var _this = this;
  448. var width = (100 / _this.wheel.length).toFixed(2);
  449. for (var i = 0; i < _this.wheel.length; i++) {
  450. _this.wheel[i].style.width = width + '%';
  451. }
  452. },
  453. getIndex: function(distance) {
  454. return Math.round((2 * this.liHeight - distance) / this.liHeight);
  455. },
  456. getIndexArr: function() {
  457. var _this = this;
  458. var temp = [];
  459. for (var i = 0; i < _this.curDistance.length; i++) {
  460. temp.push(_this.getIndex(_this.curDistance[i]));
  461. }
  462. return temp;
  463. },
  464. getValue: function() {
  465. var _this = this;
  466. var temp = [];
  467. var positionArr = _this.getIndexArr();
  468. if (_this.cascade) {
  469. for (var i = 0; i < _this.wheel.length; i++) {
  470. temp.push(_this.displayJson[i][positionArr[i]]);
  471. }
  472. } else if (_this.jsonType) {
  473. for (var i = 0; i < _this.curDistance.length; i++) {
  474. temp.push(_this.wheelsData[i].data[_this.getIndex(_this.curDistance[i])]);
  475. }
  476. } else {
  477. for (var i = 0; i < _this.curDistance.length; i++) {
  478. temp.push(_this.getInnerHtml(i));
  479. }
  480. }
  481. return temp;
  482. },
  483. calcDistance: function(index) {
  484. return 2 * this.liHeight - index * this.liHeight;
  485. },
  486. setCurDistance: function(indexArr) {
  487. var _this = this;
  488. var temp = [];
  489. for (var i = 0; i < _this.slider.length; i++) {
  490. temp.push(_this.calcDistance(indexArr[i]));
  491. _this.movePosition(_this.slider[i], temp[i]);
  492. }
  493. _this.curDistance = temp;
  494. },
  495. fixPosition: function(distance) {
  496. return -(this.getIndex(distance) - 2) * this.liHeight;
  497. },
  498. movePosition: function(theSlider, distance) {
  499. theSlider.style.webkitTransform = 'translate3d(0,' + distance + 'px, 0)';
  500. theSlider.style.transform = 'translate3d(0,' + distance + 'px, 0)';
  501. },
  502. locatePostion: function(index, posIndex) {
  503. this.curDistance[index] = this.calcDistance(posIndex);
  504. this.movePosition(this.slider[index], this.curDistance[index]);
  505. },
  506. updateCurDistance: function(theSlider, index) {
  507. this.curDistance[index] = parseInt(theSlider.style.transform.split(',')[1]);
  508. },
  509. getDistance: function(theSlider) {
  510. return parseInt(theSlider.style.transform.split(',')[1]);
  511. },
  512. getInnerHtml: function(sliderIndex) {
  513. var _this = this;
  514. var index = _this.getIndex(_this.curDistance[sliderIndex]);
  515. return _this.slider[sliderIndex].getElementsByTagName('li')[index].innerHTML;
  516. },
  517. touch: function(event, theSlider, index) {
  518. var _this = this;
  519. event = event || window.event;
  520. switch (event.type) {
  521. case "touchstart":
  522. _this.startY = event.touches[0].clientY;
  523. _this.oldMoveY = _this.startY;
  524. break;
  525. case "touchend":
  526. _this.moveEndY = event.changedTouches[0].clientY;
  527. _this.offsetSum = _this.moveEndY - _this.startY;
  528. //修正位置
  529. _this.updateCurDistance(theSlider, index);
  530. _this.curDistance[index] = _this.fixPosition(_this.curDistance[index]);
  531. _this.movePosition(theSlider, _this.curDistance[index]);
  532. _this.oversizeBorder = -(theSlider.getElementsByTagName('li').length - 3) * _this.liHeight;
  533. //反弹
  534. if (_this.curDistance[index] + _this.offsetSum > 2 * _this.liHeight) {
  535. _this.curDistance[index] = 2 * _this.liHeight;
  536. setTimeout(function() {
  537. _this.movePosition(theSlider, _this.curDistance[index]);
  538. }, 100);
  539. } else if (_this.curDistance[index] + _this.offsetSum < _this.oversizeBorder) {
  540. _this.curDistance[index] = _this.oversizeBorder;
  541. setTimeout(function() {
  542. _this.movePosition(theSlider, _this.curDistance[index]);
  543. }, 100);
  544. }
  545. _this.transitionEnd(_this.getIndexArr(), _this.getValue());
  546. if (_this.cascade) {
  547. var tempPosArr = _this.getIndexArr();
  548. tempPosArr[index] = _this.getIndex(_this.curDistance[index]);
  549. _this.checkRange(index, tempPosArr);
  550. }
  551. break;
  552. case "touchmove":
  553. event.preventDefault();
  554. _this.moveY = event.touches[0].clientY;
  555. _this.offset = _this.moveY - _this.oldMoveY;
  556. _this.updateCurDistance(theSlider, index);
  557. _this.curDistance[index] = _this.curDistance[index] + _this.offset;
  558. _this.movePosition(theSlider, _this.curDistance[index]);
  559. _this.oldMoveY = _this.moveY;
  560. break;
  561. }
  562. },
  563. dragClick: function(event, theSlider, index) {
  564. var _this = this;
  565. event = event || window.event;
  566. switch (event.type) {
  567. case "mousedown":
  568. _this.startY = event.clientY;
  569. _this.oldMoveY = _this.startY;
  570. _this.clickStatus = true;
  571. break;
  572. case "mouseup":
  573. _this.moveEndY = event.clientY;
  574. _this.offsetSum = _this.moveEndY - _this.startY;
  575. //修正位置
  576. _this.updateCurDistance(theSlider, index);
  577. _this.curDistance[index] = _this.fixPosition(_this.curDistance[index]);
  578. _this.movePosition(theSlider, _this.curDistance[index]);
  579. _this.oversizeBorder = -(theSlider.getElementsByTagName('li').length - 3) * _this.liHeight;
  580. //反弹
  581. if (_this.curDistance[index] + _this.offsetSum > 2 * _this.liHeight) {
  582. _this.curDistance[index] = 2 * _this.liHeight;
  583. setTimeout(function() {
  584. _this.movePosition(theSlider, _this.curDistance[index]);
  585. }, 100);
  586. } else if (_this.curDistance[index] + _this.offsetSum < _this.oversizeBorder) {
  587. _this.curDistance[index] = _this.oversizeBorder;
  588. setTimeout(function() {
  589. _this.movePosition(theSlider, _this.curDistance[index]);
  590. }, 100);
  591. }
  592. _this.clickStatus = false;
  593. _this.transitionEnd(_this.getIndexArr(), _this.getValue());
  594. if (_this.cascade) {
  595. var tempPosArr = _this.getIndexArr();
  596. tempPosArr[index] = _this.getIndex(_this.curDistance[index]);
  597. _this.checkRange(index, tempPosArr);
  598. }
  599. break;
  600. case "mousemove":
  601. event.preventDefault();
  602. if (_this.clickStatus) {
  603. _this.moveY = event.clientY;
  604. _this.offset = _this.moveY - _this.oldMoveY;
  605. _this.updateCurDistance(theSlider, index);
  606. _this.curDistance[index] = _this.curDistance[index] + _this.offset;
  607. _this.movePosition(theSlider, _this.curDistance[index]);
  608. _this.oldMoveY = _this.moveY;
  609. }
  610. break;
  611. }
  612. },
  613. singleClick: function(theLi, index, sliderIndex) {
  614. var _this = this;
  615. if (_this.cascade) {
  616. var tempPosArr = _this.getIndexArr();
  617. tempPosArr[sliderIndex] = index;
  618. _this.checkRange(sliderIndex, tempPosArr);
  619. } else {
  620. _this.curDistance[sliderIndex] = (2 - index) * _this.liHeight;
  621. _this.movePosition(theLi.parentNode, _this.curDistance[sliderIndex]);
  622. }
  623. }
  624. };
  625. $scope.gerdirect = function(MobileSelectdata) {
  626. if (typeof exports == "object") {
  627. module.exports = MobileSelect;
  628. } else if (typeof define == "function" && define.amd) {
  629. define([], function() {
  630. // $scope.getareadata();
  631. // $scope.getplacedata(1);
  632. return MobileSelect(MobileSelectdata);
  633. })
  634. } else {
  635. // $scope.getareadata();
  636. // $scope.getplacedata(1);
  637. window.MobileSelect = MobileSelect(MobileSelectdata);
  638. }
  639. }
  640. }]
  641. }
  642. })