a400e7c124ed9ecfead5e74293d50450c5367432.svn-base 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. /*
  2. * Created with Sublime Text 2.
  3. * license: http://www.lovewebgames.com/jsmodule/index.html
  4. * User: 田想兵
  5. * Date: 2015-03-31
  6. * Time: 09:49:11
  7. * Contact: 55342775@qq.com
  8. */
  9. ;
  10. (function(root, factory) {
  11. //amd
  12. if (typeof define === 'function' && define.amd) {
  13. define(['$', 'dialog'], factory);
  14. }else if (typeof define === 'function' && define.cmd) {
  15. define(function(require, exports, module) {
  16. var $ = require("$");
  17. var Dialog=require("dialog");
  18. return factory($,Dialog);
  19. });
  20. } else if (typeof exports === 'object') { //umd
  21. module.exports = factory();
  22. } else {
  23. root.MobileSelectArea = factory(window.Zepto || window.jQuery || $);
  24. }
  25. })(this, function($, Dialog) {
  26. var MobileSelectArea = function() {
  27. var rnd = Math.random().toString().replace('.', '');
  28. this.id = 'scroller_' + rnd;
  29. this.scroller;
  30. this.data;
  31. this.index = 0;
  32. this.value = [0, 0, 0];
  33. this.oldvalue;
  34. this.oldtext=[];
  35. this.text = ['', '', ''];
  36. this.level = 3;
  37. this.mtop = 30;
  38. this.separator = ' ';
  39. };
  40. MobileSelectArea.prototype = {
  41. init: function(settings) {
  42. this.settings = $.extend({
  43. eventName: 'click'
  44. }, settings);
  45. this.trigger = $(this.settings.trigger);
  46. this.settings.default == undefined ? this.default = 1 : this.default = 0; //0为空,1时默认选中第一项
  47. level = parseInt(this.settings.level);
  48. this.level = level > 0 ? level : 3;
  49. this.trigger.attr("readonly", "readonly");
  50. this.value = (this.settings.value && this.settings.value.split(",")) || [0, 0, 0];
  51. this.text = this.settings.text || this.trigger.val().split(' ') || ['', '', ''];
  52. this.oldvalue = this.value.concat([]);
  53. this.oldtext = this.text.concat([]);
  54. this.clientHeight = document.documentElement.clientHeight || document.body.clientHeight;
  55. this.clientWidth = document.documentElement.clientWidth || document.body.clientWidth;
  56. // this.promise = this.getData();
  57. this.bindEvent();
  58. },
  59. getData: function() {
  60. var _this = this;
  61. var dtd = $.Deferred();
  62. if (typeof this.settings.data == "object") {
  63. this.data = this.settings.data;
  64. dtd.resolve();
  65. } else {
  66. $.ajax({
  67. dataType: 'json',
  68. cache: true,
  69. url: this.settings.data,
  70. type: 'GET',
  71. success: function(result) {
  72. _this.data = result.data;
  73. dtd.resolve();
  74. },
  75. accepts: {
  76. json: "application/json, text/javascript, */*; q=0.01"
  77. }
  78. });
  79. }
  80. return dtd;
  81. },
  82. bindEvent: function() {
  83. var _this = this;
  84. this.trigger[_this.settings.eventName](function(e) {
  85. var dlgContent = '';
  86. for (var i = 0; i < _this.level; i++) {
  87. dlgContent += '<div></div>';
  88. };
  89. $.confirm('<div class="ui-scroller-mask"><div id="' + _this.id + '" class="ui-scroller">' + dlgContent + '<p></p></div></div>', null, function(t, c) {
  90. if (t == "yes") {
  91. _this.submit()
  92. }
  93. if (t == 'no') {
  94. _this.cancel();
  95. }
  96. this.dispose();
  97. }, {
  98. width: 320,
  99. height: 215
  100. });
  101. _this.scroller = $('#' + _this.id);
  102. _this.getData().done(function() {
  103. _this.format();
  104. });
  105. var start = 0,
  106. end = 0
  107. _this.scroller.children().bind('touchstart', function(e) {
  108. start = (e.changedTouches || e.originalEvent.changedTouches)[0].pageY;
  109. });
  110. _this.scroller.children().bind('touchmove', function(e) {
  111. end = (e.changedTouches || e.originalEvent.changedTouches)[0].pageY;
  112. var diff = end - start;
  113. var dl = $(e.target).parent();
  114. if (dl[0].nodeName != "DL") {
  115. return;
  116. }
  117. var top = parseInt(dl.css('top') || 0) + diff;
  118. dl.css('top', top);
  119. start = end;
  120. return false;
  121. });
  122. _this.scroller.children().bind('touchend', function(e) {
  123. end = (e.changedTouches || e.originalEvent.changedTouches)[0].pageY;
  124. var diff = end - start;
  125. var dl = $(e.target).parent();
  126. if (dl[0].nodeName != "DL") {
  127. return;
  128. }
  129. var i = $(dl.parent()).index();
  130. var top = parseInt(dl.css('top') || 0) + diff;
  131. if (top > _this.mtop) {
  132. top = _this.mtop;
  133. }
  134. if (top < -$(dl).height() + 60) {
  135. top = -$(dl).height() + 60;
  136. }
  137. var mod = top / _this.mtop;
  138. var mode = Math.round(mod);
  139. var index = Math.abs(mode) + 1;
  140. if (mode == 1) {
  141. index = 0;
  142. }
  143. _this.value[i] = $(dl.children().get(index)).attr('ref');
  144. _this.value[i] == 0 ? _this.text[i] = "" : _this.text[i] = $(dl.children().get(index)).html();
  145. for (var j = _this.level - 1; j > i; j--) {
  146. _this.value[j] = 0;
  147. _this.text[j] = "";
  148. }
  149. if (!$(dl.children().get(index)).hasClass('focus')) {
  150. _this.format();
  151. }
  152. $(dl.children().get(index)).addClass('focus').siblings().removeClass('focus');
  153. dl.css('top', mode * _this.mtop);
  154. return false;
  155. });
  156. return false;
  157. });
  158. },
  159. format: function() {
  160. var _this = this;
  161. var child = _this.scroller.children();
  162. this.f(this.data);
  163. // console.log(_this.text)
  164. },
  165. f: function(data) {
  166. var _this = this;
  167. var item = data;
  168. if (!item) {
  169. item = [];
  170. };
  171. var str = '<dl><dd ref="0">——</dd>';
  172. var focus = 0,
  173. childData, top = _this.mtop;
  174. if (_this.index !== 0 && _this.value[_this.index - 1] == "0" && this.default == 0) {
  175. str = '<dl><dd ref="0" class="focus">——</dd>';
  176. _this.value[_this.index] = 0;
  177. _this.text[_this.index] = "";
  178. focus = 0;
  179. } else {
  180. if (_this.value[_this.index] == "0") {
  181. str = '<dl><dd ref="0" class="focus">——</dd>';
  182. focus = 0;
  183. }
  184. if (item.length > 0 && this.default == 1) {
  185. str = '<dl>';
  186. var pid = item[0].pid || 0;
  187. var id = item[0].id || 0;
  188. focus = item[0].id;
  189. childData = item[0].child;
  190. if (!_this.value[this.index]) {
  191. _this.value[this.index] = id;
  192. _this.text[this.index] = item[0].name;
  193. }
  194. str += '<dd pid="' + pid + '" class="' + cls + '" ref="' + id + '">' + item[0].name + '</dd>';
  195. }
  196. for (var j = _this.default, len = item.length; j < len; j++) {
  197. var pid = item[j].pid || 0;
  198. var id = item[j].id || 0;
  199. var cls = '';
  200. if (_this.value[_this.index] == id) {
  201. cls = "focus";
  202. focus = id;
  203. childData = item[j].child;
  204. top = _this.mtop * (-(j - _this.default));
  205. };
  206. str += '<dd pid="' + pid + '" class="' + cls + '" ref="' + id + '">' + item[j].name + '</dd>';
  207. }
  208. }
  209. str += "</dl>";
  210. var newdom = $(str);
  211. newdom.css('top', top);
  212. var child = _this.scroller.children();
  213. $(child[_this.index]).html(newdom);
  214. _this.index++;
  215. if (_this.index > _this.level - 1) {
  216. _this.index = 0;
  217. return;
  218. }
  219. _this.f(childData);
  220. },
  221. submit: function() {
  222. this.oldvalue = this.value.concat([]);
  223. this.oldtext = this.text.concat([]);
  224. if (this.trigger[0].nodeType == 1) {
  225. //input
  226. this.trigger.val(this.text.join(this.separator));
  227. this.trigger.attr('data-value', this.value.join(','));
  228. }
  229. this.trigger.next(':hidden').val(this.value.join(','));
  230. this.settings.callback && this.settings.callback.call(this, this.scroller, this.text, this.value);
  231. },
  232. cancel: function() {
  233. this.value = this.oldvalue.concat([]);
  234. this.text = this.oldtext.concat([]);
  235. }
  236. };
  237. return MobileSelectArea;
  238. });