mui.poppicker.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. colNameElement = pickerElement.querySelector('.mui-picker-col-name');
  90. colNameElement.innerText = colNames[i-1]||"";
  91. self.body.appendChild(pickerElement);
  92. var picker = $(pickerElement).picker();
  93. self.pickers.push(picker);
  94. pickerElement.addEventListener('change', function(event) {
  95. var nextPickerElement = this.nextSibling;
  96. if (nextPickerElement && nextPickerElement.picker) {
  97. var eventData = event.detail || {};
  98. var preItem = eventData.item || {};
  99. nextPickerElement.picker.setItems(preItem.children);
  100. }
  101. }, false);
  102. }
  103. },
  104. //填充数据
  105. setData: function(data) {
  106. var self = this;
  107. data = data || [];
  108. self.pickers[0].setItems(data);
  109. },
  110. //获取选中的项(数组)
  111. getSelectedItems: function() {
  112. var self = this;
  113. var items = [];
  114. for (var i in self.pickers) {
  115. var picker = self.pickers[i];
  116. items.push(picker.getSelectedItem() || {});
  117. }
  118. return items;
  119. },
  120. //显示
  121. show: function(callback) {
  122. var self = this;
  123. self.callback = callback;
  124. self.mask.show();
  125. document.body.classList.add($.className('poppicker-active-for-page'));
  126. self.panel.classList.add($.className('active'));
  127. //处理物理返回键
  128. self.__back = $.back;
  129. $.back = function() {
  130. self.hide();
  131. };
  132. },
  133. //隐藏
  134. hide: function() {
  135. var self = this;
  136. if (self.disposed) return;
  137. self.panel.classList.remove($.className('active'));
  138. self.mask.close();
  139. document.body.classList.remove($.className('poppicker-active-for-page'));
  140. //处理物理返回键
  141. $.back=self.__back;
  142. },
  143. dispose: function() {
  144. var self = this;
  145. self.hide();
  146. setTimeout(function() {
  147. self.panel.parentNode.removeChild(self.panel);
  148. for (var name in self) {
  149. self[name] = null;
  150. delete self[name];
  151. };
  152. self.disposed = true;
  153. }, 300);
  154. }
  155. });
  156. })(mui, document);