UNPKG

@lspriv/wx-calendar

Version:

微信小程序日历

1 lines 8.34 kB
"use strict";var e=this&&this.__awaiter||function(e,l,o,i){return new(o=o||Promise)(function(s,t){function r(e){try{a(i.next(e))}catch(e){t(e)}}function n(e){try{a(i.throw(e))}catch(e){t(e)}}function a(e){var t;e.done?s(e.value):((t=e.value)instanceof o?t:new o(function(e){e(t)})).then(r,n)}a((i=i.apply(e,l||[])).next())})};Object.defineProperty(exports,"__esModule",{value:!0}),exports.PluginService=exports.intercept=exports.isPluginConstructor=void 0;let s=require("./tools"),y=require("./constants"),d=require("../utils/shared"),p=require("../interface/calendar"),a="PLUGIN_ON_",l="PLUGIN_CATCH_",o=[/^background/,"color",/^border/],t=e=>"function"==typeof e&&(0,d.hasOwn)(e,"KEY");exports.isPluginConstructor=t;class i extends Error{constructor(e,t=0){super(e),this.code=t}}let r=e=>{throw new i(void 0,e)};exports.intercept=r;class n{constructor(e,t){this._plugins_=[],this.component=e,this._plugins_=(t||[]).flatMap(e=>e.construct.KEY?{key:e.construct.KEY,instance:new e.construct(e.options)}:[]),this.initialize()}initialize(){this.traversePlugins(e=>{var t;null!=(t=e.PLUGIN_ON_INITIALIZE||e.PLUGIN_INITIALIZE)&&t.call(e,this)})}walkForDate(s){let r={};return this.traversePlugins(e=>{var t=null==(t=e.PLUGIN_TRACK_DATE)?void 0:t.call(e,s);t&&(t.corner&&!r.corner&&(r.corner=t.corner),t.festival&&!r.festival&&(r.festival=t.festival),t.solar&&!r.solar&&(r.solar=t.solar),null!=(e=t.schedule)&&e.length&&(r.schedule=(r.schedule||[]).concat(t.schedule)),t.style)&&(e=(0,p.styleParse)(t.style),r.style=Object.assign(Object.assign({},e),r.style))}),(0,d.notEmptyObject)(r)?r:null}walkForYear(r){let n={};return this.traversePlugins((e,t)=>{var s=null==(s=e.PLUGIN_TRACK_YEAR)?void 0:s.call(e,r);s&&(s.subinfo&&(n.subinfo=[...(0,p.fillAnnualSubs)(t,r.year,s.subinfo)||[],...n.subinfo||[]]),null!=(e=s.marks))&&e.size&&(n.marks=(0,p.mergeAnnualMarks)(s.marks,n.marks))}),(0,d.notEmptyObject)(n)?n:null}catchMonth(l){return e(this,void 0,void 0,function*(){var e=yield(0,s.nextTick)(()=>{var s,r=[];for(let t=l.weeks.length;t--;){var n=l.weeks[t];for(let e=n.days.length;e--;){var a=n.days[e],a=this.walkForDate(a);a&&(a.style&&(a.style=(0,p.styleStringify)(a.style)),null!=(s=a.corner)&&s.style&&(a.corner.style=(0,p.reorderStyle)(a.corner.style)),null!=(s=a.festival)&&s.style&&(a.festival.style=(0,p.reorderStyle)(a.festival.style)),null!=(s=a.solar)&&s.style&&(a.solar.style=(0,p.reorderStyle)(a.solar.style)),null!=(s=a.schedule)&&s.length&&a.schedule.forEach(e=>{e.style&&(e.style=(0,p.reorderStyle)(e.style,o))}),r.push({wdx:t,ddx:e,record:a}))}}return r});yield this.component._annual_.interaction(),this.setMonth({year:l.year,month:l.month,days:e})})}catchYear(t){return e(this,void 0,void 0,function*(){var e=yield(0,s.nextTick)(()=>this.walkForYear(t));yield this.component._annual_.interaction(),e&&this.setYear({year:t.year,result:e})})}setMonth(i){(0,s.nextTick)(()=>{var s=this.component.data.panels,r={};for(let t=s.length;t--;){var e=s[t];if(e.year===i.year&&e.month===i.month)for(let e=i.days.length;e--;){var{wdx:n,ddx:a,record:l}=i.days[e],o=s[t].weeks[n].days[a],n=`panels[${t}].weeks[${n}].days[${a}]`;(0,d.compareSame)(l.solar,o.solar)||(r[n+".solar"]=l.solar||null),(0,d.compareSame)(l.corner,o.corner)||(r[n+".corner"]=l.corner||null),(0,d.compareSame)(l.festival,o.mark)||(r[n+".mark"]=l.festival||null),(0,d.compareSame)(l.schedule,o.schedules)||(r[n+".schedules"]=l.schedule||null),l.style!==o.style&&(r[n+".style"]=l.style||"")}}(0,d.notEmptyObject)(r)&&this.component.setData(r)})}setYear(r){(0,s.nextTick)(()=>{var e=this.component.data.years,t={},s=e.findIndex(e=>e.year===r.year);0<=s&&((0,d.compareSame)(r.result.subinfo,e[s].subinfo)||(t[`years[${s}].subinfo`]=r.result.subinfo||null),(0,p.sameAnnualMarks)(this.component._years_[s].marks,r.result.marks)||(this.component._years_[s].marks=r.result.marks||new Map,this.component._printer_.update([s]))),(0,d.notEmptyObject)(t)&&this.component.setData(t)})}setDates(h){return(0,s.nextTick)(()=>{var t=this.component.data.panels,s={};for(let e=h.length;e--;){var{year:r,month:n,day:a,record:l}=h[e];for(let e=t.length;e--;){var o,i,c,u=t[e];(u.year===r&&u.month===n||-1===(0,p.monthDiff)({year:r,month:n},u)&&a<=7||1===(0,p.monthDiff)({year:r,month:n},u)&&a>=y.GREGORIAN_MONTH_DAYS[n-1]-7)&&({wdx:o,ddx:i}=(0,p.getWeekDateIdx)({year:r,month:n,day:a},u.weeks),0<=o)&&(c=`panels[${e}].weeks[${o}].days[${i}]`,u=u.weeks[o].days[i],(0,d.compareSame)(u.solar,l.solar)||(s[c+".solar"]=l.solar||null),(0,d.compareSame)(u.corner,l.corner)||(s[c+".corner"]=l.corner||null),(0,d.compareSame)(u.mark,l.festival)||(s[c+".mark"]=l.festival||null),(0,d.compareSame)(u.schedules,l.schedule)||(s[c+".schedules"]=l.schedule||null),l.style!==u.style)&&(s[c+".style"]=l.style||"")}}(0,d.notEmptyObject)(s)&&this.component.setData(s)})}updateDates(r){return e(this,void 0,void 0,function*(){var e=this.component.data.panels,t=(r=r||e.flatMap(e=>e.weeks.flatMap(e=>e.days)),new Map);for(let e=r.length;e--;){var s=r[e];this.setUpdateRecord(t,s)}return yield this.component._annual_.interaction(),this.setDates([...t.values()])})}updateRange(u){return e(this,void 0,void 0,function*(){var e=this.component.data.panels,t=this.component.data.current,s=Math.floor(y.CALENDAR_PANELS/2),r=(0,p.timestamp)(e[(t-s+y.CALENDAR_PANELS)%y.CALENDAR_PANELS].weeks[0].days[0]),[e]=e[(t+s)%y.CALENDAR_PANELS].weeks.slice(-1),n=(0,p.timestamp)(e.days.slice(-1)[0]),a=new Map;for(let e=0;e<u.length;e++){var l=u[e];if(l.length)if(1===l.length)this.setUpdateRecord(a,l[0]);else{var o=(0,p.timestamp)(l[0]),l=(0,p.timestamp)(l[1]);if(!(n<o||l<r)){let e=Math.max(o,r);for(var i=Math.min(l,n);e<=i;){var c=(0,p.normalDate)(e);this.setUpdateRecord(a,c),e+=y.MS_ONE_DAY}}}}return yield this.component._annual_.interaction(),this.setDates([...a.values()])})}setUpdateRecord(e,t){var s,r,n=t.year+`_${t.month}_`+t.day;e.has(n)||((r=this.walkForDate(t)||{}).style&&(r.style=(0,p.styleStringify)(r.style)),null!=(s=r.corner)&&s.style&&(r.corner.style=(0,p.reorderStyle)(r.corner.style)),null!=(s=r.festival)&&s.style&&(r.festival.style=(0,p.reorderStyle)(r.festival.style)),null!=(s=r.solar)&&s.style&&(r.solar.style=(0,p.reorderStyle)(r.solar.style)),null!=(s=r.schedule)&&s.length&&r.schedule.forEach(e=>{e.style&&(e.style=(0,p.reorderStyle)(e.style,o))}),e.set(n,{year:t.year,month:t.month,day:t.day,record:r}))}updateAnnuals(o){return e(this,void 0,void 0,function*(){if(null!=o&&o.length){var e=this.component.data.years,s={},r=[];for(let t=o.length;t--;){var n,a,l=e.findIndex(e=>e.year===o[t]);0<=l&&(n=e[l],a=this.walkForYear(n))&&((0,d.compareSame)(a.subinfo,n.subinfo)||(s[`years[${l}].subinfo`]=a.subinfo||null),(0,p.sameAnnualMarks)(this.component._years_[l].marks,a.marks)||(this.component._years_[l].marks=a.marks||new Map,r.push(l)))}yield this.component._annual_.interaction(),r.length&&this.component._printer_.update(r),(0,d.notEmptyObject)(s)&&this.component.setData(s)}})}getEntireMarks(r){let n={solar:[],corner:[],festival:[],schedule:[],style:[]};return this.traversePlugins((e,t)=>{var s=null==(s=e.PLUGIN_TRACK_DATE)?void 0:s.call(e,r);s&&(s.style&&n.style.push(Object.assign(Object.assign({},(0,p.styleParse)(s.style)),{key:t})),s.solar&&n.solar.push(Object.assign(Object.assign({},s.solar),{key:t})),s.corner&&n.corner.push(Object.assign(Object.assign({},s.corner),{key:t})),s.festival&&n.festival.push(Object.assign(Object.assign({},s.festival),{key:t})),null!=(e=s.schedule))&&e.length&&n.schedule.push(...s.schedule.map(e=>Object.assign(Object.assign({},e),{key:t})))}),n}dispatchEvent(e,...t){var s,r=""+a+(0,d.camelToSnake)(e).toUpperCase();try{for(let e=0;e<this._plugins_.length;e++){var n=this._plugins_[e];null!=(s=n.instance[r])&&s.call(n.instance,this,...t)}}catch(e){}}interceptEvent(e,t,s){var r=""+l+(0,d.camelToSnake)(e).toUpperCase();let n=!0;for(let e=this._plugins_.length;e--;){var a=this._plugins_[e].instance;try{a[r].call(a,this,t,exports.intercept)}catch(e){if(e instanceof i){if(!e.code)return;n=!1}}}return!n||null==s?void 0:s()}getPlugin(t){var e=this._plugins_.find(e=>e.key===t);return null==e?void 0:e.instance}traversePlugins(t){for(let e=this._plugins_.length;e--;){var s=this._plugins_[e];t(s.instance,s.key)}}getConf(e){let t=this._plugins_.length;for(;t--;){var s=this._plugins_[t].instance;if(null!=s[e])return s[e]}}}exports.PluginService=n;