No Description

mui.poppicker.js 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /**
  2. * 弹出选择列表插件
  3. * 此组件依赖 listpcker ,请在页面中先引入 mui.picker.css + mui.picker.js
  4. * varstion 1.0.1
  5. * by Houfeng
  6. * Houfeng@DCloud.io
  7. */
  8. (function($, document) {
  9. //创建 DOM
  10. $.dom = function(str) {
  11. if (typeof(str) !== 'string') {
  12. if ((str instanceof Array) || (str[0] && str.length)) {
  13. return [].slice.call(str);
  14. } else {
  15. return [str];
  16. }
  17. }
  18. if (!$.__create_dom_div__) {
  19. $.__create_dom_div__ = document.createElement('div');
  20. }
  21. $.__create_dom_div__.innerHTML = str;
  22. return [].slice.call($.__create_dom_div__.childNodes);
  23. };
  24. var panelBuffer = '<div class="mui-poppicker">\
  25. <div class="mui-poppicker-header">\
  26. <div class="mui-contain">\
  27. <div class="mui-btn">\
  28. <button class="mui-btn mui-icon mui-icon-arrowup"></button>\
  29. <button class="mui-btn mui-icon mui-icon-arrowdown"></button>\
  30. </div>\
  31. <div id="mui-title1">111</div>\
  32. <button class="mui-btn mui-btn-blue mui-poppicker-btn-ok">确定</button>\
  33. </div>\
  34. </div>\
  35. <div class="mui-poppicker-body">\
  36. </div>\
  37. </div>';
  38. var pickerBuffer = '<div class="mui-picker">\
  39. <div class="mui-picker-inner">\
  40. <div class="mui-pciker-rule mui-pciker-rule-ft" id="picker1"></div>\
  41. <ul class="mui-pciker-list">\
  42. </ul>\
  43. <div class="mui-pciker-rule mui-pciker-rule-bg"></div>\
  44. </div>\
  45. </div>';
  46. //定义弹出选择器类
  47. var PopPicker = $.PopPicker = $.Class.extend({
  48. data:[],
  49. //构造函数
  50. init: function(options) {
  51. var self = this;
  52. self.options = options || {};
  53. self.options.buttons = self.options.buttons || ['取消', '确定'];
  54. self.options.title = self.options.title || '';
  55. self.panel = $.dom(panelBuffer)[0];
  56. document.body.appendChild(self.panel);
  57. self.ok = self.panel.querySelector('.mui-poppicker-btn-ok');
  58. // 上翻
  59. self.arrowup = self.panel.querySelector('.mui-icon-arrowup');
  60. // 下翻
  61. self.arrowdown = self.panel.querySelector('.mui-icon-arrowdown');
  62. self.body = self.panel.querySelector('.mui-poppicker-body');
  63. self.mask = $.createMask();
  64. self.ok.innerText = self.options.buttons[1];
  65. self.arrowdown.addEventListener('tap',function(event) {
  66. var index1=self.pickers[0].getSelectedIndex()
  67. if(index1<self.data.length-1) {
  68. self.pickers[0].setSelectedIndex(index1+1,1000);
  69. }
  70. })
  71. self.arrowup.addEventListener('tap', function(event) {
  72. var index2=self.pickers[0].getSelectedIndex()
  73. if(index2>0) {
  74. self.pickers[0].setSelectedIndex(index2-1,1000);
  75. }
  76. }, false);
  77. self.ok.addEventListener('tap', function(event) {
  78. if (self.callback) {
  79. var rs = self.callback(self.getSelectedItems());
  80. if (rs !== false) {
  81. self.hide();
  82. }
  83. }
  84. }, false);
  85. self.mask[0].addEventListener('tap', function() {
  86. self.hide();
  87. }, false);
  88. self._createPicker();
  89. //防止滚动穿透
  90. self.panel.addEventListener('touchstart', function(event) {
  91. event.preventDefault();
  92. }, false);
  93. self.panel.addEventListener('touchmove', function(event) {
  94. event.preventDefault();
  95. }, false);
  96. },
  97. _createPicker: function() {
  98. var self = this;
  99. var layer = self.options.layer || 1;
  100. var width = (100 / layer) + '%';
  101. self.pickers = [];
  102. for (var i = 1; i <= layer; i++) {
  103. var pickerElement = $.dom(pickerBuffer)[0];
  104. pickerElement.style.width = width;
  105. self.body.appendChild(pickerElement);
  106. var picker = $(pickerElement).picker();
  107. self.pickers.push(picker);
  108. pickerElement.addEventListener('change', function(event) {
  109. var nextPickerElement = this.nextSibling;
  110. if (nextPickerElement && nextPickerElement.picker) {
  111. var eventData = event.detail || {};
  112. var preItem = eventData.item || {};
  113. nextPickerElement.picker.setItems(preItem.children);
  114. }
  115. }, false);
  116. }
  117. },
  118. //填充数据
  119. setData: function(data,title) {
  120. var self = this;
  121. data = data || [];
  122. self.pickers[0].setItems(data);
  123. this.data=data
  124. self.panel.querySelector("#mui-title1").innerText=title
  125. },
  126. //获取选中的项(数组)
  127. getSelectedItems: function() {
  128. var self = this;
  129. var items = [];
  130. for (var i in self.pickers) {
  131. var picker = self.pickers[i];
  132. items.push(picker.getSelectedItem() || {});
  133. items.index=i
  134. }
  135. return items;
  136. },
  137. //显示
  138. show: function(callback) {
  139. var self = this;
  140. self.callback = callback;
  141. self.mask.show();
  142. document.body.classList.add($.className('poppicker-active-for-page'));
  143. self.panel.classList.add($.className('active'));
  144. //处理物理返回键
  145. self.__back = $.back;
  146. $.back = function() {
  147. self.hide();
  148. };
  149. },
  150. //隐藏
  151. hide: function() {
  152. var self = this;
  153. if (self.disposed) return;
  154. self.panel.classList.remove($.className('active'));
  155. self.mask.close();
  156. document.body.classList.remove($.className('poppicker-active-for-page'));
  157. //处理物理返回键
  158. $.back=self.__back;
  159. },
  160. dispose: function() {
  161. var self = this;
  162. self.hide();
  163. setTimeout(function() {
  164. self.panel.parentNode.removeChild(self.panel);
  165. for (var name in self) {
  166. self[name] = null;
  167. delete self[name];
  168. };
  169. self.disposed = true;
  170. }, 300);
  171. }
  172. });
  173. })(mui, document);