diff --git a/API.md b/API.md index 3f7d1ad..3515366 100644 --- a/API.md +++ b/API.md @@ -161,9 +161,11 @@ Type: `Array|Object` selectable: ['2016-01-01', '2016-01-02'] // Date array selectable: [new Date(2016, 0, 1), new Date(2016, 0, 2)] +// range +selectable: { range: [{from: '2016-01-01', to: '2016-01-02'}] } +selectable: { range: [{from: new Date(2016, 0, 1), to: new Date(2016, 0, 2)}] } // Object -selectable: {from: '2016-01-01', to: '2016-01-02'} -selectable: {from: new Date(2016, 0, 1), to: new Date(2016, 0, 2)} +selectable: { '2016-01-01': true, '2016-01-02': true } ``` @@ -230,7 +232,8 @@ Set the date / year / month that can be selected from the `Calendar`. ```js myCalendar.setSelectable(['2016-01-01', ...]); myCalendar.setSelectable([new Date(), ...]); -myCalendar.setSelectable({from: '2016-01-01', to: '2016-01-10'}); -myCalendar.setSelectable({from: new Date(), to: new Date()}); +myCalendar.setSelectable({ range: [{from: '2016-01-01', to: '2016-01-10'}] }); +myCalendar.setSelectable({ range: [{from: new Date(), to: new Date()}] }); +myCalendar.setSelectable({ '2016-01-01': true, '2016-01-02': true }); ``` diff --git a/bower.json b/bower.json index c9f24d1..f7ac5a5 100644 --- a/bower.json +++ b/bower.json @@ -1,6 +1,6 @@ { "name": "bootstrap-ax5calendar", - "version": "0.5.1", + "version": "0.6.0", "authors": [ "ThomasJ " ], diff --git a/dist/ax5calendar.js b/dist/ax5calendar.js index d05846f..5800488 100755 --- a/dist/ax5calendar.js +++ b/dist/ax5calendar.js @@ -240,6 +240,7 @@ po.push(''); po.push(''); po.push(''); + loopDate = tableStartDate; i = 0; while (i < 6) @@ -736,44 +737,79 @@ return this.selection; }; - this.setSelectable = function (selectable, isPrint) { - // collect selectableMap - if (cfg.selectable = selectable) { - var - _seltb = cfg.selectable; - this.selectableMap = {}; // clear selectableMap - - if (U.isArray(_seltb)) { - _seltb.forEach(function (n) { + this.setSelectable = (function () { + this.selectableMap = {}; + var processor = { + 'arr': function (v, map) { + map = {}; + if(!U.isArray(v)) return map; + v.forEach(function (n) { if (U.isDate(n)) n = U.date(n, {'return': cfg.dateFormat}); - self.selectableMap[n] = true; + map[n] = true; }); - } - else if (U.isObject(_seltb)) { - if (_seltb["from"] && _seltb["to"]) { - if (U.isDateFormat(_seltb["from"]) && U.isDateFormat(_seltb["to"])) { - for (var d = U.date(_seltb["from"]); d <= U.date(_seltb["to"]); d.setDate(d.getDate() + 1)) { - self.selectableMap[U.date(d, {"return": cfg.dateFormat})] = true; + return map; + }, + 'obj': function (v, map) { + map = {}; + if(U.isArray(v)) return map; + if(v.range) return map; + for (var k in v) { + map[k] = v[k]; + } + return map; + }, + 'range': function (v, map) { + map = {}; + if(U.isArray(v)) return map; + if(!v.range) return map; + + v.range.forEach(function (n) { + if (U.isDateFormat(n.from) && U.isDateFormat(n.to)) { + for (var d = U.date(n.from); d <= U.date(n.to); d.setDate(d.getDate() + 1)) { + map[U.date(d, {"return": cfg.dateFormat})] = true; } } else { - for (var i = _seltb["from"]; i <= _seltb["to"]; i++) { - self.selectableMap[i] = true; + for (var i = n.from; i <= n.to; i++) { + map[i] = true; } } + }); + + return map; + } + }; + + return function (selectable, isPrint) { + + var + key, + result = {} + ; + + if (cfg.selectable = selectable) { + if (U.isArray(selectable)) { + result = processor.arr(selectable); } - else if(!_seltb["from"] && !_seltb["to"]) { - for (var k in _seltb) { - self.selectableMap[k] = _seltb[k]; + else { + for (key in processor) { + if (selectable[key]) { + result = processor[key](selectable); + break; + } + } + if(Object.keys(result).length === 0){ + result = processor.obj(selectable); } } } - } - // 변경내용 적용하여 출력 - if(isPrint !== false) this.changeMode(); - }; + this.selectableMap = result; + // 변경내용 적용하여 출력 + if (isPrint !== false) this.changeMode(); + }; + })(); // 클래스 생성자 this.main = (function () { diff --git a/dist/ax5calendar.min.js b/dist/ax5calendar.min.js index 6c546a4..5a6e622 100755 --- a/dist/ax5calendar.min.js +++ b/dist/ax5calendar.min.js @@ -1 +1 @@ -!function(e,t){var a=ax5.util,i=function(){t&&t.call(this);var e,i=this,n=1;this.target=null,this.selection=[],this.selectableMap={},this.printedDay={start:"",end:""},this.config={clickEventName:"click",theme:"default",mode:"day",dateFormat:"yyyy-mm-dd",displayDate:new Date,animateTime:250,dimensions:{controlHeight:"40px",controlButtonWidth:"40px",itemPadding:2},lang:{yearHeading:"Choose the year",monthHeading:"Choose the month",yearTmpl:"%s",months:["January","February","March","April","May","June","July","August","September","October","November","December"],dayTmpl:"%s"},multipleSelect:!1,selectMode:"day"},e=this.config,this.init=function(){e.target||console.log(ax5.info.getError("ax5calendar","401","setConfig")),this.target=jQuery(e.target),e.displayDate=a.date(e.displayDate),this.target.html(this.getFrame()),this.$={root:this.target.find('[data-calendar-els="root"]'),control:this.target.find('[data-calendar-els="control"]'),"control-display":this.target.find('[data-calendar-els="control-display"]'),body:this.target.find('[data-calendar-els="body"]')},e.control&&this.$.control.find("[data-calendar-move]").on(e.clickEventName,function(e){this.move(e||window.event)}.bind(this)),e.selectable&&this.setSelectable(e.selectable,!1),"day"===e.mode||"d"===e.mode?this.printDay(e.displayDate):"month"===e.mode||"m"===e.mode?this.printMonth(e.displayDate):("year"===e.mode||"y"===e.mode)&&this.printYear(e.displayDate)},this.getFrame=function(){var t=[];if(t.push('
'),e.control){var i={},n={};n.height=i.height=a.cssNumber(e.dimensions.controlHeight),n["line-height"]=i["line-height"]=a.cssNumber(e.dimensions.controlHeight),n.width=a.cssNumber(e.dimensions.controlHeight),t.push('
'),t.push(''+e.control.left+""),t.push('
'),t.push(''+e.control.right+""),t.push("
")}return t.push('
'),t.push("
"),t.join("")},this.setDisplay=function(){if(e.control){var t=a.date(e.displayDate),i="",n="";if("day"==e.mode||"d"==e.mode)i=e.control.yearTmpl?e.control.yearTmpl.replace("%s",t.getFullYear()):t.getFullYear(),n=e.control.monthTmpl?e.control.monthTmpl.replace("%s",e.lang.months[t.getMonth()]):e.lang.months[t.getMonth()],this.$["control-display"].html(function(){return e.control.yearFirst?''+i+''+n+"":''+n+''+i+""}());else if("month"==e.mode||"m"==e.mode)i=e.control.yearTmpl?e.control.yearTmpl.replace("%s",t.getFullYear()):t.getFullYear(),this.$["control-display"].html(''+i+"");else if("year"==e.mode||"y"==e.mode){var s=e.control.yearTmpl?e.control.yearTmpl.replace("%s",t.getFullYear()-10):t.getFullYear()-10,l=e.control.yearTmpl?e.control.yearTmpl.replace("%s",Number(t.getFullYear())+9):Number(t.getFullYear())+9;this.$["control-display"].html(s+" ~ "+l)}this.$["control-display"].find("[data-calendar-display]").on(e.clickEventName,function(e){if(target=a.findParentNode(e.target,function(e){return e.getAttribute("data-calendar-display")?!0:void 0}),target){var t=target.getAttribute("data-calendar-display");this.changeMode(t)}}.bind(this))}return this},this.printDay=function(t){var n,s,l,d=a.date(t),r=[],o=new Date(d.getFullYear(),d.getMonth(),1,12),c=e.displayDate,h=function(){var e=o.getDay();return 0==e&&(e=7),a.date(o,{add:{d:-e}})}(),p=d.getMonth(),u=(d.getDate(),{}),m=this.$.body.width(),y=Math.floor(m*(6/7));for(e.dimensions.height&&(y=a.number(e.dimensions.height)),u.height=Math.floor(y/6)-2*a.number(e.dimensions.itemPadding)+"px",u["line-height"]=u.height,u.padding=a.cssNumber(e.dimensions.itemPadding),r.push(''),r.push(""),r.push(""),l=0;7>l;)r.push('"),l++;for(r.push(""),r.push(""),r.push(""),n=h,s=0;6>s;){for(r.push(""),l=0;7>l;)r.push('"),l++,n=a.date(n,{add:{d:1}});r.push(""),s++}if(r.push(""),r.push("
'),r.push(ax5.info.weekNames[l].label),r.push("
'),r.push(''+e.lang.dayTmpl.replace("%s",n.getDate())+''),r.push("
"),this.$.body.html(r.join("")),this.$.body.find("[data-calendar-item-date]").on(e.clickEventName,function(e){e=e||window.event,i.onclick(e,"date");try{e.preventDefault&&e.preventDefault(),e.stopPropagation&&e.stopPropagation(),e.cancelBubble=!0}catch(e){}return!1}),this.printedDay={start:h,end:n},e.onStateChanged){var g={action:"printDay",printedDay:this.printedDay};e.onStateChanged.call(g,g)}this.setDisplay()},this.printMonth=function(t){var n,s,l,d,r=a.date(t),o=r.getMonth(),c=[],h={},p=this.$.body.width(),u=Math.floor(p*(6/7));for(e.dimensions.height&&(u=a.number(e.dimensions.height)),h.height=Math.floor(u/4)-2*a.number(e.dimensions.itemPadding)+"px",h["line-height"]=h.height,h.padding=a.cssNumber(e.dimensions.itemPadding),c.push(''),c.push(""),c.push(""),c.push('"),c.push(""),c.push(""),c.push(""),d=0,l=0,n=0;4>n;){for(c.push(""),s=0;3>s;)c.push('"),l++,s++;c.push(""),n++}if(c.push(""),c.push("
'+e.lang.monthHeading+"
'),c.push(''+e.lang.months[l]+""),c.push("
"),this.$.body.html(c.join("")),this.$.body.find("[data-calendar-item-month]").on(e.clickEventName,function(e){e=e||window.event,i.onclick(e,"month");try{e.preventDefault&&e.preventDefault(),e.stopPropagation&&e.stopPropagation(),e.cancelBubble=!0}catch(e){}return!1}),this.printedDay={start:r.getFullYear()+"-"+a.setDigit(d+1,2),end:r.getFullYear()+"-"+a.setDigit(l,2)},e.onStateChanged){var m={action:"printMonth",printedDay:this.printedDay};e.onStateChanged.call(m,m)}return this.setDisplay(),this},this.printYear=function(t){var n,s,l,d,r=a.date(t),o=r.getFullYear(),c=[],h={},p=this.$.body.width(),u=Math.floor(p*(6/7));for(e.dimensions.height&&(u=a.number(e.dimensions.height)),h.height=Math.floor(u/5)-2*a.number(e.dimensions.itemPadding)+"px",h["line-height"]=h.height,h.padding=a.cssNumber(e.dimensions.itemPadding),c.push(''),c.push(""),c.push(""),c.push('"),c.push(""),c.push(""),c.push(""),d=o-10,l=o-10,n=0;5>n;){for(c.push(""),s=0;4>s;)c.push('"),l++,s++;c.push(""),n++}if(c.push(""),c.push("
'+e.lang.yearHeading+"
'),c.push(''+e.lang.yearTmpl.replace("%s",l)+""),c.push("
"),this.$.body.html(c.join("")),this.$.body.find("[data-calendar-item-year]").on(e.clickEventName,function(e){e=e||window.event,i.onclick(e,"year");try{e.preventDefault&&e.preventDefault(),e.stopPropagation&&e.stopPropagation(),e.cancelBubble=!0}catch(e){}return!1}),this.printedDay={start:d,end:l-1},e.onStateChanged){var m={action:"printYear",printedDay:this.printedDay};e.onStateChanged.call(m,m)}return this.setDisplay(),this},this.onclick=function(t,s,l,d){if(s=s||"date",l=a.findParentNode(t.target,function(e){return e.getAttribute("data-calendar-item-"+s)?!0:void 0})){d=l.getAttribute("data-calendar-item-"+s);var r=a.date(d,{"return":e.dateFormat}),o=!0;if(n=e.multipleSelect?a.isNumber(e.multipleSelect)?e.multipleSelect:2:1,e.selectable&&(i.selectableMap[r]||(o=!1)),"date"==s){if(o){if(i.selection.length>=n){var c=i.selection.splice(0,i.selection.length-(n-1));c.forEach(function(t){i.$.body.find('[data-calendar-item-date="'+a.date(t,{"return":e.dateFormat})+'"]').removeClass("selected-day")})}jQuery(l).addClass("selected-day"),i.selection.push(d),e.onClick&&e.onClick.call({date:d,target:this.target,dateElement:l})}}else if("month"==s)if("month"==e.selectMode){if(o){if(i.selection.length>=n){var c=i.selection.splice(0,i.selection.length-(n-1));c.forEach(function(e){i.$.body.find('[data-calendar-item-month="'+a.date(e,{"return":"yyyy-mm-dd"})+'"]').removeClass("selected-month")})}jQuery(l).addClass("selected-month"),i.selection.push(d),e.onClick&&e.onClick.call({date:d,target:this.target,dateElement:l})}}else i.changeMode("day",d);else if("year"==s)if("year"==e.selectMode){if(o){if(i.selection.length>=n){var c=i.selection.splice(0,i.selection.length-(n-1));c.forEach(function(e){i.$.body.find('[data-calendar-item-year="'+a.date(e,{"return":"yyyy-mm-dd"})+'"]').removeClass("selected-year")})}jQuery(l).addClass("selected-year"),i.selection.push(d),e.onClick&&e.onClick.call({date:d,target:this.target,dateElement:l})}}else i.changeMode("month",d)}},this.move=function(t,i,n){return i=a.findParentNode(t.target,function(e){return e.getAttribute("data-calendar-move")?!0:void 0}),i&&(n=i.getAttribute("data-calendar-move"),"day"==e.mode?("left"==n?e.displayDate=a.date(e.displayDate,{add:{m:-1}}):e.displayDate=a.date(e.displayDate,{add:{m:1}}),this.printDay(e.displayDate)):"month"==e.mode?("left"==n?e.displayDate=a.date(e.displayDate,{add:{y:-1}}):e.displayDate=a.date(e.displayDate,{add:{y:1}}),this.printMonth(e.displayDate)):"year"==e.mode&&("left"==n?e.displayDate=a.date(e.displayDate,{add:{y:-10}}):e.displayDate=a.date(e.displayDate,{add:{y:10}}),this.printYear(e.displayDate))),this},this.changeMode=function(t,a){return"undefined"!=typeof a&&(e.displayDate=a),t&&(e.mode=t),this.$.body.removeClass("fadein").addClass("fadeout"),setTimeout(function(){"day"==e.mode||"d"==e.mode?this.printDay(e.displayDate):"month"==e.mode||"m"==e.mode?this.printMonth(e.displayDate):("year"==e.mode||"y"==e.mode)&&this.printYear(e.displayDate),this.$.body.removeClass("fadeout").addClass("fadein")}.bind(this),e.animateTime),this},this.setDisplayDate=function(t){return e.displayDate=a.date(t),this.$.body.removeClass("fadein").addClass("fadeout"),setTimeout(function(){"day"==e.mode||"d"==e.mode?this.printDay(e.displayDate):"month"==e.mode||"m"==e.mode?this.printMonth(e.displayDate):("year"==e.mode||"y"==e.mode)&&this.printYear(e.displayDate),this.$.body.removeClass("fadeout").addClass("fadein")}.bind(this),e.animateTime),this},this.setSelection=function(t){return a.isArray(t)?(n=e.multipleSelect?a.isNumber(e.multipleSelect)?e.multipleSelect:2:1,this.selection=t.splice(0,n),this.selection.forEach(function(t){i.$.body.find('[data-calendar-item-date="'+a.date(t,{"return":e.dateFormat})+'"]').addClass("selected-day")}),this):this},this.getSelection=function(){return this.selection},this.setSelectable=function(t,n){if(e.selectable=t){var s=e.selectable;if(this.selectableMap={},a.isArray(s))s.forEach(function(t){a.isDate(t)&&(t=a.date(t,{"return":e.dateFormat})),i.selectableMap[t]=!0});else if(a.isObject(s))if(s.from&&s.to)if(a.isDateFormat(s.from)&&a.isDateFormat(s.to))for(var l=a.date(s.from);l<=a.date(s.to);l.setDate(l.getDate()+1))i.selectableMap[a.date(l,{"return":e.dateFormat})]=!0;else for(var d=s.from;d<=s.to;d++)i.selectableMap[d]=!0;else if(!s.from&&!s.to)for(var r in s)i.selectableMap[r]=s[r]}n!==!1&&this.changeMode()},this.main=function(){arguments&&a.isObject(arguments[0])&&this.setConfig(arguments[0])}.apply(this,arguments)};a.isFunction(t)&&(i.prototype=new t),e.calendar=i}(ax5.ui,ax5.ui.root); \ No newline at end of file +!function(t,e){var a=ax5.util,n=function(){e&&e.call(this);var t,n=this,i=1;this.target=null,this.selection=[],this.selectableMap={},this.printedDay={start:"",end:""},this.config={clickEventName:"click",theme:"default",mode:"day",dateFormat:"yyyy-mm-dd",displayDate:new Date,animateTime:250,dimensions:{controlHeight:"40px",controlButtonWidth:"40px",itemPadding:2},lang:{yearHeading:"Choose the year",monthHeading:"Choose the month",yearTmpl:"%s",months:["January","February","March","April","May","June","July","August","September","October","November","December"],dayTmpl:"%s"},multipleSelect:!1,selectMode:"day"},t=this.config,this.init=function(){t.target||console.log(ax5.info.getError("ax5calendar","401","setConfig")),this.target=jQuery(t.target),t.displayDate=a.date(t.displayDate),this.target.html(this.getFrame()),this.$={root:this.target.find('[data-calendar-els="root"]'),control:this.target.find('[data-calendar-els="control"]'),"control-display":this.target.find('[data-calendar-els="control-display"]'),body:this.target.find('[data-calendar-els="body"]')},t.control&&this.$.control.find("[data-calendar-move]").on(t.clickEventName,function(t){this.move(t||window.event)}.bind(this)),t.selectable&&this.setSelectable(t.selectable,!1),"day"===t.mode||"d"===t.mode?this.printDay(t.displayDate):"month"===t.mode||"m"===t.mode?this.printMonth(t.displayDate):("year"===t.mode||"y"===t.mode)&&this.printYear(t.displayDate)},this.getFrame=function(){var e=[];if(e.push('
'),t.control){var n={},i={};i.height=n.height=a.cssNumber(t.dimensions.controlHeight),i["line-height"]=n["line-height"]=a.cssNumber(t.dimensions.controlHeight),i.width=a.cssNumber(t.dimensions.controlHeight),e.push('
'),e.push(''+t.control.left+""),e.push('
'),e.push(''+t.control.right+""),e.push("
")}return e.push('
'),e.push("
"),e.join("")},this.setDisplay=function(){if(t.control){var e=a.date(t.displayDate),n="",i="";if("day"==t.mode||"d"==t.mode)n=t.control.yearTmpl?t.control.yearTmpl.replace("%s",e.getFullYear()):e.getFullYear(),i=t.control.monthTmpl?t.control.monthTmpl.replace("%s",t.lang.months[e.getMonth()]):t.lang.months[e.getMonth()],this.$["control-display"].html(function(){return t.control.yearFirst?''+n+''+i+"":''+i+''+n+""}());else if("month"==t.mode||"m"==t.mode)n=t.control.yearTmpl?t.control.yearTmpl.replace("%s",e.getFullYear()):e.getFullYear(),this.$["control-display"].html(''+n+"");else if("year"==t.mode||"y"==t.mode){var s=t.control.yearTmpl?t.control.yearTmpl.replace("%s",e.getFullYear()-10):e.getFullYear()-10,d=t.control.yearTmpl?t.control.yearTmpl.replace("%s",Number(e.getFullYear())+9):Number(e.getFullYear())+9;this.$["control-display"].html(s+" ~ "+d)}this.$["control-display"].find("[data-calendar-display]").on(t.clickEventName,function(t){if(target=a.findParentNode(t.target,function(t){return t.getAttribute("data-calendar-display")?!0:void 0}),target){var e=target.getAttribute("data-calendar-display");this.changeMode(e)}}.bind(this))}return this},this.printDay=function(e){var i,s,d,l=a.date(e),r=[],o=new Date(l.getFullYear(),l.getMonth(),1,12),c=t.displayDate,h=function(){var t=o.getDay();return 0==t&&(t=7),a.date(o,{add:{d:-t}})}(),p=l.getMonth(),u=(l.getDate(),{}),m=this.$.body.width(),y=Math.floor(m*(6/7));for(t.dimensions.height&&(y=a.number(t.dimensions.height)),u.height=Math.floor(y/6)-2*a.number(t.dimensions.itemPadding)+"px",u["line-height"]=u.height,u.padding=a.cssNumber(t.dimensions.itemPadding),r.push(''),r.push(""),r.push(""),d=0;7>d;)r.push('"),d++;for(r.push(""),r.push(""),r.push(""),i=h,s=0;6>s;){for(r.push(""),d=0;7>d;)r.push('"),d++,i=a.date(i,{add:{d:1}});r.push(""),s++}if(r.push(""),r.push("
'),r.push(ax5.info.weekNames[d].label),r.push("
'),r.push(''+t.lang.dayTmpl.replace("%s",i.getDate())+''),r.push("
"),this.$.body.html(r.join("")),this.$.body.find("[data-calendar-item-date]").on(t.clickEventName,function(t){t=t||window.event,n.onclick(t,"date");try{t.preventDefault&&t.preventDefault(),t.stopPropagation&&t.stopPropagation(),t.cancelBubble=!0}catch(t){}return!1}),this.printedDay={start:h,end:i},t.onStateChanged){var g={action:"printDay",printedDay:this.printedDay};t.onStateChanged.call(g,g)}this.setDisplay()},this.printMonth=function(e){var i,s,d,l,r=a.date(e),o=r.getMonth(),c=[],h={},p=this.$.body.width(),u=Math.floor(p*(6/7));for(t.dimensions.height&&(u=a.number(t.dimensions.height)),h.height=Math.floor(u/4)-2*a.number(t.dimensions.itemPadding)+"px",h["line-height"]=h.height,h.padding=a.cssNumber(t.dimensions.itemPadding),c.push(''),c.push(""),c.push(""),c.push('"),c.push(""),c.push(""),c.push(""),l=0,d=0,i=0;4>i;){for(c.push(""),s=0;3>s;)c.push('"),d++,s++;c.push(""),i++}if(c.push(""),c.push("
'+t.lang.monthHeading+"
'),c.push(''+t.lang.months[d]+""),c.push("
"),this.$.body.html(c.join("")),this.$.body.find("[data-calendar-item-month]").on(t.clickEventName,function(t){t=t||window.event,n.onclick(t,"month");try{t.preventDefault&&t.preventDefault(),t.stopPropagation&&t.stopPropagation(),t.cancelBubble=!0}catch(t){}return!1}),this.printedDay={start:r.getFullYear()+"-"+a.setDigit(l+1,2),end:r.getFullYear()+"-"+a.setDigit(d,2)},t.onStateChanged){var m={action:"printMonth",printedDay:this.printedDay};t.onStateChanged.call(m,m)}return this.setDisplay(),this},this.printYear=function(e){var i,s,d,l,r=a.date(e),o=r.getFullYear(),c=[],h={},p=this.$.body.width(),u=Math.floor(p*(6/7));for(t.dimensions.height&&(u=a.number(t.dimensions.height)),h.height=Math.floor(u/5)-2*a.number(t.dimensions.itemPadding)+"px",h["line-height"]=h.height,h.padding=a.cssNumber(t.dimensions.itemPadding),c.push(''),c.push(""),c.push(""),c.push('"),c.push(""),c.push(""),c.push(""),l=o-10,d=o-10,i=0;5>i;){for(c.push(""),s=0;4>s;)c.push('"),d++,s++;c.push(""),i++}if(c.push(""),c.push("
'+t.lang.yearHeading+"
'),c.push(''+t.lang.yearTmpl.replace("%s",d)+""),c.push("
"),this.$.body.html(c.join("")),this.$.body.find("[data-calendar-item-year]").on(t.clickEventName,function(t){t=t||window.event,n.onclick(t,"year");try{t.preventDefault&&t.preventDefault(),t.stopPropagation&&t.stopPropagation(),t.cancelBubble=!0}catch(t){}return!1}),this.printedDay={start:l,end:d-1},t.onStateChanged){var m={action:"printYear",printedDay:this.printedDay};t.onStateChanged.call(m,m)}return this.setDisplay(),this},this.onclick=function(e,s,d,l){if(s=s||"date",d=a.findParentNode(e.target,function(t){return t.getAttribute("data-calendar-item-"+s)?!0:void 0})){l=d.getAttribute("data-calendar-item-"+s);var r=a.date(l,{"return":t.dateFormat}),o=!0;if(i=t.multipleSelect?a.isNumber(t.multipleSelect)?t.multipleSelect:2:1,t.selectable&&(n.selectableMap[r]||(o=!1)),"date"==s){if(o){if(n.selection.length>=i){var c=n.selection.splice(0,n.selection.length-(i-1));c.forEach(function(e){n.$.body.find('[data-calendar-item-date="'+a.date(e,{"return":t.dateFormat})+'"]').removeClass("selected-day")})}jQuery(d).addClass("selected-day"),n.selection.push(l),t.onClick&&t.onClick.call({date:l,target:this.target,dateElement:d})}}else if("month"==s)if("month"==t.selectMode){if(o){if(n.selection.length>=i){var c=n.selection.splice(0,n.selection.length-(i-1));c.forEach(function(t){n.$.body.find('[data-calendar-item-month="'+a.date(t,{"return":"yyyy-mm-dd"})+'"]').removeClass("selected-month")})}jQuery(d).addClass("selected-month"),n.selection.push(l),t.onClick&&t.onClick.call({date:l,target:this.target,dateElement:d})}}else n.changeMode("day",l);else if("year"==s)if("year"==t.selectMode){if(o){if(n.selection.length>=i){var c=n.selection.splice(0,n.selection.length-(i-1));c.forEach(function(t){n.$.body.find('[data-calendar-item-year="'+a.date(t,{"return":"yyyy-mm-dd"})+'"]').removeClass("selected-year")})}jQuery(d).addClass("selected-year"),n.selection.push(l),t.onClick&&t.onClick.call({date:l,target:this.target,dateElement:d})}}else n.changeMode("month",l)}},this.move=function(e,n,i){return n=a.findParentNode(e.target,function(t){return t.getAttribute("data-calendar-move")?!0:void 0}),n&&(i=n.getAttribute("data-calendar-move"),"day"==t.mode?("left"==i?t.displayDate=a.date(t.displayDate,{add:{m:-1}}):t.displayDate=a.date(t.displayDate,{add:{m:1}}),this.printDay(t.displayDate)):"month"==t.mode?("left"==i?t.displayDate=a.date(t.displayDate,{add:{y:-1}}):t.displayDate=a.date(t.displayDate,{add:{y:1}}),this.printMonth(t.displayDate)):"year"==t.mode&&("left"==i?t.displayDate=a.date(t.displayDate,{add:{y:-10}}):t.displayDate=a.date(t.displayDate,{add:{y:10}}),this.printYear(t.displayDate))),this},this.changeMode=function(e,a){return"undefined"!=typeof a&&(t.displayDate=a),e&&(t.mode=e),this.$.body.removeClass("fadein").addClass("fadeout"),setTimeout(function(){"day"==t.mode||"d"==t.mode?this.printDay(t.displayDate):"month"==t.mode||"m"==t.mode?this.printMonth(t.displayDate):("year"==t.mode||"y"==t.mode)&&this.printYear(t.displayDate),this.$.body.removeClass("fadeout").addClass("fadein")}.bind(this),t.animateTime),this},this.setDisplayDate=function(e){return t.displayDate=a.date(e),this.$.body.removeClass("fadein").addClass("fadeout"),setTimeout(function(){"day"==t.mode||"d"==t.mode?this.printDay(t.displayDate):"month"==t.mode||"m"==t.mode?this.printMonth(t.displayDate):("year"==t.mode||"y"==t.mode)&&this.printYear(t.displayDate),this.$.body.removeClass("fadeout").addClass("fadein")}.bind(this),t.animateTime),this},this.setSelection=function(e){return a.isArray(e)?(i=t.multipleSelect?a.isNumber(t.multipleSelect)?t.multipleSelect:2:1,this.selection=e.splice(0,i),this.selection.forEach(function(e){n.$.body.find('[data-calendar-item-date="'+a.date(e,{"return":t.dateFormat})+'"]').addClass("selected-day")}),this):this},this.getSelection=function(){return this.selection},this.setSelectable=function(){this.selectableMap={};var e={arr:function(e,n){return n={},a.isArray(e)?(e.forEach(function(e){a.isDate(e)&&(e=a.date(e,{"return":t.dateFormat})),n[e]=!0}),n):n},obj:function(t,e){if(e={},a.isArray(t))return e;if(t.range)return e;for(var n in t)e[n]=t[n];return e},range:function(e,n){return n={},a.isArray(e)?n:e.range?(e.range.forEach(function(e){if(a.isDateFormat(e.from)&&a.isDateFormat(e.to))for(var i=a.date(e.from);i<=a.date(e.to);i.setDate(i.getDate()+1))n[a.date(i,{"return":t.dateFormat})]=!0;else for(var s=e.from;s<=e.to;s++)n[s]=!0}),n):n}};return function(n,i){var s,d={};if(t.selectable=n)if(a.isArray(n))d=e.arr(n);else{for(s in e)if(n[s]){d=e[s](n);break}0===Object.keys(d).length&&(d=e.obj(n))}this.selectableMap=d,i!==!1&&this.changeMode()}}(),this.main=function(){arguments&&a.isObject(arguments[0])&&this.setConfig(arguments[0])}.apply(this,arguments)};a.isFunction(e)&&(n.prototype=new e),t.calendar=n}(ax5.ui,ax5.ui.root); \ No newline at end of file diff --git a/package.json b/package.json index e5e9656..fa7f670 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "bootstrap-ax5calendar", - "version": "0.5.1", + "version": "0.6.0", "description": "A calendar plugin that works with Bootstrap & jQuery", "license": "MIT", "repository": { diff --git a/src/ax5calendar.js b/src/ax5calendar.js index d05846f..5800488 100755 --- a/src/ax5calendar.js +++ b/src/ax5calendar.js @@ -240,6 +240,7 @@ po.push(''); po.push(''); po.push(''); + loopDate = tableStartDate; i = 0; while (i < 6) @@ -736,44 +737,79 @@ return this.selection; }; - this.setSelectable = function (selectable, isPrint) { - // collect selectableMap - if (cfg.selectable = selectable) { - var - _seltb = cfg.selectable; - this.selectableMap = {}; // clear selectableMap - - if (U.isArray(_seltb)) { - _seltb.forEach(function (n) { + this.setSelectable = (function () { + this.selectableMap = {}; + var processor = { + 'arr': function (v, map) { + map = {}; + if(!U.isArray(v)) return map; + v.forEach(function (n) { if (U.isDate(n)) n = U.date(n, {'return': cfg.dateFormat}); - self.selectableMap[n] = true; + map[n] = true; }); - } - else if (U.isObject(_seltb)) { - if (_seltb["from"] && _seltb["to"]) { - if (U.isDateFormat(_seltb["from"]) && U.isDateFormat(_seltb["to"])) { - for (var d = U.date(_seltb["from"]); d <= U.date(_seltb["to"]); d.setDate(d.getDate() + 1)) { - self.selectableMap[U.date(d, {"return": cfg.dateFormat})] = true; + return map; + }, + 'obj': function (v, map) { + map = {}; + if(U.isArray(v)) return map; + if(v.range) return map; + for (var k in v) { + map[k] = v[k]; + } + return map; + }, + 'range': function (v, map) { + map = {}; + if(U.isArray(v)) return map; + if(!v.range) return map; + + v.range.forEach(function (n) { + if (U.isDateFormat(n.from) && U.isDateFormat(n.to)) { + for (var d = U.date(n.from); d <= U.date(n.to); d.setDate(d.getDate() + 1)) { + map[U.date(d, {"return": cfg.dateFormat})] = true; } } else { - for (var i = _seltb["from"]; i <= _seltb["to"]; i++) { - self.selectableMap[i] = true; + for (var i = n.from; i <= n.to; i++) { + map[i] = true; } } + }); + + return map; + } + }; + + return function (selectable, isPrint) { + + var + key, + result = {} + ; + + if (cfg.selectable = selectable) { + if (U.isArray(selectable)) { + result = processor.arr(selectable); } - else if(!_seltb["from"] && !_seltb["to"]) { - for (var k in _seltb) { - self.selectableMap[k] = _seltb[k]; + else { + for (key in processor) { + if (selectable[key]) { + result = processor[key](selectable); + break; + } + } + if(Object.keys(result).length === 0){ + result = processor.obj(selectable); } } } - } - // 변경내용 적용하여 출력 - if(isPrint !== false) this.changeMode(); - }; + this.selectableMap = result; + // 변경내용 적용하여 출력 + if (isPrint !== false) this.changeMode(); + }; + })(); // 클래스 생성자 this.main = (function () {