mui.poppicker.js 4.5 KB

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