index.js 10 KB

1
  1. "use strict";function _typeof(e){return(_typeof="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e})(e)}var _baseComponent=_interopRequireDefault(require("../helpers/baseComponent")),_classNames=_interopRequireDefault(require("../helpers/libs/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=_toPropertyKey(t))in e?Object.defineProperty(e,t,{value:a,enumerable:!0,configurable:!0,writable:!0}):e[t]=a,e}function _toPropertyKey(e){e=_toPrimitive(e,"string");return"symbol"==_typeof(e)?e:String(e)}function _toPrimitive(e,t){if("object"!=_typeof(e)||!e)return e;var a=e[Symbol.toPrimitive];if(void 0===a)return("string"===t?String:Number)(e);a=a.call(e,t||"default");if("object"!=_typeof(a))return a;throw new TypeError("@@toPrimitive must return a primitive value.")}function _construct(e,t,a){var r;return _isNativeReflectConstruct()?Reflect.construct.apply(null,arguments):((r=[null]).push.apply(r,t),t=new(e.bind.apply(e,r)),a&&_setPrototypeOf(t,a.prototype),t)}function _isNativeReflectConstruct(){try{var e=!Boolean.prototype.valueOf.call(Reflect.construct(Boolean,[],function(){}))}catch(e){}return(_isNativeReflectConstruct=function(){return!!e})()}function _setPrototypeOf(e,t){return(_setPrototypeOf=Object.setPrototypeOf?Object.setPrototypeOf.bind():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){var a;if(e)return"string"==typeof e?_arrayLikeToArray(e,t):"Map"===(a="Object"===(a=Object.prototype.toString.call(e).slice(8,-1))&&e.constructor?e.constructor.name: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&&null!=e[Symbol.iterator]||null!=e["@@iterator"])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=t.mode,t=t.use12Hours,i=new Date,o=i.getFullYear(),u=i.getMonth(),i=i.getDate(),s=e.map(function(e){return Number(e)});for(t&&[DATETIME,TIME].includes(n)&&(e=n===DATETIME?3:0,t=s.length-1,a=Number(s[t]),r=Number(s[e]),r=1===a?(r<=12&&(r+=12),24<=r?0:r):(12<(r=0===r?12:r)&&(r-=12),12<=r?0:r),s.splice(e,1,r),s.splice(t,1)),n===TIME?(s.unshift(i),s.unshift(u),s.unshift(o)):n===MONTH?s.push(i):n===YEAR&&(s.push(u),s.push(i));s.length<=6;)s.push(0);return _construct(Date,_toConsumableArray(s))}(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(){var e=0<arguments.length&&void 0!==arguments[0]?arguments[0]:"min",t=1<arguments.length&&void 0!==arguments[1]?arguments[1]:"year",a={min:"getMinDate",max:"getMaxDate",year:"getFullYear",month:"getMonth",day:"getDate",hour:"getHours",minute:"getMinutes"};return this[a[e]]()[a[t]]()},getDisplayHour:function(e){return this.data.use12Hours&&12<(e=0===e?12:e)&&(e-=12),e},setHours:function(e,t){var a,r;this.data.use12Hours?(a=e.getHours(),r=t,e.setHours(r=24<=(r=12<=a?t+12:t)?0:r)):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),e=this.data.mode,r=cloneDate(this.getDate());if(e===DATETIME||e===DATE||e===YEAR||e===MONTH)switch(t){case 0:r.setFullYear(a);break;case 1:setMonth(r,a);break;case 2:r.setDate(a);break;case 3:this.setHours(r,a);break;case 4:r.setMinutes(a);break;case 5:this.setAmPm(r,a)}else if(e===TIME)switch(t){case 0:this.setHours(r,a);break;case 1:r.setMinutes(a);break;case 2:this.setAmPm(r,a)}return this.clipDate(r)},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 t=r.getHours(),n=r.getMinutes(),i=a.getHours(),o=a.getMinutes(),u=e.getHours(),s=e.getMinutes();if(u<i||u===i&&s<o)return cloneDate(a);if(t<u||u===t&&n<s)return cloneDate(r)}return e},getDate:function(e){e=e||this.data.value;return this.clipDate(e?valueToDate(e,this.data):this.getMinDate())},getDateData:function(e){var t,a=this.data,r=a.mode,a=a.lang,n=_index.default[a],a=e.getFullYear(),i=e.getMonth(),o=this.getDateMember("min","year"),u=this.getDateMember("max","year"),s=this.getDateMember("min","month"),l=this.getDateMember("max","month"),c=this.getDateMember("min","day"),h=this.getDateMember("max","day"),f=fomartArray(o,u).map(function(e){return{value:e+"",label:e+n.year+""}});return r===YEAR?[f]:(t=fomartArray(o===a?s:0,u===a?l:11).map(function(e){return{value:e+"",label:e+1+n.month+""}}),r===MONTH?[f,t]:[f,t,fomartArray(o===a&&s===i?c:1,u===a&&l===i?h:getDaysInMonth(e)).map(function(e){return{value:e+"",label:e+n.day+""}})])},getTimeData:function(e){for(var t,a,r,n,i,o,u=this.data,s=u.minHour,l=u.maxHour,c=u.minMinute,h=u.maxMinute,u=this.data,f=u.mode,m=u.minuteStep,p=u.use12Hours,u=u.lang,D=_index.default[u],u=this.getDateMember("min","minute"),g=this.getDateMember("max","minute"),y=this.getDateMember("min","hour"),d=this.getDateMember("max","hour"),b=e.getHours(),M=(f===DATETIME?(f=e.getFullYear(),t=e.getMonth(),a=e.getDate(),M=this.getDateMember("min","year"),r=this.getDateMember("max","year"),T=this.getDateMember("min","month"),n=this.getDateMember("max","month"),i=this.getDateMember("min","day"),o=this.getDateMember("max","day"),M===f&&T===t&&i===a&&(s=y)===b&&(c=u),r===f&&n===t&&o===a&&(l=d)===b&&(h=g)):((s=y)===b&&(c=u),(l=d)===b&&(h=g)),[]),v=(0===s&&0===l||0!==s&&0!==l?s=this.getDisplayHour(s):0===s&&p&&(s=1,M.push({value:"0",label:D.hour?"12"+D.hour:"12"})),l=this.getDisplayHour(l),M=[].concat(_toConsumableArray(M),_toConsumableArray(fomartArray(s,l).map(function(e){return{value:e+"",label:D.hour?e+D.hour+"":pad(e)}}))),[]),_=e.getMinutes(),A=c;A<=h;A+=m)v.push({value:A+"",label:D.minute?A+D.minute+"":pad(A)}),A<_&&_<A+m&&v.push({value:_+"",label:D.minute?_+D.minute+"":pad(_)});var T=[{value:"0",label:D.am},{value:"1",label:D.pm}];return[M,v].concat(p?[T]:[])},getValueCols:function(e){var t,a=this.data,r=a.mode,a=a.use12Hours,e=this.getDate(e),n=[],i=[];return r===YEAR?{cols:this.getDateData(e),value:[e.getFullYear()+""]}:r===MONTH?{cols:this.getDateData(e),value:[e.getFullYear()+"",e.getMonth()+""]}:(r!==DATETIME&&r!==DATE||(n=this.getDateData(e),i=[e.getFullYear()+"",e.getMonth()+"",e.getDate()+""]),r!==DATETIME&&r!==TIME||(n=n.concat(this.getTimeData(e)),t=[(r=e.getHours())+"",(e=e.getMinutes())+""],a&&(t=[(0===r?12:12<r?r-12:r)+"",e+"",(12<=r?1:0)+""]),i=i.concat(t)),{value:i,cols:n})},onValueChange:function(e){var t=e.detail,a=t.value,t=t.index,a=this.getNewDate(a,t),t=this.getValueCols(a),r=t.value,t=t.cols,r=this.getValue(r,t);this.triggerEvent("valueChange",_objectSpread(_objectSpread(_objectSpread({},e.detail),r),{},{date:+a}))},updatedCols:function(){var e=this.getValueCols().cols;this.setData({cols:e})},updated:function(e){this.data.inputValue!==e&&this.setData({inputValue:e})},setValue:function(){0<arguments.length&&void 0!==arguments[0]||this.data.inputValue;var e=this.getValueCols().value;this.updated(e)},getValue:function(){var e=0<arguments.length&&void 0!==arguments[0]?arguments[0]:this.data.inputValue,t=1<arguments.length&&void 0!==arguments[1]?arguments[1]:this.data.cols;return this.picker=this.picker||this.querySelector("#wux-picker"),_objectSpread(_objectSpread({},this.picker.getValue(e,t)),{},{date:+this.getDate()})}},attached:function(){this.setValue(this.data.value)}});