l-picker.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. $(function(){
  2. var html =
  3. '<div id="datePick" class="l-time-sel-box">' +
  4. '<ul>' +
  5. '<li class="l-over">全部</li>' +
  6. '<li>最近一周</li>' +
  7. '<li>最近一月</li>' +
  8. // '<li>过去365天</li>' +
  9. '</ul>' +
  10. '<div class="l-time-sel-custom">' +
  11. '自定义' +
  12. '<div style="float: right; margin-top: .5rem;">' +
  13. '<div id="lpSwitch" class="mui-switch mui-switch-mini">' +
  14. '<div class="mui-switch-handle"></div>' +
  15. '</div>' +
  16. '</div>' +
  17. '</div>' +
  18. '<div class="l-date-form">' +
  19. '<div class="l-date-start">' +
  20. '开始于:<br>' +
  21. '<input id="l-p-from" readonly="readonly" type="text" class="mui-input-clear" >' +
  22. '</div>' +
  23. '<div class="l-date-end">' +
  24. '结束于:<br>' +
  25. '<input id="l-p-to" readonly="readonly" type="text" class="mui-input-clear" >' +
  26. '</div>' +
  27. '</div>' +
  28. '<div class="l-btn-arr">' +
  29. '<div class="l-btn-cancel">取消</div>' +
  30. '<div class="l-btn-ok">确定</div>' +
  31. '</div>' +
  32. '</div>';
  33. var options = {
  34. _el: undefined,
  35. mask: undefined,
  36. type: 1, //类型 1选项, 2自定义
  37. onCheck: undefined,
  38. old: {type: 1, val: 0} //就值, 用于还原
  39. };
  40. function Lpicker(){
  41. var g= this;
  42. g.options = {};
  43. }
  44. Lpicker.prototype.toggle = function(){
  45. var g=this, p=g.options;
  46. if(p.mask){
  47. g.close();
  48. }else
  49. g.show();
  50. }
  51. Lpicker.prototype.close = function(t){
  52. var g=this, p=g.options;
  53. p._el.hide();
  54. if(!t)
  55. p.mask.close();
  56. p.mask = undefined;
  57. }
  58. Lpicker.prototype.show = function(){
  59. var g=this, p=g.options;
  60. // if(!p.mask){
  61. p.mask = mui.createMask(function(){
  62. g.reset();
  63. g.close(1);
  64. });
  65. p.mask.show();
  66. p._el.show();
  67. // }
  68. }
  69. function getPreDate(pre){
  70. var c = new Date();
  71. c.setDate(c.getDate() - pre);
  72. return formatDate(c);
  73. }
  74. function formatDate(d){
  75. return d.getFullYear() + "-" + getMonth(d.getMonth()) + "-" + getDay(d.getDate());
  76. }
  77. Lpicker.prototype.getValue = function(){
  78. var g=this, p=g.options;
  79. var from, to;
  80. if(p.type == 1){
  81. var i =$('.l-time-sel-box li.l-over', p._el).index() + 1;
  82. switch(i){
  83. case 1: from = ""; break;
  84. case 2: from = getPreDate(6); break;
  85. case 3: from = getPreDate(29); break;
  86. }
  87. to = getPreDate(0);
  88. }
  89. else{
  90. from = $("#l-p-from").val();
  91. to = $("#l-p-to").val();
  92. }
  93. return {from: from, to: to};
  94. }
  95. Lpicker.prototype.getChecked = function(){
  96. var g=this, p=g.options;
  97. return $('.l-time-sel-box li.l-over', p._el).index();
  98. }
  99. /**
  100. * 设值
  101. * @param {Object} from 可是开始时间 或选择类型
  102. * @param {Object} to
  103. */
  104. Lpicker.prototype.setValue = function(from, to){
  105. var g=this, p=g.options;
  106. if(to){
  107. $('#lpSwitch').addClass('mui-active');
  108. mui('#lpSwitch')['switch']();
  109. g.selectCustom({from: from, to: to});
  110. p.old = {type: 2, val: {from: from, to: to}};
  111. } else {
  112. g.selectItem($('.l-time-sel-box li', p._el).eq(from));
  113. p.old = {type: 1, val: from};
  114. }
  115. }
  116. Lpicker.prototype.reset = function(){
  117. var g=this, p=g.options;
  118. var old = p.old;
  119. if(!old)
  120. return;
  121. if(old.type == 1){
  122. g.selectItem($('.l-time-sel-box li', p._el).eq(old.val));
  123. }else{
  124. $('#lpSwitch').empty().append('<div class="mui-switch-handle"></div>').addClass('mui-active');
  125. mui('#lpSwitch')['switch']();
  126. g.selectCustom(old.val);
  127. // $("#l-p-from").val(old.val.from);
  128. // $("#l-p-to").val(old.val.to);
  129. }
  130. }
  131. function isSwitchOpened(){
  132. return document.getElementById("lpSwitch").classList.contains("mui-active");
  133. }
  134. Lpicker.prototype.init = function(_el, opts){
  135. var g= this;
  136. var p = g.options = $.extend(true, options, opts);
  137. _el.hide();
  138. _el.append(html);
  139. //取消按钮
  140. $('.l-btn-cancel', p._el).on("tap", function(){
  141. g.close();
  142. // alert(1)
  143. // g.reset();
  144. })
  145. //确认按钮
  146. $('.l-btn-ok', p._el).on("tap", function(){
  147. p.old = {};
  148. p.old.type = p.type;
  149. if(p.type==1)
  150. p.old.val = $('.l-time-sel-box li.l-over', p._el).index();
  151. else
  152. p.old.val = g.getValue();
  153. g.close();
  154. if(p.onCheck) p.onCheck.call(g);
  155. })
  156. //选项点击事件
  157. var items = $('.l-time-sel-box li', p._el);
  158. items.on("tap", function(){
  159. g.selectItem($(this));
  160. })
  161. //自定义按钮
  162. document.getElementById("lpSwitch").addEventListener("toggle",function(event){
  163. if(event.detail.isActive){
  164. var tim = $('#lpSwitch').data('time');
  165. if(tim){
  166. $('#lpSwitch').removeData('time');
  167. g.selectCustom(tim);
  168. } else
  169. g.selectCustom();
  170. }else{
  171. $('.l-date-form', p._el).hide();
  172. if($('.l-time-sel-box li.l-over', p._el).length==0){
  173. p.type = 1;
  174. $('.l-time-sel-box li:first', p._el).addClass("l-over");
  175. }
  176. }
  177. })
  178. //开始日期
  179. $('#l-p-from').on("tap", function(){
  180. var self = $(this);
  181. var v = self.val().split('-');
  182. var dDate = new Date();
  183. dDate.setFullYear(parseInt(v[0]), parseInt(v[1]) - 1, parseInt(v[2]));
  184. var minDate = new Date();
  185. minDate.setFullYear(2016, 7, 1);
  186. var maxDate = new Date();
  187. var to = $('#l-p-to').val();
  188. if(to){
  189. to = to.split("-");
  190. maxDate.setFullYear(parseInt(to[0]), parseInt(to[1])-1, parseInt(to[2]));
  191. } else
  192. maxDate.setDate(maxDate.getDate());
  193. plus.nativeUI.pickDate(function(e) {
  194. var d = e.date;
  195. self.val(formatDate(d));
  196. }, function(e) {
  197. }, {
  198. title: "请选择开始日期",
  199. date: dDate,
  200. minDate: minDate,
  201. maxDate: maxDate
  202. });
  203. })
  204. //结束日期
  205. $('#l-p-to').on("tap", function(){
  206. var self = $(this);
  207. var v = self.val().split('-');
  208. var dDate = new Date();
  209. dDate.setFullYear(parseInt(v[0]), parseInt(v[1]) - 1, parseInt(v[2]));
  210. var minDate = new Date();
  211. var from = $('#l-p-from').val();
  212. if(from){
  213. from = from.split("-");
  214. minDate.setFullYear(parseInt(from[0]), parseInt(from[1])-1, parseInt(from[2]));
  215. } else
  216. minDate.setFullYear(1990, 0, 1);
  217. var maxDate = new Date();
  218. maxDate.setDate(maxDate.getDate());
  219. plus.nativeUI.pickDate(function(e) {
  220. var d = e.date;
  221. self.val(formatDate(d));
  222. }, function(e) {
  223. info.innerText = "您没有选择日期";
  224. }, {
  225. title: "请选择结束日期",
  226. date: dDate,
  227. minDate: minDate,
  228. maxDate: maxDate
  229. });
  230. })
  231. }
  232. Lpicker.prototype.selectItem = function(el){
  233. var g= this, p= g.options;
  234. p.type = 1;
  235. $('.l-time-sel-box li', p._el).removeClass("l-over");
  236. $(el).addClass("l-over");
  237. if(isSwitchOpened()){
  238. mui("#lpSwitch").switch().toggle();
  239. }
  240. }
  241. Lpicker.prototype.selectCustom = function(v){
  242. var g= this, p= g.options;
  243. var v = v || g.getValue();
  244. if(v.from=='')
  245. v.from = getPreDate(6);
  246. p.type = 2;
  247. $('.l-time-sel-box li', p._el).removeClass("l-over");
  248. $('#l-p-from').val(v.from);
  249. $('#l-p-to').val(v.to);
  250. $('.l-date-form', p._el).show();
  251. }
  252. function getMonth(m){
  253. m++;
  254. if(m<10)
  255. return "0" + m.toString();
  256. return m.toString();
  257. }
  258. function getDay(m){
  259. if(m<10)
  260. return "0" + m.toString();
  261. return m.toString();
  262. }
  263. $.fn.lpicker = function (opts) {
  264. this.css("position", "fixed");
  265. this.css("width", "100%");
  266. this.css("z-index", "1000");
  267. this.css("top", "0");
  268. this.css("left", "0");
  269. opts = opts || {};
  270. opts._el = $(this);
  271. var o = new Lpicker();
  272. o.init(this, opts);
  273. this.data("Lpicker", o);
  274. return o;
  275. };
  276. $.fn.getLpicker = function () {
  277. return this.data("Lpicker");
  278. };
  279. })