mui.poppicker.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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. <button class="mui-btn mui-poppicker-btn-cancel">取消</button>\
  27. <button class="mui-btn mui-btn-blue mui-poppicker-btn-ok">确定</button>\
  28. <div class="mui-poppicker-clear"></div>\
  29. </div>\
  30. <div class="mui-poppicker-body">\
  31. </div>\
  32. </div>';
  33. var pickerBuffer = '<div class="mui-picker">\
  34. <div class="mui-picker-col-name"></div>\
  35. <div class="mui-picker-inner">\
  36. <div class="mui-pciker-rule mui-pciker-rule-ft"></div>\
  37. <ul class="mui-pciker-list">\
  38. </ul>\
  39. <div class="mui-pciker-rule mui-pciker-rule-bg"></div>\
  40. </div>\
  41. </div>';
  42. //定义弹出选择器类
  43. var PopPicker = $.PopPicker = $.Class.extend({
  44. //构造函数
  45. init: function(options) {
  46. var self = this;
  47. self.options = options || {};
  48. self.options.buttons = self.options.buttons || ['取消', '确定'];
  49. self.panel = $.dom(panelBuffer)[0];
  50. document.body.appendChild(self.panel);
  51. self.ok = self.panel.querySelector('.mui-poppicker-btn-ok');
  52. self.cancel = self.panel.querySelector('.mui-poppicker-btn-cancel');
  53. self.body = self.panel.querySelector('.mui-poppicker-body');
  54. self.mask = $.createMask();
  55. self.cancel.innerText = self.options.buttons[0];
  56. self.ok.innerText = self.options.buttons[1];
  57. self.cancel.addEventListener('tap', function(event) {
  58. self.hide();
  59. }, false);
  60. self.ok.addEventListener('tap', function(event) {
  61. if (self.callback) {
  62. var rs = self.callback(self.getSelectedItems());
  63. if (rs !== false) {
  64. self.hide();
  65. }
  66. }
  67. }, false);
  68. self.mask[0].addEventListener('tap', function() {
  69. self.hide();
  70. }, false);
  71. self._createPicker();
  72. //防止滚动穿透
  73. self.panel.addEventListener('touchstart', function(event) {
  74. event.preventDefault();
  75. }, false);
  76. self.panel.addEventListener('touchmove', function(event) {
  77. event.preventDefault();
  78. }, false);
  79. },
  80. _createPicker: function() {
  81. var self = this;
  82. var layer = self.options.layer || 1;
  83. var width = (100 / layer) + '%';
  84. var colNames = self.options.layerNames||[];
  85. self.pickers = [];
  86. for (var i = 1; i <= layer; i++) {
  87. var pickerElement = $.dom(pickerBuffer)[0];
  88. pickerElement.style.width = width;
  89. var colNameElement = pickerElement.querySelector('.mui-picker-col-name');
  90. if(layer>1) {
  91. colNameElement.innerText = colNames[i-1]||"";
  92. } else {
  93. colNameElement.style.display = "none";
  94. }
  95. self.body.appendChild(pickerElement);
  96. var picker = $(pickerElement).picker();
  97. self.pickers.push(picker);
  98. pickerElement.addEventListener('change', function(event) {
  99. var nextPickerElement = this.nextSibling;
  100. if (nextPickerElement && nextPickerElement.picker) {
  101. var eventData = event.detail || {};
  102. var preItem = eventData.item || {};
  103. nextPickerElement.picker.setItems(preItem.children);
  104. }
  105. }, false);
  106. }
  107. },
  108. //填充数据
  109. setData: function(data) {
  110. var self = this;
  111. data = data || [];
  112. self.pickers[0].setItems(data);
  113. },
  114. //获取选中的项(数组)
  115. getSelectedItems: function() {
  116. var self = this;
  117. var items = [];
  118. for (var i in self.pickers) {
  119. var picker = self.pickers[i];
  120. items.push(picker.getSelectedItem() || {});
  121. }
  122. return items;
  123. },
  124. //显示
  125. show: function(callback) {
  126. var self = this;
  127. self.callback = callback;
  128. self.mask.show();
  129. document.body.classList.add($.className('poppicker-active-for-page'));
  130. self.panel.classList.add($.className('active'));
  131. //处理物理返回键
  132. self.__back = $.back;
  133. $.back = function() {
  134. self.hide();
  135. };
  136. },
  137. //隐藏
  138. hide: function() {
  139. var self = this;
  140. if (self.disposed) return;
  141. self.panel.classList.remove($.className('active'));
  142. self.mask.close();
  143. document.body.classList.remove($.className('poppicker-active-for-page'));
  144. //处理物理返回键
  145. $.back=self.__back;
  146. },
  147. dispose: function() {
  148. var self = this;
  149. self.hide();
  150. setTimeout(function() {
  151. self.panel.parentNode.removeChild(self.panel);
  152. for (var name in self) {
  153. self[name] = null;
  154. delete self[name];
  155. };
  156. self.disposed = true;
  157. }, 300);
  158. }
  159. });
  160. })(mui, document);