UNPKG

tt-mp

Version:

一套组件化、可复用、易扩展的头条小程序 UI 组件库

1 lines 9.67 kB
"use strict";var _baseComponent=_interopRequireDefault(require("../helpers/baseComponent")),_classNames=_interopRequireDefault(require("../helpers/classNames")),_index=_interopRequireDefault(require("./locales/index")),_props=require("./props");function _interopRequireDefault(e){return e&&e.__esModule?e:{default:e}}function ownKeys(t,e){var a,r=Object.keys(t);return Object.getOwnPropertySymbols&&(a=Object.getOwnPropertySymbols(t),e&&(a=a.filter(function(e){return Object.getOwnPropertyDescriptor(t,e).enumerable})),r.push.apply(r,a)),r}function _objectSpread(t){for(var e=1;e<arguments.length;e++){var a=null!=arguments[e]?arguments[e]:{};e%2?ownKeys(Object(a),!0).forEach(function(e){_defineProperty(t,e,a[e])}):Object.getOwnPropertyDescriptors?Object.defineProperties(t,Object.getOwnPropertyDescriptors(a)):ownKeys(Object(a)).forEach(function(e){Object.defineProperty(t,e,Object.getOwnPropertyDescriptor(a,e))})}return t}function _defineProperty(e,t,a){return t in e?Object.defineProperty(e,t,{value:a,enumerable:!0,configurable:!0,writable:!0}):e[t]=a,e}function _construct(e,t,a){return(_construct=_isNativeReflectConstruct()?Reflect.construct:function(e,t,a){var r=[null];r.push.apply(r,t);var n=new(Function.bind.apply(e,r));return a&&_setPrototypeOf(n,a.prototype),n}).apply(null,arguments)}function _isNativeReflectConstruct(){if("undefined"==typeof Reflect||!Reflect.construct)return!1;if(Reflect.construct.sham)return!1;if("function"==typeof Proxy)return!0;try{return Date.prototype.toString.call(Reflect.construct(Date,[],function(){})),!0}catch(e){return!1}}function _setPrototypeOf(e,t){return(_setPrototypeOf=Object.setPrototypeOf||function(e,t){return e.__proto__=t,e})(e,t)}function _toConsumableArray(e){return _arrayWithoutHoles(e)||_iterableToArray(e)||_unsupportedIterableToArray(e)||_nonIterableSpread()}function _nonIterableSpread(){throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}function _unsupportedIterableToArray(e,t){if(e){if("string"==typeof e)return _arrayLikeToArray(e,t);var a=Object.prototype.toString.call(e).slice(8,-1);return"Object"===a&&e.constructor&&(a=e.constructor.name),"Map"===a||"Set"===a?Array.from(e):"Arguments"===a||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(a)?_arrayLikeToArray(e,t):void 0}}function _iterableToArray(e){if("undefined"!=typeof Symbol&&Symbol.iterator in Object(e))return Array.from(e)}function _arrayWithoutHoles(e){if(Array.isArray(e))return _arrayLikeToArray(e)}function _arrayLikeToArray(e,t){(null==t||t>e.length)&&(t=e.length);for(var a=0,r=new Array(t);a<t;a++)r[a]=e[a];return r}var DATETIME="datetime",DATE="date",TIME="time",MONTH="month",YEAR="year",ONE_DAY=864e5;function fomartArray(e,t){for(var a=2<arguments.length&&void 0!==arguments[2]?arguments[2]:1,r=e,n=[];r<=t;)n.push(r),r+=a;return n}function getDaysInMonth(e){return new Date(e.getFullYear(),e.getMonth()+1,0).getDate()}function pad(e){return e<10?"0".concat(e):e+""}function cloneDate(e){return new Date(+e)}function setMonth(e,t){e.setDate(Math.min(e.getDate(),getDaysInMonth(new Date(e.getFullYear(),t)))),e.setMonth(t)}function valueToDate(e){var t=1<arguments.length&&void 0!==arguments[1]?arguments[1]:{};if(!Array.isArray(e))return"string"==typeof e&&(e=e.replace(/\-/g,"/")),isNaN(Number(e))||(e=Number(e)),new Date(e);var a,r,n,i,u=t.mode,o=t.use12Hours,s=new Date,l=s.getFullYear(),c=s.getMonth(),h=s.getDate(),f=e.map(function(e){return Number(e)});for(o&&[DATETIME,TIME].includes(u)&&(a=u===DATETIME?3:0,r=f.length-1,n=Number(f[r]),i=Number(f[a]),i=1===n?(i<=12&&(i+=12),24<=i?0:i):(0===i&&(i=12),12<i&&(i-=12),12<=i?0:i),f.splice(a,1,i),f.splice(r,1)),u===TIME?(f.unshift(h),f.unshift(c),f.unshift(l)):u===MONTH?f.push(h):u===YEAR&&(f.push(c),f.push(h));f.length<=6;)f.push(0);return _construct(Date,_toConsumableArray(f))}(0,_baseComponent.default)({properties:_props.props,data:{inputValue:[],options:[]},observers:_defineProperty({inputValue:function(){this.updatedCols()},value:function(e){this.setValue(e)}},"mode, minuteStep, use12Hours, minDate, maxDate, minHour, maxHour, minMinute, maxMinute, lang",function(){this.setValue(this.data.inputValue)}),methods:{getDefaultMinDate:function(){return this.defaultMinDate||(this.defaultMinDate=new Date(2e3,1,1,0,0,0)),this.defaultMinDate},getDefaultMaxDate:function(){return this.defaultMaxDate||(this.defaultMaxDate=new Date(2030,1,1,23,59,59)),this.defaultMaxDate},getMinDate:function(){return this.data.minDate?valueToDate(this.data.minDate,this.data):this.getDefaultMinDate()},getMaxDate:function(){return this.data.maxDate?valueToDate(this.data.maxDate,this.data):this.getDefaultMaxDate()},getDateMember:function(e,t){var a=1<arguments.length&&void 0!==t?t:"year",r={min:"getMinDate",max:"getMaxDate",year:"getFullYear",month:"getMonth",day:"getDate",hour:"getHours",minute:"getMinutes"};return this[r[0<arguments.length&&void 0!==e?e:"min"]]()[r[a]]()},getDisplayHour:function(e){return this.data.use12Hours&&(0===e&&(e=12),12<e&&(e-=12)),e},setHours:function(e,t){var a;this.data.use12Hours?(a=t,a=24<=(a=12<=e.getHours()?t+12:t)?0:a,e.setHours(a)):e.setHours(t)},setAmPm:function(e,t){0===t?e.setTime(e-ONE_DAY/2):e.setTime(+e+ONE_DAY/2)},getNewDate:function(e,t){var a=parseInt(e[t],10),r=this.data.mode,n=cloneDate(this.getDate());if(r===DATETIME||r===DATE||r===YEAR||r===MONTH)switch(t){case 0:n.setFullYear(a);break;case 1:setMonth(n,a);break;case 2:n.setDate(a);break;case 3:this.setHours(n,a);break;case 4:n.setMinutes(a);break;case 5:this.setAmPm(n,a)}else if(r===TIME)switch(t){case 0:this.setHours(n,a);break;case 1:n.setMinutes(a);break;case 2:this.setAmPm(n,a)}return this.clipDate(n)},clipDate:function(e){var t=this.data.mode,a=this.getMinDate(),r=this.getMaxDate();if(t===DATETIME){if(e<a)return cloneDate(a);if(r<e)return cloneDate(r)}else if(t===DATE||t===YEAR||t===MONTH){if(+e+ONE_DAY<=a)return cloneDate(a);if(+r+ONE_DAY<=e)return cloneDate(r)}else if(t===TIME){var n=r.getHours(),i=r.getMinutes(),u=a.getHours(),o=a.getMinutes(),s=e.getHours(),l=e.getMinutes();if(s<u||s===u&&l<o)return cloneDate(a);if(n<s||s===n&&i<l)return cloneDate(r)}return e},getDate:function(e){var t=e||this.data.value;return this.clipDate(t?valueToDate(t,this.data):this.getMinDate())},getDateData:function(e){var t=this.data,a=t.mode,r=t.lang,n=_index.default[r],i=e.getFullYear(),u=e.getMonth(),o=this.getDateMember("min","year"),s=this.getDateMember("max","year"),l=this.getDateMember("min","month"),c=this.getDateMember("max","month"),h=this.getDateMember("min","day"),f=this.getDateMember("max","day"),m=fomartArray(o,s).map(function(e){return{value:e+"",label:e+n.year+""}});if(a===YEAR)return[m];var D=fomartArray(o===i?l:0,s===i?c:11).map(function(e){return{value:e+"",label:e+1+n.month+""}});return a===MONTH?[m,D]:[m,D,fomartArray(o===i&&l===u?h:1,s===i&&c===u?f:getDaysInMonth(e)).map(function(e){return{value:e+"",label:e+n.day+""}})]},getTimeData:function(e){var t,a,r,n,i,u,o,s,l,c=this.data,h=c.minHour,f=c.maxHour,m=c.minMinute,D=c.maxMinute,p=this.data,g=p.mode,d=p.minuteStep,y=p.use12Hours,b=p.lang,M=_index.default[b],v=this.getDateMember("min","minute"),_=this.getDateMember("max","minute"),A=this.getDateMember("min","hour"),T=this.getDateMember("max","hour"),E=e.getHours();g===DATETIME?(t=e.getFullYear(),a=e.getMonth(),r=e.getDate(),n=this.getDateMember("min","year"),i=this.getDateMember("max","year"),u=this.getDateMember("min","month"),o=this.getDateMember("max","month"),s=this.getDateMember("min","day"),l=this.getDateMember("max","day"),n===t&&u===a&&s===r&&(h=A)===E&&(m=v),i===t&&o===a&&l===r&&(f=T)===E&&(D=_)):((h=A)===E&&(m=v),(f=T)===E&&(D=_));var O=[];0===h&&0===f||0!==h&&0!==f?h=this.getDisplayHour(h):0===h&&y&&(h=1,O.push({value:"0",label:M.hour?"12"+M.hour:"12"})),f=this.getDisplayHour(f),O=[].concat(_toConsumableArray(O),_toConsumableArray(fomartArray(h,f).map(function(e){return{value:e+"",label:M.hour?e+M.hour+"":pad(e)}})));for(var H=[],x=e.getMinutes(),w=m;w<=D;w+=d)H.push({value:w+"",label:M.minute?w+M.minute+"":pad(w)}),w<x&&x<w+d&&H.push({value:x+"",label:M.minute?x+M.minute+"":pad(x)});var I=[{value:"0",label:M.am},{value:"1",label:M.pm}];return[O,H].concat(y?[I]:[])},getValueCols:function(e){var t,a,r,n=this.data,i=n.mode,u=n.use12Hours,o=this.getDate(e),s=[],l=[];return i===YEAR?{cols:this.getDateData(o),value:[o.getFullYear()+""]}:i===MONTH?{cols:this.getDateData(o),value:[o.getFullYear()+"",o.getMonth()+""]}:(i!==DATETIME&&i!==DATE||(s=this.getDateData(o),l=[o.getFullYear()+"",o.getMonth()+"",o.getDate()+""]),i!==DATETIME&&i!==TIME||(s=s.concat(this.getTimeData(o)),r=[(t=o.getHours())+"",(a=o.getMinutes())+""],u&&(r=[(0===t?12:12<t?t-12:t)+"",a+"",(12<=t?1:0)+""]),l=l.concat(r)),{value:l,cols:s})},onValueChange:function(e){var t=e.detail,a=t.value,r=t.index,n=this.getNewDate(a,r),i=this.getValueCols(n),u=i.value,o=i.cols,s=this.getValue(u,o);this.triggerEvent("valueChange",_objectSpread(_objectSpread(_objectSpread({},e.detail),s),{},{date:+n}))},updatedCols:function(){var e=this.getValueCols().cols;this.setData({cols:e})},updated:function(e){this.data.inputValue!==e&&this.setData({inputValue:e})},setValue:function(e){0<arguments.length&&void 0!==e||this.data.inputValue;var t=this.getValueCols().value;this.updated(t)},getValue:function(e,t){var a=0<arguments.length&&void 0!==e?e:this.data.inputValue,r=1<arguments.length&&void 0!==t?t:this.data.cols;return this.picker=this.picker||this.selectComponent("#wux-picker"),_objectSpread(_objectSpread({},this.picker.getValue(a,r)),{},{date:+this.getDate()})}},attached:function(){this.setValue(this.data.value)}});